<?php 
include_once("../connect.php");
include_once("../session.php");

/*
class Payment {
	public $PaymentID, $Customer, $PaymentDate, $CPD, $CPDDate, $Invoices, $Credits, $CashAmount, $CheckAmount, $Checks, $Salesman, $Subtotal, $TotalDiscount, $TotalTax, $TotalCredits, $TotalToApply, $TotalToPay, $ProcessDate, $Processed;
	public function __construct($ID) {
		$query = "SELECT * FROM PAYMENT_HEAD WHERE ID = $ID";
		global $con;
		error_log("Query class creation: " . $query);
		$result = mysqli_query($con, $query);
		$row = mysqli_fetch_array($result);
		$this->PaymentID = $row['ID'];
		$this->Customer = new Customer($row['AccountNumber']);
		$this->PaymentDate = $row['PaymentDate'];
		$this->CPD = $row['CPD'];
        $this->CPDDate = $row['CPDDate'];
        $this->CashAmount = $row['CashAmount'];
        $this->CheckAmount = $row['CheckAmount'];
		$this->Salesman = new Salesman($row['SalesmanID']);
		$this->ProcessDate = $row['ProcessDate'];
		$this->Processed = $row['Processed'];
		$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->InvoiceNumber = $row['InvoiceNumber'];
			$invoice->Reference = $row['Reference'];
			$invoice->PartialPayment = $row['PartialPayment'];
			$invoice->Subtotal = $row['SubtotalAmount'];
			$invoice->Discount = $row['DiscountAmount'];
			$invoice->Tax = $row['TaxAmount'];
			$invoice->Total = $row['SubtotalAmount'] - $row['DiscountAmount'] + $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->Checks, $check);
			unset($check);
		}
	}
	public function ProcessPayment(){
		global $con;
		$query = "UPDATE PAYMENT_HEAD SET ProcessDate = DATE_ADD(NOW(), INTERVAL -4 HOUR), Processed = TRUE WHERE id = " . $this->PaymentID;
		$result = mysqli_query($con,$query);
		$query = "SELECT ProcessDate FROM PAYMENT_HEAD WHERE id = " . $this->PaymentID;
		$result = mysqli_query($con,$query);
		$row = mysqli_fetch_array($result);
		return $row['ProcessDate'];
		
	}
}

*/



$isAdmin = $_SESSION['isAdmin'];
if (!$isAdmin) {
	header("Location: logout.php");
}
$payment = new Payment($_REQUEST['id']);
$payment->getReminderData();

