<?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).
 *
 *******************************************************************************
*/

	require "../_container/container_top.php";
	require_once ('business/PaymentMgr.php');
	require_once ('business/TenantMgr.php');
	require_once ('business/ReservationMgr.php');
	require_once ('business/MailMgr.php');
	require_once("config/Tagger.php");

	$paymentMgr = new PaymentMgr();
	$tenantMgr = new TenantMgr();
	$mailMgr = new MailMgr();
	$reservationMgr = new ReservationMgr();
	$tagger = new Tagger();

	$query = getPost('query');
	$action = getPost('action');
	$reservationid = getPost('reservationid');
	$paymentid = getPost('paymentid');

	$filter_year = getPost('filter_year');
	if (empty($filter_year)) {
		$filter_year = date("Y");
	}

// print_r($_POST);

	if (!empty($action)) {
		if ($action == "DELETE_PAYMENT") {
			$res = $paymentMgr->delete(getPost('paymentid'));
			if ($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "EDIT_PAYMENT") {
			$payment = array();
			$payment['payment_id'] = $paymentid;
			$payment['payment_date'] = getPost('paymentdate');
			$payment['reservation_id'] = getPost('reservid');
			$payment['method'] = getPost('payment_method');
			$payment['type'] = getPost('payment_type');
			$payment['amount'] = getPost('payment_montant');
			$payment['comment'] = getPost('inputComment');
			$res = $paymentMgr->update($payment);
			if ($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "PAY_ALL") {
			$reservation['paid'] = 1;
			$reservation['status'] = ReservationMgr::RESERV_CLOSED;
			$reservation['reservation_id'] = getPost('reservid');
			$res = $reservationMgr->update($reservation);
			if ($res == 1) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		if ($action == "PAY_PART") {
			$payment = array();
			$payment['payment_date'] = getPost('paymentdate');
			$payment['reservation_id'] = getPost('reservid');
			$payment['method'] = getPost('payment_method');
			$payment['type'] = getPost('payment_type');
			$payment['amount'] = getPost('payment_montant');
			$payment['comment'] = getPost('inputComment');
			$res = $paymentMgr->addPayment($payment);
			Logger::debug(' --> Add Payment res: ' . $res);
			if ($res > 0) {
				$msgType = "success";
				$msg = $tr->translate("msgSuccess");

				$mail = $mailMgr->getMail(5);
				if (isset($mail) && !empty($mail[0]->enable)){
					$tagger->setReservationId(getPost('reservid'));
					$tagger->setPaymentId($res);
					// Logger::debug(' --> mail before tagging: ' . print_r($mail, true));
					$mail[0]->from = $config['renter_email'];
					$mail[0]->to = $tagger->tag($mail[0]->to);
					$mail[0]->cc = $tagger->tag($mail[0]->cc);
					$mail[0]->bcc = $tagger->tag($mail[0]->bcc);
					$mail[0]->subject = $tagger->tag($mail[0]->subject);
					$mail[0]->content = $tagger->tag($mail[0]->content);
					// Logger::debug(' --> mail after tagging: ' . print_r($mail, true));
					include_once ('config/Mailer.php');
					$m = new Mailer($config);
					$r = $m->sendMail($mail);
					if (!$r) {
						$msgType = "error";
						$msg = $tr->translate("msgMailError");
					}
				}

				$paid = getPost('payment_paye');
				if (!empty($paid)){
					$reservation['paid'] = 1;
					$reservation['status'] = ReservationMgr::RESERV_CLOSED;
					$reservation['reservation_id'] = getPost('reservid');
					$res = $reservationMgr->update($reservation);
					if ($res == 1) {
						$msgType = "success";
						$msg = $tr->translate("msgSuccess");
					}else{
						$msgType = "error";
						$msg = $tr->translate("msgError");
					}
				}else{
					$reserv = $reservationMgr->getReservation(getPost('reservid'));
					if ($reserv[0]->status != ReservationMgr::RESERV_VALIDATE) {
						$reservation['status'] = ReservationMgr::RESERV_VALIDATE;
						$reservation['reservation_id'] = getPost('reservid');
						$res = $reservationMgr->update($reservation);
						if ($res == 1) {
							$msgType = "success";
							$msg = $tr->translate("msgSuccess");
						}else{
							$msgType = "error";
							$msg = $tr->translate("msgError");
						}
					}
				}
			}else{
				$msgType = "error";
				$msg = $tr->translate("msgError");
			}
		}
		echo '<div class="alert alert-' . $msgType . '">
			<strong>' . $msg . '</strong>
			</div>';
	}
	
	$arrPayment = $paymentMgr->getPayments();
	// print_r($arrPayment);

?>
	<div>
		<ul class="breadcrumb">
			<li><a href="./"><?php echo $tr->translate("menuHome"); ?></a> <span class="divider">/</span></li>
			<li class="active"><?php echo $tr->translate("Payments"); ?></li>
		</ul>
	</div>
	<div class="sortable row-fluid">
		<a data-rel="tooltip" title="<?php echo $tr->translate('AddReserv'); ?>" class="well span2 top-block btn-action" href="#" name="newReservation" id="newReservation" _page="reservationDetail.php" _action="Add">
			<i class="icon-red icon-user"></i>
			<div><?php echo $tr->translate("AddReserv"); ?></div>
			<div></div>
		</a>
		<a data-rel="tooltip" title="<?php echo $tr->translate("AddPayment"); ?>" class="well span2 top-block btn-action" href="#" name="newPayment" id="newPayment" _page="paymentDetail.php" _action="Add">
			<span class="icon32 icon-red icon-user"></span>
			<div><?php echo $tr->translate("AddPayment"); ?></div>
			<div></div>
		</a>
	</div>
			
	<div class="row-fluid sortable">
		<div class="box span12">
			<div class="box-header well" data-original-title>
				<h2><i class="icon-user"></i> <?php echo $tr->translate("Payments"); ?></h2>
			</div>
			<div class="box-content">
				<form class="form-horizontal" method="post" name="paymentForm" id="paymentForm" action="">
					<input type="hidden" name="action" id="action" value="" />
					<input type="hidden" name="paymentid" id="paymentid" value="" />
				<div class="table-responsive">
				<table id="tablePayments" name="tablePayments" class="table table-condensed table-striped table-bordered bootstrap-datatable datatable">
					<thead>
						<tr>
							<th>ID</th>
							<th><?php echo $tr->translate("thPaymentDate"); ?></th>
							<th><?php echo $tr->translate("thPaymentTenancy"); ?></th>
							<th><?php echo $tr->translate("thPaymentTenant"); ?></th>
							<th><?php echo $tr->translate("thPaymentFrom"); ?></th>
							<th><?php echo $tr->translate("thPaymentTo"); ?></th>
							<th><?php echo $tr->translate("thPaymentAmount"); ?></th>
							<th><?php echo $tr->translate("thPaymentType"); ?></th>
							<th><?php echo $tr->translate("thPaymentMethod"); ?></th>
							<th><?php echo $tr->translate("thPaymentAction"); ?></th>
						</tr>
					</thead>   
					<tbody>
					<?php
					foreach ($arrPayment as $payment) {
						// if ($filter_year == date("Y",strtotime($payment->payment_date))) {
					?>
					<tr>
						<td><?php echo $payment->payment_id; ?></td>
						<td><?php echo date($config['date_format'], strtotime($payment->payment_date)); ?></td>
						<td><?php echo $payment->tenancy_name; ?></td>
						<td>
							<a data-toggle="modal" data-target="#myModal" data-tenantid="<?php echo $payment->tenant_id; ?>" class="tenant-popup icon-question-sign" href="#"></a>
							<?php echo $payment->tenant_lastname . " " . $payment->tenant_name; ?></td>
						<td><?php echo date($config['date_format'], strtotime($payment->arrival_date)); ?></td>
						<td><?php echo date($config['date_format'], strtotime($payment->departure_date)); ?></td>
						<td><?php echo $payment->amount; ?></td>
						<td><?php echo $payment->type; ?></td>
						<td><?php echo $payment->method; ?></td>
						<td>
							<a class="btn btn-info btn-mini btn-action" title="<?php echo $tr->translate("tipPaymentEdit"); ?>" data-rel="tooltip" href="#" _id="<?php echo $payment->payment_id; ?>" _page="paymentDetail.php" _action="Edit">
								<i class="icon-edit icon-white"></i>
							</a>
							<a class="btn btn-danger btn-mini" title="<?php echo $tr->translate("tipPaymentDelete"); ?>" data-rel="tooltip" href="#" onclick="goAction('DELETE_PAYMENT', <?php echo $payment->payment_id; ?>);">
								<i class="icon-trash icon-white"></i>
							</a>
						</td>
					</tr>
					<?php // } ?>
					<?php } ?>
				  </tbody>
			  </table>
			  </div>
			</div>
		</div><!--/span-->
	
	</div><!--/row-->

	<div class="row-fluid sortable" id="editPayment">
	</div><!--/row-->
<?php require "../_container/container_bottom.php"; ?>
<script>
	$(document).ready(function() {
		if ($('#tablePayments').length) {
			myTable = $('#tablePayments').dataTable();
			var oSettings = myTable.fnSettings();
			oSettings.aoColumns[1].sType = "custom-date";
			oSettings.aoColumns[4].sType = "custom-date";
			oSettings.aoColumns[5].sType = "custom-date";
			oSettings.aoColumns[6].sType = "custom-numeric";
		}
	});
	function deleteAction() {
		// $('#content').slideUp("slow");
		blockUI();
		paymentForm.submit();
	}
	function goAction(action, id){
		if (action == "DELETE_PAYMENT"){
			$('#action').val(action);
			$('#paymentid').val(id);
			$("#dialog-confirm").dialog("open");
			// paymentForm.submit();
		}
		if (action == "EDIT_PAYMENT"){
			$('#action').val(action);
			$('#paymentid').val(id);
			$("#dialog-confirm").dialog("open");
			// paymentForm.submit();
		}
	}
</script>