<?php

function getalias($title, $alias_name, $table)
{
    $ci = &get_instance();
    $new_string = trim(preg_replace('/[^A-Za-z0-9_]/', ' ', strip_tags($title)));
    $new_string = preg_replace('/\s+/', ' ', $new_string);
    $alias = strtolower(str_replace(" ", "-", trim($new_string)));
    $ci->load->model('MHelper');
    $a = $ci->MHelper->checkIfAliasExist($alias, $alias_name, $table);
    $c = 1;
    $newalias = $alias;
    while ($ci->MHelper->checkIfAliasExist($newalias, $alias_name, $table)) {
        $newalias = $alias . $c;
        $c++;
    }
    return $newalias;
}
function createalias($text)
{
    $ci = &get_instance();
    $new_string = trim(preg_replace('/[^A-Za-z0-9_]/', ' ', strip_tags($text)));
    $new_string = preg_replace('/\s+/', ' ', $new_string);
    $alias = strtolower(str_replace(" ", "-", trim($new_string)));
    $alias = str_replace("-", "", $alias);
    return $alias;
}

function getLocationsForMenu()
{
    $ci = &get_instance();
    $ci->load->model('MSubmission');
    $Locations = $ci->MSubmission->getLocation();
    return $Locations;
}

function getDisplayOrder($table)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $max = $ci->MHelper->getMaxDisplayOrder($table);
    return $max + 1;
}
function countHolidayByDaterangeInFactory($from_date, $to_date)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getHolidayByDaterangeInFactory($from_date, $to_date);
    return $Holidays;
}

function countTotaldaysByDaterange($from_date, $to_date)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalDaysByDaterange($from_date, $to_date);
    //pre($Holidays);
    return $Holidays;
}
function countTotalPresentsdaysByDaterange($from_date, $to_date, $id)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalPresentsDaysByDaterange($from_date, $to_date, $id);
    return $Holidays;
}

function countTotalPaidLeavesByDaterange($from_date, $to_date, $id)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalPaidLeavesByDaterange($from_date, $to_date, $id);
    return $Holidays;
}
function countTotalUnPaidLeavesByDaterange($from_date, $to_date, $id)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalUnPaidLeavesByDaterange($from_date, $to_date, $id);
    return $Holidays;
}
function countTotalAnnualLeavesByDaterange($from_date, $to_date, $id)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalAnnualLeavesByDaterange($from_date, $to_date, $id);
    return $Holidays;
}
function countTotalSickLeavesByDaterange($from_date, $to_date, $id)
{
    $ci = &get_instance();
    $ci->load->model('MHelper');
    $Holidays = $ci->MHelper->getTotalSickLeavesByDaterange($from_date, $to_date, $id);
    return $Holidays;
}
function getImagePath($path)
{
    return str_replace(base_url(), '', $path);
}
function pre($array, $death = false)
{
    echo '<pre>';
    print_r($array);
    echo '</pre>';
    if ($death) die();
}

function image_url($path = '')
{
    $path = explode("/", $path);
    $image = end($path);
    return config_item('kcfinder_path') . $image;
}


function myDate($originalDate)
{
    return date("F d Y", strtotime($originalDate));
}

function dbDate($date)
{
    return date("Y-m-d", strtotime($date));
}

function myTime($originalDate)
{
    return date("g:i A", strtotime($originalDate));
}

