Class: ItemsController

Inherits:
CrmController show all
Includes:
Controllers::ContentLocalizable, Controllers::Destroyable
Defined in:
app/controllers/items_controller.rb

Overview

Controller: items.

Constant Summary

Constants included from Controllers::ReferenceFindable

Controllers::ReferenceFindable::ID_EMBEDDED_PATTERNS

Constants included from Controllers::AnalyticsEvents

Controllers::AnalyticsEvents::MAX_QUEUED_EVENTS, Controllers::AnalyticsEvents::SESSION_KEY

Constants included from Controllers::ErrorRendering

Controllers::ErrorRendering::NON_CONTENT_PATH_PREFIXES

Constants included from Www::SeoHelper

Www::SeoHelper::AWARDS, Www::SeoHelper::CA_ADDRESS, Www::SeoHelper::CA_BUSINESS_HOURS, Www::SeoHelper::CA_CONTACT_POINT, Www::SeoHelper::CA_CURRENCIES, Www::SeoHelper::CA_DESCRIPTION, Www::SeoHelper::CA_FOUNDING_DATE, Www::SeoHelper::CA_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::CA_LEGAL_NAME, Www::SeoHelper::CA_LOCAL_BUSINESS, Www::SeoHelper::CA_ONLINE_STORE, Www::SeoHelper::CA_RETURN_POLICY, Www::SeoHelper::CA_SALES_DEPARTMENT, Www::SeoHelper::CA_SERVICE_AREA, Www::SeoHelper::CA_URL, Www::SeoHelper::CA_VAT_ID, Www::SeoHelper::CA_WAREHOUSE_DEPARTMENT, Www::SeoHelper::CA_WAREHOUSE_HOURS, Www::SeoHelper::COMPANY_EMAIL, Www::SeoHelper::COMPANY_LOGO, Www::SeoHelper::COMPANY_NAME, Www::SeoHelper::COMPANY_SLOGAN, Www::SeoHelper::EXPERTISE, Www::SeoHelper::FAX_NUMBER, Www::SeoHelper::GS1_COMPANY_PREFIX, Www::SeoHelper::ISO6523_CODE, Www::SeoHelper::PAYMENT_METHODS, Www::SeoHelper::PHONE_NUMBER, Www::SeoHelper::PRIMARY_NAICS, Www::SeoHelper::REFUND_TYPE, Www::SeoHelper::RETURN_FEES, Www::SeoHelper::RETURN_METHOD, Www::SeoHelper::RETURN_POLICY_CATEGORY, Www::SeoHelper::SECONDARY_NAICS, Www::SeoHelper::SOCIAL_PROFILES, Www::SeoHelper::US_ADDRESS, Www::SeoHelper::US_BUSINESS_HOURS, Www::SeoHelper::US_CONTACT_POINT, Www::SeoHelper::US_CURRENCIES, Www::SeoHelper::US_DESCRIPTION, Www::SeoHelper::US_FOUNDING_DATE, Www::SeoHelper::US_GLOBAL_LOCATION_NUMBER, Www::SeoHelper::US_IMAGE, Www::SeoHelper::US_LEGAL_NAME, Www::SeoHelper::US_LOCAL_BUSINESS, Www::SeoHelper::US_ONLINE_STORE, Www::SeoHelper::US_RETURN_POLICY, Www::SeoHelper::US_SALES_DEPARTMENT, Www::SeoHelper::US_SERVICE_AREA, Www::SeoHelper::US_TAX_ID, Www::SeoHelper::US_URL, Www::SeoHelper::US_WAREHOUSE_DEPARTMENT, Www::SeoHelper::US_WAREHOUSE_HOURS

Constants included from IconHelper

IconHelper::CUSTOM_ICON_MAP, IconHelper::CUSTOM_SVG_DIR, IconHelper::DEFAULT_FAMILY

Instance Method Summary collapse

Methods included from Controllers::ContentLocalizable

#allow_content_locale_switch, #default_url_options, #set_content_locale

Methods included from Controllers::Destroyable

#destroy, #perform_destroy

Methods inherited from CrmController

#access_denied, #context_id, #context_object, #crm_home_path, #current_ability, #default_url_options, #download_temp, #get_tempfile_path_for_download, #init_status_job_collector, #initialize_crm_lazy_chunks, #persist_enqueued_status_jobs, #record_not_found, #redirect_to_job_or_fallback, #set_context, #set_download_path, #stash_file_for_temp_download, #sync_admin_presence_cookie

