Module: ItemsHelper

Defined in:
app/helpers/items_helper.rb

Instance Method Summary collapse

Instance Method Details

#item_alerts(item = @item) ⇒ Object



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
# File 'app/helpers/items_helper.rb', line 130

def item_alerts(item = @item)
  successes = []
  errors = []
  if item.product_category.is_publication?
    if item.publication_visible_to_public?
      successes << "Document available online at #{link_to publication_public_url(item), publication_public_url(item)} #{clipboard_copy(publication_public_url(item))}"
    else
      errors << 'Document NOT available online. Publication must have digital asset, be available to public and active and be in at least one store.'
    end
  end
  if item.orderable_online?
    portal_url = catalog_link(item, full_url: true)
    portal_label = portal_url ? link_to('View in Product Portal', portal_url, target: '_blank', rel: "noopener noreferrer") : nil
    successes << "Item can be ordered online.#{" #{portal_label}" if portal_label}".html_safe
    item.store_items.active.available.where(store_id: [1, 2]).each do |si|
      successes << "Available in #{si.store.country_iso3} through catalogs: #{si.catalog_items.active.map { |ci| ci.catalog.name }.join(', ')}." if si.catalog_items.active.present?
    end
  else
    errors << 'Item cannot be ordered online, it must have a primary image, be available to public, active, packageable and available in at least one store and catalog.'
  end
  if item.visible_for_support?
    support_url = catalog_link(item, section: :support, full_url: true)
    if support_url
      successes << "Item visible on support portal. #{link_to 'View in Support Portal', support_url, target: '_blank', rel: "noopener noreferrer"}".html_safe
    else
      successes << 'Item visible on support portal.'
    end
  end
  errors << 'Item must have base dimensions and shipping dimensions defined or it will not be available online or pickable by employees.' unless item.can_be_packaged?
  errors << 'No Product thumbnail found, item will not display properly online.' unless item.thumbnail_url
  if item.has_stock?
    successes << 'Item has available stock in one or more stores.'
  elsif item.dropship?
    successes << 'Item is a drop ship item from supplier.'
  else
    errors << 'Item does not have physical stock in any warmlyyours store.'
  end
  errors << 'Item will be restricted for sales in US and Canadian catalog unless Tech or Marketing order/quote or manager level permissions' if item.will_be_restricted_for_sales?

  tag.ul(class: 'list-unstyled') do
    safe_join(
      errors.map do |e|
        tag.li(style: 'margin-bottom: 1em') do
          tag.span('Error', class: 'badge bg-danger me-2') + e.to_s.html_safe
        end
      end +
      successes.map do |s|
        tag.li(style: 'margin-bottom: 1em') do
          tag.span('Ok', class: 'badge bg-success me-2') + s.to_s.html_safe
        end
      end
    )
  end
end

#item_breadcrumb(item = @item) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/helpers/items_helper.rb', line 14

def item_breadcrumb(item = @item)
  capture do
    concat (:li, link_to('Items', @return_path || items_path), class: 'breadcrumb-item')
    concat (:li, link_to('Product Lines', product_lines_path), class: 'breadcrumb-item')

    if item.primary_product_line
      @item.primary_product_line.self_and_ancestors.reverse_each do |pl|
        concat (:li, link_to(pl.name.presence || pl.name_en, product_line_path(pl, tab: 'items')), class: 'breadcrumb-item')
      end
    end
    concat build_breadcrumb(item)
  end
end

#item_command_options(item = nil) ⇒ Object



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'app/helpers/items_helper.rb', line 76

