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

$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</h3>
 		  <p>
 		  	<b>Payment Number: </b><span style="font-style: italic;"><?= $payment->PaymentID ?></span><br>
			<?php
			if($payment->OfflineID){
				
			?>
				<b>Offline ID: </b><span style="font-style: italic;"><?= $payment->OfflineID ?></span><br>
			<?php
			
			}
			?>
 		  <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->InvoiceID ?></span>
							  <?php
							  if(round($invoice->Subtotal-$invoice->Discount+$invoice->Tax,2) > $invoice->PaymentAmount){
								echo "<b><i>Abono</i></b>";
							  }
							  ?>
						  </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-xs-12">
			<h4>Credits</h4>
            <?php
                if(count($payment->Credits) > 0){
            ?>
			<table class="table table-condensed" style="font-size: small;">
			  <thead>
				<tr>
				  <th style="width: 20%">Credit Number</th>
                  <th style="width: 20%">Credit Date</th>
				  <th style="width: 20%">Reference</th>
				  <th style="width: 10%">Subtotal</th>
				  <th style="width: 10%">Discount</th>
				  <th style="width: 10%">IVU</th>
				  <th style="width: 10%">Total</th>
				</tr>
			  </thead>
			  <tbody>
			  <?php
                $totalCreditTax = 0;
                $totalCreditDiscount = 0;
                $totalCredit = 0;
				  foreach($payment->Credits as $credit){
                    $totalCreditTax += $credit->Tax;
                    $totalCreditDiscount += $credit->Discount;
                    $totalCredit += $credit->Total;
			  ?>
				  <tr>
						  <td>
							  <span class="itemInfo"><?= $credit->CreditNumber ?></span>
						  </td>
                          <td>
							  <span class="itemInfo"><?= ($credit->CreditDate == "0000-00-00")?"Not on File":date("m/d/Y",strtotime($credit->CreditDate)) ?></span>
						  </td>
						  <td>
							  <span class="itemInfo"><?= $credit->Reference ?></span>
						  </td>
						  <td class="centered">
							  <?= moneyFormat($credit->Subtotal) ?>
						  </td>
						  <td>
							  <?= moneyFormat($credit->Discount) ?>
						  </td>
						  <td>
							  <?= moneyFormat($credit->Tax) ?>
						  </td>
						  <td>
							  <?= moneyFormat($credit->Total) ?>
						  </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->TotalCredits) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($totalCreditDiscount) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($totalCreditTax) ?></span></td>
					  <td><span style="font-weight: bold;"><?= moneyFormat($totalCredit) ?></span></td>
				  </tr>
				</tr>
			  </tbody>
			</table>
            <?php
                }else{
                    echo "No credits applied";
                }
                
            ?>
            
		</div>
	</div>
	<div class="row">
		<div class="col-sm-12">
            <div class="col-sm-8">
                <h4>Payment Information</h4>
                <div class="col-xs-4">
                    <table class="table table-condensed">
                      <tbody>
                          <tr>
                            <td><span class="paymentSummaryHeader">Invoice Subtotal</span></td>
                            <td><?= moneyFormat($payment->InvoiceSubtotal) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Total Discount</span></td>
                            <td><?= moneyFormat($payment->TotalDiscount) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Total Tax</span></td>
                            <td><?= moneyFormat($payment->TotalTax) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Total Charged</span></td>
                            <td><?= moneyFormat($payment->TotalToPay + $totalCredit) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Total To Apply</span></td>
                            <td><?= moneyFormat($payment->TotalToApply) ?></td>
                          </tr>
                        </tr>
                      </tbody>
                    </table>
                </div>
                <div class="col-xs-4">
                    <table class="table table-condensed">
                      <tbody>
                        <tr>
                            <td><span class="paymentSummaryHeader">Credits Subtotal</span></td>
                            <td><?= moneyFormat($payment->TotalCredits) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Cash Amount</span></td>
                            <td><?= moneyFormat($payment->CashAmount) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Check Amount</span></td>
                            <td><?= moneyFormat($payment->CheckAmount) ?></td>
                          </tr>
                          <tr>
                            <td><span class="paymentSummaryHeader">Total Amount</span></td>
                            <td><?= moneyFormat($payment->CheckAmount+$payment->CashAmount) ?></td>
                          </tr>
                          <tr>
                            <td></td>
                            <td></td>
                          </tr>
                      </tbody>
                    </table>
                </div>
            </div>
    <?php
        if(count($payment->Checks) > 0){	
	?>
            <div class="col-sm-3">
                <h4>Checks Detail</h4>
                <div class="col-xs-12">
                    <table class="table table-condensed">
                      <thead>
                          <tr>
                            <th><span class="paymentSummaryHeader">Reference</span></th>
                            <th><span class="paymentSummaryHeader">Amount</span></td>
                          </tr>
                      </thead>
                      <tbody>
                        <?php
                            foreach($payment->Checks as $check){
                        ?>
                          <tr>
                            <td><?= $check->CheckNumber ?></td>
                            <td><?= moneyFormat($check->Amount) ?></td>
                          </tr>
                        <?php
                                if($payment->CPD){
                        ?>
                                <tr>
                                    <td><b>CPD Date</b></td>
                                    <td><?= date("m/d/Y",strtotime($payment->CPDDate)) ?></td>
                                </tr>
                                
                        <?php
                                }
                            }
                        ?>
                      </tbody>
                    </table>
                </div>    
            </div>
    <?php
        }
    ?>
        </div>
	</div>
     <div>
        <span><b><big>Salesman Notes: </big></b><?= $payment->Notes ?></span>
     </div>
</div>
 
        <div class="panel panel-default">
          <div class="panel-body">
           
		   
          </div>
        </div>
        
    </body>
  </html>