<?php

class Requisition_model extends CI_Model {

    function __construct() {
        // Call the Model constructor
        parent::__construct();
    }


    function get_requisitions($business_id = null){

        if($business_id==null){
            return [];
        } else {

            $sql = "SELECt requisition_number, '' as id_business_products, '' as product, business_brand_name,
             date_format(requisition_date, '%d-%m-%Y') as requistion_date, 
                    branch_requisition.business_id, business.business_name,
                    sum(required_qty) as required_qty,
                    sum(transfered_qty) as transfered_qty,
                    '' as requisition_status, branch_requisition.store_id
                    FROM branch_requisition
                    JOIN business on business.id_business = branch_requisition.business_id
                    JOIN business_products on business_products.id_business_products = branch_requisition.product_id 
                    JOIN business_brands on business_brands.id_business_brands = business_products.brand_id
                    WHERE requisition_status='Active' ";
            if($business_id > 0){
                $sql .=  " AND  branch_requisition.business_id=".$business_id." ";
            }
            $sql .=  " GROUP BY requisition_number ;";
            $query = $this->db->query($sql);
            
            return $query->result_array();
        }
    }

    function get_requisition($req_number){

        $sql="SELECT requisition_number, id_branch_requisition, branch_requisition.product_id, business_brand_name,
        product, branch_requisition.business_id, 
        business_name, id_business_stores, business_store, branch_requisition.store_id,
        requisition_status, date_format(requisition_date, '%d-%m-%Y') as requistion_date, 
        required_qty, transfered_qty, ifnull(transfer_notes.tranfer_in_qty,0) as tranfer_in_qty, branch_requisition.created_by
        FROM branch_requisition
        JOIN business_products on business_products.id_business_products = branch_requisition.product_id
        JOIN business_brands on business_brands.id_business_brands = business_products.brand_id
        JOIN (select id_business, business_name, id_business_stores, business_store
            From business Join business_stores on business.id_business = business_stores.business_id
            Group by id_business
            ) as business on business.id_business = branch_requisition.business_id
        LEFT JOIN (
            SELECT requisition_id, sum(tranfer_out_qty) tranfer_out_qty, sum(tranfer_in_qty) tranfer_in_qty 
            from transfer_notes            
            
            GROUP BY requisition_id
            ) as transfer_notes on transfer_notes.requisition_id = branch_requisition.id_branch_requisition
        WHERE requisition_number = ".$req_number." AND requisition_status in ('Not Approved','Active');";

            // echo $sql; exit();

        $query = $this->db->query($sql);
            
        return $query->result_array();
    }

    function get_product_requisitions($product_id){

        $sql="SELECT requisition_number, id_branch_requisition, branch_requisition.product_id, business_brand_name,
        product, branch_requisition.business_id, 
        business_name, id_business_stores, business_store, branch_requisition.store_id,
        requisition_status, date_format(requisition_date, '%d-%m-%Y') as requistion_date, 
        required_qty, ifnull(transfer_notes.tranfer_in_qty,0) as tranfer_in_qty, branch_requisition.created_by
        FROM branch_requisition
        JOIN business_products on business_products.id_business_products = branch_requisition.product_id
        JOIN business_brands on business_brands.id_business_brands = business_products.brand_id
        JOIN (select id_business, business_name, id_business_stores, business_store
            From business Join business_stores on business.id_business = business_stores.business_id
            Group by id_business
            ) as business on business.id_business = branch_requisition.business_id
        LEFT JOIN (
            SELECT requisition_id, sum(tranfer_out_qty) tranfer_out_qty, sum(tranfer_in_qty) tranfer_in_qty 
            from transfer_notes            
            WHERE product_id=".$product_id."
            GROUP BY requisition_id
            ) as transfer_notes on transfer_notes.requisition_id = branch_requisition.id_branch_requisition
        WHERE branch_requisition.product_id = ".$product_id." AND requisition_status='Active';";

        $query = $this->db->query($sql);
            
        return $query->result_array();
    }


    function get_max_requisition_number(){

        $sql = "select max(requisition_number)+1 as requisition_number from branch_requisition where 
        requisition_status='Active'";
        $query = $this->db->query($sql);
            
        return $query->row();

    }


    function add_branch_requistion(){

        $data = array(
            'requisition_number' => $this->input->post('requisition_number'),
            'product_id' => $this->input->post('product_id'),
            'required_qty' => $this->input->post('required_qty'),
            'business_id' => $this->session->userdata('businessid'),
            'requisition_date' => date('Y-m-d H:i:s'),
            'created_by' => $this->session->userdata('username')
        );

        $this->db->insert('branch_requisition', $data);

        return $this->db->insert_id();

    }

   

}
