mirror of
https://github.com/internetee/registry.git
synced 2025-07-28 05:26:17 +02:00
change logic of state machine
This commit is contained in:
parent
a4bf967e1d
commit
e00e87a15c
1 changed files with 13 additions and 5 deletions
|
@ -17,28 +17,36 @@ class InvoiceStateMachine
|
|||
when :unpaid
|
||||
mark_as_unpaid
|
||||
else
|
||||
raise "Inavalid state #{invoice.status}"
|
||||
push_error
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def mark_as_paid
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.unpaid? || invoice.paid?
|
||||
return push_error unless invoice.payable? || invoice.paid?
|
||||
|
||||
invoice.autobind_manually
|
||||
invoice
|
||||
end
|
||||
|
||||
def mark_as_cancel
|
||||
# Paid invoice cannot be cancelled?
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.cancellable? || invoice.cancelled?
|
||||
return push_error unless invoice.cancellable? || invoice.cancelled?
|
||||
|
||||
invoice.cancel
|
||||
invoice
|
||||
end
|
||||
|
||||
def mark_as_unpaid
|
||||
raise "Inavalid state #{invoice.status}" unless invoice.paid? && invoice.payment_orders.present? || invoice.unpaid?
|
||||
return push_error if invoice.paid? || !invoice.cancellable?
|
||||
|
||||
invoice.cancel_manualy
|
||||
invoice
|
||||
end
|
||||
|
||||
def push_error
|
||||
invoice.errors.add(:base, "Inavalid state #{status}")
|
||||
|
||||
false
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue