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 #1075

Open
wants to merge 39 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
42b9091
Update invoice.rb
mjavurek Nov 10, 2024
e228570
Update order.rb
mjavurek Nov 10, 2024
c0654c2
Update show.html.haml
mjavurek Nov 10, 2024
ab4ff5b
Update invoice.rb
mjavurek Nov 10, 2024
c5761bb
Update invoice.rb
mjavurek Nov 11, 2024
34e1c35
Update show.html.haml
mjavurek Nov 11, 2024
46e54ca
Update invoice.rb
mjavurek Nov 13, 2024
895eb6b
Update show.html.haml
mjavurek Nov 13, 2024
e0dfb47
Update de.yml
mjavurek Nov 13, 2024
876789c
Update en.yml
mjavurek Nov 13, 2024
728de44
Update invoice.rb
mjavurek Nov 13, 2024
02290c8
Update invoice.rb - unnecessary whitspaces deleted
mjavurek Nov 13, 2024
b932440
Update show.html.haml
mjavurek Nov 13, 2024
25b43b4
Update invoice.rb
mjavurek Nov 13, 2024
a66c4b4
Update order.rb
mjavurek Nov 13, 2024
aa89f20
Update show.html.haml
mjavurek Nov 13, 2024
cc21da8
Update unpaid.html.haml
mjavurek Nov 13, 2024
d2198f6
Update invoice.rb: calculate expected_amount also if invoice has no o…
mjavurek Nov 17, 2024
ab424ac
Update de.yml: Gewinn > Überschuss
mjavurek Nov 19, 2024
be6b11d
Update en.yml: profit > surplus
mjavurek Nov 19, 2024
b49d21f
Update en.yml: surplus lower case after FC
mjavurek Nov 19, 2024
f2cc262
Update order.rb: sum for stock-order, transport-costs-skip set groups…
mjavurek Nov 21, 2024
d5c6ac7
Update bootstrap_and_overrides.css.less: added span.positive_green
mjavurek Nov 21, 2024
e817345
Update application_helper.rb: added format_currency_difference()
mjavurek Nov 21, 2024
9040f72
Update bootstrap_and_overrides.css.less: typo amout > amount
mjavurek Nov 21, 2024
165dab6
Update application_helper.rb: typo amout > amount
mjavurek Nov 21, 2024
44d6b24
Update show.html.haml: use format_currency_difference()
mjavurek Nov 21, 2024
268d1bb
Update unpaid.html.haml: use format_currency_difference()
mjavurek Nov 21, 2024
ae55dc0
Update _summary.haml: added stock order sum and transport costs, move…
mjavurek Nov 21, 2024
3adab90
Update _invoice.html.haml: buttons for invoice view and edit
mjavurek Nov 21, 2024
dbb14f2
Update _invoice.html.haml: show numbers of invoices and deliveries, s…
mjavurek Nov 21, 2024
00f3317
Update de.yml: finance balancing summary, invoice
mjavurek Nov 21, 2024
13c06e1
Update en.yml: finance balancing summary, invoice
mjavurek Nov 21, 2024
41c6866
Update application_helper.rb whitespace removed
mjavurek Nov 21, 2024
6706813
Update .rubocop_todo.yml: increased Metrics/ModuleLength to 200
mjavurek Nov 21, 2024
cb00c9e
Update order.rb: whitespace removed, %i for string array
mjavurek Nov 21, 2024
ae7c497
Update unpaid.html.haml: invoice_profit formatting corrected
mjavurek Nov 21, 2024
2d711f4
Update .rubocop_todo.yml: Metrics/ClassLength increased
mjavurek Nov 21, 2024
45d2215
Update invoice.rb: missing profit() method added
mjavurek Nov 21, 2024
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
4 changes: 2 additions & 2 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ Metrics/BlockNesting:
# Offense count: 18
# Configuration parameters: CountComments, CountAsOne.
Metrics/ClassLength:
Max: 304
Max: 350

# Offense count: 51
# Configuration parameters: AllowedMethods, AllowedPatterns.
Expand All @@ -223,7 +223,7 @@ Metrics/MethodLength:
# Offense count: 4
# Configuration parameters: CountComments, CountAsOne.
Metrics/ModuleLength:
Max: 192
Max: 200

