Class: Stripe2QB::Converters::TransferToDeposit

Inherits:
Base
  • Object
show all
Defined in:
lib/stripe2qb/converters/transfer_to_deposit.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#quickbooks_api, #stripe_api

Instance Method Summary collapse

Methods inherited from Base

#exists?, #find_or_create

Constructor Details

#initialize(transfer, configuration) ⇒ TransferToDeposit



9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 9

def initialize(transfer, configuration)
  super(configuration)

  @transfer = transfer

  charges = stripe_api.get_transfer_charges(transfer.id)
  @charge_to_sales_receipts = charges.map {|charge| ChargeToSalesReceipt.new(charge, configuration) }

  refunds = stripe_api.get_transfer_refunds(transfer.id)
  @refund_to_refund_receipts = refunds.map {|refund| RefundToRefundReceipt.new(refund, configuration) }

  @deposit = build_empty_deposit
end

Instance Attribute Details

#charge_to_sales_receiptsObject (readonly)

Returns the value of attribute charge_to_sales_receipts.



5
6
7
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 5

def charge_to_sales_receipts
  @charge_to_sales_receipts
end

#depositObject (readonly)

Returns the value of attribute deposit.



7
8
9
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 7

def deposit
  @deposit
end

#refund_to_refund_receiptsObject (readonly)

Returns the value of attribute refund_to_refund_receipts.



6
7
8
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 6

def refund_to_refund_receipts
  @refund_to_refund_receipts
end

#transferObject (readonly)

Returns the value of attribute transfer.



4
5
6
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 4

def transfer
  @transfer
end

Instance Method Details

#create!Object



37
38
39
40
41
42
43
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 37

def create!
  raise "Deposit for #{transfer.id} already exists: #{find.id}" if exists?

  create_dependencies!
  build_deposit_line_items
  quickbooks_api.deposit_service.create(deposit)
end

#create_dependencies!Object



45
46
47
48
49
50
51
52
53
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 45

def create_dependencies!
  charge_to_sales_receipts.each do |charge_to_sales_receipt|
    charge_to_sales_receipt.find_or_create
  end

  refund_to_refund_receipts.each do |refund_to_refund_receipt|
    refund_to_refund_receipt.find_or_create
  end
end

#delete!Object



55
56
57
58
59
60
61
62
63
64
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 55

def delete!
  return false unless exists?

  result = quickbooks_api.deposit_service.delete(find)
  @found = nil

  delete_dependencies!

  result
end

#delete_dependencies!Object



66
67
68
69
70
71
72
73
74
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 66

def delete_dependencies!
  charge_to_sales_receipts.each do |charge_to_sales_receipt|
    charge_to_sales_receipt.delete! if charge_to_sales_receipt.exists?
  end

  refund_to_refund_receipts.each do |refund_to_refund_receipt|
    refund_to_refund_receipt.delete! if refund_to_refund_receipt.exists?
  end
end

#findObject



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/stripe2qb/converters/transfer_to_deposit.rb', line 23

def find
  return @found if @found

  # Quickbooks doesn't have an external ID on Deposits, so we have to
  # search for a match on private_note...
  # first narrow down results by txn_date
  deposits = quickbooks_api.deposit_service.find_by(:txn_date, format_date(transfer.created))
  deposits.each do |deposit|
    return @found = deposit if deposit.private_note =~ /#{transfer.id}/
  end

  nil
end