<?php

require_once('DF80000.php');
require_once("DI32000_CL.php");
class Invent{
	var  $tabla = '0';
	var  $nparte1 = '0';
	var  $np_desc1 = '0';
	var  $ubicacion = '0';
	var  $exi_min = '0';
	var  $exi_max = '0';
	var  $linea = '0';
	var  $gpo_basico = '0';
	var  $cve_marca = '0';
	var  $lim_vta1 = '0';
	var  $lim_vta2 = '0';
	var  $lim_vta3 = '0';
	var  $ven_esp1 = '0';
	var  $lim_1 = '0';
	var  $lim_2 = '0';
	var  $lim_3 = '0';
	var  $cve_uni_com = '0';
	var  $cve_uni_vta = '0';
	var  $afe_inv = '0';
	var  $cont_nser = '0';
	var  $cve_iva = '0';
	var  $tip_mon = '0';
	var  $cod_pro = '0';
	var  $uc_fec_doc = '0';
	var  $uc_num_doc = '0';
	var  $uc_pre_com = '0';
	var  $cos_prom = '0';
	var  $cos_prom_ant = '0';
	var  $exi_fis = '0';
	var  $exi_dis = '0';
	var  $fec_bck_ord = '0';
	var  $bck_ord = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $cod_cli = '0';
	var  $uv_fec_doc = '0';
	var  $uv_num_doc = '0';
	var  $uv_pre_com = '0';
	var  $lastInsId = '0';

	function Invent($data){
		$this->tabla='dginvent';
		$this->resetData($data);
	}

	function resetData($data){
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->np_desc1=$this->checkData($data['desPro'],'');
		$this->ubicacion=$this->checkData($data['ubiPro'],'');
		$this->exi_min=$this->checkData($data['exiMin'],'');
		$this->exi_max=$this->checkData($data['exiMax'],'');
		$this->linea=$this->checkData($data['idLineas'],'');
		$this->gpo_basico=$this->checkData($data['idGrupo'],'');
		$this->cve_marca=$this->checkData($data['idMarca'],'');
		$this->lim_vta1=$this->checkData($data['limVta1'],'');
		$this->lim_vta2=$this->checkData($data['limVta2'],'');
		$this->lim_vta3=$this->checkData($data['limVta3'],'');
		$this->ven_esp1=$this->checkData($data['venEsp1'],'');
		$this->lim_1=$this->checkData($data['lim1'],'');
		$this->lim_2=$this->checkData($data['lim2'],'');
		$this->lim_3=$this->checkData($data['lim3'],'');
		$this->cve_uni_com=$this->checkData($data['cveUniCom'],'');
		$this->cve_uni_vta=$this->checkData($data['cveUniVta'],'');
		$this->afe_inv=$this->checkData($data['afeInv'],'');
		$this->cont_nser=$this->checkData($data['conNumSer'],'');
		$this->cve_iva=$this->checkData($data['idIva'],'');
		$this->tip_mon=$this->checkData($data['tipMon'],'');
		$this->cod_pro=$this->checkData($data['idCatPro'],'');
		$this->uc_fec_doc=$this->checkData($data['ultComFecDoc'],'');
		$this->uc_num_doc=$this->checkData($data['ultComNumDoc'],'');
		$this->uc_pre_com=$this->checkData($data['ultComPreCom'],'');
		$this->cod_cli=$this->checkData($data['idCatCli'],'');
		$this->uv_fec_doc=$this->checkData($data['ultVenFecDoc'],'');
		$this->uv_num_doc=$this->checkData($data['ultVenNumDoc'],'');
		$this->uv_pre_com=$this->checkData($data['ultVenPreCom'],'');
		$this->cos_prom=$this->checkData($data['cosPro'],'');
		$this->cos_prom_ant=$this->checkData($data['cosProAnt'],'');
		$this->exi_fis=$this->checkData($data['exiFis'],'');
		$this->exi_dis=$this->checkData($data['exiDis'],'');
		$this->fec_bck_ord=$this->checkData($data['fecBckOrd'],'');
		$this->bck_ord=$this->checkData($data['bckOrd'],'');
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuario'],$_SESSION['num_usu']);
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET nparte1='$this->nparte1', np_desc1='$this->np_desc1', ubicacion='$this->ubicacion', exi_min='$this->exi_min', exi_max='$this->exi_max', linea='$this->linea', gpo_basico='$this->gpo_basico', cve_marca='$this->cve_marca', lim_vta1='$this->lim_vta1', lim_vta2='$this->lim_vta2', lim_vta3='$this->lim_vta3', ven_esp1='$this->ven_esp1', lim_1='$this->lim_1', lim_2='$this->lim_2', lim_3='$this->lim_3', cve_uni_com='$this->cve_uni_com', cve_uni_vta='$this->cve_uni_vta', afe_inv='$this->afe_inv', cont_nser='$this->cont_nser', cve_iva='$this->cve_iva', tip_mon='$this->tip_mon', cod_pro='$this->cod_pro', uc_fec_doc='$this->uc_fec_doc', uc_num_doc='$this->uc_num_doc', uc_pre_com='$this->uc_pre_com', cos_prom='$this->cos_prom', cos_prom_ant='$this->cos_prom_ant', exi_fis='$this->exi_fis', exi_dis='$this->exi_dis', fec_bck_ord='$this->fec_bck_ord', bck_ord='$this->bck_ord', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario', cod_cli='$this->cod_cli', uv_fec_doc='$this->uv_fec_doc', uv_num_doc='$this->uv_num_doc', uv_pre_com='$this->uv_pre_com'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET nparte1='$this->nparte1', np_desc1='$this->np_desc1', ubicacion='$this->ubicacion', exi_min='$this->exi_min', exi_max='$this->exi_max', linea='$this->linea', gpo_basico='$this->gpo_basico', cve_marca='$this->cve_marca', lim_vta1='$this->lim_vta1', lim_vta2='$this->lim_vta2', lim_vta3='$this->lim_vta3', ven_esp1='$this->ven_esp1', lim_1='$this->lim_1', lim_2='$this->lim_2', lim_3='$this->lim_3', cve_uni_com='$this->cve_uni_com', cve_uni_vta='$this->cve_uni_vta', afe_inv='$this->afe_inv', cont_nser='$this->cont_nser', cve_iva='$this->cve_iva', tip_mon='$this->tip_mon', cod_pro='$this->cod_pro', uc_fec_doc='$this->uc_fec_doc', uc_num_doc='$this->uc_num_doc', uc_pre_com='$this->uc_pre_com', cos_prom='$this->cos_prom', cos_prom_ant='$this->cos_prom_ant', exi_fis='$this->exi_fis', exi_dis='$this->exi_dis', fec_bck_ord='$this->fec_bck_ord', bck_ord='$this->bck_ord', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario', cod_cli='$this->cod_cli', uv_fec_doc='$this->uv_fec_doc', uv_num_doc='$this->uv_num_doc', uv_pre_com='$this->uv_pre_com' WHERE nparte1='$this->nparte1'";		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function busReg(){
		$sq_delete="SELECT * FROM $this->tabla WHERE nparte1='$this->nparte1'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;
	}

