<?php
error_reporting('E_ALL');
ini_set('display_errors', '1');
try {

    if (!isset($_REQUEST['employee_id'])) {
        header('location: ../invoice.php');
        exit;
    } else {
        //$id = $_GET['print'];
    }

    $employee_id = $_GET['employee_id'];

    include_once('../../../classes/manager.class.php');
    include_once('../../../classes/PHPMailer/PHPMailerAutoload.php');
    include_once('../../../fpdf/fpdf1.php');


    $m = new Manager();
    extract($_REQUEST);
    $param_data = ControlConfifguration::GetConfiguration();
    define('PROJECT_NAME', $param_data->name);
    define('PROJECT_ADDRESS', $param_data->address);
    define('PROJECT_TEL', $param_data->contact);
    define('PROJECT_EMAIL', $param_data->email);
    define('PROJECT_WEBSITE', $param_data->website);
    define('PROJECT_COLOR', $param_data->color);
    define('PROJECT_DISCOUNT', $param_data->is_discount);
    define('PROJECT_TAX', $param_data->is_tax);

    //For Employee Detail
    if($employee_id == 'all'){
        $filterGetCust = " ORDER BY status";
    }else{
        $filterGetCust = " WHERE id = '".$employee_id."' ORDER BY status";
    }
    $qry = EmployeeSetup::GetEmployeeSetupList($filterGetCust);

//    $getYearMonth = date('Y-m-00', strtotime($year . '-' . $month . '-01'));
//    $filterSalaryReport = " WHERE DATE_FORMAT(date_time,'%Y-%m-00')='$getYearMonth' ";
//    $filterSalaryReport .= " ORDER BY employee_id";
//    $qry = SalarySetup::GetSalarySetupList($filterSalaryReport);

    if (!empty($qry)) {

        $font = 10;
        $picx = 100;
        $setx = 5;
        $multi = 210;
        $picsetx = 150;
        $w = array(50, 80, 50, 80);
        $path = "../../../";

        $pdf = new FPDFWithOutWaterMark('P', 'mm', 'A4');
        $pdf->SetMargins(0, 0, 0);

        $pdf->SetTitle('Loan Report');
        $pdf->AddPage();
        $pdf->Ln();
        $pdf->SetFont('Arial', 'B', 14);
        $pdf->Cell(0, 6, PROJECT_NAME, 0, 0, 'C');
        $pdf->SetX($setx);
        $pdf->SetFont('Arial', '', 8);
        $pdf->Cell(0, 8, 'Printed Date: ' . date('d-m-Y h:i A'), 0, 0, 'L');
        $pdf->Ln();
        $pdf->SetFont('Arial', '', 12);
        $pdf->Cell(0, 5, 'Loan Report', 0, 0, 'C');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('Loan Description', 'Amount', 'Installment', 'Start Date', 'End Date', 'Paid');

        function FancyTable($header, $qry, $setx)
        {
            global $pdf;

            // Colors, line width and bold font
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetDrawColor(128, 0, 0);
            $pdf->SetLineWidth(.1);
            $pdf->SetFont('Arial', 'B', 8);
            // Header
            $wr = array(90, 20, 20, 20, 20, 20);

            // Color and font restoration
            $pdf->SetFillColor(224, 235, 255);
            $pdf->SetTextColor(0);
            $pdf->SetFont('');
            // Data
            $fill = false;

            if ($qry) {
                foreach ($qry AS $qryEmp) {
                    $loanfilter = " WHERE employee_id = '".$qryEmp->id."' ";
                    $qryloan = LoanSetup::GetLoanList($loanfilter);
                    if($qryloan){
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell($wr[0], 7, $qryEmp->name, 'LBT', 0, 'L', $fill);
                        $pdf->Cell($wr[1], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[3], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[4], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 7, $qryEmp->status, 'LRBT', 0, 'L', $fill);

                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);

                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'L', true);

                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);

                        $loanstatus = 0;
                        foreach($qryloan AS $qreyloan){
                            if($qreyloan->employee_id == $qryEmp->id){
                                $pdf->Cell($wr[0], 7, $qreyloan->description, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[1], 7, $qreyloan->amount, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[2], 7, $qreyloan->installment, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[3], 7, date('d-m-Y', strtotime($qreyloan->start_date)), 'LRBT', 0, 'L', $fill);
                                $endDate = date("d-m-Y", strtotime("+".intval($qreyloan->installment)." month", strtotime($qreyloan->start_date)));
                                $pdf->Cell($wr[4], 7, $endDate, 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[5], 7, "-", 'LRBT', 0, 'L', $fill);
                                $loanstatus = 1;
                                $installmentprice = $qreyloan->amount/$qreyloan->installment;
                            }
                        }
                        if($loanstatus == 0){
                            $pdf->Cell($wr[0], 7, "-", 'LRBT', 0, 'L', $fill);
                        }

                        $startDateSal = date("d-m-Y", strtotime($qreyloan->start_date));
                        $endDateSal = date("d-m-Y", strtotime($endDate));
                        $salfilter = " WHERE employee_id = '".$qryEmp->id."' AND DATE_FORMAT(date_time,'%Y-%m-%d')>='".date('Y-m-d', strtotime($startDateSal))."' AND DATE_FORMAT(date_time,'%Y-%m-%d')<='".date('Y-m-d', strtotime($endDateSal))."' AND advance = 0 ORDER BY date_time";
                        $salaryemp = SalarySetup::GetSalarySetupList($salfilter);
                        $totinstallmentprice = 0;

                        $checksal = 0;

                        foreach($salaryemp AS $salaryemps){
                            if($salaryemps->employee_id == $qryEmp->id){
                                $pdf->Ln();
                                $pdf->SetX($setx);
                                $pdf->SetFont('Arial', '', 7.5);

                                $pdf->Cell($wr[0], 7, date('m-Y', strtotime($salaryemps->date_time)), 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[1], 7, "", 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[2], 7, "", 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[3], 7, "", 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[4], 7, "", 'LRBT', 0, 'L', $fill);
                                $pdf->Cell($wr[5], 7, $installmentprice, 'LRBT', 0, 'L', $fill);
                                $totinstallmentprice += $installmentprice;
                                $checksal = 1;
                            }
                        }

                            $pdf->Ln();
                            $pdf->SetX($setx);
                            $pdf->SetFont('Arial', '', 7.5);


                        $pdf->Cell($wr[0], 7, "Total", 'LBT', 0, 'R', $fill);
                        $pdf->Cell($wr[1], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[3], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[4], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 7, $totinstallmentprice, 'LRBT', 0, 'R', $fill);

                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell($wr[0], 7, "Total Remaining Balance", 'LBT', 0, 'R', $fill);
                        $pdf->Cell($wr[1], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[3], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[4], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 7, ($qreyloan->amount - $totinstallmentprice), 'LRBT', 0, 'L', $fill);

                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                    }
                }
            }
            //$pdf->SetFillColor(255, 204, 0);

            $wr = array(80, 21, 20, 80);
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->Ln();
            $pdf->SetX($setx);
            $pdf->SetFont('Arial', '', 9);
            $pdf->Cell($wr[0], 7, "Company Signature's", 'T', 0, 'C');
            $pdf->Cell($wr[1], 7, "", '', 0, 'C');

            $pdf->Cell($wr[2], 7, "", '', 0, 'C');
            $pdf->Cell($wr[3], 7, "", 'T', 0, 'C');
        }

        FancyTable($header, $qry, $setx);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

    $pdf->Output();
    ob_end_flush();
    exit;
} catch (Exception $e) {
    echo $e->getMessage();
}
?>