Skip to content

Conversation

@atm-irvine
Copy link
Contributor

FIX|Fix bad computation of rest column for invoices on list

I've added getAvailableDiscounts method to get the good rest


$totalallpayments = $paiement + $totalcreditnotes + $totaldeposits;
$remaintopay = $obj->total_ttc - $totalallpayments;
$remaintopay = -$discount->getAvailableDiscounts($companystatic, '', 'fk_facture_source=' . $facturestatic->id);
Copy link
Member

@eldy eldy Apr 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The getAvailableDiscounts with this parameters seems to return the amount of discount generated by a credit note that have the invoice as source, not the remain to pay of the invoice.
The remain to pay is the total to pay minus all things that reduces the amount to pay, so the credit notes applied to the invoice, the deposits and the payments already done.
Old code seems correct.

Can you provide a screenshot of error and how to reproduce the bug ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eldy The problem arises when you declare an overpayment, for example.
Example:
Invoice of €500, payment received of €1000 => "Créance" field indicated in the invoice list = -€500
If the overpayment is transformed into a discount (button for this on the invoice) and then used in part to pay another invoice (breakdown of the discount on the customer file, customer tab, link to fixed discounts), say €400 is used to pay another invoice, leaving €100 of overpayment => In this case, the "Créance" on the original invoice is still -€500, which is not right, it should show -€100.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So pb is when we convert the overpayment into a discount. Once this is done, the balance of invoice is no more -500 but 0 because the overpaid amount has been moved into another area and will be consumed independently.
Balance for user should be:
invoice 0 euro + available discount = 500 euros
So we should removed, in the existing calculated "remain to pay", the amount of converted discount that is no more due because given as discount.

@eldy eldy added the Discussion Some questions or discussions are opened and wait answers of author or other people to be processed label Apr 7, 2025
@eldy
Copy link
Member

eldy commented Apr 9, 2025

The valu shown must follow the same business rule than the business rule defined to show the invoice (compta/facture/card.php)

The current rule in compta/facture/card.php to define amount paid and remain to pay is:

	$totalpaid = $object->getSommePaiement();
	$totalcreditnotes = $object->getSumCreditNotesUsed();
	$totaldeposits = $object->getSumDepositsUsed();
	//print "totalpaid=".$totalpaid." totalcreditnotes=".$totalcreditnotes." totaldeposts=".$totaldeposits."
	// selleruserrevenuestamp=".$selleruserevenustamp;

	// We can also use bcadd to avoid pb with floating points
	// For example print 239.2 - 229.3 - 9.9; does not return 0.
	$resteapayer = price2num($object->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');

	// Multicurrency
	if (isModEnabled('multicurrency')) {
		$multicurrency_totalpaid = $object->getSommePaiement(1);
		$multicurrency_totalcreditnotes = $object->getSumCreditNotesUsed(1);
		$multicurrency_totaldeposits = $object->getSumDepositsUsed(1);
		$multicurrency_resteapayer = price2num($object->multicurrency_total_ttc - $multicurrency_totalpaid - $multicurrency_totalcreditnotes - $multicurrency_totaldeposits, 'MT');
		// Code to fix case of corrupted data
		// TODO We should not need this. Also data comes from a not reliable value of $object->multicurrency_total_ttc that may be wrong if it was
		// calculated by summing lines that were in a currency for some of them and into another for others (lines from discount/down payment into another currency for example)
		if ($resteapayer == 0 && $multicurrency_resteapayer != 0 && $object->multicurrency_code != $conf->currency) {
			$resteapayer = price2num((float) $multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
		}
	}

	if ($object->paye) {
		$resteapayer = 0;
	}

So the difference with the list is that we have at end

if ($object->paye) {
		$resteapayer = 0;
}

in the card that we don't have.
So i think the fix should be to add this test only.
Can you check if it works as expected for you with this simple change ?

@atm-irvine
Copy link
Contributor Author

@eldy The correct amount is on the comm/remx.php page. In the section "Réductions ou crédits disponibles", we have a list of overpayment invoice with discount available. The amount of discount available is correct in this section.

@eldy
Copy link
Member

eldy commented Apr 12, 2025

@eldy The correct amount is on the comm/remx.php page. In the section "Réductions ou crédits disponibles", we have a list of overpayment invoice with discount available. The amount of discount available is correct in this section.

The value into remx are the discount available for consumption into other invoices, not the remain to pay of an invoice.

If you have an invoice of 100, then you pay 20, then you consume an old credit not of 30, and you apply a commercial discount of 10, the remain to pay is 40.
You won't fin 'd any entry into comx with a value of 40. You will just find an entryvfor the 30 of the old credit not to consume it and an entry of 10 for the commercial disount to consume.

I think the trouble of overpayment has a different origin. The calculation for column value must be the same than when reading the card. I pushed recently a fix on develop to align the code on list with the one on code so you should find a remain to pay of 0 when there is an overpayment now.
Fix may be backported.

@rycks
Copy link
Contributor

rycks commented Sep 18, 2025

I pushed recently a fix on develop to align the code on list with the one on code so you should find a remain to pay of 0 when there is an overpayment now.

@eldy did you have the commit link of that for the backport ?

or at least the file name impacted by your fix ?

@eldy
Copy link
Member

eldy commented Oct 2, 2025

Sorry it was a long time ago, and i can't retreive where the fix was. I think it was "several fixes" to fix the bad consistency.

Nevermind, on recent version, the remain to pay is calculated with

$totalpaid = $object->getSommePaiement();
$totalcreditnotes = $object->getSumCreditNotesUsed();
$totaldeposits = $object->getSumDepositsUsed();
$resteapayer = price2num($object->total_ttc - $totalpaid - $totalcreditnotes - $totaldeposits, 'MT');

And the part of amount of excess received seems already included into the getSumCreditNotesUsed(). So don't need to use the getAvailableDiscounts(). So i surely miss something, so the most important is to be able to find a use case to reproduce the bug. I failed to find one. So if you can describe a proces to reproduct it we may progress on this.

Seems duplicate of need for #33191

@eldy eldy added the PR to fix - See feedback in comments PR needs to be fixed to be integrated (some comments should describes the fix to do) label Oct 2, 2025
@the-dolibear-bot-for-v18 the-dolibear-bot-for-v18 bot added the Issue for v18 maintenance Team PR is in a maintenance branch with several approvers. Waiting approval of all of them. label Oct 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Discussion Some questions or discussions are opened and wait answers of author or other people to be processed Issue for v18 maintenance Team PR is in a maintenance branch with several approvers. Waiting approval of all of them. PR to fix - See feedback in comments PR needs to be fixed to be integrated (some comments should describes the fix to do)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants