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

if (isset($_GET['record_id'])) {
    $_GET['receiving_id'] = $_GET['record_id'];
}


$db = new db_safeguard();
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('P');

$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, 5, "", 0, 1, 'L');
/// header end


$pdf->SetFont('Arial', 'B', 25);
$pdf->Cell(190, 5, "RECEIVED", 0, 1, 'C');

$dispatch_details = $db->query("receiving_batches", "SELECT * FROM receiving_batches WHERE record_id = '" . $_GET['receiving_id'] . "'");
$dispatch = $dispatch_details->fetch_assoc();

// dispatch details
$pdf->SetFont('Arial', 'B', 8);
$pdf->cell(35);
$pdf->Cell(47, 5, "", 0, 1, 'L');
$pdf->Cell(47, 5, "PO NUMBER", 1, 0, 'L');
$pdf->Cell(47, 5, "REC. BY:", 1, 0, 'L');
$pdf->Cell(47, 5, "DATE:", 1, 1, 'L');
$pdf->Cell(47, 5, $dispatch['po_number'], 1, 0, 'L');
$pdf->Cell(47, 5, $call->get_username($dispatch['user_id']), 1, 0, 'L');
$pdf->Cell(47, 5, $dispatch['date_time'], 1, 1, 'L');


// get all dispatch items
$pdf->Cell(47 * 4, 5, "", 0, 1, '0');
$pdf->Cell(47, 5, "STOCK", "B", 0, 'L');
$pdf->Cell(47, 5, "AMOUNT", "B", 0, 'L');
$pdf->Cell(47, 5, "BARCODE", "B", 0, 'L');
$pdf->Cell(47, 5, "S/N", "B", 1, 'L');
$pdf->SetFont('Arial', '', 6);

$dispatch_items_res = $db->query("order_trans", "SELECT * FROM order_trans WHERE receiving_batch_id = '" . $_GET['receiving_id'] . "'");
$total_items = 0;
while ($dispatch_items = $dispatch_items_res->fetch_assoc()) {
    $pdf->Cell(47, 5, $call->get_stock_name($dispatch_items['stock_id']), "B", 0, 'L');
    $pdf->Cell(47, 5, $dispatch_items['amount'], "B", 0, 'L');
    if ($dispatch_items['units_stock_id'] != 0) {
        // get units stock item details
        $units_stock_res = $db->query("units_stock", "SELECT * FROM units_stock WHERE record_id = '" . $dispatch_items['units_stock_id'] . "'");
        $units_stock = $units_stock_res->fetch_assoc();
        $pdf->Cell(47, 5, $units_stock['serial_number'], "B", 0, 'L');
        $pdf->Cell(47, 5, $units_stock['barcode'], "B", 1, 'L');
    } else {
        if ($dispatch_items['non_stock_id'] != 0) {
            // get barcode for non stock item
            $non_stock_res = $db->query("non_stock", "SELECT * FROM non_stock WHERE record_id = '" . $dispatch_items['non_stock_id'] . "'");
            $non_stock = $non_stock_res->fetch_assoc();
            $pdf->Cell(47, 5, $non_stock['barcode'], "B", 0, 'L');
            $pdf->Cell(47, 5, "N/A", "B", 1, 'L');

        } else {
            $pdf->Cell(47, 5, "N/A", "B", 0, 'L');
            $pdf->Cell(47, 5, "N/A", "B", 1, 'L');

        }
     
    }
   $total_items += abs($dispatch_items['amount']);
}
$pdf->SetFont('Arial', 'B', 8);

// Top totals rows
$pdf->Cell(47 * 2, 5, "", "", 1, 'L');
$pdf->Cell(25, 5, "TOTAL ITEMS", 1, 0, 'L');
$pdf->Cell(25, 5, number_format($total_items, 0), 1, 1, 'L');

$pdf->Output("I");
