Your IP : 216.73.216.79


Current Path : /var/www/html/jkdm-deface/backend/controllers/
Upload File :
Current File : /var/www/html/jkdm-deface/backend/controllers/LaporanController-130220.php

<?php

namespace backend\controllers;

use Yii;
use yii\web\Controller;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use yii\filters\AccessControl;
use backend\models\application\AiApplication;
use backend\models\Ref;
use backend\models\returns\ReturnCode;
use backend\models\returns\ReturnApplication;
use backend\models\returns\ReturnPayments;

class LaporanController extends Controller
{
    public function actionTotalFlightclass()
    {
        $stesens = Ref::find()->where(['cat' => 'stesen'])->all();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('total_flightclass_excel', [
                'stesens' => $stesens,
            ]);
        }
        
        return $this->render('total_flightclass', [
            'stesens' => $stesens,
        ]);
    }
    
    public function actionTotalFlightdetails()
    {
        $stesens = Ref::find()->where(['cat' => 'stesen'])->all();
        $model = ReturnCode::find()->where(['rete_activeind' => 'A'])->all();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('total_flightdetails_excel', [
                'stesens' => $stesens,
                'model' => $model,
            ]);
        }
        
        return $this->render('total_flightdetails', [
                'stesens' => $stesens,
                'model' => $model,
        ]);
    }
    
    public function actionTaxofstate()
    {
        $stesens = Ref::find()->where(['cat' => 'stesen'])->all();
        $model = ReturnCode::find()->where(['rete_activeind' => 'A'])->all();
    
        if (isset($_GET['btnexcel'])) {
            return $this->render('tax_of_state_excel', [
                'stesens' => $stesens,
                'model' => $model,
            ]);
        }

        return $this->render('tax_of_state', [
                'stesens' => $stesens,
                'model' => $model,
        ]);
    }
    
    public function actionLedger()
    {
        $sql = "SELECT 
                        return_application.reap_id, 
                        return_application.reap_registrationno,
                        return_payments.repa_id,
                        return_payments.repa_applicationid,
                        return_payments.repa_referenceid,
                        return_payments.reap_title,
                        return_payments.reap_payment,
                        return_payments.reap_paymentdate,
                        return_receipt.rert_receiptno,
                        return_receipt.rert_receiptdate
                FROM return_application	  
                INNER JOIN return_status ON return_application.reap_id = return_status.rest_returnid
                INNER JOIN return_payments ON return_application.reap_id = return_payments.repa_applicationid
                INNER JOIN return_receipt ON return_payments.repa_id = return_receipt.rert_paymentid
                WHERE return_status.rest_activeind = '1'";
        $model = Yii::$app->db->createCommand($sql)->queryAll();

        return $this->render('ledger', [
                'model' => $model,
        ]);
    }
    
    public function actionStatement()
    {
        $sql = "SELECT * FROM return_application
            INNER JOIN return_payments ON return_payments.repa_applicationid = return_application.reap_id
            INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
            AND return_payments.reap_status = 'S'";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
        }
        
        $sql .= " GROUP BY dls_ai_application.aiap_primarydepaturecode, YEAR(return_application.reap_datefrom)";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('statement_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('statement', [
            'model' => $model,
        ]);
    }

    public function actionStatementYearly() {
        $sql = "SELECT * FROM return_application
            INNER JOIN return_payments ON return_payments.repa_applicationid = return_application.reap_id
            INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
            AND return_payments.reap_status = 'S'";
        
            if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
                $stesenkod = $_GET['stesen_code'];
                if (!empty($stesenkod)) {
                    $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
                }

                if (!empty($_GET['year_start'])) {
                    $year_start = '';
                    if (!empty($_GET['year_start'])) {
                        $year_start = $_GET['year_start'];
                    }
                    $year_end = date('Y');
                    if (!empty($_GET['year_end'])) {
                        $year_end = $_GET['year_end'];
                    }

                    $sql .= " AND YEAR(return_application.reap_datefrom) BETWEEN '" . $year_start . "' AND '" . $year_end . "'";
                }
            }
        
        $sql .= " GROUP BY YEAR(return_application.reap_datefrom), MONTH(return_application.reap_datefrom) 
                ORDER BY YEAR(return_application.reap_datefrom), MONTH(return_application.reap_datefrom) DESC";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('statement_yearly_excel', [
                'model' => $model,
            ]);
        }

        return $this->render('statement_yearly', [
                    'model' => $model,
        ]);
    }

    public function actionDailyReport()
    {
		/*
        $sql = "SELECT * FROM return_application
	    INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'";
        */

		$sql = "
			SELECT a.*, payment_collected FROM (
				SELECT 
					reap_datefrom, reap_registrationno, reap_id, aiap_primarydepaturecode, aiap_applicantname, aiap_operatortradename
					, (SELECT DISTINCT return_statement.reas_paymentstatus FROM return_statement WHERE reas_paymentid = repa_id GROUP BY return_statement.reas_paymentstatus) AS st_status
					, reap_totalamount AS total
					, reap_payment AS payment_collected
					, reap_declaredate
					, (SELECT SUM(reas_debitnote) AS total FROM return_statement WHERE reas_paymentid = repa_id GROUP BY return_statement.reas_paymentid) AS reap_totalamount
				FROM 
					return_application
					INNER JOIN return_payments ON (return_payments.repa_applicationid = return_application.reap_id AND return_payments.reap_status = 'S')
					LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid )
					INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid

				WHERE 
					dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1' 
			) AS a
			WHERE 1=1
		";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND reap_datefrom = '" . $monthdate . "'";
            }
            
            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                 }
                $sql .= " AND reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        $sql .= " ORDER BY aiap_primarydepaturecode ASC, reap_declaredate DESC";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
         
        if (isset($_GET['btnexcel'])) {
            return $this->render('daily-report_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('daily-report', [
            'model' => $model,
        ]);
    }

    public function actionDailyReport2()
    {
        $sql = "SELECT * FROM return_application
	    INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'";
            
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
            
            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                 }
                $sql .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        $sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
         
        if (isset($_GET['btnexcel'])) {
            return $this->render('daily-report_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('daily-report', [
            'model' => $model,
        ]);
    }

    public function actionPaymentSummary()
    {
        $sql = "SELECT * FROM dls_ai_application
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            INNER JOIN return_status ON return_status.rest_returnid = return_application.reap_id
            INNER JOIN dls_ai_appstatus ON dls_ai_appstatus.aias_applicationid = dls_ai_application.aiap_id
            WHERE return_status.rest_statuscode = 'A' AND return_status.rest_activeind = '1' 
            AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
            ";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('payment-summary_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('payment-summary', [
            'model' => $model,
        ]);
    }

    public function actionGeneralLedger()
    {
        $sql = "SELECT * FROM return_application
            INNER JOIN return_payments ON return_application.reap_id = return_payments.repa_applicationid
	    INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $returnno = $_GET['returnno'];
            if (!empty($returnno)) {
                $sql .= " AND (( return_application.reap_registrationno = '" . $returnno . "')
                    OR ('" . $returnno . "' IN (SELECT rbod_referenceno FROM return_billofdemand WHERE rbod_applicationid = return_application.reap_id))
                    OR ('" . $returnno . "' IN (SELECT repn_bodreferenceno FROM return_penalty WHERE repn_applicationid = return_application.reap_id))
                    )";
            }
            
            $name = $_GET['name'];
            if (!empty($name)) {
                $sql .= " AND dls_ai_application.aiap_applicantname = '" . $name . "'";
            }
            
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
            
            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                 }
                $sql .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
            
            $status = $_GET['status'];
            if (!empty($status)) {
                if ($status == 'NP') { 
                    $sql .= " AND return_application.reap_id NOT IN (SELECT repa_applicationid FROM return_payments)";
                } else {
                    $sql .= " AND return_payments.reap_status = '" . $status . "'";
                }
            }
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        if (isset($_GET['btnexcel'])) {
            return $this->render('general-ledger_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('general-ledger', [
            'model' => $model,
        ]);
    }

    public function actionDailyReceiptReport()
    {
        $sql = "SELECT * FROM return_payments 
            INNER JOIN return_receipt ON return_payments.repa_id = return_receipt.rert_paymentid
            INNER JOIN return_application ON return_application.reap_id = return_payments.repa_applicationid
            INNER JOIN dls_ai_application ON dls_ai_application.aiap_registrantid = return_application.reap_registrantid
            WHERE return_payments.reap_status = 'S' 
            ";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $name = $_GET['name']; 
            if (!empty($name)) {
                $sql .= " AND (dls_ai_application.aiap_applicantname LIKE '%" . $name . "%'
                    OR dls_ai_application.aiap_businessno LIKE '%" . $name . "%'
                    OR dls_ai_application.aiap_operatortradename LIKE '%" . $name . "%')";
            }
            
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            $payment_type = $_GET['payment_type'];
            if (!empty($payment_type)) {
                $sql .= " AND return_payments.reap_title = '" . $payment_type . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
            
            if (!empty($_GET['date_start'])) {
                $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                $dateend = date('Y-m-d');
                if (!empty($_GET['date_end'])) {
                    $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                }
                
                $sql .= " AND ((return_payments.reap_paymentrecievedate BETWEEN '" . $datestart . "' AND '" . $dateend . "') OR (return_payments.reap_paymentdate BETWEEN '" . $datestart . "' AND '" . $dateend . "'))";
            }
        }
        
        $sql .= " ORDER BY rert_receiptno DESC;";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('daily-receipt-report_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('daily-receipt-report', [
            'model' => $model,
        ]);
    }

    public function actionCashBook()
    {
		$q_date = '';
		$q_stesen = '';
		$start_date = '';
		$end_date = '';
		
		if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {

            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
				 
				 $start_date = date('d-m-Y', strtotime($_GET['date_start']));
                 $end_date = date('d-m-Y');				 
				 
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
					 
					 $end_date = date('d-m-Y', strtotime($_GET['date_end']));
                 }
                $q_date .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
		
		$connection = Yii::$app->getDb();
		$command = $connection->createCommand("
				SELECT 	return_application.reap_id, return_payments.reap_title, return_payments.reap_status, rert_status, 
					reas_paymentstatus, rert_receiptdate, reap_paymentdate, rert_receiptno, reas_federalcode, reas_debitnote, 
					reas_creditnote, reap_payment, reas_transactiondate, reap_registrationno, reap_totalamount 
				
				FROM 	return_application 
					LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid)
					INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
					INNER JOIN return_payments ON return_application.reap_id = return_payments.repa_applicationid 
					LEFT JOIN return_receipt ON return_payments.repa_id = return_receipt.rert_paymentid
					LEFT JOIN return_statement ON return_payments.repa_id = return_statement.reas_paymentid AND return_statement.reas_paymentstatus = 'S'
					
				WHERE 
					return_payments.reap_status='S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
					$q_date
				"
				);
		
		$result = $command->queryAll();
		//print_r($result); return;
		$model = (object)$result;
		
        if (isset($_GET['btnexcel'])) {
			//echo 'excel';
            return $this->render('cash-book-excel', [
                'model' => $model, 'start_date' => $start_date, 'end_date' => $end_date
            ]);
        } else {
			return $this->render('cash-book', [
				'model' => $model,
			]);
		}
    }

    public function actionIncomeReconcillationReport()
    {
		$q_date = '';
		$q_stesen = '';
		$start_date = '';
		$end_date = '';
		
		if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {

            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
				 
				 $start_date = date('d-m-Y', strtotime($_GET['date_start']));
                 $end_date = date('d-m-Y');				 
				 
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
					 
					 $end_date = date('d-m-Y', strtotime($_GET['date_end']));
                 }
                $q_date .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
		
		$connection = Yii::$app->getDb();
		$command = $connection->createCommand("
				SELECT return_payments.*, eod_data.pmt_msgtoken, eod_data.pmt_bank, eod_data.pmt_refno 

				FROM 	
					return_application 
					INNER JOIN return_payments ON (return_payments.repa_applicationid = return_application.reap_id)
					INNER JOIN eod_data ON eod_data.transactionid = return_payments.repa_transactionid 
					LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid)
					INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
				WHERE 
					return_payments.reap_status='S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
					$q_date
				"
				);
		
		$result = $command->queryAll();
		//print_r($result); return;
		$model = (object)$result;
		
        if (isset($_GET['btnexcel'])) {
            return $this->render('income-reconcillation-report-excel', [
                'model' => $model, 'start_date' => $start_date, 'end_date' => $end_date
            ]);
        }

        //$model = ReturnPayments::find()->where(['reap_status' => 'S'])->all();
        
        return $this->render('income-reconcillation-report', [
            'model' => $model,
        ]);
    }

    public function actionIncomeReconcillationReport2()
    {
        $model = ReturnPayments::find()->where(['reap_status' => 'S'])->all();
        
        return $this->render('income-reconcillation-report2', [
            'model' => $model,
        ]);
    }

    public function actionCollectionReport()
    {
		$q_date = '';
		$q_stesen = '';
		$start_date = '';
		$end_date = '';
		$stesen = '';
		
		if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $q_stesen = " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
				
				$stesens = Ref::find()->where(['cat' => 'stesen'])->where(['code' => $stesenkod])->one();	
				$stesen = $stesens->descr;	
            }

            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
				 
				 $start_date = date('d-m-Y', strtotime($_GET['date_start']));
                 $end_date = date('d-m-Y');				 
				 
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
					 
					 $end_date = date('d-m-Y', strtotime($_GET['date_end']));
                 }
                $q_date .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        //$sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";

		$connection = Yii::$app->getDb();
		$command = $connection->createCommand("
				SELECT 
						DISTINCT reas_code, reas_federalcode, SUM(reas_debitnote) AS total
				FROM return_statement 
					 INNER JOIN return_application ON (return_application.reap_id = return_statement.reas_reapid)
					 LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
					 INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
				
				WHERE 
					reas_paymentstatus = 'S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
					$q_date
				
				GROUP BY reas_code, reas_federalcode
				"
				);
		
		$result = $command->queryAll();
		//print_r($result); return;
		$model = (object)$result;
		
		
		$command2 = $connection->createCommand("
				SELECT 
					(
					CASE
						WHEN pmt_msgToken = '01' THEN 'FPX B2C'
						WHEN pmt_msgToken = '02' THEN 'FPX B2B'
						ELSE 'EFT'
					END 
					) AS pmt_mode, SUM(reap_payment) AS total,
					COUNT(*) AS jumlah
				FROM 
						 return_application 
						 INNER JOIN return_payments ON (return_payments.repa_applicationid = return_application.reap_id)
						 LEFT JOIN payment_trace ON (repa_transactionid = pmt_receipt_no)
						 LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
						 INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
					
				WHERE 	return_payments.reap_status = 'S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
						$q_date
						
				GROUP BY pmt_mode
				ORDER BY pmt_mode DESC
				"
				);
		
		$result2 = $command2->queryAll();
		$model2 = (object)$result2;


