<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../services');
	ini_set('include_path', ini_get('include_path') . PATH_SEPARATOR . '../../services/lib');
	// require_once('config/Settings.php');
	require_once ('business/ReservationMgr.php');
	require_once('config/Logger.php');
	include_once('business/LeaseContentMgr.php');
	include_once('business/ConfigMgr.php');
	include_once("config/Tagger.php");

	$action = $_POST['action'];

	$leaseContentMgr = new LeaseContentMgr();

	$configMgr = new ConfigMgr();
	$config = $configMgr->getConfigs();

	$tagger = new Tagger();
	if ($action !== "preview"){
		$reservationid = $_POST['reservationid'];
		$tagger->setReservationId($reservationid);
		$reservationMgr = new ReservationMgr();
		$res = $reservationMgr->getReservation($reservationid);
		$selectTenancy = $res[0]->tenancy_id;
		$tenancyName = $res[0]->tenancy_name;
	}else{
		require_once ('business/TenancyMgr.php');
		$tenancyMgr = new TenancyMgr();
		$selectTenancy = $_POST['selectTenancy'];
		$res = $tenancyMgr->getTenancy($selectTenancy);
		$tenancyName = $res[0]->name;
	}

	$arrLeaseContent = $leaseContentMgr->getLease($selectTenancy);


	/////////////////////  PDF  ////////////////////
	$filename = ($action == "preview") ? 'preview.pdf' : $tagger->tag($config['lease_filename']) . '.pdf';
	require_once('lib/myPdf.php');
	$pdf = new MYPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
	$pdf->SetOutput("D");
	$pdf->SetPageBreak($leaseContentMgr->getPageBreak($selectTenancy));
	$pdf->SetFileName($filename);
	$pdf->SetImageFilename(LEASE_IMAGE_PATH . "/lease_" . $selectTenancy . ".jpg");
	$pdf->SetSigningFileName(LEASE_IMAGE_PATH . "/signing_" . $selectTenancy . ".jpg");
	$pdf->SetDuplicateLease($config['lease_create_2copy']);
	$pdf->SetDateFormat($config['date_format']);
	$pdf->SetLeaseContentTitle($tr->translate("leaseContentTitle"));
	// $pdf->SetLeaseContentSubTitle($tenancyName);
	$pdf->SetLeaseContentInitials($tr->translate("leaseContentInitials"));
	$pdf->SetLeaseContentPage($tr->translate("leaseContentPage"));
	$pdf->SetLeaseContentSigningHeader($tr->translate("leaseContentSigningHeader"));
	$pdf->SetLeaseContentSigningTenant($tr->translate("leaseContentSigningTenant"));
	$pdf->SetLeaseContentSigningLandlord($tr->translate("leaseContentSigningLandlord"));
	$pdf->SetLeaseContentSigningTenant2($tr->translate("leaseContentSigningTenant2"));
	$pdf->BuildContent($arrLeaseContent, $action);
	/////////////////////  PDF  ////////////////////
?>