Class: ISDOCOutputBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/isdoc_output_builder.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ledger_item, options) ⇒ ISDOCOutputBuilder

Returns a new instance of ISDOCOutputBuilder.



6
7
8
9
# File 'lib/isdoc_output_builder.rb', line 6

def initialize(ledger_item, options)
  @ledger_item = ledger_item
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_id, *args, &block) ⇒ Object



234
235
236
237
238
239
240
241
242
# File 'lib/isdoc_output_builder.rb', line 234

def method_missing(method_id, *args, &block)
  # method renaming if requested in options
  if options.has_key?(method_id.to_sym)
    method_id = options[method_id.to_sym]
    # allows setting default values directly instead of calling a method
    return method_id unless ledger_item.respond_to?(method_id)
  end
  return ledger_item.send(method_id, *args) if ledger_item.respond_to?(method_id)
end

Instance Attribute Details

#ledger_itemObject (readonly)

Returns the value of attribute ledger_item.



4
5
6
# File 'lib/isdoc_output_builder.rb', line 4

def ledger_item
  @ledger_item
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/isdoc_output_builder.rb', line 4

def options
  @options
end

Instance Method Details

#buildObject



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/isdoc_output_builder.rb', line 11

def build
  isdoc = Builder::XmlMarkup.new :indent => 4
  isdoc.instruct! :xml

  isdoc.encoded_tag!( :Invoice, :xmlns=>"http://isdoc.cz/namespace/invoice", :version=>"5.2") do |invoice|
    invoice.encoded_tag! :DocumentType, document_type
    invoice.encoded_tag! :ID, document_id
    invoice.encoded_tag! :UUID, document_uuid

    invoice.encoded_tag! :IssueDate, issue_date
    invoice.encoded_tag! :TaxPointDate, tax_point_date if tax_point_date

    invoice.encoded_tag! :Note, note if note

    invoice.encoded_tag! :OrderReferences do |order_references|
      order_references.encoded_tag! :OrderReference do |order_reference|
        order_reference.encoded_tag! :SalesOrderID
        order_reference.encoded_tag! :ExternalOrderID, external_order_id
        order_reference.encoded_tag! :IssueDate, issue_date
      end
    end if external_order_id

    invoice.encoded_tag! :LocalCurrencyCode, local_currency_code
    invoice.encoded_tag! :CurrRate, 1
    invoice.encoded_tag! :RefCurrRate, 1

    build_dispatches(invoice)

    invoice.encoded_tag! :AccountingSupplierParty do |supplier|
      build_party supplier, seller_details
    end

    invoice.encoded_tag! :SellerSupplierParty do |sender|
      build_party sender, sender_details
    end if sender_details

    invoice.encoded_tag! :AccountingCustomerParty do |customer|
      build_party customer, customer_details
    end

    invoice.encoded_tag! :BuyerCustomerParty do |recipient|
      build_party recipient, recipient_details
    end if recipient_details

    invoice.encoded_tag! :InvoiceLines do |invoice_lines_tag|
      build_invoice_lines invoice_lines_tag, invoice_lines
    end

    invoice.encoded_tag! :NonTaxedDeposits do |non_taxed_deposits_tag|
      build_non_taxed_deposits non_taxed_deposits_tag, non_taxed_deposits
    end if non_taxed_deposits

    invoice.encoded_tag! :TaxedDeposits do |taxed_deposits_tag|
      build_taxed_deposits taxed_deposits_tag, taxed_deposits
    end if taxed_deposits

    invoice.encoded_tag! :TaxTotal do |tax_total|
      build_tax_sub_totals(tax_total, tax_sub_totals)
      tax_total.encoded_tag! :TaxAmount, tax_amount
    end

    invoice.encoded_tag! :LegalMonetaryTotal do |legal_monetary_total|
      legal_monetary_total.encoded_tag! :TaxExclusiveAmount, tax_exclusive_amount
      legal_monetary_total.encoded_tag! :TaxInclusiveAmount, tax_inclusive_amount
      legal_monetary_total.encoded_tag! :AlreadyClaimedTaxExclusiveAmount, already_claimed_tax_exclusive_amount
      legal_monetary_total.encoded_tag! :AlreadyClaimedTaxInclusiveAmount, already_claimed_tax_inclusive_amount
      legal_monetary_total.encoded_tag! :DifferenceTaxExclusiveAmount, difference_tax_exclusive_amount
      legal_monetary_total.encoded_tag! :DifferenceTaxInclusiveAmount, difference_tax_inclusive_amount
      legal_monetary_total.encoded_tag! :PaidDepositsAmount, paid_deposits_amount
      legal_monetary_total.encoded_tag! :PayableAmount, payable_amount
    end

    invoice.encoded_tag! :PaymentMeans do |payment_means|
      payment_means.encoded_tag! :Payment do |payment|
        payment.encoded_tag! :PaidAmount, payment_means_detail[:paid_amount]
        payment.encoded_tag! :PaymentMeansCode, payment_means_detail[:payments_mean_code]
        payment.encoded_tag! :Details do |details|
          (details, payment_means_detail, true)
        end #if payment_means_detail[:payments_mean_code.to_i==42]
      end
      payment_means.encoded_tag! :AlternateBankAccounts do |alternate_bank_accounts|
        for  in payment_means_detail[:alternate_bank_accounts]
          alternate_bank_accounts.encoded_tag! :AlternateBankAccount do
            (alternate_bank_accounts, )
          end
        end
      end if payment_means_detail[:alternate_bank_accounts]
    end if payment_means_detail
  end
  isdoc.target!
