<?php
include("cls_conexion.php");$CNN=new ClsMySQL();include("conexion23.php");
//
function CLIENTE_GET_ID($TIP,$DNI){
	
}
function VENTAS_DET_PARSE($FK_ID,$FK_TI,$HWN){
	$NRO=0;	
	$z="SELECT PRO_ID FROM vis_ventas_det_productos WHERE PRE>0 ORDER BY RAND() LIMIT 0,$FK_TI";
	$rz=grs($z);
	while($z=$rz->fetch_assoc()){
		$PRO_KOD=$z['PRO_ID'];
		$PRO_PRE=round(gn("MAX(pr)","ventas_det","id_producto=$PRO_KOD"),4);		
		$NRO=$NRO+1;
		$DET_TOT=gn("tot","ventas_rnd_det","id_fk=$FK_ID AND pro_nro=$NRO");
		if($PRO_PRE >=$DET_TOT){
			$GEN_PRE=$DET_TOT;
			$GEN_CAN=1;
			$GEN_PIE=1;
			echo "X01<br>";
		}else{
			$PRO_PIE=rand(1,51);//gn("MIN(can_uni)","ventas_det","id_producto=$PRO_KOD");
			if($PRO_PIE<5){
				$R=rand(1,4);
				if($R==1)$PRO_PIE=1;
				if($R==2)$PRO_PIE=2;
				if($R==3)$PRO_PIE=4;
				if($R==4)$PRO_PIE=5;
			}else{
				if($PRO_PIE>=5 && $PRO_PIE<=10){
					$PRO_PIE=5;
				}else{
					if($PRO_PIE>=11 && $PRO_PIE<=50){
						//10-20/40/50
						$R=rand(1,4);
						if($R==1)$PRO_PIE=10;
						if($R==2)$PRO_PIE=20;
						if($R==3)$PRO_PIE=40;
						if($R==4)$PRO_PIE=50;
					}else{
						$PRO_PIE=100;							
					}
				}
			}
			$GEN_PRE=round($DET_TOT / $PRO_PIE,4);
			$GEN_PIE=$PRO_PIE;
			if($GEN_PIE==100){
				$R=rand(1,3);
				if($R==1){
					$GEN_PIE=50;
					$GEN_CAN=2;
				}else{
					if($R==2){
						$GEN_PIE=25;
						$GEN_CAN=4;
					}else{
						$GEN_PIE=20;
						$GEN_CAN=5;
					}
				}					
			}else{
				$GEN_CAN=1;
			}
			echo "X02<br>";
				
		}	
		$DET_TOT_PARSE=	round($GEN_PRE * $GEN_CAN * $GEN_PIE,2);
		if($DET_TOT_PARSE>$DET_TOT){
			echo $DET_TOT_PARSE." es mayor a ".$DET_TOT."<br>";
		}else{
			if($DET_TOT_PARSE==$DET_TOT){
				echo "<b>".$DET_TOT_PARSE." = ".$DET_TOT."</b><br>";
			}else{
				echo $DET_TOT_PARSE." es menor a ".$DET_TOT."<br>";
			}
		}
		$s="UPDATE ventas_rnd_det 
			SET pro_id=$PRO_KOD,pro_pre=$GEN_PRE,pro_pie=$GEN_PIE,pro_can=$GEN_CAN,pro_tot=$DET_TOT_PARSE 
			WHERE id_fk=$FK_ID AND pro_nro=$NRO";
		echo $s."<br><br><br><br>";
		ejt($s);
	}
	$SUM_DET=gn("SUM(pro_tot)","ventas_rnd_det","id_fk=$FK_ID");
	$s="UPDATE ventas_rnd_tot SET tot_det=$SUM_DET WHERE cx=$FK_ID";
	ejt($s);
}
function VENTAS_GENERAR($EMP,$COM,$SER,$FEC,$TOT,$MIN,$MAX,$TIP,$NDI,$HWN){
	$EMP_=gn("id_emp","empresas","emp_nomc='$EMP'");
	$COM_=gn("id_comprobante","comprobantes","com_nombre='$COM'");
	$SER_=vbval($SER);
	$FEC_=ToDate($FEC);
	$TOT_=vbval($TOT);
	$MIN_=vbval($MIN);
	$MAX_=vbval($MAX);
	//
	ejt("DELETE FROM ventas_rnd_tot WHERE hwn='$HWN'");
	ejt("DELETE FROM ventas_rnd_det WHERE hwn='$HWN'");
	//
	$RND_TOT=0;
	for($i=1;$i<1000;$i++){
		$RND=rand($MIN_,$MAX_);		
		if( $RND_TOT +  $RND > $TOT_){
			break;
		}
		$CX=GenId("cx","ventas_rnd_tot");
		$s="INSERT INTO ventas_rnd_tot VALUES ($CX,$RND,'$HWN',0,0)";
		ejt($s);
		$RND_TOT=$RND_TOT + $RND;
	}
	if($TOT_ > $RND_TOT){
		$RND=round($TOT_ - $RND_TOT,1);
		$CX=GenId("cx","ventas_rnd_tot");
		$s="INSERT INTO ventas_rnd_tot VALUES ($CX,$RND,'$HWN',0)";
		ejt($s);
	}	
	//
	$s="SELECT * FROM ventas_rnd_tot WHERE hwn='$HWN' ORDER BY cx";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$VEN_KOD=$r['cx'];
		$VEN_TOT=$r['tot'];
		//
		$NRO=0;
		$DET_TOT=0;
		$DET_ITE_CAN=rand(3,5);
		$DET_ITE_PRM=floor($VEN_TOT/$DET_ITE_CAN);
		for($j=1;$j<=10000;$j++){
			$DET_ITE_RND=rand(ceil($DET_ITE_PRM/2),$DET_ITE_PRM);
			if($DET_TOT+$DET_ITE_RND > $VEN_TOT){
				break;
			}
			$NRO=$NRO+1;
			$CX=GenId("id","ventas_rnd_det");
			$s="INSERT INTO ventas_rnd_det VALUES ($CX,'$HWN',$VEN_KOD,$DET_ITE_RND,0,0,0,0,0,$NRO)";
			ejt($s);
			$DET_TOT=$DET_TOT + $DET_ITE_RND;					
		}
		if( $VEN_TOT > $DET_TOT){
			$DET_ITE_RND=round($VEN_TOT - $DET_TOT,1);
			$NRO=$NRO+1;
			$CX=GenId("id","ventas_rnd_det");
			$s="INSERT INTO ventas_rnd_det VALUES ($CX,'$HWN',$VEN_KOD,$DET_ITE_RND,0,0,0,0,0,$NRO)";
			ejt($s);
		}
		$s="UPDATE ventas_rnd_tot SET ti=$NRO WHERE cx=".$VEN_KOD;
		ejt($s);
	}
	//
	$s="SELECT * FROM ventas_rnd_tot WHERE hwn='$HWN' ORDER BY cx";
	$rs=grs($s);
	while($r=$rs->fetch_assoc()){
		$FK_ID=$r['cx'];
		$FK_TI=$r['ti'];
		VENTAS_DET_PARSE($FK_ID,$FK_TI,"");
		
	}
	//
	//
	/*
	$DET_TOT_ITEM=rand(3,10);
	$DET_TOT_ITEM_PRM=floor($VEN_TOT/$DET_TOT_ITEM);//ENTERO MENOR
	$PRO_KOD=0;
	$PRO_PRE=0;
	$z="SELECT id_producto FROM productos ORDER BY RAND() LIMIT 0,5";
	$rz=grs($z);
	while($z=$rz->fetch_assoc()){
		$PRO_KOD=$z['id_producto'];
		$PRO_PRE=gn("MAX(pr)","ventas_det","id_producto=$PRO_KOD");
		if($PRO_PRE>0){
			break;
		}			
	}
	*/
	echo "OK";
}
//
//MIN>=MAX/2
VENTAS_GENERAR("EMP","COM","001","01-01-21",50000,500,800,"","","");
?>