
<?php
// print_r($_POST);
if(isset($_GET["opt"]) && $_GET["opt"]=="one"){
$product = ProductData::getById($_POST["product_id"]);
$operations = OperationData::getAllProductsBySellId($_POST["sell_id"]);
$found=false;
foreach($operations as $op){
	if($op->product_id==$_POST["product_id"]){
		$op->q+=$_POST["q"];
		$op->update_q();
		$found=true;
		break;
	}
}
if(!$found){
$operation = new OperationData();
$operation->stock_id =1;
$operation->price_in =$product->price_in;
$operation->price_out =$product->price_out;

$operation->product_id = $_POST["product_id"];
$operation->operation_type_id =1;
$operation->is_oficial =1;
$operation->q = $_POST["q"];
$operation->sell_id = $_POST["sell_id"];
$operation->add();
}

header("Location: index.php?view=onesell&id=$_POST[sell_id]");
}
else if(isset($_GET["opt"]) && $_GET["opt"]=="many"){

    $operations = OperationData::getAllProductsBySellId($_POST["sell_id"]);
	$products = ProductData::getFavorites();
	foreach($products as $p){
		$add = false;
		if(isset($_POST["q-".$p->id]) && $_POST["q-".$p->id]!=0){
			if($p->use_inventory){
				$q = OperationData::getQByStock($p->id,StockData::getPrincipal()->id);
				if($q>0){
					$add=true;
				}
			}else{
				$add =true;

			}
		}
		if($add){
			$found=false;
			foreach($operations as $op){
				if($op->product_id==$p->id){
					$op->q+=$_POST["q-".$p->id];
					$op->update_q();
					$found=true;
					break;
				}
			}
			if(!$found){
				$operation = new OperationData();
				$operation->stock_id =1;
				$operation->price_in =$p->price_in;
				$operation->price_out =$p->price_out;

				$operation->product_id = $p->id;
				$operation->operation_type_id =1;
				$operation->q = $_POST["q-".$p->id];
				$operation->sell_id = $_POST["sell_id"];
				$operation->add();
			}

		}
	}
	header("Location: index.php?view=onesell&id=$_POST[sell_id]");



}


?>
