mirror of
https://github.com/internetee/registry.git
synced 2025-07-30 14:36:22 +02:00
Merge pull request #2531 from internetee/68-creating-syncronization-of-invoice-changes
creating sync with billing
This commit is contained in:
commit
da514f9a50
11 changed files with 715 additions and 60 deletions
|
@ -31,4 +31,22 @@ module Invoice::Cancellable
|
|||
def not_cancelled?
|
||||
!cancelled?
|
||||
end
|
||||
|
||||
def cancel_manualy
|
||||
account_activity = AccountActivity.find_by(invoice_id: id)
|
||||
account_activity_dup = account_activity.dup
|
||||
account_activity_dup.sum = -account_activity.sum.to_f
|
||||
account_activity_dup.save
|
||||
account_activity.update(invoice_id: nil)
|
||||
account_activity_dup.update(invoice_id: nil)
|
||||
mark_cancelled_payment_order
|
||||
account_activity.save && account_activity_dup.save
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mark_cancelled_payment_order
|
||||
payment_order = payment_orders.last
|
||||
payment_order.update(notes: 'Cancelled')
|
||||
end
|
||||
end
|
||||
|
|
|
@ -23,4 +23,34 @@ module Invoice::Payable
|
|||
def unpaid?
|
||||
!paid?
|
||||
end
|
||||
|
||||
def process_payment(**options)
|
||||
payment = options[:payment_type].constantize.new(invoice: self)
|
||||
payment.response = options[:everypay_response]
|
||||
payment.status = options[:payment_status]
|
||||
payment.save!
|
||||
|
||||
bank_transaction = payment.base_transaction(sum: options[:sum],
|
||||
paid_at: options[:transaction_time] || Time.zone.now,
|
||||
buyer_name: buyer_name)
|
||||
bank_transaction.bind_invoice(number)
|
||||
end
|
||||
|
||||
def autobind_manually
|
||||
return if paid?
|
||||
|
||||
bank_statement = BankStatement.new(
|
||||
bank_code: Setting.registry_bank_code,
|
||||
iban: Setting.registry_iban
|
||||
)
|
||||
bank_statement.bank_transactions.build(
|
||||
description: description,
|
||||
sum: total,
|
||||
reference_no: reference_no,
|
||||
paid_at: Time.zone.now.to_date,
|
||||
currency: 'EUR'
|
||||
)
|
||||
bank_statement.save!
|
||||
bank_statement.bind_invoices(manual: true)
|
||||
end
|
||||
end
|
||||
|
|
53
app/models/invoice_state_machine.rb
Normal file
53
app/models/invoice_state_machine.rb
Normal file
|
@ -0,0 +1,53 @@
|
|||
class InvoiceStateMachine
|
||||
attr_reader :invoice, :status
|
||||
|
||||
def initialize(invoice:, status:)
|
||||
@invoice = invoice
|
||||
@status = status.to_sym
|
||||
end
|
||||
|
||||
def call
|
||||
case status
|
||||
when :paid
|
||||
mark_as_paid
|
||||
when :cancelled
|
||||
mark_as_cancel
|
||||
when :unpaid
|
||||
mark_as_unpaid
|
||||
else
|
||||
push_error
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mark_as_paid
|
||||
return push_error unless invoice.payable?
|
||||
return true if invoice.paid?
|
||||
|
||||
invoice.autobind_manually
|
||||
invoice
|
||||
end
|
||||
|
||||
def mark_as_cancel
|
||||
return push_error unless invoice.cancellable?
|
||||
return true if invoice.cancelled?
|
||||
|
||||
invoice.cancel
|
||||
invoice
|
||||
end
|
||||
|
||||
def mark_as_unpaid
|
||||
return push_error if invoice.paid? && invoice.payment_orders&.last&.payment_reference? || invoice.cancelled?
|
||||
return true unless invoice.paid?
|
||||
|
||||
invoice.cancel_manualy
|
||||
invoice
|
||||
end
|
||||
|
||||
def push_error
|
||||
invoice.errors.add(:base, "Inavalid state #{status}")
|
||||
|
||||
false
|
||||
end
|
||||
end
|
|
@ -64,6 +64,10 @@ class PaymentOrder < ApplicationRecord
|
|||
false
|
||||
end
|
||||
|
||||
def payment_reference?
|
||||
response && response['payment_reference'].present?
|
||||
end
|
||||
|
||||
def base_transaction(sum:, paid_at:, buyer_name:)
|
||||
BankTransaction.new(
|
||||
description: invoice.order,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue