diff --git a/htdocs/compta/facture/card.php b/htdocs/compta/facture/card.php
index da77b75d9a6..80c7bafc3d7 100644
--- a/htdocs/compta/facture/card.php
+++ b/htdocs/compta/facture/card.php
@@ -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 ''.$langs->trans('DoPayment').'';
} 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 '
' . $langs->trans('DoPayment') . '
';
- //} else {
+ if ($object->type == Facture::TYPE_DEPOSIT && $resteapayer == 0) {
+ // For down payment, we refuse to receive more than amount to pay.
+ print ''.$langs->trans('DoPayment').'';
+ } 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 ''.$langs->trans('DoPayment').'';
- //}
+ }
}
}
+ $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 'id.'&action=converttoreduc" title="'.dol_escape_htmltag($langs->trans("ConfirmConvertToReduc2")).'">'.$langs->trans('ConvertToReduc').'';
}
// For deposit invoice
if ($object->type == Facture::TYPE_DEPOSIT && $usercancreate && $object->statut > 0 && empty($discount->id)) {
- print 'id.'&action=converttoreduc">'.$langs->trans('ConvertToReduc').'';
+ 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 'id.'&action=converttoreduc">'.$langs->trans('ConvertToReduc').'';
+ } else {
+ print ''.$langs->trans('ConvertToReduc').'';
+ }
}
}
// 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 ''.$langs->trans('ClassifyPaid').'';
+ 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 ''.$langs->trans('ClassifyPaid').'';
+ } else {
+ print ''.$langs->trans('ClassifyPaid').'';
+ }
}
// Classify 'closed not completely paid' (possible if validated and not yet filed paid)
diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php
index 971400afa5e..6b5ab4ef333 100644
--- a/htdocs/compta/paiement/class/paiement.class.php
+++ b/htdocs/compta/paiement/class/paiement.class.php
@@ -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";
diff --git a/htdocs/core/class/commoninvoice.class.php b/htdocs/core/class/commoninvoice.class.php
index 605ffec63bd..f6edbd3f8d4 100644
--- a/htdocs/core/class/commoninvoice.class.php
+++ b/htdocs/core/class/commoninvoice.class.php
@@ -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 {
diff --git a/htdocs/fourn/facture/card.php b/htdocs/fourn/facture/card.php
index 79a93cf3b0c..9efb21c4594 100644
--- a/htdocs/fourn/facture/card.php
+++ b/htdocs/fourn/facture/card.php
@@ -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) {
diff --git a/htdocs/install/mysql/migration/repair.sql b/htdocs/install/mysql/migration/repair.sql
index 5208a733769..4935577cbf8 100644
--- a/htdocs/install/mysql/migration/repair.sql
+++ b/htdocs/install/mysql/migration/repair.sql
@@ -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