<?php
	include("my_functions.php");
	$pagetitle = "New Job Entry";
	require('includes/applicationtop.php');
	require('includes/applicationmenu.php');

    $receipt = 0;
   
    
?>
			<!-- Content Wrapper. Contains page content -->
			<div class="content-wrapper">
				 

				<!-- Main content -->
				<section class="content">
					<!-- Default card -->
					<?php
						if($_SESSION["connectedusergroup"] == 3 || $_SESSION["connectedusergroup"] < 5)
						{


                            if(isset($_POST['specifics'])){ 
                               

                                $storehere =array();
                                $specifics = $_POST['specifics'];
                                 $receipt = $receipt_id = $_POST['receipt'];
                                //$parent_bar_code =  $parent_bar_code;
                                //$job_bar_code    = $job_bar_code ;

                                $receipt_data =  getjob_by_id($receipt_id);

                                if(!empty( $receipt_data)){ 
                                                    foreach($receipt_data as $r){
                                                        //parent_bar_code
                                                        $parent_bar_code =  $r['parent_bar_code'];  
                                                        $job_bar_code      =  $r['receipt_number']; 
                                                        $bill     =  intval($r['bill']);

                                                       
                                                    


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

                                            $bill     += $amount;

                                            

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

                                            }

                                            
                                            $s = DB::query('UPDATE    job_receipt SET  amount = '.$bill.' WHERE   id ='.$receipt_id.''  );
	
                                    }
                                }


                                exit('New task has been added <button onclick="history.go(-2)">Go Back</button> ');
                                }
                                   
 
                                

                                    
                                
                                

                        
                                
                        
                        
                            }else{
                                
                                    $actual_link = 'https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
                                
                                    $parts = explode('addnewtask/' , $actual_link);
                                    if($parts[1] > 0){
                                          $receipt =  $parts[1] ;
										   $parts  = explode('/' , $receipt);
										   
										   $receipt = $parts[0];

                                    }
                            }
					?>
					<div class="card card-info">
                                <div class="card-header">
                                    <h6 class="card-title"><?= $pagetitle ?></h6>
                                </div>
                                <div class="card-body">

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

                                <?php
                                $priced = DB::query("SELECT * FROM repair_jobnames"); 
                                
                                ?>
                            <table class='table' id='maintable'>
                               

                                     

                                                <tr> 
                                                    <td colspan='4'> 

                                                    <label>Tasks to be performed on this jobxx</label> 
                                                    <input type="hidden" name="receipt" value ="<?= $receipt;?>" >

                                                    <?php
													echo $receipt;
                                                   $receipt_data =  getjob_by_id($receipt);
		 

                                                   if(!empty( $receipt_data)){ 
                                                    foreach($receipt_data as $r){
                                                        //parent_bar_code
                                                       $bar_code =  $r['parent_bar_code'];
                                                       $sno      =  $r['receipt_number'];
                                                       $bill     =  $r['bill'];
                                                    }


                                                    ?>
                                                    <?php } 
													
												 
													
													?>
													 
                                                        <select class="select2bs4 form-control" name='specifics[]'  required multiple="multiple">
														
                                                        
                                                        <?php
                                                                   
                                                                    foreach($priced as $row)
                                                                    {
                                                                    
                                                                        ?>
                                                                        <option  value="<?php echo $row['job_id']; ?>"><?php echo $row['job_name']; ?></option>
                                                                        <?php
                                                                
                                                                }
                                                                ?>
                                                        
                                                        </select>

                                                        <br /><br />
                                                        


                                                    </td>
                                                </tr>
                                            </table>
                                        </div>
                                     </div>

                                    

                                     

                                        </td>

                                     </tr>
                                     
									
									<div class="btn-list col-md-12 text-center">
										<button onclick="cancelsale()" class="btn btn-danger"><i class="fa fa-remove"></i> Cancel Sale</button>
										
										<button onclick="processcashsale()" class="btn btn-success"><i class="fa fa-floppy-disk"></i> Process Sale</button>
									</div>
                                         </td>
                                         </tr>
                        </table>

                        </form>
							 
						</div>
					</div>
				
					 	
						 
						 
					<?php
						}
						else
						{
					?>
					<div class="alert alert-danger alert-dismissable">
						<i class="fa fa-ban-circle"></i>
						Limited Access
					</div>
					<?php
						}
					?>
				</section><!-- /.content -->
			</div><!-- /.content-wrapper -->

			 
		</div><!-- ./wrapper -->
		
		 
		
	<?php include ('includes/general_footer.php')?>

    <link href="https://adminlte.io/themes/v3/plugins/select2-bootstrap4-theme/select2-bootstrap4.min.css" rel="stylesheet">
 <link href="https://adminlte.io/themes/v3/plugins/select2/css/select2.min.css" rel="stylesheet"> 
		
		<script src="plugins/bert/jquery.price_format.min.js" type="text/javascript"></script>
		<script src="plugins/bert/storepos.js" type="text/javascript"></script>
		<script src="plugins/bert/jobcard.js" type="text/javascript"></script> 

        
        <script src="https://adminlte.io/themes/v3/plugins/select2/js/select2.full.min.js" type="text/javascript"></script> 

        <script>
