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

class Order extends CI_Controller {
    
    public function __construct() {
        parent::__construct();
         if (! $this->session->userdata ( 'user_logged_in' ) || !company_url($this->session->userdata ('company_slag'))) {
            redirect ( 'login', 'refresh' );
        }
        $this->load->vars ( array (
                'global' => 'Available to all views',
                'header' => 'common/header',
                'left' => 'common/left',
                'footer' => 'common/footer' 
        ) );
        $this->load->model('User_model');
        $this->load->model('Item_model');
        $this->load->model('Order_model');
        $this->load->model('Invoice_model');
        $this->load->model('Setings_model');
    }

    /*public function index(){
        $data['bank_list'] = $this->Bank_model->get_bank_list();		
		$bank_deposit_list = $this->Bank_model->get_bank_deposit_details();
        $data['bank_deposit_list'] = array();
        if (count($bank_deposit_list) > 0) {
            foreach ($bank_deposit_list as $key => $value) {
                $data['bank_deposit_list'][$value->account_id]['debit'] = $value->debit;
                $data['bank_deposit_list'][$value->account_id]['credit'] = $value->credit;
            }
        }		
        $this->load->view('bank_list',$data);		
    }*/
    
    
    
    public function index(){
        $data['order_list'] = $this->Order_model->get_order_list();		
		
        //$bank_deposit_list = $this->Bank_model->get_bank_deposit_details();
		//$bank_expance_list = $this->Bank_model->get_bank_expance_details();
		
		//print_r($bank_expance_list);
		//print_r($bank_deposit_list);
		
        // $data['bank_deposit_list'] = array();
        // if (count($bank_deposit_list) > 0) {
        //     foreach ($bank_deposit_list as $key => $value) {
        //         $data['bank_deposit_list'][$value->account_id]['debit'] = $value->debit;
        //         $data['bank_deposit_list'][$value->account_id]['credit'] = $value->credit;
        //     }
        // }			
		
		// if (count($bank_expance_list) > 0) {
        //     foreach ($bank_expance_list as $key => $value) {
        //         $data['bank_deposit_list'][$value->account_id]['debit'] += $value->debit;
        //     }
        // }	
        
       /* echo "<pre>";
        print_r($data);
        exit(); */		
		
        $this->load->view('order_list',$data);		
    } 

