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

accounts_range_crit, crit_params, journal_entries_states_crit, journal_period_crit, journals_crit, search_conditions

Methods inherited from ApplicationController

#after_sign_in_path_for, #authorized?, human_action_name, #human_action_name

Class Method Details

.list_conditionsObject

params:

:q Text search
:working_set

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

def self.list_conditions
  code = search_conditions(products: [: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"
  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"
  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 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 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

#takeObject

Returns value of an indicator


169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'app/controllers/backend/products_controller.rb', line 169

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 [:integer, :decimal].include? indicator.datatype
    value = { value: value.to_d.round(4) }
  end
  render json: value
end