<?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->Reminder);
if($payment->Reminder->Reason !== NULL){
	header("Location: mod.php?p=payment&a=receiptReminder&id=" . $payment->PaymentID);
}



?>
<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" />
				<span style="font-size: 28px; font-weight: bold;"><?= $config['Company'] ?></span><br>
				
				<span><?= $config['PostalAddress1'] ?></span><br />
				<span><?= $config['PostalCity'] . ", " . $config['PostalState'] . " " . $config['PostalZip'] ?></span><br />
				
				
				<h3>Payment Receipt</h3>
			</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>Offline ID #</span><?= $payment->OfflineID ?><br />-->
				<span>Transaction Date: </span><?= date("m/d/Y g:i a",strtotime($payment->PaymentDate	)) ?><br />
				<?php
				if($payment->CPD){
				?>
				<span>Cheque Posdatado: </span><?= date("m/d/Y",strtotime($payment->CPDDate	)) ?><br />
				<?php
				}
				?>
				<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>Subtotal</th>
							<th>Discount</th>
							<th>Paid</th>
						</tr>
					
				<?php
					foreach($payment->Invoices as $invoice){
						echo "<tr>\n";
						echo "<td style=\"text-align: left;\">" . $invoice->InvoiceID . "</td>\n";
						echo "<td>" . moneyFormat($invoice->Subtotal+$invoice->Tax) . "</td>\n";
						echo "<td>" . moneyFormat($invoice->Discount) . "</td>\n";
						echo "<td>" . moneyFormat($invoice->PaymentAmount) . "</td>\n";
					}
				?>
				</table>
			</div>
			<br />
			<div class="infoBlock">
				<table>
					
					<tbody>
						<tr>
							<td><b>Total Invoices:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalToApply) ?></td>
						</tr>
						
					</tbody>
				</table>
			</div>
			<?php
			
			if(count($payment->Credits) > 0){
			?>
			<br />
			<div class="center">
				<span><b>Credits</b></span><br>
				<table class="creditsTable" style="width: 90%; margin: auto;">
					
					<tr>
						<th style="text-align: left;">Number</th>
						<th>Balance</th>
						<th>Discount</th>
						<th>Applied</th>
					</tr>
				
				 <?php
				foreach($payment->Credits as $credit){
					echo "<tr>\n";
					echo "<td  style=\"text-align: left;\">" . $credit->CreditNumber . "</td>\n";
					echo "<td>" . moneyFormat($credit->Subtotal + $credit->Tax) . "</td>\n";
					echo "<td>" . moneyFormat($credit->Discount) . "</td>\n";
					echo "<td>" . moneyFormat($credit->Total) . "</td>\n";
				}
			?>    
				</table>
			</div>
			<br />
			<div class="infoBlock">
				<table>
					
					<tbody>
						<tr>
							<td><b>Total Credits:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->CreditSubtotal+$payment->CreditTax) ?></td>
						</tr>
					</tbody>
				</table>
			</div>
			<br />
			<?php    
			}
			?>
			<hr />
			<div class="center">
				<?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
				<span><b>Payment Details</b></span><br />
			</div>
			<table style="width: 90%;margin: auto;">
				
				<tr>
					<th style="text-align: left;">Method</th>
					<th>Reference</th>
					<th>Amount</th>
				</tr>
			
				<tbody>
					<tr>
						<td style="text-align: left;">Cash</td>
						<td></td>
						<td style="text-align: center;"><?= moneyFormat($payment->CashAmount) ?></td>
					</tr>
					<?php
						if(count($payment->Checks) > 0){
							foreach($payment->Checks as $check){
					?>
								<tr>
								 <td style="text-align: left;">Check</td>
								 <td style="text-align: center;"><?= $check->CheckNumber ?></td>
								 <td style="text-align: center;"><?= moneyFormat($check->Amount) ?></td>
								</tr> 
					<?php
							}
						}
						if(count($payment->Checks) > 1){
					?>
					<tr>
						<td style="text-align: left;"><b>Total Checks:</b></td>
						<td style="text-align: left;"></td>
						<td style="text-align: center;"><b><?= moneyFormat($payment->CheckAmount) ?></d></td>
					</tr>
					<?php
						}
					?>
				</tbody>
			</table>
			<?php
				if($payment->CPD){
			?>
				<center style="font-size: 22px;"><b>Cheque Posdatado: <?= date("m/d/Y",strtotime($payment->CPDDate)) ?></d></center>
			<?php
				}
			?>
			<hr />
			<br />
			<div class="infoBlock">
				<table style="width: 60%;">
					<thead></thead>
					<tbody>
						
						
						<tr>
							<td><b>Total To Apply:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalToApply) ?></td>
						</tr>
						<td><b>Subtotal Invoices:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->InvoiceSubtotal) ?></td>
						<tr>
						<td><b>Subtotal Credits:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->CreditSubtotal) ?></td>
						<tr>
						<tr>
							<td><b>Total Discount:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalDiscount) ?></td>
						</tr>
						<tr>
							<td></td>
							<td style="text-align: right;"></td>
						</tr>
							<td><b>Total IVU:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalTax) ?></td>
						</tr>
						
						<tr>
							<td></td>
							<td style="text-align: right;"></td>
						</tr>
						
						<tr>
							<td><b>Payment Total:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->CashAmount+$payment->CheckAmount) ?></td>
						</tr>
					</tbody>
				</table>
				<br />
				<span><b>Notes:</b> <?= ($payment->Notes) ?></span><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">
				<br /><br />
				<br /><br />
				<span><b>Salesman Signature</b></span>
				<br /><br />
				<br /><br />
				<span><b>Customer 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  if(false){ ?>
		
		<!--  2ND PRINT  -->
		<br /><br /><hr style="border-top: dotted 1px;" /><br />
		<div style="width: 400px;">
			<div class="center">
				<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 Receipt</h3>
			</div>
			<div class="infoBlock">
				<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 />
				<?php
				if($payment->CPD){
				?>
				<span>Cheque Posdatado: </span><?= date("m/d/Y",strtotime($payment->CPDDate	)) ?><br />
				<?php
				}
				?>
				<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">
				<span><b>Invoices</b></span><br />
				<table class="invoicesTable" style="width: 90%; margin: auto;">
					<thead>
						<tr>
							<th style="text-align: left;">Inv. Number</th>
							<th>Balance</th>
							<th>Discount</th>
							<th>Paid</th>
						</tr>
					</thead>
				<?php
					foreach($payment->Invoices as $invoice){
						echo "<tr>\n";
						echo "<td style=\"text-align: left;\">" . $invoice->InvoiceID . "</td>\n";
						echo "<td>" . moneyFormat($invoice->Subtotal+$invoice->Tax) . "</td>\n";
						echo "<td>" . moneyFormat($invoice->Discount) . "</td>\n";
						echo "<td>" . moneyFormat($invoice->PaymentAmount) . "</td>\n";
					}
				?>
				</table>
			</div>
			<br />
			<div class="infoBlock">
				<table>
					<thead></thead>
					<tbody>
						<tr>
							<td><b>Total Invoices:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalToApply) ?></td>
						</tr>
						
					</tbody>
				</table>
			</div>
			<?php
			
			if(count($payment->Credits) > 0){
			?>
			<br />
			<div class="center">
				<span><b>Credits</b></span><br>
				<table class="creditsTable" style="width: 90%; margin: auto;">
					<thead>
					<tr>
						<th style="text-align: left;">Number</th>
						<th>Balance</th>
						<th>Discount</th>
						<th>Applied</th>
					</tr>
				</thead>
				 <?php
				foreach($payment->Credits as $credit){
					echo "<tr>\n";
					echo "<td  style=\"text-align: left;\">" . $credit->CreditNumber . "</td>\n";
					echo "<td>" . moneyFormat($credit->Subtotal + $credit->Tax) . "</td>\n";
					echo "<td>" . moneyFormat($credit->Discount) . "</td>\n";
					echo "<td>" . moneyFormat($credit->Total) . "</td>\n";
				}
			?>    
				</table>
			</div>
			<br />
			<div class="infoBlock">
				<table>
					<thead></thead>
					<tbody>
						<tr>
							<td><b>Total Credits:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalCredits) ?></td>
						</tr>
					</tbody>
				</table>
			</div>
			<br />
			<?php    
			}
			?>
			<hr />
			<div class="center">
				<span><b>Payment Details</b></span><br />
			</div>
			
			<table style="width: 90%;margin: auto;">
				<thead>
				<tr>
					<th style="text-align: left;">Method</th>
					<th>Reference</th>
					<th>Amount</th>
				</tr>
				</thead>
				<tbody>
					<tr>
						<td style="text-align: left;">Cash</td>
						<td></td>
						<td style="text-align: center;"><?= moneyFormat($payment->CashAmount) ?></td>
					</tr>
					<?php
						if(count($payment->Checks) > 0){
							foreach($payment->Checks as $check){
					?>
								<tr>
								 <td style="text-align: left;">Check</td>
								 <td style="text-align: center;"><?= $check->CheckNumber ?></td>
								 <td style="text-align: center;"><?= moneyFormat($check->Amount) ?></td>
								</tr> 
					<?php
							}
						}
					?>
					<tr>
						<td style="text-align: left;"><b>Total Checks:</b></td>
						<td style="text-align: left;"></td>
						<td style="text-align: center;"><b><?= moneyFormat($payment->CheckAmount) ?></d></td>
					</tr>
					<?php
						if($payment->CPD){
					?>
					<tr>
						<td style="text-align: left;"><b>Cheque Posdatado</b></td>
						<td style="text-align: left;"></td>
						<td style="text-align: center;"><b><?= date("m/d/Y",strtotime($payment->CPDDate)) ?></d></td>
					</tr>
					<?php
						}
					?>
				</tbody>
			</table>
			<hr />
			<br />
			<div class="infoBlock">
				<table style="width: 55%;">
					<thead></thead>
					<tbody>
						
						
						<tr>
							<td><b>Total To Apply:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalToApply) ?></td>
						</tr>
						<td><b>Total Credits:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalCredits) ?></td>
						<tr>
						<tr>
							<td><b>Total Discount:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalDiscount) ?></td>
						</tr>
						<tr>
							<td></td>
							<td style="text-align: right;"></td>
						</tr>
							<td><b>Total IVU:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->TotalTax) ?></td>
						</tr>
						
						<tr>
							<td></td>
							<td style="text-align: right;"></td>
						</tr>
						
						<tr>
							<td><b>Payment Total:</b></td>
							<td style="text-align: right;"><?= moneyFormat($payment->CashAmount+$payment->CheckAmount) ?></td>
						</tr>
					</tbody>
				</table>
				<br />
				<span><b>Notes:</b> <?= ($payment->Notes) ?></span><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">
				<br /><br />
				<br /><br />
				<span><b>Salesman Signature</b></span>
				<br /><br />
				<br /><br />
				<span><b>Customer Signature</b></span>
				</div>
		</div>
		
		<!--  END 2ND PRINT  -->
		<?php
			}
		}
		?>
		
    </body>
    
</html>


