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

class Ordermdl extends CI_model
{
    
        
	/*order master*/
	public function get_all_order_data_cnt()
    {
        // echo "<pre>"; print_r($_GET);exit;
        $subsql = "";
    
        if(!empty($_GET['search_value_entry_no']))
        {
            $subsql .= " AND om.om_id =".$_GET['search_value_entry_no'];
        } 
        if(!empty($_GET['search_value_entry_date']))
        {
            $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
            $subsql .= " AND om.om_entry_date = '$entry_date'";
        }
        if(!empty($_GET['search_value_cust_id']))
        {
            $subsql .= " AND om.om_cust_id =".$_GET['search_value_cust_id'];
        }
        if(!empty($_GET['search_value_broker_id']))
        {
            $subsql .= " AND om.om_broker_id =".$_GET['search_value_broker_id'];
        }
        return $this->db->query("SELECT om.* 
                FROM order_master om WHERE om_flag = 'diam' $subsql")->num_rows();
    }

    public function get_all_order_data($per_page, $offset)
    {
        $subsql = "";
    
        if(!empty($_GET['search_value_entry_no']))
        {
            $subsql .= " AND om.om_id =".$_GET['search_value_entry_no'];
        }
        if(!empty($_GET['search_value_entry_date']))
        {
            $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
            $subsql .= " AND om.om_entry_date = '$entry_date'";
        }
        if(!empty($_GET['search_value_cust_id']))
        {
            $subsql .= " AND om.om_cust_id =".$_GET['search_value_cust_id'];
        }
        if(!empty($_GET['search_value_broker_id']))
        {
            $subsql .= " AND om.om_broker_id =".$_GET['search_value_broker_id'];
        }
        $record['order_data'] = $this->db->query("SELECT om.*, cust.acc_name as customer_name, brk.acc_name as broker_name
                FROM order_master om
                LEFT JOIN account_master cust ON(cust.acc_id =om.om_cust_id)
                LEFT JOIN account_master brk ON(brk.acc_id = om.om_broker_id)
                WHERE om_flag = 'diam' $subsql 
                ORDER BY om.om_id DESC 
                LIMIT $per_page 
                OFFSET $offset")->result_array();

        $order_data1 = $this->db->query("SELECT om.* FROM order_master om")->result_array();
        // entry no
        $record['entry_no_drop_down'][0] = 'ALL';
        foreach ($order_data1 as $key => $value) 
        {
        	$record['entry_no_drop_down'][$value['om_id']] = $value['om_entry_no'];
        }
        // entry no

        // customer
        $customer_data = $this->db->query("SELECT cust.acc_id, cust.acc_name FROM order_master om
                LEFT JOIN account_master cust ON(cust.acc_id = om.om_cust_id)
                WHERE cust.acc_grp_id = 5 ORDER BY om.om_id DESC")->result_array();
        $record['customer_drop_down'][0] = 'ALL';
        foreach ($customer_data as $key => $value) 
        {
            $record['customer_drop_down'][$value['acc_id']] = strtoupper($value['acc_name']);
        }
        // customer

        // broker
        $broker_data = $this->db->query("SELECT brk.acc_id, brk.acc_name FROM order_master om
                LEFT JOIN account_master brk ON(brk.acc_id = om.om_broker_id)
                WHERE brk.acc_grp_id = 23 ORDER BY om.om_id DESC")->result_array();
        $record['broker_drop_down'][0] = 'ALL';
        foreach ($broker_data as $key => $value) 
        {
            $record['broker_drop_down'][$value['acc_id']] = strtoupper($value['acc_name']);
        }
        // broker
        
        return $record;  
    }

    public function get_order_data_for_edit($om_id)
    {
        $record = [];
        $record['order_data'] = $this->db->query("SELECT om.*, cust.acc_name as customer, cust.acc_address, cust.acc_gst as customer_gst, cust.acc_mobile as customer_mobile, cust.acc_tel_1 as qbc_no, brk.acc_name as broker, brk.acc_pan as broker_pan, brk.acc_mobile as broker_mobile, cust.acc_type
            FROM order_master om 
            LEFT JOIN account_master cust ON(cust.acc_id = om.om_cust_id) 
            LEFT JOIN account_master brk ON(brk.acc_id = om.om_broker_id) 
            WHERE om.om_id = $om_id")->result_array();
          
        $record['order_data'][0]['is_editable'] = 1;
        $record['order_trans'] = $this->db->query("SELECT ot.*, dm.* FROM order_trans ot 
            LEFT JOIN diamond_master dm ON(dm.dm_id = ot.ot_dm_id) WHERE ot.ot_om_id = $om_id")->result_array();
        
        // echo "<pre>";
        // print_r($record['order_data']);
        // exit;

        // customer drop down
        $customer_query = "SELECT cust.* FROM account_master cust WHERE cust.acc_grp_id = 5";
        $customer_data = $this->db->query($customer_query)->result_array();
        $record['customer_data'][0] = 'PLEASE SELECT';
        foreach ($customer_data as $key => $value) 
        {
            $record['customer_data'][$value['acc_id']] = strtoupper($value['acc_name']);
        }
        // customer drop down

        // broker drop down
        $broker_query = "SELECT brk.* FROM account_master brk WHERE brk.acc_grp_id = 23";
        $broker_data = $this->db->query($broker_query)->result_array();
        $record['broker_data'][0] = 'PLEASE SELECT';
        foreach ($broker_data as $key => $value) 
        {
            $record['broker_data'][$value['acc_id']] = strtoupper($value['acc_name']) .' - '.$value['acc_pan'].' - '.$value['acc_mobile'];
        }
        // broker drop down            

        // diamond drop down
        $diamond_query = "SELECT dm.* FROM diamond_master dm WHERE dm.dm_status = 0 OR dm.dm_status = 4";
        $diamond_data = $this->db->query($diamond_query)->result_array();
        $record['diamond_data'][0] = 'PLEASE SELECT';
        foreach ($record['order_trans'] as $key => $value) {
            $record['diamond_data'][$value['ot_dm_id']] = strtoupper($value['ot_dm_code']);
            if($value['ot_delete'] == 0)
            {
                $record['cm_data'][0]['is_editable'] = 0;
            }
        }
        foreach ($diamond_data as $key => $value) 
        {
            $record['diamond_data'][$value['dm_id']] = strtoupper($value['dm_code']);
        }
        
        // diamond drop down            

        return $record;
    }

    // End Model
}