<?php
ob_start();
error_reporting(0);
include("connect.php");
session_start();
$ver = $_SESSION['distributor_user_id'];
$user_role= $_SESSION['user_role']; 
if(!isset($_SESSION["distributor_user_id"])){
   header('location:https://tarkashcorp.appexperts.net/index.php');
}
date_default_timezone_set('Asia/Calcutta');
$da  = date("Y-m-d");
?>
<!DOCTYPE html>
<html lang="en">
    <head>
      <?php include("link_head.php");?>  
    <!-- END HEAD -->
<script>
function myFunction() {
	var txt;
	var r = confirm("Are you sure to cancel!");
	if (r == true) {
		window.location.href="customer-master.php";
	} 
}

</script>
	<style>
	::-webkit-scrollbar {
		display: none;
		overflow: hidden;
	}
	</style>
	<script>
	window.onscroll = function () {
	window.scrollTo(0,0);
	}
	</script>	
		</head>
    <!-- END HEAD -->

    <body class="">
        <!-- BEGIN HEADER -->
		<?php include("header.php");?>
        <!-- END HEADER -->
		
        <!-- BEGIN CONTAINER -->
        <div class="container-fluid">
            <div class="page-content page-content-popup" style="margin-top:18px; max-height:542px;">
                
                <div class="page-sidebar-wrapper">
                    <!-- BEGIN SIDEBAR -->
                    <?php include("menu.php");?> 
				    <!-- END SIDEBAR -->
                </div>

                <!-- END SIDEBAR -->
                <!-- BEGIN CONTENT -->
                <div class="page-fixed-main-content">
                        <div class="row">
                            <div class="col-md-12">
                                <!-- BEGIN SAMPLE TABLE PORTLET-->
                                <div class="portlet">
                                    <div class="portlet-title">
                                        <div class="caption">
                                            <i class="fa fa-bell-o"></i>Edit All Order Confirmation</div>
											<div class="actions">
                                            </div>
                                    </div>
                        <!-- END PAGE BAR -->
                        <!-- BEGIN PAGE TITLE-->
                        <h1 class="page-title">
                        </h1>
						<?php
						$res_check = mysqli_query($con,"select * from tbl_distributor where distributor_id='$ver'");  
						while($fetch_check=mysqli_fetch_assoc($res_check))
						{
							$distributor_category=$fetch_check['distributor_category'];
							$distributor_place_supply=$fetch_check['place_of_supply'];
							if (strpos($distributor_category, 'FMCG') !== false) {
								$distributor_category="FMCG";
							}
							else
								$distributor_category="Service";
						}
						?>
                        <!-- END PAGE TITLE-->
                        <!-- END PAGE HEADER-->
						<?php
						if(isset($_POST['sub']))
						{
							$product_name=$_POST['$product_name;'];;
						}
						?>
                        
                        <div class="row">
                            <div class="col-md-12">
                                <!-- BEGIN SAMPLE TABLE PORTLET-->
                                <div class="portlet">
                                    <div class="portlet-body" style="padding-bottom:0; margin-bottom:0.8%; height:390px; overflow-Y:scroll; overflow-X:hidden;">
                                        <div class="table-scrollable">
										<form name="form1" method="post">
										 <div class="form-body">
									        <table class="table table-striped table-bordered table-advance table-hover">
                                                <thead>
                                                    <tr>
                                                        <th><b>S.No</b> </th>
														<th><b>Product Name</b></th>
														<th><b>Quantity</b></th>
                                                     <!--   <th><b>Add Quantity</b></th>
														<th><b>Total Amount</b></th>
														<?php if($distributor_category=="FMCG") { ?>
														<th><b>Disc Amt</b></th>
														<th><b>Total Net Amt</b></th>
														<?php } ?>-->
														
                                                    </tr>
                                                </thead>
                                                <tbody>
												<?php
												$tk=1;
												$c=1;
												$pid=$_GET['id'];
												date_default_timezone_set('Asia/Calcutta');
												$da  = date("Y-m-d");
											
												$res = mysqli_query($con,"select * from tbl_transaction_detail,tbl_master_product where
												tbl_transaction_detail.product_id=tbl_master_product.product_id and tbl_transaction_detail.transaction_id='$pid'");
												if(mysqli_num_rows($res)>0)
												{     
													while($fetch_product=mysqli_fetch_assoc($res))
													{
														$fetch_pro="proquan".$tk;
														$temp_1=$_POST[$fetch_pro];
														
														
												?>
                                                <tr>
												<td title="S.No."><?php echo $c++; ?></td>
                                                <td><?php echo $fetch_product['product_name']; ?></a></td>
												
												<td>
												<input type="text" style="width:110px;" name="proquan<?php echo $tk; ?>" readonly value="<?php echo $temp_1; ?>" oninput="this.value = this.value.replace(/[^0-9.]/g, '').replace(/(\..*)\./g, '$1');">
												</td>
												
                                                </tr>
												<?php  $tk=$tk+1; }} else { echo "No Data Available"; } ?>
												
											
												
                                                </tbody>
                                            </table>
											 <div class="form-actions">
                                                 <p>Are you sure to edit order</p><button type="submit" name="sub1" onClick="return confirm('Are you sure to add stock')" class="btn blue">Ok</button>
											   <button type="submit" name="sub11" class="btn blue">Back</button>
                                                <button type="submit" name="subcancel" onClick="return confirm('Are you sure to cancel')" class="btn blue">Cancel</button>
                                            </div>
											</form>
                                        </div>
										<?php
										if(isset($_POST['sub11']))
										{
											$pid=$_GET['id'];
											header("location:edit-all.php?id=".$pid);
										}
										if(isset($_POST['subcancel']))
										{
											$pid=$_GET['id'];
											header("location:order-detail.php?id=".$pid);
										}
										if(isset($_POST['sub1']))
										{
											date_default_timezone_set('Asia/Calcutta');
											$da  = date("Y-m-d")." ".date('H:i:sa');
											$cdate=date("Y-m-d");
											$scri=date('H:i:sa');
											$temp_id=1;
											
											$res11 = mysqli_query($con,"select * from tbl_transaction_detail,tbl_master_product where
												tbl_transaction_detail.product_id=tbl_master_product.product_id and tbl_transaction_detail.transaction_id='$pid'");
											while($fetch_product11=mysqli_fetch_assoc($res11))
											{
												$proid=$fetch_product11['product_id'];
												$temp_quantity_temp=$fetch_product11['quantity'];
												$fetch_pro1="proquan".$temp_id;
												$pquanity=$_POST[$fetch_pro1];
												
												echo "UPDATE tbl_transaction_detail SET quantity='$pquanity' where transaction_id='$pid' and product_id='$proid'";
											
												$uq1=mysqli_query($con,"UPDATE tbl_transaction_detail SET quantity='$pquanity' where transaction_id='$pid' and product_id='$proid'");
											
											/*
											$res11 = mysqli_query($con,"select * from tbl_transaction_detail,tbl_master_product where
												tbl_transaction_detail.product_id=tbl_master_product.product_id and tbl_transaction_detail.transaction_id='$pid'");
											while($fetch_product11=mysqli_fetch_assoc($res11))
											{
												$proid=$fetch_product11['product_id'];
												$temp_quantity_temp=$fetch_product11['quantity'];
												$fetch_pro1="proquan".$temp_id;
												$pquanity=$_POST[$fetch_pro1];
												
												$uq1=mysqli_query($con,"UPDATE tbl_transaction_detail SET quantity='$pquanity' and transaction_id_id='$pid'");
												
												if($pquanity!=0)
												{
													$query = mysqli_query($con,"update tbl_transaction_detail set product_id='$proid',	and quantity='$pquanity'");
													
													/*$set_quan=$temp_quantity_temp+$pquanity;
													$uq1=mysqli_query($con,"UPDATE tbl_transaction_detail SET quantity='$set_quan' and transaction_id_id='$pid'");

													$query_stock="SELECT * FROM  tbl_distributor_stock where date ='$cdate' and distributor_id='$ver' and product_id='$proid' ORDER BY `distributor_stock_id` DESC LIMIT 1";
													$res_stock = mysqli_query($con,$query_stock);
													$row_stock =  mysqli_fetch_assoc($res_stock);
													if(mysqli_num_rows($res_stock)>0)
													{
														$stock_quantity=$row_stock['closing_stock'];
														$temp_quantity=$stock_quantity+$pquanity;
														$uq=mysqli_query($con,"UPDATE tbl_distributor_stock SET closing_stock='$temp_quantity',time='$scri' where date ='$cdate' and distributor_id='$ver' and product_id='$proid'");	
													}
													else
													{
														$query_stock1="SELECT * FROM  tbl_distributor_stock where date <='$cdate' and distributor_id='$ver' and product_id='$proid' ORDER BY `distributor_stock_id` DESC LIMIT 1";
														$res_stock1 = mysqli_query($con,$query_stock1);
														$row_stock1 =  mysqli_fetch_assoc($res_stock1);
														if(mysqli_num_rows($res_stock1)>0)
														{
															$stock_quantity=$row_stock1['closing_stock'];
															$temp_quantity=$stock_quantity+$pquanity;
															$stock_insert=mysqli_query($con,"insert into tbl_distributor_stock(distributor_id,product_id,opening_stock,closing_stock,date,time)values('$ver','$proid','$stock_quantity','$temp_quantity','$cdate','$scri')");
														}
														else
														{
															$stock_insert=mysqli_query($con,"insert into tbl_distributor_stock(distributor_id,product_id,opening_stock,closing_stock,date,time)values('$ver','$proid','0','$pquanity','$cdate','$scri')");
														}
													}
												}*/
												$temp_id++;
											}											
											if($uq1)
												header("location:pending-orders.php");
										}
										?>
                                    </div>
									
                                </div>
                                <!-- END SAMPLE TABLE PORTLET-->
                            </div>
							
                           
                        </div>
                    </div>
                    <!-- END CONTENT BODY -->
                </div>
                <!-- END CONTENT -->

            </div>
            <!-- END CONTAINER -->
            <!-- BEGIN FOOTER -->
    <!--    <div class="copyright-v2" style="padding:3px; margin-top:-18px;">
		    <?//=date("Y")?> &copy; Tarkashcorp Developed By <a target="_blank" href="http://www.belltechnology.in">Bell Technology</a>
			</div>-->
		    <!-- END CONTAINER -->
            <!-- BEGIN FOOTER -->
			<a href="#index" class="go2top">
                    <i class="icon-arrow-up"></i>
                </a>
                <!-- END FOOTER -->
            </div>
        </div>
        </div>
		<div class="copyright-v2" style="padding:3px; margin-top:-18px;">
		    <?=date("Y")?> &copy; Tarkashcorp Developed By <a target="_blank" href="http://www.belltechnology.in">Bell Technology</a>
			</div>
        </div>
        <!-- END CONTAINER -->
        <!-- BEGIN QUICK SIDEBAR -->
        <a href="javascript:;" class="page-quick-sidebar-toggler">
            <i class="icon-login"></i>
        </a>
        
        <!-- END QUICK SIDEBAR -->
        <!-- BEGIN QUICK NAV -->
        
        <div class="quick-nav-overlay"></div>
        <!-- END QUICK NAV -->
        
        <!-- BEGIN CORE PLUGINS -->
        <?php include("link_script.php");?>  
        <!-- END THEME LAYOUT SCRIPTS -->
        <script>
            $(document).ready(function()
            {
                $('#clickmewow').click(function()
                {
                    $('#radio1003').attr('checked', 'checked');
                });
            });
        </script>
    </body>
</html>