Class: Dolibarr::Payments

Inherits:
Resource show all
Defined in:
lib/dolibarr/payments.rb

Overview

Payments recorded against customer invoices.

Dolibarr's per-invoice payment endpoint settles the full remaining amount (there is no partial-amount field) and can close the invoice in the same call via closepaidinvoices. #register exposes exactly that: pay and close in one shot.

Instance Method Summary collapse

Methods inherited from Resource

#initialize

Constructor Details

This class inherits a constructor from Dolibarr::Resource

Instance Method Details

#for_invoice(invoice_id:) ⇒ Array<Payment>

The payments already recorded on an invoice.

Parameters:

  • invoice_id (Integer, String)

Returns:

  • (Array<Payment>)

    empty when the invoice has none



16
17
18
# File 'lib/dolibarr/payments.rb', line 16

def for_invoice(invoice_id:)
  get_list("/invoices/#{encode(invoice_id)}/payments").map { |raw| Payment.from(raw) }
end

#register(invoice_id:, account_id:, payment_mode_id:, date: Date.today, close: true, number: nil, comment: nil) ⇒ Integer

Record a payment on an invoice, settling its remaining balance, and (by default) close it.

Parameters:

  • invoice_id (Integer, String)

    the invoice to pay

  • account_id (Integer)

    the bank account receiving the payment

  • payment_mode_id (Integer)

    the payment mode id (transfer, cheque, …)

  • date (Date, String) (defaults to: Date.today)

    the payment date (defaults to today)

  • close (Boolean) (defaults to: true)

    classify the invoice as paid in the same call (default true)

  • number (String, nil) (defaults to: nil)

    payment number (cheque/transfer ref), optional

  • comment (String, nil) (defaults to: nil)

    private note, optional

Returns:

  • (Integer)

    the id of the created payment



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dolibarr/payments.rb', line 31

def register(invoice_id:, account_id:, payment_mode_id:, date: Date.today,
             close: true, number: nil, comment: nil)
  body = {
    'accountid'         => ,
    'paymentid'         => payment_mode_id,
    'datepaye'          => iso_date(date),
    'closepaidinvoices' => close ? 'yes' : 'no',
    'num_payment'       => number,
    'comment'           => comment,
  }.compact

  call do
    connection.call(
      :POST, "/invoices/#{encode(invoice_id)}/payments",
      type: nil, auth: ['api_key'], body: body
    ).data
  end
end