Skip to content

Commit af4456d

Browse files
committed
refactor price->value and 2 minor changes
1 parent 43cb786 commit af4456d

27 files changed

+51
-56
lines changed

app/controllers/distributions_controller.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ def index
3333
.distributions
3434
.includes(:partner, :storage_location, :line_items, :items)
3535
.order(created_at: :desc)
36-
@total_price_all_distributions = total_price(@distributions)
36+
@total_value_all_distributions = total_value(@distributions)
3737
end
3838

3939
def create
@@ -137,11 +137,11 @@ def distribution_params
137137
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, line_items_attributes: %i(item_id quantity _destroy))
138138
end
139139

140-
def total_price(distributions)
141-
total_price_all_distributions = 0
140+
def total_value(distributions)
141+
total_value_all_distributions = 0
142142
distributions.each do |distribution|
143-
total_price_all_distributions += distribution.price_per_itemizable
143+
total_value_all_distributions += distribution.value_per_itemizable
144144
end
145-
total_price_all_distributions
145+
total_value_all_distributions
146146
end
147147
end

app/controllers/items_controller.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def destroy
5353
private
5454

5555
def item_params
56-
params.require(:item).permit(:name, :category, :partner_key, :price)
56+
params.require(:item).permit(:name, :category, :partner_key, :value)
5757
end
5858

5959
def filter_params(parameters = nil)

app/helpers/items_helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
module ItemsHelper
2-
def item_price(price, addition = '')
3-
if price.zero?
2+
def item_value(value, addition = '')
3+
if value.zero?
44
''
55
else
6-
addition + number_to_currency(price, precision: price.round == price ? 0 : 2)
6+
addition + ActionController::Base.helpers.number_to_currency(value, precision: value.round == value ? 0 : 2)
77
end
88
end
99
end

app/models/concerns/itemizable.rb

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,8 @@ def line_items_quantities
7272
end
7373
end
7474

75-
def price_per_itemizable
76-
tp = 0
77-
line_items.each do |line_item|
78-
tp += line_item.item.price * line_item.quantity
79-
end
80-
tp
75+
def value_per_itemizable
76+
line_items.sum(&:value_per_line_item)
8177
end
8278

8379
private

app/models/item.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
# organization_id :integer
1212
# active :boolean default(TRUE)
1313
# partner_key :string
14-
# price :decimal(5, 2) default(0.0)
14+
# value :decimal(5, 2) default(0.0)
1515
#
1616

1717
class Item < ApplicationRecord
@@ -20,7 +20,7 @@ class Item < ApplicationRecord
2020
validates :name, uniqueness: { scope: :organization }
2121
validates :name, presence: true
2222
validates :organization, presence: true
23-
validates :price, numericality: { greater_than_or_equal_to: 0 }
23+
validates :value, numericality: { greater_than_or_equal_to: 0 }
2424

2525
has_many :line_items, dependent: :destroy
2626
has_many :inventory_items, dependent: :destroy

app/models/line_item.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class LineItem < ApplicationRecord
1818
validates :item_id, presence: true
1919
validates :quantity, numericality: { other_than: 0, only_integer: true }
2020

21-
def price_per_line_item
22-
item.price * quantity
21+
def value_per_line_item
22+
item.value * quantity
2323
end
2424
end

app/pdfs/distribution_pdf.rb

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
class DistributionPdf
22
include Prawn::View
33
include ItemsHelper
4-
include ActionView::Helpers::NumberHelper
54

65
def initialize(organization, distribution)
76
@distribution = distribution
@@ -11,11 +10,11 @@ def initialize(organization, distribution)
1110
text organization.address, align: :right
1211
text organization.email, align: :right
1312
end
14-
data = [["Items Received", "Price/item", "Total price", "Quantity"]]
13+
data = [["Items Received", "Value/item", "Total value", "Quantity"]]
1514
data += @distribution.line_items.sorted.map do |c|
16-
[c.item.name, item_price(c.item.price), item_price(c.price_per_line_item), c.quantity]
15+
[c.item.name, item_value(c.item.value), item_value(c.value_per_line_item), c.quantity]
1716
end
18-
data += [["", "", "", ""], ["Total Items Received", "", item_price(@distribution.price_per_itemizable), @distribution.line_items.total]]
17+
data += [["", "", "", ""], ["Total Items Received", "", item_value(@distribution.value_per_itemizable), @distribution.line_items.total]]
1918

2019
move_down 55
2120

app/queries/items_by_storage_collection_and_quantity_query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def call
1414
@items_by_storage_collection_and_quantity[row.id] = {
1515
item_name: row.name,
1616
item_category: row.category,
17-
item_price: row.price,
17+
item_value: row.value,
1818
item_barcode_count: row.barcode_count
1919
}
2020
end

app/queries/items_by_storage_collection_query.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def call
1919
items.category,
2020
items.barcode_count,
2121
items.partner_key,
22-
items.price,
22+
items.value,
2323
storage_locations.name as storage_name,
2424
storage_locations.id as storage_id,
2525
sum(inventory_items.quantity) as quantity
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<tr>
22
<td><%= distribution_item_row.quantity %></td>
3-
<td><%= item_price(distribution_item_row.item.price) %></td>
4-
<td><%= item_price(distribution_item_row.price_per_line_item) %></td>
3+
<td><%= item_value(distribution_item_row.item.value) %></td>
4+
<td><%= item_value(distribution_item_row.value_per_line_item) %></td>
55
<td><%= distribution_item_row.item.name %></td>
66
</tr>

0 commit comments

Comments
 (0)