<?php
function isManagement(){
    return true;
    if(auth()->check())
        return (auth()->user()->role =='admin' || auth()->user()->role =='finance');
    else return false;
}

function convertToHoursMins($time, $format = '%02d:%02d') {
    if ($time < 1) {
        return;
    }
    $hours = floor($time / 60);
    $minutes = ($time % 60);
    return sprintf($format, $hours, $minutes);
}
if(env('COMPANY_REPORT_PART', 'company.system') == "company.globeso_company"){
    function __getArrLeave(){
        return [
            'casual_leave' => 'SPEL',
            'medical_leave' => 'SICL',
            'earned_leave' => 'EARN',
            'maternity_leave' => 'MATL'
        ];
    }
    function get_leave_type_arr(){
        return [
            'casual_leave' => 'casual_leave',
            'medical_leave' => 'medical_leave',
            'earned_leave' => 'earned_leave',
            'maternity_leave' => 'maternity_leave'
        ];
    }

    function get_leave_type_rate($leave_type){
        $arr = [
            'casual_leave' => 1,
            'medical_leave' => 1,
            'earned_leave' => 1,
            'maternity_leave' =>  1
        ];

        return isset($arr[$leave_type])?$arr[$leave_type]:0;
    }
}else{

    function __getArrLeave(){
        return [

            'special_leave' => 'SPEL',
            'sick_leave' => 'SICL',
            'maternity_leave' => 'MATL',
            'annual_leave' =>  'ANNL',
            'unpaid_leave' => 'UNPL',
            'other_leave' => 'OTHL',

        ];
    }
    function get_leave_type_arr(){
        return [
            'special_leave' => 'special_leave',
            'sick_leave' => 'sick_leave',
            'maternity_leave' => 'maternity_leave',
            'annual_leave' =>  'annual_leave',
            'unpaid_leave' => 'unpaid_leave',
            'other_leave' => 'other_leave',
        ];
    }

    function get_leave_type_rate($leave_type){
        $arr = [
            'special_leave' => 1,
            'sick_leave' => 1,
            'maternity_leave' =>  1,
            'annual_leave' =>  1,
            'unpaid_leave' => 1,
            'other_leave' => 1
        ];

        return isset($arr[$leave_type])?$arr[$leave_type]:0;
    }
}



//function _emp_l($emp_id){
//
//    $arr = [
//        'annual_leave_paid' => 18,
//        'annual_leave_unpaid' => 18,
//        'short_term_sick_leave' => 7,
//
//        'long_term_sick_leave' => 90,
//        'long_term_sick_leave_half_pay' => 90,
//        'long_term_sick_leave_no_pay' => 90,
//
//        'special_leave' => 7,
//        'leave_without_pay' => 7,
//        'suspend_leave' => 0,
//        'absent_leave' => 0,
//        'maternity_leave' => 0,
//        'working_accident_leave' => 0
//    ];
//
//    $em = \App\Models\EmpLeaveDetail::where('emp_id',$emp_id)
//                        ->whereRaw(" YEAR(leave_date) = YEAR(now()) ")
//                        ->selectRaw('leave_type, sum(day_num) as total_day ')
//                        ->groupBy('leave_type')->get();
//
//
//
//    $arr_em = [];
//    foreach ($arr as $k=>$v){
//        $arr_em[$k] = 0;
//    }
//
//    if(count($em)>0){
//        foreach ($em as $row){
//            $arr_em[$row->leave_type] = $row->total_day;
//        }
//    }
//
//    $arr_em_bal = [];
//    foreach ($arr as $k=>$v){
//        $arr_em_bal[$k] = ($v - $arr_em[$k])>0?($v - $arr_em[$k]):0;
//    }
//
//    $annual_leave = $arr['annual_leave_paid'] - $arr_em['annual_leave_paid'] - $arr_em['annual_leave_unpaid'] - $arr_em['special_leave'] - $arr_em['leave_without_pay'];
//
//    $arr_em_bal['annual_leave_paid'] = $annual_leave;
//    $arr_em_bal['annual_leave_unpaid'] = $annual_leave;
//
//    return [
//        'leave_year' => $arr,
//        'leave_bal' => $arr_em_bal,
//        'leave' => $arr_em
//    ];
//
//}




function getUser()
{
    $getUser = Auth::getUser();

    return $getUser;
}

function uid()
{
    $u = getUser();
    return isset($u->id) ? $u->id : 0;
}


/*function dateAdd($date, $unit, $num_unit)
{
    return \App\Helpers\IDate::dateAdd($date, $unit, $num_unit);
}*/

