Class: OrdersController

Overview

Controller: orders.

Constant Summary

Constants included from Controllers::WarehouseStreamActions

Controllers::WarehouseStreamActions::WAREHOUSE_TAB_CONTENT_FRAME

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 CrmFormHelper

#blocking_submit_button, #boolean_select, #datalist, #edit_button, #form_style_attribute_display, #opportunity_local_sales_rep_input, #sales_support_rep_input, #submit_cancel, #technical_rep_input, #technical_rep_sec_input, #yes_no_blank_boolean_collection_for_select, #yes_no_boolean_collection_for_select

Methods included from Controllers::PriceEditable

#price_edit, #price_update

Methods included from Controllers::WarehouseStreamActions

#redirect_to

Methods included from FileDownloadPresenter

#file_download_entry

Methods included from Controllers::Workflowable

#render_workflow_error_stream, #render_workflow_success_stream, #workflow_action, #workflow_action_complete

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, #render_edit_action, #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

#account_managersObject



172
173
174
# File 'app/controllers/orders_controller.rb', line 172

def 
  
end

#account_managers_definedObject



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

def 
  @order.attributes = params[:order]
  authorize!(:update, @order)
  if @order.save
    res = @order.fix_future_release_date
    flash[:info] = res[:message] if res[:message].present?
    # because of the weird way combo_contact sets contacts, and that we can also create an opportunity from the same form, we want to update a new opportunity with the new contact as well... but only if it's new... otherwise let the chips fall... ie you can get into a situation where an order is linked to one contact, but also linked to an opp with another contact... yeesh
    @order.opportunity.update(contact_id: @order.contact_id) if @order.contact && params[:order][:opportunity_attributes] && params[:order][:opportunity_attributes][:name].present? && @order.opportunity
    Rails.logger.debug { "@order.update_shipping_address_with_contact: #{@order.update_shipping_address_with_contact}" }
    if @order.update_shipping_address_with_contact
      Rails.logger.debug '@order.update_shipping_address_with_contact IS TRUE!!!'
      if @order.contact.present?
        @order.shipping_address&.person_name_override = @order.contact.name
        @order.shipping_address&.save
        @order.tracking_email = [@order.contact.email] if @order.contact.email.present? && @order.tracking_email.none?(@order.contact.email)
        @order.shipping_phone = @order.contact.phone
      else # contact reset but ant to update the order shipping address and phone and tracking email, so reset
        @order.shipping_address&.person_name_override = nil
        @order.shipping_address&.save
        @order.set_default_tracking_email(force: true)
        @order.shipping_phone = @order.customer.phone || @order.customer.cell_phone
      end
      @order.save
    end
    redirect_to_return_path_or_default(shipping_address_order_url(@order))
  else
    flash.now[:error] = "There were errors updating the order: #{@order.errors_with_deliveries_errors.to_sentence.capitalize}"
    
    render :account_managers, status: :unprocessable_content
  end
end

#approve_credit_requestObject



1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
# File 'app/controllers/orders_controller.rb', line 1124

def approve_credit_request
  authorize! :approve_credit_request, Order
  @order = Order.find(params[:id])
  @customer = @order.customer
  @billing_entity = @order.billing_entity
  @billing_entity.approve_credit_request(@order.balance, @order)
  @order.approve_credit_request
  flash[:info] = 'Credit available increased and rep notified.'
  redirect_to_return_path_or_default @order
end

#cancelObject



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'app/controllers/orders_controller.rb', line 136

def cancel
  customer = @order.customer
  success = @order.cancel_or_destroy

  if success
    @order = Order.find_by(id: @order.id)
    flash[:info] = if @order
                     'Order has been cancelled.'
                   else
                     'Order was deleted'
                   end
  elsif @order&.errors_with_deliveries_errors&.any?
    flash[:error] = @order.errors_with_deliveries_errors.join(', ')
  end

  if @order
    redirect_to_return_path_or_default order_path(@order)
  else
    redirect_to_return_path_or_default customer_path(customer)
  end
end

#cancel_cod_checkObject



422
423
424
425
426
427
428
# File 'app/controllers/orders_controller.rb', line 422

def cancel_cod_check
  @order.need_to_recalculate_shipping
  @order.update(cod_collection_type: nil)
  result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
  handle_shipping_flash_messaging(result, 'cancel_cod_check')
  redirect_to action: :shipping, id: @order.id, customer_id: @order.customer.id, return_path: @return_path
end

#cancel_cod_no_checkObject



461
462
463
464
465
466
467
# File 'app/controllers/orders_controller.rb', line 461

def cancel_cod_no_check
  @order.need_to_recalculate_shipping
  @order.update(cod_collection_type: nil)
  result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
  handle_shipping_flash_messaging(result, 'cancel_cod_no_check')
  redirect_to action: :shipping, id: @order.id, customer_id: @order.customer.id, return_path: @return_path
end

#choose_shipping_methodObject



469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
# File 'app/controllers/orders_controller.rb', line 469

def choose_shipping_method
  if @order.editing_locked?
    flash[:error] = "Can't choose shipping method on order that is locked. Please CR HOLD to change shipping method."
    redirect_to_return_path_or_default(order_url(@order))
  else
    # puts "choose_shipping_method: params: #{params.inspect}"
    @ship_quotable = @order
    %i[custom_packing_slip_upload master_carton_label_upload manual_ship_label_upload bol_upload].each do |attachment_key|
      next if params.dig(attachment_key, :attachment).blank?

      @order.uploads.build(params[attachment_key])
    end

    # Use service to handle shipping updates and discount recalculation
    # Get the order-specific parameters (empty hash if no order params)
    order_params = shipping_params

    # If there are no order parameters, just redirect (no shipping to update)
    if order_params.empty?
      redirect_to_return_path_or_default(order_url(@order))
      return
    end

    # Pass the order parameters to the service
    result = Shipping::UpdateShippingMethod.new.update_from_params(@order, { order: order_params })

    if result.success?
      # Display any shipping change messages
      result.messages.each { |msg| flash[:info] = msg } if result.messages.present?

      # If "Purchase label early" checkbox was checked, show confirmation message
      # The actual label purchase will happen when the order reaches awaiting_deliveries state
      # (triggered by after_transition hook in Order model)
      manual_label_uploaded = params.dig(:manual_ship_label_upload, :attachment).present?
      if @order.purchase_label_early? && !manual_label_uploaded && @order.walmart_sww_eligible?
        flash[:info] = 'Shipping label will be purchased automatically when the order is released to the warehouse. Tracking will be sent to Walmart immediately.'
      end

      res = @order.fix_future_release_date
      add_to_flash(res[:message]) if res[:message].present?

      if @order.includes_schedulable_service? && @order.smartservice_ticket.nil?
        redirect_to_return_path_or_default(service_order_url(@order))
      else
        @order.pending_payment
        if @order.awaiting_deliveries?
          # already had payment so it went straight to warehouse
          # Check for early label purchase result
          early_label_result = @order.early_label_flash_message
          flash[early_label_result[:type]] = early_label_result[:message] if early_label_result.present?
          redirect_to_return_path_or_default(order_url(@order))
        elsif @order.profit_review?
          flash[:warning] = 'Order profit not met'
          redirect_to_return_path_or_default(order_url(@order))
        else
          redirect_to_return_path_or_default(order_discounts_url(@order))
        end
      end
    else
      flash.now[:error] = result.errors
      render :shipping, status: :unprocessable_content
    end
  end
