Class: Sku

Inherits:
Object
  • Object
show all
Defined in:
app/models/sku.rb

Constant Summary collapse

COMPANY_CODE =
Rails.application.config.company_code
HEADERS =
[
  {
    field: 'sku_id',
    type: 'number',
    label: 'Id'
  },
  {
    field: 'sku_code',
    type: 'string',
    label: 'SKU Code'
  },
  {
    field: 'sku_description_english',
    type: 'string',
    label: 'Sku Description'
  },
  {
    field: 'product_parent_id',
    type: 'number',
    label: 'Product Parent Id'
  },
  {
    field: 'product_id',
    type: 'number',
    label: 'Product Id'
  },
  {
    field: 'minimum_quantity',
    type: 'number',
    label: 'Min.'
  },
  {
    field: 'maximum_quantity',
    type: 'number',
    label: 'Max.'
  },
  {
    field: 'actual_cost',
    type: 'price',
    label: 'Actual Cost'
  },
  {
    field: 'standard_cost',
    type: 'price',
    label: 'Standard Cost'
  },
  {
    field: 'average_cost',
    type: 'price',
    label: 'Average Cost'
  },
  {
    field: 'default_commission',
    type: 'percentage',
    label: 'Default Commission'
  },
  {
    field: 'suggested_retail_price',
    type: 'price',
    label: 'Suggested Retail Price'
  },
  {
    field: 'suggested_retail_price_uom_id',
    type: 'number',
    label: 'Suggested Retail Price Id'
  },
  {
    field: 'markup_percentage',
    type: 'percentage',
    label: 'Markup Percentage'
  },
  {
    field: 'default_volume',
    type: 'volume',
    label: 'Default Volume'
  },
  {
    field: 'default_weight',
    type: 'weight',
    label: 'Default Weight'
  },
  {
    field: 'default_quantity_per_package',
    type: 'number',
    label: 'Default Quantity Per Package'
  },
  {
    field: 'active',
    type: 'boolean',
    label: 'Active'
  },
  {
    field: 'approved_to_sell',
    type: 'boolean',
    label: 'Approved to Sell'
  },
  {
    field: 'approved_to_purchase',
    type: 'boolean',
    label: 'Approved to Purchase'
  },
  {
    field: 'approved_to_sell_online',
    type: 'boolean',
    label: 'Approved to Sell Online'
  },
  {
    field: 'eligible_for_discounts',
    type: 'boolean',
    label: 'Eligible for Discounts'
  }
]

Class Method Summary collapse

Class Method Details

.create_or_update(sku_code:, type:, description:, description_french:, default_volume:, default_weight:, parent_sku_id:, product_parent_id:, product_id:, color_id:, design_id:, size_id:, sequence_no:, active:, memo:, system_record:, deactivation_date:, sku_hyperlink:, eligible_for_discounts:, default_quantity_per_package:, national_stock_number:, ability_one_description:, minimum_quantity:, maximum_quantity:, standard_cost:, average_cost:, default_commission:, approved_to_sell:, approved_to_purchase:, approved_to_sell_online:, suggested_retail_price:, suggested_retail_price_uom_id:, markup_percentage:, landed_cost:, system_date:, system_user:, sku_id: 0, company_code: COMPANY_CODE) ⇒ Object



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
# File 'app/models/sku.rb', line 229

