<?php
include '../../fpdf.php';
include "../../root.class.php";
$call = new call_functions();


$db = new db_safeguard();
$jobcard_res = $db->query("job_card_accounts", "SELECT * FROM job_card_accounts WHERE record_id = {$_GET['record_id']}");
$jobcard = $jobcard_res->fetch_assoc();
function remove_html_tags($text)
{
    $description = strip_tags($text);
    $description = str_replace("\n", "\r\n", $description);
    $description = preg_replace('/\s\s+/', ' ', $description);
    $description = preg_replace('/\n\n+/', "\n", $description);
    return $description;
}

$pdf = new FPDF();
$pdf->AliasNbPages();
$pdf->AddPage('L');


$pdf->Image('../../logo.png', 10, 10, 30);
$pdf->SetFont('Arial', 'B', 6);
$pdf->cell(35);
$pdf->Cell(115, 5, "MIDRAND AIR SERVICES (PTY) LTD", 0, 0, 'L');
$pdf->Cell(35, 5, "REG NO: 2018/573291/07", 0, 1, 'R');
$pdf->cell(35);

$pdf->Cell(115, 5, "PO BOX 915", 0, 0, 'L');
$pdf->Cell(35, 5, "VAT NO: 4860287269", 0, 1, 'R');
$pdf->cell(35);

$pdf->Cell(115, 5, "IRENE, 0062", 0, 0, 'L');
$pdf->Cell(35, 5, "PHONE: 012 661 2211", 0, 1, 'R');
$pdf->cell(35);

$pdf->Cell(115, 5, "", 0, 0, 'L');
$pdf->Cell(35, 5, "FAX: 012 661 2212", 0, 1, 'R');
$pdf->Cell(115, 15, "", 0, 1, 'L');
/// header end

$pdf->cell(35);
$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(120, 10, $jobcard['name'] . " : " . $jobcard['description'] . " REPORT", 0, 1, 'C');
$pdf->Cell(120, 5, "", 0, 1, 'C');
$pdf->SetFont('Arial', '', 8);
$pdf->cell(28);
$pdf->Cell(30, 10, "MANAGER", 1, 0, 'L');
$pdf->SetFont('Arial', 'B', 8);
$pdf->Cell(45, 10, $call->get_username($jobcard['account_manager_id']), 1, 0, 'L');
$pdf->SetFont('Arial', '', 8);
$pdf->Cell(30, 10, "STATUS", 1, 0, 'L');
$pdf->SetFont('Arial', 'B', 8);
if ($jobcard['status'] == 1)
    $status = "ACTIVE";
else
    $status = "INACTIVE";
$pdf->Cell(35, 10, $status, 1, 1, 'L');

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(120, 5, "", 0, 1, 'C');
$pdf->Cell(120, 10, "DISPATCHED", "B", 1, 'L');
$pdf->Cell(120, 5, "", 0, 1, 'C');
$pdf->SetFont('Arial', '', 8);
// get all dispatched items under this jobcard number

$dispatched_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['name']}' AND amount < 0 ORDER BY record_id DESC");
// echo"SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['name']}' AND amount < 0 ORDER BY record_id DESC";
// headings
$pdf->Cell(25, 10, "AMOUNT", "B", 0, 'L');
$pdf->Cell(25, 10, "PRICE EA", "B", 0, 'L');
$pdf->Cell(25, 10, "TOTAL", "B", 0, 'L');
$pdf->Cell(25, 10, "DATE", "B", 0, 'L');
$pdf->Cell(25, 10, "USER", "B", 0, 'L');
$pdf->Cell(25, 10, "ITEM", "B", 1, 'L');
$total_dispatched_value = 0;
$total_items = 0;
while ($dispatched = $dispatched_res->fetch_assoc()) {
    $pdf->Cell(25, 10, $dispatched['amount'], "B", 0, 'L');
    $total_items += $dispatched['amount'];
    $pdf->Cell(25, 10, "R " . $call->get_stock_price($dispatched['stock_id']), "B", 0, 'L');
    $pdf->Cell(25, 10, "R " . abs($call->get_stock_price($dispatched['stock_id']) * $dispatched['amount']), "B", 0, 'L');
    $total_dispatched_value += abs($call->get_stock_price($dispatched['stock_id']) * $dispatched['amount']);
    $pdf->Cell(25, 10, $dispatched['date_time'], "B", 0, 'L');
    $pdf->Cell(25, 10, $call->get_username($dispatched['user_id']), "B", 0, 'L');
    $pdf->Cell(25, 10, $call->get_stock_name($dispatched['stock_id']), "B", 1, 'L');
    $index_dispatched++;
}
$pdf->Cell(25, 10, $total_items, "BT", 0, 'L');
$pdf->Cell(25, 10, "", "BT", 0, 'L');
$pdf->Cell(25, 10, "R " . number_format($total_dispatched_value, 3), "BT", 0, 'L');
$pdf->Cell(25, 10, "", "BT", 0, 'L');
$pdf->Cell(25, 10, "", "BT", 0, 'L');
$pdf->Cell(25, 10, "", "BT", 1, 'L');



