<?php
	include("my_functions.php");
	$pagetitle = "Maultiple payment form";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');
	
	$err= "";

    $actual_link = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		 
		
    $fromdate = false;
    $command = false;
	
	$startdatevalid = false;
	
    function bar_code(){
        $code =  time().rand(10,99);
	    $check = intval(DB::queryFirstField('SELECT id  FROM job_receipt where receipt_number ="'.$code.'"' )); 
		if($check > 1){
			bar_code();
		}else return $code;
			
}

    function bar_code_job(){
        $code =  time().rand(10,99);
	    $check = intval(DB::queryFirstField('SELECT id  FROM job_sale_item where local_bar_code ="'.$code.'"' )); 
		if($check > 1){
			bar_code();
		}else return $code;
			
}
 
		 
        if(   isset($_POST["cusbalance"])   ){
           

          
           
            if(!empty($_SESSION['multiplejobs'])){
                
            
          
                 $get_data = $_SESSION['multiplejobs'];
                 
                 
                $location =  $get_data['location'];
                $client =  $get_data['client'];
                $pickup = $get_data['pickup'];
                $memo = $get_data['memo'];
                $start = $get_data['start'];
                $machines = $get_data['machines'];
                $bill = $_POST['due'];
                $receiptmode = $_POST['receiptmode'];
				
                $total = 0;
                $parent_bar_code = bar_code();
                $paid = $_POST['paid'];
                $saletype = 1;
                $customer_claim = 0;
                 
                $customer_name  = getcustomer_name($client );
                
                
               

               
                        if(!empty($machines)){    
                        foreach($machines as $row){

                            $device_name = $row['machine'];
                            $type  = $row['type'];
                            $owner = $row['owner'];
							
                            $tech  = $attendant = $_SESSION["connecteduserid"];  
							$taskmemo = $row['jobmemo'];
                            $specifics = $row['jobs'];
                            $job_bar_code = bar_code_job();
                            $customerchange = $paid - $due;

                            $receipt_id = addnewJob($customer_name , $bill, $paid, $receiptmode, 
                            $customerchange, $memo, $attendant, $tech, $client, 
                        $customer_claim, $saletype, $type, $owner, $location, $pickup, 
                            $parent_bar_code , $device_name , $taskmemo, $job_bar_code
                                                );
                            
                            

                                if(!empty($specifics)){
                                    $sub_total = 0;
                                        foreach($specifics as $s){
                                            $amount =   get_job_amount($s);
                                            $job_id = $s;
                                            //job_sale_item

                                            add_job_activities(
                                                $receipt_id, $job_id, $amount,   $parent_bar_code, $job_bar_code  
                                            
                                            );

                                        




                                            }
                                    }
                            }
                        }

                        ?>
						<script> 
							window.location =  "<?= $base_url?>jobsearch";
						</script>
                        <?php

                        //jobsearch

            }
            
			
			 
			
			 
			 
		 
	}  
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				<!-- Content Header (Page header) -->
				 

				<!-- 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=="" )
									{
								?>
								   

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

                                 <div class="card-body">

                                 <?php if(!empty($_SESSION['multiplejobs'])){
                                    //print_r($_SESSION['multiplejobs']);

                                    $get_data = $_SESSION['multiplejobs'];
                                    $location = joblocation($get_data['location']);
                                    $client = customer_detailz($get_data['client']);
                                    $pickup = $get_data['pickup'];
                                    $memo = $get_data['memo'];

                                    $start = $get_data['start'];
                                    $machines = $get_data['machines'];

                                    ?>
                                    <table class="table table-bordered">
                                        <tr>   <td> Customer </td><td> <?= $client;?> </td> <td> Job site</td><td> <?=  $location;?> </td> </tr>
                                        <tr>   <td> Pick up date </td><td> <?= $pickup;?> </td> <td> Memo</td><td> <?=  $memo;?> </td> </tr>

                                        <?php 
                                        $mcndata = '';
                                        $total = 0;
                                        if(!empty($machines)){
                                            $mcndata .= '<tr> <td colspan="4"><table class="table table-bordered">';
                                            
                                        foreach($machines as $row){
                                            $mcndata .= ' <tr> <td width="50%"> Item Name:  '.$row['machine'].'</td> <td> Type: '. get_repair_type($row['type']).'</td>   </tr>';
                                            $mcndata .= ' <tr> <td width="50%"> Owner:  '. get_repair_owner($row['owner']).'</td>
											<td> Memo: '. $row['jobmemo'].'</td>  </tr>';

                                            $specifics = $row['jobs'];
                                            
                                            

                                            if(!empty($specifics)){
                                                $mcndata .= ' <tr> <td colspan="4"> <table class="table table-bordered">
                                                <tr> <td width="50%"> Name</td> <td width="25%"> Amount</td><td> Total</td> </tr>
                                                ';
                                                $sub_total = 0;
                                                foreach($specifics as $s){
                                                    $sub_total +=  get_job_amount($s);
                                                    $mcndata .= ' <tr>  <td>'.get_job_name($s).' </td><td>'.get_job_amount($s).' </td> <td> </td> </tr>';


                                                }
                                                $mcndata .= ' <tr>  <td>  </td><td>  </td> <td>'.$sub_total.' </td> </tr>';

                                                $mcndata .= ' </table></td></tr> ';

                                                $total +=  $sub_total;
                                            }
 
                                          }
                                          
                                        }
                                        //$mcndata .= ' <tr>  <td>  </td><td>  </td> <td width="25%">'.$total.' </td> </tr>';
                                        $mcndata .= '</table>';

                                        echo $mcndata;
                                        
                                        ?>

                                        <tr>  <td colspan='2'>  </td><td> <label>Total  </label></td> <td width="25%"> 

                                        <label class="col-md-6">Amount Due</label><i class="col-md-6" id="cost"><b><?= number_format($total, 2) ?> UGX</b></i>
                                    <input type='hidden' value="<?= $total?>" name='due' id ='due'/>
                                    <input type='hidden'  name ='cusbalance'  id ='balancex'/>
                                    <input type='hidden'  name ='paid'  id ='paid'/>
 
                                            
                                     </td> </tr>

                                     <tr>  <td colspan='2'>  </td><td> Paid Amount </td> <td width="25%">  

                                                                <div class="form-group col-md-12">
                                                                
                                                                <input type="text" name="cashpaidin" value='0' id="cashpaid"  maxlength="100" class="form-control"   onkeyup="return balance()">
                                                                </div> 
																
																	<div class="form-group col-md-12">
																<label for="receiptmode">Payment Type</label>
																<select  class="form-control" name="receiptmode"  >
																	<?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 -->

                                                                
                                                                    
                                                                </td> </tr>

                                                                <tr>  <td colspan='2'>  </td><td> <label class="col-md-6">Customer Change</label> </td> <td width="25%">  

                                                                         

                                                                        <i class="col-md-6" id="clientbalance"><b>0 UGX</b></i>

                                                                        <br /><br />

                                                                        <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>
                                                                            
                                                                        </td> </tr>



                                    </table>
                                    

                                    

                                    <?PHP
                                 }
                                
                                 


                  
                             ?>

                                

                         
                            

                                    </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')?> 
 <script src="<?php echo $base_url; ?>plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="<?php echo $base_url; ?>plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="<?php echo $base_url; ?>plugins/bert/jobcard.js" type="text/javascript"></script> 
        <script>
 
$("#cashpaid").priceFormat({
		prefix: "",
		thousandsSeparator: ",",
		centsLimit: 0,
		clearOnEmpty: true
	});  

    function balance(){
        var due = parseInt($('#due').val());
        var paid = parseInt($('#cashpaid').val().replace(/[^\d.]+/g, '')); 
        $('#balancex').val( paid -due);
        $('#paid').val( paid);

        $("#clientbalance").html('<b>'+ ( paid -due).toLocaleString() +' UGX</b>');
    }

            </script>

        

        

         
        
	</body>
</html>