Class: Viniteca::InterventionsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/viniteca/interventions_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, deprecated?, exchanger_name, export, exporters, find, find_and_import, find_by, 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


3
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
# File 'app/exchangers/viniteca/interventions_exchanger.rb', line 3

def import
  # Unzip file
  dir = w.tmp_dir
  Zip::File.open(file) do |zile|
    zile.each do |entry|
      entry.extract(dir.join(entry.name))
    end
  end

  # load interventions from isaculture

  procedures_transcode = {}.with_indifferent_access
  path = dir.join('procedures_transcode.csv')
  if path.exist?
    CSV.foreach(path, headers: true) do |row|
      procedures_transcode[row[0]] = row[1].to_sym
    end
  end

  variants_transcode = {}.with_indifferent_access
  path = dir.join('variants_transcode.csv')
  if path.exist?
    CSV.foreach(path, headers: true) do |row|
      variants_transcode[row[0]] = row[1].to_sym
    end
  end

  issue_natures_transcode = {}.with_indifferent_access
  path = dir.join('issue_natures_transcode.csv')
  if path.exist?
    CSV.foreach(path, headers: true) do |row|
      issue_natures_transcode[row[0]] = row[1].to_sym
    end
  end

  path = dir.join('interventions.csv')
  if path.exist?

    w.count = path.size

    information_import_context = "Import from viniteca on #{Time.zone.now.l}"

    CSV.foreach(path, headers: true, col_sep: ';') do |row|
      r = OpenStruct.new(
        cultivable_zone_name: row[1].gsub('-h', '').downcase,
        working_area: row[2].tr(',', '.').to_d,
        cultivable_zone_area: row[3].tr(',', '.').to_d,
        intervention_started_at: (row[4].blank? ? nil : DateTime.strptime(row[4].to_s, '%d/%m/%Y %H:%M')),

        intervention_stopped_at: (row[5].blank? ? nil : DateTime.strptime(row[5].to_s, '%d/%m/%Y %H:%M')),
        procedure_name: row[6].to_s.downcase, # to transcode
        product_name: row[7].to_s.downcase, # to create
        product_input_population: row[8].tr(',', '.').to_d,
        product_input_population_per_hectare: row[9].tr(',', '.').to_d,
        product_input_dose_per_hectare: (row[10].blank? ? nil : row[10].tr(',', '.').to_d), # for legal quantity ?
        issue_description: (row[11].blank? ? nil : row[11].to_s), # to transcode
        dar: (row[12].blank? ? nil : row[12].to_i), # indicator on product for delay_before_harvest in day
        product_input_approved_dose_per_hectare: (row[13].blank? ? nil : row[13].tr(',', '.').to_d) # legal quantity in liter per hectare
      )

      intervention_started_at = r.intervention_started_at + 9.hours
      intervention_year = intervention_started_at.year
      intervention_month = intervention_started_at.month
      intervention_day = intervention_started_at.day
      intervention_stopped_at = r.intervention_stopped_at + 11.hours

      campaign = Campaign.find_by(harvest_year: intervention_started_at.year)
      campaign ||= Campaign.create!(name: intervention_started_at.year, harvest_year: intervention_started_at.year)
      plant = Plant.find_by(identification_number: r.cultivable_zone_name)

      w.info "----------- #{w.count} -----------".blue
      w.info ' procedure : ' + procedures_transcode[r.procedure_name].inspect.green
      w.info ' variant : ' + variants_transcode[r.product_name].inspect.red
      w.info ' plant : ' + plant.inspect.red

      if plant && campaign
        cultivable_zone = plant.container
        if support = ActivityProduction.where(storage: cultivable_zone).of_campaign(campaign).first
          Ekylibre::FirstRun::Booker.production = support.production
          coeff = (r.working_area / 10_000.0) / 6.0

          #
          # create product
          #

          if r.product_name && (procedures_transcode[r.procedure_name] == :mineral_fertilizing || procedures_transcode[r.procedure_name] == :organic_fertilizing)

            variant = ProductNatureVariant.import_from_nomenclature(variants_transcode[r.product_name])

            intrant = variant.generate(r.product_name, r.intervention_started_at, plant.container)

            unless intrant.frozen_indicators_list.include?(:population)
              intrant.read!(:population, r.product_input_population, at: r.intervention_started_at)
            end

          elsif r.product_name && procedures_transcode[r.procedure_name] == :chemical_weed_killing

            variant = ProductNatureVariant.import_from_nomenclature(:herbicide)

            intrant = variant.generate(r.product_name, r.intervention_started_at, plant.container)
            unless intrant.frozen_indicators_list.include?(:population)
              intrant.read!(:population, r.product_input_population, at: r.intervention_started_at)
            end
            intrant.read!(:wait_before_harvest_period, r.dar.in_day, at: r.intervention_started_at) if r.dar
            intrant.read!(:approved_input_dose, r.product_input_approved_dose_per_hectare.in_kilogram_per_hectare, at: r.intervention_started_at) if r.product_input_approved_dose_per_hectare

          elsif r.product_name && procedures_transcode[r.procedure_name] == :spraying_on_cultivation

            variant = ProductNatureVariant.import_from_nomenclature(:fungicide)

            intrant = variant.generate(r.product_name, r.intervention_started_at, plant.container)

            unless intrant.frozen_indicators_list.include?(:population)
              intrant.read!(:population, r.product_input_population, at: r.intervention_started_at)
            end
            intrant.read!(:wait_before_harvest_period, r.dar.in_day, at: r.intervention_started_at) if r.dar
            intrant.read!(:approved_input_dose, r.product_input_approved_dose_per_hectare.in_kilogram_per_hectare, at: r.intervention_started_at) if r.product_input_approved_dose_per_hectare

          else

            variant = ProductNatureVariant.import_from_nomenclature(:fungicide)

            intrant = variant.generate(r.product_name, r.intervention_started_at, plant.container)
            unless intrant.frozen_indicators_list.include?(:population)
              intrant.read!(:population, r.product_input_population, at: intervention_started_at)
            end
            intrant.read!(:wait_before_harvest_period, r.dar.in_day, at: intervention_started_at) if r.dar
            intrant.read!(:approved_input_dose, r.product_input_approved_dose_per_hectare.in_kilogram_per_hectare, at: intervention_started_at) if r.product_input_approved_dose_per_hectare

          end

          if procedures_transcode[r.procedure_name] && intrant
            #
            # create intervention
            #
            if procedures_transcode[r.procedure_name] == :mineral_fertilizing
              # Mineral fertilizing
              intervention = Ekylibre::FirstRun::Booker.intervene(:mineral_fertilizing, intervention_year, intervention_month, intervention_day, 0.96 * coeff, support: support) do |i|
                i.add_cast(reference_name: 'fertilizer', actor: intrant)
                i.add_cast(reference_name: 'fertilizer_to_spread', population: r.product_input_population)
                i.add_cast(reference_name: 'spreader',    actor: i.find(Product, can: 'spread(preparation)'))
                i.add_cast(reference_name: 'driver',      actor: i.find(Worker))
                i.add_cast(reference_name: 'tractor',     actor: i.find(Product, can: 'tow(spreader)'))
                i.add_cast(reference_name: 'land_parcel', actor: cultivable_zone)
              end

            elsif procedures_transcode[r.procedure_name] == :organic_fertilizing

              # Organic fertilizing
              intervention = Ekylibre::FirstRun::Booker.intervene(:organic_fertilizing, intervention_year, intervention_month, intervention_day, 0.96 * coeff, support: support) do |i|
                i.add_cast(reference_name: 'manure',      actor: intrant)
                i.add_cast(reference_name: 'manure_to_spread', population: r.product_input_population)
                i.add_cast(reference_name: 'spreader',    actor: i.find(Product, can: 'spread(preparation)'))
                i.add_cast(reference_name: 'driver',      actor: i.find(Worker))
                i.add_cast(reference_name: 'tractor',     actor: i.find(Product, can: 'tow(spreader)'))
                i.add_cast(reference_name: 'land_parcel', actor: cultivable_zone)
              end

            elsif procedures_transcode[r.procedure_name] == :chemical_weed_killing

              # Chemical weed
              intervention = Ekylibre::FirstRun::Booker.intervene(:chemical_weed_killing, intervention_year, intervention_month, intervention_day, 1.07 * coeff, support: support, parameters: { readings: { 'base-chemical_weed_killing-0-1-readstate' => 'covered' } }) do |i|
                i.add_cast(reference_name: 'weedkiller', actor: intrant)
                i.add_cast(reference_name: 'weedkiller_to_spray', population: r.product_input_population)
                i.add_cast(reference_name: 'sprayer', actor: i.find(Product, can: 'spray'))
                i.add_cast(reference_name: 'driver',      actor: i.find(Worker))
                i.add_cast(reference_name: 'tractor',     actor: i.find(Product, can: 'catch'))
                i.add_cast(reference_name: 'land_parcel', actor: cultivable_zone)
              end

            elsif procedures_transcode[r.procedure_name] == :spraying_on_cultivation

              # Spraying on cultivation
              intervention = Ekylibre::FirstRun::Booker.intervene(:spraying_on_cultivation, intervention_year, intervention_month, intervention_day, 1.07 * coeff, support: support) do |i|
                i.add_cast(reference_name: 'plant_medicine', actor: intrant)
                i.add_cast(reference_name: 'plant_medicine_to_spray', population: r.product_input_population)
                i.add_cast(reference_name: 'sprayer',  actor: i.find(Product, can: 'spray'))
                i.add_cast(reference_name: 'driver',   actor: i.find(Worker))
                i.add_cast(reference_name: 'tractor',  actor: i.find(Product, can: 'catch'))
                i.add_cast(reference_name: 'cultivation', actor: plant)
              end

            else

              w.info 'Intervention is in a black hole'.red

            end

            if intervention
              intervention.description = information_import_context + ' - ' + row[4].to_s + ' - operation : ' + row[6].to_s + ' - support : ' + row[1].to_s + ' - intrant : ' + row[7].to_s
              intervention.save!
              w.info "Intervention n°#{intervention.id} - #{intervention.name} has been created".green
            end

          end

          # create an issue if mentionned
          if r.issue_description && nature = issue_natures_transcode[r.issue_description.downcase]
            issue = Issue.create!(target_type: plant.class.name, target_id: plant.id, priority: 3, observed_at: intervention_started_at, description: r.issue_description, nature: nature, state: 'closed')
            if intervention && issue
              intervention.issue = issue
              intervention.save!
            end
          end

        end
        w.check_point

      end
    end
  end
end