<?php include("sass/icon_for_datepicker_and_datatable.php");?>
  <?php 
  
  if(isset($_POST['submit']))
    {
  
  
  $lc_taka_id =$_POST['lc_taka_id'];
  $due_minus =$_POST['due_minus'];
			  
			  $result = mysqli_query($connection,"SELECT * FROM lc_taka WHERE lc_taka_id  = '$lc_taka_id'");
$test = mysqli_fetch_array($result);

				
			  
			  	$lc_taka_id = $test['lc_taka_id'];
	
				$lc_balance = $test['lc_balance'];
				$lc_margin_per = $test['lc_margin_per'];
				$lc_margin = $test['lc_margin'];
				$dollar_rate = $test['dollar_rate']; 
				$lc_due = $test['lc_due']; 
				$supplier_id = $test['supplier_id'];	
				 $lc_number = $test['lc_number'];
				 $farm_name = $test['farm_name'];
				 $bank_id = $test['bank_id'];
				 $date = date("Y-m-d");
				 $first_time = $test['first_time'];
				
				
				$minus_lc_dollar = $lc_due - $due_minus ;
			$minus_lc = $due_minus * $dollar_rate ;
				 
			if($first_time == 1){	
		 $query = "SELECT * FROM lc where lc_number='$lc_number'";
                                                $result = mysqli_query($connection,$query);
                                                 
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                
						$lc_id = $row['lc_id'];
						$product_id = $row['product_id'];
						$quantity = $row['quantity'];
						$buying_price = $row['buying_price'];
						$selling_price = $row['selling_price'];
						$buying_price = $buying_price * $dollar_rate ;
						$selling_price = $selling_price * $dollar_rate ;
                                               
												
		mysqli_query($connection,"INSERT INTO store (product_id, quantity, buying_price, selling_price, supplier_id, chalan_no, stock_of, date) VALUES ('$product_id','$quantity','$buying_price','$selling_price','$supplier_id','$lc_number','rr','$date')")
		or die(mysqli_error());
		
		mysqli_query($connection,"UPDATE product_setup SET buy_price ='$buying_price', selling_price ='$selling_price' WHERE product_id = '$product_id'")
				or die(mysqli_error()); 
				
				
				mysqli_query($connection,"UPDATE present_stock SET present_stock = present_stock + '$quantity' WHERE product_id = '$product_id' and stock_of = 'rr'")
				or die(mysqli_error()); 
												
												
												}
										}
				
				mysqli_query($connection,"INSERT INTO supplier_payment (supplier_id, bank, chalan_no, bank_id, sup_date) VALUES ('$supplier_id','$minus_lc','$lc_number','$bank_id','$date')")
		or die(mysqli_error()); 
		
		
		if(!empty($bank_id)) {
		mysqli_query($connection,"UPDATE bank_name_lc SET balance = balance + $minus_lc WHERE bank_id = $bank_id")
				or die(mysqli_error());
		
		mysqli_query($connection,"INSERT INTO transaction (date, ref_id, supplier_id, ledger, credit, bank_id, hint, payment_type) VALUES ('$date','$lc_number','$supplier_id','Supplier Payment From LC','$minus_lc','$bank_id','Supplier Payment From LC','b_lc')")
		or die(mysqli_error());
		
		if($minus_lc_dollar < 1){
		mysqli_query($connection,"UPDATE lc_taka SET lc_due ='$minus_lc_dollar', first_time ='0', status ='2' WHERE lc_number = '$lc_number'")
				or die(mysqli_error()); 
			}	
			else{
			
			mysqli_query($connection,"UPDATE lc_taka SET lc_due ='$minus_lc_dollar', first_time ='0' WHERE lc_number = '$lc_number'")
				or die(mysqli_error()); 
			}
				echo "<script>window.location='lc_pending.php'</script>";
		
		}
			  
  
  
  
  }
  
  
  
  ?>    
        
   

<link rel="stylesheet" href="js1/jquery-ui.css">
  <script src="js1/jquery-2.1.1.min.js"></script>
  <script src="js1/jquery-ui.js"></script>
   <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">LC List</li>
                    </ol>
                </section>
                 <section class="content">
                 <!---Main Content--->
                
                         <br />
