|
- <?php
-
- namespace Biukop;
-
- class TimeSheet{
- private $xero ;
- private $start_date;
- private $end_date;
- private $remote_timesheets;
- private $local_timesheets =[];
- private $buddy_timesheets =[];
- private $warning_timesheets =[];
-
- public function __construct($xero, $end_date){
- $this->xero = $xero;
- $this->end_date = $end_date;
- $this->cal_start(); //set start_date;
- $this->get_remote_timesheets();
- }
-
- private function cal_start(){
- $d = new \DateTime($this->end_date);
- $d->modify("-13 days");
- $this->start_date = $d->format("Y-m-d");
- // wp_send_json(array(
- // 'start'=> $this->start_date,
- // 'finish'=> $this->end_date,
- // ));
- }
-
- public function refresh_remote(){
- $this->get_remote_timesheets();
- }
-
- private function get_remote_timesheets()
- {
- $this->remote_timesheets = $this->xero->load('PayrollAU\\Timesheet')
- ->where('EndDate==DateTime.Parse("'. $this->end_date .'")')
- ->execute();
- }
-
- public function get_xero_timesheet()
- {
- return $this->remote_timesheets;
- }
-
- public function get_local_timesheet()
- {
- return $this->local_timesheets;
- }
-
- public function set_local_timesheet($lines)
- {
- $this->local_timesheets = [];
- //convert $val to Timesheet format;
- foreach ($lines as $staff_login => $rateshours){
- $ts = "";
- if (array_key_exists($staff_login, $this->local_timesheets)){
- $ts = $this->local_timesheets[$staff_login];
- }else{
- $ts = new \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
- $ts->setEmployeeID($staff_login)
- ->setTimeSheetID($this->get_timesheet_id_by_employee_id($staff_login))
- ->setStartDate(new \DateTime($this->start_date))
- ->setEndDate(new \DateTime($this->end_date))
- ->setStatus("DRAFT");
- }
- //adding lines
- foreach ($rateshours as $rateid => $hours)
- {
- $ts_line = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($this->xero);
- $ts_line->setEarningsRateID($rateid);
- for ($i=0; $i<14; $i++){
- $ts_line->addNumberOfUnit($hours[$i]);
- }
- $ts->addTimesheetLine($ts_line);
- }
- //update this timesheet;
- $this->local_timesheets[$staff_login] = $ts;
- }
- }
-
- public function save_to_xero()
- {
- $to_save=[];
- foreach ( $this->local_timesheets as $t){
- $buddy = $this->get_buddy_timesheet_by_ts($t);
- if ($buddy != NULL && $buddy->getStatus() != "DRAFT"){
- continue;//we encountered approved timesheet;
- }
- $t->setDirty('EmployeeID');
- $t->setDirty('StartDate');
- $t->setDirty('EndDate');
- $t->setDirty('TimesheetLines');
- $t->setDirty('Status');
- $t->setDirty('Hours');
- $t->setDirty('TimesheetID');
- $t->setStatus('DRAFT');
- $to_save[]=$t;
- }
- //empty remote timesheet which are not available in local
- //
- //some of buddy timesheets might be removed from local already
- //we cannot delete it but we can set it to 0 hours
- //
- foreach ($this->remote_timesheets as $ts)
- {
- $staff_login = $ts->getEmployeeID();
- if (!array_key_exists($staff_login, $this->local_timesheets)){//not found
- //we create empty timesheets for him/her
- $empty = new \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
- $empty->setEmployeeID($staff_login)
- ->setTimeSheetID($ts->getTimesheetID())
- ->setStartDate(new \DateTime($this->start_date))
- ->setEndDate(new \DateTime($this->end_date))
- ->setStatus("DRAFT");
-
- foreach($ts->getTimesheetLines() as $line){
- $eid = $line->getEarningsRateID();
- $zeroline= $this->create_empty_timesheet_lines($eid);
- $empty->addTimesheetLine($zeroline);
- }
-
- if ( $ts->getStatus() == "DRAFT" ){//good, we can save it;
- $to_save[] = $empty;//add it to save
- }else{
- $staff_name = \Biukop\AcareOffice::get_user_name_by_login($staff_login);
- $msg = sprintf("%s : %s is APPROVED, but needs to be empty it",
- $staff_name,
- $ts->getTimeSheetID());
- \Biukop\AcareOffice::log($msg);
- }
- }
- }
- $this->xero->saveAll($to_save, false);
- }
-
- private function create_empty_timesheet_lines($EarningsRateID)
- {
- $line = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($xero);
- $line->setEarningsRateID($EarningsRateID);
- for ($i=0; $i<14; $i++)
- $line->addNumberOfUnit(0);
- return $line;
- }
-
- private function get_timesheet_id_by_employee_id($id)
- {
- foreach ($this->remote_timesheets as $ts)
- {
- $staff_login = $ts->getEmployeeID();
- if($staff_login == $id){
- return $ts->getTimesheetID();
- }
- }
- return NULL;
- }
-
- public function warning_timesheet()
- {
- return $this->warning_timesheets;
- }
-
- public function approve_all(){
- $to_save=[];
- foreach ( $this->remote_timesheets as $t){
- if($t->getStatus() == 'DRAFT'){
- $t->setDirty('EmployeeID');
- $t->setDirty('StartDate');
- $t->setDirty('EndDate');
- $t->setDirty('TimesheetLines');
- $t->setDirty('Status');
- $t->setDirty('Hours');
- $t->setDirty('TimesheetID');
- $t->setStatus('APPROVED');
- $to_save[]=$t;
- }
- }
- $this->xero->saveAll($to_save, false);
- }
-
- public function get_buddy_timesheets($employee_id, $start, $end)
- {
- foreach ($this->remote_timesheets as $t){
- if ( $t->getEmployeeID() == $employee_id &&
- $t->getStartDate()->format('Y-m-d') == $start->format('Y-m-d') &&
- $t->getEndDate()->format('Y-m-d') == $end->format('Y-m-d') )
- {
- return $t;
- }
- }
- return NULL; //not found;
- }
-
- private function get_buddy_timesheet_by_ts($t)
- {
- $employee_id = $t->getEmployeeID();
- $start = $t->getStartDate();
- $end = $t->getEndDate();
- return $this->get_buddy_timesheets($employee_id, $start, $end);
- }
-
- public function main(){
- $this->warning_timesheets = [];
- $this->buddy_timesheets = [];
- $this->local_timesheets = $this->create_timesheet_from_db(
- new DateTime("2019-07-01"),
- new DateTime("2019-07-14")
- );
- $length = count($this->local_timesheets);
- $to_save =[];
- for ($i =0; $i < $length; $i++)
- {
- $me = $this->local_timesheets[$i];
- $buddy = $this->get_buddy_timesheet_by_ts($me);
-
- $this->buddy_timesheets[$i] = $buddy;
-
- if ( $buddy != NULL ) {
- $timesheet_id = $buddy->getTimeSheetID();
- $me->setTimeSheetID($timesheet_id);
- if ( $buddy->getStatus() != 'DRAFT'){
- $this->warning_timesheets[]=$me;
- continue;
- }else{
- $to_save[]=$me;
- }
- }else{
- $to_save[]=$me;
- }
-
- }
- $this->xero->saveAlL($to_save, false); //false do not check GUID, always use POST; not PUT
- //$this->approve_all();
- }
-
-
-
- }
|