<?php

/**
 * Created by PhpStorm.
 * User: aziz
 * Date: 9/26/2018
 * Time: 5:01 PM
 */
require_once "Modul_Controller.php";

class _processPihak extends Modul_Controller
{

    // private $jenisTr;

    public function __construct()
    {
        parent::__construct();
        // $this->jenisTr = $this->uri->segment(4);
        // $cCode = "_TR_" . $this->jenisTr;
    }

    public function select()
    {

        $pihakMainValueSrc = isset($this->configUi[$this->jenisTr]['pihakMainValueSrc']) ? $this->configUi[$this->jenisTr]['pihakMainValueSrc'] : array();
        $pihakValidate = isset($this->configUi[$this->jenisTr]['pihakValidate']) ? $this->configUi[$this->jenisTr]['pihakValidate'] : array();
        $pihakAddValidate = isset($this->configUi[$this->jenisTr]['pihakAddValidate']) ? $this->configUi[$this->jenisTr]['pihakAddValidate'] : array();

        $cCode = $this->cCode;
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $mdlName = $this->uri->segment(5);

        $this->load->model("Mdls/" . $mdlName);
        $b = new $mdlName();
        $tmpB = $b->lookupByID($id)->result();

//        matiHere( $this->db->last_query() );

        //---------------------------------------------------
        if (sizeof($pihakValidate) > 0) {

            foreach ($pihakValidate as $kolom => $spec) {
                if (isset($tmpB[0]->$kolom) && ($tmpB[0]->$kolom != NULL)) {
                    $result = $tmpB[0]->$kolom;
                    $tb_kolom = $spec['result'][$result]['kolom'];
                    $tb_label = $spec['result'][$result]['label'];
                    if (isset($tmpB[0]->$tb_kolom) && ($tmpB[0]->$tb_kolom != NULL)) {
                        cekHijau("LANJUT...");
                    }
                    else {
                        $label = $tmpB[0]->nama . ", " . $tb_label;
                        die(lgShowAlertBiru($label));
                    }
                }
                else {
                    $label = $tmpB[0]->nama . ", " . $spec['result']['none']['label'];
                    die(lgShowAlertBiru($label));
                }
            }
        }

        if (sizeof($pihakAddValidate) > 0) {
            $addMode = isset($pihakAddValidate['mode']) ? $pihakAddValidate['mode'] : NULL;
            $addFilter = isset($pihakAddValidate['filter']) ? $pihakAddValidate['filter'] : array();
            if (sizeof($addFilter) > 0) {
                foreach ($addFilter as $kf => $vf) {

                    cekHere(":: $kf => $vf :: $addMode ::");
                    switch ($addMode) {
                        case "!=":
                            if ($tmpB[0]->$kf != $vf) {
                                $label = $pihakAddValidate['label'][$kf];
                                die(lgShowAlertBiru($label));
                            }
                            break;
                        case "==":
                            if ($tmpB[0]->$kf == $vf) {
                                $label = $pihakAddValidate['label'][$kf];
                                die(lgShowAlertBiru($label));
                            }
                            break;
                        default:
                            cekHitam(":: masuk sini, default ::");
                            break;
                    }

                }
            }
        }
        //--------------------------------------------------

        if (isset($this->configUi[$this->jenisTr]["pihakMainNota"]) && $this->configUi[$this->jenisTr]["pihakMainNota"] == true) {
            $selectColumn = "nomer";
        }
        else {
            $selectColumn = "nama";
        }

        // region resetor session delivery dan billing detail
        $gateReset = array("main", "tableIn_master_values");
        $resetor = array(
            "vendorDetails",
            "billingDetails",
            "deliveryDetails",
        );
        foreach ($gateReset as $gate) {
            if (isset($_SESSION[$cCode][$gate])) {
                foreach ($_SESSION[$cCode][$gate] as $keys => $values) {
                    $keysTmp = explode("__", $keys);
                    // buang yang sama dulu
                    if (in_array($keys, $resetor)) {
                        unset($_SESSION[$cCode][$gate][$keys]);
                    }
                    // buang yang mengandung __
                    if (in_array($keysTmp[0], $resetor)) {
                        unset($_SESSION[$cCode][$gate][$keys]);
                    }
                }
            }
        }
        if (isset($_SESSION[$cCode]['main_elements'])) {
            foreach ($resetor as $resetValue) {
                if (array_key_exists($resetValue, $_SESSION[$cCode]['main_elements'])) {
                    unset($_SESSION[$cCode]['main_elements'][$resetValue]);
                }
            }
        }
        // endregion

        if (sizeof($tmpB) > 0) {
            $phakSpeks = $tmpB[0];
            arrPrintPink($phakSpeks);
            $pihakLevel = isset($phakSpeks->level_id) ? $phakSpeks->level_id : 0;
            if ($pihakLevel > 0) {
                /* ---------------------------------------------------------------------------------
                 * -----------------------------BACA SETTINGAN DISKON YG BERLAKU-----------------------------
                 * ---------------------------------------------------------------------------------*/
                // $this->load->library("Diskon");
                // $dk = new Diskon();
                // $dk->setTokoId(my_toko_id());
                // $dsc_param_src = $dk->callCustomerLevelDiskon($pihakLevel);
                // $dsc_params = $dsc_param_src["customer_level_diskon"];
                // // arrPrint($dsc_params);
                // $trans_nilai = 50000;
                //
                // $macem_diskon = array();
                // foreach ($dsc_params as $item) {
                //     // arrPrintPink($item);
                //     $dk_jenis = $item['jenis'];
                //
                //     $macem_diskon[$dk_jenis][] = $item;
                // }
                // // arrPrintKuning($macem_diskon);
                // $diskon_events = array();
                // foreach ($macem_diskon as $diskon_nama => $item_diskon) {
                //     $jml_even_diskon = sizeof($item_diskon);
                //     $evnt_count = 0;
                //     foreach ($item_diskon as $item) {
                //         $evnt_count++;
                //         $gro_minim = $item['minim'];
                //         $gro_maxim = $evnt_count == $jml_even_diskon ? INF : $item['maxim'];
                //         $gro_persen = $item['persen'];
                //         cekPink2("$trans_nilai >= $gro_minim) && ($trans_nilai <= $gro_maxim)");
                //
                //         if (($trans_nilai >= $gro_minim) && ($trans_nilai <= $gro_maxim)) {
                //             $d_pokok = $gro_persen;
                //             cekBiru("---- $d_pokok");
                //             $diskon_events[$diskon_nama] = $d_pokok;
                //             break;
                //         }
                //
                //     }
                // }
                // arrPrintWebs($diskon_events);
                /* --------------------------------------------
                 * script diatas ini dipakai di shoping cart
                 * ----------------------------------------------*/
            }

            $_SESSION[$cCode]['main']['pihakLevel'] = $pihakLevel;
            $_SESSION[$cCode]['main']['pihakLevelNama'] = isset($phakSpeks->level_nama) ? $phakSpeks->level_nama : '';
            $_SESSION[$cCode]['main']['pihakDisc'] = isset($tmpB[0]->diskon) ? $tmpB[0]->diskon : "";
            $_SESSION[$cCode]['main']['pihakID'] = $id;
            $_SESSION[$cCode]['main']['pihakName'] = isset($tmpB[0]->$selectColumn) ? $tmpB[0]->$selectColumn : "";
            $_SESSION[$cCode]['main']['pihakName2'] = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
			
			
			
			//total point customer
            $this->db->select('id, produk_id, qty_kredit, kredit');
            $this->db->where(array('periode'=>'forever', 'cabang_id' => -1));
            $tmpCustPoint = $this->db->get('_rek_pembantu_customer_cache');
            $custPoint = $tmpCustPoint->result();
            $arrCustPoint = array();
            foreach($custPoint as $k => $pp){
                $arrCustPoint[$pp->produk_id] = $pp->qty_kredit;
            }

            $_SESSION[$cCode]['main']['pihakPoint'] = isset($arrCustPoint[$id]) ? $arrCustPoint[$id] : "";
			

            $tmpPihakName = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
            if (isset($tmpB[0]->name)) {
                $tmpPihakName = $tmpB[0]->name;
            }
            $tmpPihakPoint = "";
            if( isset($tmpB[0]->id) && $tmpB[0]->id*1>1 ) {
                if(isset($arrCustPoint[$id])){
                    $tmpPihakPoint = " ( " . number_format($arrCustPoint[$id]) . " pts )";
                }
                else{
                    $tmpPihakPoint = " ( 0 pts )";
                }
            }

            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = isset($tmpB[0]->$src) ? $tmpB[0]->$src : 0;
                }
            }

