<?php
include_once('lib_main.php');
if(!empty($empr)){$link_data=msql_connect('','','',$empr); $cfg_empr=$empr;}



$iddic=9024;

$sql_inve='select *, ';
$sql_inve.='case when left(docu,1)="I" then 1 else -1 end as signo ';
$sql_inve.='from inve1 ';
$sql_inve.='left join inve2 on idnum1=idnum2 ';
$sql_inve.='left join item0 on item=item_codigo ';
$sql_inve.='where status not in ("O","X") ';
$sql_inve.='and item_lsub like "INAG%" ';
// $sql_inve.='and idnum1<=9024 ';
// $sql_inve.='and concat(bode,item) in(select distinct concat(bode,item) as cla from inve1 left join inve2 on idnum1=idnum2 where puni!=punibak and idnum2<="'.$iddic.'") ';
$sql_inve.='order by bode,item,fecha,signo desc ';
// $sql_inve.='limit 10 ';
$res_inve=msql_query($sql_inve,$link_data);
$regs=msql_num_rows($res_inve);


echo ' ==> '.date ('Y-m-d H:i:s');
echo '<br>';
echo '<a href="?scri=hfre_fechas.php" >SET</a>';
echo '<br>';
echo '<table border="1" style="text-align:center;" cellpadding="10">';
$litem='';
$lbode='';
while($aso_inve=msql_fetch_assoc($res_inve))
{
     $xidnum1=$aso_inve['idnum1'];
     $xfecha=$aso_inve['fecha'];
     $xdocu=$aso_inve['docu'];
     $xitem=$aso_inve['item'];
     $xbode=$aso_inve['bode'];
     $xcant=$aso_inve['cant'];
     $xpuni=$aso_inve['puni'];
     $xpunibak=$aso_inve['punibak'];
     $xnumfac=$aso_inve['numfac'];
     
     if($xitem!=$litem or $xbode!=$lbode)
     {
          $stock=0;
          $saldo=0;
          $litem=$xitem;
          $lbode=$xbode;
     }
     
     
     if(substr($xdocu,0,1)=='E'){$xcant=$xcant*-1;}
     $stock=$stock+$xcant;
          
          
//      if($stock<-0.01 or $xpuni!=$xpunibak)
     if($stock<-0.01 and $xfecha>="2021-11-01")
     {
     echo '<tr>';
     echo '<td>'.$xidnum1.'</td>';
     echo '<td>'.$xitem.'</td>';
     echo '<td>'.$xbode.'</td>';
     echo '<td>'.$xfecha.'</td>';
     echo '<td>'.$xdocu.'</td>';
     echo '<td>'.number_format($xcant,2).'</td>';
     echo '<td>'.number_format($stock,2).'</td>';
     echo '<td>'.number_format($xpuni,6).'</td>';
     echo '<td>'.number_format($xpunibak,6).'</td>';
//      if($stock<-0.01 or $xpuni!=$xpunibak)
     if($stock<-0.01)
     {
          echo '<td>'.'<a href="?scri=info_modu3&modu=inve&info=iikrdx&cost=true&prnt=1&bode='.$xbode.'&item='.$xitem.'&fecha1='.substr($xfecha,0,7).'-01&fecha2='.substr($xfecha,0,7).'-31" target="_blank">'.'LINK'.'</a>'.'</td>';
          if($stock<-0.01)
          {echo '<td>'.'*/0/*'.'</td>';}
//           if($xpuni!=$xpunibak)
//           {echo '<td>'.'*/1/*'.'</td>';}
     }
     echo '</tr>';
     }
}
echo '</table>';

?>