end

#cod_checkObject



414
415
416
417
418
419
420
# File 'app/controllers/orders_controller.rb', line 414

def cod_check
  @order.need_to_recalculate_shipping
  @order.update(cod_collection_type: 'check')
  result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
  handle_shipping_flash_messaging(result, 'cod_check')
  redirect_to action: :shipping, id: @order.id, customer_id: @order.customer.id, return_path: @return_path
end

#cod_no_checkObject



430
431
432
433
434
435
436
# File 'app/controllers/orders_controller.rb', line 430

def cod_no_check
  @order.need_to_recalculate_shipping
  @order.update(cod_collection_type: 'no_check')
  result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
  handle_shipping_flash_messaging(result, 'cod_no_check')
  redirect_to action: :shipping, id: @order.id, customer_id: @order.customer.id, return_path: @return_path
end

#confirm_serial_numbersObject



915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
# File 'app/controllers/orders_controller.rb', line 915

def confirm_serial_numbers
  @order = Order.find(params[:id])
  if @order.update(params[:order])
    if @order.needs_serial_number_reservation?
      if @order.has_incomplete_reservations?
        flash.now[:error] = 'Serial Number reservation must be completed before releasing'
        load_line_items_requiring_reservation
        render :pick_serial_numbers, status: :unprocessable_content
      else
        @order.release_order
        flash[:info] = 'Order released'
        redirect_to_return_path_or_default order_path(@order)
      end
    else
      redirect_to_return_path_or_default order_path(@order)
    end
  else
    flash.now[:error] = @order.errors_to_s.presence || 'Could not confirm serial numbers.'
    load_line_items_requiring_reservation
    render :pick_serial_numbers, status: :unprocessable_content
  end
end

#contact_lookupObject

Used by tom-select field on order/:id/account_managers
if q is specified a global lookup is run
if party_id is specified, then a single party is looked up (used to initialize form on load typically)



1097
1098
1099
1100
1101
# File 'app/controllers/orders_controller.rb', line 1097

def contact_lookup
  @order = Order.find(params[:id])
  result = Order::ContactLookup.new.process(@order, params)
  render json: { results: result.results, total: result.total_entries }
end

#createObject

PUT /customers/1/orders



114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/controllers/orders_controller.rb', line 114

def create
  @order = Order.new(params[:order].merge(customer_id: @customer.id, shipping_method: @customer.default_shipping_option_name))
  @order.single_origin = true if @customer.is_direct_buy? # patch for now for Direct Buy, must be single origin
  if @order.valid?
    authorize!(:create, @order)
    @order.pending_payment!
    flash[:error] = "Order shipping address not valid or otherwise shipping not set! Can't proceed until it is corrected." unless @order.pending_payment?
    redirect_to_return_path_or_default confirm_and_pay_order_path(@order)
  else
    render action: :new, status: :unprocessable_content
  end
end

#deny_credit_requestObject



1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
# File 'app/controllers/orders_controller.rb', line 1135

def deny_credit_request
  authorize! :deny_credit_request, Order
  @order = Order.find(params[:id])
  @customer = @order.customer
  @billing_entity = @order.billing_entity
  @order.deny_credit_request
  FinancialsMailer.credit_request_denied_notification(@customer, @order.balance, @order).deliver_later
  flash[:info] = 'Credit request denied and rep notified.'
  redirect_to_return_path_or_default @order
end

#destroyObject

DELETE /orders/1



690
691
692
693
694
695
696
697
698
699
700
701
# File 'app/controllers/orders_controller.rb', line 690

def destroy
  parent_customer = @order.customer
  if @order.ok_to_delete?()
    @order.destroy
    flash[:info] = 'Order has been deleted'
    redirect_to customer_path(parent_customer)
  else
    flash[:error] = @order.errors_with_deliveries_errors.join(',')
    redirect_to order_url(@order)

  end
end

#disable_auto_couponObject



176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# File 'app/controllers/orders_controller.rb', line 176

def disable_auto_coupon
  @order = Order.find(params[:id])
  @order.update_attribute!(:disable_auto_coupon, true)
  flash[:info] = 'Auto coupons are now disabled, if any auto coupons were applied you can delete them and they will not come back.'

  respond_to do |format|
    format.turbo_stream do
      @order.reload
      @discounts = @order.discounts.includes(:coupon)
      render turbo_stream: turbo_stream.replace(
        'discount_summary',
        partial: 'discounts/summary',
        locals: { itemizable: @order }
      )
    end
    format.html do
      redirect_to_return_path_or_default(order_discounts_path(@order))
    end
  end
end

#do_moveObject



1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
# File 'app/controllers/orders_controller.rb', line 1188

def do_move
  @order = Order.find(params[:id])
  authorize!(:edit, @order)
  customer = Customer.find(params.dig(:move, :customer_id)) if params.dig(:move, :customer_id).present?
  opportunity = customer.opportunities.find(params.dig(:move, :opportunity_id)) if params.dig(:move, :opportunity_id).present?
  res = Order::Mover.new(@order).move_to(customer:, opportunity:)
  if res.success?
    redirect_to order_path(@order)
  else
    flash.now[:error] = res.errors.to_sentence.capitalize
    render :move, status: :unprocessable_content
  end
