Class: Ekylibre::VariantsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/variants_exchanger.rb

Overview

Expected file is a OpenDocument spreadsheet. Columns are A: Variant name in DB B: reference_name (from ProductNatureVariant or ProductNature if not found) C: Variant code in DB D: Variety CF NOMENCLATURE E: Derivative CF NOMENCLATURE F: Purchase pretax amount price G: Stock pretax amount price H: Sale pretax amount price I: Price unity J: Indicators - HASH

Instance Attribute Summary

Attributes inherited from ActiveExchanger::Base

#file, #supervisor

Instance Method Summary collapse

Methods inherited from ActiveExchanger::Base

build, check, check_by_default, exchanger_name, export, exporters, find, human_name, import, import!, importers, importers_selection, inherited, #initialize, register_exchanger

Constructor Details

This class inherits a constructor from ActiveExchanger::Base

Instance Method Details

#importObject

Create or updates variants


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
102
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
135
136
137
138
139
140
141
142
143
144
145
146
# File 'app/exchangers/ekylibre/variants_exchanger.rb', line 17

def import
  currency = Preference[:currency] || 'EUR'

  s = Roo::OpenOffice.new(file)
  w.count = s.sheets.count

  s.sheets.each do |sheet_name|
    s.sheet(sheet_name)
    # first line are headers
    2.upto(s.last_row) do |row|
      next if s.cell('A', row).blank?
      r = {
        name: s.cell('A', row).blank? ? nil : s.cell('A', row).to_s.strip,
        reference_name: s.cell('B', row).blank? ? nil : s.cell('B', row).downcase.to_sym,
        work_number: s.cell('C', row).blank? ? nil : s.cell('C', row).to_s.strip,
        variety: s.cell('D', row).blank? ? nil : s.cell('D', row).to_s.strip,
        derivative_of: s.cell('E', row).blank? ? nil : s.cell('E', row).to_s.strip,
        purchase_unit_pretax_amount: s.cell('F', row).blank? ? nil : s.cell('F', row).to_d,
        stock_unit_pretax_amount: s.cell('G', row).blank? ? nil : s.cell('G', row).to_d,
        sale_unit_pretax_amount: s.cell('H', row).blank? ? nil : s.cell('H', row).to_d,
        price_unity: s.cell('I', row).blank? ? nil : s.cell('I', row).to_s.strip.split(/[\,\.\/\\\(\)]/),
        indicators: s.cell('J', row).blank? ? {} : s.cell('J', row).to_s.strip.split(/[[:space:]]*\;[[:space:]]*/).collect { |i| i.split(/[[:space:]]*\:[[:space:]]*/) }.inject({}) do |h, i|
          h[i.first.strip.downcase.to_sym] = i.second
          h
        end
      }.to_struct

      unless r.reference_name
        w.warn "Need a reference to build variant for #{r.name}"
        next
      end
      # force import variant from reference_nomenclature and update his attributes.
      if r.reference_name.to_s.start_with? '>'
        reference_name = r.reference_name[1..-1]
        if Nomen::ProductNature.find(reference_name)
          nature = ProductNature.import_from_nomenclature(reference_name)
          variant = nature.variants.new(name: r.name, active: true)
        elsif (nature = ProductNature.find_by(number: reference_name))
          variant = nature.variants.new(name: r.name, active: true)
        end
      elsif Nomen::ProductNatureVariant.find(r.reference_name)
        variant = ProductNatureVariant.import_from_nomenclature(r.reference_name, active: true)
      elsif Nomen::ProductNature.find(r.reference_name)
        nature = ProductNature.import_from_nomenclature(r.reference_name, true)
        variant = nature.variants.new(name: nature.name, active: true)
      else
        raise 'Invalid reference name: ' + r.reference_name.inspect
      end
      # update variant with attributes in the current row
      variant.name = r.name if r.name
      variant.number = r.work_number if r.work_number
      variant.unit_name ||= :unit.tl
      variant.save!

      if r.indicators.any?
        r.indicators.each do |indicator_name, value|
          variant.read! indicator_name, value
        end
      end

      if r.price_unity
        # Find unit and matching indicator

        default_indicators = {
          mass: :net_mass,
          volume: :net_volume
        }.with_indifferent_access

        unit = r.price_unity.first

        if unit.present? && !Nomen::Unit[unit]
          if u = Nomen::Unit.find_by(symbol: unit)
            unit = u.name.to_s
            measure_unit_price = 1.00.in(unit.to_sym) if unit
          else
            raise ActiveExchanger::NotWellFormedFileError, "Unknown unit #{unit.inspect} for variant #{variant.name.inspect}."
          end
        end

        unless indicator = (unit.blank? ? :population : r.price_unity.second)
          dimension = Measure.dimension(unit)
          indics = variant.indicators.select do |indicator|
            next unless indicator.datatype == :measure
            Measure.dimension(indicator.unit) == dimension
          end.map(&:name)
          if indics.count > 1
            if indics.include?(default_indicators[dimension].to_s)
              indicator = default_indicators[dimension]
            else
              raise ActiveExchanger::NotWellFormedFileError, "Ambiguity on unit #{unit.inspect} for variant #{variant.name.inspect} between #{indics.to_sentence(locale: :eng)}. Cannot known what is wanted, insert indicator name after unit like: '#{unit} (#{indics.first})'."
            end
          elsif indics.empty?
            if unit == 'hour'
              indicator = 'working_duration'
            else
              raise ActiveExchanger::NotWellFormedFileError, "Unit #{unit.inspect} is invalid for variant #{variant.name.inspect}. No indicator can be used with this unit."
            end
          else
            indicator = indics.first
          end
        end
        # Find ratio to store the good price link to existing variant indicator
        variant_default_population = variant.send(indicator.to_sym)
        ratio = (variant_default_population.to_d(unit.to_sym) / measure_unit_price.to_d(unit.to_sym)).to_d
      else
        ratio = 1.0
      end

      # create a purchase price if needed
      if r.purchase_unit_pretax_amount
        catalog = Catalog.by_default!(:purchase)
        variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: (r.purchase_unit_pretax_amount * ratio), currency: currency)
      end
      # create a stock price if needed
      if r.stock_unit_pretax_amount
        catalog = Catalog.by_default!(:stock)
        if variant.catalog_items.where(catalog: catalog).empty?
          variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.stock_unit_pretax_amount * ratio, currency: currency)
        end
      end
      # create a sale price if needed
      next unless r.sale_unit_pretax_amount
      catalog = Catalog.by_default!(:sale)
      if variant.catalog_items.where(catalog: catalog).empty?
        variant.catalog_items.create!(catalog: catalog, all_taxes_included: false, amount: r.sale_unit_pretax_amount * ratio, currency: currency)
      end
    end
    w.check_point
  end
end