Methods inherited from ApplicationController

#account_impersonated?, #add_to_flash, #after_sign_in_path_for, #bypass_forgery_protection?, #chat_enabled?, #cloudflare_cleared?, #default_catalog, #default_url_options, #enable_turbo_frames, #find_publication, #fix_invalid_accept_header, #init_js_utils, #is_globals_call?, #layout_by_resource, #locale_store, #redirect_to, #require_employee_for_crm, #set_base_host, #set_real_ip, #set_report_errors_for, #should_render_layout?, #stamp_impersonation_context, #warmlyyours_canada_ip?, #warmlyyours_ip?, #y

Methods included from Controllers::ReturnPathHandling

#check_for_return_path, #redirect_to_return_path_or_default

Methods included from Controllers::AnalyticsEvents

#consume_queued_analytics_events, #track_event

Methods included from Controllers::DeviceDetection

#device_detector, #is_ie?

Methods included from Controllers::SubdomainDetection

#is_crm_request?, #is_www_request?, #json_request?

Methods included from Controllers::TurboSafeRedirect

#redirect_to

Methods included from Controllers::TrackingDetection

#bot_request?, #gdpr_country?, #gdpr_country_data, #prevent_bots, #set_tracking_cookie, #track_visitor?

Methods included from Controllers::AcceleratedFileSending

#send_file_accelerated, #send_upload_accelerated

Methods included from Controllers::ErrorRendering

#excp_string, #mail_to_for_error_reporting, #render_400, #render_404, #render_406, #render_410, #render_500, #render_invalid_authenticity_token, #render_ip_spoof_error, #render_unpermitted_parameters, #safe_referer_or_fallback

Methods included from Controllers::TurnstileVerification

#load_turnstile_script_tag, #turnstile_lazy_widget, #turnstile_script_tag, #turnstile_widget, #validate_turnstile!

Methods included from Controllers::CloudflareCaching

edge_cached, #edge_cached_action?, #reset_cloudflare_cache, #set_cloudflare_cache, #skip_edge_cache!, #skip_session

Methods included from Controllers::Webpackable

#preload_webpack_fonts, #webpack_css_include, #webpack_css_url, #webpack_js_include, #wpd_is_running?

Methods included from Controllers::Localizable

#cloudflare_country_locale, #determine_request_locale, #geocoder_locale, #guest_user_locale_check, #locale_optional_www_auth_path?, #param_locale, #set_locale, #set_request_locale, #skip_localization?, #warmlyyours_ip_locale

Methods included from Controllers::Authenticable

#access_denied, #authenticate_account, #authenticate_account!, #authenticate_account_from_login_token!, #check_is_a_manager, #check_is_a_sales_manager, #check_is_an_admin, #check_is_an_employee, #check_party, #clear_mismatched_guest_user, #create_guest_user, #credentials?, #current_or_guest_user, #current_or_guest_user_id_read_only, #current_user, #devise_mapping, #fully_logged_in?, #generate_bot_id, #guest_user, #identifiable?, #init_current_user, #initialize_guest, #load_context_user, #logging_in, #resource, #resource_name, #restrict_access_for_non_employees, #scrubbed_request_path, #user_object, #warn_on_session_guest_id_leak

Methods included from ApplicationHelper

#better_number_to_currency, #check_force_logout, #check_or_cross, #check_or_times, #embedded_tab_frame_id, #error_messages, #general_disclaimer_on_product_installation_and_local_codes, #gridjs_from_html_table, #gridjs_table, #is_wy_ip, #line_break, #parent_layout, #pass_or_fail, #render_error_messages_list, #render_video_card, #resolved_auth_form_turbo_frame, #return_path_or, #safe_css_color, #set_return_path_if_present, #set_section_if_present, #tab_frame_id, #to_underscore, #track_page?, #turbo_section_wrapper, #turbo_tabs_request?, #url_on_same_domain_as_request, #widget_index_daily_focus_index_path, #working_hours?, #yes_or_no, #yes_or_no_highlighted, #yes_or_no_with_check_or_cross, #youtube_video

Methods included from UppyUploaderHelper

#file_uploader, #image_uploader, #large_file_uploader_s3, #lead_sketch_uploader, #rma_image_uploader, #rma_image_uploader_s3, #uppy_uploader, #video_uploader

Methods included from Www::ImagesHelper

#image_asset_tag, #image_asset_url

Methods included from Www::SeoHelper

