Class: Ekylibre::SettingsExchanger

Inherits:
ActiveExchanger::Base show all
Defined in:
app/exchangers/ekylibre/settings_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

Create or updates main settings of folder


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
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
# File 'app/exchangers/ekylibre/settings_exchanger.rb', line 4

def import
  @manifest = YAML.load_file(file) || {}
  @manifest.deep_symbolize_keys!

  @manifest[:company] ||= {}
  @manifest[:net_services] ||= {}
  @manifest[:identifiers] ||= {}
  @manifest[:language] ||= ::I18n.default_locale

  # Manual count of check points
  # $ grep -rin check_point app/exchangers/ekylibre/settings_exchanger.rb | wc -l
  w.count = 22 - 1

  # Global preferences
  language = ::I18n.locale = @manifest[:language]
  currency = @manifest[:currency] || 'EUR'
  country  = @manifest[:country] || 'fr'
  sales_conditions = @manifest[:sales_conditions] || ''
  Preference.set!(:language, language)
  Preference.set!(:currency, currency)
  Preference.set!(:country, country)
  Preference.set!(:sales_conditions, sales_conditions)
  if srs = @manifest[:map_measure_srs]
    Preference.set!(:map_measure_srs, srs)
  elsif srid = @manifest[:map_measure_srid]
    Preference.set!(:map_measure_srs, Nomen::SpatialReferenceSystem.find_by(srid: srid.to_i).name)
  end
  demo = !!@manifest[:demo]
  if demo
    Preference.set!(:demo, demo, :boolean)
    demo_user = @manifest[:users].keys.first
    Preference.set!(:demo_user, demo_user)
    Preference.set!(:demo_password, @manifest[:users][demo_user][:password])
  end
  Preference.set!(:create_activities_from_telepac, !!@manifest[:create_activities_from_telepac], :boolean)
  ::I18n.locale = Preference[:language]

  w.check_point

  # Sequences
  Sequence.load_defaults if can_load?(:sequences)
  w.check_point

  # Company entity
  attributes = {
    language: language,
    currency: currency,
    nature: :organization,
    last_name: 'Ekylibre',
    born_at: (@manifest[:financial_years] || {}).map { |_, v| v[:started_on] }.min
  }.merge(@manifest[:company].reject { |k, _v| [:addresses].include?(k) }).merge(of_company: true)
  # resolte siret to siret_number transcode
  siren_number = attributes.delete(:siren_number)
  siren_number ||= attributes.delete(:siren)
  attributes[:siret_number] ||= attributes.delete(:siret)
  if attributes[:siret_number].blank? && siren_number.present?
    attributes[:siret_number] = siren_number.to_s + '0001'
    attributes[:siret_number] << Luhn.control_digit(attributes[:siret_number]).to_s
  end
  company = Entity.create!(attributes)
  # f.close if f
  if @manifest[:company][:addresses].is_a?(Hash)
    @manifest[:company][:addresses].each do |address, value|
      if value.is_a?(Hash)
        value[:canal] ||= address
        (1..6).to_a.each do |index|
          value["mail_line_#{index}"] = value.delete("line_#{index}".to_sym)
        end
        company.addresses.create!(value)
      else
        company.addresses.create!(canal: address, coordinate: value)
      end
    end
  end
  w.check_point

  # Teams
  if can_load_default?(:teams)
    @manifest[:teams] = { default: { name: Team.tc('default') } }
  end
  create_records(:teams)
  w.check_point

  # Roles
  if can_load_default?(:roles)
    @manifest[:roles] = {
      default: { name: Role.tc('default.public') },
      administrator: { name: Role.tc('default.administrator'), rights: Ekylibre::Access.all_rights }
    }
  end
  create_records(:roles)
  w.check_point

  # Users
  if can_load_default?(:users)
    @manifest[:users] = { '[email protected]' => { first_name: 'Admin', last_name: 'EKYLIBRE' } }
  end
  for email, attributes in @manifest[:users]
    attributes[:administrator] = true unless attributes.key?(:administrator)
    attributes[:language] ||= language
    for ref in %i[role team]
      attributes[ref] ||= :default
      attributes[ref] = find_record(ref.to_s.pluralize, attributes[ref])
    end
    unless attributes[:password]
      if Rails.env.development?
        attributes[:password] = '12345678'
      else
        attributes[:password] = User.give_password(8, :normal)
        unless Rails.env.test?
          w.notice "New password for account #{attributes[:email]}: #{attributes[:password]}"
        end
      end
    end
    attributes[:password_confirmation] = attributes[:password]
    user = User.find_or_initialize_by(email: email.to_s)
    user.attributes = attributes
    user.save!
  end
  w.check_point

  # Catalogs
  create_records(:catalogs, :code)
  w.check_point

  # Load accouting accounting_system
  unless accounting_system = @manifest[:accounting_system]
    if accounting_system = @manifest[:chart_of_accounts] || @manifest[:chart_of_account]
      ActiveSupport::Deprecation.warn('chart_of_accounts has been deprecated in settings. Please use accounting_system instead.')
    end
  end
  if accounting_system
    Account.accounting_system = accounting_system
    Account.load_defaults
  end
  w.check_point

  # Load accounts
  #TODO check when method is executed
  if can_load_default?(:accounts)
    @manifest[:accounts] = Cash.nature.values.each_with_object({}) do |nature, hash|
      hash[nature] = { name: "enumerize.cash.nature.#{nature}".t,
                       number: format('%08d', rand(10**7)) }
      hash
    end
  end
  create_records(:accounts)

  w.check_point

  # Load financial_years
  create_records(:financial_years, :code, unless_exist: true)
  w.check_point

  # Load taxes from nomenclatures
  Tax.load_defaults if can_load?(:taxes)
  w.check_point

  # Load all the document templates
  DocumentTemplate.load_defaults if can_load?(:document_templates)
  w.check_point

  # Loads journals
  if can_load_default?(:journals)
    @manifest[:journals] = Journal.nature.values.each_with_object({}) do |nature, hash|
      hash[nature] = { name: "enumerize.journal.nature.#{nature}".t, nature: nature.to_s, currency: currency, closed_on: Date.new(1899, 12, 31).end_of_month }
      hash
    end
  end
  create_records(:journals, :code)
  w.check_point

  # Load cashes
  if can_load_default?(:cashes)
    @manifest[:cashes] = %i[bank_account cash_box].each_with_object({}) do |nature, hash|
      unless journal_nature = { bank_account: :bank, cash_box: :cash }[nature]
        raise StandardError, 'Need a valid journal nature to register a cash'
      end
      journal = Journal.find_by(nature: journal_nature)
       = Account.find_by(name: "enumerize.cash.nature.#{nature}".t)
      hash[nature] = { name: "enumerize.cash.nature.#{nature}".t, nature: nature.to_s,
                       main_account: , journal: journal }
      # hash[nature].merge!(iban: 'FR7611111222223333333333391') if nature == :bank_account
      hash
    end
  end
  @manifest[:cashes].each do |_n, v|
    v[:account] = find_record(:accounts, v[:account].to_s) if v[:account]
  end
  create_records(:cashes)
  w.check_point

  # Load incoming payment modes
  if can_load_default?(:incoming_payment_modes)
    @manifest[:incoming_payment_modes] = %w[cash check transfer].each_with_object({}) do |nature, hash|
      if cash = Cash.find_by(nature: Cash.nature.values.include?(nature) ? nature : :bank_account)
        hash[nature] = { name: IncomingPaymentMode.tc("default.#{nature}.name"), with_accounting: true, cash: cash, with_deposit: (nature == 'check') }
        if hash[nature][:with_deposit] && journal = Journal.find_by(nature: 'bank')
          hash[nature][:depositables_journal] = journal
          hash[nature][:depositables_account] = Account.find_or_import_from_nomenclature(:pending_deposit_payments)
        else
          hash[nature][:with_deposit] = false
        end
      end
      hash
    end
  end
  create_records(:incoming_payment_modes)
  w.check_point

  # Load outgoing payment modes
  if can_load_default?(:outgoing_payment_modes)
    @manifest[:outgoing_payment_modes] = %w[cash check transfer].each_with_object({}) do |nature, hash|
      hash[nature] = { name: OutgoingPaymentMode.tc("default.#{nature}.name"),
                       with_accounting: true,
                       cash: Cash.find_by(nature:
                                            Cash.nature.values.include?(nature) ? nature : :bank_account) }
      hash
    end
  end
  create_records(:outgoing_payment_modes)
  w.check_point

  # Load sale natures
  if can_load_default?(:sale_natures)
    nature = :sales
    usage = :sale
    journal = Journal.find_by(nature: nature, currency: currency) || Journal.create!(name: "enumerize.journal.nature.#{nature}".t, nature: nature.to_s, currency: currency, closed_on: Date.new(1899, 12, 31).end_of_month)
    catalog = Catalog.of_usage(:sale).first || Catalog.create!(name: "enumerize.catalog.usage.#{usage}".t, usage: usage, currency: currency)
    @manifest[:sale_natures] = { default: { name: SaleNature.tc('default.name'), active: true, expiration_delay: '30 day', payment_delay: '30 day', downpayment: false, downpayment_minimum: 300, downpayment_percentage: 30, currency: currency, journal: journal, catalog: catalog } }
  end
  create_records(:sale_natures)
  w.check_point

  # Load purchase natures
  if can_load_default?(:purchase_natures)
    nature = :purchases
    journal = Journal.find_by(nature: nature, currency: currency) || Journal.create!(name: "enumerize.journal.nature.#{nature}".t, nature: nature.to_s, currency: currency, closed_on: Date.new(1899, 12, 31).end_of_month)
    @manifest[:purchase_natures] = { default: { name: PurchaseNature.tc('default.name'), active: true, currency: currency, with_accounting: true, journal: journal } }
  end
  create_records(:purchase_natures)
  w.check_point

  # Load net services
  @manifest[:net_services].each do |name, identifiers|
    net_service = NetService.find_or_create_by!(reference_name: name)
    identifiers.each do |nature, value|
      identifier = net_service.identifiers.create_with(value: value).find_or_create_by!(nature: nature)
      identifier.update!(value: value)
    end
  end
  w.check_point

  # Load identifiers
  @manifest[:identifiers].each do |nature, value|
    Identifier.create!(nature: nature, value: value)
  end
  w.check_point

  MapLayer.load_defaults
  w.check_point
end