//$key = substr(md5($_REQUEST['order'] . "EJAS"), -16);
$url = $root . "mod.php?p=payment&a=receipt&id=" . $payment->PaymentID;

 ?>
 <!DOCTYPE html>
  <html lang="en">
    <head>
      <meta charset="utf-8">
      <meta http-equiv="X-UA-Compatible" content="IE=edge">
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <title><?= $config['CompanyName'] ?></title>
  
      <!-- Bootstrap -->
      <link href="<?= $root ?>css/bootstrap.min.css" rel="stylesheet">
      <link rel="stylesheet" href="<?= $root ?>css/bootstrap-combobox.css" />
  
      <!-- HTML5 shim and Respond.js for IE8 support of HTML5 elements and media queries -->
      <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
      <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
        <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
        <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
            <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
            <!-- Include all compiled plugins (below), or include individual files as needed -->
            <script src="<?= $root ?>js/bootstrap.min.js"></script>
        	 <script src="<?= $root ?>js/bootstrap-combobox.js"></script>
            <script type="text/javascript">
              $(document).ready(function(){
        //        $('.grpline').css("width", "100px");
              });
            </script>
 
      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
	<style>
		.paymentSummaryHeader{
			font-weight: bold;
		}
		
	</style>
    </head>
    <body>
    
        <?php 
        		include("navbar.php"); 
        		
 
        ?>     
 
    
    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
    <a style="float: right; margin-top: 5px; margin-right: 5px;" class="btn btn-primary hidden-print" href="<?= $url ?>">Printer Friendly Version</a>
 <div class="container-fluid" style="padding-top: 5px;">
 	<div class="row">
 	  <div class="col-xs-5">
 	  	<img class="img-responsive" style="width: 200px;" src="<?= $root ?>img/<?= $config['Logo'] ?>" alt="" />
 	  	<p>
 	  		<b>Customer Name: </b><?= $payment->Customer->Name; ?><br>
 	  		<b>City: </b><?= $payment->Customer->City; ?><br>
 	  		<b>Customer #: </b><?= $payment->Customer->AccountNumber ?><br />
            <?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
		</p>
 	  </div>
  	  <div class="col-xs-2"></div>
 	  <div class="col-xs-5">
 		  <h3><?= $payment->CPD?"CPD ":"" ?>Payment Reminder</h3>
 		  <p>
 		  	<b>Sequence Number: </b><span style="font-style: italic;"><?= $payment->PaymentID ?></span><br>
 		  <b>Date: </b><?= date("m/d/Y",strtotime($payment->PaymentDate)) ?><br>
 		  <b>Time: </b><?= date("g:i a",strtotime($payment->PaymentDate)) ?><br>
 		  <b>Salesman: </b> <?= $payment->Salesman->ID . " - " . $payment->Salesman->Name ?><br>
 		  <?= $payment->Void?"<center><h1>VOID</h1></center>":"" ?>
          </p>
          
 	  </div>
	</div> 
	<div class="row">
		<div class="col-xs-12">
			<h4>Invoices</h4>
			<table class="table table-condensed" style="font-size: small;">
			  <thead>
				<tr>
				  <th style="width: 20%">Invoice Number</th>
                  <th style="width: 20%">Invoice Date</th>
				  <th style="width: 20%">Reference</th>
				  <th style="width: 10%">Balance</th>
				  <th style="width: 10%">Discount</th>
				  <th style="width: 10%">IVU</th>
				  <th style="width: 10%">Total</th>
				</tr>
			  </thead>
			  <tbody>
			  <?php
                $totalInvoice = 0;
                $totalInvoiceTax = 0;
                $totalInvoiceDiscount = 0;
				  foreach($payment->Invoices as $invoice){
                    $totalInvoice += $invoice->Subtotal;
                    $totalInvoiceTax += $invoice->Tax;
                    $totalInvoiceDiscount += $invoice->Discount;
			  ?>
				  <tr>
						  <td>
							  <span class="itemInfo"><?= $invoice->InvoiceNumber ?></span>
						  </td>
                          <td>
							  <span class="itemInfo"><?= ($invoice->InvoiceDate == "0000-00-00")?"Not on File":date("m/d/Y",strtotime($invoice->InvoiceDate)) ?></span>
						  </td>
						  <td>
							  <span class="itemInfo"><?= $invoice->Reference ?></span>
						  </td>
						  <td class="centered">
							  <?= moneyFormat($invoice->Subtotal) ?>
						  </td>
						  <td>
							  <?= moneyFormat($invoice->Discount) ?>
						  </td>
						  <td>
							  <?= moneyFormat($invoice->Tax) ?>
						  </td>
						  <td>
							  <?= moneyFormat($invoice->PaymentAmount) ?>
						  </td>
				  </tr>
				  <?php 
				  
				  }?>
				  <tr>
					  <td><span style="font-weight: bold;">Total</span></td>
					  <td></td>
                      <td></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($payment->Subtotal) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($totalInvoiceDiscount) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($totalInvoiceTax) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($payment->TotalToPay) ?></span></td>
				  </tr>
				</tr>
			  </tbody>
			</table>
		</div>
	</div>
	
	
	<div class="row">
		<div class="col-sm-12">
            <div class="col-sm-8">
                <h4>Reminder Information</h4>
                <div class="col-xs-4">
                    <table class="table table-condensed">
                      <tbody>
                          <tr>
                            <td><span class="paymentSummaryHeader">Reason</span></td>
                            <td><?= $payment->Reminder->Reason?></td>
                          </tr>
						  <?php
						   if($payment->Reminder->Reason == "Pasar en otra Fecha"){
						  ?>
                          <tr>
                            <td><span class="paymentSummaryHeader">Next Visit Date</span></td>
                            <td><?= date("m/d/Y", strtotime($payment->Reminder->NextVisitDate)) ?></td>
                          </tr>
						  <?php
						   }
						  ?>
                          
                      </tbody>
                    </table>
                </div>
                
            </div>
    
        </div>
	</div>
     <div>
        <span><b><big>Salesman Notes: </big></b><?= $payment->Reminder->Notes ?></span>
     </div>
</div>
 
        <div class="panel panel-default">
          <div class="panel-body">
           
		   
          </div>
        </div>
        
    </body>
  </html>