..
  This file is part of GNU TALER.
  Copyright (C) 2014-2026 Taler Systems SA

  TALER is free software; you can redistribute it and/or modify it under the
  terms of the GNU Affero General Public License as published by the Free Software
  Foundation; either version 2.1, or (at your option) any later version.

  TALER is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more details.

  You should have received a copy of the GNU Affero General Public License along with
  TALER; see the file COPYING.  If not, see <http://www.gnu.org/licenses/>

  @author Bohdan Potuzhnyi
  @author Volodymyr Potuzhnyi

.. _libeufinconnector_dolibarr_transactions_incoming:

Incoming Transactions
=====================

.. contents::
  :depth: 1
  :local:

This module supports the following incoming payments:

1. Customer order payments
2. Supplier order credit notes

Normally, the module tries to auto-connect the transaction to the invoice by
auto-creating the payment and the bank line. There are also some manual
options, in case that is needed. More examples are presented below.

Customer order payments
-----------------------

Firstly, you need to have a customer order. A sample is shown in the next
picture.

.. figure:: ../../images/libeufinconnector-dolibarr/co1.png
   :alt: Customer order example in Dolibarr
   :width: 720px
   :align: center

   Customer order example in Dolibarr.

After this, you of course also need to create an invoice. The payment method
must be ``Bank transfer`` and it must match the bank account you chose during
configuration.

.. figure:: ../../images/libeufinconnector-dolibarr/co2.png
   :alt: Customer invoice configured for bank transfer
   :width: 720px
   :align: center

   Customer invoice configured for bank transfer.

As soon as you create and verify the invoice, you will see the invoice
reference number, label 1 in the next image. Then you can ask your customer to
pay it.

.. figure:: ../../images/libeufinconnector-dolibarr/co3.png
   :alt: Verified customer invoice with invoice reference
   :width: 720px
   :align: center

   Verified customer invoice with invoice reference.

As part of this tutorial, we will act as the customer. For this, we use the
special tutorial/demo screen. Press the LibEuFin Connector top navigation bar
button, label 1, then press the **Tutorial** button, label 2. After this, make
sure to input all the correct data into the fields, label 3, and press
**Create fake incoming**, label 4.

For the tutorial, I will showcase 2 possibilities:

1. The reference includes the correct number to match.
2. The reference includes an incorrect number, and the system cannot match it
   automatically.

Starting from the correct number and auto-matching: this is pretty much assured
by using the ``IN2604-0001`` string in the **Ref.** block. For testing, I am
also adding a ``#2`` payment, which is a second partial payment.

.. figure:: ../../images/libeufinconnector-dolibarr/co4.png
   :alt: Creating fake incoming payments with a matching invoice reference
   :width: 720px
   :align: center

   Creating fake incoming payments with a matching invoice reference.

Now that both payments are created, we can go to the ``Transactions`` page,
label 1, and press **Stage incoming fetch**, label 2. As soon as you press the
button, the page reloads. When it is done, notifications show up, label 3,
notifying you how many objects are there. Check the next picture.

.. figure:: ../../images/libeufinconnector-dolibarr/co5.png
   :alt: Incoming transaction fetch with automatically matched payments
   :width: 720px
   :align: center

   Incoming transaction fetch with automatically matched payments.

Now we can go and create the payment whose reference does not contain the
invoice reference.

.. figure:: ../../images/libeufinconnector-dolibarr/co6.png
   :alt: Creating a fake incoming payment without a matching invoice reference
   :width: 720px
   :align: center

   Creating a fake incoming payment without a matching invoice reference.

Run **Stage incoming fetch** again. As soon as the reload happens, the
notifications show up, telling you that there are unmatched transactions. For
this, press the link at label 2, and the transaction page will open.

.. figure:: ../../images/libeufinconnector-dolibarr/co7.png
   :alt: Notification about unmatched incoming transactions
   :width: 720px
   :align: center

   Notification about unmatched incoming transactions.

