Class: QuotesController

Inherits:
CrmController show all
Includes:
Controllers::Workflowable, Crm::ShippingParams, CrmFormHelper
Defined in:
app/controllers/quotes_controller.rb

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 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::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, #initialize_crm_lazy_chunks, #record_not_found, #redirect_to_job_or_fallback, #render_edit_action, #set_context, #set_download_path, #stash_file_for_temp_download

Methods inherited from ApplicationController

#account_impersonated?, #add_to_flash, #append_token, #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::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, #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_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!, #authenticate_account_from_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, #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, #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

#build_communicationObject



135
136
137
138
139
# File 'app/controllers/quotes_controller.rb', line 135

def build_communication
  pdfs = Upload.where(id: params[:upload_ids])
  cb = CommunicationBuilder.new(resource: @quote, current_user:, uploads: pdfs)
  redirect_to new_communication_path(cb.to_params)
end

#cancelObject



419
420
421
422
423
# File 'app/controllers/quotes_controller.rb', line 419

def cancel
  @quote.cancel if @quote.cancelable?
  flash[:error] = @quote.errors_to_s if @quote.errors.any?
  redirect_to_return_path_or_default quote_path(@quote)
end

#complete_upgradeObject



401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
# File 'app/controllers/quotes_controller.rb', line 401

def complete_upgrade
  room_configs = []
  params[:quote][:line_items].each do |li_id, li_attrs|
    if li_attrs[:resource_id].present?
      @quote.line_items.find(li_id).update(li_attrs)
      room_configs << li_attrs[:resource_id]
    end
  end
  room_configs = room_configs.uniq
  room_configs.each do |r|
    rc = @opportunity.room_configurations.find(r)
    @quote.room_configurations << rc unless @quote.room_configurations.include?(rc)
    rc.update(requires_upgrade: false)
  end
  @quote.update(requires_upgrade: false)
  redirect_to quote_path(@quote)
end

#convert_to_orderObject

PUT /customers/1/opportunities/1/quotes/1/convert_to_order



277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# File 'app/controllers/quotes_controller.rb', line 277

def convert_to_order
  if @quote.can_convert?
    @quote.refresh_tax_rate
    @quote.reload

    order = @quote.to_order(nil, params[:txid]) do |order|
      # If user cannot create this type of order we explode right here
      authorize!(:create, order)
    end
    if order.new_record?
      flash[:error] = "Unable to convert to order. #{order.errors_to_s}"
      redirect_to quote_path(@quote)
    else
      redirect_to((order))
    end
  else
    errors = @quote.convert_to_order_service.errors || []
    flash[:error] = errors.to_sentence.capitalize
    redirect_to quote_path(@quote)
  end
end

#copyObject



581
582
583
584
585
# File 'app/controllers/quotes_controller.rb', line 581

def copy
  @quote = Quote.find(params[:id])
  @opportunity = @quote.opportunity
  @customer = @quote.customer
end

#createObject

PUT /customers/1/opportunities/1/quotes



60
61
62
63
64
65
66
67
68
# File 'app/controllers/quotes_controller.rb', line 60

def create
  @quote = @opportunity.quotes.new(params[:quote])
  @quote.single_origin = true if @opportunity.customer.is_direct_buy? # patch for now for Direct Buy, must be single origin
  if @quote.save
    redirect_to rooms_quote_path(@quote)
  else
    render action: :new, status: :unprocessable_entity
  end
end

#current_support_caseObject



534
535
536
# File 'app/controllers/quotes_controller.rb', line 534

def current_support_case
  SupportCase.find_by(id: params[:support_case_id])
end

#destroyObject

DELETE /customers/1/opportunities/1/quotes/1



82
83
84
85
86
87
88
89
90
91
92
93
94
# File 'app/controllers/quotes_controller.rb', line 82

def destroy
  if @quote.ok_to_delete?
    @quote.destroy
    redirect_to opportunity_path(@opportunity)
  else
    deps = []
    deps += @quote.dependents[:orders].first(5).map { |o| view_context.link_to(o.reference_number, order_path(o)) }
    deps += @quote.dependents[:children].map { |q| view_context.link_to(q.reference_number, quote_path(q)) }
    deps += @quote.dependents[:communications].map { |c| view_context.link_to("Communication #{c.id}", communication_path(c)) }
    flash[:error] = 'Quote cannot be deleted because it is referenced by: ' + deps.join(', ').html_safe
    redirect_to quote_path(@quote)
  end
end

#disable_auto_couponObject



326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
# File 'app/controllers/quotes_controller.rb', line 326