#add_page_schema, #add_webpage_schema, #canada?, #company_social_links, #ensure_context_json, #json_ld_script_tag, #local_business_schema, #online_store_id, #online_store_schema, #page_main_entity, #page_main_entity_json, #render_auto_collection_page_schema, #render_collection_page_schema, #render_local_business_schema, #render_online_store_schema, #render_page_schemas, #render_page_video_schemas, #render_webpage_schema, #render_webpage_schema_with_collections, #usa?

Methods included from UrlsHelper

#catalog_breadcrumb_links, #catalog_link, #catalog_link_for_product_line, #catalog_link_for_sku, #cms_link, #delocalized_path, #path_to_sales_product_sku, #path_to_sales_product_sku_for_product_line, #path_to_sales_product_sku_for_product_line_slug, #product_line_from_catalog_link, #protocol_neutral_url, #sanitize_external_url, #valid_external_url?

Methods included from IconHelper

#account_nav_icon, #fa_icon, #star_rating_html

Instance Method Details

#apply_template_specsObject



182
183
184
185
186
187
188
189
190
191
192
193
# File 'app/controllers/items_controller.rb', line 182

def apply_template_specs
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  res = @item.create_template_specifications
  if res.errors.blank?
    @item.update_rendered_product_specifications(user_id: current_user.id)
    flash[:info] = 'Template specs applied'
  else
    flash[:error] = "Template Spec could not be created: #{res.errors.to_sentence.capitalize}"
  end
  redirect_to item_path(@item, tab: 'specifications')
end

#auto_translate_attributesObject



139
140
141
142
143
144
145
146
147
148
149
150
151
# File 'app/controllers/items_controller.rb', line 139

def auto_translate_attributes
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  job_options = { item_id: @item.id, reset_previous_values: params[:reset_previous_values].to_b, attributes: true }.stringify_keys
  if Rails.env.development?
    ItemTranslationWorker.new.perform(job_options)
    flash[:info] = 'Translation completed and specs refreshed'
  else
    ItemTranslationWorker.perform_async(job_options)
    flash[:info] = 'Attribute Translation Queued'
  end
  redirect_to_return_path_or_default item_path(@item)
end

#auto_translate_product_specificationsObject



153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'app/controllers/items_controller.rb', line 153

def auto_translate_product_specifications
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  job_options = { item_id: @item.id, reset_previous_values: params[:reset_previous_values].to_b, specs: true }.stringify_keys
  if Rails.env.development?
    ItemTranslationWorker.new.perform(job_options)
    flash[:info] = 'Translation completed and specs refreshed'
  else
    ItemTranslationWorker.perform_async(job_options)
    flash[:info] = 'Specs Translation Queued'
  end
  redirect_to_return_path_or_default item_path(@item)
end

#cloneObject



214
215
216
217
218
219
220
221
222
223
# File 'app/controllers/items_controller.rb', line 214

def clone
  @item = Item.find(params[:id])
  @item_actions = [['Clone/Revise', :clone]]
  if @item.refurbished_version
    flash[:warning] = 'Item already has a refurbished version, refurbish is disabled'
  else
    @allow_catalog_selection = true
    @item_actions << ['Create refurbished version (-BTK)', :refurbish]
  end
end

#consolidate_kitObject



262
263
264
265
266
267
268
269
270
# File 'app/controllers/items_controller.rb', line 262

def consolidate_kit
  @item = Item.kits.find(params[:id])
  authorize!(:update, @item)
  Item.transaction do
    Item::KitConsolidator.new(@item).consolidate_all_fields.commit
  end
  flash[:info] = 'Item Kit has been consolidated.'
  redirect_to @item
end


466
467
468
469
470
471
472
# File 'app/controllers/items_controller.rb', line 466

def copy_related_items_from_siblings
  @item = Item.find(params[:id])
  authorize!(:update, Item)
  result = Item::CopyRelatedItemsFromSiblings.new.process(item: @item, relation_type: params[:relation_type].presence)
  flash[:info] = result.message
  redirect_to item_path(tab: 'relatives')
end

#createObject

POST /items
POST /items.xml



409
410
411
412
413
414
415
416
417
418
419
420
# File 'app/controllers/items_controller.rb', line 409

def create
  @item = Item.new(params[:item])
  authorize!(:create, @item)
  # @item.generate_store_items
  if @item.save
    @item.create_template_specifications
    @item.update_rendered_product_specifications(user_id: current_user.id)
    redirect_to(@item)
  else
    render :new, status: :unprocessable_content
  end
