<?php

require_once "Modul_Controller.php";

class _followupLiveEdit extends Modul_Controller
{

//    private $jenisTr;


    public function __construct()
    {
        parent::__construct();


    }

    //region gs

//    public function getJenisTr()
//    {
//        return $this->jenisTr;
//    }
//
//    public function setJenisTr($jenisTr)
//    {
//        $this->jenisTr = $jenisTr;
//    }
    //endregion

    public function removeItem()
    {
//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");
        $configValuesJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiValues");


        $id = $_GET['id'];


        if (isset($_SESSION[$cCode]['items'][$id])) {
            $_SESSION[$cCode]['items'][$id] = null;
            unset($_SESSION[$cCode]['items'][$id]);
        }

        if (isset($_SESSION[$cCode]['tableIn_detail'][$id])) {
            $_SESSION[$cCode]['tableIn_detail'][$id] = null;
            unset($_SESSION[$cCode]['tableIn_detail'][$id]);
        }
        if (isset($_SESSION[$cCode]['tableIn_detail_values'][$id])) {
            $_SESSION[$cCode]['tableIn_detail_values'][$id] = null;
            unset($_SESSION[$cCode]['tableIn_detail_values'][$id]);
        }
        //==beberapa bagian di MAIN harus di-reset, sesuai ....
        if (isset($_SESSION[$cCode]['main']['harga'])) {
//            unset($_SESSION[$cCode]['main']['harga']);
            $_SESSION[$cCode]['main']['harga'] = 0;
        }


        if (isset($_SESSION[$cCode]['main']['status_4'])) {
            $_SESSION[$cCode]['main']['status_4'] = 5;
        }
        if (isset($_SESSION[$cCode]['main']['trash_4'])) {
            $_SESSION[$cCode]['main']['trash_4'] = 0;
        }


        //==recover nilai HARGA master
        $_SESSION[$cCode]['main']['harga'] = 0;
        if (sizeof($_SESSION[$cCode]['items']) > 0) {
            foreach ($_SESSION[$cCode]['items'] as $id => $iSpec) {
                $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);
            }
        }
//mati_disini();
        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
        fillValues_he_value_builder($jenisTr, $fromStep, $intoStep, $configCoreJenis, $configUiJenis, $this->configValuesJenis, "");


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . $this->modul . "/" . "FollowUp/followupPreview/" . $this->jenisTr . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:true,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }

//        echo "<html>";
//        echo "<head>";
////        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";
//        echo "</body>";
//        echo "</html>";

    }

    public function updateItemField()
    {

//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $id = $_GET['id'];
        $key = $_GET['key'];
        $val = $_GET['val'];

        if (isset($_SESSION[$cCode]['items'][$id][$key])) {

            $_SESSION[$cCode]['items'][$id][$key] = $val;

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

            }
            if (isset($_SESSION[$cCode]['items'][$id]['nett'])) {
                $_SESSION[$cCode]['items'][$id]['sub_nett'] = ($_SESSION[$cCode]['items'][$id]['nett'] * $_SESSION[$cCode]['items'][$id]['jml']);
            }

            if ($subAmountConfig != null) {
                $items = $_SESSION[$cCode]['items'];
                $subtotal = makeValue($subAmountConfig, $items[$id], $items[$id], 0);
            }
            else {
                $subtotal = 0;
            }
            $_SESSION[$cCode]['items'][$id]['subtotal'] = ($subtotal);

            if ($val < $_SESSION[$cCode]['items'][$id][$key]) {
                if (isset($_SESSION[$cCode]['main']['status_4'])) {
                    $_SESSION[$cCode]['main']['status_4'] = 5;
                }
                if (isset($_SESSION[$cCode]['main']['trash_4'])) {
                    $_SESSION[$cCode]['main']['trash_4'] = 0;
                }
            }


        }
        else {
//            echo(lgShowAlert("NOT replacing $key with $val"));
        }

        //==recover nilai HARGA master
        $_SESSION[$cCode]['main']['harga'] = 0;
        if (sizeof($_SESSION[$cCode]['items']) > 0) {
            foreach ($_SESSION[$cCode]['items'] as $id_ => $iSpec) {
                $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);
            }
        }

        if (sizeof($_SESSION[$cCode]['items_child'][$id])) {

            unset($_SESSION[$cCode]['items_child'][$id]);

            for ($i = 1; $i <= $val; $i++) {
                foreach ($items_child as $col => $alias) {
                    $_SESSION[$cCode]['items_child'][$id][$i][$col] = isset($_SESSION[$cCode]['items'][$id][$col]) ? $_SESSION[$cCode]['items'][$id][$col] : "";
//                    cekOrange($id);
                }
//                cekOrange($i);
            }


        }

        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);
        fillValues_he_value_builder($jenisTr, $fromStep, $intoStep, $configCoreJenis, $configUiJenis);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }
        else {
            echo "\n<script>";
            echo "\ntop.$('.kalkulasi_modal').removeClass('hidden');";
//            echo "\ntop.console.error('$rawBuilderURL');";
            echo "\n</script>";
        }


    }

    public function updateItemFieldMb()
    {

//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $id = $_GET['id'];
        $key = $_GET['key'];
        $val = is_numeric($_GET['val']) ? $_GET['val'] : htmlspecialchars($_GET['val']);
        if (isset($_SESSION[$cCode]['items'][$id][$key])) {

            $_SESSION[$cCode]['items'][$id][$key] = $val;

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

            }
            if (isset($_SESSION[$cCode]['items'][$id]['nett'])) {
                $_SESSION[$cCode]['items'][$id]['sub_nett'] = ($_SESSION[$cCode]['items'][$id]['nett'] * $_SESSION[$cCode]['items'][$id]['jml']);
            }

            if ($subAmountConfig != null) {
                $items = $_SESSION[$cCode]['items'];
                $subtotal = makeValue($subAmountConfig, $items[$id], $items[$id], 0);
            }
            else {
                $subtotal = 0;
            }
            $_SESSION[$cCode]['items'][$id]['subtotal'] = ($subtotal);

            if ($val < $_SESSION[$cCode]['items'][$id][$key]) {
                if (isset($_SESSION[$cCode]['main']['status_4'])) {
                    $_SESSION[$cCode]['main']['status_4'] = 5;
                }
                if (isset($_SESSION[$cCode]['main']['trash_4'])) {
                    $_SESSION[$cCode]['main']['trash_4'] = 0;
                }
            }


        }
        else {
//            echo(lgShowAlert("NOT replacing $key with $val"));
        }


        //==recover nilai HARGA master
        $_SESSION[$cCode]['main']['harga'] = 0;
        if (sizeof($_SESSION[$cCode]['items']) > 0) {
            foreach ($_SESSION[$cCode]['items'] as $id_ => $iSpec) {
                $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);
            }
        }

        //update child detail(aset)
        if (sizeof($_SESSION[$cCode]['items_child'][$id])) {

//            arrPrint($_SESSION[$cCode]['items_child'][$id]);
            unset($_SESSION[$cCode]['items_child'][$id]);
//            arrPrint($_SESSION[$cCode]['items_child'][$id]);
            for ($i = 1; $i <= $val; $i++) {
                foreach ($items_child as $col => $alias) {
                    $_SESSION[$cCode]['items_child'][$id][$i][$col] = isset($_SESSION[$cCode]['items'][$id][$col]) ? $_SESSION[$cCode]['items'][$id][$col] : "";
//                    cekOrange($id);
                }
//                cekOrange($i);
            }
//            arrPrint($_SESSION[$cCode]['items_child'][$id]);

        }

        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);
        fillValues_he_value_builder($jenisTr, $fromStep, $intoStep, $configCoreJenis, $configUiJenis);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "   top.BootstrapDialog.closeAll();
                            top.BootstrapDialog.show(
                                {
                                    title:'Followup preview',
                                    message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreviewMb/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                    size:top.BootstrapDialog.SIZE_WIDE,
                                    draggable:false,
                                    animate:false,
                                    closable:false,
                                    type:top.BootstrapDialog.TYPE_SUCCESS,
                                });";


