Files
crm.twinpol.com/modules/EcmInsideOrders.cp/saveProduction.php
2025-05-12 15:44:39 +00:00

160 lines
6.3 KiB
PHP
Executable File

<?php
include_once("modules/EcmSales/productionHelper.php");
include_once("modules/EcmStockDocInsideOuts/EcmStockDocInsideOut.php");
include_once("modules/EcmStockDocInsideIns/EcmStockDocInsideIn.php");
if($_REQUEST['save_documents']){
$rdoc=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select * from ecminsideorders where id='".$_REQUEST['record']."'"));
$json = getJSONobj();
$out=new EcmStockDocInsideOut();
$out->name=$rdoc['document_no'];
$out->assigned_user_id=$_SESSION['authenticated_user_id'];
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id,name from ecmstocks where id='".$rdoc['rw_stock_id']."'"));
$out->stock_id=$rdoc['rw_stock_id'];
$out->stock_name=$r['name'];
$out->ecmlanguage="pl_pl";
$out->currency_id="PLN";
$out->register_date=$GLOBALS['timedate']->to_display_date(date("Y-m-d"));
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id,name from ecmdocumenttemplates where deleted='0' order by date_entered asc"));
$out->template_id=$r['id'];
$out->template_name=$r['name'];
$out->status="accepted";
if(count($_REQUEST['rw_id'])>0){
for($i=0;$i<count($_REQUEST['rw_id']);$i++){
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select vat_id,vat_value,vat_name,name,code,id,product_category_id from ecmproducts where id='".$_REQUEST['rw_id'][$i]."'"));
$return_array['id'] = $_REQUEST['rw_id'][$i];
$return_array['guid'] = $_REQUEST['rw_guid'][$i];
$return_array['position'] = $i;
$return_array['code'] = $r['code'];
$return_array['name'] = $r['name'];
$return_array['quantity'] = ($_REQUEST['rw_quantity'][$i]);
$return_array['price'] = round(($_REQUEST['rw_price'][$i]),2);
$return_array['discount'] = 0;
$return_array['total'] = round(($_REQUEST['rw_price'][$i]),2)*round(uN($_REQUEST['rw_quantity'][$i]),2);
$return_array['unit_id'] = 1;
$return_array['unit_name'] = "szt.";
$return_array['vat_id'] = $r['vat_id'];
$return_array['vat_name'] = $r['vat_name'];
$return_array['vat_value'] = $r['vat_value'];
$return_array['category_id'] = $r['product_category_id'];
$return_array['currency_id'] = "PLN";
$return_array['currency_name'] = "PLN";
$rw_prod[]=$return_array;
}
}
$out->position_list=$rw_prod;
$out_id=$out->save(true);
$GLOBALS['db']->query("update ecminsideorders set rw_id='".$out_id."' where id='".$_REQUEST['record']."'");
$out_link='<a href="index.php?module=EcmStockDocInsideOuts&action=DetailView&record='.$out_id.'">'.$out->document_no.'</a><br>';
$in=new EcmStockDocInsideIn();
$in->name=$rdoc['document_no'];
$in->assigned_user_id=$_SESSION['authenticated_user_id'];
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id,name from ecmstocks where id='".$rdoc['pw_stock_id']."'"));
$in->stock_id=$rdoc['pw_stock_id'];
$in->stock_name=$r['name'];
$in->ecmlanguage="pl_pl";
$in->currency_id="PLN";
$in->register_date=$GLOBALS['timedate']->to_display_date(date("Y-m-d"));
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select id,name from ecmdocumenttemplates where deleted='0' order by date_entered asc"));
$in->template_id=$r['id'];
$in->template_name=$r['name'];
$in->status="accepted";
if(count($_REQUEST['pw_id'])>0){
for($i=0;$i<count($_REQUEST['pw_id']);$i++){
$wi=$GLOBALS['db']->query("select * from ecmstockdocinsideoutitems where guid='".$_REQUEST['pw_guid'][$i]."' and deleted='0'");
$rqty=0;
$rvalue=0;
$rprice=0;
while($rwi=$GLOBALS['db']->fetchByAssoc($wi)){
$tmp = round(floatval($rwi['price']),2)*floatval($rwi['quantity']);
$rvalue+=$tmp;
//echo $rwi['price'].' - '.round(floatval($rwi['price']),2).' - '.floatval($rwi['quantity']).' - '.$tmp;
//echo '<pre>';print_r($rwi);echo '</pre>';
}
/*while($rwi=$GLOBALS['db']->fetchByAssoc($wi)){
$wo=$GLOBALS['db']->query("select sum(price*quantity) as sum from ecmstockoperations where documentitem_id='".$rwi['id']."'");
$ro=$GLOBALS['db']->fetchByAssoc($wo);
$rvalue+=$ro['sum'];
}*/
if(floatval($_REQUEST['pw_quantity'][$i])>0)$rprice=$rvalue/floatval($_REQUEST['pw_quantity'][$i]);
$r=$GLOBALS['db']->fetchByAssoc($GLOBALS['db']->query("select vat_id,vat_value,vat_name,name,code,id,product_category_id from ecmproducts where id='".$_REQUEST['pw_id'][$i]."'"));
$return_array['id'] = $_REQUEST['pw_id'][$i];
$return_array['position'] = $i;
$return_array['code'] = $r['code'];
$return_array['name'] = $r['name'];
$return_array['quantity'] = ($_REQUEST['pw_quantity'][$i]);
$return_array['price'] = $rprice;
$return_array['discount'] = 0;
$return_array['total'] =$rvalue;
$return_array['unit_id'] = 1;
$return_array['unit_name'] = "szt.";
$return_array['vat_id'] = $r['vat_id'];
$return_array['vat_name'] = $r['vat_name'];
$return_array['vat_value'] = $r['vat_value'];
$return_array['category_id'] = $r['product_category_id'];
$return_array['currency_id'] = "PLN";
$return_array['currency_name'] = "PLN";
$pw_prod[]=$return_array;
}
}
$in->position_list=$pw_prod;
$in_id=$in->save(true);
$GLOBALS['db']->query("update ecminsideorders set pw_id='".$in_id."' where id='".$_REQUEST['record']."'");
$in_link='<a href="index.php?module=EcmStockDocInsideIns&action=DetailView&record='.$in_id.'">'.$in->document_no.'</a><br>';
$t.=$tbs;
$t.=$trs;
$t.=$tds1;
$t.="Document No";
$t.=$tde;
$t.=$tds1;
$t.="Description";
$t.=$tde;
$t.=$tds1;
$t.="Inventory";
$t.=$tde;
$t.=$tre;
$t.=$trs;
$t.=$tds1;
$t.=$out_link;
$t.=$tde;
$t.=$tds1;
$t.=$out->name;
$t.=$tde;
$t.=$tds1;
$t.=$out->stock_name;
$t.=$tde;
$t.=$tre;
$t.=$trs;
$t.=$tds1;
$t.=$in_link;
$t.=$tde;
$t.=$tds1;
$t.=$in->name;
$t.=$tde;
$t.=$tds1;
$t.=$in->stock_name;
$t.=$tde;
$t.=$tre;
$t.=$tbe;
echo $t;
$GLOBALS['db']->query("update ecminsideorders set status='accepted' where id='".$_REQUEST['record']."'");
/*
echo "<br><br>
delete from ecmstockoperations where parent_id='$in_id';
delete from ecmstockdocinsideinitems where ecmstockdocinsidein_id='$in_id';
delete from ecmstockdocinsideins where id='$in_id';
delete from ecmstockoperations where parent_id='$out_id';
delete from ecmstockdocinsideoutitems where ecmstockdocinsideout_id='$out_id';
delete from ecmstockdocinsideouts where id='$out_id';
";
*/
}
//header("Location: index.php?module=EcmSales&action=ListProduction");
?>