end

#do_cloneObject



225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
# File 'app/controllers/items_controller.rb', line 225

def do_clone
  original_item = Item.find(params[:id])
  action_type = params[:item][:action_type]
  catalog_ids = params.dig(:item, :catalogs)&.reject(&:empty?)&.map(&:to_i) || []
  authorize!(:create, original_item)
  res = Item::Cloner.new.process(original_item, action_type, catalog_ids)

  if res.success.to_b
    flash[:info] = res.result_message
    redirect_to_return_path_or_default item_path(res.item)
  else
    flash[:error] = res.result_message
    redirect_to_return_path_or_default item_path(original_item)
  end
end

#editObject

GET /items/1/edit



391
392
393
394
395
# File 'app/controllers/items_controller.rb', line 391

def edit
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  render_edit_action(status: :ok)
end

#edit_amazonObject



277
278
279
280
281
# File 'app/controllers/items_controller.rb', line 277

def edit_amazon
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  set_image_ransack
end

#edit_catalogsObject



272
273
274
275
# File 'app/controllers/items_controller.rb', line 272

def edit_catalogs
  @item = Item.find(params[:id])
  authorize!(:update, @item)
end

#edit_kit_contentsObject

Dedicated editor for kit contents only



398
399
400
401
402
403
404
405
# File 'app/controllers/items_controller.rb', line 398

def edit_kit_contents
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  # Render dedicated kit editor view/partials
  params[:section] = 'kit_contents'
  @kit_only = true
  # Implicitly renders app/views/items/edit_kit_contents.html.erb
end

#export_amalytics_snapshotObject



708
709
710
711
712
713
714
715
716
# File 'app/controllers/items_controller.rb', line 708

def export_amalytics_snapshot
  catalog_items = CatalogItem.amazons_sellers.joins(store_items: :item).merge(StoreItem.available.warmlyyours_warehouses).where(items: { id: params[:id] })
  file_name = "amalytix_export_#{Time.current.to_i}.xlsx"
  directory_path = Rails.root.join(Rails.application.config.x.temp_storage_path.to_s, 'users', current_user.id.to_s)
  FileUtils.mkdir_p(directory_path)
  output_file_path = Rails.root.join(directory_path, file_name)
  Amazon::ExportAmalyticsSnapshot.new.process(catalog_items:, output_file_path:)
  send_file_accelerated(output_file_path)
end

#feature_updateObject



699
700
701
702
703
704
705
706
# File 'app/controllers/items_controller.rb', line 699

def feature_update
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  @item.update(params[:item])
  respond_to do |format|
    format.json { respond_with_bip(@item) }
  end
end

#featuresObject



691
692
693
694
695
696
697
# File 'app/controllers/items_controller.rb', line 691

def features
  params[:q] ||= {}
  params[:q][:public_and_active_in_catalog_id] ||= [1, 2]
  @q = Item.non_publications.ransack(params[:q])
  @q.sorts = 'sku' if @q.sorts.blank?
  @pagy, @items = pagy(@q.result, limit: 50)
end

#flush_cacheObject



718
719
720
721
722
723
724
# File 'app/controllers/items_controller.rb', line 718

def flush_cache
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  @item.touch
  @item.purge_edge_cache
  redirect_to item_path(@item), notice: 'Cache purge has been queued, this can take a couple minutes.'
end

#identifier_labelObject



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'app/controllers/items_controller.rb', line 48

def identifier_label
  @item = Item.find(params[:id])
  upc_params = params[:upc] || {}
  size = upc_params[:size].presence || 'large'
  output_amazon_transparency = upc_params[:output_amazon_transparency].presence || false
  serial_number = upc_params[:serial_number].presence
  target_market = upc_params[:target_market].presence&.to_sym || :us_ca
  result = Pdf::Label::ItemIdentifier.call(@item,
                                          size: size,
                                          skip_claim_amazon_transparency: true,
                                          output_amazon_transparency: output_amazon_transparency,
                                          target_market: target_market,
                                          serial_number: serial_number)
  send_data result.pdf,
            filename: result.file_name,
            type: 'application/pdf',
            disposition: 'inline',
            encoding: 'utf8'
end

#import_amazon_imagesObject

POST /items/:id/import_amazon_images
Imports missing Amazon images from live catalog data into our image library



312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
# File 'app/controllers/items_controller.rb', line 312

