<?php

if(!empty($_POST)){
	$sell = SellData::getById($_POST["cotization_id"]);
	$operations = OperationData::getAllProductsBySellId($sell->id);

			$iva_val = ConfigurationData::getByPreffix("imp-val")->val;



	$sell->person_id = $_POST["person_id"]!=""?$_POST["person_id"]:"NULL";
	$sell->cashier_id = $_SESSION["user_id"];
	$sell->p_id = $_POST["p_id"];
	$sell->d_id = $_POST["d_id"];
	$sell->iva=  $iva_val;
	$sell->total = $_POST["total"];
	$sell->discount = $_POST["discount"];
	$sell->cash = $_POST["money"];

	$sell->process_cotization();

	foreach($operations as $op){
		$op->set_draft(0);
	}

///////////////////////////////////////////////////////////////
$products = OperationData::getAllProductsBySellId($sell->id);
foreach ($products as $prod) {
	$product = ProductData::getById($prod->product_id);
	if($product->use_ingredients){
		$ingredients = ProductIngredientData::getAllByProductId($prod->product_id);
		//print_r($ingredients);
		foreach($ingredients as $ing){
			$ingredient = ProductData::getById($ing->ingredient_id);
			// $q = OperationData::getQYesF($ing->ingredient_id);
			$q = OperationData::getQByStock($ing->ingredient_id,1);
			if($q>0){
				$op = new OperationData();
				$op->price_in = $ingredient->price_in;
				$op->price_out = $ingredient->price_out;
				$op->stock_id=1;
				$op->product_id = $ingredient->id ;
				$op->operation_type_id=2; // 2 - salida
				$op->sell_id=$sell->id;
				$op->q= $prod->q*$ing->q;

				$add = $op->add();			 		

			}

		}
	}
	# code...
}
///////////////////////////////////////////////////////////////

	Core::alert("Venta Procesada Exitosamente!");
	Core::redir("./?view=sells");

}


?>