{"action":"created","comment":{"url":"https://api.github.com/repos/civicrm/civihr/pulls/comments/93864360","pull_request_review_id":14391501,"id":93864360,"diff_hunk":"@@ -0,0 +1,48 @@\n+<?php\n+\n+use CRM_HRLeaveAndAbsences_BAO_LeaveBalanceChange as LeaveBalanceChange;\n+use CRM_HRLeaveAndAbsences_BAO_LeaveRequest as LeaveRequest;\n+\n+class CRM_HRLeaveAndAbsences_Service_LeaveBalanceChange {\n+\n+  /**\n+   * Creates LeaveBalanceChange instances for each of the dates of the given\n+   * LeaveRequest\n+   */\n+  public function createForLeaveRequest(LeaveRequest $leaveRequest) {\n+    $balanceChanges = $this->calculateBalanceChanges($leaveRequest);\n+\n+    $dates = $leaveRequest->getDates();\n+\n+    $balanceChangeTypes = array_flip(LeaveBalanceChange::buildOptions('type_id', 'validate'));\n+    foreach($dates as $date) {\n+      foreach($balanceChanges['breakdown'] as $balanceChange) {\n+        if($balanceChange['date'] == $date->date) {\n+          LeaveBalanceChange::create([\n+            'source_id' => $date->id,\n+            'source_type' => LeaveBalanceChange::SOURCE_LEAVE_REQUEST_DAY,\n+            'amount' => $balanceChange['amount'],\n+            'type_id' => $balanceChangeTypes['debit']\n+          ]);\n+        }\n+      }\n+    }\n+  }\n+\n+  /**\n+   * Calculates the balance changes for each of the LeaveRequest dates\n+   *\n+   * @param \\CRM_HRLeaveAndAbsences_BAO_LeaveRequest $leaveRequest\n+   *\n+   * @return array\n+   */\n+  private function calculateBalanceChanges(LeaveRequest $leaveRequest) {\n+    return LeaveRequest::calculateBalanceChange(\n+      $leaveRequest->contact_id,\n+      new DateTime($leaveRequest->from_date),\n+      $leaveRequest->from_date_type,\n+      empty($leaveRequest->to_date) ? null : new DateTime($leaveRequest->to_date),\n+      $leaveRequest->to_date_type","path":"uk.co.compucorp.civicrm.hrleaveandabsences/CRM/HRLeaveAndAbsences/Service/LeaveBalanceChange.php","position":45,"original_position":45,"commit_id":"47076c86aef694e258c461dca94b458629825946","original_commit_id":"a567e300f6f476ff737c28bab079d087cf9eb3a1","user":{"login":"omarabuhussein","id":6275540,"avatar_url":"https://avatars.githubusercontent.com/u/6275540?v=3","gravatar_id":"","url":"https://api.github.com/users/omarabuhussein","html_url":"https://github.com/omarabuhussein","followers_url":"https://api.github.com/users/omarabuhussein/followers","following_url":"https://api.github.com/users/omarabuhussein/following{/other_user}","gists_url":"https://api.github.com/users/omarabuhussein/gists{/gist_id}","starred_url":"https://api.github.com/users/omarabuhussein/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/omarabuhussein/subscriptions","organizations_url":"https://api.github.com/users/omarabuhussein/orgs","repos_url":"https://api.github.com/users/omarabuhussein/repos","events_url":"https://api.github.com/users/omarabuhussein/events{/privacy}","received_events_url":"https://api.github.com/users/omarabuhussein/received_events","type":"User","site_admin":false},"body":"also shouldn't you check for to_date_type if exist as for to_date","created_at":"2016-12-26T12:31:17Z","updated_at":"2016-12-26T12:49:27Z","html_url":"https://github.com/civicrm/civihr/pull/1311#discussion_r93864360","pull_request_url":"https://api.github.com/repos/civicrm/civihr/pulls/1311","_links":{"self":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/comments/93864360"},"html":{"href":"https://github.com/civicrm/civihr/pull/1311#discussion_r93864360"},"pull_request":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/1311"}}},"pull_request":{"url":"https://api.github.com/repos/civicrm/civihr/pulls/1311","id":99356779,"html_url":"https://github.com/civicrm/civihr/pull/1311","diff_url":"https://github.com/civicrm/civihr/pull/1311.diff","patch_url":"https://github.com/civicrm/civihr/pull/1311.patch","issue_url":"https://api.github.com/repos/civicrm/civihr/issues/1311","number":1311,"state":"open","locked":false,"title":"PCHR-1681: Create/Delete LeaveBalanceChange related to Leave and Sickness Requests","user":{"login":"davialexandre","id":388373,"avatar_url":"https://avatars.githubusercontent.com/u/388373?v=3","gravatar_id":"","url":"https://api.github.com/users/davialexandre","html_url":"https://github.com/davialexandre","followers_url":"https://api.github.com/users/davialexandre/followers","following_url":"https://api.github.com/users/davialexandre/following{/other_user}","gists_url":"https://api.github.com/users/davialexandre/gists{/gist_id}","starred_url":"https://api.github.com/users/davialexandre/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/davialexandre/subscriptions","organizations_url":"https://api.github.com/users/davialexandre/orgs","repos_url":"https://api.github.com/users/davialexandre/repos","events_url":"https://api.github.com/users/davialexandre/events{/privacy}","received_events_url":"https://api.github.com/users/davialexandre/received_events","type":"User","site_admin":false},"body":"Whenever a Leave Request gets updated, we must also update the Leave Balance Changes caused by it. This should not be the responsibility of the Leave Request BAO though. That BAO should care only about itself and its direct dependents, the Leave Request Dates. Besides that, Leave Balance Changes are also used for other purposes, like storing information about the Entitlement and expired leaves. For these reasons, it's interesting to separate this from the Leave Request BAO and try to encapsulate the logic somewhere else.\r\n\r\nThe LeaveRequest service was created to achieve this isolation. It has two methods: `create` and `delete`. The former, basically, creates the LeaveRequest using the BAO's `create` method, and then uses the `LeaveRequest::calculateBalanceChange` method in order to get the balance change amounts for each of the request dates and saves the Balance Changes.  The `delete` method does the opposite, making sure to delete everything linked to a LeaveRequest (Balance Change and dates) and finally the Leave Request itself.\r\n\r\nA SicknessRequest service was also created and it does pretty much the same thing. When creating a new SicknessRequest, an associated LeaveRequest is created (to store the dates, status, absence type etc). This is done by the BAO. The SicknessRequest service will be responsible to create the Balance Changes for the associated LeaveRequest.\r\n\r\nFinally, the `create` and `delete` methods of both the LeaveRequest and SicknessRequest APIs where updated to call the respective service methods intead of the BAO methods.\r\n\r\n### What else has changed?\r\n\r\nHow a SicknessRequest uses a LeaveRequest internally to store additional information is just an implementation detail and must be hidden from outside world. For that reason, the `SicknessRequest.create` API was customized a bit to work just like the `LeaveRequest.create` endpoint. That is, the response from `SicknessRequest.create` will be exactly like the one from LeaveRequest api and it will include of the LeaveRequest fields together with the SicknessRequest fields","created_at":"2016-12-26T09:58:09Z","updated_at":"2016-12-26T12:49:27Z","closed_at":null,"merged_at":null,"merge_commit_sha":"45363e773f747061c4690edae77f08a09f446ea6","assignee":null,"assignees":[],"milestone":null,"commits_url":"https://api.github.com/repos/civicrm/civihr/pulls/1311/commits","review_comments_url":"https://api.github.com/repos/civicrm/civihr/pulls/1311/comments","review_comment_url":"https://api.github.com/repos/civicrm/civihr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/civicrm/civihr/issues/1311/comments","statuses_url":"https://api.github.com/repos/civicrm/civihr/statuses/47076c86aef694e258c461dca94b458629825946","head":{"label":"civicrm:PCHR-1681-create-delete-leave-balance-change-for-leave-request","ref":"PCHR-1681-create-delete-leave-balance-change-for-leave-request","sha":"47076c86aef694e258c461dca94b458629825946","user":{"login":"civicrm","id":136373,"avatar_url":"https://avatars.githubusercontent.com/u/136373?v=3","gravatar_id":"","url":"https://api.github.com/users/civicrm","html_url":"https://github.com/civicrm","followers_url":"https://api.github.com/users/civicrm/followers","following_url":"https://api.github.com/users/civicrm/following{/other_user}","gists_url":"https://api.github.com/users/civicrm/gists{/gist_id}","starred_url":"https://api.github.com/users/civicrm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/civicrm/subscriptions","organizations_url":"https://api.github.com/users/civicrm/orgs","repos_url":"https://api.github.com/users/civicrm/repos","events_url":"https://api.github.com/users/civicrm/events{/privacy}","received_events_url":"https://api.github.com/users/civicrm/received_events","type":"Organization","site_admin":false},"repo":{"id":10086903,"name":"civihr","full_name":"civicrm/civihr","owner":{"login":"civicrm","id":136373,"avatar_url":"https://avatars.githubusercontent.com/u/136373?v=3","gravatar_id":"","url":"https://api.github.com/users/civicrm","html_url":"https://github.com/civicrm","followers_url":"https://api.github.com/users/civicrm/followers","following_url":"https://api.github.com/users/civicrm/following{/other_user}","gists_url":"https://api.github.com/users/civicrm/gists{/gist_id}","starred_url":"https://api.github.com/users/civicrm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/civicrm/subscriptions","organizations_url":"https://api.github.com/users/civicrm/orgs","repos_url":"https://api.github.com/users/civicrm/repos","events_url":"https://api.github.com/users/civicrm/events{/privacy}","received_events_url":"https://api.github.com/users/civicrm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/civicrm/civihr","description":"CiviHR","fork":false,"url":"https://api.github.com/repos/civicrm/civihr","forks_url":"https://api.github.com/repos/civicrm/civihr/forks","keys_url":"https://api.github.com/repos/civicrm/civihr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/civicrm/civihr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/civicrm/civihr/teams","hooks_url":"https://api.github.com/repos/civicrm/civihr/hooks","issue_events_url":"https://api.github.com/repos/civicrm/civihr/issues/events{/number}","events_url":"https://api.github.com/repos/civicrm/civihr/events","assignees_url":"https://api.github.com/repos/civicrm/civihr/assignees{/user}","branches_url":"https://api.github.com/repos/civicrm/civihr/branches{/branch}","tags_url":"https://api.github.com/repos/civicrm/civihr/tags","blobs_url":"https://api.github.com/repos/civicrm/civihr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/civicrm/civihr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/civicrm/civihr/git/refs{/sha}","trees_url":"https://api.github.com/repos/civicrm/civihr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/civicrm/civihr/statuses/{sha}","languages_url":"https://api.github.com/repos/civicrm/civihr/languages","stargazers_url":"https://api.github.com/repos/civicrm/civihr/stargazers","contributors_url":"https://api.github.com/repos/civicrm/civihr/contributors","subscribers_url":"https://api.github.com/repos/civicrm/civihr/subscribers","subscription_url":"https://api.github.com/repos/civicrm/civihr/subscription","commits_url":"https://api.github.com/repos/civicrm/civihr/commits{/sha}","git_commits_url":"https://api.github.com/repos/civicrm/civihr/git/commits{/sha}","comments_url":"https://api.github.com/repos/civicrm/civihr/comments{/number}","issue_comment_url":"https://api.github.com/repos/civicrm/civihr/issues/comments{/number}","contents_url":"https://api.github.com/repos/civicrm/civihr/contents/{+path}","compare_url":"https://api.github.com/repos/civicrm/civihr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/civicrm/civihr/merges","archive_url":"https://api.github.com/repos/civicrm/civihr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/civicrm/civihr/downloads","issues_url":"https://api.github.com/repos/civicrm/civihr/issues{/number}","pulls_url":"https://api.github.com/repos/civicrm/civihr/pulls{/number}","milestones_url":"https://api.github.com/repos/civicrm/civihr/milestones{/number}","notifications_url":"https://api.github.com/repos/civicrm/civihr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/civicrm/civihr/labels{/name}","releases_url":"https://api.github.com/repos/civicrm/civihr/releases{/id}","deployments_url":"https://api.github.com/repos/civicrm/civihr/deployments","created_at":"2013-05-15T20:06:06Z","updated_at":"2016-10-25T11:36:09Z","pushed_at":"2016-12-26T09:58:09Z","git_url":"git://github.com/civicrm/civihr.git","ssh_url":"git@github.com:civicrm/civihr.git","clone_url":"https://github.com/civicrm/civihr.git","svn_url":"https://github.com/civicrm/civihr","homepage":null,"size":37308,"stargazers_count":4,"watchers_count":4,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":27,"mirror_url":null,"open_issues_count":29,"forks":27,"open_issues":29,"watchers":4,"default_branch":"staging"}},"base":{"label":"civicrm:la-milestone-3","ref":"la-milestone-3","sha":"1afff6c80618fe912354d6b48aa3a196256d2a7e","user":{"login":"civicrm","id":136373,"avatar_url":"https://avatars.githubusercontent.com/u/136373?v=3","gravatar_id":"","url":"https://api.github.com/users/civicrm","html_url":"https://github.com/civicrm","followers_url":"https://api.github.com/users/civicrm/followers","following_url":"https://api.github.com/users/civicrm/following{/other_user}","gists_url":"https://api.github.com/users/civicrm/gists{/gist_id}","starred_url":"https://api.github.com/users/civicrm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/civicrm/subscriptions","organizations_url":"https://api.github.com/users/civicrm/orgs","repos_url":"https://api.github.com/users/civicrm/repos","events_url":"https://api.github.com/users/civicrm/events{/privacy}","received_events_url":"https://api.github.com/users/civicrm/received_events","type":"Organization","site_admin":false},"repo":{"id":10086903,"name":"civihr","full_name":"civicrm/civihr","owner":{"login":"civicrm","id":136373,"avatar_url":"https://avatars.githubusercontent.com/u/136373?v=3","gravatar_id":"","url":"https://api.github.com/users/civicrm","html_url":"https://github.com/civicrm","followers_url":"https://api.github.com/users/civicrm/followers","following_url":"https://api.github.com/users/civicrm/following{/other_user}","gists_url":"https://api.github.com/users/civicrm/gists{/gist_id}","starred_url":"https://api.github.com/users/civicrm/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/civicrm/subscriptions","organizations_url":"https://api.github.com/users/civicrm/orgs","repos_url":"https://api.github.com/users/civicrm/repos","events_url":"https://api.github.com/users/civicrm/events{/privacy}","received_events_url":"https://api.github.com/users/civicrm/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/civicrm/civihr","description":"CiviHR","fork":false,"url":"https://api.github.com/repos/civicrm/civihr","forks_url":"https://api.github.com/repos/civicrm/civihr/forks","keys_url":"https://api.github.com/repos/civicrm/civihr/keys{/key_id}","collaborators_url":"https://api.github.com/repos/civicrm/civihr/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/civicrm/civihr/teams","hooks_url":"https://api.github.com/repos/civicrm/civihr/hooks","issue_events_url":"https://api.github.com/repos/civicrm/civihr/issues/events{/number}","events_url":"https://api.github.com/repos/civicrm/civihr/events","assignees_url":"https://api.github.com/repos/civicrm/civihr/assignees{/user}","branches_url":"https://api.github.com/repos/civicrm/civihr/branches{/branch}","tags_url":"https://api.github.com/repos/civicrm/civihr/tags","blobs_url":"https://api.github.com/repos/civicrm/civihr/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/civicrm/civihr/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/civicrm/civihr/git/refs{/sha}","trees_url":"https://api.github.com/repos/civicrm/civihr/git/trees{/sha}","statuses_url":"https://api.github.com/repos/civicrm/civihr/statuses/{sha}","languages_url":"https://api.github.com/repos/civicrm/civihr/languages","stargazers_url":"https://api.github.com/repos/civicrm/civihr/stargazers","contributors_url":"https://api.github.com/repos/civicrm/civihr/contributors","subscribers_url":"https://api.github.com/repos/civicrm/civihr/subscribers","subscription_url":"https://api.github.com/repos/civicrm/civihr/subscription","commits_url":"https://api.github.com/repos/civicrm/civihr/commits{/sha}","git_commits_url":"https://api.github.com/repos/civicrm/civihr/git/commits{/sha}","comments_url":"https://api.github.com/repos/civicrm/civihr/comments{/number}","issue_comment_url":"https://api.github.com/repos/civicrm/civihr/issues/comments{/number}","contents_url":"https://api.github.com/repos/civicrm/civihr/contents/{+path}","compare_url":"https://api.github.com/repos/civicrm/civihr/compare/{base}...{head}","merges_url":"https://api.github.com/repos/civicrm/civihr/merges","archive_url":"https://api.github.com/repos/civicrm/civihr/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/civicrm/civihr/downloads","issues_url":"https://api.github.com/repos/civicrm/civihr/issues{/number}","pulls_url":"https://api.github.com/repos/civicrm/civihr/pulls{/number}","milestones_url":"https://api.github.com/repos/civicrm/civihr/milestones{/number}","notifications_url":"https://api.github.com/repos/civicrm/civihr/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/civicrm/civihr/labels{/name}","releases_url":"https://api.github.com/repos/civicrm/civihr/releases{/id}","deployments_url":"https://api.github.com/repos/civicrm/civihr/deployments","created_at":"2013-05-15T20:06:06Z","updated_at":"2016-10-25T11:36:09Z","pushed_at":"2016-12-26T09:58:09Z","git_url":"git://github.com/civicrm/civihr.git","ssh_url":"git@github.com:civicrm/civihr.git","clone_url":"https://github.com/civicrm/civihr.git","svn_url":"https://github.com/civicrm/civihr","homepage":null,"size":37308,"stargazers_count":4,"watchers_count":4,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":27,"mirror_url":null,"open_issues_count":29,"forks":27,"open_issues":29,"watchers":4,"default_branch":"staging"}},"_links":{"self":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/1311"},"html":{"href":"https://github.com/civicrm/civihr/pull/1311"},"issue":{"href":"https://api.github.com/repos/civicrm/civihr/issues/1311"},"comments":{"href":"https://api.github.com/repos/civicrm/civihr/issues/1311/comments"},"review_comments":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/1311/comments"},"review_comment":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/1311/commits"},"statuses":{"href":"https://api.github.com/repos/civicrm/civihr/statuses/47076c86aef694e258c461dca94b458629825946"}}}}
{"actor":{"display_login":"omarabuhussein"}}