//        echo "<html>";
//        echo "<head>";
////        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";
//        echo "</body>";
//        echo "</html>";

    }

    public function updateMainField()
    {

//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
//        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
//        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;

        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");


//        $id = $_GET['id'];
        $key = $_GET['key'];
        $val = $_GET['val'];
//        if (isset($_SESSION[$cCode]['items'][$id][$key])) {
//
//            $_SESSION[$cCode]['items'][$id][$key] = $val;
//
//            foreach ($itemNumLabels as $key => $label) {
//                if (isset($_SESSION[$cCode]['items'][$id][$key])) {
//                    $_SESSION[$cCode]['items'][$id]["sub_" . $key] = ($_SESSION[$cCode]['items'][$id][$key] * $_SESSION[$cCode]['items'][$id]["jml"]);
//                }
//
//            }
//            if (isset($_SESSION[$cCode]['items'][$id]['nett'])) {
//                $_SESSION[$cCode]['items'][$id]['sub_nett'] = ($_SESSION[$cCode]['items'][$id]['nett'] * $_SESSION[$cCode]['items'][$id]['jml']);
//            }
//
//            if ($subAmountConfig != null) {
//                $items = $_SESSION[$cCode]['items'];
//                $subtotal = makeValue($subAmountConfig, $items[$id], $items[$id], 0);
//            }
//            else {
//                $subtotal = 0;
//            }
//            $_SESSION[$cCode]['items'][$id]['subtotal'] = ($subtotal);
//        }
//        else {
////            echo(lgShowAlert("NOT replacing $key with $val"));
//        }


        //==recover nilai HARGA master
//        $_SESSION[$cCode]['main']['harga'] = 0;
//        if (sizeof($_SESSION[$cCode]['items']) > 0) {
//            foreach ($_SESSION[$cCode]['items'] as $id => $iSpec) {
//                $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);
//            }
//        }
        if (sizeof($_SESSION[$cCode]['main']) > 0) {
            $_SESSION[$cCode]['main'][$key] = isset($val) ? $val : "";
        }

        $this->load->helper("he_value_builder");
        resetValues_he_value_builder($this->jenisTr, $this->configCoreJenis);
        fillValues_he_value_builder($this->jenisTr, $fromStep, $intoStep, $this->configCoreJenis, $this->configUiJenis, $this->configValuesJenis);
        //        die();

        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . MODUL_PATH . "FollowUp/followupPreview/" . $this->uri->segment(4) . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

    }

    public function patchElement()
    {

//        $jenisTr = $this->uri->segment(3);
//        $cCode = "_TR_" . $jenisTr;
        $elName = $this->uri->segment(4);
        $mdlName = $this->uri->segment(5);

        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");


        $key = isset($_GET['key']) ? $_GET['key'] : "";


        heFetchElement($jenisTr, $elName, $mdlName, $key);


        $rawBuilderURL = isset($_GET['rawBuilderURL']) ? $_GET['rawBuilderURL'] : "";

        $this->load->library("MobileDetect");
        $mb = New MobileDetect();
        $isMob = $mb->isMobile();

        if ($isMob) {
            $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreviewMb/" . $this->jenisTr . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "/" . $this->uri->segment(8) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        closable:true,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
        }
        else {
            $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/" . $this->jenisTr . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "/" . $this->uri->segment(8) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        closable:true,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
            $actionTarget = "";
        }

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }

    }

    public function patchFieldElement()
    {


//        $jenisTr = $this->uri->segment(3);
//        $cCode = "_TR_" . $jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");


        $elName = $this->uri->segment(4);
        $val = ($_GET['val']);
        $elementConfigs = isset($this->config->item('heTransaksi_ui')[$jenisTr]['receiptElements']) ? $this->config->item('heTransaksi_ui')[$jenisTr]['receiptElements'] : array();
        $relElementConfigs = isset($this->config->item('heTransaksi_ui')[$jenisTr]['relativeElements']) ? $this->config->item('heTransaksi_ui')[$jenisTr]['relativeElements'] : array();


        heRecordElement($jenisTr, $elName, $val);

        $rawBuilderURL = $_GET['rawBuilderURL'];

        $this->load->library("MobileDetect");
        $mb = New MobileDetect();
        $isMob = $mb->isMobile();

        if ($isMob) {
            $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreviewMb/" . $this->jenisTr . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "/" . $this->uri->segment(8) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        closable:true,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
        }
        else {
            $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/" . $this->jenisTr . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "/" . $this->uri->segment(8) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        closable:true,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
        }

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }

