<?php 
declare(strict_types=0);
// session_start();
foreach($_GET as $campo => $valor){${$campo}=trim($valor);} 
foreach($_POST as $campo => $valor){${$campo}=trim($valor);} 

include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}
$user_codigo=$_SESSION['user_codigo'];
$user0=data_maes('user',$user_codigo);

$val_stock=0;
if(mb_stristr('imcp.orel',$cfg_empr)){$val_stock=1;}

$error=0;
if(empty($user_codigo)){$error=1;}
elseif(empty($item)){$error=2;}

if(empty($docu)){$docu;};
if(empty($docu)){$docu='CAR1';};

if(empty($lpre)){$lpre='puni1';}

if(empty($error))
{
     
//      $ptoe=str_pad($ptoe,6,'0',STR_PAD_LEFT);
     $ptoe='001001';
     $numero=str_pad($user_codigo,9,'0',STR_PAD_LEFT);
     
     $sql_carr ='select * from carr1 ';
     $sql_carr.='where docu="'.$docu.'" ';
     $sql_carr.='and iuser="'.(int)$user_codigo.'" ';
     $res_carr=msql_query($sql_carr,$link_data);

     if(msql_num_rows($res_carr)==0)
     {
          $sql_inse='insert into carr1(docu,numero,iuser,idate) value("'.$docu.'","'.$numero.'","'.$user_codigo.'",now())';
          $res_inse=msql_query($sql_inse,$link_data);
          $res_carr=msql_query($sql_carr,$link_data);
     }
     $idcarr=msql_result($res_carr,0,'idnum1');

     $sql_carr ='select * from carr2 ';
     $sql_carr.='where idnum2="'.$idcarr.'" ';
     $sql_carr.='and item="'.$item.'" ';
     $res_carr=msql_query($sql_carr,$link_data);
     if(msql_num_rows($res_carr)==0)
     {
          $iddet=get_iddet($docu,$idcarr);
          $sql_inse='insert into carr2(idnum2,iddet,item,idate2) value("'.$idcarr.'","'.$iddet.'","'.$item.'",now())';
          $res_inse=msql_query($sql_inse,$link_data);
          $res_carr=msql_query($sql_carr,$link_data);
     }
     $iddet=msql_result($res_carr,0,'iddet');

     $item0=data_maes('item',$item);
     
//      $puni=$item0['item_puni1'];
     $puni=$item0['item_puni'.substr($lpre,-1)];
     $iva=$item0['item_iva'];
     $umdd=$item0['item_umdd'];
     
     if(mb_stristr('UNID.UNI.',$umdd))
     {$cant=(int)$cant;}

     if($cfg_empr=='imcp')
     {
          $sql_updt ='update carr1 ';
          $sql_updt.='set ptoe="001001", ';
          $sql_updt.='concepto="VENTA DE INSUMOS MEDICOS", ';
          $sql_updt.='status="-" ';
          $sql_updt.='where idnum1="'.$idcarr.'" ';
          $sql_updt.='and (concepto="" or concepto is null)  ';
          $res_updt =msql_query($sql_updt,$link_data);
     }

     $sql_updt ='update carr1 left join carr2 on idnum1=idnum2 ';
//      $sql_updt.='left join item0 on item=item_codigo ';
     $sql_updt.='set ptoe="001001", ';
     $sql_updt.='item="'.$item.'", ';
     $sql_updt.='cant="'.$cant.'", ';
     $sql_updt.='puni="'.$puni.'", ';
     $sql_updt.='iva="'.$iva.'", ';
     $sql_updt.='fecha=now(), ';
     $sql_updt.='udate=now(), ';
     $sql_updt.='status="-" ';
     $sql_updt.='where idnum1="'.$idcarr.'" ';
     $sql_updt.='and iddet="'.$iddet.'" ';
     $sql_updt.='and '.$puni.'>0 ';
     $res_updt =msql_query($sql_updt,$link_data);

//      echo '*/*/*/*'.$sql_updt;
     $sql_updt='delete from carr2 where cant<=0';
     $res_updt=msql_query($sql_updt,$link_data);
     
     
     
     if($val_stock==1)
     {
          $sql_stock='select * from carr2 where idnum2='.$idcarr;
          $res_stock=msql_query($sql_stock,$link_data);
          while($aso_stock=msql_fetch_assoc($res_stock))
          {
               $xitem=$aso_stock['item'];
               $xbode=$aso_stock['bode'];
               $xcant=$aso_stock['cant'];
               $xiddet=$aso_stock['iddet'];
               
               $stock=sal_item($xitem,$xbode,'D');
               $xstock=$stock['uni_fin'];
               if($xstock<$xcant)
               {
                    $sql_updt='update carr2 set cant='.$xstock.' where idnum2='.$idcarr.' and iddet='.$xiddet;
                    $res_updt=msql_query($sql_updt,$link_data);
               }
          }
     }
     
     
     $sql_item ='select *, ';
     $sql_item.='sum(cant) as cantidad ';
     $sql_item.='from carr1 ';
     $sql_item.='left join carr2 on idnum1=idnum2 ';
     $sql_item.='left join item0 on item=item_codigo ';
     $sql_item.='where item_status not in ("I","X") ';
     $sql_item.='and docu="CAR1" ';
     $sql_item.='and iuser='.$user_codigo.' ';
     
     $calc_fields=calc_fields('carr');
     if(!empty($calc_fields))
     {$sql_item=str_replace(' from ',','.$calc_fields.' from ',$sql_item);}

     $sql_itemtot=$sql_item;
     
     $sql_item.=$xgroupby;                                 
     $sql_item.=$xorderby;     

     $res_itemtot=msql_query($sql_itemtot,$link_data);
     $cant=number_format(msql_result($res_itemtot,0,'cant_s'),2);
     $subt=number_format(msql_result($res_itemtot,0,'subt'),2);
     $tiva=number_format(msql_result($res_itemtot,0,'tiva'),2);
     $total=number_format(msql_result($res_itemtot,0,'total'),2);

     
     
     $dom = new DOMDocument();
          $dom->encoding = 'utf-8';
          $dom->xmlVersion = '1.0';
          $dom->formatOutput = true;

     $response=$dom->appendChild($dom->createElement("response"));
     // $msgsys=$response->appendChild($dom->createElement("msgsys",$sql_query)); 
     $msgswal=$response->appendChild($dom->createElement("msgswal")); 
          $msgswal->setAttribute("id","msgswal");
     $msgsys=$response->appendChild($dom->createElement("msgsysx")); 
          $msgsys->appendChild($dom->createElement("cant",$cant)); 
          $msgsys->appendChild($dom->createElement("total",$total)); 
     
          echo $dom->saveXML();
     
     
}
     
?>

