<?php

class Productkits_model extends CI_Model {

    function __construct() {

        // Call the Model constructor
        parent::__construct();
    }
 
    function getallkits($businessid){
    
        $this->db->select('id_business_products, id_business_brands, business_brand_name, product, sku, barcode_products, price, purchase_price, measure_unit, qty_per_unit, product_type');
        $this->db->join('business_brands','business_brands.id_business_brands= business_products.brand_id');
        $this->db->join('business','business.id_business = business_products.business_id');
        $this->db->where('product_type','Compiled Kit');
        if(null!==$businessid && $businessid !== ''){
            $this->db->where('business_products.business_id',$businessid);
        }
        $this->db->order_by('id_business_products', 'DESC');
        $query = $this->db->get('business_products');
        
        $kits =[];
        foreach($query->result_array() as $kit){
            $this->db->select('*');
            $this->db->join('kit_config', 'kit_config.business_product_id = business_products.id_business_products', 'left');
            $this->db->join('business_brands','business_brands.id_business_brands= business_products.brand_id');
            $this->db->join('business','business.id_business = business_products.business_id');
            $this->db->where('kit_config.kit_id', $kit['id_business_products']);
            $query1 = $this->db->get('business_products'); 
            $kit['products']= $query1->result_array();
            $kits[] = $kit; 
            
        }
        
        return $kits ;
        
       
    }
    
    
    function get_kit_config($kitid){
        
        
        $this->db->select('*');
        $this->db->join('kit_config', 'kit_config.business_product_id = business_products.id_business_products');
        $this->db->join('business_brands','business_brands.id_business_brands= business_products.brand_id');
        $this->db->join('business','business.id_business = business_products.business_id');
        $this->db->where('kit_config.kit_id', $kitid);
        $query1 = $this->db->get('business_products'); 
       
        return $query1->result_array();
    }
    
    function get_product_listing() {

        $this->db->select('bp.id_business_products, bp.product, bp.unit_type');
        $this->db->where('bp.business_id', $this->session->userdata('businessid'));
        //$this->db->where('bp.inhouse_stock >', 0);
        $this->db->where('bp.qty_per_unit >', 0);
        $this->db->where('bp.business_product_active', 'Yes');
        $this->db->order_by('bp.product', 'ASC');
        $query = $this->db->get('business_products bp');

        return $query->result_array();
    }
    
    function add_kit(){
        
         $data= array(
            'brand_id' => $this->input->post('brand_id'),
            'product' => $this->input->post('product'),
            'category' => $this->input->post('category'),
            'professional' => $this->input->post('professional'),
            'purchase_price' => $this->input->post('purchase_price'),
            'price' => $this->input->post('price'),
            'unit_type' => $this->input->post('unit_type'),
            'measure_unit' => $this->input->post('measure_unit'),
            'sku' => $this->input->post('sku'),
            'commission' => $this->input->post('commission'),
            'barcode_products' => $this->input->post('barcode_product'),
            'product_type' => $this->input->post('product_type'),
            'qty_per_unit' => $this->input->post('qty_per_unit'),
            'product_threshold' => $this->input->post('product_threshold'),
            'business_id' =>  $this->session->userdata('businessid')
                    
        );
         
        $this->db->insert('business_products', $data);
        return $this->db->insert_id();
        
    }
    
    function update_kit(){
        //exit();
        $data= array(
            'brand_id' => $this->input->post('brand_id'),
            'product' => $this->input->post('product'),
            'category' => $this->input->post('category'),
            'professional' => $this->input->post('professional'),
            'purchase_price' => $this->input->post('purchase_price'),
            'price' => $this->input->post('price'),
            'unit_type' => $this->input->post('unit_type'),
            'measure_unit' => $this->input->post('measure_unit'),
            'sku' => $this->input->post('sku'),
            'commission' => $this->input->post('commission'),
            'barcode_products' => $this->input->post('barcode_product'),
            'product_type' => $this->input->post('product_type'),
            'qty_per_unit' => $this->input->post('qty_per_unit'),
            'product_threshold' => $this->input->post('product_threshold'),
            'business_id' =>  $this->session->userdata('businessid')
                    
        );
        $this->db->where('id_business_products',$this->input->post('editproductid')); 
        $this->db->update('business_products', $data);
        return $this->db->affected_rows();
    }
    
    function add_kit_products(){
        //exit();
        $qty_per_unit = $this->input->post('qty_per_unit'); 

        if($this->input->post('unit_qty')!=='' && (float)$this->input->post('unit_qty') > 0)  {
            $unit_qty = (float)$this->input->post('unit_qty');  
            $measur_ment_qty =  $qty_per_unit*$unit_qty; 
            
        } else {
            $unit_qty = (float)$this->input->post('measure_qty') / (float)$this->input->post('qty_per_unit'); 

            $measur_ment_qty =(float)$this->input->post('measure_qty');
        }  

        $data=array(
            'kit_id' => $this->input->post('kit_id'),
            'business_product_id' => $this->input->post('kit-products'),
            'kit_qty' => $unit_qty,
            'kit_measure_qty' => $measur_ment_qty,            
            'kit_created_by'=> $this->session->userdata('username')
        );
        
        $this->db->insert('kit_config', $data);
        return $this->db->insert_id();

    }
    

    
    function remove_kit_products(){
        //exit();
        $this->db->where('id_kit_config', $this->input->post('kit_config_id'));
        
        $this->db->delete('kit_config');
        $this->db->affected_rows();
    }

    function add_kit_batch(){
        
    }

}