$(document).ready(function(){
    var k =  parseInt($('#counter').val());
  $("#btn1").click(function(){
    
    k = k+1;

    $.ajax({
		type: "POST",
		url: "dynamictable.php",
		data:'lastrow='+k,
		beforeSend: function(){
			 
			//$("#clientname").css("background","#FF0");
		},
		success: function(data){ 
			 
            $('#playground').append(data);

            
    $('#counter').val(k);
			  
		}
		});
	});
   
 
  
});
</script>
        <script>
  $(function () {
   
    //Initialize Select2 Elements
    $('.select2').select2()

    //Initialize Select2 Elements
    $('.select2bs4').select2({
      theme: 'bootstrap4'
    })

   
     
     

     

  
 
 
    $("input[data-bootstrap-switch]").each(function(){
      $(this).bootstrapSwitch('state', $(this).prop('checked'));
    })

  })
   
   
   
  // DropzoneJS Demo Code End
</script>
        
	<script>
  $(function () {
   

   
	$('#change_display').hide();

	$("#receiptmode").change(function()
    {
		//$('#change_display').hide();
		var id=$(this).val();
		
		var totalbill = 0.00;
				
			$("#cartitemstable").children("tbody").children("tr").each(function(i) 
			{
				$tr = $(this);
				var useindex = 0;

				$("td", $tr).each(function() 
				{
					 
					
					if(useindex == 4)
					{
						totalbill += parseFloat($(this).text().replace(/[^0-9\.]/g,""));
					}
					
					useindex +=1;
				});
			});

		 
         
         return false;
    });
 
  })
  
  // AJAX call for autocomplete 
$(document).ready(function(){
	$("#search-card").css("background","#ffe6e6");
	
	$("#search-card").keyup(function(){
		 
		$.ajax({
		type: "POST",
		url: "get_customers.php",
		data:'keyword='+$(this).val(),
		beforeSend: function(){
			 
			$("#clientname").css("background","#FF0");
		},
		success: function(data){ 
			$("#clientname").html(data); 
			 
			$("#clientname").css("background","#ffcccc");
			$("#search-card").css("background","#ffffff");
		}
		});
	});
	
	//search for items here
	
	$("#itemsearchtext").keyup(function(){
		 
		$.ajax({
		type: "POST",
		url: "searchjobs.php",
		data:'itemsearchtext='+$(this).val(),
		beforeSend: function(){
			 
			//$("#clientname").css("background","#FF0");
		},
		success: function(data){ 
			$("#searchresulthere").html(data); 
			  
		}
		});
	});
});
//To select country name
 
</script>
	</body>
</html>