{"action":"created","comment":{"url":"https://api.github.com/repos/annconst/payment-events/pulls/comments/53451532","id":53451532,"diff_hunk":"@@ -35,52 +35,72 @@ def edit\n \n   def update\n     @event = Event.find(params[:id])\n-    if event_params.present?\n-      @event.update(event_params)\n-    end\n-      redirect_to events_path\n+    return redirect_to event_path(@event) if @event.update(event_params)\n+    render 'edit'\n   end\n \n   def destroy\n-    @event = Event.find(params[:id])\n-    @event.destroy\n+    @event = Event.where(id: params[:id]).first.destroy\n     redirect_to events_path\n   end\n \n   def calculate\n-    return redirect_to :back, alert: t(:create_list_of_products_and_add_product_to_list) if params[:products].blank?\n-    Product.update(params[:products].keys, params[:products].values)\n     @event = Event.find(params[:event_id])\n-    @users_hash = {}\n-    @sum = 0\n-    @event.product_lists.each do |list|\n-      list.without_users? ? @sum = @sum + list.all_price : increase_users_sum(list)\n-    end\n-\n-    @users_hash = @users_hash.map{ |l, v| { name:User.find(l).name, value: v , money: get_user_money(l)} }\n-  end\n-\n-  def get_user_money(user_id)\n-    @event.invites.where(user_id: user_id).first.user_money\n+    return redirect_to :back, alert: t(:event_search_failed) unless @event\n+    @users_debts = debts_calculation(@event)\n+    @money_transactions = debts_transactions(@users_debts)\n   end\n \n   def event_report\n     @event = Event.find(params[:event_id])\n-    if @event.present?\n-      @lists = @event.product_lists\n-    end\n+    @lists = @event.product_lists if @event.present?\n   end\n \n   private\n \n-  def increase_users_sum(list)\n-    list.users.each do |user|\n-      @users_hash[user.id] = @users_hash[user.id].to_f + list.all_price/ list.users.count\n+  def debts_calculation(event)\n+    debts_hash = Hash.new(0)\n+    event.product_lists.each do |list|\n+      list.users.each do |user|\n+        debts_hash[user.id] += list.average_price\n+      end\n     end\n+    debts_hash.map { |user_id, money_required| Participant.new(user_id, money_required, event) }\n+  end\n+\n+  def debts_transactions(users)\n+    transactions = []\n+    while user_with_minimal_debt(users).debt < 0\n+      payer = user_with_maximal_debt(users)\n+      recipient = user_with_minimal_debt(users)\n+      payer_owes_less_than_recipient_needs = recipient.debt.abs >= payer.debt\n+      transactions << create_transaction(payer, recipient, payer_owes_less_than_recipient_needs)","path":"app/controllers/events_controller.rb","position":86,"original_position":86,"commit_id":"9dbd729c6c0e11531d3de634555c9376d68e9353","original_commit_id":"9dbd729c6c0e11531d3de634555c9376d68e9353","user":{"login":"reshetov","id":1003798,"avatar_url":"https://avatars.githubusercontent.com/u/1003798?v=3","gravatar_id":"","url":"https://api.github.com/users/reshetov","html_url":"https://github.com/reshetov","followers_url":"https://api.github.com/users/reshetov/followers","following_url":"https://api.github.com/users/reshetov/following{/other_user}","gists_url":"https://api.github.com/users/reshetov/gists{/gist_id}","starred_url":"https://api.github.com/users/reshetov/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/reshetov/subscriptions","organizations_url":"https://api.github.com/users/reshetov/orgs","repos_url":"https://api.github.com/users/reshetov/repos","events_url":"https://api.github.com/users/reshetov/events{/privacy}","received_events_url":"https://api.github.com/users/reshetov/received_events","type":"User","site_admin":false},"body":"+","created_at":"2016-02-19T11:46:44Z","updated_at":"2016-02-19T11:46:44Z","html_url":"https://github.com/annconst/payment-events/pull/13#discussion_r53451532","pull_request_url":"https://api.github.com/repos/annconst/payment-events/pulls/13","_links":{"self":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/comments/53451532"},"html":{"href":"https://github.com/annconst/payment-events/pull/13#discussion_r53451532"},"pull_request":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/13"}}},"pull_request":{"url":"https://api.github.com/repos/annconst/payment-events/pulls/13","id":59789271,"html_url":"https://github.com/annconst/payment-events/pull/13","diff_url":"https://github.com/annconst/payment-events/pull/13.diff","patch_url":"https://github.com/annconst/payment-events/pull/13.patch","issue_url":"https://api.github.com/repos/annconst/payment-events/issues/13","number":13,"state":"open","locked":false,"title":"Calcs","user":{"login":"SidorenkoD","id":17138519,"avatar_url":"https://avatars.githubusercontent.com/u/17138519?v=3","gravatar_id":"","url":"https://api.github.com/users/SidorenkoD","html_url":"https://github.com/SidorenkoD","followers_url":"https://api.github.com/users/SidorenkoD/followers","following_url":"https://api.github.com/users/SidorenkoD/following{/other_user}","gists_url":"https://api.github.com/users/SidorenkoD/gists{/gist_id}","starred_url":"https://api.github.com/users/SidorenkoD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SidorenkoD/subscriptions","organizations_url":"https://api.github.com/users/SidorenkoD/orgs","repos_url":"https://api.github.com/users/SidorenkoD/repos","events_url":"https://api.github.com/users/SidorenkoD/events{/privacy}","received_events_url":"https://api.github.com/users/SidorenkoD/received_events","type":"User","site_admin":false},"body":"","created_at":"2016-02-18T12:05:01Z","updated_at":"2016-02-19T11:46:44Z","closed_at":null,"merged_at":null,"merge_commit_sha":"e8231e6ee3a0bbbdc99522d1065d7e77d9d99319","assignee":null,"milestone":null,"commits_url":"https://api.github.com/repos/annconst/payment-events/pulls/13/commits","review_comments_url":"https://api.github.com/repos/annconst/payment-events/pulls/13/comments","review_comment_url":"https://api.github.com/repos/annconst/payment-events/pulls/comments{/number}","comments_url":"https://api.github.com/repos/annconst/payment-events/issues/13/comments","statuses_url":"https://api.github.com/repos/annconst/payment-events/statuses/9dbd729c6c0e11531d3de634555c9376d68e9353","head":{"label":"annconst:calcs","ref":"calcs","sha":"9dbd729c6c0e11531d3de634555c9376d68e9353","user":{"login":"annconst","id":12441117,"avatar_url":"https://avatars.githubusercontent.com/u/12441117?v=3","gravatar_id":"","url":"https://api.github.com/users/annconst","html_url":"https://github.com/annconst","followers_url":"https://api.github.com/users/annconst/followers","following_url":"https://api.github.com/users/annconst/following{/other_user}","gists_url":"https://api.github.com/users/annconst/gists{/gist_id}","starred_url":"https://api.github.com/users/annconst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annconst/subscriptions","organizations_url":"https://api.github.com/users/annconst/orgs","repos_url":"https://api.github.com/users/annconst/repos","events_url":"https://api.github.com/users/annconst/events{/privacy}","received_events_url":"https://api.github.com/users/annconst/received_events","type":"User","site_admin":false},"repo":{"id":35816524,"name":"payment-events","full_name":"annconst/payment-events","owner":{"login":"annconst","id":12441117,"avatar_url":"https://avatars.githubusercontent.com/u/12441117?v=3","gravatar_id":"","url":"https://api.github.com/users/annconst","html_url":"https://github.com/annconst","followers_url":"https://api.github.com/users/annconst/followers","following_url":"https://api.github.com/users/annconst/following{/other_user}","gists_url":"https://api.github.com/users/annconst/gists{/gist_id}","starred_url":"https://api.github.com/users/annconst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annconst/subscriptions","organizations_url":"https://api.github.com/users/annconst/orgs","repos_url":"https://api.github.com/users/annconst/repos","events_url":"https://api.github.com/users/annconst/events{/privacy}","received_events_url":"https://api.github.com/users/annconst/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/annconst/payment-events","description":"","fork":false,"url":"https://api.github.com/repos/annconst/payment-events","forks_url":"https://api.github.com/repos/annconst/payment-events/forks","keys_url":"https://api.github.com/repos/annconst/payment-events/keys{/key_id}","collaborators_url":"https://api.github.com/repos/annconst/payment-events/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/annconst/payment-events/teams","hooks_url":"https://api.github.com/repos/annconst/payment-events/hooks","issue_events_url":"https://api.github.com/repos/annconst/payment-events/issues/events{/number}","events_url":"https://api.github.com/repos/annconst/payment-events/events","assignees_url":"https://api.github.com/repos/annconst/payment-events/assignees{/user}","branches_url":"https://api.github.com/repos/annconst/payment-events/branches{/branch}","tags_url":"https://api.github.com/repos/annconst/payment-events/tags","blobs_url":"https://api.github.com/repos/annconst/payment-events/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/annconst/payment-events/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/annconst/payment-events/git/refs{/sha}","trees_url":"https://api.github.com/repos/annconst/payment-events/git/trees{/sha}","statuses_url":"https://api.github.com/repos/annconst/payment-events/statuses/{sha}","languages_url":"https://api.github.com/repos/annconst/payment-events/languages","stargazers_url":"https://api.github.com/repos/annconst/payment-events/stargazers","contributors_url":"https://api.github.com/repos/annconst/payment-events/contributors","subscribers_url":"https://api.github.com/repos/annconst/payment-events/subscribers","subscription_url":"https://api.github.com/repos/annconst/payment-events/subscription","commits_url":"https://api.github.com/repos/annconst/payment-events/commits{/sha}","git_commits_url":"https://api.github.com/repos/annconst/payment-events/git/commits{/sha}","comments_url":"https://api.github.com/repos/annconst/payment-events/comments{/number}","issue_comment_url":"https://api.github.com/repos/annconst/payment-events/issues/comments{/number}","contents_url":"https://api.github.com/repos/annconst/payment-events/contents/{+path}","compare_url":"https://api.github.com/repos/annconst/payment-events/compare/{base}...{head}","merges_url":"https://api.github.com/repos/annconst/payment-events/merges","archive_url":"https://api.github.com/repos/annconst/payment-events/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/annconst/payment-events/downloads","issues_url":"https://api.github.com/repos/annconst/payment-events/issues{/number}","pulls_url":"https://api.github.com/repos/annconst/payment-events/pulls{/number}","milestones_url":"https://api.github.com/repos/annconst/payment-events/milestones{/number}","notifications_url":"https://api.github.com/repos/annconst/payment-events/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/annconst/payment-events/labels{/name}","releases_url":"https://api.github.com/repos/annconst/payment-events/releases{/id}","deployments_url":"https://api.github.com/repos/annconst/payment-events/deployments","created_at":"2015-05-18T12:26:34Z","updated_at":"2016-02-15T09:19:54Z","pushed_at":"2016-02-19T11:22:14Z","git_url":"git://github.com/annconst/payment-events.git","ssh_url":"git@github.com:annconst/payment-events.git","clone_url":"https://github.com/annconst/payment-events.git","svn_url":"https://github.com/annconst/payment-events","homepage":null,"size":399,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"base":{"label":"annconst:master","ref":"master","sha":"c7890142b645feae397c9391d97e5193e720e565","user":{"login":"annconst","id":12441117,"avatar_url":"https://avatars.githubusercontent.com/u/12441117?v=3","gravatar_id":"","url":"https://api.github.com/users/annconst","html_url":"https://github.com/annconst","followers_url":"https://api.github.com/users/annconst/followers","following_url":"https://api.github.com/users/annconst/following{/other_user}","gists_url":"https://api.github.com/users/annconst/gists{/gist_id}","starred_url":"https://api.github.com/users/annconst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annconst/subscriptions","organizations_url":"https://api.github.com/users/annconst/orgs","repos_url":"https://api.github.com/users/annconst/repos","events_url":"https://api.github.com/users/annconst/events{/privacy}","received_events_url":"https://api.github.com/users/annconst/received_events","type":"User","site_admin":false},"repo":{"id":35816524,"name":"payment-events","full_name":"annconst/payment-events","owner":{"login":"annconst","id":12441117,"avatar_url":"https://avatars.githubusercontent.com/u/12441117?v=3","gravatar_id":"","url":"https://api.github.com/users/annconst","html_url":"https://github.com/annconst","followers_url":"https://api.github.com/users/annconst/followers","following_url":"https://api.github.com/users/annconst/following{/other_user}","gists_url":"https://api.github.com/users/annconst/gists{/gist_id}","starred_url":"https://api.github.com/users/annconst/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/annconst/subscriptions","organizations_url":"https://api.github.com/users/annconst/orgs","repos_url":"https://api.github.com/users/annconst/repos","events_url":"https://api.github.com/users/annconst/events{/privacy}","received_events_url":"https://api.github.com/users/annconst/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/annconst/payment-events","description":"","fork":false,"url":"https://api.github.com/repos/annconst/payment-events","forks_url":"https://api.github.com/repos/annconst/payment-events/forks","keys_url":"https://api.github.com/repos/annconst/payment-events/keys{/key_id}","collaborators_url":"https://api.github.com/repos/annconst/payment-events/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/annconst/payment-events/teams","hooks_url":"https://api.github.com/repos/annconst/payment-events/hooks","issue_events_url":"https://api.github.com/repos/annconst/payment-events/issues/events{/number}","events_url":"https://api.github.com/repos/annconst/payment-events/events","assignees_url":"https://api.github.com/repos/annconst/payment-events/assignees{/user}","branches_url":"https://api.github.com/repos/annconst/payment-events/branches{/branch}","tags_url":"https://api.github.com/repos/annconst/payment-events/tags","blobs_url":"https://api.github.com/repos/annconst/payment-events/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/annconst/payment-events/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/annconst/payment-events/git/refs{/sha}","trees_url":"https://api.github.com/repos/annconst/payment-events/git/trees{/sha}","statuses_url":"https://api.github.com/repos/annconst/payment-events/statuses/{sha}","languages_url":"https://api.github.com/repos/annconst/payment-events/languages","stargazers_url":"https://api.github.com/repos/annconst/payment-events/stargazers","contributors_url":"https://api.github.com/repos/annconst/payment-events/contributors","subscribers_url":"https://api.github.com/repos/annconst/payment-events/subscribers","subscription_url":"https://api.github.com/repos/annconst/payment-events/subscription","commits_url":"https://api.github.com/repos/annconst/payment-events/commits{/sha}","git_commits_url":"https://api.github.com/repos/annconst/payment-events/git/commits{/sha}","comments_url":"https://api.github.com/repos/annconst/payment-events/comments{/number}","issue_comment_url":"https://api.github.com/repos/annconst/payment-events/issues/comments{/number}","contents_url":"https://api.github.com/repos/annconst/payment-events/contents/{+path}","compare_url":"https://api.github.com/repos/annconst/payment-events/compare/{base}...{head}","merges_url":"https://api.github.com/repos/annconst/payment-events/merges","archive_url":"https://api.github.com/repos/annconst/payment-events/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/annconst/payment-events/downloads","issues_url":"https://api.github.com/repos/annconst/payment-events/issues{/number}","pulls_url":"https://api.github.com/repos/annconst/payment-events/pulls{/number}","milestones_url":"https://api.github.com/repos/annconst/payment-events/milestones{/number}","notifications_url":"https://api.github.com/repos/annconst/payment-events/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/annconst/payment-events/labels{/name}","releases_url":"https://api.github.com/repos/annconst/payment-events/releases{/id}","deployments_url":"https://api.github.com/repos/annconst/payment-events/deployments","created_at":"2015-05-18T12:26:34Z","updated_at":"2016-02-15T09:19:54Z","pushed_at":"2016-02-19T11:22:14Z","git_url":"git://github.com/annconst/payment-events.git","ssh_url":"git@github.com:annconst/payment-events.git","clone_url":"https://github.com/annconst/payment-events.git","svn_url":"https://github.com/annconst/payment-events","homepage":null,"size":399,"stargazers_count":0,"watchers_count":0,"language":"JavaScript","has_issues":true,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":2,"forks":0,"open_issues":2,"watchers":0,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/13"},"html":{"href":"https://github.com/annconst/payment-events/pull/13"},"issue":{"href":"https://api.github.com/repos/annconst/payment-events/issues/13"},"comments":{"href":"https://api.github.com/repos/annconst/payment-events/issues/13/comments"},"review_comments":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/13/comments"},"review_comment":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/annconst/payment-events/pulls/13/commits"},"statuses":{"href":"https://api.github.com/repos/annconst/payment-events/statuses/9dbd729c6c0e11531d3de634555c9376d68e9353"}}}}