<?php

function _tc($txt, $lang_file = 'customer')
{
    $txt = trim(strtolower($txt));
    $txt = str_replace('  ', ' ', $txt);
    $txt = str_replace(' ', '_', $txt);
    return trans("{$lang_file}.{$txt}");

}

function isHRM()
{
    //return 1;
    return env('HRM', 0) - 0;
}

function isAUTOTRANFER()
{
    //return 1;
    return env('AUTOTRANSFER', 0) - 0;
}

function isSALEAUTONUM()
{
    //return 1;
    return env('SALEAUTONUM', 0) - 0;
}

function isPURCHASEAUTONUM()
{
    //return 1;
    return env('PURCHASEAUTONUM', 0) - 0;
}

function isManufacture()
{
    //return 1;
    return env('Manufacture', 0) - 0;
}

function isSCHOOL()
{
    return env('SCHOOL', 0) - 0;
}

function isLOTTERY()
{
    return env('LOTTERY', 0) - 0;
}

function isLogCode()
{
    return env('IS_LOG_CODE', 0) - 0;
}

function isEDC()
{
    return env('EDC', 0) - 0;
}

function isCHAMNOL()
{
    return env('CHAMNOL', 0) - 0;
}

function isRestaurant()
{
    return env('RESTAURANT', 0) - 0;
}

function getEnvCountry()
{
    return env('COUNTRY', 'KH');
}


function companyReportPart()
{
    return env('COMPANY_REPORT_PART', 'company.system');
}

function accounting()
{
    return env('ACCOUNTING', 0) - 0;
}

function getBaseCurrency()
{
    return \App\Helpers\S::getBaseCurrency();
}

function if_not_null(&$var)
{
    return isset($var) ? ($var == null || $var == '' ? false : true) : false;
}

function isEmpty2($var)
{
    if ($var == null || $var == '' || $var == 0) {
        return true;
    }

    return false;
}


//=================================================
//=================================================
//=================================================
function allMNum(){
    return array(
        '0'=> '၀',
        '1'=> '၁',
        '2'=>'၂',
        '3'=> '၃',
        '4'=>'၄',
        '5'=> '၅',
        '6'=>'၆',
        '7'=>'၇',
        '8'=>'၈',
        '9'=>'၉'
    );
}


function allMAlphabet(){
    return array(

        'a'=> 'က',
        'b'=>'ခ',
        'c'=>'ဂ',
        'd'=>'ဃ',
        'e'=>'င',
        'f'=>'စ',
        'g'=>'ဆ',
        'h'=> 'ဇ',
        'i'=>'စ်',
        'j'=>'ည',
        'k'=>'ဋ',
        'l'=>'ဌ',
        'm'=>'ဍ',
        'n'=>'ဎ',
        'o'=>'ဏ',
        'p'=>'တ',
        'q'=>'ထ',
        'r'=>'ဒ',
        's'=>'ဓ',
        't'=>'န',
        'u'=>'ပ',
        'v'=>'ဖ',
        'w'=>'ဗ',
        'x'=>'ဘ',
        'y'=>'မ',
        'z'=>'ယ',
        '!'=>'ရ',
        '@'=>'လ',
        '*'=>'သ',
        '$'=> '၀',
        '#'=>'ဟ',
        '+'=>'ဠ',
        '-'=>'အ'
    );
}


function str_split_unicode($str, $l = 0) {
    if ($l > 0) {
        $ret = array();
        $len = mb_strlen($str, "UTF-8");
        for ($i = 0; $i < $len; $i += $l) {
            $ret[] = mb_substr($str, $i, $l, "UTF-8");
        }
        return $ret;
    }
    return preg_split("//u", $str, -1, PREG_SPLIT_NO_EMPTY);
}


function mToN($m){
    if($m == '' || $m == null) return '';
    $x =str_split_unicode($m);
    $num = '';
    $b = allMNum();
    $a = allMAlphabet();

    if(is_array($x)){
        $ct = count($x);
        $i = 0;
        foreach($x as $c){
            $i++;

            if(($ct==7 && $i<=1) || ($ct==8 && $i<=2)){
                foreach($a as $k => $v){

                    if($c == $v){
                        $num .= $k;
                    }
                }
            }else{

                foreach($b as $k => $v){

                    if($c == $v){
                        $num .= $k;
                    }
                }
            }


        }
    }

    return $num;

}



