<?php

if(count($_POST)>0){
  $product = new ProductData();
  $product->barcode = $_POST["barcode"];
  $product->name = $_POST["name"];
  $product->duration = $_POST["duration"];
  $product->price_in = $_POST["price_in"];
  $product->price_out = $_POST["price_out"];
  $product->description = $_POST["description"];

  $product->inventary_min= $_POST["inventary_min"]!=""?$_POST["inventary_min"]:"10";
  $product->unit_id= $_POST["unit_id"]!=""?$_POST["unit_id"]:"NULL";
  $product->presentation_id= $_POST["presentation_id"]!=""?$_POST["presentation_id"]:"NULL";
  $product->section_id= $_POST["section_id"]!=""?$_POST["section_id"]:"NULL";
  $product->category_id= $_POST["category_id"]!=""?$_POST["category_id"]:"NULL";

  $product->user_id = $_SESSION["user_id"];

$product->is_favorite = isset($_POST["is_favorite"])?1:0;
$product->use_ingredients = isset($_POST["use_ingredients"])?1:0;
$product->use_inventory = isset($_POST["use_inventory"])?1:0;

  if(isset($_FILES["image"])){
    $image = new Upload($_FILES["image"]);
    if($image->uploaded){
      $image->Process("storage/products/");
      if($image->processed){
        $product->image = $image->file_dst_name;
      }
    }  
  }
$prod = $product->add();



if($_POST["q"]!="" || $_POST["q"]!="0"){
 $op = new OperationData();
 $op->product_id = $prod[1] ;
 $op->stock_id = StockData::getPrincipal()->id;
 $op->operation_type_id=OperationTypeData::getByName("entrada")->id;
 $op->price_in =$_POST["price_in"];
 $op->price_out= $_POST["price_out"];
 $op->q= $_POST["q"];
 $op->sell_id="NULL";
$op->is_oficial=1;
$op->add();
}

print "<script>window.location='index.php?view=products';</script>";


}


?>