<?php
//+++++++++++++++
$StrId=V($_POST['TxtId']);
$StrCmd=V($_POST['TxtAc']);
$KI=vbval(V($_POST['TxtKI']));
$LIMPIAR=0;
//++++
$P_C="Emp";
//++++
$Emp=V($_POST['Emp']);
$Alm=V($_POST['Alm']);
$Doc=V($_POST['Doc']);
$Aut=V($_POST['ChN_AUTO']);
$Ser=V($_POST['Ser']);
$Num=V($_POST['Num']);
$Fec=V($_POST['Fec']);
$DocM=V($_POST['DocM']);
$SerM=V($_POST['SerM']);
$NumM=V($_POST['NumM']);
$FecM=V($_POST['FecM']);
$TotM=V($_POST['TotM']);
//
$Ndi=V($_POST['Ndi']);
$Nom=V($_POST['Nom']);
$Dir=V($_POST['Dir']);
$Ema=V($_POST['Ema']);
$Tip=V($_POST['Tip']);
$Obs=V($_POST['Obs']);
//
$Kod=V($_POST['Kod']);
$Des=V($_POST['Des']);
$Pre=V($_POST['Pre']);
$Can=V($_POST['Can']);
$Pie=V($_POST['Pie']);
$Imp=V($_POST['Imp']);
//
$Sto=V($_POST['Sto']);
$Igv=V($_POST['Igv']);
$Tot=V($_POST['Tot']);
//--------------------------------------------------------------------------
if($PRO_ID_ALM==0){
	if(strlen($Emp)==0)$Emp=GE("N");
	if(strlen($Alm)==0)$Alm=GA($Emp,"N");	
}else{
	$Emp=gs("ENC","vis_almacenes","IDA=$PRO_ID_ALM");
	$Alm=gs("ANC","vis_almacenes","IDA=$PRO_ID_ALM");
}
//--------------------------------------------------------------------------
PID($StrId,V($_GET['Id']),$StrCmd,$LStrPag,V($_GET['Pag']));
//--------------------------------------------------------------------------
if(strlen($StrCmd)==0){
	$StrCmd=V($_GET['TxtAc']);
	if($StrCmd=="X"){
		$LIMPIAR=1;		
	}
}
if(strlen($StrId)>0 && $StrCmd=="AR"){
	$r=gr("vis_fe_ventaz","ID=$StrId AND COM='ND. ELEC.'");
	//	
	$Obs=gs("fv_obs","fe_ventaz","id_ven=$StrId");
	$Emp=$r['EMP_NC'];
	$Alm=$r['ALM_NC'];
	$Doc=$r['COM'];
	$Ser=$r['SER'];
	$Num=$r['NUM'];
	$Fec=$r['FEC_'];
	$DocM=$r['COM_M'];
	$SerM=$r['SER_M'];
	$NumM=$r['NUM_M'];
	$Tip=$r['TAB_X']."-".$r['CAT_DES'];
	$REF_ID=$r['ID_VEN_M'];
	$FE_ANU=$r['ANU'];
	$FE_GEN=$r['GEN'];
	//
	$rf=gr("vventas_fe","ID=$REF_ID");
	$SerM=$rf['SER'];
	$NumM=$rf['NUM'];
	$FecM=$rf['FEC_FE'];
	$TotM=NF2($rf['TOT'],1);
	$Ruc=$rf['RUC'];
	$Nom=$rf['CLI'];
	$Dnic=$rf['DNI'];
	$Dir=$rf['DIR'];
	$Ema=$rf['EMAIL'];
	if($DocM=="FACT. ELEC."){
		$Ndi=$Ruc;
	}else{
		$Ndi=$Dni;
	}
	//
	$s="DELETE FROM fe_ventas_det_te WHERE hw='$HW'";
	ejt($s);
	$s="SELECT * FROM fe_ventaz_det WHERE id_fe=$StrId ORDER BY cx";
	$rs = grs($s);
	while ($r = $rs->fetch_assoc()){
		$CX=GenId("cx","fe_ventas_det_te");
		$_TIP=$r['tip'];
		$_COD=$r['cod'];
		$_DES=$r['des'];
		$_PRE=$r['pre'];
		$_PRE_=$r['pre_'];
		$_CAN=$r['can'];
		$_CAN_=$r['can_'];
		$_IMP=$r['imp'];
		$_IMP_=$r['imp_'];
		$_OBS=$r['obs'];
		$_PIE=$r['pie'];
		$_PIE_=$r['pie_'];
		$s="INSERT INTO fe_ventas_det_te VALUES 
			($CX,'$HW','$_TIP','$_COD','$_DES',
			'$_PRE',$_PRE_,'$_CAN',$_CAN_,'$_IMP',
			$_IMP_,'$_OBS','$_PIE',$_PIE_)";			
		ejt($s);
	}
	//
	$LStrFoc="jF('$P_C');";		
}
//--------------------------------------------------------------------------
if($StrCmd=="DOC_REF"){
	//
	$Kod="";
	$Des="";
	$Pre="";
	$Can="";
	$Imp="";
	//
	$Ruc="";
	$Nom="";
	$Dir="";
	$Ema="";
	ejt("DELETE FROM fe_ventas_det_te WHERE hw='$HW'");
	//
	$SerM_=vbval($SerM);
	$NumM_=vbval($NumM);
	$REF_ID=gn("ID","vventas","EMPC='$Emp' AND COM='$DocM' AND ISER=$SerM_ AND INUM=$NumM_");
	if($REF_ID==0){
		$LStrMsg="Venta no registrada";
		$LStrFoc="document.getElementById('DocM').focus();";						
		$StrGT="S";		
	}
	if(strlen($StrGT)==0){
		$ANU=gs("ANU","vventas","ID=$REF_ID");
		if($ANU=="S"){
			$LStrMsg="El documento ha sido anulado";
			$LStrFoc="document.getElementById('DocM').focus();";						
			$StrGT="S";	
		}
	}
	if(strlen($StrGT)==0){
		$Ndi="";
		$r=gr("vventas_fe","ID=$REF_ID");
		$SerM=$r['SER'];
		$NumM=$r['NUM'];
		$FecM=$r['FEC_FE'];
		$TotM=NF2($r['TOT'],1);
		$Ruc=$r['RUC'];
		$Nom=$r['CLI'];
		$Dni=$r['DNI'];
		$Dir=$r['VEN_CLIENTE_DIR'];
		if(strlen($Dir)==0){
			$Dir=$r['DIR'];
		}
		$Ema=$r['EMAIL'];
		if($DocM=="FACT. ELEC."){
			$Ndi=$Ruc;
		}else{
			$Ndi=$Dni;
		}
		//++++++++++++
		$CX=0;
		$s="SELECT * FROM pri_ven_det WHERE ID=$REF_ID ORDER BY CX";
		$rs = grs($s);			
		while ($r = $rs->fetch_assoc()){
			$CX=GenId("cx","fe_ventas_det_te");
			$_Kod=$r['COD'];
			$_Tip="ITEM";
			$_Des=$r['DES'];
			$_Pre=round($r['PRE'],3);
			$_Pre_=vbval($_Pre);
			$_Can=$r['CAN'];
			$_Can_=vbval($_Can);
			$_Pie=$r['PIE'];
			$_Pie_=vbval($_Pie);
			$_Imp_=round($_Can_ * $_Pre_ * $_Pie_,2);
			$_Imp=$_Imp_;
			$_Obs="";
			$s="INSERT INTO fe_ventas_det_te 
				VALUES ($CX,'$HW','$_Tip','$_Kod','$_Des','$_Pre',
				$_Pre_,'$_Can',$_Can_,'$_Imp',
				$_Imp_,'$_Obs','$_Pie',$_Pie_)";
			ejt($s);
		}
	}
	$P_C="Tip";
}
//--------------------------------------------------------------------------
if($StrCmd=="KI"){
	$CX=vbval(V($_POST['TXT_KI']));
	ejt("DELETE FROM fe_ventas_det_te WHERE cx=$CX");
	$P_C="Tip";
}
//--------------------------------------------------------------------------
if($StrCmd=="ADD"){
	$Pre_=vbval($Pre);
	$Can_=vbval($Can);
	$Pie_=vbval($Pie);	
	$Imp_=vbval($Imp);
	$CX=GenId("cx","fe_ventas_det_te");
	$s="INSERT INTO fe_ventas_det_te 
		VALUES ($CX,'$HW','ITEM','$Kod','$Des','$Pre',
		$Pre_,'$Can',$Can_,'$Imp',$Imp_,'$Obs','$Pie_',$Pie_)";
	ejt($s);
	$Tip="";
	$Kod="";
	$Des="";
	$Pre="";
	$Can="";
	$Imp="";
	$Obs="";
	$Pie="";
	$P_C="Kod";
}
//--------------------------------------------------------------------------
if($StrCmd=="G" || $StrCmd=="M"){
	$Emp_=gn("id_emp","empresas","emp_nomc='$Emp'");
	$Alm_=gn("id_almacen","almacenes","alm_obs='$Alm' AND alm_id_empresa=$Emp_");
	$Com_=gn("id_comprobante","comprobantes","com_nombre='$Doc'");
	$Fec_=ToDate($Fec);
	$ComM_=gn("id_comprobante","comprobantes","com_nombre='$DocM'");
	$SerM_=vbval($SerM);
	$NumM_=vbval($NumM);
	//
	$REF_ID=gn("ID","vventas",
			   "EMPC='$Emp' AND COM='$DocM' AND ISER=$SerM_ AND INUM=$NumM_");
	$Cli_=gn("ID_CLI","vventas","ID=".$REF_ID);	
	//
	$VENTA_UBI_PAI=gs("ven_ubi_pai","ventas","id_venta=$REF_ID");
	$VENTA_UBI_DEP=gs("ven_ubi_dep","ventas","id_venta=$REF_ID");
	$VENTA_UBI_PRO=gs("ven_ubi_pro","ventas","id_venta=$REF_ID");
	$VENTA_UBI_DIS=gs("ven_ubi_dis","ventas","id_venta=$REF_ID");
	$VENTA_UBI_COD=gs("ven_ubi_cod","ventas","id_venta=$REF_ID");
	//++
	$Sto_=vbval($Sto);
	$Igv_=vbval($Igv);
	$Tot_=vbval($Tot);
	$Tip_=gn("id","fe_catalogos","TRIM(CONCAT(tab,'-',des))='$Tip' AND tab='ND' ");
}
//--------------------------------------------------------------------------
if($StrCmd=="G"){		
	if(strlen($StrGT)==0){				
		$StrId=GenId("id_ven","fe_ventaz");		
		//
		if(vbval($Aut)==1){
			NDD_GEN_SER_NUM($Emp_,$Com_,$Ser,$Num);
		}
		$Ser_=vbval($Ser);
		$Num_=vbval($Num);
		$Anu="N";
		$Gen="N";
		$Usu_=vbval(V($_SESSION['SYS_USER_ID']));
		$HASH="";
		//
		$s="INSERT INTO fe_ventaz VALUES 
			($StrId,$Emp_,$Alm_,$Com_,'$Ser','$Num',$Ser_,$Num_,
			'$Fec_',$ComM_,'$SerM',$SerM_,'$NumM',$NumM_,
			$REF_ID,$Cli_,'$Ndi','$Nom','$Dir',
			'$Ema','$Anu','$Gen',$Usu_,$Sto_,$Igv_,$Tot_,$Tip_,'$HASH','$Obs',
		'$FE_SUNAT_ZIP','$FE_SUNAT_SER','$FE_SUNAT_NUM',
		'$VENTA_UBI_PAI','$VENTA_UBI_DEP','$VENTA_UBI_PRO','$VENTA_UBI_DIS','$VENTA_UBI_COD')";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="nota_de_debito.php";
		$_BIT_ID=BIT_ADD("VENTAS","NOTA DE DEBITO","GUARDAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$qqq=ejt("DELETE FROM fe_ventaz_det WHERE id_fe=$StrId");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$s="SELECT * FROM fe_ventas_det_te WHERE hw='$HW' ORDER BY cx";
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$DET_ID=GenId("cx","fe_ventaz_det");
			$_TIP=$r['tip'];
			$_COD=$r['cod'];
			$_DES=$r['des'];
			$_PRE=$r['pre'];
			$_PRE_=$r['pre_'];
			$_CAN=$r['can'];
			$_CAN_=$r['can_'];
			$_IMP=$r['imp'];
			$_IMP_=$r['imp_'];
			$_OBS=$r['obs'];
			$_PIE=$r['pie'];
			$_PIE_=$r['pie_'];
			$s="INSERT INTO fe_ventaz_det VALUES 
				($DET_ID,$StrId,'$_TIP','$_COD','$_DES',
				'$_PRE',$_PRE_,'$_CAN',$_CAN_,'$_IMP',$_IMP_,'$_OBS','$_PIE',$_PIE_)";			
			ejt($s);
			BIT_DET_ADD($_BIT_ID,$s);
		}
		//
		$LStrMsg="Datos guardados";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";	
	}
}
//--------------------------------------------------------------------------
if($StrCmd=="M"){
	if(strlen($StrGT)==0){						
		//
		$Usu_=vbval(V($_SESSION['SYS_USER_ID']));
		//
		$Ser_=vbval($Ser);
		$Num_=vbval($Num);
		//
		$s="UPDATE fe_ventaz SET
			fv_id_emp=$Emp_,fv_id_alm=$Alm_,fv_id_com=$Com_,
			fv_ser='$Ser',fv_num='$Num',fv_ser_=$Ser_,
			fv_num_=$Num_,fv_fec='$Fec_',fv_r_id_com=$ComM_,
			fv_r_ser='$SerM',fv_r_ser_=$SerM_,fv_r_num='$NumM',
			fv_r_num_=$NumM_,fv_r_id_ven=$REF_ID,
			fv_id_cli=$Cli_,fv_ndi='$Ndi',fv_cli='$Nom',
			fv_dir='$Dir',fv_ema='$Ema',fv_id_usu=$Usu_,
			fv_st=$Sto_,fv_igv=$Igv_,fv_tot=$Tot_,
			fe_id_tip=$Tip_ ,fv_obs='$Obs',
			dat_ubi_pai='$VENTA_UBI_PAI',
			dat_ubi_dep='$VENTA_UBI_DEP',
			dat_ubi_pro='$VENTA_UBI_PRO',
			dat_ubi_dis='$VENTA_UBI_DIS',
			dat_ubi_cod='$VENTA_UBI_COD'
			WHERE id_ven=$StrId";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="nota_de_debito.php";
		$_BIT_ID=BIT_ADD("VENTAS","NOTA DE DEBITO","MODIFICAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$qqq=ejt("DELETE FROM fe_ventaz_det WHERE id_fe=$StrId");
		BIT_DET_ADD($_BIT_ID,$qqq);
		//
		$s="SELECT * FROM fe_ventas_det_te WHERE hw='$HW' ORDER BY cx";
		$rs = grs($s);
		while ($r = $rs->fetch_assoc()){
			$DET_ID=GenId("cx","fe_ventaz_det");
			$_TIP=$r['tip'];
			$_COD=$r['cod'];
			$_DES=$r['des'];
			$_PRE=$r['pre'];
			$_PRE_=$r['pre_'];
			$_CAN=$r['can'];
			$_CAN_=$r['can_'];
			$_IMP=$r['imp'];
			$_IMP_=$r['imp_'];
			$_OBS=$r['obs'];
			$_PIE=$r['pie'];
			$_PIE_=$r['pie_'];
			$s="INSERT INTO fe_ventaz_det VALUES 
				($DET_ID,$StrId,'$_TIP','$_COD','$_DES',
				'$_PRE',$_PRE_,'$_CAN',$_CAN_,'$_IMP',$_IMP_,'$_OBS','$_PIE',$_PIE_)";				
			ejt($s);
			BIT_DET_ADD($_BIT_ID,$s);
		}
		//
		$LStrMsg="Datos actualizados";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";	
	}	
}
//--------------------------------------------------------------------------
if($StrCmd=="A"){
	$ANU=gs("fv_anu","fe_ventaz","id_ven=$StrId");
	if($ANU=="S"){
		$LStrMsg="El documento ya se encuentra anulado por lo tanto no se puede volver a anular";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";		
	}else{
		$s="UPDATE fe_ventaz SET fv_anu='S' WHERE id_ven=$StrId";
		ejt($s);		
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="nota_de_debito.php";
		$_BIT_ID=BIT_ADD("VENTAS","NOTA DE DEBITO","ANULAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$LStrMsg="Datos anulados";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";	
		$LIMPIAR=1;
	}
}
//--------------------------------------------------------------------------
if($StrCmd=="E"){	
	$GEN=gs("fv_gen","fe_ventaz","id_ven=$StrId");
	if($GEN=="S"){
		$LStrMsg="El documento ya ha sido generado por lo tanto no se puede eliminar";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";		
	}else{
		//
		$s="DELETE FROM fe_ventaz_det WHERE id_fe=$StrId";
		ejt($s);
		//BIT
		$_BIT_MOD_ID=$StrId;
		$_BIT_SQL_CAB=$s;
		$_BIT_SQL_DET="";
		$_BIT_PHP="nota_de_debito.php";
		$_BIT_ID=BIT_ADD("VENTAS","NOTA DE DEBITO","ELIMINAR",$_BIT_MOD_ID,$_BIT_SQL_CAB,$_BIT_SQL_DET,$_BIT_PHP);
		BIT_DET_ADD($_BIT_ID,$s);
		//BIT	
		$s="DELETE FROM fe_ventaz WHERE id_ven=$StrId";
		ejt($s);	
		BIT_DET_ADD($_BIT_ID,$s);
		//
		$LStrMsg="Datos eliminados";		
		$LStrFoc="jF('$P_C');";
		$StrGT="S";	
		$LIMPIAR=1;
	}
}
//--------------------------------------------------------------------------
if($StrCmd=="L"){
	$LIMPIAR=1;
}
//--------------------------------------------------------------------------
if($LIMPIAR==1){
	$StrId="";
	$Ser="";
	$Num="";
	$Fec=date("d-m-y");
	$DocM="";
	$SerM="";
	$NumM="";
	$FecM="";
	$TotM="";
	$Ndi="";
	$Nom="";
	$Dir="";
	$Ema="";
	$Kod="";
	$Des="";
	$Pre="";
	$Can="";
	$Imp="";
	$Tip="";
	$Obs="";
	//
	$s="DELETE FROM fe_ventas_det_te WHERE hw='$HW'";
	ejt($s);
}
//--------------------------------------------------------------------------
$LStrMsg=BL($StrGT,$LStrMsg,$LStrFoc,$P_C);
?>
