<?php
/*

 * To change this license header, choose License Headers in Project Properties.

 * To change this template file, choose Tools | Templates

 * and open the template in the editor.
 */
require_once(__DIR__ . '/../../extensions/Spreadsheet/autoload.php'); 
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
use PhpOffice\PhpSpreadsheet\Cell\Cell;
use PhpOffice\PhpSpreadsheet\Worksheet\Drawing;
use PhpOffice\PhpSpreadsheet\Style\Border;

class OrderController extends Controller {

    public function init() {

        parent::init();

        $this->initAjaxCsrfToken();
    }

    // this function will work to post csrf token.

    protected function initAjaxCsrfToken() {



        Yii::app()->clientScript->registerScript('AjaxCsrfToken', ' $.ajaxSetup({

                         data: {"YII_CSRF_TOKEN": "' . Yii::app()->request->csrfToken . '"},

                         cache:false

                    });', CClientScript::POS_HEAD);
    }

    public function accessRules() {

        return array(
            array('allow', // allow all users to perform 'index' and 'view' actions
                'actions' => array('login', 'error', 'logout'),
                'users' => array('*'),
            ),
            array('allow', // allow authenticated user to perform 'create' and 'update' actions
                'actions' => array('Index', 'DownloadOrderFormat', 'DownloadOrderFormatExcel', 'GetList', 'Save', 'View', 'Edit', 'SaveEdittedOrder', 'Delete', 'UploadOrder', 'SaveUpload', 'UploadDocuments'
                    , 'ConsolidatedOrder', 'GetConsolidatedOrderList', 'AddConsolidatedOrder', 'SaveConsolidatedOrder', 'DownloadConsolidatedOrder', 'DownloadUploadedConsolidatedOrder', 'deleteconsolOrder',
                    'GetviewList', 'GetOrderdetails', 'DownloadUploadedOrderQty','DownloadCreditHoldDetails'),
                'expression' => '$user->isMember()',
            ),
            array('deny', // deny all users
                'users' => array('*'),
            ),
        );
    }

    public function actionIndex() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $this->render('index');
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    // Uncomment the following methods and override them if needed

    public function filters() {

        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    public function actionGetList() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);
        if (count($_POST) > 0) {

            $model = new Order();

            $draw = $_POST['draw'];

            $offset = $_POST['start'];

            $limit = $_POST['length'];

            $distributer_id = $_POST['distributer_id'];

            $year = $_POST['year'];

            $month = $_POST['month'];

            $columns = array(
                // datatable column index  => database column name

                0 => 't.id',
                1 => 't.order_date',
                2 => 'distributer_det.distributer_name',
                3 => 't.month',
                4 => 't.year',
            );

            $filterColumn = $columns[$_POST['order'][0]['column']];

            $filterOrder = $_POST['order'][0]['dir'];

            if ($_POST['order'][1]['column'] > 0) {

                $filterColumn .= ' , ' . $columns[$_POST['order'][1]['column']];
            }

            $orderBy = $filterColumn . " " . $filterOrder;

            $condition = '1=1';

            if ($distributer_id > 0) {

                $condition .= ' AND distributer_id=' . $distributer_id;
            }

            if ($month != '') {

                $condition .= ' AND month=' . $month;
            }

            if ($year > 0) {

                $condition .= ' AND year=' . $year;
            }

            $arr_months = Order::getmonths();

            if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {

                $searchString = $_POST['search']['value'];

                $condition .= " and (distributer_det.distributer_name LIKE '%" . $searchString . "%' 

                         OR month LIKE '%" . $searchString . "%' OR year LIKE '%" . $searchString . "%')";
            }

            $count = $model->with('distributer_det')->count(array('condition' => $condition));

            $model = $model->with('distributer_det')->findAll(array('condition' => $condition, 'limit' => $limit, 'offset' => $offset, 'order' => $orderBy));

            //$count =$model->count();

            $recordsTotal = $count;

            $recordsFiltered = $count;

            $data['draw'] = $draw;

            $data['recordsTotal'] = $recordsTotal;

            $data['recordsFiltered'] = $recordsFiltered;

            $rowData = array();

            $m = 1;

            foreach ($model as $row) {

                $div = '';

                if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["orders_view"])) {

                    $div .= ' <button class="btn btn-primary btn-xs" title="View Order" onclick="vieworder(' . $row['id'] . ');"><i class="fa fa-eye"></i></button>';
                }

                if ($row['is_locked'] == 'N') {

                    $model_his = SupplyConfirmation::model()->find(array('condition' => 'month = "' . $row['month'] . '" and year = "' . $row['year'] . '"'));

                    if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["orders_edit"])) {

                        if (empty($model_his)) {

                            $div .= ' <button class="btn btn-primary btn-xs" title="Edit Order" onclick="loadPopupModal(\'order/edit&id=' . $row['id'] . '\',\'Edit Order\',\'xl\')"><i class="fa fa-pencil"></i></button> ';
                        }
                    }

                    if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["orders_delete"])) {

                        if (empty($model_his)) {

                            $div .= ' <button class="btn btn-primary btn-xs" title="Delte Order" onclick="deleteOrder(' . $row['id'] . ')"><i class="fa fa-trash"></i></button> ';
                        }
                    }
                }

                if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["orders_download"])) {

                    $rowdistributer_id = $row["distributer_id"];
                    $rowyear = $row["year"];
                    $rowmonth = $row["month"];

                    $div .= '<button type="button"  class="btn btn-xs btn-primary" title="Download Quantity" onclick="downloadUploadorderQty(' . $row['id'] . ');" title="Uploaded"><i class="fa fa-download"></i></button>';

                    $div .= '<button type="button"  class="btn btn-xs btn-primary" title="Download Excel" onclick="window.open($(\'#liveSite\').val()+\'index.php?r=order/DownloadOrderFormatExcel&type=2&distributer_id=' . $rowdistributer_id . '&year=' . $rowyear . '&month=' . $rowmonth . '\', \'_blank\');" title="Uploaded"><i class="fa fa-file-excel-o"></i></button>';

                    //$div .=   '<button type="button"  class="btn btn-xs btn-primary" title="Download Amount" onclick="downloadUploadorderAmt('.$row['id'].');" title="Uploaded"><i class="fa fa-download"></i></button>';
                    //}
                }
                
                $div .= '<button type="button" class="btn btn-xs btn-primary" title="Download Master" onclick="window.open($(\'#liveSite\').val() + \'index.php?r=CreditholdController/DownloadCreditHoldDetails&id=' . $row['id'] . '\', \'_blank\');">
            <i class="fa fa-download"></i>
         </button>';



                $rowData[] = array($m,
                    date('d/m/Y', strtotime($row['order_date'])), $row['distributer_det']['distributer_name'], $arr_months[(int) $row['month']], $row['year'],
                    '<div class="btn-wrap text-center">' . $div . '</div>');

                $m++;
            }

            $data['data'] = $rowData;

            echo json_encode($data);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionUploadOrder() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $model = new Order();

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false, 'jquery-2.1.4.js' => false);

            $this->render('upload_form', array('model' => $model), false, false);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDownloadOrderFormat() {



        ini_set("memory_limit", "-1");

        ini_set('max_execution_time', "-1");

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $distributer_id = Yii::app()->request->getParam('distributer_id');
            
            $year = Yii::app()->request->getParam('year');
            $selyear = Yii::app()->request->getParam('year');
            $month = Yii::app()->request->getParam('month');

            $price_date = $year . '-' . $month . '-' . '01';

            $cnd = '(is_active=:active OR (is_active=:discontinue AND ((discontinuing_month>=:month AND discontinuing_year=:year) OR discontinuing_year>:year))) AND show_in_order="Y" ';
            $current_year = date("Y");
            if($year>$current_year && $month>3){ //2027 1
                $cnd .=  ' AND category_type_id IN (1,2)';
                $order_by = ' product_category_id,gmc_sort_order ASC';
            }else{ 
                $cnd .=  ' AND future_category_type_id IN (1,2)';
                $order_by = ' product_category_id,future_gmc_sort_order ASC';
            }
            if ((int) $distributer_id > 0) {
                $modHiddenDistributerGmcs = new HiddenDistributerGmcs();
                $arr_gmcs = $modHiddenDistributerGmcs->getHiddenGMCs($distributer_id);
                if (!empty($arr_gmcs)) {
                    $cnd .= ' AND id NOT IN (' . implode(',', $arr_gmcs) . ')';
                }
            }
            $params = [
                ':active' => 'Y',
                ':discontinue' => 'D',
                ':month' => (int) $month,
                ':year' => (int) $year
            ];

            $model_inventory = Inventory::model()->findAll([
                'condition' => $cnd,
                'order' => $order_by,
                'params' => $params
            ]);

            $file = fopen("upload/test.csv", "w");

            $j = 0;

            $mod_dis = Distributers::model()->with('region_det')->findByPk((int) $distributer_id);

            $region_id = (int) $mod_dis->region_id;

            $arr_months = Order::getmonths();
            
            $list[$j++] = 'ORDER REVIEW FORM QTY';
            $modelComments = new MasComments();
            $modelComment1 = $modelComments->findBYPK(1);
            $modelComment2 = $modelComments->findBYPK(2);
            $modelComment3 = $modelComments->findBYPK(3);
            $modelComment4 = $modelComments->findBYPK(4);
            $modelComment5 = $modelComments->findBYPK(5);
            $comment1 = $comment2 = $comment3 = $comment4 = $comment5 = '';
            if (!empty($modelComment1)) {
                $comment1 = $modelComment1->comments;
            }
            if (!empty($modelComment2)) {
                $comment2 = $modelComment2->comments;
            }
            if (!empty($modelComment3)) {
                $comment3 = $modelComment3->comments;
            }
            if (!empty($modelComment4)) {
                $comment4 = $modelComment4->comments;
            }
            if (!empty($modelComment5)) {
                $comment5 = $modelComment5->comments;
            }
            $list[$j++] = '!!!!!'.$comment1;
            $list[$j++] = '!!!!!'.$comment2;
            $list[$j++] = '!!!!!'.$comment3;
            $list[$j++] = '!!!!!'.$comment4;
            $list[$j++] = '!!!!!'.$comment5;
            $jan = '';
            $feb = '';
            $mar = '';
            $apr = '';
            $may = '';
            $jun = '';
            $jul = '';
            $aug = '';
            $sep = '';
            $oct = '';
            $nov = '';
            $dec = '';

            if ($month <= 3) {

                $year = $year - 1;
            }

            if ((int) $month > 0) {

                switch ((int) $month) {

                    case 1:

                        $jan = 'Input your Order in this month=>';

                        break;

                    case 2:

                        $feb = 'Input your Order in this month=>';

                        break;

                    case 3:

                        $mar = 'Input your Order in this month =>';

                        break;

                    case 4:

                        $apr = 'Input your Order in this month=>';

                        break;

                    case 5:

                        $may = 'Input your Order in this month=>';

                        break;

                    case 6:

                        $jun = 'Input your Order in this month=>';

                        break;

                    case 7:

                        $jul = 'Input your Order in this month=>';

                        break;

                    case 8:

                        $aug = 'Input your Order in this month=>';

                        break;

                    case 9:

                        $sep = 'Input your Order in this month=>';

                        break;

                    case 10:

                        $oct = 'Input your Order in this month=>';

                        break;

                    case 11:

                        $nov = 'Input your Order in this month=>';

                        break;

                    case 12:

                        $dec = 'Input your Order in this month=>';

                        break;
                }
            }

             $list[$j++] = 'Distributor!!' . $mod_dis->distributer_name . '!!!!!!' . $apr . '!!!' . $may . '!!!' . $jun . '!!!' . '!!!' . $jul . '!!!' . $aug . '!!!' . $sep . '!!!' . '!!!' . $oct . '!!!' . $nov . '!!!' . $dec . '!!!' . '!!!' . $jan . '!!!' . $feb . '!!!' . $mar;

            //$list[$j++] = 'Distributor!!' . $mod_dis->distributer_name . '!!!!!!' . $apr . '!!!' . $may . '!!!' . $jun . '!!!' . '!!!' . $jul . '!!!' . $aug . '!!!' . $sep . '!!!' . '!!!' . $oct . '!!!' . $nov . '!!!' . $dec . '!!!' . '!!!' . $jan . '!!!' . $feb . '!!!' . $mar;

            $year1 = (int) $year + 1;

            $list[$j++] = 'Sr.#!New/Discontinued!PRODUCT CATEGORY!GMC!Model!YMGF PRICE!Supply Confirmation Month!CBM!BASE CODE!01-04-' . $year . '!!!01-05-' . $year . '!!!01-06-' . $year . '!!!1Q TOTAL!!!01-07-' . $year . '!!!01-08-' . $year . '!!!01-09-' . $year . '!!!2Q TOTAL!!!01-10-' . $year . '!!!01-11-' . $year . '!!!01-12-' . $year . '!!!3Q TOTAL!!!01-01-' . $year1 . '!!!01-02-' . $year1 . '!!!01-03-' . $year1 . '!!!4Q TOTAL!!!TOTAL' . $year . '-' . $year1;

            $list[$j++] = '!!!!!!!!!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP';

            foreach ($model_inventory as $i => $row) {

                $apr_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="4" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $may_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="5" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jun_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="6" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jul_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="7" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $aug_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="8" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $sep_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="9" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $oct_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="10" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $nov_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="11" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $dec_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="12" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jan_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="1" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $feb_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="2" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $mar_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped,is_done', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="3" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                //ordered//

                $qty_apr = (int) $apr_model->quantity_ordered;

                $qty_may = (int) $may_model->quantity_ordered;

                $qty_jun = (int) $jun_model->quantity_ordered;

                $qty_jul = (int) $jul_model->quantity_ordered;

                $qty_aug = (int) $aug_model->quantity_ordered;

                $qty_sep = (int) $sep_model->quantity_ordered;

                $qty_oct = (int) $oct_model->quantity_ordered;

                $qty_nov = (int) $nov_model->quantity_ordered;

                $qty_dec = (int) $dec_model->quantity_ordered;

                $qty_jan = (int) $jan_model->quantity_ordered;

                $qty_feb = (int) $feb_model->quantity_ordered;

                $qty_mar = (int) $mar_model->quantity_ordered;

                $qty_q1 = $qty_apr + $qty_may + $qty_jun;

                $qty_q2 = $qty_jul + $qty_aug + $qty_sep;

                $qty_q3 = $qty_oct + $qty_nov + $qty_dec;

                $qty_q4 = $qty_jan + $qty_feb + $qty_mar;

                $qty_tot = $qty_q1 + $qty_q2 + $qty_q3 + $qty_q4;

                //supplyconfirmation//



                $supplyqty_apr = (int) $apr_model->quantity_confirmed;

                $dsupplyqty_apr = '';

                if ($qty_apr > 0 && $apr_model->is_done == "Y") {

                    $dsupplyqty_apr = $supplyqty_apr;
                }

                $supplyqty_may = (int) $may_model->quantity_confirmed;

                $dsupplyqty_may = '';

                if ($qty_may > 0 && $may_model->is_done == "Y") {

                    $dsupplyqty_may = $supplyqty_may;
                }

                $supplyqty_jun = (int) $jun_model->quantity_confirmed;

                $dsupplyqty_jun = '';

                if ($qty_jun > 0 && $jun_model->is_done == "Y") {

                    $dsupplyqty_jun = $supplyqty_jun;
                }

                $supplyqty_jul = (int) $jul_model->quantity_confirmed;

                $dsupplyqty_jul = '';

                if ($qty_jul > 0 && $jul_model->is_done == "Y") {

                    $dsupplyqty_jul = $supplyqty_jul;
                }

                $supplyqty_aug = (int) $aug_model->quantity_confirmed;

                $dsupplyqty_aug = '';

                if ($qty_aug > 0 && $aug_model->is_done == "Y") {

                    $dsupplyqty_aug = $supplyqty_aug;
                }

                $supplyqty_sep = (int) $sep_model->quantity_confirmed;

                $dsupplyqty_sep = '';

                if ($qty_sep > 0 && $sep_model->is_done == "Y") {

                    $dsupplyqty_sep = $supplyqty_sep;
                }

                $supplyqty_oct = (int) $oct_model->quantity_confirmed;

                $dsupplyqty_oct = '';

                if ($qty_oct > 0 && $oct_model->is_done == "Y") {

                    $dsupplyqty_oct = $supplyqty_oct;
                }

                $supplyqty_nov = (int) $nov_model->quantity_confirmed;

                $dsupplyqty_nov = '';

                if ($qty_nov > 0 && $nov_model->is_done == "Y") {

                    $dsupplyqty_nov = $supplyqty_nov;
                }

                $supplyqty_dec = (int) $dec_model->quantity_confirmed;

                $dsupplyqty_dec = '';

                if ($qty_dec > 0 && $dec_model->is_done == "Y") {

                    $dsupplyqty_dec = $supplyqty_dec;
                }

                $supplyqty_jan = (int) $jan_model->quantity_confirmed;

                $dsupplyqty_jan = '';

                if ($qty_jan > 0 && $jan_model->is_done == "Y") {

                    $dsupplyqty_jan = $supplyqty_jan;
                }

                $supplyqty_feb = (int) $feb_model->quantity_confirmed;

                $dsupplyqty_feb = '';

                if ($qty_feb > 0 && $feb_model->is_done == "Y") {

                    $dsupplyqty_feb = $supplyqty_feb;
                }

                $supplyqty_mar = (int) $mar_model->quantity_confirmed;

                $dsupplyqty_mar = '';

                if ($qty_mar > 0 && $mar_model->is_done == "Y") {

                    $dsupplyqty_mar = $supplyqty_mar;
                }

                $supplyqty_q1 = $supplyqty_apr + $supplyqty_may + $supplyqty_jun;

                $supplyqty_q2 = $supplyqty_jul + $supplyqty_aug + $supplyqty_sep;

                $supplyqty_q3 = $supplyqty_oct + $supplyqty_nov + $supplyqty_dec;

                $supplyqty_q4 = $supplyqty_jan + $supplyqty_feb + $supplyqty_mar;

                $supplyqty_tot = $supplyqty_q1 + $supplyqty_q2 + $supplyqty_q3 + $supplyqty_q4;

                //supplyconfirmation//
                //supplycbm//

                $supplycbmqty_apr = $supplyqty_apr * $row['cbm'];

                $supplycbmqty_may = $supplyqty_may * $row['cbm'];

                $supplycbmqty_jun = $supplyqty_jun * $row['cbm'];

                $supplycbmqty_jul = $supplyqty_jul * $row['cbm'];

                $supplycbmqty_aug = $supplyqty_aug * $row['cbm'];

                $supplycbmqty_sep = $supplyqty_sep * $row['cbm'];

                $supplycbmqty_oct = $supplyqty_oct * $row['cbm'];

                $supplycbmqty_nov = $supplyqty_nov * $row['cbm'];

                $supplycbmqty_dec = $supplyqty_dec * $row['cbm'];

                $supplycbmqty_jan = $supplyqty_jan * $row['cbm'];

                $supplycbmqty_feb = $supplyqty_feb * $row['cbm'];

                $supplycbmqty_mar = $supplyqty_mar * $row['cbm'];

                $supplycbmqty_q1 = $supplycbmqty_apr + $supplycbmqty_may + $supplycbmqty_jun;

                $supplycbmqty_q2 = $supplycbmqty_jul + $supplycbmqty_aug + $supplycbmqty_sep;

                $supplycbmqty_q3 = $supplycbmqty_oct + $supplycbmqty_nov + $supplycbmqty_dec;

                $supplycbmqty_q4 = $supplycbmqty_jan + $supplycbmqty_feb + $supplycbmqty_mar;

                $supplycbmqty_tot = $supplycbmqty_q1 + $supplycbmqty_q2 + $supplycbmqty_q3 + $supplycbmqty_q4;

                //supplycbm//
                //shipped cbm//

                $shp_apr = (int) $apr_model->quantity_shipped;

                $shp_may = (int) $may_model->quantity_shipped;

                $shp_jun = (int) $jun_model->quantity_shipped;

                $shp_jul = (int) $jul_model->quantity_shipped;

                $shp_aug = (int) $aug_model->quantity_shipped;

                $shp_sep = (int) $sep_model->quantity_shipped;

                $shp_oct = (int) $oct_model->quantity_shipped;

                $shp_nov = (int) $nov_model->quantity_shipped;

                $shp_dec = (int) $dec_model->quantity_shipped;

                $shp_jan = (int) $jan_model->quantity_shipped;

                $shp_feb = (int) $feb_model->quantity_shipped;

                $shp_mar = (int) $mar_model->quantity_shipped;

                $shp_q1 = $shp_apr + $shp_may + $shp_jun;

                $shp_q2 = $shp_jul + $shp_aug + $shp_sep;

                $shp_q3 = $shp_oct + $shp_nov + $shp_dec;

                $shp_q4 = $shp_jan + $shp_feb + $shp_mar;

                $shp_tot = $shp_q1 + $shp_q2 + $shp_q3 + $shp_q4;

                //shipped cbm//
                //region value//

                $price_cnd = 'product_id=' . (int) $row['id'] . ' AND region_id=' . (int) $region_id . ' AND from_date <="' . $price_date . '" AND to_date >="' . $price_date . '"';

                $mod_inv_price = InventoryPrice::model()->find(array('condition' => $price_cnd));

                //region value//




                $new_discontinue=Inventory::model()->getinvtype((int)$row['id'],$month,$selyear);
                $pair_name =($row['gmc_type']=='P')?'PAIR':(($row['gmc_type']=='K')?'KIT':'');    

                $goodend_month = $arr_months[(int) $row['goods_in_end_month']] . ' - ' . (int) $row->goods_in_end_year;
                
                $goods_in_date = (int) $row->goods_in_end_year . '-' . str_pad((int) $row['goods_in_end_month'], 2, '0', STR_PAD_LEFT) . '-01';
                
                // Convert $goods_in_date to a DateTime object
                $goods_in_datetime = new DateTime($goods_in_date);
                
                // Subtract 1 month from $goods_in_datetime
                $supply_confirm_datetime = $goods_in_datetime->sub(new DateInterval('P1M'));
                
                // Format $supply_confirm_month
                $supply_confirm_month = $supply_confirm_datetime->format('F - Y');
                
              
                $list[$j++] = $new_discontinue.'!'.$pair_name.'!' . $row['category_det']['product_category'] . '!' . $row['gmc_code'] . '!' . $row['model_no']
                        . '!' . (float) $mod_inv_price['price'] .'!' .$supply_confirm_month. '!' . $row['cbm'] . '!' . $row['base_code']
                        . '!' . $qty_apr . '!' . $dsupplyqty_apr . '!' . $shp_apr .  '!' . $qty_may . '!' . $dsupplyqty_may . '!' . $shp_may . '!' . $qty_jun . '!' . $dsupplyqty_jun . '!' . $shp_jun . '!' . $qty_q1 . '!' . $supplyqty_q1 . '!' . $shp_q1 . '!' . 
                        $qty_jul . '!' . $dsupplyqty_jul . '!' . $shp_jul . '!' . 
                        $qty_aug . '!' . $dsupplyqty_aug . '!' . $shp_aug . '!' . 
                        $qty_sep . '!' . $dsupplyqty_sep . '!' . $shp_sep .  '!' . 
                        $qty_q2 . '!' . $supplyqty_q2 . '!' . $shp_q2 . '!' . 
                        $qty_oct . '!' . $dsupplyqty_oct . '!' . $shp_oct .'!' . 
                        $qty_nov . '!' . $dsupplyqty_nov . '!' . $shp_nov .  '!' . 
                        $qty_dec . '!' . $dsupplyqty_dec . '!' . $shp_dec .  '!' . 
                        $qty_q3 . '!' . $supplyqty_q3 . '!' . $shp_q3 . '!' . 
                        $qty_jan . '!' . $dsupplyqty_jan . '!' . $shp_jan .  '!' . 
                        $qty_feb . '!' . $dsupplyqty_feb . '!' . $shp_feb .  '!' . 
                        $qty_mar . '!' . $dsupplyqty_mar . '!' . $shp_mar . '!' . 
                        $qty_q4 . '!' . $supplyqty_q4 . '!' . $shp_q4 . '!' . 
                        $qty_tot . '!' . $supplyqty_tot . '!' . $shp_tot ;
            }

            $j++;

            foreach ($list as $line) {

                fputcsv($file, explode('!', $line));
            }

            fclose($file);

            $filename = 'upload/test.csv';

            $path = Yii::app()->getBaseUrl(true) . '/upload';

            header('Content-Type: application/csv');

            $file_name = 'Order_' . $mod_dis->distributer_name . '_' . $year . '.csv';

            header('Content-disposition: attachment; filename="' . $file_name . '"');

            readfile($filename);

            Yii::app()->end();
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    /* public function actionDownloadOrderFormatExcelold(){

      $_GET  = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

      if(count($_GET) > 0){

      $distributer_id = Yii::app()->request->getParam('distributer_id');

      $year = Yii::app()->request->getParam('year');

      $month = Yii::app()->request->getParam('month');

      $mod_dis = Distributers::model()->findByPk((int)$distributer_id);

      $file_name='Order_'.$mod_dis->distributer_name.'_'.$year.'.xls';

      header("Content-type: application/vnd.ms-excel");

      header("Content-Disposition: attachment; filename=\"" . $file_name . "\"");

      $this->renderpartial('application.views.front.order.order_download_format_excel', array('distributer_id' => $distributer_id,

      'year' => $year,'month'=>$month), false, true);

      Yii::app()->end();

      return;

      }else{

      throw new CHttpException(404, 'Page not found.');

      }

      } */

    public function actionSaveUpload() {



        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $model = new Order();
            $other = array();

            if (isset($_POST['Order'])) {

                if ((int) @$_POST['Order']['id'] > 0) {

                    $id = (int) @$_POST['Order']['id'];

                    $model = $model->findByPk($id);
                } else {

                    $model->created_by = Yii::app()->user->id;

                    $model->created_date_time = date('Y-m-d H:i:s');
                }

                $model->updated_by = Yii::app()->user->id;

                $model->updated_date_time = date('Y-m-d H:i:s');

                $model->attributes = $_POST['Order'];

                $model->order_date = date('Y-m-d');

                $file = $_POST['Order']['file_name'];

                $model->file_name = $file;

                $valid = $model->validate();

                $i = 0;
                $arr_products = [];$arr_kit = [];

                if ($file != '') {

                    if (($handle = fopen("upload/orders/" . $file, "r")) !== FALSE) {

                        $n = 1;

                        $arr_months = Order::getmonths();

                        $mod_his = Order::model()->find(array('condition' => 'distributer_id=' . (int) $_POST['Order']['distributer_id'] . ' and month = "' . $_POST['Order']['month'] . '" and year = "' . $_POST['Order']['year'] . '"'));

                        if (!empty($mod_his)) {

                            $other[] = 'Already added Data';

                            $valid = false;
                        }

                        while (($row = fgetcsv($handle)) !== FALSE) {

                            if ($n == 7) {

                                $supplier_name = $row[2];

                                $distributer_model = Distributers::model()->with('region_det')->find(array('condition' => 'distributer_name="' . $supplier_name . '"'));

                                if ($distributer_model->id != $_POST['Order']['distributer_id']) {

                                    $other[] = 'Distributor Not Matching with Selected One';

                                    $valid = false;
                                }

                                $region_id = (int) $distributer_model->region_id;
                            }

                            if ($n > 9) {

                                $product_category = trim($row[2], '\'"'); //productcategory

                                $category_id = (int) MasProductCategory::model()->find(array('condition' => 'product_category="' . $product_category . '"'))->id;

                                if ($category_id == 0) {

                                    $other[] = ' Category ' . $product_category . ' not added';

                                    $valid = false;
                                }

                                $gmc_code = trim($row[3]); //gmc

                                $model_no = trim($row[4]); //model

                                /*

                                  1-4-21 = 9

                                  1-5-21 = 12

                                  1-6-21 = 15



                                  1Qtotal= 18



                                  1-7-21 = 21

                                  1-8-21 = 24

                                  1-9-21 = 27



                                  2Q total=30



                                  1-10-21 = 33

                                  1-11-21 = 36

                                  1-12-21 = 39



                                  3Q total =42



                                  1-1-22 =45

                                  1-2-22 = 48

                                  1-3-22 = 51

                                  4Q    =  54

                                  Total  = 57

                                 */

                                switch ((int) $_POST['Order']['month']) {

                                    case '1':

                                        $quantity_ordered = (int) $row[45];

                                        break;

                                    case '2':

                                        $quantity_ordered = (int) $row[48];

                                        break;

                                    case '3':

                                        $quantity_ordered = (int) $row[51];

                                        break;

                                    case '4':

                                        $quantity_ordered = (int) $row[9];

                                        break;

                                    case '5':

                                        $quantity_ordered = (int) $row[12];

                                        break;

                                    case '6':

                                        $quantity_ordered = (int) $row[15];

                                        break;

                                    case '7':

                                        $quantity_ordered = (int) $row[21];

                                        break;

                                    case '8':

                                        $quantity_ordered = (int) $row[24];

                                        break;

                                    case '9':

                                        $quantity_ordered = (int) $row[27];

                                        break;

                                    case '10':

                                        $quantity_ordered = (int) $row[33];

                                        break;

                                    case '11':

                                        $quantity_ordered = (int) $row[36];

                                        break;

                                    case '12':

                                        $quantity_ordered = (int) $row[39];

                                        break;
                                }

                                if ($quantity_ordered < 0) {

                                    $other[] = 'GMC with code ' . $gmc_code . ' and model ' . $model_no . ' entered negative qty ' . $quantity_ordered;

                                    $valid = false;
                                } else {

                                    //AND (is_active="Y" OR (is_active="D" AND ((discontinuing_month<='.$_POST['Order']['month'].' AND discontinuing_year='.$_POST['Order']['year'].') OR discontinuing_year<'.$_POST['Order']['year'].') 

                                    $cnd = 'gmc_code="' . $gmc_code . '" AND (is_active="Y" OR (is_active="D" AND ((discontinuing_month>=' . (int) $_POST['Order']['month'] . ' AND discontinuing_year=' . (int) $_POST['Order']['year'] . ') OR discontinuing_year>' . (int) $_POST['Order']['year'] . ')))';

                                    $mod_inv = Inventory::model()->find(array('condition' => $cnd));

                                    $price_ordered = 0;

                                    if (!empty($mod_inv)) {

                                        /* region value */

                                        $price_date = (int) $_POST['Order']['year'] . '-' . (int) $_POST['Order']['month'] . '-' . '01';

                                        $price_cnd = 'product_id=' . (int) $mod_inv->id . ' AND region_id=' . (int) $region_id . ' AND from_date <="' . $price_date . '" AND to_date >="' . $price_date . '"';

                                        $mod_inv_price = InventoryPrice::model()->find(array('condition' => $price_cnd));

                                        $price_ordered = (float) $mod_inv_price->price;

                                        /* region value */

                                        if($mod_inv->gmc_type=='P'){
                                            $quantity_ordered=($quantity_ordered%2==0)?$quantity_ordered:$quantity_ordered+1;
                                        }
                                        if($mod_inv->gmc_type=='K'){
                                            $arr_kit[]=$mod_inv->id;
                                        }
                                        $modelItems[$i] = new OrderDetails();

                                        $modelItems[$i]->order_id = 0;

                                        $modelItems[$i]->category_id = (int) $category_id;

                                        $modelItems[$i]->product_id = $mod_inv->id;

                                        $modelItems[$i]->model = $model_no;

                                        $modelItems[$i]->base_code = $row[8];

                                        $modelItems[$i]->quantity_ordered = $quantity_ordered;

                                        $modelItems[$i]->price = $price_ordered;

                                        $modelItems[$i]->region_id = (int) $region_id;

                                        $modelItems[$i]->order_date = $price_date;

                                        if ($distributer_model->region_det->currency_type == 'USD') {

                                            $modelItems[$i]->exchange_rate = 1;
                                        } else {

                                            $mod_settings = MasSettings::model()->find();

                                            $currency_rate = $mod_settings->currency_rate;

                                            $modelItems[$i]->exchange_rate = (1 / $currency_rate);
                                        }

                                        $modelItems[$i]->currency_type = $distributer_model->region_det->currency_type;

                                        $modelItems[$i]->created_by = Yii::app()->user->id;

                                        $modelItems[$i]->created_date_time = date('Y-m-d H:i:s');

                                        $modelItems[$i]->updated_by = Yii::app()->user->id;

                                        $modelItems[$i]->updated_date_time = date('Y-m-d H:i:s');

                                        $valid = $modelItems[$i]->validate() && $valid;

                                        $i++;

                                        if (in_array($mod_inv->id, $arr_products)) {

                                            $other[] = $gmc_code . ' GMC Repeated ';

                                            $valid = false;
                                        } else {

                                            $arr_products[] = $mod_inv->id;
                                        }
                                    } else {

                                        $cnd = 'gmc_code="' . $gmc_code . '" AND is_active="D"';

                                        $mod_inv = Inventory::model()->find(array('condition' => $cnd));

                                        if (!empty($mod_inv)) {

                                            $other[] = 'GMC with code ' . $gmc_code . ' and model ' . $model_no . ' Discontinued';
                                        } else {

                                            $other[] = 'GMC with code ' . $gmc_code . ' and model ' . $model_no . ' not exits';
                                        }

                                        $valid = false;
                                    }
                                }
                            }

                            $n++;
                        }

                        fclose($handle);
                    }
                } else {

                    $other[] = 'File Not Uploaded';

                    $valid = false;
                }

                if ($valid) {

                    $model->save();

                    if (!empty($modelItems)) {

                        foreach ($modelItems as $each) {

                            $each->order_id = $model->id;

                            $each->save(false);
                        }
                        if(!empty($arr_kit)){
                            
                        }
                    }

                    $jsonData['status'] = 'success';

                    $jsonData['frm'] = $_POST['Order'];

                    $jsonData['id'] = $model->id;

                    $jsonData['updated_flag'] = OrderDetails::updateShippingConfirmation((int) $_POST['Order']['month'], (int) $_POST['Order']['year']);

                    echo CJSON::encode($jsonData);
                } else {

                    $error1 = json_decode(CActiveForm::validate($model, '', false), true);

                    if (!empty($modelItems)) {

                        $error2 = json_decode(CActiveForm::validateTabular($modelItems, '', false), true);
                    } else {

                        $error2[] = "Please add atleast one line item";
                    }

                    $error = CJSON::encode(array_merge_recursive($error1, $error2, $other));

                    if ($error != '[]')
                        echo $error;
                }
            }
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionView() {

        if (count($_GET) > 0) {

            $model = new Order();

            $id = (int) Yii::app()->request->getParam('id');

            if ($id > 0) {

                $model = $model->findByPk($id);
            }

            $this->render('view_form', array('model' => $model));
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionEdit() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $model = new Order();

            $modelItems = array(new OrderDetails());

            $id = (int) Yii::app()->request->getParam('id');

            if ($id > 0) {

                $model = $model->findByPk($id);

                $modelItems = OrderDetails::model()->with('category_det', 'product_det')->findAll(array('condition' => 'order_id=' . $model->id));
            }

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false, 'jquery-2.1.4.js' => false);

            $this->render('edit_form', array('model' => $model, 'modelItems' => $modelItems), false, false);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionSaveEdittedOrder() {



        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $model = new Order();
            $other = array();

            if (isset($_POST['Order'])) {

                if ((int) @$_POST['Order']['id'] > 0) {

                    $id = (int) @$_POST['Order']['id'];

                    $model = $model->findByPk($id);
                } else {

                    $model->created_by = Yii::app()->user->id;

                    $model->created_date_time = date('Y-m-d H:i:s');
                }

                $model->updated_by = Yii::app()->user->id;

                $model->updated_date_time = date('Y-m-d H:i:s');

                $model->attributes = $_POST['Order'];

                $valid = $model->validate();

                $p = 0;

                if (is_array($_POST['OrderDetails'])) {

                    foreach ($_POST['OrderDetails'] as $i => $item) {

                        $modelItems[$p] = new OrderDetails;

                        if ((int) $item['id'] > 0) {

                            $modelItems[$p] = OrderDetails::model()->findByPk((int) $item['id']);
                        } else {

                            $modelItems[$p]->created_by = Yii::app()->user->id;

                            $modelItems[$p]->created_date_time = date("Y-m-d H:i:s");
                        }

                        $modelItems[$p]->attributes = $item;

                        $modelItems[$p]->order_id = 0;

                        $modelItems[$p]->updated_by = Yii::app()->user->id;

                        $modelItems[$p]->updated_date_time = date("Y-m-d H:i:s");

                        $valid = $modelItems[$p]->validate() && $valid;

                        $p++;
                    }
                }









                if ($valid) {



                    $model->save();

                    if (!empty($modelItems)) {

                        foreach ($modelItems as $eachModel) {

                            $eachModel->order_id = $model->id;

                            $eachModel->save(false);
                        }
                    }





                    $jsonData['status'] = 'success';

                    $jsonData['id'] = $model->id;

                    $jsonData['updated_flag'] = OrderDetails::updateShippingConfirmation((int) $model->month, (int) $model->year);

                    echo CJSON::encode($jsonData);
                } else {

                    $error1 = $error2 = array();

                    $error1 = json_decode(CActiveForm::validate($model, '', false), true);

                    if (!empty($modelItems))
                        $error2 = json_decode(CActiveForm::validateTabular($modelItems, '', false), true);
                    else
                        $error2[] = "Please add atleast one data";

                    $error = CJSON::encode(array_merge_recursive($error1, $error2, $other));

                    if ($error != '[]')
                        echo $error;
                }
            }
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionGetviewList() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $order_id = (int) $_POST['order_id'];

            $model = new OrderDetails();

            $draw = $_POST['draw'];

            $offset = $_POST['start'];

            $limit = $_POST['length'];

            $columns = array(
                // datatable column index  => database column name

                0 => 't.id',
                1 => 'category_det.product_category',
                2 => 'product_det.gmc_code',
                3 => 't.model',
                4 => 't.base_code',
                5 => 't.quantity_ordered'
            );

            $filterColumn = $columns[$_POST['order'][0]['column']];

            $filterOrder = $_POST['order'][0]['dir'];

            $orderBy = $filterColumn . " " . $filterOrder;

            $condition = 'order_id=' . $order_id;

            if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {

                $searchString = $_POST['search']['value'];

                $condition .= " and (category_det.product_category LIKE '%" . $searchString . "%' 

                         OR product_det.gmc_code LIKE '%" . $searchString . "%' OR t.model LIKE '%" . $searchString . "%' 

                         OR t.base_code LIKE '%" . $searchString . "%' OR t.quantity_ordered LIKE '%" . $searchString . "%')";
            }

            $count = $model->with('category_det', 'product_det')->count(array('condition' => $condition));

            $model = $model->with('category_det', 'product_det')->findAll(array('condition' => $condition, 'limit' => $limit, 'offset' => $offset, 'order' => $orderBy));

            $recordsTotal = $count;

            $recordsFiltered = $count;

            $data['draw'] = $draw;

            $data['recordsTotal'] = $recordsTotal;

            $data['recordsFiltered'] = $recordsFiltered;

            $rowData = array();

            $m = 1;

            foreach ($model as $row) {

                $rowData[] = array($m,
                    $row['category_det']['product_category'], $row['product_det']['gmc_code'], $row['model'], $row->product_det->base_code, $row['quantity_ordered']);

                $m++;
            }

            $data['data'] = $rowData;

            echo json_encode($data);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDelete() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $id = $_POST['id'];

            if ($id > 0) {

                $model = new Order();

                $model = $model->findByPk((int) $id);

                $month = (int) $model->month;

                $year = (int) $model->year;

                $model->deleteByPk((int) $id);

                $modelItems = new OrderDetails();

                $modelItems->deleteAll('order_id=:refer_id', array(':refer_id' => $id));

                $flag = OrderDetails::updateShippingConfirmation((int) $month, (int) $year);

                echo "success";
            } else {

                echo "fail";
            }
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionConsolidatedOrder() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $this->render('consolidated_orders');
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionGetConsolidatedOrderList() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $model = new ConsolidatedOrder();

            $draw = $_POST['draw'];

            $offset = $_POST['start'];

            $limit = $_POST['length'];

            $year = $_POST['year'];

            $month = $_POST['month'];

            $columns = array(
                // datatable column index  => database column name

                0 => 't.id',
                1 => 'added_date',
                2 => 'month',
                3 => 'year',
            );

            $filterColumn = $columns[$_POST['order'][0]['column']];

            $filterOrder = $_POST['order'][0]['dir'];

            $orderBy = $filterColumn . " " . $filterOrder;

            $condition = '1=1';

            if ($month != '') {

                $condition .= ' AND month=' . $month;
            }

            if ($year > 0) {

                $condition .= ' AND year=' . $year;
            }

            $arr_months = Order::getmonths();

            if (isset($_POST['search']['value']) && $_POST['search']['value'] != "") {

                $searchString = $_POST['search']['value'];

                $condition .= " and (added_date LIKE '%" . $searchString . "%' 

                         OR month LIKE '%" . $searchString . "%' OR year LIKE '%" . $searchString . "%')";
            }

            $count = $model->count(array('condition' => $condition));

            $model = $model->findAll(array('condition' => $condition, 'limit' => $limit, 'offset' => $offset, 'order' => $orderBy));

            $recordsTotal = $count;

            $recordsFiltered = $count;

            $data['draw'] = $draw;

            $data['recordsTotal'] = $recordsTotal;

            $data['recordsFiltered'] = $recordsFiltered;
            $data['order']=$orderBy;
            $rowData = array();

            $m = 1;

            foreach ($model as $row) {

                $div = '';

                if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["consolidatedorders_download"])) {

                    //$div .=   '<button type="button"  class="btn btn-sm btn-theme" onclick="downloadconsolidatedorder('.$row['id'].');" title="ALL"><i class="fa fa-download"></i></button>';

                    $div .= '<button type="button"  class="btn btn-xs btn-primary" onclick="downloadUploadedconsolidatedorder(' . $row["id"] . ');" title="Uploaded"><i class="fa fa-download"></i></button>';
                }

                if (Yii::app()->user->authorised_user == 1 || ((int) Yii::app()->user->havePermissionOn["consolidatedorders_delete"])) {

                    if ($row['is_locked'] != "Y") {

                        $model_his = SupplyConfirmation::model()->find(array('condition' => 'month = "' . $row['month'] . '" and year = "' . $row['year'] . '"'));

                        if (empty($model_his)) {

                            $div .= '&nbsp<button type="button"  class="btn btn-xs btn-primary" onclick="deleteconOrder(' . $row['id'] . ')"><i class="fa fa-trash"></i></button>';
                        }
                    }
                }



                $rowData[] = array($m,
                    date('d/m/Y', strtotime($row['added_date'])), $arr_months[$row['month']], $row['year'],
                    '<div class="btn-wrap text-center">' . $div . '</div>');

                $m++;
            }

            $data['data'] = $rowData;

            echo json_encode($data);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionAddConsolidatedOrder() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $model = new ConsolidatedOrder();

            $id = (int) Yii::app()->request->getParam('id');

            if ($id > 0) {

                $model = $model->findByPk($id);
            }

            Yii::app()->clientScript->scriptMap = array('*jquery.js' => false, '*jquery.min.js' => false, 'jquery.min.js' => false, '*jquery-ui.js' => false, '*jquery-ui.min.js' => false, 'jquery-2.1.4.js' => false);

            $this->render('add_consolidated_order', array('model' => $model), false, false);
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionSaveConsolidatedOrder() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $model = new ConsolidatedOrder();
            $other = array();

            if (isset($_POST['ConsolidatedOrder'])) {

                if ((int) @$_POST['ConsolidatedOrder']['id'] > 0) {

                    $id = (int) @$_POST['ConsolidatedOrder']['id'];

                    $model = $model->findByPk($id);
                } else {

                    $model->created_by = Yii::app()->user->id;

                    $model->created_date_time = date('Y-m-d H:i:s');
                }

                $model->updated_by = Yii::app()->user->id;

                $model->updated_date_time = date('Y-m-d H:i:s');

                $model->attributes = $_POST['ConsolidatedOrder'];

                $model->added_date = date('Y-m-d');

                $valid = $model->validate();

                $mod_his = ConsolidatedOrder::model()->find(array('condition' => 'month = "' . $_POST['ConsolidatedOrder']['month'] . '" and year = "' . $_POST['ConsolidatedOrder']['year'] . '"'));

                if (!empty($mod_his)) {

                    $other[] = 'Already added Data';

                    $valid = false;
                }

                if ($valid) {

                    $model->save();

                    $jsonData['status'] = 'success';

                    $jsonData['frm'] = $_POST['ConsolidatedOrder'];

                    $jsonData['id'] = $model->id;

                    echo CJSON::encode($jsonData);
                } else {

                    $error1 = json_decode(CActiveForm::validate($model, '', false), true);

                    $error = CJSON::encode(array_merge_recursive($error1, $other));

                    if ($error != '[]')
                        echo $error;
                }
            }
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDownloadConsolidatedOrder() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $id = Yii::app()->request->getParam('id');

            $model = new ConsolidatedOrder();

            $model = $model->findByPK((int) $id);

            $month = $model->month;

            $year = $model->year;
            $current_year = date("Y");
            if($year>$current_year && $month>3){ //2027 1
                $cnd =  '  category_type_id IN (1,2)';
                $order_by = ' product_category_id,gmc_sort_order ASC';
            }else{ 
                $cnd =  '  future_category_type_id IN (1,2)';
                $order_by = ' product_category_id,future_gmc_sort_order ASC';
            }
            $model_inventory = Inventory::model()->findAll(['condition' => $cnd,'order' => $order_by]);

            $file = fopen("upload/test.csv", "w");

            $j = 0;

            $mod_dis = Distributers::model()->findAll(array('condition' => 'isActive="Y"', 'order' => 'id asc'));

            $arr_months = Order::getmonths();

            $list[$j++] = 'Year!' . $year;

            $list[$j++] = 'Month!' . $arr_months[$month];

            $thirdrow = 'PRODUCT CATEGORY!GMC!Model!YMGF FOB!CBM!BASE CODE';

            foreach ($mod_dis as $eachdis) {

                $thirdrow .= '!' . $eachdis->distributer_name;
            }

            $thirdrow .= '! Total';

            $list[$j++] = $thirdrow;

            foreach ($model_inventory as $i => $row) {

                $inv_row = $row['category_det']['product_category'] . '!' . $row['gmc_code'] . '!' . $row['model_no']
                        . '!' . $row['ymgf_fob'] . '!' . $row['cbm'] . '!' . $row['base_code'];

                $ind_sum = 0;

                foreach ($mod_dis as $eachdis) {

                    $ordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id . ' AND product_id=' . (int) $row['id']))->quantity_ordered;

                    $ind_sum += (int) $ordersum;

                    $inv_row .= '!' . (int) $ordersum;
                }

                $inv_row .= '!' . $ind_sum;

                $list[$j++] = $inv_row;
            }

            $tot_row = '!!!!!';

            foreach ($mod_dis as $eachdis) {

                $tot_ordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id))->quantity_ordered;

                $totind_sum += (int) $tot_ordersum;

                $tot_row .= '!' . (int) $tot_ordersum;
            }

            $tot_row .= '!' . $totind_sum;

            $list[$j++] = $tot_row;

            $list[$j++] = '';

            $list[$j++] = '';

            $list[$j++] = 'CATEGORY!!!!!!';

            $modelmascategories = MasProductCategory::model()->findAll(array('order' => 'sort_order ASC'));

            $totcat_sum = 0;

            foreach ($modelmascategories as $eachcat) {

                $cinv_row = $eachcat->product_category . '!!!!!';

                $cind_sum = 0;

                foreach ($mod_dis as $eachdis) {

                    $cordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id . ' AND category_id=' . (int) $eachcat['id']))->quantity_ordered;

                    $cind_sum += (int) $cordersum;

                    $cinv_row .= '!' . (int) $cordersum;
                }

                $cinv_row .= '!' . $cind_sum;

                $list[$j++] = $cinv_row;
            }

            $ctot_row = 'Total!!!!!';

            foreach ($mod_dis as $eachdis) {

                $ctot_ordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id))->quantity_ordered;

                $ctotind_sum += (int) $ctot_ordersum;

                $ctot_row .= '!' . (int) $ctot_ordersum;
            }

            $ctot_row .= '!' . $ctotind_sum;

            $list[$j++] = $ctot_row;

            $list[$j++] = '';

            $list[$j++] = 'CATEGORY Amount!!!!!!';

            $modelmascategories = MasProductCategory::model()->findAll(array('order' => 'sort_order ASC'));

            $totcat_sum = 0;

            foreach ($modelmascategories as $eachcat) {

                $cinv_row = $eachcat->product_category . '!!!!!';

                $cind_sum = 0;

                foreach ($mod_dis as $eachdis) {

                    $cordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(price*quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id . ' AND category_id=' . (int) $eachcat['id']))->quantity_ordered;

                    $cind_sum += $cordersum;

                    $cinv_row .= '!' . $cordersum;
                }

                $cinv_row .= '!' . $cind_sum;

                $list[$j++] = $cinv_row;
            }

            $ctot_row = 'Total!!!!!';

            foreach ($mod_dis as $eachdis) {

                $ctot_ordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(price*quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis->id))->quantity_ordered;

                $ctotind_sum += $ctot_ordersum;

                $ctot_row .= '!' . $ctot_ordersum;
            }

            $ctot_row .= '!' . $ctotind_sum;

            $list[$j++] = $ctot_row;

            $j++;

            foreach ($list as $line) {

                fputcsv($file, explode('!', $line));
            }

            fclose($file);

            $filename = 'upload/test.csv';

            $path = Yii::app()->getBaseUrl(true) . '/upload';

            header('Content-Type: application/csv');

            $file_name = 'consolidated_order_' . $month . '_' . $year . '.csv';

            header('Content-disposition: attachment; filename="' . $file_name . '"');

            readfile($filename);

            Yii::app()->end();

            return;
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDownloadUploadedConsolidatedOrder() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $id = Yii::app()->request->getParam('id');

            $model = new ConsolidatedOrder();

            $model = $model->findByPK((int) $id);

            $month = $model->month;

            $year = $model->year;
            $current_year = date("Y");
            if($year>$current_year && $month>3){ //2027 1
                $model_inventory = Inventory::model()->findAll(['condition' => 'category_type_id IN (1,2)','order' => 'product_category_id,gmc_sort_order asc']);
            }else{
                $model_inventory = Inventory::model()->findAll(['condition' => 'future_category_type_id IN (1,2)','order' => 'product_category_id,future_gmc_sort_order asc']);
            }
            //$model_inventory = Inventory::model()->findAll(['order' => 'product_category_id,gmc_sort_order asc']);

            $file = fopen("upload/test.csv", "w");

            $j = 0;

            $sql = "SELECT distributer_id AS id,D.distributer_name FROM ymh_order t LEFT JOIN ymh_distributers D ON D.id=t.distributer_id WHERE month='" . $month . "' AND year='" . $year . "' ORDER BY t.id ASC";

            $mod_dis = Yii::app()->db->createCommand($sql)->queryAll();

            //$cnd = "isActive='Y' AND t.id IN (SELECT distributer_id FROM ymh_order WHERE month='".$month."' AND year='".$year." ORDER BY id ASC')";
            //$mod_dis = Distributers::model()->findAll(array('condition'=>$cnd));

            $arr_months = Order::getmonths();

            $list[$j++] = 'Year!' . $year;

            $list[$j++] = 'Month!' . $arr_months[$month];

            $thirdrow = 'PRODUCT CATEGORY!GMC!Model!YMGF FOB!CBM!BASE CODE';

            foreach ($mod_dis as $eachdis) {

                $thirdrow .= '!' . $eachdis['distributer_name'];
            }

            $thirdrow .= '! Total';

            $list[$j++] = $thirdrow;

            foreach ($model_inventory as $i => $row) {

                $inv_row = $row['category_det']['product_category'] . '!' . $row['gmc_code'] . '!' . $row['model_no']
                        . '!' . $row['ymgf_fob'] . '!' . $row['cbm'] . '!' . $row['base_code'];

                $ind_sum = 0;

                foreach ($mod_dis as $eachdis) {

                    $modordersumdet = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis['id'] . ' AND product_id=' . (int) $row['id'], 'group' => 'parent_det.distributer_id'));
                    $ordersum = !empty($modordersumdet) ? (int)$modordersumdet->quantity_ordered : 0;
                    $ind_sum += (int) $ordersum;

                    $inv_row .= '!' .  Yii::app()->numberFormatter->formatCurrency((int)$ordersum, "");
                }

                $inv_row .= '!' . $ind_sum;

                $list[$j++] = $inv_row;
            }

            $tot_row = '!!!!!';

            $totind_sum = 0;

            foreach ($mod_dis as $eachdis) {



                $tot_ordersum = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis['id'], 'group' => 'parent_det.distributer_id'))->quantity_ordered;

                $totind_sum += (int) $tot_ordersum;

                $tot_row .= '!' .  Yii::app()->numberFormatter->formatCurrency((int)$tot_ordersum, "");
            }

            $tot_row .= '!' . $totind_sum;

            $list[$j++] = $tot_row;

            $list[$j++] = '';

            $list[$j++] = 'CATEGORY!!!!!!';
            $list1 = [];
            $list2 = [];$k = 0;
            $modelmascategories = MasProductCategory::model()->findAll(array('order' => 'sort_order ASC'));

            $totcat_sum = 0;

            foreach ($modelmascategories as $eachcat) {

                $cinv_row = $eachcat->product_category . ' - Qty !!!!!';

                $camount_row = $eachcat->product_category . ' - AMT !!!!!';

                $cind_sum = 0;
                $camtind_sum = 0;

                foreach ($mod_dis as $eachdis) {

                    $cat_det = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(price*quantity_ordered*exchange_rate) AS amount_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis['id'] . ' AND category_id=' . (int) $eachcat['id'], 'group' => 'parent_det.distributer_id'));

                    $cordersum = !empty($cat_det) ? (int)$cat_det->quantity_ordered : 0;

                    $camtordersum = !empty($cat_det) ? $cat_det->amount_ordered : 0;

                    $cind_sum += (int) $cordersum;

                    $camtind_sum += $camtordersum;

                    $cinv_row .= '!' . (int) $cordersum;

                    $camount_row .= '!' . Yii::app()->numberFormatter->formatCurrency($camtordersum, "");
                }

                $cinv_row .= '!' . $cind_sum;

                $camount_row .= '!' . Yii::app()->numberFormatter->formatCurrency($camtind_sum, "");

                $list1[$k++] = $cinv_row;
                $list2[$k++] = $camount_row;
                $j++;
                //$list[$j++] = $cinv_row;

                //$list[$j++] = $camount_row;
            }

            $ctot_row = 'Total - Qty !!!!!';

            $camttot_row = 'Total - AMT !!!!!';
            $ctotind_sum = 0;

            $ctotamtind_sum = 0;
            foreach ($mod_dis as $eachdis) {

                $cat_totdet = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(price*quantity_ordered*exchange_rate) AS amount_ordered', 'condition' => 'parent_det.month=' . $month . ' AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $eachdis['id'], 'group' => 'parent_det.distributer_id'));

                $ctot_ordersum = $cat_totdet->quantity_ordered;

                $ctotamt_ordersum = $cat_totdet->amount_ordered;

                $ctotind_sum += (int) $ctot_ordersum;

                $ctotamtind_sum += $ctotamt_ordersum;

                $ctot_row .= '!' . (int) $ctot_ordersum;

                $camttot_row .= '!' . Yii::app()->numberFormatter->formatCurrency($ctotamt_ordersum, "");
                
            }

            $ctot_row .= '!' . $ctotind_sum;

            $camttot_row .= '!' . Yii::app()->numberFormatter->formatCurrency($ctotamtind_sum, "");
            $list1[$k++] = $ctot_row;
            $list2[$k++] = $camttot_row;
            $j++;
            //$list[$j++] = $ctot_row;

            //$list[$j++] = $camttot_row;

            $list = array_merge($list, $list2);
            $list = array_merge($list, $list1);

            $j++;

            foreach ($list as $line) {

                fputcsv($file, explode('!', $line));
            }

            fclose($file);

            $filename = 'upload/test.csv';

            $path = Yii::app()->getBaseUrl(true) . '/upload';

            header('Content-Type: application/csv');

            $file_name = 'consolidated_order_' . $month . '_' . $year . '.csv';

            header('Content-disposition: attachment; filename="' . $file_name . '"');

            readfile($filename);

            Yii::app()->end();

            return;
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionGetOrderdetails() {

        $_GET = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $model = new ConsolidatedOrder();

            $month = (int) Yii::app()->request->getParam('month');

            $year = (int) Yii::app()->request->getParam('year');

            $count = Order::model()->count(array('condition' => 'month = "' . $month . '"  AND year="' . $year . '"'));

            $msg = '';

            if (($count) == 0) {

                $msg .= '<div style="color:red">Order => Not Done</div>';
            } else {

                $msg .= '<div style="color:green">Order => Done (' . $count . ')</div>';
            }

            echo $msg;
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDeleteconsolOrder() {

        $_POST = filter_input_array(INPUT_POST, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_POST) > 0) {

            $id = $_POST['id'];

            if ($id > 0) {

                $model = new ConsolidatedOrder();

                $model->deleteByPk($id);

                echo "success";
            } else {

                echo "fail";
            }
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    public function actionDownloadUploadedOrderQty() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $order_id = Yii::app()->request->getParam('id');

            $modelOrder = Order::model()->findByPk((int) $order_id);

            $year = $modelOrder->year;

            $month = $modelOrder->month;

            $distributer_id = $modelOrder->distributer_id;

            $price_date = $year . '-' . $month . '-' . '01';
            $cnd = '(is_active="Y" OR (is_active="D" AND ((discontinuing_month>=' . (int) $month . ' AND discontinuing_year=' . (int) $year . ') OR discontinuing_year>' . $year . '))) AND show_in_order="Y"  ';
            if ((int) $distributer_id > 0) {
                $modHiddenDistributerGmcs = new HiddenDistributerGmcs();
                $arr_gmcs = $modHiddenDistributerGmcs->getHiddenGMCs($distributer_id);
                if (!empty($arr_gmcs)) {
                    $cnd .= ' AND id NOT IN (' . implode(',', $arr_gmcs) . ')';
                }
            }
            //echo $cnd;exit;
            $current_year = date("Y");
            if($year>$current_year && $month>3){ //2027 1
                $cnd .=  'AND category_type_id IN (1,2)';
                $order_by = 'product_category_id,gmc_sort_order asc';
            }else{
                $cnd .=  'AND future_category_type_id IN (1,2)';
                $order_by = 'product_category_id,future_gmc_sort_order asc';
            }
            $model_inventory = Inventory::model()->findAll(array('condition' => $cnd, 'order' => $order_by));

            $file = fopen("upload/test.csv", "w");

            $j = 0;

            $mod_dis = Distributers::model()->with('region_det')->findByPk((int) $distributer_id);

            $region_id = (int) $mod_dis->region_id;

            $arr_months = Order::getmonths();

            if ($month <= 3) {

                $year = $year - 1;
            }

            $list[$j++] = 'ORDER REVIEW FORM QTY';
            $modelComments = new MasComments();
            $modelComment1 = $modelComments->findBYPK(1);
            $modelComment2 = $modelComments->findBYPK(2);
            $modelComment3 = $modelComments->findBYPK(3);
            $modelComment4 = $modelComments->findBYPK(4);
            $modelComment5 = $modelComments->findBYPK(5);
            $comment1 = $comment2 = $comment3 = $comment4 = $comment5 = '';
            if (!empty($modelComment1)) {
                $comment1 = $modelComment1->comments;
            }
            if (!empty($modelComment2)) {
                $comment2 = $modelComment2->comments;
            }
            if (!empty($modelComment3)) {
                $comment3 = $modelComment3->comments;
            }
            if (!empty($modelComment4)) {
                $comment4 = $modelComment4->comments;
            }
            if (!empty($modelComment5)) {
                $comment5 = $modelComment5->comments;
            }
            $list[$j++] = '!!!!!'.$comment1;
            $list[$j++] = '!!!!!'.$comment2;
            $list[$j++] = '!!!!!'.$comment3;
            $list[$j++] = '!!!!!'.$comment4;
            $list[$j++] = '!!!!!'.$comment5;
            $jan = '';
            $feb = '';
            $mar = '';
            $apr = '';
            $may = '';
            $jun = '';
            $jul = '';
            $aug = '';
            $sep = '';
            $oct = '';
            $nov = '';
            $dec = '';

            if ((int) $month > 0) {

                switch ((int) $month) {

                    case 1:

                        $jan = 'Input your Order in this month =>';

                        break;

                    case 2:

                        $feb = 'Input your Order in this month =>';

                        break;

                    case 3:

                        $mar = 'Input your Order in this month =>' ;//. 'https://blueskylogistics.me/yamaha_new_18_11_2022/images/darrow.png'

                        break;

                    case 4:

                        $apr = 'Input your Order in this month =>';

                        break;

                    case 5:

                        $may = 'Input your Order in this month =>';

                        break;

                    case 6:

                        $jun = 'Input your Order in this month =>';

                        break;

                    case 7:

                        $jul = 'Input your Order in this month =>';

                        break;

                    case 8:

                        $aug = 'Input your Order in this month =>';

                        break;

                    case 9:

                        $sep = 'Input your Order in this month =>';

                        break;

                    case 10:

                        $oct = 'Input your Order in this month =>';

                        break;

                    case 11:

                        $nov = 'Input your Order in this month =>';

                        break;

                    case 12:

                        $dec = 'Input your Order in this month =>';

                        break;
                }
            }

            $list[$j++] = 'Distributor!!' . $mod_dis->distributer_name . '!!!!!' . $apr . '!!!' . $may . '!!!' . $jun . '!!!' . '!!!' . $jul . '!!!' . $aug . '!!!' . $sep . '!!!' . '!!!' . $oct . '!!!' . $nov . '!!!' . $dec . '!!!' . '!!!' . $jan . '!!!' . $feb . '!!!' . $mar;

            $year1 = (int) $year + 1;

            $list[$j++] = '!PRODUCT CATEGORY!GMC!Model!' . $mod_dis->region_det->region_name . '!CBM!BASE CODE!01-04-' . $year . '!!!01-05-' . $year . '!!!01-06-' . $year . '!!!1Q TOTAL!!!01-07-' . $year . '!!!01-08-' . $year . '!!!01-09-' . $year . '!!!2Q TOTAL!!!01-10-' . $year . '!!!!!01-11-' . $year . '!!!01-12-' . $year . '!!!3Q TOTAL!!!01-01-' . $year1 . '!!!01-02-' . $year1 . '!!!01-03-' . $year1 . '!!!4Q TOTAL!!!TOTAL' . $year . '-' . $year1;

            $list[$j++] = '!!!!!!!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!ORDER!Supply Confirmation!SHIP!DIF!CBM';

            foreach ($model_inventory as $i => $row) {

                $apr_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="4" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $may_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="5" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jun_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="6" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jul_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="7" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $aug_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="8" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $sep_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="9" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $oct_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="10" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $nov_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="11" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $dec_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="12" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jan_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="1" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $feb_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="2" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $mar_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'quantity_ordered,quantity_confirmed,quantity_shipped', 'condition' => 'product_id=' . (int) $row['id'] . ' AND parent_det.month="3" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                /* ordered */

                //$qty_apr=(int)$apr_model->quantity_ordered;
                $qty_apr = !empty($apr_model) ? $apr_model->quantity_ordered : 0;

                //$qty_may=(int)$may_model->quantity_ordered;
                $qty_may = !empty($may_model) ? $may_model->quantity_ordered : 0;

                // $qty_jun=(int)$jun_model->quantity_ordered;
                $qty_jun = !empty($jun_model) ? $jun_model->quantity_ordered : 0;

                //$qty_jul=(int)$jul_model->quantity_ordered;
                $qty_jul = !empty($jul_model) ? $jul_model->quantity_ordered : 0;

                //$qty_aug=(int)$aug_model->quantity_ordered;
                $qty_aug = !empty($aug_model) ? $aug_model->quantity_ordered : 0;

                //$qty_sep=(int)$sep_model->quantity_ordered;
                $qty_sep = !empty($sep_model) ? $sep_model->quantity_ordered : 0;

                //$qty_oct=(int)$oct_model->quantity_ordered;
                $qty_oct = !empty($oct_model) ? $oct_model->quantity_ordered : 0;

                //$qty_nov=(int)$nov_model->quantity_ordered;
                $qty_nov = !empty($nov_model) ? $nov_model->quantity_ordered : 0;

                //$qty_dec=(int)$dec_model->quantity_ordered;
                $qty_dec = !empty($dec_model) ? $dec_model->quantity_ordered : 0;

                //$qty_jan=(int)$jan_model->quantity_ordered;
                $qty_jan = !empty($jan_model) ? $jan_model->quantity_ordered : 0;

                //$qty_feb=(int)$feb_model->quantity_ordered;
                $qty_feb = !empty($feb_model) ? $feb_model->quantity_ordered : 0;

                //$qty_mar=(int)$mar_model->quantity_ordered;
                $qty_mar = !empty($mar_model) ? $mar_model->quantity_ordered : 0;

                $qty_q1 = $qty_apr + $qty_may + $qty_jun;

                $qty_q2 = $qty_jul + $qty_aug + $qty_sep;

                $qty_q3 = $qty_oct + $qty_nov + $qty_dec;

                $qty_q4 = $qty_jan + $qty_feb + $qty_mar;

                $qty_tot = $qty_q1 + $qty_q2 + $qty_q3 + $qty_q4;

                /* supplyconfirmation */



                $supplyqty_apr = !empty($apr_model) ? $apr_model->quantity_confirmed : 0; //(int)$apr_model->quantity_confirmed; 

                $dsupplyqty_apr = '';

                if ($qty_apr > 0 && $apr_model->is_done == "Y") {
                    $dsupplyqty_apr = $supplyqty_apr;
                }

                $supplyqty_may = !empty($may_model) ? $may_model->quantity_confirmed : 0; //(int)$may_model->quantity_confirmed;

                $dsupplyqty_may = '';

                if ($qty_may > 0 && $may_model->is_done == "Y") {

                    $dsupplyqty_may = $supplyqty_may;
                }

                $supplyqty_jun = !empty($jun_model) ? $jun_model->quantity_confirmed : 0; //(int)$jun_model->quantity_confirmed;

                $dsupplyqty_jun = '';

                if ($qty_jun > 0 && $jun_model->is_done == "Y") {

                    $dsupplyqty_jun = $supplyqty_jun;
                }

                $supplyqty_jul = !empty($jul_model) ? $jul_model->quantity_confirmed : 0; //(int)$jul_model->quantity_confirmed;

                $dsupplyqty_jul = '';

                if ($qty_jul > 0 && $jul_model->is_done == "Y") {

                    $dsupplyqty_jul = $supplyqty_jul;
                }

                $supplyqty_aug = !empty($aug_model) ? $aug_model->quantity_confirmed : 0; //(int)$aug_model->quantity_confirmed;

                $dsupplyqty_aug = '';

                if ($qty_aug > 0 && $aug_model->is_done == "Y") {

                    $dsupplyqty_aug = $supplyqty_aug;
                }

                $supplyqty_sep = !empty($sep_model) ? $sep_model->quantity_confirmed : 0; //(int)$sep_model->quantity_confirmed;

                $dsupplyqty_sep = '';

                if ($qty_sep > 0 && $sep_model->is_done == "Y") {

                    $dsupplyqty_sep = $supplyqty_sep;
                }

                $supplyqty_oct = !empty($oct_model) ? $oct_model->quantity_confirmed : 0; //(int)$oct_model->quantity_confirmed;

                $dsupplyqty_oct = '';

                if ($qty_oct > 0 && $oct_model->is_done == "Y") {

                    $dsupplyqty_oct = $supplyqty_oct;
                }

                $supplyqty_nov = !empty($nov_model) ? $nov_model->quantity_confirmed : 0; //(int)$nov_model->quantity_confirmed;

                $dsupplyqty_nov = '';

                if ($qty_nov > 0 && $nov_model->is_done == "Y") {

                    $dsupplyqty_nov = $supplyqty_nov;
                }

                $supplyqty_dec = !empty($dec_model) ? $dec_model->quantity_confirmed : 0; //(int)$dec_model->quantity_confirmed;

                $dsupplyqty_dec = '';

                if ($qty_dec > 0 && $dec_model->is_done == "Y") {

                    $dsupplyqty_dec = $supplyqty_dec;
                }

                $supplyqty_jan = !empty($jan_model) ? $jan_model->quantity_confirmed : 0; //(int)$jan_model->quantity_confirmed;

                $dsupplyqty_jan = '';

                if ($qty_jan > 0 && $jan_model->is_done == "Y") {

                    $dsupplyqty_jan = $supplyqty_jan;
                }

                $supplyqty_feb = !empty($feb_model) ? $feb_model->quantity_confirmed : 0; //(int)$feb_model->quantity_confirmed;

                $dsupplyqty_feb = '';

                if ($qty_feb > 0 && $feb_model->is_done == "Y") {

                    $dsupplyqty_feb = $supplyqty_feb;
                }

                $supplyqty_mar = !empty($mar_model) ? $mar_model->quantity_confirmed : 0; //(int)$mar_model->quantity_confirmed;

                $dsupplyqty_mar = '';

                if ($qty_mar > 0 && $mar_model->is_done == "Y") {

                    $dsupplyqty_mar = $supplyqty_mar;
                }

                $supplyqty_q1 = $supplyqty_apr + $supplyqty_may + $supplyqty_jun;

                $supplyqty_q2 = $supplyqty_jul + $supplyqty_aug + $supplyqty_sep;

                $supplyqty_q3 = $supplyqty_oct + $supplyqty_nov + $supplyqty_dec;

                $supplyqty_q4 = $supplyqty_jan + $supplyqty_feb + $supplyqty_mar;

                $supplyqty_tot = $supplyqty_q1 + $supplyqty_q2 + $supplyqty_q3 + $supplyqty_q4;

                /* supplyconfirmation */

                /* supplycbm */

                $supplycbmqty_apr = $supplyqty_apr * $row['cbm'];

                $supplycbmqty_may = $supplyqty_may * $row['cbm'];

                $supplycbmqty_jun = $supplyqty_jun * $row['cbm'];

                $supplycbmqty_jul = $supplyqty_jul * $row['cbm'];

                $supplycbmqty_aug = $supplyqty_aug * $row['cbm'];

                $supplycbmqty_sep = $supplyqty_sep * $row['cbm'];

                $supplycbmqty_oct = $supplyqty_oct * $row['cbm'];

                $supplycbmqty_nov = $supplyqty_nov * $row['cbm'];

                $supplycbmqty_dec = $supplyqty_dec * $row['cbm'];

                $supplycbmqty_jan = $supplyqty_jan * $row['cbm'];

                $supplycbmqty_feb = $supplyqty_feb * $row['cbm'];

                $supplycbmqty_mar = $supplyqty_mar * $row['cbm'];

                $supplycbmqty_q1 = $supplycbmqty_apr + $supplycbmqty_may + $supplycbmqty_jun;

                $supplycbmqty_q2 = $supplycbmqty_jul + $supplycbmqty_aug + $supplycbmqty_sep;

                $supplycbmqty_q3 = $supplycbmqty_oct + $supplycbmqty_nov + $supplycbmqty_dec;

                $supplycbmqty_q4 = $supplycbmqty_jan + $supplycbmqty_feb + $supplycbmqty_mar;

                $supplycbmqty_tot = $supplycbmqty_q1 + $supplycbmqty_q2 + $supplycbmqty_q3 + $supplycbmqty_q4;

                /* supplycbm */

                /* shipped cbm */

                $shp_apr = !empty($apr_model) ? $apr_model->quantity_shipped : 0; //(int)$apr_model->quantity_shipped;

                $shp_may = !empty($may_model) ? $may_model->quantity_shipped : 0; //(int)$may_model->quantity_shipped;

                $shp_jun = !empty($jun_model) ? $jun_model->quantity_shipped : 0; //(int)$jun_model->quantity_shipped;

                $shp_jul = !empty($jul_model) ? $jul_model->quantity_shipped : 0; //(int)$jul_model->quantity_shipped;

                $shp_aug = !empty($aug_model) ? $aug_model->quantity_shipped : 0; //(int)$aug_model->quantity_shipped;

                $shp_sep = !empty($sep_model) ? $sep_model->quantity_shipped : 0; //(int)$sep_model->quantity_shipped;

                $shp_oct = !empty($oct_model) ? $oct_model->quantity_shipped : 0; //(int)$oct_model->quantity_shipped;

                $shp_nov = !empty($nov_model) ? $nov_model->quantity_shipped : 0; //(int)$nov_model->quantity_shipped;

                $shp_dec = !empty($dec_model) ? $dec_model->quantity_shipped : 0; //(int)$dec_model->quantity_shipped;

                $shp_jan = !empty($jan_model) ? $jan_model->quantity_shipped : 0; //(int)$jan_model->quantity_shipped;

                $shp_feb = !empty($feb_model) ? $feb_model->quantity_shipped : 0; //(int)$feb_model->quantity_shipped;

                $shp_mar = !empty($mar_model) ? $mar_model->quantity_shipped : 0; //(int)$mar_model->quantity_shipped;

                $shp_q1 = $shp_apr + $shp_may + $shp_jun;

                $shp_q2 = $shp_jul + $shp_aug + $shp_sep;

                $shp_q3 = $shp_oct + $shp_nov + $shp_dec;

                $shp_q4 = $shp_jan + $shp_feb + $shp_mar;

                $shp_tot = $shp_q1 + $shp_q2 + $shp_q3 + $shp_q4;

                /* shipped cbm */

                /* region value */

                $price_cnd = 'product_id=' . (int) $row['id'] . ' AND region_id=' . (int) $region_id . ' AND from_date <="' . $price_date . '" AND to_date >="' . $price_date . '"';
                $mod_inv_price = InventoryPrice::model()->find(array('condition' => $price_cnd));

                /* region value */

                $ymg_price = !empty($mod_inv_price) ? $mod_inv_price['price'] : 0;
                $list[$j++] = '!' . $row['category_det']['product_category'] . '!' . $row['gmc_code'] . '!' . $row['model_no']
                        . '!' . (float) $ymg_price . '!' . $row['cbm'] . '!' . $row['base_code']
                        . '!' . $qty_apr . '!' . $dsupplyqty_apr . '!' . $shp_apr . '!' . ($supplyqty_apr - $shp_apr) . '!' . $supplycbmqty_apr . '!' . $qty_may . '!' . $dsupplyqty_may . '!' . $shp_may . '!0!' . $supplycbmqty_may . '!' . $qty_jun . '!' . $dsupplyqty_jun . '!' . $shp_jun . '!0!' . $supplycbmqty_jun . '!' . $qty_q1 . '!' . $supplyqty_q1 . '!' . $shp_q1 . '!0!' . $supplycbmqty_q1
                        . '!' . $qty_jul . '!' . $dsupplyqty_jul . '!' . $shp_jul . '!0!' . $supplycbmqty_jul . '!' . $qty_aug . '!' . $dsupplyqty_aug . '!' . $shp_aug . '!0!' . $supplycbmqty_aug . '!' . $qty_sep . '!' . $dsupplyqty_sep . '!' . $shp_sep . '!0!' . $supplycbmqty_sep . '!' . $qty_q2 . '!' . $supplyqty_q2 . '!' . $shp_q2 . '!0!' . $supplycbmqty_q2
                        . '!' . $qty_oct . '!' . $dsupplyqty_oct . '!' . $shp_oct . '!0!' . $supplycbmqty_oct . '!' . $qty_nov . '!' . $dsupplyqty_nov . '!' . $shp_nov . '!0!' . $supplycbmqty_nov . '!' . $qty_dec . '!' . $dsupplyqty_dec . '!' . $shp_dec . '!0!' . $supplycbmqty_dec . '!' . $qty_q3 . '!' . $supplyqty_q3 . '!' . $shp_q3 . '!0!' . $supplycbmqty_q3
                        . '!' . $qty_jan . '!' . $dsupplyqty_jan . '!' . $shp_jan . '!0!0' . '!' . $qty_feb . '!' . $dsupplyqty_feb . '!' . $shp_feb . '!0!0' . '!' . $qty_mar . '!' . $dsupplyqty_mar . '!' . $shp_mar . '!0!0' . '!' . $qty_q4 . '!' . $supplyqty_q4 . '!' . $shp_q4 . '!0!' . $supplycbmqty_q4
                        . '!' . $qty_tot . '!' . $supplyqty_tot . '!' . $shp_tot . '!0!' . $supplycbmqty_tot;
            }

            $list[$j++] = '';

            $list[$j++] = '!PRODUCT CATEGORY!!!!!!01-04-' . $year . '!!!!!01-05-' . $year . '!!!!!01-06-' . $year . '!!!!!1Q TOTAL!!!!!01-07-' . $year . '!!!!!01-08-' . $year . '!!!!!01-09-' . $year . '!!!!!2Q TOTAL!!!!!01-10-' . $year . '!!!!!01-11-' . $year . '!!!!!01-12-' . $year . '!!!!!3Q TOTAL!!!!!01-01-' . $year1 . '!!!!!01-02-' . $year1 . '!!!!!01-03-' . $year1 . '!!!!!4Q TOTAL!!!!!TOTAL' . $year . '-' . $year1;
            $list1 = [];
            $list2 = [];
            $k = 0;
            $modelmascategories = MasProductCategory::model()->findAll(array('order' => 'sort_order ASC'));

            $totcat_sum = 0;

            $totqty_apr = $totqty_may = $totqty_jun = $totqty_jul = $totqty_aug = $totqty_sep = $totqty_oct = $totqty_nov = $totqty_dec = $totqty_jan = $totqty_feb = $totqty_mar = 0;

            $totamt_apr = $totamt_may = $totamt_jun = $totamt_jul = $totamt_aug = $totamt_sep = $totamt_oct = $totamt_nov = $totamt_dec = $totamt_jan = $totamt_feb = $totamt_mar = 0;

            $totsupplyqty_apr = $totsupplyqty_may = $totsupplyqty_jun = $totsupplyqty_jul = $totsupplyqty_aug = $totsupplyqty_sep = $totsupplyqty_oct = $totsupplyqty_nov = $totsupplyqty_dec = $totsupplyqty_jan = $totsupplyqty_feb = $totsupplyqty_mar = 0;

            $totsupplycbmqty_apr = $totsupplycbmqty_may = $totsupplycbmqty_jun = $totsupplycbmqty_jul = $totsupplycbmqty_aug = $totsupplycbmqty_sep = $totsupplycbmqty_oct = $totsupplycbmqty_nov = $totsupplycbmqty_dec = $totsupplycbmqty_jan = $totsupplycbmqty_feb = $totsupplycbmqty_mar = 0;

            $totshp_apr = $totshp_may = $totshp_jun = $totshp_jul = $totshp_aug = $totshp_sep = $totshp_oct = $totshp_nov = $totshp_dec = $totshp_jan = $totshp_feb = $totshp_mar = 0;

            $totqty_q1 = $totqty_q2 = $totqty_q3 = $totqty_q4 = $totqty_tot = 0;

            $totsupplyqty_q1 = $totsupplyqty_q2 = $totsupplyqty_q3 = $totsupplyqty_q4 = $totsupplyqty_tot = 0;

            $totsupplycbmqty_q1 = $totsupplycbmqty_q2 = $totsupplycbmqty_q3 = $totsupplycbmqty_q4 = $totsupplycbmqty_tot = 0;

            $totamt_q1 = $totamt_q2 = $totamt_q3 = $totamt_q4 = $totamt_tot = 0;

            $totsupplyamt_q1 = $totsupplyamt_q2 = $totsupplyamt_q3 = $totsupplyamt_q4 = $totsupplyamt_tot = 0;

            $totsupplycbmamt_q1 = $totsupplycbmamt_q2 = $totsupplycbmamt_q3 = $totsupplycbmamt_q4 = $totsupplycbmamt_tot = 0;

            $totshpamt_q1 = $totshpamt_q2 = $totshpamt_q3 = $totshpamt_q4 = $totshpamt_tot = 0;

            foreach ($modelmascategories as $eachcat) {

                $apr_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price*exchange_rate) AS amount_ordered,SUM(quantity_confirmed*price*exchange_rate) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="4" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $may_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="5" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jun_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="6" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jul_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="7" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $aug_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="8" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $sep_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="9" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $oct_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="10" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $nov_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="11" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $dec_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="12" AND parent_det.year=' . $year . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $jan_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="1" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $feb_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="2" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                $mar_model = OrderDetails::model()->with('parent_det')->find(array('select' => 'SUM(quantity_ordered) AS quantity_ordered,SUM(quantity_confirmed) AS quantity_confirmed,SUM(quantity_shipped) AS quantity_shipped,SUM(quantity_ordered*price) AS amount_ordered,SUM(quantity_confirmed*price) AS amount_confirmed,SUM(quantity_shipped*price) AS amount_shipped', 'condition' => 'category_id=' . (int) $eachcat['id'] . ' AND parent_det.month="3" AND parent_det.year=' . $year1 . ' AND parent_det.distributer_id=' . (int) $distributer_id));

                /* ordered */

                $qty_apr = !empty($apr_model) ? $apr_model->quantity_ordered : 0; //(int)$apr_model->quantity_ordered;

                $qty_may = !empty($may_model) ? $may_model->quantity_ordered : 0; //(int)$may_model->quantity_ordered;

                $qty_jun = !empty($jun_model) ? $jun_model->quantity_ordered : 0; //(int)$jun_model->quantity_ordered;

                $qty_jul = !empty($jul_model) ? $jul_model->quantity_ordered : 0; //(int)$jul_model->quantity_ordered;

                $qty_aug = !empty($aug_model) ? $aug_model->quantity_ordered : 0; //(int)$aug_model->quantity_ordered;

                $qty_sep = !empty($sep_model) ? $sep_model->quantity_ordered : 0; //(int)$sep_model->quantity_ordered;

                $qty_oct = !empty($oct_model) ? $oct_model->quantity_ordered : 0; //(int)$oct_model->quantity_ordered;

                $qty_nov = !empty($nov_model) ? $nov_model->quantity_ordered : 0; //(int)$nov_model->quantity_ordered;

                $qty_dec = !empty($dec_model) ? $dec_model->quantity_ordered : 0; //(int)$dec_model->quantity_ordered;

                $qty_jan = !empty($jan_model) ? $jan_model->quantity_ordered : 0; //(int)$jan_model->quantity_ordered;

                $qty_feb = !empty($feb_model) ? $feb_model->quantity_ordered : 0; //(int)$feb_model->quantity_ordered;

                $qty_mar = !empty($mar_model) ? $mar_model->quantity_ordered : 0; //(int)$mar_model->quantity_ordered;

                $qty_q1 = $qty_apr + $qty_may + $qty_jun;

                $qty_q2 = $qty_jul + $qty_aug + $qty_sep;

                $qty_q3 = $qty_oct + $qty_nov + $qty_dec;

                $qty_q4 = $qty_jan + $qty_feb + $qty_mar;

                $qty_tot = $qty_q1 + $qty_q2 + $qty_q3 + $qty_q4;

                //totals

                $totqty_apr += $qty_apr;

                $totqty_may += $qty_may;

                $totqty_jun += $qty_jun;

                $totqty_jul += $qty_jul;

                $totqty_aug += $qty_aug;

                $totqty_sep += $qty_sep;

                $totqty_oct += $qty_oct;

                $totqty_nov += $qty_nov;

                $totqty_dec += $qty_dec;

                $totqty_jan += $qty_jan;

                $totqty_feb += $qty_feb;

                $totqty_mar += $qty_mar;

                $totqty_q1 = $totqty_apr + $totqty_may + $totqty_jun;

                $totqty_q2 = $totqty_jul + $totqty_aug + $totqty_sep;

                $totqty_q3 = $totqty_oct + $totqty_nov + $totqty_dec;

                $totqty_q4 = $totqty_jan + $totqty_feb + $totqty_mar;

                $totqty_tot = $totqty_q1 + $totqty_q2 + $totqty_q3 + $totqty_q4;

                /* supplyconfirmation */

                $supplyqty_apr = !empty($apr_model) ? $apr_model->quantity_confirmed : 0; //(int)$apr_model->quantity_confirmed;

                $supplyqty_may = !empty($may_model) ? $may_model->quantity_confirmed : 0; //(int)$may_model->quantity_confirmed;

                $supplyqty_jun = !empty($jun_model) ? $jun_model->quantity_confirmed : 0; //(int)$jun_model->quantity_confirmed;

                $supplyqty_jul = !empty($jul_model) ? $jul_model->quantity_confirmed : 0; //(int)$jul_model->quantity_confirmed;

                $supplyqty_aug = !empty($aug_model) ? $aug_model->quantity_confirmed : 0; //(int)$aug_model->quantity_confirmed;

                $supplyqty_sep = !empty($sep_model) ? $sep_model->quantity_confirmed : 0; //(int)$sep_model->quantity_confirmed;

                $supplyqty_oct = !empty($oct_model) ? $oct_model->quantity_confirmed : 0; //(int)$oct_model->quantity_confirmed;

                $supplyqty_nov = !empty($nov_model) ? $nov_model->quantity_confirmed : 0; //(int)$nov_model->quantity_confirmed;

                $supplyqty_dec = !empty($dec_model) ? $dec_model->quantity_confirmed : 0; //(int)$dec_model->quantity_confirmed;

                $supplyqty_jan = !empty($jan_model) ? $jan_model->quantity_confirmed : 0; //(int)$jan_model->quantity_confirmed;

                $supplyqty_feb = !empty($feb_model) ? $feb_model->quantity_confirmed : 0; //(int)$feb_model->quantity_confirmed;

                $supplyqty_mar = !empty($mar_model) ? $mar_model->quantity_confirmed : 0; //(int)$mar_model->quantity_confirmed;

                $supplyqty_q1 = $supplyqty_apr + $supplyqty_may + $supplyqty_jun;

                $supplyqty_q2 = $supplyqty_jul + $supplyqty_aug + $supplyqty_sep;

                $supplyqty_q3 = $supplyqty_oct + $supplyqty_nov + $supplyqty_dec;

                $supplyqty_q4 = $supplyqty_jan + $supplyqty_feb + $supplyqty_mar;

                $supplyqty_tot = $supplyqty_q1 + $supplyqty_q2 + $supplyqty_q3 + $supplyqty_q4;

                /* supplyconfirmation */

                //total

                $totsupplyqty_apr += $supplyqty_apr;

                $totsupplyqty_may += $supplyqty_may;

                $totsupplyqty_jun += $supplyqty_jun;

                $totsupplyqty_jul += $supplyqty_jul;

                $totsupplyqty_aug += $supplyqty_aug;

                $totsupplyqty_sep += $supplyqty_sep;

                $totsupplyqty_oct += $supplyqty_oct;

                $totsupplyqty_nov += $supplyqty_nov;

                $totsupplyqty_dec += $supplyqty_dec;

                $totsupplyqty_jan += $supplyqty_jan;

                $totsupplyqty_feb += $supplyqty_feb;

                $totsupplyqty_mar += $supplyqty_mar;

                $totsupplyqty_q1 = $totsupplyqty_apr + $totsupplyqty_may + $totsupplyqty_jun;

                $totsupplyqty_q2 = $totsupplyqty_jul + $totsupplyqty_aug + $totsupplyqty_sep;

                $totsupplyqty_q3 = $totsupplyqty_oct + $totsupplyqty_nov + $totsupplyqty_dec;

                $totsupplyqty_q4 = $totsupplyqty_jan + $totsupplyqty_feb + $totsupplyqty_mar;

                $totsupplyqty_tot = $totsupplyqty_q1 + $totsupplyqty_q2 + $totsupplyqty_q3 + $totsupplyqty_q4;

                /* supplycbm */

                $supplycbmqty_apr = $supplyqty_apr * $row['cbm'];

                $supplycbmqty_may = $supplyqty_may * $row['cbm'];

                $supplycbmqty_jun = $supplyqty_jun * $row['cbm'];

                $supplycbmqty_jul = $supplyqty_jul * $row['cbm'];

                $supplycbmqty_aug = $supplyqty_aug * $row['cbm'];

                $supplycbmqty_sep = $supplyqty_sep * $row['cbm'];

                $supplycbmqty_oct = $supplyqty_oct * $row['cbm'];

                $supplycbmqty_nov = $supplyqty_nov * $row['cbm'];

                $supplycbmqty_dec = $supplyqty_dec * $row['cbm'];

                $supplycbmqty_jan = $supplyqty_jan * $row['cbm'];

                $supplycbmqty_feb = $supplyqty_feb * $row['cbm'];

                $supplycbmqty_mar = $supplyqty_mar * $row['cbm'];

                $supplycbmqty_q1 = $supplycbmqty_apr + $supplycbmqty_may + $supplycbmqty_jun;

                $supplycbmqty_q2 = $supplycbmqty_jul + $supplycbmqty_aug + $supplycbmqty_sep;

                $supplycbmqty_q3 = $supplycbmqty_oct + $supplycbmqty_nov + $supplycbmqty_dec;

                $supplycbmqty_q4 = $supplycbmqty_jan + $supplycbmqty_feb + $supplycbmqty_mar;

                $supplycbmqty_tot = $supplycbmqty_q1 + $supplycbmqty_q2 + $supplycbmqty_q3 + $supplycbmqty_q4;

                /* supplycbm */

                //total

                $totsupplycbmqty_apr += $supplycbmqty_apr;

                $totsupplycbmqty_may += $supplycbmqty_may;

                $totsupplycbmqty_jun += $supplycbmqty_jun;

                $totsupplycbmqty_jul += $supplycbmqty_jul;

                $totsupplycbmqty_aug += $supplycbmqty_aug;

                $totsupplycbmqty_sep += $supplycbmqty_sep;

                $totsupplycbmqty_oct += $supplycbmqty_oct;

                $totsupplycbmqty_nov += $supplycbmqty_nov;

                $totsupplycbmqty_dec += $supplycbmqty_dec;

                $totsupplycbmqty_jan += $supplycbmqty_jan;

                $totsupplycbmqty_feb += $supplycbmqty_feb;

                $totsupplycbmqty_mar += $supplycbmqty_mar;

                $totsupplycbmqty_q1 = $totsupplycbmqty_apr + $totsupplycbmqty_may + $totsupplycbmqty_jun;

                $totsupplycbmqty_q2 = $totsupplycbmqty_jul + $totsupplycbmqty_aug + $totsupplycbmqty_sep;

                $totsupplycbmqty_q3 = $totsupplycbmqty_oct + $totsupplycbmqty_nov + $totsupplycbmqty_dec;

                $totsupplycbmqty_q4 = $totsupplycbmqty_jan + $totsupplycbmqty_feb + $totsupplycbmqty_mar;

                $totsupplycbmqty_tot = $totsupplycbmqty_q1 + $totsupplycbmqty_q2 + $totsupplycbmqty_q3 + $totsupplycbmqty_q4;

                /* shipped cbm */

                $shp_apr = !empty($apr_model) ? $apr_model->quantity_shipped : 0; //(int)$apr_model->quantity_shipped;

                $shp_may = !empty($may_model) ? $may_model->quantity_shipped : 0; //(int)$may_model->quantity_shipped;

                $shp_jun = !empty($jun_model) ? $jun_model->quantity_shipped : 0; //(int)$jun_model->quantity_shipped;

                $shp_jul = !empty($jul_model) ? $jul_model->quantity_shipped : 0; //(int)$jul_model->quantity_shipped;

                $shp_aug = !empty($aug_model) ? $aug_model->quantity_shipped : 0; //(int)$aug_model->quantity_shipped;

                $shp_sep = !empty($sep_model) ? $sep_model->quantity_shipped : 0; //(int)$sep_model->quantity_shipped;

                $shp_oct = !empty($oct_model) ? $oct_model->quantity_shipped : 0; //(int)$oct_model->quantity_shipped;

                $shp_nov = !empty($nov_model) ? $nov_model->quantity_shipped : 0; //(int)$nov_model->quantity_shipped;

                $shp_dec = !empty($dec_model) ? $dec_model->quantity_shipped : 0; //(int)$dec_model->quantity_shipped;

                $shp_jan = !empty($jan_model) ? $jan_model->quantity_shipped : 0; //(int)$jan_model->quantity_shipped;

                $shp_feb = !empty($feb_model) ? $feb_model->quantity_shipped : 0; //(int)$feb_model->quantity_shipped;

                $shp_mar = !empty($mar_model) ? $mar_model->quantity_shipped : 0; //(int)$mar_model->quantity_shipped;

                $shp_q1 = $shp_apr + $shp_may + $shp_jun;

                $shp_q2 = $shp_jul + $shp_aug + $shp_sep;

                $shp_q3 = $shp_oct + $shp_nov + $shp_dec;

                $shp_q4 = $shp_jan + $shp_feb + $shp_mar;

                $shp_tot = $shp_q1 + $shp_q2 + $shp_q3 + $shp_q4;

                /* shipped cbm */

                //total

                $totshp_apr += $shp_apr;

                $totshp_may += $shp_may;

                $totshp_jun += $shp_jun;

                $totshp_jul += $shp_jul;

                $totshp_aug += $shp_aug;

                $totshp_sep += $shp_sep;

                $totshp_oct += $shp_oct;

                $totshp_nov += $shp_nov;

                $totshp_dec += $shp_dec;

                $totshp_jan += $shp_jan;

                $totshp_feb += $shp_feb;

                $totshp_mar += $shp_mar;

                //amount started

                /* ordered */

                $amt_apr = !empty($apr_model) ? $apr_model->amount_ordered : 0; //(int)$apr_model->amount_ordered;

                $amt_may = !empty($may_model) ? $may_model->amount_ordered : 0; //(int)$may_model->amount_ordered;

                $amt_jun = !empty($jun_model) ? $jun_model->amount_ordered : 0; //(int)$jun_model->amount_ordered;

                $amt_jul = !empty($jul_model) ? $jul_model->amount_ordered : 0; //(int)$jul_model->amount_ordered;

                $amt_aug = !empty($aug_model) ? $aug_model->amount_ordered : 0; //(int)$aug_model->amount_ordered;

                $amt_sep = !empty($sep_model) ? $sep_model->amount_ordered : 0; //(int)$sep_model->amount_ordered;

                $amt_oct = !empty($oct_model) ? $oct_model->amount_ordered : 0; //(int)$oct_model->amount_ordered;

                $amt_nov = !empty($nov_model) ? $nov_model->amount_ordered : 0; //(int)$nov_model->amount_ordered;

                $amt_dec = !empty($dec_model) ? $dec_model->amount_ordered : 0; //(int)$dec_model->amount_ordered;

                $amt_jan = !empty($jan_model) ? $jan_model->amount_ordered : 0; //(int)$jan_model->amount_ordered;

                $amt_feb = !empty($feb_model) ? $feb_model->amount_ordered : 0; //(int)$feb_model->amount_ordered;

                $amt_mar = !empty($mar_model) ? $mar_model->amount_ordered : 0; //(int)$mar_model->amount_ordered;

                $amt_q1 = $amt_apr + $amt_may + $amt_jun;

                $amt_q2 = $amt_jul + $amt_aug + $amt_sep;

                $amt_q3 = $amt_oct + $amt_nov + $amt_dec;

                $amt_q4 = $amt_jan + $amt_feb + $amt_mar;

                $amt_tot = $amt_q1 + $amt_q2 + $amt_q3 + $amt_q4;

                /* supplyconfirmation */

                //total

                $totamt_apr += $amt_apr;

                $totamt_may += $amt_may;

                $totamt_jun += $amt_jun;

                $totamt_jul += $amt_jul;

                $totamt_aug += $amt_aug;

                $totamt_sep += $amt_sep;

                $totamt_oct += $amt_oct;

                $totamt_nov += $amt_nov;

                $totamt_dec += $amt_dec;

                $totamt_jan += $amt_jan;

                $totamt_feb += $amt_feb;

                $totamt_mar += $amt_mar;

                $totamt_q1 = $totamt_apr + $totamt_may + $totamt_jun;

                $totamt_q2 = $totamt_jul + $totamt_aug + $totamt_sep;

                $totamt_q3 = $totamt_oct + $totamt_nov + $totamt_dec;

                $totamt_q4 = $totamt_jan + $totamt_feb + $totamt_mar;

                $totamt_tot = $totamt_q1 + $totamt_q2 + $totamt_q3 + $totamt_q4;

                //end total
                ///////////////////code added by lidiya on 27-09-2023////////
                $totsupplyamt_apr = 0;

                $totsupplyamt_may = 0;

                $totsupplyamt_jun = 0;

                $totsupplyamt_jul = 0;

                $totsupplyamt_aug = 0;

                $totsupplyamt_sep = 0;

                $totsupplyamt_oct = 0;

                $totsupplyamt_nov = 0;

                $totsupplyamt_dec = 0;

                $totsupplyamt_jan = 0;

                $totsupplyamt_feb = 0;

                $totsupplyamt_mar = 0;

                ////////////////////////

                $supplyamt_apr = !empty($apr_model) ? $apr_model->amount_confirmed : 0; //(int)$apr_model->amount_confirmed;

                $supplyamt_may = !empty($may_model) ? $may_model->amount_confirmed : 0; //(int)$may_model->amount_confirmed;

                $supplyamt_jun = !empty($jun_model) ? $jun_model->amount_confirmed : 0; //(int)$jun_model->amount_confirmed;

                $supplyamt_jul = !empty($jul_model) ? $jul_model->amount_confirmed : 0; //(int)$jul_model->amount_confirmed;

                $supplyamt_aug = !empty($aug_model) ? $aug_model->amount_confirmed : 0; //(int)$aug_model->amount_confirmed;

                $supplyamt_sep = !empty($sep_model) ? $sep_model->amount_confirmed : 0; //(int)$sep_model->amount_confirmed;

                $supplyamt_oct = !empty($oct_model) ? $oct_model->amount_confirmed : 0; //(int)$oct_model->amount_confirmed;

                $supplyamt_nov = !empty($nov_model) ? $nov_model->amount_confirmed : 0; //(int)$nov_model->amount_confirmed;

                $supplyamt_dec = !empty($dec_model) ? $dec_model->amount_confirmed : 0; //(int)$dec_model->amount_confirmed;

                $supplyamt_jan = !empty($jan_model) ? $jan_model->amount_confirmed : 0; //(int)$jan_model->amount_confirmed;

                $supplyamt_feb = !empty($feb_model) ? $feb_model->amount_confirmed : 0; //(int)$feb_model->amount_confirmed;

                $supplyamt_mar = !empty($mar_model) ? $mar_model->amount_confirmed : 0; //(int)$mar_model->amount_confirmed;

                $supplyamt_q1 = $supplyamt_apr + $supplyamt_may + $supplyamt_jun;

                $supplyamt_q2 = $supplyamt_jul + $supplyamt_aug + $supplyamt_sep;

                $supplyamt_q3 = $supplyamt_oct + $supplyamt_nov + $supplyamt_dec;

                $supplyamt_q4 = $supplyamt_jan + $supplyamt_feb + $supplyamt_mar;

                $supplyamt_tot = $supplyamt_q1 + $supplyamt_q2 + $supplyamt_q3 + $supplyamt_q4;

                /* supplyconfirmation */

                //total

                $totsupplyamt_apr += $supplyamt_apr;

                $totsupplyamt_may += $supplyamt_may;

                $totsupplyamt_jun += $supplyamt_jun;

                $totsupplyamt_jul += $supplyamt_jul;

                $totsupplyamt_aug += $supplyamt_aug;

                $totsupplyamt_sep += $supplyamt_sep;

                $totsupplyamt_oct += $supplyamt_oct;

                $totsupplyamt_nov += $supplyamt_nov;

                $totsupplyamt_dec += $supplyamt_dec;

                $totsupplyamt_jan += $supplyamt_jan;

                $totsupplyamt_feb += $supplyamt_feb;

                $totsupplyamt_mar += $supplyamt_mar;

                $totsupplyamt_q1 = $totsupplyamt_apr + $totsupplyamt_may + $totsupplyamt_jun;

                $totsupplyamt_q2 = $totsupplyamt_jul + $totsupplyamt_aug + $totsupplyamt_sep;

                $totsupplyamt_q3 = $totsupplyamt_oct + $totsupplyamt_nov + $totsupplyamt_dec;

                $totsupplyamt_q4 = $totsupplyamt_jan + $totsupplyamt_feb + $totsupplyamt_mar;

                $totsupplyamt_tot = $totsupplyamt_q1 + $totsupplyamt_q2 + $totsupplyamt_q3 + $totsupplyamt_q4;

                //end total
                /////////////////Code added by lidiya on 27-09-2023/////////

                $totsupplycbmamt_apr = 0;

                $totsupplycbmamt_may = 0;

                $totsupplycbmamt_jun = 0;

                $totsupplycbmamt_jul = 0;

                $totsupplycbmamt_aug = 0;

                $totsupplycbmamt_sep = 0;

                $totsupplycbmamt_oct = 0;

                $totsupplycbmamt_nov = 0;

                $totsupplycbmamt_dec = 0;

                $totsupplycbmamt_jan = 0;

                $totsupplycbmamt_feb = 0;

                $totsupplycbmamt_mar = 0;

                /////////////////////////////////////////////////

                /* supplycbm */

                $supplycbmamt_apr = $supplyamt_apr * $row['cbm'];

                $supplycbmamt_may = $supplyamt_may * $row['cbm'];

                $supplycbmamt_jun = $supplyamt_jun * $row['cbm'];

                $supplycbmamt_jul = $supplyamt_jul * $row['cbm'];

                $supplycbmamt_aug = $supplyamt_aug * $row['cbm'];

                $supplycbmamt_sep = $supplyamt_sep * $row['cbm'];

                $supplycbmamt_oct = $supplyamt_oct * $row['cbm'];

                $supplycbmamt_nov = $supplyamt_nov * $row['cbm'];

                $supplycbmamt_dec = $supplyamt_dec * $row['cbm'];

                $supplycbmamt_jan = $supplyamt_jan * $row['cbm'];

                $supplycbmamt_feb = $supplyamt_feb * $row['cbm'];

                $supplycbmamt_mar = $supplyamt_mar * $row['cbm'];

                $supplycbmamt_q1 = $supplycbmamt_apr + $supplycbmamt_may + $supplycbmamt_jun;

                $supplycbmamt_q2 = $supplycbmamt_jul + $supplycbmamt_aug + $supplycbmamt_sep;

                $supplycbmamt_q3 = $supplycbmamt_oct + $supplycbmamt_nov + $supplycbmamt_dec;

                $supplycbmamt_q4 = $supplycbmamt_jan + $supplycbmamt_feb + $supplycbmamt_mar;

                $supplycbmamt_tot = $supplycbmamt_q1 + $supplycbmamt_q2 + $supplycbmamt_q3 + $supplycbmamt_q4;

                /* supplycbm */

                //total

                $totsupplycbmamt_apr += $supplyamt_apr;

                $totsupplycbmamt_may += $supplyamt_may;

                $totsupplycbmamt_jun += $supplyamt_jun;

                $totsupplycbmamt_jul += $supplyamt_jul;

                $totsupplycbmamt_aug += $supplyamt_aug;

                $totsupplycbmamt_sep += $supplyamt_sep;

                $totsupplycbmamt_oct += $supplyamt_oct;

                $totsupplycbmamt_nov += $supplyamt_nov;

                $totsupplycbmamt_dec += $supplyamt_dec;

                $totsupplycbmamt_jan += $supplyamt_jan;

                $totsupplycbmamt_feb += $supplyamt_feb;

                $totsupplycbmamt_mar += $supplyamt_mar;

                $totsupplycbmamt_q1 = $totsupplycbmamt_apr + $totsupplycbmamt_may + $totsupplycbmamt_jun;

                $totsupplycbmamt_q2 = $totsupplycbmamt_jul + $totsupplycbmamt_aug + $totsupplycbmamt_sep;

                $totsupplycbmamt_q3 = $totsupplycbmamt_oct + $totsupplycbmamt_nov + $totsupplycbmamt_dec;

                $totsupplycbmamt_q4 = $totsupplycbmamt_jan + $totsupplycbmamt_feb + $totsupplycbmamt_mar;

                $totsupplycbmamt_tot = $totsupplycbmamt_q1 + $totsupplycbmamt_q2 + $totsupplycbmamt_q3 + $totsupplycbmamt_q4;

                //end total
                ///////////////////////Code Added By Lidiya On 27-09-2023///////////

                $totshpamt_apr = 0;

                $totshpamt_may = 0;

                $totshpamt_jun = 0;

                $totshpamt_jul = 0;

                $totshpamt_aug = 0;

                $totshpamt_sep = 0;

                $totshpamt_oct = 0;

                $totshpamt_nov = 0;

                $totshpamt_dec = 0;

                $totshpamt_jan = 0;

                $totshpamt_feb = 0;

                $totshpamt_mar = 0;

                ////////////////////////////////////////////////////////////////
                /* shipped cbm */

                $shpamt_apr = !empty($apr_model) ? $apr_model->amount_shipped : 0; //(int)$apr_model->amount_shipped;

                $shpamt_may = !empty($may_model) ? $may_model->amount_shipped : 0; //(int)$may_model->amount_shipped;

                $shpamt_jun = !empty($jun_model) ? $jun_model->amount_shipped : 0; //(int)$jun_model->amount_shipped;

                $shpamt_jul = !empty($jul_model) ? $jul_model->amount_shipped : 0; //(int)$jul_model->amount_shipped;

                $shpamt_aug = !empty($aug_model) ? $aug_model->amount_shipped : 0; //(int)$aug_model->amount_shipped;

                $shpamt_sep = !empty($sep_model) ? $sep_model->amount_shipped : 0; //(int)$sep_model->amount_shipped;

                $shpamt_oct = !empty($oct_model) ? $oct_model->amount_shipped : 0; //(int)$oct_model->amount_shipped;

                $shpamt_nov = !empty($nov_model) ? $nov_model->amount_shipped : 0; //(int)$nov_model->amount_shipped;

                $shpamt_dec = !empty($dec_model) ? $dec_model->amount_shipped : 0; //(int)$dec_model->amount_shipped;

                $shpamt_jan = !empty($jan_model) ? $jan_model->amount_shipped : 0; //(int)$jan_model->amount_shipped;

                $shpamt_feb = !empty($feb_model) ? $feb_model->amount_shipped : 0; //(int)$feb_model->amount_shipped;

                $shpamt_mar = !empty($mar_model) ? $mar_model->amount_shipped : 0; //(int)$mar_model->amount_shipped;

                /* shipped cbm */

                //total

                $totshpamt_apr += $shpamt_apr;

                $totshpamt_may += $shpamt_may;

                $totshpamt_jun += $shpamt_jun;

                $totshpamt_jul += $shpamt_jul;

                $totshpamt_aug += $shpamt_aug;

                $totshpamt_sep += $shpamt_sep;

                $totshpamt_oct += $shpamt_oct;

                $totshpamt_nov += $shpamt_nov;

                $totshpamt_dec += $shpamt_dec;

                $totshpamt_jan += $shpamt_jan;

                $totshpamt_feb += $shpamt_feb;

                $totshpamt_mar += $shpamt_mar;

                $totshpamt_q1 = $totshpamt_apr + $totshpamt_may + $totshpamt_jun;

                $totshpamt_q2 = $totshpamt_jul + $totshpamt_aug + $totshpamt_sep;

                $totshpamt_q3 = $totshpamt_oct + $totshpamt_nov + $totshpamt_dec;

                $totshpamt_q4 = $totshpamt_jan + $totshpamt_feb + $totshpamt_mar;

                $totshpamt_tot = $totshpamt_q1 + $totshpamt_q2 + $totshpamt_q3 + $totshpamt_q4;

                //total



                $cinv_row = '!' . $eachcat->product_category . ' - Qty!!!!!!' . $qty_apr . '!' . $supplyqty_apr . '!' . $shp_apr . '!' . ($supplyqty_apr - $shp_apr) . '!' . $supplycbmqty_apr . '!' . $qty_may . '!' . $supplyqty_may . '!' . $shp_may . '!0!' . $supplycbmqty_may . '!' . $qty_jun . '!' . $supplyqty_jun . '!' . $shp_jun . '!0!' . $supplycbmqty_jun . '!' . $qty_q1 . '!' . $supplyqty_q1 . '!0!0!' . $supplycbmqty_q1
                        . '!' . $qty_jul . '!' . $supplyqty_jul . '!' . $shp_jul . '!0!' . $supplycbmqty_jul . '!' . $qty_aug . '!' . $supplyqty_aug . '!' . $shp_aug . '!0!' . $supplycbmqty_aug . '!' . $qty_sep . '!' . $supplyqty_sep . '!' . $shp_sep . '!0!' . $supplycbmqty_sep . '!' . $qty_q2 . '!' . $supplyqty_q2 . '!0!0!' . $supplycbmqty_q2
                        . '!' . $qty_oct . '!' . $supplyqty_oct . '!' . $shp_oct . '!0!' . $supplycbmqty_oct . '!' . $qty_nov . '!' . $supplyqty_nov . '!' . $shp_nov . '!0!' . $supplycbmqty_nov . '!' . $qty_dec . '!' . $supplyqty_dec . '!' . $shp_dec . '!0!' . $supplycbmqty_dec . '!' . $qty_q3 . '!' . $supplyqty_q3 . '!0!0!' . $supplycbmqty_q3
                        . '!' . $qty_jan . '!' . $supplyqty_jan . '!' . $shp_jan . '!0!0' . '!' . $qty_feb . '!' . $supplyqty_feb . '!' . $shp_feb . '!0!0' . '!' . $qty_mar . '!' . $supplyqty_mar . '!' . $shp_mar . '!0!0' . '!' . $qty_q4 . '!' . $supplyqty_q4 . '!0!0!' . $supplycbmqty_q4
                        . '!' . $qty_tot . '!' . $supplyqty_tot . '!0!0!' . $supplycbmqty_tot;

                $camount_row = '!' . $eachcat->product_category . ' - AMT!!!!!!' . Yii::app()->numberFormatter->formatCurrency($amt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_apr, "") . '!' . ($supplyamt_apr - $shpamt_apr) . '!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_may, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_jun, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_q1, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_q1, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_q1, "")
                        . '!' . Yii::app()->numberFormatter->formatCurrency($amt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_jul, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_aug, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_sep, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_q2, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_q2, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_q2, "")
                        . '!' . Yii::app()->numberFormatter->formatCurrency($amt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_oct, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_nov, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_dec, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($amt_q3, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_q3, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_q3, "")
                        . '!' . Yii::app()->numberFormatter->formatCurrency($amt_jan, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_jan, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_jan, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($amt_feb, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_feb, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_feb, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($amt_mar, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_mar, "") . '!' . Yii::app()->numberFormatter->formatCurrency($shpamt_mar, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($amt_q4, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_q4, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_q4, "")
                        . '!' . Yii::app()->numberFormatter->formatCurrency($amt_tot, "") . '!' . Yii::app()->numberFormatter->formatCurrency($supplyamt_tot, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($supplycbmamt_tot, "");

                $list1[$k++] = $cinv_row;

                $list2[$k++] = $camount_row;
                $j++;
            }
            $ctot_row = '!Total - Qty!!!!!!' . $totqty_apr . '!' . $totsupplyqty_apr . '!' . $totshp_apr . '!' . ($totsupplyqty_apr - $totshp_apr) . '!' . $totsupplycbmqty_apr . '!' . $totqty_may . '!' . $totsupplyqty_may . '!' . $totshp_may . '!0!' . $totsupplycbmqty_may . '!' . $totqty_jun . '!' . $totsupplyqty_jun . '!' . $totshp_jun . '!0!' . $totsupplycbmqty_jun . '!' . $totqty_q1 . '!' . $totsupplyqty_q1 . '!0!0!' . $totsupplycbmqty_q1
                    . '!' . $totqty_jul . '!' . $totsupplyqty_jul . '!' . $totshp_jul . '!0!' . $totsupplycbmqty_jul . '!' . $totqty_aug . '!' . $totsupplyqty_aug . '!' . $totshp_aug . '!0!' . $totsupplycbmqty_aug . '!' . $totqty_sep . '!' . $totsupplyqty_sep . '!' . $totshp_sep . '!0!' . $totsupplycbmqty_sep . '!' . $totqty_q2 . '!' . $totsupplyqty_q2 . '!0!0!' . $totsupplycbmqty_q2
                    . '!' . $totqty_oct . '!' . $totsupplyqty_oct . '!' . $totshp_oct . '!0!' . $totsupplycbmqty_oct . '!' . $totqty_nov . '!' . $totsupplyqty_nov . '!' . $totshp_nov . '!0!' . $totsupplycbmqty_nov . '!' . $totqty_dec . '!' . $totsupplyqty_dec . '!' . $totshp_dec . '!0!' . $totsupplycbmqty_dec . '!' . $totqty_q3 . '!' . $totsupplyqty_q3 . '!0!0!' . $totsupplycbmqty_q3
                    . '!' . $totqty_jan . '!' . $totsupplyqty_jan . '!' . $totshp_jan . '!0!0' . '!' . $totqty_feb . '!' . $totsupplyqty_feb . '!' . $totshp_feb . '!0!0' . '!' . $totqty_mar . '!' . $totsupplyqty_mar . '!' . $totshp_mar . '!0!0' . '!' . $totqty_q4 . '!' . $totsupplyqty_q4 . '!0!0!' . $totsupplycbmqty_q4
                    . '!' . $totqty_tot . '!' . $totsupplyqty_tot . '!0!0!' . $totsupplycbmqty_tot;

            $ctotamount_row = '!Total - AMT!!!!!!' . Yii::app()->numberFormatter->formatCurrency($totamt_apr, "") . ' !' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency(($totsupplyamt_apr - $totshpamt_apr), "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_apr, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_may, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_may, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_jun, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_jun, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_q1, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_q1, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_q1, "")
                    . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_jul, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_jul, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_aug, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_aug, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_sep, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_sep, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_q2, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_q2, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_q2, "")
                    . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_oct, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_oct, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_nov, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_nov, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_dec, "") . '!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_dec, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_q3, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_q3, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_q3, "")
                    . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_jan, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_jan, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_jan, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_feb, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_feb, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_feb, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_mar, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_mar, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totshpamt_mar, "") . '!0!0' . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_q4, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_q4, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_q4, "")
                    . '!' . Yii::app()->numberFormatter->formatCurrency($totamt_tot, "") . '!' . Yii::app()->numberFormatter->formatCurrency($totsupplyamt_tot, "") . '!0!0!' . Yii::app()->numberFormatter->formatCurrency($totsupplycbmamt_tot, "");

            $list1[$k++] = $ctot_row;
            $list2[$k++] = $ctotamount_row;
            $list = array_merge($list, $list2);
            $list = array_merge($list, $list1);

            $j++;

            foreach ($list as $line) {

                fputcsv($file, explode('!', $line));
            }

            fclose($file);

            $filename = 'upload/test.csv';

            $path = Yii::app()->getBaseUrl(true) . '/upload';

            header('Content-Type: application/csv');

            $file_name = 'Order_' . $mod_dis->distributer_name . '_' . $year . '.csv';

            header('Content-disposition: attachment; filename="' . $file_name . '"');

            readfile($filename);

            Yii::app()->end();
        } else {

            throw new CHttpException(404, 'Page not found.');
        }
    }

    /* public function actionDownloadOrderFormatExcel1() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $type = Yii::app()->request->getParam('type');

            $distributer_id = Yii::app()->request->getParam('distributer_id');

            $year = Yii::app()->request->getParam('year');

            $selyear = Yii::app()->request->getParam('year');
            $month = Yii::app()->request->getParam('month');

            $price_date = $year . '-' . $month . '-' . '01';

            $cnd = '(is_active=:active OR (is_active=:discontinue AND ((discontinuing_month>=:month AND discontinuing_year=:year) OR discontinuing_year>:year))) AND show_in_order="Y"';
            $cndcat = '(is_active="Y" OR (is_active="D" AND ((discontinuing_month>=' . (int) $month . ' AND discontinuing_year=' . (int) $year . ') OR discontinuing_year>' . (int) $year . '))) AND show_in_order="Y"';
            $arr_gmcs = [];
            if ((int) $distributer_id > 0) {
                $modHiddenDistributerGmcs = new HiddenDistributerGmcs();
                $arr_gmcs = $modHiddenDistributerGmcs->getHiddenGMCs((int) $distributer_id);
                if (!empty($arr_gmcs)) {
                    $cnd .= ' AND id NOT IN (' . implode(',', $arr_gmcs) . ')';
                    $cndcat .= ' AND id NOT IN (' . implode(',', $arr_gmcs) . ')';
                }
            }
            //echo $cnd;exit;

            $params = [
                ':active' => 'Y',
                ':discontinue' => 'D',
                ':month' => (int) $month,
                ':year' => (int) $year
            ];

            $model_inventory = Inventory::model()->findAll([
                'condition' => $cnd,
                'order' => 'product_category_id,gmc_sort_order asc',
                'params' => $params
            ]);
            $sql_category = "SELECT DISTINCT(product_category_id)
                        FROM {{inventory}} 
                        WHERE " . $cndcat;
            $file = fopen("upload/test.csv", "w");

            $j = 0;

            $mod_dis = Distributers::model()->with('region_det')->findByPk((int) $distributer_id);

            $region_id = (int) $mod_dis->region_id;

            $arr_months = Order::getmonths();
            $monthrows = ['4' => 'J', '5' => 'M', '6' => 'P', '7' => 'V', '8' => 'Y', '9' => 'AB', '10' => 'AH', '11' => 'AK', '12' => 'AN', '1' => 'AT', '2' => 'AW', '3' => 'AZ'];

            if ($month <= 3) {
                $year = $year - 1;
            }
            $arrInpMonth = [];
            for ($mon = 1; $mon <= 12; $mon++) {
                $arrInpMonth[$mon] = '';
            }

            function setData($worksheet, $row, $data) {
                foreach ($data as $column => $value) {
                    $cell = $column . $row;
                    $worksheet->setCellValue($cell, $value);
                }
            }

            function setFillColor($worksheet, $range, $color) {
                $worksheet->getStyle($range)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB($color);
            }

            $spreadsheet = new Spreadsheet();

            $activeWorksheet = $spreadsheet->getActiveSheet();
            $highestColumn = $activeWorksheet->getHighestColumn();
            if ((int) $month > 0) {
                $arrInpMonth[$month] = '                        Input your Order in this month';
            }
            $rowNumber = 1;
            $activeWorksheet->mergeCells('A1:CN1');

            $activeWorksheet->setCellValue('A1', 'FIXED ORDER FORM');
            $modelComments = new MasComments();
            $modelComment1 = $modelComments->findBYPK(1);
            $modelComment2 = $modelComments->findBYPK(2);
            $modelComment3 = $modelComments->findBYPK(3);
            $modelComment4 = $modelComments->findBYPK(4);
            $modelComment5 = $modelComments->findBYPK(5);
            $comment1 = $comment2 = $comment3 = $comment4 = $comment5 = '';
            if (!empty($modelComment1)) {
                $comment1 = $modelComment1->comments;
            }
            if (!empty($modelComment2)) {
                $comment2 = $modelComment2->comments;
            }
            if (!empty($modelComment3)) {
                $comment3 = $modelComment3->comments;
            }
            if (!empty($modelComment4)) {
                $comment4 = $modelComment4->comments;
            }
            if (!empty($modelComment5)) {
                $comment5 = $modelComment5->comments;
            }
            //comment1
            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
            $activeWorksheet->setCellValue("F{$rowNumber}", $comment1);
            //comment2
            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
            $activeWorksheet->setCellValue("F{$rowNumber}", $comment2);
            //comment3
            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
            $activeWorksheet->setCellValue("F{$rowNumber}", $comment3);
            //comment4
            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
            $activeWorksheet->setCellValue("F{$rowNumber}", $comment4);
            //comment5
            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
            $activeWorksheet->setCellValue("F{$rowNumber}", $comment5);

            $rowNumber = $rowNumber + 1;
            $activeWorksheet->mergeCells("A{$rowNumber}:B{$rowNumber}");

            // Set text wrap for cells 
            //In this code, the $cellsToWrap array contains the references to the cells that you want to set as text wrap. The loop iterates through the array and applies the text wrap setting to each of these cells individually.
            $cellsToWrap = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8", "M8", "P8", "S8", "V8", "Y8", "AB8", "AE8", "AH8", "AK8", "AN8", "AQ8", "AT8", "AW8", "AZ8", "BC8", "BF8", "J{$rowNumber}", "M{$rowNumber}", "P{$rowNumber}", "V{$rowNumber}", "Y{$rowNumber}", "AB{$rowNumber}", "AH{$rowNumber}", "AK{$rowNumber}", "AN{$rowNumber}", "AT{$rowNumber}", "AW{$rowNumber}", "AZ{$rowNumber}"];
            foreach ($cellsToWrap as $cellReference) {
                $cell = $activeWorksheet->getCell($cellReference);
                $cell->getStyle()->getAlignment()->setWrapText(true);
            }

            //You can adjust the $cellsToWrap array to include other cell references as needed, and you can also set additional formatting properties for these cells within the loop.
            // Create an arrow image and insert it into cell $ordered_month--start
            $ordered_month = $monthrows[$month] . $rowNumber;
            $imagePath = __DIR__ . '/../../../images/darrow.png'; // Adjust the path as needed
            $drawing = new Drawing();
            $drawing->setPath("$imagePath");
            $drawing->setDescription('Input your Order in this month');
            $drawing->setCoordinates($ordered_month);
            $drawing->setWorksheet($activeWorksheet);
            $drawing->setHeight(30); // Adjust the height as needed
            // Create an arrow image and insert it into cell $ordered_month--end
            // Set column width for columns J to BH --start
            for ($column = 'J'; $column <= 'BH'; $column++) {
                $activeWorksheet->getColumnDimension($column)->setWidth(11.09);
            }
            //GECI-column width
            $activeWorksheet->getColumnDimension('C')->setWidth(23.6);
            $activeWorksheet->getColumnDimension('E')->setWidth(42.3);
            $activeWorksheet->getColumnDimension('G')->setWidth(14.22);
            $activeWorksheet->getColumnDimension('I')->setWidth(18.5);
            // Set column width for columns J to BH --end
            $columnHeaders = [
                'A' => 'DISTRIBUTOR',
                'C' => $mod_dis->distributer_name,
                'J' => $arrInpMonth[4],
                'M' => $arrInpMonth[5],
                'P' => $arrInpMonth[6],
                'V' => $arrInpMonth[7],
                'Y' => $arrInpMonth[8],
                'AB' => $arrInpMonth[9],
                'AH' => $arrInpMonth[10],
                'AK' => $arrInpMonth[11],
                'AN' => $arrInpMonth[12],
                'AT' => $arrInpMonth[1],
                'AW' => $arrInpMonth[2],
                'AZ' => $arrInpMonth[3],
            ];

            setData($activeWorksheet, $rowNumber, $columnHeaders);

            $year1 = (int) $year + 1;
            $rowNumber = $rowNumber + 1;
            // The row number where you want to set these values

            $activeWorksheet->mergeCells("J{$rowNumber}:L{$rowNumber}");
            $activeWorksheet->mergeCells("M{$rowNumber}:O{$rowNumber}");
            $activeWorksheet->mergeCells("P{$rowNumber}:R{$rowNumber}");
            $activeWorksheet->mergeCells("S{$rowNumber}:U{$rowNumber}");
            $activeWorksheet->mergeCells("V{$rowNumber}:X{$rowNumber}");
            $activeWorksheet->mergeCells("Y{$rowNumber}:AA{$rowNumber}");
            $activeWorksheet->mergeCells("AB{$rowNumber}:AD{$rowNumber}");
            $activeWorksheet->mergeCells("AE{$rowNumber}:AG{$rowNumber}");
            $activeWorksheet->mergeCells("AH{$rowNumber}:AJ{$rowNumber}");
            $activeWorksheet->mergeCells("AK{$rowNumber}:AM{$rowNumber}");
            $activeWorksheet->mergeCells("AN{$rowNumber}:AP{$rowNumber}");
            $activeWorksheet->mergeCells("AQ{$rowNumber}:AS{$rowNumber}");
            $activeWorksheet->mergeCells("AT{$rowNumber}:AV{$rowNumber}");
            $activeWorksheet->mergeCells("AW{$rowNumber}:AY{$rowNumber}");
            $activeWorksheet->mergeCells("AZ{$rowNumber}:BB{$rowNumber}");
            $activeWorksheet->mergeCells("BC{$rowNumber}:BE{$rowNumber}");
            $activeWorksheet->mergeCells("BF{$rowNumber}:BH{$rowNumber}");
            $activeWorksheet->getStyle("{$monthrows[4]}{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("M{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "MAY 2024"
            $activeWorksheet->getStyle("P{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "JUn 2024"
            $activeWorksheet->getStyle("V{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Jul 2024"
            $activeWorksheet->getStyle("Y{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Aug 2024"
            $activeWorksheet->getStyle("AB{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Sep 2024"

            $activeWorksheet->getStyle("AH{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("AK{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("AN{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("AT{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("AW{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"
            $activeWorksheet->getStyle("AZ{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy'); // Format as "Apr 2024"

            $columnHeaders = [
                'C' => 'PRODUCT CATEGORY',
                'D' => 'GMC',
                'E' => 'Model',
                'F' => 'YMGF Price (' . $mod_dis->region_det->currency_type . ')',
                'G' => 'Supply confirmation month',
                'H' => 'CBM',
                'I' => 'BASE CODE',
                'J' => "=DATE({$year},4,1)",
                'M' => "=DATE({$year},5,1)",
                'P' => "=DATE({$year},6,1)",
                'S' => '1Q TOTAL',
                'V' => "=DATE({$year},7,1)",
                'Y' => "=DATE({$year},8,1)",
                'AB' => "=DATE({$year},9,1)",
                'AE' => '2Q TOTAL',
                'AH' => "=DATE({$year},10,1)",
                'AK' => "=DATE({$year},11,1)",
                'AN' => "=DATE({$year},12,1)",
                'AQ' => '3Q TOTAL',
                'AT' => "=DATE({$year1},1,1)",
                'AW' => "=DATE({$year1},2,1)",
                'AZ' => "=DATE({$year1},3,1)",
                'BC' => '4Q TOTAL',
                'BF' => 'TOTAL ' . $year . ' - ' . $year1,
            ];

            // Hide the "I" column (Base Code)--start
            $activeWorksheet->getColumnDimension('I')->setVisible(false);
            // Hide the "I" column (Base Code)--end
            // Set text wrap and alignment for row number 8
            $cellsToWrapAndCenter = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8", "M8", "P8", "S8", "V8", "Y8", "AB8", "AE8", "AH8", "AK8", "AN8", "AQ8", "AT8", "AW8", "AZ8", "BC8", "BF8"];

            foreach ($cellsToWrapAndCenter as $cellReference) {
                $cell = $activeWorksheet->getCell($cellReference);
                $cell->getStyle()->getAlignment()->setWrapText(true);
                $cell->getStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
            }
            // Set text wrap and alignment for row number 8
            setData($activeWorksheet, $rowNumber, $columnHeaders);

            //bold total
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            // Freeze the pane at cell J10
            $activeWorksheet->freezePane('J10');
            $rowNumber = $rowNumber = $rowNumber + 1; // The row number where you want to set these values
            $arr_supply_columns_colour = [];
            $columnHeaders = [
                'A' => ' ',
                'B' => ' ',
                'C' => ' ',
                'D' => ' ',
                'E' => ' ',
                'F' => ' ',
                'G' => ' ',
                'H' => ' ',
                'I' => ' ',
                'J' => 'ORDER',
                'K' => 'Supply Confirmation',
                'L' => 'SHIP',
                'M' => 'ORDER',
                'N' => 'Supply Confirmation',
                'O' => 'SHIP',
                'P' => 'ORDER',
                'Q' => 'Supply Confirmation',
                'R' => 'SHIP',
                'S' => 'ORDER',
                'T' => 'Supply Confirmation',
                'U' => 'SHIP',
                'V' => 'ORDER',
                'W' => 'Supply Confirmation',
                'X' => 'SHIP',
                'Y' => 'ORDER',
                'Z' => 'Supply Confirmation',
                'AA' => 'SHIP',
                'AB' => 'ORDER',
                'AC' => 'Supply Confirmation',
                'AD' => 'SHIP',
                'AE' => 'ORDER',
                'AF' => 'Supply Confirmation',
                'AG' => 'SHIP',
                'AH' => 'ORDER',
                'AI' => 'Supply Confirmation',
                'AJ' => 'SHIP',
                'AK' => 'ORDER',
                'AL' => 'Supply Confirmation',
                'AM' => 'SHIP',
                'AN' => 'ORDER',
                'AO' => 'Supply Confirmation',
                'AP' => 'SHIP',
                'AQ' => 'ORDER',
                'AR' => 'Supply Confirmation',
                'AS' => 'SHIP',
                'AT' => 'ORDER',
                'AU' => 'Supply Confirmation',
                'AV' => 'SHIP',
                'AW' => 'ORDER',
                'AX' => 'Supply Confirmation',
                'AY' => 'SHIP',
                'AZ' => 'ORDER',
                'BA' => 'Supply Confirmation',
                'BB' => 'SHIP',
                'BC' => 'ORDER',
                'BD' => 'Supply Confirmation',
                'BE' => 'SHIP',
                'BF' => 'ORDER',
                'BG' => 'Supply Confirmation',
                'BH' => 'SHIP',
            ];

            setData($activeWorksheet, $rowNumber, $columnHeaders);
            // Loop through each cell in the range "A9:BH9" and set wrap text
            for ($column = 'A'; $column <= 'BH'; $column++) {
                $cell = $column . '9';
                $activeWorksheet->getStyle($cell)->getAlignment()->setWrapText(true);
            }
            //category_id
            $sql_inv = "SELECT product_id,distributer_id,parent_det.month,parent_det.year,quantity_ordered,quantity_confirmed,quantity_shipped,is_done
                        FROM {{order_details}} OD
                        LEFT JOIN {{order}} parent_det ON OD.order_id=parent_det.id
                        WHERE parent_det.distributer_id =$distributer_id AND ((parent_det.month>3 AND parent_det.year=$year )OR(parent_det.month<4 AND parent_det.year=$year1))
                        GROUP BY OD.product_id,parent_det.month,parent_det.year,parent_det.distributer_id";

            $arr_inv = Yii::app()->db->createCommand($sql_inv)->queryAll();

            $arr_order = [];

            foreach ($arr_inv as $each) {

                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_ordered'] = $each['quantity_ordered'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_confirmed'] = $each['quantity_confirmed'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_shipped'] = $each['quantity_shipped'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['is_done'] = $each['is_done'];
            }

            $rowNumber = $rowNumber + 1;
            $i = 0;
            $modelOrder = new Order();
            $arr_months = $modelOrder->getmonths();
            $old_category_id = 0;
            $arr_cat = [];
            $dis_range = [];
            foreach ($model_inventory as $i => $row) {

                $goodend_month_name = isset($arr_months[(int) $row['goods_in_end_month']]) ? $arr_months[(int) $row['goods_in_end_month']] : '';
                $goodend_month = $goodend_month_name . ' - ' . (int) $row->goods_in_end_year;
                $goods_in_date = (int) $row->goods_in_end_year . '-' . str_pad((int) $row['goods_in_end_month'], 2, '0', STR_PAD_LEFT) . '-01';

                // Convert $goods_in_date to a DateTime object
                $goods_in_datetime = new DateTime($goods_in_date);

                // Subtract 1 month from $goods_in_datetime
                $supply_confirm_datetime = $goods_in_datetime->sub(new DateInterval('P1M'));

                // Format $supply_confirm_month
                $supply_confirm_month = $supply_confirm_datetime->format('F - Y');

                //ordered//
                $qty_apr = isset($arr_order[(int) $row['id']][$year][4]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][4]['quantity_ordered'] : 0;
                $qty_may = isset($arr_order[(int) $row['id']][$year][5]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][5]['quantity_ordered'] : 0;
                $qty_jun = isset($arr_order[(int) $row['id']][$year][6]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][6]['quantity_ordered'] : 0;
                $qty_jul = isset($arr_order[(int) $row['id']][$year][7]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][7]['quantity_ordered'] : 0;
                $qty_aug = isset($arr_order[(int) $row['id']][$year][8]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][8]['quantity_ordered'] : 0;
                $qty_sep = isset($arr_order[(int) $row['id']][$year][9]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][9]['quantity_ordered'] : 0;
                $qty_oct = isset($arr_order[(int) $row['id']][$year][10]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][10]['quantity_ordered'] : 0;
                $qty_nov = isset($arr_order[(int) $row['id']][$year][11]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][11]['quantity_ordered'] : 0;
                $qty_dec = isset($arr_order[(int) $row['id']][$year][12]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year][12]['quantity_ordered'] : 0;
                $qty_jan = isset($arr_order[(int) $row['id']][$year1][1]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year1][1]['quantity_ordered'] : 0;
                $qty_feb = isset($arr_order[(int) $row['id']][$year1][2]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year1][2]['quantity_ordered'] : 0;
                $qty_mar = isset($arr_order[(int) $row['id']][$year1][3]['quantity_ordered']) ? (int) $arr_order[(int) $row['id']][$year1][3]['quantity_ordered'] : 0;

                //supplyconfirmation//
                //supplyconfirmation//

                $dsupplyqty_apr = '';
                $dsupplyqty_may = '';
                $dsupplyqty_jun = '';
                $dsupplyqty_jul = '';
                $dsupplyqty_aug = '';
                $dsupplyqty_sep = '';
                $dsupplyqty_oct = '';
                $dsupplyqty_nov = '';
                $dsupplyqty_dec = '';
                $dsupplyqty_jan = '';
                $dsupplyqty_feb = '';
                $dsupplyqty_mar = '';

                $supplyqty_apr = (isset($arr_order[(int) $row['id']][$year][4]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][4]['quantity_confirmed'] : 0;
                $supplyqty_may = (isset($arr_order[(int) $row['id']][$year][5]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][5]['quantity_confirmed'] : 0;
                $supplyqty_jun = (isset($arr_order[(int) $row['id']][$year][6]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][6]['quantity_confirmed'] : 0;
                $supplyqty_jul = (isset($arr_order[(int) $row['id']][$year][7]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][7]['quantity_confirmed'] : 0;
                $supplyqty_aug = (isset($arr_order[(int) $row['id']][$year][8]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][8]['quantity_confirmed'] : 0;
                $supplyqty_sep = (isset($arr_order[(int) $row['id']][$year][9]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][9]['quantity_confirmed'] : 0;
                $supplyqty_oct = (isset($arr_order[(int) $row['id']][$year][10]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][10]['quantity_confirmed'] : 0;
                $supplyqty_nov = (isset($arr_order[(int) $row['id']][$year][11]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][11]['quantity_confirmed'] : 0;
                $supplyqty_dec = (isset($arr_order[(int) $row['id']][$year][12]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year][12]['quantity_confirmed'] : 0;
                $supplyqty_jan = (isset($arr_order[(int) $row['id']][$year1][1]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year1][1]['quantity_confirmed'] : 0;
                $supplyqty_feb = (isset($arr_order[(int) $row['id']][$year1][2]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year1][2]['quantity_confirmed'] : 0;
                $supplyqty_mar = (isset($arr_order[(int) $row['id']][$year1][3]['quantity_confirmed'])) ? (int) $arr_order[(int) $row['id']][$year1][3]['quantity_confirmed'] : 0;

                if ($arr_order[(int) $row['id']][$year][4]['is_done'] == "Y") {
                    $dsupplyqty_apr = $supplyqty_apr;
                }
                if ($arr_order[(int) $row['id']][$year][5]['is_done'] == "Y") {
                    $dsupplyqty_may = $supplyqty_may;
                    
                }
                if ($arr_order[(int) $row['id']][$year][6]['is_done'] == "Y") {
                    $dsupplyqty_jun = $supplyqty_jun;
                    
                }
                if ($arr_order[(int) $row['id']][$year][7]['is_done'] == "Y") {
                    $dsupplyqty_jul = $supplyqty_jul;
                    
                }
                if ($arr_order[(int) $row['id']][$year][8]['is_done'] == "Y") {
                    $dsupplyqty_aug = $supplyqty_aug;
                   
                }
                if ($arr_order[(int) $row['id']][$year][9]['is_done'] == "Y") {
                    $dsupplyqty_sep = $supplyqty_sep;
                    
                }
                if ($arr_order[(int) $row['id']][$year][10]['is_done'] == "Y") {
                    $dsupplyqty_oct = $supplyqty_oct;
                   
                }
                if ($arr_order[(int) $row['id']][$year][11]['is_done'] == "Y") {
                    $dsupplyqty_nov = $supplyqty_nov;
                  
                }
                if ($arr_order[(int) $row['id']][$year][12]['is_done'] == "Y") {
                    $dsupplyqty_dec = $supplyqty_dec;
                   
                }
                if ($arr_order[(int) $row['id']][$year1][1]['is_done'] == "Y") {
                    $dsupplyqty_jan = $supplyqty_jan;
                   
                }
                if ($arr_order[(int) $row['id']][$year1][2]['is_done'] == "Y") {
                    $dsupplyqty_feb = $supplyqty_feb;
                   
                }
                if ($arr_order[(int) $row['id']][$year1][3]['is_done'] == "Y") {
                    $dsupplyqty_mar = $supplyqty_mar;
                    
                }
                //shipped cbm//
                $shp_apr = isset($arr_order[(int) $row['id']][$year][4]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][4]['quantity_shipped'] : 0;
                $shp_may = isset($arr_order[(int) $row['id']][$year][5]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][5]['quantity_shipped'] : 0;
                $shp_jun = isset($arr_order[(int) $row['id']][$year][6]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][6]['quantity_shipped'] : 0;
                $shp_jul = isset($arr_order[(int) $row['id']][$year][7]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][7]['quantity_shipped'] : 0;
                $shp_aug = isset($arr_order[(int) $row['id']][$year][8]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][8]['quantity_shipped'] : 0;
                $shp_sep = isset($arr_order[(int) $row['id']][$year][9]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][9]['quantity_shipped'] : 0;
                $shp_oct = isset($arr_order[(int) $row['id']][$year][10]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][10]['quantity_shipped'] : 0;
                $shp_nov = isset($arr_order[(int) $row['id']][$year][11]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][11]['quantity_shipped'] : 0;
                $shp_dec = isset($arr_order[(int) $row['id']][$year][12]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year][12]['quantity_shipped'] : 0;
                $shp_jan = isset($arr_order[(int) $row['id']][$year1][1]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year1][1]['quantity_shipped'] : 0;
                $shp_feb = isset($arr_order[(int) $row['id']][$year1][2]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year1][2]['quantity_shipped'] : 0;
                $shp_mar = isset($arr_order[(int) $row['id']][$year1][3]['quantity_shipped']) ? (int) $arr_order[(int) $row['id']][$year1][3]['quantity_shipped'] : 0;

                $for_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $for_q1_sc = "=IFERROR(K{$rowNumber}+N{$rowNumber}+Q{$rowNumber},0)";
                $for_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";
                $for_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $for_q2_sc = "=IFERROR(W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber},0)";
                $for_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";
                $for_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $for_q3_sc = "=IFERROR(AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber},0)";
                $for_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";
                $for_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $for_q4_sc = "=IFERROR(AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber},0)";
                $for_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";
                $for_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $for_tot_sc = "=IFERROR(T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber},0)";
                $for_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                //for categorywise--sum --start
                if ($old_category_id != $row['product_category_id']) {
                    $arr_cat[$row['product_category_id']]['start'] = $rowNumber;
                    $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
                    $old_category_id = $row['product_category_id'];
                }
                //for categorywise--sum --end
                // region value

                $price_cnd = 'product_id=' . (int) $row['id'] . ' AND region_id=' . (int) $region_id . ' AND from_date <="' . $price_date . '" AND to_date >="' . $price_date . '"';
                $mod_inv_price = InventoryPrice::model()->find(array('condition' => $price_cnd));
                $ymg_price = !empty($mod_inv_price) ? $mod_inv_price['price'] : '00" - (TBA)"';

                // region value
                $new_discontinue = Inventory::model()->getinvtype((int) $row['id'], $month, $selyear);
                $pair_name = ($row['gmc_type'] == 'P') ? 'PAIR' : (($row['gmc_type'] == 'K') ? 'KIT' : '');
                $columnHeaders = [
                    'A' => $new_discontinue,
                    'B' => $pair_name,
                    'C' => $row['category_det']['product_category'],
                    'D' => $row['gmc_code'],
                    'E' => $row['model_no'],
                    'F' => $ymg_price,
                    'G' => $supply_confirm_month,
                    'H' => $row['cbm'],
                    'I' => $row['base_code'],
                    'J' => $qty_apr,
                    'K' => $dsupplyqty_apr,
                    'L' => $shp_apr,
                    'M' => $qty_may,
                    'N' => $dsupplyqty_may,
                    'O' => $shp_may,
                    'P' => $qty_jun,
                    'Q' => $dsupplyqty_jun,
                    'R' => $shp_jun,
                    'S' => $for_q1_o,
                    'T' => $for_q1_sc,
                    'U' => $for_q1_shp,
                    'V' => $qty_jul,
                    'W' => $dsupplyqty_jul,
                    'X' => $shp_jul,
                    'Y' => $qty_aug,
                    'Z' => $dsupplyqty_aug,
                    'AA' => $shp_aug,
                    'AB' => $qty_sep,
                    'AC' => $dsupplyqty_sep,
                    'AD' => $shp_sep,
                    'AE' => $for_q2_o,
                    'AF' => $for_q2_sc,
                    'AG' => $for_q2_shp,
                    'AH' => $qty_oct,
                    'AI' => $dsupplyqty_oct,
                    'AJ' => $shp_oct,
                    'AK' => $qty_nov,
                    'AL' => $dsupplyqty_nov,
                    'AM' => $shp_nov,
                    'AN' => $qty_dec,
                    'AO' => $dsupplyqty_dec,
                    'AP' => $shp_dec,
                    'AQ' => $for_q3_o,
                    'AR' => $for_q3_sc,
                    'AS' => $for_q3_shp,
                    'AT' => $qty_jan,
                    'AU' => $dsupplyqty_jan,
                    'AV' => $shp_jan,
                    'AW' => $qty_feb,
                    'AX' => $dsupplyqty_feb,
                    'AY' => $shp_feb,
                    'AZ' => $qty_mar,
                    'BA' => $dsupplyqty_mar,
                    'BB' => $shp_mar,
                    'BC' => $for_q4_o,
                    'BD' => $for_q4_sc,
                    'BE' => $for_q4_shp,
                    'BF' => $for_tot_o,
                    'BG' => $for_tot_sc,
                    'BH' => $for_tot_shp,
                ];
                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //discontinuing gmc-- colour grey----start
                if ($row['is_active'] == 'D') {
                    $dis_range[] = "{$monthrows[$row['discontinuing_month']]}{$rowNumber}:BH{$rowNumber}";
                    //setFillColor($activeWorksheet, $dis_range, 'b1b1b1'); // Quarter 1
                }
                //discontinuing gmc-- colour grey----end
                $rowNumber++;
            }

            
            $lastpdtrow = $rowNumber - 1;

            if (!empty($dis_range)) {
                foreach ($dis_range as $each_discontinue) {
                    setFillColor($activeWorksheet, $each_discontinue, 'b1b1b1');
                }
            }
            $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
            $rowNumber++;
            $columnHeaders = [
                'C' => 'PRODUCT CATEGORY',
                'I' => 'BASE CODE',
                'J' => " ",
                'M' => " ",
                'P' => " ",
                'S' => '1Q TOTAL',
                'V' => " ",
                'Y' => " ",
                'AB' => " ",
                'AE' => '2Q TOTAL',
                'AH' => " ",
                'AK' => " ",
                'AN' => " ",
                'AQ' => '3Q TOTAL',
                'AT' => " ",
                'AW' => " ",
                'AZ' => " ",
                'BC' => '4Q TOTAL',
                'BF' => 'TOTAL ' . $year . ' - ' . $year1,
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $rowNumber++;

            $modelmascategories = MasProductCategory::model()->findAll(array('condition' => 'id IN (' . $sql_category . ')', 'order' => 'sort_order ASC'));
            $start_row_amt = $rowNumber;
            $start_row = $rowNumber;
            // Define months as an array
            $cat_months = ['apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar'];

            foreach ($modelmascategories as $eachcat) {
                if (isset($arr_cat[(int) $eachcat['id']])) {
                    // The element exists in the array
                    $start = $arr_cat[(int) $eachcat['id']]['start'];
                    $end = $arr_cat[(int) $eachcat['id']]['end'];
                } else {
                    // The element does not exist in the array
                    // to avoid circular reference
                    $start = 1;
                    $end = 1;
                }

                $camt_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},J{$start}:J{$end}),0)";
                $camt_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},M{$start}:M{$end}),0)";
                $camt_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},P{$start}:P{$end}),0)";
                $camt_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},V{$start}:V{$end}),0)";
                $camt_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Y{$start}:Y{$end}),0)";
                $camt_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AB{$start}:AB{$end}),0)";
                $camt_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AH{$start}:AH{$end}),0)";
                $camt_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AK{$start}:AK{$end}),0)";
                $camt_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AN{$start}:AN{$end}),0)";
                $camt_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AT{$start}:AT{$end}),0)";
                $camt_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AW{$start}:AW{$end}),0)";
                $camt_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AZ{$start}:AZ{$end}),0)";

                //supplyconfirmed

                $csupplyamt_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},K{$start}:K{$end}),0)";
                $csupplyamt_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},N{$start}:N{$end}),0)";
                $csupplyamt_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Q{$start}:Q{$end}),0)";
                $csupplyamt_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},W{$start}:W{$end}),0)";
                $csupplyamt_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Z{$start}:Z{$end}),0)";
                $csupplyamt_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AC{$start}:AC{$end}),0)";
                $csupplyamt_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AI{$start}:AI{$end}),0)";
                $csupplyamt_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AL{$start}:AL{$end}),0)";
                $csupplyamt_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AO{$start}:AO{$end}),0)";
                $csupplyamt_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AU{$start}:AU{$end}),0)";
                $csupplyamt_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AX{$start}:AX{$end}),0)";
                $csupplyamt_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},BA{$start}:BA{$end}),0)";

                //shipped
                $cshp_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},L{$start}:L{$end}),0)";
                $cshp_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},O{$start}:O{$end}),0)";
                $cshp_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},R{$start}:R{$end}),0)";
                $cshp_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},X{$start}:X{$end}),0)";
                $cshp_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AA{$start}:AA{$end}),0)";
                $cshp_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AD{$start}:AD{$end}),0)";
                $cshp_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AJ{$start}:AJ{$end}),0)";
                $cshp_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AM{$start}:AM{$end}),0)";
                $cshp_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AP{$start}:AP{$end}),0)";
                $cshp_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AV{$start}:AV{$end}),0)";
                $cshp_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AY{$start}:AY{$end}),0)";
                $cshp_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},BB{$start}:BB{$end}),0)";

                //formulas for category
                $cfor_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $cfor_q1_sc = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $cfor_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";

                $cfor_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $cfor_q2_sc = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $cfor_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";

                $cfor_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $cfor_q3_sc = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $cfor_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";

                $cfor_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $cfor_q4_sc = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $cfor_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";

                $cfor_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $cfor_tot_sc = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $cfor_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - AMT',
                    'J' => $camt_apr,
                    'K' => $csupplyamt_apr,
                    'L' => $cshp_apr,
                    'M' => $camt_may,
                    'N' => $csupplyamt_may,
                    'O' => $cshp_may,
                    'P' => $camt_jun,
                    'Q' => $csupplyamt_jun,
                    'R' => $cshp_jun,
                    'S' => $cfor_q1_o,
                    'T' => $cfor_q1_sc,
                    'U' => $cfor_q1_shp,
                    'V' => $camt_jul,
                    'W' => $csupplyamt_jul,
                    'X' => $cshp_jul,
                    'Y' => $camt_aug,
                    'Z' => $csupplyamt_aug,
                    'AA' => $cshp_aug,
                    'AB' => $camt_sep,
                    'AC' => $csupplyamt_sep,
                    'AD' => $cshp_sep,
                    'AE' => $cfor_q2_o,
                    'AF' => $cfor_q2_sc,
                    'AG' => $cfor_q2_shp,
                    'AH' => $camt_oct,
                    'AI' => $csupplyamt_oct,
                    'AJ' => $cshp_oct,
                    'AK' => $camt_nov,
                    'AL' => $csupplyamt_nov,
                    'AM' => $cshp_nov,
                    'AN' => $camt_dec,
                    'AO' => $csupplyamt_dec,
                    'AP' => $cshp_dec,
                    'AQ' => $cfor_q3_o,
                    'AR' => $cfor_q3_sc,
                    'AS' => $cfor_q3_shp,
                    'AT' => $camt_jan,
                    'AU' => $csupplyamt_jan,
                    'AV' => $cshp_jan,
                    'AW' => $camt_feb,
                    'AX' => $csupplyamt_feb,
                    'AY' => $cshp_feb,
                    'AZ' => $camt_mar,
                    'BA' => $csupplyamt_mar,
                    'BB' => $cshp_mar,
                    'BC' => $cfor_q4_o,
                    'BD' => $cfor_q4_sc,
                    'BE' => $cfor_q4_shp,
                    'BF' => $cfor_tot_o,
                    'BG' => $cfor_tot_sc,
                    'BH' => $cfor_tot_shp,
                ];

                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //formula for total
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_amt = $rowNumber;

            $tot_qty_apr_formula = "=IFERROR(SUM(J{$start_row}:J{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";

            $tot_qty_may_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";

            $tot_qty_jun_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";

            $tot_qty_q1_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";

            $tot_qty_jul_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";

            $tot_qty_aug_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";

            $tot_qty_sep_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";

            $tot_qty_q2_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";

            $tot_qty_oct_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";

            $tot_qty_nov_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";

            $tot_qty_dec_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";

            $tot_qty_q3_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";

            $tot_qty_jan_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";

            $tot_qty_feb_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";

            $tot_qty_mar_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";

            $tot_qty_q4_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";

            $tot_qty_tq_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";
            // Set the entire total row as bold

            $columnHeaders = [
                'C' => 'Total - Amt',
                'J' => $tot_qty_apr_formula,
                'K' => $tot_sc_apr_formula,
                'L' => $tot_shp_apr_formula,
                'M' => $tot_qty_may_formula,
                'N' => $tot_sc_may_formula,
                'O' => $tot_shp_may_formula,
                'P' => $tot_qty_jun_formula,
                'Q' => $tot_sc_jun_formula,
                'R' => $tot_shp_jun_formula,
                'S' => $tot_qty_q1_formula,
                'T' => $tot_sc_q1_formula,
                'U' => $tot_shp_q1_formula,
                'V' => $tot_qty_jul_formula,
                'W' => $tot_sc_jul_formula,
                'X' => $tot_shp_jul_formula,
                'Y' => $tot_qty_aug_formula,
                'Z' => $tot_sc_aug_formula,
                'AA' => $tot_shp_aug_formula,
                'AB' => $tot_qty_sep_formula,
                'AC' => $tot_sc_sep_formula,
                'AD' => $tot_shp_sep_formula,
                'AE' => $tot_qty_q2_formula,
                'AF' => $tot_sc_q2_formula,
                'AG' => $tot_shp_q2_formula,
                'AH' => $tot_qty_oct_formula,
                'AI' => $tot_sc_oct_formula,
                'AJ' => $tot_shp_oct_formula,
                'AK' => $tot_qty_nov_formula,
                'AL' => $tot_sc_nov_formula,
                'AM' => $tot_shp_nov_formula,
                'AN' => $tot_qty_dec_formula,
                'AO' => $tot_sc_dec_formula,
                'AP' => $tot_shp_dec_formula,
                'AQ' => $tot_qty_q3_formula,
                'AR' => $tot_sc_q3_formula,
                'AS' => $tot_shp_q3_formula,
                'AT' => $tot_qty_jan_formula,
                'AU' => $tot_sc_jan_formula,
                'AV' => $tot_shp_jan_formula,
                'AW' => $tot_qty_feb_formula,
                'AX' => $tot_sc_feb_formula,
                'AY' => $tot_shp_feb_formula,
                'AZ' => $tot_qty_mar_formula,
                'AA' => $tot_sc_mar_formula,
                'BB' => $tot_shp_mar_formula,
                'BC' => $tot_qty_q4_formula,
                'BD' => $tot_sc_q4_formula,
                'BE' => $tot_shp_q4_formula,
                'BF' => $tot_qty_tq_formula,
                'BG' => $tot_sc_tq_formula,
                'BH' => $tot_shp_tq_formula,
            ];
            $tot_rowno = $rowNumber;
            setData($activeWorksheet, $rowNumber, $columnHeaders);

            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //cbmTotal

            $columnHeaders = [
                'C' => 'CBM Total',
                'J' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},J10:J{$lastpdtrow}),0)",
                'K' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},K10:K{$lastpdtrow}),0)",
                'L' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},L10:L{$lastpdtrow}),0)",
                'M' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},M10:M{$lastpdtrow}),0)",
                'N' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},N10:N{$lastpdtrow}),0)",
                'O' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},O10:O{$lastpdtrow}),0)",
                'P' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},P10:P{$lastpdtrow}),0)",
                'Q' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Q10:Q{$lastpdtrow}),0)",
                'R' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},R10:R{$lastpdtrow}),0)",
                'S' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},S10:S{$lastpdtrow}),0)",
                'T' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},T10:T{$lastpdtrow}),0)",
                'U' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},U10:U{$lastpdtrow}),0)",
                'V' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},V10:V{$lastpdtrow}),0)",
                'W' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},W10:W{$lastpdtrow}),0)",
                'X' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},X10:X{$lastpdtrow}),0)",
                'Y' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Y10:Y{$lastpdtrow}),0)",
                'Z' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Z10:Z{$lastpdtrow}),0)",
                'AA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AA10:AA{$lastpdtrow}),0)",
                'AB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AB10:AB{$lastpdtrow}),0)",
                'AC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AC10:AC{$lastpdtrow}),0)",
                'AD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AD10:A{$lastpdtrow}),0)",
                'AE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AE10:AE{$lastpdtrow}),0)",
                'AF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AF10:AF{$lastpdtrow}),0)",
                'AG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AG10:AG{$lastpdtrow}),0)",
                'AH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AH10:AH{$lastpdtrow}),0)",
                'AI' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AI10:AI{$lastpdtrow}),0)",
                'AJ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AJ10:AJ{$lastpdtrow}),0)",
                'AK' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AK10:AK{$lastpdtrow}),0)",
                'AL' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AL10:AL{$lastpdtrow}),0)",
                'AM' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AM10:AM{$lastpdtrow}),0)",
                'AN' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AN10:AN{$lastpdtrow}),0)",
                'AO' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AO10:AO{$lastpdtrow}),0)",
                'AP' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AP10:AP{$lastpdtrow}),0)",
                'AQ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AQ10:AQ{$lastpdtrow}),0)",
                'AR' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AR10:AR{$lastpdtrow}),0)",
                'AS' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AS10:AS{$lastpdtrow}),0)",
                'AT' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AT10:AT{$lastpdtrow}),0)",
                'AU' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AU10:AU{$lastpdtrow}),0)",
                'AV' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AV10:AV{$lastpdtrow}),0)",
                'AW' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AW10:AW{$lastpdtrow}),0)",
                'AX' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AX10:AX{$lastpdtrow}),0)",
                'AY' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AY10:AY{$lastpdtrow}),0)",
                'AZ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AZ10:AZ{$lastpdtrow}),0)",
                'BA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BA10:BA{$lastpdtrow}),0)",
                'BB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BB10:BB{$lastpdtrow}),0)",
                'BC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BC10:BC{$lastpdtrow}),0)",
                'BD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BD10:BD{$lastpdtrow}),0)",
                'BE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BE10:BE{$lastpdtrow}),0)",
                'BF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BF10:BF{$lastpdtrow}),0)",
                'BG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BG10:BG{$lastpdtrow}),0)",
                'BH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BH10:BH{$lastpdtrow}),0)",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //Difference
            $columnHeaders = [
                'C' => 'Difference',
                'L' => "=L{$tot_rowno}-K{$tot_rowno}",
                'O' => "=O{$tot_rowno}-N{$tot_rowno}",
                'R' => "=R{$tot_rowno}-Q{$tot_rowno}",
                'U' => "=U{$tot_rowno}-T{$tot_rowno}",
                'X' => "=X{$tot_rowno}-W{$tot_rowno}",
                'AA' => "=AA{$tot_rowno}-Z{$tot_rowno}",
                'AD' => "=AD{$tot_rowno}-AC{$tot_rowno}",
                'AG' => "=AG{$tot_rowno}-AF{$tot_rowno}",
                'AJ' => "=AJ{$tot_rowno}-AI{$tot_rowno}",
                'AM' => "=AM{$tot_rowno}-AL{$tot_rowno}",
                'AP' => "=AP{$tot_rowno}-AO{$tot_rowno}",
                'AS' => "=AS{$tot_rowno}-AR{$tot_rowno}",
                'AV' => "=AV{$tot_rowno}-AU{$tot_rowno}",
                'AY' => "=AY{$tot_rowno}-AX{$tot_rowno}",
                'BB' => "=BB{$tot_rowno}-BA{$tot_rowno}",
                'BE' => "=BE{$tot_rowno}-BD{$tot_rowno}",
                'BH' => "=BH{$tot_rowno}-BG{$tot_rowno}",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;

            //quantity
            $start_row_qty = $rowNumber;
            $start_row = $rowNumber;
            foreach ($modelmascategories as $eachcat) {
                if (isset($arr_cat[(int) $eachcat['id']])) {
                    // The element exists in the array
                    $start = $arr_cat[(int) $eachcat['id']]['start'];
                    $end = $arr_cat[(int) $eachcat['id']]['end'];
                } else {
                    // The element does not exist in the array
                    // to avoid circular reference
                    $start = 1;
                    $end = 1;
                }

                //ordered//
                $cqty_apr = "=IFERROR(SUM(J{$start}:J{$end}),0)";
                $cqty_may = "=IFERROR(SUM(M{$start}:M{$end}),0)";
                $cqty_jun = "=IFERROR(SUM(P{$start}:P{$end}),0)";
                $cqty_jul = "=IFERROR(SUM(V{$start}:V{$end}),0)";
                $cqty_aug = "=IFERROR(SUM(Y{$start}:Y{$end}),0)";
                $cqty_sep = "=IFERROR(SUM(AB{$start}:AB{$end}),0)";
                $cqty_oct = "=IFERROR(SUM(AH{$start}:AH{$end}),0)";
                $cqty_nov = "=IFERROR(SUM(AK{$start}:AK{$end}),0)";
                $cqty_dec = "=IFERROR(SUM(AN{$start}:AN{$end}),0)";
                $cqty_jan = "=IFERROR(SUM(AT{$start}:AT{$end}),0)";
                $cqty_feb = "=IFERROR(SUM(AW{$start}:AW{$end}),0)";
                $cqty_mar = "=IFERROR(SUM(AZ{$start}:AZ{$end}),0)";

                //supplyconfirmed

                $csupplyqty_apr = "=IFERROR(SUM(K{$start}:K{$end}),0)";
                $csupplyqty_may = "=IFERROR(SUM(N{$start}:N{$end}),0)";
                $csupplyqty_jun = "=IFERROR(SUM(Q{$start}:Q{$end}),0)";
                $csupplyqty_jul = "=IFERROR(SUM(W{$start}:W{$end}),0)";
                $csupplyqty_aug = "=IFERROR(SUM(Z{$start}:Z{$end}),0)";
                $csupplyqty_sep = "=IFERROR(SUM(AC{$start}:AC{$end}),0)";
                $csupplyqty_oct = "=IFERROR(SUM(AI{$start}:AI{$end}),0)";
                $csupplyqty_nov = "=IFERROR(SUM(AL{$start}:AL{$end}),0)";
                $csupplyqty_dec = "=IFERROR(SUM(AO{$start}:AO{$end}),0)";
                $csupplyqty_jan = "=IFERROR(SUM(AU{$start}:AU{$end}),0)";
                $csupplyqty_feb = "=IFERROR(SUM(AX{$start}:AX{$end}),0)";
                $csupplyqty_mar = "=IFERROR(SUM(BA{$start}:BA{$end}),0)";

                //shipped
                $cshp_apr = "=IFERROR(SUM(L{$start}:L{$end}),0)";
                $cshp_may = "=IFERROR(SUM(O{$start}:O{$end}),0)";
                $cshp_jun = "=IFERROR(SUM(R{$start}:R{$end}),0)";
                $cshp_jul = "=IFERROR(SUM(X{$start}:X{$end}),0)";
                $cshp_aug = "=IFERROR(SUM(AA{$start}:AA{$end}),0)";
                $cshp_sep = "=IFERROR(SUM(AD{$start}:AD{$end}),0)";
                $cshp_oct = "=IFERROR(SUM(AJ{$start}:AJ{$end}),0)";
                $cshp_nov = "=IFERROR(SUM(AM{$start}:AM{$end}),0)";
                $cshp_dec = "=IFERROR(SUM(AP{$start}:AP{$end}),0)";
                $cshp_jan = "=IFERROR(SUM(AV{$start}:AV{$end}),0)";
                $cshp_feb = "=IFERROR(SUM(AY{$start}:AY{$end}),0)";
                $cshp_mar = "=IFERROR(SUM(BB{$start}:BB{$end}),0)";

                //formulas for category
                $cfor_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $cfor_q1_sc = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $cfor_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";

                $cfor_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $cfor_q2_sc = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $cfor_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";

                $cfor_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $cfor_q3_sc = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $cfor_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";

                $cfor_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $cfor_q4_sc = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $cfor_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";

                $cfor_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $cfor_tot_sc = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $cfor_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";

                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - Qty',
                    'J' => $cqty_apr,
                    'K' => $csupplyqty_apr,
                    'L' => $cshp_apr,
                    'M' => $cqty_may,
                    'N' => $csupplyqty_may,
                    'O' => $cshp_may,
                    'P' => $cqty_jun,
                    'Q' => $csupplyqty_jun,
                    'R' => $cshp_jun,
                    'S' => $cfor_q1_o,
                    'T' => $cfor_q1_sc,
                    'U' => $cfor_q1_shp,
                    'V' => $cqty_jul,
                    'W' => $csupplyqty_jul,
                    'X' => $cshp_jul,
                    'Y' => $cqty_aug,
                    'Z' => $csupplyqty_aug,
                    'AA' => $cshp_aug,
                    'AB' => $cqty_sep,
                    'AC' => $csupplyqty_sep,
                    'AD' => $cshp_sep,
                    'AE' => $cfor_q2_o,
                    'AF' => $cfor_q2_sc,
                    'AG' => $cfor_q2_shp,
                    'AH' => $cqty_oct,
                    'AI' => $csupplyqty_oct,
                    'AJ' => $cshp_oct,
                    'AK' => $cqty_nov,
                    'AL' => $csupplyqty_nov,
                    'AM' => $cshp_nov,
                    'AN' => $cqty_dec,
                    'AO' => $csupplyqty_dec,
                    'AP' => $cshp_dec,
                    'AQ' => $cfor_q3_o,
                    'AR' => $cfor_q3_sc,
                    'AS' => $cfor_q3_shp,
                    'AT' => $cqty_jan,
                    'AU' => $csupplyqty_jan,
                    'AV' => $cshp_jan,
                    'AW' => $cqty_feb,
                    'AX' => $csupplyqty_feb,
                    'AY' => $cshp_feb,
                    'AZ' => $cqty_mar,
                    'BA' => $csupplyqty_mar,
                    'BB' => $cshp_mar,
                    'BC' => $cfor_q4_o,
                    'BD' => $cfor_q4_sc,
                    'BE' => $cfor_q4_shp,
                    'BF' => $cfor_tot_o,
                    'BG' => $cfor_tot_sc,
                    'BH' => $cfor_tot_shp,
                ];

                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //formula for total
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_qty = $rowNumber - 1;
            $tot_qty_apr_formula = "=IFERROR(SUM(J{$start_row}:J{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";

            $tot_qty_may_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";

            $tot_qty_jun_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";

            $tot_qty_q1_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";

            $tot_qty_jul_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";

            $tot_qty_aug_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";

            $tot_qty_sep_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";

            $tot_qty_q2_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";

            $tot_qty_oct_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";

            $tot_qty_nov_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";

            $tot_qty_dec_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";

            $tot_qty_q3_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";

            $tot_qty_jan_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";

            $tot_qty_feb_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";

            $tot_qty_mar_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";

            $tot_qty_q4_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";

            $tot_qty_tq_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";

            $columnHeaders = [
                'C' => 'Total-QTY',
                'J' => $tot_qty_apr_formula,
                'K' => $tot_sc_apr_formula,
                'L' => $tot_shp_apr_formula,
                'M' => $tot_qty_may_formula,
                'N' => $tot_sc_may_formula,
                'O' => $tot_shp_may_formula,
                'P' => $tot_qty_jun_formula,
                'Q' => $tot_sc_jun_formula,
                'R' => $tot_shp_jun_formula,
                'S' => $tot_qty_q1_formula,
                'T' => $tot_sc_q1_formula,
                'U' => $tot_shp_q1_formula,
                'V' => $tot_qty_jul_formula,
                'W' => $tot_sc_jul_formula,
                'X' => $tot_shp_jul_formula,
                'Y' => $tot_qty_aug_formula,
                'Z' => $tot_sc_aug_formula,
                'AA' => $tot_shp_aug_formula,
                'AB' => $tot_qty_sep_formula,
                'AC' => $tot_sc_sep_formula,
                'AD' => $tot_shp_sep_formula,
                'AE' => $tot_qty_q2_formula,
                'AF' => $tot_sc_q2_formula,
                'AG' => $tot_shp_q2_formula,
                'AH' => $tot_qty_oct_formula,
                'AI' => $tot_sc_oct_formula,
                'AJ' => $tot_shp_oct_formula,
                'AK' => $tot_qty_nov_formula,
                'AL' => $tot_sc_nov_formula,
                'AM' => $tot_shp_nov_formula,
                'AN' => $tot_qty_dec_formula,
                'AO' => $tot_sc_dec_formula,
                'AP' => $tot_shp_dec_formula,
                'AQ' => $tot_qty_q3_formula,
                'AR' => $tot_sc_q3_formula,
                'AS' => $tot_shp_q3_formula,
                'AT' => $tot_qty_jan_formula,
                'AU' => $tot_sc_jan_formula,
                'AV' => $tot_shp_jan_formula,
                'AW' => $tot_qty_feb_formula,
                'AX' => $tot_sc_feb_formula,
                'AY' => $tot_shp_feb_formula,
                'AZ' => $tot_qty_mar_formula,
                'AA' => $tot_sc_mar_formula,
                'BB' => $tot_shp_mar_formula,
                'BC' => $tot_qty_q4_formula,
                'BD' => $tot_sc_q4_formula,
                'BE' => $tot_shp_q4_formula,
                'BF' => $tot_qty_tq_formula,
                'BG' => $tot_sc_tq_formula,
                'BH' => $tot_shp_tq_formula,
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            // Add filter for row 9
            $activeWorksheet->setAutoFilter('A9:BH9');

            // Add filter for row 9
            //bold total
            $boldrow = 'A' . $rowNumber . ':BH' . $rowNumber;
            $activeWorksheet->getStyle("$boldrow")->getFont()->setBold(true);
            $highestRow = $activeWorksheet->getHighestRow();

            $monRange = "{$monthrows[$month]}10:{$monthrows[$month]}{$lastpdtrow}";
            setFillColor($activeWorksheet, "{$monRange}", 'C4D79B'); // Light green
            // Set fill colors for quarters
            setFillColor($activeWorksheet, 'S8:U' . $lastpdtrow, 'ccffff'); // Quarter 1
            setFillColor($activeWorksheet, 'AE8:AG' . $lastpdtrow, 'ccffff'); // Quarter 2
            setFillColor($activeWorksheet, 'AQ8:AS' . $lastpdtrow, 'ccffff'); // Quarter 3
            setFillColor($activeWorksheet, 'BC8:BE' . $lastpdtrow, 'ccffff'); // Quarter 4
            // Set fill color for total
            setFillColor($activeWorksheet, 'BF8:BH' . $lastpdtrow, '99ccff'); // Dark blue
            // Set fill colors for fixed sections
            setFillColor($activeWorksheet, 'A1:H1', 'CC99FF'); // Fixed Order Form
            setFillColor($activeWorksheet, 'C9:H' . $lastpdtrow, 'ffff99'); // Yellow
            setFillColor($activeWorksheet, 'A8:BH8', 'CCFFCC'); // Light green
            setFillColor($activeWorksheet, 'A8:C' . $lastpdtrow, 'CCFFCC'); // Light green

           
            // Set fill color for total qty and amount
            $oddColor_a = 'e6f3ff'; // Specify your odd row color here
            $evenColor_a = 'cce6ff'; // Even row color
            for ($row1 = $start_row_amt; $row1 <= $end_row_amt; $row1++) {
                $color = $row1 % 2 == 0 ? $evenColor_a : $oddColor_a;
                $range_c1 = "A{$row1}:BH{$row1}";
                setFillColor($activeWorksheet, $range_c1, $color);
            }
            $oddColor_q = 'e6ffcc'; // Specify your odd row color here
            $evenColor_q = 'cdff99'; // Even row color
            for ($row = $start_row_qty; $row <= $end_row_qty; $row++) {
                $color = $row % 2 == 0 ? $evenColor_q : $oddColor_q;
                $range_c2 = "A{$row}:BH{$row}";
                setFillColor($activeWorksheet, $range_c2, $color);
            }


            // Set thiN border line from A1 to LAST
            $allColumnsRange = "A8:BH{$highestRow}";
            $activeWorksheet->getStyle($allColumnsRange)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
            // Set thick border line from J8 to L997
            $columns = ['J', 'M', 'P', 'S', 'V', 'Y', 'AB', 'AE', 'AH', 'AK', 'AN', 'AQ', 'AT', 'AW', 'AZ', 'BC', 'BF'];
            foreach ($columns as $startColumn) {
                $endColumn = $startColumn;
                for ($i = 0; $i < 2; $i++) {
                    $endColumn++;
                }
                $range = "{$startColumn}8:{$endColumn}{$end_row_qty}";
                $activeWorksheet->getStyle($range)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);
            }
            $width = 25; // Desired width in pixels
            for ($row = $start_row_amt; $row <= $end_row_qty; $row++) {
                $activeWorksheet->getRowDimension($row)->setRowHeight($width);
            }
            $activeWorksheet->getRowDimension('9')->setRowHeight('44');

            // Define the range
            $range_for_number_comma = "J{$start_row_amt}:BH{$end_row_amt}";
            $range_comma_2 = "F10:F{$lastpdtrow}";
            // Set comma-separated number format for the ranges
            $activeWorksheet->getStyle($range_for_number_comma)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');

            $activeWorksheet->getStyle($range_comma_2)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');
            // Create a writer and save the spreadsheet to a file
            $writer = new Xlsx($spreadsheet);
            $mod_dis = Distributers::model()->findByPk((int) $distributer_id);
            $file_name = 'Order_' . $mod_dis->distributer_name . '_' . $year . '.xlsx';
            // Create a writer
            // Set the appropriate HTTP headers for Excel file download
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment; filename="' . $file_name . '"');
            header('Cache-Control: max-age=0');
            // Save the spreadsheet to the output (i.e., it will trigger a file download)
            $writer->save('php://output');
            return;
        } else {
            throw new CHttpException(404, 'Page not found.');
        }
    }*/
    
    public function actionDownloadOrderFormatExcel() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $type = Yii::app()->request->getParam('type');

            $distributer_id = Yii::app()->request->getParam('distributer_id');

            $year = Yii::app()->request->getParam('year');

            $selyear = Yii::app()->request->getParam('year');
            $month = Yii::app()->request->getParam('month');

            $price_date = $year . '-' . $month . '-' . '01';
            
			$mod_dis = Distributers::model()->with('region_det')->findByPk((int) $distributer_id);
			$region_id = (int) $mod_dis->region_id;
            $cndcat = 'show_in_order="Y" AND (I.is_active="Y" OR (I.is_active="D" AND ((discontinuing_month>=' . (int) $month . ' AND discontinuing_year=' . (int) $year . ') OR discontinuing_year>' . (int) $year . '))) ';
            $arr_gmcs = [];
            if ((int)$mod_dis->distributer_type> 0) {				
				$cndcat .= ' AND I.id NOT IN (SELECT IFNULL(product_id,0) AS product_id FROM {{hidden_distributer_gmcs}} WHERE distributer_type_id='.(int)$mod_dis->distributer_type.')';
            }
            $current_year = date("Y");
            if($year>$current_year && $month>3){ //2027 1
                $cndcat .=  ' AND category_type_id IN (1,2)';
                $order_by = ' product_category_id,gmc_sort_order ASC';
            }else{ 
                $cndcat .=  ' AND future_category_type_id IN (1,2)';
                $order_by = ' product_category_id,future_gmc_sort_order ASC';
            }
            $sql_inventory = "SELECT I.id,goods_in_end_month,goods_in_end_year,product_category_id,gmc_code,model_no,cbm,base_code,I.sub_category,I.is_active,discontinuing_month,gmc_type,C.product_category
                        FROM {{inventory}} I 
                        LEFT JOIN {{mas_product_category}} C ON C.id = I.product_category_id
                        WHERE " . $cndcat."
                        ORDER by ".$order_by;//LIMIT 950
			
			$model_inventory = Yii::app()->db->createCommand($sql_inventory)->queryAll();
            $sql_category = "SELECT C.id AS id,C.product_category
                        FROM {{inventory}} I
                        LEFT JOIN {{mas_product_category}} C ON C.id = I.product_category_id
                        WHERE " . $cndcat."
                        GROUP BY C.id
                        ORDER by C.sort_order ASC
                        ";
            $modelmascategories = Yii::app()->db->createCommand($sql_category)->queryAll();           
            //$modelmascategories = MasProductCategory::model()->findAll(array('condition' => 'id IN (' . $sql_category . ')', 'order' => 'sort_order ASC'));
                        
            $file = fopen("upload/test.csv", "w");

            $j = 0;

            

            $arr_months = Order::getmonths();
            $monthrows = ['4' => 'K', '5' => 'N', '6' => 'Q', '7' => 'W', '8' => 'Z', '9' => 'AC', '10' => 'AI', '11' => 'AL', '12' => 'AO', '1' => 'AU', '2' => 'AX', '3' => 'BA'];

            if ($month <= 3) {
                $year = $year - 1;
            }
            $year1 = (int) $year + 1;
            $arrInpMonth = [];
            for ($mon = 1; $mon <= 12; $mon++) {
                $arrInpMonth[$mon] = '';
            }
            if ((int) $month > 0) {
                $arrInpMonth[$month] = '                        Input your Order in this month';
            }
            function setData($worksheet, $erow, $data) {
                foreach ($data as $column => $value) {
                    $cell = $column . $erow;
                    $worksheet->setCellValue($cell, $value);
                }
            }

            function setFillColor($worksheet, $range, $color) {
                $worksheet->getStyle($range)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB($color);
            }
            function getExcelColumn($index) {
                $letters = range('A', 'Z');
                if ($index < 26) {
                    return $letters[$index];
                } else {
                    return $letters[floor($index / 26) - 1] . $letters[$index % 26];
                }
            }
            $spreadsheet = new Spreadsheet();
            $activeWorksheet = $spreadsheet->getActiveSheet();
            
            $rowNumber = 1;
            
            // Merge and set title
            $activeWorksheet->mergeCells('A1:BI1')->setCellValue('A1', 'FIXED ORDER FORM');
            // Fetch comments and populate them
            $modelComments = (new MasComments())->findAll(['condition' => 'id<6', 'order' => 'id ASC']);
            $comments = array_column($modelComments, 'comments');
            $rowNumber = 1;
            foreach ($comments as $comment) {
                $rowNumber++;
                $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}")
                                ->setCellValue("F{$rowNumber}", $comment);
            }
            $rowNumber = max($rowNumber + 1, 7);
            $activeWorksheet->mergeCells("A{$rowNumber}:B{$rowNumber}");
            
            // Set text wrap for specific cells
            $cellsToWrap = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8","K8", "N8", "Q8", "T8", "W8", "Z8", "AC8", "AF8", "AI8", "AL8", "AO8", "AR8", "AU8", "AX8", "BA8", "BD8", "BG8", "K{$rowNumber}", "N{$rowNumber}", "Q{$rowNumber}", "W{$rowNumber}", "Z{$rowNumber}", "AC{$rowNumber}", "AI{$rowNumber}", "AL{$rowNumber}", "AO{$rowNumber}", "AU{$rowNumber}", "AX{$rowNumber}", "BA{$rowNumber}"];
            foreach ($cellsToWrap as $cell) {
                $activeWorksheet->getStyle($cell)->getAlignment()->setWrapText(true);
            }
            
            // Insert arrow image
            $ordered_month = $monthrows[$month] . $rowNumber;
            $drawing = new Drawing();
            $drawing->setPath(__DIR__ . '/../../../images/darrow.png')// Adjust the path as needed
                    ->setDescription('Input your Order in this month')
                    ->setCoordinates($ordered_month)
                    ->setHeight(30)
                    ->setWorksheet($activeWorksheet);
            
            // Set column widths
            $columnWidths = ['C' => 23.6, 'E' => 42.3, 'G' => 14.22, 'I' => 18.5, 'J' => 18.5];
            foreach ($columnWidths as $col => $width) {
                $activeWorksheet->getColumnDimension($col)->setWidth($width);
            }
            for ($col = 'K'; $col <= 'BI'; $col++) {
                $activeWorksheet->getColumnDimension($col)->setWidth(11.09);
            }

            // Set column headers
            $columnHeaders = [
                'A' => 'DISTRIBUTOR', 'C' => $mod_dis->distributer_name,
                'K' => $arrInpMonth[4], 'N' => $arrInpMonth[5], 'Q' => $arrInpMonth[6],
                'W' => $arrInpMonth[7], 'Z' => $arrInpMonth[8], 'AC' => $arrInpMonth[9], 
                'AI' => $arrInpMonth[10], 'AL' => $arrInpMonth[11],  'AO' => $arrInpMonth[12], 
                'AU' => $arrInpMonth[1], 'AX' => $arrInpMonth[2], 'BA' => $arrInpMonth[3]
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $rowNumber++;//8
            
            
            // Merge cells
            $mergeRanges = ["K:M", "N:P", "Q:S", "T:V", "W:Y", "Z:AB", "AC:AE", "AF:AH", "AI:AK", "AL:AN", "AO:AQ", "AR:AT", "AU:AW", "AX:AZ", "BA:BC", "BD:BF", "BG:BI"];
            foreach ($mergeRanges as $eachrange) {
                // Split the range into start and end columns
                list($startCol, $endCol) = explode(":", $eachrange);
                // Construct the full range with the row number
                $cellRange = "{$startCol}{$rowNumber}:{$endCol}{$rowNumber}";
                // Merge the cells in the specified range
                $activeWorksheet->mergeCells($cellRange);
            }
            // Set date formats//row8
            $dateColumns = ["K", "N", "Q", "W", "Z", "AC", "AI", "AL", "AO", "AU", "AX", "BA"];
            foreach ($dateColumns as $col) {
                $activeWorksheet->getStyle("{$col}{$rowNumber}")->getNumberFormat()->setFormatCode('mmm yyyy');
            }
            $columnHeaders = [
                'A' => 'Sr.#','B' => 'New/Discontinued','C' => 'PRODUCT CATEGORY', 'D' => 'GMC', 'E' => 'Model',
                'F' => 'YMGF Price (' . $mod_dis->region_det->currency_type . ')',
                'G' => 'Supply confirmation month', 'H' => 'CBM', 'I' => 'Unshipped', 'J' => 'BASE CODE',
                'K' => "=DATE({$year},4,1)", 'N' => "=DATE({$year},5,1)", 'Q' => "=DATE({$year},6,1)",
                'T' => '1Q TOTAL', 'W' => "=DATE({$year},7,1)", 'Z' => "=DATE({$year},8,1)",
                'AC' => "=DATE({$year},9,1)", 'AF' => '2Q TOTAL', 'AI' => "=DATE({$year},10,1)",
                'AL' => "=DATE({$year},11,1)", 'AO' => "=DATE({$year},12,1)", 'AR' => '3Q TOTAL',
                'AU' => "=DATE({$year1},1,1)", 'AX' => "=DATE({$year1},2,1)", 'BA' => "=DATE({$year1},3,1)",
                'BD' => '4Q TOTAL', 'BG' => 'TOTAL ' . $year . ' - ' . $year1,
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            //echo $rowNumber;
            //exit;
            // Set text wrap and center alignment
            $cellsToFormat = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8","K8", "N8", "Q8", "T8", "W8", "Z8", "AC8", "AF8", "AI8", "AL8", "AO8", "AR8", "AU8", "AX8", "BA8", "BD8", "BG8"];
            foreach ($cellsToFormat as $cell) {
                $activeWorksheet->getStyle($cell)->getAlignment()
                                ->setWrapText(true)
                                ->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
            }
            // Hide base code column
            $activeWorksheet->getColumnDimension('J')->setVisible(false);
            
            // Bold total row and freeze panes
            $activeWorksheet->getStyle("A{$rowNumber}:BI{$rowNumber}")->getFont()->setBold(true);
            $activeWorksheet->freezePane('K10');
            $rowNumber++;
            $arr_supply_columns_colour = [];
            // Column headers setup with extra column at I
            $columnHeaders = array_merge(array_fill_keys(range('A', 'J'), ' '), [
            
                'K' => 'ORDER', 'L' => 'Supply Confirmation', 'M' => 'SHIP',
                'N' => 'ORDER', 'O' => 'Supply Confirmation', 'P' => 'SHIP',
                'Q' => 'ORDER', 'R' => 'Supply Confirmation', 'S' => 'SHIP',
                'T' => 'ORDER', 'U' => 'Supply Confirmation', 'V' => 'SHIP',
                'W' => 'ORDER', 'X' => 'Supply Confirmation', 'Y' => 'SHIP',
                'Z' => 'ORDER', 'AA' => 'Supply Confirmation', 'AB' => 'SHIP',
                'AC' => 'ORDER', 'AD' => 'Supply Confirmation', 'AE' => 'SHIP',
                'AF' => 'ORDER', 'AG' => 'Supply Confirmation', 'AH' => 'SHIP',
                'AI' => 'ORDER', 'AJ' => 'Supply Confirmation', 'AK' => 'SHIP',
                'AL' => 'ORDER', 'AM' => 'Supply Confirmation', 'AN' => 'SHIP',
                'AO' => 'ORDER', 'AP' => 'Supply Confirmation', 'AQ' => 'SHIP',
                'AR' => 'ORDER', 'AS' => 'Supply Confirmation', 'AT' => 'SHIP',
                'AU' => 'ORDER', 'AV' => 'Supply Confirmation', 'AW' => 'SHIP',
                'AX' => 'ORDER', 'AY' => 'Supply Confirmation', 'AZ' => 'SHIP',
                'BA' => 'ORDER', 'BB' => 'Supply Confirmation', 'BC' => 'SHIP',
                'BD' => 'ORDER', 'BE' => 'Supply Confirmation', 'BF' => 'SHIP',
                'BG' => 'ORDER', 'BH' => 'Supply Confirmation', 'BI' => 'SHIP'
            ]);
            
            setData($activeWorksheet, $rowNumber++, $columnHeaders);

           // Apply wrap text to header row
            foreach (range('A', 'BI') as $col) {
                $activeWorksheet->getStyle("{$col}9")->getAlignment()->setWrapText(true);
            }
            
            // Fetch order details
            $sql_inv = "SELECT product_id, distributer_id, parent_det.month, parent_det.year, 
                               quantity_ordered, quantity_confirmed, quantity_shipped, is_done
                        FROM {{order_details}} OD
                        LEFT JOIN {{order}} parent_det ON OD.order_id = parent_det.id
                        WHERE parent_det.distributer_id = $distributer_id 
                        AND ((parent_det.month > 3 AND parent_det.year = $year) 
                             OR (parent_det.month < 4 AND parent_det.year = $year1))
                        GROUP BY OD.product_id, parent_det.month, parent_det.year, parent_det.distributer_id";
            $arr_inv =Yii::app()->db->createCommand($sql_inv)->queryAll() ;
            $arr_order = [];$arr_unshipped=[];
            if(!empty($arr_inv)){
                foreach ($arr_inv as $each) {
                    $arr_order[$each['product_id']][$each['year']][$each['month']] = [
                        'quantity_ordered' => $each['quantity_ordered'],
                        'quantity_confirmed' => $each['quantity_confirmed'],
                        'quantity_shipped' => $each['quantity_shipped'],
                        'is_done' => $each['is_done']
                    ];
                }
            }
            $modelCH = new CreditHold();//shipment_month,shipment_year
            $modelCHDet =$modelCH->find(['condition'=>'is_master="Y"','order'=>'id DESC']);
            $credit_hold_id = (int)$modelCHDet->id;
            /*$sql_unshipped = "SELECT OD.product_id,SUM(CH.quantity) as unshipped
                        FROM {{order_details}} OD
                        LEFT JOIN {{credit_hold_details}} CH ON CH.product_id=OD.product_id AND credit_hold_id =$credit_hold_id AND((CH.shipment_month<= $month AND CH.shipment_year=$year) OR ( CH.shipment_year<$year) )
                        LEFT JOIN {{order}} parent_det ON OD.order_id = parent_det.id
                        WHERE parent_det.distributer_id = $distributer_id  AND CH.distributor_id=$distributer_id
                        GROUP BY CH.product_id,CH.distributor_id";*/
            $sql_unshipped = "SELECT CH.product_id,SUM(CH.quantity) as unshipped
                        FROM {{credit_hold_details}} CH 
                        WHERE credit_hold_id =$credit_hold_id AND ((CH.shipment_month< $modelCHDet->ch_month AND CH.shipment_year=$modelCHDet->ch_year) OR ( CH.shipment_year<$modelCHDet->ch_year) ) AND CH.distributor_id=$distributer_id
                        GROUP BY CH.product_id,CH.distributor_id";
            $arr_unshp =Yii::app()->db->createCommand($sql_unshipped)->queryAll() ;
            if(!empty($arr_unshp)){
                foreach ($arr_unshp as $eachunsp) {
                    $arr_unshipped[$eachunsp['product_id']] =$eachunsp['unshipped'];
                }
            }
            //echo '<pre>';print_r($arr_unshipped);exit;
            $i = 0;
            
            // Processing inventory
            $modelOrder = new Order();
            $arr_months = $modelOrder->getmonths();
            $old_category_id = 0;
            $arr_cat = [];
            $dis_range = [];
            //echo '<pre>';print_r($model_inventory);echo $rowNumber;
            $w=0;
            foreach ($model_inventory as $row) {
                $unshipped =isset($arr_unshipped[$row['id']])?$arr_unshipped[$row['id']]:0;
                $goods_in_date = new DateTime("{$row['goods_in_end_year']}-" . str_pad($row['goods_in_end_month'], 2, '0', STR_PAD_LEFT) . "-01");
                $supply_confirm_month = $goods_in_date->sub(new DateInterval('P1M'))->format('F - Y');
                
                $processed_row = [];
                
                for ($omonth = 1; $omonth <= 12; $omonth++) {
                    $oyear = ($omonth <= 3) ? $year1 : $year;
                    $processed_row[$omonth] = array_map('intval', $arr_order[$row['id']][$oyear][$omonth] ?? []);
                }
                
                $category_changed = ($old_category_id !== $row['product_category_id']);
                if ($category_changed) {
                    $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
                    $arr_cat[$row['product_category_id']]['start'] = $rowNumber;
                    $old_category_id = $row['product_category_id'];
                }
            
                $ymg_price = InventoryPrice::model()->find([ 'condition' => "product_id={$row['id']} AND region_id=$region_id AND from_date <= '$price_date' AND to_date >= '$price_date'" ])['price'] ?? '00" - (TBA)"';
                $pair_name = $row['gmc_type'] === 'P' ? 'PAIR' : ($row['gmc_type'] === 'K' ? 'KIT' : '');
                
                $for_q1_o = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $for_q1_sc = "=SUM(IFERROR(L{$rowNumber},0), IFERROR(O{$rowNumber},0), IFERROR(R{$rowNumber},0))";
                $for_q1_shp = "=M{$rowNumber}+P{$rowNumber}+S{$rowNumber}";
                
                $for_q2_o = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $for_q2_sc = "=SUM(IFERROR(X{$rowNumber},0), IFERROR(AA{$rowNumber},0), IFERROR(AD{$rowNumber},0))";
                $for_q2_shp = "=Y{$rowNumber}+AB{$rowNumber}+AE{$rowNumber}";
                
                $for_q3_o = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $for_q3_sc = "=SUM(IFERROR(AJ{$rowNumber},0), IFERROR(AM{$rowNumber},0), IFERROR(AP{$rowNumber},0))";
                $for_q3_shp = "=AK{$rowNumber}+AN{$rowNumber}+AQ{$rowNumber}";
                
                $for_q4_o = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $for_q4_sc = "=SUM(IFERROR(AV{$rowNumber},0), IFERROR(AY{$rowNumber},0), IFERROR(BB{$rowNumber},0))";
                $for_q4_shp = "=AW{$rowNumber}+AZ{$rowNumber}+BC{$rowNumber}";
                
                $for_tot_o = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $for_tot_sc = "=IFERROR(U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber},0)";
                $for_tot_shp = "=V{$rowNumber}+AH{$rowNumber}+AT{$rowNumber}+BF{$rowNumber}";
                $columnHeaders = [
                    'A' => Inventory::model()->getinvtype($row['id'], $month, $selyear),
                    'B' => $pair_name,
                    'C' => $row['sub_category'],
                    'D' => $row['gmc_code'],
                    'E' => $row['model_no'],
                    'F' => $ymg_price,
                    'G' => $supply_confirm_month,
                    'H' => $row['cbm'],
                    'I' => $unshipped,
                    'J' => $row['base_code'],
                    'T' => $for_q1_o,
                    'U' => $for_q1_sc,
                    'V' => $for_q1_shp,
                    'AF' => $for_q2_o,
                    'AG' => $for_q2_sc,
                    'AH' => $for_q2_shp,
                    'AR' => $for_q3_o,
                    'AS' => $for_q3_sc,
                    'AT' => $for_q3_shp,
                    'BD' => $for_q4_o,
                    'BE' => $for_q4_sc,
                    'BF' => $for_q4_shp,
                    'BG' => $for_tot_o,
                    'BH' => $for_tot_sc,
                    'BI' => $for_tot_shp,
                ];
                
                $arr_month_no       = range(1, 12); // Months 1 to 12
                $baseColumns  = [46, 49, 52, 10, 13, 16, 22, 25, 28, 34, 37, 40]; // base columns for months 1–12
                $dataTypes    = ['quantity_ordered', 'quantity_confirmed', 'quantity_shipped'];
                foreach ($arr_month_no as $i => $month) {
                    $baseCol = $baseColumns[$i]; // Match month to its corresponding base column
                    foreach ($dataTypes as $offset => $key) {
                        $colLetter = getExcelColumn($baseCol + $offset);
                        $columnHeaders[$colLetter] = $processed_row[$month][$key] ?? 0;
                    }
                }
                //echo '<pre>';print_r($columnHeaders);echo $rowNumber.' '.$w;
                setData($activeWorksheet, $rowNumber++, $columnHeaders);
                if ($row['is_active'] === 'D') {
                    $dis_range[] = "{$monthrows[$row['discontinuing_month']]}{$rowNumber}:BI{$rowNumber}";
                }
                $w++;
            }
            //echo 'end';exit;
            $lastpdtrow = (int)$rowNumber - 1;

            if (!empty($dis_range)) {
                foreach ($dis_range as $each_discontinue) {
                    setFillColor($activeWorksheet, $each_discontinue, 'b1b1b1');
                }
            }
            $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
            $rowNumber++;
            $columnHeaders = [
                'C' => 'PRODUCT CATEGORY',
                'I' => 'UNSHIPPED QTY',
                'J' => "BASE CODE",
                'K' => " ",
                'N' => " ",
                'Q' => " ",
                'T' => '1Q TOTAL',
                'W' => " ",
                'Z' => " ",
                'AC' => " ",
                'AF' => '2Q TOTAL',
                'AI' => " ",
                'AL' => " ",
                'AO' => " ",
                'AR' => '3Q TOTAL',
                'AU' => " ",
                'AX' => " ",
                'BA' => " ",
                'BD' => '4Q TOTAL',
                'BG' => 'TOTAL ' . $year . ' - ' . $year1,
            ];
            setData($activeWorksheet, $rowNumber++, $columnHeaders);
            //echo '<pre>';print_r($columnHeaders);echo $rowNumber;exit;
        
            $start_row_amt = $rowNumber;
            $start_row = $rowNumber;
            // Create a helper function to minimize repeated array accesses
            function getCategoryRange($categoryId, $arr_cat) {
                if (isset($arr_cat[(int)$categoryId])) {
                    return [$arr_cat[(int)$categoryId]['start'], $arr_cat[(int)$categoryId]['end']];
                }
                return [1, 1]; // Default if not found
            }
            // Define month names and corresponding columns
            $months = [
                'apr' => ['K', 'L', 'M'],
                'may' => ['N', 'M', 'P'],
                'jun' => ['Q', 'R', 'S'],
                'jul' => ['W', 'X', 'Y'],
                'aug' => ['Z', 'AA', 'AB'],
                'sep' => ['AC', 'AD', 'AE'],
                'oct' => ['AI', 'AJ', 'AK'],
                'nov' => ['AL', 'AM', 'AN'],
                'dec' => ['AO', 'AP', 'AQ'],
                'jan' => ['AU', 'AV', 'AW'],
                'feb' => ['AX', 'AY', 'AZ'],
                'mar' => ['BA', 'BB', 'BC']
            ];
            
            // Arrays to hold formulas for each type
            $formulas = [
                'camt' => [],
                'csupplyamt' => [],
                'cshp' => []
            ];
            
            foreach ($modelmascategories as $eachcat) {
                // Get the category range($start,$end) for current category
                list($start, $end) = getCategoryRange($eachcat['id'], $arr_cat);
                 // Loop through each month and create formulas dynamically
                foreach ($months as $emonth => $cols) {
                    list($amtCol, $supplyCol, $shpCol) = $cols;
            
                    // Generate formulas for camt, csupplyamt, and cshp for each month
                    $formulas['camt'][$emonth] = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},{$amtCol}{$start}:{$amtCol}{$end}),0)";
                    $formulas['csupplyamt'][$emonth] = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},{$supplyCol}{$start}:{$supplyCol}{$end}),0)";
                    $formulas['cshp'][$emonth] = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},{$shpCol}{$start}:{$shpCol}{$end}),0)";
                }
                
                // Assigning the generated formulas for all months
                $columns = [];
                foreach ($months as $emonth => $cols) {
                    $columns["{$cols[0]}"] = $formulas['camt'][$emonth];
                    $columns["{$cols[1]}"] = $formulas['csupplyamt'][$emonth];
                    $columns["{$cols[2]}"] = $formulas['cshp'][$emonth];
                }
            
                // Formulas for quarter totals
                $quarters = ['q1', 'q2', 'q3', 'q4'];
                foreach ($quarters as $q) {
                    $columns["T"] = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                    $columns["U"] = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";
                    $columns["V"] = "=M{$rowNumber}+P{$rowNumber}+S{$rowNumber}";
                }
            
                // Total formulas for category totals
                $columns['BG'] = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $columns['BH'] = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                $columns['BI'] = "=V{$rowNumber}+AH{$rowNumber}+AT{$rowNumber}+BF{$rowNumber}";
            
                // Column headers with formulas
                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - AMT',
                ] + $columns;
            
                // Set data in the worksheet
                setData($activeWorksheet, $rowNumber, $columnHeaders);
            
                // Increment rowNumber for next category
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_amt = $rowNumber;
            
            $tot_qty_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            
            $tot_qty_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            
            $tot_qty_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            
            $tot_qty_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            
            $tot_qty_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            
            $tot_qty_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            
            $tot_qty_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";
            
            $tot_qty_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            
            $tot_qty_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            
            $tot_qty_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            
            $tot_qty_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            
            $tot_qty_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            
            $tot_qty_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            
            $tot_qty_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            
            $tot_qty_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            
            $tot_qty_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            
            $tot_qty_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BI{$start_row}:BI{$end_row}),0)";


            $columnHeaders = [
                'C' => 'Total - Amt',
                'K' => $tot_qty_apr_formula,
                'L' => $tot_sc_apr_formula,
                'M' => $tot_shp_apr_formula,
                'N' => $tot_qty_may_formula,
                'O' => $tot_sc_may_formula,
                'P' => $tot_shp_may_formula,
                'Q' => $tot_qty_jun_formula,
                'R' => $tot_sc_jun_formula,
                'S' => $tot_shp_jun_formula,
                'T' => $tot_qty_q1_formula,
                'U' => $tot_sc_q1_formula,
                'V' => $tot_shp_q1_formula,
                'W' => $tot_qty_jul_formula,
                'X' => $tot_sc_jul_formula,
                'Y' => $tot_shp_jul_formula,
                'Z' => $tot_qty_aug_formula,
                'AA' => $tot_sc_aug_formula,
                'AB' => $tot_shp_aug_formula,
                'AC' => $tot_qty_sep_formula,
                'AD' => $tot_sc_sep_formula,
                'AE' => $tot_shp_sep_formula,
                'AF' => $tot_qty_q2_formula,
                'AG' => $tot_sc_q2_formula,
                'AH' => $tot_shp_q2_formula,
                'AI' => $tot_qty_oct_formula,
                'AJ' => $tot_sc_oct_formula,
                'AK' => $tot_shp_oct_formula,
                'AL' => $tot_qty_nov_formula,
                'AM' => $tot_sc_nov_formula,
                'AN' => $tot_shp_nov_formula,
                'AO' => $tot_qty_dec_formula,
                'AP' => $tot_sc_dec_formula,
                'AQ' => $tot_shp_dec_formula,
                'AR' => $tot_qty_q3_formula,
                'AS' => $tot_sc_q3_formula,
                'AT' => $tot_shp_q3_formula,
                'AU' => $tot_qty_jan_formula,
                'AV' => $tot_sc_jan_formula,
                'AW' => $tot_shp_jan_formula,
                'AX' => $tot_qty_feb_formula,
                'AY' => $tot_sc_feb_formula,
                'AZ' => $tot_shp_feb_formula,
                'BA' => $tot_qty_mar_formula,
                'BB' => $tot_sc_mar_formula,
                'BC' => $tot_shp_mar_formula,
                'BD' => $tot_qty_q4_formula,
                'BE' => $tot_sc_q4_formula,
                'BF' => $tot_shp_q4_formula,
                'BG' => $tot_qty_tq_formula,
                'BH' => $tot_sc_tq_formula,
                'BI' => $tot_shp_tq_formula,
            ];
            $tot_rowno = $rowNumber;
            setData($activeWorksheet, $rowNumber, $columnHeaders);

            $activeWorksheet->getStyle('A' . $rowNumber . ':BI' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //cbmTotal
            $columnHeaders = [
                'C' => 'CBM Total',
                'K' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},K10:K{$lastpdtrow}),0)",
                'L' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},L10:L{$lastpdtrow}),0)",
                'M' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},M10:M{$lastpdtrow}),0)",
                'N' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},N10:N{$lastpdtrow}),0)",
                'O' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},O10:O{$lastpdtrow}),0)",
                'P' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},P10:P{$lastpdtrow}),0)",
                'Q' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Q10:Q{$lastpdtrow}),0)",
                'R' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},R10:R{$lastpdtrow}),0)",
                'S' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},S10:S{$lastpdtrow}),0)",
                'T' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},T10:T{$lastpdtrow}),0)",
                'U' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},U10:U{$lastpdtrow}),0)",
                'V' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},V10:V{$lastpdtrow}),0)",
                'W' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},W10:W{$lastpdtrow}),0)",
                'X' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},X10:X{$lastpdtrow}),0)",
                'Y' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Y10:Y{$lastpdtrow}),0)",
                'Z' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Z10:Z{$lastpdtrow}),0)",
                'AA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AA10:AA{$lastpdtrow}),0)",
                'AB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AB10:AB{$lastpdtrow}),0)",
                'AC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AC10:AC{$lastpdtrow}),0)",
                'AD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AD10:AD{$lastpdtrow}),0)",
                'AE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AE10:AE{$lastpdtrow}),0)",
                'AF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AF10:AF{$lastpdtrow}),0)",
                'AG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AG10:AG{$lastpdtrow}),0)",
                'AH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AH10:AH{$lastpdtrow}),0)",
                'AI' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AI10:AI{$lastpdtrow}),0)",
                'AJ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AJ10:AJ{$lastpdtrow}),0)",
                'AK' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AK10:AK{$lastpdtrow}),0)",
                'AL' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AL10:AL{$lastpdtrow}),0)",
                'AM' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AM10:AM{$lastpdtrow}),0)",
                'AN' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AN10:AN{$lastpdtrow}),0)",
                'AO' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AO10:AO{$lastpdtrow}),0)",
                'AP' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AP10:AP{$lastpdtrow}),0)",
                'AQ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AQ10:AQ{$lastpdtrow}),0)",
                'AR' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AR10:AR{$lastpdtrow}),0)",
                'AS' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AS10:AS{$lastpdtrow}),0)",
                'AT' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AT10:AT{$lastpdtrow}),0)",
                'AU' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AU10:AU{$lastpdtrow}),0)",
                'AV' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AV10:AV{$lastpdtrow}),0)",
                'AW' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AW10:AW{$lastpdtrow}),0)",
                'AX' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AX10:AX{$lastpdtrow}),0)",
                'AY' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AY10:AY{$lastpdtrow}),0)",
                'AZ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AZ10:AZ{$lastpdtrow}),0)",
                'BA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BA10:BA{$lastpdtrow}),0)",
                'BB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BB10:BB{$lastpdtrow}),0)",
                'BC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BC10:BC{$lastpdtrow}),0)",
                'BD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BD10:BD{$lastpdtrow}),0)",
                'BE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BE10:BE{$lastpdtrow}),0)",
                'BF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BF10:BF{$lastpdtrow}),0)",
                'BG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BG10:BG{$lastpdtrow}),0)",
                'BH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BH10:BH{$lastpdtrow}),0)",
                'BI' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BI10:BI{$lastpdtrow}),0)",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //Difference
            $columnHeaders = [
                'C' => 'Difference',
                'N' => "=N{$tot_rowno}-M{$tot_rowno}",
                'P' => "=P{$tot_rowno}-O{$tot_rowno}",
                'S' => "=S{$tot_rowno}-R{$tot_rowno}",
                'V' => "=V{$tot_rowno}-U{$tot_rowno}",
                'Y' => "=Y{$tot_rowno}-X{$tot_rowno}",
                'AB' => "=AB{$tot_rowno}-AA{$tot_rowno}",
                'AE' => "=AE{$tot_rowno}-AD{$tot_rowno}",
                'AH' => "=AH{$tot_rowno}-AG{$tot_rowno}",
                'AK' => "=AK{$tot_rowno}-AJ{$tot_rowno}",
                'AN' => "=AN{$tot_rowno}-AM{$tot_rowno}",
                'AQ' => "=AQ{$tot_rowno}-AP{$tot_rowno}",
                'AT' => "=AT{$tot_rowno}-AS{$tot_rowno}",
                'AW' => "=AW{$tot_rowno}-AV{$tot_rowno}",
                'AZ' => "=AZ{$tot_rowno}-AY{$tot_rowno}",
                'BC' => "=BC{$tot_rowno}-BB{$tot_rowno}",
                'BF' => "=BF{$tot_rowno}-BE{$tot_rowno}",
                'BI' => "=BI{$tot_rowno}-BH{$tot_rowno}",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;

            //quantity
            $start_row_qty = $rowNumber;
            $start_row = $rowNumber;
            foreach ($modelmascategories as $eachcat) {
                if (isset($arr_cat[(int) $eachcat['id']])) {
                    // The element exists in the array
                    $start = $arr_cat[(int) $eachcat['id']]['start'];
                    $end = $arr_cat[(int) $eachcat['id']]['end'];
                } else {
                    // The element does not exist in the array
                    // to avoid circular reference
                    $start = 1;
                    $end = 1;
                }

                //ordered//
                $cqty_apr = "=IFERROR(SUM(K{$start}:K{$end}),0)";
                $cqty_may = "=IFERROR(SUM(N{$start}:N{$end}),0)";
                $cqty_jun = "=IFERROR(SUM(Q{$start}:Q{$end}),0)";
                $cqty_jul = "=IFERROR(SUM(W{$start}:W{$end}),0)";
                $cqty_aug = "=IFERROR(SUM(Z{$start}:Z{$end}),0)";
                $cqty_sep = "=IFERROR(SUM(AC{$start}:AC{$end}),0)";
                $cqty_oct = "=IFERROR(SUM(AI{$start}:AI{$end}),0)";
                $cqty_nov = "=IFERROR(SUM(AL{$start}:AL{$end}),0)";
                $cqty_dec = "=IFERROR(SUM(AO{$start}:AO{$end}),0)";
                $cqty_jan = "=IFERROR(SUM(AU{$start}:AU{$end}),0)";
                $cqty_feb = "=IFERROR(SUM(AX{$start}:AX{$end}),0)";
                $cqty_mar = "=IFERROR(SUM(BA{$start}:BA{$end}),0)";
                
                //supplyconfirmed
                $csupplyqty_apr = "=IFERROR(SUM(L{$start}:L{$end}),0)";
                $csupplyqty_may = "=IFERROR(SUM(O{$start}:O{$end}),0)";
                $csupplyqty_jun = "=IFERROR(SUM(R{$start}:R{$end}),0)";
                $csupplyqty_jul = "=IFERROR(SUM(X{$start}:X{$end}),0)";
                $csupplyqty_aug = "=IFERROR(SUM(AA{$start}:AA{$end}),0)";
                $csupplyqty_sep = "=IFERROR(SUM(AD{$start}:AD{$end}),0)";
                $csupplyqty_oct = "=IFERROR(SUM(AJ{$start}:AJ{$end}),0)";
                $csupplyqty_nov = "=IFERROR(SUM(AM{$start}:AM{$end}),0)";
                $csupplyqty_dec = "=IFERROR(SUM(AP{$start}:AP{$end}),0)";
                $csupplyqty_jan = "=IFERROR(SUM(AV{$start}:AV{$end}),0)";
                $csupplyqty_feb = "=IFERROR(SUM(AY{$start}:AY{$end}),0)";
                $csupplyqty_mar = "=IFERROR(SUM(BB{$start}:BB{$end}),0)";

                //shipped
                $cshp_apr = "=IFERROR(SUM(M{$start}:M{$end}),0)";
                $cshp_may = "=IFERROR(SUM(P{$start}:P{$end}),0)";
                $cshp_jun = "=IFERROR(SUM(S{$start}:S{$end}),0)";
                $cshp_jul = "=IFERROR(SUM(Y{$start}:Y{$end}),0)";
                $cshp_aug = "=IFERROR(SUM(AB{$start}:AB{$end}),0)";
                $cshp_sep = "=IFERROR(SUM(AE{$start}:AE{$end}),0)";
                $cshp_oct = "=IFERROR(SUM(AK{$start}:AK{$end}),0)";
                $cshp_nov = "=IFERROR(SUM(AN{$start}:AN{$end}),0)";
                $cshp_dec = "=IFERROR(SUM(AQ{$start}:AQ{$end}),0)";
                $cshp_jan = "=IFERROR(SUM(AW{$start}:AW{$end}),0)";
                $cshp_feb = "=IFERROR(SUM(AZ{$start}:AZ{$end}),0)";
                $cshp_mar = "=IFERROR(SUM(BC{$start}:BC{$end}),0)";//over

                
                //formulas for category
                $cfor_q1_o = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $cfor_q1_sc = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";
                $cfor_q1_shp = "=M{$rowNumber}+P{$rowNumber}+S{$rowNumber}";
                
                $cfor_q2_o = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $cfor_q2_sc = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";
                $cfor_q2_shp = "=Y{$rowNumber}+AB{$rowNumber}+AE{$rowNumber}";
                
                $cfor_q3_o = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $cfor_q3_sc = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";
                $cfor_q3_shp = "=AK{$rowNumber}+AN{$rowNumber}+AQ{$rowNumber}";
                
                $cfor_q4_o = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $cfor_q4_sc = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";
                $cfor_q4_shp = "=AW{$rowNumber}+AZ{$rowNumber}+BC{$rowNumber}";
                
                $cfor_tot_o = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $cfor_tot_sc = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                $cfor_tot_shp = "=V{$rowNumber}+AH{$rowNumber}+AT{$rowNumber}+BF{$rowNumber}";
            
                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - Qty',
                    'K' => $cqty_apr,
                    'L' => $csupplyqty_apr,
                    'M' => $cshp_apr,
                    'N' => $cqty_may,
                    'O' => $csupplyqty_may,
                    'P' => $cshp_may,
                    'Q' => $cqty_jun,
                    'R' => $csupplyqty_jun,
                    'S' => $cshp_jun,
                    'T' => $cfor_q1_o,
                    'U' => $cfor_q1_sc,
                    'V' => $cfor_q1_shp,
                    'W' => $cqty_jul,
                    'X' => $csupplyqty_jul,
                    'Y' => $cshp_jul,
                    'Z' => $cqty_aug,
                    'AA' => $csupplyqty_aug,
                    'AB' => $cshp_aug,
                    'AC' => $cqty_sep,
                    'AD' => $csupplyqty_sep,
                    'AE' => $cshp_sep,
                    'AF' => $cfor_q2_o,
                    'AG' => $cfor_q2_sc,
                    'AH' => $cfor_q2_shp,
                    'AI' => $cqty_oct,
                    'AJ' => $csupplyqty_oct,
                    'AK' => $cshp_oct,
                    'AL' => $cqty_nov,
                    'AM' => $csupplyqty_nov,
                    'AN' => $cshp_nov,
                    'AO' => $cqty_dec,
                    'AP' => $csupplyqty_dec,
                    'AQ' => $cshp_dec,
                    'AR' => $cfor_q3_o,
                    'AS' => $cfor_q3_sc,
                    'AT' => $cfor_q3_shp,
                    'AU' => $cqty_jan,
                    'AV' => $csupplyqty_jan,
                    'AW' => $cshp_jan,
                    'AX' => $cqty_feb,
                    'AY' => $csupplyqty_feb,
                    'AZ' => $cshp_feb,
                    'BA' => $cqty_mar,
                    'BB' => $csupplyqty_mar,
                    'BC' => $cshp_mar,
                    'BD' => $cfor_q4_o,
                    'BE' => $cfor_q4_sc,
                    'BF' => $cfor_q4_shp,
                    'BG' => $cfor_tot_o,
                    'BH' => $cfor_tot_sc,
                    'BI' => $cfor_tot_shp,
                ];

                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //formula for total
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_qty = $rowNumber - 1;
            
            $tot_qty_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            
            $tot_qty_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            
            $tot_qty_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            
            $tot_qty_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            
            $tot_qty_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            
            $tot_qty_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            
            $tot_qty_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";

            $tot_qty_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            
            $tot_qty_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            
            $tot_qty_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            
            $tot_qty_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            
            $tot_qty_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            
            $tot_qty_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            
            $tot_qty_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            
            $tot_qty_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            
            $tot_qty_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            
            $tot_qty_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BI{$start_row}:BI{$end_row}),0)";

            $columnHeaders = [
                'C' => 'Total-QTY',
                'K' => $tot_qty_apr_formula,
                'L' => $tot_sc_apr_formula,
                'M' => $tot_shp_apr_formula,
                'N' => $tot_qty_may_formula,
                'O' => $tot_sc_may_formula,
                'P' => $tot_shp_may_formula,
                'Q' => $tot_qty_jun_formula,
                'R' => $tot_sc_jun_formula,
                'S' => $tot_shp_jun_formula,
                'T' => $tot_qty_q1_formula,
                'U' => $tot_sc_q1_formula,
                'V' => $tot_shp_q1_formula,
                'W' => $tot_qty_jul_formula,
                'X' => $tot_sc_jul_formula,
                'Y' => $tot_shp_jul_formula,
                'Z' => $tot_qty_aug_formula,
                'AA' => $tot_sc_aug_formula,
                'AB' => $tot_shp_aug_formula,
                'AC' => $tot_qty_sep_formula,
                'AD' => $tot_sc_sep_formula,
                'AE' => $tot_shp_sep_formula,
                'AF' => $tot_qty_q2_formula,
                'AG' => $tot_sc_q2_formula,
                'AH' => $tot_shp_q2_formula,
                'AI' => $tot_qty_oct_formula,
                'AJ' => $tot_sc_oct_formula,
                'AK' => $tot_shp_oct_formula,
                'AL' => $tot_qty_nov_formula,
                'AM' => $tot_sc_nov_formula,
                'AN' => $tot_shp_nov_formula,
                'AO' => $tot_qty_dec_formula,
                'AP' => $tot_sc_dec_formula,
                'AQ' => $tot_shp_dec_formula,
                'AR' => $tot_qty_q3_formula,
                'AS' => $tot_sc_q3_formula,
                'AT' => $tot_shp_q3_formula,
                'AU' => $tot_qty_jan_formula,
                'AV' => $tot_sc_jan_formula,
                'AW' => $tot_shp_jan_formula,
                'AX' => $tot_qty_feb_formula,
                'AY' => $tot_sc_feb_formula,
                'AZ' => $tot_shp_feb_formula,
                'BA' => $tot_qty_mar_formula,
                'BB' => $tot_sc_mar_formula,
                'BC' => $tot_shp_mar_formula,
                'BD' => $tot_qty_q4_formula,
                'BE' => $tot_sc_q4_formula,
                'BF' => $tot_shp_q4_formula,
                'BG' => $tot_qty_tq_formula,
                'BH' => $tot_sc_tq_formula,
                'BI' => $tot_shp_tq_formula,
            ];
            
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            // Add filter for row 9
            $activeWorksheet->setAutoFilter('A9:BI9');

            // Add filter for row 9
            //bold total
            $boldrow = 'A' . $rowNumber . ':BI' . $rowNumber;
            $activeWorksheet->getStyle("$boldrow")->getFont()->setBold(true);
            $highestRow = $activeWorksheet->getHighestRow();

            $monRange = "{$monthrows[$month]}10:{$monthrows[$month]}{$lastpdtrow}";
            setFillColor($activeWorksheet, "{$monRange}", 'C4D79B'); // Light green
            // Set fill colors for quarters
            setFillColor($activeWorksheet, 'T8:V' . $lastpdtrow, 'ccffff'); // Quarter 1
            setFillColor($activeWorksheet, 'AF8:AH' . $lastpdtrow, 'ccffff'); // Quarter 2
            setFillColor($activeWorksheet, 'AR8:AT' . $lastpdtrow, 'ccffff'); // Quarter 3
            setFillColor($activeWorksheet, 'BD8:BF' . $lastpdtrow, 'ccffff'); // Quarter 4
            // Set fill color for total
            setFillColor($activeWorksheet, 'BG8:BI' . $lastpdtrow, '99ccff'); // Dark blue
            // Set fill colors for fixed sections
            setFillColor($activeWorksheet, 'A1:BI1', 'CC99FF'); // Fixed Order Form
            setFillColor($activeWorksheet, 'C9:H' . $lastpdtrow, 'ffff99'); // Yellow
            setFillColor($activeWorksheet, 'A8:BI8', 'CCFFCC'); // Light green
            setFillColor($activeWorksheet, 'A8:C' . $lastpdtrow, 'CCFFCC'); // Light green

            /* if (!empty($arr_supply_columns_colour)) {
              foreach ($arr_supply_columns_colour as $eachsupplycolor) {
              setFillColor($activeWorksheet, "{$eachsupplycolor}:{$eachsupplycolor}" , 'ffc000');
              $rowNumber++;
              $columnHeaders = ['C' => $eachsupplycolor];
              setData($activeWorksheet, $rowNumber, $columnHeaders);
              }
              } */
            // Set fill color for total qty and amount
            $oddColor_a = 'e6f3ff'; // Specify your odd row color here
            $evenColor_a = 'cce6ff'; // Even row color
            for ($row1 = $start_row_amt; $row1 <= $end_row_amt; $row1++) {
                $color = $row1 % 2 == 0 ? $evenColor_a : $oddColor_a;
                $range_c1 = "A{$row1}:BI{$row1}";
                setFillColor($activeWorksheet, $range_c1, $color);
            }
            $oddColor_q = 'e6ffcc'; // Specify your odd row color here
            $evenColor_q = 'cdff99'; // Even row color
            for ($row = $start_row_qty; $row <= $end_row_qty; $row++) {
                $color = $row % 2 == 0 ? $evenColor_q : $oddColor_q;
                $range_c2 = "A{$row}:BI{$row}";
                setFillColor($activeWorksheet, $range_c2, $color);
            }


            // Set thiN border line from A1 to LAST
            $allColumnsRange = "A8:BI{$highestRow}";
            $activeWorksheet->getStyle($allColumnsRange)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
            // Set thick border line from J8 to L997
            $columns = ['K', 'N', 'Q', 'T', 'W', 'Z', 'AC', 'AF', 'AI', 'AL', 'AO', 'AR', 'AU', 'AX', 'BA', 'BD', 'BG'];
            foreach ($columns as $startColumn) {
                $endColumn = $startColumn;
                for ($i = 0; $i < 2; $i++) {
                    $endColumn++;
                }
                $range = "{$startColumn}8:{$endColumn}{$end_row_qty}";
                $activeWorksheet->getStyle($range)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);
            }
            $width = 25; // Desired width in pixels
            for ($row = $start_row_amt; $row <= $end_row_qty; $row++) {
                $activeWorksheet->getRowDimension($row)->setRowHeight($width);
            }
            $activeWorksheet->getRowDimension('9')->setRowHeight('44');

            // Define the range
            $range_for_number_comma = "I{$start_row_amt}:BI{$end_row_amt}";
            $range_comma_2 = "F10:F{$lastpdtrow}";
            // Set comma-separated number format for the ranges
            $activeWorksheet->getStyle($range_for_number_comma)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');

            $activeWorksheet->getStyle($range_comma_2)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');
            // Create a writer and save the spreadsheet to a file
            $writer = new Xlsx($spreadsheet);
            $mod_dis = Distributers::model()->findByPk((int) $distributer_id);
            $file_name = 'Order_' . $mod_dis->distributer_name . '_' . $year . '.xlsx';
            // Create a writer
            // Set the appropriate HTTP headers for Excel file download
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment; filename="' . $file_name . '"');
            header('Cache-Control: max-age=0');
            // Save the spreadsheet to the output (i.e., it will trigger a file download)
            $writer->save('php://output');
            return;
        } else {
            throw new CHttpException(404, 'Page not found.');
        }
    }
    
    
    /*public function actionDownloadOrderFormatExcelNew() {

        $_GET = filter_input_array(INPUT_GET, FILTER_SANITIZE_SPECIAL_CHARS);

        if (count($_GET) > 0) {

            $type = Yii::app()->request->getParam('type');

            $distributer_id = Yii::app()->request->getParam('distributer_id');

            $year = Yii::app()->request->getParam('year');

            $month = Yii::app()->request->getParam('month');

            $price_date = $year . '-' . $month . '-' . '01';
            
			$mod_dis = Distributers::model()->with('region_det')->findByPk((int) $distributer_id);
			$region_id = (int) $mod_dis->region_id;
            $cndcat = 'show_in_order="Y" AND (I.is_active="Y" OR (I.is_active="D" AND ((discontinuing_month>=' . (int) $month . ' AND discontinuing_year=' . (int) $year . ') OR discontinuing_year>' . (int) $year . ')))';
            $arr_gmcs = [];
            if ((int)$mod_dis->distributer_type> 0) {				
				$cndcat .= ' AND I.id NOT IN (SELECT IFNULL(product_id,0) AS product_id FROM {{hidden_distributer_gmcs}} WHERE distributer_type_id='.(int)$mod_dis->distributer_type.')';
            }
            
            $sql_inventory = "SELECT I.id,goods_in_end_month,goods_in_end_year,product_category_id,gmc_code,model_no,cbm,base_code,I.is_active,discontinuing_month,gmc_type,C.product_category
                        FROM {{inventory}} I 
                        LEFT JOIN {{mas_product_category}} C ON C.id = I.product_category_id
                        WHERE " . $cndcat."
                        ORDER by product_category_id,gmc_sort_order ASC
                        
                        ";//LIMIT 950
			
			$model_inventory = Yii::app()->db->createCommand($sql_inventory)->queryAll();
            $sql_category = "SELECT C.id AS id,C.product_category
                        FROM {{inventory}} I
                        LEFT JOIN {{mas_product_category}} C ON C.id = I.product_category_id
                        WHERE " . $cndcat."
                        GROUP BY C.id
                        ORDER by C.sort_order ASC
                        ";
            $modelmascategories = Yii::app()->db->createCommand($sql_category)->queryAll();           
            //$modelmascategories = MasProductCategory::model()->findAll(array('condition' => 'id IN (' . $sql_category . ')', 'order' => 'sort_order ASC'));
                        
            $file = fopen("upload/test.csv", "w");

            $j = 0;

            

            $arr_months = Order::getmonths();
            $monthrows = ['4' => 'J', '5' => 'M', '6' => 'P', '7' => 'V', '8' => 'Y', '9' => 'AB', '10' => 'AH', '11' => 'AK', '12' => 'AN', '1' => 'AT', '2' => 'AW', '3' => 'AZ'];

            if ($month <= 3) {
                $year = $year - 1;
            }
            $year1 = (int) $year + 1;
            $arrInpMonth = [];
            for ($mon = 1; $mon <= 12; $mon++) {
                $arrInpMonth[$mon] = '';
            }
            if ((int) $month > 0) {
                $arrInpMonth[$month] = '                        Input your Order in this month';
            }
            function setData($worksheet, $erow, $data) {
                foreach ($data as $column => $value) {
                    $cell = $column . $erow;
                    $worksheet->setCellValue($cell, $value);
                }
            }

            function setFillColor($worksheet, $range, $color) {
                $worksheet->getStyle($range)->getFill()->setFillType(\PhpOffice\PhpSpreadsheet\Style\Fill::FILL_SOLID)->getStartColor()->setARGB($color);
            }

            $spreadsheet = new Spreadsheet();
            $activeWorksheet = $spreadsheet->getActiveSheet();
            
            $rowNumber = 1;
            $activeWorksheet->mergeCells('A1:CA1');
            $activeWorksheet->setCellValue('A1', 'FIXED ORDER FORM');
            $modelComments = new MasComments();
            $modelComments = $modelComments->findAll(['condition'=>'id<6','order'=>'id ASC']);
            
            // Array to store comments
            $comments = [];
            
            if (!empty($modelComments)) {
                foreach($modelComments as $j => $each) {
                    $k = (int)$j + 1;
                    $comments[$k] = $each->comments; // Storing comments in the array
                }
                // Writing comments to the worksheet
                foreach ($comments as $k => $comment) {
                    $rowNumber++;
                    $activeWorksheet->mergeCells("F{$rowNumber}:N{$rowNumber}");
                    $activeWorksheet->setCellValue("F{$rowNumber}", $comment);
                }
            }else{
                 $rowNumber=  $rowNumber+5;
            }
            $rowNumber = 7;
            $activeWorksheet->mergeCells("A{$rowNumber}:B{$rowNumber}");

            // Set text wrap for cells 
            //In this code, the $cellsToWrap array contains the references to the cells that you want to set as text wrap. The loop iterates through the array and applies the text wrap setting to each of these cells individually.
            $cellsToWrap = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8", "M8", "P8", "S8", "V8", "Y8", "AB8", "AE8", "AH8", "AK8", "AN8", "AQ8", "AT8", "AW8", "AZ8", "BC8", "BF8", "J{$rowNumber}", "M{$rowNumber}", "P{$rowNumber}", "V{$rowNumber}", "Y{$rowNumber}", "AB{$rowNumber}", "AH{$rowNumber}", "AK{$rowNumber}", "AN{$rowNumber}", "AT{$rowNumber}", "AW{$rowNumber}", "AZ{$rowNumber}"];
            foreach ($cellsToWrap as $cellReference) {
                $cell = $activeWorksheet->getCell($cellReference);
                $cell->getStyle()->getAlignment()->setWrapText(true);
            }

            //You can adjust the $cellsToWrap array to include other cell references as needed, and you can also set additional formatting properties for these cells within the loop.
            // Create an arrow image and insert it into cell $ordered_month--start
            $ordered_month = $monthrows[$month] . $rowNumber;
            $imagePath = __DIR__ . '/../../../images/darrow.png'; // Adjust the path as needed
            $drawing = new Drawing();
            $drawing->setPath("$imagePath");
            $drawing->setDescription('Input your Order in this month');
            $drawing->setCoordinates($ordered_month);
            $drawing->setWorksheet($activeWorksheet);
            $drawing->setHeight(30); // Adjust the height as needed
            // Create an arrow image and insert it into cell $ordered_month--end
            // Set column width for columns J to BH --start
            for ($column = 'J'; $column <= 'BH'; $column++) {
                $activeWorksheet->getColumnDimension($column)->setWidth(11.09);
            }
            //GECI-column width
            $activeWorksheet->getColumnDimension('C')->setWidth(23.6);
            $activeWorksheet->getColumnDimension('E')->setWidth(42.3);
            $activeWorksheet->getColumnDimension('G')->setWidth(14.22);
            $activeWorksheet->getColumnDimension('I')->setWidth(18.5);
            // Set column width for columns J to BH --end
            $columnHeaders = [
                'A' => 'DISTRIBUTOR',
                'C' => $mod_dis->distributer_name,
                'J' => $arrInpMonth[4],
                'M' => $arrInpMonth[5],
                'P' => $arrInpMonth[6],
                'V' => $arrInpMonth[7],
                'Y' => $arrInpMonth[8],
                'AB' => $arrInpMonth[9],
                'AH' => $arrInpMonth[10],
                'AK' => $arrInpMonth[11],
                'AN' => $arrInpMonth[12],
                'AT' => $arrInpMonth[1],
                'AW' => $arrInpMonth[2],
                'AZ' => $arrInpMonth[3],
            ];

            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $rowNumber = (int)$rowNumber + 1; // Increment row number
            
            // The row number where you want to set these values

            // Columns and their respective formats
            $columnFormats = [
                "J" => 'mmm yyyy',
                "M" => 'mmm yyyy',
                "P" => 'mmm yyyy',
                "V" => 'mmm yyyy',
                "Y" => 'mmm yyyy',
                "AB" => 'mmm yyyy',
                "AH" => 'mmm yyyy',
                "AK" => 'mmm yyyy',
                "AN" => 'mmm yyyy',
                "AT" => 'mmm yyyy',
                "AW" => 'mmm yyyy',
                "AZ" => 'mmm yyyy',
            ];
            
            // Merge cells and set number formats
            foreach ($columnFormats as $column => $format) {
                //$activeWorksheet->mergeCells("{$column}{$rowNumber}:{$column}{$rowNumber}");
                $activeWorksheet->getStyle("{$column}{$rowNumber}")->getNumberFormat()->setFormatCode($format);
            }    
            $activeWorksheet->mergeCells("J{$rowNumber}:L{$rowNumber}");
            $activeWorksheet->mergeCells("M{$rowNumber}:O{$rowNumber}");
            $activeWorksheet->mergeCells("P{$rowNumber}:R{$rowNumber}");
            $activeWorksheet->mergeCells("S{$rowNumber}:U{$rowNumber}");
            $activeWorksheet->mergeCells("V{$rowNumber}:X{$rowNumber}");
            $activeWorksheet->mergeCells("Y{$rowNumber}:AA{$rowNumber}");
            $activeWorksheet->mergeCells("AB{$rowNumber}:AD{$rowNumber}");
            $activeWorksheet->mergeCells("AE{$rowNumber}:AG{$rowNumber}");
            $activeWorksheet->mergeCells("AH{$rowNumber}:AJ{$rowNumber}");
            $activeWorksheet->mergeCells("AK{$rowNumber}:AM{$rowNumber}");
            $activeWorksheet->mergeCells("AN{$rowNumber}:AP{$rowNumber}");
            $activeWorksheet->mergeCells("AQ{$rowNumber}:AS{$rowNumber}");
            $activeWorksheet->mergeCells("AT{$rowNumber}:AV{$rowNumber}");
            $activeWorksheet->mergeCells("AW{$rowNumber}:AY{$rowNumber}");
            $activeWorksheet->mergeCells("AZ{$rowNumber}:BB{$rowNumber}");
            $activeWorksheet->mergeCells("BC{$rowNumber}:BE{$rowNumber}");
            $activeWorksheet->mergeCells("BF{$rowNumber}:BH{$rowNumber}");
                
            $columnHeaders = [
                'C' => 'PRODUCT CATEGORY',
                'D' => 'GMC',
                'E' => 'Model',
                'F' => 'YMGF Price (' . $mod_dis->region_det->currency_type . ')',
                'G' => 'Supply confirmation month',
                'H' => 'CBM',
                'I' => 'BASE CODE',
                'J' => "=DATE({$year},4,1)",
                'M' => "=DATE({$year},5,1)",
                'P' => "=DATE({$year},6,1)",
                'S' => '1Q TOTAL',
                'V' => "=DATE({$year},7,1)",
                'Y' => "=DATE({$year},8,1)",
                'AB' => "=DATE({$year},9,1)",
                'AE' => '2Q TOTAL',
                'AH' => "=DATE({$year},10,1)",
                'AK' => "=DATE({$year},11,1)",
                'AN' => "=DATE({$year},12,1)",
                'AQ' => '3Q TOTAL',
                'AT' => "=DATE({$year1},1,1)",
                'AW' => "=DATE({$year1},2,1)",
                'AZ' => "=DATE({$year1},3,1)",
                'BC' => '4Q TOTAL',
                'BF' => 'TOTAL ' . $year . ' - ' . $year1,
            ];

            // Hide the "I" column (Base Code)--start
            $activeWorksheet->getColumnDimension('I')->setVisible(false);
            // Hide the "I" column (Base Code)--end
            // Set text wrap and alignment for row number 8
            $cellsToWrapAndCenter = ["C8", "D8", "E8", "F8", "G8", "H8", "I8", "J8", "M8", "P8", "S8", "V8", "Y8", "AB8", "AE8", "AH8", "AK8", "AN8", "AQ8", "AT8", "AW8", "AZ8", "BC8", "BF8"];

            foreach ($cellsToWrapAndCenter as $cellReference) {
                $cell = $activeWorksheet->getCell($cellReference);
                $cell->getStyle()->getAlignment()->setWrapText(true);
                $cell->getStyle()->getAlignment()->setHorizontal(\PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER);
            }
            // Set text wrap and alignment for row number 8
            setData($activeWorksheet, $rowNumber, $columnHeaders);

            //bold total
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            // Freeze the pane at cell J10
            $activeWorksheet->freezePane('J10');
            $rowNumber = $rowNumber = $rowNumber + 1; // The row number where you want to set these values
            $arr_supply_columns_colour = [];
            $columnHeaders = [
                'A' => ' ',
                'B' => ' ',
                'C' => ' ',
                'D' => ' ',
                'E' => ' ',
                'F' => ' ',
                'G' => ' ',
                'H' => ' ',
                'I' => ' ',
                'J' => 'ORDER',
                'K' => 'Supply Confirmation',
                'L' => 'SHIP',
                'M' => 'ORDER',
                'N' => 'Supply Confirmation',
                'O' => 'SHIP',
                'P' => 'ORDER',
                'Q' => 'Supply Confirmation',
                'R' => 'SHIP',
                'S' => 'ORDER',
                'T' => 'Supply Confirmation',
                'U' => 'SHIP',
                'V' => 'ORDER',
                'W' => 'Supply Confirmation',
                'X' => 'SHIP',
                'Y' => 'ORDER',
                'Z' => 'Supply Confirmation',
                'AA' => 'SHIP',
                'AB' => 'ORDER',
                'AC' => 'Supply Confirmation',
                'AD' => 'SHIP',
                'AE' => 'ORDER',
                'AF' => 'Supply Confirmation',
                'AG' => 'SHIP',
                'AH' => 'ORDER',
                'AI' => 'Supply Confirmation',
                'AJ' => 'SHIP',
                'AK' => 'ORDER',
                'AL' => 'Supply Confirmation',
                'AM' => 'SHIP',
                'AN' => 'ORDER',
                'AO' => 'Supply Confirmation',
                'AP' => 'SHIP',
                'AQ' => 'ORDER',
                'AR' => 'Supply Confirmation',
                'AS' => 'SHIP',
                'AT' => 'ORDER',
                'AU' => 'Supply Confirmation',
                'AV' => 'SHIP',
                'AW' => 'ORDER',
                'AX' => 'Supply Confirmation',
                'AY' => 'SHIP',
                'AZ' => 'ORDER',
                'BA' => 'Supply Confirmation',
                'BB' => 'SHIP',
                'BC' => 'ORDER',
                'BD' => 'Supply Confirmation',
                'BE' => 'SHIP',
                'BF' => 'ORDER',
                'BG' => 'Supply Confirmation',
                'BH' => 'SHIP',
            ];

            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $rowNumber = (int)$rowNumber + 1;
            // Loop through each cell in the range "A9:BH9" and set wrap text
            for ($column = 'A'; $column <= 'BH'; $column++) {
                $cell = $column . '9';
                $activeWorksheet->getStyle($cell)->getAlignment()->setWrapText(true);
            }
            
           
            
            //category_id
            $sql_inv = "SELECT product_id,distributer_id,parent_det.month,parent_det.year,quantity_ordered,quantity_confirmed,quantity_shipped,is_done
                        FROM {{order_details}} OD
                        LEFT JOIN {{order}} parent_det ON OD.order_id=parent_det.id
                        WHERE parent_det.distributer_id =$distributer_id AND ((parent_det.month>3 AND parent_det.year=$year )OR(parent_det.month<4 AND parent_det.year=$year1))
                        GROUP BY OD.product_id,parent_det.month,parent_det.year,parent_det.distributer_id";

            $arr_inv = Yii::app()->db->createCommand($sql_inv)->queryAll();
            $arr_order = [];
            foreach ($arr_inv as $each) {
                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_ordered'] = $each['quantity_ordered'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_confirmed'] = $each['quantity_confirmed'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['quantity_shipped'] = $each['quantity_shipped'];
                $arr_order[$each['product_id']][$each['year']][$each['month']]['is_done'] = $each['is_done'];
            }

            $i = 0;
            $modelOrder = new Order();
            $arr_months = $modelOrder->getmonths();
            $old_category_id = 0;
            $arr_cat = [];
            $dis_range = [];
            foreach ($model_inventory as $i => $row) {
                // Pre-calculate constant values outside the loop
                $goodend_month_name = isset($arr_months[(int) $row['goods_in_end_month']]) ? $arr_months[(int) $row['goods_in_end_month']] : '';
                $goodend_month = $goodend_month_name . ' - ' . (int) $row['goods_in_end_year'];
                $goods_in_date = (int) $row['goods_in_end_year'] . '-' . str_pad((int) $row['goods_in_end_month'], 2, '0', STR_PAD_LEFT) . '-01';
                // Convert $goods_in_date to a DateTime object
                $goods_in_datetime = new DateTime($goods_in_date);
                // Subtract 1 month from $goods_in_datetime
                $supply_confirm_datetime = $goods_in_datetime->sub(new DateInterval('P1M'));
                // Format $supply_confirm_month
                $supply_confirm_month = $supply_confirm_datetime->format('F - Y');
                // Process each row
                $processed_row = [];
            
                // Iterate over the months and extract quantity and supply data
                for ($omonth = 1; $omonth <= 12; $omonth++) {
                    $oyear = ($omonth <= 3) ? $year1 : $year;
                    $quantity_ordered = isset($arr_order[(int)$row['id']][$oyear][$omonth]['quantity_ordered']) ? (int)$arr_order[(int)$row['id']][$oyear][$omonth]['quantity_ordered'] : 0;
                    $quantity_confirmed = isset($arr_order[(int)$row['id']][$oyear][$omonth]['quantity_confirmed']) ? (int)$arr_order[(int)$row['id']][$oyear][$omonth]['quantity_confirmed'] : 0;
                    $quantity_shipped = isset($arr_order[(int)$row['id']][$oyear][$omonth]['quantity_shipped']) ? (int)$arr_order[(int)$row['id']][$oyear][$omonth]['quantity_shipped'] : 0;
            
                    $processed_row[$omonth] = [
                        'quantity_ordered' => $quantity_ordered,
                        'quantity_confirmed' => $quantity_confirmed,
                        'quantity_shipped' => $quantity_shipped,
                    ];
                }
                $for_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $for_q1_sc = "=IFERROR(K{$rowNumber}+N{$rowNumber}+Q{$rowNumber},0)";
                $for_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";
                $for_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $for_q2_sc = "=IFERROR(W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber},0)";
                $for_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";
                $for_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $for_q3_sc = "=IFERROR(AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber},0)";
                $for_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";
                $for_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $for_q4_sc = "=IFERROR(AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber},0)";
                $for_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";
                $for_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $for_tot_sc = "=IFERROR(T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber},0)";
                $for_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                //for categorywise--sum --start
                if ($old_category_id != $row['product_category_id']) {
                    $arr_cat[$row['product_category_id']]['start'] = $rowNumber;
                    $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
                    $old_category_id = $row['product_category_id'];
                }
                //for categorywise--sum --end
                // region value 

                $price_cnd = 'product_id=' . (int) $row['id'] . ' AND region_id=' . (int) $region_id . ' AND from_date <="' . $price_date . '" AND to_date >="' . $price_date . '"';
                $mod_inv_price = InventoryPrice::model()->find(array('condition' => $price_cnd));
                $ymg_price = !empty($mod_inv_price) ? $mod_inv_price['price'] : '00" - (TBA)"';

                //region value 
                $new_discontinue = Inventory::model()->getinvtype((int) $row['id'], $month, $year);
                $pair_name = ($row['gmc_type'] == 'P') ? 'PAIR' : (($row['gmc_type'] == 'K') ? 'KIT' : '');
                $columnHeaders = [
                    'A' => $new_discontinue,
                    'B' => $pair_name,
                    'C' => $row['product_category'],
                    'D' => $row['gmc_code'],
                    'E' => $row['model_no'],
                    'F' => $ymg_price,
                    'G' => $supply_confirm_month,
                    'H' => $row['cbm'],
                    'I' => $row['base_code'],
                    'J' => $processed_row[4]['quantity_ordered'],
                    'K' => $processed_row[4]['quantity_confirmed'],
                    'L' => $processed_row[4]['quantity_shipped'],
                    'M' => $processed_row[5]['quantity_ordered'],
                    'N' => $processed_row[5]['quantity_confirmed'],
                    'O' => $processed_row[5]['quantity_shipped'],
                    'P' => $processed_row[6]['quantity_ordered'],
                    'Q' => $processed_row[6]['quantity_confirmed'],
                    'R' => $processed_row[6]['quantity_shipped'],
                    'S' => $for_q1_o,
                    'T' => $for_q1_sc,
                    'U' => $for_q1_shp,
                    'V' => $processed_row[7]['quantity_ordered'],
                    'W' => $processed_row[7]['quantity_confirmed'],
                    'X' => $processed_row[7]['quantity_shipped'],
                    'Y' => $processed_row[8]['quantity_ordered'],
                    'Z' => $processed_row[8]['quantity_confirmed'],
                    'AA' => $processed_row[8]['quantity_shipped'],
                    'AB' => $processed_row[9]['quantity_ordered'],
                    'AC' => $processed_row[9]['quantity_confirmed'],
                    'AD' => $processed_row[9]['quantity_shipped'],
                    'AE' => $for_q2_o,
                    'AF' => $for_q2_sc,
                    'AG' => $for_q2_shp,
                    'AH' => $processed_row[10]['quantity_ordered'],
                    'AI' => $processed_row[10]['quantity_confirmed'],
                    'AJ' => $processed_row[10]['quantity_shipped'],
                    'AK' => $processed_row[11]['quantity_ordered'],
                    'AL' => $processed_row[11]['quantity_confirmed'],
                    'AM' => $processed_row[11]['quantity_shipped'],
                    'AN' => $processed_row[12]['quantity_ordered'],
                    'AO' => $processed_row[12]['quantity_confirmed'],
                    'AP' => $processed_row[12]['quantity_shipped'],
                    'AQ' => $for_q3_o,
                    'AR' => $for_q3_sc,
                    'AS' => $for_q3_shp,
                    'AT' => $processed_row[1]['quantity_ordered'],
                    'AU' => $processed_row[1]['quantity_confirmed'],
                    'AV' => $processed_row[1]['quantity_shipped'],
                    'AW' => $processed_row[2]['quantity_ordered'],
                    'AX' => $processed_row[2]['quantity_confirmed'],
                    'AY' => $processed_row[2]['quantity_shipped'],
                    'AZ' => $processed_row[3]['quantity_ordered'],
                    'BA' => $processed_row[3]['quantity_confirmed'],
                    'BB' => $processed_row[3]['quantity_shipped'],
                    'BC' => $for_q4_o,
                    'BD' => $for_q4_sc,
                    'BE' => $for_q4_shp,
                    'BF' => $for_tot_o,
                    'BG' => $for_tot_sc,
                    'BH' => $for_tot_shp,
                ];
                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //discontinuing gmc-- colour grey----start
                if ($row['is_active'] == 'D') {
                    $dis_range[] = "{$monthrows[$row['discontinuing_month']]}{$rowNumber}:BH{$rowNumber}";
                    //setFillColor($activeWorksheet, $dis_range, 'b1b1b1'); // Quarter 1
                }
                //discontinuing gmc-- colour grey----end
                $rowNumber++;
            }

           
            $lastpdtrow = (int)$rowNumber - 1;

            if (!empty($dis_range)) {
                foreach ($dis_range as $each_discontinue) {
                    setFillColor($activeWorksheet, $each_discontinue, 'b1b1b1');
                }
            }
            $arr_cat[$old_category_id]['end'] = $rowNumber - 1;
            $rowNumber++;
            $columnHeaders = [
                'C' => 'PRODUCT CATEGORY',
                'I' => 'BASE CODE',
                'J' => " ",
                'M' => " ",
                'P' => " ",
                'S' => '1Q TOTAL',
                'V' => " ",
                'Y' => " ",
                'AB' => " ",
                'AE' => '2Q TOTAL',
                'AH' => " ",
                'AK' => " ",
                'AN' => " ",
                'AQ' => '3Q TOTAL',
                'AT' => " ",
                'AW' => " ",
                'AZ' => " ",
                'BC' => '4Q TOTAL',
                'BF' => 'TOTAL ' . $year . ' - ' . $year1,
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $rowNumber++;

            $start_row_amt = $rowNumber;
            $start_row = $rowNumber;
            // Define months as an array
            $cat_months = ['apr', 'may', 'jun', 'jul', 'aug', 'sep', 'oct', 'nov', 'dec', 'jan', 'feb', 'mar'];

            foreach ($modelmascategories as $eachcat) {
                if (isset($arr_cat[(int) $eachcat['id']])) {
                    // The element exists in the array
                    $start = $arr_cat[(int) $eachcat['id']]['start'];
                    $end = $arr_cat[(int) $eachcat['id']]['end'];
                } else {
                    // The element does not exist in the array
                    // to avoid circular reference
                    $start = 1;
                    $end = 1;
                }

                $camt_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},J{$start}:J{$end}),0)";
                $camt_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},M{$start}:M{$end}),0)";
                $camt_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},P{$start}:P{$end}),0)";
                $camt_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},V{$start}:V{$end}),0)";
                $camt_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Y{$start}:Y{$end}),0)";
                $camt_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AB{$start}:AB{$end}),0)";
                $camt_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AH{$start}:AH{$end}),0)";
                $camt_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AK{$start}:AK{$end}),0)";
                $camt_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AN{$start}:AN{$end}),0)";
                $camt_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AT{$start}:AT{$end}),0)";
                $camt_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AW{$start}:AW{$end}),0)";
                $camt_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AZ{$start}:AZ{$end}),0)";

                //supplyconfirmed

                $csupplyamt_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},K{$start}:K{$end}),0)";
                $csupplyamt_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},N{$start}:N{$end}),0)";
                $csupplyamt_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Q{$start}:Q{$end}),0)";
                $csupplyamt_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},W{$start}:W{$end}),0)";
                $csupplyamt_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},Z{$start}:Z{$end}),0)";
                $csupplyamt_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AC{$start}:AC{$end}),0)";
                $csupplyamt_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AI{$start}:AI{$end}),0)";
                $csupplyamt_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AL{$start}:AL{$end}),0)";
                $csupplyamt_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AO{$start}:AO{$end}),0)";
                $csupplyamt_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AU{$start}:AU{$end}),0)";
                $csupplyamt_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AX{$start}:AX{$end}),0)";
                $csupplyamt_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},BA{$start}:BA{$end}),0)";

                //shipped
                $cshp_apr = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},L{$start}:L{$end}),0)";
                $cshp_may = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},O{$start}:O{$end}),0)";
                $cshp_jun = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},R{$start}:R{$end}),0)";
                $cshp_jul = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},X{$start}:X{$end}),0)";
                $cshp_aug = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AA{$start}:AA{$end}),0)";
                $cshp_sep = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AD{$start}:AD{$end}),0)";
                $cshp_oct = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AJ{$start}:AJ{$end}),0)";
                $cshp_nov = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AM{$start}:AM{$end}),0)";
                $cshp_dec = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AP{$start}:AP{$end}),0)";
                $cshp_jan = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AV{$start}:AV{$end}),0)";
                $cshp_feb = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},AY{$start}:AY{$end}),0)";
                $cshp_mar = "=IFERROR(SUMPRODUCT(F{$start}:F{$end},BB{$start}:BB{$end}),0)";

                //formulas for category
                $cfor_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $cfor_q1_sc = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $cfor_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";

                $cfor_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $cfor_q2_sc = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $cfor_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";

                $cfor_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $cfor_q3_sc = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $cfor_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";

                $cfor_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $cfor_q4_sc = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $cfor_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";

                $cfor_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $cfor_tot_sc = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $cfor_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";
                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - AMT',
                    'J' => $camt_apr,
                    'K' => $csupplyamt_apr,
                    'L' => $cshp_apr,
                    'M' => $camt_may,
                    'N' => $csupplyamt_may,
                    'O' => $cshp_may,
                    'P' => $camt_jun,
                    'Q' => $csupplyamt_jun,
                    'R' => $cshp_jun,
                    'S' => $cfor_q1_o,
                    'T' => $cfor_q1_sc,
                    'U' => $cfor_q1_shp,
                    'V' => $camt_jul,
                    'W' => $csupplyamt_jul,
                    'X' => $cshp_jul,
                    'Y' => $camt_aug,
                    'Z' => $csupplyamt_aug,
                    'AA' => $cshp_aug,
                    'AB' => $camt_sep,
                    'AC' => $csupplyamt_sep,
                    'AD' => $cshp_sep,
                    'AE' => $cfor_q2_o,
                    'AF' => $cfor_q2_sc,
                    'AG' => $cfor_q2_shp,
                    'AH' => $camt_oct,
                    'AI' => $csupplyamt_oct,
                    'AJ' => $cshp_oct,
                    'AK' => $camt_nov,
                    'AL' => $csupplyamt_nov,
                    'AM' => $cshp_nov,
                    'AN' => $camt_dec,
                    'AO' => $csupplyamt_dec,
                    'AP' => $cshp_dec,
                    'AQ' => $cfor_q3_o,
                    'AR' => $cfor_q3_sc,
                    'AS' => $cfor_q3_shp,
                    'AT' => $camt_jan,
                    'AU' => $csupplyamt_jan,
                    'AV' => $cshp_jan,
                    'AW' => $camt_feb,
                    'AX' => $csupplyamt_feb,
                    'AY' => $cshp_feb,
                    'AZ' => $camt_mar,
                    'BA' => $csupplyamt_mar,
                    'BB' => $cshp_mar,
                    'BC' => $cfor_q4_o,
                    'BD' => $cfor_q4_sc,
                    'BE' => $cfor_q4_shp,
                    'BF' => $cfor_tot_o,
                    'BG' => $cfor_tot_sc,
                    'BH' => $cfor_tot_shp,
                ];

                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //formula for total
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_amt = $rowNumber;

            $tot_qty_apr_formula = "=IFERROR(SUM(J{$start_row}:J{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";

            $tot_qty_may_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";

            $tot_qty_jun_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";

            $tot_qty_q1_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";

            $tot_qty_jul_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";

            $tot_qty_aug_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";

            $tot_qty_sep_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";

            $tot_qty_q2_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";

            $tot_qty_oct_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";

            $tot_qty_nov_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";

            $tot_qty_dec_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";

            $tot_qty_q3_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";

            $tot_qty_jan_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";

            $tot_qty_feb_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";

            $tot_qty_mar_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";

            $tot_qty_q4_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";

            $tot_qty_tq_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";
            // Set the entire total row as bold

            $columnHeaders = [
                'C' => 'Total - Amt',
                'J' => $tot_qty_apr_formula,
                'K' => $tot_sc_apr_formula,
                'L' => $tot_shp_apr_formula,
                'M' => $tot_qty_may_formula,
                'N' => $tot_sc_may_formula,
                'O' => $tot_shp_may_formula,
                'P' => $tot_qty_jun_formula,
                'Q' => $tot_sc_jun_formula,
                'R' => $tot_shp_jun_formula,
                'S' => $tot_qty_q1_formula,
                'T' => $tot_sc_q1_formula,
                'U' => $tot_shp_q1_formula,
                'V' => $tot_qty_jul_formula,
                'W' => $tot_sc_jul_formula,
                'X' => $tot_shp_jul_formula,
                'Y' => $tot_qty_aug_formula,
                'Z' => $tot_sc_aug_formula,
                'AA' => $tot_shp_aug_formula,
                'AB' => $tot_qty_sep_formula,
                'AC' => $tot_sc_sep_formula,
                'AD' => $tot_shp_sep_formula,
                'AE' => $tot_qty_q2_formula,
                'AF' => $tot_sc_q2_formula,
                'AG' => $tot_shp_q2_formula,
                'AH' => $tot_qty_oct_formula,
                'AI' => $tot_sc_oct_formula,
                'AJ' => $tot_shp_oct_formula,
                'AK' => $tot_qty_nov_formula,
                'AL' => $tot_sc_nov_formula,
                'AM' => $tot_shp_nov_formula,
                'AN' => $tot_qty_dec_formula,
                'AO' => $tot_sc_dec_formula,
                'AP' => $tot_shp_dec_formula,
                'AQ' => $tot_qty_q3_formula,
                'AR' => $tot_sc_q3_formula,
                'AS' => $tot_shp_q3_formula,
                'AT' => $tot_qty_jan_formula,
                'AU' => $tot_sc_jan_formula,
                'AV' => $tot_shp_jan_formula,
                'AW' => $tot_qty_feb_formula,
                'AX' => $tot_sc_feb_formula,
                'AY' => $tot_shp_feb_formula,
                'AZ' => $tot_qty_mar_formula,
                'AA' => $tot_sc_mar_formula,
                'BB' => $tot_shp_mar_formula,
                'BC' => $tot_qty_q4_formula,
                'BD' => $tot_sc_q4_formula,
                'BE' => $tot_shp_q4_formula,
                'BF' => $tot_qty_tq_formula,
                'BG' => $tot_sc_tq_formula,
                'BH' => $tot_shp_tq_formula,
            ];
            $tot_rowno = $rowNumber;
            setData($activeWorksheet, $rowNumber, $columnHeaders);

            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //cbmTotal

            $columnHeaders = [
                'C' => 'CBM Total',
                'J' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},J10:J{$lastpdtrow}),0)",
                'K' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},K10:K{$lastpdtrow}),0)",
                'L' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},L10:L{$lastpdtrow}),0)",
                'M' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},M10:M{$lastpdtrow}),0)",
                'N' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},N10:N{$lastpdtrow}),0)",
                'O' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},O10:O{$lastpdtrow}),0)",
                'P' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},P10:P{$lastpdtrow}),0)",
                'Q' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Q10:Q{$lastpdtrow}),0)",
                'R' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},R10:R{$lastpdtrow}),0)",
                'S' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},S10:S{$lastpdtrow}),0)",
                'T' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},T10:T{$lastpdtrow}),0)",
                'U' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},U10:U{$lastpdtrow}),0)",
                'V' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},V10:V{$lastpdtrow}),0)",
                'W' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},W10:W{$lastpdtrow}),0)",
                'X' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},X10:X{$lastpdtrow}),0)",
                'Y' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Y10:Y{$lastpdtrow}),0)",
                'Z' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},Z10:Z{$lastpdtrow}),0)",
                'AA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AA10:AA{$lastpdtrow}),0)",
                'AB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AB10:AB{$lastpdtrow}),0)",
                'AC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AC10:AC{$lastpdtrow}),0)",
                'AD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AD10:A{$lastpdtrow}),0)",
                'AE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AE10:AE{$lastpdtrow}),0)",
                'AF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AF10:AF{$lastpdtrow}),0)",
                'AG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AG10:AG{$lastpdtrow}),0)",
                'AH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AH10:AH{$lastpdtrow}),0)",
                'AI' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AI10:AI{$lastpdtrow}),0)",
                'AJ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AJ10:AJ{$lastpdtrow}),0)",
                'AK' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AK10:AK{$lastpdtrow}),0)",
                'AL' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AL10:AL{$lastpdtrow}),0)",
                'AM' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AM10:AM{$lastpdtrow}),0)",
                'AN' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AN10:AN{$lastpdtrow}),0)",
                'AO' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AO10:AO{$lastpdtrow}),0)",
                'AP' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AP10:AP{$lastpdtrow}),0)",
                'AQ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AQ10:AQ{$lastpdtrow}),0)",
                'AR' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AR10:AR{$lastpdtrow}),0)",
                'AS' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AS10:AS{$lastpdtrow}),0)",
                'AT' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AT10:AT{$lastpdtrow}),0)",
                'AU' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AU10:AU{$lastpdtrow}),0)",
                'AV' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AV10:AV{$lastpdtrow}),0)",
                'AW' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AW10:AW{$lastpdtrow}),0)",
                'AX' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AX10:AX{$lastpdtrow}),0)",
                'AY' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AY10:AY{$lastpdtrow}),0)",
                'AZ' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},AZ10:AZ{$lastpdtrow}),0)",
                'BA' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BA10:BA{$lastpdtrow}),0)",
                'BB' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BB10:BB{$lastpdtrow}),0)",
                'BC' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BC10:BC{$lastpdtrow}),0)",
                'BD' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BD10:BD{$lastpdtrow}),0)",
                'BE' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BE10:BE{$lastpdtrow}),0)",
                'BF' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BF10:BF{$lastpdtrow}),0)",
                'BG' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BG10:BG{$lastpdtrow}),0)",
                'BH' => "=IFERROR(SUMPRODUCT(H10:H{$lastpdtrow},BH10:BH{$lastpdtrow}),0)",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;
            //Difference
            $columnHeaders = [
                'C' => 'Difference',
                'L' => "=L{$tot_rowno}-K{$tot_rowno}",
                'O' => "=O{$tot_rowno}-N{$tot_rowno}",
                'R' => "=R{$tot_rowno}-Q{$tot_rowno}",
                'U' => "=U{$tot_rowno}-T{$tot_rowno}",
                'X' => "=X{$tot_rowno}-W{$tot_rowno}",
                'AA' => "=AA{$tot_rowno}-Z{$tot_rowno}",
                'AD' => "=AD{$tot_rowno}-AC{$tot_rowno}",
                'AG' => "=AG{$tot_rowno}-AF{$tot_rowno}",
                'AJ' => "=AJ{$tot_rowno}-AI{$tot_rowno}",
                'AM' => "=AM{$tot_rowno}-AL{$tot_rowno}",
                'AP' => "=AP{$tot_rowno}-AO{$tot_rowno}",
                'AS' => "=AS{$tot_rowno}-AR{$tot_rowno}",
                'AV' => "=AV{$tot_rowno}-AU{$tot_rowno}",
                'AY' => "=AY{$tot_rowno}-AX{$tot_rowno}",
                'BB' => "=BB{$tot_rowno}-BA{$tot_rowno}",
                'BE' => "=BE{$tot_rowno}-BD{$tot_rowno}",
                'BH' => "=BH{$tot_rowno}-BG{$tot_rowno}",
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            $activeWorksheet->getStyle('A' . $rowNumber . ':BH' . $rowNumber)->getFont()->setBold(true);
            $rowNumber++;

            //quantity
            $start_row_qty = $rowNumber;
            $start_row = $rowNumber;
            foreach ($modelmascategories as $eachcat) {
                if (isset($arr_cat[(int) $eachcat['id']])) {
                    // The element exists in the array
                    $start = $arr_cat[(int) $eachcat['id']]['start'];
                    $end = $arr_cat[(int) $eachcat['id']]['end'];
                } else {
                    // The element does not exist in the array
                    // to avoid circular reference
                    $start = 1;
                    $end = 1;
                }

                //ordered//
                $cqty_apr = "=IFERROR(SUM(J{$start}:J{$end}),0)";
                $cqty_may = "=IFERROR(SUM(M{$start}:M{$end}),0)";
                $cqty_jun = "=IFERROR(SUM(P{$start}:P{$end}),0)";
                $cqty_jul = "=IFERROR(SUM(V{$start}:V{$end}),0)";
                $cqty_aug = "=IFERROR(SUM(Y{$start}:Y{$end}),0)";
                $cqty_sep = "=IFERROR(SUM(AB{$start}:AB{$end}),0)";
                $cqty_oct = "=IFERROR(SUM(AH{$start}:AH{$end}),0)";
                $cqty_nov = "=IFERROR(SUM(AK{$start}:AK{$end}),0)";
                $cqty_dec = "=IFERROR(SUM(AN{$start}:AN{$end}),0)";
                $cqty_jan = "=IFERROR(SUM(AT{$start}:AT{$end}),0)";
                $cqty_feb = "=IFERROR(SUM(AW{$start}:AW{$end}),0)";
                $cqty_mar = "=IFERROR(SUM(AZ{$start}:AZ{$end}),0)";

                //supplyconfirmed

                $csupplyqty_apr = "=IFERROR(SUM(K{$start}:K{$end}),0)";
                $csupplyqty_may = "=IFERROR(SUM(N{$start}:N{$end}),0)";
                $csupplyqty_jun = "=IFERROR(SUM(Q{$start}:Q{$end}),0)";
                $csupplyqty_jul = "=IFERROR(SUM(W{$start}:W{$end}),0)";
                $csupplyqty_aug = "=IFERROR(SUM(Z{$start}:Z{$end}),0)";
                $csupplyqty_sep = "=IFERROR(SUM(AC{$start}:AC{$end}),0)";
                $csupplyqty_oct = "=IFERROR(SUM(AI{$start}:AI{$end}),0)";
                $csupplyqty_nov = "=IFERROR(SUM(AL{$start}:AL{$end}),0)";
                $csupplyqty_dec = "=IFERROR(SUM(AO{$start}:AO{$end}),0)";
                $csupplyqty_jan = "=IFERROR(SUM(AU{$start}:AU{$end}),0)";
                $csupplyqty_feb = "=IFERROR(SUM(AX{$start}:AX{$end}),0)";
                $csupplyqty_mar = "=IFERROR(SUM(BA{$start}:BA{$end}),0)";

                //shipped
                $cshp_apr = "=IFERROR(SUM(L{$start}:L{$end}),0)";
                $cshp_may = "=IFERROR(SUM(O{$start}:O{$end}),0)";
                $cshp_jun = "=IFERROR(SUM(R{$start}:R{$end}),0)";
                $cshp_jul = "=IFERROR(SUM(X{$start}:X{$end}),0)";
                $cshp_aug = "=IFERROR(SUM(AA{$start}:AA{$end}),0)";
                $cshp_sep = "=IFERROR(SUM(AD{$start}:AD{$end}),0)";
                $cshp_oct = "=IFERROR(SUM(AJ{$start}:AJ{$end}),0)";
                $cshp_nov = "=IFERROR(SUM(AM{$start}:AM{$end}),0)";
                $cshp_dec = "=IFERROR(SUM(AP{$start}:AP{$end}),0)";
                $cshp_jan = "=IFERROR(SUM(AV{$start}:AV{$end}),0)";
                $cshp_feb = "=IFERROR(SUM(AY{$start}:AY{$end}),0)";
                $cshp_mar = "=IFERROR(SUM(BB{$start}:BB{$end}),0)";

                //formulas for category
                $cfor_q1_o = "=J{$rowNumber}+M{$rowNumber}+P{$rowNumber}";
                $cfor_q1_sc = "=K{$rowNumber}+N{$rowNumber}+Q{$rowNumber}";
                $cfor_q1_shp = "=L{$rowNumber}+O{$rowNumber}+R{$rowNumber}";

                $cfor_q2_o = "=V{$rowNumber}+Y{$rowNumber}+AB{$rowNumber}";
                $cfor_q2_sc = "=W{$rowNumber}+Z{$rowNumber}+AC{$rowNumber}";
                $cfor_q2_shp = "=X{$rowNumber}+AA{$rowNumber}+AD{$rowNumber}";

                $cfor_q3_o = "=AH{$rowNumber}+AK{$rowNumber}+AN{$rowNumber}";
                $cfor_q3_sc = "=AI{$rowNumber}+AL{$rowNumber}+AO{$rowNumber}";
                $cfor_q3_shp = "=AJ{$rowNumber}+AM{$rowNumber}+AP{$rowNumber}";

                $cfor_q4_o = "=AT{$rowNumber}+AW{$rowNumber}+AZ{$rowNumber}";
                $cfor_q4_sc = "=AU{$rowNumber}+AX{$rowNumber}+BA{$rowNumber}";
                $cfor_q4_shp = "=AV{$rowNumber}+AY{$rowNumber}+BB{$rowNumber}";

                $cfor_tot_o = "=S{$rowNumber}+AE{$rowNumber}+AQ{$rowNumber}+BC{$rowNumber}";
                $cfor_tot_sc = "=T{$rowNumber}+AF{$rowNumber}+AR{$rowNumber}+BD{$rowNumber}";
                $cfor_tot_shp = "=U{$rowNumber}+AG{$rowNumber}+AS{$rowNumber}+BE{$rowNumber}";

                $columnHeaders = [
                    'C' => $eachcat['product_category'] . ' - Qty',
                    'J' => $cqty_apr,
                    'K' => $csupplyqty_apr,
                    'L' => $cshp_apr,
                    'M' => $cqty_may,
                    'N' => $csupplyqty_may,
                    'O' => $cshp_may,
                    'P' => $cqty_jun,
                    'Q' => $csupplyqty_jun,
                    'R' => $cshp_jun,
                    'S' => $cfor_q1_o,
                    'T' => $cfor_q1_sc,
                    'U' => $cfor_q1_shp,
                    'V' => $cqty_jul,
                    'W' => $csupplyqty_jul,
                    'X' => $cshp_jul,
                    'Y' => $cqty_aug,
                    'Z' => $csupplyqty_aug,
                    'AA' => $cshp_aug,
                    'AB' => $cqty_sep,
                    'AC' => $csupplyqty_sep,
                    'AD' => $cshp_sep,
                    'AE' => $cfor_q2_o,
                    'AF' => $cfor_q2_sc,
                    'AG' => $cfor_q2_shp,
                    'AH' => $cqty_oct,
                    'AI' => $csupplyqty_oct,
                    'AJ' => $cshp_oct,
                    'AK' => $cqty_nov,
                    'AL' => $csupplyqty_nov,
                    'AM' => $cshp_nov,
                    'AN' => $cqty_dec,
                    'AO' => $csupplyqty_dec,
                    'AP' => $cshp_dec,
                    'AQ' => $cfor_q3_o,
                    'AR' => $cfor_q3_sc,
                    'AS' => $cfor_q3_shp,
                    'AT' => $cqty_jan,
                    'AU' => $csupplyqty_jan,
                    'AV' => $cshp_jan,
                    'AW' => $cqty_feb,
                    'AX' => $csupplyqty_feb,
                    'AY' => $cshp_feb,
                    'AZ' => $cqty_mar,
                    'BA' => $csupplyqty_mar,
                    'BB' => $cshp_mar,
                    'BC' => $cfor_q4_o,
                    'BD' => $cfor_q4_sc,
                    'BE' => $cfor_q4_shp,
                    'BF' => $cfor_tot_o,
                    'BG' => $cfor_tot_sc,
                    'BH' => $cfor_tot_shp,
                ];

                setData($activeWorksheet, $rowNumber, $columnHeaders);
                //formula for total
                $rowNumber++;
            }
            $end_row = $rowNumber - 1;
            $end_row_qty = $rowNumber - 1;
            $tot_qty_apr_formula = "=IFERROR(SUM(J{$start_row}:J{$end_row}),0)";
            $tot_sc_apr_formula = "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)";
            $tot_shp_apr_formula = "=IFERROR(SUM(L{$start_row}:L{$end_row}),0)";

            $tot_qty_may_formula = "=IFERROR(SUM(M{$start_row}:M{$end_row}),0)";
            $tot_sc_may_formula = "=IFERROR(SUM(N{$start_row}:N{$end_row}),0)";
            $tot_shp_may_formula = "=IFERROR(SUM(O{$start_row}:O{$end_row}),0)";

            $tot_qty_jun_formula = "=IFERROR(SUM(P{$start_row}:P{$end_row}),0)";
            $tot_sc_jun_formula = "=IFERROR(SUM(Q{$start_row}:Q{$end_row}),0)";
            $tot_shp_jun_formula = "=IFERROR(SUM(R{$start_row}:R{$end_row}),0)";

            $tot_qty_q1_formula = "=IFERROR(SUM(S{$start_row}:S{$end_row}),0)";
            $tot_sc_q1_formula = "=IFERROR(SUM(T{$start_row}:T{$end_row}),0)";
            $tot_shp_q1_formula = "=IFERROR(SUM(U{$start_row}:U{$end_row}),0)";

            $tot_qty_jul_formula = "=IFERROR(SUM(V{$start_row}:V{$end_row}),0)";
            $tot_sc_jul_formula = "=IFERROR(SUM(W{$start_row}:W{$end_row}),0)";
            $tot_shp_jul_formula = "=IFERROR(SUM(X{$start_row}:X{$end_row}),0)";

            $tot_qty_aug_formula = "=IFERROR(SUM(Y{$start_row}:Y{$end_row}),0)";
            $tot_sc_aug_formula = "=IFERROR(SUM(Z{$start_row}:Z{$end_row}),0)";
            $tot_shp_aug_formula = "=IFERROR(SUM(AA{$start_row}:AA{$end_row}),0)";

            $tot_qty_sep_formula = "=IFERROR(SUM(AB{$start_row}:AB{$end_row}),0)";
            $tot_sc_sep_formula = "=IFERROR(SUM(AC{$start_row}:AC{$end_row}),0)";
            $tot_shp_sep_formula = "=IFERROR(SUM(AD{$start_row}:AD{$end_row}),0)";

            $tot_qty_q2_formula = "=IFERROR(SUM(AE{$start_row}:AE{$end_row}),0)";
            $tot_sc_q2_formula = "=IFERROR(SUM(AF{$start_row}:AF{$end_row}),0)";
            $tot_shp_q2_formula = "=IFERROR(SUM(AG{$start_row}:AG{$end_row}),0)";

            $tot_qty_oct_formula = "=IFERROR(SUM(AH{$start_row}:AH{$end_row}),0)";
            $tot_sc_oct_formula = "=IFERROR(SUM(AI{$start_row}:AI{$end_row}),0)";
            $tot_shp_oct_formula = "=IFERROR(SUM(AJ{$start_row}:AJ{$end_row}),0)";

            $tot_qty_nov_formula = "=IFERROR(SUM(AK{$start_row}:AK{$end_row}),0)";
            $tot_sc_nov_formula = "=IFERROR(SUM(AL{$start_row}:AL{$end_row}),0)";
            $tot_shp_nov_formula = "=IFERROR(SUM(AM{$start_row}:AM{$end_row}),0)";

            $tot_qty_dec_formula = "=IFERROR(SUM(AN{$start_row}:AN{$end_row}),0)";
            $tot_sc_dec_formula = "=IFERROR(SUM(AO{$start_row}:AO{$end_row}),0)";
            $tot_shp_dec_formula = "=IFERROR(SUM(AP{$start_row}:AP{$end_row}),0)";

            $tot_qty_q3_formula = "=IFERROR(SUM(AQ{$start_row}:AQ{$end_row}),0)";
            $tot_sc_q3_formula = "=IFERROR(SUM(AR{$start_row}:AR{$end_row}),0)";
            $tot_shp_q3_formula = "=IFERROR(SUM(AS{$start_row}:AS{$end_row}),0)";

            $tot_qty_jan_formula = "=IFERROR(SUM(AT{$start_row}:AT{$end_row}),0)";
            $tot_sc_jan_formula = "=IFERROR(SUM(AU{$start_row}:AU{$end_row}),0)";
            $tot_shp_jan_formula = "=IFERROR(SUM(AV{$start_row}:AV{$end_row}),0)";

            $tot_qty_feb_formula = "=IFERROR(SUM(AW{$start_row}:AW{$end_row}),0)";
            $tot_sc_feb_formula = "=IFERROR(SUM(AX{$start_row}:AX{$end_row}),0)";
            $tot_shp_feb_formula = "=IFERROR(SUM(AY{$start_row}:AY{$end_row}),0)";

            $tot_qty_mar_formula = "=IFERROR(SUM(AZ{$start_row}:AZ{$end_row}),0)";
            $tot_sc_mar_formula = "=IFERROR(SUM(BA{$start_row}:BA{$end_row}),0)";
            $tot_shp_mar_formula = "=IFERROR(SUM(BB{$start_row}:BB{$end_row}),0)";

            $tot_qty_q4_formula = "=IFERROR(SUM(BC{$start_row}:BC{$end_row}),0)";
            $tot_sc_q4_formula = "=IFERROR(SUM(BD{$start_row}:BD{$end_row}),0)";
            $tot_shp_q4_formula = "=IFERROR(SUM(BE{$start_row}:BE{$end_row}),0)";

            $tot_qty_tq_formula = "=IFERROR(SUM(BF{$start_row}:BF{$end_row}),0)";
            $tot_sc_tq_formula = "=IFERROR(SUM(BG{$start_row}:BG{$end_row}),0)";
            $tot_shp_tq_formula = "=IFERROR(SUM(BH{$start_row}:BH{$end_row}),0)";

            $columnHeaders = [
                'C' => 'Total-QTY',
                'J' => "=IFERROR(SUM(J{$start_row}:J{$end_row}),0)",
                'K' => "=IFERROR(SUM(K{$start_row}:K{$end_row}),0)",
                'L' => $tot_shp_apr_formula,
                'M' => $tot_qty_may_formula,
                'N' => $tot_sc_may_formula,
                'O' => $tot_shp_may_formula,
                'P' => $tot_qty_jun_formula,
                'Q' => $tot_sc_jun_formula,
                'R' => $tot_shp_jun_formula,
                'S' => $tot_qty_q1_formula,
                'T' => $tot_sc_q1_formula,
                'U' => $tot_shp_q1_formula,
                'V' => $tot_qty_jul_formula,
                'W' => $tot_sc_jul_formula,
                'X' => $tot_shp_jul_formula,
                'Y' => $tot_qty_aug_formula,
                'Z' => $tot_sc_aug_formula,
                'AA' => $tot_shp_aug_formula,
                'AB' => $tot_qty_sep_formula,
                'AC' => $tot_sc_sep_formula,
                'AD' => $tot_shp_sep_formula,
                'AE' => $tot_qty_q2_formula,
                'AF' => $tot_sc_q2_formula,
                'AG' => $tot_shp_q2_formula,
                'AH' => $tot_qty_oct_formula,
                'AI' => $tot_sc_oct_formula,
                'AJ' => $tot_shp_oct_formula,
                'AK' => $tot_qty_nov_formula,
                'AL' => $tot_sc_nov_formula,
                'AM' => $tot_shp_nov_formula,
                'AN' => $tot_qty_dec_formula,
                'AO' => $tot_sc_dec_formula,
                'AP' => $tot_shp_dec_formula,
                'AQ' => $tot_qty_q3_formula,
                'AR' => $tot_sc_q3_formula,
                'AS' => $tot_shp_q3_formula,
                'AT' => $tot_qty_jan_formula,
                'AU' => $tot_sc_jan_formula,
                'AV' => $tot_shp_jan_formula,
                'AW' => $tot_qty_feb_formula,
                'AX' => $tot_sc_feb_formula,
                'AY' => $tot_shp_feb_formula,
                'AZ' => $tot_qty_mar_formula,
                'AA' => $tot_sc_mar_formula,
                'BB' => $tot_shp_mar_formula,
                'BC' => $tot_qty_q4_formula,
                'BD' => $tot_sc_q4_formula,
                'BE' => $tot_shp_q4_formula,
                'BF' => $tot_qty_tq_formula,
                'BG' => $tot_sc_tq_formula,
                'BH' => $tot_shp_tq_formula,
            ];
            setData($activeWorksheet, $rowNumber, $columnHeaders);
            // Add filter for row 9
            $activeWorksheet->setAutoFilter('A9:BH9');

            // Add filter for row 9
            //bold total
            $boldrow = 'A' . $rowNumber . ':BH' . $rowNumber;
            $activeWorksheet->getStyle("$boldrow")->getFont()->setBold(true);
            $highestRow = $activeWorksheet->getHighestRow();

            $monRange = "{$monthrows[$month]}10:{$monthrows[$month]}{$lastpdtrow}";
            setFillColor($activeWorksheet, "{$monRange}", 'C4D79B'); // Light green
            // Set fill colors for quarters
            setFillColor($activeWorksheet, 'S8:U' . $lastpdtrow, 'ccffff'); // Quarter 1
            setFillColor($activeWorksheet, 'AE8:AG' . $lastpdtrow, 'ccffff'); // Quarter 2
            setFillColor($activeWorksheet, 'AQ8:AS' . $lastpdtrow, 'ccffff'); // Quarter 3
            setFillColor($activeWorksheet, 'BC8:BE' . $lastpdtrow, 'ccffff'); // Quarter 4
            // Set fill color for total
            setFillColor($activeWorksheet, 'BF8:BH' . $lastpdtrow, '99ccff'); // Dark blue
            // Set fill colors for fixed sections
            setFillColor($activeWorksheet, 'A1:H1', 'CC99FF'); // Fixed Order Form
            setFillColor($activeWorksheet, 'C9:H' . $lastpdtrow, 'ffff99'); // Yellow
            setFillColor($activeWorksheet, 'A8:BH8', 'CCFFCC'); // Light green
            setFillColor($activeWorksheet, 'A8:C' . $lastpdtrow, 'CCFFCC'); // Light green

           
            // Set fill color for total qty and amount
            $oddColor_a = 'e6f3ff'; // Specify your odd row color here
            $evenColor_a = 'cce6ff'; // Even row color
            for ($row1 = $start_row_amt; $row1 <= $end_row_amt; $row1++) {
                $color = $row1 % 2 == 0 ? $evenColor_a : $oddColor_a;
                $range_c1 = "A{$row1}:BH{$row1}";
                setFillColor($activeWorksheet, $range_c1, $color);
            }
            $oddColor_q = 'e6ffcc'; // Specify your odd row color here
            $evenColor_q = 'cdff99'; // Even row color
            for ($row = $start_row_qty; $row <= $end_row_qty; $row++) {
                $color = $row % 2 == 0 ? $evenColor_q : $oddColor_q;
                $range_c2 = "A{$row}:BH{$row}";
                setFillColor($activeWorksheet, $range_c2, $color);
            }


            // Set thiN border line from A1 to LAST
            $allColumnsRange = "A8:BH{$highestRow}";
            $activeWorksheet->getStyle($allColumnsRange)->getBorders()->getAllBorders()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN);
            // Set thick border line from J8 to L997
            $columns = ['J', 'M', 'P', 'S', 'V', 'Y', 'AB', 'AE', 'AH', 'AK', 'AN', 'AQ', 'AT', 'AW', 'AZ', 'BC', 'BF'];
            foreach ($columns as $startColumn) {
                $endColumn = $startColumn;
                for ($i = 0; $i < 2; $i++) {
                    $endColumn++;
                }
                $range = "{$startColumn}8:{$endColumn}{$end_row_qty}";
                $activeWorksheet->getStyle($range)->getBorders()->getOutline()->setBorderStyle(\PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THICK);
            }
            $width = 25; // Desired width in pixels
            for ($row = $start_row_amt; $row <= $end_row_qty; $row++) {
                $activeWorksheet->getRowDimension($row)->setRowHeight($width);
            }
            $activeWorksheet->getRowDimension('9')->setRowHeight('44');

            // Define the range
            $range_for_number_comma = "J{$start_row_amt}:BH{$end_row_amt}";
            $range_comma_2 = "F10:F{$lastpdtrow}";
            // Set comma-separated number format for the ranges
            $activeWorksheet->getStyle($range_for_number_comma)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');

            $activeWorksheet->getStyle($range_comma_2)
                    ->getNumberFormat()
                    ->setFormatCode('#,##0.00');
            // Create a writer and save the spreadsheet to a file
            $writer = new Xlsx($spreadsheet);
            $mod_dis = Distributers::model()->findByPk((int) $distributer_id);
            $file_name = 'Order_' . $mod_dis->distributer_name . '_' . $year . '.xlsx';
            // Create a writer
            // Set the appropriate HTTP headers for Excel file download
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
            header('Content-Disposition: attachment; filename="' . $file_name . '"');
            header('Cache-Control: max-age=0');
            // Save the spreadsheet to the output (i.e., it will trigger a file download)
            $writer->save('php://output');
            return;
        } else {
            throw new CHttpException(404, 'Page not found.');
        }
    }*/
    
    
    
      public function actionDownloadCreditHoldDetails()
      {
        $credit_hold_id                  = $_REQUEST['credit_hold_id'];
    
        $param = array(
          'credit_hold_id'     => $credit_hold_id,
        );
    
        Yii::app()->clientScript->scriptMap = array('*.js' => false, '*.min.js' => false, '*.css' => false);
        $filename = "CreditHoldDetails_" . date("d-m-Y_H-i-s") . ".xls";
        header("Content-type: application/vnd.ms-excel");
        header("Content-Disposition: attachment; filename=\"" . $filename . "\"");
        $this->renderPartial('application.views.reports.excel.credit_hold_details_excel', $param, false, true);
        Yii::app()->end();
      }

        
        
}