From ac1af4ac515e54aad8c56e43646e78a0af70f65d Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:59:32 +0100 Subject: [PATCH 1/7] Update invoice.rb --- app/models/invoice.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index e01be6c6a..8c30d9c6c 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -29,22 +29,30 @@ def net_amount 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)') + def orders_sum(type = :without_markup) + if type == :without_markup + orders.sum { |order| order.sum(:groups_without_markup) } + elsif type == :with_markup + orders.sum { |order| order.sum(:groups) } + end end def orders_transport_sum - orders.sum(:transport) + orders.sum { |order| order.sum(:transport) } + end + + def deliveries_sum(type = :without_markup) + if type == :without_markup + deliveries.sum(&:sum) + elsif type == :with_markup + deliveries.sum { |delivery| delivery.sum(:fc) } + end end - def expected_amount + def expected_amount(type = :without_markup) return net_amount unless orders.any? - orders_sum + orders_transport_sum + orders_sum(type) + orders_transport_sum + deliveries_sum(type) end protected From 39c1abd134011f07f959c8cbac0e2b63c4394b71 Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:06:48 +0100 Subject: [PATCH 2/7] Update order.rb --- app/models/order.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/models/order.rb b/app/models/order.rb index f4038f82a..7c8d31e93 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -223,7 +223,7 @@ def sum(type = :gross) end end elsif %i[groups groups_without_markup].include?(type) - for go in group_orders.includes(group_order_articles: { order_article: %i[article article_price] }) + for go in group_orders.includes(group_order_articles: { order_article: %i[article article_price] }).where.not(ordergroup: nil) for goa in go.group_order_articles case type when :groups @@ -233,6 +233,8 @@ def sum(type = :gross) end end end + elsif type == :transport + total = group_orders.where.not(ordergroup: nil).sum(:transport) end total end From 8249f70499952888ffe8bfe89aae6743d2253146 Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:11:29 +0100 Subject: [PATCH 3/7] Update show.html.haml --- app/views/finance/invoices/show.html.haml | 36 +++++++++++++++++++---- 1 file changed, 30 insertions(+), 6 deletions(-) diff --git a/app/views/finance/invoices/show.html.haml b/app/views/finance/invoices/show.html.haml index 4d80ea956..7ede50bad 100644 --- a/app/views/finance/invoices/show.html.haml +++ b/app/views/finance/invoices/show.html.haml @@ -1,6 +1,7 @@ - title t('.title', number: @invoice.number) - total = 0 +- total_fc = 0 .row-fluid .span6 @@ -20,6 +21,7 @@ - @invoice.deliveries.order(:date).each_with_index do |delivery, index| - sum = delivery.sum - total += sum + - total_fc += delivery.sum(:fc) = ', ' if index > 0 = link_to format_date(delivery.date), [delivery.supplier, delivery] = ' (' + number_to_currency(sum) + ')' @@ -28,14 +30,21 @@ %dt= heading_helper(Invoice, :orders) + ':' %dd>< - @invoice.orders.order(:ends).each_with_index do |order, index| - - sum = order.sum + - sum_og = order.sum(:groups_without_markup) # without markup + - sum = order.sum # without markup + - transport_og = order.sum(:transport) - transport = order.transport || 0 - - total += sum + transport + - total += sum_og + transport_og # without markup + - total_fc += order.sum(:groups) + transport_og # with_markup = ', ' if index > 0 = link_to format_date(order.ends), new_finance_order_path(order_id: order) - = ' (' + number_to_currency(sum) + = ' (' + number_to_currency(sum_og) + - if sum_og < sum + = ' / ' + number_to_currency(sum) - if transport != 0 - = ' + ' + number_to_currency(transport) + = ' + ' + number_to_currency(transport_og) + - if transport_og < transport + = ' / ' + number_to_currency(transport) = ')' %dt= heading_helper(Invoice, :number) + ':' @@ -60,8 +69,23 @@ %dd= number_to_currency @invoice.net_amount - if @invoice.deliveries.any? || @invoice.orders.any? - %dt= heading_helper(Invoice, :total) + ':' - %dd= number_to_currency total + %dt= heading_helper(Invoice, :total) + (total_fc > total ? t('.without_extra_charge') : '') + ':' # + %dd= number_to_currency(total) + - if total_fc > total + %dt= heading_helper(Invoice, :total) + t('.with_extra_charge') + ':' + %dd= number_to_currency(total_fc) + + %dt= t('.fc_profit') + (total_fc > total ? t('.without_extra_charge') : '') + ':' + - profit = total - @invoice.net_amount + %dd + %span{style: "color:#{profit < 0 ? 'red' : 'green'}"} + = number_to_currency(profit) + - if total_fc > total + %dt= t('.fc_profit') + ' ' + t('.with_extra_charge') + ':' + - profit = total_fc - @invoice.net_amount + %dd + %span{style: "color:#{profit < 0 ? 'red' : 'green'}"} + = number_to_currency(profit) - if @invoice.attachments.attached? %dt= heading_helper(Invoice, :attachment) + ':' From ec39f11ea166451a173275c526684695c0d9db1a Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:21:51 +0100 Subject: [PATCH 4/7] Update unpaid.html.haml if markup is set, show both differences without and with markup --- app/views/finance/invoices/unpaid.html.haml | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/app/views/finance/invoices/unpaid.html.haml b/app/views/finance/invoices/unpaid.html.haml index e04d514e0..4674c631b 100644 --- a/app/views/finance/invoices/unpaid.html.haml +++ b/app/views/finance/invoices/unpaid.html.haml @@ -12,6 +12,7 @@ %p - for invoice in invoices - invoice_amount_diff = invoice.expected_amount - invoice.net_amount + - invoice_profit = invoice.expected_amount(:with_markup) - invoice.net_amount - invoices_sum += invoice.amount - invoices_text << invoice.number - if supplier.supplier_category.bank_account&.bank_gateway @@ -21,10 +22,12 @@ = 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) + %span{style: "color:#{invoice_amount_diff < 0 ? 'red' : 'green'}"} + = number_to_currency(invoice_amount_diff) + - if invoice_profit > invoice_amount_diff + = ' / ' + %span{style: "color:#{invoice_profit < 0 ? 'red' : 'green'}"} + = number_to_currency(invoice_profit) - if invoice.attachments.attached? - for attachment in invoice.attachments = link_to attachment.filename, attachment.url From a1fdc6ae01a9f0865ca51512737e5d151fe318db Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:49:49 +0100 Subject: [PATCH 5/7] Update de.yml --- config/locales/de.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/locales/de.yml b/config/locales/de.yml index 5e207ae02..c8d21a4e8 100644 --- a/config/locales/de.yml +++ b/config/locales/de.yml @@ -999,6 +999,9 @@ de: title: Neue Rechnung anlegen show: title: Rechnung %{number} + fc_profit: FC Gewinn + with_extra_charge: ' mit Aufschlag' + without_extra_charge: ' ohne Aufschlag' unpaid: invoices_sum: Gesamtsumme invoices_text: Verwendungszweck From 43e3329b52376bab3f99b40aa5d51e430311126f Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Wed, 13 Nov 2024 13:50:41 +0100 Subject: [PATCH 6/7] Update en.yml --- config/locales/en.yml | 3 +++ 1 file changed, 3 insertions(+) diff --git a/config/locales/en.yml b/config/locales/en.yml index c1386d4b2..aa8111266 100644 --- a/config/locales/en.yml +++ b/config/locales/en.yml @@ -999,6 +999,9 @@ en: title: Create new invoice show: title: Invoice %{number} + fc_profit: FC Profit + with_extra_charge: ' with margin' + without_extra_charge: ' without margin' unpaid: invoices_sum: Total sum invoices_text: Reference From e4e416ba0350e62604ace615f306eb5693a0f198 Mon Sep 17 00:00:00 2001 From: mjavurek <57668028+mjavurek@users.noreply.github.com> Date: Mon, 18 Nov 2024 00:29:38 +0100 Subject: [PATCH 7/7] Update invoice.rb: calculate expected_amount also if invoice has no orders --- app/models/invoice.rb | 2 -- 1 file changed, 2 deletions(-) diff --git a/app/models/invoice.rb b/app/models/invoice.rb index 8c30d9c6c..72938e3c2 100644 --- a/app/models/invoice.rb +++ b/app/models/invoice.rb @@ -50,8 +50,6 @@ def deliveries_sum(type = :without_markup) end def expected_amount(type = :without_markup) - return net_amount unless orders.any? - orders_sum(type) + orders_transport_sum + deliveries_sum(type) end