<?php
	session_start();
	include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
	//+++++
	$CMD=V($_GET['CMD']);
	$P01=V($_GET['P01']);
	$P02=V($_GET['P02']);
	$P03=V($_GET['P03']);
	$P04=V($_GET['P04']);
	$P05=V($_GET['P05']);
	$P06=V($_GET['P06']);
	$P07=V($_GET['P07']);	
	$P08=V($_GET['P08']);	
	$P09=V($_GET['P09']);	
	$P10=V($_GET['P10']);	
	$P11=V($_GET['P11']);	
	$P12=V($_GET['P12']);	
	$P13=V($_GET['P13']);	
	$P14=V($_GET['P14']);	
	$P15=V($_GET['P15']);	
	$P16=V($_GET['P16']);	
	$P17=V($_GET['P17']);
	$P18=V($_GET['P18']);
	$P19=V($_GET['P19']);
	$P20=V($_GET['P20']);
	//++
	
	//++
	if($CMD=="PER_G"){
		$PER=trim($P01);
		$r=gr("ges_periodos","per_nom='$PER'");
		$T1=$r['per_tex1'];
		$T2=$r['per_tex2'];
		echo "|OK|$T1|$T2|";return;
	}
	//++
	if($CMD=="INV_DET_EDI2"){
		//pid,pie,caj,und,tot_pie,tot_uni,tot_uwi,pre,imp,hwn
		$PID=vbval($P01);
		$PIE=vbval($P02);
		$CAJ=vbval($P03);
		$UND=gn("id_und","ges_unidades","und_abr='$P04'");
		$TOT_PIE=vbval($P05);
		$TOT_UNI=vbval($P06);
		$TOT_UWI=vbval($P07);
		$PRE=vbval($P08);
		$IMP=vbval($P09);
		$HWN=trim($P10);
		$COD=trim($P11);
		$DES=trim($P12);
		//
		$TMP_PID=gn("dt_id_pro","ges_invoice_det_t","cx=$PID");
		$P_COD=gs("pro_cod","ges_productos","id_pro=$TMP_PID");
		$P_DES=gs("pro_des","ges_productos","id_pro=$TMP_PID");
		//
		if(strlen($P_COD)==0 || strlen($P_DES)==0){
			if(strlen($COD)>0 && strlen($DES)>0){
				$TPX=gn("count(*)","ges_productos","pro_cod='$COD' AND pro_des='$DES' AND id_pro<>$TMP_PID");
				if($TPX>0){
					echo "|MS|Codigo y descripcion ya registrados|";return;	
				}else{
					$s="UPDATE ges_productos SET pro_cod='$COD',pro_des='$DES' WHERE id_pro=$TMP_PID";
					ejt($s);
				}
			}else{
				echo "|MS|El item a modificar debe tener codigo y descripcion|";return;
			}
		}
		//		
		$s="UPDATE ges_invoice_det_t 
			SET 
			dt_pie=$PIE,
			dt_caj=$CAJ,
			dt_id_und=$UND,
			dt_tot_pie=$TOT_PIE,
			dt_tot_uni=$TOT_UNI,
			dt_tot_uwi=$TOT_UWI,
			dt_pre=$PRE,
			dt_imp=$IMP	WHERE cx=$PID ";
		ejt($s);
		echo "|OK|OK|";return;
	}
	//++
	if($CMD=="INV_DET_EDI"){
		$PID=vbval($P01);
		$PRE=vbval($P02);
		$PIE=vbval($P03);
		$CAJ=vbval($P04);
		$PIE_TOT=vbval($P05);
		$IMP=vbval($P06);
		$HWN=trim($P07);
		
		$s="UPDATE ges_invoice_det_t 
			SET 
			dt_pre=$PRE,
			dt_pie=$PIE,
			dt_caj=$CAJ,
			dt_tot_pie=$PIE_TOT,
			dt_imp=$IMP WHERE cx=$PID ";
		ejt($s);
		echo "|OK|OK|";return;
	}
	//++
	if($CMD=="PRV_GD"){
		$PRV=trim($P01);
		$r=gr("ges_proveedores","prv_nom='$PRV'");
		$KOD=$r['id_prv'];
		$NOM=$r['prv_nom'];
		$ORI=$r['prv_ori'];
		echo "|OK|$KOD|$NOM|$ORI|";return;
	}
	//++
	if($CMD=="PRV_GSA"){
		$PRV=trim($P01);
		$COD=vbval($P02);
		$PRV_ORI=trim($P03);
		$PRV_NUM=trim($P04);
		//
		$r=gr("ges_proveedores","prv_nom='$PRV'");
		$KOD=$r['id_prv'];
		$SER=$r['prv_serie'];
		$ANI=$r['prv_ani'];
		$AGE=$r['prv_age'];
		$BAN=$r['prv_ban'];
		$BANDIR=$r['prv_ban_dir'];
		$SWI=$r['prv_swi'];
		$EXP_AGE=$r['prv_exp_age'];
		$SER_ANI=$SER.$ANI;
		$NRO=$r['prv_nro'];
		$CX=gn("count(*)","ges_invoice","in_id_prv=".$KOD);
		if($COD==0){
			if($CX==0){
				$NRO=$NRO+1;
				$NRO=vbr("000000".$NRO,6);
			}else{
				$MAX=gn("MAX(id_in)","ges_invoice","in_id_prv=".$KOD);
				$NRO=gn("in_pro_num_","ges_invoice","id_in=".$MAX);
				$NRO=$NRO+1;
				$NRO=vbr("000000".$NRO,6);
			}
		}else{
			if($PRV==$PRV_ORI){				
				$PRV_NUM=vbval($PRV_NUM)*1;
				$NRO=vbr("000000".$PRV_NUM,6);
			}else{
				if($CX==0){
					$NRO=$NRO+1;
					$NRO=vbr("000000".$NRO,6);
				}else{
					$MAX=gn("MAX(id_in)","ges_invoice","in_id_prv=".$KOD);
					$NRO=gn("in_pro_num_","ges_invoice","id_in=".$MAX);
					$NRO=$NRO+1;
					$NRO=vbr("000000".$NRO,6);
				}
			}
		}
		echo "|OK|$SER_ANI|$EXP_AGE|$AGE|$BAN|$BANDIR|$SWI|$ANI|$NRO|";return;
	}
	//++
	if($CMD=="EMPC_GD"){
		$EMP=trim($P01);
		$r=gr("ges_empresas","emp_nom_cor='$EMP'");
		$KOD=$r['id_emp'];
		$NOM=$r['emp_nom_com'];
		$NOW=$r['emp_nom_cor'];
		$RUC=$r['emp_ruc'];
		$DIR=$r['emp_dir'];
		echo "|OK|$KOD|$NOM|$NOW|$RUC|$DIR|";return;
	}
	//++
	if($CMD=="GET_PRO_COD"){
		$COD=trim($P01);
		$CX=gn("count(*)","vis_ges_productos","COD='$COD'");
		if($CX==0){
			echo "|MS|Codigo no registrado|";return;
		}else{
			$r=gr("vis_ges_productos","COD='$COD'");
			$KOD=$r['ID'];
			$COD=$r['COD'];
			$DES=$r['DES'];
			$PRE=$r['PRE'];
			echo "|OK|".$KOD."|".$COD."|".$DES."|".$PRE."|";return;
		}
	}
	//++
	if($CMD=="INVOICE_GET_PRO"){
		$KOD=vbval($P01);
		$r=gr("vis_ges_productos","ID=$KOD");
		$KOD=$r['ID'];
		$COD=$r['COD'];
		$DES=$r['DES'];
		$PRE=$r['PRE'];
		$UND=$r['UND'];
		$MED=$r['MEDIDAS'];
		if($UND=="-")$UND="";
		echo "|OK|".$KOD."|".$COD."|".$DES."|".$PRE."|".$UND."|".$MED."|";return;
	}
	//++
	if($CMD=="INVOICE_LISTAR"){
		$COD="%".$P01."%";
		$DES=$P02;
		//
		$s='<table width="1080" border="1" 
			cellpadding="0" cellspacing="0"  style="table-layout:fixed">';
		$t=$s;
		$s="";
		//
		$FF=TRIW($DES);
		$LL=explode(" ",$FF);
		$WW="";
		if(count($LL)>0){
			for($i=0;$i<count($LL);$i++){
				if(strlen(trim($LL[$i]))>0){
					$WW=$WW." DES LIKE '%".$LL[$i]."%' AND";
				}
			}
		}
		//
		if(strlen($WW)>0){
			$WW="(".vbmid($WW,1,strlen($WW)-3).")";
		}else{
			$WW="DES LIKE '%".$DES."%'";
		}
		//											
		$sql="SELECT CONCAT('@ ',' * ',':','z(',ID,')>') as C_ID,							
					 CONCAT('%',COD,';') as C_CO,														
					 CONCAT('%',TRIM(LEFT(DES,45)),';') as C_DE,
					 CONCAT('%',TRIM(LEFT(MEDIDAS,45)),';') as C_ME,						
					 CONCAT('%',ROUND(PRE,3),';') as C_PR,
					 CONCAT('%',TRIM(LEFT(UND,45)),';') as C_UN
					 FROM vis_ges_productos WHERE $WW AND COD Like '$COD' ORDER BY DES,COD";								
		$rs=grs($sql);
		$cx=0;
		//
		$s=$s.'<tr id="z0" ondblclick="z(0)" tabindex="0">';
		$s=$s.'<td  width="128px"></td>';
		$s=$s.'<td  width="400px"></td>';
		$s=$s.'<td  width="100px"></td>';
		$s=$s.'<td  width="55px"></td>';
		$s=$s.'<td  width="40px"></td>';
		$s=$s.'<td  width="40px"></td>';
	    $s=$s.'</tr>';
		//
		while($r=$rs->fetch_assoc()){
			$cx++;
		  	$F='id="z'.$cx.'" tabindex="'.$cx.'" ';
		  	$s=$s.str_replace("*",$F,$r['C_ID']).$r['C_CO'].$r['C_DE'].$r['C_ME'].$r['C_UN'].$r['C_PR'];        	 
		}
		if($s!=""){
	        $s=$t.$s."</table>";		
		}		
		echo $s;	
	}
	
?>
