mirror of
https://github.com/LaCasemate/fab-manager.git
synced 2025-01-18 07:52:23 +01:00
(api) accounting: payment details
This commit is contained in:
parent
e3eb8eb516
commit
31ba4d3486
@ -7,8 +7,14 @@ class OpenAPI::V1::AccountingController < OpenAPI::V1::BaseController
|
||||
expose_doc
|
||||
|
||||
def index
|
||||
@codes = {
|
||||
card: Setting.get('accounting_card_client_code'),
|
||||
wallet: Setting.get('accounting_wallet_client_code'),
|
||||
other: Setting.get('accounting_other_client_code')
|
||||
}
|
||||
|
||||
@lines = AccountingLine.order(date: :desc)
|
||||
.includes(:invoice, :invoicing_profile)
|
||||
.includes(:invoicing_profile, invoice: :payment_gateway_object)
|
||||
|
||||
@lines = @lines.where('date >= ?', DateTime.parse(params[:after])) if params[:after].present?
|
||||
@lines = @lines.where('date <= ?', DateTime.parse(params[:before])) if params[:before].present?
|
||||
|
@ -147,7 +147,7 @@ class Invoice < PaymentDocument
|
||||
# return a summary of the payment means used
|
||||
def payment_means
|
||||
res = []
|
||||
res.push(means: :wallet, amount: wallet_amount) if wallet_transaction && wallet_amount.positive?
|
||||
res.push(means: :wallet, amount: wallet_amount) if paid_by_wallet?
|
||||
if paid_by_card?
|
||||
res.push(means: :card, amount: amount_paid)
|
||||
else
|
||||
@ -156,6 +156,22 @@ class Invoice < PaymentDocument
|
||||
res
|
||||
end
|
||||
|
||||
def payment_details(mean)
|
||||
case mean
|
||||
when :card
|
||||
if paid_by_card?
|
||||
{
|
||||
gateway_object_id: payment_gateway_object.gateway_object_id,
|
||||
gateway_object_type: payment_gateway_object.gateway_object_type
|
||||
}
|
||||
end
|
||||
when :wallet
|
||||
{ wallet_transaction_id: wallet_transaction_id } if paid_by_wallet?
|
||||
else
|
||||
{}
|
||||
end
|
||||
end
|
||||
|
||||
def footprint_children
|
||||
invoice_items
|
||||
end
|
||||
@ -164,6 +180,10 @@ class Invoice < PaymentDocument
|
||||
!payment_gateway_object.nil? && payment_method == 'card'
|
||||
end
|
||||
|
||||
def paid_by_wallet?
|
||||
wallet_transaction && wallet_amount.positive?
|
||||
end
|
||||
|
||||
def render_resource
|
||||
{ partial: 'api/invoices/invoice', locals: { invoice: self } }
|
||||
end
|
||||
|
@ -7,6 +7,10 @@ json.lines @lines do |line|
|
||||
json.extract! line.invoice, :reference, :id
|
||||
json.label Invoices::LabelService.build(line.invoice)
|
||||
json.url download_open_api_v1_invoice_path(line.invoice)
|
||||
if @codes.values.include?(line.account_code)
|
||||
mean = @codes.select { |_key, value| value == line.account_code }
|
||||
json.payment_details line.invoice.payment_details(mean.keys[0])
|
||||
end
|
||||
end
|
||||
end
|
||||
if line.association(:invoicing_profile).loaded?
|
||||
|
@ -78,4 +78,27 @@ class OpenApi::AccountingTest < ActionDispatch::IntegrationTest
|
||||
assert lines[:lines].count.positive?
|
||||
assert(lines[:lines].all? { |line| %w[client vat].include?(line[:line_type]) })
|
||||
end
|
||||
|
||||
test 'list all accounting client lines have payment details' do
|
||||
get '/open_api/v1/accounting?type=client', headers: open_api_headers(@token)
|
||||
assert_response :success
|
||||
assert_equal Mime[:json], response.content_type
|
||||
|
||||
card_code = Setting.get('accounting_card_client_code')
|
||||
wallet_code = Setting.get('accounting_wallet_client_code')
|
||||
other_code = Setting.get('accounting_other_client_code')
|
||||
|
||||
lines = json_response(response.body)
|
||||
assert lines[:lines].count.positive?
|
||||
assert(lines[:lines].all? { |line| line[:line_type] == 'client' })
|
||||
assert(lines[:lines].all? { |line| !line[:invoice][:payment_details].nil? })
|
||||
assert(lines[:lines].filter { |line| line[:account_code] == card_code }
|
||||
.none? { |line| line[:invoice][:payment_details][:gateway_object_id].nil? })
|
||||
assert(lines[:lines].filter { |line| line[:account_code] == card_code }
|
||||
.none? { |line| line[:invoice][:payment_details][:gateway_object_type].nil? })
|
||||
assert(lines[:lines].filter { |line| line[:account_code] == wallet_code }
|
||||
.none? { |line| line[:invoice][:payment_details][:wallet_transaction_id].nil? })
|
||||
assert(lines[:lines].filter { |line| line[:account_code] == other_code }
|
||||
.all? { |line| line[:invoice][:payment_details].empty? })
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user