<?php
class Product_model extends CI_Model{
    
    function chk_dup($tablename = '',$column_ = '',$where_ = array() ){
      
            $this->db->select($column_);
            $this->db->from($tablename);
            if (!empty($where_)) {
                $this->db->where($where_);
            }
            
            $detail = $this->db->get();
            if($detail->num_rows()>0){
                return FALSE;
            }else{
                return TRUE;
            }
                
    
    }
    
    function product_list(){
            $this->db->from('mes_item');
            $this->db->select("mes_item.*,mes_category.*,mes_brand.*,mes_unit.*,mes_model.*,mes_spec.*,item.*");
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
            $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
            $this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'left');
            $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
           // $this->db->join( "mes_stock", "item.itm_id = mes_stock.stc_itemid", 'left');
           
            $this->db->order_by('brand_id', 'ASC');
            
            $detailtax = $this->db->get();
            return $detailtax->result();
                
    }
    
        function product_grn_list(){
            $this->db->from('mes_item');
            $this->db->select("mes_item.*,mes_category.*,mes_brand.*,mes_unit.*,mes_model.*,item.*");
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
            $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
           // $this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'inner');
            $this->db->join( "item", "item.itm_id = mes_item.item_name", 'left');
            $this->db->where('mes_item.item_status',1);
            
            $detailtax = $this->db->get();
            return $detailtax->result();
                
    }
    
            function batch_list(){
                
                 $this->db->from('batch');
            $this->db->select("batch.*,item.*,mes_item.*");
            $this->db->join( "mes_item", "mes_item.item_id = batch.product", 'left');
            //$this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'inner');
            //$this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'inner');
           // $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
            //$this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'inner');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            
            $this->db->where('batch.avl_qty!=',0);
            
            $detailtax = $this->db->get();
            return $detailtax->result();
                
    }
    
    function product_cat_list($cat_id){
            $this->db->from('mes_item');
            $this->db->select("mes_item.*,mes_category.*,mes_brand.*,mes_unit.*,item.*,mes_spec.*,mes_model.*");
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'inner');
            $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'inner');
            $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'inner');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->join( "mes_spec", "mes_spec.spec_id = mes_item.item_spec", 'inner');
            $this->db->where('mes_item.item_cat',$cat_id);
            $detailtax = $this->db->get();
            return $detailtax->result();
                
              
    } 
    
    function single_product($pro_id){
        
            $this->db->from('mes_item');
            $this->db->select("mes_item.*,mes_category.*,mes_brand.*,item.*,mes_unit.*");
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
            $this->db->where('mes_item.item_id',$pro_id);
            $detailtax = $this->db->get();
            return $detailtax->result();
                
              
    }
    
        function single_product_rep($pro_id,$sts){
        
            $this->db->from('mes_item1');
            $this->db->select("mes_item.*,mes_category.*,mes_brand.*,item.*,mes_unit.*");
            $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
            $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
            $this->db->join( "item", "mes_item.item_name = item.itm_id");
            $this->db->join( "mes_unit", "mes_unit.unit_id = mes_item.item_unit", 'left');
            $this->db->where('mes_item.item_id',$pro_id);
            if($sts=="A"){
                
               $this->db->where('mes_item.item_status',1); 
            }else if($sts=="I"){
                
                $this->db->where('mes_item.item_status',0);  
            }
            
            $detailtax = $this->db->get();
            return $detailtax->result();
                
              
    }
    
    
    function cat_list(){
                $this->db->from('mes_category');
                $this->db->select("cat_id,cat_name,cat_description,cat_shortcode");
                $this->db->where('cat_status!=',0);
                //$this->db->where('cat_id!=',0);
                $catall = $this->db->get();
                return $catall->result();
    }
    
    function item_list(){
                $this->db->from('item');
                $this->db->select("*");
                $this->db->where('itm_status!=',0);
                $this->db->where('itm_id!=',0);
                $all = $this->db->get();
                return $all->result();
    }
    
    function brand_list(){
                $this->db->from('mes_brand');
                $this->db->select("brand_id,brand_name,brand_description,brand_shordcode");
                $this->db->where('brand_id!=',0);
                $all = $this->db->get();
                return $all->result();
    }
    
    function unit_list(){
                $this->db->from('mes_unit');
                $this->db->select("unit_id,unit_name,unit_symble");
                $this->db->where('unit_id!=',0);
                $all = $this->db->get();
                return $all->result();
    }
    
    function currency_list(){
                $this->db->from('mes_currancy');
                $this->db->select("curr_id,curr_value,curr_symble");
                $this->db->where('curr_status!=',0);
                $all = $this->db->get();
                return $all->result();
    }
    
    function country_list(){
                $this->db->from('mes_country');
                $this->db->select("*");
                $all = $this->db->get();
                return $all->result();
    }
    
    function spec_list(){
                $this->db->from('mes_spec');
                $this->db->select("*");
                $this->db->where('spec_id!=',0);
                $all = $this->db->get();
                return $all->result();
    }
    
    function part_num($item_id){
                $this->db->from('mes_partnumber');
                $this->db->select("part_no,part_id");
                $this->db->where('part_itmno',$item_id);
                $this->db->where('part_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
     function mes_model(){
                $this->db->from('mes_model');
                $this->db->select("*");
                $this->db->where('model_id!=',0);
                $this->db->where('model_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
     function tax_list(){
                $this->db->from('mes_tax');
                $this->db->select("*");
                //$this->db->where('model_id!=',0);
                $this->db->where('tax_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
    function customer_list(){
                $this->db->select("*");
                $this->db->from('mes_customer');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
                $this->db->where('cust_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
        function po_list(){
                $this->db->select("*");
                $this->db->from('mes_cus_order');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
                $this->db->where('co_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
         function dpo_list(){
                $this->db->select("*");
                $this->db->from('mes_depot');
                $this->db->where('dep_stetus','1');
                $all = $this->db->get();
                return $all->result();
    }
    
    function province(){
        
         $this->db->select("*");
                $this->db->from('provinces');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
               // $this->db->where('dep_stetus','1');
                $all = $this->db->get();
                return $all->result();
    }
    
        function districts(){
        
         $this->db->select("*");
                $this->db->from('districts');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
               // $this->db->where('dep_stetus','1');
                $all = $this->db->get();
                return $all->result();
    }
    
            function cities(){
        
         $this->db->select("*");
                $this->db->from('cities');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
               // $this->db->where('dep_stetus','1');
                $all = $this->db->get();
                return $all->result();
    }
    
     function salserep_list(){
        
         $this->db->select("*");
                $this->db->from('mes_user');
                $this->db->where('user_departmentid=','Marketing');
                $this->db->where('user_lvl=',20);
                
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
                if($this->session->userdata['login_data']['userlevel']>=20){
                    
                    $this->db->where('user_id',$this->session->userdata['login_data']['user_id']);
                }
                $this->db->where('user_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
    function get_stock_prod(){
        if(isset($this->session->userdata['login_data']['depot'])){$depo = $this->session->userdata['login_data']['depot'];}else{$depo = 0;}
        if($depo!=0){
                    
                $this->db->select('*');
                    $this->db->from('mes_depot_stock');
                    $this->db->where('dpstock_dpoid',$depo);
                        $this->db->join('mes_item', 'mes_depot_stock.dpstock_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_depot_stock.dpstock_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }else{
                $this->db->select('*');
                    $this->db->from('mes_stock');
                        $this->db->join('mes_item', 'mes_stock.stc_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_stock.stc_qty>','0');
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }
            
            return $resultitem;
    }
    
    function get_stockwise_prod($depo){
        //if(isset($this->session->userdata['login_data']['depot'])){$depo = $this->session->userdata['login_data']['depot'];}else{$depo = 0;}
        if($depo!=0){
                    
                $this->db->select('*');
                    $this->db->from('mes_depot_stock');
                    $this->db->where('dpstock_dpoid',$depo);
                        $this->db->join('mes_item', 'mes_depot_stock.dpstock_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_depot_stock.dpstock_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }else{
                $this->db->select('*');
                    $this->db->from('mes_stock');
                        $this->db->join('mes_item', 'mes_stock.stc_itemid   = mes_item.item_id','left');
                        $this->db->join('item', 'item.itm_id   = mes_item.item_name','left');
                        $this->db->join( "mes_model", "mes_model.model_id = mes_item.item_model", 'left');
                        $this->db->join( "mes_brand", "mes_brand.brand_id = mes_item.item_brand", 'left');
                        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat",'left');
                        $this->db->where('mes_stock.stc_qty>','0');
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            }
            
            return $resultitem;
    }
    
    function get_trn_reason(){
        
                $this->db->select("*");
                $this->db->from('mes_ret_reason');
                //$this->db->where('model_id!=',0);
                //$this->db->join( "cust_address", "cust_address.cusad_cusid = mes_customer.cust_id", 'inner');
               $this->db->where('ret_reason_status','1');
                $all = $this->db->get();
                return $all->result();
    }
    
    function single_price($item){
        
        $this->db->from('batch');
            $this->db->select("selling_price");
            $this->db->where('batch.product',$item);
            $detailtax = $this->db->get();
           $result = $detailtax->result();
        
        if(count($result)>0){
            $price = $result[0];
            $prc = $price->selling_price;
        }else{
            $price = 0;
            $prc = 0;
        }
        
        
        return $prc;
    }
    
    function price_update($item,$price){
                        $this->db->set('selling_price', $price);
                        $this->db->where('product',$item);
                        $this->db->update('batch');
    
                $this->db->set('selling_price', $price);
                $this->db->where('product',$item);
                $this->db->update('batch_depo');
                        
       $dataedit = array(
                'batch_edit_batch_id'  => 0,
                'batch_edit_item_id'  => $item,
                'batch_edit_buying'  => 0,
                'batch_edit_selling'  => $price,
                'batch_edit_retail'  => 0,
                'batch_edit_mafd'  => '',
                'batch_edit_exp'  => '',
                'batch_edit_comment'  => 'All Batch Price change',
                'batch_edit_user'  => $this->session->userdata['login_data']['user_id'],
                'batch_edit_date'  => (date('Y-m-d'))
                
                    );
                    $this->db->insert('batch_edit_log',$dataedit);
        
    }
    
  
}

?>