oauth2 = $oauth2; $this->apiPayrollAu = $this->oauth2->get_payroll_au_instance(); $this->end_date = $end_date; $this->start_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"); return $d->format("Y-m-d"); } public function refresh_remote(){ $this->get_remote_timesheets(); } private function get_remote_timesheets() { $api = $this->oauth2->get_payroll_au_instance(); $where = 'EndDate==DateTime.Parse("'. $this->end_date .'")'; $result = $api->getTimeSheets($this->oauth2->getTenantId(), null, $where, null, null); $this->remote_timesheets =$result->getTimeSheets(); } 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 \XeroAPI\XeroPHP\Models\PayrollAu\Timesheet; $ts->setEmployeeID($staff_login); $ts->setTimeSheetID($this->get_timesheet_id_by_employee_id($staff_login)); $ts->setStartDate($this->start_date); $ts->setEndDate($this->end_date); $ts->setStatus(\XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT); } //adding lines $tsLines=[]; foreach ($rateshours as $rateId => $hours) { $ts_line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine; $ts_line->setEarningsRateID($rateId); $numOfUnits =[]; for ($i=0; $i<14; $i++){ $numOfUnits[]=$hours[$i]; } $ts_line->setNumberOfUnits($numOfUnits); $tsLines[] = $ts_line; $ts->setTimesheetLines($tsLines); } //update this timesheet; $this->local_timesheets[$staff_login] = $ts; } } 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 save_to_xero() { $to_create=[]; $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; } if ( trim($t->getTimeSheetId() ) != "" ){ $to_save[]=$t; }else{ $to_save[] = $t; // TODO: maybe update will work for new timesheet // $to_create[]= $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 \XeroAPI\XeroPHP\Models\PayrollAu\Timesheet; $empty->setEmployeeID($staff_login); $empty->setTimeSheetID($ts->getTimesheetID()); $empty->setStartDate($this->start_date); $empty->setEndDate($this->end_date); $empty->setStatus(\XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT); foreach($ts->getTimesheetLines() as $line){ $eid = $line->getEarningsRateID(); $zeroline= $this->create_empty_timesheet_lines($eid); $zerolines[] = $zeroline; $empty->setTimesheetLines($zerolines); } 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->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(),$to_save); } public function approve_all(){ $to_save=[]; foreach ( $this->remote_timesheets as $t){ if($t->getStatus() == 'DRAFT'){ $to_save[]=$t; } } $this->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(), false); } 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 get_buddy_timesheets($employee_id, $start, $end) { foreach ($this->remote_timesheets as $t){ if ( $t->getEmployeeID() == $employee_id && $t->getStartDateAsDate()->format('Y-m-d') == $start && $t->getEndDateAsDate()->format('Y-m-d') == $end ) { return $t; } } return NULL; //not found; } private function create_empty_timesheet_lines($EarningsRateID) { $unit = []; for ($i=0; $i<14; $i++) $unit[] = 0; $line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine; $line->setEarningsRateID($EarningsRateID); $line->setNumberOfUnits($unit); return $line; } }