<?php include("sass/icon_for_datepicker_and_datatable.php");?>
                 
        <div class="wrapper row-offcanvas row-offcanvas-left">
            <!-- Left side column. contains the logo and sidebar -->
            <?php include("sass/sidebar.php");?>

<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>

  
  <?php
if(isset($_POST['new_single_sale_submit']))
    {
	
	$product_id = $_POST['product_id'];
	$quantity = $_POST['quantity'];
	$buying_price = $_POST['buying_price'];
	$selling_price = $_POST['selling_price'];
	$product_name_ex = explode("-",$product_id);
	$product_id_ex = $product_name_ex[0];
	$product_name_last = $product_name_ex[1];
	
 $check=mysqli_query($connection,"select * from product_setup where product_id = $product_id_ex");
$checkrows=mysqli_num_rows($check);
if($checkrows<1){$message= "<div class='alert alert-danger alert-dismissable'><button type='button' class='close' data-dismiss='alert' 
      aria-hidden='true'>
      &times;
   </button>
   sorry! Product Not Found!. 
</div>";}

else{ 



mysqli_query($connection,"INSERT INTO lc(product_id, quantity, buying_price, selling_price, status) 
VALUES ('$product_id_ex','$quantity','$buying_price','$selling_price','1')")
or die(mysqli_error());




}



}


if(isset($_POST['submit']))
    {
	
	$lc_balance = $_POST['lc_balance'];
	$lc_margin_per = $_POST['lc_margin_per'];
	$dollar_rate = $_POST['dollar_rate'];
	$supplier_id = $_POST['supplier_id'];
	$lc_number = $_POST['lc_number'];
	$farm_name = $_POST['farm_name'];
	$bank_id = $_POST['bank_id'];
	$date = $_POST['date'];
	
	
	
	if(empty($lc_margin_per)){
	$lc_margin_per = 0 ;
	}
	
	
	if(!empty($date)) {
$date = date("Y-m-d", strtotime($date));
}
else {
$date = "";
}
	$minus_from_bank = ($lc_balance * $lc_margin_per)/100; 
	
	$lc_due = $lc_balance - $minus_from_bank ;
	
	$minus_from_bank = $minus_from_bank * $dollar_rate ;
	
	if(!empty($bank_id)){
	
	mysqli_query($connection,"UPDATE bank_name_lc SET balance = balance + $minus_from_bank WHERE bank_id = $bank_id")
				or die(mysqli_error());
				
	
	mysqli_query($connection,"INSERT INTO transaction (date, ref_id, ledger, credit, bank_id, hint, payment_type) VALUES ('$date','$lc_number','LC Margin','$minus_from_bank','$bank_id','LC Margin','b_lc')")
	or die(mysqli_error());
	
	}
	
	
mysqli_query($connection,"INSERT INTO lc_taka (`lc_balance`, `lc_margin_per`, `lc_margin`, `dollar_rate`, `lc_due`, `supplier_id`, `lc_number`, `farm_name`, `bank_id`, `date`, `first_time`, `status`) VALUES ('$lc_balance','$lc_margin_per','$minus_from_bank','$dollar_rate','$lc_due','$supplier_id','$lc_number','$farm_name','$bank_id','$date','1','1')")
or die(mysqli_error());
	
	mysqli_query($connection,"UPDATE lc SET lc_number = '$lc_number' , status = 2 WHERE status = 1")
				or die(mysqli_error());
	$errMsg="LC Successfully Created";
	}


?>


  
            <!-- 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">Dashboard</li>
                    </ol>
                </section>
                 <section class="content">
                 	 <div class="row">
                        <div class="col-md-8">
                 <!---Main Content--->
                 		
                 			
                            <div class="box box-success">
                                <div class="box-header">
                                    <i class="fa fa-shopping-cart"></i>
                                    <h3 class="box-title">LC Goods Details</h3>
                                <input type="hidden" name=type id='bt2' value='Sack Sale' onclick="setPakSale('sub4');"; style="margin-top:7px;"> 
                                </div><!-- /.box-header -->
                                <div class="box-body">
                                 <?php
							if (!empty($message))
							{echo "$message";}
						
                        ?> <form role="form" name="form" action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                                   
                                    <div class="row">
                                    	<div class="col-md-7">
										
										<select name="product_id" class="form-control">
												<?php  
                                                $query_bank = "SELECT product_id, product_name FROM product_setup";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
												
												$product_name=$row_bank['product_name'];
				
                                                
                                                echo "<option value='" . $row_bank['product_id'] . "'>";echo "$product_name";  echo "</option>";}?> 
                                            
                                            </select>
                                        </div>
                                        <div class="col-md-2">
                                        	<input class="form-control" type="number" name="quantity" placeholder="quantity" required/>
                                        </div>
                                        <div class="col-md-3">
                                        	<input class="form-control" type="input" name="buying_price" placeholder="Buying Price (in Dollar)" required/>
                                        </div>
                                    </div>
                                    <div class="row">
                                    	<div class="col-md-4">
                                        	<input class="form-control" type="input" name="selling_price" placeholder="Selling Price (in Dollar)" required/>
                                        </div>
                                        <div class="col-md-3 pull-right" style="margin-top:5px;">
                                        	<button type="submit" name="new_single_sale_submit" class="btn btn-success">OK</button>
                                        </div>
                                     </div>
                                    </form>
                                    <br />
                                    
                                    <div class="row">
                                    <table class="table table-hover">
 										<tr>
                                        <th>Serial</th>
                                        <th>Name</th>
                                        <th>Qty</th>
                                        <th>Buying Price</th>
                                        <th>Selling Price</th>
                                        <th>Total Buying Price</th>
                                        </tr>
                                        
                                       <?php
                                        
                                        $query = "SELECT * FROM lc where status = '1'";
                                        $result = mysqli_query($connection,$query);
                                        
										$i=0;
										$grand_total = 0 ;
                                        while($row=mysqli_fetch_array($result))
                                        {
                                        
										 $i++;
                                        $lc_id = $row['lc_id'];
                                        $product_id_to = $row['product_id'];
										$quantity = $row['quantity'];
										
										

$result_for_product = mysqli_query($connection,"SELECT * FROM product_setup WHERE product_setup.product_id  = $product_id_to");
		$test_for_product = mysqli_fetch_array($result_for_product);
				
				$product_name_to=$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_con = $quantity ;
										  $quantity_show = ($quantity_con);
										  $buying_price = $row['buying_price'];
										$selling_price = $row['selling_price'];
                                       $selling_price_show = ($selling_price);
                                        $total = $quantity * $buying_price ;
										
										$grand_total = $grand_total + $total ;
										
                                        $total_show = ($total);
											
																		
                                        echo "<tr>
                                                <td>$i</td>
                                                <td>$product_name_to</td>
												<td>$quantity_show $unit_type_name</td>
                                                <td>$buying_price Dollar</td>
                                                <td>$selling_price_show Dollar</td>";
                                            echo    "<td>$total_show</td>
                                                                                            
                                                <td>
                                                
                                             <button type='button' class='btn btn-danger' data-toggle='modal' data-target='#myModal$i'><i class='fa fa-minus-square'></i>&nbsp; Remove</button> 
                                                </td>
                                            </tr>";
											echo "<div class='modal fade' id='myModal$i' 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'>be sure</h4>
      </div>
      <div class='modal-body'>
       are you sure want to delete <strong> $product_name_to </strong>?
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>No</button>
        <a href='delete.php?tbl=lc&idcol=lc_id&id=$lc_id&redirect=lc_form'><button type='button' class='btn btn-danger'>Yes</button></a>
      </div>
    </div>
  </div>
</div>";
											}	
                                        ?>
                                        <tr>
                                        <th></th>
                                        <th></th>
                                        <th>Total Price</th>
                                        <th><?php echo ($grand_total); ?></th>
                                        </tr>
									</table>
                                    </div>
                                </div><!-- /.box-body -->
                            </div><!-- /.box -->
                     
                 
                 		</div>
                        <div class="col-md-4">
                        	<form role="form" name="form" action="<?php echo "https://".$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']; ?>" method="post">
                            	<div class="box box-info">
                                <div class="box-header">
                                    <i class="fa fa-file-text"></i>
                                    <h3 class="box-title">Create New LC</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body">
                                
                                	<table style="margin:auto">
<input type="hidden" name="decrese" value="yes" />
<tr><td valign="top">
<label for="text">LC Balance(In Dollar)</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="lc_balance" value="<?php echo ($grand_total); ?>" /></span> 
</td></tr>
<tr><td valign="top">
<label for="text">LC Margin(In %)</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="lc_margin_per" value="" /></span> 
</td></tr>
<tr><td valign="top">
<label for="text">1 Dollar = BDT:</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="dollar_rate" /></span> 
</td></tr>
<tr><td valign="top">
<label for="text">LC Number</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="lc_number" value="" /></span> 
</td></tr>
<tr><td valign="top">
<label for="text">Farm Name</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control" name="farm_name" value="" /></span> 
</td></tr>



<tr><td valign="top">
<label for="text">Bank Name</label>	
</td><td>
	<span class="input-wrap">	<select name="bank_id" class="form-control">
                                            <option value="">Select</option>
												<?php  
                                                $query_bank = "select * from bank_name_lc";
                                                $result_bank = mysqli_query($connection,$query_bank);
                                                
                                               
                                                while($row_bank=mysqli_fetch_array($result_bank))
                                                {
                                                
                                                echo "<option value='" . $row_bank['bank_id'] . "'>" . $row_bank['bank_name_lc'] . "</option>";}?> 
                                            
                                            </select></span> 
</td></tr>

<tr><td valign="top">
<label for="text">Supplier Name</label>	
</td><td>
<select name="supplier_id" class="form-control">
 <option value="">Select</option>
                                            
												<?php  
                                                $query = "select * from supplier_setup ";
                                                $result_techname = mysqli_query($connection,$query);
                                                
                                               
                                                while($row_techname=mysqli_fetch_array($result_techname))
                                                {
                                                
                                                echo "<option value='" . $row_techname['supplier_id'] . "'>" . $row_techname['supplier_name'] . "</option>";}?> 
                                            
                                            </select>
</td></tr>

<tr><td valign="top">
<label for="text">Date</label>	
</td><td>
	<span class="input-wrap">	<input type="input" class="form-control datepicker" autocomplete="off" name="date" value="<?php echo date("d-m-Y");?>" /></span> 
</td></tr>

<tr>
<td>
<label for="text"></label>	
</td>
<td>
<input style="width:90px"  type="submit" name="submit" value="Submit" />
</td>
</tr>
	
</table>

                                
                                </div>
                            	
                            
                            </form>
                        
                        
                        </div>
                    </div>
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

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

        <!-- jQuery 2.0.2 -->
          <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>
        
        <script>
$(function() {
$( ".datepicker" ).datepicker({ dateFormat: 'dd-mm-yy', changeYear: true });
});

</script>

    </body>
</html>