From a4f6eed488a98a7fbfa4711b7f0985363b5a02a0 Mon Sep 17 00:00:00 2001 From: Sergei Tsoganov Date: Tue, 3 Oct 2023 14:21:54 +0300 Subject: [PATCH] Added balance inbound to e-invoices to support deposits --- app/controllers/invoices_controller.rb | 2 +- app/models/concerns/invoice/book_keeping.rb | 5 +-- app/services/eis_billing/send_e_invoice.rb | 37 +++++++++++++-------- app/views/invoices/show.html.erb | 18 +++++----- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index bb2fe392d..2bc31662a 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -93,7 +93,7 @@ def oneoff def send_e_invoice invoice = Invoice.accessible_by(current_ability).find_by!(uuid: params[:uuid]) - response = EisBilling::SendEInvoice.call(invoice: invoice, payable: false) + response = EisBilling::SendEInvoice.call(invoice: invoice, payable: !invoice.paid?) if response.result? redirect_to invoice_path(invoice.uuid), notice: t('.sent_to_omniva') diff --git a/app/models/concerns/invoice/book_keeping.rb b/app/models/concerns/invoice/book_keeping.rb index 886d8f77c..d01c8660e 100644 --- a/app/models/concerns/invoice/book_keeping.rb +++ b/app/models/concerns/invoice/book_keeping.rb @@ -29,8 +29,9 @@ def e_invoice_data invoice['buyer_zip'] = postal_code invoice['buyer_city'] = city invoice['buyer_country_code'] = billing_alpha_two_country_code - invoice['total'] = total.to_f + (enable_deposit? ? deposit : 0.0) - invoice['vat_rate'] = vat_rate * 100 + invoice['total'] = price.to_f * (1 + vat_rate) + invoice['total_to_pay'] = total.to_f + invoice['vat_rate'] = vat_rate.to_f * 100 invoice['currency'] = Setting.find_by(code: 'auction_currency').retrieve invoice diff --git a/app/services/eis_billing/send_e_invoice.rb b/app/services/eis_billing/send_e_invoice.rb index d62e49471..d0dd803e2 100644 --- a/app/services/eis_billing/send_e_invoice.rb +++ b/app/services/eis_billing/send_e_invoice.rb @@ -11,36 +11,47 @@ def initialize(invoice:, payable:) end def call - prepared_data = { + prepared_data = build_prepared_data + struct_response(post(e_invoice_url, prepared_data)) + end + + private + + def build_prepared_data + { invoice: invoice.e_invoice_data, - vat_amount: invoice.vat.amount, - invoice_subtotal: invoice.price.amount, + vat_amount: invoice.vat.to_f, + invoice_subtotal: invoice.price.to_f, buyer_billing_email: invoice.user&.email, payable: payable, + inbound: prepare_inbound_data(invoice), initiator: INITIATOR, - items: prepare_items(invoice) + items: prepare_items(invoice), } - - struct_response(post(e_invoice_url, prepared_data)) end - private - def prepare_items(invoice) invoice.items.map do |invoice_item| { - description: item_description(invoice.result), - price: invoice_item.price.amount, + description: invoice_item.name, + price: invoice_item.price.to_f, quantity: 1, unit: 'piece', - subtotal: invoice_item.price.amount, + subtotal: invoice_item.price.to_f, vat_rate: invoice.vat_rate.to_f * 100, - vat_amount: invoice.vat.amount, - total: invoice.total.to_f, + vat_amount: invoice_item.price.to_f * invoice.vat_rate, + total: invoice_item.price.to_f * (1 + invoice.vat_rate), } end end + def prepare_inbound_data(invoice) + total = invoice.price.to_f * (1 + invoice.vat_rate) + return total if payable == false + + invoice.enable_deposit? ? invoice.deposit.to_f : 0 + end + def e_invoice_url '/api/v1/e_invoice/e_invoice' end diff --git a/app/views/invoices/show.html.erb b/app/views/invoices/show.html.erb index bd5be1725..5ac91779d 100644 --- a/app/views/invoices/show.html.erb +++ b/app/views/invoices/show.html.erb @@ -109,16 +109,18 @@ - <% if @invoice.payable? %> -
+
+ <% if @invoice.payable? %> <%= button_to t('invoices.show.pay'), oneoff_invoice_path(uuid: @invoice.uuid), class: 'ui button primary' %> -
- <% end %> + <% end %> +
- <% if @invoice.do_not_send_e_invoice? %> - EInvoice - <% else %> - <%= button_to t('invoices.show.send_e_invoice'), send_e_invoice_path(uuid: @invoice.uuid), class: 'ui button default', data: { turbo: false } %> + <% unless @invoice.cancelled? %> + <% if @invoice.e_invoice_sent? %> + EInvoice + <% else %> + <%= button_to t('invoices.show.send_e_invoice'), send_e_invoice_path(uuid: @invoice.uuid), class: 'ui button default', data: { turbo: false } %> + <% end %> <% end %>