//        $model = Yii::$app->db->createCommand($sql)->queryAll();
         
        if (isset($_GET['btnexcel'])) {
            return $this->render('collection-report-excel', [
                'model' => $model, 'model2' => $model2, 'start_date' => $start_date, 'end_date' => $end_date, 'stesen' => $stesen,
            ]);
        }
                
		
        //$model = ReturnApplication::find()->all();
        
        return $this->render('collection-report', [
            'model' => $model, 'model2' => $model2
        ]);
    }
	    
	public function actionCollectionReport3()
    {
		$q_date = '';
		$q_stesen = '';
		$q_title = '';
		$q_ptrace = '';
		$start_date = '';
		$end_date = '';
		$stesen = '';
		$payment_mode = '';
		$payment_title = '';
		
		if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $q_stesen = " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
				
				$stesens = Ref::find()->where(['cat' => 'stesen'])->where(['code' => $stesenkod])->one();	
				$stesen = $stesens->descr;	
            }
			
            if (!empty($_GET['payment_mode'])) {
	            $payment_mode = $_GET['payment_mode'];

				if ($payment_mode == 'EFT') {
					$q_title = "AND return_payments.reap_title = 'Electronic Funds Transfer (EFT)'";
					
					$payment_title = 'Electronic Funds Transfer (EFT)';
				}
				if ($payment_mode == '01' || $payment_mode == '02') {
					$q_ptrace = "
							INNER JOIN payment_trace ON (
									SUBSTRING_INDEX(SUBSTRING_INDEX(pmt_refno, '@', 1), '-', -1) = return_payments.repa_id AND 
									SUBSTRING_INDEX(payment_trace.pmt_refno, '-', 1) = return_application.reap_id AND 
									payment_trace.pmt_code = '00' AND payment_trace.pmt_msgToken IN ('$payment_mode')
									)					
					";
					
					$q_title = "AND return_payments.reap_title = 'Financial Process Exchange (FPX)'";

					$payment_title = ($payment_mode == '01')?'FPX B2C':'FPX B2B';	
				}
            }

            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
				 
				 $start_date = date('d-m-Y', strtotime($_GET['date_start']));
                 $end_date = date('d-m-Y');				 
				 
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
					 
					 $end_date = date('d-m-Y', strtotime($_GET['date_end']));
                 }
                $q_date .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        //$sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";

		$connection = Yii::$app->getDb();
		$command = $connection->createCommand("
				SELECT 
						DISTINCT reas_code, reas_federalcode, SUM(reas_debitnote) AS total
				FROM return_statement 
					 INNER JOIN return_application ON (return_application.reap_id = return_statement.reas_reapid)
					 INNER JOIN return_payments ON (return_application.reap_id = return_payments.repa_applicationid)
	 				 LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
					 INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
	 				 INNER JOIN return_receipt ON return_payments.repa_id = return_receipt.rert_paymentid
				
					 $q_ptrace
				WHERE return_payments.reap_status='S' AND
					reas_paymentstatus = 'S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
					$q_date
					$q_title
				
				GROUP BY reas_code, reas_federalcode
				"
				);
		
		$result = $command->queryAll();
		//print_r($result); return;
		$model = (object)$result;
		
		
		$command2 = $connection->createCommand("
				SELECT 
					(
					CASE
						WHEN pmt_msgToken = '01' THEN 'FPX B2C'
						WHEN pmt_msgToken = '02' THEN 'FPX B2B'
						ELSE 'EFT'
					END 
					) AS pmt_mode, SUM(reap_payment) AS total,
					COUNT(*) AS jumlah
				FROM 
						 return_application 
						 INNER JOIN return_payments ON (return_payments.repa_applicationid = return_application.reap_id)
						 LEFT JOIN payment_trace ON (repa_transactionid = pmt_receipt_no)
						 LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
						 INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
					
				WHERE 	return_payments.reap_status = 'S' AND dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
						$q_date
						
				GROUP BY pmt_mode
				ORDER BY pmt_mode DESC
				"
				);
		
		$result2 = $command2->queryAll();
		$model2 = (object)$result2;


