Skip to content

Commit

Permalink
fix: invoices and journal entries
Browse files Browse the repository at this point in the history
  • Loading branch information
diegosteiner committed Dec 7, 2024
1 parent dc614af commit b3f643e
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 28 deletions.
5 changes: 1 addition & 4 deletions app/models/data_digest_templates/accounting_journal_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,7 @@ def crunch(records)

formatter(:taf) do |_options = {}|
records.keys.map do |source|
TafBlock::Collection.new do
derive(source.booking.tenant)
derive(source)
end
TafBlock::Collection.new { derive(source) }
end.join("\n\n")
end

Expand Down
10 changes: 5 additions & 5 deletions app/models/invoice.rb
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,16 @@ def journal_entries
[debitor_journal_entry] + invoice_parts.map(&:journal_entries)
end

def human_ref
format('HV%05d', id)
def accounting_ref
format('HV%05d', id + 1)
end

def debitor_journal_entry
Accounting::JournalEntry.new(
account: booking.tenant.accounting_debitor_account_nr,
account: organisation.accounting_settings.debitor_account_nr,
date: issued_at, amount:, amount_type: :brutto, side: :soll,
reference: human_ref, source: self, currency:, booking:,
text: [self.class.model_name.human, human_ref].join(' ')
reference: accounting_ref, source: self, currency:, booking:,
text: "#{self.class.model_name.human} #{accounting_ref} - #{booking.tenant.last_name}"
)
end
end
4 changes: 0 additions & 4 deletions app/models/invoice_part.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,6 @@ def journal_entries
nil
end

# def amount_netto
# amount - (vat_category&.amount_tax(amount) || 0)
# end

def self.from_usage(usage, **attributes)
return unless usage

Expand Down
4 changes: 2 additions & 2 deletions app/models/invoice_parts/add.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ def journal_entries # rubocop:disable Metrics/AbcSize
[
Accounting::JournalEntry.new(
account: tarif&.accounting_account_nr, date: invoice.issued_at, amount: amount.abs, amount_type: :brutto,
side: :haben, tax_code: vat_category&.accounting_vat_code, reference: invoice.human_ref, source: self,
side: :haben, tax_code: vat_category&.accounting_vat_code, reference: invoice.accounting_ref, source: self,
currency: organisation.currency, booking:, cost_center: tarif&.accounting_profit_center_nr,
text: [invoice.class.model_name.human, invoice.human_ref, label].join(' ')
text: "#{invoice.class.model_name.human} #{invoice.accounting_ref} #{label}"
)
]
end
Expand Down
8 changes: 4 additions & 4 deletions app/models/vat_category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ def to_s
"#{label} (#{formatted_percentage})"
end

def amount_tax(amount)
return 0 if percentage.blank? || percentage.zero?

(amount / (100 + percentage)) * percentage
def breakdown(amount)
tax = 0
tax = (amount / (100 + percentage)) * percentage if percentage.present?
{ tax:, brutto: amount, netto: (amount - tax) }
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ def vat_table_data
number_to_percentage(vat_category.percentage, precision: 2),
organisation.currency,
number_to_currency(amount, unit: ''),
number_to_currency(vat_category.amount_tax(amount), unit: '')
number_to_currency(vat_category.breakdown(amount)[:tax], unit: '')
]
end
end
Expand Down
17 changes: 9 additions & 8 deletions app/services/taf_block.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,9 @@ def self.block(...)
}.freeze

CAST_CLASSES = { # rubocop:disable Lint/ConstantDefinitionInBlock
boolean: [::FalseClass, ::TrueClass],
decimal: [::BigDecimal, ::Float],
number: [::Numeric],
date: [::Date, ::DateTime, ::ActiveSupport::TimeWithZone],
string: [::String]
boolean: [::FalseClass, ::TrueClass], decimal: [::BigDecimal, ::Float],
number: [::Numeric], date: [::Date, ::DateTime, ::ActiveSupport::TimeWithZone],
string: [::String], symbol: [::Symbol]
}.freeze

def self.cast(value, as: nil)
Expand Down Expand Up @@ -142,6 +140,8 @@ def self.derive(value, **override)
# String[5]; The Id of the tax. [MWSt-Kürzel]
TaxId: journal_entry.tax_code,

MkTxB: journal_entry.tax_code.present?,

# String[61*]; This string specifies the first line of the booking text.
Text: journal_entry.text&.slice(0..59)&.lines&.first&.strip || '-', # rubocop:disable Style/SafeNavigationChainLength

Expand Down Expand Up @@ -187,16 +187,17 @@ def self.derive(value, **override)
derive_from Invoice do |invoice, **_override|
next unless invoice.is_a?(Invoices::Invoice) || invoice.is_a?(Invoices::Deposit)

op_id = Value.cast(invoice.human_ref, as: :symbol)
op_id = Value.cast(invoice.accounting_ref, as: :symbol)
pk_key = [invoice.booking.tenant.accounting_debitor_account_nr,
invoice.organisation.accounting_settings.currency_account_nr].then { "[#{_1.join(',')}]" }

journal_entries = invoice.journal_entries.flatten.compact

[
derive(invoice.booking.tenant),
new(:OPd, **{ PkKey: pk_key, OpId: op_id, ZabId: '15T' }),
new(:Blg, **{ OpId: op_id, Date: invoice.issued_at, Orig: true }) do
derive(journal_entries.shift, Flags: 1, OpId: op_id)
new(:Blg, **{ Date: invoice.issued_at, Orig: true }) do
derive(journal_entries.shift, Flags: 1, OpId: op_id, PkKey: pk_key, CAcc: :div)
journal_entries.each { derive(_1, OpId: op_id) }
end
]
Expand Down
19 changes: 19 additions & 0 deletions app/views/renderables/invoice_parts/deposit/_form_fields.html.slim
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
div
= f.hidden_field :id
= f.hidden_field :usage_id
= f.hidden_field :type
= f.hidden_field :vat_category_id

.row
.col-1.py-2
= f.check_box :_destroy, { checked: invoice_part.apply, hide_label: true }, '0', '1'
.col-4
= f.text_field :label, hide_label: true
.col-4
= f.text_field :breakdown, hide_label: true
.col-2
= f.text_field :amount, hide_label: true, class: 'text-end', inputmode: "numeric"
.col-1.p-2
- unless invoice_part.new_record? || invoice_part.invoice.blank?
= link_to edit_manage_invoice_invoice_part_path(invoice_part.invoice, invoice_part)
i.fa.fa-pencil

0 comments on commit b3f643e

Please sign in to comment.