def import_amazon_images
  @item = Item.find(params[:id])
  authorize!(:update, @item)

  # Use specific catalog item if provided, otherwise service will use first active Amazon catalog
  catalog_item = params[:catalog_item_id].present? ? CatalogItem.find_by(id: params[:catalog_item_id]) : nil
  result = Catalog::AmazonImageBackfillService.new(@item, catalog_item: catalog_item).process

  if result[:errors].any?
    flash[:error] = "Import completed with errors: #{result[:errors].join(', ')}"
  elsif result[:imported].positive?
    flash[:notice] = "Successfully imported #{result[:imported]} image(s). #{result[:skipped]} slot(s) skipped."
  else
    flash[:notice] = "No new images to import. #{result[:skipped]} slot(s) already have images or Amazon has no image."
  end

  redirect_to_return_path_or_default item_path(@item, tab: 'amazon')
end

#indexObject



11
# File 'app/controllers/items_controller.rb', line 11

def index; end

#item_labelObject



68
69
70
71
72
73
74
75
76
# File 'app/controllers/items_controller.rb', line 68

def item_label
  @items = Item.where(id: params[:id])
  result = Pdf::Label::StorageLocationItem.call(items: @items)
  send_data result.pdf,
            filename: result.file_name,
            type: 'application/pdf',
            disposition: 'inline',
            encoding: 'utf8'
end

#lookupObject



437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
# File 'app/controllers/items_controller.rb', line 437

def lookup
  if (item_id = params[:item_id])
    @results = Item.where(id: item_id)
  else
    @results = case params[:scope]
               when 'all'
                 Item.all
               when 'publications'
                 Item.publications
               else
                 Item.non_publications
               end
    @results = @results.active unless params[:include_inactive].to_b
    @results = @results.where.not(id: params[:exclude_item_ids]) if params[:exclude_item_ids].present?
    @results = @results.where(condition: params[:item_conditions]) if params[:item_conditions].present?
    if params[:kits] == 'only'
      @results = @results.kits
    elsif params[:kits] == 'exclude'
      @results = @results.non_kits
    end
    @results = if (q = params[:q].presence || params[:term].presence)
                 @results.wild_search(q)
               else
                 @results.order(Item[:sku])
               end
  end
  render json: TomSelect.format_json_results(self, @results, params[:page], params[:per_page])
end

#make_primary_imageObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/controllers/items_controller.rb', line 78

def make_primary_image
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  if (img = Image.find(params[:image_id]))
    @item.primary_image = img
    if @item.save
      flash[:info] = "Image #{params[:image_id]} is not primary image of item #{@item.sku}"
    else
      flash[:error] = "Could not make image id #{params[:image_id]} the primary image of item #{@item.sku}"
    end
  else
    flash[:error] = "Image #{params[:image_id]} not found"
  end
  redirect_to_return_path_or_default item_path(@item)
end

#newObject

GET /items/new
GET /items/new.xml



385
386
387
388
# File 'app/controllers/items_controller.rb', line 385

def new
  authorize!(:create, Item)
  @item = Item.new(params[:item])
end


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
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/controllers/items_controller.rb', line 94

def print_identifier_label
  require 'hexapdf'

  @item = Item.find(params[:id])
  authorize!(:read, @item)
  print_profile = PrintProfile.where(id: params.dig(:upc, :print_profile_id)).first
  quantity = params.dig(:upc, :quantity)&.to_i || 1
  size = params.dig(:upc, :size).presence || 'large'
  target_market = params.dig(:upc, :target_market)
  serial_number = params.dig(:upc, :serial_number).presence
  output_amazon_transparency = params.dig(:upc, :output_amazon_transparency).to_b
  amazon_transparency_code = params.dig(:upc, :amazon_transparency_code).presence
  if quantity.between?(1, 100)
    pdf = PdfCombinator.new
    quantity.times do
      upc_result = Pdf::Label::ItemIdentifier.call(@item,
                                                 size: size,
                                                 output_amazon_transparency: output_amazon_transparency,
                                                                  amazon_transparency_code: amazon_transparency_code,
                                                                  target_market: target_market,
                                                                  serial_number: serial_number)
      pdf << upc_result.pdf
    end

    if print_profile
      base64_output = Base64.encode64(pdf.to_pdf)
      r = print_profile.print(base64_output, quantity: 1)
      flash[:info] = "UPC label for #{@item.sku} sent to printer: #{print_profile.printer.name} with job id #{r}"
    else
      # Stash the PDF for the download-bridge Stimulus controller to surface
      # via the Jobs offcanvas + auto-trigger. Streaming binary back from a
      # Turbo-submitted form gets corrupted by TurboStreamFlashable.
      res = get_tempfile_path_for_download("#{@item.sku}_identifier_label.pdf")
      File.binwrite(res[:local_file_path], pdf.to_pdf)
      session[:download_path] = res[:request_path]
      session[:download_disposition] = :inline
      flash[:info] = "PDF generated for #{@item.sku} (#{quantity} label#{'s' if quantity > 1})"
    end
    redirect_to_return_path_or_default(item_path(@item, tab: 'logistics'))
  else
    flash[:error] = "Print Profile #{params[:print_profile_id]} missing or unknown or quantity exceeds limit of 100"
    redirect_to_return_path_or_default(item_path(@item, tab: 'logistics'))
  end