//        $model = Yii::$app->db->createCommand($sql)->queryAll();
         
        if (isset($_GET['btnexcel'])) {
            return $this->render('collection-report-excel2', [
                'model' => $model, 'model2' => $model2, 'start_date' => $start_date, 'end_date' => $end_date, 'stesen' => $stesen, 'payment_title' => $payment_title, 'payment_mode' => $payment_mode,
            ]);
        }
                
		
        //$model = ReturnApplication::find()->all();
        
        return $this->render('collection-report2', [
            'model' => $model, 'model2' => $model2, 'payment_mode' => $payment_mode,
        ]);
    }
	
	public function actionCollectionReport2()
    {
		$q_date = '';
		$q_stesen = '';
		$q_title = '';
		$q_ptrace = '';
		$start_date = '';
		$end_date = '';
		$stesen = '';
		$payment_mode = '';
		$payment_title = '';
		
		if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $q_stesen = " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
				
				$stesens = Ref::find()->where(['cat' => 'stesen'])->where(['code' => $stesenkod])->one();	
				$stesen = $stesens->descr;	
            }
			
            if (!empty($_GET['payment_mode'])) {
	            $payment_mode = $_GET['payment_mode'];

				if ($payment_mode == 'EFT') {
					$q_title = "WHERE reap_title = 'Electronic Funds Transfer (EFT)'";
					
					$payment_title = 'Electronic Funds Transfer (EFT)';
				}
				if ($payment_mode == '01' || $payment_mode == '02') {
					$q_ptrace = "
							INNER JOIN payment_trace ON (
									SUBSTRING_INDEX(SUBSTRING_INDEX(pmt_refno, '@', 1), '-', -1) = return_payments.repa_id AND 
									SUBSTRING_INDEX(payment_trace.pmt_refno, '-', 1) = return_application.reap_id AND 
									payment_trace.pmt_code = '00' AND payment_trace.pmt_msgToken IN ('$payment_mode')
									)					
					";
					
					//$q_title = "AND return_payments.reap_title = 'Financial Process Exchange (FPX)'";
					$q_title = "WHERE pmt_msgToken IN ('$payment_mode')";

					$payment_title = ($payment_mode == '01')?'FPX B2C':'FPX B2B';	
				}
            }

            if (!empty($_GET['date_start'])) {
                 $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                 $dateend = date('Y-m-d');
				 
				 $start_date = date('d-m-Y', strtotime($_GET['date_start']));
                 $end_date = date('d-m-Y');				 
				 
                 if (!empty($_GET['date_end'])) {
                     $dateend = date('Y-m-d', strtotime($_GET['date_end']));
					 
					 $end_date = date('d-m-Y', strtotime($_GET['date_end']));
                 }
                $q_date .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        //$sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";

		$connection = Yii::$app->getDb();
		$command = $connection->createCommand("
				SELECT DISTINCT reas_code, reas_federalcode, SUM(newtotal) AS total, reap_title
				FROM (
					SELECT a.*, payment_trace.pmt_msgToken FROM (
						SELECT 
							reas_reapid, reas_code, reas_federalcode, (reas_debitnote) AS newtotal,	
							( SELECT DISTINCT repa_transactionid FROM return_payments WHERE reas_paymentid = repa_id GROUP BY repa_transactionid ) AS transid,
							( SELECT DISTINCT reap_title FROM return_payments WHERE reas_paymentid = repa_id GROUP BY reap_title ) AS reap_title
						FROM 
							return_application
							INNER JOIN return_statement ON (return_application.reap_id = return_statement.reas_reapid 
							AND reas_paymentstatus = 'S')
							LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
							INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid

						WHERE 
							dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1' 
							$q_date
							
					) AS a
					LEFT JOIN payment_trace ON (transid = pmt_receipt_no)
				) AS b
				$q_title
				GROUP BY reas_code, reas_federalcode
				"
				);
		
		$result = $command->queryAll();
		//print_r($result); return;
		$model = (object)$result;
		
		
		$command2 = $connection->createCommand("
				SELECT (
						CASE
						WHEN pmt_msgToken = '01' THEN 'FPX B2C'
						WHEN pmt_msgToken = '02' THEN 'FPX B2B'
						ELSE 'EFT'
						END
					) AS pmt_mode, 
					SUM(jum) AS total,
					COUNT(*) AS jumlah,
					reap_title
				FROM (
					SELECT transid, pmt_msgToken, SUM(newtotal) AS jum, reap_title
					FROM (
						SELECT a.*, payment_trace.pmt_msgToken FROM (
							SELECT 
								reas_reapid, reas_code, reas_federalcode, (reas_debitnote) AS newtotal,	
								( SELECT DISTINCT repa_transactionid FROM return_payments WHERE reas_paymentid = repa_id GROUP BY repa_transactionid ) AS transid,
								( SELECT DISTINCT reap_title FROM return_payments WHERE reas_paymentid = repa_id GROUP BY reap_title ) AS reap_title
							FROM 
								return_application
								INNER JOIN return_statement ON (return_application.reap_id = return_statement.reas_reapid 
								AND reas_paymentstatus = 'S')
								LEFT JOIN dls_ai_application ON (return_application.reap_registrantid = dls_ai_application.aiap_registrantid $q_stesen)
								INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid

							WHERE 
								dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1' 
								$q_date
						
						) AS a
						LEFT JOIN payment_trace ON (transid = pmt_receipt_no)
					) AS b
					GROUP BY transid, pmt_msgToken
				) AS c
				GROUP BY pmt_msgToken
				ORDER BY pmt_mode DESC
				"
				);
		
		$result2 = $command2->queryAll();
		$model2 = (object)$result2;


//        $model = Yii::$app->db->createCommand($sql)->queryAll();
         
        if (isset($_GET['btnexcel'])) {
            return $this->render('collection-report-excel2', [
                'model' => $model, 'model2' => $model2, 'start_date' => $start_date, 'end_date' => $end_date, 'stesen' => $stesen, 'payment_title' => $payment_title, 'payment_mode' => $payment_mode,
            ]);
        }
                
		
        //$model = ReturnApplication::find()->all();
        
        return $this->render('collection-report2', [
            'model' => $model, 'model2' => $model2, 'payment_mode' => $payment_mode,
        ]);
    }
	
    public function actionDl01List()
    {
        $sql = "SELECT *,count(*) as cnt FROM dls_ai_application
        LEFT JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'";
            
            if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }

            if (!empty($_GET['year'])) {
                $year = $_GET['year'];
                $sql .= " AND YEAR(dls_ai_application.created_at) = '" . $year . "'" ;
            }
        }
        
        $sql .= "GROUP BY dls_ai_application.aiap_primarydepaturecode,dls_ai_application.aiap_persontype ";
        $sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('dl01-excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('dl01-list', [
            'model' => $model,
        ]);
    }

    public function actionDl02List()
    {
        $sql = "SELECT * FROM return_application
	    INNER JOIN dls_ai_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid 
            INNER JOIN dls_ai_appstatus ON dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'";
            
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }

            if (!empty($_GET['date_start'])) {
                $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                $dateend = date('Y-m-d');
                if (!empty($_GET['date_end'])) {
                    $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                }
                $sql .= " AND return_application.reap_declaredate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        $sql .= " ORDER BY dls_ai_application.aiap_primarydepaturecode ASC";
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('dl02-excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('dl02-list', [
            'model' => $model,
        ]);
    }
    
    public function actionPaymentStatement()
    {
        $sql = "SELECT * FROM return_payments 
            INNER JOIN return_receipt ON return_payments.repa_id = return_receipt.rert_paymentid
            INNER JOIN return_application ON return_application.reap_id = return_payments.repa_applicationid
            INNER JOIN dls_ai_application ON dls_ai_application.aiap_registrantid = return_application.reap_registrantid
            WHERE return_payments.reap_status = 'S'
            ";
        
        if (isset($_GET['btnSearch']) || isset($_GET['btnexcel'])) {
            $name = $_GET['name'];
            if (!empty($name)) {
                $sql .= " AND (dls_ai_application.aiap_applicantname LIKE '%" . $name . "%'
                    OR dls_ai_application.aiap_businessno LIKE '%" . $name . "%'
                    OR dls_ai_application.aiap_operatortradename LIKE '%" . $name . "%')";
            }
            
            $stesenkod = $_GET['stesen_code'];
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            $payment_type = $_GET['payment_type'];
            if (!empty($payment_type)) {
                $sql .= " AND return_payments.reap_title = '" . $payment_type . "'";
            }
            
            if (!empty($_GET['month'])) {
                $bulan = '';
                if (!empty($_GET['month'])) {
                    $month = $_GET['month'];
                    if (strlen($month) > 1) {
                        $bulan = $month;
                    } else {
                        $bulan = '0' . $month;
                    }
                }
                $year = date('Y');
                if (!empty($_GET['year'])) {
                    $year = $_GET['year'];
                }
                $date = '01-' . $bulan . '-' . $year;

                $monthdate = date('Y-m-d', strtotime($date));
                $sql .= " AND return_application.reap_datefrom = '" . $monthdate . "'";
            }
            
            if (!empty($_GET['date_start'])) {
                $datestart = date('Y-m-d', strtotime($_GET['date_start']));
                $dateend = date('Y-m-d');
                if (!empty($_GET['date_end'])) {
                    $dateend = date('Y-m-d', strtotime($_GET['date_end']));
                }
                
                $sql .= " AND return_application.reap_paymentdate BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
            }
        }
        
        $sql .= " ORDER BY rert_receiptno DESC;";
        $model = Yii::$app->db->createCommand($sql)->queryAll();

        if (isset($_GET['btnexcel'])) {
            return $this->render('payment_statement_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('payment_statement', [
            'model' => $model,
        ]);
    }
    
    public function actionNotSubmitted()
    {
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN dls_ai_appstatus ON dls_ai_appstatus.aias_applicationid = dls_ai_application.aiap_id
            WHERE dls_ai_appstatus.aias_statuscode = 'A' AND dls_ai_appstatus.aias_activeind = '1'
            ";
        
        if (!empty($_GET['month'])) {
            $month = $_GET['month'];
            if (strlen($month) > 1) {
                $bulan = $month;
            } else {
                $bulan = '0' . $month;
            }
            
            $year = date('Y');
            if (!empty($_GET['year'])) {
                $year = $_GET['year'];
            }
            $date = '01-' . $bulan . '-' . $year;

            $monthdate = date('Y-m-d', strtotime($date));

            $sql .= " AND dls_ai_application.aiap_registrantid NOT IN (
                SELECT return_application.reap_registrantid 
                FROM return_application
                WHERE return_application.reap_datefrom = '" . $monthdate . "'
                )";
        }
            
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        if (isset($_GET['btnexcel'])) {
            return $this->render('not_submitted_excel', [
                'model' => $model,
            ]);
        }
        
        return $this->render('not_submitted', [
            'model' => $model,
        ]);
    }
}