//        echo "<html>";
//        echo "<head>";
////        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";
//        echo "</body>";
//        echo "</html>";

    }

    public function updateChildField()
    {
//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $id = $_GET['id'];
        $key = $_GET['key'];
        $val = $_GET['val'];
        $key2 = $_GET['x'];

        if (isset($_SESSION[$cCode]['items_child'][$id][$key2][$key])) {
            $_SESSION[$cCode]['items_child'][$id][$key2][$key] = $val;
        }
        else {
//            echo(lgShowAlert("NOT replacing $key with $val"));
        }
        $this->load->helper("he_value_builder");
//        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }

    }

    public function selectElement()
    {
//        $this->jenisTr = $this->uri->segment(3);
        $elID = $this->uri->segment(7);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $elementConfigs = isset($configUiJenis['receiptElements']) ? $configUiJenis['receiptElements'] : array();
        $elementRelConfig = isset($configUiJenis['relativeElements']) ? $configUiJenis['relativeElements'] : array();

        $rawBuilderURL = isset($_GET['rawBuilderURL']) ? $_GET['rawBuilderURL'] : "";

        //region joint array main config denga relative element
        $relConfig = array();
        foreach ($elementConfigs as $elName => $elMain) {
            if (isset($elementRelConfig[$elName])) {
                foreach ($elementRelConfig[$elName] as $relName => $relMain) {
                    foreach ($relMain as $relCode => $relData) {
                        $relConfig[$relCode] = $relData;
                    }

                }
//                cekHEre("");
            }
        }

        //endregion

        $elementConfigs = $elementConfigs + $relConfig;


        //region elements

        $elStr = array();
        $elements = array();

        if (isset($elementConfigs[$elID])) {
            $eName = $elID;
            $eSpec = $elementConfigs[$elID];
            switch ($eSpec['elementType']) {
                case "dataModel":
                    $addStr = "";
                    $editStr = "";
                    $amdlName = $eSpec['mdlName'];
                    $aFilter = isset($eSpec['mdlFilter']) ? $eSpec['mdlFilter'] : array();

                    $elStr[$eName] = "";
                    $this->load->model("Mdls/" . $amdlName);
                    $labelSrc = $eSpec['labelSrc'];
                    $keySrc = $eSpec['key'];
                    $oo = new $amdlName();
                    $addLink = base_url() . "Data/add/" . str_replace("Mdl", "", $amdlName);
                    if (sizeof($aFilter) > 0) {
                        foreach ($aFilter as $filter) {
                            $exFilter = explode("=", $filter);
                            if (sizeof($exFilter) > 1) {
                                if (isset($_SESSION[$cCode]['main'][$exFilter[1]])) {
                                    $oo->addFilter($exFilter[0] . "='" . $_SESSION[$cCode]['main'][$exFilter[1]] . "'");
//                                    $realFilter = $exFilter[0] . "='" . $_SESSION[$cCode]['main'][$exFilter[1]] . "'";
                                    $addLink .= "?reqField=" . $exFilter[0] . "&reqVal=" . $_SESSION[$cCode]['main'][$exFilter[1]];
                                }
                            }
                        }
                    }

                    $addClick = "";
                    $dataAccess = isset($this->config->item('heDataBehaviour')[$amdlName]) ? $this->config->item('heDataBehaviour')[$amdlName] : array(
                        "viewers" => array(),
                        "creators" => array(),
                        "creatorAdmins" => array(),
                        "updaters" => array(),
                        "updaterAdmins" => array(),
                        "deleters" => array(),
                        "deleterAdmins" => array(),
                        "historyViewers" => array(),
                    );
                    $mems = isset($this->session->login['membership']) ? $this->session->login['membership'] : array();
                    if (sizeof($mems) > 0 && sizeof($dataAccess['creators']) > 0) {
                        if (sizeof(array_intersect($mems, $dataAccess['creators'])) > 0) {
                            $addClick = "
                    BootstrapDialog.show(
                                   {
                                        title:'New " . $eSpec['label'] . "',
                                        message: top.$('<div></div>').load('" . $addLink . "?rawBuilderURL=$rawBuilderURL'),
                                        draggable:true,
                                        closable:true,
                                        }
                                        );";
                            $addStr = "<a href='JavaScript:void(0)' class='btn btn-default' onclick=\"$addClick\"><span class='glyphicon glyphicon-plus'></span></a>";
                        }
                    }
//                    if (strlen($realFilter) > 0) {
//                        $oo->addFilter($realFilter);
//                    }
                    $tmpo = $oo->lookupAll()->result();
                    $elPair[$amdlName] = array();
                    $selectorTarget = "'" . base_url() . get_class($this) . "/patchElement/" . $this->jenisTr . "/$eName/$amdlName/" . $this->uri->segment(4) . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "?key='+this.value";
//                        $elStr[$eName] .= "<div class='box'>";
                    $elStr[$eName] .= "<div class='box-body'>";
                    $elStr[$eName] .= "<select class='form-control' onchange=\"document.getElementById('result').src=$selectorTarget;\">";
                    $elStr[$eName] .= "<option value=''>-select-</option>";
                    if (sizeof($tmpo) > 0) {
                        foreach ($tmpo as $row) {
                            $elPair[$amdlName][$row->id] = $row->$labelSrc;
                            $selected = isset($_SESSION[$cCode]['main_elements'][$eName]) && $_SESSION[$cCode]['main_elements'][$eName]['key'] == $row->id ? "selected" : "";
                            $elStr[$eName] .= "<option value='" . $row->$keySrc . "' $selected>" . $row->$labelSrc . "</option>";
                        }
                    }
                    $elStr[$eName] .= "</select>";
                    $elStr[$eName] .= "</div class='box-header'>";

                    $defKey = isset($_SESSION[$cCode]['main_elements'][$eName]['key']) ? $_SESSION[$cCode]['main_elements'][$eName]['key'] : 0;
                    $defValue = "";
                    if (isset($_SESSION[$cCode]['main_elements'][$eName]['key']) && $_SESSION[$cCode]['main_elements'][$eName]['contents']) {
                        if (isset($elementConfigs[$eName]['usedFields']) && sizeof($elementConfigs[$eName]['usedFields']) > 0) {
                            $defValue .= "<table class='table table-condensed no-padding' style='padding:0px;margin:0px;'>";
                            $contents[$eName] = unserialize(base64_decode($_SESSION[$cCode]['main_elements'][$eName]['contents']));
                            foreach ($elementConfigs[$eName]['usedFields'] as $src => $label) {
                                $fieldLabel = isset($contents[$eName][$src]) ? $contents[$eName][$src] : "-";
                                $defValue .= "<tr>";
                                $defValue .= "<td align='left'>$label";
                                $defValue .= "</td>";
                                $defValue .= "<td align='left'>" . $fieldLabel;
                                $defValue .= "</td>";
                                $defValue .= "</tr>";
                            }
                            $defValue .= "</table>";
                        }
                    }

                    $rawBuilderURL = $_GET['rawBuilderURL'];
                    if ($defKey > 0) {
                        if (sizeof($mems) > 0 && sizeof($dataAccess['updaters']) > 0) {
                            $editLink = base_url() . "Data/edit/" . str_replace("Mdl", "", $amdlName) . "/$defKey";
                            if (sizeof(array_intersect($mems, $dataAccess['updaters'])) > 0) {
                                $editClick = "
                    BootstrapDialog.show(
                                   {
                                        title:'New " . $eSpec['label'] . "',
                                        message: top.$('<div></div>').load('" . $editLink . "?rawBuilderURL=$rawBuilderURL'),
                                        draggable:true,
                                        size:BootstrapDialog.SIZE_WIDE,
                                        closable:true,
                                        }
                                        );";

                                $editStr = "<a href='JavaScript:void(0)' class='btn btn-default' onclick=\"$editClick\"><span class='glyphicon glyphicon-pencil'></span></a>";
                            }
                        }
                    }

                    $elStr[$eName] .= "<div id='divel_$eName' style='padding:2px;font-size:smaller;'>$defValue";
                    $elStr[$eName] .= "</div id='el$amdlName'>";
                    $elStr[$eName] .= "<div class='box-footer'>";

                    $elStr[$eName] .= "<span class='pull-right'>$editStr $addStr</span>";
                    $elStr[$eName] .= "</div class='box-footer'>";

                    $elements[] = array(
                        "mdlName" => $eSpec['mdlName'],
                        "label" => $eSpec['label'],
                        "string" => $elStr[$eName],
                    );

                    break;
                case "dataField":
                    $elStr[$eName] = "";
                    $defaultValue = isset($eSpec['defaultValue']) ? $eSpec['defaultValue'] : "";
//                    $selectorTarget = "'" . base_url() . get_class($this) . "/recordFieldElement/" . $this->jenisTr . "/$eName/$amdlName/?val='+this.value";
                    $selectorTarget = "'" . base_url() . get_class($this) . "/patchFieldElement/" . $this->jenisTr . "/$eName/amdlName/" . $this->uri->segment(4) . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "?val='+this.value";
//                        $elStr[$eName] .="<div class='box'>";

                    $elStr[$eName] .= "<div class='box-body'>";
                    switch ($eSpec['inputType']) {
                        case "text":
                            $elStr[$eName] .= "<input type=text class='form-control' value='$defaultValue' onblur=\"document.getElementById('result').src=$selectorTarget;\">";
                            break;
                        case "date":
                            $elStr[$eName] .= "<input type=date class='form-control' value='$defaultValue' onblur=\"document.getElementById('result').src=$selectorTarget;\">";
                            break;
                    }
                    $elStr[$eName] .= "</div class='box-body'>";

                    $elements[] = array(
                        "mdlName" => null,
                        "label" => $eSpec['label'],
                        "string" => $elStr[$eName],
                    );
                    break;
            }
            echo $elStr[$eName];


        }

        //endregion

    }

    public function updateItemFieldOpname()
    {

//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;

        $id = $_GET['id'];
        $key = $_GET['key'];
        $val = $_GET['val'];

        if (isset($_SESSION[$cCode]['items'][$id][$key])) {
            $_SESSION[$cCode]['items'][$id][$key] = $val;

            $stok = $_SESSION[$cCode]['items'][$id]['stok'];
            $selisih = $val - $stok;
            matiHEre();
            if ($selisih > 0) {
                $_SESSION[$cCode]['items'][$id]['qty_debet'] = $selisih;
                $_SESSION[$cCode]['items'][$id]['qty_kredit'] = 0;
            }
            elseif ($selisih < 0) {
                $_SESSION[$cCode]['items'][$id]['qty_debet'] = 0;
                $_SESSION[$cCode]['items'][$id]['qty_kredit'] = ($selisih * -1);
            }
            else {
                $_SESSION[$cCode]['items'][$id]['qty_debet'] = 0;
                $_SESSION[$cCode]['items'][$id]['qty_kredit'] = 0;
            }
            $_SESSION[$cCode]['items'][$id]['qty_selisih'] = $selisih;

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

            }

            if (isset($_SESSION[$cCode]['items'][$id]['nett'])) {
                $_SESSION[$cCode]['items'][$id]['sub_nett'] = ($_SESSION[$cCode]['items'][$id]['nett'] * $_SESSION[$cCode]['items'][$id]['jml']);
            }

            if ($subAmountConfig != null) {
                $items = $_SESSION[$cCode]['items'];
                $subtotal = makeValue($subAmountConfig, $items[$id], $items[$id], 0);
            }
            else {
                $subtotal = 0;
            }

            $_SESSION[$cCode]['items'][$id]['subtotal'] = ($subtotal);


        }
        else {
//            echo(lgShowAlert("NOT replacing $key with $val"));
        }


        //==recover nilai HARGA master
        $_SESSION[$cCode]['main']['harga'] = 0;
        if (sizeof($_SESSION[$cCode]['items']) > 0) {
            foreach ($_SESSION[$cCode]['items'] as $id => $iSpec) {
                $_SESSION[$cCode]['main']['harga'] += ($iSpec['jml'] * $iSpec['harga']);
            }
        }

        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);
        fillValues_he_value_builder($jenisTr, $fromStep, $intoStep, $configCoreJenis, $configUiJenis);


        //region kembalikan ke followupPreview
        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }
        else {
            echo "\n<script>";
            echo "\ntop.$('.kalkulasi_modal').removeClass('hidden');";
//            echo "\ntop.console.error('$rawBuilderURL');";
            echo "\n</script>";
        }

        //endregion

    }

    public function updateSourceField()
    {
//        arrPrint($this->uri->segment_array());
//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $id = $_GET['id'];
        $val = $_GET['val'];


        $_SESSION[$cCode]['main'][$id] = $val;

        $_SESSION[$cCode]['main']["efakturSource"] = isset($_SESSION[$cCode]['main']["efaktur_source"]) ? $_SESSION[$cCode]['main']["efaktur_source"] : $_SESSION[$cCode]['main']["nomer"];


        $this->load->helper("he_value_builder");
//        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreview/$jenisTr/" . $this->uri->segment(4) . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }


    }

    public function editSourceField()
    {

//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $id = $_GET['id'];
        $val = $_GET['val'];

//matiHere($val);
//        if (!isset($_SESSION[$cCode]['main'][$id])) {
        $_SESSION[$cCode]['main'][$id] = $val;
//        }
//        if(!isset($_SESSION[$cCode]['main']['efaktur_source'])){
        $_SESSION[$cCode]['main']["efakturSource"] = isset($_SESSION[$cCode]['main']["efaktur_source"]) ? $_SESSION[$cCode]['main']["efaktur_source"] : $_SESSION[$cCode]['main']["nomer"];
//        }


        $this->load->helper("he_value_builder");
//        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/editMainFaktur/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";
        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }
//        echo "<html>";
//        echo "<head>";
////        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";
//        echo "</body>";
//        echo "</html>";
    }

    public function updatePrice()
    {
        $this->jenisTr = $this->uri->segment(4);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $fields = isset($configUiJenis['itemPrice'][$intoStep]['fields']) ? $configUiJenis['itemPrice'][$intoStep]['fields'] : array();
        $ppnHarga = isset($configUiJenis['itemPrice'][$intoStep]['ppn']) ? $configUiJenis['itemPrice'][$intoStep]['ppn'] : 10;
        $this->load->model("Mdls/MdlCabang");
        $c = new MdlCabang();
        $c->addFilter("toko_id=" . $this->session->login['toko_id']);
        $tnpC = $c->lookUpAll()->result();
//        cekMerah($this->db->last_query());
        $cab = array();
        foreach ($tnpC as $tmp) {
            $cab[$tmp->toko_id] = $tmp->nama;
        }
        $pid = $_GET['pid'];
        $key = $_GET['key'];
        $cid = $_GET['cid'];
        $val = $_GET['val'];
//        arrPrint($_GET);
        // matiHEre();
        $minVal = $_GET['h'] + ($_GET['h'] * 0.025) * 1;
        if ($val < $minVal) {
            $label = htmlspecialchars(blobDecode($_GET['name']));

            $_SESSION[$cCode]['itemPrice'][$cid][$pid][$key] = $val;
            matiHEre("harga jual $label di " . $cab[$cid] . " lebih rendah dari HPP! harga jual harus lebih besar  2,5% dari " . formatField_he_format("nilai", $_GET['h']) . " ( sebesar " . formatField_he_format("nilai", $minVal) . " ) harga yang dimasukan saat ini sebesar (" . formatField_he_format("nilai", $val) . ") silahkan diperbaiki");
        }
//        matiHere($minVal);
//        if (sizeof($_SESSION[$cCode]['itemPrice'][$cid][$pid][$key])){
//            unset($_SESSION[$cCode]['itemPrice'][$cid][$pid][$key]);
//
//        }
//        $_SESSION[$cCode]['itemPrice'][$cid][$pid][$key]=$val;
//        arrPrint($fields);
        foreach ($fields as $i => $col) {
            if ($col == $key) {
                $value = $val;
            }
            else {
                if ($col == "hpp") {
                    $value = $_GET['h'];
                }
                else {
                    $value = $val + $val * ($ppnHarga / 100);
                }
            }

            $_SESSION[$cCode]['itemPrice'][$cid][$pid][$col] = $value;
            $_SESSION[$cCode]["items"][$pid]['price_setting'][$cid][$col] = $value;
        }

        //update komponen item untuk harga jual
        if (isset($_SESSION[$cCode]["items"][$pid])) {
            $_SESSION[$cCode]["items"][$pid][$key] = $val;
            // matiHEre();

        }
        //---------- mereset ulang session itemPrice_sum
        if (isset($_SESSION[$cCode]["items"])) {
            $_SESSION[$cCode]["itemPrice_sum"] = null;
            unset($_SESSION[$cCode]["itemPrice_sum"]);

            $nCtr = 0;
            foreach ($_SESSION[$cCode]["items"] as $pID => $iSpec) {
                if (isset($iSpec['price_setting'])) {
                    foreach ($iSpec['price_setting'] as $cabID => $cSpec) {
                        $nCtr++;
                        $_SESSION[$cCode]["itemPrice_sum"][$nCtr] = $cSpec;
                        $_SESSION[$cCode]["itemPrice_sum"][$nCtr]['id'] = $pID;
                        $_SESSION[$cCode]["itemPrice_sum"][$nCtr]['nama'] = $iSpec['nama'];
                        $_SESSION[$cCode]["itemPrice_sum"][$nCtr]['name'] = $iSpec['nama'];
                        $_SESSION[$cCode]["itemPrice_sum"][$nCtr]['cabang_id'] = $cabID;
                    }
                }
            }
        }


        // matiHere();

        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);