	function getDataProd(){
		$sq_delete="
			SELECT 
				count(*) as numRows, 
				nparte1 as idInvent,
				cod_var as cveProd,				
				np_desc1 as desProd,								
				exi_fis as exiFis,
				exi_dis as exiDis,
				cos_prom as cosPro,
				cos_prom_ant as cosProAnt,
				cod_pro as idCatPro,
				afe_inv as afeInv,
				lim_vta1 as preVen1,
				cve_iva as cveIva,
				uc_fec_doc as ultComFecDoc,
				uc_num_doc as ultComNumDoc,
				uc_pre_com as ultComPreCom, 
				cod_cli as idCatCli,
				uv_fec_doc as ultVenFecDoc,
				uv_num_doc as ultVenNumDoc,
				uv_pre_com as ultVenPreCom
			FROM 
				$this->tabla 
			WHERE 
				nparte1='$this->nparte1'
			GROUP BY 
				nparte1";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;	
	}
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE nparte1='$this->nparte1'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	function actDataProd(){
		$sq_update="UPDATE $this->tabla SET cod_pro='$this->cod_pro', uc_fec_doc='$this->uc_fec_doc', uc_num_doc='$this->uc_num_doc', uc_pre_com='$this->uc_pre_com', cos_prom='$this->cos_prom', cos_prom_ant='$this->cos_prom_ant', exi_fis='$this->exi_fis', exi_dis='$this->exi_dis', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario', cod_cli='$this->cod_cli', uv_fec_doc='$this->uv_fec_doc', uv_num_doc='$this->uv_num_doc', uv_pre_com='$this->uv_pre_com' WHERE nparte1='$this->nparte1'";		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;	
	} 	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}

}

class Cardex{
	var  $tabla = '0';
	var  $n_reg = '0';
	var  $nparte1 = '0';
	var  $fec_mov = '0';
	var  $tip_mov = '0';
	var  $num_fol = '0';
	var  $con_fol = '0';
	var  $almacen = '0';
	var  $mov_ent = '0';
	var  $mov_sal = '0';
	var  $cos_pro_mov = '0';
	var  $pre_comven = '0';
	var  $ex_in = '0';
	var  $ex_fi = '0';
	var  $fol_recep = '0';
	var  $num_pol = '0';
	var  $codigo = '0';
	var  $con_fec = '0';
	var  $pre_comvene = '0';
	var  $tip_cam = '0';
	var  $tip_mon = '0';
	var  $pais = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function Cardex($data){
		$this->tabla='dgcardex';
		$this->resetData($data);
	}

	function resetData($data){
		$this->n_reg=$this->checkData($data['idCardex'],'');
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->fec_mov=$this->checkData($data['fecMov'],'');
		$this->tip_mov=$this->checkData($data['tipMov'],'');
		$this->num_fol=$this->checkData($data['docMov'],'');
		$this->con_fol=$this->checkData($data['folDetMov'],'');
		$this->almacen=$this->checkData($data['idAlmacen'],'');
		$this->mov_ent=$this->checkData($data['movEnt'],'');
		$this->mov_sal=$this->checkData($data['movSal'],'');
		$this->cos_pro_mov=$this->checkData($data['cosProMov'],'');
		$this->pre_comven=$this->checkData($data['preMov'],'');
		$this->ex_in=$this->checkData($data['exIni'],'');
		$this->ex_fi=$this->checkData($data['exFin'],'');
		$this->fol_recep=$this->checkData($data['folMov'],'');
		$this->num_pol=$this->checkData($data['numPol'],'');
		$this->codigo=$this->checkData($data['codigo'],'');
		$this->con_fec=$this->checkData($data['conFec'],'');
		$this->pre_comvene=$this->checkData($data['preMov'],'');
		$this->tip_cam=$this->checkData($data['tipCam'],'');
		$this->tip_mon=$this->checkData($data['tipMon'],'');
		$this->pais=$this->checkData($data['pais'],'');
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuario'],$_SESSION['num_usu']);
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET nparte1='$this->nparte1', fec_mov='$this->fec_mov', tip_mov='$this->tip_mov', num_fol='$this->num_fol', con_fol='$this->con_fol', almacen='$this->almacen', mov_ent='$this->mov_ent', mov_sal='$this->mov_sal', cos_pro_mov='$this->cos_pro_mov', pre_comven='$this->pre_comven', ex_in='$this->ex_in', ex_fi='$this->ex_fi', fol_recep='$this->fol_recep', num_pol='$this->num_pol', codigo='$this->codigo', con_fec='$this->con_fec', pre_comvene='$this->pre_comvene', tip_cam='$this->tip_cam', tip_mon='$this->tip_mon', pais='$this->pais', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET nparte1='$this->nparte1', fec_mov='$this->fec_mov', tip_mov='$this->tip_mov', num_fol='$this->num_fol', con_fol='$this->con_fol', almacen='$this->almacen', mov_ent='$this->mov_ent', mov_sal='$this->mov_sal', cos_pro_mov='$this->cos_pro_mov', pre_comven='$this->pre_comven', ex_in='$this->ex_in', ex_fi='$this->ex_fi', fol_recep='$this->fol_recep', num_pol='$this->num_pol', codigo='$this->codigo', con_fec='$this->con_fec', pre_comvene='$this->pre_comvene', tip_cam='$this->tip_cam', tip_mon='$this->tip_mon', pais='$this->pais', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}
	
