<?php include 'inc/conx.php'; 
setlocale(LC_MONETARY, 'en_US');
?>
<?php include 'numbertoword.php'; ?>
<!DOCTYPE html>
	<html lang="en">
	<head>
		<meta charset="UTF-8">
		<title>
		</title>
	<style>
		.text-center{
			text-align:center;
		}
		.text-left{
			text-align:left;
		}
		.text-right{
			text-align:right;
		}
		*{
			margin:0px !important;
		}
		.col-md-2{
				width:16.66666666666667%;
				float: left;
		}
		.col-md-3{
				width:25%;
				float: left;
		}
		.col-md-12{
				width:100%;
				float: left;
		}
		.invoice_desc{
			/*margin-top:20px !important;*/
			text-align:center;
			overflow: hidden;
		}
		/*table{
			width: 95%;
			margin-right:auto !important;
			margin-left:auto !important;
			margin-top: 30px !important; 
			
		}
		table th, td{
			text-align:left;
		}
		thead th{
			border:1.5px solid;
			padding: 0px !important;
		}
		.item_table table thead td{
			text-align:left;
			border-bottom: 2px solid;	
			border-right:2px solid;
			padding: 0px;
		}
		tbody th, td{
			text-align:left;
			border-bottom : 1px dotted;
		}*/
		#itemsNames thead th {
				border:1px solid #000;
				border-bottom:3px solid #000;
				/*font-size:11px;*/
		}
		#itemsNames tbody td {
				border:1px dotted #000;
				font-size:14px;
		}
		#itemsNames{
			margin-left:auto !important;
			margin-right:auto !important;
		}

		#tothecus {
			font-size: 20px; 
			font-weight: bold; 
			text-align:left;
			position: relative;
			left: 32px;
		}

	</style>
	</head>
	<body>
	 <?php
			    	$store = $db->query("SELECT * FROM store");
			    	$fdata = $store->fetchArray();
	?>
	<?php 
			$allData = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$_GET['saleid']."'");
			$fDatarow = $allData->fetchArray();
	?>


		<div class="wrapper">
		<div class="text-center"><img src="images/nce_logo.jpg" width="100" height="100"></div>
	
		<div class="heading text-center">
		<span style="color: #4CAF50;text-shadow: 1px 1px 2px #000; text-transform:uppercase; font-weight:bold; font-size:18px; text-decoration:underline; border-bottom:1px solid #000; text-align:center;"><?php echo $fdata['name'];?></span>
		</div>
		</div>
		<?php 
              	$remove = preg_replace('/[^0-9]/', "", strtolower($fDatarow['name']));
                $acp = $db->query("SELECT * FROM customer_setup WHERE id = '".$remove."'");
              	$facp = $acp->fetchArray();
  			     ?>
		<div class="invoice_desc">
			<?php 
				if($fDatarow['sale_type'] == "Sale") { $st_code = "SL"; }
				if($fDatarow['sale_type'] == "Return") { $st_code = "PR"; } 
			?>
			<div class="col-md-2">Estimate # <?php echo $st_code; ?>-<?php echo $_GET['saleid'];?></div>
			<div class="col-md-2">TYPE : <?php echo $fDatarow['payment_type']; ?></div>
			<div class="col-md-3">DATE : <?php echo $fDatarow['show_date']; ?></div>
			<div class="col-md-3">TIME : <?php echo date("h:i:s"); ?></div>
			<div class="col-md-2">City :<?php echo ($facp['city']) ? $facp['city'] : $fDatarow['city']; ?></div>
		</div>
		<div class="invoice_desc">
			<div class="col-md-12" id="tothecus">To : <?php echo $fDatarow['name_word']; ?></div>
			