function convertUnitL($product_id, $qty, $isBig = 'big')
{

    //dd("hdhjfadjajfda");
    if ($isBig == 'big') {
        return _convertUnitL($product_id, $qty);

    } else {


        return _convertUnitToSmallL($product_id, $qty);

    }

}


function _convertUnitL($product_id, $qty)
{

    $mul = $qty < 0 ? -1 : 1;
    if (abs($qty) > 0) {
        /*$u = \Cache::rememberForever('ProductUnitVariant_'.$product_id, function () use ($product_id){
            return  \App\Models\ProductUnitVariant::where('product_id', $product_id)
                ->orderBy('qty', 'asc')->get();
        });*/

        $u = \App\Models\ProductUnitVariant::where('product_id', $product_id)
            ->orderBy('qty', 'asc')->get();
        $n = abs($qty);
        //dd($n);


        $n = round_up($n, 6);
        //dd($n);

        if ($u != null) {
            if ($u->count() > 0) {
                //dd($u);
                $re = '';
                $i = 0;
                $j = 0;
                $fraction = 0;
                $qty_unit = [];
                foreach ($u as $r_unit) {
                    $qty_unit[$i] = $r_unit->qty;

                    //======================

                    if ($i > 0) {
                        $n = round_up($fraction * $qty_unit[$i], 8);
                        //$whole = floor($n);      // 1
                        $whole = floor($n);// 1


                        //echo "<h1>xx $i === $fraction = $fraction - ($whole / ".$qty_unit[$i].")</h1>";
                        $fraction = $fraction - ($whole / $qty_unit[$i]); // .25
                        //echo "<h1>xx  $i ===  $fraction = $fraction - ($whole / ".$qty_unit[$i].")</h1>";
                    } else {

                        $whole = floor($n);      // 1

                        //echo "<h1>  $i ===  $fraction = $n - $whole</h1>";
                        $fraction = $n - $whole; // .25
                        //echo "<h1> $i ===  $fraction = $n - $whole</h1>";
                    }
                    //=========================
                    //=========================
                    $unit_id = $r_unit->unit_id;

                    /* $unit_name = optional(\Cache::rememberForever('Unit_'.$unit_id, function () use ($unit_id){
                         return  \App\Models\Unit::find($unit_id);
                     }))->title;*/

                    $unit_name = optional(\App\Models\Unit::find($unit_id))->title;


                    /*if($fraction == 0){
                        break;
                    }*/
                    if ($whole > 0) {
                        $re .= ($j > 0 ? ' & ' : '') . ($whole . ' ' .'('.$unit_name.')');
                        $j++;
                    }

                    $i++;
                    //=========================
                    //=========================

                }

                //dd($mul);
                return $mul > 0 ? $re : "({$re})";
            }
        }
    }

    return $qty;
}


function _convertUnitToSmallL($product_id, $qty)
{


    $mul = $qty < 0 ? -1 : 1;
    if (abs($qty) > 0) {
        $u = \Cache::rememberForever('ProductUnitVariant_small_unit_' . $product_id, function () use ($product_id) {
            return \App\Models\ProductUnitVariant::where('product_id', $product_id)
                ->orderBy('qty', 'DESC')->first();
        });


        $n = abs($qty);
        $n = round_up($n, 10);
        if ($u != null) {
            $q_v = $u->qty ?? 1;
            $unit_id = $u->unit_id;
            $unit_name = optional(\Cache::rememberForever('Unit_' . $unit_id, function () use ($unit_id) {
                return \App\Models\Unit::find($unit_id);
            }))->title;
            $qq = round($n * $q_v);
            return ($mul > 0 ? $qq .' '.'('.$unit_name.')': "({$qq} ({$unit_name}))");
        }

    }

    return $qty;
}




