<?php 
include 'dheader.php'; 
?>
	<div class="modal fade hidden-print" id="myModal" tabindex="-1" role="dialog" aria-hidden="true"></div>
	<div class="wrapper ">
		<?php include 'leftbar.php'; ?>
		<!-- left-bar -->
		<?php include 'topbar.php'; ?>
			<!-- top-bar -->
			<div class="main-content">

<form action="" method="get">
<div class="manage_buttons hidden-print">
	<div class="row">
		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange">
                <span class="input-group-addon bg">
                  From					                       	</span>
                <input type="text" class="form-control start_date datepicker" value="<?php echo $_GET['sdate']; ?>" name="sdate" id="sdate">
            </div>
		</div>
		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange1">
                <span class="input-group-addon bg">
                    To			                                </span>
                <input type="text" class="form-control end_date datepicker" value="<?php echo $_GET['edate']; ?>" name="edate" id="edate">
            </div>	
		</div>
		<div class="col-md-2">
			<div class="input-group input-daterange" id="reportrange1">
		<button type="submit" title="Submit" class="btn btn-success">Submit</button>
			</div>
		</div>	
	</div>
</div>

</form>
	<div class="container-fluid">
		<div class="row manage-table">

	
			<div class="panel panel-piluku">
				<div class="panel-heading">
				<h3 class="panel-title">
				<?php
				if (isset($_GET['sdate'])) {
				
					$sdate = $_GET['sdate'];
					$scus_date = explode('/', $sdate);
					$sget_sdate = $scus_date[1].'/'.$scus_date[0].'/'.$scus_date[2];

					// End date
	             	$edate = $_GET['edate'];
					$ecus_date = explode('/', $edate);
					$get_edate = $ecus_date[1].'/'.$ecus_date[0].'/'.$ecus_date[2];
				}
				?>
					Reports - Sale Report	<?php echo @$sget_sdate; ?>-<?php echo @$get_edate; ?>		
					<span class="panel-options custom">
											</span>
				</h3>
			</div>
				<div class="panel-body nopadding table_holder table-responsive" >
					<table class="tablesorter table  table-hover" id="sortable_table">
						<thead>
							<tr>
								<th>Description</th>
								<th>Data</th>
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
						?>
						<?php
							$profitSR = 0;
							$profitS  = 0;
							// Start date
							$sdate = $_GET['sdate'];
							$scus_date = explode('/', $sdate);
							$get_sdate = $scus_date[2].'/'.$scus_date[0].'/'.$scus_date[1];

							// End date
			             	$edate = $_GET['edate'];
							$ecus_date = explode('/', $edate);
							$get_edate = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];
			            ?>    
			            <?php
							$i = 0;
							$invoices = 0;
							$si = 0;
							$sinvoices = 0;
							$ri = 0;
							$rinvoices = 0;
							$pi = 0;
							$pinvoices = 0;
							$grandtotal = 0;
							$totalSales = 0;
							$totalReturns = 0;
							$totalPurchases = 0;
							$totalPReturns = 0;
							$totalprofit = 0;
							$totalsaleprofit = 0;
							$totalreturnprofit = 0;
							$grandexp = 0;
							$granddeb = 0;
							$totalQtySold = 0;
							$totalQtyreturn = 0;
							$totalonlyCash = 0;
							// DISTINCT INVOICES
							$disInv = "SELECT DISTINCT invoiceNo FROM customerinvoice WHERE php_date BETWEEN '".$get_sdate."' AND '".$get_edate."'  AND payment_type != 'Opening Balance' ";
							$disInv .= "AND payment_type IN ('Credit', 'Cash') ";
							$disInv .= "AND sale_type IN ('Sale', 'Return') ";
							$disInv .= "ORDER BY invoiceNo ";
							$disInv = $db->query($disInv);

							while($fInv = $disInv->fetchArray()){ 
							$i++;
		            		$invoices = $i;
							
							
							$sql = "SELECT * FROM customerinvoice WHERE invoiceNo = '".$fInv['invoiceNo']."' AND php_date BETWEEN '".$get_sdate."' AND '".$get_edate."' AND payment_type != 'Opening Balance'";
							$sql .= "AND payment_type IN ('Credit', 'Cash') ";
							$sql .= "AND sale_type IN ('Sale', 'Return')";
		            		$run = $db->query($sql);
		            		
		            		$row = $run->fetchArray(); 
		            		$grandtotal += $row['grand_total'];
							if($row['payment_type'] == 'Cash')
							{
								$totalonlyCash += $row['received'];
							}
							if($row['sale_type'] == 'Sale'){ 
								$si++;
		            			$sinvoices = $si;
							}
							if($row['sale_type'] == 'Return'){ 
								$ri++;
		            			$rinvoices = $ri;
							}

		            		$sql2 = $db->query("SELECT * FROM customerinvoice WHERE invoiceNo = '".$fInv['invoiceNo']."' AND payment_type != 'Opening Balance'");
                            
                            $profit = 0;
		            		while($row2 = $sql2->fetchArray()){
								if($row2['sale_type'] == 'Sale')
								{
									$totalQtySold += $row2['qty'];
								}
								if($row2['sale_type'] == 'Return')
								{
									$totalQtyreturn += $row2['qty'];
								}

								$totalCost = ($row2['sell'] - $row2['purchase']) * $row2['qty'];
								if($row['entire_discount_rs'])
								{
									$profit += $totalCost - $row['entire_discount_rs'];
								}
								if($row2['discount_pr'])
								{
									$profit += ( ($row2['total']) - ($row2['purchase'] * $row2['qty']) );
								}
								if($row['discount_rs'])
								{
									$profit += $totalCost - $row2['discount_rs'];
								}
								if($row['entire_discount_rs'] == '' && $row2['discount_pr'] == '' && $row['discount_rs'] == ''){
									$profit += $totalCost;
								}
		            		}
							if($row['sale_type'] == 'Sale'){ 
								$totalprofit += round($profit,2); 
								$totalsaleprofit += round($profit,2); 
								$totalSales += round($row['grand_total'],2);
							}
							if($row['sale_type'] == 'Return'){ 
								$totalprofit -= abs(round($profit,2)); 
								$totalreturnprofit += round($profit,2); 
								$totalReturns += round($row['grand_total'],2);
							}
							if($row['sale_type'] == 'Purchase'){ 
								$totalPurchases += round($row['grand_total'],2);
							}
							if($row['sale_type'] == 'PReturn'){ 
								$totalPReturns += round($row['grand_total'],2);
							}
						}
			            ?>
			            <!-- Sales &amp; Returns -->
			            <tr>
							<td align="left"><h1>Sales &amp; Returns</h1></td>
							<td align="left">--</td>
						</tr>
			            <tr>
			            	<td>Total</td>
			            	<td><?php echo $totalSales + $totalReturns; ?></td>
			            </tr>
			            <tr>
			            	<td>Profit</td>
			            	<td><?php echo $totalprofit; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of transaction</td>
			            	<td><?php echo $invoices; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of item sold</td>
			            	<td><?php echo $totalQtySold + $totalQtyreturn; ?></td>
			            </tr>
			            <tr>
			            	<td>Cash</td>
			            	<td><?php echo $totalonlyCash; ?></td>
			            </tr>

			            <!-- Sales -->
			            <tr>
							<td align="left"><h1>Sales</h1></td>
							<td align="left">--</td>
						</tr>
			            <tr>
			            	<td>Total</td>
			            	<td><?php echo $totalSales; ?></td>
			            </tr>
			            <tr>
			            	<td>Profit</td>
			            	<td><?php echo $totalsaleprofit; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of transaction</td>
			            	<td><?php echo $sinvoices; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of item sold</td>
			            	<td><?php echo $totalQtySold; ?></td>
			            </tr>
			            <!-- <tr>
			            	<td>Cash</td>
			            	<td><?php //echo ($row2['CASHS']) ? $row['CASHSR'] - abs($row['REMAN']) : 0; ?></td>
			            </tr> -->

			            <!-- Return -->
			            <tr>
							<td align="left"><h1>Return</h1></td>
							<td align="left">--</td>
						</tr>
			            <tr>
			            	<td>Total</td>
			            	<td><?php echo $totalReturns; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of transaction</td>
			            	<td><?php echo $rinvoices; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of item return</td>
			            	<td><?php echo $totalQtyreturn; ?></td>
			            </tr>
			            <!-- <tr>
			            	<td>Cash</td>
			            	<td><?php //echo ($row3['CASHR']) ? $row3['CASHR'] : 0; ?></td>
			            </tr> -->

			            <!-- Purchases -->
						<?php
						$pi = 0;
						$pinvoices = 0;
						$pri = 0;
						$prinvoices = 0;
						
						$grandtotal = 0;
						$totalPurchases = 0;
						$totalPReturns = 0;
						$totalQtyreturn = 0;
						$totalonlyCash = 0;
						$totalQtypurchase = 0;
						$totalPReturns = 0;
						// DISTINCT INVOICES
						$disInv = "SELECT DISTINCT invoiceNo FROM purchases WHERE php_date BETWEEN '".$get_sdate."' AND '".$get_edate."'  AND payment_type != 'Opening Balance' ";
						$disInv .= "AND payment_type IN ('Credit', 'Cash') ";
						$disInv .= "AND sale_type IN ('Purchase', 'PReturn') ";
						$disInv .= "ORDER BY invoiceNo ";
						$disInv = $db->query($disInv);

						while($fInv = $disInv->fetchArray()){ 
						
						
						$sql = "SELECT * FROM purchases WHERE invoiceNo = '".$fInv['invoiceNo']."' AND php_date BETWEEN '".$get_sdate."' AND '".$get_edate."' AND payment_type != 'Opening Balance'";
						$sql .= "AND payment_type IN ('Credit', 'Cash') ";
						$sql .= "AND sale_type IN ('Purchase', 'PReturn')";
						$run = $db->query($sql);
						
						$row = $run->fetchArray(); 
						$grandtotalpurchase += $row['grand_total'];
						if($row['payment_type'] == 'Cash')
						{
							$totalonlyCash += $row['received'];
						}
						if($row['sale_type'] == 'Purchase'){ 
							$pi++;
							$pinvoices = $pi;
						}
						if($row['sale_type'] == 'PReturn'){ 
							$pri++;
							$prinvoices = $pri;
						}

						$sql2 = $db->query("SELECT * FROM purchases WHERE invoiceNo = '".$fInv['invoiceNo']."' AND payment_type != 'Opening Balance'");
						
						$profit = 0;
						while($row2 = $sql2->fetchArray()){
							if($row2['sale_type'] == 'Purchase')
							{
								$totalQtypurchase += $row2['qty'];
							}
							if($row2['sale_type'] == 'PReturn')
							{
								$totalQtypreturn += $row2['qty'];
							}
						}
						if($row['sale_type'] == 'Purchase'){ 
							$totalPurchases += round($row['grand_total'],2);
						}
						if($row['sale_type'] == 'PReturn'){ 
							$totalPReturns += round($row['grand_total'],2);
						}
					}
						?>
			            <tr>
							<td align="left"><h1>Purchases</h1></td>
							<td align="left">--</td>
						</tr>
			            <tr>
			            	<td>Total</td>
			            	<td><?php echo $totalPurchases; ?></td>
			            </tr>
			            <tr>
			            	<td>Number of transaction</td>
			            	<td><?php echo $prinvoices; ?></td>
			            </tr>
			            <tr>
			            	<td>Item Recieved</td>
			            	<td><?php echo $totalQtypurchase; ?></td>
			            </tr>

			            <!-- Expenses -->
			            <tr>
							<td align="left"><h1>Expenses</h1></td>
							<td align="left">--</td>
						</tr>
						<?php
							$sql = "SELECT * FROM expenses WHERE php_date BETWEEN '".$get_sdate."' AND '".$get_edate."' ORDER BY php_date, id ";   
							$run = $db->query($sql);
							while($row = $run->fetchArray()){
						   		$grandexp += $row['payment'];
				   			}
						?>
			            <tr>
			            	<td>Total Expenses</td>
			            	<td><?php echo $grandexp; ?></td>
			            </tr>
						<?php } // Date Set ?>		
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

			
<?php include 'dfooter.php'; ?>