$pdf->Cell(120, 10, "", 0, 1, 'C');


$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(120, 10, "RECEIVED", "B", 1, 'L');
$pdf->Cell(120, 5, "", 0, 1, 'C');
$pdf->SetFont('Arial', '', 8);
// get Purchase Orders Linked to this account
$po_res = $db->query("orders", "SELECT * FROM orders WHERE job_card_account_id = {$jobcard['record_id']}");
if ($po_res->num_rows > 0) {
    $po_ids = [];
    while ($po = $po_res->fetch_assoc()) {
        $po_ids[] = " order_id = ".$po['record_id'];
    }
    // implode into sql search
    $po_ids = implode(" OR ", $po_ids);
    $received_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE ($po_ids) AND amount > 0 AND order_id != 0 ORDER BY record_id DESC");
    // echo"SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['name']}' AND amount < 0 ORDER BY record_id DESC";
// headings
    $pdf->Cell(25, 10, "AMOUNT", "B", 0, 'L');
    $pdf->Cell(25, 10, "PRICE EA", "B", 0, 'L');
    $pdf->Cell(25, 10, "TOTAL", "B", 0, 'L');
    $pdf->Cell(25, 10, "DATE", "B", 0, 'L');
    $pdf->Cell(25, 10, "USER", "B", 0, 'L');
    $pdf->Cell(25, 10, "PO", "B", 0, 'L');
    $pdf->Cell(25, 10, "ITEM", "B", 1, 'L');
    $total_received_value = 0;
    $total_items = 0;
    while ($received = $received_res->fetch_assoc()) {
        $pdf->Cell(25, 10, $received['amount'], "B", 0, 'L');
        $total_items += $received['amount'];
        $pdf->Cell(25, 10, "R " . $call->get_stock_price($received['stock_id']), "B", 0, 'L');
        $pdf->Cell(25, 10, "R " . abs($call->get_stock_price($received['stock_id']) * $received['amount']), "B", 0, 'L');
        $total_received_value += abs($call->get_stock_price($received['stock_id']) * $received['amount']);
        $pdf->Cell(25, 10, $received['date_time'], "B", 0, 'L');
        $pdf->Cell(25, 10, $call->get_username($received['user_id']), "B", 0, 'L');
        $pdf->Cell(25, 10, $call->get_po_number($received['order_id']), "B", 0, 'L');
        $pdf->Cell(25, 10, $call->get_stock_name($received['stock_id']), "B", 1, 'L');
        $index_received++;
    }
    $pdf->Cell(25, 10, $total_items, "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "R " . number_format($total_received_value, 3), "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 1, 'L');
    $pdf->Cell(120, 10, "", 0, 1, 'C');
}

$pdf->SetFont('Arial', 'B', 15);
$pdf->Cell(120, 10, "RETURNS", "B", 1, 'L');
$pdf->Cell(120, 5, "", 0, 1, 'C');
$pdf->SetFont('Arial', '', 8);

$returned_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['name']}' AND amount > 0 ORDER BY record_id DESC");
if ($returned_res->num_rows > 0) {
    // echo"SELECT * FROM order_trans WHERE job_card_no = '{$jobcard['name']}' AND amount < 0 ORDER BY record_id DESC";
// headings
    $pdf->Cell(25, 10, "AMOUNT", "B", 0, 'L');
    $pdf->Cell(25, 10, "PRICE EA", "B", 0, 'L');
    $pdf->Cell(25, 10, "TOTAL", "B", 0, 'L');
    $pdf->Cell(25, 10, "DATE", "B", 0, 'L');
    $pdf->Cell(25, 10, "USER", "B", 0, 'L');
    $pdf->Cell(25, 10, "ITEM", "B", 1, 'L');
    $total_returned_value = 0;
    $total_items = 0;
    while ($returned = $returned_res->fetch_assoc()) {
        $pdf->Cell(25, 10, $returned['amount'], "B", 0, 'L');
        $total_items += $returned['amount'];
        $pdf->Cell(25, 10, "R " . $call->get_stock_price($returned['stock_id']), "B", 0, 'L');
        $pdf->Cell(25, 10, "R " . abs($call->get_stock_price($returned['stock_id']) * $returned['amount']), "B", 0, 'L');
        $total_returned_value += abs($call->get_stock_price($returned['stock_id']) * $returned['amount']);
        $pdf->Cell(25, 10, $returned['date_time'], "B", 0, 'L');
        $pdf->Cell(25, 10, $call->get_username($returned['user_id']), "B", 0, 'L');
        $pdf->Cell(25, 10, $call->get_stock_name($returned['stock_id']), "B", 1, 'L');
        $index_returned++;
    }
    $pdf->Cell(25, 10, $total_items, "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "R " . number_format($total_returned_value, 3), "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 0, 'L');
    $pdf->Cell(25, 10, "", "BT", 1, 'L');
}





$pdf->Output("I");
