-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2531 from internetee/68-creating-syncronization-o…
…f-invoice-changes creating sync with billing
- Loading branch information
Showing
11 changed files
with
714 additions
and
59 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
module EisBilling | ||
class InvoicesController < BaseController | ||
before_action :load_invoice, only: :update | ||
|
||
def update | ||
state = InvoiceStateMachine.new(invoice: @invoice, status: params[:status]) | ||
if @invoice.update(modified_params) && state.call | ||
render json: { | ||
message: 'Invoice data was successfully updated', | ||
}, status: :ok | ||
else | ||
render json: { | ||
error: { | ||
message: @invoice.errors.full_messages | ||
} | ||
}, status: :unprocessable_entity | ||
end | ||
end | ||
|
||
private | ||
|
||
def load_invoice | ||
@invoice = Invoice.find_by(number: params[:invoice][:invoice_number]) | ||
return if @invoice.present? | ||
|
||
render json: { | ||
error: { | ||
message: "Invoice with #{params[:invoice][:invoice_number]} number not found", | ||
} | ||
}, status: :not_found and return | ||
end | ||
|
||
def modified_params | ||
{ | ||
in_directo: params[:invoice][:in_directo], | ||
e_invoice_sent_at: params[:invoice][:sent_at_omniva], | ||
} | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.