<?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('Production_Process_Model');
        $this->load->model('Admin_Request_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://catering.fab.test.newviableerp.com/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);
    }
    
    public function get_admin_request_data()
    {
        $id=$this->input->get('id');
        //---------------------------------------------------------------
        $url ="https://admin.fab.test.newviableerp.com/api/get_bulk_order_items?id=".$id;
        //$url ="http://plan.lakalibh.newviableerp.com/api/transfer_grn/".$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->ref_no;
        $total_grn_value=0;
        $operation=1;
        $err_product_code='';
        foreach($product_list as $pd){
        $ref_no=$pd->ref_no;
        $product_id=$this->Admin_Request_Model->get_product_id($pd->product_code);
        if($product_id==0){$operation=0; $err_product_code.=$pd->product_code." , "; }
        }
        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(
			'brn_from_warehouse_id'=>4,
			'brn_to_warehouse_id'=>4,
			'brm_manual_ref_no'=>$ref_no,
			'brm_ref_no'=>$ref_no,
			'added_user_id'=>$this->session->userdata('ss_user_id'),
			'added_date_time'=>date("Y-m-d H:i:s"),
		);	
         $result = $this->Production_Process_Model->insert_bulk_request_master_table($data);
        $last_id=$result;
        foreach($product_list as $pd){
        $product_id=$this->Admin_Request_Model->get_product_id($pd->product_code);
        $bri_note="";
        if(isset($pd->product_note)){
           $bri_note =$pd->product_note;
        }
        
         $data=array(
			'brm_id'=>$last_id,
			'product_id'=>$product_id,
			'request_qty'=>$pd->bulk_qty,	
			'approved_qty'=>$pd->bulk_qty,	
			'bri_note'=>$bri_note,
		);	
         $result=$this->Production_Process_Model->save_bulk_requste_master_table($data);	  
        }
        $data_return = array(
                    "status" => 1,
                    "message" => "SAVED!",
                    "err_code" => "error-400-i"
                );
                echo json_encode($data_return);
    }
    
    function tranfer_grn_rm(){
        $data['main_menu_name'] = $this->main_menu_name;
        $data['sub_menu_name']  = $this->sub_menu_name;
        $this->load->view('transfer_m/list_transfer_grn', $data); 
    }
    
    public function grn_m_transfer()
    {
        $this->load->model('Stock_M_Transfer_Model');
        $id=$this->input->get('id');
        //---------------------------------------------------------------
        //$url ="http://isurufc.newviableerp.com/api/transfer_grn?id=".$id;
        $url ="https://catering.fab.test.newviableerp.com/api/transfer_grn_m?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_M_Transfer_Model->get_product_mat_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_M_Transfer_Model->get_product_mat_id($pd->product_code);
          $data_array = array(
            'purchase_id' => $last_id,
            'product_name' => $pd->product_mat_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);
    }
    
    
    
    
}