def create_or_update(
  sku_code:, type:, description:, description_french:, default_volume:, default_weight:, parent_sku_id:, product_parent_id:, product_id:, color_id:, design_id:, size_id:, sequence_no:, active:, memo:, system_record:, deactivation_date:, sku_hyperlink:, eligible_for_discounts:, default_quantity_per_package:, national_stock_number:, ability_one_description:, minimum_quantity:, maximum_quantity:, standard_cost:, average_cost:, default_commission:, approved_to_sell:, approved_to_purchase:, approved_to_sell_online:, suggested_retail_price:, suggested_retail_price_uom_id:, markup_percentage:, landed_cost:, system_date:, system_user:, sku_id: 0,
  company_code: COMPANY_CODE
)
  ActiveRecord::Base.execute_procedure(
    'pr_insert_update_sku',
    sku_id,
    company_code,
    sku_code,
    type,
    description,
    description_french,
    default_volume,
    default_weight,
    parent_sku_id,
    product_parent_id,
    product_id,
    color_id,
    design_id,
    size_id,
    sequence_no,
    active,
    memo,
    system_record,
    deactivation_date,
    sku_hyperlink,
    eligible_for_discounts,
    default_quantity_per_package,
    national_stock_number,
    ability_one_description,
    minimum_quantity,
    maximum_quantity,
    standard_cost,
    average_cost,
    default_commission,
    approved_to_sell,
    approved_to_purchase,
    approved_to_sell_online,
    suggested_retail_price,
    suggested_retail_price_uom_id,
    markup_percentage,
    landed_cost,
    system_date,
    system_user
  )
end

.find_by(id:, product_id:) ⇒ Object



217
218
219
220
221
222
223
# File 'app/models/sku.rb', line 217

def find_by(id:, product_id:)
  sku = Sku.where(sku_id: id, product_id: product_id, limit: 1)[:data].first
  sku[:suggested_retail_price_uom_description] = UnitOfMeasurement.find_by(
    unit_of_measurement_id: sku[:suggested_retail_price_uom_id]
  )&.description
  sku
end

.find_by_sku_code(sku_code:, product_id:) ⇒ Object



225
226
227
# File 'app/models/sku.rb', line 225

def find_by_sku_code(sku_code:, product_id:)
  Sku.where(sku_code: sku_code, product_id: product_id, limit: 1)[:data].first
end

.where(fields: [], limit: 10, offset: 0, query: '', order_by: 'sku_code', sku_id: '', product_id: '', company_code: COMPANY_CODE, color_id: '', design_id: '', size_id: '') ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'app/models/sku.rb', line 118

def where(
  fields: [],
  limit: 10,
  offset: 0,
  query: '',
  order_by: 'sku_code',
  sku_id: '',
  product_id: '',
  company_code: COMPANY_CODE,
  color_id: '',
  design_id: '',
  size_id: ''
)
  # returns an array of hashes with these keys:
  # sku_id
  # sku_code
  # product_parent_id
  # product_id
  # product_code
  # color_id
  # design_id
  # size_id
  # sequence_no
  # sku_description_english
  # sku_description_french
  # minimum_quantity
  # maximum_quantity
  # first_cost
  # last_cost
  # actual_cost
  # standard_cost
  # average_cost
  # default_commission
  # approved_to_sell
  # approved_to_purchase
  # approved_to_sell_online
  # eligible_for_discounts
  # active
  # deactivation_date
  # creation_date
  # modification_date
  # created_by
  # modified_by
  # product_description_english
  # color_description_english
  # design_description_english
  # size_description_english
  # sku_key
  # upc_type
  # upc_code
  # suggested_retail_price
  # suggested_retail_price_uom_id
  # markup_percentage
  # parent_sku_id
  # default_volume
  # default_weight
  # default_quantity_per_package
  # record_modified
  # selected_for_promo_product
  # allow_deletion_of_sku
  raw_data = ActiveRecord::Base.execute_procedure(
    'pr_get_sku_by_product',
    sku_id,
    product_id,
    company_code,
    color_id,
    design_id,
    size_id
  )

  if fields.any?
    raw_data = raw_data.map do |sku|
      sku.select { |key, _| fields.include?(key) }
    end
  end

  if query.present?
    raw_data = raw_data.select do |row|
      row.any? do |_key, value|
        value.to_s.downcase.include?(query.downcase)
      end
    end
  end

  ordered_data = raw_data.sort_by { |row| row[order_by] }

  data = ordered_data.drop(offset).take(limit)
   = {
    total: raw_data.length,
    limit: limit,
    offset: offset
  }

  {
    data: data,
    metadata: 
  }
end