Class: InvoicesController

Inherits:
CrmController show all
Includes:
Controllers::Workflowable
Defined in:
app/controllers/invoices_controller.rb

Overview

== Schema Information

Table name: invoices

id :integer not null, primary key
order_id :integer
jde_number :integer
total :decimal(8, 2)
created_at :datetime
updated_at :datetime
due_date :date
currency :string(255)
invoice_type :string(255)
discount :decimal(8, 2) default(0.0)
discount_code :string(255)
line_total :decimal(8, 2)
completion_date :date
state :string(255)
billing_address_id :integer
shipping_address_id :integer
tax_total :decimal(8, 2)
shipping_cost :decimal(8, 2)
reference_number :string(255)
terms :string(255)
shipped_date :date
uploads_count :integer
transmission_state :string(255)
customer_id :integer
company_id :integer
early_payment_discount :integer
early_payment_timescale :integer
local_sales_rep_id :integer
primary_sales_rep_id :integer
secondary_sales_rep_id :integer
buying_group_id :integer
shipping_coupon :decimal(8, 2)
creator_id :integer
updater_id :integer
document_date :date
gl_date :date
tax_date :date
po_number :string(255)
store_id :integer
gl_offset_account_id :integer
business_unit_id :integer
taxable_total :decimal(12, 2)
legacy_taxes :boolean
line_total_discounted :decimal(8, 2)
delivery_id :integer
old_total :decimal(8, 2)
old_tax_total :decimal(8, 2)
old_shipping_cost :decimal(8, 2)
revenue_consolidated :decimal(8, 2)
consolidated_exchange_rate :float
profile_id :integer
report_grouping :string(255)
billing_customer_id :integer
account_specialist_id :integer
profit :decimal(8, 2) default(0.0)
profit_consolidated :decimal(8, 2) default(0.0)
source_id :integer
technical_support_rep_id :integer

Constant Summary

Constants included from Controllers::ReferenceFindable

Controllers::ReferenceFindable::ID_EMBEDDED_PATTERNS

Constants included from Controllers::AnalyticsEvents

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

Constants included from Controllers::ErrorRendering

Controllers::ErrorRendering::NON_CONTENT_PATH_PREFIXES

Constants included from Www::SeoHelper

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

Constants included from IconHelper

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

Instance Method Summary collapse

Methods included from Controllers::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

#approveObject



310
311
312
313
314
315
316
317
318
319
320
321
# File 'app/controllers/invoices_controller.rb', line 310

def approve
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  begin
    @invoice.approve!
    flash[:info] = 'Invoice approved successfully'
  rescue StandardError => e
    flash[:error] = "Invoice could not be approved, message: #{e}"
  end
  redirect_to @invoice
end

#capture_paymentObject



384
385
386
387
388
389
390
391
# File 'app/controllers/invoices_controller.rb', line 384

def capture_payment
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  InvoiceCaptureFundsWorker.perform_async(@invoice.id)
  flash[:info] = 'Payment process started. Please reload this page in a few seconds to verify the payment was captured successfuly.'
  redirect_to @invoice
end

#createObject

POST /invoices
POST /invoices.xml



193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
# File 'app/controllers/invoices_controller.rb', line 193

def create
  authorize! :create, Invoice

  @invoice = Invoice.new(params[:invoice])
  @new_address = Address.new(params[:address])
  @addresses = @invoice.customer.nil? ? [] : @invoice.customer.all_addresses.to_a
  order = @invoice.order
  if order
    @addresses << order.shipping_address unless @addresses.include?(order.shipping_address)
    @addresses << order.billing_address unless @addresses.include?(order.billing_address)
  end
  if @invoice.enter_new_address.to_b
    @invoice.shipping_address_id = nil
    @invoice.shipping_address_attributes = params[:address]
  end
  if @invoice.save
    redirect_to(@invoice, info: 'Invoice was successfully created.')
  else
    render action: 'new', status: :unprocessable_entity
  end
end

#destroyObject

DELETE /invoices/1
DELETE /invoices/1.xml



247
248
249
250
251
252
253
254
255
256
257
258
259
260
# File 'app/controllers/invoices_controller.rb', line 247

def destroy
  authorize! :delete, Invoice

  @invoice = Invoice.find(params[:id])

  if @invoice.editing_locked?
    flash[:error] = 'Invoice cannot be deleted'
    redirect_to(@invoice) and return
  end

  flash[:info] = 'Invoice successfully deleted'
  @invoice.destroy
  redirect_to_return_path_or_default(invoices_url)
