<?php
defined('BASEPATH') OR exit('No direct script access allowed');
require_once "Modul_Controller.php";
class Numpad extends Modul_Controller
{
    public function __construct()
    {
        parent::__construct();
        $this->load->model("Mdls/MdlProduk");
        $this->load->model("Mdls/MdlHargaProduk");
        $this->load->model("Mdls/MdlProdukSatuanRelasi");
    }

    public function index()
    {
        if (!isset($this->session->login['id'])) {
            redirect(base_url() . "Login");
        }

        $branchID = $this->session->login['cabang_id'];
        $tokoID = $this->session->login['toko_id'];

        $id     = isset($_GET['id']) ? $_GET['id'] : "";
        $min    = isset($_GET['min']) && $_GET['min']>0 ? $_GET['min'] : 1;
        $cCode  = isset($_GET['cCode']) ? $_GET['cCode'] : "";
        $url    = isset($_GET['url']) ? $_GET['url'] : "";
        $barcodesatuan = isset($_GET['barcodesatuan']) ? $_GET['barcodesatuan'] : "";

        $sa = new MdlProdukSatuanRelasi();
        $sa->addFilter("toko_id='$tokoID'");
        $sa->addFilter("produk_id='$id'");
        $objSa = $sa->lookupAll()->result();
        $satuanResult = array();
        if (sizeof($objSa) > 0) {
            foreach ($objSa as $k => $obj) {
                $satuanResult[$obj->produk_id][$obj->satuan_id] = $obj;
            }
        }

        $p = new MdlProduk();
        $prd = $p->lookupByID($id)->result();

        $mdlName = "MdlHargaProduk";
        $this->load->model("Mdls/MdlHargaProduk");

        $o = new $mdlName();
        $o->addFilter("produk_id='$id'");
        $o->addFilter("cabang_id='$branchID'");
        $o->addFilter("toko_id='$tokoID'");
        $o->addFilter("jenis_value='jual'");
        $tmp = $o->lookupAll()->result();

        //diskon
        $koloms = array(
            "urutan",
            "persen",
            "minim",
            "maxim",
        );

        $this->load->library("Diskon");
        $dk = new Diskon();
        $dk->setTokoId($tokoID);
        $calc_hasil = $src_diskon = $dk->callProdukDiskon();
        $prods = $src_diskon["produk"];
        $grosirs = $src_diskon["grosir"];

        $gross = array();
        foreach ($grosirs as $item) {
            $pro_id = $item["produk_id"];
            foreach ($koloms as $kolom) {
                $items[$kolom] = $item[$kolom] * 1;
            }
            $gross[$pro_id][] = $items;
        }

        $sesJml = isset($_SESSION[$cCode]['items'][$id]['qty_unit']) ? $_SESSION[$cCode]['items'][$id]['qty_unit'] : 0;
        $jml = isset($_SESSION[$cCode]['items'][$id]['qty_unit']) ? $_SESSION[$cCode]['items'][$id]['qty_unit'] : $min;

        $satuan_default = isset($prd[0]->satuan) ? $prd[0]->satuan : "";

        $satuanTxt = "";
        $arrBarcodeSatuan = array();
        if($barcodesatuan==""){
            $satuanTxt .= "<span class='badge bg-green text-uppercase'>$satuan_default</span>&nbsp;&nbsp;";
        }
        else{
            $satuanTxt .= "<span class='badge text-uppercase'>$satuan_default</span>&nbsp;&nbsp;";
            $arrBarcodeSatuan = blobDecode($barcodesatuan);
//            arrPrint($arrBarcodeSatuan);
        }

        if(isset($satuanResult[$id])){
            foreach($satuanResult[$id] as $k => $dSatuan){
                $nama = $dSatuan->satuan_nama;
                $qty = $dSatuan->qty;
                $satuan_id = $dSatuan->satuan_id;
                if(!empty($arrBarcodeSatuan) && $arrBarcodeSatuan['satuan_id'] == $satuan_id ){
                    $satuanTxt .= "<span class='badge bg-green text-uppercase'>$nama: $qty</span>&nbsp;&nbsp;";
                }
                else{
                    $satuanTxt .= "<span class='badge text-uppercase'>$nama: $qty</span>&nbsp;&nbsp;";
                }
            }
        }

        $promo = array();
        foreach ($prods as $pro_id => $proitem) {
            $promo[$pro_id] = isset($gross[$pro_id]) ? $gross[$pro_id] : array();
        }

        $promoTxt="";
        if( isset($promo[$id]) && count($promo[$id])>0 ) {
            $promoTxt .= "<div class='text-bold fa-1x text-red'>Promo Qty: </div>";
            foreach($promo[$id] as $k => $dPromo){
                $disPerc = $dPromo['persen'];
                $minBuy  = $dPromo['minim'];
                $maxBuy  = $dPromo['maxim'];
                $promoTxt .= "<div class='text-bold text-green text-left'>Min Buy $minBuy = $disPerc% </div>";
            }
        }

        $nama = isset($prd[0]->nama) ? $prd[0]->nama : "";
        $hrg = isset($tmp[0]->nilai) ? $tmp[0]->nilai : "";
        $premi_jual = isset($prd[0]->premi_jual) ? $prd[0]->premi_jual : 0;

        if($premi_jual*1>0){
            $hrg = $hrg+($hrg*$premi_jual/100);
        }
        $multisatuan = 0;
        $ms_data = "";
        if(isset($arrBarcodeSatuan['satuan_qty'])){
            $hrg = $hrg * $arrBarcodeSatuan['satuan_qty'];
            $calc_hasil = $this->selectorDiskon($id, $hrg, $arrBarcodeSatuan['satuan_qty']);
            $hrg = $hrg - $calc_hasil['nilai']; // sudah termasuk diskon satuan/grosir
//            arrPrintKuning($calc_hasil);
            $multisatuan = $arrBarcodeSatuan['satuan_qty'];
            $ms_data = blobEncode($arrBarcodeSatuan);
        }

        //belum dipakai
        //$hrg_item = isset($tmp[0]->nilai) ? $tmp[0]->nilai : "";

        //endregion
        $data = array(
            "title"     => "numpad",
            "subTitle"  => "select by numpad",
            "id"        => "$id",
            "curValue"  => $sesJml*1==0 ? 0 : $jml,
            "min"       => "$min",
            "hrg"       => $hrg,
            "nama"      => "$nama",
            "cCode"     => "$cCode",
            "url"       => "$url",
            "promo_qty" => "$promoTxt",
            "satuan"    => "$satuanTxt",
            "multisatuan"    => "$multisatuan",
            "ms_data"    => "$ms_data",
        );
        $this->load->view("numpad", $data);

    }

    private function selectorDiskon($produk_id, $produk_harga, $produk_jml)
    {
        $this->load->library("Diskon");
        $dp = new Diskon();
        $dp->setTokoId(my_toko_id());
        $src_diskon = $dp->CallProdukDiskon($produk_id);

        // diskon
        $pro_harga = $produk_harga * 1;
        $pro_jml = $produk_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'];
                if (($pro_jml >= $gro_minim) && ($pro_jml <= $gro_maxim)) {
                    $d_pokok = $gro_persen;
                    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;
    }
}