//        fillValues_he_value_builder($this->jenisTr, $fromStep, $intoStep, $this->configCore[$this->jenisTr], $this->configUi[$this->jenisTr]);
        fillValues_he_value_builder($this->jenisTr, $fromStep, $intoStep, $this->configCoreJenis, $this->configUiJenis, $this->configValuesJenis);

        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . MODUL_PATH . "FollowUp/followupPreview/" . $this->uri->segment(4) . "/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";

        if (!isset($_GET['spc'])) {
            echo "<script>$actionTarget</script>";
        }
        else {
            echo "<script>";
            echo "top.$('.kalkulasi_modal').removeClass('hidden');";
            echo "</script>";
        }
//        echo "<html>";
//        echo "<head>";
////        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";
//        echo "</body>";
//        echo "</html>";
    }

    public function updatePriceMb()
    {
//        $this->jenisTr = $this->uri->segment(3);
        $fromStep = $this->uri->segment(6);
        $intoStep = $this->uri->segment(5);
//        $cCode = "_TR_" . $this->jenisTr;
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $fields = isset($configUiJenis['itemPrice'][$intoStep]['fields']) ? $configUiJenis['itemPrice'][$intoStep]['fields'] : array();
        $this->load->model("Mdls/MdlCabang");
        $c = new MdlCabang();
        $tnpC = $c->lookUpAll()->result();
        $cab = array();
        foreach ($tnpC as $tmp) {
            $cab[$tmp->id] = $tmp->nama;
        }
        $pid = $_GET['pid'];
        $key = $_GET['key'];
        $cid = $_GET['cid'];
        $val = $_GET['val'];
        $minVal = $_GET['h'] + ($_GET['h'] * 0.025) * 1;
        if ($val < $minVal) {
            $label = htmlspecialchars(blobDecode($_GET['name']));
            $_SESSION[$cCode]['itemPrice'][$cid][$pid][$key] = $val;

            echo "
            <script>
                var source = top.$('input[name=" . $_GET['pid'] . "_" . $_GET['cid'] . "]');
                top.$(source).css('background-color', '#e428288a');
                top.$(source).val(0);
                top.$(source).on('keyup', function(){
                    top.$(source).css('background-color', '#fff');
                })
            </script>
        ";

            matiHere("harga jual $label di " . $cab[$cid] . " lebih rendah dari HPP! harga jual harus lebih besar  2,5% dari " . formatField_he_format("nilai", $_GET['h']) . " ( sebesar " . formatField_he_format("nilai", $minVal) . " ) harga yang dimasukan saat ini sebesar (" . formatField_he_format("nilai", $val) . ") silahkan diperbaiki");
        }
//        matiHere($minVal);
//        if (sizeof($_SESSION[$cCode]['itemPrice'][$cid][$pid][$key])){
//            unset($_SESSION[$cCode]['itemPrice'][$cid][$pid][$key]);
//
//        }
//        $_SESSION[$cCode]['itemPrice'][$cid][$pid][$key]=$val;
//        arrPrint($fields);
        foreach ($fields as $i => $col) {
            if ($col == $key) {
                $value = $val;
            }
            else {
                if ($col == "hpp") {
                    $value = $_GET['h'];
                }
                else {
                    $value = $val + $val * 10 / 100;
                }
            }

            $_SESSION[$cCode]['itemPrice'][$cid][$pid][$col] = $value;
        }

//        matiHere();

        $this->load->helper("he_value_builder");
        resetValues($this->jenisTr);
//        fillValues($this->jenisTr, $fromStep, $intoStep);
        fillValues_he_value_builder($jenisTr, $fromStep, $intoStep, $configCoreJenis, $configUiJenis);


        $rawBuilderURL = $_GET['rawBuilderURL'];
        $actionTarget = "top.BootstrapDialog.closeAll();top.BootstrapDialog.show(
                                   {
                                       title:'Followup preview',
                                       message: " . 'top.$' . "('<div></div>').load('" . base_url() . "$modul/FollowUp/followupPreviewMb/$jenisTr/" . $this->uri->segment(5) . "/" . $this->uri->segment(6) . "/" . $this->uri->segment(7) . "?rawBuilderURL=$rawBuilderURL'),
                                        size:top.BootstrapDialog.SIZE_WIDE,
                                        draggable:false,
                                        animate:false,
                                        closable:false,
                                        type:top.BootstrapDialog.TYPE_SUCCESS,
                                        }
                                        );";


