<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');
class Synchronization extends CI_Controller
{
    var $main_menu_name = "synchronization";
    var $sub_menu_name = "production_product_synchroniza";
    public function __construct()
    {
        parent::__construct();
        $this->load->model('API_Model');
        $this->load->model('Product_Models');
        $this->load->model('common_model');
        $this->load->model('Purchases_Model');
        $this->load->model('Warehouse_Model');
        $this->load->model('Stock_Counter_Model');
         $this->load->model('Stock_Transfer_Model');
        
        $this->load->model('Common_Model');
         ini_set('display_errors', 1); ini_set('display_startup_errors', 1); error_reporting(E_ALL);
        
    }
    public function index()
    {
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        $this->load->view('transfer/list_transfer_grn', $data);
    }
    
    
    public function grn_transfer()
    {
        $id=$this->input->get('id');
        //---------------------------------------------------------------
        //$url ="http://isurufc.newviableerp.com/api/transfer_grn?id=".$id;
        $url ="https://singhe-production2.vpos.verp.sites.lk/api/transfer_grn?id=".$id;
        $ch = curl_init($url);
        $headers = array(
            "Content-Type: application/json"
            );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
        $response = curl_exec($ch);
        curl_close($ch);
        //header("Content-type: application/json");
        $object = json_decode($response);
        //---------------------------------------------------------------
        $product_list=$object->products;
        $stm_no=$object->stm_no;
        $total_grn_value=0;
        $operation=1;
        $err_product_code='';
        foreach($product_list as $pd){
        $product_id=$this->Stock_Transfer_Model->get_product_id($pd->product_code);
       if($product_id==0){$operation=0; $err_product_code.=$pd->product_code." , "; }
       $total_grn_value+=($pd->product_cost*$pd->quantity);
               
        }
        if($operation==0){
            $data_return = array(
                    "status" => 0,
                    "message" => "PRODUCT CODE NOT FOUND : ".$err_product_code,
                    "err_code" => "error-400-i"
                );
                echo json_encode($data_return); 
                return false;
        }
         $data = array(
       'reference_no' => $this->Common_Model->gen_ref_number('id','purchases','GRN') ,
       'warehouse_id' => 4,
       'supplier_id'  => 2,
       'date'         => date("Y-m-d H:i:s"),
       'note'         => '',
       'total'        => $total_grn_value,
       'grand_total'  => $total_grn_value,
       'discount'     => 0,
       'discount_cal' => 0,
	   'supp_invocie_no'=>$stm_no
    );
      $last_id=$this->API_Model->save_grn_master($data);  
        foreach($product_list as $pd){
        $product_id=$this->Stock_Transfer_Model->get_product_id($pd->product_code);
          $data_array = array(
            'purchase_id' => $last_id,
            'product_name' => $pd->product_name,
            'product_code' => $pd->product_code,
            'product_id' => $product_id,
            'quantity' => $pd->quantity,
            'unit_price' => $pd->product_cost,
            'retail_price' => $pd->product_retail_pirce,
            'sub_total' => $pd->product_cost*$pd->quantity,
        );
        $this->API_Model->save_grn_items($data_array);
        }
        $data_return = array(
                    "status" => 1,
                    "message" => "SAVED!",
                    "err_code" => "error-400-i"
                );
                echo json_encode($data_return);
    }
    
    
    
    
}