<?php

if(false){
class Payment {
	public $PaymentID, $Customer, $PaymentDate, $CPD, $CPDDate, $Invoices, $Credits, $CashAmount, $CheckAmount, $Checks, $Salesman, $Subtotal, $TotalDiscount, $TotalTax, $TotalCredits, $TotalToApply, $TotalToPay;
	public function __construct($ID) {
		$query = "SELECT * FROM PAYMENT_HEAD WHERE ID = $ID";
		global $con;
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result);
		$this->PaymentID = $row['ID'];
		$this->Customer = new Customer($row['AccountNumber']);
		$this->PaymentDate = $row['OrderDate'];
		$this->CPD = $row['CPD'];
        $this->CPDDate = $row['CPDDate'];
        $this->CashAmount = $row['CashAmount'];
        $this->CheckAmount = $row['CheckAmount'];
		$this->Salesman = new Salesman($row['SalesmanID']);
		$this->Invoices = array();
		$query = "SELECT * FROM PAYMENT_DETAIL WHERE PaymentID = $ID";
		$result = mysqli_query($con, $query);
		$this->Subtotal = 0;
		$this->TotalDiscount = 0;
		$this->TotalTax = 0;
		$this->TotalToApply = 0;
		$this->TotalToPay = 0;
		$this->TotalCredits = 0;
		while($row = mysqli_fetch_array($result)){
			$invoice = (object) array();
			$invoice->InvoiceID = $row['InvoiceID'];
			$invoice->Reference = $row['Reference'];
			$invoice->PartialPayment = $row['PartialPayment'];
			$invoice->Subtotal = $row['SubtotalAmount'];
			$invoice->Discount = $row['DiscountAmount'];
			$invoice->Tax = $row['TaxAmount'];
			$invoice->PaymentAmount = $row['PaymentAmount'];
			$this->Subtotal += $invoice->Subtotal;
			if(!$this->CPD){
				$this->TotalDiscount += $invoice->Discount;
				$this->TotalTax += $invoice->Tax;
				$this->TotalToApply += $row['SubtotalAmount'] + $row['TaxAmount'];
			}else{
				$this->TotalToApply += $row['PaymentAmount'];
			}
			$this->TotalToPay += $row['PaymentAmount'];
			
			array_push($this->Invoices, $invoice);
			unset($invoice);
		}
		$this->Credits = array();
		$query = "SELECT * FROM PAYMENT_CREDIT WHERE PaymentID = $ID";
		$result = mysqli_query($con, $query);
		while($row = mysqli_fetch_array($result)){
			$credit = (object) array();
			$credit->CreditNumber = $row['CreditNumber'];
			$credit->CreditDate = $row['CreditDate'];
			$credit->Reference = $row['Reference'];
			$credit->Subtotal = $row['Subtotal'];
			$credit->Discount = $row['Discount'];
			$credit->Tax = $row['Tax'];
			$credit->Total = $row['Subtotal'] - $row['Discount'] + $row['Tax'];
			$this->TotalCredits += $credit->Total;
			array_push($this->Credits, $credit);
			unset($credit);
		}
		$this->Checks = array();
		$query = "SELECT * FROM PAYMENT_CHECK WHERE PaymentID = $ID";
		$result = mysqli_query($con, $query);
		while($row = mysqli_fetch_array($result)){
			$check = (object) array();
			$check->CheckNumber = $row['CheckNumber'];
			$check->Amount = $row['Amount'];
			array_push($this->Credits, $check);
			unset($check);
		}
	}
}

}
include_once("connect.php");
include_once("session.php");

$paymentID = $_REQUEST['id'];

$payment = new Payment($paymentID);
$payment->GetReminderData();

//var_dump($payment->ca);