//        echo "<html>";
//        echo "<head>";
//        echo "<script src=\"" . cdn_suport() . "AdminLTE-2.3.11/plugins/jQuery/jquery-2.2.3.min.js\"></script>";
//        echo "</head>";
//        echo "<body onload=\"$actionTarget\">";

//        arrPrint($_SESSION[$cCode]['itemPrice']);
//        arrPrintWebs($_GET);
//        arrPrintWebs($_POST);

        $valOnSession = $_SESSION[$cCode]['itemPrice'][$_GET['cid']][$_GET['pid']][$_GET['key']];

        echo "
            <script>
                var source = top.$('input[name=" . $_GET['pid'] . "_" . $_GET['cid'] . "]');
                var value = top.$(source).val();

                if(value==top.addCommas($valOnSession)){
                    top.$(source).addClass('text-bold');
                    top.$(source).css('background-color', '#8bc34a87');
                    setTimeout( function(){
                        top.$(source).css('background-color', '#fff');
                    }, 1500);
                }

            </script>
        ";
//        echo "</body>";
//        echo "</html>";

    }

    public function updatediskonjual()
    {
        $this->jenisTr = $this->uri->segment(4);
        $fromStep = $this->uri->segment(7);
        $intoStep = $this->uri->segment(6);
        $cCode = $this->cCode;
        $jenisTr = $this->jenisTr;
        $modul = $this->modul;
        $configCoreJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiCore");
        $configUiJenis = loadConfigModulJenis_he_misc($jenisTr, "coTransaksiUi");

        $itemNumLabels = isset($configUiJenis['shoppingCartNumFields'][$intoStep]) ? $configUiJenis['shoppingCartNumFields'][$intoStep] : array();
        $subAmountConfig = isset($configUiJenis['shoppingCartAmountValue'][$intoStep]) ? $configUiJenis['shoppingCartAmountValue'][$intoStep] : null;
        $items_child = isset($configUiJenis['shopingCartDetailFields'][$intoStep]['fields']) ? $configUiJenis['shopingCartDetailFields'][$intoStep]['fields'] : array();
        $fields = isset($configUiJenis['itemPrice'][$intoStep]['fields']) ? $configUiJenis['itemPrice'][$intoStep]['fields'] : array();
        $ppnHarga = isset($configUiJenis['itemPrice'][$intoStep]['ppn']) ? $configUiJenis['itemPrice'][$intoStep]['ppn'] : 10;
        // $this->load->model("Mdls/MdlCabang");
        // $c = new MdlCabang();
        // $c->addFilter("toko_id=" . $this->session->login['toko_id']);
        // $tnpC = $c->lookUpAll()->result();
        //        cekMerah($this->db->last_query());
        $cab = array();
        // foreach ($tnpC as $tmp) {
        //     $cab[$tmp->toko_id] = $tmp->nama;
        // }

        $pid = $_GET['pid'];
        $key = $_GET['key'];
        $val = $_GET['val'];
        // if(!isset($_SESSION[$cCode]["items"][$pid][$key])){
        //     $_SESSION[$cCode]["items"][$pid][$key]= array();
        // }

        if ($key == "harga_list") {
//        if ($key == "jual") {
            $_SESSION[$cCode]["items3_sum"][$pid] = $_SESSION[$cCode]["items"][$pid];
            $_SESSION[$cCode]["items3_sum"][$pid][$key] = $val;
        }
        else {
            $_SESSION[$cCode]["items"][$pid][$key] = $val;
        }
        // $cekSession =

        // arrPrint($_GET);
        // matiHEre($cCode);
    }

}