Merge pull request #2531 from internetee/68-creating-syncronization-of-invoice-changes

creating sync with billing
This commit is contained in:
Timo Võhmar 2023-04-20 16:08:47 +03:00 committed by GitHub
commit da514f9a50
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 715 additions and 60 deletions

View file

@ -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

View file

@ -3,19 +3,20 @@ module EisBilling
TYPE = 'PaymentOrders::EveryPay'.freeze
def update
payment_status = define_payment_status(params[:payment_state])
invoice = Invoice.find_by(number: params[:order_reference])
return if invoice.paid?
if invoice.paid?
render json: { message: 'Invoice already paid' }, status: :ok
else
invoice.process_payment(
payment_type: TYPE,
everypay_response: params,
payment_status: define_payment_status(params[:payment_state]),
sum: params[:standing_amount],
transaction_time: params[:transaction_time]
)
bank = create_bank_transfer(invoice: invoice, sum: params[:standing_amount], paid_at: params[:transaction_time])
create_payment_order(invoice: invoice, everypay_response: params, payment_status: payment_status)
bank.bind_invoice(params[:order_reference])
respond_to do |format|
format.json do
render status: :ok, content_type: 'application/json', layout: false, json: { message: 'ok' }
end
render json: { message: 'Payment is proccessing' }, status: :ok
end
end
@ -26,30 +27,5 @@ module EisBilling
:failed
end
def create_payment_order(invoice:, everypay_response:, payment_status:)
payment = PaymentOrder.new
payment.type = TYPE
payment.invoice = invoice
payment.response = everypay_response
payment.status = payment_status
payment.save
payment
end
def create_bank_transfer(invoice:, sum:, paid_at:)
bank = BankTransaction.new
bank.description = invoice.order
bank.reference_no = invoice.reference_no
bank.currency = invoice.currency
bank.iban = invoice.seller_iban
bank.sum = sum
bank.paid_at = paid_at
bank.buyer_name = invoice.buyer_name
bank.save
bank
end
end
end

View file

@ -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

View file

@ -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

View 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

View file

@ -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,