From 0b8a1908b930e1e2b90fb39d86a359f4d39eb3c4 Mon Sep 17 00:00:00 2001 From: Yusuke Sangenya Date: Wed, 1 Aug 2018 21:42:49 +0900 Subject: [PATCH] Consumption calculate feature. --- README.md | 15 +----- .../javascripts/admin/consumptions.coffee | 3 ++ .../stylesheets/admin/consumptions.scss | 3 ++ .../admin/consumptions_controller.rb | 50 +++++++++++++++++++ app/helpers/admin/consumptions_helper.rb | 2 + app/models/consumption.rb | 12 +++-- app/models/drive.rb | 4 +- app/views/admin/consumptions/index.html.erb | 46 +++++++++++++++++ app/views/admin/consumptions/new.html.erb | 18 +++++++ app/views/layouts/application.html.erb | 5 ++ config/routes.rb | 6 +++ lib/tasks/consumption.rake | 11 ---- .../admin/consumptions_controller_test.rb | 7 +++ 13 files changed, 152 insertions(+), 30 deletions(-) create mode 100644 app/assets/javascripts/admin/consumptions.coffee create mode 100644 app/assets/stylesheets/admin/consumptions.scss create mode 100644 app/controllers/admin/consumptions_controller.rb create mode 100644 app/helpers/admin/consumptions_helper.rb create mode 100644 app/views/admin/consumptions/index.html.erb create mode 100644 app/views/admin/consumptions/new.html.erb delete mode 100644 lib/tasks/consumption.rake create mode 100644 test/controllers/admin/consumptions_controller_test.rb diff --git a/README.md b/README.md index 70dc9ba9..31e2b6b3 100644 --- a/README.md +++ b/README.md @@ -18,18 +18,5 @@ This webapp is for smartphone's web browser. ## Restore from heroku ```shell-session -pg_restore e538a47b-9b70-4e8d-9844-2d6706bd3330 --clean --no-acl --no-owner -d sharecar -h 127.0.0.1 -U sharecar +./pull_db.sh ``` - -## Start Aggregation - -```ruby -Car.all.each do |car| - Consumption.create( - car: car, - start_at: Time.zone.local(2017, 7, 1, 0, 0, 0), - end_at: Time.zone.local(2017, 9, 30, 23, 59, 59), - price: 0 - ) -end -``` \ No newline at end of file diff --git a/app/assets/javascripts/admin/consumptions.coffee b/app/assets/javascripts/admin/consumptions.coffee new file mode 100644 index 00000000..24f83d18 --- /dev/null +++ b/app/assets/javascripts/admin/consumptions.coffee @@ -0,0 +1,3 @@ +# Place all the behaviors and hooks related to the matching controller here. +# All this logic will automatically be available in application.js. +# You can use CoffeeScript in this file: http://coffeescript.org/ diff --git a/app/assets/stylesheets/admin/consumptions.scss b/app/assets/stylesheets/admin/consumptions.scss new file mode 100644 index 00000000..42bf5099 --- /dev/null +++ b/app/assets/stylesheets/admin/consumptions.scss @@ -0,0 +1,3 @@ +// Place all the styles related to the admin::consumptions controller here. +// They will automatically be included in application.css. +// You can use Sass (SCSS) here: http://sass-lang.com/ diff --git a/app/controllers/admin/consumptions_controller.rb b/app/controllers/admin/consumptions_controller.rb new file mode 100644 index 00000000..040d0466 --- /dev/null +++ b/app/controllers/admin/consumptions_controller.rb @@ -0,0 +1,50 @@ +class Admin::ConsumptionsController < ApplicationController + before_action :should_be_admin + + def index + @consumptions = Consumption.all + end + + def recalculate + begin + Consumption.transaction do + Consumption.where(id: params[:ids]).each(&:recalculate) + end + + redirect_to action: :index + rescue + flash[:recalculate_error] = true + + redirect_to action: :index + end + end + + def new + end + + def create + start_at = Time.zone.parse(params[:start_date]) + end_at = Time.zone.parse(params[:end_date]).change(hour: 23, min: 59, sec: 59) + + Consumption.transaction do + Car.all.select do |car| + car.fuels.in(start_at, end_at).exists? + end.each do |car| + Consumption.create( + car: car, + start_at: start_at, + end_at: end_at, + price: 0 + ) + end + end + + redirect_to action: :index + end + + def destroy_multiple + Consumption.where(id: params[:ids]).destroy_all + + redirect_to action: :index + end +end diff --git a/app/helpers/admin/consumptions_helper.rb b/app/helpers/admin/consumptions_helper.rb new file mode 100644 index 00000000..17703c42 --- /dev/null +++ b/app/helpers/admin/consumptions_helper.rb @@ -0,0 +1,2 @@ +module Admin::ConsumptionsHelper +end diff --git a/app/models/consumption.rb b/app/models/consumption.rb index 0d29f29c..3316e132 100644 --- a/app/models/consumption.rb +++ b/app/models/consumption.rb @@ -5,16 +5,20 @@ def self.unfinished where(finished: false) end - def self.calc_consumption(target_fuels, target_drives) - target_fuels.sum(&:amount) / target_drives.sum(&:distance).to_f - end - def calc_fee_of(user) drives = target_drives_of(user) total_distance = drives.sum(&:distance) total_distance * price end + def recalculate + target_fuels = car.fuels.in(start_at, end_at) + target_drives = car.drives.in(start_at, end_at) + + new_price = target_fuels.sum(&:amount) / target_drives.sum(&:distance).to_f + self.update(price: new_price) + end + private def target_drives_of(user) user.drives.where(car_id: car_id).in(start_at, end_at) diff --git a/app/models/drive.rb b/app/models/drive.rb index d8c622fe..525cb0c9 100644 --- a/app/models/drive.rb +++ b/app/models/drive.rb @@ -15,6 +15,8 @@ def lacking? def distance if !end_meter.nil? && !start_meter.nil? end_meter - start_meter + else + raise "Can't calculate distance" end end @@ -71,7 +73,7 @@ def distance if start_meter.present? && end_meter.present? end_meter - start_meter else - nil + raise "Can't calculate distance of the drive #{self.id}" end end diff --git a/app/views/admin/consumptions/index.html.erb b/app/views/admin/consumptions/index.html.erb new file mode 100644 index 00000000..4b027684 --- /dev/null +++ b/app/views/admin/consumptions/index.html.erb @@ -0,0 +1,46 @@ +
+ <% if flash[:recalculate_error] %> +
+
+ + 燃費の算出に失敗しました。期間中の乗車記録に不備があります。 + +
+
+ <% end %> + <%= link_to new_admin_consumption_path, method: :GET do %> +
+
+ + 集計期間を登録する + +
+
+ <% end %> + <% @consumptions.group_by { |c| [c.start_at.to_date, c.end_at.to_date] }.sort.reverse.each do |dates, consumptions| %> +
+
+
+

