<?php
require_once "../models/Operations.php";
require('fpdf186/fpdf.php');

//A4 width : 219mm
//default margin : 10mm each side
//writable horizontal : 219-(10*2)=189mm


class PDF extends FPDF
{
	// Page footer
	function Footer()
	{
		// Position at 1.5 cm from bottom
		$this->SetY(-15);
		// Arial italic 8
		$this->SetFont('Arial', 'I', 10);
		// Page number
		$this->Cell(0, 10, 'Page ' . $this->PageNo() . '/{nb}', 0, 0, 'C');
	}
}

// Instanciation of inherited class
$pdf = new PDF('P', 'mm', 'A4');
$pdf->AliasNbPages();
$pdf->AddPage();
$pdf->SetFont('Times', '', 12);
for ($i = 1; $i <= 40; $i++);


if (empty($_GET["pur_id"])) {
	exit();
}

// CREATE VARIABLE AND COLLECT GET METHOD DATA VALUES
$pur_id = $_GET["pur_id"];

$enterprise = "VAPEO BAR";

// date_default_timezone_set('America/New_York');
$currentDateTime = date('d-M-Y h:i:s A');


// INVOICE HEADER -------------------------------

//set font to arial, bold, 14pt
$pdf->SetFont('Arial', 'B', 20);
$pdf->Cell(132, 15, strtoupper($enterprise), 0, 0);
$pdf->SetFont('Arial', '', 10);
$pdf->Cell(15, 15, 'Impresa:', 0, 0);
$pdf->Cell(37, 15, $currentDateTime, 0, 1); //end of line
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(10, 5, 'RNC:', 0, 0);
$pdf->Cell(37, 5, '000-00000-0', 0, 1); //end of line
$pdf->Cell(37, 5, 'Direccion etc..', 0, 1); //end of line

// PURCHASE INFORMATION
$sqlPurchaseInfo = "SELECT * FROM tb_purchases WHERE pur_id = {$pur_id};";
$purchaseInfo = executePDOFetchOne($sqlPurchaseInfo);

// PROVIDER INFORMATION
$sqlProviderInfo = "SELECT * FROM tb_providers WHERE prov_id = {$purchaseInfo['pur_provider_id']};";
$providerInfo = executePDOFetchOne($sqlProviderInfo);

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189, 5, '', 0, 1); //end of line
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(135, 1, '', 0, 0);

$pdf->Cell(189, 5, '', 0, 1); //end of line
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(22, 1, 'Proveedor:', 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(113, 1, mb_convert_encoding($providerInfo['prov_name'], 'ISO-8859-1', 'UTF-8'), 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(29, 1, 'NCF:', 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(20, 1, mb_convert_encoding($providerInfo['prov_fiscal_number'], 'ISO-8859-1', 'UTF-8'), 0, 1);

$pdf->Cell(189, 5, '', 0, 1); //end of line
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(22, 1, mb_convert_encoding('Código:', 'ISO-8859-1', 'UTF-8'), 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(113, 1, '...', 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(29, 1, mb_convert_encoding('Fecha factura:', 'ISO-8859-1', 'UTF-8'), 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(20, 1, date("d-M-Y", strtotime($purchaseInfo['pur_date'])), 0, 1);

$pdf->Cell(189, 5, '', 0, 1); //end of line
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(22, 1, 'RNC:', 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(113, 1, mb_convert_encoding($providerInfo['prov_fiscal_number'], 'ISO-8859-1', 'UTF-8'), 0, 0);
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(29, 1, 'Moneda:', 0, 0);
$pdf->SetFont('Arial', '', 11);
$pdf->Cell(20, 1, 'DOP (RD)', 0, 1);

//make a dummy empty cell as a vertical spacer
// $pdf->Cell(189	,10,'',0,1);//end of line
// $pdf->SetFont('Arial','B',11);
// $pdf->Cell(22	,8,'Concepto:',0,1);
// $pdf->SetFont('Arial','',11);
// $pdf->MultiCell(189	,5,'Compra de 4 monitores para el depto. De Contabilidad',0,1);

//make a dummy empty cell as a vertical spacer
$pdf->Cell(189, 10, '', 0, 1); //end of line


// INVOICE TABLE HEADER -------------------------------
$pdf->SetFont('Arial', 'B', 11);
$pdf->Cell(109, 10, mb_convert_encoding('Descripción', 'ISO-8859-1', 'UTF-8'), 1, 0, 'C');
$pdf->Cell(30, 10, mb_convert_encoding('P. Compra', 'ISO-8859-1', 'UTF-8'), 1, 0, 'C');
$pdf->Cell(20, 10, mb_convert_encoding('Cantidad', 'ISO-8859-1', 'UTF-8'), 1, 0, 'C');
$pdf->Cell(30, 10, mb_convert_encoding('Sub-toal', 'ISO-8859-1', 'UTF-8'), 1, 1, 'C');

$pdf->SetFont('Arial', '', 11);

//Obtenemos todos los detalles de la compra actual
$sql = "SELECT
pro_id
,tb_products.pro_description AS pro_description
,pur_det_pro_cost
,pur_det_pro_price
,pur_det_pro_qty
FROM tb_purchases_details
LEFT JOIN tb_products ON pro_id = pur_det_pro_id
WHERE pur_det_pur_id={$pur_id};";
// exit($sql);
$purchaseDetailsInfo = executePDOFetchAll($sql);

$purchaseTotal = 0;

// INVOICE TABLE DETAILS ------------------------------
foreach ($purchaseDetailsInfo as $key => $purDetInfo) {

	$pdf->Cell(109, 8, mb_convert_encoding($purDetInfo['pro_description'], 'ISO-8859-1', 'UTF-8'), 1, 0, 'L');
	$pdf->Cell(30, 8, number_format($purDetInfo['pur_det_pro_cost'], 2), 1, 0, 'C');
	$pdf->Cell(20, 8, $purDetInfo['pur_det_pro_qty'], 1, 0, 'C');
	$pdf->Cell(30, 8, number_format($purDetInfo['pur_det_pro_cost'] * $purDetInfo['pur_det_pro_qty'], 2), 1, 1, 'C');

	$purchaseTotal += $purDetInfo['pur_det_pro_cost'] * $purDetInfo['pur_det_pro_qty'];
}

// INVOICE TABLE FOOTER ------------------------------
$pdf->SetFont('Arial', 'B', 12);
$pdf->Cell(189, 10, '', 0, 1);
$pdf->Cell(110, 7, '', 0, 0);
$pdf->Cell(29, 7, 'TOTAL', 0, 0);
$pdf->Cell(50, 7, number_format($purchaseTotal, 2), 1, 1, 'R'); //end of line



$pdf->SetFont('Arial', '', 10);

$pdf->Output();
