<?php include 'inc/conx.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-3{
				width:25%;
				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:12px;
				/*font-size:11px;*/
		}
		#itemsNames tbody td {
				border:1px dotted #000;
				font-size:12px;
		}
		#itemsNames{
			margin-left:auto !important;
			margin-right:auto !important;
			position: relative;

		}

		#simple_table 
		{
			position: relative;
			margin-left:auto !important;
			margin-right:auto !important;
			margin-top: 20px;
		}

	</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">
		<p class="text-center" style="font-size: 10px;">Cash Memo / Bill</p>
	<h3 class="text-center"><?php echo $fdata['name']; ?></h3>
	<p class="text-center" style="font-size: 10px;"> <?php echo $fdata['address']; ?> <br><?php echo $fdata['city']; ?> - <?php echo $fdata['phone']; ?></p>
	<!-- <p class="text-center"> & </p>
	<p class="text-center"> Book Bank</p> -->
		</div>

		<table width="95%" id="simple_table" cellpadding="0" cellspacing="0">
			<tr>
				<td colspan="2">
				<div class="wrapper" style="position: relative; border: 2px solid #eee;">
					<div style="
    position: absolute;
    border: 2px solid #000;
    top: -20px;
    padding: 10px;
    font-weight: bold;
    box-shadow: 3px 3px;
	background-color: #fff;
	left: -3px;
">
						Sales Invoice
					</div>
				</div>
				</td>
			</tr>
		</table>
		<table width="95%" id="simple_table" style="top: 30px;" cellpadding="0" cellspacing="0">
			
			<tr>
				<td><strong>Estimate#</strong> <?php echo $_GET['saleid'];?></td>
				<td><strong>DATE:</strong> <?php echo $fDatarow['show_date']; ?></td>
			</tr>
			<tr>
				<td><strong>To,</strong> <ins><?php echo $fDatarow['name_word']; ?></ins></td>
				<td><strong>User:</strong> <?php echo $fDatarow['user_name']; ?></td>
			</tr>
		</table>
		<!-- <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" style="top: 50px;" cellpadding="0" cellspacing="0">
        <thead>
        	<th align="left">Item Name</th>
           	<th align="left">Qty.</th>
            <th align="left">Rate</th>
            <!-- <th align="left" style="display: none;">Dis%</th> -->
            <!-- <th align="left" style="display: none;">Dis-rs</th> -->
			<!-- <th class="item_name_heading">Description</th> -->
			<!-- <th class="item_name_heading">SR #</th> -->
			<!-- <th class="item_name_heading">Warranty</th> -->
            <th align="left">Amount</th>
        </thead>
        <tbody>
     <?php 
			    	$select = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$_GET['saleid']."'");
			    	$qty = 0 ;
			    	$total = 0;
			    	$tdis = 0;
			    	$tdisp =0;
			    	$rt = 0;
			    	while($row = $select->fetchArray()){
			    		$qty += $row['qty'];
			    		$total += $row['total'];
			    		$rt += $row['sell'] * $row['qty'];
			    		$tdis += ($row['discount_rs']) ? $row['discount_rs'] : 0;
			    		$tdisp += ($row['discount_pr']) ? $row['discount_pr'] : 0; 
	?>
	
    	<tr>
            <td align="center"><?php echo $row['item_name']; ?></td>
           <td align="center" style="border-left: none;"><?php echo $row['qty']; ?></td>
            <td align="center" style="border-left: none;"><?php echo $row['sell']; ?></td>
            <!-- <td align="center" style="border-left: none; display: none;"><?php echo ($row['discount_pr']) ? $row['discount_pr']."%" : 0; ?></td> -->
            <!-- <td align="center" style="border-left: none display: none;"><?php echo ($row['discount_rs']) ? $row['discount_rs'] : 0; ?></td> -->
            <!-- <td align="center" style="border-left: none;"><?php echo $row['scheme']; ?></td> -->
            <!-- <td align="center" style="border-left: none;"><?php echo $row['sr']; ?></td> -->
            <!-- <td align="center" style="border-left: none;"><?php echo $row['totalwarranty']; ?></td> -->
            <td align="center" style="border-left: none;"><?php echo $row['total']; ?></td>
        </tr>
 <?php } ?>
      </tbody>
</table>

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

	}
</style>
<?php 
	$select = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$_GET['saleid']."'");
	$runtotal = $select->fetchArray();
?>
<table align="right" class="totalCal" style="position: relative; top: 50px;">
	<tr>
		<th>Total Qty</th>
		<td><?php echo $qty; ?></td>
	</tr>
	<tr style="font-size:11px">
		<th>Invoice Amount</th>
		<td><?php echo $rt; ?></td>
	</tr>
	
	<?php if($runtotal['transport_expense']){ ?>
		<tr>
			<th>Sales Tax</th>
			<td><?php echo ($runtotal['transport_expense']) ? $runtotal['transport_expense'] : 0; ?></td>
		</tr>
	<?php } ?>

	<?php if($tdis){ ?>
	<tr>
		<th>Discount RS</th>
		<td><?php echo ($tdis) ? $tdis : 0; ?></td>
	</tr>
	<?php } ?>


	<?php if($runtotal['entire_discount_pr'] > 0){ ?>
	<tr>
		<th>Discount</th>
		<td><?php echo ($runtotal['entire_discount_pr']) ? $runtotal['entire_discount_pr'] : 0; ?>%</td>
	</tr>
	<?php } ?>

	<?php if($runtotal['entire_discount_rs']){ ?>
		<tr>
			<th>Discount All Invoice</th>
			<td><?php echo ($runtotal['entire_discount_rs']) ? $runtotal['entire_discount_rs'] : 0; ?></td>
		</tr>
	<?php } ?>

	<tr style="font-size:11px">	
		<th>Total Amount</th>
		<td><?php echo $runtotal['grand_total']; ?></td>
	</tr>
	
	<tr>
		<th>Received</th>
		<td><?php echo ($runtotal['received']) ? $runtotal['received'] : 0; ?></td>
	</tr>
	<tr style="font-size:11px">	
		<th>Balance</th>
		<td><?php echo $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:11px"><?php echo $preBal - $runtotal['remaining']; ?></td>
	</tr> -->
	<!-- <tr style="font-size:11px">
		<th>Grand Total</th>
		<td><?php echo $preBal; ?></td>
	</tr> -->
	<?php //}else{} ?>	
	</table>
	<?php if($runtotal['description'])
	{ ?>

<div class="policy" style="position: relative; top: 55px; overflow:hidden; width:100%; font-size: 10px; left: 30px;">
	<strong><ins>Description:</ins></strong> <?php echo $runtotal['description']; ?>
</div>
<?php
	}
?>
<?php 
	$addressquery = $db->query("select * from store");
	$runaddressquery = $addressquery->fetchArray();
?>
<div class="policy" style="position: relative; top: 55px; overflow:hidden; width:100%; font-size: 10px; left: 30px;">
	<?php echo $runaddressquery['policy_ins']; ?>
</div>

<div class="shopdesc" style="width:100%; position: relative; top: 55px; text-align:center ; font-size: 11px; left: 30px;">
	<p>Software Developed By WESKIA - 03153152923 - 03355552923</p>
</div>


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