Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

correct invoice balance with stock orders and deliveries (foodcoopsat) #5

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,22 +29,28 @@ 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 expected_amount
return net_amount unless orders.any?
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

orders_sum + orders_transport_sum
def expected_amount(type = :without_markup)
orders_sum(type) + orders_transport_sum + deliveries_sum(type)
end

protected
Expand Down
4 changes: 3 additions & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
36 changes: 30 additions & 6 deletions app/views/finance/invoices/show.html.haml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
- title t('.title', number: @invoice.number)

- total = 0
- total_fc = 0

.row-fluid
.span6
Expand All @@ -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) + ')'
Expand All @@ -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) + ':'
Expand All @@ -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) + ':'
Expand Down
11 changes: 7 additions & 4 deletions app/views/finance/invoices/unpaid.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading