Class: Effective::QbReceipt
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- Effective::QbReceipt
- Defined in:
- app/models/effective/qb_receipt.rb
Class Method Summary collapse
-
.create_from_order!(order) ⇒ Object
Create a QbReceipt from an Effective::Order.
Instance Method Summary collapse
- #complete! ⇒ Object
- #error! ⇒ Object
-
#qb_receipt_item(order_item:) ⇒ Object
Find or build.
- #skip! ⇒ Object
- #sync!(force: false) ⇒ Object
- #to_s ⇒ Object
Class Method Details
.create_from_order!(order) ⇒ Object
Create a QbReceipt from an Effective::Order
44 45 46 47 |
# File 'app/models/effective/qb_receipt.rb', line 44 def self.create_from_order!(order) raise('Expected a purchased Effective::Order') unless order.kind_of?(Effective::Order) && order.purchased? Effective::QbReceipt.where(order: order).first_or_create end |
Instance Method Details
#complete! ⇒ Object
109 110 111 |
# File 'app/models/effective/qb_receipt.rb', line 109 def complete! completed! end |
#error! ⇒ Object
113 114 115 116 117 118 119 |
# File 'app/models/effective/qb_receipt.rb', line 113 def error! errored! EffectiveLogger.error(result, associated: self) if defined?(EffectiveLogger) EffectiveQbOnline.send_email(:sync_error, self) if EffectiveOrders.send_qb_online_sync_error false end |
#qb_receipt_item(order_item:) ⇒ Object
Find or build
54 55 56 57 |
# File 'app/models/effective/qb_receipt.rb', line 54 def qb_receipt_item(order_item:) qb_receipt_items.find { |item| item.order_item == order_item } || qb_receipt_items.build(order_item: order_item) end |
#skip! ⇒ Object
104 105 106 107 |
# File 'app/models/effective/qb_receipt.rb', line 104 def skip! assign_attributes(result: 'skipped') skipped! end |
#sync!(force: false) ⇒ Object
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 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 |
# File 'app/models/effective/qb_receipt.rb', line 59 def sync!(force: false) raise('Already created SalesReceipt with QuickBooks Online') if sales_receipt_id.present? && !force save! api = EffectiveQbOnline.api begin sales_receipt = Effective::QbSalesReceipt.build_from_receipt!(self, api: api) sales_receipt = api.create_sales_receipt(sales_receipt: sales_receipt) # The SalesReceipt now exists in QuickBooks. Persist its id immediately so a # later failure (e.g. the total sanity checks below) doesn't orphan it. Without # this, a re-sync would try to create it again and QuickBooks rejects the # reused DocNumber with a Duplicate Document Number Error. update_column(:sales_receipt_id, sales_receipt.id) # Sanity check if EffectiveOrders.try(:credit_card_surcharge_qb_item_name).present? && !EffectiveOrders.try(:fee_saver?) if (expected = api.price_to_amount(order.total)) != sales_receipt.total raise("A QuickBooks Online Sales Receipt has been created with an unexpected total. QuickBooks total is #{sales_receipt.total} but we expected #{expected}. Please adjust the Sales Receipt on QuickBooks") end end if EffectiveOrders.try(:credit_card_surcharge_qb_item_name).present? && EffectiveOrders.try(:fee_saver?) if (expected = api.price_to_amount(order.amount_owing + order.surcharge.to_i)) != sales_receipt.total raise("A QuickBooks Online Sales Receipt has been created with an unexpected total. QuickBooks total is #{sales_receipt.total} but we expected #{expected}. Please adjust the Sales Receipt on QuickBooks") end end if EffectiveOrders.try(:credit_card_surcharge_qb_item_name).blank? if (expected = api.price_to_amount(order.amount_owing)) != sales_receipt.total raise("A QuickBooks Online Sales Receipt has been created with an unexpected total. QuickBooks total is #{sales_receipt.total} but we expected #{expected}. Please adjust the Sales Receipt on QuickBooks") end end assign_attributes(result: 'completed successfully', sales_receipt_id: sales_receipt.id) complete! rescue => e result = [e., ("(intuit_tid: #{e.intuit_tid})" if e.try(:intuit_tid).present?)].compact.join("\n\n") assign_attributes(result: result) error! end end |
#to_s ⇒ Object
49 50 51 |
# File 'app/models/effective/qb_receipt.rb', line 49 def to_s order.to_s end |