end

#edi_force_price_matchObject



1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
# File 'app/controllers/orders_controller.rb', line 1167

def edi_force_price_match
  errs = []
  if @order.edi_price_matcheable?
    authorize!(:edi_force_price_match, @order)
    errs = @order.edi_force_price_match
  else
    errs << 'Order is not an EDI order or already matches the EDI pricing'
  end
  flash.now[:error] = errs.join('. ') if errs.any?

  respond_to do |format|
    format.turbo_stream { render turbo_stream: itemizable_recalculate_streams(@order) }
    format.html { redirect_to_return_path_or_default order_url(@order) }
  end
end

#editObject

GET /customers/1/orders/1/edit



111
# File 'app/controllers/orders_controller.rb', line 111

def edit; end

#edit_sales_support_repObject



703
704
705
706
# File 'app/controllers/orders_controller.rb', line 703

def edit_sales_support_rep
  authorize!(:edit_sales_support_rep, @order)
  @employees = Employee.active.order(:full_name)
end

#enable_auto_couponObject



197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# File 'app/controllers/orders_controller.rb', line 197

def enable_auto_coupon
  @order = Order.find(params[:id])
  @order.disable_auto_coupon = false
  @order.save
  # Recalculate discounts after auto coupons are applied
  @order.reload.reset_discount
  flash[:info] = 'Auto coupons are enabled, if any auto coupons qualified they have been added automatically.'

  respond_to do |format|
    format.turbo_stream do
      @order.reload
      @discounts = @order.discounts.includes(:coupon)
      render turbo_stream: turbo_stream.replace(
        'discount_summary',
        partial: 'discounts/summary',
        locals: { itemizable: @order }
      )
    end
    format.html do
      redirect_to_return_path_or_default(order_discounts_path(@order))
    end
  end
end

#enter_roomsObject



735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
# File 'app/controllers/orders_controller.rb', line 735

def enter_rooms
  params[:order] ||= {}
  params[:order][:room_configuration_ids] ||= []
  new_rc_ids = (params[:order][:room_configuration_ids] || []).uniq.sort

  job_options = {
    order_id: @order.id,
    room_configuration_ids: new_rc_ids,
    redirect_path: order_path(@order, tab: 'rooms')
  }.stringify_keys

  job_id = EnterOrderRoomsWorker.perform_async(job_options)

  if job_id.blank? && (job_id = BackgroundJobStatus.search(worker_klass: 'EnterOrderRoomsWorker', args: job_options).first&.dig(:job_id))
    flash[:warning] = 'A similar job was already enqueued, watching status of that job now'
  end

  if job_id.present?
    redirect_to job_path(job_id)
  else
    flash[:warning] = 'There was a problem queuing the job'
    redirect_to order_path(@order, tab: 'rooms')
  end
end

#exclude_from_payment_checkObject



164
165
166
# File 'app/controllers/orders_controller.rb', line 164

def exclude_from_payment_check
  @order.update(exclude_from_payment_check: true)
end

#fix_catalogObject



253
254
255
256
257
# File 'app/controllers/orders_controller.rb', line 253

def fix_catalog
  @order = Order.find(params[:id])
  flash[:info] = @order.fix_catalog.join('<br>')
  redirect_to_return_path_or_default order_path(@order)
end

#force_invoiced_statusObject



406
407
408
409
410
411
412
# File 'app/controllers/orders_controller.rb', line 406

def force_invoiced_status
  # Method created for admins to force an order to be Invoiced. Usefull for when the order remains in pending payment
  # after all deliveries have been invoiced, due to an error in coupons or shipping. Oscar says it's better to inform
  # accounting to take care of the mismatch than to leave the order in pending_payment
  @order.trigger_invoiced!
  redirect_to order_url(@order)
end

#get_detailsObject



886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
# File 'app/controllers/orders_controller.rb', line 886

def get_details
  id = params[:order_id]
  if id.blank?
    render json: { error: 'not-found' }.to_json, status: :not_found
  else
    order = Order.find(id)
    lines = LineItem.where(resource_type: 'Order', resource_id: order.id).goods
    product_lines = []
    line_collection = []
    lines.each do |l|
      line_collection << { id: l.item.id, sku: l.item.sku, quantity: l.quantity }
      product_lines << l.item.primary_product_line.id unless l.item.primary_product_line.nil?
    end
    render json: { room_ids: order.room_configuration_ids.join(','), items: line_collection, product_lines: product_lines.uniq }
  end
end

#get_spiff_enrollmentsObject



808
809
810
811
812
813
814
815
816
# File 'app/controllers/orders_controller.rb', line 808

def get_spiff_enrollments
  @contact = @customer.contacts.find_by(id: params[:order_spiff_rep_id])
  if @contact && @contact.active_spiff_enrollment.present? && @contact.active_spiff_enrollment.is_active?
    @spiff_enrollments = [@contact.active_spiff_enrollment]
    @order.spiff_enrollment = @contact.active_spiff_enrollment
  else
    @spiff_enrollments = []
  end
end

#include_in_payment_checkObject



168
169
170
# File 'app/controllers/orders_controller.rb', line 168

def include_in_payment_check
  @order.update(exclude_from_payment_check: false)
end

#indexObject

GET /customers/1/orders



23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'app/controllers/orders_controller.rb', line 23

def index
  if context_object
    build_orders_tab_data(context_object)
  else
    @context_object = nil
    @q = Order.none.ransack(params[:q])
    @q.sorts = 'created_at DESC' if @q.sorts.blank?
    @pagy, @orders = pagy(:countish, @q.result)
  end
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#lookupObject



818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
# File 'app/controllers/orders_controller.rb', line 818