A page like the next one will open. To match it, scroll down to the manual
connection and select the object against which you want to make a link:

1. Invoice/credit note
2. Payment
3. Bank line

Now I select the invoice and press the save button, label 1 in the next
picture. After pressing this button, links appear in the region labeled with 2.

.. figure:: ../../images/libeufinconnector-dolibarr/co8.png
   :alt: Manual matching of an incoming transaction
   :width: 720px
   :align: center

   Manual matching of an incoming transaction.

What we now want to verify is that this object has actually counted for the
invoice amount. For this, return to the order invoice. There you can find
payments, label 1 in the next picture, that were done by the customer, or in
this case by us acting as the fake customer.

.. figure:: ../../images/libeufinconnector-dolibarr/co9.png
   :alt: Customer invoice showing linked payments
   :width: 720px
   :align: center

   Customer invoice showing linked payments.

We can as well finish it by creating the final payment, in my case ``#4``, and
fetching it.

.. figure:: ../../images/libeufinconnector-dolibarr/co10.png
   :alt: Creating the final incoming payment for the customer invoice
   :width: 720px
   :align: center

   Creating the final incoming payment for the customer invoice.

After updating the customer payment, we can see that the order is paid.
Afterwards, you can follow your internal business steps to verify the payment
and finalize the order.

.. figure:: ../../images/libeufinconnector-dolibarr/co11.png
   :alt: Customer invoice marked as paid after incoming transactions
   :width: 720px
   :align: center

   Customer invoice marked as paid after incoming transactions.

Supplier order credit note
--------------------------

This logic is added to support cases where the company that you bought
something from is supposed to send you a refund by bank transfer. For this
showcase, we use a supplier order from the
:ref:`libeufinconnector_dolibarr_transactions_outgoing` tutorial.

The logic is quite similar to the customer order invoice.

Firstly, you have to create a credit note for the supplier invoice.

.. figure:: ../../images/libeufinconnector-dolibarr/socn1.png
   :alt: Supplier invoice credit note in Dolibarr
   :width: 720px
   :align: center

   Supplier invoice credit note in Dolibarr.

After this, similarly to the customer invoice, go to the tutorial page and
create a payment for this credit note. Check the next picture for how to do it.

.. figure:: ../../images/libeufinconnector-dolibarr/socn2.png
   :alt: Creating a fake incoming payment for a supplier credit note
   :width: 720px
   :align: center

   Creating a fake incoming payment for a supplier credit note.

Navigate to the ``Transactions`` page, label 1, and press the
**Stage incoming fetch** button, label 2. After some time and a page reload, a
note appears notifying you that the incoming transaction was auto-imported,
label 3.

.. figure:: ../../images/libeufinconnector-dolibarr/socn3.png
   :alt: Auto-imported incoming transaction for supplier credit note
   :width: 720px
   :align: center

   Auto-imported incoming transaction for supplier credit note.

After adding a couple more incoming payments, we can go to the credit note page
and investigate that it was paid out. As previously, it is up to business rules
to decide how to proceed forward and make the full resolution. As well as in
the previous user example, you can make a manual match against the credit note,
as previously we did for the customer invoice.

.. figure:: ../../images/libeufinconnector-dolibarr/socn4.png
   :alt: Supplier credit note after incoming payments
   :width: 720px
   :align: center

   Supplier credit note after incoming payments.

Next step
---------

As you have observed how incoming transactions work, you are really welcome to
check the :ref:`libeufinconnector_dolibarr_transactions_outgoing` tutorial. If
you still have not installed the module, check
:ref:`libeufinconnector_dolibarr_install_config`.

For details on the standard Dolibarr objects used here, also check the official
Dolibarr documentation for
`customer invoices <https://wiki.dolibarr.org/index.php/Module_Customers_Invoices>`_
and
`supplier invoices <https://wiki.dolibarr.org/index.php/Module_Suppliers_Invoices>`_.
