Class: Backend::EntitiesController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/entities_controller.rb

Instance Attribute Summary

Attributes inherited from ApplicationController

#current_theme

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseController

account_lettering_states_crit, accounts_range_crit, amount_range_crit, crit_params, journal_entries_states_crit, journal_letter_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name, #session_controller?

Class Method Details

.entities_conditionsObject

params:

:q Text search

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
# File 'app/controllers/backend/entities_controller.rb', line 39

def self.entities_conditions
  code = ''
  code = search_conditions(entities: %i[number full_name], entity_addresses: [:coordinate]) + " ||= []\n"

  code << "  c[0] << ' AND #{Entity.table_name}.of_company IS FALSE'\n"

  code << "unless params[:state].blank?\n"
  code << "  if params[:state].include?('client')\n"
  code << "    c[0] << ' AND #{Entity.table_name}.client IS TRUE'\n"
  code << "  end\n"
  code << "  if params[:state].include?('supplier')\n"
  code << "    c[0] << ' AND #{Entity.table_name}.supplier IS TRUE'\n"
  code << "  end\n"
  code << "  if params[:state].include?('active')\n"
  code << "    c[0] << ' AND #{Entity.table_name}.active IS TRUE'\n"
  code << "  end\n"
  code << "end\n"

  code << "unless params[:nature].blank?\n"
  code << "  if Entity.nature.values.include?(params[:nature].to_sym)\n"
  code << "    c[0] << ' AND #{Entity.table_name}.nature = ?'\n"
  code << "    c << params[:nature]\n"
  code << "  end\n"
  code << "end\n"

  code << "unless params[:mail_line_6].blank?\n"
  code << "  c[0] << ' AND #{Entity.table_name}.id IN (SELECT entity_id FROM entity_addresses WHERE mail_line_6 ILIKE E? AND by_default AND deleted_at IS NULL)'\n"
  code << "  c << '%' + params[:mail_line_6].to_s.strip.gsub(/[\,\s\-]+/, '%') + '%'\n"
  code << "end\n"

  code << "unless params[:subscription_nature_id].blank? || params[:subscription_test].blank?\n"
  code << "  if params[:subscription_test] == 'subscribed_on'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id IN (SELECT subscriber_id FROM subscriptions WHERE nature_id = ? AND ? BETWEEN started_on AND stopped_on)'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "    c << params[:subscribed_on]\n"
  code << "  elsif params[:subscription_test] == 'expired_within'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id IN (SELECT subscriber_id FROM subscriptions WHERE nature_id = ? AND stopped_on BETWEEN CURRENT_DATE AND CURRENT_DATE + ?::INTERVAL) AND #{Entity.table_name}.id NOT IN (SELECT subscriber_id FROM subscriptions WHERE nature_id = ? AND stopped_on > CURRENT_DATE + ?::INTERVAL)'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "    c << params[:expired_within] + ' days'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "    c << params[:expired_within] + ' days'\n"
  code << "  elsif params[:subscription_test] == 'expired_since'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id IN (SELECT s.subscriber_id FROM subscriptions AS s WHERE s.nature_id = ? AND s.stopped_on BETWEEN (CURRENT_DATE - ?::INTERVAL) AND CURRENT_DATE)'\n"
  code << "    c << params[:subscription_nature_id].to_i\n"
  code << "    c << params[:expired_since] + ' days'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id NOT IN (SELECT s.subscriber_id FROM subscriptions AS s WHERE s.nature_id = ? AND s.stopped_on > CURRENT_DATE)'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "  elsif params[:subscription_test] == 'active_finishing_within'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id IN (SELECT s.subscriber_id FROM subscriptions AS s WHERE nature_id = ? AND started_on <= CURRENT_DATE AND stopped_on BETWEEN CURRENT_DATE AND (CURRENT_DATE + ?::INTERVAL) AND (subscriber_id, nature_id) NOT IN (SELECT subscriber_id, nature_id FROM subscriptions WHERE (CURRENT_DATE + ?::INTERVAL) BETWEEN started_on AND stopped_on))'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "    c << (params[:delay].to_i - 1).to_s + ' days'\n"
  code << "    c << params[:delay].to_i.to_s + ' days'\n"
  code << "  elsif params[:subscription_test] == 'active_finishing_after'\n"
  code << "    c[0] << ' AND #{Entity.table_name}.id IN (SELECT s.subscriber_id FROM subscriptions AS s WHERE nature_id = ? AND started_on <= CURRENT_DATE AND stopped_on >= CURRENT_DATE + ?::INTERVAL)'\n"
  code << "    c << params[:subscription_nature_id]\n"
  code << "    c << params[:delay].to_i.to_s + ' days'\n"
  code << "  end\n"
  code << "end\n"
  code << "c\n"
  code.c
end

.entities_moves_client_conditions(params) ⇒ Object


306
307
308
309
310
311
312
313
# File 'app/controllers/backend/entities_controller.rb', line 306

