diff --git a/htdocs/compta/bank/account.class.php b/htdocs/compta/bank/account.class.php index e799e7b96c4..9cb871e9e2d 100644 --- a/htdocs/compta/bank/account.class.php +++ b/htdocs/compta/bank/account.class.php @@ -78,23 +78,39 @@ class Account return 1; } - /* - * Efface une entree dans la table ".MAIN_DB_PREFIX."bank + /** + * \brief Efface une entree dans la table ".MAIN_DB_PREFIX."bank + * \param rowid Id de l'ecriture a effacer + * \return int <0 si ko, >0 si ok */ function deleteline($rowid) { + $nbko=0; + $this->db->begin(); - $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=$rowid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_class WHERE lineid=".$rowid; $result = $this->db->query($sql); + if (! $result) $nbko++; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=$rowid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank WHERE rowid=".$rowid; $result = $this->db->query($sql); + if (! $result) $nbko++; - $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=$rowid"; + $sql = "DELETE FROM ".MAIN_DB_PREFIX."bank_url WHERE fk_bank=".$rowid; $result = $this->db->query($sql); + if (! $result) $nbko++; - $this->db->commit(); + if (! $nbko) + { + $this->db->commit(); + return 1; + } + else + { + $this->db->rollback(); + return -$nbko; + } } /** diff --git a/htdocs/compta/bank/ligne.php b/htdocs/compta/bank/ligne.php index 2cde72a2d1c..eedad88ea8b 100644 --- a/htdocs/compta/bank/ligne.php +++ b/htdocs/compta/bank/ligne.php @@ -333,17 +333,23 @@ if ($result) foreach($links as $key=>$val) { if ($key) print '
'; - print ''; if ($links[$key]['type']=='payment') { + print ''; print img_object($langs->trans('ShowPayment'),'payment').' '; print $langs->trans("Payment"); + print ''; } else if ($links[$key]['type']=='company') { + print ''; print img_object($langs->trans('ShowCustomer'),'company').' '; print $links[$key]['label']; + print ''; + } + else { + print ''; + print $links[$key]['label']; + print ''; } - else print $links[$key]['label']; - print ''; } print ' '; } diff --git a/htdocs/compta/paiement/fiche.php b/htdocs/compta/paiement/fiche.php index 1d8170cc7b2..5233155ec67 100644 --- a/htdocs/compta/paiement/fiche.php +++ b/htdocs/compta/paiement/fiche.php @@ -38,48 +38,53 @@ $langs->load('bills'); $langs->load('banks'); $langs->load('companies'); +$mesg=''; +/* + * Actions + */ + if ($_POST['action'] == 'confirm_delete' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) { + $db->begin(); + $paiement = new Paiement($db); $paiement->fetch($_GET['id']); - $bank_line_id = $paiement->bank_line; - $deleted = $paiement->delete(); - if ($deleted) + $result = $paiement->delete(); + if ($result > 0) { - // Supprimer l'écriture bancaire - if (!empty($bank_line_id)) - { - $acc = new Account($db); - $acc->deleteline($bank_line_id); - Header('Location: liste.php'); - } - else - { - print 'bank_line_id='.$bank_line_id; - exit; - } + $db->commit(); + Header("Location: liste.php"); + exit; } else { - print 'deleted='.$deleted; - exit; + $mesg='
'.$paiement->error.'
'; + $db->rollback(); } } if ($_POST['action'] == 'confirm_valide' && $_POST['confirm'] == 'yes' && $user->rights->facture->creer) { + $db->begin(); + $paiement = new Paiement($db); $paiement->id = $_GET['id']; if ( $paiement->valide() == 0 ) { + $db->commit(); Header('Location: fiche.php?id='.$paiement->id); + exit; + } + else + { + $mesg='
'.$paiement->error.'
'; + $db->rollback(); } } - /* * Visualisation de la fiche */ @@ -123,27 +128,39 @@ if ($_GET['action'] == 'valide') print '
'; } + +if ($mesg) print $mesg.'
'; + + print ''; -print ''; -if ($paiement->bank_account) +print ''; +if ($conf->banque->enabled) { - // Si compte renseigné, on affiche libelle - $bank=new Account($db); - $bank->fetch($paiement->bank_account); - - print ''; - print ''; - print ''; + if ($paiement->bank_account) + { + // Si compte renseigné, on affiche libelle + $bank=new Account($db); + $bank->fetch($paiement->bank_account); + + $bankline=new AccountLine($db); + $bankline->fetch($paiement->bank_line); + + print ''; + print ''; + print ''; + print ''; + print ''; + } } -print ''; -print ''; +print ''; +print ''; if ($paiement->numero) { - print ''; + print ''; } -print ''; -print ''; +print ''; +print ''; print '
'.$langs->trans('Ref').''.$paiement->id.'
'.$langs->trans('Ref').''.$paiement->id.'
'.$langs->trans('BankAccount').''.img_object($langs->trans("ShowAccount"),'account').' '.$bank->label.'
'.$langs->trans('BankAccount').''.img_object($langs->trans("ShowAccount"),'account').' '.$bank->label.''.$langs->trans("BankLineConciliated").''.yn($bankline->rappro).'
'.$langs->trans('Date').''.dolibarr_print_date($paiement->date).'
'.$langs->trans('Type').''.$paiement->type_libelle.'
'.$langs->trans('Date').''.dolibarr_print_date($paiement->date).'
'.$langs->trans('Type').''.$paiement->type_libelle.'
'.$langs->trans('Numero').''.$paiement->numero.'
'.$langs->trans('Numero').''.$paiement->numero.'
'.$langs->trans('Amount').''.price($paiement->montant).' '.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Note').''.nl2br($paiement->note).'
'.$langs->trans('Amount').''.price($paiement->montant).' '.$langs->trans('Currency'.$conf->monnaie).'
'.$langs->trans('Note').''.nl2br($paiement->note).'
'; diff --git a/htdocs/compta/paiement/liste.php b/htdocs/compta/paiement/liste.php index eb33f9070bf..bc7bcef9905 100644 --- a/htdocs/compta/paiement/liste.php +++ b/htdocs/compta/paiement/liste.php @@ -147,7 +147,10 @@ if ($resql) print ''.dolibarr_print_date($objp->dp).''; print ''.$objp->paiement_type.' '.$objp->num_paiement.''; - print ''.$objp->label.''; + print ''; + if ($objp->bid) print ''.img_object($langs->trans("ShowAccount"),'account').' '.$objp->label.''; + else print ' '; + print ''; print ''.price($objp->amount).''; print ''; diff --git a/htdocs/facture.class.php b/htdocs/facture.class.php index cfb58d765c8..3f71a8c296e 100644 --- a/htdocs/facture.class.php +++ b/htdocs/facture.class.php @@ -71,8 +71,9 @@ class Facture function Facture($DB, $soc_idp='', $facid='') { $this->db = $DB ; + + $this->id = $facid; $this->socidp = $soc_idp; - $this->products = array(); // Tableau de lignes de factures $this->amount = 0; $this->remise = 0; @@ -81,9 +82,10 @@ class Facture $this->total = 0; $this->propalid = 0; $this->projetid = 0; - $this->id = $facid; $this->prefixe_facture = ''; // utilisé dans le module de numérotation saturne $this->remise_exceptionnelle = 0; + + $this->products = array(); // Tableau de lignes de factures } /** @@ -716,9 +718,11 @@ class Facture */ function set_unpayed($rowid) { + dolibarr_syslog("Facture.class.php::set_unpayed rowid=".$rowid); $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture set paye=0 WHERE rowid = '.$rowid ; $resql = $this->db->query( $sql); } + /** * \brief Tag la facture comme payer partiellement * \param rowid id de la facture à modifier diff --git a/htdocs/langs/en_US/banks.lang b/htdocs/langs/en_US/banks.lang index 1919cedd3dc..0f9823fa16e 100644 --- a/htdocs/langs/en_US/banks.lang +++ b/htdocs/langs/en_US/banks.lang @@ -76,4 +76,5 @@ LineRecord=Transaction AddBankRecord=Add transaction AddBankRecordLong=Add transaction manually ConciliatedBy=Conciliated by -DateConciliating=Conciliate date \ No newline at end of file +DateConciliating=Conciliate date +BankLineConciliated=Transaction conciliated \ No newline at end of file diff --git a/htdocs/langs/fr_FR/banks.lang b/htdocs/langs/fr_FR/banks.lang index 4e82ececf82..9acda26b069 100644 --- a/htdocs/langs/fr_FR/banks.lang +++ b/htdocs/langs/fr_FR/banks.lang @@ -76,4 +76,5 @@ LineRecord=Ecriture AddBankRecord=Ajouter écriture AddBankRecordLong=Saisie d'une écriture manuelle hors facture ConciliatedBy=Rapproché par -DateConciliating=Date rapprochement \ No newline at end of file +DateConciliating=Date rapprochement +BankLineConciliated=Ecriture rapprochée \ No newline at end of file diff --git a/htdocs/paiement.class.php b/htdocs/paiement.class.php index 03f9ffa36cd..92d6b91c037 100644 --- a/htdocs/paiement.class.php +++ b/htdocs/paiement.class.php @@ -209,40 +209,98 @@ class Paiement $form->select($name, $sql, $id); } - /** - * - * - * - */ - + /** + * \brief Supprime un paiement ainsi que les lignes qu'il a généré dans comptes + * Si le paiement porte sur un écriture compte qui est rapprochée, on refuse + * Si le paiement porte sur au moins une facture à "payée", on refuse + * \return int <0 si ko, >0 si ok + */ function delete() { + $bank_line_id = $this->bank_line; + $this->db->begin(); - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture WHERE fk_paiement = '.$this->id; + + // Vérifier si paiement porte pas sur une facture à l'état payée + // Si c'est le cas, on refuse la suppression + $billsarray=$this->getBillsArray('paye=1'); + if (is_array($billsarray)) + { + if (sizeof($billsarray)) + { + $this->error="Impossible de supprimer un paiement portant sur au moins une facture à l'état payé"; + $this->db->rollback(); + return -1; + } + } + else + { + $this->db->rollback(); + return -2; + } + + // Vérifier si paiement ne porte pas sur ecriture bancaire rapprochée + // Si c'est le cas, on refuse le paiement + if ($bank_line_id) + { + $accline = new AccountLine($this->db,$bank_line_id); + $accline->fetch($bank_line_id); + if ($accline->rappro) + { + $this->error="Impossible de supprimer un paiement qui a généré une écriture qui a été rapprochée"; + $this->db->rollback(); + return -3; + } + } + + // Efface la ligne de paiement (dans paiement_facture et paiement) + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement_facture'; + $sql.= ' WHERE fk_paiement = '.$this->id; $result = $this->db->query($sql); if ($result) { - $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement WHERE rowid = '.$this->id; + $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'paiement'; + $sql.= ' WHERE rowid = '.$this->id; $result = $this->db->query($sql); - $this->db->commit(); - return 1; + if (! $result) + { + $this->error=$this->db->error(); + $this->db->rollback(); + return -3; + } + + // Supprimer l'écriture bancaire si paiement lié à écriture + if ($bank_line_id) + { + $acc = new Account($this->db); + $result=$acc->deleteline($bank_line_id); + if ($result < 0) + { + $this->error=$acc->error; + $this->db->rollback(); + return -4; + } + } + + $this->db->commit(); + return 1; } else { - dolibarr_print_error($this->db); + $this->error=$this->db->error; $this->db->rollback(); - return 0; + return -5; } } - /* - * Mise a jour du lien entre le paiement et la ligne générée dans llx_bank - * - */ - + /** + * \brief Mise a jour du lien entre le paiement et la ligne générée dans llx_bank + * \param id_bank Id compte bancaire + */ function update_fk_bank($id_bank) { - $sql = 'UPDATE llx_paiement set fk_bank = '.$id_bank.' where rowid = '.$this->id; + $sql = 'UPDATE llx_paiement set fk_bank = '.$id_bank; + $sql.= ' WHERE rowid = '.$this->id; $result = $this->db->query($sql); if ($result) { @@ -255,9 +313,10 @@ class Paiement } } - /** - * \brief Valide le paiement - */ + /** + * \brief Valide le paiement + * \return int <0 si ko, >0 si ok + */ function valide() { $sql = 'UPDATE '.MAIN_DB_PREFIX.'paiement SET statut = 1 WHERE rowid = '.$this->id; @@ -273,12 +332,11 @@ class Paiement } } - /* - * \brief Information sur l'objet - * \param id id du paiement dont il faut afficher les infos - */ - - function info($id) + /* + * \brief Information sur l'objet + * \param id id du paiement dont il faut afficher les infos + */ + function info($id) { $sql = 'SELECT c.rowid, '.$this->db->pdate('datec').' as datec, fk_user_creat, fk_user_modif'; $sql .= ', '.$this->db->pdate('tms').' as tms'; @@ -313,5 +371,41 @@ class Paiement dolibarr_print_error($this->db); } } + + /** + * \brief Retourne la liste des factures sur lesquels porte le paiement + * \param filter Critere de filtre + * \return array Tableau des id de factures + */ + function getBillsArray($filter='') + { + $sql = 'SELECT fk_facture'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf, '.MAIN_DB_PREFIX.'facture as f'; + $sql.= ' WHERE pf.fk_facture = f.rowid AND fk_paiement = '.$this->id; + if ($filter) $sql.= ' AND '.$filter; + $resql = $this->db->query($sql); + if ($resql) + { + $i=0; + $num=$this->db->num_rows($resql); + $billsarray=array(); + + while ($i < $num) + { + $obj = $this->db->fetch_object($resql); + $billsarray[$i]=$obj->fk_facture; + $i++; + } + + return $billsarray; + } + else + { + $this->error=$this->db->error(); + dolibarr_syslog('Paiement::getBillsArray Error '.$this->error.' - sql='.$sql); + return -1; + } + } + } ?>