	function modCanMov(){
		$sq_update="UPDATE $this->tabla SET mov_ent='$this->mov_ent', mov_sal='$this->mov_sal', cos_pro_mov='$this->cos_pro_mov', pre_comven='$this->pre_comven', pre_comvene='$this->pre_comvene', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;	
	}
	function busReg(){
		$sq_delete="SELECT * FROM $this->tabla WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function getRegByMovFol(){
		$sq_delete="
			SELECT
				count(*) as numRows,
				$this->tabla.n_reg as idCardex,
				$this->tabla.fec_mov as fecMov,
				$this->tabla.tip_mov as tipMov,
				$this->tabla.almacen as idAlmacen,
				$this->tabla.nparte1 as idInvent,
				$this->tabla.fol_recep as folMov,
				$this->tabla.num_fol as docMov,
				$this->tabla.con_fol as folDetmov,
				$this->tabla.mov_ent as movEnt, 					
				$this->tabla.mov_sal as movSal,					
				$this->tabla.cos_pro_mov as cosProMov, 					
				$this->tabla.pre_comven as preMov 									
			FROM 
				$this->tabla 
			WHERE 
				nparte1='$this->nparte1' AND almacen='$this->almacen' AND tip_mov='$this->tip_mov' AND fol_recep='$this->fol_recep'
			GROUP BY 
				nparte1
		";
		//echo $sq_delete;
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;			
	}
	function delRegByMovFol(){
		$sq_delete="
			DELETE FROM $this->tabla 
			WHERE 
				nparte1='$this->nparte1' AND almacen='$this->almacen' AND tip_mov='$this->tip_mov' AND fol_recep='$this->fol_recep'
		";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;	
	}	
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE n_reg='$this->n_reg'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}

}

class InvAlm{
	var  $tabla = '0';
	var  $nparte1 = '0';
	var  $almacen = '0';
	var  $exi_fis_alm = '0';
	var  $can_dis_alm = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function InvAlm($data){
		$this->tabla='dginvalm';
		$this->resetData($data);
	}

	function resetData($data){
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->almacen=$this->checkData($data['idAlmacen'],'');
		$this->exi_fis_alm=$this->checkData($data['exiFis'],'');
		$this->can_dis_alm=$this->checkData($data['exiDis'],'');
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuario'],$_SESSION['num_usu']);
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET nparte1='$this->nparte1', almacen='$this->almacen', exi_fis_alm='$this->exi_fis_alm', can_dis_alm='$this->can_dis_alm', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET nparte1='$this->nparte1', almacen='$this->almacen', exi_fis_alm='$this->exi_fis_alm', can_dis_alm='$this->can_dis_alm', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE nparte1='$this->nparte1' AND almacen='$this->almacen'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}
	function actDataProd(){
		if(mysqli_num_rows($this->busRegAlm())>0){
			$sq_update="UPDATE $this->tabla SET exi_fis_alm=exi_fis_alm+$this->exi_fis_alm, can_dis_alm =can_dis_alm + $this->can_dis_alm, acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE nparte1='$this->nparte1' AND almacen='$this->almacen'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
		}
		return $this->agregar();
	}
	function busReg(){
		$sq_delete="SELECT * FROM $this->tabla WHERE nparte1='$this->nparte1'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function busRegAlm(){
		$sq_delete="SELECT * FROM $this->tabla WHERE nparte1='$this->nparte1' AND almacen='$this->almacen'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;
	}
	
	function getDataProdAlm(){
		$sq_delete="
		SELECT 
			nparte1 as idInvent,
			almacen as idAlmacen,
			exi_fis_alm as exiFis,
			can_dis_alm as exiDis 
		FROM $this->tabla 
		WHERE nparte1='$this->nparte1' AND almacen='$this->almacen'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return $rs;
		else
			return 0;
	}
	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE nparte1='$this->nparte1' AND almacen='$this->almacen'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}

}

class InvAcu{
	var  $tabla = '0';
	var  $n_reg = '0';
	var  $nparte1 = '0';
	var  $axo = '0';
	var  $mes = '0';
	var  $exi_ini = '0';
	var  $compras = '0';
	var  $dev_com = '0';
	var  $tra_ent = '0';
	var  $tra_sal = '0';
	var  $ventas = '0';
	var  $dev_ven = '0';
	var  $requi = '0';
	var  $dev_requi = '0';
	var  $exi_fin = '0';
	var  $fec_ini = '0';
	var  $fec_fin = '0';
	var  $can_mov = '0';
	var  $cos_prof = '0';
	var  $acc_fec = '0';
	var  $acc_hor = '0';
	var  $usuario = '0';
	var  $lastInsId = '0';

	function InvAcu($data){
		$this->tabla='dginvacu';
		$this->resetData($data);
	}

	function resetData($data){
		$this->n_reg=$this->checkData($data['idInvAcu'],'');
		$this->nparte1=$this->checkData($data['idInvent'],'');
		$this->axo=$this->checkData($data['axo'],'');
		$this->mes=$this->checkData($data['mes'],'');
		$this->exi_ini=$this->checkData($data['exiIni'],0);
		$this->compras=$this->checkData($data['com'],0);
		$this->dev_com=$this->checkData($data['devCom'],0);
		$this->tra_ent=$this->checkData($data['traEnt'],0);
		$this->tra_sal=$this->checkData($data['traSal'],0);
		$this->ventas=$this->checkData($data['ven'],0);
		$this->dev_ven=$this->checkData($data['devVen'],0);
		$this->requi=$this->checkData($data['req'],0);
		$this->dev_requi=$this->checkData($data['devReq'],0);
		$this->exi_fin=$this->checkData($data['exiFin'],0);
		$this->can_mov=$this->checkData($data['canMov'],0);		
		$this->fec_ini=$this->checkData($data['fecIni'],'');
		$this->fec_fin=$this->checkData($data['fecFin'],'');
		$this->cos_prof=$this->checkData($data['cosPro'],'');
		$this->acc_fec=$this->checkData($data['accFec'],date('Y-m-d'));
		$this->acc_hor=$this->checkData($data['accHor'],date('H:i:s'));
		$this->usuario=$this->checkData($data['usuario'],$_SESSION['num_usu']);
	}

	function agregar(){
		$sq_create="INSERT INTO $this->tabla SET nparte1='$this->nparte1', axo='$this->axo', mes='$this->mes', exi_ini='$this->exi_ini', compras='$this->compras', dev_com='$this->dev_com', tra_ent='$this->tra_ent', tra_sal='$this->tra_sal', ventas='$this->ventas', dev_ven='$this->dev_ven', requi='$this->requi', dev_requi='$this->dev_requi', exi_fin='$this->exi_fin', fec_ini='$this->fec_ini', fec_fin='$this->fec_fin', cos_prof='$this->cos_prof', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_create);
		if($rs){
			$this->lastInsId=mysqli_insert_id();
			return 1;
		}
		else
			return -1;
	}

