Add expected_amount to Invoice

expected_amount returns the sum of all associated orders together with
the transport costs.
This commit is contained in:
Patrick Gansterer 2022-02-18 13:03:34 +01:00
parent 093313f0f3
commit 052d297bff
4 changed files with 31 additions and 2 deletions

View file

@ -41,6 +41,24 @@ class Invoice < ApplicationRecord
amount - deposit + deposit_credit amount - deposit + deposit_credit
end end
def orders_sum
orders
.joins(order_articles: [:article_price])
.sum("COALESCE(order_articles.units_received, order_articles.units_billed, order_articles.units_to_order)" \
+ "* article_prices.unit_quantity" \
+ "* ROUND((article_prices.price + article_prices.deposit) * (100 + article_prices.tax) / 100, 2)")
end
def orders_transport_sum
orders.sum(:transport)
end
def expected_amount
return net_amount unless orders.any?
orders_sum + orders_transport_sum
end
protected protected
def valid_attachment def valid_attachment

View file

@ -29,10 +29,14 @@
%dd>< %dd><
- @invoice.orders.order(:ends).each_with_index do |order, index| - @invoice.orders.order(:ends).each_with_index do |order, index|
- sum = order.sum - sum = order.sum
- total += sum - transport = order.transport || 0
- total += sum + transport
= ', ' if index > 0 = ', ' if index > 0
= link_to format_date(order.ends), new_finance_order_path(order_id: order) = link_to format_date(order.ends), new_finance_order_path(order_id: order)
= ' (' + number_to_currency(sum) + ')' = ' (' + number_to_currency(sum)
- if transport != 0
= ' + ' + number_to_currency(transport)
= ')'
%dt= heading_helper(Invoice, :number) + ':' %dt= heading_helper(Invoice, :number) + ':'
%dd= @invoice.number %dd= @invoice.number

View file

@ -8,12 +8,17 @@
- invoices_text = [] - invoices_text = []
%p %p
- for invoice in invoices - for invoice in invoices
- invoice_amount_diff = invoice.expected_amount - invoice.net_amount
- invoices_sum += invoice.amount - invoices_sum += invoice.amount
- invoices_text << invoice.number - invoices_text << invoice.number
= link_to finance_invoice_path(invoice) do = link_to finance_invoice_path(invoice) do
= format_date invoice.date = format_date invoice.date
= ' ' + invoice.number = ' ' + invoice.number
= ' ' + number_to_currency(invoice.amount) = ' ' + number_to_currency(invoice.amount)
- if invoice_amount_diff != 0
%span{style: "color:#{invoice_amount_diff < 0 ? 'red' : 'green'}"}
= invoice_amount_diff > 0 ? '+' : '-'
= number_to_currency(invoice_amount_diff.abs)
- if invoice.attachment_data? - if invoice.attachment_data?
= link_to finance_invoice_attachment_path(invoice) do = link_to finance_invoice_attachment_path(invoice) do
= glyph :download = glyph :download

View file

@ -11,6 +11,7 @@ class InvoicesCsv < RenderCSV
Invoice.human_attribute_name(:supplier), Invoice.human_attribute_name(:supplier),
Invoice.human_attribute_name(:number), Invoice.human_attribute_name(:number),
Invoice.human_attribute_name(:amount), Invoice.human_attribute_name(:amount),
Invoice.human_attribute_name(:total),
Invoice.human_attribute_name(:deposit), Invoice.human_attribute_name(:deposit),
Invoice.human_attribute_name(:deposit_credit), Invoice.human_attribute_name(:deposit_credit),
Invoice.human_attribute_name(:paid_on), Invoice.human_attribute_name(:paid_on),
@ -27,6 +28,7 @@ class InvoicesCsv < RenderCSV
t.supplier.name, t.supplier.name,
t.number, t.number,
t.amount, t.amount,
t.expected_amount,
t.deposit, t.deposit,
t.deposit_credit, t.deposit_credit,
t.paid_on, t.paid_on,