{"action":"created","comment":{"url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/comments/59864620","id":59864620,"diff_hunk":"@@ -32,6 +33,105 @@ def active_companies\n     self.companies.where(\"user_companies.active = TRUE\")\n   end\n \n+  def upcoming_off_days\n+    self.off_days.where(\"approved = TRUE and off_days.from IN (?)\", (Date.today..Date.today+30.days))\n+  end\n+\n+  def total_stats(company)\n+    all_records = TimeTable.where(user_id: self.id, company_id: company.id, approved: true)\n+    if all_records.any?\n+      first_day = all_records.first.start.to_date\n+    else\n+      first_day = Date.today\n+    end\n+    last_day = Date.today\n+    return self.stats(company,first_day,last_day)\n+  end\n+\n+  def stats(company, from, to)\n+    stats = { normal_minutes: 0, holiday_minutes: 0, weekend_minutes: 0, offday_minutes: 0, vacation_days: 0, sick_days: 0, other_off_days: 0, total_work_days: 0, work_days: 0, weekend_work_days: 0, holiday_work_days: 0, offday_work_days: 0, not_on_work_days: 0 }\n+\n+    all_records = TimeTable.where(\"user_id = ? and company_id = ? and approved = true and date(start) between ? and ? and multirecord = FALSE\",\n+                                  self.id, company.id, from, to)\n+    all_offdays = OffDay.where(\"user_id = ? and company_id = ? and approved = true and (off_days.from between ? and ? or off_days.to between ? and ?)\",\n+                                  self.id, company.id, from, to, from, to)\n+    all_holidays = company.all_holidays.where(\"date between ? and ?\", from, to)\n+\n+    days_when_user_worked = all_records.pluck(\"start\").map{ |datetime| datetime.to_date}.to_set\n+    days_when_user_was_on_offday = Set.new\n+    all_offdays.each do |offday|\n+      dates = (offday.from..offday.to).to_set\n+      days_when_user_was_on_offday += dates\n+    end\n+\n+    days_when_it_was_holiday = all_holidays.pluck(\"date\").map{ |datetime| datetime.to_date}.to_set\n+    monday_to_friday = (from..to).select{|day| (1..5).include?(day.wday) }.to_set\n+\n+    days_when_user_was_on_offday = days_when_user_was_on_offday & monday_to_friday\n+\n+\n+    stats[:total_work_days] = (monday_to_friday - days_when_it_was_holiday).count\n+    stats[:work_days] = ((days_when_user_worked - days_when_user_was_on_offday - days_when_it_was_holiday) & monday_to_friday).count\n+    stats[:weekend_work_days] = (days_when_user_worked - monday_to_friday).count\n+    stats[:holiday_work_days] = (days_when_user_worked & days_when_it_was_holiday).count\n+    stats[:offday_work_days] = (days_when_user_worked & days_when_user_was_on_offday).count\n+    stats[:not_on_work_days] = (monday_to_friday - days_when_it_was_holiday - days_when_user_was_on_offday - days_when_user_worked).count\n+\n+\n+\n+    stats[:normal_minutes] = all_records.where(\"date(start) IN (?)\",\n+                                        (days_when_user_worked-\n+                                        days_when_it_was_holiday-\n+                                        days_when_user_was_on_offday)&\n+                                        monday_to_friday\n+                                        ).sum(:total_minutes)\n+\n+    stats[:normal_minutes] -= stats[:not_on_work_days]*480","path":"app/models/user.rb","position":65,"original_position":65,"commit_id":"82712b5339b456deb31cff39325783926a96ab4f","original_commit_id":"82712b5339b456deb31cff39325783926a96ab4f","user":{"login":"sls-lnluksa","id":6939272,"avatar_url":"https://avatars.githubusercontent.com/u/6939272?v=3","gravatar_id":"","url":"https://api.github.com/users/sls-lnluksa","html_url":"https://github.com/sls-lnluksa","followers_url":"https://api.github.com/users/sls-lnluksa/followers","following_url":"https://api.github.com/users/sls-lnluksa/following{/other_user}","gists_url":"https://api.github.com/users/sls-lnluksa/gists{/gist_id}","starred_url":"https://api.github.com/users/sls-lnluksa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sls-lnluksa/subscriptions","organizations_url":"https://api.github.com/users/sls-lnluksa/orgs","repos_url":"https://api.github.com/users/sls-lnluksa/repos","events_url":"https://api.github.com/users/sls-lnluksa/events{/privacy}","received_events_url":"https://api.github.com/users/sls-lnluksa/received_events","type":"User","site_admin":false},"body":"Add # TODO\r\nThis does not work correct. We may calculate minutes for days before user start to work.","created_at":"2016-04-15T12:08:06Z","updated_at":"2016-04-15T12:08:06Z","html_url":"https://github.com/sls-lnluksa/work_time_tracker/pull/41#discussion_r59864620","pull_request_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41","_links":{"self":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/comments/59864620"},"html":{"href":"https://github.com/sls-lnluksa/work_time_tracker/pull/41#discussion_r59864620"},"pull_request":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41"}}},"pull_request":{"url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41","id":65579505,"html_url":"https://github.com/sls-lnluksa/work_time_tracker/pull/41","diff_url":"https://github.com/sls-lnluksa/work_time_tracker/pull/41.diff","patch_url":"https://github.com/sls-lnluksa/work_time_tracker/pull/41.patch","issue_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/41","number":41,"state":"open","locked":false,"title":"User stats page","user":{"login":"Baxxx","id":17880055,"avatar_url":"https://avatars.githubusercontent.com/u/17880055?v=3","gravatar_id":"","url":"https://api.github.com/users/Baxxx","html_url":"https://github.com/Baxxx","followers_url":"https://api.github.com/users/Baxxx/followers","following_url":"https://api.github.com/users/Baxxx/following{/other_user}","gists_url":"https://api.github.com/users/Baxxx/gists{/gist_id}","starred_url":"https://api.github.com/users/Baxxx/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Baxxx/subscriptions","organizations_url":"https://api.github.com/users/Baxxx/orgs","repos_url":"https://api.github.com/users/Baxxx/repos","events_url":"https://api.github.com/users/Baxxx/events{/privacy}","received_events_url":"https://api.github.com/users/Baxxx/received_events","type":"User","site_admin":false},"body":"Closes #33 and #19. \r\nThis page prints out statistics for employee (his total/monthly bonus minutes, used vacation/sick days, working on weekends/holidays, etc.\r\nReview pull requests #37 and #31, as this branch uses/updates some code from those branches.","created_at":"2016-04-07T06:23:30Z","updated_at":"2016-04-15T12:08:06Z","closed_at":null,"merged_at":null,"merge_commit_sha":"674ca55551f90f9851e56918ac9ba2557a1815bd","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41/commits","review_comments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41/comments","review_comment_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/comments{/number}","comments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/41/comments","statuses_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/statuses/82712b5339b456deb31cff39325783926a96ab4f","head":{"label":"sls-lnluksa:user_stats_page","ref":"user_stats_page","sha":"82712b5339b456deb31cff39325783926a96ab4f","user":{"login":"sls-lnluksa","id":6939272,"avatar_url":"https://avatars.githubusercontent.com/u/6939272?v=3","gravatar_id":"","url":"https://api.github.com/users/sls-lnluksa","html_url":"https://github.com/sls-lnluksa","followers_url":"https://api.github.com/users/sls-lnluksa/followers","following_url":"https://api.github.com/users/sls-lnluksa/following{/other_user}","gists_url":"https://api.github.com/users/sls-lnluksa/gists{/gist_id}","starred_url":"https://api.github.com/users/sls-lnluksa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sls-lnluksa/subscriptions","organizations_url":"https://api.github.com/users/sls-lnluksa/orgs","repos_url":"https://api.github.com/users/sls-lnluksa/repos","events_url":"https://api.github.com/users/sls-lnluksa/events{/privacy}","received_events_url":"https://api.github.com/users/sls-lnluksa/received_events","type":"User","site_admin":false},"repo":{"id":54373593,"name":"work_time_tracker","full_name":"sls-lnluksa/work_time_tracker","owner":{"login":"sls-lnluksa","id":6939272,"avatar_url":"https://avatars.githubusercontent.com/u/6939272?v=3","gravatar_id":"","url":"https://api.github.com/users/sls-lnluksa","html_url":"https://github.com/sls-lnluksa","followers_url":"https://api.github.com/users/sls-lnluksa/followers","following_url":"https://api.github.com/users/sls-lnluksa/following{/other_user}","gists_url":"https://api.github.com/users/sls-lnluksa/gists{/gist_id}","starred_url":"https://api.github.com/users/sls-lnluksa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sls-lnluksa/subscriptions","organizations_url":"https://api.github.com/users/sls-lnluksa/orgs","repos_url":"https://api.github.com/users/sls-lnluksa/repos","events_url":"https://api.github.com/users/sls-lnluksa/events{/privacy}","received_events_url":"https://api.github.com/users/sls-lnluksa/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sls-lnluksa/work_time_tracker","description":"","fork":false,"url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker","forks_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/forks","keys_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/teams","hooks_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/hooks","issue_events_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/events{/number}","events_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/events","assignees_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/assignees{/user}","branches_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/branches{/branch}","tags_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/tags","blobs_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/refs{/sha}","trees_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/statuses/{sha}","languages_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/languages","stargazers_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/stargazers","contributors_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/contributors","subscribers_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/subscribers","subscription_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/subscription","commits_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/commits{/sha}","git_commits_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/commits{/sha}","comments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/comments{/number}","issue_comment_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/comments{/number}","contents_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/contents/{+path}","compare_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/merges","archive_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/downloads","issues_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues{/number}","pulls_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls{/number}","milestones_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/milestones{/number}","notifications_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/labels{/name}","releases_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/releases{/id}","deployments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/deployments","created_at":"2016-03-21T08:56:28Z","updated_at":"2016-03-29T12:39:57Z","pushed_at":"2016-04-15T10:26:41Z","git_url":"git://github.com/sls-lnluksa/work_time_tracker.git","ssh_url":"git@github.com:sls-lnluksa/work_time_tracker.git","clone_url":"https://github.com/sls-lnluksa/work_time_tracker.git","svn_url":"https://github.com/sls-lnluksa/work_time_tracker","homepage":null,"size":386,"stargazers_count":1,"watchers_count":1,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":7,"forks":0,"open_issues":7,"watchers":1,"default_branch":"master"}},"base":{"label":"sls-lnluksa:master","ref":"master","sha":"f879651ff48c69747a5b300d7343752b5af9cf61","user":{"login":"sls-lnluksa","id":6939272,"avatar_url":"https://avatars.githubusercontent.com/u/6939272?v=3","gravatar_id":"","url":"https://api.github.com/users/sls-lnluksa","html_url":"https://github.com/sls-lnluksa","followers_url":"https://api.github.com/users/sls-lnluksa/followers","following_url":"https://api.github.com/users/sls-lnluksa/following{/other_user}","gists_url":"https://api.github.com/users/sls-lnluksa/gists{/gist_id}","starred_url":"https://api.github.com/users/sls-lnluksa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sls-lnluksa/subscriptions","organizations_url":"https://api.github.com/users/sls-lnluksa/orgs","repos_url":"https://api.github.com/users/sls-lnluksa/repos","events_url":"https://api.github.com/users/sls-lnluksa/events{/privacy}","received_events_url":"https://api.github.com/users/sls-lnluksa/received_events","type":"User","site_admin":false},"repo":{"id":54373593,"name":"work_time_tracker","full_name":"sls-lnluksa/work_time_tracker","owner":{"login":"sls-lnluksa","id":6939272,"avatar_url":"https://avatars.githubusercontent.com/u/6939272?v=3","gravatar_id":"","url":"https://api.github.com/users/sls-lnluksa","html_url":"https://github.com/sls-lnluksa","followers_url":"https://api.github.com/users/sls-lnluksa/followers","following_url":"https://api.github.com/users/sls-lnluksa/following{/other_user}","gists_url":"https://api.github.com/users/sls-lnluksa/gists{/gist_id}","starred_url":"https://api.github.com/users/sls-lnluksa/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/sls-lnluksa/subscriptions","organizations_url":"https://api.github.com/users/sls-lnluksa/orgs","repos_url":"https://api.github.com/users/sls-lnluksa/repos","events_url":"https://api.github.com/users/sls-lnluksa/events{/privacy}","received_events_url":"https://api.github.com/users/sls-lnluksa/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/sls-lnluksa/work_time_tracker","description":"","fork":false,"url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker","forks_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/forks","keys_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/keys{/key_id}","collaborators_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/teams","hooks_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/hooks","issue_events_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/events{/number}","events_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/events","assignees_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/assignees{/user}","branches_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/branches{/branch}","tags_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/tags","blobs_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/refs{/sha}","trees_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/trees{/sha}","statuses_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/statuses/{sha}","languages_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/languages","stargazers_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/stargazers","contributors_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/contributors","subscribers_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/subscribers","subscription_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/subscription","commits_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/commits{/sha}","git_commits_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/git/commits{/sha}","comments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/comments{/number}","issue_comment_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/comments{/number}","contents_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/contents/{+path}","compare_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/compare/{base}...{head}","merges_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/merges","archive_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/downloads","issues_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues{/number}","pulls_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls{/number}","milestones_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/milestones{/number}","notifications_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/labels{/name}","releases_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/releases{/id}","deployments_url":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/deployments","created_at":"2016-03-21T08:56:28Z","updated_at":"2016-03-29T12:39:57Z","pushed_at":"2016-04-15T10:26:41Z","git_url":"git://github.com/sls-lnluksa/work_time_tracker.git","ssh_url":"git@github.com:sls-lnluksa/work_time_tracker.git","clone_url":"https://github.com/sls-lnluksa/work_time_tracker.git","svn_url":"https://github.com/sls-lnluksa/work_time_tracker","homepage":null,"size":386,"stargazers_count":1,"watchers_count":1,"language":"Ruby","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":7,"forks":0,"open_issues":7,"watchers":1,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41"},"html":{"href":"https://github.com/sls-lnluksa/work_time_tracker/pull/41"},"issue":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/41"},"comments":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/issues/41/comments"},"review_comments":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41/comments"},"review_comment":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/pulls/41/commits"},"statuses":{"href":"https://api.github.com/repos/sls-lnluksa/work_time_tracker/statuses/82712b5339b456deb31cff39325783926a96ab4f"}}}}