function dateDiff($f_date, $t_date)
{
    return \App\Helpers\IDate::dateDiff($f_date, $t_date);
}

function getDayName($date)
{
    return \App\Helpers\IDate::getDayName($date);
}

function getDayNameKH($date)
{
    return \App\Helpers\IDate::getDayNameKH($date);
}

function dateNext($date, $num, $exDaya = [])
{
    return \App\Helpers\IDate::dateNext($date, $num, $exDaya);
}

function dateNext15($date, $num, $exDaya = [])
{
    return \App\Helpers\IDate::dateNext15($date, $num, $exDaya);
}


function convert_number_to_words($number)
{
    return \App\Helpers\ITPC::convert_number_to_words($number);
}

function convert_number_to_words_kh($number)
{
    return \App\Helpers\ITPC::convert_number_to_words_kh($number);
}

function xContains($mystring, $findme)
{
    return strpos($mystring, $findme) !== false;
}


function _tt($txt, $lang_file = 'report')
{
    //return $txt;
    $txt = trim(strtolower($txt));
    $txt = str_replace('  ', ' ', $txt);
    $txt = str_replace(' ', '_', $txt);
    return trans("{$lang_file}.{$txt}");
    //return $txt;
    //return \App\Helpers\itpc::translate($txt, $lang);
}




function get_number_of_days_in_month($month, $year)
{
    // Using first day of the month, it doesn't really matter
    $date = $year . "-" . $month . "-1";
    return date("t", strtotime($date));
}

function getInvFormat($n, $date, $pre = 'INV')
{
    return ' ' . $pre . '-' . \Carbon\Carbon::parse($date)->format('dmY')
        . '-' . str_pad($n, 7, '0', STR_PAD_LEFT);
}

function can($t)
{
    return true;
    return \App\Helpers\ITPC::can($t);
}


function cal_salary_tax($cross_salary_r, $n_spouse = 0, $n_children = 0)
{
    $st = 0;
    $allowance = ($n_spouse + $n_children) * 150000;
    $tax_rate = 0;
    $cross_salary_r = $cross_salary_r - $allowance;
//    $m = 10^6;
    $m = 1000000;

    if ($cross_salary_r > 0 && $cross_salary_r <= $m) {
        $st = 0;
        $tax_rate = 0;
    } elseif ($cross_salary_r > $m && $cross_salary_r <= 1.5 * $m) {
        $st = $cross_salary_r * 5 / 100 - 50000;
        $tax_rate = 5;
    } elseif ($cross_salary_r > 1.5 * $m && $cross_salary_r <= 8.5 * $m) {
        $st = $cross_salary_r * 10 / 100 - 125000;
        $tax_rate = 10;
    } elseif ($cross_salary_r > 8.5 * $m && $cross_salary_r <= 12.5 * $m) {
        $st = $cross_salary_r * 15 / 100 - 550000;
        $tax_rate = 15;
    } else {
        $st = $cross_salary_r * 20 / 100 - 1175000;
        $tax_rate = 20;
    }

    return ['allowance' => $allowance, 'tax_rate' => $tax_rate, 'tax_on_salary' => $st];

}


function time_elapsed_string($datetime, $full = false)
{
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}


function _c($amt, $c = 'USD')
{

    if ($c == 'KHR' || $c == '៛') {
        return (ceil($amt / 100)) * 100 > 500 ? (ceil($amt / 100)) * 100 : 0;
    } else {
        return $amt;
    }

}

function _cFormat($amt, $c = 'USD')
{

    if ($c == 'KHR' || $c == '៛') {
        return number_format((ceil($amt / 100)) * 100 > 500 ? (ceil($amt / 100)) * 100 : 0, 0);
    } else {
        return number_format($amt, 2);
    }




}



