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

$isAdmin = isset($_SESSION['isAdmin'])?$_SESSION['isAdmin']:false;
$payment = new Payment($_REQUEST['id']);
$payment->getReminderData();
if($payment->isReminder){
	header("Location: mod.php?p=payment&a=viewReminder&id=" . $_REQUEST['id'] . "&key=" . $_REQUEST['key']);
}
if (!$isAdmin) {
	if(isset($_REQUEST['key'])){
		if($payment->getTokenKey() != $_REQUEST['key']){
			echo $payment->getTokenKey();
		//	header("Location: index.php");
		}
	}else{
		header("Location: index.php");
	}

}




 ?>
 <!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(){
                //$('.combobox').bscombobox();
				//window.print();
				/*
				setTimeout(function(){
					if(confirm('Close?')){
						window.close();
					}else{

					}

				}, 1000);
				*/
              });
            </script>

      <![endif]-->
 	<link rel="stylesheet" href="<?= $root ?>css/styles.css"/>
    </head>
    <body>
    <style>
		@media print {
			body {
				font-size: 12pt;
			}
		}
	</style>
    <!-- Stack the columns on mobile by making one full-width and the other half-width -->
 <div class="container-fluid" style="margin-top: -50px;">

	<h2><img style="width: 175px;" src="/img/Logo_250.png"></h2>
	<div style="font-size: 10px; margin-left: 10px;">
	<?= $config["PostalAddess1"] ?> &#x25cf; <?= ($config["PostalAddess2"]!=""?($config["PostalAddess2"] . "&#x25cf;"):"") ?> <?= $config["PostalCity"] ?>,<?= $config["PostalState"] . " " . $config["PostalZip"] ?><br>
	<?= $config["CompanyPhone"] ?><br>
		<?= $config["OrderEmail"] ?>

	</div>
 	<div class="row">
 	  <div class="col-xs-5">

 	  	<p style="">
			<div style=""><b>Customer</b><br></div>
 	  		<div style=" font-size: 12px;">
				<b><?= $payment->Customer->AccountNumber; ?></b><br>
				<b><?= $payment->Customer->Name; ?></b><br>
				<?= $payment->Customer->Address1?$payment->Customer->Address1 . "":"" ?>
				<?= $payment->Customer->Address2?$payment->Customer->Address2 . "<br>":"<br>" ?>
				<?= $payment->Customer->City . "," . $payment->Customer->State . " " . $payment->Customer->ZipCode ?><br>

			</div>
 	  </p>

 	  </div>
  	  <div class="col-xs-5">
		<p style="">

 	  		<div style="">



			</div>
		</p>

	  </div>
 	  <div class="col-xs-2" style="text-align: center; float: right; position:  absolute; margin-top: -110px;width: 200px; right: 20px; ">
 		  <h2>
		  <?php

				echo "Payment";

		  ?>
		  <br>
		  <b><?= $payment->PaymentID ?></b>
		  </h2>
 		  <p style="font-size: 12px;">

 		  <b>Date: </b><?= date("m/d/Y",strtotime($payment->PaymentDate)) ?><br>
		  <b>Time: </b><?= date("g:i a",strtotime($payment->PaymentDate)) ?><br>
		  <b>Salesperson:</b> <?= $payment->Salesman->Name; ?>
 		  </p>
 	  </div>

 	  <table  class="table table-condensed" style="font-size: 12pt;">
 	    <thead>
 	      <tr>

 	        <th style="width: 10%">InvoiceID</th>
			<th style="width: 10%">Inv. Date</th>
			<th style="width: 10%">Type</th>
			<th style="width: 5%" class="centered">Subtotal</th>
			<th style="width: 5%" class="centered">Tax</th>
			<th style="width: 5%" class="centered">Total</th>
			<th style="width: 5%" class="centered">Balance</th>
			<?php
				if($payment->TotalDiscount){
			?>
			<th style="width: 5%" class="centered">Discount</th>
			<?php
				}
			?>
 	        <th style="width: 5%; text-align: right;">Payment</th>

 	      </tr>
 	    </thead>
 	  	<tbody>
 	  	<?php

 	  		$index = 0;
			$totalItems = 0;

			foreach($payment->Invoices as $key => $invoice){
				$hasPartialPayment = false;
 	  	?>
 	  		<tr>

 	        		<td style="text-overflow: ellipsis;" >
 	        			<span class="itemInfo"><b><?= $invoice->InvoiceID ?>
						<?php
						$hasPartialPayment = $hasPartialPayment || round($invoice->Subtotal+$invoice->TotalTax-$invoice->PaymentAmount-$invoice->Discount,2)!=0;
						 if($hasPartialPayment){
							echo "*";
						}
						?></b></span>
					</td>
					<td>
 	        			<span class="itemInfo"><?= date("m/d/Y" , strtotime($invoice->InvoiceDate) ) ?>

						</span>

					</td>
					<td>
						<?= ($invoice->InvoiceSubtotal < 0)?"Credit":"Invoice" ?>
					</td>
					<td class="centered">
						<?= moneyFormat($invoice->InvoiceSubtotal) ?>
					</td>
					<td class="centered">
						<?= moneyFormat($invoice->TotalTax) ?>
					</td>
					<td class="centered">
						<?= moneyFormat($invoice->InvoiceSubtotal+$invoice->TotalTax) ?>
					</td>
					<td class="centered">
						<?= moneyFormat($invoice->Subtotal+$invoice->TotalTax) ?>
					</td>
					<?php
						if($payment->TotalDiscount){
					?>
					<td class="centered">
						<?= moneyFormat($invoice->Discount) ?>
					</td>
					<?php
						}
					?>
					<td style="text-align: right;">
						<?= moneyFormat($invoice->PaymentAmount) ?>
					</td>
 	      	</tr>
 	      	<?php
 	      	$index++;
			$colspan =5;
			//if customer has discount colspan += 1 to add discount column
				if($payment->TotalDiscount){
					$colspan ++;
				}
 	      	}?>
 	      	<tr>
				<td colspan="2"><?php

				if($hasPartialPayment){
					echo "<i>*Partial Payment</i></td>";
				}
				?>
 	      		<td colspan="<?= $colspan ?>" style="text-align: right;">
					<span style="font-weight: bold; text-align: right;">
						Subtotal<br>
						<?= ($payment->TotalDiscount)?"Discount<br>":"" ?>
						<?= getSetting("Tax1Name")['value'] ?><br>
						<?= (getSetting("Tax2Name")['value']) ?><br>
						Total

					</span>
				</td>
				<td style="text-align: right;">
					<?= moneyFormat($payment->InvoiceSubtotal); ?><br>
					<?= ($payment->TotalDiscount)?moneyFormat($payment->TotalDiscount) . "<br>":"" ?>

					<?= moneyFormat($payment->TotalTax1); ?><br>
					<?= moneyFormat($payment->TotalTax2); ?><br>
					<?= moneyFormat($payment->TotalToPay); ?>
				</td>
 	      	</tr>
 	    </tbody>
 	  </table>



	  <?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 class="row" style="font-size: 10pt;">
		<div class="col-xs-8 col-md-8">
            <div class="col-xs-12">
                <div style="font-weight: bold;">Payment Information</div>
                <div class="col-xs-6">
                    <table class="table table-condensed">
                      <tbody>
                          <tr>
                            <td>
								<span class="paymentSummaryHeader">Invoice Subtotal</span><br>
								<?php
									if($payment->TotalDiscount){
								?>
								<span class="paymentSummaryHeader">Total Discount</span><br>
								<?php
									}
								?>
								<span class="paymentSummaryHeader">Total Tax</span><br>
								<span class="paymentSummaryHeader">Total Paid</span><br>
								<span class="paymentSummaryHeader">Total To Apply</span><br>
							</td>
                            <td>
								<?= moneyFormat($payment->InvoiceSubtotal) ?><br>
								<?php
									if($payment->TotalDiscount){
								?>
								<?= moneyFormat($payment->TotalDiscount) ?><br>
								<?php
									}
								?>
								<?= moneyFormat($payment->TotalTax) ?><br>
								<?= moneyFormat($payment->TotalToPay + $totalCredit) ?><br>
								<?= moneyFormat($payment->TotalToApply) ?>
							</td>
                          </tr>

                      </tbody>
                    </table>
                </div>
                <div class="col-xs-6">
                    <table class="table table-condensed">
                      <tbody>
                        <tr>
                            <td>
								<span class="paymentSummaryHeader">Credits Subtotal</span><br>
								<span class="paymentSummaryHeader">Cash Amount</span><br>
								<span class="paymentSummaryHeader">Check Amount</span><br>
								<span class="paymentSummaryHeader">Total Amount</span><br>

							</td>
                            <td>
								<?= moneyFormat($payment->TotalCredits) ?><br>
								<?= moneyFormat($payment->CashAmount) ?><br>
								<?= moneyFormat($payment->CheckAmount) ?><br>
								<?= moneyFormat($payment->CheckAmount+$payment->CashAmount) ?>
							</td>
                          </tr>

                      </tbody>
                    </table>
                </div>
            </div>
    <?php
        if(count($payment->Checks) > 0){
	?>
            <div class="col-xs-4">
                <div style="font-weight: bold;">Check Reference</div>
                <div class="col-xs-12">
                    <table class="table table-condensed">

                      <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>
 <style>
	.borderless {
		border: hidden;
		border-top: hidden;
		border-left-style: none;
		border-right-style: none;
		border-bottom-style: none;
		line-height: 1;
	}
	.borderless>td {
		line-height: 1;
	}

	.borderless td span {
		line-height: 1;
	}
 </style>
 <style type="text/css" media="print">
	body { margin: 0;  font-size: 2em }


 </style>
 </div>
        <div style="border: 1px solid black; font-size: 12px;" class="panel panel-default">
          <div class="panel-body">
            <span><b>Payment Notes:</b></span><br />
            <?= $payment->Notes ?>



          </div>
        </div>
				<br>
				<center>
					<strong>¡Gracias por su patrocinio!</strong>
				</center>



    </body>
  </html>
