Module: Models::Pickable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- app/concerns/models/pickable.rb
Overview
ActiveSupport::Concern mixin: pickable.
Has many collapse
- #line_discounts ⇒ ActiveRecord::Relation<LineDiscount>
- #line_items ⇒ ActiveRecord::Relation<LineItem>
Instance Method Summary collapse
- #all_my_publications ⇒ Object
- #append_suggested_items(suggested_items_list, containing_resource) ⇒ Object
- #append_suggested_materials(suggested_items_list, _containing_resource = nil) ⇒ Object
- #control_capacity(product_line_url = nil) ⇒ Object
- #determine_catalog_for_picking ⇒ Object
- #determine_skus_to_filter ⇒ Object
- #discounted_shipping_total ⇒ Object
- #electrical_heating_elements ⇒ Object
- #fix_catalog ⇒ Object
-
#get_material_alerts(for_www: false, for_cart: false, _cache: true) ⇒ Array<MaterialAlert>
Returns persisted material alerts for this resource.
- #has_custom_products? ⇒ Boolean
-
#invalidate_material_alerts! ⇒ Integer
Invalidates (deletes) all cached material alerts for this resource.
- #line_items_grouped_by_room_configuration(ungrouped_name: nil, group_by_item_category: true, list_empty_group: true, include_shipping_lines: true, sort_by_product_category_priority: false, exclude_unnamed_empty_group: true) ⇒ Object
- #meets_custom_products_threshold? ⇒ Boolean
- #pricing_program_discount_factor ⇒ Object
- #purge_empty_line_items ⇒ Object
-
#soft_recalc(_reset_to_catalog_price = true) ⇒ Object
this is an admin method for us when fixing order discrepancies.
- #subtotal(limit_to_types = []) ⇒ Object
- #subtotal_after_trade_discount_without_shipping ⇒ Object
- #subtotal_discounted_without_shipping ⇒ Object
- #subtotal_msrp_without_shipping ⇒ Object
- #synchronize_lines(target = nil, save_targets = false, _do_not_recalc_shipping = false) ⇒ Object
- #total_amps_by_product_line ⇒ Object
- #total_coverage_by_product_line ⇒ Object
- #total_linear_feet_by_product_line ⇒ Object
- #total_spec_by_product_line(scope, spec, multiplier = 1) ⇒ Object
- #total_watts_by_product_line ⇒ Object
- #underlayments ⇒ Object
Instance Method Details
#all_my_publications ⇒ Object
269 270 271 272 273 274 275 276 277 278 |
# File 'app/concerns/models/pickable.rb', line 269 def all_my_publications pubs = nil line_items.each do |li| item_pubs = li.item.all_my_publications pubs = pubs.nil? ? item_pubs : (pubs | item_pubs) # This builds up an or condition end pubs = pubs.in_store(store.id) # Only publications relevant to what the order's store should be used regardless of locale pubs = yield(pubs) if block_given? pubs end |
#append_suggested_items(suggested_items_list, containing_resource) ⇒ Object
280 281 282 283 284 |
# File 'app/concerns/models/pickable.rb', line 280 def append_suggested_items(suggested_items_list, containing_resource) suggested_items_list.tap do |suggested_items_list| append_suggested_materials(suggested_items_list, containing_resource) end end |
#append_suggested_materials(suggested_items_list, _containing_resource = nil) ⇒ Object
286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 |
# File 'app/concerns/models/pickable.rb', line 286 def append_suggested_materials(suggested_items_list, _containing_resource = nil) suggested_items_list.tap do |suggested_items_list| catalog = customer.catalog # Now figure out the suggested items material_alerts = get_material_alerts material_alerts.select { |ma| ma.items.any? and (ma.recommended_qty > ma.actual_qty) }.each do |ma| logger.debug "append_suggested_materials, ma.group_name: #{ma.group_name}" logger.debug "append_suggested_materials, ma.items.map{|i| i.sku}: #{ma.items.map(&:sku).inspect}" ma.items.each do |item| ci = catalog.catalog_items.by_skus(item.sku).first next unless ci # re-apply stock thresholds and customer filters for quote pdf stock_threshold_ok = false stock_threshold_ok = true if ci.item.suggested_item_applies_to(customer) && (ci.qty_available > item.suggested_item_stock_threshold) add_item = true if stock_threshold_ok && (ma.group_name.to_s == 'Installation Kits') # ugh, hacky but this is the y to accomodate all the weird patterns for suggested add ons in the quote add_item = false logger.debug "append_suggested_materials, item.sku: #{item.sku}, add_item: #{add_item}" end next unless add_item ci_discount = [customer.pricing_program_discount, ci.max_discount].min discount_unit_price = (ci.amount * (1 - (ci_discount / 100)).to_f).round(2) suggested_items_list ||= {} suggested_items_list[self] ||= [] suggested_items_list[self] << LineItem.new(item: item, resource: self, catalog_item_id: ci.id, description: ci.public_name, quantity: (ma.recommended_qty - ma.actual_qty), price: ci.amount, discounted_price: discount_unit_price) end end end end |
#control_capacity(product_line_url = nil) ⇒ Object
66 67 68 69 70 71 72 73 |
# File 'app/concerns/models/pickable.rb', line 66 def control_capacity(product_line_url = nil) lines = if product_line_url.present? [line_items.controls.by_product_line_url(product_line_url), line_items.integration_kits.by_product_line_url(product_line_url), line_items.powers.by_product_line_url(product_line_url)].flatten else [line_items.controls, line_items.integration_kits, line_items.powers].flatten end lines.sum { |li| li.quantity * li.item.control_capacity } end |
#determine_catalog_for_picking ⇒ Object
261 262 263 |
# File 'app/concerns/models/pickable.rb', line 261 def determine_catalog_for_picking try(:is_store_transfer?) ? from_store.primary_catalog : customer.catalog end |
#determine_skus_to_filter ⇒ Object
265 266 267 |
# File 'app/concerns/models/pickable.rb', line 265 def determine_skus_to_filter try(:is_store_transfer?) ? to_store.primary_catalog.items.pluck(:sku) : nil end |
#discounted_shipping_total ⇒ Object
175 176 177 |
# File 'app/concerns/models/pickable.rb', line 175 def discounted_shipping_total line_items.active_shipping_lines.sum(&:total) end |
#electrical_heating_elements ⇒ Object
31 32 33 |
# File 'app/concerns/models/pickable.rb', line 31 def electrical_heating_elements line_items.goods.joins(item: :primary_product_line).heating_elements.order('product_lines.lineage_expanded') end |
#fix_catalog ⇒ Object
257 258 259 |
# File 'app/concerns/models/pickable.rb', line 257 def fix_catalog CatalogItem::Remapper.new(self).fix_all end |
#get_material_alerts(for_www: false, for_cart: false, _cache: true) ⇒ Array<MaterialAlert>
Returns persisted material alerts for this resource.
Uses database persistence instead of Rails.cache to avoid serialization issues.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 |
# File 'app/concerns/models/pickable.rb', line 326 def get_material_alerts(for_www: false, for_cart: false, _cache: true) return [] unless respond_to?(:line_items) # Eager load items to avoid N+1 during signature calculation (which calls line_item.sku) current_signature = line_items.includes(:item).signature if current_signature.nil? ErrorReporting.warning("Material alerts: line_items.signature returned nil for #{self.class.name} #{id}") return [] end # Check for existing valid alerts with matching signature existing = MaterialAlert.for_resource(self, signature: current_signature) return existing.to_a if existing.exists? # Generate fresh alerts and persist them = { for_www: for_www, for_cart: for_cart } check_results = Item::Materials::Check.new.perform_checks_raw( container: self, options: ) MaterialAlert.regenerate_for(self, check_results: check_results, signature: current_signature) end |
#has_custom_products? ⇒ Boolean
23 24 25 |
# File 'app/concerns/models/pickable.rb', line 23 def has_custom_products? line_items.joins(:item).where(items: { product_category_id: ProductCategory.custom_product_ids }).exists? end |
#invalidate_material_alerts! ⇒ Integer
Invalidates (deletes) all cached material alerts for this resource.
Call this when line items change significantly outside normal flow,
such as during order split operations.
355 356 357 |
# File 'app/concerns/models/pickable.rb', line 355 def invalidate_material_alerts! MaterialAlert.invalidate_for(self) end |
#line_discounts ⇒ ActiveRecord::Relation<LineDiscount>
17 |
# File 'app/concerns/models/pickable.rb', line 17 has_many :line_discounts, through: :line_items |
#line_items ⇒ ActiveRecord::Relation<LineItem>
11 12 13 14 15 16 |
# File 'app/concerns/models/pickable.rb', line 11 has_many :line_items, as: :resource, dependent: :destroy, extend: LineItemExtension, autosave: true, inverse_of: :resource |
#line_items_grouped_by_room_configuration(ungrouped_name: nil, group_by_item_category: true, list_empty_group: true, include_shipping_lines: true, sort_by_product_category_priority: false, exclude_unnamed_empty_group: true) ⇒ Object
114 115 116 117 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 |
# File 'app/concerns/models/pickable.rb', line 114 def line_items_grouped_by_room_configuration(ungrouped_name: nil, group_by_item_category: true, list_empty_group: true, include_shipping_lines: true, sort_by_product_category_priority: false, exclude_unnamed_empty_group: true) ungrouped_name ||= 'Ungrouped items' items = {} if instance_of? RoomConfiguration l_items = if include_shipping_lines line_items.parents_only.with_associations else line_items.parents_only.with_associations.reject(&:is_shipping?) end items[name] = l_items.to_a else rc_ids = [] room_configurations.order(:name).each do |rc| rc_ids << rc.id items[rc] ||= [] # Required to set order end items[ungrouped_name] = [] line_items.parents_only .includes(:item, :line_discounts, :inventory_commits, catalog_item: :store_item) .reject(&:is_shipping?).each do |li| rc = (li.resource.is_a?(RoomConfiguration) ? li.resource : li.room_configuration) group_id = (!rc.nil? && rc_ids.include?(rc.id) ? rc : ungrouped_name) items[group_id] ||= [] items[group_id] << li end end items.each { |key, lines| items[key] = LineItem.group_by_category(lines) } if group_by_item_category if sort_by_product_category_priority items.each do |key, lines| items[key] = lines.sort_by do |li| [(begin li.item.product_category.priority rescue StandardError 99 end), li.item.sku] end end end items = items.delete_if { |_key, lines| lines.empty? } unless list_empty_group items = items = items.delete_if { |key, lines| key == ungrouped_name && lines.empty? } if exclude_unnamed_empty_group items end |
#meets_custom_products_threshold? ⇒ Boolean
27 28 29 |
# File 'app/concerns/models/pickable.rb', line 27 def meets_custom_products_threshold? has_custom_products? && subtotal >= OrderConstants::CUSTOM_ORDER_AGREEMENT_THRESHOLD end |
#pricing_program_discount_factor ⇒ Object
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 |
# File 'app/concerns/models/pickable.rb', line 179 def pricing_program_discount_factor effective_discount = (begin pricing_program_discount rescue StandardError nil end) effective_discount ||= begin quote.pricing_program_discount rescue StandardError nil end # Order's quote will have precedence over customers effective_discount ||= begin customer.pricing_program_discount rescue StandardError nil end (effective_discount.to_f / 100).round(2) end |
#purge_empty_line_items ⇒ Object
75 76 77 78 |
# File 'app/concerns/models/pickable.rb', line 75 def purge_empty_line_items logger.info "#{self.class.name}:#{id} purge_empty_line_items called" line_items.select { |li| li.quantity == 0 }.each(&:destroy) end |
#soft_recalc(_reset_to_catalog_price = true) ⇒ Object
this is an admin method for us when fixing order discrepancies
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'app/concerns/models/pickable.rb', line 81 def soft_recalc(_reset_to_catalog_price = true) prev_value = begin disable_auto_coupon rescue StandardError nil end unless is_a? RoomConfiguration reload self.disable_auto_coupon = true self.line_total = nil self.tax_total = nil self.total = nil end save! update_attribute(:disable_auto_coupon, prev_value) unless is_a? RoomConfiguration end |
#subtotal(limit_to_types = []) ⇒ Object
164 165 166 167 168 169 170 171 172 173 |
# File 'app/concerns/models/pickable.rb', line 164 def subtotal(limit_to_types = []) l_its = line_items.active_non_shipping_lines if limit_to_types.present? && limit_to_types.any? l_its = [] limit_to_types.each do |ltype| l_its += line_items.send(ltype) end end l_its.sum(&:total) end |
#subtotal_after_trade_discount_without_shipping ⇒ Object
104 105 106 107 108 |
# File 'app/concerns/models/pickable.rb', line 104 def subtotal_after_trade_discount_without_shipping # Use line_discounts for exact totals (prevents rounding errors) # This matches LineItem#discounted_total and SQL calculate_itemizable_total function line_items.non_shipping.includes(:line_discounts).to_a.sum(&:discounted_total) end |
#subtotal_discounted_without_shipping ⇒ Object
98 99 100 101 102 |
# File 'app/concerns/models/pickable.rb', line 98 def subtotal_discounted_without_shipping # Use line_discounts for exact totals (prevents rounding errors) # This matches LineItem#discounted_total and SQL calculate_itemizable_total function line_items.non_shipping.includes(:line_discounts).to_a.sum(&:discounted_total) end |
#subtotal_msrp_without_shipping ⇒ Object
110 111 112 |
# File 'app/concerns/models/pickable.rb', line 110 def subtotal_msrp_without_shipping BigDecimal(line_items.non_shipping.sum('(price * quantity)')) end |
#synchronize_lines(target = nil, save_targets = false, _do_not_recalc_shipping = false) ⇒ Object
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 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 |
# File 'app/concerns/models/pickable.rb', line 200 def synchronize_lines(target = nil, save_targets = false, _do_not_recalc_shipping = false) lines_changed = [] targets = [target || try(:synchronization_targets)].flatten # Rooms push to their quotes, quotes and orders push to their room targets.each do |i| # The lines we match against varies, a room configuration will sync into quotes and order and the lines need to be filtered to be just those of the room configuration, but from a quote or order to room there's no need to do a filter target_lines = [] rc_id_filter = nil if i.is_a? RoomConfiguration rc_id_filter = i.id target_lines = i.line_items.active_parent_lines source_lines = line_items.active_parent_lines.select { |li| li.room_configuration_id == i.id } else rc_id_filter = id source_lines = line_items.active_parent_lines target_lines = i.line_items.active_parent_lines.select { |lir| lir.room_configuration_id == id } end # Try to match these room lines in the itemizable to our room merged_line_ids = [] change_detected = false target_lines.each do |lir| # iterate through each target line and find it in the source lines. beware of what was already matched. matched_line_in_source = source_lines.find do |li| li.catalog_item_id == lir.catalog_item_id and merged_line_ids.exclude?(li.id) end if matched_line_in_source # update quantities if different unless lir.quantity == matched_line_in_source.quantity lir.quantity = matched_line_in_source.quantity lines_changed << "#{i.id}[#{lir.id}M]" change_detected = true end # Keep track of lines we processed merged_line_ids << matched_line_in_source.id else lines_changed << "#{i.id}[#{lir.id}-]" lir.destroy change_detected = true end end # Anything unmatched is added source_lines.reject { |li| merged_line_ids.include? li.id }.each do |new_line| change_detected = true attrs = new_line.attributes.except('id', 'resource_id', 'resource_type', 'delivery_id', 'created_at', 'updated_at') attrs['room_configuration_id'] = rc_id_filter i.line_items.build(attrs) lines_changed << "#{i.id}[#{new_line.id}+]" end i.save if save_targets i.refresh_plans(delay: 10.seconds) if i.is_a?(RoomConfiguration) && i.respond_to?(:refresh_plans) && change_detected end lines_changed end |
#total_amps_by_product_line ⇒ Object
62 63 64 |
# File 'app/concerns/models/pickable.rb', line 62 def total_amps_by_product_line total_spec_by_product_line(electrical_heating_elements, 'amps') end |
#total_coverage_by_product_line ⇒ Object
48 49 50 51 52 |
# File 'app/concerns/models/pickable.rb', line 48 def total_coverage_by_product_line total_spec_by_product_line(electrical_heating_elements, 'coverage').merge( total_spec_by_product_line(underlayments, 'coverage') ) end |
#total_linear_feet_by_product_line ⇒ Object
54 55 56 |
# File 'app/concerns/models/pickable.rb', line 54 def total_linear_feet_by_product_line total_spec_by_product_line(electrical_heating_elements, 'length', 1.to_f / 12) end |
#total_spec_by_product_line(scope, spec, multiplier = 1) ⇒ Object
39 40 41 42 43 44 45 46 |
# File 'app/concerns/models/pickable.rb', line 39 def total_spec_by_product_line(scope, spec, multiplier = 1) r = scope.where(parent_id: nil).pluck(Arel.sql("product_lines.lineage_expanded,items.rendered_product_specifications->'#{spec}'->'raw',quantity")).select { |e| e[1] && e[2] } r.each_with_object({}) do |r, hsh| hsh[r[0]] ||= 0.0 hsh[r[0]] += (r[1].to_f * r[2].to_f) * multiplier hsh[r[0]] = hsh[r[0]].round end end |
#total_watts_by_product_line ⇒ Object
58 59 60 |
# File 'app/concerns/models/pickable.rb', line 58 def total_watts_by_product_line total_spec_by_product_line(electrical_heating_elements, 'watts') end |
#underlayments ⇒ Object
35 36 37 |
# File 'app/concerns/models/pickable.rb', line 35 def underlayments line_items.goods.joins(item: :primary_product_line).underlayments.order('product_lines.lineage_expanded') end |