<?php 
$header_file=APPPATH.'views/include/header.php';
require($header_file);
$this->db2 = $this->load->database('otherdb', TRUE);
?>
<style>
    .mask{
      display: none; /*This hides the mask*/
    }
    
    .mask.ajax{
      display: block;
      width: 100%;
      height: 100%;
      position: relative; /*required for z-index*/
      z-index: 1000; /*puts on top of everything*/
      background-image: url (loading-icon.png);
    }
</style>
<!-- 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/searchdailyOperationtest/');?>">
					            <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">
									</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_name; ?></option>
											<?php
												} 
											?>
										</select>
										<input type="hidden" name="bcode" id="bcode" class="form-control" >
									</div>
								</div>
							<!--	<div class="col-md-3">
									<div class="form-group">
										<select id="pcategory" name="pcategory[]" class="form-control select2" multiple="multiple" >
											<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" id="smresult" value="Search">
									</div>
								</div>
								</form>
								<?php  
								if(isset($_POST['submit'])) {
								if(file_exists('sim_data.json'))
{
	$filename = 'sim_data.json';
	$data = file_get_contents($filename); //data read from json file
	//print_r($data);
	$user = json_decode($data);  //decode a data

	//print_r($user); //array format data printing
	 $message = "<h3 class='text-success'>JSON file data</h3>";
}else{
	 $message = "<h3 class='text-danger'>JSON file Not found</h3>";
}
//echo $message;
/*foreach($user->sim_data as $mydata)

    {
         echo $mydata->pr_cat_id . "\n";
         foreach($mydata->product as $values)
         {
              echo $values->item_name . "\n";
         }
    }  */ 
    ?>
    <table  id="tableID" border="1" style="border:1px solid!important;" class="table">
    <thead><tr><th>Branch</th><th><?php echo $_POST['bcode']; ?></th><th>Fdate</th><th><?php echo $_POST['fmonth']; ?></th><th></th><th>Tdate</th><th><?php echo $_POST['tmonth']; ?></th></tr>
    <?php foreach($user->sim_data as $mydata)
    { ?>
		       <tr><th><?php echo $mydata->prod_cat ;?></th><th><?php echo $mydata->opttl; ?></th><th><?php echo $mydata->inttl; ?></th><th><?php echo $mydata->slttl; ?></th><th><?php echo $mydata->wstttl; ?></th><th><?php echo $mydata->clsttl; ?></th><th><?php echo $mydata->diffttl; ?></th></tr></thead>
		       <tbody>
		       <tr><th>Item</th><th>O/P Stk</th><th>Inward</th><th>Sales</th><th>S/W Wastge</th><th>Cls Stk</th><th>Difference</th></tr>
		       <?php foreach($mydata->product as $values){
		        ?>
			        
			       <tr><td><?php echo $values->item_name; ?></td><td><?php echo $values->opening_stock; ?></td><td><?php echo $values->tinward; ?></td><td><?php echo $values->tl_qty; ?></td><td><?php echo $values->Wastage; ?></td><td><?php echo $values->closing_stock; ?></td><td><?php echo $values->diff; ?></td></tr>
				<?php } ?>
				<tr><td colspan='9'> </td></tr>
			    </tbody>
		<?php } ?>
		</table>
	<?php } ?>	
				<?php			/*	$fdate='2024-06-05';
								$tdate='2024-06-07';
								$branch ='11';
								$bcode= 'BY2C7';
							$pcat1='4';
	     $query1 = $this->db->query("SELECT pr_cat_id,pr_cat_name from product_category where pr_cat_id in ($pcat1) order by pr_cat_name");
	    foreach($query1->result() as $pr_cat)
	    {
	        $prod_cat = $pr_cat->pr_cat_name;
			$pr_cat_id = $pr_cat->pr_cat_id;
	       
	        $results1 = $this->db->query("SELECT R.branchname,R.rname as item_name,R.recipe_no,R.product_no,K.transaction_date as start_date,IFNULL(K.opening_stock,0) as opening_stock,IFNULL(K.inward,0) as inward,IFNULL(K.rinward,0) as rinward,IFNULL((K.inward+K.rinward),0) as tinward,IFNULL(K.Transfer_in,0) as Transfer_in,IFNULL(K.Transfer_out,0) as Transfer_out,IFNULL(K.Wastage,0) as Wastage,R.pr_cat_id as category_id,IFNULL((K.opening_stock+K.inward+K.rinward+K.Transfer_in-K.Transfer_out-K.Wastage),0) as difference,K.opening_stock as opstk from(select * from recipe where branch_id='$branch' and pr_cat_id='$pr_cat_id' and  rname!='GARLIC KHARA BOONDI [200GM]' and status='Enabled' and product_no!='') R left join (SELECT item_id,opening_stock,item_name,SUM(CASE WHEN trans_type = 'PrInward' THEN in_qty ELSE 0 END) AS inward,SUM(CASE WHEN trans_type = 'Inward' THEN in_qty ELSE 0 END) AS rinward,SUM(CASE WHEN trans_type = 'transPrInward' THEN in_qty ELSE 0 END) AS Transfer_in,SUM(CASE WHEN trans_type = 'transPrIssue' THEN out_qty ELSE 0 END) AS Transfer_out,SUM(CASE WHEN trans_ref_no like 'DMG%' THEN out_qty ELSE 0 END) as Wastage,branchname,recipe_no,transaction_date,category_id FROM `stock_detail` WHERE ((branch_id='$branch' and category_id='$pr_cat_id' and recipe_no!='') and (transaction_date between '$fdate' and '$tdate') and item_name!='GARLIC KHARA BOONDI [200GM]') GROUP BY recipe_no)K on R.recipe_no=K.recipe_no");  
	       // $results1 = $this->db->query("SELECT R.branchname,R.rname as item_name,R.recipe_no,R.product_no,K.transaction_date as start_date,IFNULL(K.opening_stock,0) as opening_stock,IFNULL(K.inward,0) as inward,IFNULL(K.rinward,0) as rinward,IFNULL((K.inward+K.rinward),0) as tinward,IFNULL(K.Transfer_in,0) as Transfer_in,IFNULL(K.Transfer_out,0) as Transfer_out,IFNULL(K.Wastage,0) as Wastage,R.pr_cat_id as category_id,IFNULL((K.opening_stock+K.inward+K.rinward+K.Transfer_in-K.Transfer_out-K.Wastage),0) as difference,K.opening_stock as opstk from(select * from recipe where branch_id='$branch' and  rname!='GARLIC KHARA BOONDI [200GM]' and status='Enabled' and product_no!='') R left join (SELECT item_id,opening_stock,item_name,SUM(CASE WHEN trans_type = 'PrInward' THEN in_qty ELSE 0 END) AS inward,SUM(CASE WHEN trans_type = 'Inward' THEN in_qty ELSE 0 END) AS rinward,SUM(CASE WHEN trans_type = 'transPrInward' THEN in_qty ELSE 0 END) AS Transfer_in,SUM(CASE WHEN trans_type = 'transPrIssue' THEN out_qty ELSE 0 END) AS Transfer_out,SUM(CASE WHEN trans_ref_no like 'DMG%' THEN out_qty ELSE 0 END) as Wastage,branchname,recipe_no,transaction_date,category_id FROM `stock_detail` WHERE ((branch_id='$branch'  and recipe_no!='') and (transaction_date between '$fdate' and '$tdate') and item_name!='GARLIC KHARA BOONDI [200GM]')  GROUP BY recipe_no)K on R.recipe_no=K.recipe_no ");
	        
	         unset($product);
	        
	        //total
            $opttl = 0;
            $inttl = 0;
            $trinttl = 0;
            $troutttl = 0;
            $slttl = 0;
            $wstttl = 0;
            $clsttl = 0;
            $diffttl = 0;
            
            $op_stk = 0;
            $cls_stk = 0;
            $diff1 = 0;
            $s_qty = 0;
	        
	        foreach($results1->result() as $prod)
	        {
	            $opstk = $prod->opstk;
	            $rc_no = $prod->recipe_no;
	            $pr_no = $prod->product_no;
	            echo $rc_no;
	            echo $pr_no;
	            if($opstk == '')
	            {
    	            $rcq = $this->db->query("SELECT closing_stock FROM stock_detail WHERE transaction_date<'$fdate' and recipe_no='$rc_no' and branch_id='$branch' and recipe_no!='' ORDER BY stock_id DESC limit 1");
    	            $rcrow = $rcq->row_array();
			        $opttl = $opttl + $rcrow['closing_stock'];
			        $op_stk = $rcrow['closing_stock'];
			        $clsttl = $clsttl + $rcrow['closing_stock'];
			        $cls_stk = $rcrow['closing_stock'];
			        $diff1 = $op_stk + $prod->tinward + $prod->Transfer_in - $prod->Transfer_out - $prod->Wastage - $cls_stk;
	            }
	            else
	            {
	                $rcq = $this->db->query("SELECT closing_stock FROM stock_detail WHERE transaction_date between '$fdate' and '$tdate' and recipe_no='$rc_no' and branch_id='$branch' and recipe_no!='' ORDER BY stock_id DESC limit 1");
    	            $rcrow = $rcq->row_array();
    	            $clsttl = $clsttl + $rcrow['closing_stock'];
	                $cls_stk = $rcrow['closing_stock'];
    	            
    	         
	               
	                $opttl = $opttl + $prod->opening_stock;
	                $op_stk = $prod->opening_stock;
	                
	                $diff1 = $op_stk + $prod->tinward + $prod->Transfer_in - $prod->Transfer_out - $prod->Wastage - $cls_stk;
	            }
	            $inttl = $inttl + $prod->tinward;
                $trinttl = $trinttl + $prod->Transfer_in;
                $troutttl = $troutttl + $prod->Transfer_out;
                $wstttl = $wstttl + $prod->Wastage;
                
             
                $bq = $this->db2->query("SELECT branch_title as branch,branch_slno from branch_creation where status ='Active' order by branch_slno ASC");
				$branches = $bq->result_array();
				$count = 1;
				foreach($branches as $br)
				{
				    $branch1 = $br['branch'];
				    $slno = $br['branch_slno'];
				    
				    if($bcode == $branch1)
					{
					    $spr_table = 'product_billdetail'.$slno;
    					$sbp_table = 'billed_product'.$slno;
    					
    					    $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`='$pr_no' and A.status='closed'))");
    					    $slrow = $q1->row_array();
    					    $iname = $slrow['product_name'];
    					    $s_qty = $slrow['sales_qty'];
    					
    				}
				
					$count++;
				}
				
		
    				$tl_qty = $s_qty; // $olrow['ol_qty'] + $icrow['ic_qty'] + 
    				$diff = $diff1 - $tl_qty;
				
				
        	    //total
        	    $slttl = $slttl + $tl_qty;
        	    $diffttl = $diffttl + $diff;
        	    
        	    $product[] = array(
        	        'pr_cat_name' => $prod_cat,
        			'item_name' => $prod->item_name,
        			'opening_stock' => $op_stk,
        			'tinward' => $prod->tinward,
        			'Transfer_in' => $prod->Transfer_in,
        			'Transfer_out' => $prod->Transfer_out,
        			'tl_qty' => $tl_qty,
        			'Wastage' => $prod->Wastage,
        			'closing_stock' => $cls_stk,
        			'diff' => $diff
				);
	        }
	        if(isset($product))
	        {
    	        $json[] = array(
    	            'prod_cat' => $prod_cat,
        			'pr_cat_id' => $pr_cat_id,
        			'opttl' => $opttl,
        			'inttl' => $inttl,
        			'trinttl' => $trinttl,
        			'troutttl' => $troutttl,
        			'slttl' => $slttl,
        			'wstttl' => $wstttl,
        			'clsttl' => $clsttl,
        			'diffttl' => $diffttl,
        			'product' => $product
    			);
    	    }
	    }
	    $sm_data = array(
		'sim_data' => $json
		);
		
		echo json_encode($sm_data);*/
			
				?>
							</div>
					<!--	</form>-->
                                    <div class="table-responsive" >
                         <button onclick="method(btn)" class="btn btn-info pull-left" style="margin-bottom: 10px;">Export</button>               	
						
					<!--	<table id="smtable" class="table table-striped table-bordered simtest" >--
						<table  id="tableID" border="1" style="border:1px solid!important;" class="table">
						
                        </table>-->
                        <div class="result"></div>
                        <div id="loading" style="display:none;font-size:large;text-align:center;">Loading..</div>
					            </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>

