<?php
include('config.inc.php');

require_once('library/tcpdf/tcpdf.php');


define(SIDE_MARGIN, 0.5);
define(TOP_MARGIN, 0.5);

// retrieve the receipt from the database
$ch = curl_init(MVADMIN_WEBSERVICE . '/site/receipt?key=' . $_GET['key'] . '&id=' . $_GET['id'] . '&sale_id=' . $_GET['sale_id']); // start CURL - set URL
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // get the response as a variable
$jsonData = curl_exec($ch); // connect and get your JSON response 
$info = curl_getinfo($ch);
curl_close($ch);
$data = json_decode($jsonData);


// create new PDF document
$pdf = new TCPDF('Portrait', 'in', 'LETTER');

$pdf->SetCreator('mozola-ventures-admin');
$pdf->SetAuthor('Mozola Ventures Corp.');
$pdf->SetTitle('Mozola Ventures Receipt');

$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);

$pdf->SetMargins(SIDE_MARGIN, TOP_MARGIN, SIDE_MARGIN);

$pdf->AddPage();

// company section
$pdf->Image('assets/img/mozola_ventures_logo_banner.png', 2.75, 0.5, 3.0);

$pdf->SetFont('helvetica', 'B', 15);
$pdf->SetXY(SIDE_MARGIN, 1.5);
$pdf->Cell(0, 0, $data->company->name, 0, 1);

$pdf->SetFont('helvetica', '', 8);
$pdf->Cell(0, 0, $data->company->email, 0, 1);

$pdf->SetFont('helvetica', 'B', 8);
$pdf->Cell(0, 0, $data->company->address, 0, 1);
$text = $data->company->city . ', ' . $data->company->province . ' ' . $data->company->postal_code;
$pdf->Cell(0, 0, $text, 0, 1);
$pdf->Cell(0, 0, $data->company->country, 0, 1);

// receipt section
$pdf->SetFont('helvetica', 'BI', 15);
$pdf->SetXY(-SIDE_MARGIN, 1.5);
$pdf->Cell(0, 0, 'RECEIPT', 0, 1, 'R');

$pdf->SetFont('helvetica', 'B', 8);
$pdf->SetXY(-SIDE_MARGIN, 1.8);
$text = '#' . $data->sale_id;
$pdf->Cell(0, 0, $text, 0, 1, 'R');

$pdf->SetX(-SIDE_MARGIN);
$text = substr($data->create_time, 0, 10);
$pdf->Cell(0, 0, $text, 0, 1, 'R');

$posY = 2.8;
// customer section
$pdf->SetFont('helvetica', 'B', 8);
$pdf->SetXY(SIDE_MARGIN, $posY);
$text = 'Customer' . ': ';
$pdf->Cell(0, 0, $text, 0, 0, '', 0, '', 0, false, 'B');
$xpos = SIDE_MARGIN + $pdf->GetStringWidth($text, 'helvetica', 'B', 8);

$pdf->SetFont('helvetica', '', 8);
$pdf->SetXY($xpos, $posY);
$text = $data->payer->payer_info->first_name . ' ' . $data->payer->payer_info->last_name;
$pdf->Cell(0, 0, $text, 0, 1, '', 0, '', 0, false, 'B');

$pdf->SetX($xpos);
$pdf->Cell(0, 0, $data->payer->payer_info->email, 0, 1);

$posY = 3.25;
// business number/HST number section
$pdf->SetFont('helvetica', '', 8);
$pdf->SetXY(-SIDE_MARGIN, $posY);
$text = 'Business Number: ' . $data->company->business_number;
$pdf->Cell(0, 0, $text, 0, 1, 'R');

if (isset($data->amount->tax_code)) {
	if ($data->amount->tax_code == 'HST') {
		$pdf->SetX(-SIDE_MARGIN);
		$text = 'HST Number: ' . $data->company->hst_number;
		$pdf->Cell(0, 0, $text, 0, 1, 'R');
	}
}

$posY = $pdf->GetY();

// table header
$colWidth = array(0.75, 4, 1.25, 1.5);
$pdf->SetFillColor(200);
$pdf->SetFont('helvetica', 'B', 8);
$pdf->SetXY(SIDE_MARGIN, $posY);
$pdf->Cell($colWidth[0], 0.25, 'QUANTITY', 'LTR', 0, 'C', true, '', 0, false, 'T', 'C');
$pdf->Cell($colWidth[1], 0.25, 'DESCRIPTION', 'LTR', 0, 'C', true, '', 0, false, 'T', 'C');
$pdf->Cell($colWidth[2], 0.25, 'PRICE', 'LTR', 0, 'C', true, '', 0, false, 'T', 'C');
$pdf->Cell($colWidth[3], 0.25, 'AMOUNT', 'LTR', 1, 'C', true, '', 0, false, 'T', 'C');

// items
$pdf->SetFont('helvetica', '', 8);
foreach ($data->items as $item) {
	$pdf->Cell($colWidth[0], 0, $item->quantity, 'LR', 0, 'R');
	$pdf->Cell($colWidth[1], 0, $item->description, 'LR', 0, 'L');
	$text = '$' . number_format($item->price,2);
	$pdf->Cell($colWidth[2], 0, $text, 'LR', 0, 'R');
	$text = '$' . number_format($item->quantity * $item->price,2);
	$pdf->Cell($colWidth[3], 0, $text, 'LR', 1, 'R');
}

$totalBorders = 'TR';	// setting the borders for the totals row - this depends on if there is a tax line or not

// tax
if (isset($data->amount->tax_code)) {
	if ($data->amount->tax_code == 'HST') {
		$pdf->SetFont('helvetica', 'B', 8);
		$pdf->Cell($colWidth[0] + $colWidth[1] + $colWidth[2], 0, 'HST (13%)', 'TR', 0, 'R');
		$pdf->SetFont('helvetica', '', 8);
		$text = '$' . number_format($data->amount->tax,2);
		$pdf->Cell($colWidth[3], 0, $text, 'LTR', 1, 'R');
		$totalBorders = 'R';	// since the tax row did the upper line, make sure the totals row does not
	}
}

// total
$pdf->SetFont('helvetica', 'B', 8);
$text = 'TOTAL (' . $data->amount->currency . ')';
$pdf->Cell($colWidth[0] + $colWidth[1] + $colWidth[2], 0, $text, $totalBorders, 0, 'R');
$text = '$' . number_format($data->amount->total,2);
$pdf->Cell($colWidth[3], 0, $text, 'LTRB', 1, 'R', true);


// payment method
$pdf->SetFont('helvetica', 'I', 8);
$pdf->Ln();
$text = 'Payment made via ' . $data->payer->payment_method;
$pdf->Cell(0,0, $text, '', 1, 'C');


$pdf->Output('receipt.pdf', 'I');
?>