function getPriceGroup2($product_id, $unit_id, $price_group_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{
    $price_group = \App\Models\ProductPriceGroupLottery::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->first();



   // return 2;

    if ($price_group != null) {

        //dd("hello world");
        return exchangeRate($from_currency_id, $to_currency_id, $price_group->price);



    } else {
        $product = \App\Models\ProductLottery::find($product_id);

        $product_price = $product->product_price;
        $unit_sale_price = $product->default_sale_unit;
        $price_unit = convertPriceUnit($unit_sale_price, $unit_id, $product_price, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $price_unit);

        return 0;
    }
    return 0;
}



function getCostGroup2($product_id, $unit_id, $price_group_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{
    $cost_group = \App\Models\ProductPriceGroupLottery::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->first();
    if ($cost_group != null) {

        return exchangeRate2($from_currency_id, $to_currency_id, $cost_group->cost);
    } else {
        $product = \App\Models\ProductLottery::find($product_id);
        $product_cost = $product->purchase_cost;

        $unit_sale_cost = $product->default_purchase_unit;
        $cost_unit = convertPriceUnit2($unit_sale_cost, $unit_id, $product_cost, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $cost_unit);

    }
    return 0;
}


function exchangeRate2($from_currency_id, $to_currency_id, $amount)
{

    if ($from_currency_id == $to_currency_id) {
        return $amount;
    } else {
        $from_exchange = optional(\App\Models\Currency::find($from_currency_id))->exchange_rate ?? 1;
        $to_exchange = optional(\App\Models\Currency::find($to_currency_id))->exchange_rate ?? 1;

        if ($from_exchange > 0) {
            $price = ($amount / $from_exchange) * $to_exchange;
            return $price ?? 0;
        }
        return $amount;
    }

}

function convertPriceUnit2($from_unit, $to_unit, $price, $product_id = 0)
{
    if ($from_unit == $to_unit) {
        return $price;
    } else {
        $var_unit_from = optional(\App\Models\ProductUnitVariantU::where('unit_id', $from_unit)
                ->where(function ($q) use ($product_id) {
                    if ($product_id > 0) {
                        return $q->where('product_id', $product_id);
                    }
                })
                ->first())->qty ?? 1;
        $var_unit_to = optional(\App\Models\ProductUnitVariantU::where('unit_id', $to_unit)
                ->where(function ($q) use ($product_id) {
                    if ($product_id > 0) {
                        return $q->where('product_id', $product_id);
                    }
                })
                ->first())->qty ?? 1;

        //dd($var_unit_from,$var_unit_to);

        if ($var_unit_from > 0 && $var_unit_to > 0) {
            $p = ($price * $var_unit_from) / $var_unit_to;
            return $p ?? 0;
        }
        return $price;
    }
}




function getPriceGroupWarehouse($product_id, $unit_id, $price_group_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{
    $price_group = \App\Models\ProductPriceGroup::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->first();

    if ($price_group != null) {
        return exchangeRate($from_currency_id, $to_currency_id, $price_group->price);
    } else {
        $product = \App\Models\Product::find($product_id);

        $product_price = $product->product_price;
        $unit_sale_price = $product->default_sale_unit;
        $price_unit = convertPriceUnit($unit_sale_price, $unit_id, $product_price, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $price_unit);

        return 0;
    }
    return 0;
}

function getCostGroupWarehouse($product_id, $unit_id, $price_group_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{
    $cost_group = \App\Models\ProductPriceGroup::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->first();
    if ($cost_group != null) {

        return exchangeRate2($from_currency_id, $to_currency_id, $cost_group->cost);
    } else {
        $product = \App\Models\Product::find($product_id);
        $product_cost = $product->purchase_cost;

        $unit_sale_cost = $product->default_purchase_unit;
        $cost_unit = convertPriceUnit2($unit_sale_cost, $unit_id, $product_cost, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $cost_unit);

    }
    return 0;
}




function getPriceGroupByRound($product_id, $unit_id, $price_group_id,$round_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{

    $price_group = \App\Models\ProductPriceGroupLottery::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->where('round_id',$round_id)->first();


    ///dd($price_group);


    if ($price_group != null) {

        //dd("hello world");
        return exchangeRate($from_currency_id, $to_currency_id, $price_group->price);



    } else {
        $product = \App\Models\ProductLottery::find($product_id);

        $product_price = $product->product_price;
        $unit_sale_price = $product->default_sale_unit;
        $price_unit = convertPriceUnit($unit_sale_price, $unit_id, $product_price, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $price_unit);

        return 0;
    }
    return 0;
}



function getCostGroupByRound($product_id, $unit_id, $price_group_id,$round_id, $from_currency_id = null, $to_currency_id = null, $date = null)
{
    $cost_group = \App\Models\ProductPriceGroup::where('product_id', $product_id)->where('unit_id', $unit_id)
        ->where('price_group_id', $price_group_id)->where('round_id',$round_id)->first();


    if ($cost_group != null) {

        return exchangeRate2($from_currency_id, $to_currency_id, $cost_group->cost);
    } else {
        $product = \App\Models\Product::find($product_id);
        $product_cost = $product->purchase_cost;

        $unit_sale_cost = $product->default_purchase_unit;
        $cost_unit = convertPriceUnit2($unit_sale_cost, $unit_id, $product_cost, $product_id);
        return exchangeRate($from_currency_id, $to_currency_id, $cost_unit);

    }
    return 0;
}