<script>
/*$("#smresult").click(function(){
    var fdate = $('#fmonth').val();
    var tdate = $('#tmonth').val();
    var branch = $('#branch').val();
    var bcode = $('#bcode').val();
    var pcategory = $('#pcategory').val();
    var smdata = "";
			$.ajax({  
            url:"<?php echo base_url();?>index.php/ExcelReport/searchdailyOperationtest",  
                    data : { fdate : fdate,tdate : tdate,branch : branch,bcode : bcode,pcategory : pcategory },
                    type: "POST",  
                    success:function(data){  
                      $(".result").html(data);
				
            }  
          });
});*/
$("#smresult").click(function(){
    var fdate = $('#fmonth').val();
    var tdate = $('#tmonth').val();
    var branch = $('#branch').val();
    var bcode = $('#bcode').val();
    var pcategory = $('#pcategory').val();
    var smdata = "";
    //alert(fdate);
    $.ajax({
		url : '<?php echo base_url();?>index.php/ExcelReport/searchdailyOperationtest',
		data : { fdate : fdate,tdate : tdate,branch : branch,bcode : bcode,pcategory : pcategory },
		type :'POST',
		dataType:'json',
		success: function(data) {
		   // alert('hgfhf');
		   // alert(data);
		   // console.log(data)
		    
		    
		    //$(".result").html(data);
		   /*$('#tableID').empty();
		   smdata += "<thead><tr><th>Branch</th><th>"+bcode+"</th><th></th><th></th><th>Fdate</th><th>"+fdate+"</th><th></th><th>Tdate</th><th>"+tdate+"</th></tr>";
		   for(i in data.sim_data)
		    {
		       alert(data.sim_data[i].pr_cat_id);
		       alert("test");
		       smdata += "<tr><th>"+data.sim_data[i].prod_cat+"</th><th>"+data.sim_data[i].opttl+"</th><th>"+data.sim_data[i].inttl+"</th><th>"+data.sim_data[i].trinttl+"</th><th>"+data.sim_data[i].troutttl+"</th><th>"+data.sim_data[i].slttl+"</th><th>"+data.sim_data[i].wstttl+"</th><th>"+data.sim_data[i].clsttl+"</th><th>"+data.sim_data[i].diffttl+"</th></tr></thead>";
		       smdata += "<tbody>";
		       smdata += "<tr><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></tr>";
		       
		       for(j in data.sim_data[i].product)
			    {
			        
			        smdata += "<tr><td>"+data.sim_data[i].product[j].item_name+"</td><td>"+data.sim_data[i].product[j].opening_stock+"</td><td>"+data.sim_data[i].product[j].tinward+"</td><td>"+data.sim_data[i].product[j].Transfer_in+"</td><td>"+data.sim_data[i].product[j].Transfer_out+"</td><td>"+data.sim_data[i].product[j].tl_qty+"</td><td>"+data.sim_data[i].product[j].Wastage+"</td><td>"+data.sim_data[i].product[j].closing_stock+"</td><td>"+data.sim_data[i].product[j].diff+"</td></tr>";
				}
				smdata += "<tr><td colspan='9'> </td></tr>";
			    smdata += "</tbody>";
			}
			
			$('#tableID').html(smdata);
			//$('#tableID').append(smdata);*/
		}
	
    });
});
</script>
<script>
   $(document).ajaxStart(function(){
          $("#loading").show();
        }).ajaxStop(function() {
          $("#loading").hide();
    });
</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>