Class: CharentesAlliance::IncomingDeliveriesExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/charentes_alliance/incoming_deliveries_exchanger.rb

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


4
5
6
7
8
9
10
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/exchangers/charentes_alliance/incoming_deliveries_exchanger.rb', line 4

def import
  here = Pathname.new(__FILE__).dirname

  catalog = Catalog.find_by_code('ACHAT') || Catalog.first
   = Account.find_or_import_from_nomenclature(:suppliers)
  # TODO: take care of no taxes present before
  Tax.load_defaults unless Tax.any?
  default_tax = Tax.first
  building_division = BuildingDivision.first
  suppliers = Entity.where(of_company: false, supplier: true).reorder(:supplier_account_id, :last_name)
  suppliers ||= Entity.create!(
    sale_catalog_id: catalog.id,
    nature: :organization,
    language: 'fra',
    last_name: 'All',
    supplier_account_id: .id,
    currency: 'EUR',
    supplier: true
  )

  variants_transcode = {}.with_indifferent_access
  CSV.foreach(here.join('variants.csv'), headers: true) do |row|
    variants_transcode[row[0]] = row[1].to_sym
  end

  cooperative = Entity.where('last_name ILIKE ?', 'CHARENTES ALLIANCE').first ||
                Entity.find_by_last_name('Charentes Alliance')

  # map sub_family to product_nature_variant XML Nomenclature

  # add Coop incoming deliveries

  # status to map
  status = {
    'Liquidé' => :order,
    'A livrer' => :estimate,
    'Supprimé' => :aborted
  }

  previous_order_number = nil
  order = nil

  rows = CSV.read(file, encoding: 'UTF-8', col_sep: ';', headers: true)
  w.count = rows.size

  rows.each do |row|
    r = OpenStruct.new(
      order_number: row[0],
      ordered_on: Date.civil(*row[1].to_s.split(/\//).reverse.map(&:to_i)),
      product_nature_name: (variants_transcode[row[3].to_s] || 'common_consumable'),
      matter_name: row[4],
      coop_variant_reference_name: 'coop:' + row[4].downcase.gsub(/[\W\_]+/, '_'),
      coop_reference_name: row[4].to_s,
      quantity: (row[5].blank? ? nil : row[5].tr(',', '.').to_d),
      product_deliver_quantity: (row[6].blank? ? nil : row[6].tr(',', '.').to_d),
      product_unit_price: (row[7].blank? ? nil : row[7].tr(',', '.').to_d),
      order_status: (status[row[8]] || :draft)
    )

    # Create delivery if order_number change and all items concerning the same order are already created.
    if previous_order_number && order && previous_order_number != r.order_number
      delivery = Delivery.create!(
        reference_number: previous_order_number,
        state: :in_preparation,
        started_at: r.ordered_on.to_time,
        stopped_at: r.ordered_on.to_time + 1
      )
      order.delivery_id = delivery.id
      order.save!
      delivery.check
      delivery.start
      delivery.finish
    end

    # create an order if not exist
    unless (order = Parcel.find_by(reference_number: r.order_number))
      order = Parcel.create!(
        nature: :incoming,
        reference_number: r.order_number,
        planned_at: r.ordered_on,
        given_at: r.ordered_on,
        state: :in_preparation,
        sender: cooperative,
        address: Entity.of_company.default_mail_address,
        delivery_mode: :third,
        storage: building_division
      )
      previous_order_number = r.order_number
    end
    # find a product_nature_variant by mapping current name of matter in coop file in coop reference_name
    unless product_nature_variant = ProductNatureVariant.find_by(number: r.coop_reference_name)
      product_nature_variant ||= if Nomen::ProductNatureVariant.find(r.coop_variant_reference_name)
                                   ProductNatureVariant.import_from_nomenclature(r.coop_variant_reference_name)
                                 else
                                   # find a product_nature_variant by mapping current sub_family of matter in coop file in Ekylibre reference_name
                                   ProductNatureVariant.import_from_nomenclature(r.product_nature_name)
                                 end
      product_nature_variant.number = r.coop_reference_name if r.coop_reference_name
      product_nature_variant.save!
    end
    # find a price from current supplier for a consider variant
    # TODO: waiting for a product price capitalization method
    catalog_item = catalog.items.find_by(variant_id: product_nature_variant.id)
    catalog_item ||= catalog.items.create!(
      currency: 'EUR',
      reference_tax_id: default_tax.id,
      amount: default_tax.amount_of(r.product_unit_price),
      variant_id: product_nature_variant.id
    )

    if r.order_status == :order
      order.items.create!(
        variant: product_nature_variant,
        product_name: r.matter_name + ' (' + r.ordered_on.to_s + ')',
        product_identification_number: r.ordered_on.to_s + '_' + r.order_number + '_' + r.matter_name,
        quantity: r.quantity
      )
    end
    w.check_point
  end
end