def self.entities_moves_client_conditions(params)
  code = ''
  code << search_conditions({ journal_entry_item: %i[name debit credit real_debit real_credit], journal_entry: [:number] }, conditions: 'c', variable: 'params[:b]'.c) + "\n"
  code << "c[0] << ' AND #{JournalEntryItem.table_name}.account_id = ?'\n"
  code << "c << Entity.find(#{params[:id]}).client_account_id\n"
  code << "c\n"
  eval code
end

.entities_moves_supplier_conditions(params) ⇒ Object


331
332
333
334
335
336
337
338
# File 'app/controllers/backend/entities_controller.rb', line 331

def self.entities_moves_supplier_conditions(params)
  code = ''
  code << search_conditions({ journal_entry_item: %i[name debit credit real_debit real_credit], journal_entry: [:number] }, conditions: 'c', variable: 'params[:b]'.c) + "\n"
  code << "c[0] << ' AND #{JournalEntryItem.table_name}.account_id = ?'\n"
  code << "c << Entity.find(#{params[:id]}).supplier_account_id\n"
  code << "c\n"
  eval code
end

Instance Method Details

#importObject


362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
# File 'app/controllers/backend/entities_controller.rb', line 362

def import
  @step = begin
            params[:id].to_sym
          rescue
            :upload
          end
  if @step == :upload
    @formats = [['CSV', :csv]] # , ["CSV Excel", :xcsv], ["XLS Excel", :xls], ["OpenDocument", :ods]]
    if request.post? && params[:upload]
      data = params[:upload]
      tmp = Rails.root.join('tmp', 'uploads')
      FileUtils.mkdir_p(tmp)
      file = tmp.join("entities_import_#{data.original_filename.gsub(/[^\w]/, '_')}")
      File.open(file, 'wb') { |f| f.write(data.read) }
      session[:entities_import_file] = file
      redirect_to action: :import, id: :columns
    end
  elsif @step == :columns
    unless File.exist?(session[:entities_import_file].to_s)
      redirect_to action: :import, id: :upload
    end
    csv = CSV.open(session[:entities_import_file])
    @columns = csv.shift
    @first_item = csv.shift
    @options = Entity.importable_columns
    if request.post?
      all_columns = params[:columns].dup.delete_if { |_k, v| v.match(/^special-dont_use/) || v.blank? }
      columns = params[:columns].delete_if { |_k, v| v.match(/^special-/) || v.blank? }
      if (columns.values.size - columns.values.uniq.size) > 0
        notify_error_now(:columns_are_already_uses)
        return
      end
      cols = {}
      columns = all_columns
      for prefix in columns.values.collect { |x| x.split(/\-/)[0] }.uniq
        cols[prefix.to_sym] = {}
        columns.select { |_k, v| v.match(/^#{prefix}-/) }.each { |k, v| cols[prefix.to_sym][k.to_s] = v.split(/\-/)[1].to_sym }
      end
      cols[:entity] ||= {}
      if cols[:entity].keys.size <= 0 || !cols[:entity].values.detect { |x| x == :last_name }
        notify_error_now(:entity_columns_are_needed)
        return
      end
      # raise StandardError.new columns.inspect+"\n"+cols.inspect
      session[:entities_import_cols] = cols
      redirect_to action: :import, id: :validate
    end
  elsif @step == :validate
    file = session[:entities_import_file]
    cols = session[:entities_import_cols]
    if request.post?
      @report = Entity.import(file, cols, no_simulation: true, ignore: session[:entities_import_ignore])
      notify_success(:importation_finished)
      redirect_to action: :import, id: :upload
    else
      @report = Entity.import(file, cols)
      session[:entities_import_ignore] = @report[:errors].keys
    end
  end
end

#mask_lettered_itemsObject


441
442
443
444
445
446
447
# File 'app/controllers/backend/entities_controller.rb', line 441

def mask_lettered_items
  preference_name = 'backend/entities'
  preference_name << ".#{params[:context]}" if params[:context]
  preference_name << '.lettered_items.masked'
  current_user.prefer!(preference_name, params[:masked].to_s == 'true', :boolean)
  head :ok
end

#mergeObject


423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
# File 'app/controllers/backend/entities_controller.rb', line 423

def merge
  if request.post?
    return unless @master = find_and_check(id: params[:master])
    return unless @double = find_and_check(id: params[:double])
    if @master.id == @double.id
      notify_error_now(:cannot_merge_an_entity_with_itself)
      return
    end
    @master.merge_with(@double, author: current_user)
    begin
      notify_success(:merge_is_done)
      redirect_to action: :show, id: @master.id
    rescue
      notify_error_now(:cannot_merge_entities)
    end
  end
end

#showObject


124
125
126
127
128
129
130
131
# File 'app/controllers/backend/entities_controller.rb', line 124

def show
  return unless @entity = find_and_check
  respond_with(@entity, include: { default_mail_address: { methods: [:mail_coordinate] } }) do |format|
    format.html do
      t3e @entity.attributes, nature: @entity.nature.l
    end
  end
end

#toggleObject


356
357
358
359
360
# File 'app/controllers/backend/entities_controller.rb', line 356

def toggle
  @entity = Entity.find_by!(id: params[:id])
  @entity.toggle!
  redirect_to params[:redirect] || { action: :show, id: @entity.id }
end