	function modificar(){
		$sq_update="UPDATE $this->tabla SET nparte1='$this->nparte1', axo='$this->axo', mes='$this->mes', exi_ini='$this->exi_ini', compras='$this->compras', dev_com='$this->dev_com', tra_ent='$this->tra_ent', tra_sal='$this->tra_sal', ventas='$this->ventas', dev_ven='$this->dev_ven', requi='$this->requi', dev_requi='$this->dev_requi', exi_fin='$this->exi_fin', fec_ini='$this->fec_ini', fec_fin='$this->fec_fin', cos_prof='$this->cos_prof', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}

	function actAcuMen(){
		$sq_update="UPDATE $this->tabla SET exi_ini='$this->exi_ini', compras='$this->compras', dev_com='$this->dev_com', tra_ent='$this->tra_ent', tra_sal='$this->tra_sal', ventas='$this->ventas', dev_ven='$this->dev_ven', requi='$this->requi', dev_requi='$this->dev_requi', exi_fin='$this->exi_fin', acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE n_reg='$this->n_reg'";
		//echo $sq_update."<br>";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}
	function actPosAcuMen(){
		$sq_update="UPDATE $this->tabla SET exi_ini=exi_ini+$this->can_mov,exi_fin=exi_fin+$this->can_mov, acc_fec='$this->acc_fec', acc_hor='$this->acc_hor', usuario='$this->usuario' WHERE  nparte1='$this->nparte1' AND ((axo='$this->axo' AND mes>'$this->mes') OR axo>'$this->axo')";
		//echo $sq_update;
		$rs=mysqli_query($GLOBALS['con'] ,$sq_update);
		if($rs)
			return 1;
		else
			return 0;
	}	
	function getAcuMen($tipAcu="AM"){
		switch($tipAcu){
			case "AM":
				$where="
					WHERE 
						nparte1='$this->nparte1' AND axo='$this->axo' AND mes='$this->mes'
					GROUP BY 
						nparte1												
				";
			break;
			case "AAM":
				$where="
					WHERE 
						nparte1='$this->nparte1' AND (axo<'$this->axo' OR (axo='$this->axo' AND mes<'$this->mes'))
					GROUP BY 
						nparte1	
					ORDER BY 
						axo desc,mes desc
					LIMIT 1
				";
			break;			
			case "PAM":
				$where="
					WHERE 
						nparte1='$this->nparte1' AND (axo>'$this->axo' OR (axo='$this->axo' AND mes>'$this->mes'))
					GROUP BY 
						nparte1	
					ORDER BY 
						axo asc,mes asc
					LIMIT 1
				";
			break;	
			case "U":
				$where="
					WHERE 
						nparte1='$this->nparte1'
					GROUP BY 
						nparte1	
					ORDER BY 
						axo desc,mes desc
					LIMIT 1
				";
			break;					
		}
		$sq_retrieve="SELECT 
						count(*) as numRows,
						$this->tabla.n_reg as idInvAcu,
						$this->tabla.axo as axo,
						$this->tabla.mes as mes,						
						$this->tabla.exi_ini as exiIni,
						$this->tabla.compras as com,
						$this->tabla.dev_com as devCom,
						$this->tabla.tra_ent as traEnt,
						$this->tabla.tra_sal as traSal,
						$this->tabla.ventas as ven,
						$this->tabla.dev_ven as devVen,
						$this->tabla.requi as req,
						$this->tabla.dev_requi as devReq,
						$this->tabla.exi_fin as exiFin,
						$this->tabla.n_reg as idInvAcu,
						$this->tabla.nparte1 as idInvent
					FROM 
						$this->tabla  
					$where
					";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;
	}

	function busReg(){
		$sq_retrieve="SELECT 						
						$this->tabla.n_reg as idInvAcu,
						$this->tabla.nparte1 as idInvent,
						$this->tabla.axo as axo,
						$this->tabla.mes as mes,
						$this->tabla.exi_ini as exiIni,
						$this->tabla.compras as com,
						$this->tabla.dev_com as devCom,
						$this->tabla.tra_ent as traEnt,
						$this->tabla.tra_sal as traSal,
						$this->tabla.ventas as ven,
						$this->tabla.dev_ven as devVen,
						$this->tabla.requi as req,
						$this->tabla.dev_requi as devReq,
						$this->tabla.exi_fin as exiFin,
						$this->tabla.fec_ini as fecIni,
						$this->tabla.fec_fin as fecFin,
						$this->tabla.cos_prof as cosPro
					FROM $this->tabla 
					WHERE n_reg='$this->n_reg'";
		$rs=mysqli_query($GLOBALS['con'] ,$sq_retrieve);
		if($rs)
			return $rs;
		else
			return 0;	
	}

	function eliminar(){
		$sq_delete="DELETE FROM $this->tabla WHERE n_reg='$this->n_reg'";
		
		$rs=mysqli_query($GLOBALS['con'] ,$sq_delete);
		if($rs)
			return 1;
		else
			return 0;
	}

	
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		$auxData=htmlentities($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}

	function getLastInsId(){
		return $this->lastInsId;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}

}


/********************************************
*      CLASE PARA MANEJAR INVENTARIO        *
*********************************************/
class ManInv{
	var  $objInvent = '0';//Para Estado
	var  $objCardex = '0';
	var  $objInvAlm = '0';
	var  $objInvAcu = '0';
	var  $arrTipMov = '0';
	var $datos=array();
	var  $arrDatInvent = '0';
	var  $arrDatInvAlm = '0';	
	var  $arrDatCardex = '0';
	var  $arrDatInvAcu = '0';
	var  $tipMov = '0';
	var  $fecMov = '0';
	var  $axoMov = '0';
	var  $mesMov = '0';
	var  $idAlm = '0';
	var  $idProd = '0';
	var  $canMov = '0';
	var  $preMov = '0';//precio movimiento
	var  $folMov = '0';//Folio de Movimiento(id de registro de tabla)
	var  $docMov = '0';//Numero de Documento de Movimiento(puede ser consecutivo de venta, numero de factura,...)
	var  $folDetMov = '0';//Id de Detalle de Movimiento(de dgpememd,etc...)
	var  $cliProMov = '0';
	var  $acuOnMod = '0';
	var  $afeAlm = '0';			
	//fecMov
	//tipMov
	//tipCom ---Tipo de compra para formar clave
	//idPro
	//canMov
	//idAlmacen
	//refOPe
	//desOpe
	
	// tip='car'=>CARGO
	// tip='abo'=>ABONO
	function ManInv($datos){
		$this->objInvent=new Invent($this->datos);
		$this->objCardex=new Cardex($this->datos);
		$this->objInvAlm=new InvAlm($this->datos);
		$this->objInvAcu=new InvAcu($this->datos);
		$this->arrTipMov=array(
							"CO"=>array(
									"id"=>"CO",
									"des"=>"COMPRA",
									"polNum"=>1,									
									"fieldInvAcu"=>"com",
									"fieldCardex"=>"movEnt",
									"afeDocInv"=>true,
									"afeCosPro"=>true,
									"fieldInvFecMov"=>"ultComFecDoc",
									"fieldInvNumMov"=>"ultComNumDoc",
									"fieldInvpreMov"=>"ultComPreCom",
									"fieldInvCod"=>"idCatPro"
								),
							"CD"=>array(
									"id"=>"CD",
									"des"=>"DEVOLUCION DE COMPRA",
									"polNum"=>-1,
									"fieldInvAcu"=>"devCom",
									"fieldCardex"=>"movSal",
									"afeDocInv"=>false,
									"afeCosPro"=>true,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvpreMov"=>"",
									"fieldInvCod"=>""
								),
							"VE"=>array(
									"id"=>"VE",
									"des"=>"VENTA",
									"polNum"=>-1,									
									"fieldInvAcu"=>"ven",
									"fieldCardex"=>"movSal",
									"afeDocInv"=>true,
									"afeCosPro"=>false,
									"fieldInvFecMov"=>"ultVenFecDoc",
									"fieldInvNumMov"=>"ultVenNumDoc",
									"fieldInvpreMov"=>"ultVenPreCom",
									"fieldInvCod"=>"idCatCli"									
								),	
							"DV"=>array(
									"id"=>"DV",
									"des"=>"DEVOLUCION DE VENTA",
									"polNum"=>1,									
									"fieldInvAcu"=>"devVen",
									"fieldCardex"=>"movEnt",
									"afeDocInv"=>false,
									"afeCosPro"=>false,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvpreMov"=>"",
									"fieldInvCod"=>""
									
								),																							
							"TS"=>array(
									"id"=>"TS",
									"des"=>"TRASPASO SALIDA",
									"polNum"=>-1,									
									"fieldInvAcu"=>"traSal",
									"fieldCardex"=>"movSal",
									"afeDocInv"=>false,
									"afeCosPro"=>false,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvpreMov"=>"",
									"fieldInvCod"=>""									
								),
							"TE"=>array(
									"id"=>"TE",
									"des"=>"TRASPASO ENTRADA",
									"polNum"=>1,									
									"fieldInvAcu"=>"traEnt",
									"fieldCardex"=>"movEnt",
									"afeDocInv"=>false,
									"afeCosPro"=>false,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvpreMov"=>"",
									"fieldInvCod"=>""									
								),
							"EV"=>array(
									"id"=>"EV",
									"des"=>"ENTRADAS VARIAS",
									"polNum"=>1,									
									"fieldInvAcu"=>"devReq",
									"fieldCardex"=>"movEnt",
									"afeDocInv"=>false,
									"afeCosPro"=>true,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvpreMov"=>"",
									"fieldInvCod"=>""									
								),
							"SV"=>array(
									"id"=>"SV",
									"des"=>"SALIDAS VARIAS",
									"polNum"=>-1,									
									"fieldInvAcu"=>"req",
									"fieldCardex"=>"movSal",
									"afeDocInv"=>false,
									"afeCosPro"=>false,
									"fieldInvFecMov"=>"",
									"fieldInvNumMov"=>"",
									"fieldInvPreMov"=>"",
									"fieldInvCod"=>""									
								)																
						);
		$this->resetData($datos);
	}
	
	function resetData($data){
		$this->tipMov=$this->checkData($data['tipMov'],'');
		$this->fecMov=$this->checkData($data['fecMov'],'');		
		$this->idAlm =$this->checkData($data['idAlm'],'0');				
		$this->idProd=$this->checkData($data['idProd'],'');		
		$this->canMov=$this->checkData($data['canMov'],'');				
		$this->preMov=$this->checkData($data['preMov'],'');						
		$this->folMov=$this->checkData($data['folMov'],'');				
		$this->docMov=$this->checkData($data['docMov'],$data['folMov']);						
		$this->folDetMov=$this->checkData($data['folDetMov'],'0');								
		$this->cliProMov=$this->checkData($data['cliProMov'],'');				
		$this->afeAlm  =$this->checkData($data['afeAlm'],true);						
		$this->acuOnMod=$this->checkData($data['acuOnMod'],false);
		$axoMes=explode("-",$this->fecMov);
		$this->axoMov=1*$axoMes[0];//solo numero
		$this->mesMov=1*$axoMes[1];	//solo numero
		$this->arrTipMov[$this->tipMov]['afeCosPro']=$this->checkData($data['afeCosPro'],$this->arrTipMov[$this->tipMov]['afeCosPro']);
		$this->arrDatInvAcu['idInvent']=$this->idProd;
		$this->arrDatInvAcu['axo']=$this->axoMov;
		$this->arrDatInvAcu['mes']=$this->mesMov;
		$this->objInvAcu->resetData($this->arrDatInvAcu);

		$this->arrDatCardex['idInvent']=$this->idProd;
		$this->arrDatCardex['fecMov']=$this->fecMov;
		$this->arrDatCardex['tipMov']=$this->tipMov.'-'.$this->idAlm;
		$this->arrDatCardex['idAlmacen']=$this->idAlm;
		$this->arrDatCardex['folMov']=$this->folMov;				
		$this->arrDatCardex['docMov']=$this->docMov;						
		$this->arrDatCardex['folDetMov']=$this->folDetMov;
		$this->arrDatCardex['preMov']=$this->preMov;								
		$this->arrDatCardex[$this->arrTipMov[$this->tipMov]['fieldCardex']=$this->canMov;								
		$this->objCardex->resetData($this->arrDatCardex);
		
		$this->arrDatInvent['idInvent']=$this->idProd;
		$this->objInvent->resetData($this->arrDatInvent);
		
		$this->arrDatInvAlm['idInvent']=$this->idProd;
		$this->arrDatInvAlm['idAlmacen']=$this->idAlm;
		$this->objInvAlm->resetData($this->arrDatInvAlm);						
	}
		
	function checkData($data,$default){
		if(isset($data)) 
			$auxData=$data;
		else 
			$auxData=$default;
		if(get_magic_quotes_gpc()) 
			$auxData = stripslashes($auxData);
		return mysqli_real_escape_string($con,$auxData);
	}
	
	function valDataAdd(){//Valida Datos Minimos Necesarios Para realizar Movimientos Sobre una Cuenta
		//fecMov, tipMov, tipCom, idPro, canMov, idAlmacen, refOpe, desOpe
		if(!isset($this->idProd) || trim($this->idProd)=='')//Si no Existe Id Inventario
			return '-1';					
		if(!isset($this->arrTipMov[$this->tipMov]))//Si no Existe Tipo de Movimiento
			return '-2';
		if(!isset($this->fecMov) || trim($this->fecMov)=='')//Si No Existe Fecha Movimiento
			return '-3';
		if(!isset($this->canMov) || trim($this->canMov)=='')//Si No Existe Cantidad 
			return '-4';
		if(!isset($this->idAlm)  || trim($this->idAlm)=='')//Si No Existe Almacen 
			return '-5';
		if(!isset($this->folMov) || trim($this->folMov)=='')//Si No Existe Folio Movimiento 
			return '-6';
			
		return 1;			
	}
	function valDataDel(){//Valida Datos Minimos Necesarios Para realizar Movimientos Sobre una Cuenta
		//fecMov, tipMov, tipCom, idPro, canMov, idAlmacen, refOpe, desOpe
		if(!isset($this->idProd) || trim($this->idProd)=='')//Si no Existe Id Inventario
			return '-1';					
		if(!isset($this->arrTipMov[$this->tipMov]))//Si no Existe Tipo de Movimiento
			return '-2';
		if(!isset($this->idAlm)  || trim($this->idAlm)=='')//Si No Existe Almacen 
			return '-5';
		if(!isset($this->folMov) || trim($this->folMov)=='')//Si No Existe Folio Movimiento 
			return '-6';
			
		return 1;			
	}
	
	function addMov(){
		$auxIdProd=$this->idProd;
		$auxAxoMov=$this->axoMov;
		$auxMesMov=$this->mesMov;
		$auxCanMov=$this->canMov;
		$auxPreMov=$this->preMov;		
		$auxDataCardex=$this->arrDatCardex;
		$auxDataInvAlm=$this->arrDatInvAlm;
		
		/*Verifica Datos Principales para poder realizar movimiento*/
		if(($rsData=$this->valDataAdd())<1) return $rsData;
		/***********************************************************/
		$dt_inventPro=mysqli_fetch_array($this->objInvent->getDataProd());
		if($dt_inventPro['afeInv']!='S' && $dt_inventPro['afeInv']!='1') return 1;
		$this->regProInvent=$dt_inventPro;
		$dt_invacuAxoMes=mysqli_fetch_array($this->objInvAcu->getAcuMen("AM"));
		if($dt_invacuAxoMes['numRows']>0){//Si Existe Acumulado DE Mes Aņo
			$arrDatInvAcu=$this->actCalAcuMen($dt_invacuAxoMes);
			$this->objInvAcu->resetData($arrDatInvAcu);
			if($this->objInvAcu->actAcuMen()){//Si Actualizo Bien
				$auxDataCardex['cosProMov']=$this->calCosPro($dt_inventPro,$auxCanMov,$auxPreMov);
				$this->objCardex->resetData($auxDataCardex);	
				if(!$this->objCardex->agregar()) return '-11';
				if(($rs=$this->actDataInv())<1) return $rs;
			}else{
				return '-10';
			}
		}
		else{//No existe Acumulado
			$dt_invacuAnt=mysqli_fetch_array($this->objInvAcu->getAcuMen("AAM"));
			$arrDatInvAcu=$this->arrDatInvAcu;
			if($dt_invacuAnt['numRows']>0)
				$arrDatInvAcu['exiIni']=$dt_invacuAnt['exiFin'];
			else
				$arrDatInvAcu['exiIni']=0 ;			
			$arrDatInvAcu=$this->actCalAcuMen($arrDatInvAcu);			
			$this->objInvAcu->resetData($arrDatInvAcu);
			if($this->objInvAcu->agregar()){
				$auxDataCardex['cosProMov']=$this->calCosPro($dt_inventPro,$auxCanMov,$auxPreMov);
				$this->objCardex->resetData($auxDataCardex);	
				if(!$this->objCardex->agregar()) return '-11';
				if(($rs=$this->actDataInv())<1) return $rs;									
			}else{
				return '-14';
			}
			$dataInvAcuAju['idInvent']=$auxIdProd;
			if($dt_invacuAnt['numRows']>0){//Si Hay Anterior Debe Ser Acumulado Posterior, agrega acu faltantes.
				$auxFecIni=addMonth($dt_invacuAnt['axo'].'-'.str_pad($dt_invacuAnt['mes'],2,'0',STR_PAD_LEFT).'-01',1);
				$auxFecFin=$auxAxoMov.'-'.str_pad($auxMesMov,2,'0',STR_PAD_LEFT).'-01';
				$dataInvAcuAju['exiIni']=$dt_invacuAnt['exiFin'];
				$dataInvAcuAju['exiFin']=$dataInvAcuAju['exiIni'];
			}elseif($dt_invacuAnt['numRows']==0){//Si no hay Anterior 
				$dt_invacuPos=mysqli_fetch_array($this->objInvAcu->getAcuMen("PAM"));
				if($dt_invacuPos['numRows']>0){//Si Hay posterior Agregar Faltantes
					$auxFecIni=addMonth($auxAxoMov.'-'.str_pad($auxMesMov,2,'0',STR_PAD_LEFT).'-01',1);
					$auxFecFin=$dt_invacuPos['axo'].'-'.str_pad($dt_invacuPos['mes'],2,'0',STR_PAD_LEFT).'-01';					
					$dataInvAcuAju['exiIni']=0;		
					$dataInvAcuAju['exiFin']=0;
					//echo "POSTERIOR, CANMOV: ".$dataInvAcuAju['exiFin'];
				}else{
					$auxFecIni=addMonth($auxAxoMov.'-'.str_pad($auxMesMov,2,'0',STR_PAD_LEFT).'-01',1);				
					$auxFecFin=$auxFecIni;
				}			
			}
			//echo "Inicial: ".$auxFecIni.' '.$auxFecFin;
			while(dateCmp($auxFecFin,$auxFecIni)>0){
				$dataInvAcuAju['axo']=substr($auxFecIni,0,4);
				$dataInvAcuAju['mes']=1*substr($auxFecIni,5,2);
				$this->objInvAcu->resetData($dataInvAcuAju);
				$this->objInvAcu->agregar();
				$auxFecIni=addMonth($auxFecIni,1);				
			}
			unset($dataInvAcuAju);
		}//TERMINA No existe Acumulado Cuenta de Aņo Mes
		/*******Actualiza Agumulados Posteriores*******/
		$this->actPosAcuMen();		
		/**********************************************/
		return 1;
	}

	function delMov(){
		$auxDataCardex=$this->arrDatCardex;
		$auxDataInvAlm=$this->arrDatInvAlm;
		$auxArrTipMov =$this->arrTipMov;
		$tipMov       =$this->tipMov;
		/*Verifica Datos Principales para poder realizar movimiento*/
		if(($rsData=$this->valDataDel())<1) return $rsData;
		$dt_inventPro=mysqli_fetch_array($this->objInvent->getDataProd());
		if($dt_inventPro['afeInv']!='S' && $dt_inventPro['afeInv']!='1') return 1;
		$dt_cardexDel=mysqli_fetch_array($this->objCardex->getRegByMovFol());

		//echo mysqli_error($GLOBALS['con']);
		$this->regProInvent=$dt_inventPro;
		if($dt_cardexDel['numRows']>0){//Si Existe Cardex
			$this->idProd=$dt_cardexDel['idInvent'];
			$this->fecMov=$dt_cardexDel['fecMov'];
			$this->axoMov   =substr($this->fecMov,0,4);
			$this->mesMov   =1*substr($this->fecMov,5,2);
			$this->canMov=-1*$dt_cardexDel[$auxArrTipMov[$tipMov]['fieldCardex']];
			$this->preMov=$dt_cardexDel['preMov'];
			$auxDataInvAcu['idInvent']=$dt_cardexDel['idInvent'];
			$auxDataInvAcu['axo']     =substr($dt_cardexDel['fecMov'],0,4);
			$auxDataInvAcu['mes']     =1*substr($dt_cardexDel['fecMov'],5,2);						
			$this->objInvAcu->resetData($auxDataInvAcu);
			$dt_invacuAct=mysqli_fetch_array($this->objInvAcu->getAcuMen("AM"));
			if($dt_invacuAct['numRows']>0){//Si Existe Acumulado DE Mes Aņo			
				$arrDatInvAcu=$this->actCalAcuMen($dt_invacuAct);
				$this->objInvAcu->resetData($arrDatInvAcu);
				if($this->objInvAcu->actAcuMen()){//Si Actualizo Bien
						$auxDataCardex['idCardex']=$dt_cardexDel['idCardex'];						
						$this->objCardex->resetData($auxDataCardex);
						if($this->objCardex->eliminar()){
							 if(($rs=$this->actDataInv())<0) return $rs;
						}else{//No Elimino cardex
							return '-15';
						}
				}else{//No Actualizo Acumulado
					return '-10';
				}
			}else{//No Existe Acumulado
				return '-21';
			}
		}
		else{//No existe Cardex
			return '-20';
		}
		
		/*******Actualiza Agumulados Posteriores*******/
		$this->actPosAcuMen();
		/**********************************************/
		return 1;
	}
	
	function modMov(){
		$auxDataCardex=$this->arrDatCardex;
		$auxDataInvAlm=$this->arrDatInvAlm;
		$auxArrTipMov =$this->arrTipMov;
		$tipMov       =$this->tipMov;
		$nueCanMov    =$this->canMov;
		/*Verifica Datos Principales para poder realizar movimiento*/
		if(($rsData=$this->valDataDel())<1) return $rsData;
		$dt_inventPro=mysqli_fetch_array($this->objInvent->getDataProd());
		if($dt_inventPro['afeInv']!='S' && $dt_inventPro['afeInv']!='1') return 1;		
		$dt_cardexDel=mysqli_fetch_array($this->objCardex->getRegByMovFol());
		$this->regProInvent=$dt_inventPro;
		if($dt_cardexDel['numRows']>0){//Si Existe Cardex
			$this->idProd=$dt_cardexDel['idInvent'];
			$this->fecMov=$dt_cardexDel['fecMov'];
			$this->axoMov=substr($this->fecMov,0,4);
			$this->mesMov=1*substr($this->fecMov,5,2);
			if($this->acuOnMod)
				$auxCanMov=$nueCanMov+$dt_cardexDel[$auxArrTipMov[$tipMov]['fieldCardex']];
			else
				$auxCanMov=$nueCanMov;
			$this->canMov=$auxCanMov-$dt_cardexDel[$auxArrTipMov[$tipMov]['fieldCardex']];
			//echo "NUE MOV DIF: $nueCanMov,".$dt_cardexDel[$auxArrTipMov[$tipMov]['fieldCardex']]." DiF:".$auxCanMov;
			$this->preMov=$dt_cardexDel['preMov'];
			$auxDataInvAcu['idInvent']=$dt_cardexDel['idInvent'];
			$auxDataInvAcu['axo']     =substr($dt_cardexDel['fecMov'],0,4);
			$auxDataInvAcu['mes']     =1*substr($dt_cardexDel['fecMov'],5,2);						
			$this->objInvAcu->resetData($auxDataInvAcu);
			$dt_invacuAct=mysqli_fetch_array($this->objInvAcu->getAcuMen("AM"));
			if($dt_invacuAct['numRows']>0){//Si Existe Acumulado DE Mes Aņo			
				$arrDatInvAcu=$this->actCalAcuMen($dt_invacuAct);
				$this->objInvAcu->resetData($arrDatInvAcu);
				if($this->objInvAcu->actAcuMen()){//Si Actualizo Bien
						$auxDataCardex=$dt_cardexDel;
						$auxDataCardex[$auxArrTipMov[$tipMov]['fieldCardex']]=$auxCanMov;						
						$this->objCardex->resetData($auxDataCardex);
						if($this->objCardex->modCanMov()){
							 if(($rs=$this->actDataInv())<0) return $rs;
						}else{//No Elimino cardex
							return '-15';
						}
				}else{//No Actualizo Acumulado
					return '-10';
				}
			}else{//No Existe Acumulado
				return '-21';
			}
		}
		else{//No existe Cardex
			return '-20';
		}
		
		/*******Actualiza Agumulados Posteriores*******/
		$this->actPosAcuMen();
		/**********************************************/
		return 1;
	}
		
	function actDataInv(){
		$arrDataInv=$this->arrDatInvent;
		$arrDataInvAlm=$this->arrDatInvAlm;
		$auxCanMov=$this->canMov;
		$auxPreMov=$this->preMov;
		$auxArrTipMov=$this->arrTipMov;
		$tipMov=$this->tipMov;
		$auxFecMov=$this->fecMov;
		$auxFolMov=$this->folMov;
		$auxPreMov=$this->preMov;
		$auxCliProMov=$this->cliProMov;
		$auxAfeAlm=$this->afeAlm;
		$dt_invent=$this->regProInvent;
		$arrDataInv=$dt_invent;//Se Iguala array nuevo con el de registro para modificar posteriormente
		if($this->regProInvent['afeInv']!='S' && $this->regProInvent['afeInv']!='1') return 1;
		if($auxArrTipMov[$tipMov]['afeCosPro'] && $auxArrTipMov[$tipMov]['polNum']>0){//Si Afecta Costo Promedio
			$arrDataInv['cosPro'] =$this->calCosPro($dt_invent,$auxCanMov,$auxPreMov);	
			$arrDataInv['cosProAnt'] =$dt_invent['cosPro'];			
		}elseif($auxArrTipMov[$tipMov]['afeCosPro']){
			$arrDataInv['cosPro'] =$dt_invent['cosProAnt'];	
			$arrDataInv['cosProAnt'] ='0';					
		}
		if($auxArrTipMov[$tipMov]['afeDocInv']){//Si Afecta Datos Compra o Venta Inventario
			$arrDataInv[$auxArrTipMov[$tipMov]['fieldInvFecMov']=$auxFecMov;
			$arrDataInv[$auxArrTipMov[$tipMov]['fieldInvNumMov']=$auxFolMov;	
			$arrDataInv[$auxArrTipMov[$tipMov]['fieldInvPreMov']=$auxPreMov;
			$arrDataInv[$auxArrTipMov[$tipMov]['fieldInvCod']=$auxCliProMov;
		}
		$arrDataInv['exiFis'] +=$auxCanMov*$auxArrTipMov[$tipMov]['polNum'];
		$arrDataInv['exiDis'] +=$auxCanMov*$auxArrTipMov[$tipMov]['polNum'];
		$arrDataInvAlm['exiFis'] =$auxCanMov*$auxArrTipMov[$tipMov]['polNum'];
		$arrDataInvAlm['exiDis'] =$auxCanMov*$auxArrTipMov[$tipMov]['polNum'];
		
		$this->objInvent->resetData($arrDataInv);
		if($this->objInvent->actDataProd()){
			if($auxAfeAlm){//Si Afecta Almacen
				$this->objInvAlm->resetData($arrDataInvAlm);
				if($this->objInvAlm->actDataProd()) return 1;
				else return '-13';			
			}
			return 1;
		}else{
			return '-12';
		}
	}
	
	function calCosPro($dtInvent,$auxCanMov=0,$auxPreMov=0){
		$cosProm=$dtInvent['cosPro'];
		if($this->arrTipMov[$this->tipMov]['afeCosPro']){
			$cosProdProm=$dtInvent['exiFis']*$dtInvent['cosPro'];
			$cosProdMov=$auxCanMov*$auxPreMov;
			$nueExiProd =$dtInvent['exiFis']+$auxCanMov*$this->arrTipMov[$this->tipMov]['polNum'];
			if($nueExiProd!=0)
				$cosProm=round(($cosProdProm+$cosProdMov)/$nueExiProd,2);
			}
		return $cosProm;
	}
	
	function actCalAcuMen($arrExiDat,$nueAcu=false){
		$auxCanMov=$this->canMov;
		$auxArrTipMov=$this->arrTipMov;
		$tipMov=$this->tipMov;
		
		if($arrExiDat['exiIni']=='') $arrExiDat['exiIni']=0;
		$arrExiDat['exiFin']=$arrExiDat['exiIni'];
		$arrExiDat[$auxArrTipMov[$tipMov]['fieldInvAcu']] +=$auxCanMov; //Sumar Cantidad Producto a Campo Correspondiente		
		foreach($auxArrTipMov as $auxTipMov=>$datTipMov)//Recalcula Existencia Final
			$arrExiDat["exiFin"] +=$arrExiDat[$datTipMov['fieldInvAcu']]*$datTipMov['polNum'];
		return $arrExiDat;
	}
	
	//Actualiza Acumulados Posteriores
	function actPosAcuMen(){
		$dataInvAcuAju['idInvent']=$this->idProd;
		$dataInvAcuAju['axo']=$this->axoMov;
		$dataInvAcuAju['mes']=$this->mesMov;
		$dataInvAcuAju['canMov']=$this->canMov;
		$this->objInvAcu->resetData($dataInvAcuAju);
		if($this->objInvAcu->actPosAcuMen()) return 1;
		else return '-10';	
	}	
	//Actualiza Datos de Acumulado y recalcula Saldo Final tomando en cuenta todos los tipos de movimiento existentes en el array de movimientos de la clase
	
	function getMovError($noError){//tipos de Error retornados por las funciones.
		switch($noError){
			case '0' :
				$error="MYSQL error";
			case '-1':
				$error='Error: Es Necesario Id Producto';
			break;
			case '-2':
				$error='Error: Movimiento No Valido';
			break;
			case '-3':
				$error='Error: Sin Fecha de Movimiento';
			break;
			case '-4':
				$error='Error: Sin Cantidad a Mover';
			break;
			case '-5':
				$error="Error: Sin Almacen";
			break;
			case '-6':
				$error="Error: Sin Folio de Movimiento";
			break;
			case '-10':
				$error="SQL Error: Imposible Actualizar Acumulado Mensual";
			break;
			case '-11':
				$error="SQL Error: Imposible Agregar Cardex";
			break;
			case '-12':
				$error="SQL Error: Imposible Modificar Datos Inventario";
			break;
			case '-13':
				$error="SQL Error: Imposible Modificar Datos Almacen";
			break;
			case '-14':
				$error="SQL Error: Imposible Agregar Acumulado Mensual";
			break;
			case '-15':
				$error="SQL Error: Imposible Eliminar Cardex";
			break;

			case '-20':
				$error="DATA Error: Cardex No Valido, Verifique La Integridad de las Tablas";
			break;
			case '-21':
				$error="DATA Error: Acumulado No Valido, Verifique La Integridad de las Tablas";
			break;
			
		}
		return $error;
	}

	function getMySQLError(){
		return 'Error N:' . mysqli_errno($GLOBALS['con']) . ": " . mysqli_error($GLOBALS['con']); 
	}
}

?>