end

#editObject

GET /invoices/1/edit



173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
# File 'app/controllers/invoices_controller.rb', line 173

def edit
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  if @invoice.editing_locked?
    flash[:error] = 'Invoice cannot be edited'
    redirect_to(@invoice) and return
  end
  @addresses = @invoice.customer.all_addresses.to_a.concat([@invoice.shipping_address]).compact.uniq
  @new_address = Address.new
  order = @invoice.order
  if order
    @addresses << order.shipping_address unless @addresses.include?(order.shipping_address)
    @addresses << order.billing_address unless @addresses.include?(order.billing_address)
  end
  @invoice.line_items.build if @invoice.line_items.empty?
end

#exclude_notificationObject



368
369
370
371
372
373
374
# File 'app/controllers/invoices_controller.rb', line 368

def exclude_notification
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  @invoice.update(exclude_fund_capture_notification: true)
  redirect_to @invoice
end

#fba_pullObject



346
347
348
349
350
# File 'app/controllers/invoices_controller.rb', line 346

def fba_pull
  authorize! :create, Invoice
  @customer = Customer.find(params[:customer_id])
  redirect_to_return_path_or_default(customer_path(@customer, tab: 'accounting'), error: 'Customer is not an Amazon seller') unless @customer.is_amazon_seller_central?
end

#include_notificationObject



376
377
378
379
380
381
382
# File 'app/controllers/invoices_controller.rb', line 376

def include_notification
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  @invoice.update(exclude_fund_capture_notification: false)
  redirect_to @invoice
end

#indexObject

GET /invoices
GET /invoices.xml



70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'app/controllers/invoices_controller.rb', line 70

def index
  authorize! :read, Invoice
  qt = InvoiceSearch.instantiate_query_template(:default)
  search = qt.search
  search.employee = current_user
  authorize! :create, search
  if search.save
    redirect_to search_path(search)
  else
    flash[:error] = 'Unable to create default invoice search'
    redirect_to root_path
  end
end

#lookupObject



436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
# File 'app/controllers/invoices_controller.rb', line 436

def lookup
  authorize! :read, Invoice

  q = params[:q].try(:upcase)&.split&.first
  invoice_id = params[:invoice_id].presence
  customer_id = params[:customer_id].presence
  state_in = params[:state_in].presence
  if invoice_id
    invoice = Invoice.find(invoice_id)
    render json: { results: [{ id: invoice.id, text: invoice.selection_name_for_rmas }] }
  else
    @results = Invoice.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_invoice_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: 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_for_rmas,
        label: e.selection_name,
        reference: e.reference_number,
        order_link: invoice_path(e),
        state: e.state,
        shipped: e.shipped_date.present? ? e.shipped_date.to_fs(:crm_default) : 'not shipped',
        customer: e.customer.full_name,
        customer_link: customer_path(e.customer)
      }
    end
    render json: json_result
  end
end

#newObject

GET /invoices/new



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'app/controllers/invoices_controller.rb', line 111

def new
  authorize! :create, Invoice

  customer = Customer.where(id: params[:customer_id]).first
  order = Order.where(id: params[:order_id]).first
  @new_address = Address.new
  if order.present?
    company_id = order.store.company_id
    currency = order.currency
    shipping_address_id = order.shipping_address_id
    store_id = order.store.id
    primary_sales_rep_id = order.primary_sales_rep.try(:id)
    secondary_sales_rep_id = order.secondary_sales_rep.try(:id)
    local_sales_rep_id = order.local_sales_rep.try(:id)
    shipped_date = order.shipped_date
    order_id = order.id
    po_number = order.po_number
  elsif customer.present?
    company_id = customer.catalog.store.company_id
    currency = customer.catalog.store.company.currency
    shipping_address_id = nil
    primary_sales_rep_id = customer.store.id
    secondary_sales_rep_id = customer.secondary_sales_rep_id
    local_sales_rep_id = customer.local_sales_rep_id
    shipped_date = nil
    order_id = nil
    po_number = nil
  else
    company_id = nil
    currency = nil
    shipping_address_id = nil
    store_id = nil
    primary_sales_rep_id = nil
    secondary_sales_rep_id = nil
    local_sales_rep_id = nil
    shipped_date = nil
    order_id = nil
    po_number = nil
  end

  @addresses = customer.nil? ? [] : customer.all_addresses
  @invoice = Invoice.new(customer_id: params[:customer_id],
                         company_id: company_id,
                         shipping_address_id: shipping_address_id,
                         billing_address_id: customer.nil? ? nil : customer.billing_address_id,
                         terms: customer.nil? ? nil : customer.terms,
                         currency: currency,
                         gl_date: Date.current,
                         document_date: Date.current,
                         tax_date: Date.current,
                         store_id: store_id,
                         primary_sales_rep_id: primary_sales_rep_id,
                         secondary_sales_rep_id: secondary_sales_rep_id,
                         local_sales_rep_id: local_sales_rep_id,
                         order_id: order_id,
                         shipped_date: shipped_date,
                         po_number: po_number,
                         report_grouping: customer.nil? ? nil : customer.report_grouping)
  @invoice.line_items.build
