<?php
ini_set("session.cookie_lifetime",60*60*10);
ini_set("session.gc_maxlifetime", 60*60*10);
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	$HW=$_SESSION['SYS_HW'];
	$HW=$HW."_AJ";
	$StrCmd=V($_GET['Cmd']);
	$P1=V($_GET['P1']);
	$P2=V($_GET['P2']);
	$P3=V($_GET['P3']);
	$P4=V($_GET['P4']);
	$P5=V($_GET['P5']);
	$P6=V($_GET['P6']);
	$P7=V($_GET['P7']);
	$P8=V($_GET['P8']);
	$P9=V($_GET['P9']);	
	$P10=V($_GET['P10']);	
	$P11=V($_GET['P11']);	
	$P12=V($_GET['P12']);	
	//
	if($StrCmd=="TRA_ELI"){
		$ID_TRA=vbval($P1);
		$VAL=gs("val","transferencias","id_trans=$ID_TRA");
		echo $VAL;
	}
	//
	if($StrCmd=="GIX"){
		$OK=1;
		$HW=trim($P1);		
		$ALM=trim($P2);
		//
		$EMP=trim($P3);
		$ISER=vbval($P4);
		$INUM=vbval($P5);
		//
		$EmpR_=gn("id_emp","empresas","emp_nomc='$EMP'");
		$IDC=gn("ID_COT","vix_cotizaciones","SER_=$ISER AND NUM_=$INUM AND ID_EMP=$EmpR_");
		//
		$AL=gn("id_almacen","almacenes","alm_obs='$ALM'");
		$s="SELECT * FROM temp_compras WHERE cod_temc='$HW' ORDER BY temc_index";
		$rs=grs($s);
		while($row=$rs->fetch_row()){
			$X_ID=$row[1];	
			$X_CAU=$row[8];	
			$X_CA=$row[2];	
			$_SA=gn("sp_cantidad","stocks_presentaciones",
					"sp_id_almacen=$AL AND sp_id_producto=$X_ID AND sp_presentacion=$X_CAU");
			if($X_CA>$_SA){
				$OK=0;
				break;
			}
		}
		echo $OK;
	}
	if($StrCmd=="GIX_24"){
		$OK=1;
		$HW=trim($P1);		
		$ALM=trim($P2);
		//
		$EMP=trim($P3);
		$ISER=vbval($P4);
		$INUM=vbval($P5);
		//
		$EmpR_=gn("id_emp","empresas","emp_nomc='$EMP'");
		$IDC=gn("ID_COT","vix_cotizaciones","SER_=$ISER AND NUM_=$INUM AND ID_EMP=$EmpR_");
		//
		FxCOT_PARSE_CANTIDADES($IDC);
		//++
		$MSG="";
		$WSG="R";
		$AL=gn("id_almacen","almacenes","alm_obs='$ALM'");
		$s="SELECT * FROM temp_compras WHERE cod_temc='$HW' ORDER BY temc_index";
		$rs=grs($s);
		while($row=$rs->fetch_row()){
			$X_ID=$row[1];	
			$X_CAU=$row[8];	
			$X_CA=$row[2];	
			//
			$CX=gn("count(*)","cotizaciones_det","id_cotizacion=$IDC AND id_producto=$X_ID AND can_uni=$X_CAU");
			if($CX==0){
				$PRO=gs("pro_des","productos","id_producto=$X_ID");
				$MSG="Incoherencia de datos producto : $PRO piezas : $X_CAU no relacionado";
				$WSG="X";
				break;
			}
			//
			$COT_DET_SAL=gn("ca - dej","cotizaciones_det","id_cotizacion=$IDC AND id_producto=$X_ID AND can_uni=$X_CAU");
			if($X_CA > $COT_DET_SAL){
				$PRO=gs("pro_des","productos","id_producto=$X_ID");
				$MSG="Saldo en proforma insuficiente. Producto:$PRO, piezas:$X_CAU ,cantidad solicitada : $X_CA , saldo en proforma : $COT_DET_SAL";
				break;
			}
			//
			$_SA=gn("sp_cantidad","stocks_presentaciones",
					"sp_id_almacen=$AL AND sp_id_producto=$X_ID AND sp_presentacion=$X_CAU");
			if($X_CA>$_SA){
				$PRO=gs("pro_des","productos","id_producto=$X_ID");
				$MSG="Stock insuficiente. Producto:$PRO, piezas:$X_CAU ,cantidad solicitada : $X_CA , stock : $_SA";
				break;
			}
		}
		//
		if(strlen($MSG)==0){
			echo "|OK|OK|$WSG|";
		}else{
			echo "|MS|$MSG|$WSG|";
		}
		return;
	}
	//
	if($StrCmd=="XX_1"){
		$COT_ID=vbval($P1);
		$CLI_ID=vbval($P2);
		$VEN_NO=$P3;
		$COT_TR=vbval($P4);
		$COT_TD=vbval($P5);
		$COT_TP=$P6;
		$COT_AC=vbval($P7);
		$COT_MN=vbl($P8,1);
		$COT_MD=$P9;
		$COT_VU=vbval($P10);
		$COT_SA=vbval($P11);
		$COT_XX=$P12;
		$COT_US=$P13;
		////////////////////////////////
		$KOT_ID=gn("count(*)","cotizaciones","id_cotizacion=$COT_ID");
		if($KOT_ID==0){
			echo "MS|ID no registrado"; return;
		}	
		$KOT_ID=$COT_ID;
		$VEN_ID=gn("id_personal","personal","per_nombre='$VEN_NO'");
		$KOT_VEN_ID=gn("id_vendedor","cotizaciones","id_cotizacion=$KOT_ID");
		if($VEN_ID!=$KOT_VEN_ID){
			$_AUT=gn("id_usuario","usuarios","id_usuario>0 AND hab='SI' AND mb='1' AND nom='$COT_XX' AND pass='$COT_US'");
			if($_AUT==0){
				echo "MS|Para cambiar el vendedor se requiere ingresar un usuario y password autorizado"; return;
			}
		}
		if(COT_V_CR($KOT_ID)){
			ejt("UPDATE cotizaciones SET id_vendedor=$VEN_ID,id_cliente=$CLI_ID WHERE id_cotizacion=$KOT_ID");
			echo "MS|Uno mas cobros registrados, solo se actualizo los datos del vendedor y del cliente"; return;
		}else{
			ejt("UPDATE cotizaciones 
				 SET id_cliente=$CLI_ID,
				 	 id_vendedor=$VEN_ID,
					 tot_real=$COT_TR,
					 tot_des=$COT_TD,
					 id_tip_pag='$COT_TP',
					 acu=$COT_AC,
					 mon='$COT_MN',
					 modalidad='$COT_MD',
					 vue=$COT_VU,
					 sal=$COT_SA
				 WHERE id_cotizacion=$KOT_ID");
			echo "OK|OK"; return;
		}
	}
	//
	if($StrCmd=="XX"){
		$ID=vbval($P1);
		$TI=$P2;
		$MO=$P3;
		$VU=vbval($P4);
		$IC=vbval($P6);
		$VE=gn("id_personal","personal","per_nombre='$P5'");
		ejt("UPDATE cotizaciones 
			 SET id_tip_pag='$TI',modalidad='$MO',vue=$VU,id_vendedor=$VE,id_cliente=$IC  
			 WHERE id_cotizacion=$ID");
		echo "OK";
	}
	//
	if($StrCmd=="V3"){
		$S=vbval($P1);
		$N=vbval($P2);
		$I=vbval($P3);		
		$T=gn("count(*)","transferencias","(tser_=$S AND tnum_=$N) AND id_trans<>$I");
		//$T=gn("count(*)","transferencias","(tser_=$S AND tnum_=$N)");
		if($T>0){
			echo "SN";
		}else{
			echo "OK";
		}
				
	}
	//
	if($StrCmd=="V2"){
		$C=vbval($P1);
		echo CLI_VE($C);		
	}
	//
	if($StrCmd=="V1"){
		$C=vbval($P1);
		$R=$P2;
		$D=$P3;
		/*
		if(strlen($R)>0){
			$I=gn("id_cliente","clientes","cli_ruc='$R' AND id_cliente<>$C");	
			if($I>0){
				echo "RR";return;
			}else{
				if(strlen($D)>0){
					$I=gn("id_cliente","clientes","cli_dni='$D' AND id_cliente<>$C");	
					if($I>0){
						echo "DR";return;
					}
				}else{
					echo "OK";
				}
			}
		}else{
			if(strlen($D)>0){
				$I=gn("id_cliente","clientes","cli_dni='$D' AND id_cliente<>$C");	
				if($I>0){
					echo "DR";return;
				}
			}else{
				echo "OK";
			}
		}
		*/
        echo "OK";
	}
	//
	if($StrCmd=="KON"){
		$ID=gn("ID","vis_contratos","NUM='$P1'");
		if($ID==0){
			echo "NR";
		}else{
			$DIS=gn("ACU - USA","vis_contratos","ID=$ID");
			echo $DIS;
		}
	}
	if($StrCmd=="GCON"){
		$ID=gn("ID","vis_contratos","NUM='$P1' AND EMPC='$P2'");
		if($ID==0){
			echo "NR";
		}else{
			$CL=gs("CLI","vis_contratos","ID=$ID");
			$TO=NF(gn("TOT","vis_contratos","ID=$ID"),1);
			$AD=NF(gn("ADE","vis_contratos","ID=$ID"),1);
			$TC=NF(gn("SUM(ci_total)","contratos_ingresos","ci_id_contrato=$ID")+ $AD,1);
			if(vbval($TO)==vbval($TC)){
				echo "CA";
			}else{
				echo $CL."|".$TO."|".$TC;
			}
		}
		
	}
	if($StrCmd=="LP"){
		$StrA=$P1;
		$StrC=$P2;
		$StrL="";
		$CX=0;
		$s="SELECT C_UNI 
		    FROM vis_stock_presentaciones 
			WHERE ALMC='$StrA' AND COD='$StrC' 
			GROUP BY C_UNI ORDER BY C_UNI";
		$result = grs($s);			
		while ($row = $result->fetch_assoc()){
			$StrL=$StrL.$row['C_UNI']."|";
			$CX++;
		}		
		echo $StrL;		
	}
	//
	if($StrCmd=="R05"){
		$DIR_A=trim(gs("alm_dir","almacenes","alm_obs='$P1'"));
		$DIR_B=trim(gs("alm_gdir","almacenes","alm_obs='$P1'"));
		if(strlen($DIR_B)>0){
			//
			$DEP=trim(gs("CONCAT(alm_gdep,' - ',alm_gpro,' - ',alm_gdis)","almacenes","alm_obs='$P1'"));
			//
			echo $DIR_B." ".$DEP;
		}else{
			echo $DIR_A;
		}
		return;
	}
	if($StrCmd=="V_V_E"){
/*
		$ID=vbval($P1);
		$_DR_ID=gn("id_com","ventas","id_venta=$ID");
		$_DR_IS=gn("iser","ventas","id_venta=$ID");
		$_DR_IN=gn("inum","ventas","id_venta=$ID");
		$CX=gn("count(*)","cobroz",
			   "cob_id_com=$_DR_ID AND ".
			   "cob_icser=$_DR_IS AND ".
			   "cob_icnum=$_DR_IN AND cob_anu='NO'");
		if($CX>0){
			echo "COB";
		}else{
			$CX=gn("id_guia","guias",
				   "id_comr=$_DR_ID AND ".
				   "i_serr=$_DR_IS AND ".
				   "i_numr=$_DR_IN AND anu='N'");
			if($CX==0){
				echo "OK";
			}else{
				$SER=gs("CONCAT(ser,'-',num)","guias","id_guia=$CX");
				echo $SER;
			}
		}
*/
        $ID=vbval($P1);
		$_DR_ID=gn("id_com","ventas","id_venta=$ID");
		$_DR_ID_ALM=gn("id_alm","ventas","id_venta=$ID");
		$_DR_ID_EMP=gn("alm_id_empresa","almacenes","id_almacen=$_DR_ID_ALM");
		$_DR_IS=gn("iser","ventas","id_venta=$ID");
		$_DR_IN=gn("inum","ventas","id_venta=$ID");
		//3F/4B
		if($_DR_ID==3 || $_DR_ID==4){
			$_EMI=gs("ven_fe_ge","ventas","id_venta=$ID");
			if($_EMI=="S"){
				echo "|MS|El comprobante electronico ya ha sido generado.|";
				return;
			}
			$_ANU=gs("anu","ventas","id_venta=$ID");
			if($_ANU=="S"){
				echo "|MS|El comprobante ha sido anulado|";
				return;
			}			
		}
		//
		$CX=gn("count(*)","cobroz",
			   "cob_id_com=$_DR_ID AND ".
			   "cob_icser=$_DR_IS AND ".
			   "cob_icnum=$_DR_IN AND cob_anu='NO'");
		if($CX>0){
			echo "|MS|Uno o mas cobros relacionados|";return;
		}else{
			$CX=gn("id_guia","guias",
				   "id_comr=$_DR_ID AND ".
				   "i_serr=$_DR_IS AND ".
				   "i_numr=$_DR_IN AND anu='N' AND id_emp_r=$_DR_ID_EMP ");
			if($CX==0){
				echo "|OK|OK|";return;
			}else{
				$SER=gs("CONCAT(ser,'-',num)","guias","id_guia=$CX");
				echo "|MS|El presente registro esta relacionado a : GUIA ".$SER."|";return;
			}
		}
	}
	//
	if($StrCmd=="KOT"){
		$Mar=$P3."%";
		$Cod=$P4."%";
		$LStrPag=vbval($P6);
		$StrCmd=$P7;
		$T=110+300+80+180+(gn("count(*)","almacenes","id_almacen>0")*60)+120;
		$s="<table style=\"width:$T"."px\" border=\"1\" cellpadding=\"0\" ".
		   "cellspacing=\"0\" bgcolor=\"#d4e3e5\" class=\"estilo888\" >";
		$t=$s;
		$s="";
		$cx=0;
		$s=$s."<tr >";
		$s=$s."<td  style=\"width:110px\"></td>";
		$s=$s."<td  style=\"width:300px\"></td>";
		$s=$s."<td  style=\"width:80px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";				
		//
		$z="SELECT alm_obs FROM almacenes ".
		   "WHERE id_almacen>0 ORDER BY alm_obs";
		$result = grs($z);
		while ($roww = $result->fetch_row()){		 
			$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		}
		//
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."</tr>";
		/*
		%=<td align=right>
		;=</td>
	    }=</tr>
		!=<td>
		{=<tr
		~=ondblclick
		*/
		//
		$TIE_CX=0;
		$TIE_AR[$TIE_CX]=0;
		$z="SELECT id_almacen FROM almacenes ".
		   "WHERE id_almacen>0 ORDER BY alm_obs";
		$resultt = grs($z);
		while ($roww = $resultt->fetch_row()){	
			$TIE_CX++;
			$TIE_AR[$TIE_CX]=$roww[0];
		}
		//
		$WW="id_pro>0 AND pro_cod LIKE '$Cod' AND pro_des LIKE '%$P1%' AND pro_lin LIKE '$Mar' AND ((pro_sto<>0 OR pro_prv=0 OR pro_etr>0) AND pro_uso='S')";
		$sql="SELECT pro_id as ID, pro_cod as COD,pro_pie as PIE,TRIM(LEFT(pro_des,45)) as DES,
			  TRIM(LEFT(pro_lin,35)) as LIN,
			  CONCAT('%',ROUND(pro_pmi,3),';','%',ROUND(pro_pma,3),';','%',pro_sto,';') as PMI_PMA_STO,
			  CONCAT('%',pro_sep,';','%',pro_etr,';') as SEP_TRA
			  FROM t_productos
			  WHERE $WW  ORDER BY pro_cod,pro_des ";
	    //echo $sql;
		//$sql=PagSql($sql,$LStrPag,$VP_RegXPag,$LStrTR);			
		$result = grs($sql);
		while ($row = $result->fetch_assoc()){
			  $_IDP=$row['ID'];
			  $_PIE=$row['PIE'];
			  $KEY=$row['ID'].".".$row['PIE'];
			  $s=$s."{ ~\"z('$KEY')\">";
			  $s=$s."!".$row['COD']."x".$row['PIE'].";";
			  $s=$s."!".$row['DES'].";";
			  $s=$s."!".$row['LIN'].";";
			  $s=$s.$row['PMI_PMA_STO'];
		      //VER EL DETALLE DE PROVICIONAL QUE SEA MANEJADO POR T_PRO..	
			  //$z="SELECT id_almacen FROM almacenes ".
			  //	 "WHERE id_almacen>0 ORDER BY alm_obs";
			  //$resultt = grs($z);
			  //while ($roww = $resultt->fetch_row()){
			  for($i=1;$i<=$TIE_CX;$i++){  		 
				//$STO=gn("SUM(sp_cantidad)",
				//	"stocks_presentaciones",
				//	"sp_id_producto=".$_IDP." AND sp_presentacion=".$_PIE." AND sp_id_almacen=".$TIE_AR[$i]);				
				//$ss="SELECT IF(SUM(sp_cantidad) IS NULL,0,SUM(sp_cantidad)) AS ST FROM stocks_presentaciones WHERE sp_id_producto=$_IDP AND sp_presentacion=$_PIE AND sp_id_almacen=".$TIE_AR[$i];
				
				$ss="SELECT COALESCE(SUM(sp_cantidad), 0) as ST FROM stocks_presentaciones WHERE sp_id_producto=$_IDP AND sp_presentacion=$_PIE AND sp_id_almacen=".$TIE_AR[$i];
				
				$rss = grs($ss);
				$roww = $rss->fetch_row();
				$STO= $roww[0];		 				
				$s=$s."%".$STO.";";
			  }
			  //}
			  //
			  $s=$s.$row['SEP_TRA']; 
			  $s=$s."}";
			  //
		 
		}
		if($s!=""){	
        	$s=$t.$s."</table>";		
		}		
		echo $s;
	}
	//
	if($StrCmd=="COT"){
		$Mar=$P3."%";
		$Cod=$P4."%";
		$T=110+300+80+180+(gn("count(*)","almacenes","id_almacen>0")*60)+120;
		$s="<table style=\"width:$T"."px\" border=\"1\" cellpadding=\"0\" ".
		   "cellspacing=\"0\" bgcolor=\"#d4e3e5\" class=\"estilo888\" >";
		$t=$s;
		$s="";
		$cx=0;
		$s=$s."<tr ondblclick=\"z('0')\">";
		$s=$s."<td  style=\"width:110px\"></td>";
		$s=$s."<td  style=\"width:300px\"></td>";
		$s=$s."<td  style=\"width:80px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";				
		//
		$z="SELECT alm_obs FROM almacenes ".
		   "WHERE id_almacen>0 ORDER BY alm_obs";
		$result = grs($z);
		while ($roww = $result->fetch_row()){		 
			$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		}
		//
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."<td  align=\"right\" style=\"width:60px\"></td>";
		$s=$s."</tr>";
		
		$sql="SELECT ID,COD,LEFT(DESCRIPCION,45),ROUND(PV_MIN,3),ROUND(PV_MAX,3),
			  LEFT(LINEA,35) FROM vproductos
			  WHERE COD LIKE '$Cod' AND DESCRIPCION LIKE '%$P1%' AND 
			  LINEA LIKE '$Mar' AND USO='SI'  ORDER BY COD,DESCRIPCION";
		$result = grs($sql);
		while ($row = $result->fetch_row()){		 		 
	      $sql_p="SELECT PIE ".
		       "FROM vis_sp ".
			   "WHERE ID_PRO=$row[0] AND (STOCK>0 OR PROVICIONAL=0) ORDER BY PIE";			  
		  $result_p = grs($sql_p);
		  //
		  ejt("DELETE FROM hw_pie WHERE hw='$HW'");
		  while ($row_p = $result_p->fetch_assoc()){
			  $_PIE=$row_p['PIE'];
			  ejt("INSERT INTO hw_pie VALUES ('$HW',$_PIE)");
			  
		  }
		  //OK	   
		  $z="SELECT PIE FROM vcot_control WHERE IDP=$row[0] 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()){
			  $_PIE=$row_p['PIE'];
			  if(gn("count(*)","hw_pie","pie=$_PIE AND hw='$HW'")==0){
				  ejt("INSERT INTO hw_pie VALUES ('$HW',$_PIE)");
			  }
		  }
		  //OK
		  $z="SELECT PIE FROM vven_control WHERE IDP=$row[0] 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()){
			  $_PIE=$row_p['PIE'];
			  if(gn("count(*)","hw_pie","pie=$_PIE AND hw='$HW'")==0){
				  ejt("INSERT INTO hw_pie VALUES ('$HW',$_PIE)");
			  }
		  }
		  //OK
		  $sql_p="SELECT pie ".
		         "FROM hw_pie ".
			     "WHERE hw='$HW' GROUP BY pie ORDER BY pie";			   			 
		  $result_p = grs($sql_p);
		  //	   
		  while ($row_p = $result_p->fetch_row()){
			  //
			  $_IDP=$row[0];
			  $_PIE=$row_p[0];
			  $_SEP=gn("SUM(CA_CONTROL - DEJ)","vcot_control","IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND PRI='1'") ;
			  $_SEP=$_SEP + gn("SUM(CA_CONTROL - DEJ)","vven_control","IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND PRI=1");
			  $_TRA=gn("SUM(CA)","vcom_control","IDP=$_IDP AND PIE=$_PIE AND ANU='N' AND ET='SI'") ;
			  //
			  $KEY=$row[0].".".$row_p[0];
			  $s=$s."<tr ondblclick=\"z('$KEY')\">";
			  $s=$s."<td>".$row[1]."x".$row_p[0]."</td>";
			  $s=$s."<td>".rtrim($row[2])."</td>";
			  if(strlen(trim($row[5]))==0){
				$s=$s."<td>&nbsp;</td>";  
			  }else{
			  	$s=$s."<td>".trim($row[5])."</td>";
			  }
			  $s=$s."<td  align=\"right\">".$row[3]."</td>";
			  $s=$s."<td  align=\"right\">".$row[4]."</td>";
			  $STO=gn("SUM(sp_cantidad)",
			  		  "stocks_presentaciones",
					  "sp_id_producto=$row[0] AND sp_presentacion=$row_p[0]");					  
			  $s=$s."<td  align=\"right\">".$STO."</td>";
			  
			  $z="SELECT id_almacen FROM almacenes ".
				 "WHERE id_almacen>0 ORDER BY alm_obs";
				 $resultt = grs($z);
				 while ($roww = $resultt->fetch_row()){		 
				    $STO=gn("SUM(sp_cantidad)",
			  		        "stocks_presentaciones",
					        "sp_id_producto=$row[0] AND sp_presentacion=$row_p[0] AND sp_id_almacen=$roww[0]");
					$s=$s."<td  align=\"right\" >$STO</td>";
				 }
			  $s=$s."<td  align=\"right\" >$_SEP</td>"; 
			  $s=$s."<td  align=\"right\" >$_TRA</td>"; 
			  $s=$s."</tr>";
			  
		  }
		}
		if($s!=""){
        $s=$t.$s."</table>";		
		}		
		echo $s;	
		
	}
	echo "";
?>