<?php
require('classes/fpdf.php');
require('classes/db.class.php');

$db = new db();

$asset_res = $db->exec_query('assets', ['*'], '', '', '', '', "status = 'ACTIVE'", 'ORDER BY asset_id DESC');

$job_card_res = $db->exec_query('job_cards', ['*']);
$job_card = $job_card_res->fetch_assoc();

$monthly_open_assets_res = $db->exec_query('monthly_jobcard_assets_totals',['*'], '' ,'' , '' , '' ,"1", 'ORDER BY asset_type_id DESC' );
$monthly_totals = $monthly_open_assets_res->fetch_assoc();

$date = new DateTime();

$curr_date = $_GET['curr_date'];

// SELECT DISTICT 

$total = 0;
$open = 0;
$closed = 0;
$awaiting_parts = 0;

$pdf = new FPDF();
$pdf->AliasNbPages();

$pdf->AddPage('L');


$pdf->Image('images/logo_1.png', 170, 5, 40);
// Arial bold 15
$pdf->SetFont('Arial', 'B', 8);
// Move to the right
$pdf->Cell(20);
$pdf->Cell(60, -4, '', 0, 1, 'C');
$pdf->Cell(-3);

$table_top = [16, 28, 50, 65, 36, 55, 30, 20, 25, 20, 38, 20];

// Title 
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 30, 'MONTHLY ASSETS REPORT', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(60, 0, '', 0, 1, 'C');

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell($table_top[0], 0, $monthly_totals['date'], 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-2);
$pdf->Cell($table_top[1], 15, "", 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 22);
$pdf->Cell($table_top[0], 15, 'ASSETS', 0, 1, 'L');
$pdf->SetFont('Arial', 'B', 8);

$pdf->Cell(-3);
$pdf->Cell($table_top[4], 5, "ASSET ID", "LBR", 0, 'L');
$pdf->Cell($table_top[4], 5, "ASSET TYPE", "LB", 0, 'l');
$pdf->Cell($table_top[0], 5, "JC OPEN", "LB", 0, 'l');
$pdf->Cell($table_top[0], 5, "CLOSED", "LB", 0, 'l');
$pdf->Cell($table_top[6], 5, "AWAITING PARTS", "LB", 0, 'l');
$pdf->Cell($table_top[0], 5, "TOTAL", "LB", 0, 'l');
$pdf->Cell($table_top[3], 5, "DESCRIPTION", "LB", 1, 'l');



while($monthly = $monthly_open_assets_res->fetch_assoc()){
    $assets_res = $db->exec_query('assets', ['*'], '', '', '', '', "record_id = {$monthly['asset_id']}", 'ORDER BY asset_id DESC');
    $assets = $assets_res->fetch_assoc();

    $asset_type_res = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$monthly['asset_type_id']}", 'ORDER BY asset_type_name ASC');
    $service_type = $asset_type_res->fetch_assoc();

    $totals = $monthly['job_card_open_total'] + $monthly['job_card_closed_total'] + $monthly['awaiting_parts_total'];

    $pdf->Cell(-3);
    
    $pdf->Cell($table_top[4], 5, $assets['asset_id'], "LB", 0, 'L');
    $pdf->Cell($table_top[4], 5, $service_type['asset_type_name'], "LB", 0, 'l');
    $pdf->Cell($table_top[0], 5, $monthly['job_card_open_total'], "LB", 0, 'l');
    $pdf->Cell($table_top[0], 5, $monthly['job_card_closed_total'], "LB", 0, 'l');
    $pdf->Cell($table_top[6], 5, $monthly['awaiting_parts_total'], "LB", 0, 'l');
    $pdf->Cell($table_top[0], 5, $totals, "LB", 0, 'l');
    $pdf->Cell($table_top[3], 5, $assets['description'], "LB", 1, 'l');
}

$pdf->Output("I");