def disable_auto_coupon
  @quote = Quote.find(params[:id])
  @quote.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
      render turbo_stream: turbo_stream.replace(
        'discount_summary',
        partial: 'discounts/summary',
        locals: { itemizable: @quote }
      )
    end
    format.html do
      redirect_to_return_path_or_default(quote_discounts_path(@quote))
    end
  end
end

#do_copyObject



587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
# File 'app/controllers/quotes_controller.rb', line 587

def do_copy
  if (opportunity_id = params.dig(:copy, :opportunity_id)).present?
    opportunity_copying_to = Opportunity.find(opportunity_id)
  end

  if opportunity_copying_to

    job_options = { opportunity_id: opportunity_copying_to.id, quote_id: @quote.id, notify_email: current_user.email }.stringify_keys
    job_id = QuoteCopyWorker.perform_async(job_options)
    if job_id.blank? && job_id = BackgroundJobStatus.search(worker_klass: 'QuoteCopyWorker', 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_return_path_or_default opportunity_path(@opportunity)
    end
  else
    flash.now[:error] = 'Invalid opportunity selected'
    @opportunity = @quote.opportunity
    @customer = @quote.customer
    render :copy, status: :unprocessable_entity
  end
end

#do_moveObject



557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
# File 'app/controllers/quotes_controller.rb', line 557

def do_move
  if (opportunity_id = params.dig(:move, :opportunity_id)).present?
    opportunity_moving_to = Opportunity.find(opportunity_id)
  end

  if opportunity_moving_to
    qm = Quote::Mover.new(@quote)
    if qm.move_to(opportunity_moving_to)
      flash[:info] = 'Quote has been moved successfully'
      redirect_to opportunity_path(opportunity_moving_to)
    else
      flash.now[:error] = qm.results.join(',')
      @opportunity = @quote.opportunity
      @customer = @quote.customer
      render :move, status: :unprocessable_entity
    end
  else
    flash.now[:error] = 'Invalid opportunity selected'
    @opportunity = @quote.opportunity
    @customer = @quote.customer
    render :move, status: :unprocessable_entity
  end
end

#editObject

GET /customers/1/opportunities/1/quotes/1/edit



55
56
57
# File 'app/controllers/quotes_controller.rb', line 55

def edit
  @addresses = @opportunity.customer.addresses.map { |a| [a.full_address, a.id] }
end

#enable_auto_couponObject



345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
# File 'app/controllers/quotes_controller.rb', line 345

def enable_auto_coupon
  @quote = Quote.find(params[:id])
  @quote.disable_auto_coupon = false
  @quote.save
  # Recalculate discounts after auto coupons are applied
  @quote.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
      render turbo_stream: turbo_stream.replace(
        'discount_summary',
        partial: 'discounts/summary',
        locals: { itemizable: @quote }
      )
    end
    format.html do
      redirect_to_return_path_or_default(quote_discounts_path(@quote))
    end
  end
end

#enter_roomsObject



241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
# File 'app/controllers/quotes_controller.rb', line 241

def enter_rooms
  params[:quote] ||= {}
  new_rc_ids = (params[:quote][:room_configuration_ids] || []).uniq.sort
  create_new_room = (params[:commit] == 'create_new_room')

  # If creating new room, skip job processing and just redirect
  # User will be warned on client-side if they have unsaved changes
  if create_new_room
    redirect_to new_opportunity_room_configuration_path(@opportunity, quote_id: @quote.id, return_path: rooms_quote_path(@quote))
    return
  end

  # Process room changes via background job
  job_options = {
    quote_id: @quote.id,
    room_configuration_ids: new_rc_ids,
    opportunity_name: params.dig(:quote, :opportunity_attributes, :name),
    quote_suffix: params.dig(:quote, :suffix),
    redirect_path: quote_path(@quote, tab: 'rooms')
  }.stringify_keys

  job_id = EnterQuoteRoomsWorker.perform_async(job_options)

  if job_id.blank? && (job_id = BackgroundJobStatus.search(worker_klass: 'EnterQuoteRoomsWorker', 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 quote_path(@quote, tab: 'rooms')
  end
end

#enter_shippingObject



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

def enter_shipping
  # Use service to handle shipping updates and discount recalculation
  # Service handles the update internally
  # Get the quote-specific parameters (empty hash if no quote params)
  quote_params = shipping_params

  # If there are no quote parameters, just redirect (no shipping to update)
  if quote_params.empty?
    @quote.ready_to_transmit if @quote.can_ready_to_transmit?
    redirect_to_return_path_or_default quote_path(@quote)
    return
  end

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

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

    @quote.ready_to_transmit if @quote.can_ready_to_transmit?
    redirect_to_return_path_or_default quote_path(@quote)
  else
    flash.now[:error] = result.errors
    @ship_quotable = @quote
    render :shipping, status: :unprocessable_entity
  end
end

#fix_catalogObject



299
300
301
302
303
# File 'app/controllers/quotes_controller.rb', line 299

def fix_catalog
  @quote = Quote.find(params[:id])
  flash[:info] = @quote.fix_catalog.join('<br>')
  redirect_to quote_path(@quote)
end

#generate_pdfObject



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

def generate_pdf
  options = (params[:select_quote_template] || {}).to_h.transform_values(&:to_b)
  job_id = GenerateQuoteWorker.perform_async({ quote_id: @quote.id,
                                               current_user_id: @context_user.id,
                                               quote_options: options }.deep_stringify_keys)
  if job_id
    redirect_to job_path(job_id)
  else
    flash[:error] = 'Job could not be queued or did not provide status, perhaps a duplicate'
    redirect_to_return_path_or_default quote_path(@quote)
  end
end

#indexObject



10
11
12
13
14
15
16
17
18
19
20
# File 'app/controllers/quotes_controller.rb', line 10

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


126
127
128
129
# File 'app/controllers/quotes_controller.rb', line 126

def link
  token = Encryption.encrypt_string(@quote.id.to_s)
  @url = "https://#{WEB_HOSTNAME}/quote-viewer/#{token}"
end

#lookupObject



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

def lookup
  q = params[:q].try(:upcase)&.split&.first
  quote_id = params[:quote_id]
  if quote_id
    quote = Quote.find(quote_id)
    render json: { results: { id: quote_id, text: "[#{quote.reference_number}] #{quote.customer.full_name} (#{quote.created_at.to_fs(:crm_default)})" } }
  else
    page = params[:page] || 1
    per_page = params[:per_page] || 10
    @results = params[:with_wildcard].present? ? Quote.like_lookup(q) : Quote.lookup(q)
    @pagy, @results = pagy(@results.joins(opportunity: :customer).includes(opportunity: :customer), page: page, limit: per_page)

    total_entries = @pagy.count
    results_array = @results.map do |e|
      { id: e.id,
        text: "[#{e.reference_number}] #{e.customer.full_name} (#{e.created_at.to_fs(:crm_date_only)})",
        label: "[#{e.reference_number}] #{e.customer.full_name} (#{e.created_at.to_fs(:crm_date_only)})",
        reference: e.reference_number,
        quote_link: quote_path(e),
        state: e.state,
        created_at: e.created_at.to_fs(:crm_date_only),
        opportunity: e.opportunity&.name,
        customer: e.customer.full_name,
        customer_link: customer_path(e.customer) }
    end
    render json: { results: results_array, total: total_entries }
  end
end

#lookup_rowObject

Return a server-rendered quote row for the support case quotes table



525
526
527
528
529
530
531
532
# File 'app/controllers/quotes_controller.rb', line 525

def lookup_row
  q = Quote.find_by(id: params[:id])
  if q.nil?
    render html: ''.html_safe, status: :not_found
  else
    render partial: 'support_cases/quotes_table_row', locals: { q: q, hide_checkbox: false, support_case: current_support_case }
  end
end

#lookup_suffixObject



538
539
540
541
542
543
544
545
546
547
548
549
# File 'app/controllers/quotes_controller.rb', line 538

def lookup_suffix
  q = params[:q]
  @pagy, @results = pagy(ViewQuoteSuffix.lookup(q), limit: params[:per_page])
  total_entries = @pagy.count
  results_array = @results.map do |e|
    {
      id: e.suffix,
      text: e.suffix
    }
  end
  render json: { results: results_array, total: total_entries }
end

#moveObject



551
552
553
554
555
# File 'app/controllers/quotes_controller.rb', line 551

def move
  @quote = Quote.find(params[:id])
  @opportunity = @quote.opportunity
  @customer = @quote.customer
end

#newObject

GET /customers/1/opportunities/1/quotes/new



46
47
48
49
50
51
52
# File 'app/controllers/quotes_controller.rb', line 46

def new
  def_cp_ids = []
  def_email = @opportunity.primary_party.contact_points.emails.first || @opportunity.primary_party.customer.contact_points.emails.first
  def_cp_ids << def_email.id if def_email.present?
  @quote = @opportunity.quotes.new(buying_group_id: @opportunity.buying_group_id, contact_point_ids: def_cp_ids, hold_for_transmission: @customer.hold_quotes_for_transmission_default?)
  @quote.single_origin = true if @opportunity.customer.is_direct_buy? # patch for now for Direct Buy, must be single origin
end

#profit_marginsObject



430
431
432
433
434
# File 'app/controllers/quotes_controller.rb', line 430

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

#recalculate_discountObject



367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
# File 'app/controllers/quotes_controller.rb', line 367

def recalculate_discount
  @quote.reset_discount
  if @quote.errors.present?
    flash.now[:error] = "Problem while recalculating discount: #{@quote.errors_to_s}"
  else
    flash.now[:success] = 'Discount recalculated.'
  end

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

#recalculate_taxesObject



383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
# File 'app/controllers/quotes_controller.rb', line 383

def recalculate_taxes
  @quote.refresh_tax_rate
  if @quote.errors.present?
    flash.now[:error] = "Problem while recalculating taxes: #{@quote.errors_to_s}"
  else
    flash.now[:success] = 'Taxes recalculated.'
  end

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

#requoteObject



305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
# File 'app/controllers/quotes_controller.rb', line 305

def requote
  job_options = {
    quote_id: @quote.id,
    rma_id: params[:rma_id],
    redirect_path: quote_path(@quote)
  }.stringify_keys

  job_id = RequoteWorker.perform_async(job_options)

  if job_id.blank? && (job_id = BackgroundJobStatus.search(worker_klass: 'RequoteWorker', 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 quote_path(@quote)
  end
end

#restoreObject



425
426
427
428
# File 'app/controllers/quotes_controller.rb', line 425

def restore
  @quote.restore if @quote.cancelled?
  redirect_to_return_path_or_default quote_path(@quote)
end

#roomsObject



233
234
235
236
237
238
239
# File 'app/controllers/quotes_controller.rb', line 233

def rooms
  # Load all room configurations (client-side filtering via Stimulus)
  # Use preload to avoid N+1 queries - loads associations in separate queries
  @room_configurations = @opportunity.room_configurations
                                     .order('room_configurations.updated_at DESC, room_configurations.name')
                                     .preload(:room_layout_images, :controlled_rooms, :controlled_by)
end

#select_pdf_templateObject



101
102
103
104
105
106
107
108
109
110
111
# File 'app/controllers/quotes_controller.rb', line 101

def select_pdf_template
  # This simple check will make sure our quote in awaiting transmission get a final profit check
  @quote.review_profit if @quote.can_review_profit?

  return unless @quote.profit_review?

  flash[:error] = 'Profit margins not met, quote cannot be sent'
  redirect_to quote_path(@quote)

  # @templates = @quote.template_options
end

#set_shipping_addressObject



145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/controllers/quotes_controller.rb', line 145

def set_shipping_address
  set_new_address
  # Intercept address id
  quote_params = params[:quote]&.to_h || {}
  address_params = params[:address]&.to_h || {}
  address_id = quote_params.delete(:shipping_address_id)
  @quote.attributes = quote_params

  if address_id == 'on'
    # No shipping address
    @quote.shipping_address_id = nil
  elsif address_id.to_i > 0
    # Address book pick
    @quote.shipping_address_id = address_id.to_i
  elsif @quote.shipping_address && @quote.shipping_address.one_time_only
    # update the one time address
    @quote.shipping_address.attributes = address_params
    @new_address = @quote.shipping_address
  elsif address_params.present?
    address_params.delete(:party_id) unless address_params[:party_id].to_i > 0
    @quote.build_shipping_address(address_params)
    @new_address = @quote.shipping_address
  end

  if @quote.save
    if @quote.ships_freight_but_address_not_freight_ready?
      check_freight_fields_and_redirect
    else
      result = begin
        eval(@quote.last_shipping_rate_request_result.to_s)
      rescue StandardError
        nil
      end
      handle_shipping_flash_messaging(result) if result.present?
      @quote.ready_to_transmit if @quote.is_warehouse_pickup? and @quote.pre_pack?
      if @customer.addresses.count <= 1 && !@new_address&.new_record? && @new_address.country_iso3 && (@new_address.country_iso3 != @customer.catalog.country_iso3)
        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, return_path: @return_path || shipping_quote_path(@quote))
      else
        redirect_to_return_path_or_default shipping_quote_path(@quote)
      end
    end
  else
    flash.now[:error] = @quote.errors_to_s.presence || 'Could not update shipping address.'
    render :shipping_address, status: :unprocessable_entity
  end
end

#shippingObject



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

def shipping
  @ship_quotable = @quote
  @shipping_address = @customer.shipping_address
  if params[:shipping_updated]
    result = eval(@quote.last_shipping_rate_request_result.to_s)
    handle_shipping_flash_messaging(result)
  end
  redirect_to_return_path_or_default quote_path(@quote) if @quote.complete?
end

#shipping_addressObject



141
142
143
# File 'app/controllers/quotes_controller.rb', line 141

def shipping_address
  set_new_address
end

#showObject

GET /customers/1/opportunities/1/quotes/1



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'app/controllers/quotes_controller.rb', line 23

def show
  set_show_variables
  respond_to do |format|
    format.html do
      flash.now[:error] = "#{flash.now[:error]}Quote cannot be converted to order. #{(@quote.convert_to_order_service.errors || []).join('. ')} " if @quote.complete? && !@quote.sold? && !@quote.can_convert?
    end
    format.pdf do
      if Rails.env.development? || params[:inline].to_b
        options = { current_user_id: @context_user.id, output_to_file: false, show_installation_plans: true, materials_only: params[:materials_only].to_b }
        combined_pdf_result = Quote::CombinedPdfGenerator.new.process(@quote, **options)
        send_data combined_pdf_result.pdf_data, type: 'application/pdf', disposition: 'inline'
      else
        job_options = {
          quote_id: @quote.id, current_user_id: @context_user.id, preview: true, quote_options: { materials_only: params[:materials_only].to_b }
        }.deep_stringify_keys
        job_id = GenerateQuoteWorker.perform_async(job_options)
        redirect_to job_path(job_id)
      end
    end
  end
end

#show_material_alerts_tabObject



96
97
98
99
# File 'app/controllers/quotes_controller.rb', line 96

def show_material_alerts_tab
  @material_alerts = @quote.get_material_alerts if @quote && @quote.id.present?
  render partial: '/shared/material_alerts_tab'
end

#tab_contactsObject



447
448
449
450
# File 'app/controllers/quotes_controller.rb', line 447

def tab_contacts
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#tab_inventoryObject



462
463
464
465
466
# File 'app/controllers/quotes_controller.rb', line 462

def tab_inventory
  authorize!(:read, @quote)
  @item_groups = Inventory::QuotedRemainingItems.new.process(@quote, 1).item_groups
  render layout: should_render_layout?
end

#tab_line_itemsObject



442
443
444
445
# File 'app/controllers/quotes_controller.rb', line 442

def tab_line_items
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#tab_mainObject

Tab actions for turbo-tabs lazy loading



437
438
439
440
# File 'app/controllers/quotes_controller.rb', line 437

def tab_main
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#tab_revisionsObject



468
469
470
471
# File 'app/controllers/quotes_controller.rb', line 468

def tab_revisions
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#tab_shippingObject



452
453
454
455
# File 'app/controllers/quotes_controller.rb', line 452

def tab_shipping
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#tab_ticketsObject



457
458
459
460
# File 'app/controllers/quotes_controller.rb', line 457

def tab_tickets
  authorize!(:read, @quote)
  render layout: should_render_layout?
end

#unopened_quotesObject



131
132
133
# File 'app/controllers/quotes_controller.rb', line 131

def unopened_quotes
  @quotes = Quotes::UnopenedQuoteFinder.new.process
end

#updateObject

PUT /customers/1/opportunities/1/quotes/1



71
72
73
74
75
76
77
78
79
# File 'app/controllers/quotes_controller.rb', line 71

def update
  @quote.validate_opportunity_and_prepared_for = true
  @quote.need_to_recalculate_shipping
  if @quote.update(params[:quote])
    redirect_to_return_path_or_default(quote_path(@quote))
  else
    render action: :edit, status: :unprocessable_entity
  end
end

#update_profit_marginsObject



473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'app/controllers/quotes_controller.rb', line 473

def update_profit_margins
  authorize!(:manage_profit_margins, @quote)

  if quote_profit_margins_params.to_hash['min_profit_markup'].present?
    if @quote.update(quote_profit_margins_params)
      if @quote.can_release_from_profit_review?
        flash[:info] = 'Quote released for transmission'
        @quote.release_from_profit_review # Try to release
      elsif @quote.profit_review?
        flash[:error] = 'Minimum profit margins not met to allow release from profit review'
      end
      redirect_to_return_path_or_default quote_path(@quote)
    else
      flash.now[:error] = @quote.errors_to_s.presence || 'Could not update profit margins.'
      render :profit_margins, status: :unprocessable_entity
    end
  else
    flash.now[:error] = 'Minimum profit margins cannot be empty'
    render :profit_margins, status: :unprocessable_entity
  end
end

#upgradeObject



399
# File 'app/controllers/quotes_controller.rb', line 399

def upgrade; end