Class: Ekylibre::BackupExchanger

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

Defined Under Namespace

Classes: Backup

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

Raises:

  • (NotWellFormedFileError)

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

def import
  # CAUTION Fixed manually by counting points
  # grep -rin check_point app/exchangers/ekylibre/backup_exchanger.rb | wc -l
  w.count = 20 - 1

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

  w.check_point

  # Check database
  database = dir.join('backup.xml')
  raise NotWellFormedFileError unless database.exist?

  w.check_point

  #
  f = File.open(database)
  doc = Nokogiri::XML(f) do |config|
    config.strict.nonet.noblanks.noent
  end
  f.close

  w.check_point

  root = doc.root
  unless root.attr(:version).to_s == '20120806083148' # Ekylibre v0.4
    raise NotSupportedFormatError
  end
  data = Backup.load(root.children.first)

  w.check_point

  # :accounts, :cultivations, :custom_fields, :delays, :districts, :document_templates, :entity_categories, :entity_link_natures, :entity_natures, :establishments, :event_natures, :incoming_delivery_modes, :journals, :land_parcel_groups, :listings, :operation_natures, :outgoing_delivery_modes, :production_chains, :professions, :roles, :sequences, :tools, :units

  delays = data[:delay].inject({}) do |hash, item|
    hash[item.id] = item.expression
    hash
  end
  w.check_point

  # Import accounts
  accounting_system = Preference[:accounting_system]
  data.browse_and_match(:account) do |item|
    unless  = Account.find_by(number: item.number)
      reference = Nomen::Account.list.detect do |ref|
        ref.send(accounting_system).to_s == item.number
      end
       = Account.create!(number: item.number, name: item.name, label: item.label, last_letter: item.last_letter, debtor: item.is_debit, reconcilable: item.reconcilable, usages: (reference ? reference.name : nil), description: item.comment)
    end
    .id
  end
  w.check_point

  # Import financial_years
  data.import(:financial_year, :code, rename: { last_journal_entry_id: nil })
  w.check_point
  data.import(:department, :name, model: :team, rename: { sales_conditions: nil })
  w.check_point
  data.import(:district, :name, :code)
  w.check_point
  data.import(:journal, :name, converters: {
                nature: ->(j) { j.nature == 'renew' ? 'forward' : j.nature }
              })
  w.check_point
  data.import(:sequence, :number_format)
  w.check_point
  data.import(:role, :name, rename: { rights: nil })
  w.check_point
  data[:user].each do |item|
    item.email = item.name + '@ekylibre.org' if item.email.blank?
  end
  password = '12345678'
  data.import(:user, :email, rename: { admin: :administrator, office: nil, profession_id: nil, arrived_on: nil, departed_on: nil, hashed_password: nil, salt: nil, reduction_percent: :maximal_grantable_reduction_percentage, connected_at: :last_sign_in_at, name: nil, rights: nil, establishment_id: nil }, default_values: { password: password, password_confirmation: password })
  w.check_point
  data.import(:entity, :code, rename: { code: :number, category_id: nil, nature_id: nil, payment_delay_id: nil, payment_mode_id: nil, webpass: nil, vat_submissive: :vat_subjected, soundex: nil, salt: nil, hashed_password: nil, invoices_count: nil, origin: :meeting_origin, attorney: nil, attorney_account_id: nil, born_on: :born_at, dead_on: :dead_at, discount_rate: nil, reduction_rate: nil, reflation_submissive: :reminder_submissive, ean13: nil, excise: nil, first_met_on: :first_met_at, website: nil, photo: nil, siren: :siret_number }, default_values: { nature: 'organization' }, converters: { siren: ->(e) { e.siren =~ /\A\d{9}\z/ ? e.siren + Luhn.control_digit(e.siren.to_s + '0001').to_s : e.siren } })
  Entity.where('title ILIKE ? OR title ILIKE ? OR title ILIKE ?', '%Madame%', '%Monsieur%', 'M%').update_all(nature: 'contact')
  w.check_point
  data.import(:cash, :account_id, rename: { iban_label: :spaced_iban, address: :bank_agency_address, agency_code: :bank_agency_code, bic: :bank_identifier_code, by_default: nil, entity_id: :owner_id, key: :bank_account_key, number: :bank_account_number })
  w.check_point
  data.import(:bank_statement, :cash_id, :number, rename: { started_on: :started_at, stopped_on: :stopped_at })
  w.check_point
  data.import(:custom_field, :name, rename: { decimal_max: :maximal_value, decimal_min: :minimal_value, length_max: :maximal_length }, default_values: { customized_type: 'Entity' })
  w.check_point
  data.import(:journal_entry, :journal_id, :number, rename: { original_debit: :real_debit, original_credit: :real_credit, original_currency: :real_currency, original_currency_rate: :real_currency_rate, created_on: nil }, undoubler_separator: 'D')
  w.check_point
  data.import(:journal_entry_line, :entry_id, :position, rename: { original_debit: :real_debit, original_credit: :real_credit }, model: :journal_entry_item)
  w.check_point

  # puts (data.keys - data.matchings.keys).inspect.red
end