timesheet source code
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

196 rindas
6.7KB

  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. $tsLines = [];
  61. foreach ($rateshours as $rateId => $hours)
  62. {
  63. $ts_line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
  64. $ts_line->setEarningsRateID($rateId);
  65. $numOfUnits =[];
  66. for ($i=0; $i<14; $i++){
  67. $numOfUnits[]=$hours[$i];
  68. }
  69. $ts_line->setNumberOfUnits($numOfUnits);
  70. $tsLines[] = $ts_line;
  71. $ts->setTimesheetLines($tsLines);
  72. }
  73. //update this timesheet;
  74. $this->local_timesheets[$staff_login] = $ts;
  75. }
  76. }
  77. private function get_timesheet_id_by_employee_id($id)
  78. {
  79. foreach ($this->remote_timesheets as $ts)
  80. {
  81. $staff_login = $ts->getEmployeeID();
  82. if($staff_login == $id){
  83. return $ts->getTimesheetID();
  84. }
  85. }
  86. return NULL;
  87. }
  88. public function save_to_xero()
  89. {
  90. $to_create=[];
  91. $to_save=[];
  92. foreach ( $this->local_timesheets as $t){
  93. $buddy = $this->get_buddy_timesheet_by_ts($t);
  94. if ($buddy != NULL && $buddy->getStatus() != "DRAFT"){
  95. continue;//we encountered approved timesheet;
  96. }
  97. if ( trim($t->getTimeSheetId() ) != "" ){
  98. $to_save[]=$t;
  99. }else{
  100. $to_save[] = $t; // TODO: maybe update will work for new timesheet
  101. // $to_create[]= $t;
  102. }
  103. }
  104. //empty remote timesheet which are not available in local
  105. //
  106. //some of buddy timesheets might be removed from local already
  107. //we cannot delete it but we can set it to 0 hours
  108. //
  109. foreach ($this->remote_timesheets as $ts)
  110. {
  111. $staff_login = $ts->getEmployeeID();
  112. if (!array_key_exists($staff_login, $this->local_timesheets)){//not found
  113. //we create empty timesheets for him/her
  114. $empty = new \XeroAPI\XeroPHP\Models\PayrollAu\Timesheet;
  115. $empty->setEmployeeID($staff_login);
  116. $empty->setTimeSheetID($ts->getTimesheetID());
  117. $empty->setStartDate($this->start_date);
  118. $empty->setEndDate($this->end_date);
  119. $empty->setStatus(\XeroAPI\XeroPHP\Models\PayrollAu\TimesheetStatus::DRAFT);
  120. foreach($ts->getTimesheetLines() as $line){
  121. $eid = $line->getEarningsRateID();
  122. $zeroline= $this->create_empty_timesheet_lines($eid);
  123. $zerolines[] = $zeroline;
  124. $empty->setTimesheetLines($zerolines);
  125. }
  126. if ( $ts->getStatus() == "DRAFT" ){//good, we can save it;
  127. $to_save[] = $empty;//add it to save
  128. }else{
  129. $staff_name = \Biukop\AcareOffice::get_user_name_by_login($staff_login);
  130. $msg = sprintf("%s : %s is APPROVED, but needs to be empty it",
  131. $staff_name,
  132. $ts->getTimeSheetID());
  133. \Biukop\AcareOffice::log($msg);
  134. }
  135. }
  136. }
  137. $this->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(),$to_save);
  138. return;
  139. }
  140. public function approve_all(){
  141. $to_save=[];
  142. foreach ( $this->remote_timesheets as $t){
  143. if($t->getStatus() == 'DRAFT'){
  144. $to_save[]=$t;
  145. }
  146. }
  147. $this->apiPayrollAu->createTimesheet($this->oauth2->getTenantId(), false);
  148. }
  149. private function get_buddy_timesheet_by_ts($t)
  150. {
  151. $employee_id = $t->getEmployeeID();
  152. $start = $t->getStartDate();
  153. $end = $t->getEndDate();
  154. return $this->get_buddy_timesheets($employee_id, $start, $end);
  155. }
  156. public function get_buddy_timesheets($employee_id, $start, $end)
  157. {
  158. foreach ($this->remote_timesheets as $t){
  159. if ( $t->getEmployeeID() == $employee_id &&
  160. $t->getStartDateAsDate()->format('Y-m-d') == $start &&
  161. $t->getEndDateAsDate()->format('Y-m-d') == $end )
  162. {
  163. return $t;
  164. }
  165. }
  166. return NULL; //not found;
  167. }
  168. private function create_empty_timesheet_lines($EarningsRateID)
  169. {
  170. $unit = [];
  171. for ($i=0; $i<14; $i++)
  172. $unit[] = 0;
  173. $line = new \XeroAPI\XeroPHP\Models\PayrollAu\TimesheetLine;
  174. $line->setEarningsRateID($EarningsRateID);
  175. $line->setNumberOfUnits($unit);
  176. return $line;
  177. }
  178. }