<?php
namespace App\Helpers;

use App\Models\EmpLeaveDetail;
use App\Models\Employee;

class RunGenATT
{
    var $arrSQL_all_fun = [];
    var $arrOT_all_fun = [];

    function isScanNotFull($emp_id,$date,$pol_id){

        //===========================
        //===========================
        $date_next_1_day = DateAdd($date,1);

        $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


        if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
            $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
            if($__mmm != null) {
                $em_policy_next_1 = $__mmm->pol_id;
            }
        }

        $arr_pol_day = [1,2,3,5,6,10,11,9];

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

        $last_month = IDate::getLastDayMonth($date);
        $getDate = getDateDiffMinute($date,$last_month);

//        dd("$emp_id,$date,$pol_id");

        if($pol_id == 1 || $pol_id == 2 || $pol_id == 3|| $pol_id == 6){
            $s_time = $date.' 05:00:00';
            $e_time = dateAdd($date,1).' 04:59:00';

            $win = $date.' 07:00:00';
            $wout = $date.' 16:00:00';

        }else if($pol_id == 4  ){
            $s_time = $date.' 16:00:00';
            $e_time = dateAdd($date,1).' 09:00:00';

            $win = $date.' 19:00:00';
            $wout =  dateAdd($date,1).' 04:00:00';

            if($em_policy_next_1 != $pol_id){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $e_time = DateAdd($date,1).' 09:00:00';
                    $wout = DateAdd($date,1).' 03:00:00';
                }
                if($getDate == 0){
                    $e_time = DateAdd($date,1).' 02:00:00';
                }
            }



        }else if( $pol_id == 13 ){

            $s_time = $date.' 16:00:00';
            $e_time = dateAdd($date,1).' 09:00:00';
            $win = $date.' 19:00:00';
            $wout =  dateAdd($date,1).' 02:00:00';

            if($em_policy_next_1 != $pol_id){
                if( in_array($em_policy_next_1,$arr_pol_day)){
                    $e_time = DateAdd($date,1).' 09:00:00';
                    $wout = DateAdd($date,1).' 09:00:00';
                }
                if($getDate == 0){
                    $e_time = DateAdd($date,1).' 02:00:00';
                }
            }

        }
        else if($pol_id == 5){
            $s_time = $date.' 04:30:00';
            $e_time = dateAdd($date,1).' 04:00:00';

            $win = $date.' 06:00:00';
            $wout = $date.' 15:00:00';

        }else if($pol_id == 7){
            $s_time = $date.' 13:00:00';
            $e_time = dateAdd($date,1).' 09:00:00';

            $win = $date.' 16:00:00';
            $wout =  dateAdd($date,1).' 02:00:00';

            if($em_policy_next_1 != $pol_id){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $e_time = DateAdd($date,1).' 09:00:00';
                    $wout = DateAdd($date,1).' 03:00:00';
                }
                if($getDate == 0){
                    $e_time = DateAdd($date,1).' 02:00:00';
                }
            }

        }else if($pol_id == 8){
            $s_time = $date.' 16:00:00';
            $e_time = dateAdd($date,1).' 09:00:00';

            $win = $date.' 19:00:00';
            $wout =  dateAdd($date,1).' 04:00:00';

            if($em_policy_next_1 != $pol_id){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $e_time = DateAdd($date,1).' 09:00:00';
                    $wout = DateAdd($date,1).' 09:00:00';
                }
                if($getDate == 0){
                    $e_time = DateAdd($date,1).' 02:00:00';
                }
            }


        }else if($pol_id == 9){
            $s_time = $date.' 03:30:00';
            $e_time = dateAdd($date,1).' 04:00:00';

            $win = $date.' 05:00:00';
            $wout =  $date.' 13:00:00';
        }else if($pol_id == 10){
            $s_time = $date.' 05:00:00';
            $e_time = dateAdd($date,1).' 03:00:00';

            $win = $date.' 05:00:00';
            $wout =  $date.' 13:00:00';
        }else if($pol_id == 11){
            $s_time = $date.' 05:00:00';
            $e_time = dateAdd($date,1).' 04:59:00';

            $win = $date.' 07:00:00';
            $wout =  $date.' 15:00:00';
        }else if($pol_id == 12){
            $s_time = $date.' 13:00:00';
            $e_time = dateAdd($date,1).' 09:00:00';

            $win = $date.' 16:00:00';
            $wout =  dateAdd($date,1).' 01:00:00';

            if($em_policy_next_1 != $pol_id){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $e_time = DateAdd($date,1).' 09:00:00';
                    $wout = DateAdd($date,1).' 09:00:00';
                }
                if($getDate == 0){
                    $e_time = DateAdd($date,1).' 02:00:00';
                }
            }
        }
//===================================================
//===================================================
//===================================================
//===================================================
        $f_c_in = getOne("SELECT attend FROM
                                    time_attends  WHERE emp_id = '{$emp_id}' AND
                                     attend >= '{$s_time}' AND  attend <= '{$e_time}'   AND `is_not_run`  =0  
                                    ORDER BY attend ASC
                                    LIMIT 1;");


        $l_c_out = getOne("SELECT attend FROM
                                    time_attends  WHERE emp_id = '{$emp_id}' AND
                                     attend >= '{$s_time}' AND  attend <= '{$e_time}'   AND `is_not_run`  =0  
                                    ORDER BY attend DESC 
                                    LIMIT 1;");


        if($f_c_in != null && $l_c_out != null) {
            if($f_c_in != "" && $l_c_out != "") {

                $diff = abs(getTimDiffMinute($f_c_in, $l_c_out));

                // echo "$diff = abs(getTimDiffMinute($f_c_in, $l_c_out));</br>";

                if ($diff >= 60) {
                    return false;
                }else{
                    //=======================================================
                    //=======================================================

                    $diff2 = abs(getTimDiffMinute($f_c_in, $win));
                    //$diff3 = abs(getTimDiffMinute($l_c_out, $wout));

                    if ($diff2 < 180) {// in
                        runSQL("INSERT INTO time_attends_gen (`emp_id`, `att_date`, `cin`) VALUES ('{$emp_id}','{$date}', '{$f_c_in}');");

                    }else{// out
                        runSQL("INSERT INTO time_attends_gen (`emp_id`, `att_date`,  `cout`) VALUES ('{$emp_id}','{$date}',  '{$l_c_out}');");
                    }
                    //=======================================================
                    //=======================================================

                }
            }
        }


        return true;


    }

    function getPol_all($emp_id, $date_loop_fr,$date_loop_to,$d_pol = 0){

//        runSQL("DELETE  FROM em_checkinout WHERE emp_id = '{$emp_id}' AND DATE(attend) >= '$date_loop_fr'  AND DATE(attend) <= '$date_loop_to';");
//        runSQL("DELETE   FROM em_checkinoutot WHERE emp_id = '{$emp_id}' AND DATE(normal_CHECKTIME) >= '{$date_loop_fr}' AND DATE(normal_CHECKTIME) <= '{$date_loop_to}';");


        //==============
        runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
        runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
        //==============
        runSQL("DELETE  FROM time_attends_gen WHERE emp_id = '{$emp_id}' AND DATE(att_date) >= '{$date_loop_fr}' AND DATE(att_date) <= '{$date_loop_to}';");
        $arr_date = getDatesFromRange($date_loop_fr, $date_loop_to);
        $sql = "INSERT INTO  em_checkinout(USERID,emp_id,attend,CHECKTYPE,TOTALLATE,TIMELATE,TOTAL_TIMEOVER,TIME_OVER,TIME_EARLY,TOTAL_EARLY,SCHCLASSID,day_meal,
                                      h_working_hour_day,is_overnight,normal_CHECKTIME,ration_1) VALUES
                            ";

        $sql_ot = "INSERT INTO  em_checkinoutot( userid,
										emp_id ,
										chin  ,
										chout ,
										ottype,
										total  ,
										realtotal ,
										completed ,
										isAuthorized ,
										SCHCLASSID ,
										otrate  ,
										ot_benefit,
										normal_CHECKTIME ) VALUES ";

        foreach ($arr_date as $_date_loop) {
            $em_policy = 0;
            $date_loop_fr = $_date_loop;
            $em_policy = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_loop_fr}'") ;

            /* if ($em_policy <= 0 || $em_policy == '') {
                 $em_policy = getOne("SELECT pol_id FROM em_dialy_policy WHERE emp_id = '{$emp_id}' ORDER BY pol_date DESC ") ;
             }*/

            if ($em_policy <= 0 || $em_policy == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy = $__mmm->pol_id;
                }
            }
            /////======================= policy 1==============
            /// ==================== check leave and finger ====================
//                    $get_att_finger = Helper::getTimesGen($emp_id,$date_loop_fr);
//
//                    $getLeave = EmpLeaveDetail::select('leave_date')
//                                ->where('emp_id',$emp_id)
//                                ->where('leave_date',$date_loop_fr)
//                                ->first();
//                    $is_check_leave = false;

