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

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

    $month = $_GET['month'];
    $year = $_GET['year'];

    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);

    if ($month) {
        $monthcount = $month;
    } else {
        $monthcount = date('F');
    }
    for ($m = 1; $m <= 12; ++$m) {
        if (date('F', mktime(0, 0, 0, $m, 1)) == trim($monthcount)) {
            $monthCheck = $m;
        }
    }

    $getYearMonth = date('Y-m-00', strtotime($year . '-' . $monthCheck . '-01'));
    $filterProductionReport = " WHERE DATE_FORMAT(batch_date,'%Y-%m-00')='$getYearMonth' AND status<>'cancel'";
    $filterProductionReport .= " ORDER BY batch_date";
    $qry = ProductionSetup::GetProductionSetupList($filterProductionReport);

    $filterProductionTotalReport = " WHERE DATE_FORMAT(batch_date,'%Y-%m-00')='$getYearMonth' AND status<>'cancel'";
    $filterProductionTotalReport .= " GROUP BY batch_id";
    $qryTotal = ProductionSetup::GetProductionBatchWiseTotalList($filterProductionTotalReport);

    //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('Production 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, 'Production Report', 0, 0, 'C');

        $pdf->Ln();
        $pdf->Ln();
        $pdf->SetX($setx);
        $header = array('S.No', 'Date', 'Batch Name', '# Bacthes', 'Oil', 'Mung', 'Mash', 'Rice Pwd', 'Total Est.', 'Production', 'Difference');

        function FancyTable($header, $qry, $setx, $qryTotal)
        {
            global $pdf;
            $batch_date = "";
            $totalqty = 0;
            $totalOil = 0;
            $totalMung = 0;
            $totalMash = 0;
            $totalRice = 0;
            $totalpackets = 0;
            $totalpropackets =0;
            $Grandtotalqty = 0;
            $GrandtotalOil = 0;
            $GrandtotalMung = 0;
            $GrandtotalMash = 0;
            $GrandtotalRice = 0;
            $Grandtotalpackets = 0;
            $Grandtotalpropackets = 0;
            $status = "";

            // 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(10, 18, 35, 18, 12, 15, 15, 18, 20, 20, 20);

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

            if ($qry) {
                $countingSr = 1;
                $counttotal = count($qry);
                foreach ($qry AS $qryRow) {
                    if ($countingSr == 1 || $batch_date != $qryRow->batch_date) {
                        $countingSrn = 1;
                        for ($i = 0; $i < count($header); $i++)
                            $pdf->Cell($wr[$i], 7, $header[$i], 1, 0, 'L', true);
                    }
                    $totalqty += $qryRow->batch_qty;
                    $totalOil += ($qryRow->batch_qty * $qryRow->per_unit_oil);
                    $totalMung += ($qryRow->batch_qty * $qryRow->per_unit_mung);
                    $totalMash += ($qryRow->batch_qty * $qryRow->per_unit_mash);
                    $totalRice += ($qryRow->batch_qty * $qryRow->per_unit_rice);
                    $totalpackets += ($qryRow->batch_qty * $qryRow->per_unit_packets);
                    $totalpropackets += $qryRow->total;

                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 7.5);
                    $pdf->Cell($wr[0], 7, $countingSrn, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[1], 7, date('Y-m-d', strtotime($qryRow->batch_date)), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $qryRow->batch_name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $qryRow->batch_qty, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[4], 7, ($qryRow->batch_qty * $qryRow->per_unit_oil), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[5], 7, ($qryRow->batch_qty * $qryRow->per_unit_mung), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[6], 7, ($qryRow->batch_qty * $qryRow->per_unit_mash), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[7], 7, ($qryRow->batch_qty * $qryRow->per_unit_rice), 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[8], 7, ($qryRow->batch_qty * $qryRow->per_unit_packets), 'LRBT', 0, 'L', $fill);

                    if ($qryRow->total == 0) {
                        $pdf->Cell($wr[9], 7, '-', 'LRBT', 0, 'L', $fill);
                    } else {
                        $pdf->Cell($wr[9], 7, $qryRow->total, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[10], 7, ($qryRow->total - ($qryRow->batch_qty * $qryRow->per_unit_packets)), 'LRBT', 0, 'L', $fill);
                    }

                    if ($counttotal == $countingSr || $qry[($countingSr)]->batch_date != $qryRow->batch_date) {
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell($wr[0], 7, "", 'LBT', 0, 'L', $fill);
                        $pdf->Cell($wr[1], 7, "", 'BT', 0, 'L', $fill);
                        $pdf->Cell($wr[2], 7, "Total", 'RBT', 0, 'L', $fill);
                        $pdf->Cell($wr[3], 7, $totalqty, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[4], 7, $totalOil, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[5], 7, $totalMung, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[6], 7, $totalMash, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[7], 7, $totalRice, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[8], 7, $totalpackets, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[9], 7, $totalpropackets, 'LRBT', 0, 'L', $fill);
                        $pdf->Cell($wr[10], 7, "-", 'LRBT', 0, 'L', $fill);

                        $pdf->Ln();
                        $pdf->Ln();
                        $pdf->SetX($setx);

                        $Grandtotalqty += $totalqty;
                        $GrandtotalOil += $totalOil;
                        $GrandtotalMung += $totalMung;
                        $GrandtotalMash += $totalMash;
                        $GrandtotalRice += $totalRice;
                        $Grandtotalpackets += $totalpackets;
                        $Grandtotalpropackets += $totalpropackets;

                        $totalqty = 0;
                        $totalOil = 0;
                        $totalMung = 0;
                        $totalMash = 0;
                        $totalRice = 0;
                        $totalpackets = 0;
                        $totalpropackets = 0;
                    }
                    $batch_date = $qryRow->batch_date;
                    $countingSr++;
                    $countingSrn++;
                }

                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total No. Of Bacthes", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $Grandtotalqty, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Oil", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $GrandtotalOil, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Mung Daal", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $GrandtotalMung, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Mash Daal", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $GrandtotalMash, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Rice Powder", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $GrandtotalRice, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Estimated", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $Grandtotalpackets, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Produced", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, $Grandtotalpropackets, 'LRBT', 0, 'L', $fill);

                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(150, 7, "Total Difference", 'LBT', 0, 'L', $fill);
                $pdf->Cell(50, 7, ($Grandtotalpropackets - $Grandtotalpackets), 'LRBT', 0, 'L', $fill);

                $pdf->AddPage();
                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell(200, 7, "Stock", 'LRBT', 0, 'C', $fill);

                $ing = IngSetup::GetIngSetupList();
                foreach($ing AS $ingRow){
                    if($ingRow->id == 1){
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell(150, 7, "Oil", 'LBT', 0, 'L', $fill);
                        $pdf->Cell(50, 7, $ingRow->availability, 'LRBT', 0, 'L', $fill);
                    }
                    if($ingRow->id == 2){
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell(150, 7, "Mung", 'LBT', 0, 'L', $fill);
                        $pdf->Cell(50, 7, $ingRow->availability, 'LRBT', 0, 'L', $fill);
                    }
                    if($ingRow->id == 3){
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell(150, 7, "Mash", 'LBT', 0, 'L', $fill);
                        $pdf->Cell(50, 7, $ingRow->availability, 'LRBT', 0, 'L', $fill);
                    }
                    if($ingRow->id == 4){
                        $pdf->Ln();
                        $pdf->SetX($setx);
                        $pdf->SetFont('Arial', '', 7.5);
                        $pdf->Cell(150, 7, "Rice", 'LBT', 0, 'L', $fill);
                        $pdf->Cell(50, 7, $ingRow->availability, 'LRBT', 0, 'L', $fill);
                    }
                }
            }

            if($qryTotal){
                $wr = array(10, 53, 18, 12, 15, 15, 18, 20, 20, 20);
                $headers = array('S.No', 'Batch Name', 'Bacthes', 'Oil', 'Mung', 'Mash', 'Rice Pwd', 'Total Est.', 'Total Pro.', 'Total Diff.');
                $pdf->Ln();
                $pdf->Ln();
                $pdf->SetX($setx);
                for ($i = 0; $i < count($headers); $i++)
                    $pdf->Cell($wr[$i], 7, $headers[$i], 1, 0, 'L', true);

                $countPro = 1;
                $grandbatches = 0;
                $grandoil = 0;
                $grandmung = 0;
                $grandmash = 0;
                $grandrice = 0;
                $grandpackets = 0;
                $grandtotal = 0;
                $granddiff = 0;
                foreach($qryTotal AS $qryTotalRow){
                    $pdf->Ln();
                    $pdf->SetX($setx);
                    $pdf->SetFont('Arial', '', 7.5);
                    $pdf->Cell($wr[0], 7, $countPro, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[1], 7, $qryTotalRow->batch_name, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[2], 7, $qryTotalRow->batches, 'RBT', 0, 'L', $fill);
                    $pdf->Cell($wr[3], 7, $qryTotalRow->oil, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[4], 7, $qryTotalRow->mung, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[5], 7, $qryTotalRow->mash, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[6], 7, $qryTotalRow->rice, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[7], 7, $qryTotalRow->packets, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[8], 7, $qryTotalRow->total, 'LRBT', 0, 'L', $fill);
                    $pdf->Cell($wr[9], 7, ($qryTotalRow->total - $qryTotalRow->packets), 'LRBT', 0, 'L', $fill);

                    $grandbatches += $qryTotalRow->batches;
                    $grandoil += $qryTotalRow->oil;
                    $grandmung += $qryTotalRow->mung;
                    $grandmash += $qryTotalRow->mash;
                    $grandrice += $qryTotalRow->rice;
                    $grandpackets += $qryTotalRow->packets;
                    $grandtotal += $qryTotalRow->total;
                    $granddiff += ($qryTotalRow->total - $qryTotalRow->packets);

                    $countPro++;
                }
                $pdf->Ln();
                $pdf->SetX($setx);
                $pdf->SetFont('Arial', '', 7.5);
                $pdf->Cell($wr[0], 7, "", 'LBT', 0, 'L', $fill);
                $pdf->Cell($wr[1], 7, "Total", 'RBT', 0, 'C', $fill);
                $pdf->Cell($wr[2], 7, $grandbatches, 'RBT', 0, 'L', $fill);
                $pdf->Cell($wr[3], 7, $grandoil, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[4], 7, $grandmung, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[5], 7, $grandmash, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[6], 7, $grandrice, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[7], 7, $grandpackets, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[8], 7, $grandtotal, 'LRBT', 0, 'L', $fill);
                $pdf->Cell($wr[9], 7, $granddiff, 'LRBT', 0, 'L', $fill);
            }

//            $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, $qryTotal);
    } else {
        header('location: ' . PROJECT_ADMIN_URL);
        exit;
    }

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