<?php
if (!defined('BASEPATH'))
    exit('No direct script access allowed');

function calculate_end_date($loan_date, $no_of_terms, $holidays)
{
    $date = new DateTime($loan_date);
    $terms_count = 0;

    while ($terms_count < $no_of_terms) {
        $date->modify('+1 day');
        $current = $date->format('Y-m-d');

        if (in_array($current, $holidays)) {
            continue; // skip holiday
        }
        $terms_count++;
    }

    return $date->format('d-m-Y'); // return in dd-mm-yyyy
}

// Helper for daily reschedule
function calculate_reschedule_end_date($loan_date, $no_of_terms, $holidays)
{
    $date = new DateTime($loan_date);
    $terms_count = 0;

    while ($terms_count < $no_of_terms) {
        $current = $date->format('Y-m-d');

        // Skip holiday
        if (in_array($current, $holidays)) {
            $date->modify('+1 day');
            continue;
        }

        // Count this day as a valid term
        $terms_count++;

        // Only increment date if we still need more terms
        if ($terms_count < $no_of_terms) {
            $date->modify('+1 day');
        }
    }

    return $date->format('Y-m-d'); // final valid term
}




if (!function_exists('display_date_time_format')) {
    function display_date_time_format($datetime)
    {
        return date('d/m/Y  H:i A', strtotime($datetime)); //$dateTime->format("d/m/y  H:i A"); 
    }
}
if (!function_exists('asset_url')) {
    function asset_url($type = 0)
    {
        return base_url() . 'thems/';
    }
}
function image_url($type = 0)
{
    return base_url() . 'thems/images';
}
function premition($sub_menu_name)
{
    $CI = &get_instance(); //get instance, access the CI superobject
    if ($CI->session->userdata('ss_user_group_name') and $CI->session->userdata('ss_user_group_name') == 'Sale mStaff')
        return true;
}
function is_logged_in()
{
    // Get current CodeIgniter instance
    $CI = &get_instance();
    // We need to use $CI->session instead of $this->session
    $ss_user_id = $CI->session->userdata('ss_user_id');
    //echo "uid:".$user_id;
    if ($ss_user_id) {
        return true;
        //redirect(base_url(),'refresh');
    } else {
        return false;
    }
}
function is_avalable_link($group_id, $whehouse_id)
{
    $CI = &get_instance();
    $ss_warehouse_id = $CI->session->userdata('ss_warehouse_id');
    if ($group_id == 2) {
        return true;
    } else if ($group_id == 3) {
        if ($ss_warehouse_id == $whehouse_id) {
            return true;
        } else {
            return false;
        }
    }
}


