
<?php include("sass/icon_for_datepicker_and_datatable.php");?>
        
        
   
        <?php
$errMsg=""; 
if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
{
	$product_name = $_POST['product_name'];
	$product_name = explode("-",$product_name);
	$product_id = $product_name[0];
	$product_name_last = $product_name[1];
	
	$quantity = $_POST['quantity'];
	$quantity_input = ($quantity);
	$details = $_POST['details'];
	
	$date = $_POST['date'];
	if(!empty($date)) {
$date = date("Y-m-d", strtotime($date));
}
else {
$date = "";
}
	if(empty($product_name)) $errMsg="write product name";
	elseif (empty($quantity)) $errMsg="Write Quantity";
	if(empty($errMsg))
	{
		$check=mysqli_query($connection,"select * from product_setup where product_name='$product_name_last'");
		$checkrows=mysqli_num_rows($check);
		if($checkrows=0){$errMsg="Sorry!! Insert product name first";}
		
		else{
		
	
		
		mysqli_query($connection,"INSERT INTO wastage (product_id, quantity, details, date, branch_id) VALUES ('$product_id','$quantity_input','$details','$date','$branch_id_for_header_user')")
		or die(mysqli_error());
		
				
				mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock - $quantity_input WHERE product_id = '$product_id' AND branch_id = '$branch_id_for_header_user'")
				or die(mysqli_error()); 
		
		 
		$errMsg="Product has been deleted successfully.";
	}
}
}
?>
<!--For connect database end-->
<?php


$query_product = "SELECT * FROM product_setup";
$result_product = mysqli_query($connection,$query_product);

while($row_product=mysqli_fetch_array($result_product))
{
	$product_id = $row_product['product_id'];
	$product_name = $row_product['product_name'];
	
	$all_product[] = $product_id."-".$product_name;
}
  ?>
   <script>
  $(function() {
  
  var availableTags = JSON.parse('<?php echo json_encode($all_product); ?>');
  
    
    $( "#tags" ).autocomplete({
      source: availableTags
    });
  });
  </script>         
        
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

            <!-- Right side column. Contains the navbar and content of the page -->
               <aside class="right-side">
                <!-- Content Header (Page header) -->
                <section class="content-header">
                    <h1>
                       <?php echo $company_name ; ?>
                        <small>Control panel</small>
                    </h1>
                    <ol class="breadcrumb">
                        <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
                        <li class="active">Product Setup</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                 
                     <div class="row">
                     
                         <div class="col-md-8 col-md-offset-2">
                   
                       
                        <?php
							if (!empty($errMsg))
							{echo "<div class='alert alert-warning alert-dismissible' role='alert'>$errMsg";
							echo "<button type='button' class='close' data-dismiss='alert'><span aria-hidden='false'>&times;</span><span class='sr-only'>		Close</span></button>
                        </div>";}
                        ?>
                        
                         <div class="box box-primary">
                                <div class="box-header">
                                    <h3 class="box-title">Deduct wasted product</h3>
                                </div><!-- /.box-header -->
                                <!-- form start -->
                                <form role="form"  action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Product Name</label>
                                            <input type="text" class="form-control" name="product_name" id="tags" placeholder="write product name">
                                        </div>  
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Quantity</label>
                                            <input type="text" class="form-control" name="quantity" id="" placeholder="Write here">
                                        </div>  
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Details</label>
                                            <textarea class="form-control" rows="2" name="details" maxlength="50"></textarea>
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Date</label>
                                            <input type="text" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y");?>" placeholder="write here">
                                        </div>
                                    </div><!-- /.box-body -->

                                    <div class="box-footer">
                                        <button type="submit" name="submit" class="btn btn-primary">Submit</button>
                                    </div>
                                   

  


                                </form>
                            </div>
                         
                         </div>
                         
                         
                         <div class="col-md-12">
                         
                         
                         <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">Wasted products list</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>Quantity</th>
                                                <th>Details</th>
                                                <th>Date</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
												<?php
                                                
                                                $query = "SELECT * FROM wastage";
                                                $result = mysqli_query($connection,$query);
                                                 
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                $i++;
												$product_id = $row['product_id'];
												
												$result_for_product = mysqli_query($connection,"SELECT product_name, unit_type_id FROM product_setup WHERE product_id  = '$product_id'");
		$test_for_product = mysqli_fetch_array($result_for_product);
				
				$product_name=$test_for_product['product_name'];
				$unit_type_id=$test_for_product['unit_type_id'];
				
				$result_for_unit_type = mysqli_query($connection,"SELECT * FROM unit_type WHERE unit_type_id  = '$unit_type_id'");
		$test_for_unit_type = mysqli_fetch_array($result_for_unit_type);
				
				$unit_type_name=$test_for_unit_type['unit_type_name'];
                                                
                                                $quantity = $row['quantity'];
												$quantity_bn_view = ($quantity);
												$details = $row['details'];
												$date = $row['date'];
												$date_bn_view = ($date);
                                                
                                                echo "<tr>";
                                                echo "<td>$product_name</td>";
                                                echo "<td>$quantity_bn_view  $unit_type_name"; echo "</td>";
                                                echo "<td>$details</td>";
                                                echo "<td>$date_bn_view</td>";
                                                echo "</tr>";
                                                }
                                                ?>
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Product Name</th>
                                                <th>Quantity</th>
                                                <th>Details</th>
                                                <th>Date</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div><!-- /.box-body -->
                            </div>
                         </div>
                     </div>
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

        <!-- add new calendar event modal -->

 <script>
function EditProduct(id) {
    window.open('edit_store.php?id='+id,'logos','width=500,height=500');}
</script>

  <!-- jQuery 2.0.2 -->
       
      <?php include("sass/footer_for_datepicker_and_datatable.php");?>

    </body>
</html>