end


44
45
46
# File 'app/controllers/items_controller.rb', line 44

def print_label
  @item = Item.find(params[:id])
end

#prune_empty_specsObject



195
196
197
198
199
200
201
202
# File 'app/controllers/items_controller.rb', line 195

def prune_empty_specs
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  @item.prune_empty_specs
  @item.update_rendered_product_specifications(user_id: current_user.id)
  flash[:info] = 'Empty template specs removed if any'
  redirect_to item_path(@item, tab: 'specifications')
end

#publicationObject



204
205
206
207
208
209
210
211
212
# File 'app/controllers/items_controller.rb', line 204

def publication
  @item = Item.publications.find(params[:id])
  authorize!(:read, @item)
  if @item
    redirect_to @item.authenticated_url
  else
    render_404
  end
end

#push_amazon_listing_data_allObject

POST /items/:id/push_amazon_listing_data_all
Pushes listing data to all active Amazon catalog items for this item



333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
# File 'app/controllers/items_controller.rb', line 333

def push_amazon_listing_data_all
  @item = Item.find(params[:id])
  authorize!(:update, @item)

  catalog_item_ids = @item.catalog_items.amazons.active.ids

  if catalog_item_ids.empty?
    flash[:warning] = 'No active Amazon catalog items found for this item.'
    redirect_to_return_path_or_default item_path(@item, tab: 'amazon')
    return
  end

  amazon_opts = {
    catalog_item_ids: catalog_item_ids,
    operation: 'PATCH',
    redirect_to: item_path(@item, tab: 'amazon')
  }
  job_id = AmazonItemOperationWorker.perform_async(**amazon_opts)
  job_id ||= SidekiqUniqueJobsJidLookup.active_jid_for_args(AmazonItemOperationWorker, amazon_opts)

  flash[:info] = "Queued listing data push for #{catalog_item_ids.size} Amazon catalog item(s). Job ID: #{job_id}"
  redirect_to_return_path_or_default item_path(@item, tab: 'amazon')
end

#refresh_embeddingObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/items_controller.rb', line 241

def refresh_embedding
  @item = Item.find(params[:id])
  authorize!(:update, @item)

  # Use chunked embeddings for publications (PDFs can be large)
  if @item.is_publication? && @item.needs_chunking?
    embeddings = @item.generate_chunked_embeddings!(:primary, force: true)
    if embeddings.any?
      flash[:info] = "Generated #{embeddings.size} chunk embeddings successfully"
    else
      flash[:warning] = 'Failed to generate embeddings'
    end
  elsif @item.generate_embedding!(:primary, force: true)
    flash[:info] = 'Embedding refreshed successfully'
  else
    flash[:warning] = 'Failed to generate embedding'
  end

  redirect_to_return_path_or_default item_path(@item, tab: 'publications')
end

#refresh_rendered_product_specificationsObject



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'app/controllers/items_controller.rb', line 167

def refresh_rendered_product_specifications
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  tokens = params[:tokens].presence
  res = @item.update_rendered_product_specifications(user_id: current_user.id, tokens: tokens)
  if res == :success
    flash[:info] = 'Specs refreshed'
  elsif res == :skipped
    flash[:warning] = 'Specs refresh were skipped (perhaps publication or unsaved item)'
  else
    flash[:error] = "Error refreshing specs, #{@item.errors_to_s}."
  end
  redirect_to_return_path_or_default item_path(@item, tab: 'specifications')
end

#showObject



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'app/controllers/items_controller.rb', line 13

