<?php

namespace App\Helpers;

use App\HrOvertimeRequestModel;
use App\Models\EmLeaveType;
use App\Models\EmpAllowance;
use App\Models\EmpFixedHoliday;
use App\Models\EmpIncentive;
use App\Models\EmpLeave;
use App\Models\EmpLeaveDetail;
use App\Models\EmpLongSickLeave;
use App\Models\Employee;
use App\Models\EmpMaternity;
use App\Models\EmpMcclaim;
use App\Models\HolidaySchedule;

use App\HrExchangeRateModel;
use App\Models\ManualRequstOT;
use App\Models\Policies;
use App\Models\RequestOt;
use App\Models\TimeAttend;
use App\Models\TimeAttendGen;
use Carbon\Carbon;
use Illuminate\Support\Facades\DB;

class IDate
{

    /**
     * Returns every date between two dates as an array
     * @param string $startDate the start of the date range
     * @param string $endDate the end of the date range
     * @param string $format DateTime format, default is Y-m-d
     * @return array returns every date between $startDate and $endDate, formatted as "Y-m-d"
     * Ex:  createDateRange("2015-01-01", "2015-02-05");
     */


    public static function getHolidayAmt($startDate, $endDate)
    {

        $hol_amt = 0;
        $_range = self::createDateRange($startDate, $endDate, null);
        $range = [];
        if (count($_range) > 0) {

            foreach ($_range as $date) {
                $m = \DB::select("select * from `holiday_schedules` 
                                where `start_date` >= '{$date->format("Y-m-d")}' and `end_date` <= '{$date->format("Y-m-d")}' order by `end_date` desc");

                if ($m != null) {
                    $hol_amt++;
                }
            }
        }

        return $hol_amt;
    }

    public static function percentageOf( $number, $everything, $decimals = 2 ){
        return round( $number / $everything * 100, $decimals );
    }
    public static function conVertminTOHour($minutes)
    {
        return date('H:i', mktime(0, $minutes));
    }

    public static function createDateRange($startDate, $endDate, $format = "Y-m-d")
    {
        $begin = new \DateTime($startDate);

        $_end = self::dateAdd($endDate, UnitDay::DAY, 1);

        $end = new \DateTime($_end);

        $interval = new \DateInterval('P1D'); // 1 Day
        $dateRange = new \DatePeriod($begin, $interval, $end);

        $range = [];
        foreach ($dateRange as $date) {
            if ($format == null) {
                $range[] = $date;
            } else {
                $range[] = $date->format($format);
            }

        }


        return $range;
    }

    static function getTotalLeave($emp_id, $leave_type, $year)
    {
        $total_leave = EmLeaveType::where('emp_id', $emp_id)
            ->where('leave_year', $year)->first();
       // dd($total_leave, $leave_type);
        if ($total_leave != null) {
            return isset($total_leave->{$leave_type}) ? $total_leave->{$leave_type} : 0;
        } else {
            return 0;
        }
    }

    static function getTotalUsedLeave($emp_id, $leave_type, $year)
    {
        $used_leave = \App\Models\EmpLeaveDetail::where('emp_id', $emp_id)
            ->where('leave_type', $leave_type)
            ->whereYear('leave_date', $year)
            ->sum('day_num');
        return $used_leave;
    }

    public static function getHol($date, $sun_day)
    {
        $hol = HolidaySchedule::get();
        $att_date = \Carbon\Carbon::parse($date)->format('Y-m-d');

        $styles_hol = '';
        foreach ($hol as $row_hol) {
            $start_date = \Carbon\Carbon::parse($row_hol->start_date)->format('Y-m-d');
            $end_date = \Carbon\Carbon::parse($row_hol->end_date)->format('Y-m-d');

            if ($att_date >= $start_date AND $att_date <= $end_date AND $sun_day != 'Sunday') {
                $styles_hol = 'color:green;';
            }
        }

        return $styles_hol;
    }

    public static function createDateRangeIncHoliday($startDate, $endDate, $format = "Y-m-d")
    {
        $_range = self::createDateRange($startDate, $endDate, null);
        $range = [];

        if (count($_range) > 0) {
            foreach ($_range as $date) {
                $type = 'n';

                if ($date->format('D') == 'Sun' || $date->format('D') == 'Sat') {
                    $type = 'w';
                } else {

                    $m = HolidaySchedule::where('end_date', '>=', $date->format('Y-m-d'))
                        ->where('start_date', '<=', $date->format('Y-m-d'))
                        ->orderBy('end_date', 'DESC')
                        ->first();

                    if ($m != null) {
                        $type = 'h';
                    }

                }
                $range[] = [
                    'date' => $format == null ? $date : $date->format($format),
                    'type' => $type
                ];
            }
        }

        return $range;
    }

    public static function createDateRangeIncHoliday_Sat_Sun($startDate, $endDate, $format = "Y-m-d")
    {
        $_range = self::createDateRange($startDate, $endDate, null);
        $range = [];

        if (count($_range) > 0) {
            foreach ($_range as $date) {

                $type = 'n';

                if ($date->format('D') == 'Sun' || $date->formar('D') == "Sat") {
                    $type = 'w';
                } else {

                    $m = HolidaySchedule::where('end_date', '>=', $date->format('Y-m-d'))
                        ->where('start_date', '<=', $date->format('Y-m-d'))
                        ->orderBy('end_date', 'DESC')
                        ->first();

                    if ($m != null) {
                        $type = 'h';
                    }

                }
                $range[] = [
                    'date' => $format == null ? $date : $date->format($format),
                    'type' => $type
                ];
            }
        }

        return $range;
    }

    public static function getHolidayOnly($startDate, $endDate, $format = "Y-m-d")
    {
        $_range = self::createDateRange($startDate, $endDate, null);
        $range = [];
        $hol_amt = 0;
        if (count($_range) > 0) {

            foreach ($_range as $date) {

                $type = 'n';

                if ($date->format('D') == 'Sun') {
                    $type = 'w';
                } else {

                    $m = HolidaySchedule::where('end_date', '>=', $date->format('Y-m-d'))
                        ->where('start_date', '<=', $date->format('Y-m-d'))
                        ->orderBy('end_date', 'DESC')
                        ->first();

                    if ($m != null) {
                        $type = 'h';
                        $hol_amt++;
                    }

                }
                $range[] = [
                    'date' => $format == null ? $date : $date->format($format),
                    'type' => $type
                ];
            }
        }

        return $hol_amt;
    }

    public static function getNotHolidayAmt($startDate, $endDate, $emp_id)
    {
        $hol_amt = 0;
        $_range = self::createDateRange($startDate, $endDate, null);
        $range = [];
        if (count($_range) > 0) {

            foreach ($_range as $date) {

                $m = \DB::select("select * from `holiday_schedules` 
                                where `end_date` >= '{$date->format("Y-m-d")}' and `start_date` <= '{$date->format("Y-m-d")}'
                                and `include` = 1 order by `end_date` desc  
                                 ");


                if ($m != null) {
//                    $_data = \DB::select("SELECT  total as ot   FROM em_checkinoutot
//                                                      WHERE DATE(normal_CHECKTIME) BETWEEN '{$date->format("Y-m-d")}' AND '{$date->format("Y-m-d")}' AND
//                                                    emp_id = '{$emp_id}'
//                                                     ");
                    $em = TimeAttendGen::where('emp_id', $emp_id)
                        ->whereDate('att_date', $date->format("Y-m-d"))->first();

                    if ($em != null) {
                        if ($em->cin != null && $em->cout != null) {
                            $hol_amt++;
                        }

                    }

                }
            }
        }
        return $hol_amt;
    }

    public static function mcclaim($emp_id, $f_y, $f_m)
    {
        $mc = EmpMcclaim::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();
        $total = $mc != null ? $mc->amount : 0;
        return $total;
    }

    public static function ajustment($emp_id, $f_y, $f_m)
    {

        $ajust_sum = EmpAjustment::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $total_sum = 0;
        $total_minus = 0;
        if ($ajust_sum != null) {

            if ($ajust_sum->type == 1) {
                $total_sum = $ajust_sum->amount;
            } else if ($ajust_sum->type == 0) {
                $total_minus = $ajust_sum->amount;
            }
        }

        // $total_minus = $ajust_sum!=null  && $ajust_sum->type == 0 ? $ajust_sum->amount : 0;

        return ['total_sum' => $total_sum, "total_minus" => $total_minus];

    }

    public static function allowance($emp_id, $f_y, $f_m)
    {

        $phone = EmpAllowance::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $total = $phone != null ? $phone->amount : 0;

        return $total;

    }

    public static function getTotalMonth($from_date, $to_date)
    {
        // SELECT TIMESTAMPDIFF(MONTH, '2015-12-20', '2015-12-25') + 1 as t_m
        $sql = "SELECT TIMESTAMPDIFF(MONTH,'{$from_date}','{$to_date}') + 1 as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function fixedHoliday($emp_id, $f_y, $f_m)
    {

        $phone = EmpFixedHoliday::where('emp_id', $emp_id)->where('f_year', $f_y)
            ->where('f_month', $f_m)->where('type', 'sun')->first();
        $phone1 = EmpFixedHoliday::where('emp_id', $emp_id)->where('f_year', $f_y)
            ->where('f_month', $f_m)->where('type', 'hol')->first();
        $hol_amt = $phone1 != null ? $phone1->amount : 0;
        $sun_amt = $phone != null ? $phone->amount : 0;
        return ["hol" => $hol_amt, "sun" => $sun_amt];

    }

    public static function incentive($emp_id, $f_y, $f_m)
    {

        $phone = EmpIncentive::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $total = $phone != null ? $phone->amount : 0;

        return $total;

    }

    public static function getServerPay($emp_id, $dates)
    {
        $st_date = self::getBackMonth($dates);
        $month = self::getMonth($st_date);
        $year = self::getYear($st_date);

        $gross_pay = 0;
        $arr = array();
        for ($i = 1; $i <= 2; $i++) {
            if ($month > 12) {
                $month = 1;
                $year = $year + 1;

            }

            $sal = GenSalary::where('emp_id', $emp_id)
                ->where('f_year', $year)
                ->where('f_month', $month)
                ->first();
            $gross_pay += $sal != null ? $sal->total_salary : 0;
            $month++;
        }
        return $gross_pay;
    }

    public static function deductMeal($emp_id, $f_y, $f_m)
    {

        $meals = EmpDeductMeal::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $meal_sum = 0;
        $meal_minus = 0;

        if ($meals != null) {
            if ($meals->type == 1) {
                $meal_sum = $meals->total;
            } else if ($meals->type == 0) {
                $meal_minus = $meals->total;
            }

        }
        return ['meal_sum' => $meal_sum, "meal_minus" => $meal_minus];
    }

    public static function dateAdd($date, $unit, $num_unit)
    {
        $sql = "SELECT DATE_ADD('{$date}', INTERVAL {$num_unit} {$unit}) as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function nextMonth($month, $num, $date)
    {
        $sql = "SELECT DATEADD({$month}, {$num}, {$date}) AS d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function dateDiff($f_date, $t_date)
    {
        $sql = "SELECT DATEDIFF('{$t_date}', '{$f_date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function dateTimeDiff($f_date, $t_date)
    {
        $sql = "SELECT TIMESTAMPDIFF(MINUTE,'{$f_date}', '{$t_date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function dateDiffFromNow($date)
    {
        $sql = "SELECT DATEDIFF('{$date}', now()) as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function dateNext($date, $num, $exDaya = [], $ifirst = 0)
    {
        if (count($exDaya) == 0) {
            $exDaya = ['saturday', 'sunday'];
        }

        if ($ifirst > 0) {
            $d = self::dateAdd($date, UnitDay::DAY, $num);
        } else {
            $d = self::dateAdd($date, UnitDay::MONTH, $num);
        }

        $m = HolidaySchedule::where('end_date', '>=', $d)
            ->where('start_date', '<=', $d)
            ->orderBy('end_date', 'DESC')
            ->first();

        if ($m != null) {
            $d = self::dateNext($m->end_date, 1, $exDaya, 10);
        }

        $dn = strtolower(getDayName($d));

        if (in_array($dn, $exDaya)) {
            $d = self::dateNext($d, 1, $exDaya, 10);
        }

        return Carbon::parse($d);
    }


    public static function dateNext15($date, $num, $exDaya = [], $ifirst = 0)
    {
        if (count($exDaya) == 0) {
            $exDaya = ['saturday', 'sunday'];
        }

        if ($ifirst > 0) {
            $d = self::dateAdd($date, UnitDay::DAY, $num);
        } else {
            $d = self::dateAdd($date, UnitDay::DAY, 15 * $num);
        }

        $m = HolidaySchedule::where('end_date', '>=', $d)
            ->where('start_date', '<=', $d)
            ->orderBy('end_date', 'DESC')
            ->first();

        if ($m != null) {
            $d = self::dateNext($m->end_date, 1, $exDaya, 10);
        }

        $dn = strtolower(getDayName($d));

        if (in_array($dn, $exDaya)) {
            $d = self::dateNext($d, 1, $exDaya, 10);
        }

        return Carbon::parse($d);
    }


    public static function getDayName($date)
    {
        $sql = "SELECT DAYNAME('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }


    public static function getYear($date)
    {
        $sql = "SELECT YEAR('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function getMonth($date)
    {
        $sql = "SELECT MONTH('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function getLastDayMonth($date)
    {
        $sql = "SELECT LAST_DAY('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function getDayFullMonth($date)
    {
        $sql = "SELECT DAY(LAST_DAY('{$date}')) as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return 0;
        }
    }

    public static function getFirstYear($date)
    {
        $sql = "SELECT year('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d . "/1/1";
        } else {
            return null;
        }
    }

    public static function getMonthName($date)
    {
        $sql = "SELECT MONTHNAME('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function getMonthNameS($date)
    {
        $sql = "SELECT DATE_FORMAT('{$date}', '%b-%y')  as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d;
        } else {
            return null;
        }
    }

    public static function getLastYear($date)
    {
        $sql = "SELECT year('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d . "/12/31";
        } else {
            return null;
        }
    }


    public static function getDayNameKH($date)
    {
        $days = array(
            'sunday' => 'អាទិត្យ',
            'monday' => 'ចន្ទ',
            'tuesday' => 'អង្គារ',
            'wednesday' => 'ពុធ',
            'thursday' => 'ព្រហស្បតិ៍',
            'friday' => 'សុក្រ',
            'saturday' => 'សៅរ៍'
        );

        $sql = "SELECT DAYNAME('{$date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $days[strtolower($d[0]->d)];
        } else {
            return null;
        }
    }


    public static function dateComp($f_date, $t_date)
    {
        $sql = "SELECT ('{$f_date}' <= '{$t_date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d > 0;
        } else {
            return false;
        }
    }

    public static function dateCompNe($f_date, $t_date)
    {
        $sql = "SELECT ('{$f_date}' < '{$t_date}') as d";
        $d = DB::select($sql);
        if (count($d) > 0) {
            return $d[0]->d > 0;
        } else {
            return false;
        }
    }

    public static function generateTimeAttendShinhan($emp_id = NULL, $from_date = NULL, $start_date = NULL, $policy_id = 1)
    {

        // dd($emp_id,$from_date,$start_date,$policy_id);;
        $dates = self::createDateRange($from_date, $start_date);

        if ($emp_id > 0 && $policy_id == 1) {
            foreach ($dates as $date) {

                TimeAttendGen::where('emp_id', $emp_id)
                    ->whereDate('att_date', $date)
                    ->delete();

                //==============================
                $policy_id = 1;
                $date_loop_fr = $date;

                $s_day = getOne("SELECT DAYNAME ('" . $date . "')");
                if ($policy_id == 1) {

                    $attend = TimeAttend::select(DB::raw('MIN(attend) AS minDate, MAX(attend) AS maxDate'))
                        ->where('emp_id', $emp_id)
                        ->whereDate('attend', $date)->first();


                    if ($attend != null) {
                        $policy = Policies::where('id', $policy_id)->first();
                        if (is_array($policy->details) || is_object($policy->details)) {
                            $polic_time = $policy->details;
                        } else {
                            $polic_time = json_decode($policy->details);
                        }
                        if (is_array($policy->details)) {
                            $win = $date . ' ' . $polic_time['win'];
                            $wout = $date . ' ' . $polic_time['wout'];
                            $fin = $date . ' ' . $polic_time['fin'];
                        } else {
                            $win = $date . ' ' . $polic_time->win;
                            $wout = $date . ' ' . $polic_time->wout;
                            $fin = $date . ' ' . $polic_time->fin;

                        }
                        //  dd($fin);
                        $in = $attend->minDate;
                        $out = $attend->maxDate;
                        //==========================
                        //==========================
//                        $att_ot = RequestOt::whereDate('ot_date',$date)
////                        ->whereRaw(
////                            'JSON_CONTAINS(emp_id, \'["' . $emp_id . '"]\')'
////                        )
//                            ->where('emp_id','like', '%'.$emp_id.'%')
//                            ->first();
                        $in_ot = $win;
                        $out_ot = $wout;
//                        if($att_ot != null){
//                            $in_ot = $date . ' ' . $att_ot->att_time_in;
//                            $out_ot = $date . ' ' . $att_ot->att_time_out;
//                        }
                        //==========================
                        //==========================

                        $h_d_rate = getOne("SELECT DISTINCT id  FROM   holiday_schedules
                                            WHERE DATE(start_date) <= DATE ('{$in_ot}')
                                            AND DATE(end_date) >= DATE ('{$in_ot}') LIMIT 1 ");


                        if (self::dateTimeDiff($in, $fin) <= 0) {
                            $in = NULL;
                        }
                        if (self::dateTimeDiff($fin, $out) <= 0) {
                            $out = NULL;
                        }

                        //=============== For Request OT
//                        if (self::dateTimeDiff($in_ot, $win) <= 0) {
//                            $in_ot = $win;
//                        }
//                        if (self::dateTimeDiff($fin, $out_ot) <= 0) {
//                            $out_ot = NULL;
//                        }
                        //=============== End Request OT
                        $total = 0;
                        //dd($in,$out_ot);

                        if ($s_day == 'Sunday' || $s_day == 'Saturday' || $h_d_rate > 0) {
                            $total_ot = self::dateTimeDiff($in, $out) - 120;
                            //$total_ot = $total_ot_ - 0;
                            $in = $in_ot;
                            $out = $out_ot;
                            $total = 0;
                            $late = 0;
                            $ot = 0;
                            $before = 0;
                            $bot = 0;// ($bot>=60?$bot:0);
                            $ot_type = $s_day == 'Sunday' || $s_day == 'Saturday' ? 'otsun' : 'othol';
                            $ot_sun = $s_day == 'Sunday' || $s_day == 'Saturday' ? $total_ot : 0;
                            $ot_hol = $s_day != 'Sunday' && $s_day != 'Saturday' ? $total_ot : 0;
                        } else {

                            if ($in != NULL || $out != NULL) {
                                $rin = $in;
                                $rout = $out;
                                $bot = 0;
                                $ot = 0;
                                $ot_hol = 0;
                                $ot_sun = 0;

                                $diffIn = self::dateTimeDiff($in, $win);
                                $diffOut = self::dateTimeDiff($wout, $out);
                                //$diffOut = self::dateTimeDiff($in,$win);

                                $late = 0;
                                $before = 0;

                                if ($diffIn >= 0) {
                                    $rin = $win;
                                    $bot = $diffIn;

                                } else {
                                    $late = (-1) * $diffIn;
                                }
                                if ($diffOut >= 0) {
                                    $rout = $wout;
                                    $ot = $diffOut;
                                } else {
                                    $before = (-1) * $diffOut;
                                }


                                $ot_type = ($bot + $ot) >= 60 ? 'ot' : '';

                                $total = self::dateTimeDiff($rin, $rout) - 60;
                            }

                        }
                        // full time if they came to work
                        if ($in_ot >= $in || $out_ot <= $out) {
                            $diffInOut = self::dateTimeDiff($in, $out);
                            if ($diffInOut != null) {
                                $in = $in_ot;
                                $out = $out_ot;
                            }
                            $total = self::dateTimeDiff($in, $out) - 60;
                        }

//                        $total_l =0;
//                        $take_leave = EmpLeaveDetail::where('emp_id',$emp_id)
//                            ->whereRaw("DAYNAME(leave_date) NOT IN ('Saturday','Sunday') ")
//                            ->whereDate('leave_date','<=',$in_ot)
//                            ->whereDate('leave_date','<=',$out_ot)
//                            ->first();
//                        if($take_leave->day_num != null) {
//                            if ($take_leave->day_num > 0) {
//                                $diffInOut = self::dateTimeDiff($in, $out);
//                                if ($diffInOut != null) {
//                                    $in = $in_ot;
//                                    $out = $out_ot;
//                                }
//                                $total_l = self::dateTimeDiff($in, $out) - 60;
//                            }
//                        }


                        TimeAttendGen::where('emp_id', $emp_id)
                            ->whereDate('att_date', $date)->delete();
                        $m = TimeAttendGen::where('emp_id', $emp_id)
                            ->whereRaw(" (date(cin) = '{$date}' OR  date(cout) = '{$date}')")
                            ->first();

                        if ($m == null) {
                            $m = new TimeAttendGen();
                        }
                        $m->emp_id = $emp_id;
                        $m->att_date = $date;
                        if ($in) {
                            $m->cin = $in;
                        }
                        if ($out) {
                            $m->cout = $out;
                        }
                        if ($in && $out) {

                            $m->total = $total;
                            $m->late = $late;
                            $m->before = $before;
                            $m->bot = $bot;// ($bot>=60?$bot:0);
                            $m->ot = $ot;//($ot>=60?$ot:0);
                            $m->ot_type = $ot_type;//($ot>=60?$ot:0);
                            $m->total_ot = (($bot + $ot) >= 60 ? ($bot + $ot) : 0);
                            $m->total_sun = $ot_sun >= 60 ? $ot_sun : 0;
                            $m->total_hol = $ot_hol >= 60 ? $ot_hol : 0;
                        }
                        if ($in == NULL || $out == NULL) {
                            $m->missing = 1;
                        }
                        $m->save();
                    }
                }

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

            }
        }
    }

    public static function generateTimeAttendSbk($emp_id = NULL, $att_type, $from_date = NULL, $start_date = NULL, $policy_id = 1)
    {

        // dd($emp_id,$from_date,$start_date,$policy_id);;
        $dates = self::createDateRange($from_date, $start_date);
        $i = 0;
        foreach ($dates as $date) {
            $i++;
            TimeAttendGen::where('emp_id', $emp_id)
                ->whereDate('att_date', $date)
                ->delete();
            //==============================
            $s_day = getOne("SELECT DAYNAME ('" . $date . "')");

//            if ($policy_id == 1) { // start policy

            $attend = TimeAttend::select(DB::raw('MIN(attend) AS minDate, MAX(attend) AS maxDate'))
                ->where('emp_id', $emp_id)
                ->whereDate('attend', $date)->first();
            TimeAttendGen::where('emp_id', $emp_id)
                ->whereDate('att_date', $date)->delete();
            if ($attend->maxDate != null && $attend->minDate != null) {
                $policy = Policies::where('id', $policy_id)->first();
                if (is_array($policy->details) || is_object($policy->details)) {
                    $polic_time = $policy->details;
                } else {
                    $polic_time = json_decode($policy->details);
                }
                if (is_array($policy->details)) {
                    $win = $date . ' ' . $polic_time['win'];
                    $wout = $date . ' ' . $polic_time['wout'];
                    $fin = $date . ' ' . $polic_time['fin'];
                    $break_in = $date . ' ' . $polic_time['break_in'];
                    $break_out = $date . ' ' . $polic_time['break_out'];
                    $start_ot = $date . ' ' . $polic_time['start_ot'];
                    $end_ot = $date . ' ' . $polic_time['end_ot'];
                } else {
                    $win = $date . ' ' . $polic_time->win;
                    $wout = $date . ' ' . $polic_time->wout;
                    $fin = $date . ' ' . $polic_time->fin;
                    $break_in = $date . ' ' . $polic_time->break_in;
                    $break_out = $date . ' ' . $polic_time->break_out;
                    $start_ot = $date . ' ' . $polic_time->start_ot;
                    $end_ot = $date . ' ' . $polic_time->end_ot;

                }
                //  dd($fin);
                $in = $attend->minDate;
                $out = $attend->maxDate;
                //==========================
                //==========================
                $in_ot = null;
                $out_ot = null;
                $otrate = 0;
                $bot = 0;
                if ($att_type == 'request') {
                    $att_ot = ManualRequestOT::whereDate('ot_date', $date)
                        ->where('emp_id', 'like', '%' . $emp_id . '%')
                        ->first();
                    if ($att_ot != null) {
                        $in_ot = $date . ' ' . $att_ot->time_in;
                        $out_ot = $date . ' ' . $att_ot->time_out;
                        $otrate = $att_ot->ot_rate;
                    }
                } else if ($att_type == "unrequest") {
                    $in_ot = $in;
                    $out_ot = $out;
                    $otrate = 2;
                }
//                    if($i == 2){
//                        dd($att_type);
//                    }
                //==========================
                //==========================
                $h_d_rate = getOne("SELECT DISTINCT id  FROM   holiday_schedules
                                            WHERE DATE(start_date) <= DATE ('{$date}')
                                            AND DATE(end_date) >= DATE ('{$date}') LIMIT 1 ");

                if (self::dateTimeDiff($in, $fin) <= 0) {
                    $in = NULL;
                }
                if (self::dateTimeDiff($fin, $out) <= 0) {
                    $out = NULL;
                }
                $total = 0;
                $bot = 0;
                $sun_ot = 0;
                $hol_ot = 0;
                if ($s_day == 'Sunday' || $s_day == 'Saturday' || $h_d_rate > 0) {
                    if ($in_ot != null || $out_ot != null) {
                        $total_ot = self::dateTimeDiff($in_ot, $out_ot);
                        $total_ot = $total_ot >= 360 ? $total_ot - 60 : $total_ot;
                        $c_in = $in_ot;
                        $c_out = $out_ot;
                        $in = $in_ot;
                        $out = $out_ot;
                        $total = 0;
                        $late = 0;
                        $ot = 0;
                        $before = 0;

                        $ot_type = $s_day == 'Sunday' || $s_day == 'Saturday' ? 'otsun' : 'othol';
                        $sun_ot = $s_day == 'Sunday' || $s_day == 'Saturday' ? $total_ot : 0;
                        $hol_ot = $s_day != 'Sunday' && $s_day != 'Saturday' ? $total_ot : 0;
                    }else{
                        $in = null;
                        $out = null;
                    }
                } else {
                    if ($in != NULL || $out != NULL) {
                        $rin = $in;
                        $rout = $out;
                        $c_in = $in;
                        $c_out = $out;
                        $c_ot = 0;
                        $c_out_ot = 0;
                        $diffIn = self::dateTimeDiff($in, $win);
                        $diffOut = self::dateTimeDiff($wout, $out);
                        $late = 0;
                        $before = 0;
                        if ($diffIn >= 0) {
                            $rin = $win;
                        } else {
                            $late = (-1) * $diffIn;
                        }
                        if ($diffOut >= 0) {
                            $rout = $wout;
                        } else {
                            $before = (-1) * $diffOut;
                        }
                        if ($in_ot != null && $out_ot != null) {
                            if (self::dateTimeDiff($in_ot, $start_ot) < 0) {
                                $in_ot = $in_ot;
                            } else {
                                $in_ot = $wout;
                            }
                            if (self::dateTimeDiff($out_ot, $end_ot) < 0) {
                                $in_ot = null;
                                $out_ot = null;
                            } else if (self::dateTimeDiff($out_ot, $end_ot) >= 0 && self::dateTimeDiff($wout, $out_ot) >= 0) {
                                $c_out = $out_ot;
                            }
                            if ($in_ot != null && $out_ot != null) {
                                $bot = self::dateTimeDiff($in_ot, $out_ot);
                                $bot = $bot > 0 ? $bot : 0;
                            }
                        }
                        $ot_type = 'ot';
                        $total = self::dateTimeDiff($rin, $rout) - 60;
                    }
                }
                $m = TimeAttendGen::where('emp_id', $emp_id)
                    ->whereRaw(" (date(cin) = '{$date}' OR  date(cout) = '{$date}')")
                    ->first();

                if ($m == null) {
                    $m = new TimeAttendGen();
                }
                $m->emp_id = $emp_id;
                $m->att_date = $date;
                if ($in) {
                    $m->cin = $c_in;
                }
                if ($out) {
                    $m->cout = $c_out;
                }
                if ($in && $out) {
                    $m->total = $total;
                    $m->late = $late;
                    $m->before = $before;
                    $m->otrate = $otrate;
                    $m->ot = $bot + $sun_ot + $hol_ot;
                    $m->ot_type = $ot_type;
                    $m->total_ot = $bot + $sun_ot + $hol_ot;
                }
                if ($in == NULL || $out == NULL) {
                    $m->missing = 1;
                }
                $m->save();
            }
            //  } // end policy
            //==============================
            //==============================

        }
    }

    public static function generateTimeAttendGlobeso($emp_id = NULL, $att_type, $from_date = NULL, $start_date = NULL, $policy_id = 1)
    {

        // dd($emp_id,$from_date,$start_date,$policy_id);;
        $dates = self::createDateRange($from_date, $start_date);
        $i = 0;
        foreach ($dates as $date) {
            $i++;
            TimeAttendGen::where('emp_id', $emp_id)
                ->whereDate('att_date', $date)
                ->delete();
            //==============================
            $s_day = getOne("SELECT DAYNAME ('" . $date . "')");

//            if ($policy_id == 1) { // start policy

            $attend = TimeAttend::select(DB::raw('MIN(attend) AS minDate, MAX(attend) AS maxDate'))
                ->where('emp_id', $emp_id)
                ->whereDate('attend', $date)->first();
            TimeAttendGen::where('emp_id', $emp_id)
                ->whereDate('att_date', $date)->delete();
            if ($attend->maxDate != null && $attend->minDate != null) {
                $policy = Policies::where('id', $policy_id)->first();
                if (is_array($policy->details) || is_object($policy->details)) {
                    $polic_time = $policy->details;
                } else {
                    $polic_time = json_decode($policy->details);
                }

                if (is_array($policy->details)) {
                    $win = $date . ' ' . $polic_time['win'];
                    $late_in = $date . ' ' . $polic_time['late_in'];
                    $wout = $date . ' ' . $polic_time['wout'];
                    $fin = $date . ' ' . $polic_time['fin'];
                    $break_in = $date . ' ' . $polic_time['break_in'];
                    $break_out = $date . ' ' . $polic_time['break_out'];
                    $start_ot = $date . ' ' . $polic_time['start_ot'];
                    $end_ot = $date . ' ' . $polic_time['end_ot'];
                } else {
                    $win = $date . ' ' . $polic_time->win;
                    $late_in = $date . ' ' . $polic_time->late_in;
                    $wout = $date . ' ' . $polic_time->wout;
                    $fin = $date . ' ' . $polic_time->fin;
                    $break_in = $date . ' ' . $polic_time->break_in;
                    $break_out = $date . ' ' . $polic_time->break_out;
                    $start_ot = $date . ' ' . $polic_time->start_ot;
                    $end_ot = $date . ' ' . $polic_time->end_ot;

                }
                if($s_day == 'Saturday'){
                    $wout =  $date . ' 12:00:00';
                }

                //  dd($fin);
                $in = $attend->minDate;
                $out = $attend->maxDate;
                //==========================
                //==========================
                $in_ot = null;
                $out_ot = null;
                $otrate = 0;
                $bot = 0;
                if ($att_type == 'request') {
                    $att_ot = ManualRequestOT::whereDate('ot_date', $date)
                        ->where('emp_id', 'like', '%' . $emp_id . '%')
                        ->first();
                    if ($att_ot != null) {
                        $in_ot = $date . ' ' . $att_ot->time_in;
                        $out_ot = $date . ' ' . $att_ot->time_out;
                        $otrate = $att_ot->ot_rate;
                    }
                } else if ($att_type == "unrequest") {
                    $in_ot = $in;
                    $out_ot = $out;
                    $otrate = 2;
                }
//                    if($i == 2){
//                        dd($att_type);
//                    }
                //==========================
                //==========================
                $h_d_rate = getOne("SELECT DISTINCT id  FROM   holiday_schedules
                                            WHERE DATE(start_date) <= DATE ('{$date}')
                                            AND DATE(end_date) >= DATE ('{$date}') LIMIT 1 ");

                if (self::dateTimeDiff($in, $fin) <= 0) {
                    $in = NULL;
                }
                if (self::dateTimeDiff($fin, $out) <= 0) {
                    $out = NULL;
                }
                $total = 0;
                $bot = 0;
                $sun_ot = 0;
                $hol_ot = 0;
                $c_in = null;
                $c_out = null;
                if ($s_day == 'Sunday' || $h_d_rate > 0) {
                    if ($in_ot != null || $out_ot != null) {
                        $total_ot = self::dateTimeDiff($in_ot, $out_ot);
                        $total_ot = $total_ot >= 360 ? $total_ot - 60 : $total_ot;
                        $c_in = $in_ot;
                        $c_out = $out_ot;
                        $in = $in_ot;
                        $out = $out_ot;
                        $total = 0;
                        $late = 0;
                        $ot = 0;
                        $before = 0;

                        $ot_type = $s_day == 'Sunday'  ? 'otsun' : 'othol';
                        $sun_ot = $s_day == 'Sunday' ? $total_ot : 0;
                        $hol_ot = $s_day != 'Sunday' ? $total_ot : 0;
                    }else{
                        $in = null;
                        $out = null;
                    }
                } else {

                    if ($in != NULL || $out != NULL) {

                        $rin = $in;
                        $rout = $out;
                        $c_in = $in;

                        $c_out = $out;
                        $c_ot = 0;
                        $c_out_ot = 0;
                        $diffIn = self::dateTimeDiff($in, $win);
                        $lateIn = self::dateTimeDiff($in, $late_in);
                        $diffOut = self::dateTimeDiff($wout, $out);
                        $late = 0;
                        $before = 0;
                        if ($diffIn >= 0) {
                            $rin = $win;
                        } else {
                            if($lateIn <=0){
                                $late = (-1) * $lateIn;
                            }

                        }
                        if ($diffOut >= 0) {
                            $rout = $wout;
                        } else {
                            $before = (-1) * $diffOut;
                        }
                        if ($in_ot != null && $out_ot != null) {
                            if (self::dateTimeDiff($in_ot, $start_ot) < 0) {
                                $in_ot = $in_ot;
                            } else {
                                $in_ot = $wout;
                            }
                            if (self::dateTimeDiff($out_ot, $end_ot) < 0) {
                                $in_ot = null;
                                $out_ot = null;
                            } else if (self::dateTimeDiff($out_ot, $end_ot) >= 0 && self::dateTimeDiff($wout, $out_ot) >= 0) {
                                $c_out = $out_ot;
                            }
                            if ($in_ot != null && $out_ot != null) {
                                $bot = self::dateTimeDiff($in_ot, $out_ot);
                                $bot = $bot > 0 ? $bot : 0;
                            }
                        }
                        $ot_type = 'ot';
                        $total = self::dateTimeDiff($rin, $rout) - 60;

                    }
                }
                $m = TimeAttendGen::where('emp_id', $emp_id)
                    ->whereRaw(" (date(cin) = '{$date}' OR  date(cout) = '{$date}')")
                    ->first();
                if ($m == null) {
                    $m = new TimeAttendGen();
                }
                $m->emp_id = $emp_id;
                $m->att_date = $date;
                if ($in) {
                    $m->cin = $c_in;
                }
                if ($out) {
                    $m->cout = $c_out;
                }
                if ($in && $out) {
                    $m->total = $total;
                    $m->late = $late;
                    $m->before = $before;
                    $m->otrate = $otrate;
                    $m->ot = $bot + $sun_ot + $hol_ot;
                    $m->ot_type = $ot_type;
                    $m->total_ot = $bot + $sun_ot + $hol_ot;
                }
                if ($in == NULL || $out == NULL) {
                    $m->missing = 1;
                }
                $m->save();
            }
            //  } // end policy
            //==============================
            //==============================

        }
    }

    public static function generateTimeAttend($emp_id = NULL, $date, $in, $out, $total)
    {

        if (!IsNullOrEmptyString($date)) {
            $m = TimeAttendGen::where('emp_id', $emp_id)
                ->whereRaw(" (date(cin) = '{$date}' OR  date(cout) = '{$date}')")
                ->first();

            if ($m == null) {
                $m = new TimeAttendGen();
            }

            $m->emp_id = $emp_id;
            $m->att_date = $date;
            if ($in) {
                $m->cin = $in;
            }
            if ($out) {
                $m->cout = $out;
            }
            if ($in && $out) {
                $m->total = $total;
                $m->bot = 0;// ($bot>=60?$bot:0);
                $m->ot = 0;//($ot>=60?$ot:0);
                $m->total_ot = 0;
            }

            if ($in == NULL || $out == NULL) {
                $m->missing = 1;
            }
            $m->save();
        }
    }

    public static function getLeaveType($emp_id, $dates)
    {
        $m = EmpLeaveDetail::where('emp_id', $emp_id)->whereDate('leave_date', $dates)->first();
        if ($m != null) {
            $arr = __getArrLeave();

            $leave_type = $m->leave_type;
            if ($m->is_half == 0) {

                return isset($arr[$leave_type]) ? $arr[$leave_type] : '';
            } else if ($m->is_half == 1) {

                return isset($arr[$leave_type]) ? $arr[$leave_type] . ' half' : '';
            }

        }

        return ' ';
    }

    public static function getLeaveTypeHalf($emp_id, $dates)
    {
        $m = EmpLeaveDetail::where('emp_id', $emp_id)->whereDate('leave_date', $dates)->first();
        if ($m != null) {
            $arr = __getArrLeave();
            $leave_type = $m->leave_type;
            return ($m->is_half == 1 ? isset($arr[$leave_type]) ? $arr[$leave_type] . ' half' : '' : '');

        }
        return '';
    }

    public static function maternity($emp_id, $f_y, $f_m)
    {

        $maternity = EmpMaternity::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $total = $maternity != null ? $maternity->amount : 0;

        return $total;

    }

    public static function longSickLeave($emp_id, $f_y, $f_m)
    {

        $phone = EmpLongSickLeave::where('emp_id', $emp_id)->where('f_year', $f_y)->where('f_month', $f_m)->first();

        $total = $phone != null ? $phone->amount : 0;

        return $total;

    }

    public static function getMaternityNormal($emp_id, $from_date, $to_date)
    {
        //$from_date = "2019-08-01 ";  //start actual work date
        //$to_date = "2019-08-31";// end actual work date
        $mat = 0;
        $norm = 0;

        $arr_date = self::createDateRange($from_date, $to_date);

        if ($arr_date != null) {
            foreach ($arr_date as $d) {
                $em_leave = EmpLeave::where('leave_type', 'maternity_leave')
                    ->where('emp_id', $emp_id)
                    ->where(function ($q) use ($d) {
                        return $q->whereDate('from_date', '<=', $d)->whereDate('to_date', '>', $d);
                    })->first();
                if ($em_leave != null) {
                    $mat = $mat + 1;
                } else {
                    $norm = $norm + 1;
                }
            }
            return ['Maternity' => $mat, 'Normal' => $norm];
        }
        return [];
    }
    public static function getMonthNameKH($date)
    {
        $days = array(
            'january' => 'មករា',
            'february' => 'កុម្ភះ',
            'march' => 'មីនា',
            'april' => 'មេសា',
            'may' => 'ឧសភា',
            'june' => 'មិថុនា',
            'july' => 'កក្កដា',
            'august' => 'សីហា',
            'september' => 'កញ្ញា',
            'october' => 'តុលា',
            'november' => 'វិច្ឆិកា',
            'december' => 'ធ្នូ'
        );

        $sql = "SELECT MONTHNAME('{$date}') as m";
        $m = DB::select($sql);
        if (count($m) > 0) {
            return $days[strtolower($m[0]->m)];
        } else {
            return null;
        }
    }
    public static function getRequestOT($emp_id, $from_date, $to_date)
    {

        $ot_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_normal')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_normal')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_normal')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        $ot_sun_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_sunday')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_sun_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_sunday')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_sun_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_sunday')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        $ot_hol_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_holiday')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_hol_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_holiday')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_hol_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_holiday')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        $ot_night_shift_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_night_shift')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_night_shift_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_night_shift')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_night_shift_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_night_shift')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        $ot_eight_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_eight')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_eight_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_eight')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_eight_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_eight')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        $ot_over_night_100 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_over_night')
            ->where('status', 1)
            ->where('ot_rate', 1)
            ->sum('total_hour');
        $ot_over_night_150 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_over_night')
            ->where('status', 1)
            ->where('ot_rate', 1.5)
            ->sum('total_hour');
        $ot_over_night_200 = HrOvertimeRequestModel::where('emp_id', $emp_id)
            ->whereDate('date', '>=', $from_date)
            ->whereDate('date', '<=', $to_date)
            ->where('ot_type', '=', 'ot_over_night')
            ->where('status', 1)
            ->where('ot_rate', 2)
            ->sum('total_hour');
        
        return [
            'ot_100' => $ot_100 > 0 ? $ot_100 : 0,
            'ot_150' => $ot_150 > 0 ? $ot_150 : 0,
            'ot_200' => $ot_200 > 0 ? $ot_200 : 0,
            'ot_sun_100' => $ot_sun_100 > 0 ? $ot_sun_100 : 0,
            'ot_sun_150' => $ot_sun_150 > 0 ? $ot_sun_150 : 0,
            'ot_sun_200' => $ot_sun_200 > 0 ? $ot_sun_200 : 0,
            'ot_hol_100' => $ot_hol_100 > 0 ? $ot_hol_100 : 0,
            'ot_hol_150' => $ot_hol_150 > 0 ? $ot_hol_150 : 0,
            'ot_hol_200' => $ot_hol_200 > 0 ? $ot_hol_200 : 0,
            'ot_night_shift_100' => $ot_night_shift_100 > 0 ? $ot_night_shift_100 : 0,
            'ot_night_shift_150' => $ot_night_shift_150 > 0 ? $ot_night_shift_150 : 0,
            'ot_night_shift_200' => $ot_night_shift_200 > 0 ? $ot_night_shift_200 : 0,
            'ot_eight_100' => $ot_eight_100 > 0 ? $ot_eight_100 : 0,
            'ot_eight_150' => $ot_eight_150 > 0 ? $ot_eight_150 : 0,
            'ot_eight_200' => $ot_eight_200 > 0 ? $ot_eight_200 : 0,
            'ot_over_night_100' => $ot_over_night_100 > 0 ? $ot_over_night_100 : 0,
            'ot_over_night_150' => $ot_over_night_150 > 0 ? $ot_over_night_150 : 0,
            'ot_over_night_200' => $ot_over_night_200 > 0 ? $ot_over_night_200 : 0
        ];
    }
    public static function getNormalExRate(){
        $m = HrExchangeRateModel::orderBy('date','DESC')
            ->where('general_rate','>',0)
            ->limit(1)->first();
        return $m != null ?($m->rate>0?$m->rate:4000):4000;
    }

}

class UnitDay
{
    const DAY = 'DAY';
    const MONTH = 'MONTH';
    const QUARTER = 'QUARTER';
    const YEAR = 'YEAR';
}
