Class: Source::SetInvoiceSource

Inherits:
BaseService show all
Defined in:
app/services/source/set_invoice_source.rb

Overview

Operation to set a new source on invoices and related order and opportunity

Defined Under Namespace

Classes: Result

Instance Attribute Summary

Attributes inherited from BaseService

#options

Instance Method Summary collapse

Methods inherited from BaseService

#initialize, #log_debug, #log_error, #log_info, #log_warning, #logger, #tagged_logger

Constructor Details

This class inherits a constructor from BaseService

Instance Method Details

#process(invoice_ids, source_id, _options = {}) ⇒ Object

Set a new source on given invoices
push the source back to the order and opportunity if present



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/services/source/set_invoice_source.rb', line 12

def process(invoice_ids, source_id, _options = {})
  messages = []
  invoices_updated = []
  invoices_failed = []
  Invoice.where(id: invoice_ids).find_each do |invoice|
    Invoice.transaction do
      invoice.update!(source_id: source_id)
      invoice.order&.update!(source_id: source_id)
      invoice.order&.opportunity&.update!(source_id: source_id)
      invoices_updated << invoice.id
    rescue ActiveRecord::RecordInvalid => e
      invoices_failed << invoice.id
      messages << "Invoice #{invoice.id}: #{e.record.errors.full_messages.to_sentence}"
      raise ActiveRecord::Rollback
    end
  end
  Result.new(all_invoices_updated: invoices_failed.empty?,
             messages: messages,
             invoices_updated: invoices_updated,
             invoices_failed: invoices_failed)
end