<?php
session_start();
if(!isset($_SESSION['employee_email'])){
    header('Location:login/index.php');
}
if($_SESSION['is_first_time']=="Yes"){
    header('Location:login/change_password.php');
}

require_once 'Config/Database.php';
require_once 'Model/Login.php';
require_once 'Model/Role.php';
require_once 'Model/Employee.php';
require_once 'Model/Item.php';
require_once 'Model/ItemCategory.php';
require_once 'Model/ItemLevel.php';
require_once 'Model/Supplier.php';
require_once 'Model/ItemType.php';
require_once 'Model/ItemRequest.php';
require_once 'Model/Student.php';
require_once 'Model/SystemGroup.php';
require_once 'Model/SystemPermission.php';
require_once 'Model/RolePermission.php';
require_once 'Model/UserPermission.php';
require_once 'Model/Notification.php';
require_once 'Model/Payroll.php';
require_once 'Model/Program.php';
require_once 'Model/Course.php';
require_once 'Model/AcademicYear.php';
require_once 'Model/PaymentType.php';
require_once 'Model/Payment.php';


$role = new Role();
$login = new Login();
$employee = new Employee();
$request = new ItemRequest();
$student = new Student();
$system_group = new SystemGroup();
$system_permission = new SystemPermission();
$role_permission = new RolePermission();
$user_permission = new UserPermission();
$notification = new Notification();
$payroll = new Payroll();
$program = new Program();
$course = new Course();
$academic_year = new AcademicYear();
$payment_type = new PaymentType();
$payment = new Payment();

// Settings
$current_month = date('m');

$students = $student->read_all_students();
$employees = $employee->read_all_employees();
$system_groups = $system_group->read_all_system_groups();
$system_permissions = $system_permission->read_all_system_permissions();
$all_employees = $employee->read_all_employees();
$list_of_employees = $employee->read_all_employees();
$programs = $program->read_all_programs();
$courses = $course->read_all_courses();
$academic_years = $academic_year->read_all_academic_years();
$payment_types = $payment_type->read_all_payment_types();
// Detelete Return Notification after a week
$notification->remove_notification_after_a_week();

$roles = $role->read_all_roles();
$all_roles = "";
$all_employees = "";
$all_system_groups = "";
$all_system_permissions = "";
$all_programs = "";
$all_courses = "";
$all_academic_years = "";
$all_payment_types = "";
$all_students = "";

if($roles){
    foreach($roles as $key){
        $all_roles .= '"'.$key['role_name'].'",';
    }
}

if($employees){
    foreach($employees as $key){
        $all_employees .= '"'.$key['employee_name'].'",';
    }
}



if($system_groups){
    foreach($system_groups as $key){
        $all_system_groups .= '"'.$key['system_group_name'].'",';
    }
}

if($system_permissions){
    foreach($system_permissions as $key){
        $all_system_permissions .= '"'.$key['display_name'].'",';
    }
}

if($programs){
    foreach($programs as $key){
        $all_programs .= '"'.$key['program_name'].'",';
    }
}

if($courses){
    foreach($courses as $key){
        $all_courses .= '"'.$key['course_name'].'",';
    }
}

if($academic_years){
    foreach($academic_years as $key){
        $all_academic_years .= '"'.$key['academic_year_name'].'",';
    }
}

if($payment_types){
    foreach($payment_types as $key){
        $all_payment_types .= '"'.$key['payment_type_name'].'",';
    }
}

if($students){
    foreach($students as $key){
        $all_students .= '"'.$key['student_name'].'",';
    }
}

// User/Role Specific
$user_ex_lim_details = array();
$user_ex_lim_details = $user_permission->read_page_user_permission($_SESSION['employee_id']);
if($user_ex_lim_details){
    $folder_page = $_SERVER['PHP_SELF'];
    $get_page = $get_class = array_map('trim', explode('/', $folder_page));
    $page = $get_page[2];
    $cant_access = $user_permission->check_current_page($page);
    if($cant_access){
        if($page=="unauthorized.php"){

        }else{
            header("Location:unauthorized.php");
        }
        
    }else{
        
    }
}

// User Achievement
$employee_achievements = $employee->read_user_reward($_SESSION['employee_id'],$current_month);

// Payroll/Slip
$pay_slip_details = array();
if(isset($_GET['slip_id'])){
    $slip_id = $_GET['slip_id'];
    $pay_slip_details = $payroll->read_payroll($slip_id,$current_month);
}

if(isset($_GET['user_id'])){
    $slip_id = $_GET['user_id'];
    $pay_slip_details = $payroll->read_user_payroll($slip_id,$current_month);
}


// User Approval
$my_requests = array();

$emp_name = '';