def lookup
  q = params[:q].try(:upcase)&.split&.first
  order_id = params[:order_id].presence
  customer_id = params[:customer_id].presence
  state_in = params[:state_in].presence
  if order_id
    order = Order.find(order_id)
    respond_to do |format|
      format.json { render json: { results: { id: order_id, text: "[#{order.reference_number}] #{order.customer.full_name} (#{order.shipped_date.to_fs(:crm_default)})" } } }
      format.html do
        @results = [order]
        render partial: 'orders/lookup_suggestions', locals: { results: @results }
      end
    end
  else
    @results = Order.all
    @results = @results.where(state: state_in) if state_in
    if customer_id
      # You can search outside the customer's order scope if this flag is specified
      if params[:allow_open_order_link].to_b && q.present?
        # if a customer id was specified, we will still limit to orders in the same company id as the original customer
        company_id = Customer.joins(:catalog).where(id: customer_id).pick('catalogs.company_id')
        @results = @results.joins(customer: :catalog).where(Catalog[:company_id].eq(company_id))
      else
        @results = @results.where(customer_id:)
      end
    end
    if q.present?
      @results = if params[:with_wildcard].to_b
                   @results.like_lookup(q)
                 else
                   @results.lookup(q)
                 end
    end
    json_result = TomSelect.format_json_results(self, @results, params[:page].presence, params[:per_page].presence) do |e|
      {
        id: e.id,
        text: e.selection_name,
        label: e.selection_name,
        reference: e.reference_number,
        order_link: order_path(e),
        state: e.state,
        shipped: e.shipped_date.present? ? e.shipped_date.to_fs(:crm_default) : 'not shipped',
        opportunity: e.opportunity&.name,
        customer: e.customer.full_name,
        customer_link: customer_path(e.customer),
        contact_id: e.contact_id,
        contact_full_name: e.contact&.full_name
      }
    end
    respond_to do |format|
      format.json { render json: json_result }
      format.html { render partial: 'orders/lookup_suggestions', locals: { results: @results.limit(20) } }
    end
  end
end

#lookup_rowObject

Return a server-rendered order row for the support case orders table



876
877
878
879
880
881
882
883
884
# File 'app/controllers/orders_controller.rb', line 876

def lookup_row
  o = Order.find_by(id: params[:id])
  if o.nil?
    render html: ''.html_safe, status: :not_found
  else
    # Reuse the shared row partial to guarantee consistent markup
    render partial: 'support_cases/orders_table_row', locals: { o: o, hide_checkbox: false }
  end
end

#moveObject



1183
1184
1185
1186
# File 'app/controllers/orders_controller.rb', line 1183

def move
  @order = Order.find(params[:id])
  authorize!(:edit, @order)
end

#newObject

GET /customers/1/orders/new



74
75
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
# File 'app/controllers/orders_controller.rb', line 74

def new
  @support_case = SupportCase.find(params[:support_case_id]) if params[:support_case_id]
  @opportunity = Opportunity.find(params[:opportunity_id]) if params[:opportunity_id]
  @room_configuration = RoomConfiguration.find(params[:room_configuration_id]) if params[:room_configuration_id]
  @opportunity ||= @room_configuration&.opportunity
  @contact = Contact.find(params[:contact_id]) if params[:contact_id]
  @customer = Customer.find(params[:customer_id]) if params[:customer_id]
  @customer ||= @opportunity&.customer
  @customer ||= @contact&.customer
  @customer ||= @support_case&.customer
  @order = @customer.orders.build(
    customer: @customer,
    opportunity: @opportunity,
    contact: @contact,
    recalculate_shipping: false,
    do_not_detect_shipping: true,
    source: @customer.source_for_opps_and_orders
  )
  @order.order_type = "#{@opportunity.opportunity_type}O" if @opportunity
  @order.order_type = params[:order_type] if params[:order_type]
  @order.support_case_ref = @support_case.case_number if @support_case
  @order.tracking_email << @order.primary_party&.email if @support_case && @order.primary_party&.email.present?
  authorize!(:create, @order)

  # Enabling this code would populate the channel notification emails if we decided to make billing_emails the ulimate authority for where invoices get sent
  # @order.billing_emails = @order.default_billing_emails

  @order.single_origin = true if @customer.try(:is_direct_buy?) # patch for now for Direct Buy, must be single origin
  if @order.save
    redirect_to_return_path_or_default (@order)
  else
    flash[:error] = "Could not create order.  #{@order.errors_with_deliveries_errors.join(', ')}"
    redirect_to_return_path_or_default customer_url(@customer)
  end
end

#pick_serial_numbersObject



909
910
911
912
913
# File 'app/controllers/orders_controller.rb', line 909

def pick_serial_numbers
  @order = Order.find(params[:id])
  @order.auto_reserve_serial_numbers
  load_line_items_requiring_reservation
end

#place_cr_holdObject



577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
# File 'app/controllers/orders_controller.rb', line 577

def place_cr_hold
  res = Order.with_advisory_lock_result("order_#{@order.id}", timeout_seconds: 120) do
    @order.reload
    if @order.can_cr_hold?(current_user)
      if @order.has_early_purchased_label? && @order.early_label_purchased_recently?
        minutes_since = ((Time.current - @order.early_label_purchased_at) / 60).round(1)
        wait_minutes = (Order::EARLY_LABEL_RAPID_VOID_THRESHOLD_MINUTES - minutes_since).ceil
        flash[:warning] = "Early label was purchased #{minutes_since} minutes ago. Please wait #{wait_minutes} more minute(s) before holding/canceling, or void the label manually first."
      else
        @order.cr_hold!
        if @order.in_cr_hold?
          @order.update(is_manual_hold: true)
        else
          flash[:error] = "Order cannot be put into CR hold. #{@order.errors_with_deliveries_errors.to_sentence.capitalize} #{@order.shipping_address.errors_to_s}."
        end
      end
    end
  end
  flash[:error] = "Could not acquire lock on order #{@order}, try again later" unless res.lock_was_acquired?
  if @order.in_cr_hold? && params[:reserve_stock].to_b
    redirect_to pick_lines_inventory_commits_path(resource_type: 'Order', resource_id: @order.id), notice: 'Order is on hold, please select which items you would like to reserve and click Save'
  else
    redirect_to_return_path_or_default order_url(@order)
  end
end

#process_spiffObject



62
63
64
65
66
67
68
69
70
71
# File 'app/controllers/orders_controller.rb', line 62

