Skip to content

Commit b2d7c55

Browse files
authored
Merge pull request #711 from rubyforgood/710-items-value
display an estimated value of contents on the Distribution manifests
2 parents a83ae6a + af4456d commit b2d7c55

26 files changed

+120
-14
lines changed

app/controllers/distributions_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ def index
3333
.distributions
3434
.includes(:partner, :storage_location, :line_items, :items)
3535
.order(created_at: :desc)
36+
@total_value_all_distributions = total_value(@distributions)
3637
end
3738

3839
def create
@@ -135,4 +136,12 @@ def send_notification(org, dist)
135136
def distribution_params
136137
params.require(:distribution).permit(:comment, :agency_rep, :issued_at, :partner_id, :storage_location_id, line_items_attributes: %i(item_id quantity _destroy))
137138
end
139+
140+
def total_value(distributions)
141+
total_value_all_distributions = 0
142+
distributions.each do |distribution|
143+
total_value_all_distributions += distribution.value_per_itemizable
144+
end
145+
total_value_all_distributions
146+
end
138147
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)
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
module ItemsHelper
2+
def item_value(value, addition = '')
3+
if value.zero?
4+
''
5+
else
6+
addition + ActionController::Base.helpers.number_to_currency(value, precision: value.round == value ? 0 : 2)
7+
end
8+
end
9+
end

app/models/concerns/itemizable.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ def line_items_quantities
7272
end
7373
end
7474

75+
def value_per_itemizable
76+
line_items.sum(&:value_per_line_item)
77+
end
78+
7579
private
7680

7781
def line_item_items_quantity_is_positive

app/models/item.rb

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,16 @@
22
#
33
# Table name: items
44
#
5-
# id :bigint(8) not null, primary key
5+
# id :integer not null, primary key
66
# name :string
77
# category :string
8-
# created_at :datetime
9-
# updated_at :datetime
8+
# created_at :datetime not null
9+
# updated_at :datetime not null
1010
# barcode_count :integer
1111
# organization_id :integer
1212
# active :boolean default(TRUE)
1313
# partner_key :string
14+
# value :decimal(5, 2) default(0.0)
1415
#
1516

1617
class Item < ApplicationRecord
@@ -19,6 +20,7 @@ class Item < ApplicationRecord
1920
validates :name, uniqueness: { scope: :organization }
2021
validates :name, presence: true
2122
validates :organization, presence: true
23+
validates :value, numericality: { greater_than_or_equal_to: 0 }
2224

2325
has_many :line_items, dependent: :destroy
2426
has_many :inventory_items, dependent: :destroy

app/models/line_item.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,8 @@ class LineItem < ApplicationRecord
1717

1818
validates :item_id, presence: true
1919
validates :quantity, numericality: { other_than: 0, only_integer: true }
20+
21+
def value_per_line_item
22+
item.value * quantity
23+
end
2024
end

app/pdfs/distribution_pdf.rb

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
class DistributionPdf
22
include Prawn::View
3+
include ItemsHelper
34

45
def initialize(organization, distribution)
56
@distribution = distribution
@@ -9,11 +10,11 @@ def initialize(organization, distribution)
910
text organization.address, align: :right
1011
text organization.email, align: :right
1112
end
12-
data = [["Items Received", "Quantity"]]
13+
data = [["Items Received", "Value/item", "Total value", "Quantity"]]
1314
data += @distribution.line_items.sorted.map do |c|
14-
[c.item.name, c.quantity]
15+
[c.item.name, item_value(c.item.value), item_value(c.value_per_line_item), c.quantity]
1516
end
16-
data += [["", ""], ["Total Items Received", @distribution.line_items.total]]
17+
data += [["", "", "", ""], ["Total Items Received", "", item_value(@distribution.value_per_itemizable), @distribution.line_items.total]]
1718

1819
move_down 55
1920

@@ -45,22 +46,23 @@ def initialize(organization, distribution)
4546
row(0).borders = [:bottom]
4647
row(0).border_width = 2
4748
row(0).font_style = :bold
48-
row(0).column(-1).borders = %i(bottom left)
49+
row(0).column(1..-1).borders = %i(bottom left)
4950

5051
# Total Items footer row
5152
row(-1).borders = [:top]
5253
row(-1).font_style = :bold
53-
row(-1).column(-1).borders = %i(top left)
54+
row(-1).column(2..-1).borders = %i(top left)
55+
row(-1).column(2..-1).border_left_color = "aaaaaa"
5456

5557
# Footer spacing row
5658
row(-2).borders = [:top]
5759
row(-2).padding = [2, 0, 2, 0]
5860

59-
column(0).width = 400
61+
column(0).width = 250
6062

6163
# Quantity column
62-
column(1).row(1..-3).borders = [:left]
63-
column(1).row(1..-3).border_left_color = "aaaaaa"
64+
column(1..3).row(1..-3).borders = [:left]
65+
column(1..3).row(1..-3).border_left_color = "aaaaaa"
6466
column(1).style align: :right
6567
end
6668

app/queries/items_by_storage_collection_and_quantity_query.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +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_value: row.value,
1718
item_barcode_count: row.barcode_count
1819
}
1920
end

app/queries/items_by_storage_collection_query.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def call
1919
items.category,
2020
items.barcode_count,
2121
items.partner_key,
22+
items.value,
2223
storage_locations.name as storage_name,
2324
storage_locations.id as storage_id,
2425
sum(inventory_items.quantity) as quantity
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
<tr>
22
<td><%= distribution_item_row.quantity %></td>
3+
<td><%= item_value(distribution_item_row.item.value) %></td>
4+
<td><%= item_value(distribution_item_row.value_per_line_item) %></td>
35
<td><%= distribution_item_row.item.name %></td>
46
</tr>

0 commit comments

Comments
 (0)