//                    if ($getLeave != null){
//                        $in = null;
//                        if(isset($get_att_finger['in'])){
//                            $in = $get_att_finger['in'];
//                        }
//                        if($in != null){
//                            $is_check_leave = true;
//                        }
//
//                    }
            /// ================================================================
            ///========
            ///
            ///


            if(!($this->isScanNotFull($emp_id,$date_loop_fr,$em_policy))) {
                if ($em_policy == 1) {
                    $this->getPol_1($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 2) {
                    $this->getPol_2($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 3) {
                    $this->getPol_3($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 4) {
                    $this->getPol_4($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 5) {
                    $this->getPol_5($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 6) {
                    $this->getPol_6($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 7) {
                    $this->getPol_7($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 8) {
                    $this->getPol_8($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 9) {
                    $this->getPol_9($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 10) {
                    $this->getPol_10($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 11) {
                    $this->getPol_11($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 12) {
                    $this->getPol_12($em_policy, $emp_id, $date_loop_fr);
                } else if ($em_policy == 13) {
                    $this->getPol_13($em_policy, $emp_id, $date_loop_fr);
                }
            }
        }
        //dd($this->arrSQL_all_fun);
        if (count($this->arrSQL_all_fun)>0) {
            $sql = $sql . implode(',',$this->arrSQL_all_fun);

            $sql = str_replace('0000-00-00 00:00:00','1999-01-01 01:00:00',$sql);
            runSQL($sql);
        }
        if (count($this->arrOT_all_fun)>0) {
            $sql_ot = $sql_ot . implode(',', $this->arrOT_all_fun);
            $sql_ot = str_replace('0000-00-00 00:00:00','1999-01-01 01:00:00',$sql_ot);
            runSQL($sql_ot);
        }
    }

    function getPol_1($em_policy,$emp_id,$date_loop_fr){
        if ($em_policy == 1){//======================= policy 1==============

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
                      DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

            $fwin = $date_loop_fr.' 05:00:01';
            $win = $date_loop_fr.' 07:00:00';
            $wout = $date_loop_fr.' 16:00:00';
            $w_break_in = $date_loop_fr.' 11:00:00';
            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:00';
            $checkinout_ = getOneRow("SELECT
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        //  $cal_wout = $wout;
                        $cal_wout = $r_check_out;

                    } else {
                        //  $cal_wout = $r_check_out;
                        $cal_wout = $wout;
                    }


                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));

                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 660 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break

                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);

                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                            '{$emp_id}',
                                             '{$lcheck}',
                                             'out',
                                           '0',
                                          '0',
                                            '0',
                                            '0',
                                            '0',
                                        '{$em_policy}',
                                          '0',
                                            '0',
                                           '{$haft_out_hour}',
                                            '{$is_night}',
                                           '{$lcheck}',0)   
                                    ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================

                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTimDiffMinute(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;
                    if ($total_time_work >= 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }



                    if(getTimDiffMinute($cal_win,$w_break_in) >= 0 &&  getTimDiffMinute($w_break_out,$lcheck) >= 0
                        &&  getTimDiffMinute($lcheck,$wout) >= 0
                    ){
                        $total_time_work = getTotalTimeWork($cal_win,$lcheck) - 60;
                        //dd("  $total_time_work = getTimDiffMinute($cal_win,$lcheck) - 60; ");
                    }

                }// $w_break_out
                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {

                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);


                    //$date_loop_fr
//                    $leave = EmpLeaveDetail::where('emp_id',$emp_id)
//                                            ->whereDate('leave_date',$date_loop_fr)
//                                            ->first();
//                    if($leave != null){
//                        $haft_in_hour = 4 * 60;
//                        $haft_out_hour =0;
                    //  }else {

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;
                    //  }

                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        //  $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_2($em_policy,$emp_id,$date_loop_fr)
    {
        dd("polic 2");


        if ($em_policy == 2) {//======================= policy 2==============

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR
DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

            $fwin = $date_loop_fr . ' 05:00:01';
            $win = $date_loop_fr . ' 07:00:00';
            $wout = $date_loop_fr . ' 16:00:00';
            $w_break_in = $date_loop_fr . ' 11:00:00';
            $w_break_out = $date_loop_fr . ' 11:59:59';
            $win_10 = $date_loop_fr . ' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr, 1) . ' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr, 1) . ' 06:59:59';
            $lwin = DateAdd($date_loop_fr, 1) . ' 04:00:00';


            $checkinout_ = getOneRow("SELECT
                                            MIN(attend) as fcheck  ,
                                            MAX(attend) as lcheck
                                            FROM time_attends WHERE emp_id = '{$emp_id}' AND
                                            attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

//=============================
//=============================
            $s_day = getOne("SELECT DAYNAME ('" . $fcheck . "')");

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

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

            if ($s_day == 'Sunday' || $h_d_rate > 0) {
//============ sun day =====================
//============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if (getTimDiffMinute($r_check_out, $lcheck) > 0) {
                    $r_check_out = $lcheck;
                }
//=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 660 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    } else {
                        if (getTimDiffMinute($cal_win, $w_break_in) > 0 && getTimDiffMinute($w_break_out, $lcheck) > 0) {
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if (getTimDiffMinute($cal_win, $w_break_in) > 0 && getTimDiffMinute($lcheck, $w_break_out) > 0
                        && getTimDiffMinute($w_break_in, $lcheck) > 0) {
                        $total_time_work = getTotalTimeWork($cal_win, $w_break_in);
                    } else {
                        if (getTimDiffMinute($w_break_out, $lcheck) > 0 && getTimDiffMinute($fcheck, $w_break_out) > 0
                            && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                            $total_time_work = getTotalTimeWork($w_break_out, $cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                '{$emp_id}',
                                '{$lcheck}',
                                'out',
                                '0',
                                '0',
                                '0',
                                '0',
                                '0',
                                '{$em_policy}',
                                '0',
                                '0',
                                '{$haft_out_hour}',
                                '{$is_night}',
                                '{$lcheck}',0)
                                ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $w_meal = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                '{$emp_id}',
                                '{$r_check_in}',
                                'in',
                                '0',
                                '0',
                                '0',
                                '0',
                                '0',
                                '0',
                                '{$em_policy}',
                                '{$w_meal}',
                                '{$haft_in_hour}',
                                '{$is_night}',
                                '{$r_check_in}',0),
                                ( 'api',
                                '{$emp_id}',
                                '{$r_check_out}',
                                'out',
                                '0',
                                '0',
                                '0',
                                '0',
                                '0',
                                '{$em_policy}',
                                '0',
                                '0',
                                '{$haft_out_hour}',
                                '{$is_night}',
                                '{$r_check_in}',0)
                                ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }

                if ($total_time_work > 0) {

                    $ottype = $s_day == 'Sunday' ? 'otsun' : 'othol';
                    $ot_rate = $s_day == 'Sunday' ? 2 : $h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;

                }
//=================== calculate total work ==================


//============ sun day =====================
//============ sun day =====================
//============ sun day =====================
            } else {

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
//=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;
                    if ($total_time_work >= 300) {
                        $total_time_work = $total_time_work - 60;// break
                    } else {
                        if (getTimDiffMinute($cal_win, $w_break_in) > 0 && getTimDiffMinute($w_break_out, $lcheck) > 0) {
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if (getTimDiffMinute($cal_win, $w_break_in) > 0 && getTimDiffMinute($lcheck, $w_break_out) > 0
                        && getTimDiffMinute($w_break_in, $lcheck) > 0) {
                        $total_time_work = getTotalTimeWork($cal_win, $w_break_in);
                    } else {
                        if (getTimDiffMinute($w_break_out, $lcheck) > 0 && getTimDiffMinute($fcheck, $w_break_out) > 0
                            && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                            $total_time_work = getTotalTimeWork($w_break_out, $cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                            '{$emp_id}',
                            '{$lcheck}',
                            'out',
                            '0',
                            '0',
                            '0',
                            '0',
                            '0',
                            '{$em_policy}',
                            '0',
                            '0',
                            '{$haft_out_hour}',
                            '{$is_night}',
                            '{$lcheck}',0)
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    $w_meal = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                        '{$emp_id}',
                        '{$r_check_in}',
                        'in',
                        '{$late}',
                        '{$time_late}',
                        '{$early}',
                        '{$time_early}',
                        '{$time_early}',
                        '{$early}',
                        '{$em_policy}',
                        '{$w_meal}',
                        '{$haft_in_hour}',
                        '{$is_night}',
                        '{$r_check_in}',0),
                        ('api',
                        '{$emp_id}',
                        '{$r_check_out}',
                        'out',
                        '{$before}',
                        '{$time_before}',
                        '{$after}',
                        '{$time_after}',
                        '{$time_after}',
                        '{$after}',
                        '{$em_policy}',
                        '0',
                        '{$haft_out_hour}',
                        '{$is_night}',
                        '{$r_check_in}',0)
                        ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


//=================== calculate total work ==================

// ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        // $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';


                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTimDiffMinute(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';


                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


// ================ calculate ot ==============================
            }


        }


    }


    function getPol_3($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 3){//======================= policy 3==============
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
                      DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            $fwin = $date_loop_fr.' 05:00:01';
            $win = $date_loop_fr.' 07:00:00';
            $wout = $date_loop_fr.' 16:00:00';
            $w_break_in = $date_loop_fr.' 11:00:00';
            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:00';



            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}' AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 660 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $w_meal = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0),
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

//                if($total_time_work >0) {
//
//                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
//                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
//
//                    $real_ot =  getRealMinuteOT($total_time_work);
//                    $meal_10_i = 0;
//                    $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$r_check_in}',
//                                      '{$lcheck}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}' ,
//                                       '{$fcheck}')
//                                      ");
//                }
//                =================== calculate total work ==================
//
//
//                ============ sun day =====================
//                ============ sun day =====================
//                ============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTimDiffMinute(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    $w_meal = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
//                $start_ot = null;
//                $end_ot = null;
//                if ($r_check_in != null && $r_check_out != null) {
//                    $start_ot = $wout;
//                }
//
//                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
//                    $start_ot = $fcheck;
//                }
//
//                if ($start_ot != null) {
//                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
//                        $end_ot = $lcheck;
//                    }
//                }

//                $total_ot_150 = 0;
//                $total_ot_200 = 0;
//                if ($start_ot != null && $end_ot != null) {
//                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
//                      //  $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
//                        $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
//                        $real_ot = getRealMinuteOT($total_ot_150);
//                        $ot_rate = 1.5;
//                        $meal_10_i = 0;
//                        $ottype = 'ot';
//                        if ($real_ot > 0) {
//                            $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$start_ot}',
//                                      '{$end_ot}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}',
//                                       '{$fcheck}'  )");
//                        }
//
//                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
//                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
//                            $total_ot_150 = getTimDiffMinute(($start_ot), ($win_10));
//
//                            $real_ot = getRealMinuteOT($total_ot_150);
//                            $ot_rate = 1.5;
//                            $meal_10_i = 0;
//                            $ottype = 'ot';
//                            if ($real_ot > 0) {
//                                $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$start_ot}',
//                                      '{$win_10}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}' ,
//                                       '{$fcheck}' )");
//                            }
//
//                            $total_ot_200 = getTimDiffMinute(($win_10), ($end_ot));
//
//                            $real_ot = getRealMinuteOT($total_ot_200);
//                            $ot_rate = 2;
//                            $meal_10_i = 0;
//                            $ottype = 'ot';
//                            if ($real_ot > 0) {
//                                $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$win_10}',
//                                      '{$end_ot}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}',
//                                       '{$fcheck}' ) ");
//                            }
//
//
//                        } else {// check in after 22 and out before 05
//                            $total_ot_200 = getTimDiffMinute(($start_ot), ($end_ot));
//                            $real_ot = getRealMinuteOT($total_ot_200);
//                            $ot_rate = 2;
//                            $meal_10_i = 0;
//                            $ottype = 'ot';
//                            if ($real_ot > 0) {
//                                $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$start_ot}',
//                                      '{$end_ot}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}',
//                                       '{$fcheck}' ) ");
//                            }
//                        }
//                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05
//
//                    }
//                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 3==============
    }


    function getPol_4($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 4){//======================= policy 4==============

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

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            //============
            $fwin = $date_loop_fr.' 16:00:01';
            $win = $date_loop_fr.' 19:00:00';
            $wout = DateAdd($date_loop_fr,1).' 04:00:00';
//            $wout = $date_loop_fr.' 04:00:00';
            $w_break_in = $date_loop_fr.' 11:00:00';
            $w_break_out = $date_loop_fr.' 11:59:59';
            /*$win_10 = $date_loop_fr.' 22:00:00';
        $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
        $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';*/
            $lwin = DateAdd($date_loop_fr,1).' 08:00:59';


            //===========================
            //===========================
            $date_next_1_day = DateAdd($date_loop_fr,1);

            $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


            if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy_next_1 = $__mmm->pol_id;
                }
            }
            $arr_pol_day = [1,2,3,5,6,10,11,9];
            if($em_policy_next_1 != $em_policy){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
                    $wout_7 =  DateAdd($date_loop_fr,1).' 07:00:00';
                    $lwin =  DateAdd($date_loop_fr,1).' 09:00:00';
                }
                $last_month = IDate::getLastDayMonth($date_loop_fr);
                $getDate = getDateDiffMinute($date_loop_fr,$last_month);
                if($getDate == 0){
                    $lwin =  DateAdd($date_loop_fr,1).' 02:00:00';
                }

            }
            //===========================
            //===========================
            //===========================


            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}' AND `is_not_run`  =0   ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

//                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
//                    dd("$total_time_work = getTotalTimeWork(($cal_win), ($lcheck));");
//                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = 0;//$total_time_work >= 600 ? 0.75 : 0;
                    /* if ($total_time_work > 300) {
                         $total_time_work = $total_time_work - 60;// break
                     }else{
                         if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                             $total_time_work = $total_time_work - 60;// break
                         }
                     }*/

                    /*   if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                           && getTimDiffMinute($w_break_in,$lcheck) >0){
                           $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                       }else{
                           if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                               && getTimDiffMinute($w_break_in,$fcheck) >0){
                               $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                           }
                       }*/

                }
//
                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
//                        $haft_in_hour = 0;
//                        $haft_out_hour = 0;
//                        $is_night = 0;
//                        $this->arrSQL_all_fun[] = "  ( 'api',
//                                    '{$emp_id}',
//                                     '{$lcheck}',
//                                     'out',
//                                   '0',
//                                  '0',
//                                    '0',
//                                    '0',
//                                    '0',
//                                '{$em_policy}',
//                                  '0',
//									'0',
//                                   '{$haft_out_hour}',
//                                    '{$is_night}',
//                                   '{$lcheck}')
//                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                }
                else {


//                    $haft_in_hour = 0;// $total_time_work / 2;
//                    $haft_out_hour = 0;// $total_time_work / 2;
//                    $is_night = 0;
//                    $w_meal = 0;
//                    $this->arrSQL_all_fun[] = "( 'api',
//                                    '{$emp_id}',
//                                     '{$r_check_in}',
//                                      'in',
//                                      '0',
//                                      '0',
//                                      '0',
//                                      '0',
//                                      '0',
//                                     '0',
//                                     '{$em_policy}',
//                                     '{$w_meal}',
//                                      '{$haft_in_hour}',
//                                     '{$is_night}',
//                                      '{$r_check_in}'),
//                                      ( 'api',
//                                    '{$emp_id}',
//                                     '{$r_check_out}',
//                                     'out',
//                                   '0',
//                                  '0',
//                                    '0',
//                                    '0',
//                                    '0',
//                                '{$em_policy}',
//                                  '0',
//									'0',
//                                   '{$haft_out_hour}',
//                                    '{$is_night}',
//                                   '{$r_check_in}')
//                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,0);
                }

//                if($total_time_work >0) {
//
//                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
//                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
//                    $real_ot = $total_time_work;
//                    $meal_10_i = 0;
//                    $this->arrOT_all_fun[] = (" ( 'api',
//                                      '{$emp_id}',
//                                     '{$r_check_in}',
//                                      '{$lcheck}',
//                                    '{$ottype}',
//                                      '{$real_ot}',
//                                      '{$real_ot}',
//                                      '1',
//                                      '1',
//                                      '{$em_policy}',
//                                     '{$ot_rate}',
//                                      '{$meal_10_i}' ,
//                                       '{$fcheck}')
//                                      ");
//                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /*    if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*  if ($r_check_in == null) {
                  if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                      $r_check_in = $fcheck;
                  }
              }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }
//dd(getTimDiffMinute($wout, $r_check_out));
                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
//                        $cal_wout = $wout;
                        $cal_wout = $r_check_out;
                    } else {
//                        $cal_wout = $r_check_out;
                        $cal_wout = $wout;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    if($total_time_work >= 480){
                        $total_time_work = 480;
                    }
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = 0;// getTimDiffMinute(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);

                    $total_time_work = getRealMinute($total_time_work);


                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;

                    $is_night = 0;
                    $w_meal = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 4==============
    }


    function getPol_5($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 5){//======================= policy 5==============
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
                      DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

            $fwin = $date_loop_fr.' 04:00:01';
            $win = $date_loop_fr.' 06:00:00';
            $wout = $date_loop_fr.' 15:00:00';
            $w_break_in = $date_loop_fr.' 11:00:00';
            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:59';
            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}' AND `is_not_run`  =0   ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
                    $real_ot = getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        // $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============



    }



    function getPol_6($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 6){//======================= policy 6==============

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
                      DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

            $fwin = $date_loop_fr.' 05:00:01';
            $win = $date_loop_fr.' 07:00:00';
            $wout = $date_loop_fr.' 16:00:00';

            $w_break_in = $date_loop_fr.' 11:00:00';
            $w_break_out = $date_loop_fr.' 11:59:59';

            /* $w_break_in_18 = $date_loop_fr.' 18:00:00';
         $w_break_out_19 = $date_loop_fr.' 19:00:00';*/

            $w_break_in_00 = DateAdd($date_loop_fr,1).' 00:00:01';
            $w_break_out_01 = DateAdd($date_loop_fr,1).' 01:00:00';

            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:59';



            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}' AND `is_not_run`  =0   ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $cal_wout = $lcheck;

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    //    dd(" $total_time_work = getTimDiffMinute(($cal_win), ($lcheck));");
                    $total_time_work = getRealMinute($total_time_work);
                    //$w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
                    $real_ot = getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTimDiffMinute(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;

                    if ($total_time_work >= 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));

                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';

                        //=============================
                        //============================= break 18 - 19

                        /* if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0){
                         $total_ot_150 = getTimDiffMinute($w_break_out_19,$end_ot);
                     }*/

                        /* if(getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                         $total_ot_150 = getTimDiffMinute($start_ot,$w_break_in_18);
                     }*/

                        /* if(getTimDiffMinute($start_ot,$w_break_in_18) >= 0 && getTimDiffMinute($w_break_out_19,$end_ot) >= 0){
                         $total_ot_150 = $total_ot_150 - 60;
                     }*/

                        /* if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0 &&
                         getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                         $total_ot_150 = 0;
                     }*/

                        //=============================
                        //=============================
                        $real_ot = getRealMinuteOT($total_ot_150);

                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05

                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTimDiffMinute(($start_ot), ($win_10));


                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //===========================
                            //===========================
                            /*if(getTimDiffMinute($start_ot,$w_break_in_18) >=0){
                            $total_ot_150 = $total_ot_150 -60;
                        }*/

                            /* if(getTimDiffMinute($w_break_in_18,$start_ot) >=0 && getTimDiffMinute($start_ot,$w_break_out_19) >=0) {
                             $total_ot_150 = getTimDiffMinute($w_break_out_19,$win_10);
                         }*/

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

                            $real_ot = getRealMinuteOT($total_ot_150);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            //===================================================
                            $total_ot_200 = getTotalTimeWork($win_10, $end_ot);


                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            if(getTimDiffMinute($w_break_out_01,$end_ot)>=0){
                                $total_ot_200 = $total_ot_200 - 60;
                            }

                            if(getTimDiffMinute($w_break_in_00,$end_ot) >=0 && getTimDiffMinute($end_ot,$w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($win_10,$w_break_in_00);
                            }



                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));

                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //==================================
                            //==================================
                            if(getTimDiffMinute($start_ot,$w_break_in_00)>=0 && getTimDiffMinute($w_break_in_00,$end_ot) >= 0 &&
                                getTimDiffMinute($end_ot, $w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($start_ot,$w_break_in_00);
                            }

                            if(getTimDiffMinute($w_break_in_00,$start_ot)>=0 && getTimDiffMinute($start_ot, $w_break_out_01) >=0
                                && getTimDiffMinute($w_break_out_01,$end_ot) >= 0){
                                $total_ot_200 = getTotalTimeWork($w_break_out_01,$end_ot);
                            }

                            if(getTimDiffMinute($start_ot ,$w_break_in_00) >0 && getTimDiffMinute($w_break_out_01,$end_ot)>0){
                                $total_ot_200 = $total_ot_200 - 1;
                            }
                            //==================================
                            //==================================
                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_7($em_policy,$emp_id,$date_loop_fr){

        if ($em_policy == 7){//======================= policy 7==============

//            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
//            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR
//                      DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

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

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            //==========

            $fwin = $date_loop_fr.' 14:00:01';
            $win = $date_loop_fr.' 16:00:00';
            $wout = $date_loop_fr.' 22:00:00';

            $w_break_in = $date_loop_fr.' 18:00:00';
            $w_break_out = $date_loop_fr.' 19:00:00';

            $w_break_in_18 = $date_loop_fr.' 18:00:00';
            $w_break_out_19 = $date_loop_fr.' 19:00:00';

            $w_break_in_00 = DateAdd($date_loop_fr,1).' 00:00:01';
            $w_break_out_01 = DateAdd($date_loop_fr,1).' 01:00:00';

            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 07:0:00';
            $lwin = DateAdd($date_loop_fr,1).' 09:00:59';



            //===========================
            //===========================
            $date_next_1_day = DateAdd($date_loop_fr,1);

            $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


            if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy_next_1 = $__mmm->pol_id;
                }
            }

            $arr_pol_day = [1,2,3,5,6,10,11,9];
            if($em_policy_next_1 != $em_policy){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
                    $wout_7 =  DateAdd($date_loop_fr,1).' 07:00:00';
                    $lwin =  DateAdd($date_loop_fr,1).' 09:00:00';
                }
                $last_month = IDate::getLastDayMonth($date_loop_fr);
                $getDate = getDateDiffMinute($date_loop_fr,$last_month);
                if($getDate == 0){
                    $lwin =  DateAdd($date_loop_fr,1).' 02:00:00';
                }
            }
            //===========================
            //===========================
            //===========================


            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}' AND `is_not_run`  =0   ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){

                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) >= 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>=0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    //   dd($total_time_work);
                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break

                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) >= 0 && getTimDiffMinute($w_break_out,$lcheck) >=0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) >= 0 && getTimDiffMinute($lcheck,$w_break_out) >=0
                        && getTimDiffMinute($w_break_in,$lcheck) >=0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);


                    }
                    else{

                        if(getTimDiffMinute($w_break_out,$lcheck) >= 0 && getTimDiffMinute($fcheck,$w_break_out) >=0
                            && getTimDiffMinute($w_break_in,$fcheck) >= 60){

                            // $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                            $total_time_work = getTotalTimeWork(($cal_win), ($lcheck)) - 60;


                        }else if(getTimDiffMinute($w_break_out,$lcheck) >= 0 && getTimDiffMinute($fcheck,$w_break_out) >=0
                            && getTimDiffMinute($w_break_in,$fcheck) >=0 && getTimDiffMinute($w_break_in,$fcheck) <=60){

                            //$total_time_work = getTotalTimeWork(($cal_win), ($lcheck)) - 60;
                            $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                            if($total_time_work <= 660){
                                $total_time_work = $total_time_work-60;
                            }else{
                                $late_hour = getTimDiffMinute($w_break_in,$fcheck);
                                $total_time_work = $total_time_work -  ($late_hour + 60);
                            }


                        }else if(getTimDiffMinute($w_break_out,$lcheck) >= 0 && getTimDiffMinute($fcheck,$w_break_out) >=0
                            && getTimDiffMinute($w_break_in,$fcheck) <=0 && getTimDiffMinute($w_break_in,$fcheck) <=60){
                            $total_time_work = getTotalTimeWork(($cal_win), ($lcheck)) - 60;

                        }
                    }

                    //============================
                    //============================
                    if(getTimDiffMinute($cal_win,$w_break_in) >= 0 && getTimDiffMinute($w_break_out_01,$lcheck)>0){
                        $total_time_work = getTotalTimeWork($cal_win,$lcheck) - 120;// 2 break
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) >= 0 && getTimDiffMinute($lcheck,$w_break_in_00) >=0
                        && getTimDiffMinute($w_break_out,$lcheck) >=0
                    ){
                        $total_time_work = getTotalTimeWork($cal_win,$lcheck) - 60;// 1 break
                    }

                    if(getTimDiffMinute($w_break_in,$cal_win) >= 0 && getTimDiffMinute($lcheck,$w_break_in_00) >=0
                        && getTimDiffMinute($w_break_out,$lcheck) >=0
                    ){
                        $total_time_work = getTotalTimeWork($w_break_out,$lcheck) - 60;// 1 break

                    }

                    if(getTimDiffMinute($w_break_in,$cal_win) > 0 && getTimDiffMinute($lcheck,$w_break_in_00) >0
                        && getTimDiffMinute($lcheck,$w_break_out) >0
                    ){
                        $total_time_work = 0;// 1 break
                    }

                    if(getTimDiffMinute($w_break_out,$cal_win) > 0 && getTimDiffMinute($lcheck,$w_break_in_00) >0  ){
                        $total_time_work = getTotalTimeWork($cal_win,$lcheck);// 0 break

                    }

                    if(getTimDiffMinute($w_break_out,$cal_win) > 0 && getTimDiffMinute($w_break_in_00,$lcheck) >0
                        && getTimDiffMinute($lcheck,$w_break_out_01) >0
                    ){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in_00);// 0 break
                    }

                    if(getTimDiffMinute($w_break_out,$cal_win) > 0   && getTimDiffMinute($w_break_out_01,$lcheck) >0
                    ){
                        $total_time_work = getTotalTimeWork($cal_win,$lcheck) - 60;// 0 break

                    }