def show
  @item = Item.find_by(id: params[:id]) || Item.find_by(sku: params[:id])
  if @item.nil?
    flash[:error] = "Item #{params[:id]} not found"
    redirect_to items_path
  elsif @item.id.to_s != params[:id].to_s
    redirect_to item_path(@item)
  elsif @item.is_publication? && params[:force_item_view].blank?
    redirect_to publication_path(@item)
  else
    authorize!(:read, @item)
    if params[:refresh_specs].to_b
      @item.update_rendered_product_specifications(user_id: current_user.id)
      @item.reload
    end
    # When a form (e.g. custom_mats#create, image profile modal) submits
    # with Turbo Drive and we redirect here, Turbo's follow-up GET reuses
    # the original `Accept: text/vnd.turbo-stream.html` header. There is
    # no show.turbo_stream template, so:
    #   - returning `head :no_content` (204) makes Turbo "do nothing" and
    #     the user sees the form spin and clear with no navigation;
    #   - omitting the format.turbo_stream block raises 406 UnknownFormat.
    # Render the HTML show template with an explicit text/html Content-Type
    # so Turbo Drive treats it as a normal page response and replaces <body>.
    respond_to do |format|
      format.html
      format.turbo_stream { render :show, formats: [:html], content_type: 'text/html' }
    end
  end
end

#tab_alertsObject

Tab actions for turbo-tabs lazy loading



475
476
477
478
479
480
481
482
# File 'app/controllers/items_controller.rb', line 475

def tab_alerts
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_all_imagesObject



565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
# File 'app/controllers/items_controller.rb', line 565

def tab_all_images
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  @selected_tag = params[:tag].presence
  @image_locale = params[:image_locale].presence
  tag_filter = @selected_tag == 'all' ? nil : @selected_tag
  locale_filter = @image_locale == 'all' ? nil : @image_locale
  @all_images = Item::ImageRetriever.new(tags: [tag_filter].compact.uniq.presence,
                                         locales: locale_filter,
                                         include_secondary_product_lines: true,
                                         product_line_query_limit: 500,
                                         max_images: 500,
                                         respect_primary_image_tags: true).process(@item).all_images
  ActiveRecord::Associations::Preloader.new(records: @all_images, associations: [{ taggings: :tag }]).call
  @image_profile_types_by_image_id = ImageProfile
                                     .where(image_id: @all_images.map(&:id), item_id: @item.id)
                                     .group_by(&:image_id)
                                     .transform_values { |recs| recs.to_set(&:image_type) }
  @image_profile_types_in_item = ImageProfile
                                 .where(item_id: @item.id)
                                 .then { |q| locale_filter.present? ? q.where(locale: locale_filter) : q }
                                 .distinct
                                 .pluck(:image_type)
                                 .to_set
  @all_tags = (['all'] + Image.all_tags).uniq.compact.sort
  @all_locales = (['all'] + Image.pluck(Arel.sql('distinct unnest(locales) as locale'))).uniq.compact.sort
  render layout: false
end

#tab_amazonObject



673
674
675
676
677
678
679
680
# File 'app/controllers/items_controller.rb', line 673

def tab_amazon
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_catalog_itemsObject



538
539
540
541
542
543
544
545
# File 'app/controllers/items_controller.rb', line 538

def tab_catalog_items
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_classificationsObject



502
503
504
505
506
507
508
509
# File 'app/controllers/items_controller.rb', line 502

def tab_classifications
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_descriptionsObject



493
494
495
496
497
498
499
500
# File 'app/controllers/items_controller.rb', line 493

def tab_descriptions
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_faqsObject



623
624
625
626
627
628
629
630
# File 'app/controllers/items_controller.rb', line 623

def tab_faqs
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_floor_plansObject



603
604
605
606
607
608
609
610
# File 'app/controllers/items_controller.rb', line 603

def tab_floor_plans
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_imagesObject



556
557
558
559
560
561
562
563
# File 'app/controllers/items_controller.rb', line 556

def tab_images
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_kit_contentsObject



653
654
655
656
657
658
659
660
661
# File 'app/controllers/items_controller.rb', line 653

def tab_kit_contents
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  @item_relations = @item.target_item_relations.kit_components.joins(target_item: :product_category).order(ProductCategory[:priority], Item[:sku])
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_kitsObject



663
664
665
666
667
668
669
670
671
# File 'app/controllers/items_controller.rb', line 663

def tab_kits
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  @item_relations = @item.source_item_relations.kit_components.joins(:source_item).order(Item[:is_discontinued], Item[:sku])
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_logisticsObject



