mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-04-02 08:21:57 -04:00
Refactor persist method in InvoiceSyncService to use 'local' variable for clarity and add logging for updates
This commit is contained in:
@@ -68,20 +68,25 @@ class InvoiceSyncService
|
|||||||
|
|
||||||
# Create or update a local Invoice record based on the QBO remote data
|
# Create or update a local Invoice record based on the QBO remote data
|
||||||
def persist(remote)
|
def persist(remote)
|
||||||
invoice = Invoice.find_or_initialize_by(id: remote.id)
|
local = Invoice.find_or_initialize_by(id: remote.id)
|
||||||
|
|
||||||
invoice.doc_number = remote.doc_number
|
local.doc_number = remote.doc_number
|
||||||
invoice.txn_date = remote.txn_date
|
local.txn_date = remote.txn_date
|
||||||
invoice.due_date = remote.due_date
|
local.due_date = remote.due_date
|
||||||
invoice.total_amount = remote.total
|
local.total_amount = remote.total
|
||||||
invoice.balance = remote.balance
|
local.balance = remote.balance
|
||||||
invoice.qbo_updated_at = remote.meta_data&.last_updated_time
|
local.qbo_updated_at = remote.meta_data&.last_updated_time
|
||||||
|
|
||||||
invoice.customer = Customer.find_by(id: remote.customer_ref&.value)
|
local.customer = Customer.find_by(id: remote.customer_ref&.value)
|
||||||
|
|
||||||
invoice.save!
|
if local.changed?
|
||||||
|
local.save
|
||||||
|
log "Updated invoice #{remote.doc_number} (#{remote.id})"
|
||||||
|
end
|
||||||
|
|
||||||
InvoiceAttachmentService.new(invoice, remote).attach
|
InvoiceAttachmentService.new(local, remote).attach
|
||||||
|
rescue => e
|
||||||
|
log "Failed to sync invoice #{remote.doc_number} (#{remote.id}): #{e.message}"
|
||||||
end
|
end
|
||||||
|
|
||||||
def log(msg)
|
def log(msg)
|
||||||
|
|||||||
Reference in New Issue
Block a user