<?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'])) 
{
	
	$supplier_name = $_POST['supplier_name'];
	$address = $_POST['address'];
	$district = $_POST['district'];
	$mobile_no = $_POST['mobile_no'];
	$iscustomer = $_POST['iscustomer'];
	
	
	if(empty($supplier_name)) $errMsg="write supplier name";
	if(empty($errMsg))
	{
		
		
		mysqli_query($connection,"INSERT INTO supplier_setup (supplier_name, address, district, mobile_no, iscustomer, branch_id) VALUES ('$supplier_name','$address','$district','$mobile_no','$iscustomer','$branch_id_for_header_user')")
		or die(mysqli_error()); 
		
		$result_for_sup_tran = mysqli_query($connection,"SELECT supplier_id FROM supplier_setup WHERE supplier_name  = '$supplier_name' AND address  = '$address' AND branch_id = '$branch_id_for_header_user'");
		$test_for_sup_tran = mysqli_fetch_array($result_for_sup_tran);
				
				$supplier_id=$test_for_sup_tran['supplier_id'];
		
		mysqli_query($connection,"INSERT INTO transaction (date, supplier_id, ledger, credit, hint, payment_type, branch_id) VALUES ('2014-01-02','$supplier_id','1','0','supplier due','due','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		if($iscustomer == 'y') {
		
		
		mysqli_query($connection,"INSERT INTO customer_setup (customer_name, address, district, mobile_no, iscustomer, branch_id) VALUES ('$supplier_name','$address','$district','$mobile_no','$iscustomer','$branch_id_for_header_user')")
		or die(mysqli_error());
		
		
		$result_for_cus_tran = mysqli_query($connection,"SELECT customer_id FROM customer_setup WHERE customer_name  = '$supplier_name' AND address  = '$address' AND iscustomer = 'y' AND branch_id = '$branch_id_for_header_user'");
		$test_for_cus_tran = mysqli_fetch_array($result_for_cus_tran);
				
				$customer_id=$test_for_cus_tran['customer_id'];
		
		
		
		mysqli_query($connection,"INSERT INTO transaction (date, customer_id, ledger, credit, hint, payment_type, branch_id) VALUES ('2014-01-02','$customer_id','2','0','sale due permanent customer','due','$branch_id_for_header_user')")
		or die(mysqli_error()) ;
		
		
		
		}
		
		
		$errMsg="Supplier name inserted successfully";
	
}
}






 if(isset($_POST['update']))
    {
	
$supplier_id = $_POST['supplier_id'];
$supplier_name = $_POST['supplier_name'];
	$address = $_POST['address'];
	$district = $_POST['district'];
	$mobile_no = $_POST['mobile_no'];


mysqli_query($connection,"UPDATE supplier_setup SET supplier_name ='$supplier_name', address ='$address', district ='$district' 
, mobile_no ='$mobile_no' WHERE supplier_id = '$supplier_id' AND branch_id = '$branch_id_for_header_user'")
			or die(mysqli_error());  
	$errMsg="Supplier Successfully Updated";
	}
?>

        
        
        <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">supplier 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-success 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">Supplier Setup</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">
                                    <p class="help-block" style="color:#990000">If You Select 'Also Customer', This Supplier will be automatically included in
									Customer.</p>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Supplier Name</label>
                                            <input type="text" class="form-control" name="supplier_name" id="" placeholder="write supplier name">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Address</label>
                                            <input type="text" class="form-control" name="address" id="" placeholder="write address">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">District</label>
                                            <input type="text" class="form-control" name="district" id="" placeholder="write district name">
                                        </div>
                                        <div class="form-group">
                                            <label for="exampleInputEmail1">Mobile Number</label>
                                            <input type="text" class="form-control" name="mobile_no" id="" placeholder="write mobile number">
                                        </div>
                                       
                                         <div class="form-group">
                                            <label for="exampleInputEmail1">Also Customer</label>
                                            <select name="iscustomer" class="form-control">
                                            	<option value="n">No</option>
                                                <option value="y">Yes</option>
                                            </select>
                                           
                                        </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">Suppliers list with details</h3>
                                </div><!-- /.box-header -->
                                <div class="box-body table-responsive">
                                    <table id="example1" class="table table-bordered table-striped">
                                        <thead>
                                            <tr>
                                                <th>Supplier Name</th>
                                                <th>Address</th>
                                                <th>District</th>
                                                <th>Mobile Number</th>
                                                <th>Payment</th>
                                                <th>Due</th>
                                                <th>Apply</th>
                                            </tr>
                                        </thead>
                                        <tbody>
                                            
												<?php
                                                
                                                $query = "SELECT * FROM supplier_setup WHERE branch_id = '$branch_id_for_header_user'";
                                                $result = mysqli_query($connection,$query);
                                                
                                                $i=0;
                                                while($row=mysqli_fetch_array($result))
                                                {
                                                $i++;
                                                $supplier_setup_id = $row['supplier_id'];
                                                $supplier_name = $row['supplier_name'];
                                                $address = $row['address'];
                                                $district = $row['district'];
                                                $mobile_no = $row['mobile_no'];
												$due = $row['due'];
												
											
				
				$result_for_unit_type = mysqli_query($connection,"SELECT SUM(cash+bank) as paid FROM supplier_payment WHERE supplier_id  = '$supplier_setup_id'");
		$test_for_unit_type = mysqli_fetch_array($result_for_unit_type);
				
				$paid=$test_for_unit_type['paid'];
				
				
                                           
                                                echo "<tr>";
                                                echo "<td>$supplier_name
										<input type='hidden' id='supplier_name$i' value='$supplier_name'/></td>";
                                                echo "<td>$address
										<input type='hidden' id='address$i' value='$address'/></td>";
                                                echo "<td>$district
										<input type='hidden' id='district$i' value='$district'/></td>";
                                                echo "<td>$mobile_no
										<input type='hidden' id='mobile_no$i' value='$mobile_no'/></td>";
                                                echo "<td>$paid
										<input type='hidden' id='paid$i' value='$paid'/></td>";
                                                echo "<td>$due
										<input type='hidden' id='due$i' value='$due'/></td>";
                                                echo "<td>
                                       <input type='hidden' id='zipsearch$i' value='$supplier_setup_id'/><button type='button' class='btn btn-info' data-toggle='modal' data-target='#myModal' onClick='sync$i()'><i class='fa fa-pencil-square-o'></i></button>
												
												</td>";
                                                echo "</tr>";
												
												
					echo "<script>
function sync$i()
{
  var collect = document.getElementById('zipsearch$i');
  var entry = document.getElementById('xpaytype');
  
  
  var supplier_namecollect = document.getElementById('supplier_name$i');
  var supplier_nameentry = document.getElementById('supplier_name');
  
  
  var addresscollect = document.getElementById('address$i');
  var addressentry = document.getElementById('address');
  
  
  var districtcollect = document.getElementById('district$i');
  var districtentry = document.getElementById('district');
  
  
  var mobile_nocollect = document.getElementById('mobile_no$i');
  var mobile_noentry = document.getElementById('mobile_no');
  
  entry.value = collect.value;
  supplier_nameentry.value = supplier_namecollect.value;
  addressentry.value = addresscollect.value;
  districtentry.value = districtcollect.value;
  mobile_noentry.value = mobile_nocollect.value;
  
}
</script> ";
												
                                                }
                                                
                                                ?>
                                         
                                        </tbody>
                                        <tfoot>
                                            <tr>
                                                <th>Supplier Name</th>
                                                <th>Address</th>
                                                <th>District</th>
                                                <th>Mobile Number</th>
                                                <th>Payment</th>
                                                <th>Due</th>
                                                <th>Apply</th>
                                            </tr>
                                        </tfoot>
                                    </table>
                                </div><!-- /.box-body -->
                            </div>
                         
                         </div>
                     
                     </div>
                 
                  <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'>Update Supplier</h4>
      </div>
      <div class='modal-body'>
      <input type="hidden" id='xpaytype' name='supplier_id' class='form-control'/>
        
                    <div class="form-group">
                        <input type="text" id='supplier_name' name="supplier_name" class="form-control" placeholder="Movie Name"/>
                    </div>
                    <div class="form-group">
                        <input type="text" id='address' name="address" class="form-control" placeholder="address"/>
                    </div>
                    <div class="form-group">
                        <input type="text" id='district' name="district" class="form-control" placeholder="district"/>
                    </div>
                    <div class="form-group">
                 	
                     <input type="text" id='mobile_no' name="mobile_no" class="form-control" placeholder="district"/>
                        
                    </div>
                   
      </div>
      <div class='modal-footer'>
        <button type='button' class='btn btn-default' data-dismiss='modal'>Close</button>
        <button type='submit'  name="update" class='btn btn-primary'>Save</button>
      </div>
    </div>
  </div>
</div>
</form>
                 
                 
                 </section>
                </aside>
                <!-- /.right-side -->
        </div><!-- ./wrapper -->

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


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