Class: ProductsController

Inherits:
ApplicationController show all
Includes:
ApplicationHelper
Defined in:
app/controllers/products_controller.rb

Instance Method Summary collapse

Methods included from AuthentificationHelper

#accessible_company_ids_from_tenant_and_user, #application_mode_from_hostname, #current_company_id_from_accessible_company_ids, #current_tenant_from_user, #current_user_and_locale_from_token

Instance Method Details

#createObject



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

def create
  handler = Products::Handlers::CreateOrUpdate.new(
    product_code: product_params[:product_code],
    description_english: product_params[:description_english],
    brand_description: product_params[:brand_description],
    brand_category_description: product_params[:brand_category_description],
    group_description: product_params[:group_description],
    subgroup_description: product_params[:subgroup_description],
    production_specs_header_id: product_params[:production_specs_header_id],
    production_specs_description_english: product_params[:production_specs_description_english],
    package_specs_header_id: product_params[:package_specs_header_id],
    default_storage_uom_description: product_params[:default_storage_uom_description],
    default_sales_uom_description: product_params[:default_sales_uom_description],
    default_purchase_uom_description: product_params[:default_purchase_uom_description],
    initiated_by_company: Rails.application.config.company_code,
    approved_to_sell: product_params[:approved_to_sell],
    approved_to_sell_online: product_params[:approved_to_sell_online],
    approved_to_purchase: product_params[:approved_to_purchase],
    active: product_params[:active],
    product_category_description: product_params[:product_category_description],
    season_description: product_params[:season_description],
    memo: product_params[:memo],
    system_date: Time.now,
    system_user: @current_user.email
  )
  created_product_data = handler.call
  @message = 'Successfully created product'
  @product = created_product_data[0]
rescue ActiveRecord::RecordInvalid => e
  @message = 'Failed to create product'
  @errors = e.record.errors
  render 'create', status: :unprocessable_entity
end

#formObject



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/controllers/products_controller.rb', line 142

def form
  render json: {
    data: {
      brands: Brand.select(:brand_id, :description).order(:description),
      brand_categories: BrandCategory.select(:brand_category_id, :description_english).order(:description_english),
      groups: Group.select(:group_id, :description).order(:description),
      sub_groups: SubGroup.select(:sub_group_id, :description).order(:description),
      seasons: Season.select(:season_id, :description).order(:description),
      product_categories: ProductCategory.select(:product_category_id,
                                                 :description_english).order(:description_english),
      units_of_measurement: UnitOfMeasurement.select(:unit_of_measurement_id, :description).order(:description)
    }
  }
end

#indexObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/products_controller.rb', line 8

def index
  query = params[:query] || ''
  @limit = params[:limit].to_i || 10
  @offset = params[:offset].to_i || 0
  @id = params[:id].to_i || ''
  fields = Product::HEADERS.map { |header| header[:field] }

  @headers = Product::HEADERS
  @data, @metadata = Product.where(
    fields: fields,
    search_value: query,
    limit: @limit,
    offset: @offset,
    product_id: @id
  ).values_at(:data, :metadata)
end

#optionsObject



157
158
159
160
161
162
163
164
165
166
167
# File 'app/controllers/products_controller.rb', line 157

def options
  query = params[:query] || ''
  product_options = ProductMaster
                    .select(:product_id, :description_english)
                    .where('description_english LIKE ?', "%#{query}%")
                    .order(:description_english)
                    .first(10)
  render json: {
    data: product_options
  }
end

#showObject



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

def show
  @id = params[:id].to_i
  product = Product.find_by!(product_id: @id)[0]

  default_storage_uom_description = if (default_storage_uom_id = product[:default_storage_uom]).nonzero?
                                      UnitOfMeasurement.find_by(unit_of_measurement_id: default_storage_uom_id)[:description]
                                    end

  default_sales_uom_description = if (default_sales_uom_id = product[:default_sales_uom]).nonzero?
                                    UnitOfMeasurement.find_by(unit_of_measurement_id: default_sales_uom_id)[:description]
                                  end

  default_purchase_uom_description = if (default_purchase_uom_id = product[:default_purchase_uom]).nonzero?
                                       UnitOfMeasurement.find_by(unit_of_measurement_id: default_purchase_uom_id)[:description]
                                     end

  product.merge!({
                   default_storage_uom_description: default_storage_uom_description,
                   default_sales_uom_description: default_sales_uom_description,
                   default_purchase_uom_description: default_purchase_uom_description,
                   product_category_description: ProductCategory.find_by(
                     product_category_id: product[:product_category_id]
                   )&.description_english,
                   season_description: Season.find_by(
                     season_id: product[:season_id]
                   )&.description

                 })

  @data = [product]
end

#sku_translationsObject



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'app/controllers/products_controller.rb', line 126

def sku_translations
  query = params[:query] || ''
  @limit = params[:limit].to_i || 10
  @offset = params[:offset].to_i || 0
  @id = params[:product_id].to_i || ''
  SkuTranslation::HEADERS.map { |header| header[:field] }

  @headers = SkuTranslation::HEADERS
  @data, @metadata = SkuTranslation.where(
    search_value: query,
    limit: @limit,
    offset: @offset,
    product_id: @id
  ).values_at(:data, :metadata)
end

#updateObject



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

def update
  handler = Products::Handlers::CreateOrUpdate.new(
    product_id: product_params[:product_id],
    product_code: product_params[:product_code],
    description_english: product_params[:description_english],
    brand_description: product_params[:brand_description],
    brand_category_description: product_params[:brand_category_description],
    group_description: product_params[:group_description],
    subgroup_description: product_params[:subgroup_description],
    production_specs_header_id: product_params[:production_specs_header_id],
    production_specs_description_english: product_params[:production_specs_description_english],
    package_specs_header_id: product_params[:package_specs_header_id],
    default_storage_uom_description: product_params[:default_storage_uom_description],
    default_sales_uom_description: product_params[:default_sales_uom_description],
    default_purchase_uom_description: product_params[:default_purchase_uom_description],
    initiated_by_company: Rails.application.config.company_code,
    approved_to_sell: product_params[:approved_to_sell],
    approved_to_sell_online: product_params[:approved_to_sell_online],
    approved_to_purchase: product_params[:approved_to_purchase],
    active: product_params[:active],
    product_category_description: product_params[:product_category_description],
    season_description: product_params[:season_description],
    memo: product_params[:memo],
    system_date: Time.now,
    system_user: @current_user.email
  )
  updated_product_data = handler.call
  @message = 'Successfully updated product'
  @result = updated_product_data[0]
rescue ActiveRecord::RecordInvalid => e
  @message = 'Failed to update product'
  @errors = e.record.errors
  render 'update', status: :unprocessable_entity
end