class MossGenerator::Stripe

Generate MOSS CSV string from Stripe charges

Attributes

charges[R]
period[R]
rates[R]
sale_type[R]
vat_number[R]
year[R]

Public Class Methods

call(charges, vat_number, period, year, rates, sale_type) click to toggle source
# File lib/moss_generator/stripe.rb, line 22
def self.call(charges, vat_number, period, year, rates, sale_type)
  new(charges, vat_number, period, year, rates, sale_type).call
end
new(charges, vat_number, period, year, rates, sale_type) click to toggle source
# File lib/moss_generator/stripe.rb, line 13
def initialize(charges, vat_number, period, year, rates, sale_type)
  @charges = charges
  @vat_number = vat_number
  @period = period
  @year = year
  @rates = rates
  @sale_type = sale_type
end

Public Instance Methods

call() click to toggle source
# File lib/moss_generator/stripe.rb, line 26
def call
  CSV.generate(csv_options) do |csv|
    csv << first_row
    csv << second_row
    generate_charges_rows(csv)
  end
end

Private Instance Methods

build_charges_rows() click to toggle source
# File lib/moss_generator/stripe.rb, line 60
def build_charges_rows
  group_charges_rows.map do |(country, vat), charges|
    next if country == 'SE' && turnover_country == 'SE'
    next if vat.nil?

    country_row(country, charges, vat)
  end.compact
end
column_separator() click to toggle source
# File lib/moss_generator/stripe.rb, line 86
def column_separator
  ';'
end
country_row(country, charges, vat) click to toggle source
# File lib/moss_generator/stripe.rb, line 69
def country_row(country, charges, vat)
  [turnover_country,
   country,
   format_to_two_decimals(vat),
   format_to_two_decimals(charges.sum(&:amount_without_vat)),
   format_to_two_decimals(charges.sum(&:vat_amount)),
   sale_type]
end
csv_options() click to toggle source
# File lib/moss_generator/stripe.rb, line 78
def csv_options
  { col_sep: column_separator, row_sep: row_separator }
end
first_row() click to toggle source
# File lib/moss_generator/stripe.rb, line 36
def first_row
  ['OSS_001']
end
format_to_two_decimals(number) click to toggle source
# File lib/moss_generator/stripe.rb, line 90
def format_to_two_decimals(number)
  format('%.2f', number).sub('.', ',')
end
generate_charges_rows(csv) click to toggle source
# File lib/moss_generator/stripe.rb, line 44
def generate_charges_rows(csv)
  build_charges_rows.each { |row| csv << row }
end
group_charges_rows() click to toggle source
# File lib/moss_generator/stripe.rb, line 48
def group_charges_rows
  charges_rows = charges.map do |charge|
    charge_row = MossGenerator::StripeChargeRow.new(charge, rates)
    next if charge_row.skippable?

    charge_row
  end.compact
  charges_rows.group_by do |row|
    [row.country_code, row.vat_rate]
  end
end
row_separator() click to toggle source
# File lib/moss_generator/stripe.rb, line 82
def row_separator
  ";\r\n"
end
second_row() click to toggle source
# File lib/moss_generator/stripe.rb, line 40
def second_row
  [vat_number, period, year]
end
turnover_country() click to toggle source

vat_numbers first two characters shall contain the country code

# File lib/moss_generator/stripe.rb, line 95
def turnover_country
  country_code = vat_number[0...2]
  if country_code.nil?
    raise NoTurnoverCountryError,
          "vat_number: #{vat_number}"
  end
  return country_code if country_code == 'SE'

  vat_number
end