+ <%= dates[0] %> 〜 <%= dates[1] %> +

+

+

    + <% consumptions.each do |consumption| %> +
  • <%= consumption.car.name %>:<%= consumption.price.round(1) %> 円/km
  • + <% end %> +
+

+
+ <%= link_to recalculate_admin_consumptions_path(ids: consumptions.map(&:id)), method: :POST, class: 'Card--Action--FAB green' do %> + autorenew + <% end %> + <%= link_to destroy_multiple_admin_consumptions_path(ids: consumptions.map(&:id)), method: :DELETE, class: 'Card--Action--FAB red' do %> + delete + <% end %> +
+
+
+
+ <% end %> +
diff --git a/app/views/admin/consumptions/new.html.erb b/app/views/admin/consumptions/new.html.erb new file mode 100644 index 00000000..6ae4d43a --- /dev/null +++ b/app/views/admin/consumptions/new.html.erb @@ -0,0 +1,18 @@ +
+
+ 集計期間の登録 + <%= form_tag admin_consumptions_path, html: { class: 'Form' } do |f| %> +
+ <%= label_tag '開始日' %> + <%= date_field_tag :start_date, class: 'datepicker' %> +
+ +
+ <%= label_tag '終了日' %> + <%= date_field_tag :end_date, class: 'datepicker' %> +
+ + <%= submit_tag "登録", class: "Form--SubmitBtn" %> + <% end %> +
+
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 2bd24d6c..aee8dd0a 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -29,6 +29,11 @@ supervisor_account 管理者画面 <% end %> +
  • + <%= link_to admin_consumptions_path do %> + attach_money 燃費計算 + <% end %> +
  • <%= link_to new_admin_payment_path do %> payment 支払い記録 diff --git a/config/routes.rb b/config/routes.rb index 98fa02c0..f80a106e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,6 +23,12 @@ end namespace :admin do + resources :consumptions, only: [:index, :new, :create] do + collection do + post :recalculate + delete :destroy_multiple + end + end resources :payments resources :cars, only: [:index, :edit, :update] do resources :drives diff --git a/lib/tasks/consumption.rake b/lib/tasks/consumption.rake deleted file mode 100644 index 83a87eed..00000000 --- a/lib/tasks/consumption.rake +++ /dev/null @@ -1,11 +0,0 @@ -task cc: :environment do - Consumption.unfinished.all.each do |consumption| - start_at = consumption.start_at - end_at = consumption.end_at - target_fuels = consumption.car.fuels.in(start_at, end_at) - target_drives = consumption.car.drives.in(start_at, end_at) - - cons_value = Consumption.calc_consumption(target_fuels, target_drives) - consumption.update(price: cons_value) - end -end diff --git a/test/controllers/admin/consumptions_controller_test.rb b/test/controllers/admin/consumptions_controller_test.rb new file mode 100644 index 00000000..0fe70b6a --- /dev/null +++ b/test/controllers/admin/consumptions_controller_test.rb @@ -0,0 +1,7 @@ +require 'test_helper' + +class Admin::ConsumptionsControllerTest < ActionDispatch::IntegrationTest + # test "the truth" do + # assert true + # end +end