<!--               Address : <?php echo $facp['address']; ?> <br>
              City :  <br>
              Phone : <?php echo $facp['phone']; ?> -->
			<!-- <div class="col-md-3">City :<?php echo ($facp['city']) ? $facp['city'] : $fDatarow['city']; ?></div> -->
			<!-- <div class="col-md-3">User : <?php echo $fDatarow['user_name']; ?></div> -->
		</div>

		<!-- <div class="item_table">
			<table>
				<thead>
					<tr>
						<th>Item Name</th>
						<th>Qty</th>
						<th>Rate</th>
						<th>Dis</th>
						<th>Amount</th>
					</tr>
				</thead>
				<tbody>
					<tr>
						<td>Ppc</td>
						<td>5</td>
						<td>290</td>
						<td>0</td>
						<td>1450</td>
					</tr>
					<tr>
						<td>Ppc</td>
						<td>5</td>
						<td>290</td>
						<td>0</td>
						<td>1450</td>
					</tr>
				</tbody>
			</table>
		</div> -->
		
		<table width="95%" id="itemsNames" cellpadding="0" cellspacing="0">
        <thead>
        	<th align="left">SR.no</th>
        	<th align="center">Publisher</th>
        	<th align="center">Item Name</th>
           	<th align="center">Qty.</th>
            <th align="center">Rate</th>
			<th align="center">Gross Amount</th>
            <th align="right">Discount <?php echo $fDatarow['discount_pr']."%"; ?></th>
            <th align="right">Amount</th>
        </thead>
        <tbody>
     <?php 
			    	$select = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$_GET['saleid']."'");
			    	$qty = 0 ;
			    	$total = 0;
			    	$i = 1;
			    	while($row = $select->fetchArray()){
			    		$qty += $row['qty'];
			    		$total += $row['total'];

			    	$catshow = $db->query("SELECT * FROM itemsetup INNER JOIN categories WHERE item_name = '".$row['item_name']."'");
			    	$fetchcat = $catshow->fetchArray();
			    	//var_dump($fetchcat);
	?>	
	
    	<tr>
			<td align="left"><?php echo $i; ?></td>
			<td align="center"><?php //echo $fetchcat['name']; ?>NCE</td>
            <td align="center"><?php echo $row['item_name']; ?></td>
           	<td align="center"><?php echo $row['qty']; ?><input type="checkbox" style="float:right;"></td>
            <td align="center"><?php echo $row['sell']; ?></td>
            <td align="center"><?php echo $row['shtotal']; ?></td>
            <td align="right"><?php echo ($row['discount_rs']) ? $row['discount_rs'] : 0; ?></td>
            <td align="right"><?php echo round($row['total'], 2); ?></td>
        </tr>
 <?php $i++; } ?>
 <?php 
	$select = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$_GET['saleid']."'");
	$runtotal = $select->fetchArray();
?>
 		<tr>
			
            <td align="center" style="border:none;">&nbsp;</td>
            <td align="center" style="border:none;">&nbsp;</td>
            <td align="center" style="border:none;">&nbsp;</td>
            <td align="center">Total Qty: <?php echo $qty; ?></td>
            <td align="center" style="border:none;">&nbsp;</td>
            <td align="center" style="border:none;">&nbsp;</td>
        </tr>
      </tbody>
</table>

<style>
	.totalCal {
			table-layout: fixed;
			font-size: 10px;
			margin-right:31px !important;
		}
	.totalCal td{
		text-align: right;
		border-bottom:1px solid;

	}