def process_spiff
  access_denied && return unless &.has_role?('accounting_rep')

  if @order.spiff_paid
    flash[:info] = 'Order spiff status updated successfully'
  else
    flash[:error] = 'Unable to update order spiff status'
  end
  redirect_to_return_path_or_default order_path(@order)
end

#profit_marginsObject



903
904
905
906
907
# File 'app/controllers/orders_controller.rb', line 903

def profit_margins
  @order = Order.find(params[:id])
  authorize! :manage_profit_margins, @order
  render layout: 'crm/crm'
end

#recalculate_discountObject



760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
# File 'app/controllers/orders_controller.rb', line 760

def recalculate_discount
  @order = @customer.orders.find(params[:id])
  @order.reset_discount
  if @order.errors_with_deliveries_errors.any?
    flash.now[:error] = "Problem while recalculating discount: #{@order.errors_with_deliveries_errors.join(', ')}"
  else
    flash.now[:success] = 'Discount recalculated.'
  end

  respond_to do |format|
    format.turbo_stream { render turbo_stream: itemizable_recalculate_streams(@order) }
    format.html do
      redirect_to_return_path_or_default order_url(@order)
    end
  end
end

#recalculate_taxesObject



777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
# File 'app/controllers/orders_controller.rb', line 777

def recalculate_taxes
  if @order.ci_invoice_credit_order?
    flash.now[:warning] = 'Tax recalculation is disabled for credit orders from CI invoices. Taxes are inherited from the original invoice.'
  else
    @order.refresh_tax_rate
    if @order.errors.any?
      flash.now[:error] = "Problem while recalculating taxes: #{@order.errors.full_messages.join(', ')}"
    else
      flash.now[:success] = 'Taxes recalculated.'
    end
  end

  respond_to do |format|
    format.turbo_stream { render turbo_stream: itemizable_recalculate_streams(@order) }
    format.html do
      redirect_to_return_path_or_default order_url(@order)
    end
  end
end

#release_back_orderObject



565
566
567
568
569
570
571
572
573
574
575
# File 'app/controllers/orders_controller.rb', line 565

def release_back_order
  old_state = @order.state
  @order.release_order
  if @order.state == old_state
    flash[:info] = "Order can't be released. #{@order.errors_with_deliveries_errors.to_sentence.capitalize} #{@order.shipping_address.errors_to_s}."
  elsif @order.needs_serial_number_reservation?
    flash[:info] = 'Order released, but requires serial number reservations. Please complete the reservations and save'
    redirect_to(pick_serial_numbers_order_path(@order)) && return
  end
  redirect_to @order
end

#release_cr_holdObject



603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
# File 'app/controllers/orders_controller.rb', line 603

def release_cr_hold
  early_label_result = nil
  res = Order.with_advisory_lock_result("order_#{@order.id}", timeout_seconds: 120) do
    # Reload because we want to make sure the order is still in the right state
    @order.reload
    if @order.can_release_order?
      @order.update(is_manual_hold: false)
      @order.release_order
      if @order.in_cr_hold?
        flash[:error] =
          "Order cannot be released from CR hold. #{@order.hold_order_reasons&.join('. ')} #{@order.errors_with_deliveries_errors.to_sentence.capitalize} #{@order&.shipping_address&.errors&.full_messages&.join('. ')}."
      else
        # Capture early label result for flash message
        early_label_result = @order.early_label_flash_message
      end
    end
  end
  flash[:error] = "Could not acquire lock on order #{@order}, try again later" unless res.lock_was_acquired?

  # Display early label purchase result
  flash[early_label_result[:type]] = early_label_result[:message] if early_label_result.present?

  redirect_to_return_path_or_default order_url(@order)
end

#release_management_holdObject



678
679
680
681
682
# File 'app/controllers/orders_controller.rb', line 678

def release_management_hold
  authorize!(:release_management_hold, @order)
  @order.release_management_hold! if @order.in_management_hold?
  redirect_to_return_path_or_default order_url(@order)
end

#resend_google_offline_conversionObject



641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
# File 'app/controllers/orders_controller.rb', line 641

def resend_google_offline_conversion
  res = Invoicing::GoogleConversionReporter.new.send_new_order_conversion(@order)
  meta = res[:meta] || {}
  # Include reason/status/error to aid debugging when meta is empty
  summary_rows = []
  summary_rows << ['status', meta['result'] || res[:status] || res[:success]]
  summary_rows << ['reason', res[:reason]] if res[:reason]
  summary_rows << ['error', meta['error'] || res[:error]] if (meta['error'] || res[:error]).present?
  pretty_meta = (meta.to_a + summary_rows).map { |key, value| "<strong>#{key}:</strong> #{value}" }.join('<br>')
  rb_debug_link = "<a href='https://appsignal.com/warmlyyours/sites/heatwave-production/errors?search=google' target='_blank'>Debug</a>"

  if res[:success]
    flash[:info] = "Success:<br>#{pretty_meta}<br>" + rb_debug_link
  else
    flash[:error] = "Failed: #{res[:reason] || 'unknown'}<br>#{pretty_meta}<br>" + rb_debug_link
  end
  redirect_to_return_path_or_default order_url(@order)
end

#resend_pinterest_conversionObject



660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
# File 'app/controllers/orders_controller.rb', line 660

def resend_pinterest_conversion
  res = Pinterest::ConversionReporter.new.send_order_conversion(@order)
  meta = res[:meta] || {}
  summary_rows = []
  summary_rows << ['status', meta[:result] || meta['result'] || res[:status] || res[:success]]
  summary_rows << ['reason', res[:reason]] if res[:reason]
  summary_rows << ['error', meta[:error] || meta['error'] || res[:error]] if (meta[:error] || meta['error'] || res[:error]).present?
  pretty_meta = (meta.to_a + summary_rows).map { |key, value| "<strong>#{key}:</strong> #{value}" }.join('<br>')
  debug_link = "<a href='https://appsignal.com/warmlyyours/sites/heatwave-production/errors?search=pinterest' target='_blank'>Debug</a>"

  if res[:success]
    flash[:info] = "Pinterest conversion sent:<br>#{pretty_meta}<br>#{debug_link}".html_safe
  else
    flash[:error] = "Pinterest conversion failed: #{res[:reason] || 'unknown'}<br>#{pretty_meta}<br>#{debug_link}".html_safe
  end
  redirect_to_return_path_or_default order_url(@order)
