timesheet source code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

194 satır
6.6KB

  1. <?php
  2. namespace Biukop;
  3. class XeroOauth2Timesheet
  4. {
  5. private $oauth2 ;
  6. private $apiPayrollAu ;
  7. private $start_date;
  8. private $end_date;
  9. private $remote_timesheets;
  10. private $local_timesheets =[];
  11. private $buddy_timesheets =[];
  12. private $warning_timesheets =[];
  13. public function __construct($oauth2, $end_date){
  14. $this->oauth2 = $oauth2;
  15. $this->apiPayrollAu = $this->oauth2->get_payroll_au_instance();
  16. $this->end_date = $end_date;
  17. $this->start_date = $this->cal_start(); //set start_date;
  18. $this->get_remote_timesheets();
  19. }
  20. private function cal_start(){
  21. $d = new \DateTime($this->end_date);
  22. $d->modify("-13 days");
  23. return $d->format("Y-m-d");
  24. }
  25. public function refresh_remote(){
  26. $this->get_remote_timesheets();
  27. }
  28. private function get_remote_timesheets()
  29. {
  30. $api = $this->oauth2->get_payroll_au_instance();
  31. $where = 'EndDate==DateTime.Parse("'. $this->end_date .'")';
  32. $result = $api->getTimeSheets($this->oauth2->getTenantId(), null, $where, null, null);
  33. $this->remote_timesheets =$result->getTimeSheets();
  34. }
  35. public function get_xero_timesheet()
  36. {
  37. return $this->remote_timesheets;
  38. }
  39. public function get_local_timesheet()
  40. {
  41. return $this->local_timesheets;
  42. }
  43. public function set_local_timesheet($lines)
  44. {
  45. $this->local_timesheets = [];
  46. //convert $val to Timesheet format;
  47. foreach ($lines as $staff_login => $rateshours){
  48. $ts = "";
  49. if (array_key_exists($staff_login, $this->local_timesheets)){
  50. $ts = $this->local_timesheets[$staff_login];
  51. }else{
  52. $ts = new \XeroAPI\XeroPHP\Models\PayrollAu\Timesheet;
  53. $ts->setEmployeeID($staff_login);
  54. $ts->setTimeSheetID($this->get_timesheet_id_by_employee_id($staff_login));
  55. $ts->setStartDate($this->start_date);
  56. $ts->setEndDate($this->end_date);
  57. $ts->setStatus(\XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT);
  58. }
  59. //adding lines
  60. foreach ($rateshours as $rateId => $hours)
  61. {
  62. $ts_line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
  63. $ts_line->setEarningsRateID($rateId);
  64. $numOfUnits =[];
  65. for ($i=0; $i<14; $i++){
  66. $numOfUnits[]=$hours[$i];
  67. }
  68. $ts_line->setNumberOfUnits($numOfUnits);
  69. $tsLines[] = $ts_line;
  70. $ts->setTimesheetLines($tsLines);
  71. }
  72. //update this timesheet;
  73. $this->local_timesheets[$staff_login] = $ts;
  74. }
  75. }
  76. private function get_timesheet_id_by_employee_id($id)
  77. {
  78. foreach ($this->remote_timesheets as $ts)
  79. {
  80. $staff_login = $ts->getEmployeeID();
  81. if($staff_login == $id){
  82. return $ts->getTimesheetID();
  83. }
  84. }
  85. return NULL;
  86. }
  87. public function save_to_xero()
  88. {
  89. $to_create=[];
  90. $to_save=[];
  91. foreach ( $this->local_timesheets as $t){
  92. $buddy = $this->get_buddy_timesheet_by_ts($t);
  93. if ($buddy != NULL && $buddy->getStatus() != "DRAFT"){
  94. continue;//we encountered approved timesheet;
  95. }
  96. if ( trim($t->getTimeSheetId() ) != "" ){
  97. $to_save[]=$t;
  98. }else{
  99. $to_save[] = $t; // TODO: maybe update will work for new timesheet
  100. // $to_create[]= $t;
  101. }
  102. }
  103. //empty remote timesheet which are not available in local
  104. //
  105. //some of buddy timesheets might be removed from local already
  106. //we cannot delete it but we can set it to 0 hours
  107. //
  108. foreach ($this->remote_timesheets as $ts)
  109. {
  110. $staff_login = $ts->getEmployeeID();
  111. if (!array_key_exists($staff_login, $this->local_timesheets)){//not found
  112. //we create empty timesheets for him/her
  113. $empty = new \XeroAPI\XeroPHP\Models\PayrollAu\Timesheet;
  114. $empty->setEmployeeID($staff_login);
  115. $empty->setTimeSheetID($ts->getTimesheetID());
  116. $empty->setStartDate($this->start_date);
  117. $empty->setEndDate($this->end_date);
  118. $empty->setStatus(\XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT);
  119. foreach($ts->getTimesheetLines() as $line){
  120. $eid = $line->getEarningsRateID();
  121. $zeroline= $this->create_empty_timesheet_lines($eid);
  122. $zerolines[] = $zeroline;
  123. $empty->setTimesheetLines($zerolines);
  124. }
  125. if ( $ts->getStatus() == "DRAFT" ){//good, we can save it;
  126. $to_save[] = $empty;//add it to save
  127. }else{
  128. $staff_name = \Biukop\AcareOffice::get_user_name_by_login($staff_login);
  129. $msg = sprintf("%s : %s is APPROVED, but needs to be empty it",
  130. $staff_name,
  131. $ts->getTimeSheetID());
  132. \Biukop\AcareOffice::log($msg);
  133. }
  134. }
  135. }
  136. $this->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(),$to_save);
  137. }
  138. public function approve_all(){
  139. $to_save=[];
  140. foreach ( $this->remote_timesheets as $t){
  141. if($t->getStatus() == 'DRAFT'){
  142. $to_save[]=$t;
  143. }
  144. }
  145. $this->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(), false);
  146. }
  147. private function get_buddy_timesheet_by_ts($t)
  148. {
  149. $employee_id = $t->getEmployeeID();
  150. $start = $t->getStartDate();
  151. $end = $t->getEndDate();
  152. return $this->get_buddy_timesheets($employee_id, $start, $end);
  153. }
  154. public function get_buddy_timesheets($employee_id, $start, $end)
  155. {
  156. foreach ($this->remote_timesheets as $t){
  157. if ( $t->getEmployeeID() == $employee_id &&
  158. $t->getStartDateAsDate()->format('Y-m-d') == $start &&
  159. $t->getEndDateAsDate()->format('Y-m-d') == $end )
  160. {
  161. return $t;
  162. }
  163. }
  164. return NULL; //not found;
  165. }
  166. private function create_empty_timesheet_lines($EarningsRateID)
  167. {
  168. $unit = [];
  169. for ($i=0; $i<14; $i++)
  170. $unit[] = 0;
  171. $line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
  172. $line->setEarningsRateID($EarningsRateID);
  173. $line->setNumberOfUnits($unit);
  174. return $line;
  175. }
  176. }