<?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">Selling Item Movement</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">Selling Item Movement
                            </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 autocomplete="off" method="POST" action="<?php echo site_url('ExcelReport/searchdailyOperationCtr/');?>">
					            <div class="row">
							<div class="col-md-3">
									<div class="form-group">
										<!--<input class="form-control pickadate datepicker picker__input picker__input--active" type="text" name="fmonth"  id="fmonth" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="From Date">-->
									<input type="text" name="fmonth" id="fmonth" class="form-control date-picker">
									<?php 
                                    //$my_string = "Hello World!";//echo json_encode($my_string);
									?>
									</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="tmonth"  id="tmonth" required data-uk-datepicker="{format:'DD-MM-YYYY'}" placeholder="To Date">-->
										<input type="text" name="tmonth" id="tmonth" class="form-control date-picker">
										<input type="hidden" name="branch" id="branch" class="form-control" value="<?php echo $_SESSION['branch_id'] ?>" >
										<input type="hidden" name="bcode" id="bcode" class="form-control" value="<?php echo $_SESSION['branch_code'] ?>" >
									</div>
								</div>
							<!--	<div class="col-md-2">
									<div class="form-group">
										<select id="branch" name="branch" class="form-control select2" >
											<option value="">Branch</option>
											<?php /* foreach($branch as $row)
												{
											?>
												<option value="<?php echo $row->branch_id; ?>"><?php echo $row->branch_code; ?></option>
											<?php
												} */
											?>
										</select>
										<input type="hidden" name="bcode" id="bcode" class="form-control" >
									</div>
								</div>-->
								<div class="col-md-2">
									<div class="form-group">
										<select id="pcategory" name="pcategory" class="form-control select2" >
											<option value="">Category</option>
											<?php foreach($pcat as $pcrow)
												{
											?>
												<option value="<?php echo $pcrow->pr_cat_id; ?>"><?php echo $pcrow->pr_cat_name; ?></option>
											<?php
												}
											?>
										</select>
										<input type="hidden" name="pcat" id="pcat" class="form-control" >
									</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($_POST['submit'])) {
					 
					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" >
                                            <thead>
                                                <tr>
                                                  <!--  <th>Branch</th> -->
                                                    <th>Branch : <?php echo $_POST['bcode']; ?></th>
                                                    <th></th>
                                                    <th>Fdate</th>
                                                    <th><?php echo $_POST['fmonth']; ?></th>
                                                    <th></th>
                                                    <th>Tdate</th>
                                                    <th><?php echo $_POST['tmonth']; ?></th>
                                                    <th></th>
                                                    <th>Cat : <?php echo $_POST['pcat']; ?></th>
                                                </tr>
                                            </thead>
                                        <tbody>
                                           <tr>
                                               <th>#</th>
                                                <!--    <th>Item</th>-->
                                                    <th>O/P Stk</th>
                                                    <th>Inward</th>
                                                    <th>Trans In</th>
                                                    <th>Trans Out</th>
                                                    <th>Sales</th>
                                                    <th>S/W Wastge</th>
                                                    <th>Cls Stk</th>
                                                    <th>Difference</th>
                                            <?php
                                            //total
                                            $opttl = 0;
                                            $inttl = 0;
                                            $trinttl = 0;
                                            $troutttl = 0;
                                            $slttl = 0;
                                            $wstttl = 0;
                                            $clsttl = 0;
                                            $diffttl = 0;
                                            
                                            $fdate = date('Y-m-d',strtotime($_POST['fmonth']));
                                            $tdate = date('Y-m-d',strtotime($_POST['tmonth']));
                                                if(isset($result_display) && is_array($result_display) && count($result_display)): $i=1;
						                    	foreach ($result_display as $data) {
						                    	    $opttl = $opttl + $data['opening_stock'];
                                                    $inttl = $inttl + $data['tinward'];
                                                    $trinttl = $trinttl + $data['Transfer_in'];
                                                    $troutttl = $troutttl + $data['Transfer_out'];
                                                    $wstttl = $wstttl + $data['Wastage'];
                                                    $clsttl = $clsttl + $data['closing_stock'];
            		                    	        
            		                    	        //$iname = $data['item_name'];
            		                    	        $rc_no = $data['recipe_no'];
						                    	    $br_code=$_POST['bcode'];
						                    	    $bq = $this->db2->query("SELECT DISTINCT(branch) from users where branch like 'CW%'");
						                    	    $branches = $bq->result_array();
								    $count = 1;
								    foreach($branches as $br)
    								{
    								    $branch1 = $br['branch'];
    								    if($br_code == $branch1)
    									{
    										$spr_table = 'product_billdetail'.$count;
    										$sbp_table = 'billed_product'.$count;
    										
    										 $q1 = $this->db2->query("SELECT B.`product_name`,sum(B.`quantity`) as sales_qty FROM $spr_table A inner join $sbp_table B on A.bill_id=B.bill_id WHERE A.invoice_date BETWEEN '$fdate' and '$tdate' and B.`product_no`='$rc_no' and B.bill_status='Closed'");
						                    	   $slrow = $q1->row_array();
						                    	   $iname = $slrow['product_name'];
						                    	   $s_qty = $slrow['sales_qty'];
						                    	   //$diff = $data['difference'] - $s_qty;
    									}
    									$count++;
    								}
    								
    								$q2 = $this->db2->query("SELECT B.`product_name`,sum(B.`quantity`) as ol_qty FROM social_orders A inner join `items_orders_social` B on A.socialorder_id=B.bill_id WHERE A.invoice_date BETWEEN '$fdate' and '$tdate' and B.`product_name`='$iname' and (A.order_state='Completed' or A.order_state='Dispatched') and A.branch='$br_code'");
						                    	   $olrow = $q2->row_array();
						                    	   $tl_qty = $olrow['ol_qty'] + $s_qty;
						                    	   $diff = $data['difference'] - $tl_qty;
						                    	   
						                    	   //total
						                    	   $slttl = $slttl + $tl_qty;
						                    	   $diffttl = $diffttl + $diff;
						          ?>
                                            <tr>
                                             <!--   <td><?php // echo $i; ?></td> -->
                                                <td><?php echo $data['item_name']; ?></td>
                                                <td><?php echo $data['opening_stock']; ?></td>
                                               <td><?php echo $data['tinward']; ?></td>
                                               <td><?php echo $data['Transfer_in']; ?></td>
                                               <td><?php echo $data['Transfer_out']; ?></td>
                                               <td><?php echo $tl_qty; ?></td>
                                               <td><?php echo $data['Wastage']; ?></td>
                                               <td><?php echo $data['closing_stock']; ?></td>
                                               <td><?php echo $diff; ?></td>
                                            </tr>
                                            	<?php
									$i++;
									} ?>
									<tr>
									    <td style="font-weight:bold;">Total</td>
							<!--	<td></td>-->
                                <td style="font-weight:bold;"><?php echo $opttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $inttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $trinttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $troutttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $slttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $wstttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $clsttl; ?></td>
                                <td style="font-weight:bold;"><?php echo $diffttl; ?></td>
                                </tr>
									<?php else:
								?>
								
								<tr>
									<td colspan="3" align="center" >No Records Found..</td>
								</tr>
								<?php
									endif;
								?>
                                            </tbody>
                                        </table>
						
						<?php }
					}
				}else{ ?>
                                        
                                        <?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);
	}); */
	
	$("#pcategory").change(function (){
		var pcat = $(this).find("option:selected").text();
		$("#pcat").val(pcat);
	});
</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>