diff --git a/app/models/invoice.rb b/app/models/invoice.rb index a5c04e97..8a9dde9f 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -41,6 +41,24 @@ class Invoice < ApplicationRecord amount - deposit + deposit_credit 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 def valid_attachment diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index b285798c..22f5026a 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -29,10 +29,14 @@ %dd>< - @invoice.orders.order(:ends).each_with_index do |order, index| - sum = order.sum - - total += sum + - transport = order.transport || 0 + - total += sum + transport = ', ' if index > 0 = 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) + ':' %dd= @invoice.number diff --git a/app/views/finance/invoices/unpaid.html.haml b/app/views/finance/invoices/unpaid.html.haml index 743546cf..3a0246a9 100644 --- a/app/views/finance/invoices/unpaid.html.haml +++ b/app/views/finance/invoices/unpaid.html.haml @@ -8,12 +8,17 @@ - invoices_text = [] %p - for invoice in invoices + - invoice_amount_diff = invoice.expected_amount - invoice.net_amount - invoices_sum += invoice.amount - invoices_text << invoice.number = link_to finance_invoice_path(invoice) do = format_date invoice.date = ' ' + invoice.number = ' ' + 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? = link_to finance_invoice_attachment_path(invoice) do = glyph :download diff --git a/lib/invoices_csv.rb b/lib/invoices_csv.rb index 90353ced..aa20cd08 100644 --- a/lib/invoices_csv.rb +++ b/lib/invoices_csv.rb @@ -11,6 +11,7 @@ class InvoicesCsv < RenderCSV Invoice.human_attribute_name(:supplier), Invoice.human_attribute_name(:number), Invoice.human_attribute_name(:amount), + Invoice.human_attribute_name(:total), Invoice.human_attribute_name(:deposit), Invoice.human_attribute_name(:deposit_credit), Invoice.human_attribute_name(:paid_on), @@ -27,6 +28,7 @@ class InvoicesCsv < RenderCSV t.supplier.name, t.number, t.amount, + t.expected_amount, t.deposit, t.deposit_credit, t.paid_on,