<?php
	include("my_functions.php");
	$pagetitle = "Pay job balance";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$err= "";

    $actual_link = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$parts = explode('dothetopups' , $actual_link);
		
    $fromdate = false;
    $command = false;
	
	$startdatevalid = false;
	
	 
		 
        if(!empty($_POST["dispatch"]) and !empty($_POST["focus"])   ){

            $bar_code = $_POST['focus'];
            $amount = intval($_POST["dispatch"]);
            $status = intval($_POST["status"]); 			
			
            $job_balance =  intval(jobbalance($bar_code  )) + $amount ;

             
            DB::startTransaction();
					DB::query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
					DB::update
					('job_receipt', 
						array
						(
							 
                            'amount' => $job_balance,
                            'updated_date' => date('Y-m-d'),
							'order_status' => $status
                        ),"parent_bar_code=%i", $bar_code 
					);
					
					DB::commit();

                    DB::query("SET SESSION TRANSACTION ISOLATION LEVEL SERIALIZABLE");
					DB::insert
					('topups', 
						array
						(
							 
                            'topup' => $amount ,
                            'top_bar_code' => $bar_code,
							'branch'=>job_branch($bar_code),
							'pmode'=>$_POST['topmode'],
							'enteredby' =>$_SESSION["connecteduserid"]
                            
                        )
					);

                    DB::commit();
 
                    
                    
                    ?>
						<script>
							window.location = "<?= $base_url?>notcleared";
						</script>
                        <?php
			
			 
			
			 
			 
		 
	}else{

        $end = $parts[1];
		 $parts = explode('/' , $end);

         $command =  $parts[1];

       
        

        
								 
								  
 
    }
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				 

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<div class="card card-primary">
						<?php
							if($_SESSION["connectedusergroup"] == 1 || $_SESSION["connectedusergroup"] == 4)
							{
						?>
						<div class="card-body">
							<div id="servererrormessage">
							</div>
							
							<?php
								if($err!="")
								{
							?>
							<div class="alert alert-danger alert-dismissable">
								<i class="fa fa-ban"></i>
								<button type="button" class="close" data-dismiss="alert" aria-hidden="true">&times;</button><br>
								<?php 
									echo $err; 
								?>
							</div>
							<?php
								}
							?>
							
							<div class="card card-info">
                                <div class="card-header">
                                    <h6 class="card-title"><?= $pagetitle ?></h6>
                                </div>
                                <div class="card-body">
                            
                               
							
							 
								<?php
									if($err=="" )
									{

                                        //echo $command;

                                        

                                         $topay = topay($command   );
								?>
                                <h5> Amount due <?= $topay ?>  </h5>
								   

                                 <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
 
                                         
                                         <!-- form-group -->

                                         <input type="hidden" name="focus" id="name" maxlength="40" class="form-control" value="<?php echo $command; ?>" >
                                      

                                         <div class="form-group">
										      <label for="firstname"> Enter Amount </label>
                                                <input type="number" name="dispatch"     class="form-control"  >
                                            </div> 
											
											 <div class="form-group">
										      <label for="firstname"> Enter Amount </label>
                                                
												<select  class="form-control" name="topmode" id="topmode">
													<?php
														$paymentmodedata = DB::query("SELECT id, payment_name FROM payment_type");
														
														foreach ($paymentmodedata as $paymentmode)
														{
													?>
													<option value="<?php echo $paymentmode['id']; ?>"><?php echo $paymentmode['payment_name']; ?></option>
													<?php
														}
													?>
												</select>
											</div><!-- form-group -->
											
											
											 <div class="form-group">
										      <label for="firstname"> Dispatch </label>
                                                
													<select  class="form-control" name="status" id="employee_role" data-placeholder="Choose One">
													<option  value="5">Waiting</option>
													<option  value="3">Dispatch</option>
													<option  value="6">Ready for pick up</option>
													 
													 
													</select>
													
													 
											</div><!-- form-group -->

                                            

                                            <div class="btn-list">
								<div class="text-right">
									<a href="dashboard.php" class="btn btn-danger"><i class="glyphicon glyphicon-remove"></i> Cancel</a>
									
									<button type="submit" name="savedatabtn" id="savedatabtn" class="btn btn-success"><i class="glyphicon glyphicon-floppy-disk"></i> Save</button>
								</div>
							</div>

                                    </form>
								  
								   
                                     

                                </div>
                            </div>
              <!-- /.card-body -->
            </div>
									 
								
								   
								 
										 
								
								
								
								</div>
							</div>
						</div><!-- /.card-body -->
						<?php
							}
							}
							else
							{
						?>
						<div class="alert alert-danger alert-dismissable">
							<i class="fa fa-ban-circle"></i><br>
							Limited Access
						</div>
						<?php
							}
						?>
					</div><!-- /.card -->
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->
 
		</div><!-- ./wrapper -->
	 
	
 <?php include ('includes/general_footer.php')?>  
 
 
	</body>
</html>