diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 3440378a341..9cec181e645 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -174,6 +174,7 @@ class Paiement extends CommonObject $resql=$this->db->query($sql); if ($resql) { + // If we want to closed payed invoices if ($closepaidinvoices) { $invoice=new Facture($this->db); diff --git a/htdocs/fourn/class/fournisseur.facture.class.php b/htdocs/fourn/class/fournisseur.facture.class.php index f3d9bddbf84..b0673300fe9 100644 --- a/htdocs/fourn/class/fournisseur.facture.class.php +++ b/htdocs/fourn/class/fournisseur.facture.class.php @@ -638,9 +638,9 @@ class FactureFournisseur extends Facture } /** - * \brief Tag la facture comme payee completement - * \param user Objet utilisateur qui modifie l'etat - * \return int <0 si ko, >0 si ok + * Tag invoice as a payed invoice + * @param user Object user + * @return int <0 si ko, >0 si ok */ function set_paid($user) { diff --git a/htdocs/fourn/class/paiementfourn.class.php b/htdocs/fourn/class/paiementfourn.class.php index ea4bd666e1e..9070c567018 100644 --- a/htdocs/fourn/class/paiementfourn.class.php +++ b/htdocs/fourn/class/paiementfourn.class.php @@ -116,11 +116,12 @@ class PaiementFourn extends Paiement } /** - * \brief Create payment in database - * \param user Object of creating user - * \return int id of created payment, < 0 if error + * Create payment in database + * @param user Object of creating user + * @param closepaidinvoices 1=Also close payed invoices to paid, 0=Do nothing more + * @return int id of created payment, < 0 if error */ - function create($user) + function create($user,$closepaidinvoices=0) { global $langs,$conf; @@ -162,11 +163,34 @@ class PaiementFourn extends Paiement $amount = price2num($amount); $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'paiementfourn_facturefourn (fk_facturefourn, fk_paiementfourn, amount)'; $sql .= ' VALUES ('.$facid.','. $this->id.',\''.$amount.'\')'; - if (! $this->db->query($sql) ) + $resql=$this->db->query($sql); + if ($resql) + { + // If we want to closed payed invoices + if ($closepaidinvoices) + { + $invoice=new FactureFournisseur($this->db); + $invoice->fetch($facid); + $paiement = $invoice->getSommePaiement(); + //$creditnotes=$invoice->getSumCreditNotesUsed(); + $creditnotes=0; + //$deposits=$invoice->getSumDepositsUsed(); + $deposits=0; + $alreadypayed=price2num($paiement + $creditnotes + $deposits,'MT'); + $remaintopay=price2num($invoice->total_ttc - $paiement - $creditnotes - $deposits,'MT'); + if ($remaintopay == 0) + { + $result=$invoice->set_paid($user,'',''); + } + else dol_syslog("Remain to pay for invoice ".$facid." not null. We do nothing."); + } + } + else { dol_syslog('Paiement::Create Erreur INSERT dans paiement_facture '.$facid); $error++; } + } else { diff --git a/htdocs/fourn/facture/paiement.php b/htdocs/fourn/facture/paiement.php index 0ebf412e0ae..4d702718b8d 100644 --- a/htdocs/fourn/facture/paiement.php +++ b/htdocs/fourn/facture/paiement.php @@ -128,11 +128,10 @@ if ($action == 'add_paiement') $paiement->paiementid = $_POST['paiementid']; $paiement->num_paiement = $_POST['num_paiement']; $paiement->note = $_POST['comment']; - if (! $error) { - $paiement_id = $paiement->create($user); - if (! $paiement_id > 0) + $paiement_id = $paiement->create($user,(GETPOST('closepaidinvoices')=='on'?1:0)); + if ($paiement_id < 0) { $errmsg='