Class: Backend::ProductsController

Inherits:
BaseController show all
Defined in:
app/controllers/backend/products_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

.list_conditionsObject

params:

:q Text search
:working_set

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

def self.list_conditions
  code = search_conditions(products: %i[name work_number number description uuid], product_nature_variants: [:name]) + " ||= []\n"
  code << "unless params[:working_set].blank?\n"
  code << "  item = Nomen::WorkingSet.find(params[:working_set])\n"
  code << "  c[0] << \" AND products.nature_id IN (SELECT id FROM product_natures WHERE \#{WorkingSet.to_sql(item.expression)})\"\n"
  code << "end\n"

  # State
  code << "if params[:s] == 'available'\n"
  code << "  c[0] << ' AND #{Product.table_name}.dead_at IS NULL'\n"
  code << "elsif params[:s] == 'consume'\n"
  code << "  c[0] << ' AND #{Product.table_name}.dead_at IS NOT NULL'\n"
  code << "end\n"

  # Label
  code << "if params[:label_id].to_i > 0\n"
  code << "  c[0] << ' AND #{Product.table_name}.id IN (SELECT product_id FROM product_labellings WHERE label_id IN (?))'\n"
  code << "  c << params[:label_id].to_i\n"
  code << "end\n"

  # Period
  code << "if params[:period].to_s != 'all'\n"
  code << "  started_on = params[:started_on]\n"
  code << "  stopped_on = params[:stopped_on]\n"
  code << "  c[0] << ' AND #{Product.table_name}.born_at::DATE BETWEEN ? AND ?'\n"
  code << "  c << started_on\n"
  code << "  c << stopped_on\n"
  code << "  if params[:s] == 'consume'\n"
  code << "    c[0] << ' AND #{Product.table_name}.dead_at::DATE BETWEEN ? AND ?'\n"
  code << "    c << started_on\n"
  code << "    c << stopped_on\n"
  code << "  end\n"
  code << "end\n"
  code << "c\n"
  code.c
end

Instance Method Details

#edit_manyObject


244
245
246
247
248
249
250
251
252
253
# File 'app/controllers/backend/products_controller.rb', line 244

def edit_many
  activity = Activity.find_by(id: params[:activity_id]) if params[:activity_id]
  targetable_products = Product.where(type: %w[Animal])

  @targets = activity.present? ? targetable_products.where(activity_production_id: activity.productions.pluck(:id).push(nil)) : targetable_products
  @targets = @targets.order(:activity_production_id)

  @activity_productions = ActivityProduction.all
  @activity_productions = @activity_productions.of_activity(activity) if activity
end

#takeObject

Returns value of an indicator


224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'app/controllers/backend/products_controller.rb', line 224

def take
  return unless @product = find_and_check
  indicator = Nomen::Indicator.find(params[:indicator])
  unless indicator
    head :unprocessable_entity
    return
  end

  value = @product.get(indicator)
  if indicator.datatype == :measure
    if unit = Nomen::Unit[params[:unit]]
      value = value.convert(unit)
    end
    value = { unit: value.unit, value: value.to_d.round(4) }
  elsif %i[integer decimal].include? indicator.datatype
    value = { value: value.to_d.round(4) }
  end
  render json: value
end

#update_manyObject


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

def update_many
  activity = Activity.find_by(id: params[:activity_id]) if params[:activity_id]
  @activity_productions = ActivityProduction.all
  @activity_productions = @activity_productions.of_activity(activity) if activity
  saved = true
  @targets = if params[:target_distributions]
               params[:target_distributions].map do |_id, target_distribution|
                 product = Product.find(target_distribution[:target_id])
                 activity_production_id = target_distribution[:activity_production_id]
                 if activity_production_id.empty? && product.activity_production_id.present?
                   saved = false unless product.update(activity_production_id: nil)
                 elsif !activity_production_id.empty? && product.activity_production_id != activity_production_id.to_i
                   saved = false unless product.update(activity_production_id: activity_production_id)
                 end
                 product
               end.sort { |a, b| a.activity_production_id <=> b.activity_production_id || (b.activity_production_id && 1) || -1 }
             else
               []
             end
  if saved
    redirect_to params[:redirect] || backend_activities_path
  else
    render 'edit_many'
  end
end