<?php

/**
 * Created by PhpStorm.
 * User: aziz
 * Date: 9/18/2018
 * Time: 8:45 PM
 */
require_once "Modul_Controller.php";

class _processSelectProductStock extends Modul_Controller
{
    // private $jenisTr;

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

        $this->load->model("Mdls/MdlProduk");
        $this->load->model("Mdls/MdlLockerStock");
        $this->load->model("Mdls/MdlHargaProduk");
    }

    public function select()
    {

        $produk_id = $id = $_GET['id'];
        $jml = 0;
        if (isset($_GET['jml'])) {
            $jml = $_GET['jml'];
        }
        if (isset($_GET['minValue'])) {
            $jml = $_GET['minValue'];
        }
        if (isset($_GET['qty_unit'])) {
            $jml = $_GET['qty_unit'];
        }
        if (isset($_GET['newQty'])) {
            $jml = $_GET['newQty'];
        }

        arrPrint($_GET);
        $stepNum = $this->uri->segment(5) > 0 ? $this->uri->segment(5) : 1;
        $this->load->library("FieldCalculator");
        $cal = new FieldCalculator();
        $cCode = $this->cCode;
        // cekHere("$cCode");
        $subAmountConfig = isset($this->configUi[$this->jenisTr]['shoppingCartAmountValue'][1]) ? $this->configUi[$this->jenisTr]['shoppingCartAmountValue'][1] : null;
        $itemNumLabels = isset($this->configUi[$this->jenisTr]['shoppingCartNumFields'][1]) ? $this->configUi[$this->jenisTr]['shoppingCartNumFields'][1] : array();
        $fieldSrcs = isset($this->configUi[$this->jenisTr]['shoppingCartFieldSrc']) ? $this->configUi[$this->jenisTr]['shoppingCartFieldSrc'] : array("nama" => "nama");
        $priceConfig = isset($this->configUi[$this->jenisTr]['selectedPrice']) ? $this->configUi[$this->jenisTr]['selectedPrice'] : array();
        $lockerConfig = isset($this->configUi[$this->jenisTr]['lockerCheck']) ? $this->configUi[$this->jenisTr]['lockerCheck'] : array();
        $lockerWarnOnly = isset($this->configUi[$this->jenisTr]['lockerCheckWarnOnly']) ? $this->configUi[$this->jenisTr]['lockerCheckWarnOnly'] : false;
        $connectedDiscountConfig = isset($this->configUi[$this->jenisTr]['connectedDiscount']) ? $this->configUi[$this->jenisTr]['connectedDiscount'] : array();


        $b = new MdlProduk();
        $b->addFilter("id=" . $id);
        $tmpB = $b->lookupAll($id)->result();

        /* ------------------------------------------------------
         * locker
         * ------------------------------------------------------*/
        $jml_nambah = $jml;
        $locker_datas = $this->selectorLocker($id);
        $currentActiveLocker = $locker_datas["currentActiveLocker"];
        $currentHoldLocker = $locker_datas["currentHoldLocker"];
        $stok = $locker_datas["stok"];
        // cekAlert("stok last $stok");
        /* -----------------------------------------
         * ==jumlahnya berupa entri baru atau edit?
         * ==sudah ada di shopcart atau belum
         * -----------------------------------------*/
        if (!array_key_exists($id, $_SESSION[$cCode]['items'])) {//belum ada
            $mode = "new";
            $jml_sudah_diambil = 0;
            if (isset($_GET['newQty'])) {
                $jml_diperlukan = $_GET['newQty'];
            }
            else {
                if (isset($_GET['jml'])) {
                    $jml_diperlukan = $_GET['jml'];
                }
                else {
                    $jml_diperlukan = $jml;
                }
            }
            $jmlBaru = $jml_diperlukan;
        }
        else {//sudah ada
            // matiHere();
            if (isset($_SESSION[$cCode]['items'][$id]['qty']) && ($_SESSION[$cCode]['items'][$id]['qty'] > $jml)) {
                $_SESSION[$cCode]['main']['oto_diskon'] = 0;
            }

            if (isset($_GET['newQty']) && $lockerWarnOnly == true) {
                if ($_GET['newQty'] < 1) {
                    echo(lgShowAlert("qty must be more than 0!"));
                    echo "
                                <script>
                            top.$('#shopping_cart').load('" . MODUL_PATH . "_shoppingCart/viewCart/" . $this->jenisTr . "?ohyes=ohno');reLoad();
                                </script>
                            ";
                    die();
                }
                $mode = "edit";
                $jml_sudah_diambil = $_SESSION[$cCode]['items'][$produk_id]['jml'];
                $jml_diperlukan = $_GET['newQty'];
                $jml_nambah = $jml_diperlukan - $jml_sudah_diambil;

                if ($jml_nambah > $currentActiveLocker['jml']) {
                    echo(lgShowAlert("stok $id tidak cukup. (perlu $jml_diperlukan, dengan tambahan $jml_nambah, stok saat ini" . $currentActiveLocker['jml'] . ")"));
                    echo "
                                <script>
                                top.$('#shopping_cart').load('" . MODUL_PATH . "_shoppingCart/viewCart/" . $this->jenisTr . "?ohyes=ohno');reLoad();
                                </script>
                            ";

                    die();
                }
                $jmlBaru = $_GET['newQty'];

            }
            else {
                if (isset($_GET['jml'])) {
                    $mode = "add";
                    $jml_sudah_diambil = $_SESSION[$cCode]['items'][$produk_id]['jml'];
                    $jml_diperlukan = $jml_sudah_diambil + $jml;
                    $jml_nambah = $_GET['jml'];
                    $jmlBaru = $jml_diperlukan;
                }
                else {
                    $jml_sudah_diambil = $_SESSION[$cCode]['items'][$produk_id]['jml'];
                    $jml_diperlukan = 0;
                    $jml_nambah = 0;
                    $jmlBaru = $jml_sudah_diambil;
                }
            }

        }

        /* ---------------------------------------------
         * ===update lokernya
         * ---------------------------------------------*/
        if ($jml_nambah != 0) {
            if (isset($lockerConfig['enabled']) && $lockerConfig['enabled'] == true) {

                if ($jml_diperlukan > ($currentActiveLocker['jml'] + $currentHoldLocker['jml'])) {
                    echo(lgShowAlert("stock amount doesnt match. needed: $jml_diperlukan. avail: " . ($currentActiveLocker['jml'] + $currentHoldLocker['jml'])));
                    echo "
                                <script>                                
                                top.$('#shopping_cart').load('" . MODUL_PATH . "_shoppingCart/viewCart/" . $this->jenisTr . "?ohyes=ohno');reLoad();
                                </script>
                            ";
                    die();
                }

                $this->db->trans_start();

                //  region update locker active
                $where = array(
                    "id" => $currentActiveLocker['id'],
                );
                $data_active = array(
                    "jumlah" => $currentActiveLocker['jml'] - $jml_nambah,
                    "state"  => "active",
                );
                $c = new MdlLockerStock();
                $c->updateData($where, $data_active);
                cekHere($this->db->last_query());
                //  endregion update locker active

                //  region locker hold
                $c = new MdlLockerStock();
                $array_hold_sebelumnya = $c->cekLoker(
                    $this->session->login['cabang_id'],
                    $produk_id,
                    "hold",
                    $this->session->login['id'],
                    "",
                    $this->session->login['gudang_id'],
                    $this->session->login['toko_id']
                );
                if (sizeof($array_hold_sebelumnya) > 0) {
                    $where = array(
                        "id" => $array_hold_sebelumnya['id'],
                    );
                    $data_hold = array(
                        "jumlah" => $array_hold_sebelumnya['jumlah'] + $jml_nambah,
                    );
                    $c = new MdlLockerStock();
                    $c->updateData($where, $data_hold);
                    cekBiru($this->db->last_query());
                }
                else {
                    $data_hold = array(
                        "jenis"     => "produk",
                        "cabang_id" => $this->session->login['cabang_id'],
                        "produk_id" => $id,
                        "nama"      => $currentActiveLocker['nama'],
                        "satuan"    => $currentActiveLocker['satuan'],
                        "state"     => "hold",
                        "jumlah"    => $jml_nambah,
                        "oleh_id"   => $this->session->login['id'],
                        "oleh_nama" => $this->session->login['nama'],
                        "gudang_id" => $this->session->login['gudang_id'],
                        "toko_id"   => $this->session->login['toko_id'],
                    );

                    $c = new MdlLockerStock();
                    $c->addData($data_hold);
                    cekHere($this->db->last_query());
                }
                //  endregion locker hold
                // matiHere(__LINE__ . " belum update <hr>");
                $this->db->trans_complete();// or die("Gagal bro");
            }
            else {
                if ($lockerWarnOnly == true) {
                    if ($jml_diperlukan > ($currentActiveLocker['jml'] + $currentHoldLocker['jml'])) {
                        echo(lgShowAlert("stock amount doesnt match to make on order. needed: $jml_diperlukan. avail: " . ($currentActiveLocker['jml'] + $currentHoldLocker['jml'])));
                        echo "
                                <script>                                
                                top.$('#shopping_cart').load('" . MODUL_PATH . "_shoppingCart/viewCart/" . $this->jenisTr . "?ohyes=ohno');reLoad();
                                </script>
                            ";
                        die();
                    }
                }
            }
        }

        $tmp = array(
                "handler"  => $this->uri->segment(1) . "/" . $this->uri->segment(2),
                "id"       => $produk_id,
                "nama"     => $currentActiveLocker['nama'],
                "satuan"   => $currentActiveLocker['satuan'],
                "harga"    => 0,
                "subtotal" => 0,
                "jml"      => $jmlBaru,
                // "stok"     => $stok,

            ) + $currentActiveLocker;

        /* ---------------------------------------------------
         * selector harga
         * -------------------------------------------*/
        $price = $this->selectorHarga($id);
        foreach ($price as $k => $v) {
            $tmp[$k] = $v;
        }
        arrPrintPink($price);
        // arrPrintWebs($tmp);
        // matiHere();
        $lastNett = isset($tmp[$priceConfig['mainSrc']]) ? $tmp[$priceConfig['mainSrc']] : 0;
        $lastDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
        $akumDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
        $lastNett -= $akumDisc;
        $tmp['lastNett'] = $lastNett;
        $tmp['akumDisc'] = $akumDisc;
        // arrPrintHijau($tmp['jml']);
        $_SESSION[$cCode]['items'][$id]['jml'] = $jml;
        $_SESSION[$cCode]['items'][$id]['qty'] = $jml;
        $_SESSION[$cCode]['items'][$id]['qty_unit'] = $jml;
        $_SESSION[$cCode]['items'][$id]['stok'] = $stok;
        //===baru update shopcartnya
        // cekMerah("$cCode || $id");
        // cekHijau(__LINE__ . __FILE__);
        // arrPrint($_SESSION[$cCode]['items']);
        // arrPrintPink($tmpB);
        arrPrintWebs($tmp);
        // matiHere(__LINE__);
        /* ----------------------------------------------------------
         * diskon-diskonan
         * ----------------------------------------------------------*/
        $pro_harga = $tmp['harga'] * 1;
        $pro_jml = $tmp['jml'];
        $calc_hasil = $this->selectorDiskon($id, $pro_harga, $pro_jml);
        arrPrintKuning($calc_hasil);
        //        arrprint($tmp);die();
        $tmp['discPersen'] = $calc_hasil['persen'];
        $tmp['harga_disc'] = $calc_hasil['harga_af'];
        $tmp['lastNett'] = $calc_hasil['harga_af'];
        $tmp['harga'] = $calc_hasil['harga_af'];
        $tmp['harga_jual'] = $calc_hasil['harga_be'];
        $tmp['discNilai'] = $calc_hasil['nilai'];
        $tmp['stok'] = $stok;
        // ------------------------------------------------------------------------------------------
        foreach ($fieldSrcs as $key => $src) {
            // cekUngu(":: $key => $src ::");
            $tmpSes = isset($_SESSION[$cCode]['items'][$id]) ? $_SESSION[$cCode]['items'][$id] : "";
            if (isset($tmp[$key])) {
                $tmp[$key] = makeValue($src, $tmpSes, $tmp, $tmpB[0]->$key);
            }
        }

        if (sizeof($itemNumLabels) > 0) {
            echo("iterating subNums.. " . __LINE__);
            foreach ($itemNumLabels as $key => $label) {
                if (isset($_GET[$key]) && $_GET[$key] > 0) {
                    $newValue = $_GET[$key];
                    $tmp[$key] = $newValue;
                    //                    $_SESSION[$cCode]['items'][$id][$key] = $newValue;
                    $tmp[$key] = $newValue;
                    echo "replacing value for $key with " . $newValue . "<br>";
                }
            }
        }

        if ($subAmountConfig != null) {

            $tmp['subtotal'] = makeValue($subAmountConfig, $tmp, (isset($_SESSION[$cCode]['items'][$id]) ? $_SESSION[$cCode]['items'][$id] : ""), 0);
        }
        else {
            $tmp['subtotal'] = 0;
        }
        //---------------------------------------------------
        // cekHijau(__LINE__ . __FILE__);
        arrPrint($tmp);
        // cekHijau(__LINE__ . __FILE__);
        // arrPrint($_SESSION[$cCode]['items']);
        // matiHere(__LINE__);
        // $_SESSION[$cCode]['items'][$produk_id] = $tmp;
        foreach ($tmp as $tmp_key => $tmp_val) {
            // cekMerah("$tmp_key -- $tmp_val");
            $_SESSION[$cCode]['items'][$produk_id][$tmp_key] = $tmp_val;
        }
        // cekMerah("$id");
        // mati_disini(__LINE__);
        // arrPrintKuning($_SESSION[$cCode]);
        // cekHijau(__LINE__ . __FILE__);
        arrPrintKuning($_SESSION[$cCode]['items']);

        //region untuk merebuild lagi gerbang nilai
        $this->load->library("ValueGate");
        $vg = new ValueGate();
        $vg->setConfigUiJenis($this->configUiJenis);
        $vg->setConfigCoreJenis($this->configCoreJenis);
        $vg->setConfigValuesJenis($this->configValuesJenis);
        $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"    => 1,
            "stepCode"      => isset($this->configUiJenis['steps'][1]['target']) ? $this->configUiJenis['steps'][1]['target'] : 0,
            "dtime"         => dtimeNow(),
            "fulldate"      => dtimeNow("Y-m-d"),
            // "jenis_pajak"=>$this->session->login['jenis_usaha'],
        );
        $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);
        //endregion


        echo "<script>";
        echo "  if(top.document.getElementById('shopping_cart')){";
        echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
        echo "  }";
        echo "</script>";

        // matiHere(__LINE__);
        //        if (isset($_GET['spc'])) {
        //            echo "<script>";
        //            echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?selID=$id&spc=1')";
        //            echo "</script>";
        //        }
        //        else {
        //            // echo "<script>";
        //            // echo "top.$('#result').load('" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?selID=$id')";
        //            // echo "</script>";
        //
        //            $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" => $stepNum,
        //                "stepCode" => $this->configUiJenis['steps'][$stepNum]['target'],
        //                "dtime" => dtimeNow(),
        //                "fulldate" => dtimeNow("Y-m-d"),
        //                // "jenis_pajak"=>$this->session->login['jenis_usaha'],
        //            );
        //            // $vg->buildValue($this->jenisTr, $id, $initMasterValues, $configUiJenis);
        //            $vg->buildValue($this->jenisTr, $id, $initMasterValues, $this->modul);
        //            // mati_disini(__FILE__ . "<hr> @" . __LINE__);
        //            // echo "<script>";
        //            // echo "top.document.getElementById('result').src='" . base_url() . "ValueGate/buildValues/" . $this->jenisTr . "?".$f_selector."selID=$id&modul=".$this->modul."';";
        //            // echo "</script>";
        //
        //            // matiHere(__METHOD__ . __LINE__);
        //            /* --------------------------------------------------
        //             * ngereload shoping cart dlm modul
        //             * --------------------------------------------------*/
        //            echo "<script>";
        //            echo "  if(top.document.getElementById('shopping_cart')){";
        //            echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
        //            echo "  }";
        //            echo "</script>";
        //        }


    }

    public function multiSelect()
    {
        $this->load->library("FieldCalculator");
        $cal = new FieldCalculator();

        $items = $_GET['items'];
        $mains = $_GET['main'];

        $arrItems = isset($_GET['items']) ? unserialize(base64_decode($items)) : array();
        $arrTrID = isset($_GET['trs']) ? unserialize(base64_decode($_GET['trs'])) : array();


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

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

        $selectorModel = $this->configUi[$this->jenisTr]['selectorModel'];
        $selectorSrcModel = $this->configUi[$this->jenisTr]['selectorSrcModel'];

        //        $this->load->model("Mdls/" . $selectorSrcModel);
        //        $b = new $selectorSrcModel();

        $this->load->model("Mdls/" . $selectorModel);
        $b = new $selectorModel();

        $itemNumLabels = isset($this->configUi[$this->jenisTr]['shoppingCartNumFields'][1]) ? $this->configUi[$this->jenisTr]['shoppingCartNumFields'][1] : array();
        $priceConfig = isset($this->configUi[$this->jenisTr]['selectedPrice']) ? $this->configUi[$this->jenisTr]['selectedPrice'] : array();
        $lockerConfig = isset($this->configUi[$this->jenisTr]['lockerCheck']) ? $this->configUi[$this->jenisTr]['lockerCheck'] : array();
        $lockerWarnOnly = isset($this->configUi[$this->jenisTr]['lockerCheckWarnOnly']) ? $this->configUi[$this->jenisTr]['lockerCheckWarnOnly'] : false;
        $subAmountConfig = isset($this->configUi[$this->jenisTr]['shoppingCartAmountValue'][1]) ? $this->configUi[$this->jenisTr]['shoppingCartAmountValue'][1] : null;
        $connectedDiscountConfig = isset($this->configUi[$this->jenisTr]['connectedDiscount']) ? $this->configUi[$this->jenisTr]['connectedDiscount'] : array();


        if (sizeof($arrItems) > 0) {
            foreach ($arrItems as $id => $jmlParam) {

                $tmpB = $b->lookupByID($id)->result();
                //                $tmpB = $b->lookupAll()->result();
                //                cekOrange($this->db->last_query());

                //arrPrint($tmpB);

                $jml = $jmlParam;
                if (sizeof($tmpB) > 0) {
                    foreach ($tmpB as $row) {
                        $satuan = strlen($row->satuan) > 0 ? $row->satuan : "n/a";
                        $kode = strlen($row->kode) > 0 ? $row->kode : "n/a";
                        $jml_stok = 0;
                        $jml_dipakai = 0;
                        $tmpJml = $jmlParam;
                        if (isset($lockerConfig['enabled']) && $lockerConfig['enabled'] == true) {
                            cekMerah("masuk locker config");

                            $this->db->trans_start();


                            //                            die();
                            $mdlName = $lockerConfig['mdlName'];
                            $this->load->model("Mdls/" . $mdlName);
                            $c = new $mdlName();
                            $c->addFilter("produk_id='$id'");
                            $c->addFilter("state='active'");
                            $c->addFilter("cabang_id=" . $this->session->login['cabang_id']);
                            $c->addFilter("gudang_id=" . $this->session->login['gudang_id']);
                            $tmpC = $c->lookupAll($id)->result();
                            cekHere($this->db->last_query());


                            if (sizeof($tmpC) > 0) {
                                //                                arrPrint($tmpC);
                                foreach ($tmpC as $row2) {

                                    //                                    arrPrintWebs($row);
                                    //                                    $satuan = strlen($row->satuan) > 0 ? $row->satuan : "n/a";
                                    $nama = $row2->nama;

                                    $jml_stok = $jml_now = $row2->jumlah;

                                    if (!array_key_exists($id, $_SESSION[$cCode]['items'])) {
                                        $jml_sudah_diambil = 0;
                                        //                                        $jml_diperlukan = 1;
                                        $jml_diperlukan = $arrItems[$id];
                                        //                                        $jml_nambah = 1;
                                        $jml_nambah = $arrItems[$id];
                                    }
                                    else {
                                        if (isset($_GET['newQty'])) {
                                            $jml_sudah_diambil = $_SESSION[$cCode]['items'][$id]['jml'];
                                            $jml_diperlukan = $_GET['newQty'];
                                            $jml_nambah = $jml_diperlukan - $jml_sudah_diambil;
                                        }
                                        else {
                                            $jml_sudah_diambil = $_SESSION[$cCode]['items'][$id]['jml'];
                                            $jml_diperlukan = $jml_sudah_diambil + $jml;
                                            $jml_nambah = $jml;
                                        }
                                    }

                                    //  region validasi stok
                                    if ($jml_nambah > $jml_now) {
                                        echo "<script>top.alert('stok $nama tidak cukup. (perlu $jml_diperlukan, nambah $jml_nambah stok $jml_now)')";
                                        echo "</script>";
                                        die();
                                    }
                                    //  endregion validasi stok

                                    //  region update locker active
                                    $where = array(
                                        "id" => $row2->id,
                                    );
                                    $data_active = array(
                                        "jumlah" => $jml_now - $jml_nambah,
                                        "state"  => "active",
                                    );
                                    $c->updateData($where, $data_active);
                                    cekHere($this->db->last_query());
                                    //  endregion update locker active


                                    //  region locker hold
                                    $array_hold_sebelumnya = $c->cekLoker($this->session->login['cabang_id'], $id, "hold", $this->session->login['id'], "0", $this->session->login['gudang_id']);
                                    if (sizeof($array_hold_sebelumnya) > 0) {
                                        $where = array(
                                            "id" => $array_hold_sebelumnya['id'],
                                        );
                                        $data_hold = array(
                                            "jumlah" => $array_hold_sebelumnya['jumlah'] + $jml_nambah,
                                        );
                                        $c->updateData($where, $data_hold);
                                        cekHere($this->db->last_query());
                                    }
                                    else {
                                        $data_hold = array(
                                            "jenis"     => "produk",
                                            "cabang_id" => $this->session->login['cabang_id'],
                                            "produk_id" => $id,
                                            "nama"      => $nama,
                                            "kode"      => $kode,
                                            "satuan"    => $row2->satuan,
                                            "state"     => "hold",
                                            "jumlah"    => $jml_nambah,
                                            "oleh_id"   => $this->session->login['id'],
                                            "oleh_nama" => $this->session->login['nama'],
                                            "gudang_id" => $this->session->login['gudang_id'],
                                        );
                                        $c->addData($data_hold);
                                        cekHere($this->db->last_query());
                                    }
                                    //  endregion locker hold


                                    $this->db->trans_complete();// or die("Gagal bro");

                                    $jml_dipakai = $tmpJml = $jml_diperlukan;


                                }
                            }
                            else {
                                mati_disini("tidak ditemukan item " . $row->nama . " di locker stock.");
                            }

                        }
                        else {
                            //                            cekmerah("tidak pakai loker");
                        }

                        if ($lockerWarnOnly == true) {
                            $mdlName = $lockerConfig['mdlName'];
                            $this->load->model("Mdls/" . $mdlName);
                            $c = new $mdlName();
                            $c->addFilter("produk_id='$id'");
                            $c->addFilter("state='active'");
                            $c->addFilter("cabang_id=" . $this->session->login['cabang_id']);
                            $c->addFilter("gudang_id=" . $this->session->login['gudang_id']);
                            $tmpC = $c->lookupAll($id)->result();
                            cekHere($this->db->last_query());
                            if (sizeof($tmpC) < 1) {
                                if ($jml_nambah > $jml_now) {
                                    echo "<script>top.alert('$nama tidak memiliki stok. \\nsilahkan hubungi bagian terkait untuk pengadaan stok')";
                                    echo "</script>";
                                    die();
                                }
                            }
                        }

                        $fieldSrcs = isset($this->configUi[$this->jenisTr]['shoppingCartFieldSrc']) ? $this->configUi[$this->jenisTr]['shoppingCartFieldSrc'] : array("nama" => "nama");
                        if (!array_key_exists($id, $_SESSION[$cCode]['items'])) {

                            cekOrange("sini kodenya: " . $kode);

                            $tmp = array(
                                "handler"  => $this->uri->segment(1) . "/" . $this->uri->segment(2),
                                "id"       => $id,
                                "jml"      => $tmpJml,
                                "kode"     => $kode,
                                "stok"     => $jml_stok - $jml_dipakai,
                                "harga"    => 0,
                                "subtotal" => 0,
                                "satuan"   => strlen($row->satuan) > 0 ? $row->satuan : "n/a",
                            );

                            cekHere("sini tmp nya");
                            arrPrint($tmp);

                            /* -----------------------------------------------
                             * harga
                             * -----------------------------------------------*/
                            if (sizeof($priceConfig) > 0) {
                                $mdlName = $priceConfig['model'];
                                $this->load->model("Mdls/" . $mdlName);
                                $h = new $mdlName();
                                $h->addFilter("produk_id='$id'");
                                $h->addFilter("status='1'");
                                //                                $h->addFilter("jenis_value='" . $priceConfig['label'] . "'");
                                $h->addFilter("jenis_value in ('" . implode("','", $priceConfig['label']) . "')");
                                $h->addFilter("cabang_id=" . $this->session->login['cabang_id']);
                                $tmpH = $h->lookupAll($id)->result();
                                cekMerah($this->db->last_query());
                                if (sizeof($tmpH) > 0) {
                                    $rawPrices = array();
                                    foreach ($tmpH as $hSpec) {
                                        foreach ($priceConfig['key_label'] as $key => $val) {
                                            if ($key == $hSpec->jenis_value) {
                                                $rawPrices[$key] = isset($hSpec->nilai) ? $hSpec->nilai : 0;
                                            }
                                        }
                                    }
                                    $prices = normalizePrices("produk", $rawPrices);
                                    arrPrint($prices);
                                    if (sizeof($prices) > 0) {
                                        foreach ($prices as $k => $v) {
                                            $tmp[$k] = $v;
                                        }
                                        $tmp['harga'] = isset($tmp[$priceConfig['mainSrc']]) ? $tmp[$priceConfig['mainSrc']] : 0;
                                    }
                                }

                            }

                            matiHere(__LINE__);

                            $lastNett = $tmp[$priceConfig['mainSrc']];
                            //                            $lastNett = $priceConfig['mainSrc'];
                            $lastDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
                            $akumDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
                            $lastNett -= $akumDisc;
                            $tmp['lastNett'] = $lastNett;
                            $tmp['akumDisc'] = $akumDisc;

                            //                            cekHere('$tmp #2');
                            //                            arrPrint($tmp);

                            if (sizeof($connectedDiscountConfig) > 0) {
                                if ($connectedDiscountConfig['enabled'] == 1) {
                                    $mdlNameRelation = $connectedDiscountConfig['mdlNameRelation'];
                                    $mdlNameSource = $connectedDiscountConfig['mdlNameSource'];

                                    $this->load->model("Mdls/" . $mdlNameRelation);
                                    $dr = new $mdlNameRelation();
                                    $dr->addFilter("produk_id='$id'");
                                    $dr->addFilter("status='1'");
                                    $tmpDr = $dr->lookupAll($id)->result();


                                    $produkQty = $tmpJml;
                                    //                                    $tmp['lastNett'] = $tmp['jual_nppn'];
                                    //                                    $tmp['akumDisc'] = 0;
                                    if (sizeof($tmpDr) > 0) {
                                        $arrDiscount = array();
                                        foreach ($tmpDr as $drSpec) {
                                            $this->load->model("Mdls/" . $mdlNameSource);
                                            $sr = new $mdlNameSource();
                                            $sr->addFilter("id='" . $drSpec->diskon_id . "'");
                                            $sr->addFilter("status='1'");
                                            $tmpSr = $sr->lookupAll($id)->result();


                                            foreach ($tmpSr as $srSpec) {
                                                if (($produkQty >= $srSpec->min_qty) && ($produkQty <= $srSpec->max_qty)) {
                                                    $discountPersen = $srSpec->discount_persen;
                                                    //                                $discountQty = $srSpec->discount_qty;
                                                }
                                                else {
                                                    $discountPersen = 0;
                                                    //                                $discountQty = 0;
                                                }
                                                $arrDiscount[$id][$srSpec->var_name] = array(
                                                    "persen" => $discountPersen,
                                                    //                                "qty" => $discountQty,
                                                );

                                                cekMerah("pID: $id ::: disc_persen: $discountPersen ::: disc_qty: 0 ::: prod_qty: $produkQty");

                                            }
                                        }


                                        if (isset($arrDiscount[$id]) && sizeof($arrDiscount[$id]) > 0) {

                                            //                                            $lastNett = $tmp['jual_nppn'];
                                            //                                            $lastDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
                                            //                                            $akumDisc = isset($tmp['disc']) ? $tmp['disc'] : 0;
                                            //                                            $lastNett -= $akumDisc;
                                            //                                            $tmp['lastNett'] = $lastNett;
                                            //                                            $tmp['akumDisc'] = $akumDisc;
                                            foreach ($arrDiscount[$id] as $code => $dSpec) {

                                                foreach ($dSpec as $dKey => $dVal) {
                                                    if (!isset($tmp['__disc_' . $code . '_' . $dKey])) {
                                                        $tmp['__disc_' . $code . '_' . $dKey] = 0;
                                                    }
                                                    $tmp['__disc_' . $code . '_' . $dKey] = $dVal;


                                                    $lastDisc = ($dVal / 100) * $lastNett;
                                                    $akumDisc += $lastDisc;
                                                    $lastNett -= $lastDisc;

                                                    $tmp['__disc_' . $code . '_value'] = $lastDisc;

                                                    cekLime("eksekusi code: $code, lastDisc: $lastDisc, lastNett: $lastNett, akumDisc: $akumDisc");
                                                }

                                            }
                                            cekOrange("lastNett: $lastNett, akumDisc: $akumDisc");

                                            $tmp['lastNett'] = $lastNett;
                                            $tmp['akumDisc'] = $akumDisc;
                                        }
                                    }
                                }
                            }

                            //                            cekMerah('ini $fieldSrcs');
                            //                            arrprintWebs($fieldSrcs);

                            foreach ($fieldSrcs as $key => $src) {
                                $tmpEx = $cal->multiExplode($src);
                                //                                arrPrint($tmpEx);
                                if (sizeof($tmpEx) > 1) {//===berarti mengandung karakter simbol perhitungan
                                    cekBiru("$key perhitungan");
                                    $newSrc = $src;
                                    foreach ($tmpEx as $key2 => $val2) {
                                        echo "$key2 - $val2 <br>";
                                        if (!is_numeric($val2)) {
                                            if (isset($tmp[$val2]) && $tmp[$val2] > 0) {
                                                $newSrc = str_replace($val2, $tmp[$val2], $newSrc);
                                            }
                                            else {
                                                $newSrc = str_replace($val2, 0, $newSrc);
                                            }
                                        }
                                        //                                else {
                                        //                                    if (isset($_SESSION[$cCode]['out_master'][$val2]) && $_SESSION[$cCode]['out_master'][$val2] > 0) {
                                        //                                        $newSrc = str_replace($val2, $_SESSION[$cCode]['out_master'][$val2], $newSrc);
                                        //                                    } else {
                                        //                                        if (isset($_SESSION[$cCode]['main'][$val2]) && $_SESSION[$cCode]['main'][$val2] > 0) {
                                        //                                            $newSrc = str_replace($val2, $_SESSION[$cCode]['main'][$val2], $newSrc);
                                        //                                        } else {
                                        //                                            $newSrc = str_replace($val2, 0, $newSrc);
                                        //                                        }
                                        //                                    }
                                        //                                }
                                    }
                                    //                                    cekBiru("$$src -> $newSrc -> " . $cal->calculate($newSrc));
                                    $tmp[$key] = $cal->calculate($newSrc);
                                }
                                else {
                                    //                                    cekBiru("$key BUKAN perhitungan");
                                    $tmp[$key] = isset($row->$src) ? $row->$src : 0;
                                }


                            }

                            //===perhitungan subtotal
                            $cal = new FieldCalculator();


                            if ($subAmountConfig != null) {
                                $tmpEx = $cal->multiExplode($subAmountConfig);
                                if (sizeof($tmpEx) > 1) {
                                    $newSrc = $subAmountConfig;
                                    foreach ($tmpEx as $key2 => $val2) {
                                        if (isset($tmp[$val2])) {
                                            $newSrc = str_replace($val2, $tmp[$val2], $newSrc);
                                            cekKuning("$val2 direplace dengan " . $tmp[$val2]);
                                        }
                                        else {
                                            $newSrc = str_replace($val2, "0", $newSrc);
                                            cekKuning("$val2 direplace dengan NOL");
                                        }

                                    }
                                    $subtotal = $cal->calculate($newSrc);
                                    cekHijau("subtotal dari perhitungan $subAmountConfig $newSrc");

                                }
                                else {
                                    $subtotal = 0;
                                    cekHijau("subtotal dari perhitungan yang gak ada");
                                }
                            }
                            else {
                                $subtotal = 0;
                                cekHijau("subtotal NOL");
                            }
                            $tmp["subtotal"] = $subtotal;
                            $_SESSION[$cCode]['items'][$id] = $tmp;
                        }
                        else {
                            if (isset($_GET['newQty'])) {
                                $_SESSION[$cCode]['items'][$id]['jml'] = $_GET['newQty'];
                            }
                            else {
                                $_SESSION[$cCode]['items'][$id]['jml'] += $jml;
                            }


                            if (sizeof($connectedDiscountConfig) > 0) {
                                if ($connectedDiscountConfig['enabled'] == 1) {
                                    $mdlNameRelation = $connectedDiscountConfig['mdlNameRelation'];
                                    $mdlNameSource = $connectedDiscountConfig['mdlNameSource'];

                                    $this->load->model("Mdls/" . $mdlNameRelation);
                                    $dr = new $mdlNameRelation();
                                    $dr->addFilter("produk_id='$id'");
                                    $dr->addFilter("status='1'");
                                    $tmpDr = $dr->lookupAll($id)->result();


                                    $produkQty = isset($_SESSION[$cCode]['items'][$id]['jml']) ? $_SESSION[$cCode]['items'][$id]['jml'] : 0;
                                    $_SESSION[$cCode]['items'][$id]['lastNett'] = $_SESSION[$cCode]['items'][$id][$priceConfig['mainSrc']];
                                    $_SESSION[$cCode]['items'][$id]['akumDisc'] = 0;
                                    if (sizeof($tmpDr) > 0) {
                                        $arrDiscount = array();
                                        foreach ($tmpDr as $drSpec) {
                                            $this->load->model("Mdls/" . $mdlNameSource);
                                            $sr = new $mdlNameSource();
                                            $sr->addFilter("id='" . $drSpec->diskon_id . "'");
                                            $sr->addFilter("status='1'");
                                            $tmpSr = $sr->lookupAll($id)->result();


                                            foreach ($tmpSr as $srSpec) {
                                                if (($produkQty >= $srSpec->min_qty) && ($produkQty <= $srSpec->max_qty)) {
                                                    $discountPersen = $srSpec->discount_persen;
                                                    //                                $discountQty = $srSpec->discount_qty;
                                                }
                                                else {
                                                    $discountPersen = 0;
                                                    //                                $discountQty = 0;
                                                }

                                                $arrDiscount[$id][$srSpec->var_name] = array(
                                                    "persen" => $discountPersen,
                                                    //                                "qty" => $discountQty,
                                                );

                                                cekMerah("pID: $id ::: disc_persen: $discountPersen ::: disc_qty: 0 ::: prod_qty: $produkQty");

                                            }
                                        }


                                        if (isset($arrDiscount[$id]) && sizeof($arrDiscount[$id]) > 0) {

                                            $lastNett = $_SESSION[$cCode]['items'][$id][$priceConfig['mainSrc']];
                                            $lastDisc = 0;
                                            $akumDisc = 0;
                                            foreach ($arrDiscount[$id] as $code => $dSpec) {

                                                foreach ($dSpec as $dKey => $dVal) {
                                                    if (!isset($tmp['__disc_' . $code . '_' . $dKey])) {
                                                        $_SESSION[$cCode]['items'][$id]['__disc_' . $code . '_' . $dKey] = 0;
                                                    }
                                                    $_SESSION[$cCode]['items'][$id]['__disc_' . $code . '_' . $dKey] = $dVal;


                                                    $lastDisc = ($dVal / 100) * $lastNett;
                                                    $akumDisc += $lastDisc;
                                                    $lastNett -= $lastDisc;

                                                    $_SESSION[$cCode]['items'][$id]['__disc_' . $code . '_value'] = $lastDisc;

                                                    cekLime("eksekusi code: $code, lastDisc: $lastDisc, lastNett: $lastNett, akumDisc: $akumDisc");
                                                }

                                            }
                                            cekOrange("lastNett: $lastNett, akumDisc: $akumDisc");

                                            $_SESSION[$cCode]['items'][$id]['lastNett'] = $lastNett;
                                            $_SESSION[$cCode]['items'][$id]['akumDisc'] = $akumDisc;
                                        }
                                    }
                                }
                            }


                            if (sizeof($itemNumLabels) > 0) {
                                echo("iterating subNums..");
                                foreach ($itemNumLabels as $key => $label) {
                                    if (isset($_GET[$key]) && $_GET[$key] > 0) {
                                        $newValue = $_GET[$key];
                                        $tmp[$key] = $newValue;
                                        $_SESSION[$cCode]['items'][$id][$key] = $newValue;
                                        echo "replacing value for $key with " . $newValue . "<br>";
                                    }

                                }

                                foreach ($itemNumLabels as $key => $label) {
                                    $_SESSION[$cCode]['items'][$id]["sub_" . $key] = ($_SESSION[$cCode]['items'][$id][$key] * $_SESSION[$cCode]['items'][$id]["jml"]);
                                }


                                $cal = new FieldCalculator();
                                if ($subAmountConfig != null) {
                                    $tmpEx = $cal->multiExplode($subAmountConfig);
                                    if (sizeof($tmpEx) > 1) {
                                        $newSrc = $subAmountConfig;
                                        foreach ($tmpEx as $key2 => $val2) {
                                            if (isset($tmp[$val2])) {
                                                $newSrc = str_replace($val2, $tmp[$val2], $newSrc);
                                                cekKuning("$val2 direplace dengan " . $tmp[$val2]);
                                            }
                                            else {
                                                $newSrc = str_replace($val2, "0", $newSrc);
                                                cekKuning("$val2 direplace dengan NOL");
                                            }

                                        }
                                        $subtotal = $cal->calculate($newSrc);
                                        cekHijau("subtotal dari perhitungan $subAmountConfig $newSrc");

                                    }
                                    else {
                                        $subtotal = 0;
                                        cekHijau("subtotal dari perhitungan yang gak ada");
                                    }
                                }
                                else {
                                    $subtotal = 0;
                                    cekHijau("subtotal NOL");
                                }
                                $_SESSION[$cCode]['items'][$id]["subtotal"] = $subtotal;
                            }


                        }
                    }

                    if (sizeof($_SESSION[$cCode]['items']) > 0) {
                        $_SESSION[$cCode]['main']['harga'] = 0;

                        foreach ($_SESSION[$cCode]['items'] as $id => $iSpec) {
                            $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);

                        }
                    }

                }
                else {
                    cekMerah("tidak ada itemnya!");
                    die();
                }

            }
        }

        if (sizeof($arrTrID) > 0) {
            $_SESSION[$cCode]['main']['references'] = $arrTrID;

        }
        if (isset($_GET['singleRefID']) && strlen($_GET['singleRefID']) > 0) {
            $_SESSION[$cCode]['main']['singleReference'] = $_GET['singleRefID'];

        }


        $this->load->library("ValueGate");
        $vg = new ValueGate();
        $vg->setConfigUiJenis($this->configUiJenis);
        $vg->setConfigCoreJenis($this->configCoreJenis);
        $vg->setConfigValuesJenis($this->configValuesJenis);
        $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"    => 1,
            "stepCode"      => isset($this->configUiJenis['steps'][1]['target']) ? $this->configUiJenis['steps'][1]['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 "  if(top.document.getElementById('shopping_cart')){";
        echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
        echo "  }";
        echo "</script>";
    }

    public function remove()
    {

        $id = $_GET['id'];
        $cCode = "_TR_" . $this->jenisTr;
        if (isset($_SESSION[$cCode]['items'][$id])) {


            $this->db->trans_start();

            $c = new MdlLockerStock();
            $array_hold_sebelumnya = $c->cekLoker($this->session->login['cabang_id'], $id, "hold", $this->session->login['id'], "0", $this->session->login['gudang_id']);
            $where = array(
                "id" => $array_hold_sebelumnya['id'],
            );
            $data_hold = array(
                "jumlah" => 0,
            );

            $c->updateData($where, $data_hold);
            cekBiru($this->db->last_query());
            //die();

            $c = new MdlLockerStock();
            $array_active_sebelumnya = $c->cekLoker($this->session->login['cabang_id'], $id, "active", "0", "0", $this->session->login['gudang_id']);
            cekHijau($this->db->last_query());


            $where = array(
                "id" => $array_active_sebelumnya['id'],
            );
            $data_active = array(
                "jumlah" => $array_active_sebelumnya['jumlah'] + $array_hold_sebelumnya['jumlah'],
            );
            $c->updateData($where, $data_active);


            $this->db->trans_complete();// or die("Gagal bro");


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

        }
        if (sizeof($_SESSION[$cCode]['items']) < 1) {
            //            matiHere();
            //            $_SESSION[$cCode] = null;
            //            unset($_SESSION[$cCode]);
        }
        if (isset($_SESSION[$cCode]['main']['oto_diskon'])) {
            $_SESSION[$cCode]['main']['oto_diskon'] = 0;
        }
        $this->load->library("ValueGate");
        $vg = new ValueGate();
        $vg->setConfigUiJenis($this->configUiJenis);
        $vg->setConfigCoreJenis($this->configCoreJenis);
        $vg->setConfigValuesJenis($this->configValuesJenis);
        $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"    => 1,
            "stepCode"      => isset($this->configUiJenis['steps'][1]['target']) ? $this->configUiJenis['steps'][1]['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 "  if(top.document.getElementById('shopping_cart')){";
        echo "  top.$('#shopping_cart').load('" . base_url() . $this->modul . "/_shoppingCart/viewCart/" . $this->jenisTr . "?selID=$id');";
        echo "  }";
        echo "</script>";
    }

    public function updateValues()
    {
        echo "---------------------------your input params needed------------------------------";
        arrprint($_POST);
        $cCode = "_TR_" . $this->jenisTr;
        $rawParam = $_POST['param'];
        arrPrint($rawParam);
        die("updating.............................. (will be available sooner or later)");
        $rawParam = $_GET['param'];
        $param = unserialize(base64_decode($rawParam));
        if (is_array($param) && sizeof($param) > 0) {

        }
    }

    /* ----------------------------------------------
     * per gerbang dibuat method terpisah
     * ----------------------------------------------*/
    private function selectorLocker($id)
    {

        $lockerConfig = isset($this->configUi[$this->jenisTr]['lockerCheck']) ? $this->configUi[$this->jenisTr]['lockerCheck'] : array();
        $c = new MdlLockerStock();
        $c->addFilter("jenis='produk'");
        $c->addFilter("produk_id='$id'");
        $c->addFilter("state='active'");
        $c->addFilter("cabang_id=" . $this->session->login['cabang_id']);
        $c->addFilter("gudang_id=" . $this->session->login['gudang_id']);
        $c->addFilter("toko_id=" . $this->session->login['toko_id']);
        $c->addFilter("oleh_id='0'");
        $c->addFilter("transaksi_id='0'");
        $tmpC = $c->lookupAll($id)->result();
        cekBiru($this->db->last_query());
        arrPrintWebs($tmpC);

        $produk_id = isset($tmpC[0]->produk_id) ? $tmpC[0]->produk_id : "";
        $stok = isset($tmpC[0]->jumlah) ? $tmpC[0]->jumlah : 0;

        if (sizeof($tmpC) > 0) {
            $currentActiveLocker = array(
                "id"     => $tmpC[0]->id,
                "jml"    => $tmpC[0]->jumlah,
                "nama"   => $tmpC[0]->nama,
                "satuan" => $tmpC[0]->satuan,
                "stok"   => $tmpC[0]->jumlah,
                // "no_part"     => $tmpB[0]->no_part,
                // "lokasi_nama" => $tmpB[0]->lokasi_nama,
                // "merek_nama"  => $tmpB[0]->merek_nama,
            );
        }
        else {
            $currentActiveLocker = array(
                "id"          => 0,
                "jml"         => 0,
                "nama"        => "noname",
                "no_part"     => "nopart",
                "satuan"      => "-",
                "stok"        => 0,
                "lokasi_nama" => "-",
                "merek_nama"  => "-",
            );
        }

        $c = new MdlLockerStock();
        $c->addFilter("jenis='produk'");
        $c->addFilter("produk_id='$id'");
        $c->addFilter("state='hold'");
        $c->addFilter("cabang_id=" . $this->session->login['cabang_id']);
        $c->addFilter("gudang_id=" . $this->session->login['gudang_id']);
        $c->addFilter("oleh_id=" . $this->session->login['id']);
        $c->addFilter("toko_id=" . $this->session->login['toko_id']);
        $c->addFilter("transaksi_id='0'");
        $tmpC = $c->lookupAll($id)->result();

        if (sizeof($tmpC) > 0) {
            $currentHoldLocker = array(
                "id"     => $tmpC[0]->id,
                "jml"    => $tmpC[0]->jumlah,
                "nama"   => $tmpC[0]->nama,
                "satuan" => $tmpC[0]->satuan,
                "stok"   => $tmpC[0]->jumlah,
                // "no_part" => $tmpB[0]->no_part,
            );
        }
        else {
            $currentHoldLocker = array(
                "id"      => 0,
                "jml"     => 0,
                "nama"    => "noname",
                "no_part" => "nopart",
                "satuan"  => "-",
                "stok"    => 0,
            );
        }

        // $persediaan = (sizeof($tmpC) > 0 && $tmpC[0]->jumlah > 0) ? $tmpC[0]->jumlah : "0";

        $datas = array();
        $datas["currentActiveLocker"] = $currentActiveLocker;
        $datas["currentHoldLocker"] = $currentHoldLocker;
        $datas["stok"] = $stok;

        return $datas;
    }

    public function selectorHarga($id)
    {

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

        if (sizeof($priceConfig) > 0) {
            $mdlName = $priceConfig['model'];
            $this->load->model("Mdls/" . $mdlName);
            $h = new $mdlName();
            $h->addFilter("produk_id='$id'");
            $h->addFilter("status='1'");
            //                                $h->addFilter("jenis_value='" . $priceConfig['label'] . "'");
            $h->addFilter("jenis_value in ('" . implode("','", $priceConfig['label']) . "')");
            $h->addFilter("cabang_id=" . $this->session->login['cabang_id']);
            $tmpH = $h->lookupAll($id)->result();
            cekMerah($this->db->last_query());
            // arrPrint($tmpH);
            // matiHEre();
            if (sizeof($tmpH) > 0) {
                $rawPrices = array();
                foreach ($tmpH as $hSpec) {
                    foreach ($priceConfig['key_label'] as $key => $val) {
                        // arrPrint($key);
                        if ($key == $hSpec->jenis_value) {
                            $rawPrices[$key] = isset($hSpec->nilai) ? $hSpec->nilai : 0;
                        }
                    }
                }
                $prices = normalizePrices("produk", $rawPrices);
                if (sizeof($prices) > 0) {
                    foreach ($prices as $k => $v) {
                        $tmp[$k] = $v;
                    }
                    $tmp['harga'] = isset($tmp[$priceConfig['mainSrc']]) ? $tmp[$priceConfig['mainSrc']] : 0;
                }
            }
        }
        else {
            $tmp = array();
        }

        return $tmp;
    }

    private function selectorDiskon($produk_id, $produk_harga, $produk_jml)
    {

        // cekHitam("produk_id $produk_id || tokid " . my_toko_id());
        $this->load->library("Diskon");
        $dp = new Diskon();
        $dp->setTokoId(my_toko_id());
        $src_diskon = $dp->CallProdukDiskon($produk_id);
        // arrPrintHijau($src_diskon);
        // diskon
        $pro_harga = $produk_harga * 1;
        $pro_jml = $produk_jml;
        // cekPink("$pro_harga || $pro_jml");
        /*---menentukan diskon pokok dari produk atau grosir----*/
        $d_pokok = isset($src_diskon['produk']) ? $src_diskon['produk'] : 0;

        $jml_spek_grosir = sizeof($src_diskon['grosir']);
        if ($jml_spek_grosir > 0) {
            $gro_count = 0;
            foreach ($src_diskon['grosir'] as $item) {
                $gro_count++;
                $gro_minim = $item['minim'];
                $gro_maxim = $gro_count == $jml_spek_grosir ? INF : $item['maxim'];
                $gro_persen = $item['persen'];
                // cekPink2("$pro_jml >= $gro_minim) && ($pro_jml <= $gro_maxim)");

                if (($pro_jml >= $gro_minim) && ($pro_jml <= $gro_maxim)) {
                    $d_pokok = $gro_persen;
                    // cekBiru("---- $d_pokok");
                    break;
                }

            }
        }
        else {
            // $d_pokok = isset($src_diskon['produk']) ? $src_diskon['produk'] : 0;
        }

        $diskon_pokok["produk"] = $d_pokok;
        $diskon_event = array();

        $calc_hasil = $dp->calcDiskon($pro_harga, $diskon_pokok, $diskon_event, "diskon");

        return $calc_hasil;
    }
}