<?php
	
	require("conexion.php");
	$tipoord = $_POST['tipoord'];
	$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'];

	$idsuc = $_POST['suc'];

	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);

	}

//PRECIO PRODUCTO
	$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '$cod'");
	if(mysqli_num_rows($sql)>0){

		$p = mysqli_fetch_array($sql);
		$cat = $p['IdCategoria'];
		$idp = $p['IdProducto'];
		

		if($cat != 3){
			if($tipo == 1){
				$precio = $p['preciovm'];
				$costof = $p['Costtot'];
			}else if($tipo == 2){
				$precio = $p['PrecioVenta'];
				$costof = $p['Costtot'];
			}
		}else if($cat == 3){
			$costo = $p['Costtot'];

			if($med == 2){
				$anchomts = ($ancho * 0.0254)+0.002;
				$largomts = ($largo * 0.0254)+0.002;
				$des = ($p['Desperdicio'])/100;
				$aream = (($largomts + $anchomts) * 2) + $des;
			}else if($med == 1){
				$anchomts = $ancho/100;
				$largomts = $largo/100;
				$des = ($p['Desperdicio'])/100;
				$aream = (($largomts + $anchomts) * 2) + $des;

			}
			if($tipo == 1){
				$pr = $p['preciovm'];
				$precio = $aream * $pr;
				$costof = $aream * $costo;
			}else if($tipo == 2){
				$pr = $p['PrecioVenta'];
				$precio = $aream * $pr;
				$costof = $aream * $costo;
			}
		}
	}
	if($tira == 1){

		$ptira = $p['PrecioTira'];
		if($ptira > 0){
		$precio = 0;
		$costof = 0;
		$ctiram = $p['Costtot'];
		if($idp != 200 && $idp != 201 && $idp != 202 && $idp != 203 && $idp != 204 && $idp != 205 && $idp != 206 && $idp != 207 && $idp != 208 && $idp != 209 && $idp != 211 && $idp != 210 && $idp != 212 && $idp != 213 && $idp != 214 && $idp != 215 && $idp != 216 ){
		$ctira = $ctiram * 2.9;
		}else{
			$ctira = $ctiram * 2.4;
		}
		}else {
			$ptira = 0;
			$costof = 0;
			$precio = 0;
			$ctira = 0;
		}
	}

	

//CRISTAL NORMAL
	$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '287'");
	$a = mysqli_fetch_array($sql);
	$sqle = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '701'");
	$e = mysqli_fetch_array($sqle);
	$preciocmf = $a['PrecioVenta'];
	$preciocmm = $a['preciovm'];
	$costo = $a['Costtot'];

	$precioef = $e['PrecioVenta'];
	$precioem = $e['preciovm'];
	$costoe = $e['Costtot'];
	if($cm == 1){
		if($tipo == 1){
			$pcm = $area * $preciocmm;
			$ccn = $area * $costo;
		}else if($tipo == 2){
			$pcm = $area * $preciocmf;
			$ccn = $area * $costo;
		}
	}else if($cm == 2){
		if($tipo == 1){
			$costo = $costo * 2;
			$preciocmm = $preciocmm * 2;
			$pcm = $area * $preciocmm;
			$ccn = $area * $costo;
		}else if($tipo == 2){
			$costo = $costo * 2;
			$preciocmf = $preciocmf *2;
			$pcm = $area * $preciocmf;
			$ccn = $area * $costo;

		}
	}else if($cm == 3){
		$pcm = 0;
	}else if($cm == 4){
		if($tipo == 1){
			$pcm = $area * $precioem;
			$ccn = $area * $costoe;
		}else if($tipo == 2){
			$pcm = $area * $precioef;
			$ccn = $area * $costoe;
		}
	}

//CRISTAL ANTIRREFLEJANTE
	$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '288'");
	$a = mysqli_fetch_array($sql);
	$preciocaf = $a['PrecioVenta'];
	$preciocam = $a['preciovm'];
	$costo = $a['Costtot'];
	if($ca == 1){
		if($tipo == 1){
			$pca = $area * $preciocam;
			$cca = $area * $costo;
		}else if($tipo == 2){
			$pca = $area * $preciocaf;
			$cca = $area * $costo;
		}
	}else{
		$pca = 0;
	}