if (!function_exists('common_outstanding_calculation')) {
    function common_outstanding_calculation($data)
    {

        $loan_id = $data['loan_id'];
        $payment_term = $data['payment_term'];
        $total_paid_amount = $data['total_paid_amount'];
        $total_panalty = $data['total_panalty'];
        $total_due = $data['total_due'];
        $total_paid_panalty = $data['total_paid_panalty'];
        $total_paid_due = $data['total_paid_due'];
        $total_paid_loan = $data['total_paid_loan'];
        $users = $data['users'];


        $CI = &get_instance();
        $CI->load->model('Holiday_Model');
        //$CI->load->model('Loan_Payment_Received_Model');
        $should_be_received_value = 0;
        $total_paid_amount        = 0;
        $a                        = 0;
        $today                    = date('Y-m-d');
        if ($total_panalty['panalty_amount'] > 0) {
            $panalty_charge = $total_panalty['panalty_amount'];
        } else {
            $panalty_charge = 0;
        }
        if ($total_due['due_amount'] > 0) {
            $due_charge = $total_due['due_amount'];
        } else {
            $due_charge = 0;
        }
        if ($total_paid_panalty['panalty_received'] > 0) {
            $paid_panalty_charge = $total_paid_panalty['panalty_received'];
        } else {
            $paid_panalty_charge = 0;
        }
        if ($total_paid_due['due_received'] > 0) {
            $paid_due_charge = $total_paid_due['due_received'];
        } else {
            $paid_due_charge = 0;
        }
        if ($total_paid_loan['loan_received'] > 0) {
            $paid_loan_only = $total_paid_loan['loan_received'];
        } else {
            $paid_loan_only = 0;
        }
        //monthly outstanding calculate 
        if ($payment_term == 3) {
            $loan_req_date = $users->loan_date;
            $date1         = new DateTime($today);
            $date2         = new DateTime($loan_req_date);
            if ($date2 > $date1) {
                $diff          = $date1->diff($date2);
                $mature_month  = (($diff->format('%y') * 12) + $diff->format('%m'));
                $mature_month += 1;
                $loan_term_amount         = $users->loan_term_amount;
                $should_be_received_value = $loan_term_amount * $mature_month;
                $total_loan_amount        = $users->loan_payable_total_amount;
                if ($should_be_received_value > $total_loan_amount) {
                    $should_be_received_value = $total_loan_amount;
                }
                $oustanding_value = $should_be_received_value - $paid_loan_only;
            } else {
                $oustanding_value = 0;
            }
        }
        //daily outstanding calculate
        if ($payment_term == 1) {
            $date1 = date_create($users->loan_date);
            $date2 = date_create($today);
            if ($date2 >= $date1) {
                $holiday_count = $CI->Holiday_Model->get_holiday_count_between_loan_start_date_and_today($users->loan_date, $today);
                $diff  = date_diff($date1, $date2);
                $days  = $diff->format("%a");
                // echo $days . ",";
                $mature_days              = $days + 1;
                $mature_days = $mature_days - $holiday_count;
                $loan_term_amount         = $users->loan_term_amount;
                $should_be_received_value = $loan_term_amount * $mature_days;
                $total_loan_amount        = $users->loan_payable_total_amount;
                if ($should_be_received_value > $total_loan_amount) {
                    $should_be_received_value = $total_loan_amount;
                }
                $oustanding_value = $should_be_received_value - $paid_loan_only;
            } else {
                $oustanding_value = 0;
            }
        }
        //Weekly outstanding calculate
        if ($payment_term == 2) {
            $date1                    = date_create($users->loan_date);
            $date2                    = date_create($today);
            $diff                     = date_diff($date1, $date2);
            if ($date2 > $date1) {
                $days                     = $diff->format("%a");
                $week                     = floor($days / 7);
                $mature_week              = $week + 1;
                $loan_term_amount         = $users->loan_term_amount;
                $should_be_received_value = $loan_term_amount * $mature_week;
                $total_loan_amount        = $users->loan_payable_total_amount;
                if ($should_be_received_value > $total_loan_amount) {
                    $should_be_received_value = $total_loan_amount;
                }
                $oustanding_value = $should_be_received_value - $paid_loan_only;
            } else {
                $oustanding_value = 0;
            }
        }
        //cheque outstanding calculate
        if ($payment_term == 4) {
            $should_be_received_value = 0;
            //if cheque reduce from cheque date
            if ($users->reduce_fron_ceque == 1) {
                $total_realize_cheque_for_today = $CI->Loan_Payment_Received_Model->get_total_realize_cheque_for_today_by_loan_id($loan_id, $today);
                $total_cheque_for_today         = $CI->Loan_Payment_Received_Model->get_total_cheque_for_today_by_loan_id($loan_id, $today);
                $should_be_received_value       = $total_cheque_for_today['loan_received'];
                if ($total_realize_cheque_for_today['loan_received'] > 0) {
                    $realize_cheque_value = $total_realize_cheque_for_today['loan_received'];
                } else {
                    $realize_cheque_value = 0;
                }
                $oustanding_value = $should_be_received_value - $realize_cheque_value;
            } else {
                $total_realize_cheque_for_today = $CI->Loan_Payment_Received_Model->get_total_realize_cheque_for_today_by_loan_id($loan_id, $today);
                if ($total_realize_cheque_for_today['loan_received'] > 0) {
                    $realize_cheque_value = $total_realize_cheque_for_today['loan_received'];
                } else {
                    $realize_cheque_value = 0;
                }
                $date1       = date_create($users->loan_date);
                $date2       = date_create($today);
                if ($date2 > $date1) {
                    $diff        = date_diff($date1, $date2);
                    $days        = $diff->format("%a");
                    $mature_days = $days;
                    if ($users->loan_payment_term_qty > 0) {
                        $time_periad = $users->loan_payment_term_qty;
                    } else {
                        $time_periad = 1;
                    }
                    $term_count               = $mature_days / $time_periad;
                    $diff                     = intval($term_count);
                    $should_be_received_value = $users->loan_term_amount * $diff;
                    $total_loan_amount        = $users->loan_payable_total_amount;
                    if ($should_be_received_value > $total_loan_amount) {
                        $should_be_received_value = $total_loan_amount;
                    }
                    $oustanding_value = $should_be_received_value - $realize_cheque_value - $paid_loan_only;
                } else {
                    $oustanding_value = 0;
                }
            }
        }
        return $oustanding_value;
    }
}
