<?php 
$header_file=APPPATH.'views/include/header.php';
require($header_file);
$this->db2 = $this->load->database('otherdb', TRUE);
?>
<!-- BEGIN: Content-->
<div class="app-content content">
    <div class="content-overlay"></div>
    <div class="content-wrapper">
        <div class="content-header row">
            <div class="content-header-left col-md-12 col-12 mb-2 breadcrumb-new">
                <h3 class="content-header-title mb-0 d-inline-block">Consumption</h3>
                <div class="row breadcrumbs-top d-inline-block">
                    <div class="breadcrumb-wrapper col-12">
                        <ol class="breadcrumb">
                            <li class="breadcrumb-item">
                                <a href="#">Home</a>
                            </li>
                            <li class="breadcrumb-item">
                                <a href="#">Report</a>
                            </li>
                            <li class="breadcrumb-item active">Consumption
                            </li>
                        </ol>
                    </div>
                </div>
            </div>
        </div>
        <div class="content-body"><!-- Zero configuration table -->
            <section id="configuration">
                <div class="row">
                    <div class="col-md-12">
                        <div class="card">
                            <div class="card-content collapse show">
                                <div class="card-body card-dashboard">
                                    <form method="post" autocomplete="off" action="<?php echo site_url('ExcelReport/searchConsumptionReport/');?>" method="POST">
                        	<div class="row justify-content-md-center">
							    <?php $umane = $_SESSION['username']; ?>
								<div class="col-md-3">
									<div class="form-group">
										<input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="from_date"  id="from_date" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="From Date" >
									</div>
								</div>
								<div class="col-md-3">
									<div class="form-group">
										<input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="to_date"  id="to_date" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="To Date">
									</div>
								</div>
								
								<div class="col-md-2">
									<div class="form-group">
										<select id="branch" name="branch" class="form-control select2" >
											<option value="">Branch</option>
											<option value="All">All</option>
											<?php  foreach($branch as $row)
												{
											?>
												<option value="<?php echo $row->branch_name; ?>"><?php echo $row->branch_name; ?></option>
											<?php
												} 
											?>
										</select>
									</div>
								</div>
							
								
								
								<div class="col-md-1">
									<div class="form-group">
										<input type="submit" name="submit" class="btn btn-primary " value="Search">
									</div>
								</div>
							
							</div>
						</form>
                                    <div class="table-responsive">
                  <?php
if (isset($result_display)) {
    //echo "<p><u>Result</u></p>";
    if ($result_display == 'No record found !') {
        echo $result_display;
    } else {
        ?>   
            <table class="table table-striped table-bordered dataex-html5-export">
                    <thead>
                        <tr>
                            <th>Product Name</th>
							<th>Total Stk</th>
							<th>Sales</th>
							<th>Current Stk</th>
						</tr>
                    </thead>
                    <tbody>
                 <?php
                  //$count =1;
                  $from_date = date('Y-m-d',strtotime($_POST['from_date']));
                  $to_date = date('Y-m-d',strtotime($_POST['to_date']));
                  $branch_name = $_POST['branch'];
                  $i=1;
				  foreach ($result_display as $record) { 
                  $q1 = $this->db->query("SELECT branch_name from user where branch_code!=''");
                	$branches = $q1->result_array();
                	$count = 1;
                	foreach($branches as $br)
                	{
                  
                        $cdate = date('Y-m-d');
                       $pname = $record['rname'];
                       $pcode = $record['product_code'];
                       $branch1 = $br['branch_name'];
                		if($branch_name == $branch1)
                		{
    			            $pr_table = 'product_billdetail'.$count;
    			            $bp_table = 'billed_product'.$count;
    			            //echo $branch1;
    			            //echo $pr_table;
                               $q1 = $this->db2->query("SELECT B.`product_name`,sum(B.`quantity`) as sales_qty FROM $pr_table A inner join `$bp_table` B on A.bill_id=B.bill_id WHERE A.invoice_date between '$from_date' and '$to_date' and B.`product_name`='$pname'  and B.bill_status='Closed'");
                                $slrow = $q1->row_array();
        					    $s_qty = $slrow['sales_qty'];
        					    $diff = $record['production_stock'] - $s_qty;
                           ?>
                        <tr>
        					  <td><?php echo $record['rname']; ?></td>
        					  <td><?php echo $record['production_stock']; ?></td>
        					  <td><?php echo $s_qty; ?></td>
        					  <td><?php echo $diff; ?></td>
                        </tr>
                                    
                                   <?php
                		}
                		$count++;
            				  }
				  }
                        ?>
                                </table>
        
        <?php
    		}
			
		}else{
        ?>		
        
				 <table class="table table-striped table-bordered dataex-html5-export">
                    <thead>
                        <tr>
                            <th>Product Name</th>
							<th>Total Stk</th>
							<th>Sales</th>
							<th>Current Stk</th>
						</tr>
                    </thead>
                    <tbody>
                 <?php
                  $count =1;
                  foreach($consumption as $cnp)
                  {
                      $cdate = date('Y-m-d');
                       $pname = $cnp->rname;
                       $pcode = $cnp->product_code;
                       $q1 = $this->db2->query("SELECT B.`product_name`,sum(B.`quantity`) as sales_qty FROM product_billdetail1 A inner join `billed_product1` B on A.bill_id=B.bill_id WHERE A.invoice_date='$cdate' and B.`product_name`='$pname'  and B.bill_status='Closed'");
                        $slrow = $q1->row_array();
					    $s_qty = $slrow['sales_qty'];
					    $diff = $cnp->production_stock - $s_qty;
                   ?>
                <tr>
					  <td><?php echo $cnp->rname; ?></td>
					  <td><?php echo $cnp->production_stock; ?></td>
					  <td><?php echo $s_qty; ?></td>
					  <td><?php echo $diff; ?></td>
                </tr>
                                    
                                   <?php
                  $count++;
                }
                
              ?>
                                </table>
				
                 <?php } ?>                       
                                   
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </div>
</div>
<?php 
    $footer_file=APPPATH.'views/include/footer.php';
    require($footer_file);
?>
<script type="text/javascript">
	$(function() {
		$('.date-picker').datepicker({
			changeMonth: true,
			changeYear: true,
			showButtonPanel: true,
		//	dateFormat: 'MM yy',
		    dateFormat: 'dd-mm-yy',
		/*	onClose: function(dateText, inst) { 
				$(this).datepicker('setDate', new Date(inst.selectedYear, inst.selectedMonth, 1));
			}*/
		});
	});	
</script>
<script>
    $("#branch").change(function (){
		var branch_code = $(this).find("option:selected").text();
		$("#bcode").val(branch_code);
	});
</script>
</body>
<!-- END: Body-->

<!-- Mirrored from pixinvent.com/modern-admin-clean-bootstrap-4-dashboard-html-template/html/ltr/vertical-modern-menu-template/form-layout-basic.html by HTTrack Website Copier/3.x [XR&CO'2014], Mon, 27 Apr 2020 09:38:02 GMT -->
</html>