<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Requisition_controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        // Your own constructor code       
        $this->load->model('product_model');
        $this->load->model('store_model');
        $this->load->model('requisition_model');
        $this->load->model('business_model');
        
        if ($this->session->userdata('role') == '') {
            redirect('logout');
        }
        //storemanager,hr,reception...user serial
        checkroles(1, 1, 1);
    }

    public function branch_requisition_list(){

        $business_id=0;
        if($this->input->post('business_id') != null ){
            $business_id= $this->input->post('business_id');            
        } 

        if($this->session->userdata('ho')=='Yes'){
            $data['businesses']=$this->business_model->get_all_businesses();
        }else{
            $data['businesses']=$this->business_model->getbusinessdetails();
        }
        

        $data['requisitions']=$this->requisition_model->get_requisitions($business_id);

        $data['selected_business'] = $business_id;

        $this->load->view('includes/header', $data);
        $this->load->view('requisitions/branch_requisitions.php');
        $this->load->view('includes/footer');

    }


    public function branch_requisition($req_number){

        $requisition_detail=$this->requisition_model->get_requisition($req_number);
        
        $data['heading']="Details of Requistion Number ".$req_number;

        $data['stores'] = $this->product_model->get_stores();
        $data['hostores'] = $this->store_model->get_business_stores();
        
        $data['requisition_detail']=$requisition_detail;
        /////batches
        // $batches = [];
        // $hostores =  $data['hostores'];
       
        // foreach($requisition_detail as $req){
        //     foreach($hostores as $s){
        //         $batches = $this->product_model->get_all_batches($req['product_id'],'', $s['id_business_stores']);
        //         array_push($req, array('batches'=> $batches));                
                
        //     } 
        //     array_push($data['requisition_detail'], $req);
        // }
       
        /////
        
        $this->load->view('includes/header', $data);
        $this->load->view('requisitions/requisition_detail.php');
        $this->load->view('includes/footer');

    }

    public function product_requisitions($product_id){
       

        $data['requisition_detail']=$this->requisition_model->get_product_requisitions($product_id);
        if(sizeof($data['requisition_detail'])>0){
            $data['heading']="Requisitions From All Branches for ".$data['requisition_detail'][0]['product'];
        }else{
            $data['heading']="Requisitions From All Branches for (No requisition found)";
        }
        $data['stores'] = $this->product_model->get_stores();
        $data['hostores'] = $this->store_model->get_business_stores();

        $this->load->view('includes/header', $data);
        $this->load->view('requisitions/requisition_detail.php');
        $this->load->view('includes/footer');

    }

    function open_add_requisition($req_number = null){
        if(null==$req_number){
            $req_number_data = $this->requisition_model->get_max_requisition_number();
            $req_number=$req_number_data->requisition_number;
            $data['requisition_number'] = $req_number;
        }else {
            $data['requisition_number'] = $req_number;
        }

        $data['stores'] = $this->product_model->get_business_stores();

       // print_r($data['requisition_number']); exit();
        $this->load->view('includes/header', $data);
        $this->load->view('requisitions/add_requisition.php');
        $this->load->view('includes/footer');

    }

    function add_branch_requistion(){

        $storeid = $this->input->post('store');

        $tableData = stripcslashes($_POST['requisition']);
        // Decode the JSON array
        $tableData = json_decode($tableData,TRUE);
        $data=[];
        foreach($tableData as $tdata){
            $row = array(
                'requisition_number' => $tdata['requisition_number'],
                'product_id' => $tdata['product_id'],
                'required_qty' => $tdata['requisition_qty'],
                'business_id' => $this->session->userdata('businessid'),
                'requisition_date' => date('Y-m-d H:i:s'),
                'store_id' => $storeid
            );
            array_push($data, $row);
        }
        
        $this->db->insert_batch('branch_requisition', $data);

        echo 'success';
    }

    public function batches_ajax($product_id=''){

       
        if(null !== $this->input->post('product_id') && $this->input->post('product_id')!=""){
            $product_id=$this->input->post('product_id');
        }       

        if($this->session->userdata('role')=='' || $this->session->userdata('role')=='Sh-Users'){
            $this->load->view('login_view');
        } else {

            $hostores = $this->store_model->get_business_stores();
            $batches=[];
            foreach($hostores as $s){
                $data =  $this->product_model->get_all_batches($product_id, '', $s['id_business_stores'], "true");
                array_push($batches, $data);
            }
            

            echo (json_encode($batches));
            die;

        }
    }

    public function update_requisition_qty(){
        //exit();
        $id_branch_requisition=$this->input->post('id_branch_requisition');
        $qty = $this->input->post('qty');

        
        $sql = "SELECT requisition_id, sum(tranfer_out_qty) tranfer_out_qty, sum(tranfer_in_qty) tranfer_in_qty 
        from transfer_notes'
        WHERE requisition_id=".$id_branch_requisition."
        GROUP BY requisition_id";

        $query = $this->db->query($sql);
        $transfered_qty = $query->row();

        if($transfered_qty->tranfer_in_qty < $qty){
            $this->db->set('required_qty', $qty);
            $this->db->where('id_branch_requisition', $id_branch_requisition);
        
            $a = $this->db->update('branch_requisition');
        
            echo 'success';
        } else {
            echo $transfered_qty->tranfer_in_qty .' pieces have already been transfered against this requisition!';
        }
    }
    public function update_requisition_status(){
        //exit();
        $id_branch_requisition=$this->input->post('id_branch_requisition');
        $status = $this->input->post('status');
        $this->db->set('requisition_status', $status);
        $this->db->where('id_branch_requisition', $id_branch_requisition);
        $a = $this->db->update('branch_requisition');
        
            echo 'success';
    }
}