def item_command_options(item = nil)
  item ||= @item

  [].tap do |opts|
    opts << link_to('Transmit', new_communication_path(upload_id: item.literature_id, return_path: item_path(item))) if item.is_publication?
    if can?(:manage, item)
      opts << link_to('Edit Main Attributes', edit_item_path(item, section: 'main_attributes'))
      opts << link_to('Edit Descriptions', edit_item_path(item, section: 'descriptions', return_path: item_path(tab: 'descriptions')))
      opts << link_to('Edit Classification', edit_item_path(item, section: 'classification', return_path: item_path(tab: 'classifications')))
      opts << link_to('Edit Logistics', edit_item_path(item, section: 'logistics', return_path: item_path(tab: 'logistics')))
      opts << link_to('Edit Images', edit_item_path(item, section: 'images', return_path: item_path(tab: 'images')))
      opts << link_to('Edit Assets', edit_item_path(item, section: 'assets', return_path: item_path(tab: 'images')))
      opts << link_to('Edit Specifications', edit_item_path(item, section: 'specifications', return_path: item_path(tab: 'specifications')))
      opts << link_to('Edit Kit/Related Items', edit_item_path(item, section: 'relationships', return_path: item_path(tab: 'relatives')))
      opts << link_to('Edit Amazon Data', edit_amazon_item_path(item, return_path: item_path(tab: 'amazon')))
      opts << link_to('Clone/Revise', clone_item_path(item))
      opts << link_to('Flush Cache', flush_cache_item_path(item), data: { turbo_method: :post })
      opts << link_to('Auto Translate Specs', auto_translate_product_specifications_item_path(item), data: { turbo_method: :post })
      opts << link_to('Auto Translate Specs (force)', auto_translate_product_specifications_item_path(item, reset_previous_values: true), data: { turbo_method: :post, turbo_confirm: 'This will reset previously set values if any' })
      opts << link_to('Auto Translate Attributes', auto_translate_attributes_item_path(item), data: { turbo_method: :post })
      opts << link_to('Auto Translate Attributes (force)', auto_translate_attributes_item_path(item, reset_previous_values: true), data: { turbo_method: :post, turbo_confirm: 'This will reset previously set values if any' })
    end
    opts << link_to('Print Labels', print_label_item_path(item, return_path: item_path(tab: 'logistics'))) if current_user.has_role?('item_receiver_usa') || current_user.has_role?('item_receiver_can') || can?(:manage, item)
    if can?(:manage, item)
      opts << link_to('Consolidate Kit', consolidate_kit_item_path(item)) if item.is_kit?
      opts << link_to('New Item', new_item_path)
      opts << link_to('New Custom Mat', new_custom_mat_path)
      if can?(:destroy, item)
        opts << { class: 'divider' }
        opts << link_to(fa_icon('trash', text: 'Delete'), item, data: { turbo_confirm: 'Are you sure?', turbo_method: :delete })
      end
    end
  end.compact
end

#item_state_label(item) ⇒ Object



8
9
10
11
12
# File 'app/helpers/items_helper.rb', line 8

def item_state_label(item)
  css_class = { discontinued: 'danger', published: 'success', private: 'warning' }[item.item_state&.downcase&.to_sym]
  css_class ||= 'secondary'
  (:span, item.item_state, class: "badge bg-#{css_class}")
end

#item_tab_options(item = @item) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'app/helpers/items_helper.rb', line 111

def item_tab_options(item = @item)
  tabs = %i[alerts main_attributes descriptions classifications specifications logistics store_items catalog_items]
  publication = item.product_category&.is_publication?
  if publication
    tabs << :revisions
  else
    tabs += %i[images videos floor_plans reviews faqs publications]
    tabs << :suppliers if can?(:read, Supplier)
    # Always show Kit Contents tab; is_kit is derived from components
    tabs << :kit_contents
    tabs << :kits if item.is_part_of_a_kit?
    tabs << :amazon if item.is_amazon_item?
  end
  tabs << :relatives
  hsh = tabs.index_with { |c| { remote_href: send("tab_#{c}_item_path", item) } }
  hsh[:attachments] = { counter: item.all_uploads.size, remote_href: item_uploads_path(item) }
  hsh
end

#item_taxonomy(item) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'app/helpers/items_helper.rb', line 62

def item_taxonomy(item)
  capture do
    tag.small do
      tag.ul(class: 'list-unstyled text-muted') do
        concat tag.li(link_to(item.product_category.lineage_expanded, product_category_path(item.product_category)))
        concat tag.li do
          concat link_to(item.primary_product_line.try(:lineage_expanded), product_line_path(item.primary_product_line, tab: 'items')) if item.primary_product_line
          concat tag.span('&#x2654;', title: 'Primary Product Line')
        end
      end
    end
  end
end

#setup_item(item) ⇒ Object



28
29
30
31
32
33
34
35
36
37
# File 'app/helpers/items_helper.rb', line 28

def setup_item(item)
  item.tap do |i|
    Store.all.each do |store|
      if i.store_items.select { |si| si.store_id == store.id }.empty?
        si = i.store_items.build(location: 'AVAILABLE', store:, unit_cogs: 0, handling_charge: 0)
        setup_store_item(si)
      end
    end
  end
end

#setup_item_amazon(item) ⇒ Object

Prepare an item for amazon editing



55
56
57
58
59
# File 'app/helpers/items_helper.rb', line 55

def setup_item_amazon(item)
  item.tap do
    # placeholder
  end
end

#setup_item_literature(item) ⇒ Object



39
40
41
42
43
# File 'app/helpers/items_helper.rb', line 39

def setup_item_literature(item)
  item.tap do |i|
    i.build_literature unless i.literature
  end
end

#setup_item_relationship(item) ⇒ Object



45
46
47
48
49
50
51
52
# File 'app/helpers/items_helper.rb', line 45

def setup_item_relationship(item)
  item.tap do
    if item.target_item_relations.empty?
      tir = item.target_item_relations.build(source_item_id: item.id, quantity: 1)
      tir.relation_type = 'Kit Component' if item.is_kit?
    end
  end
end

#sorted_items_by_state_and_sku(items) ⇒ Object



4
5
6
# File 'app/helpers/items_helper.rb', line 4

def sorted_items_by_state_and_sku(items)
  items.sort_by { |i| [%w[Published Private Discontinued].index(i.item_state) || 999, i.sku] }
end