end


323
324
325
326
327
# File 'app/controllers/invoices_controller.rb', line 323

def payment_link
  authorize! :read, Invoice

  @invoice = Invoice.find(params[:id])
end

#pdfObject



294
295
296
297
298
299
300
# File 'app/controllers/invoices_controller.rb', line 294

def pdf
  authorize! :read, Invoice

  @invoice = Invoice.find(params[:id])
  @pdf = @invoice.get_or_regen_pdf
  redirect_to upload_path(@pdf)
end

#process_fba_pullObject



352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
# File 'app/controllers/invoices_controller.rb', line 352

def process_fba_pull
  authorize! :create, Invoice
  @customer = Customer.find(params[:customer_id])
  redirect_to_return_path_or_default(customer_path(@customer, tab: 'accounting'), error: 'Customer is not an Amazon seller') unless @customer.is_amazon_seller_central?
  po_numbers = params[:po_numbers].split(',').map { |po| po.strip.delete(' ') }
  if po_numbers.any?
    orchestrator = @customer.edi_orchestrator
    orchestrator.fba_pull_orders_by_po_numbers(po_numbers)
    res = orchestrator.fba_order_message_processor.process
    invoices_created = res.batch_process_results.sum { |bpr| bpr.invoices_created.size }
    redirect_to_return_path_or_default(customer_path(@customer, tab: 'accounting'), notice: "PO number(s) processed and #{invoices_created} consignment invoice(s) created")
  else
    redirect_to_return_path_or_default(customer_path(@customer, tab: 'accounting'), error: 'No PO number(s) provided, skipping')
  end
end

#regenerate_edi_documentsObject



279
280
281
282
283
284
285
286
# File 'app/controllers/invoices_controller.rb', line 279

def regenerate_edi_documents
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  Edi::InvoiceEventProcessor.new.process_creation(@invoice)
  flash[:info] = "EDI documents queued for regeneration for #{@invoice.reference_number}."
  redirect_to params[:return_to].presence || @invoice
end

#regenerate_pdfObject



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

def regenerate_pdf
  authorize! :read, Invoice

  @invoice = Invoice.find(params[:id])
  @pdf = @invoice.generate_pdf
  redirect_to upload_path(@pdf)
end

#regenerate_receiptsObject



270
271
272
273
274
275
276
277
# File 'app/controllers/invoices_controller.rb', line 270

def regenerate_receipts
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  @invoice.create_receipts_for_captured_payments
  flash[:info] = 'Regeneration process finished. If no receipts were created there are probably no payments attached to this invoice.'
  redirect_to @invoice
end


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

def send_payment_link
  authorize! :read, Invoice

  @invoice = Invoice.find(params[:id])
  @customer = @invoice.customer
  token = Encryption.encrypt_string(params[:id])
  url = "https://#{WEB_HOSTNAME}/pay-invoice/#{token}"
  comm = CommunicationBuilder.new(sender_party: @customer.primary_sales_rep || current_user,
                                  recipient_party: @customer,
                                  emails: params[:payment_link][:email],
                                  merge_options: { receipt_link: url },
                                  save_merge_options: true,
                                  use_best_email: true,
                                  template_system_code: 'INVOICE_PAYMENT').create(keep_as_draft: true)
  redirect_to communication_path(comm)
end

#showObject

GET /invoices/1
GET /invoices/1.xml
Accepts ID or reference_number (e.g., IN123456)



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/invoices_controller.rb', line 87