end

#build_bank_account(xml, details, main_bank_account = false) ⇒ Object



179
180
181
182
183
184
185
186
187
# File 'lib/isdoc_output_builder.rb', line 179

def (xml, details, =false)
  xml.encoded_tag! :PaymentDueDate, details[:payment_due_date] if details[:payment_due_date]
  xml.encoded_tag! :ID, details[:id]
  xml.encoded_tag! :BankCode, details[:bank_code]
  xml.encoded_tag! :Name, details[:name]
  xml.encoded_tag! :IBAN, details[:iban]
  xml.encoded_tag! :BIC, details[:bic]
  xml.encoded_tag! :VariableSymbol, payment_means_detail[:variable_symbol] if 
end

#build_dispatches(xml) ⇒ Object



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/isdoc_output_builder.rb', line 214

def build_dispatches(xml)
  if ledger_item.respond_to?(:dispatches) && dispatches
    xml.tag! :Extensions do |extensions|
      extensions.tag! :Dispatches, :xmlns => "http://czreg.cz/isdoc/namespace/dispatch-1.0" do |disps|
        dispatches.each_key do |dispatch|
          disps.encoded_tag! "Dispatch" do |disp|
            if dispatch == :emails
              Array(dispatches[dispatch]).each do |email|
                disp.encoded_tag! "Email", email
              end
            else
              disp.encoded_tag! dispatch.to_s.capitalize, dispatches[dispatch]
            end
          end
        end
      end
    end
  end
end

#build_invoice_lines(invoice_lines, items) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# File 'lib/isdoc_output_builder.rb', line 136

def build_invoice_lines(invoice_lines, items)
  items.each_with_index do |item, index|
    invoice_lines.encoded_tag! :InvoiceLine do |invoice_line|
      invoice_line.encoded_tag! :ID, index+1
      invoice_line.encoded_tag! :LineExtensionAmount, item[:line_extension_amount]
      invoice_line.encoded_tag! :LineExtensionAmountTaxInclusive, item[:line_extension_amount_tax_inclusive]
      invoice_line.encoded_tag! :LineExtensionTaxAmount, item[:line_extension_tax_amount]
      invoice_line.encoded_tag! :UnitPrice, item[:unit_price]
      invoice_line.encoded_tag! :UnitPriceTaxInclusive, item[:unit_price_tax_inclusive]
      invoice_line.encoded_tag! :ClassifiedTaxCategory do |classified_tax_category|
        classified_tax_category.encoded_tag! :Percent, item[:tax_percent]
        classified_tax_category.encoded_tag! :VATCalculationMethod, item[:vat_calculation_method]
      end
      invoice_line.encoded_tag! :Item do |item_tag|
        item_tag.encoded_tag! :Description, item[:description] if item[:description]
        item_tag.encoded_tag! :SellersItemIdentification do |sellers_item_identification_tag|
          sellers_item_identification_tag.encoded_tag! :ID, item[:sellers_item_identification]
        end if item[:sellers_item_identification]
      end
      draw_invoice_line_extensions(invoice_line, item) if ledger_item.respond_to?(:draw_invoice_line_extensions)
    end
  end
end

#build_non_taxed_deposits(xml, details) ⇒ Object



189
190
191
192
193
194
195
196
197
# File 'lib/isdoc_output_builder.rb', line 189

def build_non_taxed_deposits(xml, details)
  for detail in details
    xml.encoded_tag! :NonTaxedDeposit do |non_taxed_deposit|
      non_taxed_deposit.encoded_tag! :ID, detail[:id]
      non_taxed_deposit.encoded_tag! :VariableSymbol, detail[:variable_symbol]
      non_taxed_deposit.encoded_tag! :DepositAmount, detail[:deposit_amount]
    end
  end