            echo "<script>";
            echo "top.document.getElementById('pihakName').value='" . $tmpPihakName . " " . $tmpPihakPoint . "';";
            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            echo "</script>";

        }
        else {
            $warehouse = getDefaultWarehouseID($this->session->login['cabang_id']);
            $_SESSION[$cCode]['main']['pihakID'] = $id;
            $_SESSION[$cCode]['main']['pihakName'] = $warehouse['gudang_nama'];
            $_SESSION[$cCode]['main']['pihakName2'] = "";
            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                }
            }
        }

        $this->load->library("ValueGate");
        $vg = new ValueGate();
        $vg->setSessionLogin($_SESSION['login']);
        $vg->setJenisTr($this->jenisTr);
        $vg->setConfigUiJenis($this->configUiJenis);
        $vg->setConfigCoreJenis($this->configCoreJenis);
        $vg->setConfigValuesJenis($this->configValuesJenis);
        $vg->setPpnFactor( my_ppn_factor() );
        $initMasterValues = $vg->initMasterValue();
            // $initMasterValues = array(
            //     "olehID"        => my_id(),
            //     "olehName"      => 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"    => isset($stepNum) ? $stepNum : ($stepNum = 1),
            //     "stepCode"      => isset($this->configUiJenis['steps'][1]['target']) ? $this->configUiJenis['steps'][1]['target'] : 0,
            //     "dtime"         => dtimeNow(),
            //     "fulldate"      => dtimeNow("Y-m-d"),
            // );

