<?php
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
$COD=V($_GET['COD']);
if(strlen($COD)>0){
	//INI_GENERANDO LAS PIEZAS
	ejt("DELETE FROM t_productos");
	$ZX=0;
	$CX=0;
	if($COD=="TODOS"){
		$s="SELECT ID,CO,DE,LI,ELI,PMI,PMA,ROJ,AMA FROM par_productos  ORDER BY ID ";
	}else{
		$s="SELECT ID,CO,DE,LI,ELI,PMI,PMA,ROJ,AMA FROM par_productos WHERE CO='$COD' ORDER BY ID ";
	}
	$rs = grs($s);
	while ($r = $rs->fetch_assoc()){
		$ID=$r['ID'];
		$CO=$r['CO'];
		$DE=$r['DE'];
		$LI=$r['LI'];
		$EL=$r['ELI'];
		$MI=$r['PMI'];
		$MA=$r['PMA'];
		$RO=$r['ROJ'];
		$AM=$r['AMA'];
		$SE=0;
		$ET=0;
		//
		$s="SELECT sp_presentacion as PIE 
			FROM stocks_presentaciones WHERE sp_id_producto=$ID 
			GROUP BY sp_presentacion 
			ORDER BY sp_presentacion";
		$rp = grs($s);
		while ($p = $rp->fetch_assoc()){
			$PI=$p['PIE'];
			$ST=gn("SUM(sp_cantidad)","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI");
			$PV=gn("sp_provicional","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI AND sp_provicional=1");		
			$ZX++;
			$s="INSERT INTO t_productos VALUES ($ZX,$ID,'$CO','$DE',$MI,$MA,'$LI',$PI,$ST,$PV,$SE,$ET,'$EL',$AM,$RO)";
			ejt($s);
			$CX++;
		}
		//
		$s="SELECT PIE FROM vcot_control WHERE IDP=$ID AND (CA_CONTROL - DEJ)>0 AND ANU='N' AND PRI='1' GROUP BY PIE ";
		$rp = grs($s);
		while ($p = $rp->fetch_assoc()){
		  $PI=$p['PIE'];
		  if(gn("count(*)","t_productos","pro_id=$ID AND pro_pie=$PI")==0){
			$ST=gn("SUM(sp_cantidad)","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI");
			$PV=gn("sp_provicional","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI AND sp_provicional=1");
			$ZX++;
			$s="INSERT INTO t_productos VALUES ($ZX,$ID,'$CO','$DE',$MI,$MA,'$LI',$PI,$ST,$PV,$SE,$ET,'$EL',$AM,$RO)";
			ejt($s);
			$CX++;
		  }
		}
		//
		$z="SELECT PIE FROM vven_control WHERE IDP=$ID AND (CA_CONTROL - DEJ)>0 AND ANU='N' AND PRI=1 GROUP BY PIE ";
		$result_p = grs($z);
		while ($row_p = $result_p->fetch_assoc()){
		  $PI=$p['PIE'];
		  if(gn("count(*)","t_productos","pro_id=$ID AND pro_pie=$PI")==0){
			$ST=gn("SUM(sp_cantidad)","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI");
			$PV=gn("sp_provicional","stocks_presentaciones","sp_id_producto=$ID AND sp_presentacion=$PI AND sp_provicional=1");
			$ZX++;
			$s="INSERT INTO t_productos VALUES ($ZX,$ID,'$CO','$DE',$MI,$MA,'$LI',$PI,$ST,$PV,$SE,$ET,'$EL',$AM,$RO)";
			ejt($s);
			$CX++;
		  }
		}	
	}
	echo $CX." productos/piezas <p>";
	//FIN_GENERANDO LAS PIEZAS
}

?>