Let a credit note say which invoice it corrects (fk_facture_source) - #29
Open
Pichinov-Jose wants to merge 1 commit into
Open
Pichinov-Jose wants to merge 1 commit into
Pichinov-Jose wants to merge 1 commit into
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Closes #28.
The gap
The module already builds credit notes.
src/Objects/CreditNote.phpreuses the invoice traits, andInvoice/CRUDTraitsetstype = Facture::TYPE_CREDIT_NOTEon create. What it cannot express iswhich invoice the credit note corrects:
fk_facture_sourceappears nowhere insrc/.So every credit note that reaches Dolibarr through Splash arrives an orphan.
That link is not decoration:
compta/facture/card.phpreachesconfirm_converttoreducthrough the source invoice, and that is the correct treatment when themoney never actually moved (a waived fee, a goodwill gesture, a 100 % discount);
The change
fk_facture_sourceis exposed as an Invoice object id, declared only when the object is aCreditNote, so standard invoices are untouched:Reads encode the stored id back into an
Invoiceobject id, ornullwhen 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: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
CreditNotecurrently carriesprotected static bool $disabled = true;, so the object is notoffered 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
CreditNoteobject 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.