Your IP : 216.73.216.79


Current Path : /var/www/html/mydlvsacc/backend/models/
Upload File :
Current File : /var/www/html/mydlvsacc/backend/models/Laporan.backup.php

<?php

namespace backend\models;

use Yii;
use backend\models\application\AiApplication;
use backend\models\returns\ReturnApplication;
use backend\models\returns\ReturnStatement;
use backend\models\returns\ReturnPayments;
use backend\models\returns\ReturnReceipt;

class Laporan extends \yii\db\ActiveRecord
{
    /* LAPORAN HASIL MENGIKUT KELAS PENERBANGAN */
    public static function sumFlightseat($type, $code)
    { 
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        $dateend = '';
        if (!empty($_GET['date_end'])) {
            $dateend = date('Y-m-d', strtotime($_GET['date_end']));
        }
        
        
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            WHERE dls_ai_application.aiap_primarydepaturecode = :stesen
            ";
        
        if (!empty($datestart)) {
            if ($dateend == '') {
                $dateend = date('Y-m-d');
            }
            $sql .= " AND return_application.reap_datefrom BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)
                ->bindValue(':stesen', $code)
                ->queryAll();
        
        $total = 0;
        
        if (!empty($model)) {
            foreach ($model as $mod) {
                $asean = 0;
                $nonasean = 0;
                $asianeco = 0;
                $nonasianeco = 0;
                $asiannoneco = 0;
                $nonasiannoneco = 0;
                $economy1 = 0;
                $economy2 = 0;
                $economy3 = 0;
                $economy4 = 0;
                $model3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'D', 'reas_paymentstatus' => 'S'])->all();
                if (!empty($model3)) {
                    foreach ($model3 as $mod3) {
                        switch ($mod3->reas_code) {
                            case '506':
                                $asean += $mod3->reas_debitnote;
                                break;
                            case '507':
                                $nonasean += $mod3->reas_debitnote;
                                break;
                        }
                    }
                }

                $model4 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'C'])->all();
                if (!empty($model4)) {
                    foreach ($model4 as $mod4) {
                        switch ($mod4->reas_syscode) {
                            case 'EA':
                                $asianeco += $mod4->reas_creditnote;
                                break;
                            case 'EN':
                                $nonasianeco += $mod4->reas_creditnote;
                                break;
                            case 'OA':
                                $asiannoneco += $mod4->reas_creditnote;
                                break;
                            case 'ON':
                                $nonasiannoneco += $mod4->reas_creditnote;
                                break;
                        }
                    }
                }
                
                $economy1 = $asean - $asianeco;
                if ($economy1 > 0) {
                    $economy2 = $economy1 - $asiannoneco;
                }

                $economy3 = $nonasean - $nonasianeco;
                if ($economy3 > 0) {
                    $economy4 = $economy3 - $nonasiannoneco;
                }

                if ($type == 'E') {
                    if ($economy1 >= 0) {
                        $total += $asianeco;
                    } else {
                        $total += $asean;
                    }
                    if ($economy3 >= 0) {
                        $total += $nonasianeco;
                    } else {
                        $total += $nonasean;
                    }
                }

