<?php
class Product {

    public static  function create($input)
    {

       $OPFGlobal = new OPFGlobal();
       $OPFGlobal->initOPF();

       $productcode = $input["productcode"];

       $SQL = "SELECT productid FROM vtiger_products
             INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid AND deleted='0'
             where productcode = '$productcode'";

       $result = mysqli_query($OPFGlobal->link,$SQL); 
       
        if($result){
            $rowData = mysqli_fetch_assoc($result);
            if ($rowData <> null) {
                return ['productid' => $rowData["productid"]];
            }

        }else{
            throw new Exception("Error consultando la BD ".mysqli_error($OPFGlobal->link),500);
        }

       $dataWS = array();
       $dataWS["productname"] = $input["productname"];
       $dataWS["productcode"] = $productcode;
       $dataWS["productcategory"] = $input["productcategory"];
       $dataWS["discontinued"] = $input["producteactive"] ? 0 : 1;
       $dataWS["unit_price"] = $input["unit_price"];

       ob_start();
       $productid = @$OPFGlobal->crearByWS('Products', $dataWS,'ProductosWS.log',false,true);
       $output = ob_get_clean();

       if ($output <> "") {
          throw new Exception(json_decode($productid,true)["id"]["message"],500);   
       }

       return ['productid' => $productid];

    }

    public static  function update($input)
    {

       $OPFGlobal = new OPFGlobal();
       $OPFGlobal->initOPF();

       $productcode = $input["productcode"];

       $SQL = "SELECT productid FROM vtiger_products
             INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_products.productid AND deleted='0'
             where productcode = '$productcode'";

       $result = mysqli_query($OPFGlobal->link,$SQL); 

       $productid = 0 ;
       
        if($result){
            $rowData = mysqli_fetch_assoc($result);
            if ($rowData <> null) {
                $productid = $rowData["productid"];
            }else{
                throw new Exception("El producto '$productcode' no existe",500);
            }

        }else{
            throw new Exception("Error consultando la BD ".mysqli_error($OPFGlobal->link),500);
        }

       $dataWS = array();
       $dataWS["productname"] = $input["productname"];
       $dataWS["productcode"] = $productcode;
       $dataWS["productcategory"] = $input["productcategory"];
       $dataWS["discontinued"] = $input["producteactive"] ? 0 : 1;
       $dataWS["unit_price"] = $input["unit_price"];

       ob_start();
       $productid =  @$OPFGlobal->updateWS($dataWS,14,$productid, 'ProductosWS.log', false,true);
       $output = ob_get_clean();

       if ($output <> "") {
          throw new Exception(json_decode($productid,true)["id"]["message"],500);   
       }

       
   
       return ['productid' => $productid];

    }

    public static  function getStock()
    {

       $OPFGlobal = new OPFGlobal();
       $OPFGlobal->initOPF();


       $SQL = "CALL `Stock_Total`();";

       $result = mysqli_query($OPFGlobal->link,$SQL); 

       $datResponse = array();
       
        if($result){
            
            while ($rowData = mysqli_fetch_assoc($result)) {
                $datResponse[] = $rowData;
            }

        }else{
            throw new Exception("Error consultando la BD ".mysqli_error($OPFGlobal->link),500);
        }

        return $datResponse;

    }

}
