<?php
/**
 * Created by PhpStorm.
 * User: DEV1776
 * Date: 1/13/2020
 * Time: 4:33 PM
 */

namespace App\Helpers;


use App\Models\EmpLeave;
use App\Models\EmployeeLeaveTotal;

class dev1776_
{
// code by software developer 1776

//if (function_exists('check_permission_if_exists_in_sidebar')) {
// check_permission_if_exists_in_sidebar is defined
    static function canTakeLeave($em_id = null, $leave_id= null, $from_date = null, $to_date = null){

        $getTotalLeave___ = EmployeeLeaveTotal::getEmployeeLeaveTotal($em_id,$from_date, $to_date,null,$leave_id );
//        $getTotalLeaveUsed___ = EmpLeave::getTotalAuthorizeTakeLeaveOfEmployeeByIdType($em_id, $leave_id);
        $getTotalLeaveUsed___ = EmpLeave::getTotalTakeLeaveOfEmployeeByIdType($em_id, $leave_id);
        $ask = dev1776_::DatePeriod($from_date, $to_date);
        //dd(gettype($getTotalLeave___->dd()),  $getTotalLeaveUsed___);

        $t = abs($getTotalLeave___)  -abs($getTotalLeaveUsed___)  -abs(count($ask) + 1);
        //dd(abs($getTotalLeave___), -abs($getTotalLeaveUsed___), abs($getTotalLeave___)  -abs($getTotalLeaveUsed___), -abs(count($ask) + 1), $t);;
        if ($t == 0 || $t >0){
            return true;
        }
        return false;
    }
    static function DatePeriod($from_date, $to_date){
        $period = new \DatePeriod(
            new \DateTime($from_date),
            new \DateInterval('P1D'),
            new \DateTime($to_date)
        );
        $arr = [];
        foreach ($period as $key => $value) {
            $arr[] = $value->format('Y-m-d') ;
        }
        return $arr;
    }

   public static function dateIsBiggerThanOrEqual($fieldName='', $date){
        if ($fieldName !=''){
            $q = where($fieldName, '>=', $date.' 00:00:00');
            return $q;
        }
        return '';
    }

   public static function getTextInBracket($text = null)
    {
        if ($text != null) {
            preg_match('#\((.*?)\)#', $text, $match);
            return $match[1];
            //Ex: hello(world)
            //out put : world
        }
        return null;
    }

   public static function getTextExceptInBracket($text = null)
    {

        if ($text != null) {
            $txt = preg_replace("/\([^)]+\)/", "", $text);
            return $txt;
            //Ex: hello(world)
            //out put : hello
        }
        return null;
    }

   public static function datedMY($date)
    {
        if ($date != null) {
            $d = date('d M Y', strtotime($date));
            return $d;
            //Ex: 2019-11-1 04:01:57
            //out put : 1 Nov 2019
        }
        return '';
    }
   public static function dateDifferentForHuman($date)
    {
        if ($date != null) {
            $d = \Carbon\Carbon::parse($date)->diffForHumans();
            return $d;
        }
        return '';
    }

   public static function shinHan($company = null)
    {
        if (companyReportPart() == "company.shinhan_bank") {
            return true;
        }
        return false;
    }

   public static function settingKey($key){
        if ($key != null){
            $d = \App\Models\Setting::where('key', $key)->first();
            if ($d !=null){
                return $d->value;
            }
        }
        return;
    }
   public static function underscoreDashToSpace($str){
        if (!is_null( $str)){
            return str_replace('-',' ',str_replace('_',' ', $str));
        }
        return;
    }

   public static function datediffy($f_date, $t_date){
        $date1 = new DateTime($f_date);
        $date2 = new DateTime($t_date);
        $interval = $date1->diff($date2);
        return  $interval->y ;
    }
   public static function datediffm($f_date, $t_date){
        $date1 = new DateTime($f_date);
        $date2 = new DateTime($t_date);
        $interval = $date1->diff($date2);
        return  $interval->m ;
    }
   public static function datediffd($f_date, $t_date){
        $date1 = new DateTime($f_date);
        $date2 = new DateTime($t_date);
        $interval = $date1->diff($date2);
        return  $interval->d ;
    }

   public static function datediffTotalAmountOfDays($f_date, $t_date){
        $date1 = new DateTime($f_date);
        $date2 = new DateTime($t_date);
        $interval = $date1->diff($date2);
        return  $interval->days ;
    }

   public static function dateDifferenceymd($date_1 , $date_2 , $differenceFormat = '%a' )
    {
        $format = '%y Year %m Month %d Day';
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);

        $interval = date_diff($datetime1, $datetime2);

        return $interval->format($format);

    }
   public static function dateDifferenceym($date_1 , $date_2 , $differenceFormat = '%a' )
    {
        $format = '%y Year %m Month ';
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);

        $interval = date_diff($datetime1, $datetime2);

        return $interval->format($format);

    }
   public static function dateDifferenceYDoteM($date_1 , $date_2 , $differenceFormat = '%a' )
    {
        $format = '%y.%m';
        $datetime1 = date_create($date_1);
        $datetime2 = date_create($date_2);

        $interval = date_diff($datetime1, $datetime2);

        return $interval->format($format);

    }






}