<?php
require("conexion.php");
	date_default_timezone_set('America/Mazatlan');
	$codcliente = $_POST['cliente'];
	$cant = $_POST['cant'];
	$ancho = $_POST['ancho'];
	$largo = $_POST['largo'];
	$med = $_POST['umed'];
	$cod = $_POST['cod'];

	$cm = $_POST['cm'];
	$ca = $_POST['ca'];
	$ml = $_POST['ml'];
	$res = $_POST['res'];
	$pap = $_POST['pap'];
	$text = $_POST['text'];
	$maq = $_POST['maq'];
	$canva = $_POST['canva'];
	$dobcar = $_POST['dobcar'];
	$acri = $_POST['acri'];
	$tira = $_POST['tira'];	

	$totprecio = $_POST['totprecio'];
	$totcm = $_POST['totcm'];
	$totca = $_POST['totca'];
	$totml = $_POST['totml'];
	$totres = $_POST['totres'];
	$totpap = $_POST['totpap'];
	$tottext = $_POST['tottext'];
	$totcanva = $_POST['totcanva'];
	$totdobca = $_POST['totdobca'];
	$totacrilico = $_POST['totacrilico'];
	$tottira = $_POST['tottira'];

	$costot = $_POST['costot'];
	$ccn = $_POST['ccn'];
	$cca = $_POST['cca'];
	$cml = $_POST['cml'];
	$cr = $_POST['cr'];
	$cpap = $_POST['cpap'];
	$ctext = $_POST['ctext'];
	$ccan = $_POST['ccan'];
	$cdc = $_POST['cdc'];
	$cacr = $_POST['cacr'];
	$ctot = $_POST['ctot'];
	$cttira = $_POST['cttira'];

	$obs = $_POST['obs'];
	$total = $_POST['total'];
	$totalo = $_POST['totalo'];
	$anti = $_POST['anti'];
	$dcto = $_POST['dcto'];

	$costofinal = $_POST['costofi'];
	$util = $_POST['util'];

	$user = $_SESSION['id_user'];
	$idsuc = $_POST['suc'];
	$fecha = date("Y-m-d");
