/** * Used to enhance the provided timesheet with data from other tables. * * @param obj The timesheet object to enhance. * * @return Returns the enhanced timesheet. */ public function enhanceTimesheet($obj) { // Set the other timesheet information. if (is_numeric($obj->id)) { // Set the bills in the timesheet. $billDao = new BillDao(); $obj->bills = $billDao->getForTimesheet($obj->id, $obj->employee_id); // Set the employee. $employeeDao = new EmployeeDao(); $obj->employee = $employeeDao->get($obj->employee_id); // Set the audit log info. $auditLogDao = new AuditLogDao(); $obj->audit_log = $auditLogDao->getForTimesheet($obj->id); // Set the pay period. $payPeriodDao = new PayPeriodDao(); $obj->pay_period = $payPeriodDao->get($obj->pp_start); // Set the employee's contracts. $contractDao = new ContractDao(); $obj->contracts = $contractDao->getEmployeeContractsForPayPeriod($obj->employee_id, $obj->pay_period); // Set the holidays. $holidayDao = new HolidayDao(); $obj->holidays = $holidayDao->getForPayPeriod($obj->pay_period); } // Perform post-processing. $this->postProcess($obj); // Return the enhanced timesheet object. return $obj; }
/** * 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.")); } } } } }