123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354 |
- <?php
- class timesheetController extends Staple_Controller
- {
- public function _start()
- {
- }
- public function index($year = null, $month = null)
- {
- //Typecast variables
- $month = (int) $month;
- $year = (int) $year;
- //Build new insert form
- $form = new insertTimeForm();
- //Check for form submission
- if($form->wasSubmitted())
- {
- //Add submitted data to the form
- $form->addData($_POST);
- //Check form validation
- if($form->validate())
- {
- //Export form data into an array
- $data = $form->exportFormData();
- //Check if dates are within the current pay period.
- $startMonth = date('m',strtotime('last month'));
- if($startMonth == 1)
- {
- $startYear = date('Y',strtotime('last year'));
- }
- else
- {
- $startYear = date('Y');
- }
- $endMonth = date('m');
- $endYear = date('Y');
- $startDate= strtotime($startMonth.'/26/'.$startYear);
- $endDate = strtotime($endMonth.'/25/'.$endYear);
- $userDate = strtotime($data['date']);
- //Date is within pay period
- if($userDate >= $startDate && $userDate <= $endDate)
- {
- //Compare in Times and out Times.
- if(strtotime($data['inTime']) < strtotime($data['outTime']))
- {
- //Create a new entry object and set properties
- $entry = new timeEntryModel();
- $entry->setDate($data['date']);
- $entry->setInTime($data['inTime']);
- $entry->setOutTime($data['outTime']);
- $entry->setLessTime($data['lessTime']);
- $entry->setCodeId($data['code']);
- //Save entry data to table.
- if($entry->save())
- {
- //Return a new time form with success message
- $form = new insertTimeForm();
- $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
- $this->view->insertTimeForm = $form;
- }
- else
- {
- //Return the same form with a warning message
- $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. Please add a new entry or edit an already existing one.";
- $form->errorMessage = array($message);
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return the same form with error message.
- $form->errorMessage = array("<b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return the same form with error message.
- $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return form with invalid data.
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return form
- $this->view->insertTimeForm = $form;
- }
- //Set year and month variables if undefined.
- if($year == null)
- {
- $date = new DateTime();
- $year = $date->format('Y');
- }
- if($month == null)
- {
- $date = new DateTime();
- $month = $date->format('m');
- }
- //Load timesheet for user.
- $timesheet = new timesheetModel($year,$month);
- //Pass timesheet object to view
- $this->view->timesheet = $timesheet;
- //Check for unvalidated entries
- $i = 0;
- foreach($timesheet->getEntries() as $entry)
- {
- if($entry->batchId == $timesheet->getBatch())
- {
- $i++;
- }
- }
- if($i > 0)
- {
- $this->view->needsValidation = true;
- }
- else
- {
- $this->view->needsValidation = false;
- }
- $changeYearForm = new changeYearForm();
- $this->view->changeYearForm = $changeYearForm;
- }
- public function remove($id)
- {
- if($id != null)
- {
- //Confirm entry for user
- $timesheet = new timesheetModel();
- if($timesheet->exists($id))
- {
- //Delete Item
- if($timesheet->remove($id))
- {
- $this->view->message = "Entry removed.";
- }
- else
- {
- $this->view->message = "ERROR: Could not remove entry.";
- }
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- public function edit($id = null)
- {
- if($id != null)
- {
- $entry = new timeEntryModel($id);
- $data['inTime'] = $entry->getInTime();
- $data['outTime'] = $entry->getOutTime();
- $data['date'] = $entry->getDate();
- $data['lessTime'] = $entry->getLessTime();
- $data['code'] = $entry->getCodeId();
- $form = new editTimeForm();
- $form->setAction($this->_link(array('timesheet','edit',$id)));
- $form->addData($data);
- //Check for form submission
- if($form->wasSubmitted())
- {
- //Add submitted data to the form
- $form->addData($_POST);
- //Check form validation
- if($form->validate())
- {
- //Export form data into an array
- $data = $form->exportFormData();
- //Check if dates are within the current pay period.
- $startMonth = date('m',strtotime('last month'));
- if($startMonth == 1)
- {
- $startYear = date('Y',strtotime('last year'));
- }
- else
- {
- $startYear = date('Y');
- }
- $endMonth = date('m');
- $endYear = date('Y');
- $startDate= strtotime($startMonth.'/26/'.$startYear);
- $endDate = strtotime($endMonth.'/25/'.$endYear);
- $userDate = strtotime($data['date']);
- //Date is within pay period
- if($userDate >= $startDate && $userDate <= $endDate)
- {
- //Compare in Times and out Times.
- if(strtotime($data['inTime']) < strtotime($data['outTime']))
- {
- //Create a new entry object and set properties
- $entry = new timeEntryModel();
- $entry->setId($id);
- $entry->setDate($data['date']);
- $entry->setInTime($data['inTime']);
- $entry->setOutTime($data['outTime']);
- $entry->setLessTime($data['lessTime']);
- $entry->setCodeId($data['code']);
- //Save entry data to table.
- if($entry->save())
- {
- //Return a new time form with success message
- $form->successMessage = array("<i class=\"fa fa-check\"></i> Entry saved for ".$data['date']."");
- $this->view->form = $form;
- }
- else
- {
- //Return the same form with a warning message
- $message = "<i class=\"fa fa-warning\"></i> Cannot insert overlapping time entries. If you are updating an already existing entry, remove that entry and submit a new one.";
- $form->errorMessage = array($message);
- $this->view->form = $form;
- }
- }
- else
- {
- //Return the same form with error message.
- $form->errorMessage = array("<i class='fa fa-warning'></i> <b>'Time In'</b> entry cannot be before <b>'Time Out'</b> entry.");
- $this->view->form = $form;
- }
- }
- else
- {
- //Return the same form with error message.
- $form->errorMessage = array("<i class='fa fa-warning'></i> You may only submit time for the current date period.");
- $this->view->form = $form;
- }
- }
- else
- {
- //Return form with invalid data.
- $this->view->form = $form;
- }
- }
- else
- {
- //Return form
- $this->view->form = $form;
- }
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- public function changeyear()
- {
- $form = new changeYearForm();
- if($form->wasSubmitted())
- {
- $form->addData($_POST);
- if($form->validate())
- {
- $data = $form->exportFormData();
- header("location: ".$this->_link(array('timesheet',$data['year']))."");
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- public function validate($year, $month)
- {
- $timesheet = new timesheetModel($year,$month);
- //Get Current Batch ID
- $auth = Staple_Auth::get();
- $user = new userModel($auth->getAuthId());
- $batchId = $user->getBatchId();
- //Check for unvalidated entries
- $i = 0;
- foreach($timesheet->getEntries() as $entry)
- {
- if($entry->batchId == $timesheet->getBatch())
- {
- $i++;
- }
- }
- if($i > 0)
- {
- $this->view->timesheet = $timesheet;
- $form = new validateTimeSheetForm();
- $form->setAction($this->_link(array('timesheet','validate',$timesheet->getCurrentYear(),$timesheet->getCurrentMonth())));
- if($form->wasSubmitted())
- {
- $timesheet->validate($batchId);
- header("location:".$this->_link(array('timesheet'))."");
- }
- else
- {
- $this->view->form = $form;
- $this->view->needsValidation = false;
- }
- }
- else
- {
- $this->view->needsValidation = false;
- $this->view->timesheet = array();
- }
- }
- }
- ?>
|