<?php 

 
if(isset($_POST['customer_data'])){
 include("my_functions.php");
 $get_data =  $_POST['customer_data'];
 $thecode = $get_data['cleancode'];
  $cleancode =  intval(check_code(intval($thecode)));

 
 if($cleancode > 0){

    echo ' <h3 class="alert alert-warning"> Error, details about this job are allready captured </h3> <div class="row"> 
    <div class="col-md-6"><a href="'. $base_url.'jobsearch/today/0" class="btn btn-block   btn-sm btn-info"  > See Today\'s Jobs</a>
    </div>
    
    <div class="col-md-6"><a href="'. $base_url.'multiple/" class="btn btn-block   btn-sm btn-dark"  > Add a new job</a>
    </div>
    </div>
    ';

    exit();

 }
 service_code($thecode);
 
 $jobs     =  $_POST['jobs'];
    
    


    
                $location =  $get_data['location'];
                $client   =  intval($get_data['client']);
                $pickup   = date('Y-m-d', strtotime($get_data['pickup']));
                if(strtotime($get_data['pickup']) < time()){
					 $pickup   = Date('Y-m-d', strtotime('+3 days'));
				}
				
                $memo     = $get_data['memo'];
                $tech  = $attendant = $_SESSION["connecteduserid"];  
                $bill       = $get_data['bill'];
                $receiptmode = $get_data['receiptmode'];
				
                $total = 0;
                $parent_bar_code = bar_code();
                $paid            = $get_data['paid'];
                $owner            = $get_data['ownercat'];
                
                $saletype = 1;
                $customer_claim = 0;
                 
                $customer_name  = getcustomer_name($client );
                
                $customerchange =   $bill - $paid;
                if($paid > $bill){
                    $customerchange = 0;
                    $paid = $bill;

                }

                $themoney = 0;


    //$job_bar_code = bar_code_job();

    foreach($jobs as $job){ 
        $products     = $job['product'];
        $job_name =  $products['job'];
        $qty     =  $products['qty'];
        $kind    =  $products['jtp']; 
        $tasks       =  $job['tasks']; 

        
        
              $job_bar_code = bar_code_job();
             
       
        $receipt_id = addnewJob($customer_name , $bill, $paid, $receiptmode, 
                        $customerchange, $memo, $attendant, $tech, $client, 
                    $customer_claim, $saletype, $kind, $owner, $location, $pickup, 
                        $parent_bar_code , $job_name , $qty, $job_bar_code
                                            );

                                          



        for($i=1; $i <= $qty; $i++){
           
                                                

        foreach($tasks as $t){
            $job_id= $t['task'];
             $amount = $t['charged'];

             $themoney +=  $amount;
               
          add_job_activities(
                $receipt_id, $job_id, $amount,   $parent_bar_code, $job_bar_code  
            
            ); 
            

            
              
        }
    }

       
    }
    
    echo ' <h3> Success </h3> <div class="row"> 
    <div class="col-md-6"><a href="'. $base_url.'customreceipt/'.$parent_bar_code.'" class="btn btn-block   btn-sm btn-info" target="_blank"> Get receipt</a>
    </div>
    
    <div class="col-md-6"><a href="'. $base_url.'thebarcodes/'.$parent_bar_code.'" class="btn btn-block   btn-sm btn-dark" target="_blank"> Get Barcodes</a>
    </div>
    </div>
    ';

    
    

}