?>
<html>
    <head>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
		<style>
			.center {
				text-align: center;
			}
			.invoicesTable td{
				text-align: right;
				padding-right: 15px;
			}
			.creditsTable td{
				text-align: right;
				padding-right: 15px;
			}
			.infoBlock {
				margin-left: 10px;
			}
			.moneyBlock {
				margin-left: 60px;
			}
			body {
				font-family: "Trebuchet MS", Helvetica, sans-serif;
			}
			
			@media print {    
				.no-print, .no-print *
				{
					display: none !important;
				}
			}
		</style>
    </head>
    <body>
		<div style="width: 400px;">
			<div class="center">
				<button onclick="reprintandclose()" class="no-print">Reprint</button><br class="no-print" />
				<img style="width: 200px;" src="img/logo_ej_small.png" alt=""><br />
				<!--<span style="font-size: 28px; font-weight: bold;">EuroJapon Auto Supply</span>-->
				<span>PO Box 191538</span><br />
				<span>San Juan, PR 00919-1538</span><br />
				<span>(787) 760-7295</span>
				
				<h3>Payment Reminder</h3>
				<h4>---This is NOT a payment receipt---</h4>
			</div>
			<div class="infoBlock">
				<?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
				<span>Customer: </span><?= $payment->Customer->AccountNumber ?><br />
				<?= $payment->Customer->Name . " - " . $payment->Customer->City ?><br />
				<span>Salesman: </span><?= $payment->Salesman->ID . " - " . $payment->Salesman->Name ?><br />
				<span>Receipt #</span><?= $payment->PaymentID ?><br />
				<span>Transaction Date: </span><?= date("m/d/Y g:i a",strtotime($payment->PaymentDate	)) ?><br />
				
				<br />
			</div>
			<?php
				if($payment->Loaded){
			?>
			<div class="center">
				<span><b>*R E P R I N T*</b></span><br /><br />
			</div>
			<?php
				}
			?>
			<div class="center">
				<?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
				<span><b>Invoices</b></span><br />
				<table class="invoicesTable" style="width: 90%; margin: auto;">
					
						<tr>
							<th style="text-align: left;">Inv. Number</th>
							<th>Date</th>
							<th>Days</th>
							<th>Subtotal</th>
							
							
						</tr>
					
				<?php
					$totalPastDue = 0;
					foreach($payment->Invoices as $invoice){
						
						echo "<tr>\n";
						echo "<td style=\"text-align: left;\">" . $invoice->InvoiceID . "</td>\n";
						echo "<td>" . date("m/d/Y", strtotime(($invoice->InvoiceDate))) . "</td>\n";
						echo "<td>" . ceil((time()-strtotime(($invoice->InvoiceDate)))/(60*60*24)) . "</td>\n";
						echo "<td>" . moneyFormat($invoice->Subtotal+$invoice->Tax) . "</td>\n";
						$totalPastDue += $invoice->Subtotal+$invoice->Tax;
						
					}
				?>
				</table>
			</div>
			<br />
			<div class="infoBlock">
				<table>
					
					<tbody>
						<tr>
							<td><b>Total Past Due:</b></td>
							
							<td style="text-align: right;"><?= moneyFormat($totalPastDue) ?></td>
						</tr>
						
					</tbody>
				</table>
			</div>
			
			<hr />
			<div class="center">
				<?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
				<span><b>Reminder Details</b></span><br />
			</div>
			<table style="width: 90%;margin: auto;">
				
				
			
				<tbody>
					<tr>
						<td style="text-align: left;">Reason</td>
						<td style="text-align: right;"><?= $payment->Reminder->Reason ?></td>
					</tr>
				<?php
					if($payment->Reminder->Reason == "Pasar en otra Fecha"){
				?>
					<tr>
						<td style="text-align: left;">Next Visit Date</td>
						<td style="text-align: right;"><?= date('m/d/Y', strtotime($payment->Reminder->NextVisitDate)) ?></td>
					</tr>
				<?php
					}
				?>
					
				</tbody>
			</table>
			
			<hr />
			<br />
			<b>Notes:</b>
			<?= ($payment->Reminder->Notes) ?>
			<br />
			
			<br />
			<div class="center">
			<h4>---This is NOT a payment receipt---</h4>
			</div>
			<?php
				if($payment->Loaded){
			?>
			<br />
			<div class="center">
				<span><b>*R E P R I N T*</b></span><br /><br />
			</div>
			<?php
				}
			?>
			<div class="center">
				<br /><br />
				<br /><br />
				<span><b>Salesman Signature</b></span>
				<br /><br />
				<br /><br />
				<span><b>Cutomer Signature</b></span>
				</div>
		</div>
		<script>
			function reprintandclose(){
				window.print();
				setTimeout(function(){
					window.close();
				},500);
			}
		</script>
		<?php
			if(!$payment->Loaded){
		?>
		<script>
			$.get("mod.php?p=payment&a=functions&f=flagLoadedReceipt&pid=" + <?= $paymentID ?>, function(data){
				
			});
			window.print();
			//alert("Press Ok to Print Customer Copy");
			window.print();
			
		</script>
		
		
		
		<?php
		
		}
		?>
		
    </body>
    
</html>


