Skip to content

Let a credit note say which invoice it corrects (fk_facture_source) - #29

Open
Pichinov-Jose wants to merge 1 commit into
SplashSync:2.0from
Pichinov-Jose:fix/creditnote-source-invoice
Open

Pichinov-Jose wants to merge 1 commit into
SplashSync:2.0from
Pichinov-Jose:fix/creditnote-source-invoice

Conversation

@Pichinov-Jose

Copy link
Copy Markdown

Closes #28.

The gap

The module already builds credit notes. src/Objects/CreditNote.php reuses the invoice traits, and
Invoice/CRUDTrait sets type = Facture::TYPE_CREDIT_NOTE on create. What it cannot express is
which invoice the credit note corrects: fk_facture_source appears nowhere in src/.

So every credit note that reaches Dolibarr through Splash arrives an orphan.

That link is not decoration:

  • the source invoice stays settled at its full amount, so the customer ledger never nets out;
  • Dolibarr's own screen cannot offer convert into a discountcompta/facture/card.php reaches
    confirm_converttoreduc through the source invoice, and that is the correct treatment when the
    money never actually moved (a waived fee, a goodwill gesture, a 100 % discount);
  • reporting cannot pair a credit note with what it corrects.

The change

fk_facture_source is exposed as an Invoice object id, declared only when the object is a
CreditNote, so standard invoices are untouched:

if ($this instanceof CreditNote) {
    $this->fieldsFactory()->create((string) self::objects()->encode("Invoice", SPL_T_ID))
        ->identifier("fk_facture_source")
        ->name($langs->trans("CorrectInvoice"))
        ->microData("http://schema.org/Invoice", "referencesOrder")
        ->isIndexed()
    ;
}

Reads encode the stored id back into an Invoice object id, or null when unset.

Writes are checked before they land

Dolibarr stores this link as a bare integer with no foreign key. An invalid value is therefore
accepted silently and only surfaces much later, when the source invoice fails to load — at which
point the trail is cold. isValidSourceInvoice() refuses three cases up front:

  • an id that matches no invoice;
  • an id pointing at another credit note;
  • an id pointing at the credit note itself.

Each refusal is logged with the offending id rather than failing quietly.

Verified

Replayed against a production install carrying 109 credit notes: all 109 resolve to a valid source
under this rule, and the three rejection cases are correctly refused while a genuine invoice is
accepted.

One question for the maintainers

CreditNote currently carries protected static bool $disabled = true;, so the object is not
offered at all. This PR deliberately does not touch that flag — enabling the object is a product
decision, not a bug fix. But the field is inert until it is lifted, so I would rather ask than
assume: is the object disabled because it is unfinished, or on purpose?

Related

Filed as SplashSync/Wordpress#17: WooCommerce refunds are never exported — there is no CreditNote
object and no refund hook on that side, so nothing produces the credit note this field would
receive. The two together are what make a refund representable end to end; this one is the half that
can be fixed today.

The module already builds credit notes: CreditNote.php reuses the invoice
traits and Invoice/CRUDTrait sets type = Facture::TYPE_CREDIT_NOTE on create.
What it cannot express is fk_facture_source, so every credit note arrives an
orphan.

That link is not decoration. Without it the source invoice stays settled at
its full amount, the customer ledger never nets out, and Dolibarr's own screen
cannot offer "convert into a discount" — compta/facture/card.php reaches that
action through the source invoice.

Adds fk_facture_source as an Invoice object id, declared only when the object
is a CreditNote so standard invoices are untouched.

Writes are checked before they land. Dolibarr stores the link as a bare id
with no foreign key, so an invalid value would be accepted silently and only
surface later, when the source fails to load. isValidSourceInvoice() refuses
an id that does not exist, one that points at another credit note, and one
that points at the credit note itself.

Verified against a production install: all 109 credit notes resolve to a valid
source under this rule, and the three rejection cases are refused.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CreditNote cannot be attached to its source invoice — fk_facture_source is not exposed

1 participant