    public function print_order(){
        $data['setings'] = $this->Setings_model->get_setings();
        $data['order_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['order_data'] = $this->Order_model->get_order_details($data['order_id']);	
        $this->load->view('order_print',$data);		
    }   

    public function neworder(){
        $data['order_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['parent_order_id'] = 0;
        if ($data['order_id'] > 0) {
            $data['order_data'] = $this->Order_model->get_order_details($data['order_id']);
            //$data['order_child_data'] = $this->Order_model->get_order_child_details($data['order_id']);
            $data['order_data_detail_qty'] = $this->Order_model->get_order_details_qty($data['order_id']);
            $data['order_current_step_qty'] = $this->Order_model->get_order_step_qty($data['order_data']->order_status_step_id);
            $data['order_step_data'] = $this->Order_model->get_order_step_details($data['order_id']);
            $where = array('contact_type' => 2,'status' => 1);
            $data['customer_list'] = $this->User_model->user_list_where($where);
            $data['item_list'] = $this->Item_model->get_items();
            $data['order_step'] = $this->Order_model->get_order_steps();
            $item_gst = array();
            $item_sale_price = array();
            $item_uom = array();
            $item_hsn = array();
            $items_autocomplete = array();
            $items_barcode = array();
            if (count($data['item_list'])>0) {
                foreach ($data['item_list'] as $key => $value) {
                    $items_autocomplete[$key] = new stdClass();
                    $items_autocomplete[$key]->id = $value->item_id;
                    $items_autocomplete[$key]->name = $value->item_name;
    
                    $items_barcode[$value->item_no] = $value;
    
                    $item_hsn[$value->item_id]=$value->hsn_code;
                    $item_gst[$value->item_id]=$value->gst_rate;
                    $item_sale_price[$value->item_id]=$value->rate_sale;
                    $item_uom[$value->item_id]=$value->unit;
                }
            }
            $data['items_autocomplete'] = json_encode($items_autocomplete);
            $data['items_barcode'] = json_encode($items_barcode);
            $data['item_hsn'] = json_encode($item_hsn);
            $data['item_gst'] = json_encode($item_gst);
            $data['item_sale_price'] = json_encode($item_sale_price);
            $data['item_uom'] = json_encode($item_uom);
        }else{            
            $where = array('contact_type' => 2,'status' => 1);
            $data['customer_list'] = $this->User_model->user_list_where($where);
            $data['item_list'] = $this->Item_model->get_items();
            $data['order_step'] = $this->Order_model->get_order_steps();
            $item_gst = array();
            $item_sale_price = array();
            $item_uom = array();
            $item_hsn = array();
            $items_autocomplete = array();
            $items_barcode = array();
            if (count($data['item_list'])>0) {
                foreach ($data['item_list'] as $key => $value) {
                    $items_autocomplete[$key] = new stdClass();
                    $items_autocomplete[$key]->id = $value->item_id;
                    $items_autocomplete[$key]->name = $value->item_name;
    
                    $items_barcode[$value->item_no] = $value;
    
                    $item_hsn[$value->item_id]=$value->hsn_code;
                    $item_gst[$value->item_id]=$value->gst_rate;
                    $item_sale_price[$value->item_id]=$value->rate_sale;
                    $item_uom[$value->item_id]=$value->unit;
                }
            }
            $data['items_autocomplete'] = json_encode($items_autocomplete);
            $data['items_barcode'] = json_encode($items_barcode);
            $data['item_hsn'] = json_encode($item_hsn);
            $data['item_gst'] = json_encode($item_gst);
            $data['item_sale_price'] = json_encode($item_sale_price);
            $data['item_uom'] = json_encode($item_uom);
        }
        $this->load->view('neworder',$data);
    }
    
    public function newsuborder(){
        $data['parent_order_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $data['order_data'] = $this->Order_model->get_order_details($data['parent_order_id']);
        $data['order_id'] = 0;
        $where = array('contact_type' => 2,'status' => 1);
        $data['customer_list'] = $this->User_model->user_list_where($where);
        $data['item_list'] = $this->Item_model->get_items();
        $data['order_step'] = $this->Order_model->get_order_steps();
        $item_gst = array();
        $item_sale_price = array();
        $item_uom = array();
        $item_hsn = array();
        $items_autocomplete = array();
        $items_barcode = array();
        if (count($data['item_list'])>0) {
            foreach ($data['item_list'] as $key => $value) {
                $items_autocomplete[$key] = new stdClass();
                $items_autocomplete[$key]->id = $value->item_id;
                $items_autocomplete[$key]->name = $value->item_name;

                $items_barcode[$value->item_no] = $value;

                $item_hsn[$value->item_id]=$value->hsn_code;
                $item_gst[$value->item_id]=$value->gst_rate;
                $item_sale_price[$value->item_id]=$value->rate_sale;
                $item_uom[$value->item_id]=$value->unit;
            }
        }
        $data['items_autocomplete'] = json_encode($items_autocomplete);
        $data['items_barcode'] = json_encode($items_barcode);
        $data['item_hsn'] = json_encode($item_hsn);
        $data['item_gst'] = json_encode($item_gst);
        $data['item_sale_price'] = json_encode($item_sale_price);
        $data['item_uom'] = json_encode($item_uom);
        $this->load->view('neworder',$data);
    }

    public function save_order(){

        $parent_order_id  = $this->input->post('parent_order_id');
        $order_id  = $this->input->post('order_id');
        $status_id  = $this->input->post('status_id');
        $worker_id  = $this->input->post('worker_id');
        $metal  = $this->input->post('metal');
        $item_id    = $this->input->post('item_id');
        $qty  = $this->input->post('qty'); 
        $weight_assign  = $this->input->post('weight_assign');
        $loss  = $this->input->post('loss');
        $weight_receive = $this->input->post('weight_receive');
        $date_assign  = $this->input->post('date_assign');
        $date_receive  = $this->input->post('date_receive');
        
            $order_data = array(
                'parent_order_id' => $parent_order_id,
                'item_id' => $item_id,  
                'qty' => $qty,  
                'metal' => $metal,
                'current_status' => $status_id,
                'cur_worker_id' => $worker_id,
                'date_added' => $date_assign
            );

            $order_data_update = array(
                'item_id' => $item_id,  
                'qty' => $qty,  
                'metal' => $metal,
                'current_status' => $status_id,
                'cur_worker_id' => $worker_id,
                'date_updated' => $date_assign
            );

            if ($order_id == "0") {
                $order_id = $this->Order_model->insert_order($order_data);
            } else {
               $this->Order_model->update_order($order_data_update,$order_id);
            }

            //exit;

            $order_step_detail = array(
                'order_id' => $order_id,  
                'status_id' => $status_id,  
                'worker_id' => $worker_id,
                'item_id' => $item_id,  
                'qty' => $qty,        
                'weight_assign' => $weight_assign, 
                'loss' => $loss, 
                'is_current_status' => '1',    
                'date_assign' => $date_assign   
            );

            $order_step_detail_update = array( 
                'is_current_status' => '0'   
            );

            $this->Order_model->update_insert_order_step_detail_status($order_step_detail_update,$order_id);
            $this->Order_model->insert_order_step_detail($order_step_detail);            

        echo $order_id;   
    }

    public function receve_order(){

        $order_id  = $this->input->post('order_id');
        $order_status_step_id  = $this->input->post('order_status_step_id');
        $date_receive_old  = $this->input->post('date_receive_old');
        $status_id  = $this->input->post('status_id');
        $worker_id  = $this->input->post('worker_id');
        $item_id    = $this->input->post('item_id');
        $qty  = $this->input->post('qty'); 
        $qty_received  = $this->input->post('qty_received'); 
        $qty_left  = $this->input->post('qty_left');
        $weight_assign  = $this->input->post('weight_assign');
        $weight_receive = $this->input->post('weight_receive');
        $weight_receive_till = $this->input->post('weight_receive_till');
        $date_assign  = $this->input->post('date_assign');
        $date_receive  = $this->input->post('date_receive');

        

            // $order_data = array(
            //     'item_id' => $item_id,  
            //     'qty' => $qty,  
            //     'current_status' => $status_id,
            //     'date_added' => $date_assign,  
            //     'date_updated' => $date_assign
            // );

            // if (1) {
            //    $oid = $this->Order_model->insert_order($order_data);
            // } else {
            //    $this->Order_model->update_order($order_data,$oid);
            // }

            //exit;


            if($status_id == 6){
                $item_stock = array(
                    'item_id' => $item_id,  
                    'qty_purchase' => $qty,  
                    'date_added' => $date_receive,
                    'is_note' => "2" 
                );
                $this->Order_model->insert_item_stock($item_stock);

                $order_data_update = array(                    
                    'current_status' => $status_id,
                    'cur_worker_id' => '19',
                    'date_updated' => $date_assign
                );
                $this->Order_model->update_order($order_data_update,$order_id);
            }else{
                if($qty_left == $qty){
                    $qty2 = $qty + $qty_received;
                    $weight_receive2 = $weight_receive + $weight_receive_till;
                    $order_step_detail = array(
                        'order_id' => $order_id,  
                        'status_id' => $status_id,  
                        'worker_id' => $worker_id,
                        'item_id' => $item_id,  
                        'qty_receive' => $qty2,        
                        'weight_assign' => $weight_assign,
                        'weight_receive' => $weight_receive2,   
                        'is_current_status' => '1',    
                        'date_receive' => $date_receive   
                    );
                }else{
                    $qty2 = $qty + $qty_received;
                    $weight_receive2 = $weight_receive + $weight_receive_till;
                    $order_step_detail = array(
                        'order_id' => $order_id,  
                        'status_id' => $status_id,  
                        'worker_id' => $worker_id,
                        'item_id' => $item_id,  
                        'qty_receive' => $qty2,        
                        'weight_assign' => $weight_assign,
                        'weight_receive' => $weight_receive2,   
                        'is_current_status' => '1'  
                    );
                }         

                $item_received_qty = array(
                        'order_status_step_id' => $order_status_step_id,  
                        'qty_receive' => $qty,  
                        'weight_receive' => $weight_receive,  
                        'date_added' => $date_receive
                    );
                            
                $this->Order_model->update_insert_order_step_detail($order_step_detail,$order_id);
                $this->Order_model->insert_item_received_qty($item_received_qty);
            }

        echo 1;   
    }
	
	public function deposit(){
        $data['bank_id'] = !empty($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $where_bank = array('status' => 1);
        $data['accounts'] = $this->Invoice_model->get_from_table('bank_accounts','*',$where_bank);
        $this->load->view('new_bank_deposit',$data);
    }

    public function deposit_amount(){
        $deposit_arr['to_acc_id'] = $this->input->post('bank_id'); 
        $deposit_arr['from_acc_id'] = $this->input->post('from_acc_id'); 
        $deposit_arr['receive_via'] = $this->input->post('receive_via'); 
        $deposit_arr['reference'] = $this->input->post('reference'); 
        $deposit_arr['amount'] = $this->input->post('amount'); 
        $deposit_arr['notes'] = $this->input->post('notes'); 
        $t_date = $this->input->post('tran_date');
        $arr1 = explode("/",$t_date);
        $deposit_arr['tran_date'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $deposit_arr['date_added'] = $arr1[2]."-".$arr1[1]."-".$arr1[0];
        $this->Bank_model->insert_bank_deposit($deposit_arr);
        echo 1;
    }
	
}
?>