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

    class Reportmdl extends CI_model{
    /*Stock Report*/
        public function get_all_stock_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

        // if(!empty($_REQUEST['search_value_supplier_id']))
        // {
        //     $supplier_id = implode(',', $_REQUEST['search_value_supplier_id']);
        //     // echo "<pre>"; print_r($supplier_id); exit;
        //     $subsql .= " AND pm.per_supplier_id IN (".$supplier_id. ")";
        // }
        if(!empty($_REQUEST['search_value_stock_id']))
        {
            $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
        }
        if(!empty($_REQUEST['search_value_size']))
        {
            $size = '';
            foreach ($_REQUEST['search_value_size'] as $key => $value)
            {
                $db_size = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_size'];
                $size .= "'".$db_size."',";
            }
            $size = rtrim($size, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_size IN (".$size. ")";
        }
        if(!empty($_REQUEST['search_value_cpd']))
        {
            $cpd = '';
            foreach ($_REQUEST['search_value_cpd'] as $key => $value)
            {
                $db_cpd = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cpd'];
                $cpd .= "'".$db_cpd."',";
            }
            $cpd = rtrim($cpd, ',');
            // echo "<pre>"; print_r($cpd); exit;
            $subsql .= " AND dm.dm_cpd IN (".$cpd. ")";
        }
        if(!empty($_REQUEST['search_value_sh']))
        {
            $sh = '';
            foreach ($_REQUEST['search_value_sh'] as $key => $value)
            {
                $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                $sh .= "'".$db_sh."',";
            }
            $sh = rtrim($sh, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_sh IN (".$sh. ")";
        }
        if(!empty($_REQUEST['search_value_wt']))
        {
            $array = str_replace( "-", " AND ", $_REQUEST['search_value_wt']);
            /*$wt = implode(',',$array);*/
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= "AND";
            $weight = '';
             foreach($array as $wt)
             {
                $weight .= " dm.dm_wt BETWEEN ".$wt."OR";
             }
              $subsql .= rtrim($weight,"OR");
        }
        if(!empty($_REQUEST['search_value_col']))
        {
            $col = '';
            foreach ($_REQUEST['search_value_col'] as $key => $value)
            {
                $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                $col .= "'".$db_col."',";
            }
            $col = rtrim($col, ',');
            // echo "<pre>"; print_r($col); exit;
            $subsql .= " AND dm.dm_col IN (".$col. ")";
        }
        if(!empty($_REQUEST['search_value_cla']))
        {
            $cla = '';
            foreach ($_REQUEST['search_value_cla'] as $key => $value)
            {
                $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                $cla .= "'".$db_cla."',";
            }
            $cla = rtrim($cla, ',');
            // echo "<pre>"; print_r($cla); exit;
            $subsql .= " AND dm.dm_cla IN (".$cla. ")";
        }
        if(!empty($_REQUEST['search_value_cut']))
        {
            $cut = '';
            foreach ($_REQUEST['search_value_cut'] as $key => $value)
            {
                $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                $cut .= "'".$db_cut."',";
            }
            $cut = rtrim($cut, ',');
            // echo "<pre>"; print_r($cut); exit;
            $subsql .= " AND dm.dm_cut IN (".$cut. ")";
        }
        if(!empty($_REQUEST['search_value_pol']))
        {
            $pol = '';
            foreach ($_REQUEST['search_value_pol'] as $key => $value)
            {
                $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                $pol .= "'".$db_pol."',";
            }
            $pol = rtrim($pol, ',');
            // echo "<pre>"; print_r($pol); exit;
            $subsql .= " AND dm.dm_pol IN (".$pol. ")";
        }
        if(!empty($_REQUEST['search_value_sym']))
        {
            $sym = '';
            foreach ($_REQUEST['search_value_sym'] as $key => $value)
            {
                $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                $sym .= "'".$db_sym."',";
            }
            $sym = rtrim($sym, ',');
            // echo "<pre>"; print_r($sym); exit;
            $subsql .= " AND dm.dm_sym IN (".$sym. ")";
        }
        if(!empty($_REQUEST['search_value_fl']))
        {
            $fl = '';
            foreach ($_REQUEST['search_value_fl'] as $key => $value)
            {
                $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                $fl .= "'".$db_fl."',";
            }
            $fl = rtrim($fl, ',');
            // echo "<pre>"; print_r($fl); exit;
            $subsql .= " AND dm.dm_fl IN (".$fl. ")";
        }
        if(!empty($_REQUEST['search_value_lab']))
        {
            $lab = '';
            foreach ($_REQUEST['search_value_lab'] as $key => $value)
            {
                $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                $lab .= "'".$db_lab."',";
            }
            $lab = rtrim($lab, ',');
            // echo "<pre>"; print_r($lab); exit;
            $subsql .= " AND dm.dm_lab IN (".$lab. ")";
        }
           if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
        {
            if($_REQUEST['search_value_status'] == 1)
            {
                $status = 0;
            }
            elseif ($_REQUEST['search_value_status'] == 2)
            {
                $status = 1;
            }
            elseif ($_REQUEST['search_value_status'] == 3)
            {
                $status = 2;
            }
            else
            {
                $status = 3;
            }
            $subsql .= " AND dm.dm_status =".$status;
        }
        else
        {
            $subsql .= " AND (dm.dm_status != 3)";
        }
        if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
        {
            $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
            $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
            $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }
        else
        {
            $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }


       if(!empty($_REQUEST['search_value_from_dollar']) && !empty($_REQUEST['search_value_to_dollar']))
        {
            $subsql .= " AND dm.dm_dollar BETWEEN ".$_REQUEST['search_value_from_dollar']." AND ".$_REQUEST['search_value_to_dollar'];
        }

        if(!empty($_REQUEST['search_value_from_ppc']) && !empty($_REQUEST['search_value_to_ppc']))
        {
            $subsql .= " AND dm.dm_price_ct BETWEEN ".$_REQUEST['search_value_from_ppc']." AND ".$_REQUEST['search_value_to_ppc'];
        }

        if(!empty($_REQUEST['search_value_from_pc_amt']) && !empty($_REQUEST['search_value_to_pc_amt']))
        {
            $subsql .= " AND dm.dm_pc_amt BETWEEN ".$_REQUEST['search_value_from_pc_amt']." AND ".$_REQUEST['search_value_to_pc_amt'];
        }

        if(!empty($_REQUEST['search_value_from_dollar_amt']) && !empty($_REQUEST['search_value_to_dollar_amt']))
        {
            $subsql .= " AND dm.dm_dollar_amt BETWEEN ".$_REQUEST['search_value_from_dollar_amt']." AND ".$_REQUEST['search_value_to_dollar_amt'];
        }

        if(!empty($_REQUEST['search_value_key2sym']))
        {

            $db_key2sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$_REQUEST['search_value_key2sym']]);
            if(!empty($db_key2sym))
            {
                $key2sym = $db_key2sym[0]['dm_key2sym'];
                $subsql .= " AND dm.dm_key2sym = '".$key2sym."'";
            }
        }
        if(!empty($_REQUEST['search_value_r2net']))
        {
            $dm_r2net = $_REQUEST['search_value_r2net'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_r2net = ".$dm_r2net;
        }

        if(!empty($_REQUEST['search_value_suspense']))
        {
            $dm_suspense = $_REQUEST['search_value_suspense'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_suspense = ".$dm_suspense;
        }

        if (isset($_REQUEST['search_value_category']) && $_REQUEST['search_value_category'] !== '') 
        {
            $cat = $_REQUEST['search_value_category'];

            // 2 = ALL → do not apply filter
            if ($cat != 2) {
                // CERTIFIED = 1 → dm_category = 1
                // NON-CERTIFIED = 0 → dm_category = 0
                $dm_category = ($cat == 1 ? 1 : 0);
                $subsql .= " AND dm.dm_category = ".$dm_category;
            }
        }

            return $this->db->query("SELECT dm.*
                    FROM diamond_master dm

                    -- Only show if stock is purchased (exists in purchase_trans)
                    INNER JOIN purchase_trans prt ON prt.prt_dm_code = dm.dm_code
                    LEFT JOIN purchase_master per ON per.per_id = prt.prt_per_id

                    LEFT JOIN challan_trans ct ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 $subsql
                    GROUP BY dm.dm_id
                ")->num_rows();

            

            // return $this->db->query("SELECT 
            //                             per.*, 
            //                             prt.*, 
            //                             dm.*

            //                         FROM purchase_trans prt

            //                         LEFT JOIN purchase_master per 
            //                             ON per.per_id = prt.prt_per_id

            //                         LEFT JOIN diamond_master dm 
            //                             ON dm.dm_code = prt.prt_dm_code
            //         WHERE 1 $subsql
            //     ")->num_rows();

        }

        public function get_all_stock_data($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

        // if(!empty($_REQUEST['search_value_supplier_id']))
        // {
        //     $supplier_id = implode(',', $_REQUEST['search_value_supplier_id']);
        //     // echo "<pre>"; print_r($supplier_id); exit;
        //     $subsql .= " AND pm.per_supplier_id IN (".$supplier_id. ")";
        // }
        if(!empty($_REQUEST['search_value_stock_id']))
        {
            $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= " AND dm.dm_code IN (".$stock_id. ")";
        }
         if(!empty($_REQUEST['search_value_size']))
        {
            $size = '';
            foreach ($_REQUEST['search_value_size'] as $key => $value)
            {
                $db_size = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_size'];
                $size .= "'".$db_size."',";
            }
            $size = rtrim($size, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_size IN (".$size. ")";
        }
        if(!empty($_REQUEST['search_value_cpd']))
        {
            $cpd = '';
            foreach ($_REQUEST['search_value_cpd'] as $key => $value)
            {
                $db_cpd = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cpd'];
                $cpd .= "'".$db_cpd."',";
            }
            $cpd = rtrim($cpd, ',');
            // echo "<pre>"; print_r($cpd); exit;
            $subsql .= " AND dm.dm_cpd IN (".$cpd. ")";
        }
        if(!empty($_REQUEST['search_value_sh']))
        {
            $sh = '';
            foreach ($_REQUEST['search_value_sh'] as $key => $value)
            {
                $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                $sh .= "'".$db_sh."',";
            }
            $sh = rtrim($sh, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_sh IN (".$sh. ")";
        }
        if(!empty($_REQUEST['search_value_wt']))
        {
            $array = str_replace( "-", " AND ", $_REQUEST['search_value_wt']);
            /*$wt = implode(',',$array);*/
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= "AND";
            $weight = '';
             foreach($array as $wt)
             {
                $weight .= " dm.dm_wt BETWEEN ".$wt."OR";
             }
              $subsql .= rtrim($weight,"OR");
        }
        if(!empty($_REQUEST['search_value_col']))
        {
            $col = '';
            foreach ($_REQUEST['search_value_col'] as $key => $value)
            {
                $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                $col .= "'".$db_col."',";
            }
            $col = rtrim($col, ',');
            // echo "<pre>"; print_r($col); exit;
            $subsql .= " AND dm.dm_col IN (".$col. ")";
        }
        if(!empty($_REQUEST['search_value_cla']))
        {
            $cla = '';
            foreach ($_REQUEST['search_value_cla'] as $key => $value)
            {
                $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                $cla .= "'".$db_cla."',";
            }
            $cla = rtrim($cla, ',');
            // echo "<pre>"; print_r($cla); exit;
            $subsql .= " AND dm.dm_cla IN (".$cla. ")";
        }
        if(!empty($_REQUEST['search_value_cut']))
        {
            $cut = '';
            foreach ($_REQUEST['search_value_cut'] as $key => $value)
            {
                $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                $cut .= "'".$db_cut."',";
            }
            $cut = rtrim($cut, ',');
            // echo "<pre>"; print_r($cut); exit;
            $subsql .= " AND dm.dm_cut IN (".$cut. ")";
        }
        if(!empty($_REQUEST['search_value_pol']))
        {
            $pol = '';
            foreach ($_REQUEST['search_value_pol'] as $key => $value)
            {
                $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                $pol .= "'".$db_pol."',";
            }
            $pol = rtrim($pol, ',');
            // echo "<pre>"; print_r($pol); exit;
            $subsql .= " AND dm.dm_pol IN (".$pol. ")";
        }
        if(!empty($_REQUEST['search_value_sym']))
        {
            $sym = '';
            foreach ($_REQUEST['search_value_sym'] as $key => $value)
            {
                $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                $sym .= "'".$db_sym."',";
            }
            $sym = rtrim($sym, ',');
            // echo "<pre>"; print_r($sym); exit;
            $subsql .= " AND dm.dm_sym IN (".$sym. ")";
        }
        if(!empty($_REQUEST['search_value_fl']))
        {
            $fl = '';
            foreach ($_REQUEST['search_value_fl'] as $key => $value)
            {
                $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                $fl .= "'".$db_fl."',";
            }
            $fl = rtrim($fl, ',');
            // echo "<pre>"; print_r($fl); exit;
            $subsql .= " AND dm.dm_fl IN (".$fl. ")";
        }
        if(!empty($_REQUEST['search_value_lab']))
        {
            $lab = '';
            foreach ($_REQUEST['search_value_lab'] as $key => $value)
            {
                $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                $lab .= "'".$db_lab."',";
            }
            $lab = rtrim($lab, ',');
            // echo "<pre>"; print_r($lab); exit;
            $subsql .= " AND dm.dm_lab IN (".$lab. ")";
        }
           if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
        {
            if($_REQUEST['search_value_status'] == 1)
            {
                $status = 0;
            }
            elseif ($_REQUEST['search_value_status'] == 2)
            {
                $status = 1;
            }
            elseif ($_REQUEST['search_value_status'] == 3)
            {
                $status = 2;
            }
            else
            {
                $status = 3;
            }
            $subsql .= " AND dm.dm_status =".$status;
        }
        else
        {
            $subsql .= " AND (dm.dm_status != 3)";
        }
        if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
        {
            $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
            $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
            $subsql .= " AND DATE_FORMAT(per.per_create_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }
        else
        {
            $subsql .= " AND DATE_FORMAT(per.per_create_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }


       if(!empty($_REQUEST['search_value_from_dollar']) && !empty($_REQUEST['search_value_to_dollar']))
        {
            $subsql .= " AND dm.dm_dollar BETWEEN ".$_REQUEST['search_value_from_dollar']." AND ".$_REQUEST['search_value_to_dollar'];
        }

        if(!empty($_REQUEST['search_value_from_ppc']) && !empty($_REQUEST['search_value_to_ppc']))
        {
            $subsql .= " AND dm.dm_price_ct BETWEEN ".$_REQUEST['search_value_from_ppc']." AND ".$_REQUEST['search_value_to_ppc'];
        }

        if(!empty($_REQUEST['search_value_from_pc_amt']) && !empty($_REQUEST['search_value_to_pc_amt']))
        {
            $subsql .= " AND dm.dm_pc_amt BETWEEN ".$_REQUEST['search_value_from_pc_amt']." AND ".$_REQUEST['search_value_to_pc_amt'];
        }

        if(!empty($_REQUEST['search_value_from_dollar_amt']) && !empty($_REQUEST['search_value_to_dollar_amt']))
        {
            $subsql .= " AND dm.dm_dollar_amt BETWEEN ".$_REQUEST['search_value_from_dollar_amt']." AND ".$_REQUEST['search_value_to_dollar_amt'];
        }

        if(!empty($_REQUEST['search_value_key2sym']))
        {

            $db_key2sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$_REQUEST['search_value_key2sym']]);
            if(!empty($db_key2sym))
            {
                $key2sym = $db_key2sym[0]['dm_key2sym'];
                $subsql .= " AND dm.dm_key2sym = '".$key2sym."'";
            }
        }
        if(!empty($_REQUEST['search_value_r2net']))
        {
            $dm_r2net = $_REQUEST['search_value_r2net'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_r2net = ".$dm_r2net;
        }

        if(!empty($_REQUEST['search_value_suspense']))
        {
            $dm_suspense = $_REQUEST['search_value_suspense'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_suspense = ".$dm_suspense;
        }

        if (isset($_REQUEST['search_value_category']) && $_REQUEST['search_value_category'] !== '') 
        {
            $cat = $_REQUEST['search_value_category'];

            // 2 = ALL → do not apply filter
            if ($cat != 2) {
                // CERTIFIED = 1 → dm_category = 1
                // NON-CERTIFIED = 0 → dm_category = 0
                $dm_category = ($cat == 1 ? 1 : 0);
                $subsql .= " AND dm.dm_category = ".$dm_category;
            }
        }

            $available_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 0 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['available_data'] = $this->db->query($available_query)->num_rows();

            $return_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 4 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['return_data'] = $this->db->query($return_query)->num_rows();

            $issue_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 1 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['issue_data'] = $this->db->query($issue_query)->num_rows();

            $hold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 2 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['hold_data'] = $this->db->query($hold_query)->num_rows();

            $sold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 3 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['sold_data'] = $this->db->query($sold_query)->num_rows();
            $record['total_data'] = $record['available_data'] + $record['return_data'] + $record['issue_data'] + $record['hold_data'] + $record['sold_data'];


           $diamond_query = "
    SELECT dm.*, um.*, cm.*, crm.*,
           cm_cust.acc_name as cm_customer, 
           cm_brk.acc_name as cm_broker, 
           crm_cust.acc_name as crm_customer, 
           crm_brk.acc_name as crm_broker
    FROM diamond_master dm
    
    -- Only show if stock is purchased (exists in purchase_trans)
    INNER JOIN purchase_trans prt ON prt.prt_dm_code = dm.dm_code
    LEFT JOIN purchase_master per ON per.per_id = prt.prt_per_id
    
    LEFT JOIN challan_trans ct ON ct.ct_id = dm.dm_ct_id
    LEFT JOIN challan_master cm ON cm.cm_id = ct.ct_cm_id
    LEFT JOIN challan_return_trans crt ON crt.crt_id = dm.dm_crt_id
    LEFT JOIN challan_return_master crm ON crm.crm_id = crt.crt_crm_id
    LEFT JOIN user_master um ON um.user_id = dm.dm_update_id
    LEFT JOIN account_master cm_cust ON cm_cust.acc_id = cm.cm_cust_id
    LEFT JOIN account_master cm_brk ON cm_brk.acc_id = cm.cm_broker_id
    LEFT JOIN account_master crm_cust ON crm_cust.acc_id = crm.crm_cust_id
    LEFT JOIN account_master crm_brk ON crm_brk.acc_id = crm.crm_broker_id
    
    WHERE dm.dm_suspense = 0 
    $subsql 
    
    GROUP BY dm.dm_id
    ORDER BY dm.dm_date_update DESC 
    LIMIT $per_page OFFSET $offset
";
// $purchase_query = "
//     SELECT 
//         per.*, 
//         prt.*, 
//         dm.*

//     FROM purchase_trans prt

//     LEFT JOIN purchase_master per 
//         ON per.per_id = prt.prt_per_id

//     LEFT JOIN diamond_master dm 
//         ON dm.dm_code = prt.prt_dm_code
    
//     WHERE 1
//     $subsql
    
//     ORDER BY per.per_update_date DESC 
//     LIMIT $per_page OFFSET $offset
// ";

// echo "<pre>"; print_r($purchase_query); exit;
$record['diamond_data'] = $this->db->query($diamond_query)->result_array();
// echo "<pre>"; print_r($record['diamond_data']); exit;

           /* Drop Down*/

        // supplier id
        // $supplier_id_data = $this->db->query("SELECT am.acc_id, am.acc_name FROM purchase_master per LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)")->result_array();
        // $record['supplier_id_drop_down'] = [];
                
        // foreach ($supplier_id_data as $key => $value)
        // {
        // 	$record['supplier_id_drop_down'][$value['acc_id']] = $value['acc_name'];
        // }
        // if (!empty($record['supplier_id_drop_down'])) {
        //     asort($record['supplier_id_drop_down']);
        // }
        // supplier id

        // stock id
        $stock_id_data = $this->db->query("SELECT dm.dm_id, dm.dm_code FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'")->result_array();
        $record['stock_id_drop_down'] = [];
        foreach ($stock_id_data as $key => $value)
        {
        	$record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
        }
        if (!empty($record['stock_id_drop_down'])) {
            asort($record['stock_id_drop_down']);
        }
        // stock id

        // size
        $size_data = $this->db->query("SELECT dm.dm_size,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_size ORDER BY dm_size ASC")->result_array();
        $record['size_drop_down'] = [];
        foreach ($size_data as $key => $value)
        {
            $record['size_drop_down'][$value['dm_id']] = $value['dm_size'];
        }
        // size

        // cpd
        $size_data = $this->db->query("SELECT dm.dm_cpd,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cpd ORDER BY dm_cpd ASC")->result_array();
        $record['cpd_drop_down'] = [];
        foreach ($size_data as $key => $value)
        {
            $record['cpd_drop_down'][$value['dm_id']] = $value['dm_cpd'];
        }
        // cpd

        // shape
        $shape_data = $this->db->query("SELECT dm.dm_sh,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_sh ORDER BY FIELD(dm.dm_sh,'CUSHION MODIFIED','MARQUISE','OVAL','HEART','PRINCESS','EMERALD','PEAR','ROUND')desc")->result_array();
        $record['shape_drop_down'] = [];
        foreach ($shape_data as $key => $value)
        {
            $record['shape_drop_down'][$value['dm_id']] = $value['dm_sh'];
        }
        // shape

        // weight
        $weight_data = $this->db->query("SELECT dm.dm_id,dm.dm_wt FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_wt")->result_array();
        $weight_dataa = $this->db->query("SELECT MAX(dm.dm_wt) as max ,MIN(dm.dm_wt) as min FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'")->result_array();

           $a = 0.00;
           $b = 0.09 ;
           $data= array();
           for($i =0.10 ; $i<=$weight_dataa[0]['max']+0.20;)
           {
             $data[] = number_format($a,2). '-' .number_format($b,2);
             $a = $b + 0.01;
             $b = $b + 0.10;
             $i = $i + 0.10;
           }


        $record['weight_drop_down']= [];
        foreach ($data as $key => $value)
        {
            $record['weight_drop_down'][$value] = $value;

        }
        if (!empty($record['weight_drop_down'])) {
            asort($record['weight_drop_down']);
        }
        // weight

        // color
        $color_data = $this->db->query("SELECT dm.dm_id, dm.dm_col FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_col ORDER BY FIELD(dm.dm_col,'D','E','F','G','H','I','J','K','L')")->result_array();
        $record['color_drop_down'] = [];
        foreach ($color_data as $key => $value)
        {
            $record['color_drop_down'][$value['dm_id']] = $value['dm_col'];
        }

        // color

        // clarity
        $clarity_data = $this->db->query("SELECT dm.dm_id, dm.dm_cla FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cla ORDER BY FIELD(dm.dm_cla,'FL','IF','VVS1','VVS2','VS1','SI1','VS2','SI2')")->result_array();
        $record['clarity_drop_down'] = [];
        foreach ($clarity_data as $key => $value)
        {
            $record['clarity_drop_down'][$value['dm_id']] = $value['dm_cla'];
        }

        // clarity

        // cut
        $cut_data = $this->db->query("SELECT dm.dm_id, dm.dm_cut FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cut ORDER BY FIELD(dm.dm_cut,'ID','EX','VG','GD')")->result_array();
        $record['cut_drop_down'] = [];
        foreach ($cut_data as $key => $value)
        {
            $record['cut_drop_down'][$value['dm_id']] = $value['dm_cut'];
        }
        // cut

        // pol
        $pol_data = $this->db->query("SELECT dm.dm_id, dm.dm_pol FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_pol ORDER BY FIELD(dm.dm_pol,'EX','VG','G','GD','FR')")->result_array();
        $record['pol_drop_down'] = [];
        foreach ($pol_data as $key => $value)
        {
            $record['pol_drop_down'][$value['dm_id']] = $value['dm_pol'];
        }
        // pol

        // sym
        $sym_data = $this->db->query("SELECT dm.dm_id, dm.dm_sym FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'GROUP BY dm.dm_sym ORDER BY FIELD(dm.dm_sym,'EX','VG','G','GD','FR')")->result_array();
        $record['sym_drop_down'] = [];
        foreach ($sym_data as $key => $value)
        {
            $record['sym_drop_down'][$value['dm_id']] = $value['dm_sym'];
        }
        // sym

        // fl
        $fl_data = $this->db->query("SELECT dm.dm_id, dm.dm_fl FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_fl ORDER BY FIELD(dm.dm_fl,'N','F','VSL','M','STR')")->result_array();
        $record['fl_drop_down'] = [];
        foreach ($fl_data as $key => $value)
        {
            $record['fl_drop_down'][$value['dm_id']] = $value['dm_fl'];
        }
        // fl

        // lab
        $lab_data = $this->db->query("SELECT dm.dm_id, dm.dm_lab FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_lab")->result_array();
        $record['lab_drop_down'] = [];
        foreach ($lab_data as $key => $value)
        {
            $record['lab_drop_down'][$value['dm_id']] = $value['dm_lab'];
        }
        if (!empty($record['lab_drop_down'])) {
            ksort($record['lab_drop_down']);
        }
        // lab

        // key2sym
        $key2sym_data = $this->db->query("SELECT dm.dm_id, dm.dm_key2sym FROM diamond_master dm WHERE dm.dm_key2sym != '' AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_key2sym")->result_array();
        $record['key2sym_drop_down'][0] = 'ALL';
        foreach ($key2sym_data as $key => $value)
        {
            $record['key2sym_drop_down'][$value['dm_id']] = $value['dm_key2sym'];
        }


        
        
    if (isset($_REQUEST['submit']) && $_REQUEST['submit'] == 'excel') {

    $selected_category = $_REQUEST['search_value_category'] ?? '2'; // 0=NON-CERTIFIED, 1=CERTIFIED, 2=ALL

    // Base headers
    $excel_array[0] = [
        'SR NO','DATE','STOCK ID','PRICE/CT','PC AMT','DOLLAR/CT','DOLLAR AMT',
        'SIZE','PCS','CPD','SHAPE','LAB','CERT. NO.','CARAT','COL','CLAR.',
        'CUT','POL','SYM','FLUR.','LENGTH','WIDTH','HEIGHT','NAT/LAB GROWN',
        'DISC','RAP','DIAMOND IMAGE AND VIDEO','STATUS','UPLOADED DATE','NO OF DAYS'
    ];

    // Certified headers (only if user selected CERTIFIED or ALL)
    $certified_fields = [
        'Location','DNA','Rap Dollar','Tbl','TD %','L/W','BIC','BIS','WIC','Col Tinge',
        'LUS','Remarks','H/A','Girdle','Girdle %','Girdle Condition','CA','CH','PA',
        'PD','Str Ln','LR Half','Culet','KTS','Rep Com','Cert Date','Image Link','Certificate Link'
    ];

    if ($selected_category == '1' || $selected_category == '2') {
        $excel_array[0] = array_merge($excel_array[0], $certified_fields);
    }

    $wt_total = 0;
    $total_amount = 0;
    $total_discount = 0;
    $total_cert = 0;
    $total_rap = 0;
    $count = 1;

         $excel_diamond_query = "
    SELECT dm.*, um.*, cm.*, crm.*,
           cm_cust.acc_name as cm_customer, 
           cm_brk.acc_name as cm_broker, 
           crm_cust.acc_name as crm_customer, 
           crm_brk.acc_name as crm_broker
    FROM diamond_master dm
    
    -- Only show if stock is purchased (exists in purchase_trans)
    INNER JOIN purchase_trans prt ON prt.prt_dm_code = dm.dm_code
    LEFT JOIN purchase_master per ON per.per_id = prt.prt_per_id
    
    LEFT JOIN challan_trans ct ON ct.ct_id = dm.dm_ct_id
    LEFT JOIN challan_master cm ON cm.cm_id = ct.ct_cm_id
    LEFT JOIN challan_return_trans crt ON crt.crt_id = dm.dm_crt_id
    LEFT JOIN challan_return_master crm ON crm.crm_id = crt.crt_crm_id
    LEFT JOIN user_master um ON um.user_id = dm.dm_update_id
    LEFT JOIN account_master cm_cust ON cm_cust.acc_id = cm.cm_cust_id
    LEFT JOIN account_master cm_brk ON cm_brk.acc_id = cm.cm_broker_id
    LEFT JOIN account_master crm_cust ON crm_cust.acc_id = crm.crm_cust_id
    LEFT JOIN account_master crm_brk ON crm_brk.acc_id = crm.crm_broker_id
    
    WHERE dm.dm_suspense = 0 
    $subsql 
    
    GROUP BY dm.dm_id
    ORDER BY dm.dm_date_update DESC 
";

$record['excel_diamond_data'] = $this->db->query($excel_diamond_query)->result_array();

    foreach ($record['excel_diamond_data'] as $key => $value) {

        // Determine status
        $status = match($value['dm_status']) {
            1 => 'Issue',
            2 => 'Hold',
            3 => 'Sold',
            default => 'Available'
        };

        // Calculate days since uploaded
        $uploaded_date = strtotime($value['dm_date_create']);
        $current_date = time();
        $nod = round(($current_date - $uploaded_date) / (60*60*24));

        $wt_total += $value['dm_wt'];
        $total_amount += $value['dm_dollar'];
        $total_discount += $value['dm_disc'];
        $total_cert += $value['dm_rap'] * (100 + $value['dm_disc']) / 100;
        $total_rap += $value['dm_rap'];

        // Base row
        $row = [
            $key + 1,
            !empty($value['dm_date'] && $value['dm_date'] != '0000-00-00') ? date('d-m-Y', strtotime($value['dm_date'])) : '',
            $value['dm_code'] ?? '',
            $value['dm_price_ct'] ?? '',
            $value['dm_pc_amt'] ?? '',
            $value['dm_dollar'] ?? '',
            $value['dm_dollar_amt'] ?? '',
            $value['dm_size'] ?? '',
            $value['dm_pcs'] ?? '',
            $value['dm_cpd'] ?? '',
            $value['dm_sh'] ?? '',
            $value['dm_lab'] ?? '',
            $value['dm_cert_no'] ?? '',
            $value['dm_carat'] ?? '',
            $value['dm_col'] ?? '',
            $value['dm_cla'] ?? '',
            $value['dm_cut'] ?? '',
            $value['dm_pol'] ?? '',
            $value['dm_sym'] ?? '',
            $value['dm_fl'] ?? '',
            $value['dm_len'] ?? '',
            $value['dm_wid'] ?? '',
            $value['dm_hei'] ?? '',
            $value['dm_nat_lab_grown'] ?? '',
            $value['dm_disc'] ?? '',
            $value['dm_rap'] ?? '',
            $value['dm_video_link'] ?? '',
            $status,
            !(!empty($value['dm_date_create']) && $value['dm_date_create'] != '0000-00-00')
            ? date('d-m-Y', strtotime($value['dm_date_create']))
            : '',
            $nod
        ];

        // Add certified fields only if row is certified AND user selected CERTIFIED/ALL
        if (($selected_category == '1' || $selected_category == '2') && ($value['dm_category'] ?? 0) != 0) {
            $extra_fields = [
                $value['dm_location'] ?? '',
                $value['dm_dna'] ?? '',
                $value['dm_rap_dollar'] ?? '',
                $value['dm_tbl'] ?? '',
                $value['dm_td_percentage'] ?? '',
                $value['dm_l_w'] ?? '',
                $value['dm_bic'] ?? '',
                $value['dm_bis'] ?? '',
                $value['dm_wic'] ?? '',
                $value['dm_col_tinge'] ?? '',
                $value['dm_lus'] ?? '',
                $value['dm_remarks'] ?? '',
                $value['dm_h_a'] ?? '',
                $value['dm_girdle'] ?? '',
                $value['dm_girdle_percentage'] ?? '',
                $value['dm_girdle_cond'] ?? '',
                $value['dm_ca'] ?? '',
                $value['dm_ch'] ?? '',
                $value['dm_pa'] ?? '',
                $value['dm_pd'] ?? '',
                $value['dm_str_ln'] ?? '',
                $value['dm_lr_half'] ?? '',
                $value['dm_culet'] ?? '',
                $value['dm_kts'] ?? '',
                $value['dm_rep_com'] ?? '',
                !empty($value['dm_cert_date'] && $value['dm_cert_date'] != '0000-00-00') ? date('Y-m-d', strtotime($value['dm_cert_date'])) : '',
                $value['dm_image_link'] ?? '',
                $value['dm_certificate_link'] ?? ''
            ];
            $row = array_merge($row, $extra_fields);
        }

        $excel_array[$key + 1] = $row;
        $count++;
    }

    // Totals row
    // $tot_price = $total_cert * round($wt_total);
    // $tot_rap = $total_rap * round($wt_total);
    // $tot_disc = (!empty($tot_rap) && $tot_rap != 0) ? (1 - ($tot_price / $tot_rap)) * 100 : 0;

    // $tot_row = array_fill(0, count($excel_array[0]), '');
    // $tot_row[2] = 'TOTAL';
    // $tot_row[3] = $wt_total;
    // $tot_row[11] = '-' . $tot_disc;
    // $tot_row[16] = $total_amount;

    // $excel_array[$count] = $tot_row;

    $record['excel_array'] = $excel_array;

    // echo "<pre>"; print_r($record['excel_array']); exit;
}

            return $record;

        }
        public function get_all_stock_data_print()
        {
            $subsql = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

        // if(!empty($_REQUEST['search_value_supplier_id']))
        // {
        //     $supplier_id = implode(',', $_REQUEST['search_value_supplier_id']);
        //     // echo "<pre>"; print_r($supplier_id); exit;
        //     $subsql .= " AND pm.per_supplier_id IN (".$supplier_id. ")";
        // }
        if(!empty($_REQUEST['search_value_stock_id']))
        {
            $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
        }
         if(!empty($_REQUEST['search_value_size']))
        {
            $size = '';
            foreach ($_REQUEST['search_value_size'] as $key => $value)
            {
                $db_size = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_size'];
                $size .= "'".$db_size."',";
            }
            $size = rtrim($size, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_size IN (".$size. ")";
        }
        if(!empty($_REQUEST['search_value_cpd']))
        {
            $cpd = '';
            foreach ($_REQUEST['search_value_cpd'] as $key => $value)
            {
                $db_cpd = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cpd'];
                $cpd .= "'".$db_cpd."',";
            }
            $cpd = rtrim($cpd, ',');
            // echo "<pre>"; print_r($cpd); exit;
            $subsql .= " AND dm.dm_cpd IN (".$cpd. ")";
        }
        if(!empty($_REQUEST['search_value_sh']))
        {
            $sh = '';
            foreach ($_REQUEST['search_value_sh'] as $key => $value)
            {
                $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                $sh .= "'".$db_sh."',";
            }
            $sh = rtrim($sh, ',');
            // echo "<pre>"; print_r($sh); exit;
            $subsql .= " AND dm.dm_sh IN (".$sh. ")";
        }
        if(!empty($_REQUEST['search_value_wt']))
        {
            $array = str_replace( "-", " AND ", $_REQUEST['search_value_wt']);
            /*$wt = implode(',',$array);*/
            // echo "<pre>"; print_r($stock_id); exit;
            $subsql .= "AND";
            $weight = '';
             foreach($array as $wt)
             {
                $weight .= " dm.dm_wt BETWEEN ".$wt."OR";
             }
              $subsql .= rtrim($weight,"OR");
        }
        if(!empty($_REQUEST['search_value_col']))
        {
            $col = '';
            foreach ($_REQUEST['search_value_col'] as $key => $value)
            {
                $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                $col .= "'".$db_col."',";
            }
            $col = rtrim($col, ',');
            // echo "<pre>"; print_r($col); exit;
            $subsql .= " AND dm.dm_col IN (".$col. ")";
        }
        if(!empty($_REQUEST['search_value_cla']))
        {
            $cla = '';
            foreach ($_REQUEST['search_value_cla'] as $key => $value)
            {
                $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                $cla .= "'".$db_cla."',";
            }
            $cla = rtrim($cla, ',');
            // echo "<pre>"; print_r($cla); exit;
            $subsql .= " AND dm.dm_cla IN (".$cla. ")";
        }
        if(!empty($_REQUEST['search_value_cut']))
        {
            $cut = '';
            foreach ($_REQUEST['search_value_cut'] as $key => $value)
            {
                $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                $cut .= "'".$db_cut."',";
            }
            $cut = rtrim($cut, ',');
            // echo "<pre>"; print_r($cut); exit;
            $subsql .= " AND dm.dm_cut IN (".$cut. ")";
        }
        if(!empty($_REQUEST['search_value_pol']))
        {
            $pol = '';
            foreach ($_REQUEST['search_value_pol'] as $key => $value)
            {
                $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                $pol .= "'".$db_pol."',";
            }
            $pol = rtrim($pol, ',');
            // echo "<pre>"; print_r($pol); exit;
            $subsql .= " AND dm.dm_pol IN (".$pol. ")";
        }
        if(!empty($_REQUEST['search_value_sym']))
        {
            $sym = '';
            foreach ($_REQUEST['search_value_sym'] as $key => $value)
            {
                $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                $sym .= "'".$db_sym."',";
            }
            $sym = rtrim($sym, ',');
            // echo "<pre>"; print_r($sym); exit;
            $subsql .= " AND dm.dm_sym IN (".$sym. ")";
        }
        if(!empty($_REQUEST['search_value_fl']))
        {
            $fl = '';
            foreach ($_REQUEST['search_value_fl'] as $key => $value)
            {
                $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                $fl .= "'".$db_fl."',";
            }
            $fl = rtrim($fl, ',');
            // echo "<pre>"; print_r($fl); exit;
            $subsql .= " AND dm.dm_fl IN (".$fl. ")";
        }
        if(!empty($_REQUEST['search_value_lab']))
        {
            $lab = '';
            foreach ($_REQUEST['search_value_lab'] as $key => $value)
            {
                $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                $lab .= "'".$db_lab."',";
            }
            $lab = rtrim($lab, ',');
            // echo "<pre>"; print_r($lab); exit;
            $subsql .= " AND dm.dm_lab IN (".$lab. ")";
        }
           if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
        {
            if($_REQUEST['search_value_status'] == 1)
            {
                $status = 0;
            }
            elseif ($_REQUEST['search_value_status'] == 2)
            {
                $status = 1;
            }
            elseif ($_REQUEST['search_value_status'] == 3)
            {
                $status = 2;
            }
            else
            {
                $status = 3;
            }
            $subsql .= " AND dm.dm_status =".$status;
        }
        else
        {
            $subsql .= " AND (dm.dm_status != 3)";
        }
        if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
        {
            $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
            $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
            $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }
        else
        {
            $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
        }


       if(!empty($_REQUEST['search_value_from_dollar']) && !empty($_REQUEST['search_value_to_dollar']))
        {
            $subsql .= " AND dm.dm_dollar BETWEEN ".$_REQUEST['search_value_from_dollar']." AND ".$_REQUEST['search_value_to_dollar'];
        }

        if(!empty($_REQUEST['search_value_from_ppc']) && !empty($_REQUEST['search_value_to_ppc']))
        {
            $subsql .= " AND dm.dm_price_ct BETWEEN ".$_REQUEST['search_value_from_ppc']." AND ".$_REQUEST['search_value_to_ppc'];
        }

        if(!empty($_REQUEST['search_value_from_pc_amt']) && !empty($_REQUEST['search_value_to_pc_amt']))
        {
            $subsql .= " AND dm.dm_pc_amt BETWEEN ".$_REQUEST['search_value_from_pc_amt']." AND ".$_REQUEST['search_value_to_pc_amt'];
        }

        if(!empty($_REQUEST['search_value_from_dollar_amt']) && !empty($_REQUEST['search_value_to_dollar_amt']))
        {
            $subsql .= " AND dm.dm_dollar_amt BETWEEN ".$_REQUEST['search_value_from_dollar_amt']." AND ".$_REQUEST['search_value_to_dollar_amt'];
        }

        if(!empty($_REQUEST['search_value_key2sym']))
        {

            $db_key2sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$_REQUEST['search_value_key2sym']]);
            if(!empty($db_key2sym))
            {
                $key2sym = $db_key2sym[0]['dm_key2sym'];
                $subsql .= " AND dm.dm_key2sym = '".$key2sym."'";
            }
        }
        if(!empty($_REQUEST['search_value_r2net']))
        {
            $dm_r2net = $_REQUEST['search_value_r2net'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_r2net = ".$dm_r2net;
        }

        if(!empty($_REQUEST['search_value_suspense']))
        {
            $dm_suspense = $_REQUEST['search_value_suspense'] == 1 ? 0 : 1;
            $subsql .= " AND dm.dm_suspense = ".$dm_suspense;
        }

        if (isset($_REQUEST['search_value_category']) && $_REQUEST['search_value_category'] !== '') 
        {
            $cat = $_REQUEST['search_value_category'];

            // 2 = ALL → do not apply filter
            if ($cat != 2) {
                // CERTIFIED = 1 → dm_category = 1
                // NON-CERTIFIED = 0 → dm_category = 0
                $dm_category = ($cat == 1 ? 1 : 0);
                $subsql .= " AND dm.dm_category = ".$dm_category;
            }
        }

            $available_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 0 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['available_data'] = $this->db->query($available_query)->num_rows();

            $return_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 4 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['return_data'] = $this->db->query($return_query)->num_rows();

            $issue_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 1 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['issue_data'] = $this->db->query($issue_query)->num_rows();

            $hold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 2 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['hold_data'] = $this->db->query($hold_query)->num_rows();

            $sold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_status = 3 AND dm.dm_suspense = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['sold_data'] = $this->db->query($sold_query)->num_rows();
            $record['total_data'] = $record['available_data'] + $record['return_data'] + $record['issue_data'] + $record['hold_data'] + $record['sold_data'];


            $diamond_query = "SELECT dm.*, um.*, cm.*, crm.*, cm_cust.acc_name as cm_customer, cm_brk.acc_name as cm_broker, crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker
                    FROM diamond_master dm
                    -- Only show if stock is purchased (exists in purchase_trans)
                    INNER JOIN purchase_trans prt ON prt.prt_dm_code = dm.dm_code
                    LEFT JOIN purchase_master per ON per.per_id = prt.prt_per_id
                    LEFT JOIN challan_trans ct ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                 
                    WHERE dm.dm_suspense = 0 $subsql ORDER BY dm.dm_date_update DESC ";

        //     $purchase_query = "SELECT 
        //         per.*, 
        //         prt.*, 
        //         dm.*

        //     FROM purchase_trans prt

        //     LEFT JOIN purchase_master per 
        //         ON per.per_id = prt.prt_per_id

        //     LEFT JOIN diamond_master dm 
        //         ON dm.dm_code = prt.prt_dm_code
            
        //     WHERE 1
        //     $subsql
            
        //     ORDER BY per.per_update_date DESC 
        // ";
            // echo "<pre>"; print_r($purchase_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            return $record;

        }
    /*/stock report*/

    /*history Report*/
        public function get_all_history_data($per_page, $offset)
        {
            if(!empty($_GET['search_value_stock_id']))
            {
                $dm_id = $_GET['search_value_stock_id'];

                $diamond_query = "SELECT dm.* FROM diamond_master dm WHERE dm.dm_suspense = 0 AND dm.dm_id = $dm_id";
                $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

                foreach ($record['diamond_data'] as $key => $value)
                {
                    if($value['dm_ct_id'] != 0)
                    {
                        $latest_query = "SELECT cm.*, ct.* FROM challan_trans ct LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id) WHERE ct.ct_id =".$value['dm_ct_id'];
                        $record['latest_data'] = $this->db->query($latest_query)->result_array();
                    }
                    if($value['dm_crt_id'] != 0)
                    {
                        $latest_query = "SELECT crm.*,crt.* FROM challan_return_trans crt LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id) WHERE crt.crt_id = ".$value['dm_crt_id'];
                        $record['latest_data'] = $this->db->query($latest_query)->result_array();
                    }
                }

                $history_query = "SELECT
                                    cm.*,ct.*,
                                    crm.*,crt.*,
                                    cm_cust.acc_name as cm_customer, cm_cust.acc_id as cm_cust_id,
                                    cm_brk.acc_name as cm_broker, cm_brk.acc_id as cm_broker_id,
                                    crm_cust.acc_name as crm_customer, crm_cust.acc_id as crm_cust_id,
                                    crm_brk.acc_name as crm_broker, crm_brk.acc_id as crm_broker_id,
                                    cm_um.user_full_name as cm_user, crm_um.user_full_name as crm_user
                                FROM challan_trans ct
                                LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                LEFT JOIN challan_return_trans crt ON(crt.crt_ct_id = ct.ct_id)
                                LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                LEFT JOIN user_master cm_um ON(cm_um.user_id = cm.cm_update_userid)
                                LEFT JOIN user_master crm_um ON(crm_um.user_id = crm.crm_update_userid)
                                LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                                LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                                LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                                LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                                WHERE ct.ct_dm_id = $dm_id
                                ORDER BY cm.cm_id DESC";
                $record['history_data'] = $this->db->query($history_query)->result_array();


            }
            else
            {
                 $history_query = "SELECT
                                    cm.*,ct.*,
                                    crm.*,crt.*,
                                    cm_cust.acc_name as cm_customer, cm_cust.acc_id as cm_cust_id,
                                    cm_brk.acc_name as cm_broker, cm_brk.acc_id as cm_broker_id,
                                    crm_cust.acc_name as crm_customer, crm_cust.acc_id as crm_cust_id,
                                    crm_brk.acc_name as crm_broker, crm_brk.acc_id as crm_broker_id,
                                    cm_um.user_full_name as cm_user, crm_um.user_full_name as crm_user
                                FROM challan_trans ct
                                LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                LEFT JOIN challan_return_trans crt ON(crt.crt_ct_id = ct.ct_id)
                                LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                LEFT JOIN user_master cm_um ON(cm_um.user_id = cm.cm_update_userid)
                                LEFT JOIN user_master crm_um ON(crm_um.user_id = crm.crm_update_userid)
                                LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                                LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                                LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                                LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                                ORDER BY cm.cm_id DESC";
                $record['history_data'] = $this->db->query($history_query)->result_array();
            }


            $drop_down_data = $this->db->query("
                SELECT  dm.*
                FROM diamond_master dm
                WHERE dm.dm_suspense = 0
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'PLEASE SELECT';
            foreach ($drop_down_data as $key => $value)
            {
                $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
            }
            // stock id



            return $record;

        }
        public function get_all_history_data_print()
        {
            if(!empty($_GET['search_value_stock_id']))
            {
                $dm_id = $_GET['search_value_stock_id'];

                $diamond_query = "SELECT dm.* FROM diamond_master dm WHERE dm.dm_suspense = 0 AND dm.dm_id = $dm_id";
                $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

                foreach ($record['diamond_data'] as $key => $value)
                {
                    if($value['dm_ct_id'] != 0)
                    {
                        $latest_query = "SELECT cm.*, ct.* FROM challan_trans ct LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id) WHERE ct.ct_id =".$value['dm_ct_id'];
                        $record['latest_data'] = $this->db->query($latest_query)->result_array();
                    }
                    if($value['dm_crt_id'] != 0)
                    {
                        $latest_query = "SELECT crm.*,crt.* FROM challan_return_trans crt LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id) WHERE crt.crt_id = ".$value['dm_crt_id'];
                        $record['latest_data'] = $this->db->query($latest_query)->result_array();
                    }
                }

                $history_query = "SELECT
                                    cm.*,ct.*,
                                    crm.*,crt.*,
                                    cm_cust.acc_name as cm_customer, cm_cust.acc_id as cm_cust_id,
                                    cm_brk.acc_name as cm_broker, cm_brk.acc_id as cm_broker_id,
                                    crm_cust.acc_name as crm_customer, crm_cust.acc_id as crm_cust_id,
                                    crm_brk.acc_name as crm_broker, crm_brk.acc_id as crm_broker_id,
                                    cm_um.user_full_name as cm_user, crm_um.user_full_name as crm_user
                                FROM challan_trans ct
                                LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                LEFT JOIN challan_return_trans crt ON(crt.crt_ct_id = ct.ct_id)
                                LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                LEFT JOIN user_master cm_um ON(cm_um.user_id = cm.cm_update_userid)
                                LEFT JOIN user_master crm_um ON(crm_um.user_id = crm.crm_update_userid)
                                LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                                LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                                LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                                LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                                WHERE ct.ct_dm_id = $dm_id
                                ORDER BY cm.cm_id DESC";
                $record['history_data'] = $this->db->query($history_query)->result_array();

            }


            $drop_down_data = $this->db->query("
                SELECT  dm.*
                FROM diamond_master dm
                WHERE dm.dm_suspense = 0
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'PLEASE SELECT';
            foreach ($drop_down_data as $key => $value)
            {
                $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
            }
            // stock id



            return $record;

        }
    /*/history report*/

    /*issue Report*/
        public function get_all_issue_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (cm_cust.acc_id =".$_GET['search_value_party']." OR cm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (cm_brk.acc_id =".$_GET['search_value_broker']." OR cm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND cm.cm_entry_date LIKE '".$entry_date."%'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND cm.cm_entry_no =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }

            return $this->db->query("SELECT dm.*
                    FROM diamond_master dm
                    LEFT JOIN challan_trans ct ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    WHERE dm.dm_suspense = 0 AND ct.ct_delete = 1 $subsql
                ")->num_rows();
        }
        public function get_all_issue_data($per_page, $offset)
        {
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                $subsql .= " AND (cm_cust.acc_id =".$_GET['search_value_party']." OR cm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (cm_brk.acc_id =".$_GET['search_value_broker']." OR cm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_update LIKE '".$entry_date."%'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND cm.cm_entry_no =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            $diamond_query = "SELECT dm.*, um.*, cm.*, cm_cust.acc_name as cm_customer, cm_brk.acc_name as cm_broker
                    FROM challan_trans ct
                    LEFT JOIN diamond_master dm ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    WHERE dm.dm_suspense = 0 AND ct.ct_delete = 1 $subsql ORDER BY dm.dm_date_update DESC LIMIT $per_page OFFSET $offset";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $diamond_data = $this->db->query("
                SELECT  dm.*, um.*, cm.*, cm_cust.acc_name as cm_customer, cm_cust.acc_id as cm_cust_id, cm_brk.acc_name as cm_broker,
                cm_brk.acc_id as cm_broker_id
                FROM challan_trans ct
                    LEFT JOIN diamond_master dm ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    WHERE dm.dm_suspense = 0 AND ct.ct_delete = 1
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['dm_id'] != '' && $value['dm_id'] > 0)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }

            }
            // stock id

            // user
            $record['user_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['user_drop_down'][$value['user_id']] = $value['user_full_name'];
            }
            // stock id

            // party
            $record['party_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['cm_cust_id'] != '' && $value['cm_cust_id'] > 0)
                {
                    $record['party_drop_down'][$value['cm_cust_id']] = $value['cm_customer'];
                }

            }
            // party

            // broker
            $record['broker_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['cm_broker_id'] != '' && $value['cm_broker_id'] > 0)
                {
                    $record['broker_drop_down'][$value['cm_broker_id']] = $value['cm_broker'];
                }

            }
            // broker

            // entry no
            $record['entry_no_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['entry_no_drop_down'][$value['cm_id']] = $value['cm_entry_no'];

            }
            // entry no

            return $record;

        }
        public function get_all_issue_data_print()
        {
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                $subsql .= " AND (cm_cust.acc_id =".$_GET['search_value_party']." OR cm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (cm_brk.acc_id =".$_GET['search_value_broker']." OR cm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_update LIKE '".$entry_date."%'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND cm.cm_entry_no =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            $diamond_query = "SELECT dm.*, um.*, cm.*, cm_cust.acc_name as cm_customer, cm_brk.acc_name as cm_broker
                    FROM challan_trans ct
                    LEFT JOIN diamond_master dm ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    WHERE dm.dm_suspense = 0 AND ct.ct_delete = 1 $subsql ORDER BY dm.dm_date_update DESC ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();



            return $record;

        }
    /*/issue report*/

    /*sold Report*/
        public function get_all_sold_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql = "";
            $start_date = date('Y-m-d');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.dm_id
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 3 $subsql
                ";
            // echo "<pre>"; print_r($diamond_query); exit;

            return $this->db->query($diamond_query)->num_rows();
        }
        public function get_all_sold_data($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('Y-m-d');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker, crt.crt_dm_cert
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 3  $subsql ORDER BY dm.dm_date_update DESC LIMIT $per_page OFFSET $offset";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $diamond_data = $this->db->query("
                SELECT  dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_cust.acc_id as crm_cust_id, crm_brk.acc_name as crm_broker,
                crm_brk.acc_id as crm_broker_id
                FROM challan_return_trans crt
                    LEFT JOIN diamond_master dm ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 3
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['dm_id'] != '' && $value['dm_id'] > 0)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }

            }
            // stock id

            // user
            $record['user_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['user_drop_down'][$value['user_id']] = $value['user_full_name'];
            }
            // stock id

            // party
            $record['party_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['crm_cust_id'] != '' && $value['crm_cust_id'] > 0)
                {
                    $record['party_drop_down'][$value['crm_cust_id']] = $value['crm_customer'];
                }

            }
            // party

            // broker
            $record['broker_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['crm_broker_id'] != '' && $value['crm_broker_id'] > 0)
                {
                    $record['broker_drop_down'][$value['crm_broker_id']] = $value['crm_broker'];
                }

            }
            // broker

            // entry no
            $record['entry_no_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['entry_no_drop_down'][$value['crm_id']] = $value['crm_entry_no'];

            }
            // entry no

            return $record;

        }
        public function get_all_sold_data_print()
        {

            $subsql = "";
            $start_date = date('Y-m-d');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker, crt.crt_dm_cert
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 3  $subsql ORDER BY dm.dm_date_update DESC";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            return $record;

        }
    /*/sold report*/

    /*hold Report*/
        public function get_all_hold_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql = "";
            $start_date = date('2014-04-01');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.dm_id
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 2  $subsql
                ";
            // echo "<pre>"; print_r($diamond_query); exit;

            return $this->db->query($diamond_query)->num_rows();
        }
        public function get_all_hold_data($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('2014-04-01');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker, crt.crt_dm_cert
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 2  $subsql ORDER BY dm.dm_date_update DESC LIMIT $per_page OFFSET $offset";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $diamond_data = $this->db->query("
                SELECT  dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_cust.acc_id as crm_cust_id, crm_brk.acc_name as crm_broker,
                crm_brk.acc_id as crm_broker_id
                FROM challan_return_trans crt
                    LEFT JOIN diamond_master dm ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 2
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['dm_id'] != '' && $value['dm_id'] > 0)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }

            }
            // stock id

            // user
            $record['user_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['user_drop_down'][$value['user_id']] = $value['user_full_name'];
            }
            // stock id

            // party
            $record['party_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['crm_cust_id'] != '' && $value['crm_cust_id'] > 0)
                {
                    $record['party_drop_down'][$value['crm_cust_id']] = $value['crm_customer'];
                }

            }
            // party

            // broker
            $record['broker_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['crm_broker_id'] != '' && $value['crm_broker_id'] > 0)
                {
                    $record['broker_drop_down'][$value['crm_broker_id']] = $value['crm_broker'];
                }

            }
            // broker

            // entry no
            $record['entry_no_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['entry_no_drop_down'][$value['crm_id']] = $value['crm_entry_no'];

            }
            // entry no

            return $record;

        }
        public function get_all_hold_data_print()
        {

            $subsql = "";
            $start_date = date('Y-m-d');
            $end_date   = date('Y-m-d');

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                 $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_start_date']) && !empty($_GET['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(crm.crm_entry_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.*, um.*, crm.*, crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker, crt.crt_dm_cert
                    FROM diamond_master dm
                    LEFT JOIN challan_return_trans crt ON(crt.crt_id = dm.dm_crt_id)
                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                    LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                    WHERE dm.dm_suspense = 0 AND dm.dm_status = 2  $subsql ORDER BY dm.dm_date_update DESC";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            return $record;

        }
    /*/hold report*/

    /*group Report*/
        public function get_all_group_data()
        {
            $subsql = "";

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND crm.crm_entry_date LIKE '".$entry_date."%'";
            }
            // else
            // {
            //     $subsql .= " AND crm.crm_entry_date LIKE '".date('Y-m-d')."%'";
            // }

            if(!empty($_GET['search_value_cert']) && $_GET['search_value_cert'] != 0)
            {
                if($_GET['search_value_cert'] == 1)
                {
                    $cert = 1;
                }
                else
                {
                    $cert = 0;
                }
                $subsql .= " AND crt.crt_dm_cert =".$cert;
            }
            $diamond_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 $subsql ORDER BY dm.dm_date_update ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $diamond_data = $this->db->query($diamond_query)->result_array();
            // echo "<pre>"; print_r($diamond_data); exit;
            $group_01_69_in_hand = 0;
            $group_01_69_issue = 0;
            $group_01_69_hold = 0;
            $group_01_69_sold = 0;
            $group_70_79_in_hand = 0;
            $group_70_79_issue = 0;
            $group_70_79_hold = 0;
            $group_70_79_sold = 0;
            $group_80_89_in_hand = 0;
            $group_80_89_issue = 0;
            $group_80_89_hold = 0;
            $group_80_89_sold = 0;
            $group_90_99_in_hand = 0;
            $group_90_99_issue = 0;
            $group_90_99_hold = 0;
            $group_90_99_sold = 0;
            $group_100_149_in_hand = 0;
            $group_100_149_issue = 0;
            $group_100_149_hold = 0;
            $group_100_149_sold = 0;
            $group_150_199_in_hand = 0;
            $group_150_199_issue = 0;
            $group_150_199_hold = 0;
            $group_150_199_sold = 0;
            $group_200_299_in_hand = 0;
            $group_200_299_issue = 0;
            $group_200_299_hold = 0;
            $group_200_299_sold = 0;
            $group_300_1000_in_hand = 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold = 0;
            $group_300_1000_sold = 0;
            $group_fancy_in_hand = 0;
            $group_fancy_issue = 0;
            $group_fancy_hold = 0;
            $group_fancy_sold = 0;
            $group_non_in_hand = 0;
            $group_non_issue = 0;
            $group_non_hold = 0;
            $group_non_sold = 0;
            foreach ($diamond_data as $key => $value) {
                if(strtoupper($value['dm_sh']) == 'ROUND')
                {
                    // echo "<pre>"; print_r($value['dm_wt']); exit;
                    if(strtoupper($value['dm_lab']) == 'NON')
                    {
                        if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                        {
                            $group_non_in_hand = $group_non_in_hand + 1;
                        }
                        elseif($value['dm_status'] == 3)
                        {
                            $group_non_sold = $group_non_sold + 1;
                        }
                        elseif($value['dm_status'] == 1)
                        {
                            $group_non_issue = $group_non_issue + 1;
                        }
                        else
                        {
                            $group_non_hold = $group_non_hold + 1;
                        }
                    }
                    else
                    {
                        if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_01_69_in_hand = $group_01_69_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_01_69_sold = $group_01_69_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_01_69_issue = $group_01_69_issue + 1;
                            }
                            else
                            {
                                $group_01_69_hold = $group_01_69_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_70_79_in_hand = $group_70_79_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_70_79_sold = $group_70_79_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_70_79_issue = $group_70_79_issue + 1;
                            }
                            else
                            {
                                $group_70_79_hold = $group_70_79_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_80_89_in_hand = $group_80_89_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_80_89_sold = $group_80_89_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_80_89_issue = $group_80_89_issue + 1;
                            }
                            else
                            {
                                $group_80_89_hold = $group_80_89_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_90_99_in_hand = $group_90_99_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_90_99_sold = $group_90_99_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_90_99_issue = $group_90_99_issue + 1;
                            }
                            else
                            {
                                $group_90_99_hold = $group_90_99_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_100_149_in_hand = $group_100_149_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_100_149_sold = $group_100_149_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_100_149_issue = $group_100_149_issue + 1;
                            }
                            else
                            {
                                $group_100_149_hold = $group_100_149_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_150_199_in_hand = $group_150_199_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_150_199_sold = $group_150_199_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_150_199_issue = $group_150_199_issue + 1;
                            }
                            else
                            {
                                $group_150_199_hold = $group_150_199_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_200_299_in_hand = $group_200_299_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_200_299_sold = $group_200_299_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_200_299_issue = $group_200_299_issue + 1;
                            }
                            else
                            {
                                $group_200_299_hold = $group_200_299_hold + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                        {
                            if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                            {
                                $group_300_1000_in_hand = $group_300_1000_in_hand + 1;
                            }
                            elseif($value['dm_status'] == 3)
                            {
                                $group_300_1000_sold = $group_300_1000_sold + 1;
                            }
                            elseif($value['dm_status'] == 1)
                            {
                                $group_300_1000_issue = $group_300_1000_issue + 1;
                            }
                            else
                            {
                                $group_300_1000_hold = $group_300_1000_hold + 1;
                            }
                        }
                    }
                }
                else
                {
                    if($value['dm_status'] == 0 || $value['dm_status'] == 4)
                    {
                        $group_fancy_in_hand = $group_fancy_in_hand + 1;
                    }
                    elseif($value['dm_status'] == 3)
                    {
                        $group_fancy_sold = $group_fancy_sold + 1;
                    }
                    elseif($value['dm_status'] == 1)
                    {
                        $group_fancy_issue = $group_fancy_issue + 1;
                    }
                    else
                    {
                        $group_fancy_hold = $group_fancy_hold + 1;
                    }
                }
            }
            // exit;
            $record['diamond_data']['NON_in_hand'] = $group_non_in_hand;
            $record['diamond_data']['NON_issue'] = $group_non_issue;
            $record['diamond_data']['NON_hold'] = $group_non_hold;
            $record['diamond_data']['NON_sold'] = $group_non_sold;
            $record['diamond_data']['FANCY (PE-PR-CU-HT-EM-OV)_in_hand'] = $group_fancy_in_hand;
            $record['diamond_data']['FANCY (PE-PR-CU-HT-EM-OV)_issue'] = $group_fancy_issue;
            $record['diamond_data']['FANCY (PE-PR-CU-HT-EM-OV)_hold'] = $group_fancy_hold;
            $record['diamond_data']['FANCY (PE-PR-CU-HT-EM-OV)_sold'] = $group_fancy_sold;
            $record['diamond_data']['3.00 UP ( 3.00 TO 10.00)_in_hand'] = $group_300_1000_in_hand;
            $record['diamond_data']['3.00 UP ( 3.00 TO 10.00)_issue'] = $group_300_1000_issue;
            $record['diamond_data']['3.00 UP ( 3.00 TO 10.00)_hold'] = $group_300_1000_hold;
            $record['diamond_data']['3.00 UP ( 3.00 TO 10.00)_sold'] = $group_300_1000_sold;
            $record['diamond_data']['2.00 TO 2.99_in_hand'] = $group_200_299_in_hand;
            $record['diamond_data']['2.00 TO 2.99_issue'] = $group_200_299_issue;
            $record['diamond_data']['2.00 TO 2.99_hold'] = $group_200_299_hold;
            $record['diamond_data']['2.00 TO 2.99_sold'] = $group_200_299_sold;
            $record['diamond_data']['1.50 TO 1.99_in_hand'] = $group_150_199_in_hand;
            $record['diamond_data']['1.50 TO 1.99_issue'] = $group_150_199_issue;
            $record['diamond_data']['1.50 TO 1.99_hold'] = $group_150_199_hold;
            $record['diamond_data']['1.50 TO 1.99_sold'] = $group_150_199_sold;
            $record['diamond_data']['1.00 TO 1.49_in_hand'] = $group_100_149_in_hand;
            $record['diamond_data']['1.00 TO 1.49_issue'] = $group_100_149_issue;
            $record['diamond_data']['1.00 TO 1.49_hold'] = $group_100_149_hold;
            $record['diamond_data']['1.00 TO 1.49_sold'] = $group_100_149_sold;
            $record['diamond_data']['0.90 TO 0.99_in_hand'] = $group_90_99_in_hand;
            $record['diamond_data']['0.90 TO 0.99_issue'] = $group_90_99_issue;
            $record['diamond_data']['0.90 TO 0.99_hold'] = $group_90_99_hold;
            $record['diamond_data']['0.90 TO 0.99_sold'] = $group_90_99_sold;
            $record['diamond_data']['0.80 TO 0.89_in_hand'] = $group_80_89_in_hand;
            $record['diamond_data']['0.80 TO 0.89_issue'] = $group_80_89_issue;
            $record['diamond_data']['0.80 TO 0.89_hold'] = $group_80_89_hold;
            $record['diamond_data']['0.80 TO 0.89_sold'] = $group_80_89_sold;
            $record['diamond_data']['0.70 TO 0.79_in_hand'] = $group_70_79_in_hand;
            $record['diamond_data']['0.70 TO 0.79_issue'] = $group_70_79_issue;
            $record['diamond_data']['0.70 TO 0.79_hold'] = $group_70_79_hold;
            $record['diamond_data']['0.70 TO 0.79_sold'] = $group_70_79_sold;
            $record['diamond_data']['0.01 TO 0.69_in_hand'] = $group_01_69_in_hand;
            $record['diamond_data']['0.01 TO 0.69_issue'] = $group_01_69_issue;
            $record['diamond_data']['0.01 TO 0.69_hold'] = $group_01_69_hold;
            $record['diamond_data']['0.01 TO 0.69_sold'] = $group_01_69_sold;
            // echo "<pre>"; print_r($record); exit;

            return $record;

        }
    /*/group report*/

    /*group daily Report*/
        public function get_all_group_daily_data()
        {
            $subsql     = '';
            $subsql1    = '';
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }

            $opening_query = "  SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create
                            ";
            // echo "<pre>"; print_r($opening_query); exit;
            $opening_data = $this->db->query($opening_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;

            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;

            if(!empty($opening_data))
            {
                foreach ($opening_data as $key => $value)
                {
                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_pur++;
                    }
                }
            }

            $opening_non_issue        = 0;
            $opening_01_69_issue      = 0;
            $opening_70_79_issue      = 0;
            $opening_80_89_issue      = 0;
            $opening_90_99_issue      = 0;
            $opening_100_149_issue    = 0;
            $opening_150_199_issue    = 0;
            $opening_200_299_issue    = 0;
            $opening_300_1000_issue   = 0;
            $opening_fancy_issue      = 0;

            $opening_issue_query = "  SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_suspense = 0 AND dm.dm_status = 1 AND dm.dm_date_create <= '".$entry_date."' ORDER BY dm.dm_date_create
                            ";
            // echo "<pre>"; print_r($opening_issue_query); exit;
            $opening_issue_data = $this->db->query($opening_issue_query)->result_array();
            // echo "<pre>"; print_r($opening_issue_data); exit;

            if(!empty($opening_issue_data))
            {
                foreach ($opening_issue_data as $key => $value)
                {
                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            $opening_non_issue++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                $opening_01_69_issue++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                $opening_70_79_issue++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                $opening_80_89_issue++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                $opening_90_99_issue++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                $opening_100_149_issue++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                $opening_150_199_issue++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                $opening_200_299_issue++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_issue++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_issue++;
                    }
                }
            }

            $record['opening_data']['total_non_issue']        = $opening_non_issue;
            $record['opening_data']['total_fancy_issue']      = $opening_fancy_issue;
            $record['opening_data']['total_300_1000_issue']   = $opening_300_1000_issue;
            $record['opening_data']['total_200_299_issue']    = $opening_200_299_issue;
            $record['opening_data']['total_150_199_issue']    = $opening_150_199_issue;
            $record['opening_data']['total_100_149_issue']    = $opening_100_149_issue;
            $record['opening_data']['total_90_99_issue']      = $opening_90_99_issue;
            $record['opening_data']['total_80_89_issue']      = $opening_80_89_issue;
            $record['opening_data']['total_70_79_issue']      = $opening_70_79_issue;
            $record['opening_data']['total_01_69_issue']      = $opening_01_69_issue;

            $challan_return_query ="
                                    SELECT crm.*, crt.*
                                    FROM challan_return_trans crt
                                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                    LEFT JOIN diamond_master dm ON(dm.dm_id = crt.crt_dm_id)
                                    WHERE crm.crm_entry_date < '$entry_date'
                                    AND crt.crt_dm_status = 3
                                    AND dm.dm_suspense = 0
                                    ORDER BY crt.crt_id DESC
                                ";
            // echo "<pre>"; print_r($challan_return_query); exit;
            $challan_return_data = $this->db->query($challan_return_query)->result_array();
            // echo "<pre>"; print_r($challan_return_data); exit();

            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($challan_return_data))
            {
                foreach ($challan_return_data as $key => $value)
                {
                    if(strtoupper($value['crt_dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['crt_dm_lab']) == 'NON')
                        {
                            $opening_non_sold++;
                        }
                        else
                        {
                            if($value['crt_dm_wt'] >= 0.01 && $value['crt_dm_wt'] <= 0.69)
                            {
                                $opening_01_69_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.70 && $value['crt_dm_wt'] <= 0.79)
                            {
                                $opening_70_79_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.80 && $value['crt_dm_wt'] <= 0.89)
                            {
                                $opening_80_89_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.90 && $value['crt_dm_wt'] <= 0.99)
                            {
                                $opening_90_99_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 1.00 && $value['crt_dm_wt'] <= 1.49)
                            {
                                // echo "<pre>"; print_r($opening_100_149_sold);
                                $opening_100_149_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 1.50 && $value['crt_dm_wt'] <= 1.99)
                            {
                                $opening_150_199_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 2.00 && $value['crt_dm_wt'] <= 2.99)
                            {
                                $opening_200_299_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 3.00 && $value['crt_dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_sold++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_sold++;
                    }
                }
            }

            // echo "<pre>"; print_r($opening_100_149_pur);
            // echo "<pre>"; print_r($opening_100_149_sold); exit();



            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;

            // echo "<pre>"; print_r($record); exit();

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;

            $data_query = "
                            SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create
                          ";
            // echo "<pre>"; print_r($data_query); exit;
            $data = $this->db->query($data_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;

            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id  = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id  = $value['dm_id'];

                    if($ct_id != 0)
                    {
                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            LEFT JOIN diamond_master dm ON(dm.dm_id = ct.ct_dm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            AND dm.dm_suspense = 0
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            LEFT JOIN diamond_master dm ON(dm.dm_id = crt.crt_dm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            AND dm.dm_suspense = 0
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_return_query); exit();

                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data);
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else
                    {
                         $dm_status = 0;
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
                // exit();
            }

            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            // echo "<pre>"; print_r($record); exit();
            return $record;
        }
        public function get_all_group_daily_data1()
        {
            $subsql     = '';
            $subsql1    = '';
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }

            $opening_query = "  SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create
                            ";
            // echo "<pre>"; print_r($opening_query); exit;
            $opening_data = $this->db->query($opening_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;

            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;

            if(!empty($opening_data))
            {
                foreach ($opening_data as $key => $value)
                {
                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_pur++;
                    }
                }
            }

            $challan_return_query ="
                                    SELECT crm.*, crt.*
                                    FROM challan_return_trans crt
                                    LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                    LEFT JOIN diamond_master dm ON(dm.dm_id = crt.crt_dm_id)
                                    WHERE crm.crm_entry_date < '$entry_date'
                                    AND crt.crt_dm_status = 3
                                    AND dm.dm_suspense = 0
                                    ORDER BY crt.crt_id DESC
                                ";
            // echo "<pre>"; print_r($challan_return_query); exit;
            $challan_return_data = $this->db->query($challan_return_query)->result_array();
            // echo "<pre>"; print_r($challan_return_data); exit();

            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($challan_return_data))
            {
                foreach ($challan_return_data as $key => $value)
                {
                    if(strtoupper($value['crt_dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['crt_dm_lab']) == 'NON')
                        {
                            $opening_non_sold++;
                        }
                        else
                        {
                            if($value['crt_dm_wt'] >= 0.01 && $value['crt_dm_wt'] <= 0.69)
                            {
                                $opening_01_69_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.70 && $value['crt_dm_wt'] <= 0.79)
                            {
                                $opening_70_79_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.80 && $value['crt_dm_wt'] <= 0.89)
                            {
                                $opening_80_89_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 0.90 && $value['crt_dm_wt'] <= 0.99)
                            {
                                $opening_90_99_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 1.00 && $value['crt_dm_wt'] <= 1.49)
                            {
                                // echo "<pre>"; print_r($opening_100_149_sold);
                                $opening_100_149_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 1.50 && $value['crt_dm_wt'] <= 1.99)
                            {
                                $opening_150_199_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 2.00 && $value['crt_dm_wt'] <= 2.99)
                            {
                                $opening_200_299_sold++;
                            }
                            elseif($value['crt_dm_wt'] >= 3.00 && $value['crt_dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_sold++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_sold++;
                    }
                }
            }

            // echo "<pre>"; print_r($opening_100_149_pur);
            // echo "<pre>"; print_r($opening_100_149_sold); exit();
            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;

            // echo "<pre>"; print_r($record); exit();

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;

            $data_query = "
                            SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create
                          ";
            // echo "<pre>"; print_r($data_query); exit;
            $data = $this->db->query($data_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;

            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id  = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id  = $value['dm_id'];

                    if($ct_id != 0)
                    {
                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            LEFT JOIN diamond_master dm ON(dm.dm_id = ct.ct_dm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            AND dm.dm_suspense = 0
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            LEFT JOIN diamond_master dm ON(dm.dm_id = crt.crt_dm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            AND dm.dm_suspense = 0
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_return_query); exit();

                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data);
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else
                    {
                         $dm_status = 0;
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
                // exit();
            }

            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            // echo "<pre>"; print_r($record); exit();
            return $record;
        }
        public function get_all_group_daily_data2()
        {
            $subsql     = '';
            $subsql1    = '';
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            $purchase_query = "  SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create
                            ";

            // echo "<pre>"; print_r($purchase_query); exit;
            $purchase_data = $this->db->query($purchase_query)->result_array();
            // echo "<pre>"; print_r($purchase_data); exit;

            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;

            if(!empty($purchase_data))
            {
                foreach ($purchase_data as $key => $value)
                {
                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_pur++;
                    }
                }
            }

            $sold_query = "  SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_status = 3 AND dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create
                            ";

            // echo "<pre>"; print_r($sold_query); exit;
            $sold_data = $this->db->query($sold_query)->result_array();
            // echo "<pre>"; print_r($sold_data); exit;

            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($sold_data))
            {
                foreach ($sold_data as $key => $value)
                {
                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            $opening_non_sold++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                $opening_01_69_sold++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                $opening_70_79_sold++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                $opening_80_89_sold++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                $opening_90_99_sold++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                $opening_100_149_sold++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                $opening_150_199_sold++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                $opening_200_299_sold++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                $opening_300_1000_sold++;
                            }
                        }
                    }
                    else
                    {
                        $opening_fancy_sold++;
                    }
                }
            }

            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;


            $data_query = "
                            SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create
                          ";
            // echo "<pre>"; print_r($data_query); exit;
            $data = $this->db->query($data_query)->result_array();
            // echo "<pre>"; print_r($data); exit;

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;


            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id  = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id  = $value['dm_id'];

                    if($ct_id != 0)
                    {
                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_return_query); exit();

                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data);
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else
                    {
                         $dm_status = 0;
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
                // exit();
            }

            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            return $record;
        }
        public function get_all_group_daily_data3()
        {
            $subsql     = "";
            $subsql1    = "";
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";

                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }

            $opening_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($opening_query); exit;
            $opening_data = $this->db->query($opening_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;
            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;


            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($opening_data))
            {
                foreach ($opening_data as $key => $value)
                {
                    $prev_date  = date('Y-m-d', strtotime("-1 day", strtotime($entry_date)));
                    $dm_id      = $value['dm_id'];

                    $challan_query ="
                                        SELECT cm.*, ct.*
                                        FROM challan_trans ct
                                        LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                        WHERE ct.ct_dm_id = $dm_id
                                        AND cm.cm_entry_date = '$prev_date'
                                        ORDER BY ct.ct_id DESC
                                        LIMIT 1
                                    ";
                    // echo "<pre>"; print_r($challan_query); exit();

                    $challan_data = $this->db->query($challan_query)->result_array();
                    // echo "<pre>"; print_r($challan_data); exit();

                    if(!empty($challan_data))
                    {
                        $ct_id = $challan_data[0]['ct_id'];
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_ct_id = $ct_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data); exit();

                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 1;
                        }
                    }
                    else
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_dm_id = $dm_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 3)
                            {
                                $opening_non_sold++;
                            }
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_01_69_sold++;
                                }
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_70_79_sold++;
                                }
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_80_89_sold++;
                                }
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_90_99_sold++;
                                }
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_100_149_sold++;
                                }
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_150_199_sold++;
                                }
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_200_299_sold++;
                                }
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_300_1000_sold++;
                                }
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 3)
                        {
                            $opening_fancy_sold++;
                        }
                        $opening_fancy_pur++;
                    }

                }
            }

            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;

            // echo "<pre>"; print_r($record); exit();

            $query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($query); exit;
            $data = $this->db->query($query)->result_array();
            // echo "<pre>"; print_r($data); exit;

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;
            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id = $value['dm_id'];
                    if($ct_id != 0)
                    {

                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_return_query); exit();

                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data);
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }

                    }
                    else
                    {
                         $dm_status = 0;
                    }






                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
                // exit();
            }

            $group_01_69_issue = 0;
            $group_70_79_issue = 0;
            $group_80_89_issue = 0;
            $group_90_99_issue = 0;
            $group_100_149_issue = 0;
            $group_150_199_issue = 0;
            $group_200_299_issue = 0;
            $group_300_1000_issue = 0;
            $group_fancy_issue = 0;
            $group_non_issue = 0;


            foreach ($data as $key => $value) {
                if(strtoupper($value['dm_sh']) == 'ROUND')
                {
                    // echo "<pre>"; print_r($value['dm_wt']); exit;
                    if(strtoupper($value['dm_lab']) != 'NON')
                    {
                        if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_01_69_issue = $group_01_69_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_70_79_issue = $group_70_79_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_80_89_issue = $group_80_89_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_90_99_issue = $group_90_99_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_100_149_issue = $group_100_149_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_150_199_issue = $group_150_199_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_200_299_issue = $group_200_299_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_300_1000_issue = $group_300_1000_issue + 1;
                            }
                        }
                    }
                }
                else
                {
                    if($value['dm_status'] == 1)
                    {
                        $group_fancy_issue = $group_fancy_issue + 1;
                    }
                }
            }
            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            // echo "<pre>"; print_r($record); exit();
            return $record;

        }
        public function get_all_group_daily_data4()
        {
            $subsql     = "";
            $subsql1    = "";
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";

                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }

            $opening_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($opening_query); exit;
            $opening_data = $this->db->query($opening_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;
            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;


            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($opening_data))
            {
                foreach ($opening_data as $key => $value)
                {
                    $prev_date  = date('Y-m-d', strtotime("-1 day", strtotime($entry_date)));
                    $dm_id      = $value['dm_id'];

                    $challan_query ="
                                        SELECT cm.*, ct.*
                                        FROM challan_trans ct
                                        LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                        WHERE ct.ct_dm_id = $dm_id
                                        AND cm.cm_entry_date = '$prev_date'
                                        ORDER BY ct.ct_id DESC
                                        LIMIT 1
                                    ";
                    // echo "<pre>"; print_r($challan_query); exit();

                    $challan_data = $this->db->query($challan_query)->result_array();
                    // echo "<pre>"; print_r($challan_data); exit();

                    if(!empty($challan_data))
                    {
                        $ct_id = $challan_data[0]['ct_id'];
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_ct_id = $ct_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data); exit();

                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 1;
                        }
                    }
                    else
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_dm_id = $dm_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 3)
                            {
                                $opening_non_sold++;
                            }
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_01_69_sold++;
                                }
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_70_79_sold++;
                                }
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_80_89_sold++;
                                }
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_90_99_sold++;
                                }
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_100_149_sold++;
                                }
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_150_199_sold++;
                                }
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_200_299_sold++;
                                }
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_300_1000_sold++;
                                }
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 3)
                        {
                            $opening_fancy_sold++;
                        }
                        $opening_fancy_pur++;
                    }

                }
            }

            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;

            // echo "<pre>"; print_r($record); exit();

            $query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($query); exit;
            $data = $this->db->query($query)->result_array();
            // echo "<pre>"; print_r($data); exit;

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;
            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id = $value['dm_id'];
                    if($ct_id != 0)
                    {

                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data); exit();
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }

                    }
                    else
                    {
                         $dm_status = 0;
                    }






                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
            }

            $diamond_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_update ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $diamond_data = $this->db->query($diamond_query)->result_array();
            // echo "<pre>"; print_r($diamond_data); exit;

            $group_01_69_issue = 0;
            $group_70_79_issue = 0;
            $group_80_89_issue = 0;
            $group_90_99_issue = 0;
            $group_100_149_issue = 0;
            $group_150_199_issue = 0;
            $group_200_299_issue = 0;
            $group_300_1000_issue = 0;
            $group_fancy_issue = 0;
            $group_non_issue = 0;
            foreach ($diamond_data as $key => $value) {
                if(strtoupper($value['dm_sh']) == 'ROUND')
                {
                    // echo "<pre>"; print_r($value['dm_wt']); exit;
                    if(strtoupper($value['dm_lab']) != 'NON')
                    {
                        if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_01_69_issue = $group_01_69_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_70_79_issue = $group_70_79_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_80_89_issue = $group_80_89_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_90_99_issue = $group_90_99_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_100_149_issue = $group_100_149_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_150_199_issue = $group_150_199_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_200_299_issue = $group_200_299_issue + 1;
                            }
                        }
                        elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                        {
                            if($value['dm_status'] == 1)
                            {
                                $group_300_1000_issue = $group_300_1000_issue + 1;
                            }
                        }
                    }
                }
                else
                {
                    if($value['dm_status'] == 1)
                    {
                        $group_fancy_issue = $group_fancy_issue + 1;
                    }
                }
            }
            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            // echo "<pre>"; print_r($record); exit();
            return $record;

        }
        public function get_all_group_daily_data5()
        {
            $subsql     = "";
            $subsql1    = "";
            $entry_date = '';

            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";

                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }
            else
            {
                $entry_date = date('Y-m-d');
                $subsql .= " AND dm.dm_date_create LIKE '".$entry_date."%'";
                $subsql1 .= " AND dm.dm_date_create < '".$entry_date."'";
            }

            $opening_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 $subsql1 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($opening_query); exit;
            $opening_data = $this->db->query($opening_query)->result_array();
            // echo "<pre>"; print_r($opening_data); exit;
            $opening_non_pur        = 0;
            $opening_01_69_pur      = 0;
            $opening_70_79_pur      = 0;
            $opening_80_89_pur      = 0;
            $opening_90_99_pur      = 0;
            $opening_100_149_pur    = 0;
            $opening_150_199_pur    = 0;
            $opening_200_299_pur    = 0;
            $opening_300_1000_pur   = 0;
            $opening_fancy_pur      = 0;


            $opening_01_69_sold     = 0;
            $opening_70_79_sold     = 0;
            $opening_80_89_sold     = 0;
            $opening_90_99_sold     = 0;
            $opening_100_149_sold   = 0;
            $opening_150_199_sold   = 0;
            $opening_200_299_sold   = 0;
            $opening_300_1000_sold  = 0;
            $opening_fancy_sold     = 0;
            $opening_non_sold       = 0;

            if(!empty($opening_data))
            {
                foreach ($opening_data as $key => $value)
                {
                    $prev_date  = date('Y-m-d', strtotime("-1 day", strtotime($entry_date)));
                    $dm_id      = $value['dm_id'];

                    $challan_query ="
                                        SELECT cm.*, ct.*
                                        FROM challan_trans ct
                                        LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                        WHERE ct.ct_dm_id = $dm_id
                                        AND cm.cm_entry_date = '$prev_date'
                                        ORDER BY ct.ct_id DESC
                                        LIMIT 1
                                    ";
                    // echo "<pre>"; print_r($challan_query); exit();

                    $challan_data = $this->db->query($challan_query)->result_array();
                    // echo "<pre>"; print_r($challan_data); exit();

                    if(!empty($challan_data))
                    {
                        $ct_id = $challan_data[0]['ct_id'];
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_ct_id = $ct_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data); exit();

                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 1;
                        }
                    }
                    else
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_dm_id = $dm_id
                                            AND crm.crm_entry_date = '$prev_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }

                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 3)
                            {
                                $opening_non_sold++;
                            }
                            $opening_non_pur++;
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_01_69_sold++;
                                }
                                $opening_01_69_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_70_79_sold++;
                                }
                                $opening_70_79_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_80_89_sold++;
                                }
                                $opening_80_89_pur++;
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_90_99_sold++;
                                }
                                $opening_90_99_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_100_149_sold++;
                                }
                                $opening_100_149_pur++;
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_150_199_sold++;
                                }
                                $opening_150_199_pur++;
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_200_299_sold++;
                                }
                                $opening_200_299_pur++;
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 3)
                                {
                                    $opening_300_1000_sold++;
                                }
                                $opening_300_1000_pur++;
                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 3)
                        {
                            $opening_fancy_sold++;
                        }
                        $opening_fancy_pur++;
                    }

                }
            }

            $record['opening_data']['total_non_bal']        = $opening_non_pur - $opening_non_sold;
            $record['opening_data']['total_fancy_bal']      = $opening_fancy_pur - $opening_fancy_sold;
            $record['opening_data']['total_300_1000_bal']   = $opening_300_1000_pur - $opening_300_1000_sold;
            $record['opening_data']['total_200_299_bal']    = $opening_200_299_pur - $opening_200_299_sold;
            $record['opening_data']['total_150_199_bal']    = $opening_150_199_pur - $opening_150_199_sold;
            $record['opening_data']['total_100_149_bal']    = $opening_100_149_pur - $opening_100_149_sold;
            $record['opening_data']['total_90_99_bal']      = $opening_90_99_pur - $opening_90_99_sold;
            $record['opening_data']['total_80_89_bal']      = $opening_80_89_pur - $opening_80_89_sold;
            $record['opening_data']['total_70_79_bal']      = $opening_70_79_pur - $opening_70_79_sold;
            $record['opening_data']['total_01_69_bal']      = $opening_01_69_pur - $opening_01_69_sold;

            // echo "<pre>"; print_r($record); exit();

            $query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 ORDER BY dm.dm_date_create ";
            // echo "<pre>"; print_r($query); exit;
            $data = $this->db->query($query)->result_array();
            // echo "<pre>"; print_r($data); exit;

            $group_non_pur        = 0;
            $group_01_69_pur      = 0;
            $group_70_79_pur      = 0;
            $group_80_89_pur      = 0;
            $group_90_99_pur      = 0;
            $group_100_149_pur    = 0;
            $group_150_199_pur    = 0;
            $group_200_299_pur    = 0;
            $group_300_1000_pur   = 0;
            $group_fancy_pur      = 0;

            $group_01_69_in_hand  = 0;
            $group_01_69_issue    = 0;
            $group_01_69_hold     = 0;
            $group_01_69_sold     = 0;
            $group_01_69_return   = 0;
            $group_70_79_in_hand  = 0;
            $group_70_79_issue    = 0;
            $group_70_79_hold     = 0;
            $group_70_79_sold     = 0;
            $group_70_79_return   = 0;
            $group_80_89_in_hand  = 0;
            $group_80_89_issue    = 0;
            $group_80_89_hold     = 0;
            $group_80_89_sold     = 0;
            $group_80_89_return   = 0;
            $group_90_99_in_hand  = 0;
            $group_90_99_issue    = 0;
            $group_90_99_hold     = 0;
            $group_90_99_sold     = 0;
            $group_90_99_return   = 0;
            $group_100_149_in_hand= 0;
            $group_100_149_issue  = 0;
            $group_100_149_hold   = 0;
            $group_100_149_sold   = 0;
            $group_100_149_return = 0;
            $group_150_199_in_hand= 0;
            $group_150_199_issue  = 0;
            $group_150_199_hold   = 0;
            $group_150_199_sold   = 0;
            $group_150_199_return = 0;
            $group_200_299_in_hand= 0;
            $group_200_299_issue  = 0;
            $group_200_299_hold   = 0;
            $group_200_299_sold   = 0;
            $group_200_299_return = 0;
            $group_300_1000_in_hand= 0;
            $group_300_1000_issue = 0;
            $group_300_1000_hold  = 0;
            $group_300_1000_sold  = 0;
            $group_300_1000_return= 0;
            $group_fancy_in_hand  = 0;
            $group_fancy_issue    = 0;
            $group_fancy_hold     = 0;
            $group_fancy_sold     = 0;
            $group_fancy_return   = 0;
            $group_non_in_hand    = 0;
            $group_non_issue      = 0;
            $group_non_hold       = 0;
            $group_non_sold       = 0;
            $group_non_return     = 0;
            if(!empty($data))
            {
                // echo "<pre>"; print_r($data); exit();
                foreach ($data as $key => $value)
                {
                    // echo "<pre>"; print_r($value); exit();
                    // $str=substr($str, 0, strrpos($str, ' '));
                    $created_data = substr($value['dm_date_create'], 0, strrpos($value['dm_date_create'], ' '));

                    $ct_id = $value['dm_ct_id'];
                    $crt_id = $value['dm_crt_id'];
                    $dm_id = $value['dm_id'];
                    if($ct_id != 0)
                    {

                        $challan_query ="
                                            SELECT cm.*, ct.*
                                            FROM challan_trans ct
                                            LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                                            WHERE ct.ct_id = $ct_id
                                            AND cm.cm_entry_date = '$entry_date'
                                            ORDER BY ct.ct_id DESC
                                            LIMIT 1
                                        ";
                        // echo "<pre>"; print_r($challan_query); exit();

                        $challan_data = $this->db->query($challan_query)->result_array();
                        // echo "<pre>"; print_r($challan_data); exit();

                        if(!empty($challan_data))
                        {
                            $dm_status = 1;
                        }
                        else
                        {
                            $dm_status = 0;
                        }
                    }
                    else if($crt_id != 0)
                    {
                        $challan_return_query ="
                                            SELECT crm.*, crt.*
                                            FROM challan_return_trans crt
                                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                                            WHERE crt.crt_id = $crt_id
                                            AND crm.crm_entry_date = '$entry_date'
                                            ORDER BY crt.crt_id DESC
                                            LIMIT 1
                                        ";
                        $challan_return_data = $this->db->query($challan_return_query)->result_array();
                        // echo "<pre>"; print_r($challan_return_data); exit();
                        if(!empty($challan_return_data))
                        {
                            $dm_status = $challan_return_data[0]['crt_dm_status'];
                        }
                        else
                        {
                            $dm_status = 0;
                        }

                    }
                    else
                    {
                         $dm_status = 0;
                    }






                    if(strtoupper($value['dm_sh']) == 'ROUND')
                    {
                        // echo "<pre>"; print_r($value['dm_wt']); exit;
                        if(strtoupper($value['dm_lab']) == 'NON')
                        {
                            if($dm_status == 1)
                            {
                                $group_non_issue++;
                            }
                            elseif($dm_status == 2)
                            {
                                $group_non_hold++;
                            }
                            elseif($dm_status == 3)
                            {
                                $group_non_sold++;
                            }
                            elseif($dm_status == 4)
                            {
                                $group_non_return++;
                            }
                            else
                            {
                                $group_non_in_hand++;
                            }

                            if($entry_date == $created_data)
                            {
                                $group_non_pur++;
                            }
                        }
                        else
                        {
                            if($value['dm_wt'] >= 0.01 && $value['dm_wt'] <= 0.69)
                            {
                                if($dm_status == 1)
                                {
                                    $group_01_69_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_01_69_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_01_69_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_01_69_return++;
                                }
                                else
                                {
                                    $group_01_69_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_01_69_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.70 && $value['dm_wt'] <= 0.79)
                            {
                                if($dm_status == 1)
                                {
                                    $group_70_79_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_70_79_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_70_79_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_70_79_return++;
                                }
                                else
                                {
                                    $group_70_79_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_70_79_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.80 && $value['dm_wt'] <= 0.89)
                            {
                                if($dm_status == 1)
                                {
                                    $group_80_89_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_80_89_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_80_89_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_80_89_return++;
                                }
                                else
                                {
                                    $group_80_89_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_80_89_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 0.90 && $value['dm_wt'] <= 0.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_90_99_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_90_99_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_90_99_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_90_99_return++;
                                }
                                else
                                {
                                    $group_90_99_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_90_99_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.00 && $value['dm_wt'] <= 1.49)
                            {
                                if($dm_status == 1)
                                {
                                    $group_100_149_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_100_149_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_100_149_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_100_149_return++;
                                }
                                else
                                {
                                    $group_100_149_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_100_149_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 1.50 && $value['dm_wt'] <= 1.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_150_199_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_150_199_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_150_199_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_150_199_return++;
                                }
                                else
                                {
                                    $group_150_199_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_150_199_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 2.00 && $value['dm_wt'] <= 2.99)
                            {
                                if($dm_status == 1)
                                {
                                    $group_200_299_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_200_299_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_200_299_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_200_299_return++;
                                }
                                else
                                {
                                    $group_200_299_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_200_299_pur++;
                                }
                            }
                            elseif($value['dm_wt'] >= 3.00 && $value['dm_wt'] <= 10.00)
                            {
                                if($dm_status == 1)
                                {
                                    $group_300_1000_issue++;
                                }
                                elseif($dm_status == 2)
                                {
                                    $group_300_1000_hold++;
                                }
                                elseif($dm_status == 3)
                                {
                                    $group_300_1000_sold++;
                                }
                                elseif($dm_status == 4)
                                {
                                    $group_300_1000_return++;
                                }
                                else
                                {
                                    $group_300_1000_in_hand++;
                                }

                                if($entry_date == $created_data)
                                {
                                    $group_300_1000_pur++;
                                }

                            }
                        }
                    }
                    else
                    {
                        if($dm_status == 1)
                        {
                            $group_fancy_issue++;
                        }
                        elseif($dm_status == 2)
                        {
                            $group_fancy_hold++;
                        }
                        elseif($dm_status == 3)
                        {
                            $group_fancy_sold++;
                        }
                        elseif($dm_status == 4)
                        {
                            $group_fancy_return++;
                        }
                        else
                        {
                            $group_fancy_in_hand++;
                        }

                        if($entry_date == $created_data)
                        {
                            $group_fancy_pur++;
                        }
                    }


                }
            }

            $record['diamond_data']['total_non_pur']        = $group_non_pur;
            $record['diamond_data']['total_non_in_hand']    = $group_non_in_hand;
            $record['diamond_data']['total_non_issue']      = $group_non_issue;
            $record['diamond_data']['total_non_hold']       = $group_non_hold;
            $record['diamond_data']['total_non_return']     = $group_non_return;
            $record['diamond_data']['total_non_sold']       = $group_non_sold;
            $record['diamond_data']['total_non_bal']        = $group_non_pur - $group_non_sold;

            $record['diamond_data']['total_fancy_pur']      = $group_fancy_pur;
            $record['diamond_data']['total_fancy_in_hand']  = $group_fancy_in_hand;
            $record['diamond_data']['total_fancy_issue']    = $group_fancy_issue;
            $record['diamond_data']['total_fancy_hold']     = $group_fancy_hold;
            $record['diamond_data']['total_fancy_return']   = $group_fancy_return;
            $record['diamond_data']['total_fancy_sold']     = $group_fancy_sold;
            $record['diamond_data']['total_fancy_bal']      = $group_fancy_pur - $group_fancy_sold;

            $record['diamond_data']['total_300_1000_pur']   = $group_300_1000_pur;
            $record['diamond_data']['total_300_1000_in_hand']= $group_300_1000_in_hand;
            $record['diamond_data']['total_300_1000_issue'] = $group_300_1000_issue;
            $record['diamond_data']['total_300_1000_hold']  = $group_300_1000_hold;
            $record['diamond_data']['total_300_1000_return']= $group_300_1000_return;
            $record['diamond_data']['total_300_1000_sold']  = $group_300_1000_sold;
            $record['diamond_data']['total_300_1000_bal']   = $group_300_1000_pur - $group_300_1000_sold;

            $record['diamond_data']['total_200_299_pur']    = $group_200_299_pur;
            $record['diamond_data']['total_200_299_in_hand']= $group_200_299_in_hand;
            $record['diamond_data']['total_200_299_issue']  = $group_200_299_issue;
            $record['diamond_data']['total_200_299_hold']   = $group_200_299_hold;
            $record['diamond_data']['total_200_299_return'] = $group_200_299_return;
            $record['diamond_data']['total_200_299_sold']   = $group_200_299_sold;
            $record['diamond_data']['total_200_299_bal']    = $group_200_299_pur - $group_200_299_sold;

            $record['diamond_data']['total_150_199_pur']    = $group_150_199_pur;
            $record['diamond_data']['total_150_199_in_hand']= $group_150_199_in_hand;
            $record['diamond_data']['total_150_199_issue']  = $group_150_199_issue;
            $record['diamond_data']['total_150_199_hold']   = $group_150_199_hold;
            $record['diamond_data']['total_150_199_return'] = $group_150_199_return;
            $record['diamond_data']['total_150_199_sold']   = $group_150_199_sold;
            $record['diamond_data']['total_150_199_bal']    = $group_150_199_pur - $group_150_199_sold;

            $record['diamond_data']['total_100_149_pur']    = $group_100_149_pur;
            $record['diamond_data']['total_100_149_in_hand']= $group_100_149_in_hand;
            $record['diamond_data']['total_100_149_issue']  = $group_100_149_issue;
            $record['diamond_data']['total_100_149_hold']   = $group_100_149_hold;
            $record['diamond_data']['total_100_149_return'] = $group_100_149_return;
            $record['diamond_data']['total_100_149_sold']   = $group_100_149_sold;
            $record['diamond_data']['total_100_149_bal']    = $group_100_149_pur - $group_100_149_sold;

            $record['diamond_data']['total_90_99_pur']      = $group_90_99_pur;
            $record['diamond_data']['total_90_99_in_hand']  = $group_90_99_in_hand;
            $record['diamond_data']['total_90_99_issue']    = $group_90_99_issue;
            $record['diamond_data']['total_90_99_hold']     = $group_90_99_hold;
            $record['diamond_data']['total_90_99_return']   = $group_90_99_return;
            $record['diamond_data']['total_90_99_sold']     = $group_90_99_sold;
            $record['diamond_data']['total_90_99_bal']      = $group_90_99_pur - $group_90_99_sold;

            $record['diamond_data']['total_80_89_pur']      = $group_80_89_pur;
            $record['diamond_data']['total_80_89_in_hand']  = $group_80_89_in_hand;
            $record['diamond_data']['total_80_89_issue']    = $group_80_89_issue;
            $record['diamond_data']['total_80_89_hold']     = $group_80_89_hold;
            $record['diamond_data']['total_80_89_return']   = $group_80_89_return;
            $record['diamond_data']['total_80_89_sold']     = $group_80_89_sold;
            $record['diamond_data']['total_80_89_bal']      = $group_80_89_pur - $group_80_89_sold;

            $record['diamond_data']['total_70_79_pur']      = $group_70_79_pur;
            $record['diamond_data']['total_70_79_in_hand']  = $group_70_79_in_hand;
            $record['diamond_data']['total_70_79_issue']    = $group_70_79_issue;
            $record['diamond_data']['total_70_79_hold']     = $group_70_79_hold;
            $record['diamond_data']['total_70_79_return']   = $group_70_79_return;
            $record['diamond_data']['total_70_79_sold']     = $group_70_79_sold;
            $record['diamond_data']['total_70_79_bal']      = $group_70_79_pur - $group_70_79_sold;

            $record['diamond_data']['total_01_69_pur']      = $group_01_69_pur;
            $record['diamond_data']['total_01_69_in_hand']  = $group_01_69_in_hand;
            $record['diamond_data']['total_01_69_issue']    = $group_01_69_issue;
            $record['diamond_data']['total_01_69_hold']     = $group_01_69_hold;
            $record['diamond_data']['total_01_69_return']   = $group_01_69_return;
            $record['diamond_data']['total_01_69_sold']     = $group_01_69_sold;
            $record['diamond_data']['total_01_69_bal']      = $group_01_69_pur - $group_01_69_sold;

            // echo "<pre>"; print_r($record); exit();
            return $record;

        }
    /*group daily Report*/

    /*certificate Report*/
        public function get_all_certificate_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND crt.crt_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND crm.crm_entry_date ='".$entry_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }

            return $this->db->query("SELECT
                                crm.crm_id
                            FROM challan_return_trans crt
                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                            LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                            LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                            WHERE crt.crt_dm_cert = 0 AND crt.crt_dm_status = 3
                            AND crm.crm_entry_date >= '2019-11-20'
                            $subsql
                ")->num_rows();
        }
        public function get_all_certificate_data($per_page, $offset)
        {
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND crt.crt_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                $subsql .= " AND (crm_cust.acc_id =".$_GET['search_value_party']." OR crm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (crm_brk.acc_id =".$_GET['search_value_broker']." OR crm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND crm.crm_entry_date ='".$entry_date."'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND crm.crm_id =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            $diamond_query = "SELECT
                                crm.crm_id, crm.crm_entry_date, crm.crm_entry_no,
                                crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker,
                                crt.*
                            FROM challan_return_trans crt
                            LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                            LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                            LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                            WHERE crt.crt_dm_cert = 0 AND crt.crt_dm_status = 3 $subsql
                            AND crm.crm_entry_date >= '2019-11-20'
                            ORDER BY crt.crt_id DESC
                            LIMIT $per_page
                             OFFSET $offset";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $diamond_data = $this->db->query("
                SELECT
                    crm.crm_id, crm.crm_entry_date, crm.crm_entry_no, crm.crm_cust_id, crm.crm_broker_id,
                    crm_cust.acc_name as crm_customer, crm_brk.acc_name as crm_broker,
                    crt.*
                FROM challan_return_trans crt
                LEFT JOIN challan_return_master crm ON(crm.crm_id = crt.crt_crm_id)
                LEFT JOIN account_master crm_cust ON(crm_cust.acc_id = crm.crm_cust_id)
                LEFT JOIN account_master crm_brk ON(crm_brk.acc_id = crm.crm_broker_id)
                WHERE crt.crt_dm_cert = 0 AND crt.crt_dm_status = 3
                ORDER BY crt.crt_id DESC
            ")->result_array();

            // stock id
            $record['stock_id_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['stock_id_drop_down'][$value['crt_id']] = $value['crt_dm_code'];

            }
            // stock id


            // party
            $record['party_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                if($value['crm_cust_id'] != '' && $value['crm_cust_id'] > 0)
                {
                    $record['party_drop_down'][$value['crm_cust_id']] = $value['crm_customer'];
                }

            }
            // party

            // broker
            $record['broker_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['broker_drop_down'][$value['crm_broker_id']] = $value['crm_broker'];

            }
            // broker

            // entry no
            $record['entry_no_drop_down'][0] = 'ALL';
            foreach ($diamond_data as $key => $value)
            {
                $record['entry_no_drop_down'][$value['crm_id']] = $value['crm_entry_no'];

            }
            // entry no

            return $record;

        }
        public function get_all_certificate_data_print()
        {
            $subsql = "";

            if(!empty($_GET['search_value_stock_id']))
            {
                $subsql .= " AND dm.dm_id =".$_GET['search_value_stock_id'];
            }
            if(!empty($_GET['search_value_user']))
            {
                $subsql .= " AND um.user_id =".$_GET['search_value_user'];
            }
            if(!empty($_GET['search_value_party']))
            {
                $subsql .= " AND (cm_cust.acc_id =".$_GET['search_value_party']." OR cm_cust.acc_id = ".$_GET['search_value_party'].")";
            }
            if(!empty($_GET['search_value_broker']))
            {
                $subsql .= " AND (cm_brk.acc_id =".$_GET['search_value_broker']." OR cm_brk.acc_id = ".$_GET['search_value_broker'].")";
            }
            if(!empty($_GET['search_value_entry_date']))
            {
                $entry_date = date('Y-m-d', strtotime($_GET['search_value_entry_date']));
                $subsql .= " AND dm.dm_date_update LIKE '".$entry_date."%'";
            }
            if(!empty($_GET['search_value_entry_no']))
            {
                $subsql .= " AND cm.cm_entry_no =".$_GET['search_value_entry_no'];
            }
            if(!empty($_GET['search_value_status']) && $_GET['search_value_status'] != 0)
            {
                if($_GET['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_GET['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_GET['search_value_status'] == 3)
                {
                    $status = 2;
                }
                elseif ($_GET['search_value_status'] == 4)
                {
                    $status = 3;
                }
                else
                {
                    $status = 4;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            $diamond_query = "SELECT dm.*, um.*, cm.*, cm_cust.acc_name as cm_customer, cm_brk.acc_name as cm_broker
                    FROM challan_trans ct
                    LEFT JOIN diamond_master dm ON(ct.ct_id = dm.dm_ct_id)
                    LEFT JOIN challan_master cm ON(cm.cm_id = ct.ct_cm_id)
                    LEFT JOIN user_master um ON(um.user_id = dm.dm_update_id)
                    LEFT JOIN account_master cm_cust ON(cm_cust.acc_id = cm.cm_cust_id)
                    LEFT JOIN account_master cm_brk ON(cm_brk.acc_id = cm.cm_broker_id)
                    WHERE dm.dm_suspense = 0 AND ct.ct_delete = 1 AND dm.dm_suspense = 0 $subsql ORDER BY dm.dm_date_update DESC ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();



            return $record;

        }
    /*/certificate report*/

    /*diamond master*/
        public function get_all_diamond_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql     = "";
            $start_date = date('Y-06-01');
            $end_date   = date('Y-m-d');

            if(!empty($_REQUEST['search_value_stock_id']))
            {
                $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
            }
            if(!empty($_REQUEST['search_value_sh']))
            {
                $sh = '';
                foreach ($_REQUEST['search_value_sh'] as $key => $value)
                {
                    $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                    $sh .= "'".$db_sh."',";
                }
                $sh = rtrim($sh, ',');
                // echo "<pre>"; print_r($sh); exit;
                $subsql .= " AND dm.dm_sh IN (".$sh. ")";
            }
            if(!empty($_REQUEST['search_value_wt']))
            {
                $wt = implode(',', $_REQUEST['search_value_wt']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_wt IN (".$wt. ")";
            }
            if(!empty($_REQUEST['search_value_col']))
            {
                $col = '';
                foreach ($_REQUEST['search_value_col'] as $key => $value)
                {
                    $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                    $col .= "'".$db_col."',";
                }
                $col = rtrim($col, ',');
                // echo "<pre>"; print_r($col); exit;
                $subsql .= " AND dm.dm_col IN (".$col. ")";
            }
            if(!empty($_REQUEST['search_value_cla']))
            {
                $cla = '';
                foreach ($_REQUEST['search_value_cla'] as $key => $value)
                {
                    $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                    $cla .= "'".$db_cla."',";
                }
                $cla = rtrim($cla, ',');
                // echo "<pre>"; print_r($cla); exit;
                $subsql .= " AND dm.dm_cla IN (".$cla. ")";
            }
            if(!empty($_REQUEST['search_value_cut']))
            {
                $cut = '';
                foreach ($_REQUEST['search_value_cut'] as $key => $value)
                {
                    $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                    $cut .= "'".$db_cut."',";
                }
                $cut = rtrim($cut, ',');
                // echo "<pre>"; print_r($cut); exit;
                $subsql .= " AND dm.dm_cut IN (".$cut. ")";
            }
            if(!empty($_REQUEST['search_value_pol']))
            {
                $pol = '';
                foreach ($_REQUEST['search_value_pol'] as $key => $value)
                {
                    $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                    $pol .= "'".$db_pol."',";
                }
                $pol = rtrim($pol, ',');
                // echo "<pre>"; print_r($pol); exit;
                $subsql .= " AND dm.dm_pol IN (".$pol. ")";
            }
            if(!empty($_REQUEST['search_value_sym']))
            {
                $sym = '';
                foreach ($_REQUEST['search_value_sym'] as $key => $value)
                {
                    $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                    $sym .= "'".$db_sym."',";
                }
                $sym = rtrim($sym, ',');
                // echo "<pre>"; print_r($sym); exit;
                $subsql .= " AND dm.dm_sym IN (".$sym. ")";
            }
            if(!empty($_REQUEST['search_value_fl']))
            {
                $fl = '';
                foreach ($_REQUEST['search_value_fl'] as $key => $value)
                {
                    $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                    $fl .= "'".$db_fl."',";
                }
                $fl = rtrim($fl, ',');
                // echo "<pre>"; print_r($fl); exit;
                $subsql .= " AND dm.dm_fl IN (".$fl. ")";
            }
            if(!empty($_REQUEST['search_value_lab']))
            {
                $lab = '';
                foreach ($_REQUEST['search_value_lab'] as $key => $value)
                {
                    $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                    $lab .= "'".$db_lab."',";
                }
                $lab = rtrim($lab, ',');
                // echo "<pre>"; print_r($lab); exit;
                $subsql .= " AND dm.dm_lab IN (".$lab. ")";
            }
            if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
            {
                if($_REQUEST['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_REQUEST['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_REQUEST['search_value_status'] == 3)
                {
                    $status = 2;
                }
                else
                {
                    $status = 3;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            // else
            // {
            //     $subsql .= " AND (dm.dm_status = 0)";
            // }
            if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }




            return $this->db->query("SELECT dm.*
                    FROM diamond_master dm
                    WHERE dm.dm_suspense = 0 $subsql
                ")->num_rows();
        }
        public function get_all_diamond_data($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('Y-06-01');
            $end_date   = date('Y-m-d');

            // echo "<pre>"; print_r($_REQUEST);exit;


            if(!empty($_REQUEST['search_value_stock_id']))
            {
                $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
            }
            if(!empty($_REQUEST['search_value_sh']))
            {
                $sh = '';
                foreach ($_REQUEST['search_value_sh'] as $key => $value)
                {
                    $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                    $sh .= "'".$db_sh."',";
                }
                $sh = rtrim($sh, ',');
                // echo "<pre>"; print_r($sh); exit;
                $subsql .= " AND dm.dm_sh IN (".$sh. ")";
            }
            if(!empty($_REQUEST['search_value_wt']))
            {
                $wt = implode(',', $_REQUEST['search_value_wt']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_wt IN (".$wt. ")";
            }
            if(!empty($_REQUEST['search_value_col']))
            {
                $col = '';
                foreach ($_REQUEST['search_value_col'] as $key => $value)
                {
                    $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                    $col .= "'".$db_col."',";
                }
                $col = rtrim($col, ',');
                // echo "<pre>"; print_r($col); exit;
                $subsql .= " AND dm.dm_col IN (".$col. ")";
            }
            if(!empty($_REQUEST['search_value_cla']))
            {
                $cla = '';
                foreach ($_REQUEST['search_value_cla'] as $key => $value)
                {
                    $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                    $cla .= "'".$db_cla."',";
                }
                $cla = rtrim($cla, ',');
                // echo "<pre>"; print_r($cla); exit;
                $subsql .= " AND dm.dm_cla IN (".$cla. ")";
            }
            if(!empty($_REQUEST['search_value_cut']))
            {
                $cut = '';
                foreach ($_REQUEST['search_value_cut'] as $key => $value)
                {
                    $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                    $cut .= "'".$db_cut."',";
                }
                $cut = rtrim($cut, ',');
                // echo "<pre>"; print_r($cut); exit;
                $subsql .= " AND dm.dm_cut IN (".$cut. ")";
            }
            if(!empty($_REQUEST['search_value_pol']))
            {
                $pol = '';
                foreach ($_REQUEST['search_value_pol'] as $key => $value)
                {
                    $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                    $pol .= "'".$db_pol."',";
                }
                $pol = rtrim($pol, ',');
                // echo "<pre>"; print_r($pol); exit;
                $subsql .= " AND dm.dm_pol IN (".$pol. ")";
            }
            if(!empty($_REQUEST['search_value_sym']))
            {
                $sym = '';
                foreach ($_REQUEST['search_value_sym'] as $key => $value)
                {
                    $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                    $sym .= "'".$db_sym."',";
                }
                $sym = rtrim($sym, ',');
                // echo "<pre>"; print_r($sym); exit;
                $subsql .= " AND dm.dm_sym IN (".$sym. ")";
            }
            if(!empty($_REQUEST['search_value_fl']))
            {
                $fl = '';
                foreach ($_REQUEST['search_value_fl'] as $key => $value)
                {
                    $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                    $fl .= "'".$db_fl."',";
                }
                $fl = rtrim($fl, ',');
                // echo "<pre>"; print_r($fl); exit;
                $subsql .= " AND dm.dm_fl IN (".$fl. ")";
            }
            if(!empty($_REQUEST['search_value_lab']))
            {
                $lab = '';
                foreach ($_REQUEST['search_value_lab'] as $key => $value)
                {
                    $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                    $lab .= "'".$db_lab."',";
                }
                $lab = rtrim($lab, ',');
                // echo "<pre>"; print_r($lab); exit;
                $subsql .= " AND dm.dm_lab IN (".$lab. ")";
            }
            if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
            {
                if($_REQUEST['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_REQUEST['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_REQUEST['search_value_status'] == 3)
                {
                    $status = 2;
                }
                else
                {
                    $status = 3;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            // else
            // {
            //     $subsql .= " AND (dm.dm_status = 0)";
            // }
            if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }



            $diamond_query = "SELECT dm.*
                                FROM diamond_master dm
                                WHERE dm.dm_suspense = 0 $subsql
                                ORDER BY dm.dm_date_create DESC
                                LIMIT $per_page
                                OFFSET $offset
                            ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $stock_id_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_code
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                            ")->result_array();

            /* Drop Down */
            // stock id
                $record['stock_id_drop_down'] = [];
                // $record['stock_id_drop_down'][0] = 'ALL';
                foreach ($stock_id_data as $key => $value)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // stock id

            // size
            $size_data = $this->db->query("SELECT dm.dm_size,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_size ORDER BY dm_size ASC")->result_array();
            $record['size_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['size_drop_down'][$value['dm_id']] = $value['dm_size'];
            }
            // size

            // cpd
            $size_data = $this->db->query("SELECT dm.dm_cpd,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cpd ORDER BY dm_cpd ASC")->result_array();
            $record['cpd_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['cpd_drop_down'][$value['dm_id']] = $value['dm_cpd'];
            }
            // cpd
            
            // shape
                $shape_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_sh
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_sh
                                                ")->result_array();
                $record['shape_drop_down'] = [];
                foreach ($shape_data as $key => $value)
                {
                    $record['shape_drop_down'][$value['dm_id']] = $value['dm_sh'];
                }
                if (!empty($record['shape_drop_down'])) {
                    asort($record['shape_drop_down']);
                }
            // shape

            // weight
                $weight_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_wt
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_wt
                                                ")->result_array();
                $record['weight_drop_down'] = [];
                foreach ($weight_data as $key => $value)
                {
                    $record['weight_drop_down'][$value['dm_wt']] = $value['dm_wt'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // weight

            // color
                $color_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_col
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_col
                                                ")->result_array();
                $record['color_drop_down'] = [];
                foreach ($color_data as $key => $value)
                {
                    $record['color_drop_down'][$value['dm_id']] = $value['dm_col'];
                }
                if (!empty($record['color_drop_down'])) {
                    asort($record['color_drop_down']);
                }
            // color

            // clarity
                $clarity_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_cla
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_cla
                                                ")->result_array();
                $record['clarity_drop_down'] = [];
                foreach ($clarity_data as $key => $value)
                {
                    $record['clarity_drop_down'][$value['dm_id']] = $value['dm_cla'];
                }
                if (!empty($record['clarity_drop_down'])) {
                    asort($record['clarity_drop_down']);
                }
            // clarity

            // cut
                $cut_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_cut
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_cut
                                            ")->result_array();
                $record['cut_drop_down'] = [];
                foreach ($cut_data as $key => $value)
                {
                    $record['cut_drop_down'][$value['dm_id']] = $value['dm_cut'];
                }
                if (!empty($record['cut_drop_down'])) {
                    asort($record['cut_drop_down']);
                }
            // cut

            // pol
                $pol_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_pol
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d')
                                                BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_pol
                                            ")->result_array();
                $record['pol_drop_down'] = [];
                foreach ($pol_data as $key => $value)
                {
                    $record['pol_drop_down'][$value['dm_id']] = $value['dm_pol'];
                }
                if (!empty($record['pol_drop_down'])) {
                    asort($record['pol_drop_down']);
                }
            // pol

            // sym
                $sym_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_sym
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_sym
                                            ")->result_array();
                $record['sym_drop_down'] = [];
                foreach ($sym_data as $key => $value)
                {
                    $record['sym_drop_down'][$value['dm_id']] = $value['dm_sym'];
                }
                if (!empty($record['sym_drop_down'])) {
                    asort($record['sym_drop_down']);
                }
            // sym

            // fl
                $fl_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_fl
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_fl
                                            ")->result_array();
                $record['fl_drop_down'] = [];
                foreach ($fl_data as $key => $value)
                {
                    $record['fl_drop_down'][$value['dm_id']] = $value['dm_fl'];
                }
                if (!empty($record['fl_drop_down'])) {
                    asort($record['fl_drop_down']);
                }
            // fl

            // lab
                $lab_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_lab
                                                FROM diamond_master dm
                                                WHERE dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_lab
                                            ")->result_array();
                $record['lab_drop_down'] = [];
                foreach ($lab_data as $key => $value)
                {
                    $record['lab_drop_down'][$value['dm_id']] = $value['dm_lab'];
                }
                if (!empty($record['lab_drop_down'])) {
                    asort($record['lab_drop_down']);
                }
            // lab
            /* Drop Down */

            $available_query = "
                                    SELECT dm.*
                                    FROM diamond_master dm
                                    WHERE dm.dm_suspense = 0
                                    $subsql
                                    AND dm.dm_status = 0
                                ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['available_data'] = $this->db->query($available_query)->num_rows();

            $sold_query = "
                            SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_suspense = 0
                            $subsql
                            AND dm.dm_status = 3
                        ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['sold_data'] = $this->db->query($sold_query)->num_rows();
            return $record;
        }
    /*/diamond master*/

    /*R2NET REPORT*/
        public function get_all_r2net_data_cnt()
        {
            // echo "<pre>"; print_r($_GET);exit;
            $subsql     = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

            if(!empty($_REQUEST['search_value_stock_id']))
            {
                $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
            }
            if(!empty($_REQUEST['search_value_sh']))
            {
                $sh = '';
                foreach ($_REQUEST['search_value_sh'] as $key => $value)
                {
                    $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                    $sh .= "'".$db_sh."',";
                }
                $sh = rtrim($sh, ',');
                // echo "<pre>"; print_r($sh); exit;
                $subsql .= " AND dm.dm_sh IN (".$sh. ")";
            }
            if(!empty($_REQUEST['search_value_wt']))
            {
                $wt = implode(',', $_REQUEST['search_value_wt']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_wt IN (".$wt. ")";
            }
            if(!empty($_REQUEST['search_value_col']))
            {
                $col = '';
                foreach ($_REQUEST['search_value_col'] as $key => $value)
                {
                    $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                    $col .= "'".$db_col."',";
                }
                $col = rtrim($col, ',');
                // echo "<pre>"; print_r($col); exit;
                $subsql .= " AND dm.dm_col IN (".$col. ")";
            }
            if(!empty($_REQUEST['search_value_cla']))
            {
                $cla = '';
                foreach ($_REQUEST['search_value_cla'] as $key => $value)
                {
                    $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                    $cla .= "'".$db_cla."',";
                }
                $cla = rtrim($cla, ',');
                // echo "<pre>"; print_r($cla); exit;
                $subsql .= " AND dm.dm_cla IN (".$cla. ")";
            }
            if(!empty($_REQUEST['search_value_cut']))
            {
                $cut = '';
                foreach ($_REQUEST['search_value_cut'] as $key => $value)
                {
                    $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                    $cut .= "'".$db_cut."',";
                }
                $cut = rtrim($cut, ',');
                // echo "<pre>"; print_r($cut); exit;
                $subsql .= " AND dm.dm_cut IN (".$cut. ")";
            }
            if(!empty($_REQUEST['search_value_pol']))
            {
                $pol = '';
                foreach ($_REQUEST['search_value_pol'] as $key => $value)
                {
                    $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                    $pol .= "'".$db_pol."',";
                }
                $pol = rtrim($pol, ',');
                // echo "<pre>"; print_r($pol); exit;
                $subsql .= " AND dm.dm_pol IN (".$pol. ")";
            }
            if(!empty($_REQUEST['search_value_sym']))
            {
                $sym = '';
                foreach ($_REQUEST['search_value_sym'] as $key => $value)
                {
                    $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                    $sym .= "'".$db_sym."',";
                }
                $sym = rtrim($sym, ',');
                // echo "<pre>"; print_r($sym); exit;
                $subsql .= " AND dm.dm_sym IN (".$sym. ")";
            }
            if(!empty($_REQUEST['search_value_fl']))
            {
                $fl = '';
                foreach ($_REQUEST['search_value_fl'] as $key => $value)
                {
                    $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                    $fl .= "'".$db_fl."',";
                }
                $fl = rtrim($fl, ',');
                // echo "<pre>"; print_r($fl); exit;
                $subsql .= " AND dm.dm_fl IN (".$fl. ")";
            }
            if(!empty($_REQUEST['search_value_lab']))
            {
                $lab = '';
                foreach ($_REQUEST['search_value_lab'] as $key => $value)
                {
                    $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                    $lab .= "'".$db_lab."',";
                }
                $lab = rtrim($lab, ',');
                // echo "<pre>"; print_r($lab); exit;
                $subsql .= " AND dm.dm_lab IN (".$lab. ")";
            }
            if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
            {
                if($_REQUEST['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_REQUEST['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_REQUEST['search_value_status'] == 3)
                {
                    $status = 2;
                }
                else
                {
                    $status = 3;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            else
            {
                $subsql .= " AND (dm.dm_status != 3)";
            }
            if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            $diamond_query = "SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_r2net = 0
                            AND dm.dm_suspense = 0
                            AND dm.dm_lab != 'HRD'
                            $subsql
                            ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $diamond_data = $this->db->query($diamond_query)->result_array();

            $sr_no = 1;
            $shp_arr = ['ROUND', 'PRINCESS', 'SQ RADIANT', 'EMERALD', 'CUSHION', 'PEAR', 'OVAL', 'MARQUISE', 'HEART'];
            $cla_arr = ['I1', 'IF', 'SI1', 'SI2', 'VS1', 'VS2', 'VVS1', 'VVS2'];
            $col_arr = ['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
            foreach ($diamond_data as $key => $value)
            {
                if((in_array($value['dm_cla'], $cla_arr)) && (in_array($value['dm_col'], $col_arr)) && ($value['dm_wt'] >= 0.20))
                {
                    if($value['dm_sh'] == 'ROUND' &&
                        ($value['dm_td'] >= 57.50 && $value['dm_td'] <= 64.90) &&
                        ($value['dm_ta'] >= 51.00 && $value['dm_td'] <= 67.00))
                    {
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'PRINCESS' &&
                        ($value['dm_td'] >= 60.00 && $value['dm_td'] <= 79.90) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 83.00))
                    {
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'SQ RADIANT' &&
                        ($value['dm_td'] >= 57.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'EMERALD' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 76.00))
                    {
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'CUSHION' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $sr_no++;
                    }
                    else if(($value['dm_sh'] == 'PEAR' || $value['dm_sh'] == 'OVAL' || $value['dm_sh'] == 'MARQUISE') &&
                        ($value['dm_td'] >= 54.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $sr_no++;
                    }

                    else if($value['dm_sh'] == 'HEART' &&
                        ($value['dm_td'] >= 45.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $sr_no++;
                    }
                }
            }

            return $sr_no;
        }
        public function get_all_r2net_data($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

            // echo "<pre>"; print_r($_REQUEST);exit;


            if(!empty($_REQUEST['search_value_stock_id']))
            {
                $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
            }
            if(!empty($_REQUEST['search_value_sh']))
            {
                $sh = '';
                foreach ($_REQUEST['search_value_sh'] as $key => $value)
                {
                    $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                    $sh .= "'".$db_sh."',";
                }
                $sh = rtrim($sh, ',');
                // echo "<pre>"; print_r($sh); exit;
                $subsql .= " AND dm.dm_sh IN (".$sh. ")";
            }
            if(!empty($_REQUEST['search_value_wt']))
            {
                $wt = implode(',', $_REQUEST['search_value_wt']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_wt IN (".$wt. ")";
            }
            if(!empty($_REQUEST['search_value_col']))
            {
                $col = '';
                foreach ($_REQUEST['search_value_col'] as $key => $value)
                {
                    $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                    $col .= "'".$db_col."',";
                }
                $col = rtrim($col, ',');
                // echo "<pre>"; print_r($col); exit;
                $subsql .= " AND dm.dm_col IN (".$col. ")";
            }
            if(!empty($_REQUEST['search_value_cla']))
            {
                $cla = '';
                foreach ($_REQUEST['search_value_cla'] as $key => $value)
                {
                    $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                    $cla .= "'".$db_cla."',";
                }
                $cla = rtrim($cla, ',');
                // echo "<pre>"; print_r($cla); exit;
                $subsql .= " AND dm.dm_cla IN (".$cla. ")";
            }
            if(!empty($_REQUEST['search_value_cut']))
            {
                $cut = '';
                foreach ($_REQUEST['search_value_cut'] as $key => $value)
                {
                    $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                    $cut .= "'".$db_cut."',";
                }
                $cut = rtrim($cut, ',');
                // echo "<pre>"; print_r($cut); exit;
                $subsql .= " AND dm.dm_cut IN (".$cut. ")";
            }
            if(!empty($_REQUEST['search_value_pol']))
            {
                $pol = '';
                foreach ($_REQUEST['search_value_pol'] as $key => $value)
                {
                    $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                    $pol .= "'".$db_pol."',";
                }
                $pol = rtrim($pol, ',');
                // echo "<pre>"; print_r($pol); exit;
                $subsql .= " AND dm.dm_pol IN (".$pol. ")";
            }
            if(!empty($_REQUEST['search_value_sym']))
            {
                $sym = '';
                foreach ($_REQUEST['search_value_sym'] as $key => $value)
                {
                    $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                    $sym .= "'".$db_sym."',";
                }
                $sym = rtrim($sym, ',');
                // echo "<pre>"; print_r($sym); exit;
                $subsql .= " AND dm.dm_sym IN (".$sym. ")";
            }
            if(!empty($_REQUEST['search_value_fl']))
            {
                $fl = '';
                foreach ($_REQUEST['search_value_fl'] as $key => $value)
                {
                    $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                    $fl .= "'".$db_fl."',";
                }
                $fl = rtrim($fl, ',');
                // echo "<pre>"; print_r($fl); exit;
                $subsql .= " AND dm.dm_fl IN (".$fl. ")";
            }
            if(!empty($_REQUEST['search_value_lab']))
            {
                $lab = '';
                foreach ($_REQUEST['search_value_lab'] as $key => $value)
                {
                    $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                    $lab .= "'".$db_lab."',";
                }
                $lab = rtrim($lab, ',');
                // echo "<pre>"; print_r($lab); exit;
                $subsql .= " AND dm.dm_lab IN (".$lab. ")";
            }
            if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
            {
                if($_REQUEST['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_REQUEST['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_REQUEST['search_value_status'] == 3)
                {
                    $status = 2;
                }
                else
                {
                    $status = 3;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            else
            {
                $subsql .= " AND (dm.dm_status != 3)";
            }
            if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }



            $diamond_query = "SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_r2net = 0
                            AND dm.dm_suspense = 0
                            AND dm.dm_lab != 'HRD'
                            $subsql
                            ORDER BY dm.dm_date_create DESC
                            LIMIT $per_page
                            OFFSET $offset
                            ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $diamond_data = $this->db->query($diamond_query)->result_array();
            $sr_no = 1;
            $shp_arr = ['ROUND', 'PRINCESS', 'SQ RADIANT', 'EMERALD', 'CUSHION', 'PEAR', 'OVAL', 'MARQUISE', 'HEART'];
            $cla_arr = ['I1', 'IF', 'SI1', 'SI2', 'VS1', 'VS2', 'VVS1', 'VVS2'];
            $col_arr = ['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
            foreach ($diamond_data as $key => $value)
            {
                if((in_array($value['dm_cla'], $cla_arr)) && (in_array($value['dm_col'], $col_arr)) && ($value['dm_wt'] >= 0.20))
                {
                    if($value['dm_sh'] == 'ROUND' &&
                        ($value['dm_td'] >= 57.50 && $value['dm_td'] <= 64.90) &&
                        ($value['dm_ta'] >= 51.00 && $value['dm_td'] <= 67.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'PRINCESS' &&
                        ($value['dm_td'] >= 60.00 && $value['dm_td'] <= 79.90) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 83.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'SQ RADIANT' &&
                        ($value['dm_td'] >= 57.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'EMERALD' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 76.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'CUSHION' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if(($value['dm_sh'] == 'PEAR' || $value['dm_sh'] == 'OVAL' || $value['dm_sh'] == 'MARQUISE') &&
                        ($value['dm_td'] >= 54.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }

                    else if($value['dm_sh'] == 'HEART' &&
                        ($value['dm_td'] >= 45.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                }
            }


            $stock_id_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_code
                                                FROM diamond_master dm
                                                WHERE dm.dm_status != 3 AND dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                            ")->result_array();

            /* Drop Down */
            // stock id
                $record['stock_id_drop_down'] = [];
                // $record['stock_id_drop_down'][0] = 'ALL';
                foreach ($stock_id_data as $key => $value)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // stock id

            // size
            $size_data = $this->db->query("SELECT dm.dm_size,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_size ORDER BY dm_size ASC")->result_array();
            $record['size_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['size_drop_down'][$value['dm_id']] = $value['dm_size'];
            }
            // size

            // cpd
            $size_data = $this->db->query("SELECT dm.dm_cpd,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cpd ORDER BY dm_cpd ASC")->result_array();
            $record['cpd_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['cpd_drop_down'][$value['dm_id']] = $value['dm_cpd'];
            }
            // cpd

            // shape
                $shape_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_sh
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_sh
                                                ")->result_array();
                $record['shape_drop_down'] = [];
                foreach ($shape_data as $key => $value)
                {
                    $record['shape_drop_down'][$value['dm_id']] = $value['dm_sh'];
                }
                if (!empty($record['shape_drop_down'])) {
                    asort($record['shape_drop_down']);
                }
            // shape

            // weight
                $weight_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_wt
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_wt
                                                ")->result_array();
                $record['weight_drop_down'] = [];
                foreach ($weight_data as $key => $value)
                {
                    $record['weight_drop_down'][$value['dm_wt']] = $value['dm_wt'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // weight

            // color
                $color_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_col
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_col
                                                ")->result_array();
                $record['color_drop_down'] = [];
                foreach ($color_data as $key => $value)
                {
                    $record['color_drop_down'][$value['dm_id']] = $value['dm_col'];
                }
                if (!empty($record['color_drop_down'])) {
                    asort($record['color_drop_down']);
                }
            // color

            // clarity
                $clarity_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_cla
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_cla
                                                ")->result_array();
                $record['clarity_drop_down'] = [];
                foreach ($clarity_data as $key => $value)
                {
                    $record['clarity_drop_down'][$value['dm_id']] = $value['dm_cla'];
                }
                if (!empty($record['clarity_drop_down'])) {
                    asort($record['clarity_drop_down']);
                }
            // clarity

            // cut
                $cut_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_cut
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_cut
                                            ")->result_array();
                $record['cut_drop_down'] = [];
                foreach ($cut_data as $key => $value)
                {
                    $record['cut_drop_down'][$value['dm_id']] = $value['dm_cut'];
                }
                if (!empty($record['cut_drop_down'])) {
                    asort($record['cut_drop_down']);
                }
            // cut

            // pol
                $pol_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_pol
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_pol
                                            ")->result_array();
                $record['pol_drop_down'] = [];
                foreach ($pol_data as $key => $value)
                {
                    $record['pol_drop_down'][$value['dm_id']] = $value['dm_pol'];
                }
                if (!empty($record['pol_drop_down'])) {
                    asort($record['pol_drop_down']);
                }
            // pol

            // sym
                $sym_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_sym
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_sym
                                            ")->result_array();
                $record['sym_drop_down'] = [];
                foreach ($sym_data as $key => $value)
                {
                    $record['sym_drop_down'][$value['dm_id']] = $value['dm_sym'];
                }
                if (!empty($record['sym_drop_down'])) {
                    asort($record['sym_drop_down']);
                }
            // sym

            // fl
                $fl_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_fl
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_fl
                                            ")->result_array();
                $record['fl_drop_down'] = [];
                foreach ($fl_data as $key => $value)
                {
                    $record['fl_drop_down'][$value['dm_id']] = $value['dm_fl'];
                }
                if (!empty($record['fl_drop_down'])) {
                    asort($record['fl_drop_down']);
                }
            // fl

            // lab
                $lab_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_lab
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_lab
                                            ")->result_array();
                $record['lab_drop_down'] = [];
                foreach ($lab_data as $key => $value)
                {
                    $record['lab_drop_down'][$value['dm_id']] = $value['dm_lab'];
                }
                if (!empty($record['lab_drop_down'])) {
                    asort($record['lab_drop_down']);
                }
            // lab
            /* Drop Down */

            $available_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE 1 $subsql AND dm.dm_status = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['available_data'] = $this->db->query($available_query)->num_rows();

            $sold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE 1 $subsql AND dm.dm_status = 3";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['sold_data'] = $this->db->query($sold_query)->num_rows();
            return $record;
        }
        public function r2net_report_excel()
        {

            $diamond_query = "SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0 AND dm.dm_lab != 'HRD'
                            ORDER BY dm.dm_date_create DESC
                            ";
            $diamond_data = $this->db->query($diamond_query)->result_array();
            $sr_no = 1;
            $shp_arr = ['ROUND', 'PRINCESS', 'SQ RADIANT', 'EMERALD', 'CUSHION', 'PEAR', 'OVAL', 'MARQUISE', 'HEART'];
            $cla_arr = ['I1', 'IF', 'SI1', 'SI2', 'VS1', 'VS2', 'VVS1', 'VVS2'];
            $col_arr = ['D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
            foreach ($diamond_data as $key => $value)
            {
                if((in_array($value['dm_cla'], $cla_arr)) && (in_array($value['dm_col'], $col_arr)) && ($value['dm_wt'] >= 0.20))
                {
                    if($value['dm_sh'] == 'ROUND' &&
                        ($value['dm_td'] >= 57.50 && $value['dm_td'] <= 64.90) &&
                        ($value['dm_ta'] >= 51.00 && $value['dm_td'] <= 67.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'PRINCESS' &&
                        ($value['dm_td'] >= 60.00 && $value['dm_td'] <= 79.90) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 83.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'SQ RADIANT' &&
                        ($value['dm_td'] >= 57.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'EMERALD' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 76.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if($value['dm_sh'] == 'CUSHION' &&
                        ($value['dm_td'] >= 56.00 && $value['dm_td'] <= 76.00) &&
                        ($value['dm_ta'] >= 52.00 && $value['dm_td'] <= 78.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                    else if(($value['dm_sh'] == 'PEAR' || $value['dm_sh'] == 'OVAL' || $value['dm_sh'] == 'MARQUISE') &&
                        ($value['dm_td'] >= 54.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }

                    else if($value['dm_sh'] == 'HEART' &&
                        ($value['dm_td'] >= 45.00 && $value['dm_td'] <= 69.00) &&
                        ($value['dm_ta'] >= 50.00 && $value['dm_td'] <= 68.00))
                    {
                        $record['diamond_data'][$sr_no] = $value;
                        $sr_no++;
                    }
                }
            }

            // echo "<pre>"; print_r($record); exit();

           $excel_array[0] = array(
                0 =>  'STOCK ID',
                1 =>  'SH',
                2 =>  'WT',
                3 =>  'COL',
                4 =>  'CLA',
                5 =>  'CUT',
                6 =>  'POL',
                7 =>  'SYM',
                8 =>  'FL',
                9 =>  'TD',
                10 => 'TA',
                11 => 'LEN',
                12 => 'WID',
                13 => 'HEI',
                14 => 'LAB',
                15 => 'CERT COMMENTS',
                16 => 'REPORT NO',
                17 => 'DISC',
                18 => 'DISC',
                19 => 'RAP',
                20 => 'PAR CARATE',
                21 => 'TOTAL',
                22 => 'NR PPC',
                23 => 'NR TOTAL'
            );

            // echo "<pre>"; print_r($record); exit();
            foreach ($record['diamond_data'] as $key => $value)
            {
                $disc1 = $value['dm_disc']-2;
                $disc2 = $value['dm_disc']-3;

                $disc_amt1 = ($value['dm_rap'] * $disc1) / 100;
                $per_crt   = $value['dm_rap'] + $disc_amt1;
                $total     = $per_crt * $value['dm_wt'];

                $disc_amt2 = ($value['dm_rap'] * $disc2) / 100;
                $nr_ppc    = $value['dm_rap'] + $disc_amt2;
                $nr_total  = $nr_ppc * $value['dm_wt'];
                /*excel_array*/
               $excel_array[$key+1][0] = $value['dm_code'];
               $excel_array[$key+1][1] = $value['dm_sh'];
               $excel_array[$key+1][2] = $value['dm_wt'];
               $excel_array[$key+1][3] = $value['dm_col'];
               $excel_array[$key+1][4] = $value['dm_cla'];
               $excel_array[$key+1][5] = $value['dm_cut'];
               $excel_array[$key+1][6] = $value['dm_pol'];
               $excel_array[$key+1][7] = $value['dm_sym'];
               $excel_array[$key+1][8] = $value['dm_fl'];
               $excel_array[$key+1][9] = $value['dm_td'];
               $excel_array[$key+1][10] = $value['dm_ta'];
               $excel_array[$key+1][11] = $value['dm_len'];
               $excel_array[$key+1][12] = $value['dm_wid'];
               $excel_array[$key+1][13] = $value['dm_hei'];
               $excel_array[$key+1][14] = $value['dm_lab'];
               $excel_array[$key+1][15] = $value['dm_cert_com'];
               $excel_array[$key+1][16] = $value['dm_report_no'];
               $excel_array[$key+1][17] = number_format($disc1, 2);
               $excel_array[$key+1][18] = number_format($disc2, 2);
               $excel_array[$key+1][19] = $value['dm_rap'];
               $excel_array[$key+1][20] = number_format($per_crt, 2);
               $excel_array[$key+1][21] = number_format($total, 2);
               $excel_array[$key+1][22] = number_format($nr_ppc, 2);
               $excel_array[$key+1][23] = number_format($nr_total, 2);
            }
            // echo "<pre>"; print_r($excel_array); exit;




            // echo "<pre>"; print_r($excel_array); exit;

            return $excel_array;
        }
        public function get_all_r2net_data1($per_page, $offset)
        {
            $subsql = "";
            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');

            // echo "<pre>"; print_r($_REQUEST);exit;


            if(!empty($_REQUEST['search_value_stock_id']))
            {
                $stock_id = implode(',', $_REQUEST['search_value_stock_id']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_id IN (".$stock_id. ")";
            }
            if(!empty($_REQUEST['search_value_sh']))
            {
                $sh = '';
                foreach ($_REQUEST['search_value_sh'] as $key => $value)
                {
                    $db_sh = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sh'];
                    $sh .= "'".$db_sh."',";
                }
                $sh = rtrim($sh, ',');
                // echo "<pre>"; print_r($sh); exit;
                $subsql .= " AND dm.dm_sh IN (".$sh. ")";
            }
            if(!empty($_REQUEST['search_value_wt']))
            {
                $wt = implode(',', $_REQUEST['search_value_wt']);
                // echo "<pre>"; print_r($stock_id); exit;
                $subsql .= " AND dm.dm_wt IN (".$wt. ")";
            }
            if(!empty($_REQUEST['search_value_col']))
            {
                $col = '';
                foreach ($_REQUEST['search_value_col'] as $key => $value)
                {
                    $db_col = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_col'];
                    $col .= "'".$db_col."',";
                }
                $col = rtrim($col, ',');
                // echo "<pre>"; print_r($col); exit;
                $subsql .= " AND dm.dm_col IN (".$col. ")";
            }
            if(!empty($_REQUEST['search_value_cla']))
            {
                $cla = '';
                foreach ($_REQUEST['search_value_cla'] as $key => $value)
                {
                    $db_cla = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cla'];
                    $cla .= "'".$db_cla."',";
                }
                $cla = rtrim($cla, ',');
                // echo "<pre>"; print_r($cla); exit;
                $subsql .= " AND dm.dm_cla IN (".$cla. ")";
            }
            if(!empty($_REQUEST['search_value_cut']))
            {
                $cut = '';
                foreach ($_REQUEST['search_value_cut'] as $key => $value)
                {
                    $db_cut = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_cut'];
                    $cut .= "'".$db_cut."',";
                }
                $cut = rtrim($cut, ',');
                // echo "<pre>"; print_r($cut); exit;
                $subsql .= " AND dm.dm_cut IN (".$cut. ")";
            }
            if(!empty($_REQUEST['search_value_pol']))
            {
                $pol = '';
                foreach ($_REQUEST['search_value_pol'] as $key => $value)
                {
                    $db_pol = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_pol'];
                    $pol .= "'".$db_pol."',";
                }
                $pol = rtrim($pol, ',');
                // echo "<pre>"; print_r($pol); exit;
                $subsql .= " AND dm.dm_pol IN (".$pol. ")";
            }
            if(!empty($_REQUEST['search_value_sym']))
            {
                $sym = '';
                foreach ($_REQUEST['search_value_sym'] as $key => $value)
                {
                    $db_sym = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_sym'];
                    $sym .= "'".$db_sym."',";
                }
                $sym = rtrim($sym, ',');
                // echo "<pre>"; print_r($sym); exit;
                $subsql .= " AND dm.dm_sym IN (".$sym. ")";
            }
            if(!empty($_REQUEST['search_value_fl']))
            {
                $fl = '';
                foreach ($_REQUEST['search_value_fl'] as $key => $value)
                {
                    $db_fl = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_fl'];
                    $fl .= "'".$db_fl."',";
                }
                $fl = rtrim($fl, ',');
                // echo "<pre>"; print_r($fl); exit;
                $subsql .= " AND dm.dm_fl IN (".$fl. ")";
            }
            if(!empty($_REQUEST['search_value_lab']))
            {
                $lab = '';
                foreach ($_REQUEST['search_value_lab'] as $key => $value)
                {
                    $db_lab = $this->db_operations->get_record('diamond_master', ['dm_id'=>$value])[0]['dm_lab'];
                    $lab .= "'".$db_lab."',";
                }
                $lab = rtrim($lab, ',');
                // echo "<pre>"; print_r($lab); exit;
                $subsql .= " AND dm.dm_lab IN (".$lab. ")";
            }
            if(!empty($_REQUEST['search_value_status']) && $_REQUEST['search_value_status'] != 0)
            {
                if($_REQUEST['search_value_status'] == 1)
                {
                    $status = 0;
                }
                elseif ($_REQUEST['search_value_status'] == 2)
                {
                    $status = 1;
                }
                elseif ($_REQUEST['search_value_status'] == 3)
                {
                    $status = 2;
                }
                else
                {
                    $status = 3;
                }
                $subsql .= " AND dm.dm_status =".$status;
            }
            else
            {
                $subsql .= " AND (dm.dm_status != 3)";
            }
            if(!empty($_REQUEST['search_value_start_date']) && !empty($_REQUEST['search_value_end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_REQUEST['search_value_start_date']));
                $end_date = date('Y-m-d', strtotime($_REQUEST['search_value_end_date']));
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }
            else
            {
                $subsql .= " AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }



            $diamond_query = "SELECT dm.*
                            FROM diamond_master dm
                            WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                            $subsql
                            ORDER BY dm.dm_date_create DESC
                            LIMIT $per_page
                            OFFSET $offset
                            ";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['diamond_data'] = $this->db->query($diamond_query)->result_array();

            $stock_id_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_code
                                                FROM diamond_master dm
                                                WHERE dm.dm_status != 3 AND dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                            ")->result_array();

            /* Drop Down */
            // stock id
                $record['stock_id_drop_down'] = [];
                // $record['stock_id_drop_down'][0] = 'ALL';
                foreach ($stock_id_data as $key => $value)
                {
                    $record['stock_id_drop_down'][$value['dm_id']] = $value['dm_code'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // stock id

            // size
            $size_data = $this->db->query("SELECT dm.dm_size,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_size ORDER BY dm_size ASC")->result_array();
            $record['size_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['size_drop_down'][$value['dm_id']] = $value['dm_size'];
            }
            // size
            
            // cpd
            $size_data = $this->db->query("SELECT dm.dm_cpd,dm.dm_id FROM diamond_master dm WHERE 1 AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_cpd ORDER BY dm_cpd ASC")->result_array();
            $record['cpd_drop_down'] = [];
            foreach ($size_data as $key => $value)
            {
                $record['cpd_drop_down'][$value['dm_id']] = $value['dm_cpd'];
            }
            // cpd

            // shape
                $shape_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_sh
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_sh
                                                ")->result_array();
                $record['shape_drop_down'] = [];
                foreach ($shape_data as $key => $value)
                {
                    $record['shape_drop_down'][$value['dm_id']] = $value['dm_sh'];
                }
                if (!empty($record['shape_drop_down'])) {
                    asort($record['shape_drop_down']);
                }
            // shape

            // weight
                $weight_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_wt
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_wt
                                                ")->result_array();
                $record['weight_drop_down'] = [];
                foreach ($weight_data as $key => $value)
                {
                    $record['weight_drop_down'][$value['dm_wt']] = $value['dm_wt'];
                }
                if (!empty($record['weight_drop_down'])) {
                    asort($record['weight_drop_down']);
                }
            // weight

            // color
                $color_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_col
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_col
                                                ")->result_array();
                $record['color_drop_down'] = [];
                foreach ($color_data as $key => $value)
                {
                    $record['color_drop_down'][$value['dm_id']] = $value['dm_col'];
                }
                if (!empty($record['color_drop_down'])) {
                    asort($record['color_drop_down']);
                }
            // color

            // clarity
                $clarity_data = $this->db->query("
                                                    SELECT dm.dm_id, dm.dm_cla
                                                    FROM diamond_master dm
                                                    WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                    AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                    GROUP BY dm.dm_cla
                                                ")->result_array();
                $record['clarity_drop_down'] = [];
                foreach ($clarity_data as $key => $value)
                {
                    $record['clarity_drop_down'][$value['dm_id']] = $value['dm_cla'];
                }
                if (!empty($record['clarity_drop_down'])) {
                    asort($record['clarity_drop_down']);
                }
            // clarity

            // cut
                $cut_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_cut
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_cut
                                            ")->result_array();
                $record['cut_drop_down'] = [];
                foreach ($cut_data as $key => $value)
                {
                    $record['cut_drop_down'][$value['dm_id']] = $value['dm_cut'];
                }
                if (!empty($record['cut_drop_down'])) {
                    asort($record['cut_drop_down']);
                }
            // cut

            // pol
                $pol_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_pol
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_pol
                                            ")->result_array();
                $record['pol_drop_down'] = [];
                foreach ($pol_data as $key => $value)
                {
                    $record['pol_drop_down'][$value['dm_id']] = $value['dm_pol'];
                }
                if (!empty($record['pol_drop_down'])) {
                    asort($record['pol_drop_down']);
                }
            // pol

            // sym
                $sym_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_sym
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'
                                                GROUP BY dm.dm_sym
                                            ")->result_array();
                $record['sym_drop_down'] = [];
                foreach ($sym_data as $key => $value)
                {
                    $record['sym_drop_down'][$value['dm_id']] = $value['dm_sym'];
                }
                if (!empty($record['sym_drop_down'])) {
                    asort($record['sym_drop_down']);
                }
            // sym

            // fl
                $fl_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_fl
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_fl
                                            ")->result_array();
                $record['fl_drop_down'] = [];
                foreach ($fl_data as $key => $value)
                {
                    $record['fl_drop_down'][$value['dm_id']] = $value['dm_fl'];
                }
                if (!empty($record['fl_drop_down'])) {
                    asort($record['fl_drop_down']);
                }
            // fl

            // lab
                $lab_data = $this->db->query("
                                                SELECT dm.dm_id, dm.dm_lab
                                                FROM diamond_master dm
                                                WHERE dm.dm_r2net = 0 AND dm.dm_suspense = 0
                                                AND DATE_FORMAT(dm.dm_date_create, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."' GROUP BY dm.dm_lab
                                            ")->result_array();
                $record['lab_drop_down'] = [];
                foreach ($lab_data as $key => $value)
                {
                    $record['lab_drop_down'][$value['dm_id']] = $value['dm_lab'];
                }
                if (!empty($record['lab_drop_down'])) {
                    asort($record['lab_drop_down']);
                }
            // lab
            /* Drop Down */

            $available_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE 1 $subsql AND dm.dm_status = 0";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['available_data'] = $this->db->query($available_query)->num_rows();

            $sold_query = "SELECT dm.*
                    FROM diamond_master dm
                    WHERE 1 $subsql AND dm.dm_status = 3";
            // echo "<pre>"; print_r($diamond_query); exit;
            $record['sold_data'] = $this->db->query($sold_query)->num_rows();
            return $record;
        }

    /*/R2NET REPORT*/
        
        /*   START SALES REPORT   */
        public function get_all_sales_data_cnt()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(inv.inv_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND inv.inv_customer_id=".$_GET['acc_id'];
            }

            return $this->db->query("SELECT inv.*, am.*, am.acc_name, am.acc_mobile, am.acc_email, sm.state_name, cm.city_name FROM invoice_master inv 
                LEFT JOIN account_master am ON(am.acc_id = inv.inv_customer_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                LEFT JOIN city_master cm ON(cm.city_id = am.acc_city_id)
                WHERE 1 $subsql ")->num_rows();
        }
        public function get_all_sales_data($per_page,$offset)
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(inv.inv_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND inv.inv_customer_id=".$_GET['acc_id'];
            }

            $record['inv_data'] = $this->db->query("SELECT inv.*, am.*, am.acc_name, am.acc_mobile, am.acc_email, sm.state_name, cm.city_name FROM invoice_master inv 
                LEFT JOIN account_master am ON(am.acc_id = inv.inv_customer_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                LEFT JOIN city_master cm ON(cm.city_id = am.acc_city_id)
                WHERE 1 $subsql LIMIT $per_page OFFSET $offset")->result_array();

            $account_data = $this->db->query("SELECT * FROM account_master WHERE acc_grp_id = 5")->result_array();
            $record['account_data'][0] = "PLEASE SELECT";
            foreach ($account_data as $key => $value) {
                $record['account_data'][$value['acc_id']] = $value['acc_name'];
            }

            return $record;
        }

        public function get_all_sales_data_print()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(inv.inv_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND inv.inv_customer_id=".$_GET['acc_id'];
            }

            $record['inv_data'] =  $this->db->query("SELECT invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, invt.invt_sub_total_amt as total_taxable_amt, invt.invt_amt_cgst as total_cgst, invt.invt_amt_sgst as total_sgst, invt.invt_amt_igst as total_igst, invt.invt_total_amt as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code, inv.inv_invoice_date, am.acc_name, am.acc_gst, inv.inv_entry_no, sm.state_name FROM invoice_trans invt 
                LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
                LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
                LEFT JOIN account_master am ON(am.acc_id = inv.inv_customer_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                WHERE 1 $subsql")->result_array();

            // echo "<pre>";
            // print_r($record);
            // exit;
            return $record;
        }

        public function get_all_sale_data_excel()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(inv.inv_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND inv.inv_customer_id=".$_GET['acc_id'];
            }
            
            // $sale_data =  $this->db->query("SELECT invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, invt.invt_sub_total_amt as total_taxable_amt, invt.invt_amt_cgst as total_cgst, invt.invt_amt_sgst as total_sgst, invt.invt_amt_igst as total_igst, invt.invt_total_amt as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code, inv.inv_invoice_date, am.acc_name, am.acc_gst, inv.inv_entry_no, sm.state_name FROM invoice_trans invt 
            //     LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
            //     LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
            //     LEFT JOIN account_master am ON(am.acc_id = inv.inv_customer_id)
            //     LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
            //     WHERE 1 $subsql")->result_array();
            
            $sale_data =  $this->db->query("SELECT inv.inv_id
                FROM invoice_master inv WHERE 1 $subsql ")->result_array();
            
            foreach($sale_data as $key => $value)
            {
                $inv_id = $value['inv_id'];
                
                $sale_data[$key] =  $this->db->query("SELECT invt.invt_inv_id, invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, SUM(invt.invt_qty) as invt_qty, SUM(invt.invt_sub_total_amt) as total_taxable_amt, SUM(invt.invt_amt_cgst) as total_cgst, SUM(invt.invt_amt_sgst) as total_sgst, SUM(invt.invt_amt_igst) as total_igst, SUM(invt.invt_total_amt) as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code, inv.inv_invoice_date, am.acc_name, am.acc_gst, inv.inv_entry_no, sm.state_name 
                FROM invoice_trans invt
                LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
                LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
                 LEFT JOIN account_master am ON(am.acc_id = inv.inv_customer_id)
                 LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                WHERE invt.invt_inv_id = $inv_id GROUP BY invt_inv_id")->result_array()[0];
            }
            
            $sr_no = 0;
            $excel_array[0] = array(
                0 =>  'SR NO',
                1 =>  'Customer',
                2 =>  'GST No',
                3 =>  'State Name',
                4 =>  'Inv. No',
                5 =>  'Inv. Date',
                6 =>  'HSN/SAC Code',
                7 =>  'Qty',
                8 =>  'Taxable Value',
                9 =>  '0.75% CGST',
                10 =>  '0.75% SGST',
                11 => '1.50% IGST',
                12 => 'NET AMT'
            );

            // echo "<pre>"; print_r($record); exit();
            $ctot_wt = 0;
            $ctot_taxable_amt = 0;
            $ctot_cgst_amt = 0;
            $ctot_sgst_amt = 0;
            $ctot_igst_amt = 0;
            $ctot_final_amt = 0;
            foreach ($sale_data as $key => $value) 
            {
                $sr_no          += 1;
                /*excel_array*/
                $excel_array[$key+1][0] = $sr_no;
                $excel_array[$key+1][1] = $value['acc_name'];
                $excel_array[$key+1][2] = $value['acc_gst'];
                $excel_array[$key+1][3] = $value['state_name'];
                $excel_array[$key+1][4] = $value['inv_entry_no'];
                $excel_array[$key+1][5] = date('d-m-Y', strtotime($value['inv_invoice_date']));
                $excel_array[$key+1][6] = $value['pro_hsn_code'];
                $excel_array[$key+1][7] = $value['invt_qty'];
                $excel_array[$key+1][8] = $value['total_taxable_amt'];
                $excel_array[$key+1][9] = $value['total_cgst'];
                $excel_array[$key+1][10] = $value['total_sgst'];
                $excel_array[$key+1][11] = $value['total_igst'];
                $excel_array[$key+1][12] = $value['total_amt'];
               
                $ctot_wt += $value['invt_qty'];
                $ctot_taxable_amt += $value['total_taxable_amt'];
                $ctot_cgst_amt += $value['total_cgst'];
                $ctot_sgst_amt += $value['total_sgst'];
                $ctot_igst_amt += $value['total_igst'];
                $ctot_final_amt += $value['total_amt'];
            }
            
            $tot_cnt = count($excel_array);
            
            $excel_array[$tot_cnt] = array(
                0 =>  '',
                1 =>  '',
                2 =>  '',
                3 =>  '',
                4 =>  '',
                5 =>  '',
                6 =>  'Total',
                7 =>  $ctot_wt,
                8 =>  $ctot_taxable_amt,
                9 =>  $ctot_cgst_amt,
                10 => $ctot_sgst_amt,
                11 => $ctot_igst_amt,
                12 => $ctot_final_amt
            );

            return $excel_array;            
        }
        /*   END SALES REPORT   */

        /*   START HSN Wise SALES REPORT   */
        public function get_all_hsnwise_sales_data_cnt()
        {
            $subsql = "";
            
            if(!empty($_GET['hsn_no']))
            {
                $subsql .= " AND invt.invt_hsn_code=".$_GET['hsn_no'];
            }

            if(!empty($_GET['pro_id']))
            {
                $subsql .= " AND invt.invt_pro_id=".$_GET['pro_id'];
            }

            return $this->db->query("SELECT invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, SUM(invt.invt_qty) as total_qty, SUM(invt.invt_rate) as total_rate, SUM(invt.invt_amt_cgst) as total_cgst, SUM(invt.invt_amt_sgst) as total_sgst, SUM(invt.invt_amt_igst) as total_igst, SUM(invt.invt_total_amt) as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code FROM invoice_trans invt 
                LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
                LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
                WHERE 1 $subsql GROUP BY invt.invt_pro_id")->num_rows();
        }

        public function get_all_hsnwise_sales_data($per_page,$offset)
        {
            $subsql = "";
            
            if(!empty($_GET['hsn_no']))
            {
                $subsql .= " AND invt.invt_hsn_code=".$_GET['hsn_no'];
            }

            if(!empty($_GET['pro_id']))
            {
                $subsql .= " AND invt.invt_pro_id=".$_GET['pro_id'];
            }

            $record['hsn_data'] =  $this->db->query("SELECT invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, SUM(invt.invt_qty) as total_qty, SUM(invt.invt_rate) as total_rate, SUM(invt.invt_amt_cgst) as total_cgst, SUM(invt.invt_amt_sgst) as total_sgst, SUM(invt.invt_amt_igst) as total_igst, SUM(invt.invt_total_amt) as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code FROM invoice_trans invt 
                LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
                LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
                WHERE 1 $subsql GROUP BY invt.invt_pro_id  LIMIT $per_page OFFSET $offset")->result_array();

            $products =  $this->db->query("SELECT pm.pro_id, pm.pro_name, pm.pro_hsn_code FROM product_master pm WHERE pro_status = 1")->result_array();
            $record['products'][''] = "Please select";
            foreach($products as $key => $value)
            {
                $record['products'][$value['pro_id']] = $value['pro_name'];
            }

            $record['hsn_no'][''] = "Please select";
            foreach($products as $key => $value)
            {
                $record['hsn_no'][$value['pro_hsn_code']] = $value['pro_hsn_code'];
            }

            return $record;
        }

        public function get_all_hsnwise_sales_data_print()
        {
            $subsql = "";
            
            if(!empty($_GET['hsn_no']))
            {
                $subsql .= " AND invt.invt_hsn_code=".$_GET['hsn_no'];
            }

            if(!empty($_GET['pro_id']))
            {
                $subsql .= " AND invt.invt_pro_id=".$_GET['pro_id'];
            }

            $record['hsn_data'] =  $this->db->query("SELECT invt.invt_hsn_code, invt.invt_id, invt.invt_pro_id, SUM(invt.invt_qty) as total_qty, SUM(invt.invt_rate) as total_rate, SUM(invt.invt_amt_cgst) as total_cgst, SUM(invt.invt_amt_sgst) as total_sgst, SUM(invt.invt_amt_igst) as total_igst, SUM(invt.invt_total_amt) as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code FROM invoice_trans invt 
                LEFT JOIN invoice_master inv ON(inv.inv_id = invt.invt_inv_id)
                LEFT JOIN product_master pm ON(pm.pro_id = invt.invt_pro_id)
                WHERE 1 $subsql GROUP BY invt.invt_pro_id")->result_array();
            
            return $record;
        }
        /*   END HSN Wise SALES REPORT   */
        
        /*   START PURCHASE REPORT   */
        public function get_all_purchase_data_cnt()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(per.per_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND per.per_supplier_id=".$_GET['acc_id'];
            }

            return $this->db->query("SELECT per.*, am.*, am.acc_name, am.acc_mobile, am.acc_email, sm.state_name, cm.city_name FROM purchase_master per 
                LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                LEFT JOIN city_master cm ON(cm.city_id = am.acc_city_id)
                WHERE 1 $subsql")->num_rows();
        }

        public function get_all_purchase_data($per_page,$offset)
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(per.per_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND per.per_supplier_id=".$_GET['acc_id'];
            }

            $record['purchase_data'] = $this->db->query("SELECT per.*, am.*, am.acc_name, am.acc_mobile, am.acc_email, sm.state_name, cm.city_name FROM purchase_master per 
                LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                LEFT JOIN city_master cm ON(cm.city_id = am.acc_city_id)
                WHERE 1 $subsql LIMIT $per_page OFFSET $offset")->result_array();

            $account_data = $this->db->query("SELECT * FROM account_master WHERE acc_grp_id = 6")->result_array();
            $record['account_data'][0] = "PLEASE SELECT";
            foreach ($account_data as $key => $value) {
                $record['account_data'][$value['acc_id']] = $value['acc_name'];
            }

            return $record;
        }

        public function get_all_purchase_data_print()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(per.per_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND per.per_supplier_id=".$_GET['acc_id'];
            }
            
            // $record['purchase_data'] = $this->db->query("SELECT per.*, am.*, am.acc_name, am.acc_mobile, am.acc_email, sm.state_name, cm.city_name FROM purchase_master per 
            //     LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)
            //     LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
            //     LEFT JOIN city_master cm ON(cm.city_id = am.acc_city_id)
            //     WHERE 1 $subsql")->result_array();

            $record['purchase_data'] =  $this->db->query("SELECT prt.prt_hsn_code, prt.prt_id, prt.prt_pro_id, prt.prt_sub_total_amt as total_taxable_amt, prt.prt_amt_cgst as total_cgst, prt.prt_amt_sgst as total_sgst, prt.prt_amt_igst as total_igst, prt.prt_total_amt as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code, per.per_invoice_date, am.acc_name, am.acc_gst, per.per_entry_no, sm.state_name FROM purchase_trans prt 
                LEFT JOIN purchase_master per ON(per.per_id = prt.prt_per_id)
                LEFT JOIN product_master pm ON(pm.pro_id = prt.prt_pro_id)
                LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                WHERE 1 $subsql")->result_array();
            
            return $record;
        }

         public function get_all_purchase_data_excel()
        {
            $subsql = "";

            $start_date = date('2019-06-01');
            $end_date   = date('Y-m-d');
            if(!empty($_GET['start_date']) && !empty($_GET['end_date']))
            {
                $start_date = date('Y-m-d', strtotime($_GET['start_date']));
                $end_date = date('Y-m-d', strtotime($_GET['end_date']));
                $subsql .= " AND DATE_FORMAT(per.per_invoice_date, '%Y-%m-%d') BETWEEN '".$start_date."' AND '".$end_date."'";
            }

            if(!empty($_GET['acc_id']))
            {
                $subsql .= " AND per.per_supplier_id=".$_GET['acc_id'];
            }

            $purchase_data =  $this->db->query("SELECT prt.prt_hsn_code, prt.prt_id, prt.prt_pro_id, prt.prt_sub_total_amt as total_taxable_amt, prt.prt_amt_cgst as total_cgst, prt.prt_amt_sgst as total_sgst, prt.prt_amt_igst as total_igst, prt.prt_total_amt as total_amt, pm.pro_id, pm.pro_name, pm.pro_hsn_code, per.per_invoice_date, am.acc_name, am.acc_gst, per.per_entry_no, sm.state_name FROM purchase_trans prt 
                LEFT JOIN purchase_master per ON(per.per_id = prt.prt_per_id)
                LEFT JOIN product_master pm ON(pm.pro_id = prt.prt_pro_id)
                LEFT JOIN account_master am ON(am.acc_id = per.per_supplier_id)
                LEFT JOIN state_master sm ON(sm.state_id = am.acc_state_id)
                WHERE 1 $subsql")->result_array();

            $sr_no = 0;
            $excel_array[0] = array(
                0 =>  'SR NO',
                1 =>  'Supplier',
                2 =>  'GST No',
                3 =>  'State Name',
                4 =>  'Inv. No',
                5 =>  'Inv. Date',
                6 =>  'HSN/SAC Code',
                7 =>  'Taxable Value',
                8 =>  '0.125% CGST',
                9 =>  '0.125% SGST',
                10 => '0.125% IGST',
                11 => 'NET AMT'
            );

            // echo "<pre>"; print_r($record); exit();
             $tot_taxable_amt = 0;
            $tot_cgst_amt = 0;
            $tot_sgst_amt = 0;
            $tot_igst_amt = 0;
            $tot_final_amt = 0;
            foreach ($purchase_data as $key => $value) 
            {
                $sr_no          += 1;
                /*excel_array*/
               $excel_array[$key+1][0] = $sr_no;
               $excel_array[$key+1][1] = $value['acc_name'];
               $excel_array[$key+1][2] = $value['acc_gst'];
               $excel_array[$key+1][3] = $value['state_name'];
               $excel_array[$key+1][4] = $value['per_entry_no'];
               $excel_array[$key+1][5] = date('d-m-Y', strtotime($value['per_invoice_date']));
               $excel_array[$key+1][6] = $value['pro_hsn_code'];
               $excel_array[$key+1][7] = $value['total_taxable_amt'];
               $excel_array[$key+1][8] = $value['total_cgst'];
               $excel_array[$key+1][9] = $value['total_sgst'];
               $excel_array[$key+1][10] = $value['total_igst'];
               $excel_array[$key+1][11] = $value['total_amt'];
               
               $tot_taxable_amt += $value['total_taxable_amt'];
               $tot_cgst_amt += $value['total_cgst'];
               $tot_sgst_amt += $value['total_sgst'];
               $tot_igst_amt += $value['total_igst'];
               $tot_final_amt += $value['total_amt'];
            }
            
            $tot_cnt = count($excel_array);
            
            $excel_array[$tot_cnt] = array(
                0 =>  '',
                1 =>  '',
                2 =>  '',
                3 =>  '',
                4 =>  '',
                5 =>  '',
                6 =>  'Total',
                7 =>  $tot_taxable_amt,
                8 =>  $tot_cgst_amt,
                9 =>  $tot_sgst_amt,
                10 => $tot_igst_amt,
                11 => $tot_final_amt
            );

            return $excel_array;            
        }
        /*   END PURCHASE REPORT   */

    }
?>