end

#resend_review_requestObject



628
629
630
631
632
633
634
635
636
637
638
639
# File 'app/controllers/orders_controller.rb', line 628

def resend_review_request
  result = Api::ReviewsIo::InvitationSender.new(order: @order).process
  case result
  when Api::ReviewsIo::InvitationSender::RESULT_SUCCESS
    flash[:info] = 'Reviews.io invitation queued successfully.'
  when Api::ReviewsIo::InvitationSender::RESULT_SKIPPED
    flash[:warning] = 'Reviews.io invitation skipped (order may not be eligible or missing customer email).'
  else
    flash[:error] = 'Reviews.io invitation failed. Check logs for details.'
  end
  redirect_to_return_path_or_default order_url(@order, tab: 'reviews')
end

#restoreObject



158
159
160
161
162
# File 'app/controllers/orders_controller.rb', line 158

def restore
  @order.restore
  flash[:error] = @order.errors_with_deliveries_errors.join(', ') if @order.errors_with_deliveries_errors.any?
  redirect_to_return_path_or_default order_path(@order)
end

#review_credit_requestObject



1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
# File 'app/controllers/orders_controller.rb', line 1103

def review_credit_request
  authorize! :review_credit_request, Order
  @order = Order.find(params[:id])
  @customer = @order.customer
  @billing_entity = @order.billing_entity
  return unless !@order.credit_request_pending? || @order.editing_locked?

  if @order.credit_request_approved?
    notice = 'Credit request has already been approved.'
  elsif @order.credit_request_denied?
    notice = 'Credit request has already been denied.'
  elsif @order.credit_request_cancelled?
    notice = 'Credit request has already been cancelled.'
  elsif @order.no_credit_requested?
    notice = 'No credit has been requested for this order.'
  elsif @order.editing_locked?
    notice = 'Order has already been processed.'
  end
  redirect_to(@order, notice:)
end

#review_echeck_requestObject



1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
# File 'app/controllers/orders_controller.rb', line 1146

def review_echeck_request
  authorize! :review_echeck_request, Order
  @order = Order.find(params[:id])
  @customer = @order.customer
  @billing_entity = @order.billing_entity
  @echeck_payment = @order.echecks_requiring_authorization.sum(&:amount)
  return unless !@order.pending_release_authorization? || @order.editing_locked?

  redirect_to(@order, notice: 'Order has already been processed.')
end

#roomsObject



724
725
726
727
728
729
730
731
732
733
# File 'app/controllers/orders_controller.rb', line 724

def rooms
  # Load all available room configurations (client-side filtering via Stimulus)
  # Get all available room configurations from order and opportunity
  available_rc_ids = (@order.room_configuration_ids + (@order.opportunity&.room_configuration_ids || [])).uniq

  # Use preload to avoid N+1 queries - loads associations in separate queries
  @room_configurations = RoomConfiguration.where(id: available_rc_ids)
                                          .order('room_configurations.updated_at DESC, room_configurations.name')
                                          .preload(:room_layout_images, :controlled_rooms, :controlled_by)
end


1157
1158
1159
1160
1161
1162
1163
1164
1165
# File 'app/controllers/orders_controller.rb', line 1157

def send_public_payment_link
  email = params[:communication][:email]
  @order = Order.find(params[:id])
  comm = CommunicationBuilder.new(resource: @order,
                                  template_system_code: 'ORDER_PAYMENT').to_params

  comm = comm.merge(emails: email) unless email.nil?
  redirect_to new_communication_path(comm)
end

#send_tracking_emailObject



797
798
799
800
801
802
803
804
805
806
# File 'app/controllers/orders_controller.rb', line 797

def send_tracking_email
  order = Order.find(params[:id])
  result = if order.is_warehouse_pickup?
             order.send_ready_for_pickup_email
           else
             order.send_tracking_email
           end
  flash[:info] = result[:status_message] if result[:status_message].present?
  redirect_to_return_path_or_default order_url(order)
end

#serviceObject



339
340
341
342
343
344
345
346
347
348
349
# File 'app/controllers/orders_controller.rb', line 339

def service
  if @order.includes_schedulable_service?
    @support_case = @order.smartservice_ticket
    @support_case ||= SupportCase.new(case_type: 'SmartService')
    @addresses = @customer.addresses.order(:company_name_override, :person_name_override, :state_code, :street1)
    @new_address = @customer.addresses.new
  else
    flash[:error] = 'This order does not contain any schedulable services.'
    redirect_to_return_path_or_default(order_url(@order))
  end
end

#set_shipping_addressObject



270
271
272
273
274
275
276
277
278
279
280
281
282
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
309
310
311
312
313
314
315
316
317
318
319
320
# File 'app/controllers/orders_controller.rb', line 270

def set_shipping_address
  set_new_address
  @block_address_edit = @order.editing_locked? || (@order.has_authorized_payment? && !@context_user.manager?)
  if @block_address_edit
    flash.now[:error] = 'Shipping address cannot be updated because the order has pending authorized pre-payments. Void the payment first and re-authorize payment afterwards.'
    render :shipping_address, status: :unprocessable_content
  else
    # Intercept address id
    address_id = params[:order]&.delete(:shipping_address_id)
    @order.attributes = params[:order] if params[:order].present?
    validate_new_address = false
    if address_id.to_i > 0
      # Address book pick
      @order.shipping_address_id = address_id.to_i
    elsif @order.shipping_address&.one_time_only
      # update the one time address
      @order.shipping_address.attributes = params[:address]
      @new_address = @order.shipping_address
      @new_address&.save
      validate_new_address = true
    elsif params[:address].present?
      params[:address].delete(:party_id) unless params[:address][:party_id] && (params[:address][:party_id].to_i > 0)
      @new_address = Address.create(params[:address])
      @order.shipping_address = @new_address
      validate_new_address = true
    end
    if ((validate_new_address && @new_address.errors.blank?) || !validate_new_address) && @order.save
      save_shipping_as_billing_address if params[:save_as_billing_address] == '1' && @customer.billing_address.nil?

      if @order.ships_freight_but_address_not_freight_ready?
        check_freight_fields_and_redirect
      else
        result = @order.last_shipping_rate_request_result

        handle_shipping_flash_messaging(result)
        next_step_url = pick_items_path(order_id: @order.id, return_path: shipping_order_path(@order))

        if @customer.addresses.count <= 1 && !@new_address&.new_record? && @new_address.country_iso3 && (@new_address.country_iso3 != @customer.catalog.country_iso3) && (cat = Store.find_by(country_iso3: @new_address.country_iso3)&.primary_catalog)
          flash[:error] =
            "Customer's first address country of #{@new_address.country.printable_name} does not match customer's existing catalog country of #{@customer.catalog.country.printable_name}!"
          redirect_to edit_catalog_customer_path(@customer, new_catalog_id: cat.id, return_path: @return_path || next_step_url)
        else
          redirect_to_return_path_or_default next_step_url
        end
      end
    else
      flash.now[:error] = @order.errors_to_s.presence || 'Could not update shipping address.'
      render :shipping_address, status: :unprocessable_content
    end
  end