//        for($ii=0;$ii<2;$ii++){
            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);
//        }


//        cekHitam("reloaded");
        // matiHere();
        /* --------------------------------------------------
             * ngereload shoping cart dlm modul
             * --------------------------------------------------*/
       echo "<script>";
       echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
       echo "</script>";
    }

    public function select2()
    {

        $pihakMainValueSrc = isset($this->configUi[$this->jenisTr]['pihakMainValueSrc']) ? $this->configUi[$this->jenisTr]['pihakMainValueSrc'] : array();
        $accountChilds = $this->config->item("accountChilds") != null ? $this->config->item("accountChilds") : array();

        $cCode = "_TR_" . $this->jenisTr;
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $mdlName = $this->uri->segment(5);

        $this->load->model("Mdls/" . $mdlName);
        $b = new $mdlName();
        $tmpB = $b->lookupByID($id)->result();

        if (isset($this->configUi[$this->jenisTr]["pihakMainNota"]) && $this->configUi[$this->jenisTr]["pihakMainNota"] == true) {
            $selectColumn = "nomer";
        }
        else {
            $selectColumn = "nama";
        }

        if (sizeof($tmpB) > 0) {
            $_SESSION[$cCode]['main']['pihak2ID'] = $id;
            $_SESSION[$cCode]['main']['pihak2Name'] = isset($tmpB[0]->$selectColumn) ? $tmpB[0]->$selectColumn : "";
            $_SESSION[$cCode]['main']['pihak2Name2'] = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
            $_SESSION[$cCode]['main']['pihak2Disc'] = isset($tmpB[0]->diskon) ? $tmpB[0]->diskon : "";
            $_SESSION[$cCode]['main']['pihak2Mdl'] = isset($tmpB[0]->mdl_name) ? $tmpB[0]->mdl_name : "";
            $_SESSION[$cCode]['main']['pihak2Com'] = isset($accountChilds[$tmpB[0]->nama]) ? $accountChilds[$tmpB[0]->nama] : "";

            if (isset($tmpB[0]->exchange)) {
                $_SESSION[$cCode]['main']['pihak2Exchange'] = $tmpB[0]->exchange;
            }


            $resetorPihak3 = array(
                "pihak3ID",
                "pihak3Name",
                "pihak3Name3",
                "pihak3Disc",
                "pihak3Mdl",
                "pihak3Com",
            );
            foreach ($resetorPihak3 as $isi) {
                if (isset($_SESSION[$cCode]['main'][$isi])) {
                    $_SESSION[$cCode]['main'][$isi] = null;
                    unset($_SESSION[$cCode]['main'][$isi]);
                }
                if (isset($_SESSION[$cCode]['items'])) {
                    foreach ($_SESSION[$cCode]['items'] as $pid => $iSpec) {
                        if (isset($iSpec[$isi])) {
                            $iSpec[$isi] = null;
                            unset($iSpec[$isi]);
                        }
                        $_SESSION[$cCode]['items'][$pid] = $iSpec;
                    }
                }
            }


            $tmpPihakName = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
            if (isset($tmpB[0]->name)) {
                $tmpPihakName = $tmpB[0]->name;
            }
            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                }
            }

            $this->load->library("ValueGate");
            $vg = new ValueGate();
            $vg->setSessionLogin($_SESSION['login']);
            $vg->setJenisTr($this->jenisTr);
            $vg->setConfigUiJenis($this->configUiJenis);
            $vg->setConfigCoreJenis($this->configCoreJenis);
            $vg->setConfigValuesJenis($this->configValuesJenis);
            $initMasterValues = $vg->initMasterValue();
            // $initMasterValues = array(
            //     "olehID"        => my_id(),
            //     "olehName"      => 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"    => isset($stepNum) ? $stepNum : ($stepNum = 0),
            //     "stepCode"      => isset($this->configUiJenis['steps'][$stepNum]['target']) ? $this->configUiJenis['steps'][$stepNum]['target'] : 0,
            //     "dtime"         => dtimeNow(),
            //     "fulldate"      => dtimeNow("Y-m-d"),
            //     // "jenis_pajak"=>$this->session->login['jenis_usaha'],
            // );
            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);

            echo "<script>";
            echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
            echo "top.document.getElementById('pihak2Name').value='" . $tmpPihakName . "';";
            echo "top.document.getElementById('pilihan_outlet2').innerHTML='';";
            echo "</script>";
        }
        else {
            $_SESSION[$cCode]['main']['pihakID'] = $id;
            $_SESSION[$cCode]['main']['pihakName'] = "default warehouse";
            $_SESSION[$cCode]['main']['pihakName2'] = "";
            //            $_SESSION[$cCode]['out_master']['pihakID'] = $id;
            //            $_SESSION[$cCode]['out_master']['pihakName'] = "default warehouse";
            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                    //                    $_SESSION[$cCode]['out_master'][$key] = $tmpB[0]->$src;
                }
            }
            echo "<script>";
            echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?ohYes=ohNo');";
            echo "top.document.getElementById('pihakName').value='" . $_SESSION[$cCode]['main']['pihakName2'] . "';";
            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            echo "</script>";
        }
    }

    public function select3()
    {

        $pihakMainValueSrc = isset($this->configUi[$this->jenisTr]['pihakMainValueSrc']) ? $this->configUi[$this->jenisTr]['pihakMainValueSrc'] : array();
        $accountChilds = $this->config->item("accountChilds") != null ? $this->config->item("accountChilds") : array();

        $cCode = "_TR_" . $this->jenisTr;
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        //        $mdlName = $this->uri->segment(5);
        if (isset($_SESSION[$cCode]['main']['pihak2Mdl'])) {
            $mdlName = $_SESSION[$cCode]['main']['pihak2Mdl'];
        }
        else {
            $mdlName = $this->uri->segment(5);
        }

        $this->load->model("Mdls/" . $mdlName);
        $b = new $mdlName();
        $tmpB = $b->lookupByID($id)->result();

        if (isset($this->configUi[$this->jenisTr]["pihakMainNota"]) && $this->configUi[$this->jenisTr]["pihakMainNota"] == true) {
            $selectColumn = "nomer";
        }
        else {
            $selectColumn = "nama";
        }

        if (sizeof($tmpB) > 0) {
            $_SESSION[$cCode]['main']['pihak3ID'] = $id;
            $_SESSION[$cCode]['main']['pihak3Name'] = isset($tmpB[0]->$selectColumn) ? $tmpB[0]->$selectColumn : "";
            $_SESSION[$cCode]['main']['pihak3Name3'] = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
            $_SESSION[$cCode]['main']['pihak3Disc'] = isset($tmpB[0]->diskon) ? $tmpB[0]->diskon : "";
            $_SESSION[$cCode]['main']['pihak3Mdl'] = isset($tmpB[0]->mdl_name) ? $tmpB[0]->mdl_name : "";
            $_SESSION[$cCode]['main']['pihak3Com'] = isset($accountChilds[$tmpB[0]->nama]) ? $accountChilds[$tmpB[0]->nama] : "";


            $tmpPihakName = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";

            if (isset($tmpB[0]->name)) {
                $tmpPihakName = $tmpB[0]->name;
            }


            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                }
            }

            $this->load->library("ValueGate");
            $vg = new ValueGate();
            $vg->setSessionLogin($_SESSION['login']);
            $vg->setJenisTr($this->jenisTr);
            $vg->setConfigUiJenis($this->configUiJenis);
            $vg->setConfigCoreJenis($this->configCoreJenis);
            $vg->setConfigValuesJenis($this->configValuesJenis);
            $initMasterValues = $vg->initMasterValue();
            // $initMasterValues = array(
            //     "olehID"        => my_id(),
            //     "olehName"      => 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"    => isset($stepNum) ? $stepNum : ($stepNum = 0),
            //     "stepCode"      => isset($this->configUiJenis['steps'][$stepNum]['target']) ? $this->configUiJenis['steps'][$stepNum]['target'] : 0,
            //     "dtime"         => dtimeNow(),
            //     "fulldate"      => dtimeNow("Y-m-d"),
            //     // "jenis_pajak"=>$this->session->login['jenis_usaha'],
            // );
            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);

            echo "<script>";
            echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
            echo "top.document.getElementById('pihak3Name').value='" . $tmpPihakName . "';";
            echo "top.document.getElementById('pilihan_outlet3').innerHTML='';";
            echo "</script>";
        }
        else {
            $_SESSION[$cCode]['main']['pihakID'] = $id;
            $_SESSION[$cCode]['main']['pihakName'] = "default warehouse";
            $_SESSION[$cCode]['main']['pihakName2'] = "";
            //            $_SESSION[$cCode]['out_master']['pihakID'] = $id;
            //            $_SESSION[$cCode]['out_master']['pihakName'] = "default warehouse";
            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                    //                    $_SESSION[$cCode]['out_master'][$key] = $tmpB[0]->$src;
                }
            }
            echo "<script>";
            echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?ohYes=ohNo');";
            echo "top.document.getElementById('pihakName').value='" . $_SESSION[$cCode]['main']['pihakName2'] . "';";
            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            echo "</script>";
        }
    }

    public function selectExtern()
    {

        $cCode = "_TR_" . $this->jenisTr;
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $stepNum = isset($stepNum) ? $stepNum : 1;
        if (isset($_SESSION[$cCode]['main']['pihak2Mdl'])) {
            $mdlName = $_SESSION[$cCode]['main']['pihak2Mdl'];
        }
        else {
            $mdlName = $this->uri->segment(5);
        }

        $this->load->model("Mdls/" . $mdlName);
        $b = new $mdlName();
        $tmpB = $b->lookupByID($id)->result();

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

        if (isset($this->configUi[$this->jenisTr]["pihakMainNota"]) && $this->configUi[$this->jenisTr]["pihakMainNota"] == true) {
            $selectColumn = "nomer";
        }
        else {
            $selectColumn = "nama";
        }

        if (sizeof($tmpB) > 0) {

            $_SESSION[$cCode]['main']['pihakExternID'] = $id;
            $_SESSION[$cCode]['main']['pihakExternMasterID'] = isset($tmpB[0]->id_master) ? $tmpB[0]->id_master : "";
            $_SESSION[$cCode]['main']['pihakExternName'] = isset($tmpB[0]->$selectColumn) ? $tmpB[0]->$selectColumn : "";
            $_SESSION[$cCode]['main']['pihakExternValueSrc'] = isset($tmpB[0]->value_src) ? $tmpB[0]->value_src : "";
            $_SESSION[$cCode]['main']['pihakExternRevertStep'] = isset($tmpB[0]->revertStep) ? $tmpB[0]->revertStep : false;
            $_SESSION[$cCode]['main']['pihakExternDetailGate'] = isset($tmpB[0]->detailGate) ? $tmpB[0]->detailGate : "items";

            $tmpPihakName = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";

            if (isset($tmpB[0]->name)) {
                $tmpPihakName = $tmpB[0]->name;
            }

            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                }
            }

            $this->load->library("ValueGate");
            $vg = new ValueGate();
            $vg->setSessionLogin($_SESSION['login']);
            $vg->setJenisTr($this->jenisTr);
            $vg->setConfigUiJenis($this->configUiJenis);
            $vg->setConfigCoreJenis($this->configCoreJenis);
            $vg->setConfigValuesJenis($this->configValuesJenis);
            $vg->setPpnFactor(my_ppn_factor());
            $initMasterValues = $vg->initMasterValue();
            // $initMasterValues = array(
            //     "olehID"        => my_id(),
            //     "olehName"      => 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"    => isset($stepNum) ? $stepNum : ($stepNum = 0),
            //     "stepCode"      => isset($this->configUiJenis['steps'][$stepNum]['target']) ? $this->configUiJenis['steps'][$stepNum]['target'] : 0,
            //     "dtime"         => dtimeNow(),
            //     "fulldate"      => dtimeNow("Y-m-d"),
            //     // "jenis_pajak"=>$this->session->login['jenis_usaha'],
            // );
            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);

            echo "<script>";
            echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
            echo "top.document.getElementById('pihakExternName').value='" . $tmpPihakName . "';";
            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            echo "</script>";
        }
        else {
            //            $_SESSION[$cCode]['main']['pihakID'] = $id;
            //            $_SESSION[$cCode]['main']['pihakName'] = "default warehouse";
            //            $_SESSION[$cCode]['main']['pihakName2'] = "";
            ////            $_SESSION[$cCode]['out_master']['pihakID'] = $id;
            ////            $_SESSION[$cCode]['out_master']['pihakName'] = "default warehouse";
            //            if (sizeof($pihakMainValueSrc) > 0) {
            //                foreach ($pihakMainValueSrc as $key => $src) {
            //                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
            ////                    $_SESSION[$cCode]['out_master'][$key] = $tmpB[0]->$src;
            //                }
            //            }
            //            echo "<script>";
            //            echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?ohYes=ohNo');";
            //            echo "top.document.getElementById('pihakName').value='" . $_SESSION[$cCode]['main']['pihakName2'] . "';";
            //            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            //            echo "</script>";
        }
    }

    public function remove()
    {
        $cCode = "_TR_" . $this->jenisTr;

        $_SESSION[$cCode]['main']['pihakID'] = null;
        $_SESSION[$cCode]['main']['pihakName'] = null;
        $_SESSION[$cCode]['main']['pihakName2'] = null;
        unset($_SESSION[$cCode]['main']['pihakID']);
        unset($_SESSION[$cCode]['main']['pihakName']);
        unset($_SESSION[$cCode]['main']['pihakName2']);

        $_SESSION[$cCode]['main']['pihak2ID'] = null;
        $_SESSION[$cCode]['main']['pihak2Name'] = null;
        $_SESSION[$cCode]['main']['pihak2Name2'] = null;
        unset($_SESSION[$cCode]['main']['pihak2ID']);
        unset($_SESSION[$cCode]['main']['pihak2Name']);
        unset($_SESSION[$cCode]['main']['pihak2Name2']);

        echo "<script>";
        echo "top.localStorage.member = 0;";
        echo "top.selectNonMember();";
        echo "top.$('#pihakName').attr('disable', true);";
        echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=');";
        echo "</script>";

    }

    //---------------------------
    public function select3UM()
    {

        $pihakMainValueSrc = isset($this->configUi[$this->jenisTr]['pihakMainValueSrc']) ? $this->configUi[$this->jenisTr]['pihakMainValueSrc'] : array();
        $accountChilds = $this->config->item("accountChilds") != null ? $this->config->item("accountChilds") : array();

        $cCode = "_TR_" . $this->jenisTr;
        $id = isset($_GET['id']) ? $_GET['id'] : 0;
        $mdlName = $this->uri->segment(5);
        //        if (isset($_SESSION[$cCode]['main']['pihak3Mdl'])) {
        //            $mdlName = $_SESSION[$cCode]['main']['pihak3Mdl'];
        //        }
        //        else {
        //            $mdlName = $this->uri->segment(5);
        //        }

        $this->load->model("Mdls/" . $mdlName);
        $b = new $mdlName();
        $tmpB = $b->lookupByID($id)->result();

        if (isset($this->configUi[$this->jenisTr]["pihakMainNota"]) && $this->configUi[$this->jenisTr]["pihakMainNota"] == true) {
            $selectColumn = "nomer";
        }
        else {
            $selectColumn = "nama";
        }

        $dummyElementResetor = isset($this->configUi[$this->jenisTr]["dummyElementResetor"]) ? $this->configUi[$this->jenisTr]["dummyElementResetor"] : array();
        if (sizeof($dummyElementResetor) > 0) {
            foreach ($dummyElementResetor as $rVal) {
                $_SESSION[$cCode]['main'][$rVal] = NULL;
                unset($_SESSION[$cCode]['main'][$rVal]);
            }
        }


        if (sizeof($tmpB) > 0) {
            $_SESSION[$cCode]['main']['pihak3ID'] = $id;
            $_SESSION[$cCode]['main']['pihak3Name'] = isset($tmpB[0]->$selectColumn) ? $tmpB[0]->$selectColumn : "";
            $_SESSION[$cCode]['main']['pihak3Name3'] = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";
            $_SESSION[$cCode]['main']['pihak3Disc'] = isset($tmpB[0]->diskon) ? $tmpB[0]->diskon : "";
            $_SESSION[$cCode]['main']['pihak3Mdl'] = isset($tmpB[0]->mdl_name) ? $tmpB[0]->mdl_name : "";
            $_SESSION[$cCode]['main']['pihak3Com'] = isset($accountChilds[$tmpB[0]->nama]) ? $accountChilds[$tmpB[0]->nama] : "";


            $tmpPihakName = isset($tmpB[0]->$selectColumn) ? formatNota($selectColumn, $tmpB[0]->$selectColumn) : "";

            if (isset($tmpB[0]->name)) {
                $tmpPihakName = $tmpB[0]->name;
            }


            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                }
            }


            $this->load->library("ValueGate");
            $vg = new ValueGate();
            $vg->setSessionLogin($_SESSION['login']);
            $vg->setJenisTr($this->jenisTr);
            $vg->setConfigUiJenis($this->configUiJenis);
            $vg->setConfigCoreJenis($this->configCoreJenis);
            $vg->setConfigValuesJenis($this->configValuesJenis);
            $initMasterValues = $vg->initMasterValue();
            // $initMasterValues = array(
            //     "olehID"        => my_id(),
            //     "olehName"      => 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"    => isset($stepNum) ? $stepNum : ($stepNum = 0),
            //     "stepCode"      => isset($this->configUiJenis['steps'][$stepNum]['target']) ? $this->configUiJenis['steps'][$stepNum]['target'] : 0,
            //     "dtime"         => dtimeNow(),
            //     "fulldate"      => dtimeNow("Y-m-d"),
            //     // "jenis_pajak"=>$this->session->login['jenis_usaha'],
            // );
            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);

            echo "<script>";
            echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
            echo "top.document.getElementById('pihak3Name').value='" . $tmpPihakName . "';";
            echo "top.document.getElementById('pilihan_outlet3').innerHTML='';";
            echo "</script>";
        }
        else {
            $_SESSION[$cCode]['main']['pihakID'] = $id;
            $_SESSION[$cCode]['main']['pihakName'] = "default warehouse";
            $_SESSION[$cCode]['main']['pihakName2'] = "";
            //            $_SESSION[$cCode]['out_master']['pihakID'] = $id;
            //            $_SESSION[$cCode]['out_master']['pihakName'] = "default warehouse";
            if (sizeof($pihakMainValueSrc) > 0) {
                foreach ($pihakMainValueSrc as $key => $src) {
                    $_SESSION[$cCode]['main'][$key] = $tmpB[0]->$src;
                    //                    $_SESSION[$cCode]['out_master'][$key] = $tmpB[0]->$src;
                }
            }
            echo "<script>";
            echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?ohYes=ohNo');";
            echo "top.document.getElementById('pihakName').value='" . $_SESSION[$cCode]['main']['pihakName2'] . "';";
            echo "top.document.getElementById('pilihan_outlet').innerHTML='';";
            echo "</script>";
        }
    }
}