# Offense count: 1
# Configuration parameters: CountKeywordArgs, MaxOptionalParameters.
Expand Down
8 changes: 6 additions & 2 deletions app/assets/stylesheets/bootstrap_and_overrides.css.less
Original file line number Diff line number Diff line change
Expand Up @@ -537,7 +537,11 @@ span.positive_amount {
color: black;
}

span.negative_amout {
span.positive_green {
color: green;
}

span.negative_amount {
color: red;
}

Expand All @@ -547,4 +551,4 @@ span.negative_amout {

details {
cursor: pointer;
}
}
7 changes: 6 additions & 1 deletion app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,15 @@ def format_datetime_timespec(time, format)
def format_currency(amount)
return nil if amount.nil?

class_name = amount < 0 ? 'negative_amout' : 'positive_amount'
class_name = amount < 0 ? 'negative_amount' : 'positive_amount'
content_tag :span, number_to_currency(amount), class: class_name
end

def format_currency_difference(amount)
class_name = amount < 0 ? 'negative_amount' : 'positive_green'
content_tag :span, (amount > 0 ? '+' : '') + number_to_currency(amount), class: class_name
end

# Splits an IBAN into groups of 4 digits displayed with margins in between
def format_iban(iban)
iban && iban.scan(/..?.?.?/).map { |item| content_tag(:span, item, style: 'margin-right: 0.5em;') }.join.html_safe
Expand Down
30 changes: 20 additions & 10 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,22 +30,32 @@ 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

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

orders_sum + orders_transport_sum
def profit(type = :without_markup)
expected_amount(type) - net_amount
end

protected
Expand Down
12 changes: 11 additions & 1 deletion app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,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 @@ -230,6 +230,14 @@ def sum(type = :gross)
end
end
end
elsif type == :stock_order
for go in group_orders.includes(group_order_articles: { order_article: %i[article article_price] }).where(ordergroup: nil)
for goa in go.group_order_articles
total += goa.result * goa.order_article.price.gross_price
end
end
elsif type == :transport
total = group_orders.where.not(ordergroup: nil).sum(:transport)
end
total
end
Expand Down Expand Up @@ -397,6 +405,8 @@ def distribute_transport
group_orders.each do |go|
go.transport = (amount * go.group_order_articles.sum(:result)).ceil(2)
end
when Order.transport_distributions[:skip]
group_orders.each { |go| go.transport = 0 }
end
end

Expand Down
32 changes: 31 additions & 1 deletion app/views/finance/balancing/_invoice.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,38 @@
%tr
%td= t('.refund_adjusted_amount')
%td.curreny= number_to_currency invoice.net_amount

- n_orders = invoice.orders.count
- n_deliveries = invoice.deliveries.count
- if n_orders > 1
%tr
%td= t('.orders')
%td.numeric= n_orders.to_s
- if n_deliveries > 0
%tr
%td= t('.deliveries')
%td.numeric= n_deliveries.to_s

- profit_without_markup = invoice.profit(:without_markup)
- profit_with_markup = invoice.profit(:with_markup)
%tr
%td
= t('.fc_profit')
- if profit_with_markup > profit_without_markup
%small= t('.without_extra_charge')
- else
= ':'
%td.numeric= format_currency_difference(profit_without_markup)
- if profit_with_markup > profit_without_markup
%tr
%td
= t('.fc_profit')
%small= t('.with_extra_charge')
%td#order_profit.numeric= format_currency_difference(profit_with_markup)

%br/
= link_to t('.edit'), edit_finance_invoice_path(invoice)
= link_to t('.view'), finance_invoice_path(invoice), class: 'btn'
= link_to t('.edit'), edit_finance_invoice_path(invoice), class: 'btn'

- else
= t '.new_body'
Expand Down
42 changes: 28 additions & 14 deletions app/views/finance/balancing/_summary.haml
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,42 @@
= t('.duration', {starts: format_date(order.starts), ends: format_date(order.ends)})
%p
%table
- gross_sum = order.sum(:gross)
- fc_sum = order.sum(:fc)
- stock_order_amount = order.sum(:stock_order)
%tr
%td= t('.net_amount')
%td.numeric= number_to_currency(order.sum(:net))
%tr
%td= t('.gross_amount')
%td.numeric= number_to_currency(order.sum(:gross))
%tr
%td= t('.fc_amount')
%td.numeric= number_to_currency(order.sum(:fc))
%td.numeric= number_to_currency(gross_sum)
- if fc_sum > gross_sum
%tr
%td= t('.fc_amount')
%td.numeric= number_to_currency(fc_sum)
%tr
%td= t('.groups_amount')
%td.numeric= number_to_currency(order.sum(:groups))
%tr
%td
= t('.fc_profit')
%small= t('.without_extra_charge')
%td.numeric= number_to_currency(order.profit(:without_markup => true))
%tr
%td
= t('.fc_profit')
%small= t('.with_extra_charge')
%td#order_profit.numeric= number_to_currency(order.profit)

- if stock_order_amount > 0
%tr
%td= t('.stock_order_amount')
%td.numeric= number_to_currency(stock_order_amount)

- total_transport_costs = order.transport || 0
- if total_transport_costs > 0
%tr
%td= t('.total_transport_costs')
%td.numeric= number_to_currency(total_transport_costs)
- group_transport_costs = order.sum(:transport)
- fc_transport_costs = total_transport_costs - group_transport_costs
%tr
%td= t('.group_transport_costs')
%td.numeric= number_to_currency(group_transport_costs)
%tr
%td= t('.fc_transport_costs')
%td.numeric= number_to_currency(fc_transport_costs)

#summaryChangedWarning.alert(style="display:none;")
%strong= t '.changed'
%br/
Expand Down
32 changes: 26 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,19 @@
%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') : '') + ':'
%dd
= format_currency_difference(total - @invoice.net_amount)
- if total_fc > total
%dt= t('.fc_profit') + ' ' + t('.with_extra_charge') + ':'
%dd
= format_currency_difference(total_fc - @invoice.net_amount)

- if @invoice.attachments.attached?
%dt= heading_helper(Invoice, :attachment) + ':'
Expand Down
9 changes: 5 additions & 4 deletions app/views/finance/invoices/unpaid.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
%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
= 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)
= format_currency_difference(invoice_amount_diff)
- if invoice_profit > invoice_amount_diff
= ' / '
= format_currency_difference(invoice_profit)
- if invoice.attachments.attached?
- for attachment in invoice.attachments
= link_to attachment.filename, url_for(attachment)
Expand Down
14 changes: 13 additions & 1 deletion config/locales/de.yml
Original file line number Diff line number Diff line change
Expand Up @@ -820,6 +820,7 @@ de:
title: Beendete Bestellungen
invoice:
edit: Rechnung bearbeiten
view: Rechnung ansehen
invoice_amount: 'Rechnungsbetrag:'
invoice_date: 'Rechnungsdatum:'
invoice_number: 'Rechnungsnummer:'
Expand All @@ -828,6 +829,11 @@ de:
new_body: 'Eine Rechnung für diese Bestellung anlegen:'
plus_refund_credited: "+ Pfand gutgeschrieben:"
refund_adjusted_amount: 'pfandbereinigter Betrag:'
fc_profit: 'FC Überschuss'
with_extra_charge: 'mit Aufschlag:'
without_extra_charge: 'ohne Aufschlag:'
orders: 'Anzahl Bestellungen:'
deliveries: 'Anzahl Lager-Lieferungen:'
new:
alert: Achtung, Bestellung wurde schon abgerechnet
articles_overview: Artikelübersicht
Expand Down Expand Up @@ -859,13 +865,16 @@ de:
changed: Daten wurden verändert!
duration: von %{starts} bis %{ends}
fc_amount: 'FC-Betrag:'
fc_profit: FC Gewinn
gross_amount: 'Bruttobetrag:'
groups_amount: 'Gruppenbeträge:'
stock_order_amount: 'Lager-Bestellung:'
net_amount: 'Nettobetrag:'
reload: Zusammenfassung neu laden
with_extra_charge: 'mit Aufschlag:'
without_extra_charge: 'ohne Aufschlag:'
total_transport_costs: 'Transportkosten gesamt:'
group_transport_costs: 'Transportkosten Gruppen:'
fc_transport_costs: 'Transportkosten FC:'
bank_accounts:
assign_unlinked_transactions:
notice: 'Es wurden %{count} Transaktionen zugeordnet.'
Expand Down Expand Up @@ -990,6 +999,9 @@ de:
title: Neue Rechnung anlegen
show:
title: Rechnung %{number}
fc_profit: FC Überschuss
with_extra_charge: ' mit Aufschlag'
without_extra_charge: ' ohne Aufschlag'
unpaid:
invoices_sum: Gesamtsumme
invoices_text: Verwendungszweck
Expand Down
Loading
Loading