520
521
522
523
524
525
526
527
# File 'app/controllers/items_controller.rb', line 520

def tab_logistics
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_main_attributesObject



484
485
486
487
488
489
490
491
# File 'app/controllers/items_controller.rb', line 484

def tab_main_attributes
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_publicationsObject



632
633
634
635
636
637
638
639
# File 'app/controllers/items_controller.rb', line 632

def tab_publications
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_relativesObject



682
683
684
685
686
687
688
689
# File 'app/controllers/items_controller.rb', line 682

def tab_relatives
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_reviewsObject



612
613
614
615
616
617
618
619
620
621
# File 'app/controllers/items_controller.rb', line 612

def tab_reviews
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  @item_reviews = ReviewsIo.active.visible.product_reviews.by_sku(@item.sku).recent.limit(50)
  @review_stats = ReviewsIo.stats_for_skus([@item.sku])
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_revisionsObject



547
548
549
550
551
552
553
554
# File 'app/controllers/items_controller.rb', line 547

def tab_revisions
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_specificationsObject



511
512
513
514
515
516
517
518
# File 'app/controllers/items_controller.rb', line 511

def tab_specifications
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_store_itemsObject



529
530
531
532
533
534
535
536
# File 'app/controllers/items_controller.rb', line 529

def tab_store_items
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_suppliersObject



641
642
643
644
645
646
647
648
649
650
651
# File 'app/controllers/items_controller.rb', line 641

def tab_suppliers
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  authorize!(:read, Supplier)
  @supplier_items = @item.supplier_items
  @purchase_order_items = @item.purchase_order_items.joins(:purchase_order).includes(:purchase_order).where(state: %w[not_receipted partially_receipted])
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_videosObject



594
595
596
597
598
599
600
601
# File 'app/controllers/items_controller.rb', line 594

def tab_videos
  @item = Item.find(params[:id])
  authorize!(:read, @item)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#updateObject

PUT /items/1
PUT /items/1.xml



424
425
426
427
428
429
430
431
432
433
434
435
# File 'app/controllers/items_controller.rb', line 424

def update
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  # Ensure kits save cleanly when editing kit contents
  update_attrs = params.require(:item).permit!
  if @item.update(update_attrs)
    @item.discontinue_store_and_catalog_items if @item.is_discontinued?
    redirect_to_return_path_or_default(@item)
  else
    render_edit_action
  end
end

#update_amazonObject



283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'app/controllers/items_controller.rb', line 283

def update_amazon
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  @item.refresh_specs = true
  item_params = params[:item].to_h
  image_profiles_attributes = item_params[:image_profiles_attributes] || {}
  image_profiles_attributes.transform_values! do |v|
    v.delete(:id)
    v
  end
  save_result = false
  Item.transaction do
    if image_profiles_attributes.present?
      # Delete all of them first, its a lot easier than trying to figure out which ones to delete
      @item.image_profiles.destroy_all
    end
    save_result = @item.update(item_params)
  end
  if save_result
    redirect_to_return_path_or_default item_path(@item, tab: 'amazon')
  else
    flash.now[:error] = @item.errors_to_s.presence || 'Could not update Amazon item.'
    set_image_ransack
    render :edit_amazon, status: :unprocessable_content
  end
end

#update_boxes_from_discrepancy_packingObject



357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'app/controllers/items_controller.rb', line 357

def update_boxes_from_discrepancy_packing
  @item = Item.find(params[:id])
  authorize!(:update, @item)
  all_good = false
  err_msgs = []
  if @item.packaging_discrepancy_packing
    package = @item.packaging_discrepancy_packing.to_packages.select { |p| p.contents&.first&.item_id == @item.id }
    if package
      if @item.update_boxes_from_packages(package)
        all_good = true
        flash[:info] = 'Item boxes have been updated.'
        @item.update_column(:review_product_packaging_flag, false)
        @item.update_column(:packaging_discrepancy_packing_id, nil)
      else
        err_msgs = @item.errors.full_messages
      end
    else
      err_msgs << "Could not match package to item, packages: #{@item.packaging_discrepancy_packing.to_packages}"
    end
  else
    err_msgs << "Could not find packing ID: #{@item.packaging_discrepancy_packing_id}"
  end
  flash[:error] = "Item boxes could not be updated. #{err_msgs.join(', ')}" unless all_good
  redirect_to edit_item_path(@item, section: 'logistics')
end