<?php
function updateaccountnumber($postdata,$xcrud){
	$ac = $postdata->get('account_number');
	$sysac = $postdata->get('sys_account_number');
	
	if (!isset($ac) OR $ac =='') {
	$postdata->set('account_number', $sysac);
	
	}


	
}
function addmaterials($postdata,$xcrud){
	
	
	$projectid = $postdata->get('projectid');
	
	
	 $db01 = Xcrud_db::get_instance();
	    $query01 = "SELECT id
					  
		          FROM  technician_worksheet
				  ORDER BY id DESC LIMIT 1
				  				  
				   ";
        
        $db01->query($query01);
        $outm011 = $db01->result();
        foreach($outm011 as $outputm011){
		
			if ($outputm011['id'] == '0'){
				
				$id = '1';
			}
			else {
				
				$id = $outputm011['id'] + 1;
				
			}
		}
	
	
	 $db0 = Xcrud_db::get_instance();
	    $query0 = "SELECT dispatchid
					  
		          FROM  stockrequest
				 WHERE jobno ='".$projectid."'";		  
				   
        
        $db0->query($query0);
        $outm0 = $db0->result();
        foreach($outm0 as $outputm0){
		
		     $db1 = Xcrud_db::get_instance();
	    $query1 = "SELECT stockid,qtydelivered
					  
		          FROM  stockrequestitems
				 WHERE dispatchid ='".$outputm0['dispatchid']."'";		  
				   
        
        $db1->query($query1);
        $outm1 = $db1->result();
			
			  foreach($outm as $outputm){
				  
				  	$db2 = Xcrud_db::get_instance();
			 $query11 = "INSERT into technician_worksheet_materials set  id = '".$id."',
			 									 stockrequest_id = '".$outputm0['dispatchid']."',
					  							 stockid= '".$outputm['stockid']."',
												 qty = '".$outputm['qty']."'
												
		          
				  				  
				   ";
        
        $db2->query($query11);
				  
				  
				 
			
			
			  }
			
			
			
			
			
			
        } 

	
}
function calculatetotalhours($postdata,$xcrud){
	
	$montime_in = $postdata->get('time_in');
	$montime_out = $postdata->get('time_out');
	
	
	
	
	
	$monhours = abs(strtotime($montime_in) - strtotime($montime_out)) / 3600;
		
    $postdata->set('hrs_worked', $monhours);
	
	  $postdata->set('billable_hrs', $monhours);
	
}