Fix several pb with down payments

This commit is contained in:
Laurent Destailleur 2021-12-16 12:33:45 +01:00
parent 964656c728
commit 43e682b56f
5 changed files with 41 additions and 14 deletions

View File

@ -970,10 +970,13 @@ if (empty($reshook)) {
$object->fetch($id);
if ($object->statut == Facture::STATUS_VALIDATED && $object->paye == 0) {
$paiement = new Paiement($db);
$result = $paiement->fetch(GETPOST('paiement_id'));
$result = $paiement->fetch(GETPOST('paiement_id', 'int'));
if ($result > 0) {
$result = $paiement->delete(); // If fetch ok and found
header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
if ($result >= 0) {
header("Location: ".$_SERVER['PHP_SELF']."?id=".$id);
exit;
}
}
if ($result < 0) {
setEventMessages($paiement->error, $paiement->errors, 'errors');
@ -3857,7 +3860,6 @@ if ($action == 'create') {
// For example print 239.2 - 229.3 - 9.9; does not return 0.
// $resteapayer=bcadd($object->total_ttc,$totalpaye,$conf->global->MAIN_MAX_DECIMALS_TOT);
// $resteapayer=bcadd($resteapayer,$totalavoir,$conf->global->MAIN_MAX_DECIMALS_TOT);
$resteapayer = price2num($object->total_ttc - $totalpaye - $totalcreditnotes - $totaldeposits, 'MT');
// Multicurrency
@ -3866,7 +3868,10 @@ if ($action == 'create') {
$multicurrency_totalcreditnotes = $object->getSumCreditNotesUsed(1);
$multicurrency_totaldeposits = $object->getSumDepositsUsed(1);
$multicurrency_resteapayer = price2num($object->multicurrency_total_ttc - $multicurrency_totalpaye - $multicurrency_totalcreditnotes - $multicurrency_totaldeposits, 'MT');
$resteapayer = price2num($multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
// Code to fix case of corrupted data
if ($resteapayer == 0 && $multicurrency_resteapayer != 0) {
$resteapayer = price2num($multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
}
}
if ($object->paye) {
@ -5297,7 +5302,7 @@ if ($action == 'create') {
$discount = new DiscountAbsolute($db);
$result = $discount->fetch(0, $object->id);
// Reopen a standard paid invoice
// Reopen an invoice
if ((($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT)
|| ($object->type == Facture::TYPE_CREDIT_NOTE && empty($discount->id))
|| ($object->type == Facture::TYPE_DEPOSIT && empty($discount->id))
@ -5362,14 +5367,18 @@ if ($action == 'create') {
if ($objectidnext) {
print '<span class="butActionRefused classfortooltip" title="'.$langs->trans("DisabledBecauseReplacedInvoice").'">'.$langs->trans('DoPayment').'</span>';
} else {
//if ($resteapayer == 0) { // Sometimes we can receive more, so we accept to enter more and will offer a button to convert into discount (but it is not a credit note, just a prepayment done)
// print '<div class="inline-block divButAction"><span class="butActionRefused classfortooltip" title="' . $langs->trans("DisabledBecauseRemainderToPayIsZero") . '">' . $langs->trans('DoPayment') . '</span></div>';
//} else {
if ($object->type == Facture::TYPE_DEPOSIT && $resteapayer == 0) {
// For down payment, we refuse to receive more than amount to pay.
print '<span class="butActionRefused" title="'.$langs->trans("DisabledBecauseRemainderToPayIsZero").'">'.$langs->trans('DoPayment').'</span>';
} else {
// Sometimes we can receive more, so we accept to enter more and will offer a button to convert into discount (but it is not a credit note, just a prepayment done)
print '<a class="butAction" href="'.DOL_URL_ROOT.'/compta/paiement.php?facid='.$object->id.'&amp;action=create&amp;accountid='.$object->fk_account.'">'.$langs->trans('DoPayment').'</a>';
//}
}
}
}
$sumofpayment = $object->getSommePaiement();
// Reverse back money or convert to reduction
if ($object->type == Facture::TYPE_CREDIT_NOTE || $object->type == Facture::TYPE_DEPOSIT || $object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_SITUATION) {
// For credit note only
@ -5387,21 +5396,32 @@ if ($action == 'create') {
}
// For credit note
if ($object->type == Facture::TYPE_CREDIT_NOTE && $object->statut == Facture::STATUS_VALIDATED && $object->paye == 0 && $usercancreate
&& (!empty($conf->global->INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED) || $object->getSommePaiement() == 0)
&& (!empty($conf->global->INVOICE_ALLOW_REUSE_OF_CREDIT_WHEN_PARTIALLY_REFUNDED) || $sumofpayment == 0)
) {
print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc" title="'.dol_escape_htmltag($langs->trans("ConfirmConvertToReduc2")).'">'.$langs->trans('ConvertToReduc').'</a>';
}
// For deposit invoice
if ($object->type == Facture::TYPE_DEPOSIT && $usercancreate && $object->statut > 0 && empty($discount->id)) {
print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc">'.$langs->trans('ConvertToReduc').'</a>';
if (price2num($object->total_ttc, 'MT') == price2num($sumofpayment, 'MT')) {
// We can close a down payment only if paid amount is same than amount of down payment (by definition)
print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER["PHP_SELF"].'?facid='.$object->id.'&amp;action=converttoreduc">'.$langs->trans('ConvertToReduc').'</a>';
} else {
print '<span class="butActionRefused" title="'.$langs->trans("AmountPaidMustMatchAmountOfDownPayment").'">'.$langs->trans('ConvertToReduc').'</span>';
}
}
}
// Classify paid
if (($object->statut == Facture::STATUS_VALIDATED && $object->paye == 0 && $usercanissuepayment && (($object->type != Facture::TYPE_CREDIT_NOTE && $object->type != Facture::TYPE_DEPOSIT && $resteapayer <= 0) || ($object->type == Facture::TYPE_CREDIT_NOTE && $resteapayer >= 0)))
|| ($object->type == Facture::TYPE_DEPOSIT && $object->paye == 0 && $object->total_ttc > 0 && $resteapayer == 0 && $usercanissuepayment && empty($discount->id))
|| ($object->type == Facture::TYPE_DEPOSIT && $object->paye == 0 && $object->total_ttc > 0 && $resteapayer == 0 && $usercanissuepayment && !empty($discount->id)) // If down payment is paid and then converted in credit, we can close it.
) {
print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?facid='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaid').'</a>';
if (price2num($object->total_ttc, 'MT') == price2num($sumofpayment, 'MT')) {
// We can close a down payment only if paid amount is same than amount of down payment (by definition)
print '<a class="butAction'.($conf->use_javascript_ajax ? ' reposition' : '').'" href="'.$_SERVER['PHP_SELF'].'?facid='.$object->id.'&amp;action=paid">'.$langs->trans('ClassifyPaid').'</a>';
} else {
print '<span class="butActionRefused" title="'.$langs->trans("AmountPaidMustMatchAmountOfDownPayment").'">'.$langs->trans('ClassifyPaid').'</span>';
}
}
// Classify 'closed not completely paid' (possible if validated and not yet filed paid)

View File

@ -483,7 +483,7 @@ class Paiement extends CommonObject
// Verifier si paiement porte pas sur une facture classee
// Si c'est le cas, on refuse la suppression
$billsarray = $this->getBillsArray('fk_statut > 1');
$billsarray = $this->getBillsArray('f.fk_statut > 1');
if (is_array($billsarray)) {
if (count($billsarray)) {
$this->error = "ErrorDeletePaymentLinkedToAClosedInvoiceNotPossible";

View File

@ -168,6 +168,7 @@ abstract class CommonInvoice extends CommonObject
$discountstatic = new DiscountAbsolute($this->db);
$result = $discountstatic->getSumDepositsUsed($this, $multicurrency);
if ($result >= 0) {
return $result;
} else {

View File

@ -2412,7 +2412,10 @@ if ($action == 'create') {
$multicurrency_totalcreditnotes = $object->getSumCreditNotesUsed(1);
$multicurrency_totaldeposits = $object->getSumDepositsUsed(1);
$multicurrency_resteapayer = price2num($object->multicurrency_total_ttc - $multicurrency_totalpaye - $multicurrency_totalcreditnotes - $multicurrency_totaldeposits, 'MT');
$resteapayer = price2num($multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
// Code to fix case of corrupted data
if ($resteapayer == 0 && $multicurrency_resteapayer != 0) {
$resteapayer = price2num($multicurrency_resteapayer / $object->multicurrency_tx, 'MT');
}
}
if ($object->paye) {

View File

@ -534,6 +534,9 @@ UPDATE llx_facturedet SET situation_percent = 100 WHERE situation_percent IS NUL
--update llx_facture set paye = 1, fk_statut = 2 where rowid in (select rowid from tmp_invoice_deposit_mark_as_available);
-- TODO We should fix multicurrency_amount that are empty into llx_societe_remise_except, but we can't because we don't know what is the rate
-- We may retreive info fro minvoice line by using fk_facture_line or fk_facture_supplier_line
-- select * from llx_societe_remise_except where multicurrency_amount_ht = 0 and amount_ht <> 0;
-- Note to migrate from old counter aquarium to new one