249 lines
9.4 KiB
PHP
Executable File
249 lines
9.4 KiB
PHP
Executable File
<?php
|
|
/** Klasa do wystawiania dokumentów Ks z Faktur Korygujących
|
|
*
|
|
*
|
|
*/
|
|
echo "<pre>";
|
|
Class correctToKs{
|
|
|
|
/** Zmienne Globalne;
|
|
*
|
|
*/
|
|
public $db;
|
|
public $total_value;
|
|
public $return_list;
|
|
public $document_list;
|
|
public $invoice_id;
|
|
public $wz_id;
|
|
public $wz_id_item;
|
|
public $ask;
|
|
public $return;
|
|
public $invoiceType;
|
|
public $operation_type;
|
|
public $correct_id;
|
|
public $stock_id;
|
|
public $stock_name;
|
|
public $assigned_user_id;
|
|
public $created_by;
|
|
public $document_no;
|
|
public $sell_date;
|
|
public $ks_id;
|
|
private $tmp_quantity;
|
|
private $tmp_to_use_operations;
|
|
private $tmp_total_quantity;
|
|
private $return_operations;
|
|
private $can_use;
|
|
|
|
/** Konstruktor klasy, ładuje polecenia sql do zmiennej $db, ładuje dane z korekty i faktury głownej;
|
|
* parametry:
|
|
* @ $correct_id : id korekty (jesli brak drugiego parametru twórzy dokumenty KS typu zwrot lub zwrot i likwidacje)
|
|
* @ $array : czy zwrócić tablice dla dokumetu KS (opcjonalne)
|
|
*/
|
|
public function __construct($correct_id,$array=false) {
|
|
$this->db=$GLOBALS['db'];
|
|
$this->getPrimaryInvoice($correct_id);
|
|
if($this->invoiceType!='correct')return false;
|
|
if($this->invoice_id!='')$this->getWzId();
|
|
$this->createReturnList();
|
|
if($array==true) return $this->return_list;
|
|
$this->createKsDocuments();
|
|
}
|
|
|
|
public function savePlusKs(){
|
|
|
|
if(count($this->return_list)>0){
|
|
|
|
//make ks
|
|
$ks=new EcmStockDocCorrect();
|
|
$ks->stock_id=$this->stock_id;
|
|
$ks->stock_name=$this->stock_name;
|
|
$ks->register_date=date('d.m.Y');
|
|
$ks->type='2';
|
|
$ks->assigned_user_id=$this->assigned_user_id;
|
|
$ks->created_by=$this->created_by;
|
|
$ks->position_list=$this->return_list;
|
|
$ks->pdf_text="Do Faktury korekty nr: ".$this->document_no.' z dnia '.$this->sell_date;
|
|
$ks->total=$this->total_value;
|
|
$this->total_value=$this->total_value*-1;
|
|
$this->ks_id=$ks->save();
|
|
$this->db->query("update ecminvoiceouts set purchase_price='".$this->total_value."', ks_id='".$this->ks_id."' where id='".$this->correct_id."'");
|
|
|
|
}
|
|
}
|
|
|
|
public function saveMinusKs(){
|
|
$this->total_value=0;
|
|
$ks=new EcmStockDocCorrect();
|
|
$ks->retrieve($this->ks_id);
|
|
|
|
$this->return_list=$ks->getPositionList(true);
|
|
|
|
foreach ($this->return_list as $key=>$val){
|
|
|
|
$this->ask=$this->db->query("select id,parent_name from ecmstockoperations where documentitem_id='".$this->return_list[$key]['record_id']."'");
|
|
$this->return=$this->db->fetchByAssoc($this->ask);
|
|
var_dump($this->return);
|
|
$this->return_list[$key]['quantity']=floatval ( abs( $this->return_list[$key]['quantity'])*-1);
|
|
$this->return_list[$key]['total']=floatval($this->return_list[$key]['quantity']*$this->return_list[$key]['price']);
|
|
$this->return_list[$key]['quantity_consignment_id']=$this->return['id'];
|
|
$this->return_list[$key]['quantity_consignment_doc']=$this->return['parent_name'];
|
|
$this->total_value=$this->total_value+$this->return_list[$key]['total'];
|
|
}
|
|
unset($ks);
|
|
|
|
$ks=new EcmStockDocCorrect();
|
|
$ks->stock_id=$this->stock_id;
|
|
$ks->stock_name=$this->stock_name;
|
|
$ks->register_date=date('d.m.Y');
|
|
$ks->type='1';
|
|
$ks->assigned_user_id=$this->assigned_user_id;
|
|
$this->created_by=$this->created_by;
|
|
$ks->position_list=$this->return_list;
|
|
$ks->total=$this->total_value;
|
|
$ks->save();
|
|
}
|
|
|
|
public function createKsDocuments(){
|
|
|
|
switch ($this->operation_type) {
|
|
case '0':
|
|
$this->savePlusKs();
|
|
break;
|
|
case '1':
|
|
$this->savePlusKs();
|
|
$this->saveMinusKs();
|
|
break;
|
|
}
|
|
}
|
|
|
|
public function getPrimaryInvoice($correct_id){
|
|
$this->correct_id=$correct_id;
|
|
$tmp=new EcmInvoiceOut();
|
|
$tmp->retrieve($correct_id);
|
|
$this->invoice_id=$tmp->ecminvoiceout_id;
|
|
$this->invoiceType=$tmp->type;
|
|
$this->document_no=$tmp->document_no;
|
|
$this->sell_date=$tmp->sell_date;
|
|
$this->operation_type=$tmp->operation_type;
|
|
$this->stock_id=$tmp->stock_id;
|
|
$this->stock_name=$tmp->stock_name;
|
|
$this->assigned_user_id=$tmp->assigned_user_id;
|
|
$this->created_by=$tmp->created_by;
|
|
$this->document_list=$tmp->getPositionList(true);
|
|
unset($tmp);
|
|
}
|
|
|
|
public function getWzId(){
|
|
$tmp=new EcmInvoiceOut();
|
|
$tmp->retrieve($this->invoice_id);
|
|
$this->wz_id=$tmp->wz_id;
|
|
unset($tmp);
|
|
}
|
|
|
|
public function getIdToFindWzOperations($record){
|
|
$this->wz_id_item='';
|
|
if($this->wz_id==''){
|
|
|
|
$this->ask=$this->db->query("select i.wz_id from ecminvoiceouts i inner join ecminvoiceoutitems ii on i.id=ii.ecminvoiceout_id where ii.id='".$record['ecminvoiceoutitem_id']."'") ;
|
|
$this->return=$this->db->fetchByAssoc($this->ask);
|
|
$this->wz_id_item=$this->return['wz_id'];
|
|
} else {
|
|
$this->wz_id_item=$this->wz_id;
|
|
}
|
|
$this->wz_id_item=$this->invoice_id;
|
|
}
|
|
|
|
public function hasOneOperation($record){
|
|
$quantity = $record['quantity'] -
|
|
$record['old_quantity'];
|
|
// nadaje cene i ilość
|
|
$this->return_operations = $this->db->fetchByAssoc($this->ask);
|
|
$record['price'] = $this->return_operations['price'];
|
|
$record['quantity'] =floatval ( abs($this->tmp_quantity));
|
|
$this->return_operations['quantity_restored'] = $this->return_operations['quantity_restored'] +
|
|
$record['quantity'];
|
|
// wpisywanie do operacji ile z tej parti spowrotem
|
|
// wrócilismy na magazyn
|
|
$this->db->query(
|
|
'update ecmstockoperations set quantity_restored="' .
|
|
$this->return_operations['quantity_restored'] .
|
|
'" where id="' . $this->return_operations['id'] . '"');
|
|
|
|
$this->total_value= $this->total_value+($record['quantity']*$record['price']);
|
|
$this->return_list[] = $record;
|
|
}
|
|
|
|
public function hasManyOperations($record){
|
|
// razem ilość z pozycji do dopasowania
|
|
$this->tmp_total_quantity=abs($this->tmp_quantity);
|
|
|
|
$this->tmp_to_use_operations = array();
|
|
// tablica dopasowan
|
|
while ($this->return_operations = $this->db->fetchByAssoc($this->ask)) {
|
|
$this->tmp_to_use_operations[] =$this->return_operations;
|
|
}
|
|
// rozkladanie ilosci na partie
|
|
foreach ($this->tmp_to_use_operations as $klucz=>$war){
|
|
if($this->tmp_total_quantity>0){
|
|
|
|
if($this->tmp_total_quantity-$this->tmp_to_use_operations[$klucz]['quantity']>=0){
|
|
$this->can_use=floatval ( $this->tmp_to_use_operations[$klucz]['quantity']);
|
|
} else {
|
|
$this->can_use=floatval ( $this->tmp_total_quantity);
|
|
}
|
|
$record['quantity']=$this->can_use;
|
|
$record['price']=$this->tmp_to_use_operations[$klucz]['price'];
|
|
$this->tmp_to_use_operations[$klucz]['quantity_restored']=$record['quantity']+$this->tmp_to_use_operations[$klucz]['quantity_restored'];
|
|
$this->db->query(
|
|
'update ecmstockoperations set quantity_restored="' .
|
|
$this->tmp_to_use_operations[$klucz]['quantity_restored'] .
|
|
'" where id="' . $this->tmp_to_use_operations[$klucz]['id'] . '"');
|
|
|
|
$this->return_list[] = $record;
|
|
$this->total_value=$this->total_value+($record['quantity']*$record['price']);
|
|
$this->tmp_total_quantity=$this->tmp_total_quantity-$this->tmp_to_use_operations[$klucz]['quantity'];
|
|
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
public function findWzOperations($record){
|
|
$this->ask = $this->db->query(
|
|
"select id,price,(quantity-IFNULL(quantity_restored,0)) as quantity,quantity_restored from ecmstockoperations where parent_id='" .
|
|
$this->wz_id_item.
|
|
"' and quantity!=IFNULL(quantity_restored,0) and product_id='" .
|
|
$record['product_id'] .
|
|
"' order by price asc");
|
|
// wiele partii
|
|
|
|
if($this->ask->num_rows==0)return false;
|
|
if ($this->ask->num_rows > 1) {
|
|
$this->hasManyOperations($record);
|
|
} else {
|
|
$this->hasOneOperation($record);
|
|
|
|
}
|
|
}
|
|
|
|
public function createReturnList(){
|
|
$this->return_list=array();
|
|
foreach ($this->document_list as $pos=>$k) {
|
|
|
|
$this->tmp_quantity = $this->document_list[$pos]['quantity'] - $this->document_list[$pos]['old_quantity'];
|
|
// jak to faktura korygująca ilość na minus to kontunuuj
|
|
if ($this->tmp_quantity < 0) {
|
|
|
|
$this->getIdToFindWzOperations( $this->document_list[$pos]);
|
|
$this->findWzOperations($this->document_list[$pos]);
|
|
} else {
|
|
continue;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
?>
|