<br />
<br />
 <button type="button" class="btn btn-info" onClick="printDiv('printableArea')"><i class="fa fa-print"></i>  Print</button>
                  <div class="row" id="printableArea">
                         
                         
                         <div class="box">
                                <div class="box-header">
                                    <h3 class="box-title">All LC List Pending For LTR</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                               <th>LC Number</th>
                                               <th>Farm Name</th>
                                                <th>Total Product Type</th>
                                                <th>Supplier</th>
                                                <th>LC Total</th>
                                                <th>LC Margin</th>
                                                <th>Dollar Rate</th>
                                                <th>LC Present Due</th>
                                                <th>Bank Name</th>
                                                <th>Date</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
												<?php
												
												
												if(isset($_POST['submit'])) // To check 'submmited or not' by if(isset($_POST['submit'])) 
														{
														$date1 = $_POST['date1'];
														$date1 = date("Y-m-d", strtotime($date1));
														$date2 = $_POST['date2'];
														$date2 = date("Y-m-d", strtotime($date2));
														
														if (empty($date1)) {
															 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
																  aria-hidden='true'>
																  &times;
															   </button>
															  please write starting date
															</div>";
															 
															 }
															 elseif (empty($date2)) {
															 $message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
																  aria-hidden='true'>
																  &times;
															   </button>
															    please write ending date
															</div>";
															 
															 }
														
		
		 $query = "SELECT * FROM lc_taka WHERE date >= '$date1' AND date <= '$date2'";
														
                                                $result = mysqli_query($connection,$query);
                                                 
                                              
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                
                                                $lc_taka_id = $row['lc_taka_id'];
												$lc_balance = $row['lc_balance'];
                                                $lc_margin_per = $row['lc_margin_per'];
												$dollar_rate = $row['dollar_rate']; 
												$lc_due = $row['lc_due']; 
                                                $supplier_id = $row['supplier_id'];	
												 $lc_number = $row['lc_number'];
												 $farm_name = $row['farm_name'];
												 $bank_id = $row['bank_id'];
												
												$result_for_supplier = mysqli_query($connection,"SELECT * FROM supplier_setup WHERE supplier_id  = '$supplier_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				
				$supplier_name=$test_for_supplier['supplier_name'];
				
				$result_for_pro = mysqli_query($connection,"SELECT COUNT(lc_id) as total_pro FROM lc WHERE lc_number  = '$lc_number'");
		$test_for_pro = mysqli_fetch_array($result_for_pro);
				
				$total_pro=$test_for_pro['total_pro'];
				
				
				$result_for_bank = mysqli_query($connection,"SELECT bank_name_lc FROM bank_name_lc WHERE bank_id  = '$bank_id'");
		$test_for_bank = mysqli_fetch_array($result_for_bank);
				
				$bank_name=$test_for_bank['bank_name_lc'];
												
												$date = $row['date'];
												$date_bn_view = ($date);
												 $status = $row['status'];
										if($status == "1"){
										$status_txt = "pending";
										}	
elseif	($status == "2"){
										$status_txt = "Accepted";
										}										
                                                
                                               echo "<tr>";
												 echo "<td>$lc_number</td>";
												 echo "<td>$farm_name</td>";
                                                echo "<td><a href='lc_product.php?lc_number=$lc_number' target='_blank'>$total_pro</a></td>";
                                                echo "<td>$supplier_name</td>";
                                                echo "<td>$lc_balance Dollar</td>";
                                                echo "<td>$lc_margin_per %</td>";
                                                echo "<td>$dollar_rate</td>";
                                                echo "<td>$lc_due</td>";
                                                echo "<td>$bank_name</td>";
                                                echo "<td>$date_bn_view</td>";
                                                echo "<td>$status_txt</td>";
                                              
									echo "<td><a href='lc_accept.php?lc_taka_id=$lc_taka_id'><button type='button' class='btn btn-info'>LTR</button></a></td>";
                                                echo "</tr>";
                                                }
														
														
														
														}
												
												
												
												
												
                                                else {
                                                $query = "SELECT * FROM lc_taka where status=1";
                                                $result = mysqli_query($connection,$query);
                                                 
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                $i++;
                                                $lc_taka_id = $row['lc_taka_id'];
												$lc_balance = $row['lc_balance'];
                                                $lc_margin_per = $row['lc_margin_per'];
												$dollar_rate = $row['dollar_rate']; 
												$lc_due = $row['lc_due']; 
                                                $supplier_id = $row['supplier_id'];	
												 $lc_number = $row['lc_number'];
												 $farm_name = $row['farm_name'];
												 $bank_id = $row['bank_id'];
												
												$result_for_supplier = mysqli_query($connection,"SELECT * FROM supplier_setup WHERE supplier_id  = '$supplier_id'");
		$test_for_supplier = mysqli_fetch_array($result_for_supplier);
				
				$supplier_name=$test_for_supplier['supplier_name'];
				
				$result_for_pro = mysqli_query($connection,"SELECT COUNT(lc_id) as total_pro FROM lc WHERE lc_number  = '$lc_number'");
		$test_for_pro = mysqli_fetch_array($result_for_pro);
				
				$total_pro=$test_for_pro['total_pro'];
				
				
				$result_for_bank = mysqli_query($connection,"SELECT bank_name_lc FROM bank_name_lc WHERE bank_id  = '$bank_id'");
		$test_for_bank = mysqli_fetch_array($result_for_bank);
				
				$bank_name=$test_for_bank['bank_name_lc'];
												
												$date = $row['date'];
												$date_bn_view = ($date);
												 $status = $row['status'];
										if($status == "1"){
										$status_txt = "pending";
										}	
elseif	($status == "2"){
										$status_txt = "Accepted";
										}										
                                                
                                               echo "<tr>";
												 echo "<td>$lc_number</td>";
												 echo "<td>$farm_name</td>";
                                                echo "<td><a href='lc_product.php?lc_number=$lc_number' target='_blank'>$total_pro</a></td>";
                                                echo "<td>$supplier_name</td>";
                                                echo "<td>$lc_balance Dollar</td>";
                                                echo "<td>$lc_margin_per %</td>";
                                                echo "<td>$dollar_rate</td>";
                                                echo "<td>$lc_due Dollar</td>";
                                                echo "<td>$bank_name</td>";
                                                echo "<td>$date_bn_view</td>";
                                                echo "<td>$status_txt</td>";
                                              
									echo "<td>
									<input type='hidden' id='zipsearch$i' value='$lc_taka_id'/> <button type='button' class='btn btn-primary' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-edit'></i> &nbsp;LTR</button></td>";
                                                echo "</tr>";
												
												
					
			echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  entry.value = collect.value;
}
</script> ";	
				
                                                }
                                                }
                                                ?>
                                         
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                               <th>LC Number</th>
                                               <th>Farm Name</th>
                                                <th>Total Product Type</th>
                                                <th>Supplier</th>
                                                <th>LC Total</th>
                                                <th>LC Margin</th>
                                                <th>Dollar Rate</th>
                                                <th>LC Present Due</th>
                                                <th>Bank Name</th>
                                                <th>Date</th>
                                                <th>Status</th>
                                                <th>Action</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                    <?php
						if (!empty($message))
						echo "$message";
                    ?>
                                </div><!-- /.box-body -->
                            </div>
                         
                         </div>
                     
                     </div>
                 
                 
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

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

                    
                            <form method='post' action='<?php echo $_SERVER['SCRIPT_NAME']; ?>'>
										<div class='modal fade' id='myModal' tabindex='-1' role='dialog' aria-labelledby='myModalLabel' aria-hidden='true'>
  <div class='modal-dialog'>
    <div class='modal-content'>
      <div class='modal-header'>
        <button type='button' class='close' data-dismiss='modal' aria-label='Close'><span aria-hidden='true'>&times;</span></button>
        <h4 class='modal-title' id='myModalLabel'>LTR</h4>
      </div>
      <div class='modal-body'>
      <input type="hidden" id='xpaytype' name='lc_taka_id' class='form-control'/>
        
                    <div class="form-group">
                        <input type="text" name="due_minus" class="form-control" placeholder="LTR Payment Amount In Dollar" required/>
                    </div>
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        <button type='submit'  name="submit" class='btn btn-primary'>Update</button>
      </div>
    </div>
  </div>
</div>
</form>

  <!-- jQuery 2.0.2 -->
       
        <!-- Bootstrap -->
        <script src="js/bootstrap.min.js" type="text/javascript"></script>
        <!-- DATA TABES SCRIPT -->
        <script src="js/plugins/datatables/jquery.dataTables.js" type="text/javascript"></script>
        <script src="js/plugins/datatables/dataTables.bootstrap.js" type="text/javascript"></script>
        <!-- AdminLTE App -->
        <script src="js/AdminLTE/app.js" type="text/javascript"></script>
        <!-- AdminLTE for demo purposes -->
        <script src="js/AdminLTE/demo.js" type="text/javascript"></script>
        <!-- page script -->
        <script type="text/javascript">
            $(function() {
                $("#example1").dataTable();
                $('#example2').dataTable({
                    "bPaginate": true,
                    "bLengthChange": false,
                    "bFilter": false,
                    "bSort": true,
                    "bInfo": true,
                    "bAutoWidth": false
                });
            });
        </script>
<script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy', changeYear: true });
});

</script>

    </body>
</html>