<?php
require_once dirname(__FILE__)."/".str_repeat("../",4)."lib-tk/shield.php";
require_once "lib-action/action.php";

class DownloadInvoiceFactura extends MB_Action
{
	public function execute()
	{
		parent::execute();
		
		$invoiceFactura = Factory()->InvoiceFactura(bgpc("invoiceFacturaID"));
		
		try {
			$dstPath = $invoiceFactura->generatePDF();
			
			$filename = addslashes(blmsg("TRANS_INVOICE_FACTURA_FILENAME", array("NUMBER" => $invoiceFactura->id)));
			
			header("Content-Type: application/pdf");
			// header("Content-Length: " . filesize($dstPath));
			header("Content-Disposition: attachment; filename=\"" . $filename . ".pdf\"");
			header("Pragma: no-cache");
			header("Expires: 0");
			
			readfile($dstPath);
		} catch (ProductException $productException) {
			getCurrentSession()->Storage->errorMessages[] = $productException->getMessage();
			redirect(action_url("invoices", "ShowInvoiceFacturas"));
		}
	}
}