//MARIA LUISA
	$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '289'");
	$a = mysqli_fetch_array($sql);
	$preciomlf = $a['PrecioVenta'];
	$preciomlm = $a['preciovm'];
	$costo = $a['Costtot'];

	if($ml == 1){
		if($tipo == 1){
			$pml = $area * $preciomlm;
			$cml = $area * $costo;
		}else if($tipo == 2){
			$pml = $area * $preciomlf;
			$cml = $area * $costo;
		}
	}else if($ml == 2){
		$costo = $costo * 2;
		if($tipo == 1){
			$preml = $preciomlm * 2;
			$pml = $area * $preml;
		}else if($tipo == 2){
			$preml = $preciomlf * 2;
			$pml = $area * $preml;
		}
		$cml = $area * $costo;
	}else{
		$pml = 0;
	}

//RESPALDO
	if($res == 1){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '114'");
		$a = mysqli_fetch_array($sql);
		$preciom3f = $a['PrecioVenta'];
		$preciom3m = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $preciom3m;
		}else if($tipo == 2){
			$pres = $area * $preciom3f;
		}
	}else if($res == 2){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '280'");
		$a = mysqli_fetch_array($sql);
		$preciofoamf = $a['PrecioVenta'];
		$preciofoamm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $preciofoamm;
		}else if($tipo == 2){
			$pres = $area * $preciofoamf;
		}
	}else if($res == 3){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '281'");
		$a = mysqli_fetch_array($sql);
		$preciopvcf = $a['PrecioVenta'];
		$preciopvcm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $preciopvcm;
		}else if($tipo == 2){
			$pres = $area * $preciopvcf;
		}
	}else if($res == 4){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '282'");
		$a = mysqli_fetch_array($sql);
		$precioaluf = $a['PrecioVenta'];
		$precioalum = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $precioalum;
		}else if($tipo == 2){
			$pres = $area * $precioaluf;
		}
	}else if($res == 5){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '278'");
		$a = mysqli_fetch_array($sql);
		$preciom6f = $a['PrecioVenta'];
		$preciom6m = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $preciom6m;
		}else if($tipo == 2){
			$pres = $area * $preciom6f;
		}
	}else if($res == 6){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '279'");
		$a = mysqli_fetch_array($sql);
		$preciom9f = $a['PrecioVenta'];
		$preciom9m = $a['preciovm'];
		$costo = $a['Costtot'];
		$cr = $costo * $area;

		if($tipo == 1){
			$pres = $area * $preciom9m;
		}else if($tipo == 2){
			$pres = $area * $preciom9f;
		}
	}else if($res == 0){
		$pres = 0;
	}


//PAPEL
	if($pap == 1){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '183'");
		$a = mysqli_fetch_array($sql);
		$precioevef = $a['PrecioVenta'];
		$precioevem = $a['preciovm'];
		$costo = $a['Costtot'];
		$cpap = $costo * $area;

		if($tipo == 1){
			$ppap = $area * $precioevem;
		}else if($tipo == 2){
			$ppap = $area * $precioevef;
		}
	}else if($pap == 2){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '283'");
		$a = mysqli_fetch_array($sql);
		$preciomatf = $a['PrecioVenta'];
		$preciomatm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cpap = $costo * $area;

		if($tipo == 1){
			$ppap = $area * $preciomatm;
		}else if($tipo == 2){
			$ppap = $area * $preciomatf;
		}
	}else if($pap == 3){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '284'");
		$a = mysqli_fetch_array($sql);
		$preciopref = $a['PrecioVenta'];
		$precioprem = $a['preciovm'];
		$costo = $a['Costtot'];
		$cpap = $costo * $area;

		if($tipo == 1){
			$ppap = $area * $precioprem;
		}else if($tipo == 2){
			$ppap = $area * $preciopref;
		}
	}else if($pap == 4){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '285'");
		$a = mysqli_fetch_array($sql);
		$precioartf = $a['PrecioVenta'];
		$precioartm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cpap = $costo * $area;

		if($tipo == 1){
			$ppap = $area * $precioartm;
		}else if($tipo == 2){
			$ppap = $area * $precioartf;
		}
	}else if($pap == 5){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '286'");
		$a = mysqli_fetch_array($sql);
		$precioperf = $a['PrecioVenta'];
		$precioperm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cpap = $costo * $area;

		if($tipo == 1){
			$ppap = $area * $precioperm;
		}else if($tipo == 2){
			$ppap = $area * $precioperf;
		}
	}else if($pap == 0){
		$ppap = 0;
	}

