timesheet source code
Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

215 lines
7.0KB

  1. <?php
  2. namespace Biukop;
  3. class TimeSheet{
  4. private $xero ;
  5. private $start_date;
  6. private $end_date;
  7. private $remote_timesheets;
  8. private $local_timesheets =[];
  9. private $buddy_timesheets =[];
  10. private $warning_timesheets =[];
  11. public function __construct($xero, $end_date){
  12. $this->xero = $xero;
  13. $this->end_date = $end_date;
  14. $this->cal_start(); //set start_date;
  15. $this->get_remote_timesheets();
  16. }
  17. private function cal_start(){
  18. $d = new \DateTime($this->end_date);
  19. $d->modify("-13 days");
  20. $this->start_date = $d->format("Y-m-d");
  21. // wp_send_json(array(
  22. // 'start'=> $this->start_date,
  23. // 'finish'=> $this->end_date,
  24. // ));
  25. }
  26. private function get_remote_timesheets()
  27. {
  28. $this->remote_timesheets = $this->xero->load('PayrollAU\\Timesheet')
  29. ->where('EndDate==DateTime.Parse("'. $this->end_date .'")')
  30. ->execute();
  31. }
  32. public function get_xero_timesheet()
  33. {
  34. return $this->remote_timesheets;
  35. }
  36. public function get_local_timesheet()
  37. {
  38. return $this->local_timesheets;
  39. }
  40. public function set_local_timesheet($lines)
  41. {
  42. $this->local_timesheets = [];
  43. //convert $val to Timesheet format;
  44. foreach ($lines as $staff_login => $rateshours){
  45. $ts = "";
  46. if (array_key_exists($staff_login, $this->local_timesheets)){
  47. $ts = $this->local_timesheets[$staff_login];
  48. }else{
  49. $ts = new \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
  50. $ts->setEmployeeID($staff_login)
  51. ->setTimeSheetID($this->get_timesheet_id_by_employee_id($staff_login))
  52. ->setStartDate(new \DateTime($this->start_date))
  53. ->setEndDate(new \DateTime($this->end_date))
  54. ->setStatus("DRAFT");
  55. }
  56. //adding lines
  57. foreach ($rateshours as $rateid => $hours)
  58. {
  59. $ts_line = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($this->xero);
  60. $ts_line->setEarningsRateID($rateid);
  61. for ($i=0; $i<14; $i++){
  62. $ts_line->addNumberOfUnit($hours[$i]);
  63. }
  64. $ts->addTimesheetLine($ts_line);
  65. }
  66. //update this timesheet;
  67. $this->local_timesheets[$staff_login] = $ts;
  68. }
  69. }
  70. public function save_to_xero()
  71. {
  72. $to_save=[];
  73. foreach ( $this->local_timesheets as $t){
  74. $t->setDirty('EmployeeID');
  75. $t->setDirty('StartDate');
  76. $t->setDirty('EndDate');
  77. $t->setDirty('TimesheetLines');
  78. $t->setDirty('Status');
  79. $t->setDirty('Hours');
  80. $t->setDirty('TimesheetID');
  81. $t->setStatus('DRAFT');
  82. $to_save[]=$t;
  83. }
  84. //empty remote timesheet which are not available in local
  85. //
  86. //some of buddy timesheets might be removed from local already
  87. //we cannot delete it but we can set it to 0 hours
  88. //
  89. foreach ($this->remote_timesheets as $ts)
  90. {
  91. $staff_login = $ts->getEmployeeID();
  92. if (!array_key_exists($staff_login, $this->local_timesheets)){//not found
  93. //we create empty timesheets for him/her
  94. $empty = new \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
  95. $empty->setEmployeeID($staff_login)
  96. ->setTimeSheetID($ts->getTimesheetID())
  97. ->setStartDate(new \DateTime($this->start_date))
  98. ->setEndDate(new \DateTime($this->end_date))
  99. ->setStatus("DRAFT");
  100. if ( $ts->getStatus() == "DRAFT" ){//good, we can save it;
  101. $to_save[] = $empty;//add it to save
  102. }else{
  103. $staff_name = \Biukop\AcareOffice::get_user_name_by_login($staff_login);
  104. $msg = sprintf("%s : %s is APPROVED, but needs to be empty it",
  105. $staff_name,
  106. $ts->getTimeSheetID());
  107. \Biukop\AcareOffice::log($msg);
  108. }
  109. }
  110. }
  111. $this->xero->saveAll($to_save, false);
  112. }
  113. private function get_timesheet_id_by_employee_id($id)
  114. {
  115. foreach ($this->remote_timesheets as $ts)
  116. {
  117. $staff_login = $ts->getEmployeeID();
  118. if($staff_login == $id){
  119. return $ts->getTimesheetID();
  120. }
  121. }
  122. return NULL;
  123. }
  124. public function warning_timesheet()
  125. {
  126. return $this->warning_timesheets;
  127. }
  128. private function approve_all(){
  129. $to_save=[];
  130. foreach ( $this->local_timesheets as $t){
  131. $t->setDirty('EmployeeID');
  132. $t->setDirty('StartDate');
  133. $t->setDirty('EndDate');
  134. $t->setDirty('TimesheetLines');
  135. $t->setDirty('Status');
  136. $t->setDirty('Hours');
  137. $t->setDirty('TimesheetID');
  138. $t->setStatus('APPROVED');
  139. $to_save[]=$t;
  140. }
  141. $this->xero->saveAll($to_save, false);
  142. }
  143. public function get_buddy_timesheets($employee_id, $start, $end)
  144. {
  145. foreach ($this->remote_timesheets as $t){
  146. if ( $t->getEmployeeID() == $employee_id &&
  147. $t->getStartDate()->format('Y-m-d') == $start->format('Y-m-d') &&
  148. $t->getEndDate()->format('Y-m-d') == $end->format('Y-m-d') )
  149. {
  150. return $t;
  151. }
  152. }
  153. return NULL; //not found;
  154. }
  155. private function get_buddy_timesheet_by_ts($t)
  156. {
  157. $employee_id = $t->getEmployeeID();
  158. $start = $t->getStartDate();
  159. $end = $t->getEndDate();
  160. return $this->get_buddy_timesheets($employee_id, $start, $end);
  161. }
  162. public function main(){
  163. $this->warning_timesheets = [];
  164. $this->buddy_timesheets = [];
  165. $this->local_timesheets = $this->create_timesheet_from_db(
  166. new DateTime("2019-07-01"),
  167. new DateTime("2019-07-14")
  168. );
  169. $length = count($this->local_timesheets);
  170. $to_save =[];
  171. for ($i =0; $i < $length; $i++)
  172. {
  173. $me = $this->local_timesheets[$i];
  174. $buddy = $this->get_buddy_timesheet_by_ts($me);
  175. $this->buddy_timesheets[$i] = $buddy;
  176. if ( $buddy != NULL ) {
  177. $timesheet_id = $buddy->getTimeSheetID();
  178. $me->setTimeSheetID($timesheet_id);
  179. if ( $buddy->getStatus() != 'DRAFT'){
  180. $this->warning_timesheets[]=$me;
  181. continue;
  182. }else{
  183. $to_save[]=$me;
  184. }
  185. }else{
  186. $to_save[]=$me;
  187. }
  188. }
  189. $this->xero->saveAlL($to_save, false); //false do not check GUID, always use POST; not PUT
  190. //$this->approve_all();
  191. }
  192. }