</style>
<table align="right" class="totalCal">
	
	<?php if($fDatarow['bilty']){ ?>
	<tr style="font-size:15px; position: absolute; left: 30px; top: 295px;">
		<th>Bilty : </th>
		<td><?php echo $fDatarow['bilty']; ?></td>
	</tr>
	<?php } ?>
	

	<tr style="font-size:15px">
		<th>Invoice Amount</th>
		<td><?php echo round($total,2); ?></td>
	</tr>
	
	<tr>
		<th>Transport</th>
		<td><?php echo ($runtotal['transport_expense']) ? $runtotal['transport_expense'] : 0; ?></td>
	</tr>
	<!-- <tr>
		<th>Discount</th>
		<td><?php echo ($runtotal['entire_discount_rs']) ? $runtotal['entire_discount_rs'] : 0; ?></td>
	</tr> -->
	<tr>
		<th>Recieve</th>
		<td><?php echo $runtotal['received']; ?></td>
	</tr>
	<tr style="font-size:15px">	
		<th>Remaining</th>
		<td><?php echo round($runtotal['remaining']); ?></td>
	</tr>
	<?php 
	$ID = 0;
	$countQuery = $db->query("SELECT count(*) totalCount FROM customer_setup WHERE customer_name = '".$runtotal['name_word']."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
	if ($totalCount < 1) {
	
	$countQuery = $db->query("SELECT count(*) totalCount FROM supplier_setup WHERE supplier_name = '".$runtotal['name_word']."'");
	$rowcount = $countQuery->fetchArray();
	$totalCount = $rowcount['totalCount'];
		if ($totalCount < 1) {
			$ID = 0;
	 	}else{
	 		$supplier = $db->query("SELECT * FROM supplier_setup WHERE supplier_name = '".$runtotal['name_word']."'");
	 		$fetchdate = $supplier->fetchArray();
	 		$ID = "s".$fetchdate['id'];
	 	}
	 }else{
	 	$customer = $db->query("SELECT * FROM customer_setup WHERE customer_name = '".$runtotal['name_word']."'");
 		$fetchdate = $customer->fetchArray();
 		$ID = "c".$fetchdate['id'];
	 }
	 
	    if($ID){
	?>
	 <?php
							$date = "2015/12/30";
							
			                // End date
							$sedate = date("m/d/Y");
							$secus_date = explode('/', $sedate);
							$seget_sdate = $secus_date[2].'/'.$secus_date[0].'/'.$secus_date[1];
			                $end_date = date ("Y/m/d", strtotime("-0 day", strtotime($seget_sdate)));
							
							$preBal = 0;
							while (strtotime($date) <= strtotime($end_date)) {
							//echo "$date\n";
							
        
							$open = "SELECT * FROM customerinvoice ";
							$open .= "WHERE name = '".$fDatarow['name']."' AND  payment_type = 'Opening Balance' AND php_date = '".$date."'";
							$open_run = $db->query($open);
							while($open_row = $open_run->fetchArray()){
								$preBal += $open_row['remaining'];
							}


							$sql = "SELECT DISTINCT invoiceNo FROM customerinvoice ";
							$sql .= "WHERE name = '".$fDatarow['name']."' ";
							//$sql .= "AND (date BETWEEN '".$_GET['sdate']."' AND '".$_GET['edate']."') ";
							$sql .= "AND php_date = '".$date."' AND payment_type != 'Opening Balance' ";
							$sql .= "ORDER BY date, id";
							
							$run = $db->query($sql);
							while($row = $run->fetchArray()){
								$rem = "SELECT * FROM customerinvoice ";
								$rem .= "WHERE invoiceNo = '".$row['invoiceNo']."' ";
								$rem_run = $db->query($rem);
								$rem_row = $rem_run->fetchArray();

								if ($rem_row['sale_type'] == 'Sale') {
									$preBal += $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Purchase') {
									$preBal -= $rem_row['remaining'];
								}elseif ($rem_row['sale_type'] == 'Return') {
									$preBal -= $rem_row['remaining'];
								}
							}
							
							
							$sql2 = "SELECT * FROM payments ";
				            $sql2 .= "WHERE cs_id = '".$fDatarow['name']."' AND action = '1' ";
				            $sql2 .= "AND php_date = '".$date."' ";
				            $sql2 .= "ORDER BY php_date, id";
				            $run2 = $db->query($sql2);
				            while($row2 = $run2->fetchArray()){
				            	if ($row2['debcre'] == 1) {
									$preBal -= $row2['amount'];
								}elseif ($row2['debcre'] == 2) {
									$preBal += $row2['amount'];
								}
							}
							
							$date = date ("Y/m/d", strtotime("+1 day", strtotime($date)));

						}
					
							//$preBal -= $total;
					
						?>
	<tr>	
		<th>Previous Amount</th>
		<td style="font-size:15px"><?php echo $preBal - $runtotal['remaining']; ?></td>
	</tr>
	<tr style="font-size:15px">
		<th>Grand Total</th>
		<?php if($preBal < 0){ $crdr = "CR"; }else{ $crdr = "DR"; } ?>
		<td><?php echo $preBal . $crdr; ?></td>
	</tr>
	<?php }else{} ?>
	
	</table>
	<div style="font-size:15px; position: relative; left: 30px; text-decoration: underline; font-weight: bold;" align="left">
		<span>Amount : </span>
		<?php if(@$preBal){ ?>
				<span><?php echo ucwords(number_to_words(@$preBal)); ?> Only</span>
		<?php }else{ ?>
			<span><?php echo ucwords(number_to_words(@$runtotal['remaining'])); ?> Only</span>
		<?php } ?>
		
	</div>	
<?php 
$query = $db->query("Select * from policy"); 
$runquery = $query->fetchArray();
?>
<div class="policy" style="overflow:hidden; width:100%; position: relative; left: 30px;">
<table>
	<tr style="font-size:15px; ">
		<th>Company Signature : </th>
		<td>____________________</td>
		<th>Client Signature : </th>
		<td>____________________</td>
	</tr>
</table>	
	<ul style="list-style:none;">
		<li><p><?php echo $runquery['policy']; ?></p></li>
	</ul>
</div>
<?php 
	$addressquery = $db->query("select * from store");
	$runaddressquery = $addressquery->fetchArray();
?>
<div class="shopdesc" style="width:100%; text-align:center ; margin-top:25px !important;">
	<p> <?php echo $runaddressquery['address']; ?>, <?php echo $runaddressquery['city']; ?> PH : <?php echo $runaddressquery['phone']; ?></p>
	<p>Software Developed By WESKIA - 03153152923</p>
</div>


	</body>
	</html>	
<script>
    window.print();
</script>