'1' ,'EcmStockDocIns'=>'0','EcmStockDocInsideIns'=>'0','EcmStockDocInsideOuts'=>'1','EcmStockDocMoves'=>'2','EcmStockDocCorrects'=>'3','EcmInvoiceOuts'=>'1'); function __construct ($module, $id,$type) { $this->module=$module; $this->module_class=substr($module, 0, -1); $this->id=$id; if($type!=''){ $this->type=$type; } $this->db=$GLOBALS['db']; $this->error=true; $this->codes=''; $this->last=false; } function isLast(){ if($this->types[$this->module]==2){ $module = new $this->module_class(); $module->retrieve($this->id); $zap=$this->db->query("select id from ".strtolower($this->module_class.'s')." where stock_in_id='".$module->stock_in_id."' and deleted=0 order by date_entered desc limit 1"); $dane=$this->db->fetchByAssoc($zap); if($dane['id']==$this->id){ // $this->error=true; } else { $this->error=false; } } if($this->module=='EcmInvoiceOuts'){ $module = new $this->module_class(); $module->retrieve($this->id); $zap=$this->db->query("select id from ".strtolower($this->module_class.'s')." where stock_id='".$module->stock_id."' and type='".$module->type."' and deleted=0 order by document_autoincrement desc limit 1"); $dane=$this->db->fetchByAssoc($zap); if($dane['id']==$this->id){ //$this->error=true; } else { $this->error=false; } }else { $module = new $this->module_class(); $module->retrieve($this->id); $zap=$this->db->query("select id from ".strtolower($this->module_class.'s')." where stock_id='".$module->stock_id."' and deleted=0 order by date_entered desc limit 1"); $dane=$this->db->fetchByAssoc($zap); if($dane['id']==$this->id){ //$this->error=true; } else { $this->error=false; } } } function removeIsPossible() { $module = new $this->module_class(); $module->retrieve($this->id); if($module->deleted==1){ $this->error=false; } $this->getItemList(); $this->checkStates(); $this->isLast(); return $this->error; } function cancelIsPossible() { $module = new $this->module_class(); $module->retrieve($this->id); if($module->canceled==1){ $this->error=false; } $this->getItemList(); $this->checkStates(); return $this->error; } function refreshState() { } function rebuildKsDocs() { foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used,in_id from ecmstockoperations where documentitem_id='".$val['id']."'"; $row=$this->db->query($query); while($res=$this->db->fetchByAssoc($row)){ if($val['quantity']<0){ $query="select id,used from ecmstockoperations where id='".$res['in_id']."'"; $row2=$this->db->query($query); $res2=$this->db->fetchByAssoc($row2); if($res2['used']==1){ $this->db->query('update ecmstockoperations set used="0" where id="'.$res2['id'].'"'); } } } } } function rebuildMmDocs() { foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used,in_id from ecmstockoperations where documentitem_id='".$val['id']."' and type='1'"; $row=$this->db->query($query); while($res=$this->db->fetchByAssoc($row)){ $query="select id,used from ecmstockoperations where id='".$res['in_id']."'"; $row2=$this->db->query($query); $res2=$this->db->fetchByAssoc($row2); if($res2['used']==1){ $this->db->query('update ecmstockoperations set used="0" where id="'.$res2['id'].'"'); } } } } function rebuildInsDocs() { foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used,in_id from ecmstockoperations where documentitem_id='".$val['id']."'"; $row=$this->db->query($query); while($res=$this->db->fetchByAssoc($row)){ $query="select id,used from ecmstockoperations where id='".$res['in_id']."'"; $row2=$this->db->query($query); $res2=$this->db->fetchByAssoc($row2); if($res2['used']==1){ $this->db->query('update ecmstockoperations set used="0" where id="'.$res2['id'].'"'); } } } } function cancelDoc() { if($this->error!=false){ if($this->types[$this->module]==1){ $this->rebuildInsDocs(); } if($this->types[$this->module]==2){ $this->rebuildMmDocs(); } if($this->types[$this->module]==3){ $this->rebuildKsDocs(); } $this->db->query("delete from ecmstockoperations where parent_id='".$this->id."'"); $this->db->query("update ".strtolower($this->module)." set canceled='1', pdf_text='Dokument został anulowany' where id='".$this->id."'"); $this->db->query("update ecmtransactions set deleted=1 where record_id='".$this->id."'"); $ss=new EcmStockState(); foreach ($this->item_list as $key=>$val){ $w=$this->db->query("select id from ecmstocks where deleted='0'"); while($r=$this->db->fetchByAssoc($w)){ $ss->UpdateStockState($r['id'],$val['ecmproduct_id']); } } } } function removeDoc() { if($this->error!=false){ if($this->types[$this->module]==1){ $this->rebuildInsDocs(); } if($this->types[$this->module]==2){ $this->rebuildMmDocs(); } if($this->types[$this->module]==3){ $this->rebuildKsDocs(); } $this->db->query("delete from ecmstockoperations where parent_id='".$this->id."'"); $this->db->query("update ecmtransactions set deleted=1 where record_id='".$this->id."'"); $this->db->query("update ".strtolower($this->module)." set deleted='1', pdf_text='Dokument został anulowany' where id='".$this->id."'"); $ss=new EcmStockState(); foreach ($this->item_list as $key=>$val){ $w=$this->db->query("select id from ecmstocks where deleted='0'"); while($r=$this->db->fetchByAssoc($w)){ $ss->UpdateStockState($r['id'],$val['ecmproduct_id']); } } } } function getItemList() { $query="select id,ecmproduct_id,code,name,quantity from ".strtolower($this->module_class)."items where ".strtolower($this->module_class)."_id='".$this->id."'"; $this->item_list=array(); $zap=$this->db->query($query); while($res=$this->db->fetchByAssoc($zap)){ $this->item_list[]=$res; } } function checkStates() { if($this->types[$this->module]==0){ foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used from ecmstockoperations where documentitem_id='".$val['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['used']!=0){ $this->error=false; } $query="select sum(quantity) as quantity from ecmstockoperations where in_id='".$res['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['quantity']!='')$this->error=false; $this->codes.=$val['code'].','; } } if($this->types[$this->module]==1){ $module = new $this->module_class(); $module->retrieve($this->id); if($module->type=='correct'){ foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used from ecmstockoperations where documentitem_id='".$val['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['used']!=0){ $this->error=false; } $query="select sum(quantity) as quantity from ecmstockoperations where in_id='".$res['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['quantity']!='')$this->error=false; $this->codes.=$val['code'].','; } } } if($this->types[$this->module]==2){ foreach ($this->item_list as $key=>$val){ $query="select id,quantity,used from ecmstockoperations where documentitem_id='".$val['id']."' where type=0"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['used']!=0){ $this->error=false; } $query="select sum(quantity) as quantity from ecmstockoperations where in_id='".$res['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['quantity']!='')$this->error=false; $this->codes.=$val['code'].','; } } // for KS if($this->types[$this->module]==3){ foreach ($this->item_list as $key=>$val){ if($val['quantity']<0){ continue; } $query="select id,quantity,used from ecmstockoperations where documentitem_id='".$val['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['used']!=0){ $this->error=false; } $query="select sum(quantity) as quantity from ecmstockoperations where in_id='".$res['id']."'"; $row=$this->db->query($query); $res=$this->db->fetchByAssoc($row); if($res['quantity']!='')$this->error=false; $this->codes.=$val['code'].','; } } } } ?>