end

#setup_serviceObject



351
352
353
354
355
356
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
# File 'app/controllers/orders_controller.rb', line 351

def setup_service
  ticket_options = params[:service_creation_option]
  existing_ticket = params[:existing_support_case]

  unless ticket_options == 'skip_support_case'
    if ticket_options == 'existing_support_case'
      sc = SupportCase.find(existing_ticket)
      sc.update(params[:support_case].except(:service_address_id))
      sc.convert_to_stk
      sc.room_configuration_ids = @order.room_configuration_ids if @order.room_configuration_ids.any?
      sc.order_ids = [@order.id]
    elsif ticket_options == 'new_support_case'
      sc = SupportCase.new(params[:support_case])
      sc.case_type = 'SmartService'
      sc.send(:set_case_number)
      sc.state = 'pending_service_payment'
      sc.priority = 'Low'
      sc.build_participants_and_rooms(order_id: @order.id)
    end

    if !@order.is_remote_service? && (params[:support_case][:service_address_id].to_i == 0)
      address = @customer.addresses.new(params[:address])
      address.save
      sc.service_address_id = address.id
    end
    service = @order.line_items.smartservice_items.map { |a| a.item.name }.join('; ')
    descript = '<p>Customer requested: </p>'
    service.split(';').each do |ser|
      descript += "<li>#{ser}</li>"
    end
    descript += "<br><p>Preferred time frame is <b>#{sc.preferred_service_timeframe}</b></p>"
    sc.description = descript
    sc.service = service
  end

  if ticket_options == 'skip_support_case' || sc.try(:save)
    @order.pending_payment
    if @order.awaiting_deliveries?
      # already had payment so it went straight to warehouse
      # Check for early label purchase result
      early_label_result = @order.early_label_flash_message
      flash[early_label_result[:type]] = early_label_result[:message] if early_label_result.present?
      redirect_to_return_path_or_default(order_url(@order))
    elsif @order.profit_review?
      flash[:warning] = 'Order profit not met'
      redirect_to_return_path_or_default(order_url(@order))
    else
      redirect_to_return_path_or_default(order_discounts_url(@order))
    end
  else
    flash.now[:error] = sc&.errors&.full_messages&.to_sentence.presence || 'Could not save support case for service order.'
    render :service, status: :unprocessable_content
  end
end

#ship_confirm_service_deliveriesObject



684
685
686
687
# File 'app/controllers/orders_controller.rb', line 684

def ship_confirm_service_deliveries
  @order.deliveries.select(&:service_ready_to_fulfill?).each(&:individual_auto_ship_confirm)
  redirect_to_return_path_or_default order_path(@order)
end

#shippingObject



322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
# File 'app/controllers/orders_controller.rb', line 322

def shipping
  @ship_quotable = @order
  if @order.shipping_address
    @order.label_instructions ||= @order.po_number.to_s
    phone_party = @order.contact || @order.customer
    @order.shipping_phone ||= phone_party.try(:phone) || phone_party.try(:cell_phone)
    @order.set_default_tracking_email
    if params[:shipping_updated]
      result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
      handle_shipping_flash_messaging(result, params[:shipping_update_type])
    end
  else
    flash[:error] = 'Please specify an address before looking at shipping costs.'
    redirect_to_return_path_or_default shipping_address_order_path(@order)
  end
end

#shipping_addressObject



259
260
261
262
263
264
265
266
267
268
# File 'app/controllers/orders_controller.rb', line 259

def shipping_address
  @order.shipping_address_id = @order.customer.try(:shipping_address_id) || @order.customer.addresses.first.try(:id) if @order.shipping_address_id.nil?
  set_new_address
  if @order.shipping_phone.blank?
    party = @order.contact || @order.customer
    @order.shipping_phone = party.phone || party.cell_phone
    @order.shipping_phone ||= @order.customer.phone || @order.customer.cell_phone
  end
  @block_address_edit = @order.editing_locked? || (@order.has_authorized_payment? && !@context_user.manager?)
end

#showObject

GET /customers/1/orders/1
Accepts ID or reference_number (e.g., SO123456)



40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/controllers/orders_controller.rb', line 40

def show
  if params[:shipping_updated]
    result = eval(@order.last_shipping_rate_request_result.to_s) # rubocop:disable Security/Eval
    handle_shipping_flash_messaging(result, params[:shipping_update_type])
  end
  # Reviews.io data is loaded lazily when reviews tab is viewed (see tab_reviews)
  respond_to do |format|
    format.html do
      # material_alerts
      @flag = @order.shipping_address.try(:country).try(:iso).try(:downcase)
    end
  end
end

#splitObject



534
# File 'app/controllers/orders_controller.rb', line 534

def split; end

#split_back_order_allObject



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

def split_back_order_all
  Order.transaction do
    @order.back_order!
  end
  redirect_to(order_url(@order))
end

#split_selectedObject



536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
# File 'app/controllers/orders_controller.rb', line 536

