diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 25d032982a9..b3d365650b5 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -486,14 +486,21 @@ else if ($action == 'confirm_canceled' && $confirm == 'yes') { // Convertir en reduc else if ($action == 'confirm_converttoreduc' && $confirm == 'yes' && $user->rights->facture->creer) { - $db->begin(); - $object->fetch($id); $object->fetch_thirdparty(); $object->fetch_lines(); - if (empty($object->paye)) // protection against multiple submit + // Check if there is already a discount (protection to avoid duplicate creation when resubmit post) + $discountcheck=new DiscountAbsolute($db); + $result=$discountcheck->fetch(0,$object->id); + + $canconvert=0; + if ($object->type == Facture::TYPE_DEPOSIT && $object->paye == 1 && empty($discountcheck->id)) $canconvert=1; // we can convert deposit into discount if deposit is payed completely and not already converted (see real condition into condition used to show button converttoreduc) + if ($object->type == Facture::TYPE_CREDIT_NOTE && $object->paye == 0 && empty($discountcheck->id)) $canconvert=1; // we can convert credit note into discount if credit note is not payed back and not already converted and amount of payment is 0 (see real condition into condition used to show button converttoreduc) + if ($canconvert) { + $db->begin(); + // Boucle sur chaque taux de tva $i = 0; foreach ($object->lines as $line) { @@ -1745,9 +1752,10 @@ if ($action == 'update_extras') { $action = 'edit_extras'; } + /* * View -*/ + */ $form = new Form($db); $formother = new FormOther($db); @@ -1941,10 +1949,12 @@ if ($action == 'create') $options .= ''; } - $facids = $facturestatic->list_qualified_avoir_invoices($soc->id); - if ($facids < 0) { - dol_print_error($db, $facturestatic); - exit(); + // Show link for credit note + $facids=$facturestatic->list_qualified_avoir_invoices($soc->id); + if ($facids < 0) + { + dol_print_error($db,$facturestatic); + exit; } $optionsav = ""; $newinvoice_static = new Facture($db); @@ -3277,10 +3287,11 @@ if ($action == 'create') } // Reopen a standard paid invoice - if (($object->type == Facture::TYPE_STANDARD || $object->type == Facture::TYPE_REPLACEMENT) && ($object->statut == Facture::TYPE_CREDIT_NOTE || $object->statut == Facture::TYPE_DEPOSIT) && $user->rights->facture->creer) // A paid - // invoice - // (partially or - // completely) + 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))) + && ($object->statut == 2 || $object->statut == 3) + && $user->rights->facture->creer) // A paid invoice (partially or completely) { if (! $objectidnext && $object->close_code != 'replaced') // Not replaced by another invoice { @@ -3339,30 +3350,47 @@ if ($action == 'create') // Reverse back money or convert to reduction if ($object->type == Facture::TYPE_CREDIT_NOTE || $object->type == Facture::TYPE_DEPOSIT) { // For credit note only - if ($object->type == Facture::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0 && $user->rights->facture->paiement) { - print '
'; + if ($object->type == Facture::TYPE_CREDIT_NOTE && $object->statut == 1 && $object->paye == 0 && $user->rights->facture->paiement) + { + if ($resteapayer == 0) + { + print '