<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Order_model extends CI_Model {
    
    public function __construct() {
        parent::__construct();
        $this->db = $this->load->database('default', TRUE);
        $CI =& get_instance();
        $CI->db =& $this->db;
    }

    public function get_order_list(){ // , ac3.name, ac4.item_name, o1.qty, ac1.status_name, o1.date_added
        
        $query = 'select 
            o1.order_id, o1.parent_order_id, o1.metal, ac3.name, ac4.item_name, o1.qty, ac1.status_name, o1.date_added, ac2.worker_id
        from 
            gst_order o1
        join
            gst_order_status ac1
        on
            o1.current_status = ac1.status_id
        join
            `gst_order_step_detail` ac2
        on
            o1.order_id = ac2.order_id
        join
            gst_contact_user ac3
        on
            ac3.id = o1.cur_worker_id
        join
            gst_items ac4
        on
            ac2.item_id = ac4.item_id
        group by
            ac2.order_id
        order by 
            ac2.order_status_step_id desc';

        return $this->db->query($query)->result();
        // echo "<pre>";print_r($data);
        // echo $this->db->last_query();die();
    }
	
	public function get_bank_deposit_details(){
        $query = 'select 
            gst_bank_accounts.account_id,if(gst_bank_accounts.account_id=ac1.from_acc_id,sum(ac1.amount),0) as debit,if(gst_bank_accounts.account_id=ac2.to_acc_id,sum(ac2.amount),0) as credit
        from 
            gst_bank_accounts
        left join
            gst_bank_diposit ac1
        on
            gst_bank_accounts.account_id = ac1.from_acc_id
        left join
            gst_bank_diposit ac2
        on
            gst_bank_accounts.account_id = ac2.to_acc_id
        group by
            gst_bank_accounts.account_id';
        return $this->db->query($query)->result();
    }
    
    
    public function get_bank_expance_details(){
        $query = 'select 
            gst_bank_accounts.account_id,if(gst_bank_accounts.account_id=ac1.paid_through_acc,sum(ac1.amount),0) as debit
        from 
            gst_bank_accounts
        left join
            gst_expences ac1
        on
            gst_bank_accounts.account_id = ac1.paid_through_acc        
        group by
            gst_bank_accounts.account_id';
        return $this->db->query($query)->result();
    }   

    public function get_order_details_qty($order_id){
        $query = 'select 
                        ac3.*
                    from 
                        gst_order o1                    
                    join
                        gst_order_step_detail ac2
                    on
                        o1.order_id = ac2.order_id
                    join
                        gst_order_stock_detail  ac3
                    on
                        ac2.order_status_step_id = ac3.order_status_step_id                    
                    where
                        o1.order_id = "'.$order_id.'"
                    order by 
                        ac3.order_status_step_id desc';

        return $this->db->query($query)->result();
    }
    
    public function get_order_details($order_id){
        $query = 'select 
                        o1.order_id, o1.metal, ac3.name, ac4.item_name, ac4.image, o1.qty, ac1.status_name, o1.date_added, ac2.*
                    from 
                        gst_order o1
                    join
                        gst_order_status ac1
                    on
                        o1.current_status = ac1.status_id
                    join
                        gst_order_step_detail ac2
                    on
                        o1.order_id = ac2.order_id
                    join
                        gst_contact_user ac3
                    on
                        ac2.worker_id = ac3.id
                    join
                        gst_items ac4
                    on
                        ac2.item_id = ac4.item_id
                    where
                        o1.order_id = "'.$order_id.'"
                    order by 
                        ac2.order_status_step_id desc';

        return $this->db->query($query)->row();

    }
    
    public function get_order_step_details($order_id){
        $query = 'select 
                        o1.order_id, o1.metal, ac3.name, ac4.item_name, o1.qty, ac1.status_name, o1.date_added, ac2.*
                    from 
                        gst_order o1                    
                    join
                        gst_order_step_detail ac2
                    on
                        o1.order_id = ac2.order_id
                    join
                        gst_order_status ac1
                    on
                        ac2.status_id = ac1.status_id
                    join
                        gst_contact_user ac3
                    on
                        ac2.worker_id = ac3.id
                    join
                        gst_items ac4
                    on
                        ac2.item_id = ac4.item_id
                    where
                        o1.order_id = "'.$order_id.'"                    
                    order by 
                        ac2.order_status_step_id';

    return $this->db->query($query)->result();

    }
    
    public function get_order_step_qty($order_step_id){
        $query = 'select 
                        sum(qty_receive) as qty_receive, sum(weight_receive) as weight_receive
                    from 
                        gst_order_stock_detail   
                    where
                        order_status_step_id = "'.$order_step_id.'"                    
                    group by 
                        order_status_step_id';

    return $this->db->query($query)->result();

    }
    
    public function get_order_steps(){
    	return $this->db->select('*')
    					->from('order_status')
    					->get()
    					->result();
    }

    public function insert_order($order_data){
    	 $this->db->insert('order',$order_data);
         return $this->db->insert_id();
    }

    public function insert_item_received_qty($item_received_qty){
    	 $this->db->insert('order_stock_detail',$item_received_qty);
         return $this->db->insert_id();
    }

    public function insert_item_stock($item_stock){
    	 $this->db->insert('item_stock',$item_stock);
         return $this->db->insert_id();
    }
	
	public function insert_order_step_detail($order_step_detail){
    	$this->db->insert('order_step_detail',$order_step_detail);
    }

    public function update_order($order_data,$order_id){
    	$this->db->where('order_id',$order_id);
    	$this->db->update('order',$order_data);
    }
    
    public function update_insert_order_step_detail($order_step_detail,$order_id){
    	$this->db->where('order_id',$order_id);
        $this->db->where('is_current_status','1');
    	$this->db->update('order_step_detail',$order_step_detail);
        //print_r($this->db->last_query()); 
    }
    
    public function update_insert_order_step_detail_status($order_step_detail,$order_id){
    	$this->db->where('order_id',$order_id);
        $this->db->where('is_current_status','1');
    	$this->db->update('order_step_detail',$order_step_detail);
        //print_r($this->db->last_query()); 
    }
}