Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- class Timesheet_Controller extends Base_Controller {
- public $restful = true;
- public function post_edit($id) {
- if($this->currentUser->can('update_timesheet')) {
- $input = Input::get();
- $rules = array(
- 'job_id' => 'required|exists:jobs,id',
- 'employee_id' => 'required|exists:employees,id',
- 'start_time' => 'required',
- 'end_time' => 'required_with:start_time',
- 'date' => 'required'
- );
- $validation = Validator::make($input, $rules);
- if(!Empty($input['ajax'])) {
- $ajax = $input['ajax'];
- unset($input['ajax']);
- } else {
- $ajax = null;
- unset($input['ajax']);
- }
- if($validation->passes()) {
- $start_time = date('Y-m-d H:i:s', strtotime($input['date'].' '.$input['start_time']));
- //echo 'start_time: '.$start_time;
- $end_time = date('Y-m-d H:i:s', strtotime($input['date'].' '.$input['end_time']));
- if($input['end_time'] == '00:00') {
- $tempDate = date_create($input['date']);
- date_add($tempDate, date_interval_create_from_date_string('1 day'));
- $end_time = date('Y-m-d H:i:s', strtotime(date_format($tempDate, 'Y-m-d').' '.$input['end_time']));
- }
- $scopeTimeWorked = strtotime($end_time) - strtotime($start_time);
- $scopeHours = $scopeTimeWorked/3600;
- $scopeYear = date('Y', strtotime($input['date']));
- $scopeWeek = date('W', strtotime($input['date']));
- $scopeDayOfWeek = date('w', strtotime($input['date']));
- $scopeTimesheets = array();
- $timesheets = Timesheet::where('employee_id', '=', $input['employee_id'])->get();
- if(!Empty($timesheets)) {
- foreach($timesheets as $timesheet) {
- $thisYear = date('Y', strtotime($timesheet->date));
- $thisWeek = date('W', strtotime($timesheet->date));
- $thisDayOfWeek = date('w', strtotime($timesheet->date));
- $thisID = $timesheet->id;
- // Exclude the timesheet we're editing from the current hour calculations
- if($thisID != $id && $thisID < $id) {
- if($thisYear == $scopeYear && $thisWeek == $scopeWeek && $thisDayOfWeek == $scopeDayOfWeek) {
- $scopeTimesheets[] = $timesheet;
- }
- }
- }
- if(!Empty($scopeTimesheets)) {
- $previousTotal = 0;
- foreach($scopeTimesheets as $timesheet) {
- $timeWorked = strtotime($timesheet->end_time) - strtotime($timesheet->start_time);
- $ot_start_time = $timesheet->ot_start_time;
- if(!Empty($ot_start_time)) {
- $otWorked = strtotime($timesheet->ot_end_time) - strtotime($timesheet->ot_start_time);
- } else {
- $otWorked = 0;
- }
- $hours = $timeWorked/3600;
- $otHours = $otWorked/3600;
- $previousTotal += $hours + $otHours;
- }
- if($previousTotal >= 8) {
- $input['ot_start_time'] = $input['start_time'];
- $input['ot_end_time'] = $input['end_time'];
- $input['start_time'] = '0000-00-00 00:00:00';
- $input['end_time'] = '0000-00-00 00:00:00';
- } else if(($previousTotal + $scopeHours) >= 8) {
- $newTotal = $previousTotal + $scopeHours;
- $overtimeHours = $newTotal - 8;
- $regularHours = $scopeHours - $overtimeHours;
- $input['start_time'] = $start_time;
- $input['ot_end_time'] = $end_time;
- $tempDate = date_create($input['ot_end_time']);
- $intervalParts = explode('.', $overtimeHours);
- $intervalHours = $intervalParts[0];
- if(!Empty($intervalParts[1])) {
- $intervalMinutes = $intervalParts[1];
- $intervalMinutes = '.'.$intervalMinutes;
- $intervalMinutes *= 60;
- $intervalMinutes = number_format($intervalMinutes, '0', '', '');
- } else {
- $intervalMinutes = 0;
- }
- date_sub($tempDate, date_interval_create_from_date_string($intervalHours.' hours + '.$intervalMinutes.' minutes'));
- $input['end_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].'-'.$overtimeHours.' hours'));
- $input['ot_start_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].'-'.$overtimeHours.' hours'));
- } else {
- $input['start_time'] = $start_time;
- $input['end_time'] = $end_time;
- $input['ot_start_time'] = NULL;
- $input['ot_end_time'] = NULL;
- }
- } else {
- $timeWorked = strtotime($end_time) - strtotime($start_time);
- $hours = $timeWorked/3600;
- if($hours > 8) {
- $otHours = $hours - 8;
- $input['start_time'] = $start_time;
- $input['ot_end_time'] = $end_time;
- $tempDate = date_create($input['ot_end_time']);
- $intervalParts = explode('.', $otHours);
- $intervalHours = $intervalParts[0];
- if(!Empty($intervalParts[1])) {
- $intervalMinutes = $intervalParts[1];
- $intervalMinutes = '.'.$intervalMinutes;
- $intervalMinutes *= 60;
- $intervalMinutes = number_format($intervalMinutes, '0', '', '');
- } else {
- $intervalMinutes = 0;
- }
- date_sub($tempDate, date_interval_create_from_date_string($intervalHours.' hours + '.$intervalMinutes.' minutes'));
- $input['end_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].' -'.$otHours.' hours'));
- $input['ot_start_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].' -'.$otHours.' hours'));
- } else {
- //echo 'start_time: '.$start_time;
- $input['start_time'] = $start_time;
- $input['end_time'] = $end_time;
- $input['ot_start_time'] = NULL;
- $input['ot_end_time'] = NULL;
- }
- }
- } else {
- $timeWorked = strtotime($end_time) - strtotime($start_time);
- $hours = $timeWorked/3600;
- if($hours > 8) {
- $otHours = $hours - 8;
- $input['start_time'] = $start_time;
- $input['ot_end_time'] = $end_time;
- $tempDate = date_create($input['ot_end_time']);
- $intervalParts = explode('.', $otHours);
- $intervalHours = $intervalParts[0];
- if(!Empty($intervalParts[1])) {
- $intervalMinutes = $intervalParts[1];
- $intervalMinutes = '.'.$intervalMinutes;
- $intervalMinutes *= 60;
- $intervalMinutes = number_format($intervalMinutes, '0', '', '');
- } else {
- $intervalMinutes = 0;
- }
- date_sub($tempDate, date_interval_create_from_date_string($intervalHours.' hours + '.$intervalMinutes.' minutes'));
- $input['end_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].' -'.$otHours.' hours'));
- $input['ot_start_time'] = date_format($tempDate, 'Y-m-d H:i:s');//date('Y-m-d H:i:s', strtotime($input['ot_end_time'].' -'.$otHours.' hours'));
- } else {
- $input['start_time'] = $start_time;
- $input['end_time'] = $end_time;
- $input['ot_start_time'] = NULL;
- $input['ot_end_time'] = NULL;
- }
- }
- $timesheet = Timesheet::find($id);
- //$timesheet->fill_raw($input);
- $timesheet->job_id = $input['job_id'];
- $timesheet->employee_id = $input['employee_id'];
- $timesheet->start_time = $input['start_time'];
- $timesheet->end_time = $input['end_time'];
- $timesheet->ot_start_time = $input['ot_start_time'];
- $timesheet->ot_end_time = $input['ot_end_time'];
- $timesheet->date = $input['date'];
- $timesheet->comments = $input['comments'];
- //echo 'final start: '.$timesheet->start_time;
- $timesheet->save();
- $employee = Employee::find($timesheet->employee_id);
- $hourly_rate = $employee->hourly_rate;
- if(isset($timesheet->ot_end_time) && !Empty($timesheet->ot_end_time) && $timesheet->ot_end_time != '') {
- $otWorked = strtotime($timesheet->ot_end_time) - strtotime($timesheet->ot_start_time);
- } else {
- $otWorked = 0;
- }
- if(isset($timesheet->end_time) && !Empty($timesheet->end_time) && $timesheet->end_time != '') {
- $hoursWorked = (strtotime($timesheet->end_time) - strtotime($timesheet->start_time))/3600;
- } else {
- $hoursWorked = 0;
- }
- $otHours = $otWorked/3600;
- $cost = round($hoursWorked * $hourly_rate, 2);
- $cost += round($otHours * ($hourly_rate * 1.5), 2);
- $employeeCost = EmployeeCost::find($timesheet->cost->id);
- $employeeCost->fill_raw(array(
- 'hours' => $hoursWorked,
- 'ot_hours' => $otHours,
- 'cost' => $cost
- ));
- $employeeCost->save();
- $costType = CostType::where('title', '=', 'employee')->first();
- $jobCost = $employeeCost->jobcost()->where('cost_type_id', '=', $costType->id)->first();
- $jobCost->job_id = $input['job_id'];
- $jobCost->save();
- $interaction = new Interaction(
- array(
- 'employee_id' => $this->currentUser->employee->id,
- 'table' => 'timesheets',
- 'row' => $id,
- 'data' => json_encode($timesheet->to_array())
- )
- );
- $interaction->save();
- $otherInteraction = new Interaction(
- array(
- 'employee_id' => $this->currentUser->employee->id,
- 'table' => 'employeecosts',
- 'row' => $employeeCost->id,
- 'data' => json_encode($employeeCost->to_array())
- )
- );
- $otherInteraction->save();
- $costInteraction = new Interaction(
- array(
- 'employee_id' => $this->currentUser->employee->id,
- 'table' => 'costs',
- 'row' => $jobCost->id,
- 'data' => json_encode($jobCost->to_array())
- )
- );
- $costInteraction->save();
- Input::clear();
- //echo 'other start: '.$timesheet->start_time;
- if(!Empty($ajax)) {
- echo 'success';
- exit();
- } else {
- return Redirect::to('timesheet/'.$id);
- }
- } else {
- $badInput = array();
- if($validation->errors->has('client_id')) { $badInput[] = 'client_id'; }
- if($validation->errors->has('employee_id')) { $badInput[] = 'employee_id'; }
- if($validation->errors->has('start_time')) { $badInput[] = 'start_time'; }
- if($validation->errors->has('end_time')) { $badInput[] = 'end_time'; }
- if($validation->errors->has('date')) { $badInput[] = 'date'; }
- if($validation->errors->has('comments')) { $badInput[] = 'comments'; }
- if(!Empty($ajax)) {
- echo json_encode($badInput);
- exit();
- } else {
- return Redirect::to('timesheet/'.$id.'/edit')->with_input('except', $badInput)->with_errors($validation);
- }
- }
- } else {
- Session::flash('status', "You don't have permission to access that page.");
- return Redirect::to('/');
- }
- }
- }
- ?>
Advertisement
Add Comment
Please, Sign In to add comment