<?php 
include 'dheader.php'; 
if (isset($_GET["delete"])) {
 		$delete = $db->query("DELETE FROM other_accounts WHERE id = '".$_GET["delete"]."'");
}
?>
	<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="reportrange2">
				
                <span class="input-group-addon bg">Item</span>
                <select name="item" class=" form-control" id="search-select">
                <option value="<?php echo $_GET["uei"]; ?>"><?php echo $_GET["item"]; ?></option>
				  <option value="All">All</option>
				  <?php
					$listNames = $db->query("SELECT * FROM itemsetup");
					while($rowNames = $listNames->fetchArray()){
				?>
					<option value="<?php echo $rowNames['item_name']; ?>"><?php echo $rowNames['item_name']; ?></option>
				<?php } ?>
				</select>
            </div>
		</div>

		<div class="col-md-3">
			<div class="input-group input-daterange" id="reportrange2">
				
                <span class="input-group-addon bg">By</span>
                <select name="group" class=" form-control" id="search-select">
                  <option value="2" <?php echo ($_GET['group'] == 2) ? 'selected':''; ?>>Group</option>
				  <option value="1" <?php echo ($_GET['group'] == 1) ? 'selected':''; ?>>Individual</option>
				</select>
            </div>
		</div>
		
		<div class="col-md-2">
			<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-2">
			<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 - Stock 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>Date</th>
								<th>Item</th>
								<th>Particulars</th>
								<th>QTY</th>
								<th>Total</th>
							</tr>
						</thead>
						<tbody>
						<?php 
						if (isset($_GET['sdate'])) {
							if($_GET['group'] == 1){
						?>
						<?php
							// Start date
							$date = "2016/12/30";
							
			                // End date
							$sedate = $_GET['sdate'];
							$secus_date = explode('/', $sedate);
							$seget_sdate = $secus_date[2].'/'.$secus_date[0].'/'.$secus_date[1];
			                $end_date = date ("Y/m/d", strtotime("-2 day", strtotime($seget_sdate)));
							
							$i = 0;
							$invoices = 0;
							$payments = 0;
							$receive = 0;
							$grandtotal = 0;
							$supertotal = 0;

							while (strtotime($date) <= strtotime($end_date)) {
							//echo "$date\n";
							$date = date ("Y/m/d", strtotime("+1 day", strtotime($date)));

							if($_GET['item'] == 'All'){
								// Run All Queries
								$var = '';
							}
							else{
								$var = "WHERE item_name = '".$_GET['item']."'";
							}

								$other_title = $db->query("SELECT * FROM itemsetup $var");
								while ($fother_title = $other_title->fetchArray()) {

								$grandtotal = $fother_title['open_qty'];
								$sql = "SELECT * FROM customerinvoice WHERE item_name = '".$fother_title['item_name']."' AND php_date = '$date' ";
								$run = $db->query($sql);

		            			
		            			while($row = $run->fetchArray()){
		            				if ($row['sale_type'] == 'Sale') {
		            					$grandtotal -= $row['qty']*$row['kg'];
		            				}elseif ($row['sale_type'] == 'Purchase') {
		            					$grandtotal += $row['qty']*$row['kg'];
		            				}elseif ($row['sale_type'] == 'Misc') {
		            					($row['qty'] < 0) ? $grandtotal += ($row['qty']*$row['kg']) : $grandtotal -= ($row['qty']*$row['kg']);
		            				}elseif ($row['sale_type'] == 'Return') {
		            					$cus_ch = preg_replace('/[^a-z]/', "", strtolower($row['supplierName']));
		            					if($cus_ch == 'c'){
		            						$grandtotal += $row['qty']*$row['kg'];
		            					}elseif($cus_ch == 's'){ 
		            						$grandtotal -= $row['qty']*$row['kg'];
		            					}else{
		            						$grandtotal += $row['qty']*$row['kg'];
		            					}
		            				}
		            			}
							}
						}
							
							//$preBal = $remaining - $amount;
						?>
		                <tr>
		                	<td>&nbsp;</td>
		                    <td>&nbsp;</td>
		                    <td>Stock B/F</td>
		                    <td>0</td>
		                    <td><?php echo $grandtotal;?></td>
		                </tr>

						<?php
							// Start date
							$sdate = $_GET['sdate'];
							$scus_date = explode('/', $sdate);
							$get_sdate = $scus_date[2].'/'.$scus_date[0].'/'.$scus_date[1];
							$get_sdate = date ("Y/m/d", strtotime("-1 day", strtotime($get_sdate)));
			                
			                // End date
			             	$edate = $_GET['edate'];
							$ecus_date = explode('/', $edate);
							$get_edate = $ecus_date[2].'/'.$ecus_date[0].'/'.$ecus_date[1];
			                
			                while (strtotime($get_sdate) <= strtotime($get_edate)) {
							//echo "$get_sdate\n";
			                $get_sdate = date ("Y/m/d", strtotime("+1 day", strtotime($get_sdate)));
			            ?>  
			           	<?php
			           			$sql = "SELECT * FROM customerinvoice WHERE item_name = '".$_GET['item']."' AND php_date = '$get_sdate' ORDER BY php_date, id";
								$run = $db->query($sql);

		            			//$subqty = 0;
		            			while($row = $run->fetchArray()){
		            				//$subqty += $row['qty'];

		            				if ($row['sale_type'] == 'Sale') {
		            					$grandtotal -= $row['qty']*$row['kg'];
		            				}elseif ($row['sale_type'] == 'Purchase') {
		            					$grandtotal += $row['qty']*$row['kg'];
		            				}elseif ($row['sale_type'] == 'Misc') {
		            					($row['qty'] < 0) ? $grandtotal += ($row['qty']*$row['kg']) : $grandtotal -= ($row['qty']*$row['kg']);
		            				}elseif ($row['sale_type'] == 'Return') {
		            					$cus_ch = preg_replace('/[^a-z]/', "", strtolower($row['supplierName']));
		            					if($cus_ch == 'c'){
		            						$grandtotal += $row['qty']*$row['kg'];
		            					}elseif($cus_ch == 's'){ 
		            						$grandtotal -= $row['qty']*$row['kg'];
		            					}else{
		            						$grandtotal += $row['qty']*$row['kg'];
		            					}
		            				}
		            	?>
		            		<tr>
			            		<td><?php echo $row['show_date']; ?></td>
			            		<td><?php echo $_GET['item']; ?></td>
			            		<td>

			            			<?php $cus_ch = preg_replace('/[^a-z]/', "", strtolower($row['name'])); 
			            				if($cus_ch == 'c'){
		            						echo " Customer";
		            					}elseif($cus_ch == 's'){ 
		            						echo " Supplier";
		            					}else{
		            						echo " Customer";
		            					}
			            			?>
			            		<?php echo $row['sale_type']; ?>
			            			
			            		</td>
			            		<td><?php echo $row['qty']*$row['kg']; ?></td>
			            		<td><?php echo $grandtotal; ?></td>
			            	</tr>
		            	<?php } ?>
		            	
			            <?php } } ?>


			            <?php
			            if($_GET['group'] == 2){
			            	if($_GET['item'] == 'All'){
								// Run All Queries
								$var = '';
							}
							else{
								$var = "WHERE item_name = '".$_GET['item']."'";
							}

							$i = 0;
							$invoices = 0;
							$payments = 0;
							$receive = 0;
							$grandtotal = 0;
							$supertotal = 0;
							$supertotalcost = 0;

								$other_title = $db->query("SELECT * FROM itemsetup $var");
								while ($fother_title = $other_title->fetchArray()) {

								//$grandtotal = $fother_title['open_qty'];
								// $sql = "SELECT * FROM stock WHERE itemDescription = '".$fother_title['item_name']."' ";
								// $run = $db->query($sql);

		            			
		            			// while($row = $run->fetchArray()){
		            			// 	if ($row['type'] == 'Sale') {
		            			// 		$grandtotal -= $row['qty'];
		            			// 	}elseif ($row['type'] == 'Purchase') {
		            			// 		$grandtotal += $row['qty'];
		            			// 	}elseif ($row['type'] == 'Misc') {
		            			// 		($row['qty'] < 0) ? $grandtotal += $row['qty'] : $grandtotal -= $row['qty'];
		            			// 	}elseif ($row['type'] == 'Return') {
		            			// 		$cus_ch = preg_replace('/[^a-z]/', "", strtolower($row['supplierName']));
		            			// 		if($cus_ch == 'c'){
		            			// 			$grandtotal += $row['qty'];
		            			// 		}elseif($cus_ch == 's'){ 
		            			// 			$grandtotal -= $row['qty'];
		            			// 		}else{
		            			// 			$grandtotal += $row['qty'];
		            			// 		}
		            			// 	}
		            			// }
		            		$i++;
		            		$invoices = $i; 
		            		$supertotal += $fother_title['stock'];
		            		$supertotalcost += @$fother_title['stock'] * $fother_title['purchase_price'];
						?>
							<tr>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $fother_title['item_name']; ?></td>
			            		<td><?php echo "-"; ?></td>
			            		<td><?php echo $fother_title['stock']; ?></td>
			            		<td><?php echo $fother_title['stock'] * $fother_title['purchase_price']; ?></td>
			            	</tr>
						<?php	
								}
							}
						?>

						<?php } ?>
			            	<tr>
								<td colspan="11" align="center" style="color: green;">Total Items : <?php echo $invoices; ?> Total Amount : <?php echo @$supertotalcost; ?></td>
							</tr>
							</tbody>
						</table>			
				</div>	
		</div>	
				</div>
	</div>
</div>

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