<?php
class Common_model extends CI_Model{
    
    
    
    function todaymoq(){
        
        $this->db->select("COUNT(`item_id`) as itm"); 
        $this->db->from('mes_item');
        $this->db->join( "mes_stock", "mes_item.item_id  = mes_stock.stc_itemid", 'left');
        $this->db->where('mes_item.item_moq >=mes_stock.stc_actqty');
        $this->db->where('mes_item.item_moq!=', 0 );
        $this->db->where('item_status ', 1 );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->itm;
        
        
    }
    
    function todayexp(){
        
        $this->db->select("COUNT(`batch_id`) as batch"); 
        $this->db->from('batch');
        //$this->db->join( "mes_stock", "mes_item.item_id  = mes_stock.stc_itemid", 'left');
        $this->db->where('exp_date !=','0000-00-00');
        $this->db->where('avl_qty !=', 0 );
        $this->db->where('exp_date <=', date("Y-m-d") );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->batch;
        
    }
    
    
    function todaybin(){
        
        $this->db->select("COUNT(`to_stock_id`) as bin"); 
        $this->db->from('ret_to_stock');
        //$this->db->join( "mes_stock", "mes_item.item_id  = mes_stock.stc_itemid", 'left');
        $this->db->where('to_bin !=',0);
        $this->db->where('bin_auth', 0 );
        
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->bin;
        
    }
    
           function todotrnf(){
        
        $this->db->select("COUNT(`trncfer_id`) as todotrnf"); 
        $this->db->from('mes_stock_trancfer');
        $this->db->where('trncfer_prosses ', 0 );
        $this->db->where('trncfer_stetus ', 1 );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->todotrnf;
        
        
    }
    
