Skip to content

Commit

Permalink
Consumption calculate feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
genya0407 committed Aug 1, 2018
1 parent bcc6883 commit 0b8a190
Show file tree
Hide file tree
Showing 13 changed files with 152 additions and 30 deletions.
15 changes: 1 addition & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
3 changes: 3 additions & 0 deletions app/assets/javascripts/admin/consumptions.coffee
Original file line number Diff line number Diff line change
@@ -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/
3 changes: 3 additions & 0 deletions app/assets/stylesheets/admin/consumptions.scss
Original file line number Diff line number Diff line change
@@ -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/
50 changes: 50 additions & 0 deletions app/controllers/admin/consumptions_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/helpers/admin/consumptions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
module Admin::ConsumptionsHelper
end
12 changes: 8 additions & 4 deletions app/models/consumption.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion app/models/drive.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
46 changes: 46 additions & 0 deletions app/views/admin/consumptions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<div class="row">
<% if flash[:recalculate_error] %>
<div class="col s12">
<div class='card-panel red lighten-2' style='text-align: center;'>
<span class='white-text'>
燃費の算出に失敗しました。期間中の乗車記録に不備があります。
</span>
</div>
</div>
<% end %>
<%= link_to new_admin_consumption_path, method: :GET do %>
<div class="col s12">
<div class='card-panel blue lighten-2' style='text-align: center; font-size: large;'>
<span class='white-text'>
集計期間を登録する
</span>
</div>
</div>
<% end %>
<% @consumptions.group_by { |c| [c.start_at.to_date, c.end_at.to_date] }.sort.reverse.each do |dates, consumptions| %>
<div class="col s12">
<div class="Card">
<div class="Card--Content">
<p class="Card--Content--Title">
<%= dates[0] %><%= dates[1] %>
</p>
<p style='text-align: right;'>
<ul>
<% consumptions.each do |consumption| %>
<li><%= consumption.car.name %><%= consumption.price.round(1) %> 円/km</li>
<% end %>
</ul>
</p>
<div class="Card--Action">
<%= link_to recalculate_admin_consumptions_path(ids: consumptions.map(&:id)), method: :POST, class: 'Card--Action--FAB green' do %>
<i class='Card--Action--FAB--Icon'>autorenew</i>
<% end %>
<%= link_to destroy_multiple_admin_consumptions_path(ids: consumptions.map(&:id)), method: :DELETE, class: 'Card--Action--FAB red' do %>
<i class='Card--Action--FAB--Icon'>delete</i>
<% end %>
</div>
</div>
</div>
</div>
<% end %>
</div>
18 changes: 18 additions & 0 deletions app/views/admin/consumptions/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<div class="Card">
<div class="Card--Content">
<span class="Card--Content--Title">集計期間の登録</span>
<%= form_tag admin_consumptions_path, html: { class: 'Form' } do |f| %>
<div class='Form--InputSet--Field s6'>
<%= label_tag '開始日' %>
<%= date_field_tag :start_date, class: 'datepicker' %>
</div>

<div class='Form--InputSet--Field s6'>
<%= label_tag '終了日' %>
<%= date_field_tag :end_date, class: 'datepicker' %>
</div>

<%= submit_tag "登録", class: "Form--SubmitBtn" %>
<% end %>
</div>
</div>
5 changes: 5 additions & 0 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@
<i class="material-icons">supervisor_account</i> 管理者画面
<% end %>
</li>
<li>
<%= link_to admin_consumptions_path do %>
<i class="material-icons">attach_money</i> 燃費計算
<% end %>
</li>
<li>
<%= link_to new_admin_payment_path do %>
<i class="material-icons">payment</i> 支払い記録
Expand Down
6 changes: 6 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 0 additions & 11 deletions lib/tasks/consumption.rake

This file was deleted.

7 changes: 7 additions & 0 deletions test/controllers/admin/consumptions_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
require 'test_helper'

class Admin::ConsumptionsControllerTest < ActionDispatch::IntegrationTest
# test "the truth" do
# assert true
# end
end

0 comments on commit 0b8a190

Please sign in to comment.