Class: Xrechnung::Document

Inherits:
Object
  • Object
show all
Includes:
MemberContainer
Defined in:
lib/xrechnung.rb

Constant Summary collapse

DEFAULT_CUSTOMIZATION_ID =

Default customization specs

"urn:cen.eu:en16931:2017#compliant#urn:xeinkauf.de:kosit:xrechnung_3.0"
DEFAULT_PROFILE_ID =
"urn:fdc:peppol.eu:2017:poacc:billing:01:1.0"
COMMON_NAMESPACES =
{
  "xmlns:ubl"          => "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2",
  "xmlns:cac"          => "urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2",
  "xmlns:cbc"          => "urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2",
  "xmlns:xsi"          => "http://www.w3.org/2001/XMLSchema-instance",
  "xsi:schemaLocation" => "urn:oasis:names:specification:ubl:schema:xsd:Invoice-2 http://docs.oasis-open.org/ubl/os-UBL-2.1/xsd/maindoc/UBL-Invoice-2.1.xsd",
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from MemberContainer

#[], #[]=, included

Constructor Details

#initializeDocument

Returns a new instance of Document.



304
305
306
307
# File 'lib/xrechnung.rb', line 304

def initialize
  super
  self.legal_monetary_total ||= Xrechnung::LegalMonetaryTotal.new
end

Instance Attribute Details

#accounting_customer_partyXrechnung::Party

Returns:



151
# File 'lib/xrechnung.rb', line 151

member :accounting_customer_party, type: Xrechnung::Party

#accounting_supplier_partyXrechnung::Party

Returns:



143
# File 'lib/xrechnung.rb', line 143

member :accounting_supplier_party, type: Xrechnung::Party

#additional_document_referencesArray

Returns:

  • (Array)


198
# File 'lib/xrechnung.rb', line 198

member :additional_document_references, type: Array, default: []

#allowance_chargesArray

Returns:

  • (Array)


294
# File 'lib/xrechnung.rb', line 294

member :allowance_charges, type: Array, default: []

#billing_referenceXrechnung::InvoiceDocumentReference



193
# File 'lib/xrechnung.rb', line 193

member :billing_reference, type: Xrechnung::InvoiceDocumentReference, optional: true

#buyer_referenceString

Returns:

  • (String)


189
# File 'lib/xrechnung.rb', line 189

member :buyer_reference, type: String

#contract_document_reference_idString

Returns:

  • (String)


210
# File 'lib/xrechnung.rb', line 210

member :contract_document_reference_id, type: String, optional: true

#customization_idString

Returns:

  • (String)


48
# File 'lib/xrechnung.rb', line 48

member :customization_id, type: String, default: DEFAULT_CUSTOMIZATION_ID

#document_currency_codeString

Returns:

  • (String)


110
# File 'lib/xrechnung.rb', line 110

member :document_currency_code, type: String, default: "EUR"

#due_dateDate

Returns:

  • (Date)


79
# File 'lib/xrechnung.rb', line 79

member :due_date, type: Date

#idString

Returns:

  • (String)


63
# File 'lib/xrechnung.rb', line 63

member :id, type: String

#invoice_linesArray

Returns:

  • (Array)


283
# File 'lib/xrechnung.rb', line 283

member :invoice_lines, type: Array, default: []

#invoice_periodXrechnung::InvoicePeriod



202
# File 'lib/xrechnung.rb', line 202

member :invoice_period, type: Xrechnung::InvoicePeriod, optional: true

#invoice_type_codeInteger

Returns:

  • (Integer)


97
# File 'lib/xrechnung.rb', line 97

member :invoice_type_code, type: Integer, default: 380

#issue_dateDate

Returns:

  • (Date)


71
# File 'lib/xrechnung.rb', line 71

member :issue_date, type: Date


274
# File 'lib/xrechnung.rb', line 274

member :legal_monetary_total, type: Xrechnung::LegalMonetaryTotal, is_private: true

#notesArray

Returns:

  • (Array)


119
# File 'lib/xrechnung.rb', line 119

member :notes, type: Array, default: []

#payee_partyXrechnung::PayeeParty



245
# File 'lib/xrechnung.rb', line 245

member :payee_party, type: Xrechnung::PayeeParty, optional: true

#payment_meansXrechnung::PaymentMeans



236
# File 'lib/xrechnung.rb', line 236

member :payment_means, type: Xrechnung::PaymentMeans

#payment_terms_noteString

Returns:

  • (String)


256
# File 'lib/xrechnung.rb', line 256

member :payment_terms_note, type: String

#profile_idString

Returns:

  • (String)


54
# File 'lib/xrechnung.rb', line 54

member :profile_id, type: String, default: DEFAULT_PROFILE_ID

#project_reference_idString

Returns:

  • (String)


218
# File 'lib/xrechnung.rb', line 218

member :project_reference_id, type: String, optional: true

#purchase_order_referenceString

Returns:

  • (String)


127
# File 'lib/xrechnung.rb', line 127

member :purchase_order_reference, type: String

#sales_order_referenceString

Returns:

  • (String)


135
# File 'lib/xrechnung.rb', line 135

member :sales_order_reference, type: String, optional: true

#tax_currency_codeString

Returns:

  • (String)


173
# File 'lib/xrechnung.rb', line 173

member :tax_currency_code, type: String

#tax_point_dateDate

Returns:

  • (Date)


160
# File 'lib/xrechnung.rb', line 160

member :tax_point_date, type: Date

#tax_representative_partyXrechnung::Party

Returns:



227
# File 'lib/xrechnung.rb', line 227

member :tax_representative_party, type: Xrechnung::Party, optional: true

#tax_totalXrechnung::TaxTotal

Returns:



265
# File 'lib/xrechnung.rb', line 265

member :tax_total, type: Xrechnung::TaxTotal, is_private: true

Instance Method Details

#prepaid_amount=(value) ⇒ Object



309
310
311
# File 'lib/xrechnung.rb', line 309

def prepaid_amount=(value)
  legal_monetary_total.prepaid_amount = value
end

#to_xml(indent: 2, target: "") ⇒ Object



313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/xrechnung.rb', line 313

def to_xml(indent: 2, target: "")
  update_amounts

  xml = Builder::XmlMarkup.new(indent: indent, target: target)
  xml.instruct! :xml, version: "1.0", encoding: "UTF-8"

  xml.ubl :Invoice, COMMON_NAMESPACES do
    xml.cbc :CustomizationID, customization_id
    xml.cbc :ProfileID, profile_id
    xml.cbc :ID, id
    xml.cbc :IssueDate, issue_date
    xml.cbc :DueDate, due_date
    xml.cbc :InvoiceTypeCode, invoice_type_code

    notes.each do |note|
      xml.cbc :Note, note
    end

    xml.cbc :TaxPointDate, tax_point_date unless tax_point_date.nil?
    xml.cbc :DocumentCurrencyCode, document_currency_code
    xml.cbc :TaxCurrencyCode, tax_currency_code unless tax_currency_code.nil?
    xml.cbc :BuyerReference, buyer_reference

    invoice_period&.to_xml(xml) unless self.class.members[:invoice_period].optional && invoice_period.nil?

    unless purchase_order_reference.nil? &&
           self.class.members[:sales_order_reference].optional && sales_order_reference.nil?
      xml.cac :OrderReference do
        xml.cbc :ID, purchase_order_reference
        unless self.class.members[:sales_order_reference].optional && sales_order_reference.nil?
          xml.cbc :SalesOrderID, sales_order_reference
        end
      end
    end

    unless self.class.members[:billing_reference].optional && billing_reference.nil?
      xml.cac :BillingReference do
        billing_reference&.to_xml(xml)
      end
    end

    unless self.class.members[:contract_document_reference_id].optional && contract_document_reference_id.nil?
      xml.cac :ContractDocumentReference do
        xml.cbc :ID, contract_document_reference_id
      end
    end

    unless self.class.members[:project_reference_id].optional && project_reference_id.nil?
      xml.cac :ProjectReference do
        xml.cbc :ID, project_reference_id
      end
    end

    additional_document_references.each { _1.to_xml(xml) }

    xml.cac :AccountingSupplierParty do
      accounting_supplier_party&.to_xml(xml)
    end

    xml.cac :AccountingCustomerParty do
      accounting_customer_party&.to_xml(xml)
    end

    unless self.class.members[:tax_representative_party].optional && tax_representative_party.nil?
      xml.cac :TaxRepresentativeParty do
        tax_representative_party&.to_xml(xml)
      end
    end

    if payment_means
      xml.cac :PaymentMeans do
        payment_means&.to_xml(xml)
      end
    end

    payee_party&.to_xml(xml) unless self.class.members[:payee_party].optional && payee_party.nil?

    unless payment_terms_note.blank?
      xml.cac :PaymentTerms do
        xml.cbc :Note, payment_terms_note
      end
    end

    allowance_charges.each { _1.to_xml(xml) }

    xml.cac :TaxTotal do
      tax_total&.to_xml(xml)
    end

    xml.cac :LegalMonetaryTotal do
      legal_monetary_total&.to_xml(xml)
    end

    invoice_lines.each { _1.to_xml(xml) }
  end

  target
end

#update_amountsObject



412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
# File 'lib/xrechnung.rb', line 412

def update_amounts
  self.tax_total = Xrechnung::TaxTotal.new

  invoice_lines.each do |invoice_line|
    tax_total.get_tax_subtotal(invoice_line.item.classified_tax_category).taxable_amount += invoice_line.line_extension_amount
  end

  allowance_charges.each do |ac|
    element = tax_total.get_tax_subtotal(ac.tax_category)
    if ac.charge_indicator
      element.taxable_amount += ac.amount
    else
      element.taxable_amount -= ac.amount
    end
  end

  tax_total.update_amounts

  currency_id = tax_total.tax_amount.currency_id
  zero        = Xrechnung::Currency.new(currency_id: currency_id, value: BigDecimal(0))

  legal_monetary_total.line_extension_amount  = zero
  legal_monetary_total.allowance_total_amount = zero
  legal_monetary_total.charge_total_amount    = zero
  legal_monetary_total.prepaid_amount       ||= zero

  invoice_lines.each do |line|
    legal_monetary_total.line_extension_amount += line.line_extension_amount
  end

  allowance_charges.each do |ac|
    if ac.charge_indicator
      legal_monetary_total.charge_total_amount += ac.amount
    else
      legal_monetary_total.allowance_total_amount += ac.amount
    end
  end

  legal_monetary_total.tax_exclusive_amount = legal_monetary_total.line_extension_amount + legal_monetary_total.charge_total_amount - legal_monetary_total.allowance_total_amount
  legal_monetary_total.tax_inclusive_amount = legal_monetary_total.tax_exclusive_amount + tax_total.tax_amount
  legal_monetary_total.payable_amount       = legal_monetary_total.tax_inclusive_amount - legal_monetary_total.prepaid_amount
end