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 @@ +
+ <%= dates[0] %> 〜 <%= dates[1] %> +
++