end

#build_party(xml, details) ⇒ Object



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/isdoc_output_builder.rb', line 103

def build_party(xml, details)
  details = details.symbolize_keys
  xml.encoded_tag! :Party do |party|
    party.encoded_tag! :PartyIdentification do |party_identification|
      party_identification.encoded_tag! :UserID, details[:user_id] if details[:user_id]
      # party_identification.encoded_tag! :CatalogFirmIdentification
      party_identification.encoded_tag! :ID, details[:company_id]
    end
    party.encoded_tag! :PartyName do |party_name|
      party_name.encoded_tag! :Name, details[:name]
    end
    party.encoded_tag! :PostalAddress do |postal_address|
      postal_address.encoded_tag! :StreetName, details[:street]
      postal_address.encoded_tag! :BuildingNumber, details[:building_number]
      postal_address.encoded_tag! :CityName, details[:city]
      postal_address.encoded_tag! :PostalZone, details[:postal_code]
      postal_address.encoded_tag! :Country do |country|
        country.encoded_tag! :IdentificationCode, details[:country_code]
        country.encoded_tag! :Name, details[:country]
      end
    end
    party.encoded_tag! :PartyTaxScheme do |party_tax_scheme|
      party_tax_scheme.encoded_tag! :CompanyID, details[:tax_number]
      party_tax_scheme.encoded_tag! :TaxScheme, "VAT"
    end if details[:tax_number]
    party.encoded_tag! :RegisterIdentification do |register_identification|
      register_identification.encoded_tag! :RegisterKeptAt, details[:register_kept_at]
      register_identification.encoded_tag! :RegisterFileRef, details[:register_file_ref]
      register_identification.encoded_tag! :RegisterDate, details[:register_date]
    end if details[:register_kept_at]
  end
end

#build_tax_sub_totals(tax_total, tax_sub_totals) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/isdoc_output_builder.rb', line 160

def build_tax_sub_totals(tax_total, tax_sub_totals)
  for tax_sub_total in tax_sub_totals
    tax_total.encoded_tag! :TaxSubTotal do |tax_sub_total_tag|
      tax_sub_total_tag.encoded_tag! :TaxableAmount, tax_sub_total[:taxable_amount]
      tax_sub_total_tag.encoded_tag! :TaxInclusiveAmount, tax_sub_total[:tax_inclusive_amount]
      tax_sub_total_tag.encoded_tag! :TaxAmount, tax_sub_total[:tax_amount]
      tax_sub_total_tag.encoded_tag! :AlreadyClaimedTaxableAmount, tax_sub_total[:already_claimed_taxable_amount]
      tax_sub_total_tag.encoded_tag! :AlreadyClaimedTaxAmount, tax_sub_total[:already_claimed_tax_amount]
      tax_sub_total_tag.encoded_tag! :AlreadyClaimedTaxInclusiveAmount, tax_sub_total[:already_claimed_tax_inclusive_amount]
      tax_sub_total_tag.encoded_tag! :DifferenceTaxableAmount, tax_sub_total[:difference_taxable_amount]
      tax_sub_total_tag.encoded_tag! :DifferenceTaxAmount, tax_sub_total[:difference_tax_amount]
      tax_sub_total_tag.encoded_tag! :DifferenceTaxInclusiveAmount, tax_sub_total[:difference_tax_inclusive_amount]
      tax_sub_total_tag.encoded_tag! :TaxCategory do |tax_category|
        tax_category.encoded_tag! :Percent, tax_sub_total[:tax_percent]
      end
    end
  end
end

#build_taxed_deposits(xml, details) ⇒ Object



199
200
201
202
203
204
205
206
207
208
209
210
211
212
# File 'lib/isdoc_output_builder.rb', line 199

def build_taxed_deposits(xml, details)
  for detail in details
    xml.encoded_tag! :TaxedDeposit do |taxed_deposit|
      taxed_deposit.encoded_tag! :ID, detail[:id]
      taxed_deposit.encoded_tag! :VariableSymbol, detail[:variable_symbol]
      taxed_deposit.encoded_tag! :TaxableDepositAmount, detail[:taxable_deposit_amount]
      taxed_deposit.encoded_tag! :TaxInclusiveDepositAmount, detail[:tax_inclusive_deposit_amount]
      taxed_deposit.encoded_tag! :ClassifiedTaxCategory do |classified_tax_category|
        classified_tax_category.encoded_tag! :Percent, detail[:tax_percent]
        classified_tax_category.encoded_tag! :VATCalculationMethod, detail[:vat_calculation_method]
      end
    end
  end
end