print "  Found timesheet for employee.\n";
 // If the timesheet has been completed, then we don't need
 // to do anything.
 if ($timesheet->completed == "1") {
     // Audit log that we reviewed this timesheet.
     $auditLogDao->add(array('timesheet_id' => $timesheet->id, 'log' => "Daily timesheet review: " . "Timesheet has been completed."));
     print "  Timesheet already completed.\n";
     // Continue to the next timesheet.
     continue;
 }
 // Determine whether the timesheet contains
 // non-administrative contracts that haven't expired.
 print "  Checking administrative status of contracts...\n";
 $allAdminContracts = true;
 foreach ($timesheet->contracts as $contract) {
     $allAdminContracts &= $contract->admin == "1" || TimesheetHelper::isExpired($timesheet, $dayTime, $contract->id);
 }
 print "  Contracts are all admin or expired? " . ($allAdminContracts ? "Yes" : "No") . "\n";
 // Check to see if this is a billable employee.
 if (!$allAdminContracts) {
     // This will keep track of the last time an employee
     // billed hours in this timesheet.
     $lastBill = 0;
     // Make sure some bills are available for this timesheet.
     if (isset($timesheet->bills) && count($timesheet->bills) > 0) {
         // Iterate over the bills to find the most recent
         // time for which hours were added.
         foreach ($timesheet->bills as $bill) {
             // Retrieve the time for this bill.
             $time = strtotime($bill->day . " -0000");
             // Update the last bill time.
 /**
  * Save updated timesheet data into the database and mark the timesheet
  * as being completed.
  */
 private function saveTimesheetData($timesheet, $data)
 {
     // Make sure the timesheet and data are both valid.
     if (!isset($timesheet) || !isset($timesheet->id)) {
         return;
     }
     // Get the AuditLogDao instance used to log timesheet changes.
     $auditLogDao = new AuditLogDao();
     // Get the BillDao instance used to save timesheet hour changes.
     $billDao = new BillDao();
     // Get the employee id.
     $empId = $timesheet->employee->id;
     // $data looks like this: "8_57:20100602:5.00;8_56:20100605:8.00"
     // Explode the provided data based on semi-colon.
     $explodedData = explode(';', $data);
     // This will hold all the processed hours.
     $processed = array();
     // Iterate over the hourly parts.
     foreach ($explodedData as $dailyPart) {
         // Handle an empty data value.
         if (!$dailyPart) {
             continue;
         }
         // Explode the daily part into its pieces.
         $pieces = explode(':', $dailyPart, 4);
         // Convert the date into the 'Y-m-d' format.
         $day = gmdate('Y-m-d', strtotime($pieces[1] . " -0000"));
         // Add this bill to the array of processed bills.
         $processed["{$pieces['0']}:{$day}"] = 1;
         // Parse the contract id and assignment id from piece 0.
         $ids = explode('_', $pieces[0], 2);
         // Get the contract for this bill.
         if (is_numeric($ids[1])) {
             $contract = TimesheetHelper::getContractFromAssignment($timesheet, $ids[1]);
         } else {
             $contract = TimesheetHelper::getContract($timesheet, $ids[0]);
         }
         // Make sure the contract is valid.
         if (!isset($contract)) {
             continue;
         }
         // Get the existing timesheet bill.
         if (is_numeric($ids[1])) {
             $bill = TimesheetHelper::getBillFromAssignment($timesheet, $ids[1], $day);
         } else {
             $bill = TimesheetHelper::getBill($timesheet, $ids[0], $day);
         }
         // Get the new hours as a float.
         $newHours = 0.0 + $pieces[2];
         // Check to see if the hours changed.
         if (isset($bill) && $newHours != $bill->hours) {
             // Update the bill.
             $billDao->save($bill->id, array('hours' => $newHours, 'employee_id' => $empId, 'timestamp' => null));
             // Build the log message.
             $log = "Hours for contract {$contract->description} " . (isset($contract->labor_cat) ? "(LCAT: {$contract->labor_cat}) " : "") . "on {$day} changed from {$bill->hours} to {$newHours}." . (isset($pieces[3]) ? " The user-specified reason: {$pieces['3']}" : "");
             // Add an audit log for this change in hours.
             $auditLogDao->add(array('timesheet_id' => $timesheet->id, 'log' => $log));
         } else {
             if (!isset($bill)) {
                 // Create the bill.
                 $billDao->add(array('assignment_id' => is_numeric($ids[1]) ? $ids[1] : null, 'contract_id' => $contract->contract_id, 'employee_id' => $empId, 'day' => $day, 'hours' => $newHours));
                 // Add an audit log for this addition of hours.
                 $auditLogDao->add(array('timesheet_id' => $timesheet->id, 'log' => "Added {$newHours} hours for contract {$contract->description} " . (isset($contract->labor_cat) ? "(LCAT: {$contract->labor_cat}) " : "") . "on {$day}."));
             }
         }
     }
     // Make sure the timesheet and it's bills are valid.
     if (isset($timesheet) && isset($timesheet->bills) && count($timesheet->bills) > 0) {
         // Iterate over the bills in the timesheet.
         foreach ($timesheet->bills as $bill) {
             $id = $bill->contract_id . "_" . (isset($bill->assignment_id) ? $bill->assignment_id : "");
             // Check to see if this bill was processed.
             if (!isset($processed["{$id}:{$bill->day}"])) {
                 // This bill needs to be deleted since we didn't see it
                 // in the incoming data.
                 $billDao->remove(array($bill->id));
                 // Get the contract for this bill.
                 if (isset($bill->assignment_id)) {
                     $contract = TimesheetHelper::getContractFromAssignment($timesheet, $bill->assignment_id);
                 } else {
                     $contract = TimesheetHelper::getContract($timesheet, $bill->contract_id);
                 }
                 // Make sure the contract was found.
                 if (isset($contract)) {
                     // Add an audit log for this change in hours.
                     $auditLogDao->add(array('timesheet_id' => $timesheet->id, 'log' => "Hours for contract {$contract->description} " . (isset($contract->labor_cat) ? "(LCAT: {$contract->labor_cat}) " : "") . "on {$bill->day} changed from {$bill->hours} to 0.00."));
                 }
             }
         }
     }
 }