Class: Ekylibre::FirstRun::Faker::Sales

Inherits:
Base
  • Object
show all
Defined in:
lib/ekylibre/first_run/faker/sales.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#count, #files, #initialize, #path, run

Constructor Details

This class inherits a constructor from Ekylibre::FirstRun::Faker::Base

Class Method Details

.find_or_create_catalog_item(catalog, variant, amount, _d, options = {}) ⇒ Object


44
45
46
47
48
49
50
51
52
53
# File 'lib/ekylibre/first_run/faker/sales.rb', line 44

def self.find_or_create_catalog_item(catalog, variant, amount, _d, options = {})
  options[:currency] ||= Preference[:currency]
  options[:amount]  = amount
  options[:variant] = variant
  options[:catalog] = catalog
  unless item = catalog.items.where(variant_id: variant.id).first
    item = catalog.items.create!(options)
  end
  item
end

.generate_sale_cycle(sale, d, options = {}) ⇒ Object

Generate all


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
# File 'lib/ekylibre/first_run/faker/sales.rb', line 4

def self.generate_sale_cycle(sale, d, options = {})
  factor = options[:factor] || 1
  Sale.where(id: sale.id).update_all(created_at: d)
  return if rand(factor * 20).zero?
  # Propose
  Timecop.travel(d) do
    sale.propose
  end
  # Abort
  if rand(factor * 9).zero?
    d += rand(7).days
    Timecop.travel(d) do
      sale.abort
    end
    return
  end
  # Confirmation
  return if rand(factor * 8).zero?
  d += rand(15).days
  Timecop.travel(d) do
    sale.confirm(d)
    # Sale.where(id: sale.id).update_all(confirmed_at: d)
  end
  # Invoice
  return if rand(factor * 25).zero?
  d += rand(5).days
  Timecop.travel(d) do
    sale.invoice
    # Sale.where(id: sale.id).update_all(invoiced_at: d, payment_at: (d + 30.days))
  end
  # Payment
  return if rand(factor * 4).zero? || sale.amount <= 0
  d += rand(30).days
  Timecop.travel(d) do
    payment = sale.client.incoming_payments.create!(mode: IncomingPaymentMode.all.sample, amount: (sale.amount / (1.0 + rand(3))).to_s.to_f.round(2), to_bank_at: d)
    sale.affair.attach(payment)
  end
  true
end

Instance Method Details

#runObject


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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# File 'lib/ekylibre/first_run/faker/sales.rb', line 55