def split_selected
  order_splitter = Order::Splitter.new(@order)
  order_splitter.split_selected(params[:line_items], params[:txid])
  case order_splitter.last_status
  when :ok
    new_order = order_splitter.new_order
    flash[:info] =
      "Order has been split and partly back ordered. Split Back Order: <a href = '#{order_url(new_order)}'>#{new_order.reference_number}</a> (current status: #{new_order.state}). <br/>Please confirm shipping option for current order, then release from CR Hold."
    if @order.service_only_order?
      redirect_to order_discounts_url(@order)
    else
      redirect_to shipping_order_url(@order)
    end
  when :invalid, :error
    @order.errors.add(:base, order_splitter.last_message)
    render :split, status: :unprocessable_content
  else
    flash[:error] = 'Unknown return status for split action'
    redirect_to order_path(@order)
  end
end

#tab_activitiesObject



1009
1010
1011
1012
1013
1014
1015
1016
# File 'app/controllers/orders_controller.rb', line 1009

def tab_activities
  authorize!(:read, @order)
  build_activities_tab_data(@order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_attachmentsObject



982
983
984
985
986
987
988
989
# File 'app/controllers/orders_controller.rb', line 982

def tab_attachments
  authorize!(:read, @order)
  build_uploads_tab_data(@order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_communicationsObject



991
992
993
994
995
996
997
998
# File 'app/controllers/orders_controller.rb', line 991

def tab_communications
  authorize!(:read, @order)
  build_communications_tab_data(@order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_contactsObject



954
955
956
957
958
959
960
# File 'app/controllers/orders_controller.rb', line 954

def tab_contacts
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_credit_memosObject



1068
1069
1070
1071
1072
1073
1074
# File 'app/controllers/orders_controller.rb', line 1068

def tab_credit_memos
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_edi_documentsObject



1051
1052
1053
1054
1055
1056
1057
1058
# File 'app/controllers/orders_controller.rb', line 1051

def tab_edi_documents
  authorize!(:read, @order)
  @edi_communication_logs = @order.edi_communication_logs.order(created_at: :desc)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_inventoryObject



1034
1035
1036
1037
1038
1039
1040
1041
# File 'app/controllers/orders_controller.rb', line 1034

def tab_inventory
  authorize!(:read, @order)
  @item_groups = Inventory::OrderedRemainingItems.new.process(@order, nest_kit_components: true).item_groups
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_invoicesObject



1043
1044
1045
1046
1047
1048
1049
# File 'app/controllers/orders_controller.rb', line 1043

def tab_invoices
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_line_itemsObject



938
939
940
941
942
943
944
# File 'app/controllers/orders_controller.rb', line 938

def tab_line_items
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_mainObject



946
947
948
949
950
951
952
# File 'app/controllers/orders_controller.rb', line 946

def tab_main
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_marketingObject



1086
1087
1088
1089
1090
1091
1092
# File 'app/controllers/orders_controller.rb', line 1086

def tab_marketing
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_material_alertsObject



54
55
56
57
58
59
60
# File 'app/controllers/orders_controller.rb', line 54

def tab_material_alerts
  @material_alerts = @order.get_material_alerts if @order && @order.id.present?
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_paymentsObject



1018
1019
1020
1021
1022
1023
1024
# File 'app/controllers/orders_controller.rb', line 1018

def tab_payments
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_reviewsObject



1076
1077
1078
1079
1080
1081
1082
1083
1084
# File 'app/controllers/orders_controller.rb', line 1076

def tab_reviews
  authorize!(:read, @order)
  # Fetch Reviews.io reviews for this order (only when reviews tab is viewed)
  prepare_order_reviews_io_data
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_rmasObject



1060
1061
1062
1063
1064
1065
1066
# File 'app/controllers/orders_controller.rb', line 1060

def tab_rmas
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_roomsObject



970
971
972
973
974
975
976
977
978
979
980
# File 'app/controllers/orders_controller.rb', line 970

def tab_rooms
  authorize!(:read, @order)
  @opportunity = @order.opportunity
  @editing_locked = @order.editing_locked?
  @return_path = order_path(@order, tab: 'rooms')
  build_rooms_tab_data(@order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_shippingObject



962
963
964
965
966
967
968
# File 'app/controllers/orders_controller.rb', line 962

def tab_shipping
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_smsObject



1000
1001
1002
1003
1004
1005
1006
1007
# File 'app/controllers/orders_controller.rb', line 1000

def tab_sms
  authorize!(:read, @order)
  build_sms_tab_data(@order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#tab_ticketsObject



1026
1027
1028
1029
1030
1031
1032
# File 'app/controllers/orders_controller.rb', line 1026

def tab_tickets
  authorize!(:read, @order)
  respond_to do |format|
    format.html { render layout: should_render_layout? }
    format.turbo_stream
  end
end

#updateObject

PUT /customers/1/orders/1



128
129
130
131
132
133
134
# File 'app/controllers/orders_controller.rb', line 128

def update
  if @order.update(params[:order])
    redirect_to_return_path_or_default order_url(@order)
  else
    render action: :edit, status: :unprocessable_content
  end
end

#update_profit_marginsObject



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

def update_profit_margins
  authorize!(:manage_profit_margins, @order)

  if order_profit_margins_params.to_hash['min_profit_markup'].present?
    if @order.update(order_profit_margins_params)
      if @order.can_release_from_profit_review?
        flash[:info] = 'Order released from profit review'
        @order.release_from_profit_review # Try to release
      elsif @order.profit_review?
        flash[:error] = 'Minimum profit margins not met to allow release from profit review'
      end

      redirect_to_return_path_or_default order_path(@order)
    else
      flash.now[:error] = @order.errors_to_s.presence || 'Could not update profit margins.'
      render :profit_margins, status: :unprocessable_content
    end
  else
    flash.now[:error] = 'Minimum profit margins cannot be empty'
    render :profit_margins, status: :unprocessable_content
  end
end

#update_sales_support_repObject



708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
# File 'app/controllers/orders_controller.rb', line 708

def update_sales_support_rep
  authorize!(:edit_sales_support_rep, @order)

  if @order.update_sales_support_rep(
      params[:order][:sales_support_rep_id],
      params[:order][:sales_support_commission_date]
    )
    flash[:info] = 'Sales support rep and commission date have been updated successfully.'
    redirect_to_return_path_or_default order_path(@order)
  else
    flash.now[:error] = "Unable to update sales support rep: #{@order.errors.full_messages.join(', ')}"
    @employees = Employee.active.order(:full_name)
    render :edit_sales_support_rep, status: :unprocessable_content
  end
end