function myDateTime($originalDate)
{
    return date("F d Y g:i A", strtotime($originalDate));
}
function myDaysDiff($fromDate, $toDate)
{
    $fromDate = strtotime($fromDate);
    $toDate = strtotime($toDate);
    $datediff = $toDate - $fromDate;
    return round($datediff / (60 * 60 * 24));
}
function firstDayOfNepaliMonth($engDate = "")
{
    $engDate = ($engDate != "") ? $engDate : date("Y-m-d");
    $NepaliDate = NepaliDate($engDate);
    $nD = explode("-", $NepaliDate);
    $Day = '1';
    $Month = $nD[1];
    if (strlen($Month) == 1) {
        $Month = '0' . $Month;
    }
    $Year = $nD[0];
    $ci = &get_instance();
    $t = "select bs_date from tbl_nepengcalendar where bs_date='" . $Year . "-" . $Month . "-" . $Day . "'";
    return $ci->db->query($t)->row()->bs_date;
}
function firstDayOfNepaliMonth1($engDate = "")
{
    $engDate = ($engDate != "") ? $engDate : date("Y-m-d");
    $NepaliDate = NepaliDate($engDate);
    $nD = explode("-", $NepaliDate);
    $Day = '1';
    $Month = $nD[1];
    if ($Month == 11 || $Month == 12  || $Month == 10) {
        $Month = $nD[1];
    } else {
        $Month = '0' . $nD[1];
    }

    $Year = $nD[0];
    $ci = &get_instance();

    $t = "select ad_date from tbl_nepengcalendar where bs_date='" . $Year . "-" . $Month . "-" . $Day . "'";
    //echo $t;die;
    return $ci->db->query($t)->result()[0]->ad_date;
}
function lastDayOfNepaliMonth($engDate = "")
{
    $engDate = ($engDate != "") ? $engDate : date("Y-m-d");
    //echo $engDate;
    $NepaliDate = NepaliDate1($engDate);
    //	echo $NepaliDate;
    $nD = explode("-", $NepaliDate);
    //print_r($nD);
    $Day = '1';
    $Month = $nD[1];
    $Year = $nD[0];
    $ci = &get_instance();
    $t = "select ad_date from tbl_nepengcalendar where bs_date like '" . $Year . "-" . $Month . "-%' order by ad_date";
    //	echo $t;
    $dates = $ci->db->query($t)->result();
    //print_r($dates);
    $date = $dates[sizeof($dates) - 1];
    //  print_r($date);die;
    return $date->ad_date;
}
function EnglishDate($NepaliYear, $NepaliMonth, $NepaliDay)
{
    $ci = &get_instance();
    if ($NepaliMonth < 10) $NepaliMonth = "0" . $NepaliMonth;
    $bs_date = $NepaliYear . "-" . $NepaliMonth . "-" . $NepaliDay;
    $t = "select ad_date from tbl_nepengcalendar where bs_date='$bs_date'"; //echo $t;
    $q = $ci->db->query($t)->row();
    return $q->ad_date;
}
function Today()
{
    return date("Y-m-d");
}
function NepaliToEnglishDate($NepaliDate)
{
    $NepaliDate = trim($NepaliDate);
    if (trim($NepaliDate) == "") return date("Y-m-d");
    $ci = &get_instance();
    $NepaliDate = explode("-", $NepaliDate);
    $NepaliMonth = intval($NepaliDate[1]);
    $NepaliYear = intval($NepaliDate[0]);
    $NepaliDay = intval($NepaliDate[2]);
    if ($NepaliMonth < 10) $NepaliMonth = "0" . $NepaliMonth;
    //if($NepaliDay<10)$NepaliDay="0".$NepaliDay;
    $bs_date = $NepaliYear . "-" . $NepaliMonth . "-" . $NepaliDay;
    $t = "select ad_date from tbl_nepengcalendar where bs_date='$bs_date'"; //echo $t;
    $q = $ci->db->query($t)->row();
    return $q->ad_date;
}
function NepaliDate($engDate = "")
{
    $engDate = ($engDate != "") ? $engDate : date("Y-m-d");
    if ($engDate != "0000-00-00") {
        $ci = &get_instance();
        return str_replace("-0", "-", $ci->db->query("select bs_date from tbl_nepengcalendar where ad_date='" . $engDate . "'")->result()[0]->bs_date);
    } else {
        return "";
    }
}
function NepaliDate1($engDate = "")
{
    $engDate = ($engDate != "") ? $engDate : date("Y-m-d");
    $ci = &get_instance();
    return $ci->db->query("select bs_date from tbl_nepengcalendar where ad_date='" . $engDate . "'")->result()[0]->bs_date;
}
function NepaliMonth($engDate = "")
{
    $NepaliDate = NepaliDate($engDate);
    $nD = explode("-", $NepaliDate);
    $Month = $nD[1];
    return intval($Month);
}
function NepaliYear($engDate = "")
{
    $NepaliDate = NepaliDate($engDate);
    $nD = explode("-", $NepaliDate);
    $Year = $nD[0];
    return intval($Year);
}
function NepaliMonthNameByNumber($number)
{
    $MonthNames = array("Baisakh", "Jestha", "Ashad", "Shrawan", "Bhadra", "Asoj", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra");
    return $MonthNames[$number - 1];
}
function NepaliMonthName($engDate = "")
{
    $Month = NepaliMonth($engDate);
    $MonthNames = array("Baisakh", "Jestha", "Ashad", "Shrawan", "Bhadra", "Asoj", "Kartik", "Mangsir", "Poush", "Magh", "Falgun", "Chaitra");
    return $MonthNames[$Month - 1];
}
function N2($N)
{
    return number_format($N, 2, '.', ',');
}
function loadView($viewName, $data = '')
{
    $ci = &get_instance();
    $ci->load->view('includes/header', $data);
    $ci->load->view('includes/nav', $data);
    $ci->load->view('includes/sidebar', $data);
    $ci->load->view($viewName, $data);
    $ci->load->view('includes/footer', $data);
}

function getUrlArray()
{
    $host = 'http' . ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') ? 's' : '')
        . '://' . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
    $host = str_replace(base_url(), "", $host);
    $host = str_replace('-', '_', $host);
    $host = explode("/", $host);
    return $host;
}

function showMenu($Display = "Balance By Account Groups", $Link = "accounts/reports/balance_by_group", $Icon = "fa-circle")
{
    $ci = &get_instance();
    $command = $ci->uri->segment(1);
    $param = $ci->uri->segment(2);
    $subparam = $ci->uri->segment(3);
    $subparam2 = $ci->uri->segment(4);
    $Link = explode("/", $Link);
    $Link1 = isset($Link[1]) ? $Link[1] : '';
    $Link2 = isset($Link[2]) ? $Link[2] : '';
    echo '<li class="nav-item">';
    echo '<a href="' . site_url($Link) . '" class="nav-link ' . ($param == $Link1 && $subparam == $Link2 ? "active" : "") . '">';
    //   echo '<i class="far ' . $Icon . ' nav-icon"></i>';
    echo '<p>' . $Display . '</p>';
    echo '</a>';
    echo '</li>';
}