        function todopay(){
        
        $this->db->select("COUNT(`pay_id`) as todopay"); 
        $this->db->from('mes_payment_main');
        $this->db->where('pay_authrize', 0 );
        $this->db->where('pay_stetus', 1 );
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('pay_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->todopay;
        
        
    }
    
        function todoauthcus(){
        
        $this->db->select("COUNT(`cust_id`) as todocus"); 
        $this->db->from('mes_customer');
        $this->db->where('cust_auth', 0 );
        $this->db->where('cust_status', 1 );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->todocus;
    }
    
    function todoinvtrn(){
        
        $this->db->select("COUNT(`ret_id`) as todortn"); 
        $this->db->from('inv_return');
        $this->db->where('ret_proceed', 0 );
        $this->db->where('ret_stetus', 1 );
        if($this->session->userdata['login_data']['userlevel']>15){
         $this->db->join( "mes_qto", "mes_qto.po_id = inv_return.ret_invnum", 'left');
            $this->db->where('mes_qto.po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        
        $dataa=$query->result();
        return $dataa[0]->todortn;
        
        
    }
    
      function todoauthgrn(){
        
        $this->db->select("COUNT(`grn_id`) as todogrn"); 
        $this->db->from('mes_grn');
        $this->db->where('grn_proceed', 0 );
        $this->db->where('grn_stetus', 1 );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->todogrn;
    }
    
    function todoauthinv(){
        
        $this->db->select("COUNT(`po_id`) as todoinv"); 
        $this->db->from('mes_qto');
        $this->db->where('po_auth', 0 );
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->todoinv;
    }
    
    function todayreturncount(){
        
        $this->db->select("COUNT(`ret_id`) as retn_count"); 
        $this->db->from('inv_return');
         $this->db->join( "mes_qto", "mes_qto.po_id = inv_return.ret_invnum", 'left');
        $this->db->where('ret_cdate', date("Y-m-d") );
        $this->db->where('ret_stetus', 1 );
        if($this->session->userdata['login_data']['userlevel']>15){
        $this->db->where('mes_qto.po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->retn_count;
    }
    
    function todayreturn(){
        
        $this->db->select("SUM(`ret_amount`) as retn_tot"); 
        $this->db->from('inv_return');
        $this->db->where('ret_cdate', date("Y-m-d") ); //date("Y-m-d")
        $this->db->where('ret_stetus', 1 ); //date("Y-m-d")
        if($this->session->userdata['login_data']['userlevel']>15){
         $this->db->join( "mes_qto", "mes_qto.po_id = inv_return.ret_invnum", 'left');
            $this->db->where('mes_qto.po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->retn_tot;
    }
    
    function productcount(){
        
        $this->db->select("COUNT(`item_id`) as item_count"); 
        $this->db->from('mes_item');
        //$this->db->where('item_status', 1 );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->item_count;
    }
    
    function getsalseman(){
        
        $this->db->select("COUNT(`user_id`) as user_count"); 
        $this->db->from('mes_user');
        $this->db->where('user_departmentid', 'Marketing' );
        $this->db->where('user_status', 1 );
        
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->user_count;
    }
    
    function getcus(){
        
        $this->db->select("COUNT(`cust_id`) as cus_count"); 
        $this->db->from('mes_customer');
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('cust_salseperson', $this->session->userdata['login_data']['user_id'] ); 
        }
        //$this->db->where('user_departmentid', 'Marketing' );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->cus_count;
    }
    
    function getdepo(){
        
        $this->db->select("COUNT(`dep_id`) as cus_depo"); 
        $this->db->from('mes_depot');
        //$this->db->where('user_departmentid', 'Marketing' );
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->cus_depo;
    }
    
    function todayinv(){
        
        $this->db->select("SUM(`po_final_tot`) as inv_tot"); 
        $this->db->from('mes_qto');
        $this->db->where('po_cdate', date("Y-m-d") ); //date("Y-m-d")
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        $dataa=$query->result();
        return $dataa[0]->inv_tot;
    }
    
    function todayinvcount(){
        
        $this->db->select("COUNT(`po_id`) as inv_qty"); 
        $this->db->from('mes_qto');
        $this->db->where('po_cdate', date("Y-m-d") ); //date("Y-m-d")
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('po_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        $dataa=$query->result();
        return $dataa[0]->inv_qty;
    }
    
    function todaypay(){
        
        $this->db->select("SUM(`pay_amount`) as pay_tot"); 
        $this->db->from('mes_payment_main');
        $this->db->where('pay_date', date("Y-m-d") ); //date("Y-m-d")
        $this->db->where('pay_stetus', 1 ); //date("Y-m-d")
        if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('pay_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->pay_tot;
    }
    
    function todaypaycount(){
        
        $this->db->select("COUNT(`pay_id`) as pay_qty"); 
        $this->db->from('mes_payment_main');
        $this->db->where('pay_date', date("Y-m-d") ); //date("Y-m-d")
         $this->db->where('pay_stetus', 1 ); //date("Y-m-d")
          if($this->session->userdata['login_data']['userlevel']>15){
            $this->db->where('pay_ref_id', $this->session->userdata['login_data']['user_id'] ); 
        }
        $query = $this->db->get();
        //$dataa = $query->num_rows();
        $dataa=$query->result();
        return $dataa[0]->pay_qty;
    }
    
    function dosms($id,$msg) {
       
                       //make sure 1 sec sleep any concurrent sending
                       $id = sprintf("%07d", $id);

$mask = 'NANCE';
$token = '18|nB3BHLGaLaNQuagv88TVyckmNEqXxV8OdlS9jlRz';
$api_link = 'https://sms.smsturbine.com/api/v1/sms/send';
$number = 94703838285;  //Receiver Number
$messsage = $msg.' '.'INV'.$id; //Message

$chk = curl_init();

$body = [
	'mask' => $mask,
	'number' => $number,
	'message' => $messsage,
];

$header = [
	'Accept: application/json',
	'Authorization: Bearer ' . $token
];

curl_setopt($chk, CURLOPT_URL, $api_link);		//API link
curl_setopt($chk, CURLOPT_SSL_VERIFYPEER, 1 );
curl_setopt($chk, CURLOPT_POST, 1);              	//Uing POST method
curl_setopt($chk, CURLOPT_POSTFIELDS, $body);        //Parameters
curl_setopt($chk, CURLOPT_HTTPHEADER, $header);      //Header
curl_setopt($chk, CURLOPT_RETURNTRANSFER, 1 );

$response = curl_exec($chk);

curl_close ($chk);

//you can used echo for show the details
//print $response;
//echo $response;
   }
    
    function po_item($poid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('po_items');
        $this->db->where('po_itm_po_id', $poid );
        $this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
       // $this->db->join('mes_service_type', 'po_items.service_des   = mes_service_type.serv_id');
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id); 
                }
        }
        
         return $inv;
    }
    
    function po_grnitem($poid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('po_items');
        $this->db->where('po_itm_po_id', $poid );
        $this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'po_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'po_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'po_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'po_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id); 
                }
        }
        
         return $inv;
    }
    
    function single_item($itemid){
        $inv;
        $this->db->select("*"); 
        $this->db->from('mes_item');
        $this->db->where('mes_item.item_id',$itemid);
        $this->db->join('item', 'item.itm_id   = mes_item.item_name', 'left');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id', 'left');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id', 'left');
        $this->db->join('mes_unit', 'mes_item.item_unit = mes_unit.unit_id', 'left');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'left');
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                    
                    if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} 
                   // $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->item_cat;
                    $itm = $dataa[$i]->itm_name."\n".$dataa[$i]->item_cat;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,"","","",$dataa[$i]->brand_shordcode); 
                }
        }
        
         return $inv;
    }

    function qto_item($qtid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('qto_items');
        $this->db->where('po_itm_po_id', $qtid );
        //$this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'qto_items.po_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'qto_items.po_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'qto_items.po_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'qto_items.po_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                 
                 if($dataa[$i]->service_des!=""){
                    $this->db->from('mes_service_type');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('mes_service_type.serv_id',  $dataa[$i]->service_des);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dt[0]->serv_name;
                    $inv[] = array($dt[0]->serv_code,$dataa[$i]->service_des,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id);    
                }else{
                    
                     if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id); 
                }
                    
                  /*  if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id); 
                */
                 
             }
        }
        
         return $inv;
    }
    
    function co_item($qtid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('co_items');
        $this->db->where('co_itm_co_id', $qtid );
        //$this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'co_items.co_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'co_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'co_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'co_items.co_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                 if($dataa[$i]->inv_qty<$dataa[$i]->co_itm_qty){
                      if($dataa[$i]->service_des!=""){
                    $this->db->from('mes_service_type');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('mes_service_type.serv_id',  $dataa[$i]->service_des);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dt[0]->serv_name;
                    $inv[] = array($dt[0]->serv_code,$dataa[$i]->service_des,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_item_id,$dataa[$i]->co_itm_id,$dataa[$i]->inv_qty);    
                
                 }else{
                    
                     if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_item_id,$dataa[$i]->co_itm_id,$dataa[$i]->inv_qty); 
                }
                 }
                 
                
                    
             }
        }
        
         return $inv;
    }
    
    function inv_item($qtid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('inv_items');
        $this->db->where('co_itm_qto_id', $qtid );
        //$this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'inv_items.co_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'inv_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'inv_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'inv_items.co_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                 
                 if($dataa[$i]->service_des!=""){
                    $this->db->from('mes_service_type');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('mes_service_type.serv_id',  $dataa[$i]->service_des);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dt[0]->serv_name;
                    $inv[] = array($dt[0]->serv_code,$dataa[$i]->service_des,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id);    
                }else{
                    
                     if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id); 
                }
                    
                  /*  if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id); 
                */
                 
             }
        }
        
         return $inv;
    }
    
    function inv_id_item($qtid){
        $inv[] = array();
        $this->db->select("*"); 
        $this->db->from('inv_items');
        $this->db->where('co_itm_co_id', $qtid );
        $this->db->where('co_itm_qty!=', 'co_itm_crqty' );
        //$this->db->where('po_itm_item_id!=', "" );
        $this->db->join('mes_item', 'inv_items.co_itm_item_id   = mes_item.item_id');
        $this->db->join('item', 'item.itm_id   = mes_item.item_name');
        $this->db->join('mes_model', 'mes_item.item_model   = mes_model.model_id');
        $this->db->join('mes_brand', 'mes_item.item_brand   = mes_brand.brand_id');
        $this->db->join('mes_partnumber', 'inv_items.co_itm_partno  = mes_partnumber.part_id');
        $this->db->join('mes_unit', 'inv_items.co_itm_unit   = mes_unit.unit_id');
        $this->db->join('mes_currancy', 'inv_items.co_itm_currancy   = mes_currancy.curr_id');
        $this->db->join( "mes_category", "mes_category.cat_id = mes_item.item_cat", 'inner');
        $this->db->join( "mes_spec", "mes_spec.spec_id  = mes_item.item_spec");
        $query = $this->db->get();
        $dataa=$query->result();
        
       
        
         for($i=0; $i<count($dataa); $i++){
             {
                 
                 if($dataa[$i]->service_des!=""){
                    $this->db->from('mes_service_type');
                    $this->db->join('mes_unit', 'mes_service_type.unit   = mes_unit.unit_id');
                    $this->db->where('mes_service_type.serv_id',  $dataa[$i]->service_des);
                    $hasill=$this->db->get();
                    $dt = $hasill->result();
                    $itm = $dt[0]->serv_name;
                    $inv[] = array($dt[0]->serv_code,$dataa[$i]->service_des,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_item_id,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_crqty);    
                }else{
                    
                     if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->co_itm_unitprice,$dataa[$i]->co_itm_qty,$dataa[$i]->co_itm_item_id,$dataa[$i]->co_itm_id,$dataa[$i]->co_itm_crqty); 
                }
                    
                  /*  if($dataa[$i]->brand_name!=""){$spece = "  ";}else{$spece = "";} if($dataa[$i]->part_no!=""){$spece1 = "  ";}else{$spece1 = "";}
                    if($dataa[$i]->part_no!=""){$spece2 = "  ";}else{$spece2 = "";}
                    $itm = $dataa[$i]->brand_name.$spece.$dataa[$i]->itm_name."\n".$dataa[$i]->model_name.$spece1.$dataa[$i]->part_no.$spece2.$dataa[$i]->spec_spec;
                    $inv[] = array($dataa[$i]->cat_shortcode,$dataa[$i]->item_catcount,$itm,$dataa[$i]->unit_symble,$dataa[$i]->po_itm_unitprice,$dataa[$i]->po_itm_qty,$dataa[$i]->po_itm_item_id,$dataa[$i]->po_itm_id); 
                */
                 
             }
        }
        
         return $inv;
    }
    
    function cal_stock($depo,$type,$inv_id,$item_id,$qty,$add,$from,$batch){
        if(isset($this->session->userdata['login_data']['user_id'])){
            $usr = $this->session->userdata['login_data']['user_id'];
                        $data = array(
                            'depo' => $depo,
                            'type' => $type,
                            'inv_id' => $inv_id,
                            'item_id' => $item_id,
                            'qty' => $qty,
                            'add' => $add,
                            'batch' => $batch,
                            'usr' => $usr
                       );    
                         
                        $rest_api_base_url = 'https://manchieedecoco.com/lumen/public';    
                        $post_endpoint = '/com_stock_cal';
                        $request_data = $data;
                        $response = perform_http_request('POST', $rest_api_base_url . $post_endpoint, $request_data);    
                         $det = json_decode($response); 
            if($det!=null){
                return true;
            }else{
                return false;
            }
        }else{
            return false;
        }
        
            
    }
    
    function check_stock($item_id,$qty,$depo,$batch){
        
        if($depo<1){
            $this->db->select("*");    
            $this->db->from('mes_stock');
            $this->db->where('stc_itemid', $item_id);
            $query = $this->db->get();
            $sdata = $query->result();
            $stqty = $sdata[0]->stc_qty;
            $acqty = $sdata[0]->stc_actqty;
            if($qty > $stqty){
                return false;
            }else{
                return true;
            }
            
        }else{
            
            $this->db->select('*');
                $this->db->from('batch_depo'); 
                $this->db->where('depo',$this->input->post('depo'));
                $this->db->where('batch_depo.batch_id',$batch); 
                        $this->db->where('batch_depo.avl_qty>',0);
                    $hasil=$this->db->get();
                    $resultitem= $hasil->result(); 
            
            $this->db->select("*");    
                        $this->db->from('mes_depot_stock');
                        $this->db->where('dpstock_itemid', $item_id);
                        $this->db->where('dpstock_dpoid', $depo);
                        $query = $this->db->get();
                        $sdata = $query->result();
                        
                        
                        $stqty = $sdata[0]->dpstock_qty;
                        
                        if($qty > $stqty){
                            return false;
                        }else{
                            return true;
                        }
        }
        
        
    }
 
    function cal_stock_no_inventry($depo,$type,$inv_id,$item_id,$qty,$add,$from){
        //$from -> 0=both actual and stock , 1 = only from stock, 2 = only from actual
        // $depo -> 0 for main stock
        //$add-> plus=add amount , min - deduct amount  invt_stock
        
        if($depo<1){ 
                            $table ="mes_stock";
                            $this->db->select("*");    
                        $this->db->from('mes_stock');
                        $this->db->where('stc_itemid', $item_id);
                        $query = $this->db->get();
                        $sdata = $query->result();
                        $stqty = $sdata[0]->stc_qty;
                        $acqty = $sdata[0]->stc_actqty;
                        
                            if($add=="plus"){
                                $newstk = $stqty + $qty;
                                $newact = $acqty + $qty;
                            }else{
                                $newstk = $stqty - $qty;
                                $newact = $acqty - $qty;
                            }
                           
                           $this->db->set('invt_stock',$stqty);
                    $this->db->where('invt_type',$type);
                    $this->db->where('invt_id',$inv_id);
                    $this->db->update('mes_inventry');
                            
                            $this->db->set('stc_actqty', $newstk);
                                $this->db->set('stc_qty', $newact);
                                    $this->db->where('stc_itemid', $item_id);
                                    $data=$this->db->update('mes_stock');
                                    
                                    
        }else{
            $table ="mes_depot_stock";
                        $this->db->select("*");    
                        $this->db->from('mes_depot_stock');
                        $this->db->where('dpstock_itemid', $item_id);
                        $this->db->where('dpstock_dpoid', $depo);
                        $query = $this->db->get();
                        $sdata = $query->result();
                        $stqty = $sdata[0]->dpstock_qty;
                        //$acqty = $sdata[0]->stc_actqty;
                        
                            if($add=="plus"){
                                $newstk = $stqty + $qty;
                            }else{
                                $newstk = $stqty - $qty;
                            }
                            
                            $this->db->set('invt_stock',$stqty);
                            $this->db->where('invt_type',$type);
                            $this->db->where('invt_id',$inv_id);
                            $this->db->update('mes_inventry');
                            
                                $this->db->set('dpstock_qty', $newstk);
                                $this->db->where('dpstock_itemid', $item_id);
                                $this->db->where('dpstock_dpoid', $depo);
                                $data=$this->db->update('mes_depot_stock');
        }
         
    }
    
    function cal_stock_only_stock($depo,$type,$inv_id,$item_id,$qty,$add,$from){
        //$from -> 0=both actual and stock , 1 = only from stock, 2 = only from actual
        // $depo -> 0 for main stock
        //$add-> plus=add amount , min - deduct amount  invt_stock
        
        if($depo<1){ 
                            $table ="mes_stock";
                            $this->db->select("*");    
                        $this->db->from('mes_stock');
                        $this->db->where('stc_itemid', $item_id);
                        $query = $this->db->get();
                        $sdata = $query->result();
                        $stqty = $sdata[0]->stc_qty;
                        $acqty = $sdata[0]->stc_actqty;
                        
                            if($add=="plus"){
                                $newstk = $stqty + $qty;
                                $stqty = $stqty - $qty;
                            }else{
                                $newstk = $stqty - $qty;
                                $stqty = $stqty + $qty;
                            }
                           
                           $this->db->set('invt_stock',$stqty);
                    $this->db->where('invt_type',$type);
                    $this->db->where('invt_id',$inv_id);
                    $this->db->update('mes_inventry');
                            
                            
                                    
                                    
        }else{
            $table ="mes_depot_stock";
                        $this->db->select("*");    
                        $this->db->from('mes_depot_stock');
                        $this->db->where('dpstock_itemid', $item_id);
                        $this->db->where('dpstock_dpoid', $depo);
                        $query = $this->db->get();
                        $sdata = $query->result();
                        $stqty = $sdata[0]->dpstock_qty;
                        //$acqty = $sdata[0]->stc_actqty;
                        
                            if($add=="plus"){
                                $newstk = $stqty + $qty;
                                $stqty = $stqty - $qty;
                            }else{
                                $newstk = $stqty - $qty;
                                $stqty = $stqty + $qty;
                            }
                            
                            $this->db->set('invt_stock',$stqty);
                            $this->db->where('invt_type',$type);
                            $this->db->where('invt_id',$inv_id);
                            $this->db->update('mes_inventry');
                            
        }
         
    }
    
    function cal_cust_bal($cust,$amnt,$add){
            $this->db->select("cust_credit");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id', $cust);
            $query = $this->db->get();
                        $sdata = $query->result();
                        
                    if($add!='add'){
                        $new = $sdata[0]->cust_credit -$amnt;
                    }else{
                        $new = $sdata[0]->cust_credit +$amnt;
                    }   
            
        
    }
    
    
    function outstand($cust){
        
        $this->db->select("SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret");    
            $this->db->from('mes_qto');
            $this->db->where('po_sup_id', $cust);
            $this->db->where('po_status',1);
            $this->db->where('po_auth',1);
            $query = $this->db->get();
                        $sdata = $query->result();
            
       // SELECT SUM(`pay_amount`) FROM `mes_payment_main` WHERE `pay_cust_id`='$cust' AND `pay_authrize`=1 
        $this->db->select("SUM(`pay_amount`) as totp,sum(pay_balance) as pay_bal");    
            $this->db->from('mes_payment_main');
            $this->db->where('pay_cust_id', $cust);
            $this->db->where('pay_authrize', 1);
            $this->db->where('pay_stetus', 1);
            $query = $this->db->get();
                    $pdata = $query->result();
                        
            $this->db->select("cust_pay_excess");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id', $cust);
            $query = $this->db->get();
                    $exdata = $query->result();           
                        
            $out = $sdata[0]->tot - (($sdata[0]->ret + $pdata[0]->totp) - $pdata[0]->pay_bal);          
            return $out;
                        
    }
    
    function outstandqto($inv){
        
        $this->db->select("SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret");    
            $this->db->from('mes_qto');
            $this->db->where('po_id', $inv);
            $this->db->where('po_payment', 0);
            $query = $this->db->get();
            $sdata = $query->result();
            
       // SELECT SUM(`pay_amount`) FROM `mes_payment_main` WHERE `pay_cust_id`='$cust' AND `pay_authrize`=1 
        $this->db->select("SUM(`pay_amount`) as totp");    
            $this->db->from('mes_payment');
            $this->db->where('pay_invid', $inv);
            //$this->db->where('pay_authrize', 1);
            $this->db->where('pay_stetus', 1);
            $query = $this->db->get();
            $pdata = $query->result();
                        
                  
            $outinv = $sdata[0]->tot - ($sdata[0]->ret + $pdata[0]->totp);          
            return $outinv;
                        
    }
    
        function outstandqto_($inv){
        
        $this->db->select("SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret");    
            $this->db->from('mes_qto');
            $this->db->where('po_id', $inv);
            $this->db->where('po_payment', 0);
            $query = $this->db->get();
            $sdata = $query->result();
            
       // SELECT SUM(`pay_amount`) FROM `mes_payment_main` WHERE `pay_cust_id`='$cust' AND `pay_authrize`=1 
        $this->db->select("SUM(`pay_amount`) as totp");    
            $this->db->from('mes_payment');
            $this->db->where('pay_invid', $inv);
            //$this->db->where('pay_authrize', 1);
            $this->db->where('pay_stetus', 1);
            $query = $this->db->get();
            $pdata = $query->result();
                        
                  
            $outinv = $sdata[0]->tot - ($sdata[0]->ret + $pdata[0]->totp);          
            return $outinv;
                        
    }
    
    function inv_balance($inv){
        //with unauthorized
        
        $this->db->select("SUM(`po_final_tot`) as tot,SUM(`return_amnt`) as ret");    
            $this->db->from('mes_qto');
            $this->db->where('po_id', $inv);
          //  $this->db->where('po_payment', 0);
            $query = $this->db->get();
            $sdata = $query->result();
            
       // SELECT SUM(`pay_amount`) FROM `mes_payment_main` WHERE `pay_cust_id`='$cust' AND `pay_authrize`=1 
        $this->db->select("SUM(`pay_amount`) as totp");    
            $this->db->from('mes_payment');
            $this->db->where('pay_invid', $inv);
            //$this->db->where('pay_authrize', 1);
            $this->db->where('pay_stetus', 1);
            $query = $this->db->get();
            $pdata = $query->result();
            
            $this->db->select("SUM(`ret_amount`) as unret");    
            $this->db->from('inv_return');
            $this->db->where('ret_invnum', $inv);
            $this->db->where('ret_proceed', 0);
            $this->db->where('ret_stetus', 1);
            $query = $this->db->get();
            $retdata = $query->result();
                        
                  
            $outinv = $sdata[0]->tot - ($sdata[0]->ret + $pdata[0]->totp +$retdata[0]->unret);          
            return $outinv;
                        
    }
    
    function return_excess($inv,$amnt,$cal){
            $this->db->select("mes_qto.po_sup_id,mes_customer.cust_return_excess,mes_customer.cust_id");    
            $this->db->from('mes_qto');
            $this->db->join('mes_customer', 'mes_qto.po_sup_id = mes_customer.cust_id');
            $this->db->where('mes_qto.po_id', $inv);
            $this->db->where('mes_qto.po_status', 1);
            $query = $this->db->get();
                        $sdata = $query->result();
                        
            $cus_bal = $sdata[0]->cust_return_excess;
            if($cal=="plus"){$new_bal = $cus_bal+$amnt;}
            else{
                $new_bal = $cus_bal-$amnt;
            }
        
                    $this->db->set('cust_return_excess', $new_bal);
                    $this->db->where('cust_id',$sdata[0]->cust_id);
                    $this->db->update('mes_customer');
             return "done";        
                    
    }
    
    function return_excess_cust($cust,$amnt,$cal){
            $this->db->select("mes_customer.cust_return_excess,mes_customer.cust_id");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id', $cust);
            $query = $this->db->get();
                        $sdata = $query->result();
                        
            $cus_bal = $sdata[0]->cust_return_excess;
            if($cal=="plus"){$new_bal = $cus_bal+$amnt;}
            else{
                $new_bal = $cus_bal-$amnt;
            }
        
                    $this->db->set('cust_return_excess', $new_bal);
                    $this->db->where('cust_id',$sdata[0]->cust_id);
                    $this->db->update('mes_customer');
             return "done";        
                    
    }
    
    function payment_excess($cust,$amnt,$cal){
            $this->db->select("*");    
            $this->db->from('mes_customer');
            $this->db->where('cust_id', $cust);
            $query = $this->db->get();
                        $sdata = $query->result();
                        
            $cus_bal = $sdata[0]->cust_pay_excess;
            if($cal=="plus"){$new_bal = $cus_bal+$amnt;}
            else{
                $new_bal = $cus_bal-$amnt;
            }
        
                    $this->db->set('cust_pay_excess', $new_bal);
                    $this->db->where('cust_id',$cust);
                    $this->db->update('mes_customer');
             return "done";        
                    
    }
    
    
}