{"action":"created","comment":{"url":"https://api.github.com/repos/civicrm/civihr/pulls/comments/53808719","id":53808719,"diff_hunk":"@@ -110,6 +110,161 @@ public static function findById($id) {\n   }\n \n   /**\n+   * Get 'length_of_service' in days for given Contact ID, and optionally\n+   * Date and Break (allowed number of days between Contracts).\n+   * \n+   * @param int $contactId\n+   * @param string $date\n+   * @param int $break\n+   * @throws Exception\n+   * @return int\n+   */\n+  public static function getLengthOfService($contactId, $date = null, $break = 14) {\n+    if (empty($contactId))\n+    {\n+      throw new Exception(\"Cannot update Length of Service: no Contact ID provided.\");\n+    }\n+    $dates = array();\n+    $serviceStartDate = null;\n+    $serviceEndDate = null;\n+\n+    // Setting $date to today if it's not specified.\n+    if (!$date) {\n+      $date = date('Y-m-d');\n+    }\n+\n+    // Getting all Job Contracts for given Contact ID.\n+    $contracts = civicrm_api3('HRJobContract', 'get', array(\n+      'sequential' => 1,\n+      'contact_id' => (int)$contactId,\n+      'deleted' => 0,\n+      'options' => array('limit' => 0),\n+    ));\n+\n+    // Now we fill $dates array with the Contract Start and End dates\n+    // to get the data structure as below:\n+    // $dates = [\n+    //   'start_date1' => 'end_date1',\n+    //   'start_date2' => 'end_date2',\n+    //   'start_date3' => 'end_date3',\n+    // ];\n+    // If there are two (or more) Contracts starting on the same day\n+    // then we pick only the one with latest End date.\n+    foreach ($contracts['values'] as $contract) {\n+      $details = civicrm_api3('HRJobDetails', 'getsingle', array(\n+        'sequential' => 1,\n+        'jobcontract_id' => (int)$contract['id'],\n+      ));\n+      if (empty($details['period_end_date'])) {\n+        $dates[$details['period_start_date']] = null;\n+        break;\n+      }\n+      if (!empty($dates[$details['period_start_date']])) {\n+        if ($details['period_end_date'] > $dates[$details['period_end_date']]) {\n+          $dates[$details['period_start_date']] = $details['period_end_date'];\n+        }\n+        continue;\n+      }\n+      $dates[$details['period_start_date']] = $details['period_end_date'];\n+    }\n+\n+    // Sorting $dates array by keys.\n+    ksort($dates);\n+\n+    // Calculate Service Start Date and Service End Date.\n+    foreach ($dates as $startDate => $endDate) {\n+      if (!$serviceStartDate) {\n+        $serviceStartDate = $startDate;\n+      }\n+      if (!$serviceEndDate) {\n+        $serviceEndDate = $endDate;\n+      }\n+      if ($startDate <= self::sumDateAndBreak($serviceEndDate, $break)) {\n+        $serviceEndDate = $endDate;\n+      } else {\n+        $serviceStartDate = $startDate;\n+        $serviceEndDate = $endDate;\n+      }\n+      if (!$serviceEndDate) {\n+        break;\n+      }\n+    }\n+\n+    // Restrict $serviceEndDate to date defined at the top,\n+    // so we won't get an infinite Service length.\n+    if (!$serviceEndDate ||  $serviceEndDate > $date) {\n+      $serviceEndDate = $date;\n+    }\n+\n+    // If the latest Contract has ended more than $break days ago, we return 0.\n+    if ($date > self::sumDateAndBreak($serviceEndDate, $break)) {\n+      return 0;\n+    }\n+\n+    // Final Length of Service calculations by difference of both dates.\n+    $dStart = new DateTime($serviceStartDate);\n+    $dEnd  = new DateTime($serviceEndDate);\n+    $dDiff = $dStart->diff($dEnd);\n+    return $dDiff->days;\n+  }\n+\n+  /** Calculate a new Date which is sum of given Date and number of Break days.\n+   * Returns null if given date is null.\n+   * \n+   * @param string $date\n+   * @param int $break\n+   * @return string\n+   */\n+  protected static function sumDateAndBreak($date, $break) {\n+    if (!$date) {\n+      return null;\n+    }\n+    $newDate = new DateTime($date);\n+    $newDate->add(new DateInterval('P' . $break . 'D'));\n+    return $newDate->format('Y-m-d');\n+  }\n+  \n+  /**\n+   * Update Length of Service for specific Contact.\n+   * ","path":"hrjobcontract/CRM/Hrjobcontract/BAO/HRJobContract.php","position":120,"original_position":120,"commit_id":"30197cbcecbebe87dfea3d226a744907f2e82621","original_commit_id":"30197cbcecbebe87dfea3d226a744907f2e82621","user":{"login":"AkA84","id":6400898,"avatar_url":"https://avatars.githubusercontent.com/u/6400898?v=3","gravatar_id":"","url":"https://api.github.com/users/AkA84","html_url":"https://github.com/AkA84","followers_url":"https://api.github.com/users/AkA84/followers","following_url":"https://api.github.com/users/AkA84/following{/other_user}","gists_url":"https://api.github.com/users/AkA84/gists{/gist_id}","starred_url":"https://api.github.com/users/AkA84/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/AkA84/subscriptions","organizations_url":"https://api.github.com/users/AkA84/orgs","repos_url":"https://api.github.com/users/AkA84/repos","events_url":"https://api.github.com/users/AkA84/events{/privacy}","received_events_url":"https://api.github.com/users/AkA84/received_events","type":"User","site_admin":false},"body":"Missing definition of the parameter here","created_at":"2016-02-23T16:44:05Z","updated_at":"2016-02-23T16:44:05Z","html_url":"https://github.com/civicrm/civihr/pull/865#discussion_r53808719","pull_request_url":"https://api.github.com/repos/civicrm/civihr/pulls/865","_links":{"self":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/comments/53808719"},"html":{"href":"https://github.com/civicrm/civihr/pull/865#discussion_r53808719"},"pull_request":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/865"}}},"pull_request":{"url":"https://api.github.com/repos/civicrm/civihr/pulls/865","id":60318052,"html_url":"https://github.com/civicrm/civihr/pull/865","diff_url":"https://github.com/civicrm/civihr/pull/865.diff","patch_url":"https://github.com/civicrm/civihr/pull/865.patch","issue_url":"https://api.github.com/repos/civicrm/civihr/issues/865","number":865,"state":"open","locked":false,"title":"PCHR-846 - Length of Service","user":{"login":"coldrunKacper","id":8986209,"avatar_url":"https://avatars.githubusercontent.com/u/8986209?v=3","gravatar_id":"","url":"https://api.github.com/users/coldrunKacper","html_url":"https://github.com/coldrunKacper","followers_url":"https://api.github.com/users/coldrunKacper/followers","following_url":"https://api.github.com/users/coldrunKacper/following{/other_user}","gists_url":"https://api.github.com/users/coldrunKacper/gists{/gist_id}","starred_url":"https://api.github.com/users/coldrunKacper/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/coldrunKacper/subscriptions","organizations_url":"https://api.github.com/users/coldrunKacper/orgs","repos_url":"https://api.github.com/users/coldrunKacper/repos","events_url":"https://api.github.com/users/coldrunKacper/events{/privacy}","received_events_url":"https://api.github.com/users/coldrunKacper/received_events","type":"User","site_admin":false},"body":"Implementing 'length of service' Custom Field for Contact entity and creating three custom API calls on HRJobContract CiviCRM entity.\r\n\r\nCustom Length of Service API calls:\r\n\r\n**Update the Length of Service value(s)**\r\nentity: HRJobContract\r\naction: updatelengthofservice\r\nparams: contact_id (optional)\r\ndescription: automatically updates Length of Service value for specific Contact ID or Length of Service for all Contacts if no 'contact_id' is provided.\r\nexample:\r\n```\r\nCRM.api3('HRJobContract', 'updatelengthofservice', {\r\n  \"sequential\": 1,\r\n  \"contact_id\": 49 // optional\r\n}).done(function(result) {\r\n  // do something\r\n});\r\n```\r\n// response:\r\n```\r\n{\r\n\t\"is_error\":0,\r\n\t\"version\":3,\r\n\t\"count\":1,\r\n\t\"values\":true\r\n}\r\n```\r\n\r\n**Calculate the Length of Service for specified Contact**\r\nentity: HRJobContract\r\naction: getlengthofservice\r\nparams: contact_id (required)\r\ndescription: calculates and returns Length of Service (for specified Contact ID) in days as Integer\r\nexample:\r\n```\r\nCRM.api3('HRJobContract', 'getlengthofservice', {\r\n  \"sequential\": 1,\r\n  \"contact_id\": 49\r\n}).done(function(result) {\r\n  // do something\r\n});\r\n```\r\n// response:\r\n```\r\n{\r\n\t\"is_error\":0,\r\n\t\"version\":3,\r\n\t\"count\":1,\r\n\t\"values\":387\r\n}\r\n```","created_at":"2016-02-23T10:29:09Z","updated_at":"2016-02-23T16:44:05Z","closed_at":null,"merged_at":null,"merge_commit_sha":"99faf5d891e73cb5ea7bba2d92544ff3cc2ad78c","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/civicrm/civihr/pulls/865/commits","review_comments_url":"https://api.github.com/repos/civicrm/civihr/pulls/865/comments","review_comment_url":"https://api.github.com/repos/civicrm/civihr/pulls/comments{/number}","comments_url":"https://api.github.com/repos/civicrm/civihr/issues/865/comments","statuses_url":"https://api.github.com/repos/civicrm/civihr/statuses/30197cbcecbebe87dfea3d226a744907f2e82621","head":{"label":"civicrm:PCHR-846-length-of-service","ref":"PCHR-846-length-of-service","sha":"30197cbcecbebe87dfea3d226a744907f2e82621","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-02-17T12:41:09Z","pushed_at":"2016-02-23T16:22:06Z","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":19115,"stargazers_count":2,"watchers_count":2,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":21,"mirror_url":null,"open_issues_count":4,"forks":21,"open_issues":4,"watchers":2,"default_branch":"develop"}},"base":{"label":"civicrm:develop","ref":"develop","sha":"4efb2d50431314c7fac2b25042106b61fe52e201","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-02-17T12:41:09Z","pushed_at":"2016-02-23T16:22:06Z","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":19115,"stargazers_count":2,"watchers_count":2,"language":"PHP","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":21,"mirror_url":null,"open_issues_count":4,"forks":21,"open_issues":4,"watchers":2,"default_branch":"develop"}},"_links":{"self":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/865"},"html":{"href":"https://github.com/civicrm/civihr/pull/865"},"issue":{"href":"https://api.github.com/repos/civicrm/civihr/issues/865"},"comments":{"href":"https://api.github.com/repos/civicrm/civihr/issues/865/comments"},"review_comments":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/865/comments"},"review_comment":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/civicrm/civihr/pulls/865/commits"},"statuses":{"href":"https://api.github.com/repos/civicrm/civihr/statuses/30197cbcecbebe87dfea3d226a744907f2e82621"}}}}