                if ($type == 'NE') {
                    if ($economy2 >= 0) {
                        $total += $asiannoneco;
                    } else {
                        $total += $economy1;
                    }
                    if ($economy4 >= 0) {
                        $total += $nonasiannoneco;
                    } else {
                        $total += $economy3;
                    }
                }
            }
        }
        
        return $total;
    }
    
    /* LAPORAN HASIL MENGIKUT KELAS PENERBANGAN */
    public static function sumTotaleconomy($val, $val2) 
    {
        $total = $val + $val2;
        return $total;
    }

    /* LAPORAN HASIL MENGIKUT KELAS PENERBANGAN (TERPERINCI) */
    public static function sumCategoryseat($type, $codedepartment, $code)
    {         
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        $dateend = '';
        if (!empty($_GET['date_end'])) {
            $dateend = date('Y-m-d', strtotime($_GET['date_end']));
        }
        
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            WHERE dls_ai_application.aiap_primarydepaturecode = :stesen 
            ";
        
        if (!empty($datestart)) {
            if ($dateend == '') {
                $dateend = date('Y-m-d');
            }
            $sql .= " AND return_application.reap_datefrom BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)
                ->bindValue(':stesen', $code)
                ->queryAll();
        
        $totalea = 0;
        $totalnea = 0;
        $totalena = 0;
        $totalnena = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $asean = 0;
                $nonasean = 0;
                $asianeco = 0;
                $nonasianeco = 0;
                $asiannoneco = 0;
                $nonasiannoneco = 0;
                $economy1 = 0;
                $economy2 = 0;
                $economy3 = 0;
                $economy4 = 0;
                $model3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'D', 'reas_paymentstatus' => 'S'])->all();
                if (!empty($model3)) {
                    foreach ($model3 as $mod3) {
                        switch ($mod3->reas_code) {
                            case '506':
                                $asean += $mod3->reas_debitnote;
                                break;
                            case '507':
                                $nonasean += $mod3->reas_debitnote;
                                break;
                        }
                    }
                }

                $model4 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'C'])->all();
                if (!empty($model4)) {
                    foreach ($model4 as $mod4) {
                        switch ($mod4->reas_syscode) {
                            case 'EA':
                                $asianeco += $mod4->reas_creditnote;
                                break;
                            case 'EN':
                                $nonasianeco += $mod4->reas_creditnote;
                                break;
                            case 'OA':
                                $asiannoneco += $mod4->reas_creditnote;
                                break;
                            case 'ON':
                                $nonasiannoneco += $mod4->reas_creditnote;
                                break;
                        }
                    }
                }
                
                if ($codedepartment == '506') {
                    $economy1 = $asean - $asianeco;
                    if ($economy1 > 0) {
                        $economy2 = $economy1 - $asiannoneco;
                    }

                    if ($type == 'E') {
                        if ($economy1 >= 0) {
                            $totalea += $asianeco;
                        } else {
                            $totalea += $asean;
                        }
                    }
                    if ($type == 'N') {
                        if ($economy2 >= 0) {
                            $totalnea += $asiannoneco;
                        } else {
                            $totalnea += $economy1;
                        }
                    }
                }

                if ($codedepartment == '507') {
                    $economy3 = $nonasean - $nonasianeco;
                    if ($economy3 > 0) {
                        $economy4 = $economy3 - $nonasiannoneco;
                    }

                    if ($type == 'E') {
                        if ($economy3 >= 0) {
                            $totalena += $nonasianeco;
                        } else {
                            $totalena += $nonasean;
                        }
                    }
                    if ($type == 'N') {
                        if ($economy4 >= 0) {
                            $totalnena += $nonasiannoneco;
                        } else {
                            $totalnena += $economy3;
                        }
                    }
                }
            }
        }
        
        if ($codedepartment == '506') {
            if ($type == 'E') {
                return $totalea;
            }
            if ($type == 'N') {
                return $totalnea;
            }
        }

        if ($codedepartment == '507') {
            if ($type == 'E') {
                return $totalena;
            }
            if ($type == 'N') {
                return $totalnena;
            }
        }
    }
    
    /* LAPORAN HASIL MENGIKUT KELAS PENERBANGAN (TERPERINCI) */
    public static function totalseatdetail($code)
    {
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        $dateend = '';
        if (!empty($_GET['date_end'])) {
            $dateend = date('Y-m-d', strtotime($_GET['date_end']));
        }
        
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            WHERE dls_ai_application.aiap_primarydepaturecode = :stesen 
            ";
        
        if (!empty($datestart)) {
            if ($dateend == '') {
                $dateend = date('Y-m-d');
            }
            $sql .= " AND return_application.reap_datefrom BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)
                ->bindValue(':stesen', $code)
                ->queryAll();
        
        $total = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $asean = 0;
                $nonasean = 0;
                $asianeco = 0;
                $nonasianeco = 0;
                $asiannoneco = 0;
                $nonasiannoneco = 0;
                $economy1 = 0;
                $economy2 = 0;
                $economy3 = 0;
                $economy4 = 0;
                $model3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'D', 'reas_paymentstatus' => 'S'])->all();
                if (!empty($model3)) {
                    foreach ($model3 as $mod3) {
                        switch ($mod3->reas_code) {
                            case '506':
                                $asean += $mod3->reas_debitnote;
                                break;
                            case '507':
                                $nonasean += $mod3->reas_debitnote;
                                break;
                        }
                    }
                }

                $model4 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'C'])->all();
                if (!empty($model4)) {
                    foreach ($model4 as $mod4) {
                        switch ($mod4->reas_syscode) {
                            case 'EA':
                                $asianeco += $mod4->reas_creditnote;
                                break;
                            case 'EN':
                                $nonasianeco += $mod4->reas_creditnote;
                                break;
                            case 'OA':
                                $asiannoneco += $mod4->reas_creditnote;
                                break;
                            case 'ON':
                                $nonasiannoneco += $mod4->reas_creditnote;
                                break;
                        }
                    }
                }
                
                $economy1 = $asean - $asianeco;
                if ($economy1 > 0) {
                    $economy2 = $economy1 - $asiannoneco;
                }

                $economy3 = $nonasean - $nonasianeco;
                if ($economy3 > 0) {
                    $economy4 = $economy3 - $nonasiannoneco;
                }

                if ($economy1 >= 0) {
                    $total += $asianeco;
                } else {
                    $total += $asean;
                }

                if ($economy2 >= 0) {
                    $total += $asiannoneco;
                } else {
                    $total += $economy1;
                }

                if ($economy3 >= 0) {
                    $total += $nonasianeco;
                } else {
                    $total += $nonasean;
                }

                if ($economy4 >= 0) {
                    $total += $nonasiannoneco;
                } else {
                    $total += $economy3;
                }
            }
        }
        
        return $total;
    }
    
    public static function calculateSum($codedepartment, $code, $datestart = '', $dateend = '')
    {
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        $dateend = '';
        if (!empty($_GET['date_end'])) {
            $dateend = date('Y-m-d', strtotime($_GET['date_end']));
        }
        
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            WHERE dls_ai_application.aiap_primarydepaturecode = :stesen 
            ";
        
        if (!empty($datestart)) {
            if ($dateend == '') {
                $dateend = date('Y-m-d');
            }
            $sql .= " AND return_application.reap_datefrom BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)
                ->bindValue(':stesen', $code)
                ->queryAll();
        
        $paymentval = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                switch ($codedepartment) {
                    case '506':
                        $statement = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'PA', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement)) {
                            foreach ($statement as $stat) {
                                $paymentval = $paymentval + $stat->reas_debitnote;
                            }
                        }
                        $statement2 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'EAP', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement2)) {
                            foreach ($statement2 as $stat2) {
                                $paymentval = $paymentval + $stat2->reas_debitnote;
                            }
                        }
                        $statement3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'FA', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement3)) {
                            foreach ($statement3 as $stat3) {
                                $paymentval = $paymentval + $stat3->reas_debitnote;
                            }
                        }
                        break;
                    case '507':
                        $statement = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'PN', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement)) {
                            foreach ($statement as $stat) {
                                $paymentval = $paymentval + $stat->reas_debitnote;
                            }
                        }
                        $statement2 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'ENP', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement2)) {
                            foreach ($statement2 as $stat2) {
                                $paymentval = $paymentval + $stat2->reas_debitnote;
                            }
                        }
                        $statement3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'FN', 'reas_paymentstatus' => 'S'])->all();
                        if (!empty($statement3)) {
                            foreach ($statement3 as $stat3) {
                                $paymentval = $paymentval + $stat3->reas_debitnote;
                            }
                        }
                        break;
                }
            }
        }

        return $paymentval;
    }
    
    public static function calculateSumall($code, $datestart = '', $dateend = '')
    {
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        $dateend = '';
        if (!empty($_GET['date_end'])) {
            $dateend = date('Y-m-d', strtotime($_GET['date_end']));
        }
        
        $sql = "SELECT * FROM dls_ai_application 
            INNER JOIN return_application ON return_application.reap_registrantid = dls_ai_application.aiap_registrantid
            WHERE dls_ai_application.aiap_primarydepaturecode = :stesen 
            ";
        
        if (!empty($datestart)) {
            if ($dateend == '') {
                $dateend = date('Y-m-d');
            }
            $sql .= " AND return_application.reap_datefrom BETWEEN '" . $datestart . "' AND '" . $dateend . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)
                ->bindValue(':stesen', $code)
                ->queryAll();
        
        $arrsyscode = ['PA', 'PN', 'EAP', 'ENP','FA', 'FN'];
        $paymentval = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                foreach ($arrsyscode as $syscode) {
                    $statement = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => $syscode, 'reas_paymentstatus' => 'S'])->all();
                    if (!empty($statement)) {
                        foreach ($statement as $stat) {
                            $paymentval = $paymentval + $stat->reas_debitnote;
                        }
                    }
                }
            }
        }
        
        return $paymentval;
    }
    
    //laporan Harian
    public static function taxCollected($reid)
    {
        $total = 0.00;
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reid, 'reap_status' => 'S'])->all();
        if (!empty($model)) {
            foreach ($model as $mod) {
                $total = $total + $mod->reap_payment;
            }
        }
        
        return $total;
    }
    
    //laporan Harian
    public static function sumTaxCollected()
    {
        $total = 0.00;
        $model = ReturnPayments::find()->where(['reap_status' => 'S'])->all();
        if (!empty($model)) {
            foreach ($model as $mod) {
                $total = $total + $mod->reap_payment;
            }
        }
        
        return $total;
    }
    
    //laporan Harian
    public static function penaltyDaily($repaid)
    {
        $model = ReturnStatement::find()->where(['reas_reapid' => $repaid, 'reas_syscode' => 'P', 'reas_transactiontype' => 'OD'])->one();
        
        $total = 0;
        if (!empty($model)) { 
            $total = $model->reas_bod;
        }
        
        return $total;
    }
    
    //laporan statement
    public static function countStatementyear($stesencode, $year = '')
    {
        if ($year == "") {
            $year = date("Y");
        }
        
        $sql = "SELECT COUNT(*) 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'
            AND dls_ai_application.aiap_primarydepaturecode = '" . $stesencode . "' AND YEAR(return_application.reap_datefrom) = '" . $year . "'";
        
        $model = Yii::$app->db->createCommand($sql)->queryScalar();
        
        return $model;
    }
    
    /* LAPORAN PENYATA DL-02 (statement) */
    public static function sumTaxYear($type, $stesencode, $year = '')
    {
        if ($year == "") {
            $year = date("Y");
        }
        
        $ecotype= '';
        switch ($type) {
            case ('ase'): 
                $ecotype = 'reap_carriedasean';
                break;
            case ('non'):
                $ecotype = 'reap_carriednotasean';
                break;
            case ('exase'): 
                $ecotype = 'reap_exceptasean';
                break;
            case ('exnon'):
                $ecotype = 'reap_exceptnotasean';
                break;
            case ('aseeco'):
                $ecotype = 'reap_chargeeconomyasean';
                break;
            case ('nonaseeco'):
                $ecotype = 'reap_chargeeconomynotasean';
                break;
            case ('asenoneco'):
                $ecotype = 'reap_chargenoteconomyasean';
                break;
            case ('nonasenoneco'):
                $ecotype = 'reap_chargenoteconomynotasean';
                break;
            case ('rateaseeco'):
                $ecotype = 'reap_rateeconomyaseancountry';
                break;
            case ('rateasenoneco'):
                $ecotype = 'reap_rateothersclassaseancountry';
                break;
            case ('ratenonaseeco'):
                $ecotype = 'reap_rateeconomyotherscountry';
                break;
            case ('ratenonasenoneco'):
                $ecotype = 'reap_rateothersclassotherscountry';
                break;
        }
        
        $sql = "SELECT SUM(" . $ecotype . ") 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'
            AND dls_ai_application.aiap_primarydepaturecode = '" . $stesencode . "' 
            AND YEAR(return_application.reap_datefrom) = '" . $year . "'";
        
        $model = Yii::$app->db->createCommand($sql)->queryScalar();
        
        return $model;
    }
    
    public static function bigsumTaxYear($type, $year = '')
    {
        /* carian */
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $searchyear = '';
        if (!empty($_GET['year'])) {
            $searchyear = $_GET['year'];
        }
        /* carry */
        if ($year == "") {
            $year = date("Y");
        }
        
        $ecotype= '';
        switch ($type) {
            case ('ase'): 
                $ecotype = 'reap_carriedasean';
                break;
            case ('non'):
                $ecotype = 'reap_carriednotasean';
                break;
            case ('exase'): 
                $ecotype = 'reap_exceptasean';
                break;
            case ('exnon'):
                $ecotype = 'reap_exceptnotasean';
                break;
            case ('ecoase'):
                $ecotype = 'reap_chargeeconomyasean';
                break;
            case ('econon'):
                $ecotype = 'reap_chargeeconomynotasean';
                break;
            case ('nonecoase'):
                $ecotype = 'reap_chargenoteconomyasean';
                break;
            case ('noneconon'):
                $ecotype = 'reap_chargenoteconomynotasean';
                break;
            case ('rateaseeco'):
                $ecotype = 'reap_rateeconomyaseancountry';
                break;
            case ('rateasenoneco'):
                $ecotype = 'reap_rateothersclassaseancountry';
                break;
            case ('ratenonaseeco'):
                $ecotype = 'reap_rateeconomyotherscountry';
                break;
            case ('ratenonasenoneco'):
                $ecotype = 'reap_rateothersclassotherscountry';
                break;
        }
        
        $sql = "SELECT SUM(" . $ecotype . ") 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'
            AND YEAR(return_application.reap_datefrom) = '" . $year . "'";
        
            if (!empty($stesenkod)) {
                $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
            }
            
            if (!empty($searchyear)) {
                $sql .= " AND YEAR(return_application.reap_datefrom) = '" . $searchyear . "'" ;
            }
            
        $model = Yii::$app->db->createCommand($sql)->queryScalar();
        
        return $model;
    }
    
    /* LAPORAN PENERIMAAN HARIAN */
    public static function totalPayment($reapid, $paymentid, $code = '')
    {
        $model = ReturnStatement::find()->where(['reas_reapid' => $reapid, 'reas_code' => $code, 'reas_paymentid' => $paymentid, 'reas_paymentstatus' => 'S'])->all();
        
        $total = 0;
        if (!empty($model)) { 
            foreach ($model as $mod) {
                $total += $mod->reas_debitnote;
            }
        }
                
        return $total;
    }
        
    /* LAPORAN RINGKASAN PEMBAYARAN */
    public static function totalPaymentdone($reapid)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->one();
        
        $total = 0;
        if (!empty($model)) {
            $total = $model->reap_payment;
//            foreach ($model as $mod) {
//                $total += $mod->reap_payment;
//            }
        }
        
        return $total;
    }
    
    /* LAPORAN RINGKASAN PEMBAYARAN */
    public static function totalAllPaymentdone()
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $searchyear = '';
        if (!empty($_GET['year'])) {
            $searchyear = $_GET['year'];
        }
        
        $model = ReturnPayments::find()->where(['reap_status' => 'S'])->all();
        
        $total = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $sql2 = "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'
                    AND return_application.reap_id = :applicationid
                    ";
                
                if (!empty($stesenkod)) {
                    $sql2 .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
                }
                
                if (!empty($searchyear)) {
                    $sql2 .= " AND YEAR(return_application.reap_datefrom) = '" . $searchyear . "'" ;
                }
                
                $model2 = Yii::$app->db->createCommand($sql2)->bindValue(':applicationid', $mod->repa_applicationid)->queryScalar();
                if (!empty($model2)) {
                    $total += $mod->reap_payment;
                }
            }
        }
        
        return $total;
    }
    
    /* LAPORAN RINGKASAN PEMBAYARAN */
    public static function exceptLevi($reapid) 
    {
        $model = ReturnApplication::findOne($reapid);
        
        $total = 0;
        if (!empty($model)) {
            $total = $model->reap_exceptasean + $model->reap_exceptnotasean;
        }
        
        return $total;
    }
    
    /* LAPORAN RINGKASAN PEMBAYARAN */
    public static function exceptLeviAll() 
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $searchyear = '';
        if (!empty($_GET['year'])) {
            $searchyear = $_GET['year'];
        }
        
        $model = ReturnApplication::find()->all();
        
        $total = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
            $sql2 = "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'
                    AND return_application.reap_id = :applicationid
                    ";
            
                if (!empty($stesenkod)) {
                    $sql2 .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
                }
                
                if (!empty($searchyear)) {
                    $sql2 .= " AND YEAR(return_application.reap_datefrom) = '" . $searchyear . "'" ;
                }
            
                $model2 = Yii::$app->db->createCommand($sql2)->bindValue(':applicationid', $mod->reap_id)->queryScalar();
                if (!empty($model2)) {
                    $total = $total + ($mod->reap_exceptasean + $mod->reap_exceptnotasean);
                }
            }
        }
        
        return $total;
    }
    
    /* LAPORAN RINGKASAN PEMBAYARAN */
    public static function getReceipt($reapid, $type)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->one();
        
        $data = '-';
        if (!empty($model)) {
            $model2 = ReturnReceipt::find()->where(['rert_paymentid' => $model->repa_id])->one();
            if (!empty($model2)) {
                switch ($type) {
                    case 'dt':
                        $data = date('d/m/Y H:i:s', strtotime($model2->rert_receiptdate));
                        break;
                    case 'no':
                        $data = $model2->rert_receiptno;
                        break;
                    
                    default :
                        $data = '-';
                        break;
                } 
            }
        }
        
        return $data;
    }
    
    /* LAPORAN LEJER AM */
    public static function needTopay($type, $reapid)
    {
        $model = ReturnApplication::findOne($reapid);
        $model2 = ReturnStatement::find()->where(['reas_reapid' => $reapid, 'reas_transactiontype' => 'C'])->all();
        
        $total = 0;
        $asean = 0;
        $nonasean = 0;
        $penalty = 0;
        if (!empty($model2)) {
            foreach ($model2 as $mod2) {
                switch ($mod2->reas_syscode) {
                    case 'EA':
                        $asean = $asean + $mod2->reas_creditnote;
                        break;
                    case 'OA':
                        $asean = $asean + $mod2->reas_creditnote;
                        break;
                    case 'EN':
                        $nonasean = $nonasean + $mod2->reas_creditnote;
                        break;
                    case 'ON':
                        $nonasean= $nonasean + $mod2->reas_creditnote;
                        break;
                    case 'P':
                        $penalty= $penalty + $mod2->reas_creditnote;
                        break;
                }
            }
        }
        
        if ($type == 'A') {
            $total = $asean;
        } elseif ($type == 'N') {
            $total = $nonasean;
        } else {
            $total = $penalty;
        }
        
        return $total;
    }
    
    /* LAPORAN LEJER AM */
    public static function totalNeedTopay($type)
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        /*
        $model = ReturnApplication::find()
                ->select('*')
                ->innerJoin('dls_ai_application', "dls_ai_application.aiap_registrantid = return_application.reap_registrantid")
                ->innerJoin('dls_ai_appstatus', "dls_ai_application.aiap_id = dls_ai_appstatus.aias_applicationid")
                ->where(['dls_ai_appstatus.aias_statuscode' => 'A'])
                ->andWhere(['dls_ai_appstatus.aias_activeind' => '1'])
                //->andWhere(['dls_ai_application.aiap_primarydepaturecode' => $stesenkod])
                //->andWhere('return_application.reap_declaredate BETWEEN "' . $datestart . '" AND "' . $dateend . '"')
                ->all();
        */
        $sql = "SELECT * FROM `return_application` 
            INNER JOIN `dls_ai_application` ON dls_ai_application.aiap_registrantid = return_application.reap_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 (!empty($stesenkod)) {
            $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
        }

        if (!empty($datestart)) {
            $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 . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        
        $total = 0;
        $asean = 0;
        $nonasean = 0;
        $penalty = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $model2 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_transactiontype' => 'C'])->all();

                if (!empty($model2)) {
                    foreach ($model2 as $mod2) {
                        switch ($mod2->reas_syscode) {
                            case 'EA':
                                $asean = $asean + $mod2->reas_creditnote;
                                break;
                            case 'OA':
                                $asean = $asean + $mod2->reas_creditnote;
                                break;
                            case 'EN':
                                $nonasean = $nonasean + $mod2->reas_creditnote;
                                break;
                            case 'ON':
                                $nonasean = $nonasean + $mod2->reas_creditnote;
                                break;
                            case 'P':
                                $penalty = $penalty + $mod2->reas_creditnote;
                                break;
                        }
                    }
                }
        
                if ($type == 'A') {
                    $total = $asean;
                } elseif ($type == 'N') {
                    $total = $nonasean;
                } else {
                    $total = $penalty;
                }
            }
        }
        
        return $total;
    }
    
    /* LAPORAN LEJER AM */
    public static function valPayment($type, $reapid)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->all();
        
        $fpx = 0;
        $eft = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                if ($mod->reap_title == 'Financial Process Exchange (FPX)')
                {
                    $fpx = $fpx + $mod->reap_payment;
                }
                
                if ($mod->reap_title == 'Electronic Funds Transfer (EFT)')
                {
                    $eft = $eft + $mod->reap_payment;
                }
            }
        }
        
        if ($type == 'FPX') {
            return $fpx;
        } else {
            return $eft;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function totalvalPayment($type)
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        
        $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 (!empty($stesenkod)) {
            $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
        }

        if (!empty($datestart)) {
            $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 . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
        //$model = ReturnPayments::find()->where(['reap_status' => 'S'])->all();
        
        $fpx = 0;
        $eft = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                if ($mod['reap_title'] == 'Financial Process Exchange (FPX)')
                {
                    $fpx = $fpx + $mod['reap_payment'];
                }
                
                if ($mod['reap_title'] == 'Electronic Funds Transfer (EFT)')
                {
                    $eft = $eft + $mod['reap_payment'];
                }
            }
        }
        
        if ($type == 'FPX') {
            return $fpx;
        } else {
            return $eft;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function datePayment($reapid)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->all();
        
        $date = '';
        if (!empty($model)) {
            foreach ($model as $mod) {
                if ($mod->reap_title == 'Financial Process Exchange (FPX)') {
                    $date = date('d/m/Y H:i:s', strtotime($mod->reap_paymentdate)) . '<br />';
                } else {
                    $date = date('d/m/Y H:i:s', strtotime($mod->reap_eftdate)) . '<br />';
                }
            }
        }
        
        return $date;
    }
    
    /* LAPORAN LEJER AM */
    public static function receiptPayment($type, $reapid)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->all();
        
        $receiptno = '';
        $receiptdt = '';
        if (!empty($model)) {
            foreach ($model as $mod) {
                $model2 = ReturnReceipt::find()->where(['rert_paymentid' => $mod->repa_id])->one();
                
                if (!empty($model2)) {
                    $receiptno = $model2->rert_receiptno . '<br />';
                    $receiptdt = date('d/m/Y H:i:s', strtotime($model2->rert_receiptdate)) . '<br />';
                }
            }
        }
        
        if ($type == 'no') {
            return $receiptno;
        } else {
            return $receiptdt;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function balTopay($reapid)
    {
        $model = ReturnApplication::findOne($reapid);
        $model2 = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->all();
        
        $totalpay = 0;
        $balance = 0;
        if (!empty($model2)) {
            foreach ($model2 as $mod2) {
                $totalpay += $mod2->reap_payment;
            }
        }
        
        $balance = $model->reap_totalamount - $totalpay;
        
        if ($balance > 0) {
            return $balance;
        } else {
            return 0;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function totalBalTopay()
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        
        $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 (!empty($stesenkod)) {
            $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
        }

        if (!empty($datestart)) {
            $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 . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
//        $model = ReturnApplication::find()->all();
        
        $balance = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $model2 = ReturnPayments::find()->where(['repa_applicationid' => $mod['reap_id'], 'reap_status' => 'S'])->all();
                
                $totalpay = 0;
                if (!empty($model2)) {
                    foreach ($model2 as $mod2) {
                        $totalpay += $mod2->reap_payment;
                    }
                }
        
                $totalamount = 0;
                if (!empty($mod['reap_totalamount'])) {
                    $totalamount = $mod['reap_totalamount'];
                }

                $balance = $balance + ($totalamount - $totalpay);
            }
        }
                
        if ($balance > 0) {
            return $balance;
        } else {
            return 0;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function balpanaltyTopay($reapid)
    {
        $model = ReturnApplication::findOne($reapid);
        $model2 = ReturnPayments::find()->where(['repa_applicationid' => $reapid, 'reap_status' => 'S'])->all();
        $model3 = ReturnStatement::find()->where(['reas_reapid' => $reapid, 'reas_syscode' => 'P', 'reas_transactiontype' => 'C'])->all();
        
        $totalpay = 0;
        $balance = 0;
        if (!empty($model2)) {
            foreach ($model2 as $mod2) {
                $totalpay += $mod2->reap_payment;
            }
        }
        
        $totalamount = 0;
        if (!empty($model->reap_totalamount)) {
            $totalamount = $model->reap_totalamount;
        }
        
        $balance = $totalamount - $totalpay;
        
        $panalty = 0;
        if ($balance < 0) {
            if (!empty($model3)) {
                foreach ($model3 as $mod3) {
                    $panalty += $mod3->reas_creditnote;
                }
            }
            
            $balpanalty = $totalpay - $model->reap_totalamount;
            $balpanalty2 = $balpanalty - $panalty;
            
            if ($balpanalty2 > 0) {
                return $balpanalty2;
            } else {
                return 0;
            }
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function totalBalpanaltyTopay()
    {
        $stesenkod = '';
        if (!empty($_GET['stesen_code'])) {
            $stesenkod = $_GET['stesen_code'];
        }
        $datestart = '';
        if (!empty($_GET['date_start'])) {
            $datestart = date('Y-m-d', strtotime($_GET['date_start']));
        }
        
        $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 (!empty($stesenkod)) {
            $sql .= " AND dls_ai_application.aiap_primarydepaturecode = '" . $stesenkod . "'";
        }

        if (!empty($datestart)) {
            $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 . "'";
        }
        
        $model = Yii::$app->db->createCommand($sql)->queryAll();
//        $model = ReturnApplication::find()->all();
        
        $balance = 0;     
        $balpanalty2 = 0;
        if (!empty($model)) {
            foreach ($model as $mod) {
                $model2 = ReturnPayments::find()->where(['repa_applicationid' => $mod['reap_id'], 'reap_status' => 'S'])->all();
                $model3 = ReturnStatement::find()->where(['reas_reapid' => $mod['reap_id'], 'reas_syscode' => 'P', 'reas_transactiontype' => 'C'])->all();
          
                $totalpay = 0;
                if (!empty($model2)) {
                    foreach ($model2 as $mod2) {
                        $totalpay += $mod2->reap_payment;
                    }
                }
                
                $totalamount = 0;
                if (!empty($mod['reap_totalamount'])) {
                    $totalamount = $mod['reap_totalamount'];
                }

                $balance = $totalamount - $totalpay;
                 
                $panalty = 0;
                if ($balance < 0) {
                    if (!empty($model3)) {
                        foreach ($model3 as $mod3) {
                            $panalty += $mod3->reas_creditnote;
                        }
                    }

                    $balpanalty = $totalpay - $totalamount;
                    $balpanalty2 = $balpanalty2 + ($balpanalty - $panalty);
                }
            }
        }
        
        if ($balpanalty2 > 0) {
            return $balpanalty2;
        } else {
            return 0;
        }
    }
    
    /* LAPORAN LEJER AM */
    public static function statusPayment($reapid)
    {
        $model = ReturnPayments::find()->where(['repa_applicationid' => $reapid])->all();
        
        $status = '';
        if (!empty($model)) {
            foreach ($model as $mod) {
                $status = $mod->reap_status;
                switch ($status) {
                    case 'F' :
                        return '<span class="label label-danger">Amendment</span>';
                        break;
                    case 'I':
                        return '<span class="label label-primary">Initiate</span>';
                        break;
                    case 'P' :
                        return '<span class="label label-warning">Amendment</span>';
                        break;
                    case 'S' :
                        return '<span class="label label-success">Success</span>';
                        break;
                    
                    default :
                        return '-';
                        break;
                }
            }
        }
    }
}