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

class Settlement extends Modul_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("MdlTransaksi");
        /* ----------------------------------------------------------------------------------
 * validasi session bila tidak ada dipaksa ke halaman login
 * ----------------------------------------------------------------------------------*/

    }

    public function index()
    {
//        arrPrint($this->uri->segment_array());
        $starttime = microtime(true);
        if (!isset($this->session->login['id'])) {
            redirect(base_url() . "Login");
        }

        $sesionReplacer = replaceSession();
        $jenisTr = $this->jenisTr;
        $cCode = $this->cCode;
        $curentController = $this->uri->segment(2);
        $paymentConfig = isset($this->configUi[$jenisTr]['paymentConfig']) ? $this->configUi[$jenisTr]['paymentConfig'] : false;
        $historyFields = isset($this->configUi[$jenisTr]['shortHistoryFields']) ? $this->configUi[$jenisTr]['shortHistoryFields'] : array();
        $kepoinFields = isset($this->configUi[$jenisTr]['shortKepoinFields']) ? $this->configUi[$jenisTr]['shortKepoinFields'] : array();
        $pairRegistries = isset($this->configUi[$jenisTr]['pairRegistries']) ? $this->configUi[$jenisTr]['pairRegistries'] : array();
        $shoppingCartPerTransaksi = isset($this->configUi[$jenisTr]['shoppingCartPerTransaksi']) ? $this->configUi[$jenisTr]['shoppingCartPerTransaksi'] : 0;
        $shoppingCartPerTransaksiBtn = isset($this->configUi[$jenisTr]['shoppingCartPerTransaksiBtn']) ? $this->configUi[$jenisTr]['shoppingCartPerTransaksiBtn'] : 0;
        $connectTo = isset($this->configUi[$jenisTr]['connectTo']) ? $this->configUi[$jenisTr]['connectTo'] : "";
        //-----------------------------------------------------
        $_SESSION[$cCode]["main"]['ppnFactor'] = $this->session->login["ppnFactor"];
        //-----------------------------------------------------

        //region link to add new transaction
        //tambahin hak akses custom
        if (placeCanMakeTrans_he_menu($this->session->login['membership'], $this->session->login['cabang_id'], $this->session->login['gudang_id'], $this->jenisTr, $this->configUiJenis)) {
            $createIndexes = (null != $this->config->item("transaksi_createIndex")) ? $this->config->item("transaksi_createIndex") : array();
            $isDisableMakeTrans = isset($this->configUi[$this->jenisTr]['isDisableMakeTrans']) ? $this->configUi[$this->jenisTr]['isDisableMakeTrans'] : false;
            if (array_key_exists($this->jenisTr, $createIndexes)) {
                $targetUrl = MODUL_PATH . $createIndexes[$this->jenisTr] . "/" . $this->jenisTr;
            }
            else {
                $targetUrl = MODUL_PATH . "Create/index/" . $this->jenisTr;
            }
            if ($isDisableMakeTrans) {
                $addLink = null;
            }
            else {
                $addLink = array(
                    "link" => $targetUrl,
                    "label" => "<span class='glyphicon glyphicon-plus'></span> create new " . $this->configUi[$jenisTr]["steps"][1]['label'],
                );
            }
        }
        else {
            $addLink = null;
        }
        //endregion

        $link_undoneList = base_url() . $this->modul . "/" . $curentController . "/selectPaymentExternSrc/$jenisTr/?gr=cGVtYmVsaWFu?ohyes=ohno";
        $link_historyList = base_url() . $this->modul . "/History/showData/$jenisTr/?limit=10";
        $scriptBottom = "";
        $scriptBottom .= "<script>
                            var viewUndoneList = function () {
                                top.$('#undoneList').html(\"<div class='row' style='margin-top: 14px; padding-bottom: 14px;'> <span class='col-lg-12 text-center'><img width='5%' src='".base_url()."assets/images/loading_16_p.gif'></span> </div>\");
                                setTimeout(function () {
                                    if (top.$('#undoneList').find('img').length > 0) {
                                        top.$('#undoneList').load('$link_undoneList', function(){if(typeof viewHistoryList != 'undefined') viewUndoneList})
                                    }
                                },1000);
                            }
                            var viewHistoryList = function () {
                                top.$('#historyList').html(\"<div class='row' style='margin-top: 14px; padding-bottom: 14px;'> <span class='col-lg-12 text-center'><img width='5%' src='".base_url()."assets/images/loading_16_p.gif'></span> </div>\");
                                setTimeout(function () {
                                    if (top.$('#historyList').find('img').length > 0) {
                                        top.$('#historyList').load('$link_historyList', function(){if(typeof viewHistoryList != 'undefined') viewHistoryList})
                                    }
                                },1000);
                            }
                           top.$('#undoneList').load('$link_undoneList', function(){if(typeof viewUndoneList != 'undefined') viewUndoneList });
                           top.$('#historyList').load('$link_historyList', function(){if(typeof viewHistoryList != 'undefined') viewHistoryList });
                     </script>
        ";
        //region prepare params to viewer
        $data = array(
            "mode" => "index",
            // "isMobile"                       => $isMob,
            "errMsg" => $this->session->errMsg,
            "template" => $this->configUi[$jenisTr]["template"],
            "title" => $this->configUi[$jenisTr]["label"],
            "subTitle" => $this->configUi[$jenisTr]["steps"][1]['label'],
            "jenisTr" => $jenisTr,
            "trName" => $this->configUi[$jenisTr]["label"],
            'addLink' => $addLink,

            "scriptBotto" => isset($scriptBottom) ? $scriptBottom : "",
        );
        //endregion

        $this->load->view("transaksi", $data);
    }

    public function selectPaymentExternSrc()
    {
        $targetJenis = $this->uri->segment(4);
        $this->load->model("MdlTransaksi");


        //        heInitCart($targetJenis);
        $initMasterValues = array(
            "olehID" => my_id(),
            "olehName" => my_name(),
            "sellerID" => my_id(),
            "sellerName" => my_name(),
            "placeID" => my_cabang_id(),
            "placeName" => my_cabang_nama(),
            "divID" => my_div_id(),
            "divName" => my_div_nama(),
            "cabangID" => my_cabang_id(),
            "cabangName" => my_cabang_nama(),
            "gudangID" => my_gudang_id(),
            "gudangName" => my_gudang_nama(),
            "jenis_usaha" => my_jenis_usaha(),
            "tokoID" => my_toko_id(),
            "tokoNama" => my_toko_nama(),
            "jenisTr" => $this->jenisTr,
            "jenisTrMaster" => $this->jenisTr,
            "jenisTrTop" => $this->configUiJenis['steps'][1]['target'],
            "jenisTrName" => $this->configUiJenis['steps'][1]['label'],
            "stepNumber" => 1,
            "stepCode" => $this->configUiJenis['steps'][1]['target'],
            "dtime" => dtimeNow(),
            "fulldate" => dtimeNow("Y-m-d"),
            // "jenis_pajak"=>$this->session->login['jenis_usaha'],
            //            "ppnFactor" => $this->session->login["ppnFactor"],
        );
        heInitCart_he_cart($targetJenis, $initMasterValues);

        //-----------------------------------------------------
        $_SESSION[$this->cCode]["main"]['ppnFactor'] = $this->session->login["ppnFactor"];
        //-----------------------------------------------------


        //==dapatkan srcJenis
        $paymentSources = null != ($this->config->item("payment_source")) ? $this->config->item("payment_source") : array();
        $readerDueDate = isset($this->configUi[$targetJenis]['dueDateReader']) ? $this->configUi[$targetJenis]['dueDateReader'] : false;

        if (sizeof($paymentSources) > 0) {
            foreach ($paymentSources as $src => $mainSpecs) {
                //                cekHere("$src");;
                if (sizeof($mainSpecs) > 0) {

                    foreach ($mainSpecs as $step => $sSpecTemp) {
                        //                        cekHere("$step");
                        //                        arrprint($sSpecTemp);
                        $sCtr = 0;
                        foreach ($sSpecTemp as $sSpec) {

                            //                            arrPrint($sSpec);
                            //                            cekHitam($src);
                            if (isset($sSpec['jenisTarget']) && $sSpec['jenisTarget'] == $targetJenis) {
                                $srcJenis = $sSpec['jenisSrc'];
                                $rawSrcJenis = $src;
                                $srcIndex = $sCtr;
                                $srcStep = $step;
                                //                            cekhijau($srcJenis . " memenuhi syarat");
                            }
                            else {
                                $srcJenis = isset($sSpec['jenisSrc']) ? $sSpec['jenisSrc'] : "";
                                $rawSrcJenis = "";
                                $step = "";
                                $srcIndex = "";
                                $srcStep = "";
                                //                            cekmerah($srcJenis . " TIDAK memenuhi syarat");
                            }
                        }
                        $sCtr++;

                    }
                }
            }
        }

        //        arrPrint($paymentSources[$rawSrcJenis][$srcStep][$srcIndex]);
        //cekHitam("$rawSrcJenis |$srcStep| $srcIndex");
        //        die();
        $defItemLabels = array(
            "extern_nama" => isset($paymentSources[$rawSrcJenis][$srcStep][$srcIndex]['externSrc']['extLabel']) ? $paymentSources[$rawSrcJenis][$srcStep][$srcIndex]['externSrc']['extLabel'] : "",
            //                "nomer"=>"receipt number",
            //                "fulldate"=>"date",
            "tagihan" => "due amount",
            "terbayar" => "paid",
            "diskon" => "discount",
            "sisa" => "due remain",
            "tagihan_valas" => "due amount",
            "terbayar_valas" => "paid",
            "diskon_valas" => "discount",
            "sisa_valas" => "due remain",
        );
        $itemLabels = (isset($this->configUi[$targetJenis]["shoppingCartReferenceExternFields"])) ? $this->configUi[$targetJenis]["shoppingCartReferenceExternFields"] : $defItemLabels;
        $itemLabels["due_date"] = "due date";
        $itemLabels["aging"] = "aging(days)";
        $itemLabels["over_due"] = "over due";

        //		die();
        //==dapatkan daftar kolom dari srcJenis
        //        $historyFields = $this->configUi[$srcJenis]['shortHistoryFields'];

        $tr = new MdlTransaksi();
        $tr->setFilters(array());
        $tr->addFilter("transaksi_payment_source.cabang_id='" . $this->session->login['cabang_id'] . "'");
        $tr->addFilter("sisa>0");
        $tmpSrc = $tr->lookupPaymentSrcByJenis($targetJenis)->result();
        cekHere($this->db->last_query());
        $tmpSrcDue = array();
        $dueEmployee = array();
        if ($readerDueDate) {
            $tr->setFilters(array());
            $tr->addFilter("status='1'");
            $tmpSrcDue = $tr->lookupAllDueDate()->result();
            $tempDataDues = array();
            $validDate = array();
            foreach ($tmpSrcDue as $tmpSrcDue_tmp) {
                $tempDataDues[$tmpSrcDue_tmp->customers_id][] = array(
                    "due_date" => $tmpSrcDue_tmp->due_date,
                    "aging_dtime" => $tmpSrcDue_tmp->dtime,
                );

            }
            //            arrPrint($tempDataDues);
            $dtime_now = strtotime(date("Y-m-d"));
            foreach ($tempDataDues as $cus_id => $tempDataDues_0) {
                $dueVal = array();
                $dtimeVal = array();
                foreach ($tempDataDues_0 as $dtime_val) {

                    $keyIndex = strtotime($dtime_val['due_date']);
                    $dueVal[] = $keyIndex;
                    $dtimeVal[$keyIndex] = array(
                        "due_date" => $dtime_val['due_date'],
                        "aging" => $dtime_val['aging_dtime'],
                    );

                }
                asort($dueVal);
                $key_index = $dueVal['0'];
                $date_due = $dtimeVal[$key_index]['due_date'];
                $aging = $dtimeVal[$key_index]['aging'];

                //cekHere("$date_due || $aging");
                if ($dtime_now > $key_index) {
                    $dueEmployee[$cus_id] = array(
                        "due_date" => formatField_he_format("dtime", $date_due),
                        "over_due" => umurDay($date_due) > 0 ? umurDay($date_due) : "0",
                        "aging" => umurDay($aging) > 0 ? umurDay($aging) : "0",
                    );
                }
            }


        }

        $items = array();
        $externs = array();
        $tagihans = array();
        $terbayars = array();
        $sisas = array();
        $diskons = array();
        $tagihans_valas = array();
        $terbayars_valas = array();
        $sisas_valas = array();
        $diskons_valas = array();
        $srcLabel = "";
        if (sizeof($tmpSrc) > 0) {
            foreach ($tmpSrc as $row) {
                $tmp = array();

                $classMarking = "";
                if (isset($dueEmployee[$row->extern_id])) {
                    $classMarking = "bg-danger";
                    //                    cekHere($row->extern_nama);
                }

                if (!in_array($row->extern_id, $externs)) {
                    if (!isset($tagihans[$row->extern_id])) {
                        $tagihans[$row->extern_id] = 0;
                        $terbayars[$row->extern_id] = 0;
                        $diskons[$row->extern_id] = 0;
                        $sisas[$row->extern_id] = 0;
                        $tagihans_valas[$row->extern_id] = 0;
                        $terbayars_valas[$row->extern_id] = 0;
                        $sisas_valas[$row->extern_id] = 0;
                        $diskons_valas[$row->extern_id] = 0;

                    }

                    $tmp = (array)$row;
                    $tmp["link"] = MODUL_PATH . get_class($this) . "/selectPaymentSrc/$targetJenis/" . $row->extern_id;
                    $tmp["due_date"] = isset($dueEmployee[$row->extern_id]['due_date']) ? formatField_he_format("dtime", $dueEmployee[$row->extern_id]['due_date']) : "-";
                    $tmp["aging"] = isset($dueEmployee[$row->extern_id]['aging']) ? $dueEmployee[$row->extern_id]['aging'] : "-";
                    $tmp["over_due"] = isset($dueEmployee[$row->extern_id]['over_due']) ? $dueEmployee[$row->extern_id]['over_due'] : "-";
                    $tmp["class_marking"] = $classMarking;
                    //                    $tmp["class_marking"] = "bg-danger";
                    //                    $tmp["tagihan"] = $tagihans[$row->extern_id];
                    $items[$row->extern_id] = $tmp;
                    $externs[] = $row->extern_id;
                    $externName = $row->extern_nama;
                }
                $tagihans[$row->extern_id] += isset($row->tagihan) ? $row->tagihan : 0;
                $terbayars[$row->extern_id] += isset($row->terbayar) ? $row->terbayar : 0;
                $sisas[$row->extern_id] += isset($row->sisa) ? $row->sisa : 0;
                $diskons[$row->extern_id] += isset($row->diskon) ? $row->diskon : 0;
                $tagihans_valas[$row->extern_id] += isset($row->tagihan_valas) ? $row->tagihan_valas : 0;
                $terbayars_valas[$row->extern_id] += isset($row->terbayar_valas) ? $row->terbayar_valas : 0;
                $sisas_valas[$row->extern_id] += isset($row->sisa_valas) ? $row->sisa_valas : 0;
                $diskons_valas[$row->extern_id] += isset($row->diskon_valas) ? $row->diskon_valas : 0;
                $srcLabel = $row->label;
            }

            foreach ($items as $externID => $iSpec) {
                $items[$externID]['tagihan'] = $tagihans[$externID];
                $items[$externID]['terbayar'] = $terbayars[$externID];
                $items[$externID]['diskon'] = $diskons[$externID];
                $items[$externID]['sisa'] = $sisas[$externID];
                $items[$externID]['tagihan_valas'] = $tagihans_valas[$externID];
                $items[$externID]['terbayar_valas'] = $terbayars_valas[$externID];
                $items[$externID]['diskon_valas'] = $diskons_valas[$externID];
                $items[$externID]['sisa_valas'] = $sisas_valas[$externID];
            }
        }

        $data = array(
            "mode" => $this->uri->segment(3),
            //            "template" => $this->configUi[$jenisTr]["template"],
            "title" => $this->configUi[$targetJenis]["label"],
            "subTitle" => isset($paymentSources[$rawSrcJenis][$srcStep][$srcIndex]['externSrc']['extLabel']) ? "select " . $paymentSources[$rawSrcJenis][$srcStep][$srcIndex]['externSrc']['extLabel'] . "(s) listed below" : "",
            "items" => $items,
            "srcLabel" => $srcLabel,
            "itemLabels" => $itemLabels,
            "jenisTr" => $this->jenisTr,
            //            "dueDate"    =>$dueEmployee,
        );
        //arrPrintWebs($data);
        if (!isset($_GET['json'])) {
            $this->load->view("transaksi", $data);
        }
        else {

            //            arrPrint($data);
        }

    }

    public function selectPaymentSrc()
    {
        if (!isset($this->session->login['id'])) {
            gotoLogin();
        }
        $externID = $this->uri->segment(5);
        $selectedTrID = $this->uri->segment(6);

        $scriptBottom = "";

        $jenisTr = $targetJenis = $this->jenisTr;
        $cCode = $this->cCode;


        heInitCart($targetJenis);
        $this->load->helper("he_versi_history_old");
        $_SESSION[$cCode]["main"]['ppnFactor'] = $this->session->login["ppnFactor"];
        $detailResetList = array(
            "items",
            //            "out_detail",
            //            "out_detail2",
            "tableIn_detail",
            "tableIn_detail2",
            "tableIn_detail_values",
            "tableIn_detail2_sum",
            "tableIn_detail_values2_sum",
        );
        foreach ($detailResetList as $sSName) {
            $_SESSION[$cCode][$sSName] = null;
            unset($_SESSION[$cCode][$sSName]);
        }


        if (!isset($_SESSION[$cCode])) {
            $_SESSION[$cCode] = array(
                "items" => array(),
                "main" => array(),

            );
        }
        if (!isset($_SESSION[$cCode]['main'])) {
            $_SESSION[$cCode]['main'] = array();
        }
        if (!isset($_SESSION[$cCode]['items'])) {
            $_SESSION[$cCode]['items'] = array();
        }


        //==dapatkan srcJenis
        $stepCode = $this->configUi[$targetJenis]['steps'][1]['target'];
        $steps = $this->configUi[$targetJenis]['steps'];
        $tagihanSrc = isset($this->configUi[$targetJenis]['tagihanSrc']) ? $this->configUi[$targetJenis]['tagihanSrc'] : "sisa";
        $paymentSources = null != ($this->config->item("payment_source")) ? $this->config->item("payment_source") : array();
        $returnRoutes = null != ($this->config->item("transaksi_returnRoutes")) ? $this->config->item("transaksi_returnRoutes") : array();
        $readerDueDate = isset($this->configUi[$targetJenis]['dueDateReader']) ? $this->configUi[$targetJenis]['dueDateReader'] : false;
        $isSettlement = isset($this->configUi[$targetJenis]['isSettlement']) ? $this->configUi[$targetJenis]['isSettlement'] : false;
        $rawPrevURL = isset($_GET['rawPrev']) ? $_GET['rawPrev'] : "";
        $prePrint = isset($steps[1]['prePrint']) ? $steps[1]['prePrint'] : "";

        $ppnDisabledConfig = isset($this->configUi[$targetJenis]['ppnDisabled']) ? $this->configUi[$targetJenis]['ppnDisabled'] : array();
        $kelebihanBayarConfig = isset($this->configUi[$targetJenis]['kelebihanBayar']) && ($this->configUi[$targetJenis]['kelebihanBayar'] == true) ? true : false;

        $jenisSrc = null;


        if (sizeof($paymentSources) > 0) {
            foreach ($paymentSources as $src => $sSpec) {
                $payConfigs = $paymentSources[$src];
                if (sizeof($payConfigs) > 0) {
                    //                    $sCtr = 0;
                    foreach ($payConfigs as $paymentSrcConfigTmp) {
                        $sCtr = 0;
                        foreach ($paymentSrcConfigTmp as $paymentSrcConfig) {
                            if (isset($paymentSrcConfig['jenisTarget']) && $paymentSrcConfig['jenisTarget'] == $targetJenis) {
                                $srcJenis = $paymentSrcConfig['jenisSrc'];
                                $rawSrcJenis = $src;
                                $srcIndex = $sCtr;
                                $jenisSrc = $srcJenis;
                            }
                            $sCtr++;
                        }

                    }
                }
            }
        }

        $references = array();
        if ($jenisSrc != null) {
            if (isset($returnRoutes[$jenisSrc])) {
                $retCode = $returnRoutes[$jenisSrc];


                $trr = new MdlTransaksi();
                $trr->setFilters(array());
//                $trr->addFilter("param='main'");
                $trr->addFilter("jenis='$retCode'");
//                $tmpR = $trr->lookupRegistries_joined()->result();
                $tmpR = $trr->lookupDataRegistries_joined();
                //cekHere($this->db->last_query());

                if (sizeof($tmpR) > 0) {
                    foreach ($tmpR as $row) {
//                        $main = blobDecode($row->values);
                        $main = blobDecode($row->main);
                        //                        arrprint($main);
                        $references[$main['referenceID']] = array(
                            "id" => $row->transaksi_id,
                            "nomer" => $row->nomer,
                            "refID" => $main['referenceID'],
                            "refJenis" => $main['referenceJenis'],
                            "refNum" => $main['referenceNomer'],
                            "harga" => isset($main['harga']) ? $main['harga'] : 0,
                            "nett" => isset($main['nett']) ? $main['nett'] : 0,


                        );
                    }
                }
            }
        }


        $elementConfigs = isset($this->configUi[$this->jenisTr]['receiptElements']) ? $this->configUi[$this->jenisTr]['receiptElements'] : array();;
        if ($isSettlement) {
            // cekMErah(__LINE__." || ".$this->uri->segment(3));
            $tr = new MdlTransaksi();
            $tr->setFilters(array());
            $tr->addFilter("transaksi_payment_source.cabang_id='" . $this->session->login['cabang_id'] . "'");

            if ($externID > 0) {
                $tr->addFilter("extern_id='$externID'");
            }

            $tr->addFilter("sisa>0.1");
            //        $tr->addFilter("sisa>0");

            if ($selectedTrID > 0) {
                $tr->addFilter("transaksi_id='$selectedTrID'");
            }
            $tmpSrc = $tr->lookupPaymentSrcByJenis($targetJenis)->result();
            //            $tmpSrc = $tr->lookupPaymentSrcByJenis_joined($targetJenis)->result();
            //            showLast_query("biru");
            //            $anu = blobDecode($tmpSrc[0]->externValueBlob);
            //            arrPrintPink($anu);
            //
//                        cekHitam($isSettlement);
//                        arrPrintWebs($tmpSrc);
            if (sizeof($tmpSrc) > 0) {
                foreach ($tmpSrc as $row) {
                    $externValueBlob_decode = blobDecode($row->externValueBlob);
                    //                    arrPrintPink($externValueBlob_decode);

                    $items[] = (array)$row;

                    //                    break;
                }
            }
            //            matiHEre("switcher format settlement");


            //---load ke session items, items2, items2_sum, items3_sum
            $action = "<button class='btn btn-danger'><span class='fa fa-trash'></span></button>";
// arrPrint($this->uri->segment_array());
//             matiHEre();
//             matiHere($this->uri->segment(3));
            $data = array(
                // "mode" => $this->uri->segment(3),
                "mode" => "selectPaymentSrc",
                "title" => $this->configUi[$targetJenis]["label"],
                "subTitle" => isset($externName) ? $externName : "",
                "jenisTr" => $targetJenis,
                "items" => $items,
                "prePrint" => isset($prePrint) ? $prePrint : "",
                "itemLabels" => isset($this->configUi[$targetJenis]["shoppingCartReferenceFields"]) ? $this->configUi[$targetJenis]["shoppingCartReferenceFields"] : $defaultItemLabels,
                "selectProcessor" => $this->configUi[$targetJenis]["selectorProcessor"],
                "paymentSubtitle" => "details for " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
                "btnLabel" => "continue " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
                //                "ses_outMaster"   => isset($_SESSION[$cCode]['main']) ? $_SESSION[$cCode]['main'] : array(),
                //                "ses_items"       => isset($_SESSION[$cCode]['items']) ? $_SESSION[$cCode]['items'] : array(),
                //
                "actionTarget" => "top.$('#result').load('" . MODUL_PATH . "Create/save/" . $targetJenis . "?rawPrev=$rawPrevURL');",
                //                "columnRecorderTarget"     => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/nilai_entry",
                //                "bankColumnRecorderTarget" => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/paymentMethod_cash",
                //
                //                "selectedBankID"       => isset($_SESSION[$cCode]['main']['cash_account']) ? $_SESSION[$cCode]['main']['cash_account'] : 0,
                //                "dueDateReader"        => $readerDueDate,
                //                "tagihanSrc"           => $tagihanSrc,
                //                "scriptBottom"         => $scriptBottom,
                //                "tagihanValue"         => isset($_SESSION[$cCode]['main'][$tagihanSrc]) ? $_SESSION[$cCode]['main'][$tagihanSrc] : $defSisa,
                "isPaymentRadioSelect" => isset($this->configUi[$this->jenisTr]['isPaymentRadioSelect']) ? $this->configUi[$this->jenisTr]['isPaymentRadioSelect'] : false,
                //                "kelebihanBayar"       => $kelebihanBayarConfig,
                //                "ppnDisabled"          => $ppnDisabled,
                //                "lockerDisabled"       => isset($lockerDisabled) ? $lockerDisabled : array(),
                //                "action" => "$action",
            );
            $this->load->view("transaksi3", $data);
        }
        else {
            //==dapatkan daftar kolom dari srcJenis
            $tr = new MdlTransaksi();
            $tr->setFilters(array());
            $tr->addFilter("transaksi_payment_source.cabang_id='" . $this->session->login['cabang_id'] . "'");

            if ($externID > 0) {
                $tr->addFilter("extern_id='$externID'");
            }

            $tr->addFilter("sisa>0.1");
            //        $tr->addFilter("sisa>0");

            if ($selectedTrID > 0) {
                $tr->addFilter("transaksi_id='$selectedTrID'");
            }
            $tmpSrc = $tr->lookupPaymentSrcByJenis_joined($targetJenis)->result();

            if ($readerDueDate) {
                $tr->setFilters(array());
                $tmpSrcDue = $tr->lookupAllDueDate()->result();
                //        arrPrint($tmpSrcDue);
                if (sizeof($tmpSrcDue) > 0) {
                    foreach ($tmpSrcDue as $tmpSrccDue_0) {
                        $mainDueDate[$tmpSrccDue_0->transaksi_id] = $tmpSrccDue_0->due_date;
                    }
                }
            }

            $externName = "";
            $items = array();
            $tempDueDate = array();
            $ppnDisabled = array();
            $lockerDisabled = array();
            $trIDs = array();
            if (sizeof($tmpSrc) > 0) {
                foreach ($tmpSrc as $row) {
                    $tmp = array();

                    $jenis = $row->jenis;
                    $transaksi_id = $row->id;
                    if ($readerDueDate) {
                        $tempDueDate = blobDecode($row->ids_prev);
                        $dueTime = array();
                        if (sizeof($tempDueDate) > 0) {
                            $time_due = array();
                            foreach ($tempDueDate as $k => $spdID) {
                                if (isset($mainDueDate[$spdID])) {
                                    $time_due[] = strtotime($mainDueDate[$spdID]);
                                    $dueTime[strtotime($mainDueDate[$spdID])] = $mainDueDate[$spdID];
                                }
                            }
                            asort($time_due);
                            $key_due = $time_due[0];
                        }
                        if ($key_due > 0) {
                            $date_now = strtotime(date("Y-m-d"));
                            $dateValid = strtotime($dueTime[$key_due]);
                            //                        cekHijau("$dateValid >$date_now");
                            if ($dateValid < $date_now) {
                                //                            cekHere("$dateValid >$date_now");
                                $tmp['class_bg'] = "bg-danger";
                            }
                            else {
                                $tmp['class_bg'] = "";
                            }
                        }
                        else {
                            $tmp['class_bg'] = "";
                        }

                        $tmp['due_date'] = isset($dueTime[$key_due]) ? formatField("dtime", $dueTime[$key_due]) : "-";
                        $tmp['aging'] = isset($dueTime[$key_due]) && umurDay($dueTime[$key_due]) > 0 ? umurDay($dueTime[$key_due]) : "0";
                    }
                    else {
                        $tmp['class_bg'] = "";
                    }


                    $tmp['refID'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['refID'] : "";
                    $tmp['refNum'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['refNum'] : "";
                    $tmp['refValue'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['nett'] : "";

                    if (strlen($prePrint) > 10) {
                        $linkPrint = base_url() . "$prePrint/$jenis/$transaksi_id";
                        $tmp['prePrint'] = "<button type='button' class='btn btn-link' onclick=\"window.open('$linkPrint')\"><i class='fa fa-print'></i></button>";
                    }

                    if (isset($ppnDisabledConfig['enabled']) && $ppnDisabledConfig['enabled'] == true) {
                        //                    if (($row->ppn_sisa > 0) && ($row->ppn_status == 1)) {
                        if (($row->ppn_status == 1)) {
                            $ppnDisabled[] = $row->transaksi_id;
                            $tmp['notes'] = isset($ppnDisabledConfig['notes']) ? "<span style='color:red;font-size:10px;'>" . $ppnDisabledConfig['notes'] . "</span>" : "-";
                        }
                    }

                    $items[] = (array)$row + $tmp;
                    $externName = $row->extern_nama;

                    $trIDs[] = $row->transaksi_id;
                }

                //            if (sizeof($trIDs) > 0) {
                //                $this->load->model("Mdls/MdlLockerTransaksi");
                //                $lt = New MdlLockerTransaksi();
                //                $lt->addFilter("transaksi_id in ('" . implode("','", $trIDs) . "')");
                //                $lt->addFilter("state='active'");
                //                $lt->addFilter("jumlah='0'");
                //                $lt_tmp = $lt->lookupAll()->result();
                //                if (sizeof($lt_tmp) > 0) {
                //                    foreach ($lt_tmp as $lt_row) {
                //                        $lockerDisabled[] = $lt_row->transaksi_id;
                //                    }
                //                }
                //            }

            }

            $cCode = "_TR_" . $targetJenis;

            $defaultItemLabels = array(
                "extern_label2" => "jenis",
                "nomer" => "receipt number",
                "nomer_top" => "receipt ref.",
                "refNum" => "return ref.",
                "fulldate" => "date",
                "tagihan" => "due amount",
                "refValue" => "returned",
                "terbayar" => "paid",
                "diskon" => "discount",
                "sisa" => "due remain",
            );

            $jQueryCustom = isset($this->configUi[$this->jenisTr]['jQueryCustom']) ? $this->configUi[$this->jenisTr]['jQueryCustom'] : "";

            if (sizeof($jQueryCustom) > 0) {
                $scriptBottom .= $jQueryCustom;
            }


            $defSisa = isset($_SESSION[$cCode]['main']['sisa']) ? $_SESSION[$cCode]['main']['sisa'] : 0;
            $data = array(
                "mode" => $this->uri->segment(2),
                "title" => $this->configUi[$targetJenis]["label"],
                "subTitle" => $externName,
                "jenisTr" => $targetJenis,
                "items" => $items,
                "prePrint" => $prePrint,
                "itemLabels" => isset($this->configUi[$targetJenis]["shoppingCartReferenceFields"]) ? $this->configUi[$targetJenis]["shoppingCartReferenceFields"] : $defaultItemLabels,
                "selectProcessor" => $this->configUi[$targetJenis]["selectorProcessor"],
                "paymentSubtitle" => "details for " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
                "btnLabel" => "continue " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
                "ses_outMaster" => isset($_SESSION[$cCode]['main']) ? $_SESSION[$cCode]['main'] : array(),
                "ses_items" => isset($_SESSION[$cCode]['items']) ? $_SESSION[$cCode]['items'] : array(),

                "actionTarget" => "top.$('#result').load('" . base_url() . "Transaksi/validate/" . $targetJenis . "?rawPrev=$rawPrevURL');",
                "columnRecorderTarget" => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/nilai_entry",
                "bankColumnRecorderTarget" => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/paymentMethod_cash",

                "selectedBankID" => isset($_SESSION[$cCode]['main']['cash_account']) ? $_SESSION[$cCode]['main']['cash_account'] : 0,
                "dueDateReader" => $readerDueDate,
                "tagihanSrc" => $tagihanSrc,
                "scriptBottom" => $scriptBottom,
                "tagihanValue" => isset($_SESSION[$cCode]['main'][$tagihanSrc]) ? $_SESSION[$cCode]['main'][$tagihanSrc] : $defSisa,
                "isPaymentRadioSelect" => isset($this->configUi[$this->jenisTr]['isPaymentRadioSelect']) ? $this->configUi[$this->jenisTr]['isPaymentRadioSelect'] : false,
                "kelebihanBayar" => $kelebihanBayarConfig,
                "ppnDisabled" => $ppnDisabled,
                "lockerDisabled" => isset($lockerDisabled) ? $lockerDisabled : array(),

            );

            if (!isset($_GET['json'])) {
                $this->load->view("transaksi", $data);
            }
            else {
                return json_encode($data);
            }
        }


        //        die("selecting payment src...");
    }

    public function selectPaymentSrcOLD()
    {
        if (!isset($this->session->login['id'])) {
            gotoLogin();
        }
        $externID = $this->uri->segment(5);
        $selectedTrID = $this->uri->segment(6);

        $this->load->model("MdlTransaksi");

        // arrPrintPink($this->uri->segment_array());
        $scriptBottom = "";
        $scriptTrigger = "";
        // if(isset($selectedTrID)){
        $scriptTrigger .= "$('.chRadio').trigger('click')";
        // }

        $jenisTr = $targetJenis = $this->jenisTr;
        $cCode = "_TR_" . $this->jenisTr;

        // cekHere("cCode:: $cCode " . __METHOD__ . " line " . __LINE__);
        heInitCart($targetJenis);
        $this->load->helper("he_versi_history_old");


        //-----------------------------------------------------
        $_SESSION[$cCode]["main"]['ppnFactor'] = $this->session->login["ppnFactor"];
        //-----------------------------------------------------


        $detailResetList = array(
            "items",
            //            "out_detail",
            //            "out_detail2",
            "tableIn_detail",
            "tableIn_detail2",
            "tableIn_detail_values",
            "tableIn_detail2_sum",
            "tableIn_detail_values2_sum",
        );
        foreach ($detailResetList as $sSName) {
            $_SESSION[$cCode][$sSName] = null;
            unset($_SESSION[$cCode][$sSName]);
        }


        if (!isset($_SESSION[$cCode])) {
            $_SESSION[$cCode] = array(
                "items" => array(),
                "main" => array(),

            );
        }
        if (!isset($_SESSION[$cCode]['main'])) {
            $_SESSION[$cCode]['main'] = array();
        }
        if (!isset($_SESSION[$cCode]['items'])) {
            $_SESSION[$cCode]['items'] = array();
        }


        //==dapatkan srcJenis
        $stepCode = $this->configUi[$targetJenis]['steps'][1]['target'];
        $steps = $this->configUi[$targetJenis]['steps'];
        $tagihanSrc = isset($this->configUi[$targetJenis]['tagihanSrc']) ? $this->configUi[$targetJenis]['tagihanSrc'] : "sisa";
        $paymentSources = null != ($this->config->item("payment_source")) ? $this->config->item("payment_source") : array();
        $returnRoutes = null != ($this->config->item("transaksi_returnRoutes")) ? $this->config->item("transaksi_returnRoutes") : array();
        $readerDueDate = isset($this->configUi[$targetJenis]['dueDateReader']) ? $this->configUi[$targetJenis]['dueDateReader'] : false;
        $rawPrevURL = isset($_GET['rawPrev']) ? $_GET['rawPrev'] : "";
        $prePrint = isset($steps[1]['prePrint']) ? $steps[1]['prePrint'] : "";

        $ppnDisabledConfig = isset($this->configUi[$targetJenis]['ppnDisabled']) ? $this->configUi[$targetJenis]['ppnDisabled'] : array();

        $jenisSrc = null;

        //		arrprint($paymentSources);die();
        if (sizeof($paymentSources) > 0) {
            foreach ($paymentSources as $src => $sSpec) {
                $payConfigs = $paymentSources[$src];
                if (sizeof($payConfigs) > 0) {
                    //                    $sCtr = 0;
                    foreach ($payConfigs as $paymentSrcConfigTmp) {
                        $sCtr = 0;
                        foreach ($paymentSrcConfigTmp as $paymentSrcConfig) {
                            if (isset($paymentSrcConfig['jenisTarget']) && $paymentSrcConfig['jenisTarget'] == $targetJenis) {
                                $srcJenis = $paymentSrcConfig['jenisSrc'];
                                $rawSrcJenis = $src;
                                $srcIndex = $sCtr;
                                $jenisSrc = $srcJenis;
                            }
                            $sCtr++;
                        }

                    }
                }
            }
        }

        $references = array();
        if ($jenisSrc != null) {
            if (isset($returnRoutes[$jenisSrc])) {
                $retCode = $returnRoutes[$jenisSrc];


                $trr = new MdlTransaksi();
                $trr->setFilters(array());
//                $trr->addFilter("param='main'");
                $trr->addFilter("jenis='$retCode'");
//                $tmpR = $trr->lookupRegistries_joined()->result();
                $tmpR = $trr->lookupDataRegistries_joined();
                //cekHere($this->db->last_query());

                if (sizeof($tmpR) > 0) {
                    foreach ($tmpR as $row) {
//                        $main = blobDecode($row->values);
                        $main = blobDecode($row->main);
                        //                        arrprint($main);
                        $references[$main['referenceID']] = array(
                            "id" => $row->transaksi_id,
                            "nomer" => $row->nomer,
                            "refID" => $main['referenceID'],
                            "refJenis" => $main['referenceJenis'],
                            "refNum" => $main['referenceNomer'],
                            "harga" => isset($main['harga']) ? $main['harga'] : 0,
                            "nett" => isset($main['nett']) ? $main['nett'] : 0,


                        );
                    }
                }
            }
        }

        //        arrprint($references);
        $elementConfigs = isset($this->configUi[$this->jenisTr]['receiptElements']) ? $this->configUi[$this->jenisTr]['receiptElements'] : array();


        //==dapatkan daftar kolom dari srcJenis
        //        $historyFields = $this->configUi[$srcJenis]['shortHistoryFields'];

        $tr = new MdlTransaksi();
        $tr->setFilters(array());
        $tr->addFilter("transaksi_payment_source.cabang_id='" . $this->session->login['cabang_id'] . "'");
        $tr->addFilter("extern_id='$externID'");
        $tr->addFilter("sisa>0");

        if ($selectedTrID > 0) {
            $tr->addFilter("transaksi_id='$selectedTrID'");
        }
        //        $tmpSrc = $tr->lookupPaymentSrcByJenis_joined($targetJenis)->result();
        $tmpSrc = $tr->lookupPaymentSrcByJenis($targetJenis)->result();
        // cekKuning(__LINE__. " | " .$this->db->last_query());
        //        arrPrint($tmpSrc);
        if ($readerDueDate) {
            $tr->setFilters(array());
            $tmpSrcDue = $tr->lookupAllDueDate()->result();
            //        arrPrint($tmpSrcDue);
            if (sizeof($tmpSrcDue) > 0) {
                foreach ($tmpSrcDue as $tmpSrccDue_0) {
                    $mainDueDate[$tmpSrccDue_0->transaksi_id] = $tmpSrccDue_0->due_date;
                }
            }
        }


        $externName = "";
        $items = array();
        $tempDueDate = array();
        $ppnDisabled = array();
        if (sizeof($tmpSrc) > 0) {
            $arrTrIDs = array();
            foreach ($tmpSrc as $row) {
                $arrTrIDs[] = $row->transaksi_id;
            }
            $kolomRegistry = isset($this->configUiJenis['pairRegistriesFields']) ? $this->configUiJenis['pairRegistriesFields'] : array();
            $trr = new MdlTransaksi();
            $trr->setFilters(array());
            $trr->addFilter("transaksi_id in ('" . implode("','", $arrTrIDs) . "')");
            $tmpR = $trr->lookupDataRegistries()->result();

            // saat ini hanya diambil [main] ..............
            $regDataPair = array();
            if (sizeof($tmpR) > 0) {
                foreach ($tmpR as $rSpec) {
                    foreach ($rSpec as $regKey => $valKey) {
                        if ($regKey == "main") {
                            $data = blobDecode($valKey);
                            $regData = array();
                            if (sizeof($kolomRegistry) > 0) {
                                foreach ($kolomRegistry as $kolom) {
                                    $regData[$kolom] = $data[$kolom];
                                }
                            }
                            $regDataPair[$rSpec->transaksi_id] = $regData;
                        }
                    }
                }
            }

            foreach ($tmpSrc as $row) {
                if (isset($regDataPair[$row->transaksi_id]) && sizeof($regDataPair[$row->transaksi_id]) > 0) {
                    foreach ($regDataPair[$row->transaksi_id] as $key => $val) {
                        $row->$key = $val;
                    }
                }
                $tmp = array();
                //                foreach($historyFields as $fName=>$label){
                //                    $tmp[$fName]=$row->$fName;
                //                }
                //                arrPrintPink($row);
                $jenis = $row->jenis;
                $transaksi_id = $row->id;
                if ($readerDueDate) {
                    $tempDueDate = blobDecode($row->ids_prev);
                    $dueTime = array();
                    if (sizeof($tempDueDate) > 0) {
                        $time_due = array();
                        foreach ($tempDueDate as $k => $spdID) {
                            $time_due[] = strtotime($mainDueDate[$spdID]);
                            $dueTime[strtotime($mainDueDate[$spdID])] = $mainDueDate[$spdID];
                        }
                        asort($time_due);
                        $key_due = $time_due[0];
                    }
                    if ($key_due > 0) {
                        $date_now = strtotime(date("Y-m-d"));
                        $dateValid = strtotime($dueTime[$key_due]);
                        //                        cekHijau("$dateValid >$date_now");
                        if ($dateValid < $date_now) {
                            //                            cekHere("$dateValid >$date_now");
                            $tmp['class_bg'] = "bg-danger";
                        }
                        else {
                            $tmp['class_bg'] = "";
                        }
                    }
                    else {
                        $tmp['class_bg'] = "";
                    }

                    $tmp['due_date'] = isset($dueTime[$key_due]) ? formatField_he_format("dtime", $dueTime[$key_due]) : "-";
                    $tmp['aging'] = isset($dueTime[$key_due]) && umurDay($dueTime[$key_due]) > 0 ? umurDay($dueTime[$key_due]) : "0";
                }
                else {
                    $tmp['class_bg'] = "";
                }


                $tmp['refID'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['refID'] : "";
                $tmp['refNum'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['refNum'] : "";
                $tmp['refValue'] = array_key_exists($row->transaksi_id, $references) ? $references[$row->transaksi_id]['nett'] : "";

                if (strlen($prePrint) > 10) {
                    $linkPrint = base_url() . "$prePrint/$jenis/$transaksi_id";
                    $tmp['prePrint'] = "<button type='button' class='btn btn-link' onclick=\"window.open('$linkPrint')\"><i class='fa fa-print'></i></button>";
                }

                if (isset($ppnDisabledConfig['enabled']) && $ppnDisabledConfig['enabled'] == true) {
                    //                    if (($row->ppn_sisa > 0) && ($row->ppn_status == 1)) {
                    if (($row->ppn_status == 1)) {
                        $ppnDisabled[] = $row->transaksi_id;
                        $tmp['notes'] = isset($ppnDisabledConfig['notes']) ? "<span style='color:red;font-size:10px;'>" . $ppnDisabledConfig['notes'] . "</span>" : "-";
                    }
                }

                $items[] = (array)$row + $tmp;
                $externName = $row->extern_nama;
            }
        }
        //arrPrint($items);
        $cCode = "_TR_" . $targetJenis;
        //arrPrint($ppnDisabled);
        $defaultItemLabels = array(
            "extern_label2" => "jenis",
            "nomer" => "receipt number",
            "nomer_top" => "receipt ref.",
            "refNum" => "return ref.",
            "fulldate" => "date",
            "tagihan" => "due amount",
            "refValue" => "returned",
            "terbayar" => "paid",
            "diskon" => "discount",
            "sisa" => "due remain",
        );

        $jQueryCustom = isset($this->configUi[$this->jenisTr]['jQueryCustom']) ? $this->configUi[$this->jenisTr]['jQueryCustom'] : "";

        if (sizeof($jQueryCustom) > 0) {
            $scriptBottom .= $jQueryCustom;
        }


        $defSisa = isset($_SESSION[$cCode]['main']['sisa']) ? $_SESSION[$cCode]['main']['sisa'] : 0;
        $data = array(
            "mode" => $this->uri->segment(3),
            "title" => $this->configUi[$targetJenis]["label"],
            "subTitle" => $externName,
            "jenisTr" => $targetJenis,
            "items" => $items,
            "prePrint" => $prePrint,
            "itemLabels" => isset($this->configUi[$targetJenis]["shoppingCartReferenceFields"]) ? $this->configUi[$targetJenis]["shoppingCartReferenceFields"] : $defaultItemLabels,
            "selectProcessor" => $this->configUi[$targetJenis]["selectorProcessor"],
            "paymentSubtitle" => "details for " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
            "btnLabel" => "continue " . $this->configUi[$this->jenisTr]['steps'][1]['label'],
            "ses_outMaster" => isset($_SESSION[$cCode]['main']) ? $_SESSION[$cCode]['main'] : array(),
            "ses_items" => isset($_SESSION[$cCode]['items']) ? $_SESSION[$cCode]['items'] : array(),
            // -----------
            "actionTarget" => "top.$('#result').load('" . MODUL_PATH . "Transaksi/validate/" . $targetJenis . "?rawPrev=$rawPrevURL');",
            "columnRecorderTarget" => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/nilai_entry",
            "bankColumnRecorderTarget" => base_url() . "ValueGate/recordColumn/" . $this->jenisTr . "/paymentMethod_cash",
            // -------------------
            "selectedBankID" => isset($_SESSION[$cCode]['main']['cash_account']) ? $_SESSION[$cCode]['main']['cash_account'] : 0,
            "dueDateReader" => $readerDueDate,
            "tagihanSrc" => $tagihanSrc,
            "scriptBottom" => $scriptBottom,
            "tagihanValue" => isset($_SESSION[$cCode]['main'][$tagihanSrc]) ? $_SESSION[$cCode]['main'][$tagihanSrc] : $defSisa,
            "isPaymentRadioSelect" => isset($this->configUi[$this->jenisTr]['isPaymentRadioSelect']) ? $this->configUi[$this->jenisTr]['isPaymentRadioSelect'] : false,
            "ppnDisabled" => $ppnDisabled,
            "scriptTrigger" => $scriptTrigger,
            "summary" => array("tagihan", "terbayar", "sisa", "diskon", "returned"),
        );

        if (!isset($_GET['json'])) {
            $this->load->view("transaksi", $data);
        }
        else {
            return json_encode($data);
        }

        //        die("selecting payment src...");
    }

}
