123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508 |
- <?php
- class timesheetController extends Staple_Controller
- {
- private $userId;
- private $accountLevel;
- public function _start()
- {
- $this->_setLayout('main');
- $auth = Staple_Auth::get();
- $user = new userModel();
- $user->userInfo($auth->getAuthId());
- $this->userId = $user->getId();
- $this->accountLevel = $user->getAuthLevel();
- }
- 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.
- $date = new DateTime();
- if($date->format('d') > 25)
- {
- $date->modify('first day of next month');
- $date->modify('+24 days');
- }
- $maxDate = $date->setDate($date->format('Y'),$date->format('m'),'25')->setTime(23,59,59)->getTimestamp();
- $minDate = $date->modify('-1 month +1 day')->setTime(0,0,0)->getTimestamp();
- $userDate = strtotime($data['date']);
- //Date is within pay period
- if($userDate >= $minDate && $userDate <= $maxDate)
- {
- //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("<i class='fa fa-warning'></i> You may only submit time for the current pay period.");
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return form with invalid data.
- $this->view->insertTimeForm = $form;
- }
- }
- else
- {
- //Return form
- $this->view->insertTimeForm = $form;
- }
- $date = new DateTime();
- if ($year == null)
- {
- if($date->format("m") == 12 && $date->format("d") >= 26)
- {
- $year = $date->modify('+1 year')->format('Y');
- }
- else
- {
- $year = date('Y');
- }
- }
- if ($month == null)
- {
- if($date->format("d") >= 26)
- {
- $month = $date->modify('first day of next month')->format('m');
- }
- else
- {
- $month = date('m');
- }
- }
- $date = new DateTime();
- $date->setDate($year,$month,26);
- $date->setTime(0,0,0);
- $this->view->year = $date->format('Y');
- $this->view->date = $date->format("F Y");
- $currentDate = new DateTime();
- if($currentDate->format('d') >= 26)
- {
- $currentDate->modify('+1 month');
- }
- $this->view->currentMonth = $currentDate->format('m');
- $this->view->currentYear = $currentDate->format('Y');
- $date->modify('+1 year');
- $this->view->nextYear = $date->format('Y');
- $date->modify('-2 year');
- $this->view->previousYear = $date->format('Y');
- $date->modify('+1 year');
- $month = $date->format('m');
- $this->view->month = $month;
- $date->modify('-1 month');
- $this->view->previousMonth = $date->format('m');
- $date->modify('+2 month');
- $this->view->nextMonth = $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;
- }
- }
- public function printpreview($id = null, $year = null, $month = null)
- {
- $this->_setLayout('print');
- //Set year and month variables if undefined.
- if($year == null)
- {
- $date = new DateTime();
- $year = $date->format('Y');
- }
- if($month == null)
- {
- $date = new DateTime();
- if($date->format("j") >= 26)
- {
- $month = $date->modify('+1 month')->format('m');
- }
- else
- {
- $month = $date->format('m');
- }
- }
- //Load timesheet for user.
- $timesheet = new timesheetModel($year,$month);
- $user = new userModel();
- $user->userInfo($this->userId);
- $this->view->firstName = $user->getFirstName();
- $this->view->lastName = $user->getLastName();
- $this->view->batchId = $user->getBatchId();
- //Pass timesheet object to view
- if($id == $this->userId)
- {
- $this->view->timesheet = $timesheet;
- }
- else
- {
- header("location: ".$this->_link(array('timesheet'))."");
- }
- }
- public function remove($id = null)
- {
- if($id != null)
- {
- //Confirm entry for user
- $timeEntry = new timeEntryModel($id);
- if($timeEntry->getId() !== NULL)
- {
- //Remove Entry
- if($timeEntry->remove($timeEntry->getId()))
- {
- $this->view->message = "<i class=\"fa fa-check\"></i> Removed successfully.";
- }
- else
- {
- $this->view->message = "ERROR: Cannot 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();
- $this->view->id = $entry->getId();
- $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.
- $date = new DateTime();
- if($date->format('d') > 25)
- {
- $date->modify('+1 month');
- }
- $maxDate = $date->setDate($date->format('Y'),$date->format('m'),25)->setTime(23,59,59)->getTimestamp();
- $minDate = $date->modify('-1 month +1 day')->setTime(0,0,0)->getTimestamp();
- $userDate = strtotime($data['date']);
- //Date is within pay period
- if($userDate >= $minDate && $userDate <= $maxDate)
- {
- //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> 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 within the current pay period.
- $i = 0;
- foreach($timesheet->getEntries() as $entry)
- {
- if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
- {
- 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())
- {
- if($entry->inTimeRaw >= $timesheet->getStartDateTimeString() && $entry->inTimeRaw <= $timesheet->getEndDateTimeString())
- {
- $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();
- }
- }
- /* TODO REMOVE
- public function unlocked()
- {
- $form = new unlockDatesForm();
- if($form->wasSubmitted())
- {
- $form->addData($_POST);
- if($form->validate())
- {
- $data = $form->exportFormData();
- }
- else
- {
- $this->view->form = $form;
- }
- }
- else
- {
- $this->view->form = $form;
- }
- }
- */
- public function admininsert()
- {
- if($this->accountLevel >= 900)
- {
- $form = new insertTimeForm();
- $form->admin(1);
- if($form->wasSubmitted())
- {
- $form->addData($_POST);
- if($form->validate())
- {
- $data = $form->exportFormData();
- //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']);
- $entry->setUserId($data['account']);
- $entry->setNote($data['note']);
- //Save entry data to table.
- if($entry->adminSave())
- {
- //Return a new time form with success message
- $form = new insertTimeForm();
- $form->admin(1);
- $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> Administrative action not allowed on your own timesheet.";
- $form->errorMessage = array($message);
- $this->view->form = $form;
- }
- }
- else
- {
- $this->view->form = $form;
- }
- }
- else
- {
- $this->view->form = $form;
- }
- }
- else
- {
- header("location: ".$this->_link(array('index'))."");
- }
- }
- }
- ?>
|