<?php

//$prod = $_POST['producto']; // id de farticulo
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
error_reporting(E_ALL);
ini_set('display_errors', '1');
include("DMMYSQLG.php");
@$opd = mysqli_query($con ,"update dgreqded a join dgreqdev b on a.cons_ven=b.fol_ven set a.fec_ven=b.fec_ven where  b.cancelado <> 1");
@$opd = mysqli_query($con ,"update dgrequid a join dgrequie b on a.cons_ven=b.fol_ven set a.fec_ven=b.fec_ven where  b.cancelado <> 1");

function updatepp($producto, $mes, $anio) {
    if (isset($producto) && $producto > 0) {
        $prod = $producto;

         @$inserta = mysqli_query($con ,"REPLACE INTO `resp_art` (`nparte1`, `tip_ven`, `modulo`, `np_desc1`, `cod_var`, `descripcion`, `codigo`, `calle`, `estado`, `postal`, `colonia`, `delegacion`, `tel1`, `tel2`, `fax`, `curp`, `web`, `mail`, `rfc`, `contacto`, `proveedor`, "
                 . "`dias`, `numero`, `ubicacion`, `cod_pro`, `cos_prom`, `acc_fec`, `acc_hor`, `usuario`, `por_des`, `pais`, `t6`, `t7`, `t8`, `t9`, `t10`,  `t11`, `t5`, `seccion`, `anaquel`, `cubo`, `max`, `min`, `t1`, `t2`, `t3`, `exi_fis`, `exi_dis`, `marca`, `estante`,  `fecha_inv`, `sub`, `cos_prom_ant`, `t4`, `directo`, `depto_sal`, `aingpop`, `acanpop`, `ainginv`, `acaninv`, `mes1`,  mes2, `mes3`, `mes4`, `mes5`, `mes6`, `mes7`, `mes8`, `mes9`, `mes10`, `mes11`, `mes12`)  "
                        . "select nparte1,tip_ven,modulo,np_desc1,cod_var,descripcion,codigo,calle,estado,postal,colonia,delegacion,tel1,tel2,fax,curp,web,mail,rfc,contacto,proveedor,dias,numero,ubicacion,cod_pro,cos_prom,acc_fec,acc_hor,usuario,por_des,pais,t6,t7,t8,t9,t10
,t11,t5,seccion,anaquel,cubo,max,min,t1,t2,t3,exi_fis,exi_dis,marca,estante,fecha_inv,sub,cos_prom_ant,t4,directo,depto_sal,aingpop,acanpop,ainginv,acaninv,mes1,mes2,mes3,mes4,mes5,mes6,mes7,mes8,mes9,mes10,mes11,mes12 from farticulosn where nparte1='" . $producto . "'");
//        echo $producto;
//        echo $mes;
//        echo $anio;

        for ($i = 31; $i >= 1; $i--) {
        
            @$result3 = mysqli_query($con ,"select * from dgrequid where cons_ven in (select fol_ven from dgrequie where dgrequie.cancelado <> 1 and fec_ven='$anio-$mes-$i' ) and dgrequid.nparte1=$prod");
  
            while ($row = mysqli_fetch_array($result3)) {
                @$result2 = mysqli_query($con ,"select * from resp_art where nparte1='" . $row['nparte1'] . "'");
                // echo "select * from farticulos where nparte1='".$row['nparte1']."'";

                while ($rowp = mysqli_fetch_array($result2)) {

                    if ($rowp['t5'] <= 0 && $i <= 30) {

                        $sql = mysqli_query($con ,"UPDATE dgrequid SET `fec_ven` = DATE_ADD(`fec_ven` , INTERVAL 1 DAY) WHERE fol_ven1 ='" . $row['fol_ven1'] . "' and month(fec_ven)=month(DATE_ADD(`fec_ven` , INTERVAL 1 DAY)) ;");
                        
                        break;
                    }
                    $cos = $rowp['cos_prom'];
                    $exifis =$rowp['exi_fis'];
                    $iva = $rowp['web'];
                    $cant = $row['can_ref'];
                    $subt = $cos * $cant;
                    $ivat = ($subt * $iva) / 100;
                    $exi = $rowp['t5'];
                    $tot = $subt + $ivat;
                    if ($rowp['directo'] == 0) {
                        //$sql = mysqli_query($con ,"UPDATE dgrequid "
                          //      . "SET pre_uni='$cos', iva_pre='$ivat',  subtotal = '$subt', total_cd = '$tot' WHERE fol_ven1 ='" . $row['fol_ven1'] . "'");
                       // echo mysqli_error($con);
                    }//                                                
                    $e = $exifis + $cant;
                    $e1 = $exi + $cant;
                    $sql = mysqli_query($con ,"UPDATE resp_art "
                            . "SET t5='$e1', exi_fis='$e'  WHERE nparte1 ='" . $row['nparte1'] . "'");
                    echo mysqli_error($con);
                    //    echo mysqli_affected_rows()." afectadas";
                }
            }
     
        
            @$result = mysqli_query($con ,"select * from dgreqded where cons_ven in (select fol_ven from dgreqdev where dgreqdev.cancelado <> 1 and fec_ven='$anio-$mes-$i' ) and dgreqded.nparte1=$prod");
           // echo "select * from dgreqded where cons_ven in (select fol_ven from dgreqdev where dgreqdev.cancelado <> 1 and fec_ven='$anio-$mes-$i' ) and dgreqded.nparte1=$prod";
            while ($row = mysqli_fetch_array($result)) {
                @$result2 = mysqli_query($con ,"select * from resp_art where nparte1='" . $row['nparte1'] . "'");
                $num_salidas = mysqli_num_rows($result2);
                if ($num_salidas == 0) {
                    // echo $row['nparte1'] . "nuevo <br/>";
                    @$inserta = mysqli_query($con ,"REPLACE INTO `resp_art` (`nparte1`, `tip_ven`, `modulo`, `np_desc1`, `cod_var`, `descripcion`, `codigo`, `calle`, `estado`, `postal`, `colonia`, `delegacion`, `tel1`, `tel2`, `fax`, `curp`, `web`, `mail`, `rfc`, `contacto`, `proveedor`, `dias`, `numero`, `ubicacion`, `cod_pro`, `cos_prom`, `acc_fec`, `acc_hor`, `usuario`, `por_des`, `pais`, `t6`, `t7`, `t8`, `t9`, `t10`,  `t11`, `t5`, `seccion`, `anaquel`, `cubo`, `max`, `min`, `t1`, `t2`, `t3`, `exi_fis`, `exi_dis`, `marca`, `estante`,  `fecha_inv`, `sub`, `cos_prom_ant`, `t4`, `directo`, `depto_sal`, `aingpop`, `acanpop`, `ainginv`, `acaninv`, `mes1`,  mes2, `mes3`, `mes4`, `mes5`, `mes6`, `mes7`, `mes8`, `mes9`, `mes10`, `mes11`, `mes12`)  "
                        . "select nparte1,tip_ven,modulo,np_desc1,cod_var,descripcion,codigo,calle,estado,postal,colonia,delegacion,tel1,tel2,fax,curp,web,mail,rfc,contacto,proveedor,dias,numero,ubicacion,cod_pro,'0',acc_fec,acc_hor,usuario,por_des,pais,t6,t7,t8,t9,t10
,t11,'0',seccion,anaquel,cubo,max,min,t1,t2,t3,'0',exi_dis,marca,estante,fecha_inv,sub,cos_prom_ant,t4,directo,depto_sal,aingpop,acanpop,ainginv,acaninv,mes1,mes2,mes3,mes4,mes5,mes6,mes7,mes8,mes9,mes10,mes11,mes12 from farticulos where nparte1='" . $row['nparte1'] . "'");

                    echo mysqli_error($con);
                    @$result2 = mysqli_query($con ,"select * from resp_art where nparte1='" . $row['nparte1'] . "'");
                }

              

                while ($rowp = mysqli_fetch_array($result2)) {
                    $exifis=0;
                    $exifis =$rowp['exi_fis'];
                    $exi = $rowp['t5'];
                    $cos = $rowp['cos_prom'];
                    $cant = $row['can_ref'];
                    $pre = $row['pre_uni'];
                    $e = $exifis - $cant;
                    $e1 = $exi - $cant;
                    //$pp = (($exi * $cos) + ($cant * $pre)) / $e1;
                    if($e1<=0)$pp=0;
                    else $pp=-($cant * $pre)/$e1 + ($cos*($e1+$cant))/$e1;
			
                    //    if($pp<0)$pp=(-1*$pp);
                    if ($pp <= 0&&$e1>0)
                        $pp = $row['pre_uni'];
                    if ($rowp['directo'] == 1)
                        $pp = $row['pre_uni'];
                    $sql = mysqli_query($con ,"UPDATE resp_art "
                            . "SET t5='$e1', exi_fis='$e',  cos_prom = '$pp', cos_prom_ant = '$cos' WHERE nparte1 ='" . $row['nparte1'] . "'");
                    echo mysqli_error($con);
                }//termina de actulizar articulos
                //iniciamos cambios en salidas
            }
            //echo "$anio-$mes-$i<br>";
           }
           @$inserta = mysqli_query($con ,"REPLACE INTO `farticulos` (`nparte1`, `tip_ven`, `modulo`, `np_desc1`, `cod_var`, `descripcion`, `codigo`, `calle`, `estado`, `postal`, `colonia`, `delegacion`, `tel1`, `tel2`, `fax`, `curp`, `web`, `mail`, `rfc`, `contacto`, `proveedor`, "
                 . "`dias`, `numero`, `ubicacion`, `cod_pro`, `cos_prom`, `acc_fec`, `acc_hor`, `usuario`, `por_des`, `pais`, `t6`, `t7`, `t8`, `t9`, `t10`,  `t11`, `t5`, `seccion`, `anaquel`, `cubo`, `max`, `min`, `t1`, `t2`, `t3`, `exi_fis`, `exi_dis`, `marca`, `estante`,  `fecha_inv`, `sub`, `cos_prom_ant`, `t4`, `directo`, `depto_sal`, `aingpop`, `acanpop`, `ainginv`, `acaninv`, `mes1`,  mes2, `mes3`, `mes4`, `mes5`, `mes6`, `mes7`, `mes8`, `mes9`, `mes10`, `mes11`, `mes12`)  "
                        . "select nparte1,tip_ven,modulo,np_desc1,cod_var,descripcion,codigo,calle,estado,postal,colonia,delegacion,tel1,tel2,fax,curp,web,mail,rfc,contacto,proveedor,dias,numero,ubicacion,cod_pro,cos_prom,acc_fec,acc_hor,usuario,por_des,pais,t6,t7,t8,t9,t10
,t11,t5,seccion,anaquel,cubo,max,min,t1,t2,t3,exi_fis,exi_dis,marca,estante,fecha_inv,sub,cos_prom_ant,t4,directo,depto_sal,aingpop,acanpop,ainginv,acaninv,mes1,mes2,mes3,mes4,mes5,mes6,mes7,mes8,mes9,mes10,mes11,mes12 from resp_art where nparte1='" . $producto . "'");
  echo mysqli_error($con);
 
    }
  
//    $sql = mysqli_query($con ,"truncate table farticulos");
//    $sql = mysqli_query($con ,"INSERT INTO farticulos SELECT * from farticulosn");
    //   echo 'exito';
}

$tiempo = mysqli_fetch_array(mysqli_query($con ,"select MONTH(fec_ven) AS mes,YEAR(fec_ven) AS anio  from dgreqdev  order by fol_ven desc limit 1"));
$mes = $tiempo['mes'];
$anio = $tiempo['anio'];

@$erores = mysqli_query($con ,"select  fa.nparte1,fa.np_desc1,ini.cos_prom ci,fa.cos_prom,
sum(ini.cos_prom*ini.t5) inicial ,ent.entrada entradas ,sal.salida salidas,sum(fa.cos_prom*fa.t5) total,ini.t5 t5i,ent.ent,sal.sal,fa.t5,
((IFNULL(ini.t5,0)+IFNULL(ent.ent,0)-IFNULL(sal.sal,0))-fa.t5) as exies,
(IFNULL(sum(fa.cos_prom*fa.t5),0)-( IFNULL(sum(ini.cos_prom*ini.t5),0)+ IFNULL(ent.entrada,0)- IFNULL(sal.salida,0)))as dif
from farticulos fa left outer join  farticulosn ini  on ini.nparte1=fa.nparte1 left outer join
  (select COALESCE(sum(subtotal),0) as entrada,sum(can_ref)as ent,nparte1 from dgreqded  
 where cons_ven in (select fol_ven from dgreqdev where dgreqdev.cancelado <> 1  and fec_ven between '$anio-$mes-01' and '$anio-$mes-31')  
  group by nparte1) ent
   on    ent.nparte1=fa.nparte1
left outer join 
 (select COALESCE(sum(subtotal),0) as salida,sum(can_ref)as sal,nparte1 from dgrequid 
 where cons_ven in (select fol_ven from dgrequie where dgrequie.cancelado <> 1 and fec_ven between '$anio-$mes-01' and '$anio-$mes-31')
  group by nparte1) sal
 on    sal.nparte1=fa.nparte1
group by fa.nparte1
 having dif<-.5 or dif >.5");

while ($rowp = mysqli_fetch_array($erores)) {
updatepp($rowp['nparte1'], $mes, $anio);
}
?>
	
