Class: Ksef::FA3::Order

Inherits:
Data
  • Object
show all
Includes:
Canonical
Defined in:
lib/ksef/fa3/order.rb,
lib/ksef/fa3/order.rb

Overview

Construction and serialisation for Order.

Constant Summary collapse

NEEDS_LINES =
"An order needs at least one position. `ZamowienieWiersz` is minOccurs=1 " \
"within `Zamowienie`, so an order without one cannot be serialised."

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Canonical

#with

Constructor Details

#initialize(total:, lines:) ⇒ Order

Returns a new instance of Order.

Parameters:

  • total (BigDecimal, Integer, String) —

    WartoscZamowienia, including tax

  • lines (OrderLine, Array<OrderLine>) —

    ZamowienieWiersz, at least one

Raises:



28
29
30
31
32
33
34
# File 'lib/ksef/fa3/order.rb', line 28

def initialize(total:, lines:)
  rows = Correction.wrap(lines)
  raise ValidationError, NEEDS_LINES if rows.empty?

  super(total: Formatting.decimal(total).round(Formatting::AMOUNT_SCALE),
        lines: self.class.positioned(rows).freeze)
end

Instance Attribute Details

#lines ⇒ Object (readonly)

Returns the value of attribute lines

Returns:

  • (Object) —

    the current value of lines



16
17
18
# File 'lib/ksef/fa3/order.rb', line 16

def lines
  @lines
end

#total ⇒ Object (readonly)

Returns the value of attribute total

Returns:

  • (Object) —

    the current value of total



16
17
18
# File 'lib/ksef/fa3/order.rb', line 16

def total
  @total
end

Class Method Details

.positioned(lines) ⇒ Object

The same rule Invoice.positioned follows, for the same reason: a row number that merely repeats its position carries nothing, and storing it would make an order built with explicit numbering unequal to its own parsed form.



39
40
41
42
43
44
45
# File 'lib/ksef/fa3/order.rb', line 39

def self.positioned(lines)
  lines.each_with_index.map do |line, index|
    next line unless line.is_a?(OrderLine) && line.row_number == index + 1

    line.with(row_number: nil)
  end
end

Instance Method Details

#to_fa3 ⇒ Object



47
48
49
50
51
52
# File 'lib/ksef/fa3/order.rb', line 47

def to_fa3
  {
    "WartoscZamowienia" => Formatting.amount(total),
    "ZamowienieWiersz" => lines.each_with_index.map { |line, index| line.to_fa3(row_number: index + 1) }
  }
end