function round_up ( $value, $precision ) { 
    $pow = pow ( 10, $precision ); 
    return ( ceil ( $pow * $value ) + ceil ( $pow * $value - ceil ( $pow * $value ) ) ) / $pow; 
} 

	$cli = mysqli_query($conexion,"SELECT * FROM Clientes WHERE IdCliente = '$codcliente'");
	$c = mysqli_fetch_array($cli);
	$tipo = $c['TipoCliente'];

	if($med == 2){
		$anchomts = ($ancho * 0.0254)+0.002;
		$largomts = ($largo * 0.0254)+0.002;
		/*$anchomts = round_up($anchomts,2);
		$largomts = round_up($largomts,2);*/
		$area = $anchomts * $largomts;
		$area = round_up($area,5);
	}else if($med == 1){
		$anchomts = $ancho/100;
		$largomts = $largo/100;
		$area = $anchomts * $largomts;
		$area = round_up($area,5);
	}
	if($cm ==1){
		$cris = 'CNS';
	}else if($cm == 2){
		$cris = 'CND';
	}
	if($ca == 1){
		$crisa = 'CA';
	}
	if($ml == 1){
		$mlu = 'MLS';
	}else if($ml== 2){
		$mlu = 'MLD';
	}
	if($res == 1){
		$resp = 'MACO3MM';
	}else if($res == 2){
		$resp = 'FB5MM';
	}else if($res == 3){
		$resp = 'PVC';
	}else if($res == 4){
		$resp = 'ALU';
	}else if($res == 5){
		$resp = 'MACO6MM';
	}else if($res == 6){
		$resp = 'MACO9MM';
	}

	if($pap == 1){
		$pape = 'EVE';
	}else if($pap == 2){
		$pape = 'MATE';
	}else if($pap == 3){
		$pape = 'PREM';
	}else if($pap == 4){
		$pape = 'ART';
	}else if($pap == 5){
		$pape = 'PERL';
	}

	if($text == 1){
		$te = 'PIEL';
	}else if($text == 2){
		$te = 'LINO';
	}else if($text == 3){
		$te = 'SAND';
	}

	if($canva == 1){
		$can = 'CANVA';
	}

	if($acri == 1){
		$ac = 'ACRILICO';
	}

	if($dobcar == 1){
		$do = 'DOBCARA';
	}
	$n = mysqli_query($conexion,"SELECT * FROM Productos Where Codigo = '$cod'");
	$q = mysqli_fetch_array($n);
	$nom = $q['Nombre'];
	$cat = $q['IdCategoria'];
	$costoprod = $q['Costtot'];
	if($cat != 3 && $cod != ''){
		$costot = $costoprod;
		$ctot = $costoprod * $cant;
	}

	if($tira == 1){
        $t = 'TIRA COMPLETA';
      
    }
	$ale = intval( "0" . rand(1,9) . rand(0,9) . rand(0,9) . rand(0,9) . rand(0,9)); // random(ish) 5 digit int
	$id = $cod.$ale;
	
	


	$r = mysqli_query($conexion,"INSERT INTO VentasDetalle VALUES(DEFAULT,NULL,0,'$tipo','$cant','$largo','$ancho','$med','$area','$cod','$cm','$ca','$ml','$res','$pap','$text','$canva','$acri','$dobcar','$obs','$totprecio','$totcm','$totca','$totml','$totres','$totpap','$tottext','$maq','$totcanva','$totacrilico','$totdobca','$total','$costot','$ccn','$cca','$cml','$cr','$cpap','$ctext','$ccan','$cacr','$cdc','$ctot','$user','$fecha','$idsuc',1,'$tira','$tottira','$cttira');");

	$m = mysqli_query($conexion,"INSERT INTO MovDetalle VALUES(DEFAULT,NULL,NULL,0,'$tipo','$cant','$largo','$ancho','$med','$area','$cod','$cm','$ca','$ml','$res','$pap','$text','$canva','$acri','$dobcar','$obs','$totprecio','$totcm','$totca','$totml','$totres','$totpap','$tottext','$maq','$totcanva','$totacrilico','$totdobca','$total','$costot','$ccn','$cca','$cml','$cr','$cpap','$ctext','$ccan','$cacr','$cdc','$ctot','$user','$fecha','$idsuc',1,'$tira','$tottira','$cttira');");


	//echo $tipo.' '.$cant.' '.$largo.' '.$ancho.' '.$med.' '.$area.' '.$cod.' '.$cm.' '.$ca.' '.$ml.' '.$res.' '.$pap.' '.$text.' '.$canva.' '.$acri.' '.$dobcar.' '.$obs.' '.$totprecio.' '.$totcm.' '.$totca.' '.$totml.' '.$totres.' '.$totpap.' '.$tottext.' '.$maq.' '.$totcanva.' '.$totacrilico.' '.$totdobca.' '.$total.' '.$user.' '.$fecha.' '.$idsuc;
	$totalf = $total + $totalo;
	$costofin = $ctot + $costofinal;
	$utilidad = $totalf - $costofin;
	$costofin = number_format($costofin,2,'.','');
	$utilidad = number_format($utlidad,2,'.','');
	$total = number_format($total,2,'.','');
	$totalf = number_format($totalf,2,'.','');

	/* $costofin = floor($costofin);
	$utilidad = floor($utilidad);
	$total = floor($total);
	$totalf = floor($totalf); */
	$mo = '';
	if($largo != '' && $ancho != ''){
	if($med == 1){
		$da = $largo."x".$ancho."CM";
	}else{
		$da = $largo."x".$ancho."IN";
	}
	}else{
		$da = '';
	}
	if($maq > 0){
		$mo = 'MO';
	}
	if($suc == 1){
	    $pre = 'CUL-';
	    $not = 'NV';
	    $sucn = 'SUCURSAL CULIACAN';
	}else if ($suc == 2){
	    $pre = 'MAZ-';
	    $not = 'NV';
	    $sucn = 'SUCURSAL MAZATLAN';
	}else if($suc == 3){
	    $pre = 'HER-';
	    $not = 'NV';
	    $sucn = 'SUCURSAL HERMOSILLO';
	}

	$p = $cant." * ".$nom." ".$t." ".$da." ".$cris." ".$crisa." ".$mlu." ".$resp." ".$pape." ".$te." ".$can." ".$ac." ".$do." ".$mo;
	
	$fol = mysqli_query($conexion,"SELECT * FROM VentasDetalle WHERE IdSucursal = '$idsuc'");
	if(mysqli_num_rows($fol)>0){

	    $foli = mysqli_query($conexion,"SELECT IdVentaDetalle from VentasDetalle WHERE IdSucursal = '$idsuc' order by IdVentaDetalle desc limit 1");
	    $f = mysqli_fetch_array($foli);
	    $num = $f['IdVentaDetalle'];
	}else{
	    $num = 1;
	}


	$cad = "<tr id=\"$num\"><td colspan=\"5\" >".$p."</td>
			<td >$".$total."</td><td ><input type=\"button\" id=\"$total\" value=\"Eliminar\" onclick=\"EliminarP('$num','$total','$ctot','$ale');\"></td></tr>";

			$u = $totalf - $costofin;
			$f = ($totalf * $dcto) / 100;
			$t = $totalf - $f;
			$sal = $t - $anti;
			$gan = $t - $costofin;
			/* $t = floor($t);
			$costofin = floor($costofin);
			$gan = floor($gan);
			$sal = floor($sal); */
			echo $cad.'|'.$t.'|'.$costofin.'|'.$gan.'|'.$sal.'|'.$totalf;

?>