Skip to content

Commit 43cb786

Browse files
committed
display an estimated value of contents on the Distribution manifests
1 parent eb1c225 commit 43cb786

26 files changed

+125
-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_price_all_distributions = total_price(@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_price(distributions)
141+
total_price_all_distributions = 0
142+
distributions.each do |distribution|
143+
total_price_all_distributions += distribution.price_per_itemizable
144+
end
145+
total_price_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, :price)
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_price(price, addition = '')
3+
if price.zero?
4+
''
5+
else
6+
addition + number_to_currency(price, precision: price.round == price ? 0 : 2)
7+
end
8+
end
9+
end

app/models/concerns/itemizable.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,14 @@ 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
81+
end
82+
7583
private
7684

7785
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+
# price :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 :price, 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 price_per_line_item
22+
item.price * quantity
23+
end
2024
end

app/pdfs/distribution_pdf.rb

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

46
def initialize(organization, distribution)
57
@distribution = distribution
@@ -9,11 +11,11 @@ def initialize(organization, distribution)
911
text organization.address, align: :right
1012
text organization.email, align: :right
1113
end
12-
data = [["Items Received", "Quantity"]]
14+
data = [["Items Received", "Price/item", "Total price", "Quantity"]]
1315
data += @distribution.line_items.sorted.map do |c|
14-
[c.item.name, c.quantity]
16+
[c.item.name, item_price(c.item.price), item_price(c.price_per_line_item), c.quantity]
1517
end
16-
data += [["", ""], ["Total Items Received", @distribution.line_items.total]]
18+
data += [["", "", "", ""], ["Total Items Received", "", item_price(@distribution.price_per_itemizable), @distribution.line_items.total]]
1719

1820
move_down 55
1921

@@ -45,22 +47,23 @@ def initialize(organization, distribution)
4547
row(0).borders = [:bottom]
4648
row(0).border_width = 2
4749
row(0).font_style = :bold
48-
row(0).column(-1).borders = %i(bottom left)
50+
row(0).column(1..-1).borders = %i(bottom left)
4951

5052
# Total Items footer row
5153
row(-1).borders = [:top]
5254
row(-1).font_style = :bold
53-
row(-1).column(-1).borders = %i(top left)
55+
row(-1).column(2..-1).borders = %i(top left)
56+
row(-1).column(2..-1).border_left_color = "aaaaaa"
5457

5558
# Footer spacing row
5659
row(-2).borders = [:top]
5760
row(-2).padding = [2, 0, 2, 0]
5861

59-
column(0).width = 400
62+
column(0).width = 250
6063

6164
# Quantity column
62-
column(1).row(1..-3).borders = [:left]
63-
column(1).row(1..-3).border_left_color = "aaaaaa"
65+
column(1..3).row(1..-3).borders = [:left]
66+
column(1..3).row(1..-3).border_left_color = "aaaaaa"
6467
column(1).style align: :right
6568
end
6669

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_price: row.price,
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.price,
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_price(distribution_item_row.item.price) %></td>
4+
<td><%= item_price(distribution_item_row.price_per_line_item) %></td>
35
<td><%= distribution_item_row.item.name %></td>
46
</tr>

0 commit comments

Comments
 (0)