def run
  count :variant_import do |w|
    # Create product_nature for crop plant product
    wheat_crop  = ProductNatureVariant.import_from_nomenclature(:wheat_crop)
    barley_crop = ProductNatureVariant.import_from_nomenclature(:winter_barley_crop)
    corn_crop   = ProductNatureVariant.import_from_nomenclature(:corn_crop)
    durum_wheat_crop = ProductNatureVariant.import_from_nomenclature(:hard_wheat_crop)
    fallow_crop = ProductNatureVariant.import_from_nomenclature(:fallow_crop)
    sunflower_crop = ProductNatureVariant.import_from_nomenclature(:sunflower_crop)
    sorghum_crop   = ProductNatureVariant.import_from_nomenclature(:sorghum_crop)
    temporary_meadow_crop = ProductNatureVariant.import_from_nomenclature(:temporary_meadow_crop)

    # Create product_nature for derivative plant product
    grain  = ProductNatureVariant.import_from_nomenclature(:wheat_grain)
    straw  = ProductNatureVariant.import_from_nomenclature(:bulk_wheat_straw)
    hay    = ProductNatureVariant.import_from_nomenclature(:bulk_hay)
    silage = ProductNatureVariant.import_from_nomenclature(:grass_silage)
    grass  = ProductNatureVariant.import_from_nomenclature(:grass)

    # Create product_nature_price for wheat product
    # wheat_price_template   = ProductPriceTemplate.find_by_product_nature_id(wheat.id)
    # wheat_price_template ||= ProductPriceTemplate.create!(:assignment_amount => 211, :currency => Preference[:currency], :assignment_pretax_amount => 200, :product_nature_id => wheat.id, :tax_id => wheat_price_template_tax.id, :listing_id => price_listing.id, :supplier_id => Entity.of_company.id )
    w.check_point
  end

  count :wheat_sales do |w|
    unless cooperative = Entity.where('LOWER(full_name) LIKE ?', '%Kazeni%'.mb_chars.downcase).first
      cooperative = Entity.create!(last_name: 'Kazeni',
                                   nature: :organization,
                                   vat_number: 'FR00123456789',
                                   supplier: true, client: true,
                                   mails_attributes: {
                                     0 => {
                                       canal: 'mail',
                                       mail_line_4: '145 rue du port',
                                       mail_line_6: '17000 LAROCHELLE',
                                       mail_country: :fr
                                     }
                                   },
                                   emails_attributes: {
                                     0 => {
                                       canal: 'email',
                                       coordinate: '[email protected]'
                                     }
                                   })
    end

    # Create wheat product
    wheat = ProductNatureVariant.import_from_nomenclature(:wheat_grain)
    catalog = Catalog.first
    wheat_taxes = Tax.all

    responsibles = Entity.contacts.where(id: User.pluck(:person_id))

    # Sale nature
    sale_nature   = SaleNature.actives.first
    sale_nature ||= SaleNature.create!(name: I18n.t('models.sale_nature.default.name'), currency: Preference[:currency], active: true)
    (140 + rand(20)).times do |i|
      # Sale
      d = Time.zone.now - (7 * i - rand(4)).days
      sale = Sale.create!(created_at: d, client_id: cooperative.id, nature_id: sale_nature.id, responsible: responsibles.sample)
      # Sale items
      (rand(5) + 1).times do
        catalog_item = self.class.find_or_create_catalog_item(catalog, wheat, rand(60) + 180, d, reference_tax: wheat_taxes.sample)
        sale.items.create!(quantity: rand(12.5) + 0.5, tax: wheat_taxes.sample, unit_pretax_amount: catalog_item.amount, variant: catalog_item.variant)
      end
      self.class.generate_sale_cycle(sale, d)
      w.check_point
    end
  end

  count :calf_sales do |w|
    unless cooperative = Entity.where('LOWER(full_name) LIKE ?', '%Caroli%'.mb_chars.downcase).first
      cooperative = Entity.create!(last_name: 'Caroli',
                                   nature: :organization,
                                   vat_number: 'FR00123456789',
                                   supplier: true, client: true,
                                   mails_attributes: {
                                     0 => {
                                       canal: 'mail',
                                       mail_line_4: '145 rue du port',
                                       mail_line_6: '16000 ANGOULEME',
                                       mail_country: :fr
                                     }
                                   },
                                   emails_attributes: {
                                     0 => {
                                       canal: 'email',
                                       coordinate: '[email protected]'
                                     }
                                   })
    end

    # Create cow product
    cow = ProductNatureVariant.find_by(reference_name: 'calf')
    cow ||= ProductNatureVariant.import_from_nomenclature(:calf)
    catalog = Catalog.first
    cow_catalog_item_template_taxes = Tax.all

    # Sale nature
    sale_nature   = SaleNature.actives.first
    sale_nature ||= SaleNature.create!(name: I18n.t('models.sale_nature.default.name'), currency: Preference[:currency], active: true)
    (140 + rand(20)).times do |i|
      # Sale
      d = Time.zone.now - (7 * i - rand(4)).days
      sale = Sale.create!(created_at: d, client_id: cooperative.id, nature_id: sale_nature.id)
      # Sale items
      (rand(5) + 1).times do
        catalog_item = self.class.find_or_create_catalog_item(catalog, cow, rand(40) + 140, d, reference_tax: cow_catalog_item_template_taxes.sample)
        sale.items.create!(quantity: rand(4) + 1, unit_pretax_amount: catalog_item.amount, variant: catalog_item.variant, tax: cow_catalog_item_template_taxes.sample)
      end
      self.class.generate_sale_cycle(sale, d)
      w.check_point
    end
  end

  count :milk_sales do |w|
    unless cooperative = Entity.where('LOWER(full_name) LIKE ?', '%TerriLacti%'.mb_chars.downcase).first
      cooperative = Entity.create!(last_name: 'TerriLacti',
                                   nature: :organization,
                                   vat_number: 'FR00123456789',
                                   supplier: true, client: true,
                                   mails_attributes: {
                                     0 => {
                                       canal: 'mail',
                                       mail_line_4: '145 rue du port',
                                       mail_line_6: '17000 SURGERES',
                                       mail_country: :fr
                                     }
                                   },
                                   emails_attributes: {
                                     0 => {
                                       canal: 'email',
                                       coordinate: '[email protected]'
                                     }
                                   })
    end
    # Create milk product
    milk = ProductNatureVariant.import_from_nomenclature(:cow_milk)
    catalog = Catalog.find_by(usage: 'sale')
    milk_catalog_item_template_tax = Tax.find_by(reference_name: 'french_vat_reduced')

    sale_nature   = SaleNature.actives.first
    sale_nature ||= SaleNature.create!(name: I18n.t('models.sale_nature.default.name'), currency: Preference[:currency], active: true)
    120.times do |i|
      # Sale
      d = Time.zone.now - i.months
      sale = Sale.create!(created_at: d, client: cooperative, nature: sale_nature)
      # Sale items
      catalog_item = self.class.find_or_create_catalog_item(catalog, milk, rand(0.04) + 0.300, d, reference_tax: milk_catalog_item_template_tax)
      sale.items.create!(quantity: rand(5000) + 30_000, unit_pretax_amount: catalog_item.amount, variant: catalog_item.variant, tax: milk_catalog_item_template_tax)
      self.class.generate_sale_cycle(sale, d)
      w.check_point
    end
  end

  count :bottle_wine_sales do |w|
    unless cooperative = Entity.where('LOWER(full_name) LIKE ?', '%Vitis%'.mb_chars.downcase).first
      cooperative = Entity.create!(last_name: 'Vitis',
                                   nature: :organization,
                                   vat_number: 'FR00123456789',
                                   supplier: true, client: true,
                                   mails_attributes: {
                                     0 => {
                                       canal: 'mail',
                                       mail_line_4: '145 rue du port',
                                       mail_line_6: '17300 JONZAC',
                                       mail_country: :fr
                                     }
                                   },
                                   emails_attributes: {
                                     0 => {
                                       canal: 'email',
                                       coordinate: '[email protected]'
                                     }
                                   })
    end

    # Create wheat product
    wine = ProductNatureVariant.import_from_nomenclature(:bottle_75cl_wine)
    catalog = Catalog.first
    wine_taxes = Tax.all

    responsibles = Entity.contacts.where(id: User.pluck(:person_id))

    # Sale nature
    sale_nature   = SaleNature.actives.first
    sale_nature ||= SaleNature.create!(name: I18n.t('models.sale_nature.default.name'), currency: Preference[:currency], active: true)
    (140 + rand(20)).times do |i|
      # Sale
      d = Time.zone.now - (7 * i - rand(4)).days
      sale = Sale.create!(created_at: d, client_id: cooperative.id, nature_id: sale_nature.id, responsible: responsibles.sample)
      # Sale items
      (rand(5) + 1).times do
        catalog_item = self.class.find_or_create_catalog_item(catalog, wine, rand(2.8) + 8, d, reference_tax: wine_taxes.sample)
        sale.items.create!(quantity: rand(120) + 60, tax: wine_taxes.sample, unit_pretax_amount: catalog_item.amount, variant: catalog_item.variant)
      end
      self.class.generate_sale_cycle(sale, d)
      w.check_point
    end
  end

  count :bulk_wine_sales do |w|
    unless cooperative = Entity.where('LOWER(full_name) LIKE ?', '%Vitis%'.mb_chars.downcase).first
      cooperative = Entity.create!(last_name: 'Vitis',
                                   nature: :organization,
                                   vat_number: 'FR00123456789',
                                   supplier: true, client: true,
                                   mails_attributes: {
                                     0 => {
                                       canal: 'mail',
                                       mail_line_4: '145 rue du port',
                                       mail_line_6: '17300 JONZAC',
                                       mail_country: :fr
                                     }
                                   },
                                   emails_attributes: {
                                     0 => {
                                       canal: 'email',
                                       coordinate: '[email protected]'
                                     }
                                   })
    end

    # Create wheat product
    wine = ProductNatureVariant.import_from_nomenclature(:wine)
    catalog = Catalog.first
    wine_taxes = Tax.all

    responsibles = Entity.contacts.where(id: User.pluck(:person_id))

    # Sale nature
    sale_nature   = SaleNature.actives.first
    sale_nature ||= SaleNature.create!(name: I18n.t('models.sale_nature.default.name'), currency: Preference[:currency], active: true)
    (2 + rand(2)).times do |i|
      # Sale
      d = Time.zone.now - (7 * i - rand(4)).days
      sale = Sale.create!(created_at: d, client_id: cooperative.id, nature_id: sale_nature.id, responsible: responsibles.sample)
      # Sale items
      (rand(5) + 1).times do
        # # find or create a catalog_item
        # # @FIXME = waiting for a working method in ProductCatalog_Item.catalog_item
        # catalog_item = ble.catalog_item(:amount => rand(150)+25, :tax => wheat_tax)
        catalog_item = catalog.items.find_by(variant_id: wine.id)
        catalog_item ||= catalog.items.create!(currency: Preference[:currency],
                                               amount: rand(130) + 850,
                                               reference_tax: wine_taxes.sample,
                                               variant_id: wine.id)

        sale.items.create!(quantity: rand(25) + 10,
                           tax: wine_taxes.sample, unit_pretax_amount: catalog_item.amount, variant: catalog_item.variant)
      end
      self.class.generate_sale_cycle(sale, d)
      w.check_point
    end
  end
end