//TEXTURA
	if($text != 0){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '157'");
		$a = mysqli_fetch_array($sql);
		$preciotexf = $a['PrecioVenta'];
		$preciotexm = $a['preciovm'];
		$costo = $a['Costtot'];
		$ctext = $costo * $area;

		if($tipo == 1){
			$ptex = $area * $preciotexm;
		}else if($tipo == 2){
			$ptex = $area * $preciotexf;
		}
	}else{
		$ptex = 0;
	}


//CANVA
	if($canva == 1){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '36'");
		$a = mysqli_fetch_array($sql);
		$preciocanf = $a['PrecioVenta'];
		$preciocanm = $a['preciovm'];
		$costo = $a['Costtot'];
		$ccan = $costo * $area;

		if($tipo == 1){
			$pcan = $area * $preciocanm;
		}else if($tipo == 2){
			$pcan = $area * $preciocanf;
		}
	}else{
		$pcan = 0;
	}

//DOBLE CARA
	if($dobcar == 1){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '182'");
		$a = mysqli_fetch_array($sql);
		$preciodobf = $a['PrecioVenta'];
		$preciodobm = $a['preciovm'];
		$costo = $a['Costtot'];
		$cdc = $costo * $area;

		if($tipo == 1){
			$pdob = $area * $preciodobm;
		}else if($tipo == 2){
			$pdob = $area * $preciodobf;
		}
	}else{
		$pdob = 0;
	}

if($acri == 1){
		$sql = mysqli_query($conexion,"SELECT * FROM Productos WHERE Codigo = '290'");
		$a = mysqli_fetch_array($sql);
		$precioacrif = $a['PrecioVenta'];
		$precioacrim = $a['preciovm'];
		$costo = $a['Costtot'];
		$cacr = $costo * $area;

		if($tipo == 1){
			$pacr = $area * $precioacrim;
		}else if($tipo == 2){
			$pacr = $area * $precioacrif;
		}
	}else{
		$pacr = 0;
	}

$precio = floor($precio);
$pcm = floor($pcm);
$pca = floor($pca);
$pml =floor($pml);
$pres = floor($pres);
$maq = floor($maq);
$ppap = floor($ppap);
$ptex = floor($ptex);
$pcan = floor($pcan);
$pdob = floor($pdob);
$pacr = floor($pacr);
$ptira = floor($ptira);

$costof = floor($costof);
$ccn = floor($ccn);
$cca = floor($cca);
$cml =floor($cml);
$cr = floor($cr);
$cpap = floor($cpap);
$ctext = floor($ctext);
$ccan = floor($ccan);
$cdc = floor($cdc);
$cacr = floor($cacr);
$ctira = floor($ctira);

$preciototal = $precio + $pcm + $pca + $pml + $pres + $ppap + $ptex + $pcan + $pdob + $pacr+ $maq + $ptira;

$costototal = $costof + $ccn + $cca + $cml + $cr + $cpap + $ctext + $ccan + $cdc + $cacr + $ctira;

if($tipoord == 3){
	$preciototal =0;
	$precio = 0;
	$pcm = 0;
	$pca = 0;
	$pml = 0;
	$pres = 0;
	$ppap = 0;
	$ptex = 0;
	$pcan = 0;
	$pdob = 0;
	$pacr = 0;
	$maq = 0;
	$ptira = 0;
}

$preciofinal = floor($preciototal);
$costofinal = floor($costototal);

$preciofinal = $preciofinal * $cant;
$costofinal = $costofinal * $cant;

//if($idsuc == 3){
//	$preciofinal = $preciofinal * 1.07;
//}
echo $precio.'|'.$pcm.'|'.$pca.'|'.$pml.'|'.$pres.'|'.$ppap.'|'.$ptex.'|'.$pcan.'|'.$pdob.'|'.$pacr.'|'.$preciofinal.'|'.$costof.'|'.$ccn.'|'.$cca.'|'.$cml.'|'.$cr.'|'.$cpap.'|'.$ctext.'|'.$ccan.'|'.$cdc.'|'.$cacr.'|'.$costofinal.'|'.$ptira.'|'.$ctira;





?>