def show
  authorize! :read, Invoice

  invoice_scope = Invoice.includes(line_items: [:reserved_serial_numbers], item_ledger_entries: %i[store_item_audit ledger_transaction serial_numbers])
  @invoice = find_by_id_or_reference(Invoice, :reference_number, scope: invoice_scope)
  @customer = @invoice.customer
  @show_kits_and_sn = params[:show_kits_and_sn].to_b
  @attachments = @invoice.uploads
  @pagy_activities, @activities = pagy(@invoice.activities.natural_order)
  set_context(@invoice)
  @old_style = params[:old_style]
  respond_to do |format|
    format.html # show.html.erb
    format.xml  { render xml: @invoice }
    format.pdf do
      send_data Invoicing::CombinedPdfGenerator.new.process(@invoice, { output_to_file: false }).pdf_data,
                filename: @invoice.file_name,
                type: 'application/pdf',
                disposition: 'inline'
    end
  end
end

#tab_account_ledgerObject



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

def 
  load_invoice
  render layout: should_render_layout?
end

#tab_attachmentsObject



399
400
401
402
# File 'app/controllers/invoices_controller.rb', line 399

def tab_attachments
  load_invoice
  render layout: should_render_layout?
end

#tab_credit_memosObject



420
421
422
423
424
# File 'app/controllers/invoices_controller.rb', line 420

def tab_credit_memos
  load_invoice
  @customer = @invoice.customer
  render layout: should_render_layout?
end

#tab_edi_documentsObject



288
289
290
291
292
# File 'app/controllers/invoices_controller.rb', line 288

def tab_edi_documents
  load_invoice
  @edi_communication_logs = @invoice.edi_communication_logs.order(created_at: :desc)
  render layout: should_render_layout?
end

#tab_item_ledgerObject



426
427
428
429
# File 'app/controllers/invoices_controller.rb', line 426

def tab_item_ledger
  load_invoice
  render layout: should_render_layout?
end

#tab_line_itemsObject

Tab actions for turbo-tabs lazy loading



394
395
396
397
# File 'app/controllers/invoices_controller.rb', line 394

def tab_line_items
  load_invoice
  render layout: should_render_layout?
end

#tab_paymentsObject



409
410
411
412
413
# File 'app/controllers/invoices_controller.rb', line 409

def tab_payments
  load_invoice
  @payments = payments_for_invoice_tab(@invoice)
  render layout: should_render_layout?
end

#tab_receiptsObject



404
405
406
407
# File 'app/controllers/invoices_controller.rb', line 404

def tab_receipts
  load_invoice
  render layout: should_render_layout?
end

#tab_rmasObject



415
416
417
418
# File 'app/controllers/invoices_controller.rb', line 415

def tab_rmas
  load_invoice
  render layout: should_render_layout?
end

#transmitObject



302
303
304
305
306
307
308
# File 'app/controllers/invoices_controller.rb', line 302

def transmit
  authorize! :read, Invoice

  @invoice = Invoice.find(params[:id])
  redir_params = CommunicationBuilder.new(resource: @invoice, current_user: current_user).to_params
  redirect_to new_communication_path(redir_params)
end

#updateObject

PUT /invoices/1
PUT /invoices/1.xml



217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
# File 'app/controllers/invoices_controller.rb', line 217

def update
  authorize! :update, Invoice

  @invoice = Invoice.find(params[:id])
  if @invoice.editing_locked?
    flash[:error] = 'Invoice cannot be edited'
    redirect_to(@invoice) and return
  end
  @invoice.assign_attributes(params[:invoice])
  @new_address = Address.new(params[:address])
  if @invoice.enter_new_address == '1'
    @invoice.shipping_address_id = nil
    @invoice.shipping_address_attributes = params[:address]
  end
  if @invoice.save
    redirect_to_return_path_or_default(invoice_path(@invoice))
  else
    @addresses = @invoice.customer.nil? ? [] : @invoice.customer.all_addresses
    @addresses = @addresses.to_a.concat([@invoice.shipping_address]).compact.uniq
    order = @invoice.order
    if order
      @addresses << order.shipping_address unless @addresses.include?(order.shipping_address)
      @addresses << order.billing_address unless @addresses.include?(order.billing_address)
    end
    render action: 'edit', status: :unprocessable_entity
  end
end