//                    if($total_time_work >=780){
//                        $total_time_work = $total_time_work - 120;
//                    }
                    //============================
                    //============================


                }

                //echo " $total_time_work |||| ";

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);
                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }
                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
                    $real_ot = getRealMinuteOT($total_time_work);


                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
//88888888888888888888888888888888888888888888888888888888888888888888888
                $r_check_in = null;
                $r_check_out = null;
                if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                    $r_check_in = $fcheck;
                }
                if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                    $r_check_out = $lcheck;
                }
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                if ($r_check_in == null) {
                    if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                        $r_check_in = $fcheck;
                    }
                }

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;

                    if ($total_time_work > 300) {
                        $total_time_work = $total_time_work - 60;// break
                    }else{
                        if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                            $total_time_work = $total_time_work - 60;// break
                        }
                    }

                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                        && getTimDiffMinute($w_break_in,$lcheck) >0){
                        $total_time_work = getTotalTimeWork($cal_win,$w_break_in);
                    }else{
                        if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                            && getTimDiffMinute($w_break_in,$fcheck) >0){
                            $total_time_work = getTotalTimeWork($w_break_out,$cal_wout);
                        }
                    }

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));

                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';

                        //=============================
                        //============================= break 18 - 19

                        if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0){
                            $total_ot_150 = getTotalTimeWork($w_break_out_19,$end_ot);
                        }

                        if(getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                            $total_ot_150 = getTotalTimeWork($start_ot,$w_break_in_18);
                        }

                        if(getTimDiffMinute($start_ot,$w_break_in_18) >= 0 && getTimDiffMinute($w_break_out_19,$end_ot) >= 0){
                            $total_ot_150 = $total_ot_150 - 60;
                        }

                        if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0 &&
                            getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                            $total_ot_150 = 0;
                        }

                        //=============================
                        //=============================
                        $real_ot = getRealMinuteOT($total_ot_150);

                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05

                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));


                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //===========================
                            //===========================
                            if(getTimDiffMinute($start_ot,$w_break_in_18) >=0){
                                $total_ot_150 = $total_ot_150 -60;
                            }

                            if(getTimDiffMinute($w_break_in_18,$start_ot) >=0 && getTimDiffMinute($start_ot,$w_break_out_19) >=0) {
                                $total_ot_150 = getTotalTimeWork($w_break_out_19,$win_10);
                            }

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

                            $real_ot = getRealMinuteOT($total_ot_150);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            //===================================================
                            $total_ot_200 = getTotalTimeWork($win_10, $end_ot);


                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            if(getTimDiffMinute($w_break_out_01,$end_ot)>=0){
                                $total_ot_200 = $total_ot_200 - 60;
                            }

                            if(getTimDiffMinute($w_break_in_00,$end_ot) >=0 && getTimDiffMinute($end_ot,$w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($win_10,$w_break_in_00);
                            }



                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));

                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //==================================
                            //==================================
                            if(getTimDiffMinute($start_ot,$w_break_in_00)>=0 && getTimDiffMinute($w_break_in_00,$end_ot) >= 0 &&
                                getTimDiffMinute($end_ot, $w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($start_ot,$w_break_in_00);
                            }

                            if(getTimDiffMinute($w_break_in_00,$start_ot)>=0 && getTimDiffMinute($start_ot, $w_break_out_01) >=0
                                && getTimDiffMinute($w_break_out_01,$end_ot) >= 0){
                                $total_ot_200 = getTotalTimeWork($w_break_out_01,$end_ot);
                            }

                            if(getTimDiffMinute($start_ot ,$w_break_in_00) >0 && getTimDiffMinute($w_break_out_01,$end_ot)>0){
                                $total_ot_200 = $total_ot_200 - 1;
                            }
                            //==================================
                            //==================================
                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05
                        $real_ot = getTotalTimeWork($win_10,$wout_5) -60;
                        $real_ot = getRealMinuteOT($real_ot);
                        $ot_rate = 2;
                        $meal_10_i = 0;
                        $ottype = 'ot';

                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$wout_5}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                        //==============================
                        if(getTimDiffMinute($lcheck,$wout_7) >= 0){
                            $real_ot = getTotalTimeWork($wout_5,$lcheck);
                        }else{
                            $real_ot = getTotalTimeWork($wout_5,$wout_7);
                        }


                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $real_ot = getRealMinuteOT($real_ot);

                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$wout_5}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_8($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 8){//======================= policy 8==============
//            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
//            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR
//						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

//===========
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
//===========

            $fwin = $date_loop_fr.' 14:00:01';
            $win = $date_loop_fr.' 19:00:00';
            $wout = $date_loop_fr.' 22:00:00';

            /* $w_break_in = $date_loop_fr.' 18:00:00';
         $w_break_out = $date_loop_fr.' 19:00:00';

         $w_break_in_18 = $date_loop_fr.' 18:00:00';
         $w_break_out_19 = $date_loop_fr.' 19:00:00';*/

            $w_break_in_00 = DateAdd($date_loop_fr,1).' 00:00:01';
            $w_break_out_01 = DateAdd($date_loop_fr,1).' 01:00:00';

            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 07:00:00';
            $lwin = DateAdd($date_loop_fr,1).' 09:00:59';



            //===========================
            //===========================
            $date_next_1_day = DateAdd($date_loop_fr,1);

            $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


            if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy_next_1 = $__mmm->pol_id;
                }
            }

            $arr_pol_day = [1,2,3,5,6,10,11,9];
            if($em_policy_next_1 != $em_policy){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
                    $wout_7 =  DateAdd($date_loop_fr,1).' 07:00:00';
                    $lwin =  DateAdd($date_loop_fr,1).' 09:00:00';
                }
                $last_month = IDate::getLastDayMonth($date_loop_fr);
                $getDate = getDateDiffMinute($date_loop_fr,$last_month);
                if($getDate == 0){
                    $lwin =  DateAdd($date_loop_fr,1).' 02:00:00';
                }
            }
            //===========================
            //===========================
            //===========================






            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];



            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;

                if(getTimDiffMinute($fcheck, $win) >= 0){
                    $r_check_in = $win;
                }

                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                $r_check_out = $lcheck;
            }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $cal_wout = $lcheck;

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);

                    $w_meal = $total_time_work >= 660 ? 0.75 : 0;
                    //dd($total_time_work);
                    /* if ($total_time_work > 300) {
                     $total_time_work = $total_time_work - 60;// break
                 }else{
                     if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                         $total_time_work = $total_time_work - 60;// break
                     }
                 }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}',0), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }

                $total_time_work = getTotalTimeWork($r_check_in,$lcheck) -60;// need update just put

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {

                        $cal_win = $win;
                    } else{
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }
                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);

                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 660 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));

                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';

                        //=============================
                        //============================= break 18 - 19

                        /* if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0){
                         $total_ot_150 = getTimDiffMinute($w_break_out_19,$end_ot);
                     }*/

                        /* if(getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                         $total_ot_150 = getTimDiffMinute($start_ot,$w_break_in_18);
                     }*/

                        /* if(getTimDiffMinute($start_ot,$w_break_in_18) >= 0 && getTimDiffMinute($w_break_out_19,$end_ot) >= 0){
                         $total_ot_150 = $total_ot_150 - 60;
                     }*/

                        /* if(getTimDiffMinute($start_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$start_ot) >= 0 &&
                         getTimDiffMinute($end_ot,$w_break_out_19) >= 0 && getTimDiffMinute($w_break_in_18,$end_ot) >= 0){
                         $total_ot_150 = 0;
                     }*/

                        //=============================
                        //=============================
                        $real_ot = getRealMinuteOT($total_ot_150);

                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05

                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));


                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //===========================
                            //===========================
                            /* if(getTimDiffMinute($start_ot,$w_break_in_18) >=0){
                             $total_ot_150 = $total_ot_150 -60;
                         }*/

                            /* if(getTimDiffMinute($w_break_in_18,$start_ot) >=0 && getTimDiffMinute($start_ot,$w_break_out_19) >=0) {
                             $total_ot_150 = getTimDiffMinute($w_break_out_19,$win_10);
                         }*/

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

                            $real_ot = getRealMinuteOT($total_ot_150);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            //===================================================
                            $total_ot_200 = getTotalTimeWork($win_10, $end_ot);


                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            if(getTimDiffMinute($w_break_out_01,$end_ot)>=0){
                                $total_ot_200 = $total_ot_200 - 60;
                            }

                            if(getTimDiffMinute($w_break_in_00,$end_ot) >=0 && getTimDiffMinute($end_ot,$w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($win_10,$w_break_in_00);

                            }



                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));

                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';

                            //==================================
                            //==================================
                            if(getTimDiffMinute($start_ot,$w_break_in_00)>=0 && getTimDiffMinute($w_break_in_00,$end_ot) >= 0 &&
                                getTimDiffMinute($end_ot, $w_break_out_01) >=0){
                                $total_ot_200 = getTotalTimeWork($start_ot,$w_break_in_00);
                            }

                            if(getTimDiffMinute($w_break_in_00,$start_ot)>=0 && getTimDiffMinute($start_ot, $w_break_out_01) >=0
                                && getTimDiffMinute($w_break_out_01,$end_ot) >= 0){
                                $total_ot_200 = getTotalTimeWork($w_break_out_01,$end_ot);
                            }

                            if(getTimDiffMinute($start_ot ,$w_break_in_00) >0 && getTimDiffMinute($w_break_out_01,$end_ot)>0){
                                $total_ot_200 = $total_ot_200 - 1;
                            }
                            //==================================
                            //==================================
                            $real_ot = getRealMinuteOT($total_ot_200);
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05
                        $real_ot = getTotalTimeWork($win_10,$wout_5) -60;
                        ///  dd("$real_ot = getTotalTimeWork($win_10,$wout_5) -60;");
                        $ot_rate = 2;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        $real_ot = getRealMinuteOT($real_ot);
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$wout_5}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                        //==============================
                        if(getTimDiffMinute($lcheck,$wout_7) >= 0){
                            $real_ot = getTotalTimeWork($wout_5,$lcheck);
                            // dd("aaaaaaaaa  $real_ot = getTimDiffMinute($wout_5,$lcheck); ");

                        }else{
                            $real_ot = getTotalTimeWork($wout_5,$wout_7);
                            // dd("bbbbbbbbbbbbbbbb  $real_ot = getTimDiffMinute($wout_5,$wout_7);");
                        }


                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $real_ot = getRealMinuteOT($real_ot);
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$wout_5}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_9($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 9){//======================= policy 9==============
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            $fwin = $date_loop_fr.' 03:30:01';
            $win = $date_loop_fr.' 05:00:00';
            $wout = $date_loop_fr.' 13:00:00';
//            $w_break_in = $date_loop_fr.' 11:00:00';
//            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 03:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 04:00:00';
            $lwin = DateAdd($date_loop_fr,1).' 03:00:59';

            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*   if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                   $r_check_out = $lcheck;
               }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        $ration_1 = 0;

                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}','{$ration_1}')   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $ration_1 = getTotalTimeWork($r_check_in,$r_check_out) >= 300?1:0;

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}','{$ration_1}'), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}','0')   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;
                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*if ($r_check_in == null) {
                if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                    $r_check_in = $fcheck;
                }
            }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*   if ($total_time_work > 300) {
                       $total_time_work = $total_time_work - 60;// break
                   }else{
                       if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                           $total_time_work = $total_time_work - 60;// break
                       }
                   }*/

                    /*  if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                      && getTimDiffMinute($w_break_in,$lcheck) >0){
                      $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                  }else{
                      if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                          && getTimDiffMinute($w_break_in,$fcheck) >0){
                          $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                      }
                  }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    $ration_1 = $total_time_work >= 300?1:0;

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}','{$ration_1}'), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        //   $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_10($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 10){//======================= policy 10==============
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            $fwin = $date_loop_fr.' 04:30:01';
            $win = $date_loop_fr.' 06:00:00';
            $wout = $date_loop_fr.' 14:00:00';
//            $w_break_in = $date_loop_fr.' 11:00:00';
//            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 05:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:59';

            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*   if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                   $r_check_out = $lcheck;
               }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);


                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        $ration_1 = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}','{$ration_1}')   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $ration_1 = getTotalTimeWork($r_check_in,$r_check_out) >= 300?1:0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}','{$ration_1}'), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}','0')   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*if ($r_check_in == null) {
                if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                    $r_check_in = $fcheck;
                }
            }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);
                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*   if ($total_time_work > 300) {
                       $total_time_work = $total_time_work - 60;// break
                   }else{
                       if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                           $total_time_work = $total_time_work - 60;// break
                       }
                   }*/

                    /*  if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                      && getTimDiffMinute($w_break_in,$lcheck) >0){
                      $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                  }else{
                      if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                          && getTimDiffMinute($w_break_in,$fcheck) >0){
                          $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                      }
                  }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    $ration_1 = $total_time_work >= 300?1:0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}','{$ration_1}'), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        //   $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_11($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 11){//======================= policy 11==============
            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR 
						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            $fwin = $date_loop_fr.' 05:00:01';
            $win = $date_loop_fr.' 07:00:00';
            $wout = $date_loop_fr.' 15:00:00';
//            $w_break_in = $date_loop_fr.' 11:00:00';
//            $w_break_out = $date_loop_fr.' 11:59:59';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 04:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 06:59:59';
            $lwin = DateAdd($date_loop_fr,1).' 04:00:59';

            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");



            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*   if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                   $r_check_out = $lcheck;
               }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);

                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        $ration_1 = 0;

                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}','{$ration_1}')   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $ration_1 = getTotalTimeWork($r_check_in,$r_check_out) >= 300?1:0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}','{$ration_1}'), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}','0')   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*if ($r_check_in == null) {
                if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                    $r_check_in = $fcheck;
                }
            }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);

                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*   if ($total_time_work > 300) {
                       $total_time_work = $total_time_work - 60;// break
                   }else{
                       if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                           $total_time_work = $total_time_work - 60;// break
                       }
                   }*/

                    /*  if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                      && getTimDiffMinute($w_break_in,$lcheck) >0){
                      $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                  }else{
                      if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                          && getTimDiffMinute($w_break_in,$fcheck) >0){
                          $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                      }
                  }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    $ration_1 = $total_time_work >= 300?1:0;


                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}','{$ration_1}'), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        // $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05

                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_12($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 12){//======================= policy 12 ==============
//            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
//            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR
//						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            //==========

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            //==========
            $fwin = $date_loop_fr.' 13:00:00';
            $win = $date_loop_fr.' 16:00:00';
            $wout = $date_loop_fr.' 22:00:00';

            $w_break_in_18 = $date_loop_fr.' 18:00:00';
            $w_break_out_19 = $date_loop_fr.' 19:00:00';

            $w_break_in__ = $date_loop_fr.' 23:59:59';
            $w_break_out__ = DateAdd($date_loop_fr,1).' 01:00:00';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 07:00:00';
            $lwin = DateAdd($date_loop_fr,1).' 09:00:00';

            //===========================
            //===========================
            $date_next_1_day = DateAdd($date_loop_fr,1);

            $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


            if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy_next_1 = $__mmm->pol_id;
                }
            }
            $arr_pol_day = [1,2,3,5,6,10,11,9];
            if($em_policy_next_1 != $em_policy){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
                    $wout_7 =  DateAdd($date_loop_fr,1).' 07:00:00';
                    $lwin =  DateAdd($date_loop_fr,1).' 09:00:00';
                }
                $last_month = IDate::getLastDayMonth($date_loop_fr);
                $getDate = getDateDiffMinute($date_loop_fr,$last_month);
                if($getDate == 0){
                    $lwin =  DateAdd($date_loop_fr,1).' 02:00:00';
                }
            }
            //===========================
            //===========================
            //===========================

            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");

            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*   if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                   $r_check_out = $lcheck;
               }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));

                    $total_time_work = getRealMinute($total_time_work);

                    //  dd("$total_time_work = getTimDiffMinute(($cal_win), ($lcheck));");
                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        $ration_1 = 0;


                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}','{$ration_1}')   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    $ration_1 = 0;
                    if(getTimDiffMinute($fcheck,$w_break_in_18) >=0 && getTimDiffMinute($w_break_out_19,$lcheck) >=0 ){
                        $ration_1  = 1;
                    }
                    if (getTimDiffMinute($fcheck,$w_break_in__) >= 0 && getTimDiffMinute($w_break_out__,$lcheck) >= 0){
                        $ration_1  = 1;
                    }
                    //$ration_1 = getTimDiffMinute($r_check_in,$r_check_out) >= 300?1:0;
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}','{$ration_1}'), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}','0')   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

                if($total_time_work >0) {
                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*if ($r_check_in == null) {
                if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                    $r_check_in = $fcheck;
                }
            }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }
                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));

                    if(getTimDiffMinute($cal_win,$w_break_in_18) >=0  && getTimDiffMinute($w_break_out__,$lcheck) >=0){
                        $total_time_work= $total_time_work-60;
                    }

                    if(getTimDiffMinute($w_break_in_18,$cal_win) >=0  && getTimDiffMinute($w_break_out__,$lcheck) >=0
                        && getTimDiffMinute($cal_win,$w_break_out_19) >0 && getTimDiffMinute($w_break_in_18,$cal_win) <=30){
                        $total_time_work= $total_time_work-30;

                    }


                    $total_time_work = getRealMinute($total_time_work);

                    $w_meal = getTotalTimeWork(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*   if ($total_time_work > 300) {
                       $total_time_work = $total_time_work - 60;// break
                   }else{
                       if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                           $total_time_work = $total_time_work - 60;// break
                       }
                   }*/

                    /*  if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                      && getTimDiffMinute($w_break_in,$lcheck) >0){
                      $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                  }else{
                      if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                          && getTimDiffMinute($w_break_in,$fcheck) >0){
                          $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                      }
                  }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;


                    $is_night = 0;
                    //  $ration_1 = $total_time_work >= 300?1:0;
                    $ration_1 = 0;

                    //dd("getTimDiffMinute($fcheck,$w_break_in_18)");
                    //dd(getTimDiffMinute($w_break_out_19,$lcheck));
                    if(getTimDiffMinute($fcheck,$w_break_in_18) >=0 && getTimDiffMinute($w_break_out_19,$lcheck) >=0 ){
                        $ration_1  = 1;
                    }
                    if (getTimDiffMinute($fcheck,$w_break_in__) >= 0 && getTimDiffMinute($w_break_out__,$lcheck) >= 0){
                        $ration_1  = 1;
                    }
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}','{$ration_1}'), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";
                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);

                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        // $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05
                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }

                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));
                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05
                        $real_ot = getTotalTimeWork($win_10,$wout_5);
                        $real_ot = getRealMinuteOT($real_ot);
                        $ot_rate = 2;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        // getRealMinuteOT
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$wout_5}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                        //==============================
                        if(getTimDiffMinute($lcheck,$wout_7) >= 0){
                            $real_ot = getTotalTimeWork($wout_5,$lcheck);
                        }else{
                            $real_ot = getTotalTimeWork($wout_5,$wout_7);
                        }


                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $real_ot = getRealMinuteOT($real_ot);
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$wout_5}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");


                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============
    }


    function getPol_13($em_policy,$emp_id,$date_loop_fr){


        if ($em_policy == 13){//======================= policy 13 ==============
//            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(chin) = '{$date_loop_fr}'  )  ");
//            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( DATE(attend) = '{$date_loop_fr}' OR
//						  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");

            runSQL(" DELETE FROM em_checkinoutot WHERE emp_id='{$emp_id}' AND ( DATE(normal_CHECKTIME) = '{$date_loop_fr}'  )  ");
            runSQL(" DELETE FROM em_checkinout WHERE emp_id='{$emp_id}' AND ( 	  DATE(normal_CHECKTIME) = '{$date_loop_fr}'  ) ");
            $fwin = $date_loop_fr.' 14:00:00';
            $win = $date_loop_fr.' 19:00:00';
            $wout = $date_loop_fr.' 22:00:00';
            $w_break_in__ = $date_loop_fr.' 23:59:59';
            $w_break_out__ = DateAdd($date_loop_fr,1).' 01:00:00';
            $win_10 = $date_loop_fr.' 22:00:00';
            $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
            $wout_7 = DateAdd($date_loop_fr,1).' 07:00:00';
            $lwin = DateAdd($date_loop_fr,1).' 09:00:00';

            //===========================
            //===========================
            $date_next_1_day = DateAdd($date_loop_fr,1);

            $em_policy_next_1 = getOne("SELECT pol_id FROM em_dialy_policy WHERE 
                                    emp_id = '{$emp_id}' AND DATE(pol_date) = '{$date_next_1_day}'") ;


            if ($em_policy_next_1 <= 0 || $em_policy_next_1 == '') {
                $__mmm = Employee::select('pol_id')->where('id',$emp_id)->first();
                if($__mmm != null) {
                    $em_policy_next_1 = $__mmm->pol_id;
                }
            }

            $arr_pol_day = [1,2,3,5,6,10,11,9];
            if($em_policy_next_1 != $em_policy){
                if( in_array($em_policy_next_1,$arr_pol_day)){

                    $wout_5 = DateAdd($date_loop_fr,1).' 05:00:00';
                    $wout_7 =  DateAdd($date_loop_fr,1).' 07:00:00';
                    $lwin =  DateAdd($date_loop_fr,1).' 09:00:00';
                }
                $last_month = IDate::getLastDayMonth($date_loop_fr);
                $getDate = getDateDiffMinute($date_loop_fr,$last_month);
                if($getDate == 0){
                    $lwin =  DateAdd($date_loop_fr,1).' 02:00:00';
                }

            }
            //===========================
            //===========================
            //===========================

            $checkinout_ = getOneRow("SELECT 
                                                MIN(attend) as fcheck  ,
                                                MAX(attend) as lcheck
                                          FROM time_attends WHERE emp_id = '{$emp_id}' AND 
                                          attend >= '{$fwin}'
                                            AND attend <= '{$lwin}'  AND `is_not_run`  =0  ");



            $fcheck = $checkinout_['fcheck'];
            $lcheck = $checkinout_['lcheck'];

            //=============================
            //=============================
            $s_day = getOne("SELECT DAYNAME ('".$fcheck."')");

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

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

            if($s_day == 'Sunday' || $h_d_rate>0){
                //============ sun day =====================
                //============ sun day =====================
                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /*if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                $r_check_in = $fcheck;
            }*/
                /*   if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check afternoon
                   $r_check_out = $lcheck;
               }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 05
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /* if ($r_check_in == null) {
                 if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                     $r_check_in = $fcheck;
                 }
             }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }

                if(getTimDiffMinute($r_check_out,$lcheck)>0){
                    $r_check_out = $lcheck;
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($lcheck));
                    $total_time_work = getRealMinute($total_time_work);

                    $total_time_work = $total_time_work >= 720 ? 720 :$total_time_work;
                    $w_meal = $total_time_work >= 600 ? 0.75 : 0;
                    /*if ($total_time_work > 300) {
                    $total_time_work = $total_time_work - 60;// break
                }else{
                    if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                        $total_time_work = $total_time_work - 60;// break
                    }
                }*/

                    /* if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                     && getTimDiffMinute($w_break_in,$lcheck) >0){
                     $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                 }else{
                     if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                         && getTimDiffMinute($w_break_in,$fcheck) >0){
                         $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                     }
                 }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {
                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;
                        $ration_1 = 0;
                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}','{$ration_1}')   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {


                    $haft_in_hour = 0;// $total_time_work / 2;
                    $haft_out_hour = 0;// $total_time_work / 2;
                    $is_night = 0;
                    // $ration_1 = getTimDiffMinute($r_check_in,$r_check_out) >= 360?1:0;
                    $ration_1 = 0;
                    if (getTimDiffMinute($fcheck,$w_break_in__) >= 0 && getTimDiffMinute($w_break_out__,$lcheck) >= 0){
                        $ration_1  = 1;
                    }

                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                    '{$emp_id}',
                                     '{$r_check_in}',
                                      'in',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                      '0',
                                     '0',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                      '{$haft_in_hour}',
                                     '{$is_night}',
                                      '{$r_check_in}','{$ration_1}'), 
                                      ( 'api',
                                    '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$r_check_in}','0')   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);
                }

                if($total_time_work >0) {

                    $ottype = $s_day == 'Sunday'?'otsun':'othol';
                    $ot_rate = $s_day == 'Sunday'?2:$h_d_rate;

                    $real_ot =  getRealMinuteOT($total_time_work);
                    $meal_10_i = 0;
                    $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$r_check_in}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}')
                                      ");
                }
                //=================== calculate total work ==================


                //============ sun day =====================
                //============ sun day =====================
                //============ sun day =====================
            }else {

                $r_check_in = null;
                $r_check_out = null;
                $r_check_in = $fcheck;
                /* if (getTimDiffMinute($fcheck, $w_break_in) >= 0) {// check morning
                 $r_check_in = $fcheck;
             }*/
                /* if (getTimDiffMinute($lcheck, $wout) >= 0 && getTimDiffMinute($w_break_in, $lcheck) >= 0) {// check morning
                 $r_check_out = $lcheck;
             }*/
                if (getTimDiffMinute($lcheck, $lwin) >= 0 && getTimDiffMinute($wout, $lcheck) >= 0) {// 16 - 22
                    if ($r_check_in == null) {
                        $r_check_out = null;
                    } else {
                        $r_check_out = $lcheck;
                    }
                }

                /*if ($r_check_in == null) {
                if (getTimDiffMinute($fcheck, $wout) > 0 && getTimDiffMinute($w_break_in, $fcheck) > 0) {
                    $r_check_in = $fcheck;
                }
            }*/

                if ($r_check_in != null && $r_check_out == null) {
                    if (getTimDiffMinute($r_check_in, $lcheck) > 0) {
                        $r_check_out = $lcheck;
                    }
                }
                //=================== calculate total work ==================
                $total_time_work = 0;
                $w_meal = 0;
                if ($r_check_in != null && $r_check_out != null) {
                    if (getTimDiffMinute($r_check_in, $win) >= 0) {
                        $cal_win = $win;
                    } else {
                        $cal_win = $r_check_in;
                    }

                    if (getTimDiffMinute($wout, $r_check_out) >= 0) {
                        $cal_wout = $wout;
                    } else {
                        $cal_wout = $r_check_out;
                    }

                    $total_time_work = getTotalTimeWork(($cal_win), ($cal_wout));
                    $total_time_work = getRealMinute($total_time_work);

                    // echo "checkin = $r_check_in |||||||| check out = $r_check_out ||||||||| $cal_win - $cal_wout = $total_time_work </br>";


                    $w_meal = getTimDiffMinute(($cal_win), ($lcheck)) >= 600 ? 0.75 : 0;
                    /*   if ($total_time_work > 300) {
                       $total_time_work = $total_time_work - 60;// break
                   }else{
                       if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($w_break_out,$lcheck) >0){
                           $total_time_work = $total_time_work - 60;// break
                       }
                   }*/

                    /*  if(getTimDiffMinute($cal_win,$w_break_in) > 0 && getTimDiffMinute($lcheck,$w_break_out) >0
                      && getTimDiffMinute($w_break_in,$lcheck) >0){
                      $total_time_work = getTimDiffMinute($cal_win,$w_break_in);
                  }else{
                      if(getTimDiffMinute($w_break_out,$lcheck) > 0 && getTimDiffMinute($fcheck,$w_break_out) >0
                          && getTimDiffMinute($w_break_in,$fcheck) >0){
                          $total_time_work = getTimDiffMinute($w_break_out,$cal_wout);
                      }
                  }*/

                }

                if ($r_check_in == null && $r_check_out == null) {
                    if (getTimDiffMinute($lcheck, $lwin) > 0) {

                        $haft_in_hour = 0;
                        $haft_out_hour = 0;
                        $is_night = 0;


                        if(!IsNullOrEmptyString($lcheck))
                            $this->arrSQL_all_fun[] = "  ( 'api',
                                    '{$emp_id}',
                                     '{$lcheck}',
                                     'out',
                                   '0',
                                  '0',
                                    '0',
                                    '0',
                                    '0',
                                '{$em_policy}',
                                  '0',
									'0',
                                   '{$haft_out_hour}',
                                    '{$is_night}',
                                   '{$lcheck}',0)   
                            ";
                        IDate::generateTimeAttend($emp_id,$lcheck,null,$lcheck,0);

                    }
                } else {
                    $_early = getTimDiffMinute($win, $fcheck);
                    $early = $_early < 0 ? -$_early : 0;
                    $late = $_early > 0 ? $_early : 0;
                    $late = getRealLate($late);
                    $_before = getTimDiffMinute($wout, $lcheck);

                    $before = $_before < 0 ? -$_before : 0;
                    $before = getRealLate($before);
                    $time_late = convertime($late * 60);
                    $time_before = convertime($before * 60);
                    $time_early = convertime($early * 60);
                    $after = $_before > 0 ? $_before : 0;
                    $time_after = convertime($after * 60);
                    $total_time_work = getRealMinute($total_time_work);

                    $haft_in_hour = $total_time_work / 2;
                    $haft_out_hour = $total_time_work / 2;

                    // dd("$total_time_work");


                    $is_night = 0;
                    //$ration_1 = $total_time_work >= 300?1:0;
                    //  $ration_1 = getTimDiffMinute($r_check_in,$r_check_out) >= 300 ? 1:0;
                    $ration_1 = 0;
                    if (getTimDiffMinute($fcheck,$w_break_in__) >= 0 && getTimDiffMinute($w_break_out__,$lcheck) >= 0){
                        $ration_1  = 1;
                    }
                    if(!IsNullOrEmptyString($r_check_in))
                        $this->arrSQL_all_fun[] = "( 'api',
                                     '{$emp_id}',
                                     '{$r_check_in}',
                                     'in',
                                     '{$late}',
                                     '{$time_late}',
                                     '{$early}',
                                     '{$time_early}',
                                     '{$time_early}',
                                     '{$early}',
                                     '{$em_policy}',
                                     '{$w_meal}',
                                     '{$haft_in_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}','{$ration_1}'), 
                                      ('api',
                                     '{$emp_id}',
                                     '{$r_check_out}',
                                     'out',
                                     '{$before}',
                                     '{$time_before}',
                                     '{$after}',
                                     '{$time_after}',
                                     '{$time_after}',
                                     '{$after}',
                                     '{$em_policy}',
									 '0',
                                     '{$haft_out_hour}',
                                     '{$is_night}',
                                     '{$r_check_in}',0)   
                            ";

                    IDate::generateTimeAttend($emp_id,$r_check_in,$r_check_in,$r_check_out,$haft_out_hour*2);


                }


                //=================== calculate total work ==================

                // ================ calculate ot ==============================
                $start_ot = null;
                $end_ot = null;
                if ($r_check_in != null && $r_check_out != null) {
                    $start_ot = $wout;
                }

                if ($r_check_in == null && getTimDiffMinute($wout, $fcheck) >= 0) {
                    $start_ot = $fcheck;
                }

                if ($start_ot != null) {
                    if (getTimDiffMinute($wout, $lcheck) >= 0 && getTimDiffMinute($lcheck, $lwin) >= 0) {
                        $end_ot = $lcheck;
                    }
                }

                $total_ot_150 = 0;
                $total_ot_200 = 0;
                if ($start_ot != null && $end_ot != null) {
                    if (getTimDiffMinute($end_ot, $win_10) >= 0) {// out before 22
                        //  $total_ot_150 = getTimDiffMinute(($start_ot), ($end_ot));
                        $total_ot_150 = getTotalTimeWork(($start_ot), ($end_ot));
                        $real_ot = getRealMinuteOT($total_ot_150);
                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        if ($real_ot > 0) {
                            $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}'  )");
                        }

                    } else if (getTimDiffMinute($end_ot, $wout_5) >= 0) {// // out before 05

                        if (getTimDiffMinute($start_ot, $win_10) >= 0) {
                            $total_ot_150 = getTotalTimeWork(($start_ot), ($win_10));

                            $real_ot = getRealMinuteOT($total_ot_150);
                            $ot_rate = 1.5;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$win_10}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}' ,
                                       '{$fcheck}' )");
                            }
                            if(getTimDiffMinute($win_10,$fcheck) >=0){
                                $win_10 = $fcheck;
                            }else{
                                $win_10 = $win_10;
                            }
                            $total_ot_200 = getTotalTimeWork(($win_10), ($end_ot));


                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }


                        } else {// check in after 22 and out before 05
                            $total_ot_200 = getTotalTimeWork(($start_ot), ($end_ot));

                            $real_ot = getRealMinuteOT($total_ot_200);
                            $ot_rate = 2;
                            $meal_10_i = 0;
                            $ottype = 'ot';
                            if ($real_ot > 0) {
                                $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$start_ot}',
                                      '{$end_ot}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                            }
                        }
                    } else if (getTimDiffMinute($end_ot, $lwin) >= 0 && getTimDiffMinute($wout_5, $end_ot) >= 0) {// // out before 05
                        if(getTimDiffMinute($win_10,$fcheck) >=0){
                            $win_10 = $fcheck;
                        }else{
                            $win_10 = $win_10;
                        }

                        $real_ot = getTotalTimeWork($win_10,$wout_5);


                        $ot_rate = 2;
                        $meal_10_i = 0;
                        $ottype = 'ot';
                        $real_ot = getRealMinuteOT($real_ot);
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$win_10}',
                                      '{$wout_5}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");
                        //==============================
                        if(getTimDiffMinute($lcheck,$wout_7) >= 0){
                            $real_ot = getTotalTimeWork($wout_5,$lcheck);
                        }else{
                            $real_ot = getTotalTimeWork($wout_5,$wout_7);
                        }


                        $ot_rate = 1.5;
                        $meal_10_i = 0;
                        $real_ot = getRealMinuteOT($real_ot);
                        $this->arrOT_all_fun[] = (" ( 'api',
                                      '{$emp_id}',
                                     '{$wout_5}',
                                      '{$lcheck}',
                                    '{$ottype}',
                                      '{$real_ot}', 
                                      '{$real_ot}',
                                      '1',
                                      '1',
                                      '{$em_policy}',
                                     '{$ot_rate}',
                                      '{$meal_10_i}',
                                       '{$fcheck}' ) ");


                    }
                }


                // ================ calculate ot ==============================
            }


        }
        /////======================= policy 1==============

    }

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




}