$system_request_notification = array();
$folder_page = $_SERVER['PHP_SELF'];
$get_page = $get_class = array_map('trim', explode('/', $folder_page));
$page = 'item_request_approval.php';
$page_Details = $system_permission->get_system_permission_by_page($page);
$system_permission_id = $page_Details[0]['system_permission_id'];
$has_access_to_notification = $role_permission->check_page_role_permission($_SESSION['role_id'],$system_permission_id);
if($has_access_to_notification){
    $system_request_notification = $notification->read_all_request_notifications();
   
}else{
    $page = $get_page[2];
    $system_request_notification = array();
    if($page=="item_request_approval.php"){
        if($page!="unauthorized.php"){
            header("Location:unauthorized.php");
        }
    }else{
        
    }
    
}



// Util Functions
function time_elapsed_string($datetime, $full = false) {
    $now = new DateTime;
    $ago = new DateTime($datetime);
    $diff = $now->diff($ago);

    $diff->w = floor($diff->d / 7);
    $diff->d -= $diff->w * 7;

    $string = array(
        'y' => 'year',
        'm' => 'month',
        'w' => 'week',
        'd' => 'day',
        'h' => 'hour',
        'i' => 'minute',
        's' => 'second',
    );
    foreach ($string as $k => &$v) {
        if ($diff->$k) {
            $v = $diff->$k . ' ' . $v . ($diff->$k > 1 ? 's' : '');
        } else {
            unset($string[$k]);
        }
    }

    if (!$full) $string = array_slice($string, 0, 1);
    return $string ? implode(', ', $string) . ' ago' : 'just now';
}

// Invoice
if(isset($_GET['view_invoice'])){
    $id = $_GET['id'];
    $payment_details = $payment->read_payment($id);
}

if(isset($_GET['view_installment_invoice'])){
    $in_id = $_GET['id'];
    $request_details = $request->read_item_request_for_installment($in_id);
    $item_request_id = $request_details[0]['item_request_id'];
    $request_order_details = $request->read_item_request_order($item_request_id);
}

// Report Summary
$year = date('Y');
$start = $year.'-01-01';
$end = $year.'-12-31';
$frontend_start = '01/01/'.$year;
$frontend_end = '12/31/'.$year;
$items_report_summary = array();
if(isset($_GET['summary'])){
    $start = $_GET['start'];
    $end = $_GET['end'];
    $newStart = strtotime($start); 
    $start = date('Y-m-d',$newStart);
    $newEnd = strtotime($end); 
    $end = date('Y-m-d',$newEnd);
    $frontend_start = date('m/d/Y',$newStart);
    $frontend_end = date('m/d/Y',$newEnd);
    $items_report_summary = $request->read_report_summary($start,$end);
}
// Marketing Chart
$year = date('Y');
$start = $year.'-01-01';
$ts1 = strtotime($start);
$ts2 = strtotime($frontend_end);
$ts1;
$year1 = date('Y', $ts1);
$year2 = date('Y', $ts2);

$month1 = date('m', $ts1);
$month2 = date('m', $ts2);

$diff = (($year2 - $year1) * 12) + ($month2 - $month1);
$chart_target_profit = "";
$chart_actual_profit = "";
$chart_tithe = "";
$chart_total_amount = "";

$chartjs_target_profit = "";
$chartjs_actual_profit = "";
$chartjs_tithe = "";
for($m=1;$m<=$diff+1;$m++){
    $data = $request->read_report_summary_for_month($m);
    
    if($data){
        foreach($data as $d){
            $date = $d['payment_date'];
            $total_amount = floatval($d['total_amount']);
            $amount_paid = $total_amount;
            $wholesale_amount = 0;
            if($date==0 || $date==""){
             $date = "2021/$m/28";
            }
            $i_r_t = intval($d['amount']);
            if($i_r_t=="" || $i_r_t==0){
                $i_r_t = 0;
            }
            if($total_amount=="" || $total_amount==0){
                $total_amount = 0;
                $profit = 0;
                $actual_profit = 0;
                $tithe = 0;
            }else{
                $profit = $total_amount - $wholesale_amount;
                $percentage_paid = round($amount_paid/$total_amount*100);
                $actual_profit = round($percentage_paid/100*$profit);
                $tithe = $actual_profit/10;
            }

            $chartjs_target_profit .= "$profit,";
            $chartjs_actual_profit .= "$actual_profit,";
            $chartjs_tithe .= "$tithe,";
        }
    }
}


// Student Payment History
$student_payment_history = array();
$cus_id = '';
if(isset($_GET['cus_id'])){
    $cus_id = $_GET['cus_id'];
    $student_payment_history = $request->read_student_item_requests($cus_id);
}



// Barcode
require 'vendor/autoload.php';

// This will output the barcode as HTML output to display in the browser
// $generator = new Picqer\Barcode\BarcodeGeneratorHTML();
$redColor = [255, 0, 0];

$generator = new Picqer\Barcode\BarcodeGeneratorPNG();
// file_put_contents('barcode.png', $generator->getBarcode('081231723897', $generator::TYPE_CODE_128, 3, 50, $redColor));
?>

