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.

240 lines
7.9KB

  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. public function refresh_remote(){
  27. $this->get_remote_timesheets();
  28. }
  29. private function get_remote_timesheets()
  30. {
  31. $this->remote_timesheets = $this->xero->load('PayrollAU\\Timesheet')
  32. ->where('EndDate==DateTime.Parse("'. $this->end_date .'")')
  33. ->execute();
  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 \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
  53. $ts->setEmployeeID($staff_login)
  54. ->setTimeSheetID($this->get_timesheet_id_by_employee_id($staff_login))
  55. ->setStartDate(new \DateTime($this->start_date))
  56. ->setEndDate(new \DateTime($this->end_date))
  57. ->setStatus("DRAFT");
  58. }
  59. //adding lines
  60. foreach ($rateshours as $rateid => $hours)
  61. {
  62. $ts_line = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($this->xero);
  63. $ts_line->setEarningsRateID($rateid);
  64. for ($i=0; $i<14; $i++){
  65. $ts_line->addNumberOfUnit($hours[$i]);
  66. }
  67. $ts->addTimesheetLine($ts_line);
  68. }
  69. //update this timesheet;
  70. $this->local_timesheets[$staff_login] = $ts;
  71. }
  72. }
  73. public function save_to_xero()
  74. {
  75. $to_save=[];
  76. foreach ( $this->local_timesheets as $t){
  77. $buddy = $this->get_buddy_timesheet_by_ts($t);
  78. if ($buddy != NULL && $buddy->getStatus() != "DRAFT"){
  79. continue;//we encountered approved timesheet;
  80. }
  81. $t->setDirty('EmployeeID');
  82. $t->setDirty('StartDate');
  83. $t->setDirty('EndDate');
  84. $t->setDirty('TimesheetLines');
  85. $t->setDirty('Status');
  86. $t->setDirty('Hours');
  87. $t->setDirty('TimesheetID');
  88. $t->setStatus('DRAFT');
  89. $to_save[]=$t;
  90. }
  91. //empty remote timesheet which are not available in local
  92. //
  93. //some of buddy timesheets might be removed from local already
  94. //we cannot delete it but we can set it to 0 hours
  95. //
  96. foreach ($this->remote_timesheets as $ts)
  97. {
  98. $staff_login = $ts->getEmployeeID();
  99. if (!array_key_exists($staff_login, $this->local_timesheets)){//not found
  100. //we create empty timesheets for him/her
  101. $empty = new \XeroPHP\Models\PayrollAU\Timesheet($this->xero);
  102. $empty->setEmployeeID($staff_login)
  103. ->setTimeSheetID($ts->getTimesheetID())
  104. ->setStartDate(new \DateTime($this->start_date))
  105. ->setEndDate(new \DateTime($this->end_date))
  106. ->setStatus("DRAFT");
  107. foreach($ts->getTimesheetLines() as $line){
  108. $eid = $line->getEarningsRateID();
  109. $zeroline= $this->create_empty_timesheet_lines($eid);
  110. $empty->addTimesheetLine($zeroline);
  111. }
  112. if ( $ts->getStatus() == "DRAFT" ){//good, we can save it;
  113. $to_save[] = $empty;//add it to save
  114. }else{
  115. $staff_name = \Biukop\AcareOffice::get_user_name_by_login($staff_login);
  116. $msg = sprintf("%s : %s is APPROVED, but needs to be empty it",
  117. $staff_name,
  118. $ts->getTimeSheetID());
  119. \Biukop\AcareOffice::log($msg);
  120. }
  121. }
  122. }
  123. $this->xero->saveAll($to_save, false);
  124. }
  125. private function create_empty_timesheet_lines($EarningsRateID)
  126. {
  127. $line = new \XeroPHP\Models\PayrollAU\Timesheet\TimesheetLine($xero);
  128. $line->setEarningsRateID($EarningsRateID);
  129. for ($i=0; $i<14; $i++)
  130. $line->addNumberOfUnit(0);
  131. return $line;
  132. }
  133. private function get_timesheet_id_by_employee_id($id)
  134. {
  135. foreach ($this->remote_timesheets as $ts)
  136. {
  137. $staff_login = $ts->getEmployeeID();
  138. if($staff_login == $id){
  139. return $ts->getTimesheetID();
  140. }
  141. }
  142. return NULL;
  143. }
  144. public function warning_timesheet()
  145. {
  146. return $this->warning_timesheets;
  147. }
  148. public function approve_all(){
  149. $to_save=[];
  150. foreach ( $this->remote_timesheets as $t){
  151. if($t->getStatus() == 'DRAFT'){
  152. $t->setDirty('EmployeeID');
  153. $t->setDirty('StartDate');
  154. $t->setDirty('EndDate');
  155. $t->setDirty('TimesheetLines');
  156. $t->setDirty('Status');
  157. $t->setDirty('Hours');
  158. $t->setDirty('TimesheetID');
  159. $t->setStatus('APPROVED');
  160. $to_save[]=$t;
  161. }
  162. }
  163. $this->xero->saveAll($to_save, false);
  164. }
  165. public function get_buddy_timesheets($employee_id, $start, $end)
  166. {
  167. foreach ($this->remote_timesheets as $t){
  168. if ( $t->getEmployeeID() == $employee_id &&
  169. $t->getStartDate()->format('Y-m-d') == $start->format('Y-m-d') &&
  170. $t->getEndDate()->format('Y-m-d') == $end->format('Y-m-d') )
  171. {
  172. return $t;
  173. }
  174. }
  175. return NULL; //not found;
  176. }
  177. private function get_buddy_timesheet_by_ts($t)
  178. {
  179. $employee_id = $t->getEmployeeID();
  180. $start = $t->getStartDate();
  181. $end = $t->getEndDate();
  182. return $this->get_buddy_timesheets($employee_id, $start, $end);
  183. }
  184. public function main(){
  185. $this->warning_timesheets = [];
  186. $this->buddy_timesheets = [];
  187. $this->local_timesheets = $this->create_timesheet_from_db(
  188. new DateTime("2019-07-01"),
  189. new DateTime("2019-07-14")
  190. );
  191. $length = count($this->local_timesheets);
  192. $to_save =[];
  193. for ($i =0; $i < $length; $i++)
  194. {
  195. $me = $this->local_timesheets[$i];
  196. $buddy = $this->get_buddy_timesheet_by_ts($me);
  197. $this->buddy_timesheets[$i] = $buddy;
  198. if ( $buddy != NULL ) {
  199. $timesheet_id = $buddy->getTimeSheetID();
  200. $me->setTimeSheetID($timesheet_id);
  201. if ( $buddy->getStatus() != 'DRAFT'){
  202. $this->warning_timesheets[]=$me;
  203. continue;
  204. }else{
  205. $to_save[]=$me;
  206. }
  207. }else{
  208. $to_save[]=$me;
  209. }
  210. }
  211. $this->xero->saveAlL($to_save, false); //false do not check GUID, always use POST; not PUT
  212. //$this->approve_all();
  213. }
  214. }