diff --git a/htdocs/compta/paiement/cheque/card.php b/htdocs/compta/paiement/cheque/card.php index e2422bffa70..00fc0a4a7b9 100644 --- a/htdocs/compta/paiement/cheque/card.php +++ b/htdocs/compta/paiement/cheque/card.php @@ -221,7 +221,26 @@ if ($action == 'confirm_valide' && $confirm == 'yes' && $user->rights->banque->c } else { - setEventMessage($paiement->error, 'errors'); + setEventMessage($object->error, 'errors'); + } +} + +if ($action == 'confirm_reject_check' && $confirm == 'yes' && $user->rights->banque->cheque) +{ + $reject_date = dol_mktime(0, 0, 0, GETPOST('rejectdate_month'), GETPOST('rejectdate_day'), GETPOST('rejectdate_year')); + $rejected_check = GETPOST('bankid'); + + $object->fetch($id); + $paiement_id = $object->reject_check($rejected_check, $reject_date); + if ($paiement_id > 0) + { + setEventMessage($langs->trans("CheckRejectedAndInvoicesReopened")); + header("Location: ".DOL_URL_ROOT.'/compta/paiement/card.php?id='.$paiement_id); + exit; + } + else + { + setEventMessage($object->error, 'errors'); } } @@ -334,6 +353,18 @@ else print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans("ValidateCheckReceipt"), $langs->trans("ConfirmValidateCheckReceipt"), 'confirm_valide','','',1); } + + /* + * Confirm check rejection + */ + if ($action == 'reject_check') + { + $formquestion = array( + array('type' => 'hidden','name' => 'bankid','value' => GETPOST('lineid')), + array('type' => 'date','name' => 'rejectdate_','label' => $langs->trans("RejectCheckDate"),'value' => dol_now()) + ); + print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans("RejectCheck"), $langs->trans("ConfirmRejectCheck"), 'confirm_reject_check',$formquestion,'',1); + } } $accounts = array(); @@ -377,10 +408,9 @@ if ($action == 'new') $sql = "SELECT ba.rowid as bid, b.datec as datec, b.dateo as date, b.rowid as chqid, "; $sql.= " b.amount, ba.label, b.emetteur, b.num_chq, b.banque"; - $sql.= " FROM ".MAIN_DB_PREFIX."bank as b,"; - $sql.= " ".MAIN_DB_PREFIX."bank_account as ba"; + $sql.= " FROM ".MAIN_DB_PREFIX."bank as b"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank_account as ba ON (b.fk_account = ba.rowid)"; $sql.= " WHERE b.fk_type = 'CHQ'"; - $sql.= " AND b.fk_account = ba.rowid"; $sql.= " AND ba.entity IN (".getEntity('bank_account', 1).")"; $sql.= " AND b.fk_bordereau = 0"; $sql.= " AND b.amount > 0"; @@ -599,12 +629,11 @@ else // Liste des cheques $sql = "SELECT b.rowid, b.amount, b.num_chq, b.emetteur,"; $sql.= " b.dateo as date, b.datec as datec, b.banque,"; - $sql.= " p.rowid as pid, ba.rowid as bid"; + $sql.= " p.rowid as pid, ba.rowid as bid, p.statut"; $sql.= " FROM ".MAIN_DB_PREFIX."bank_account as ba"; - $sql.= ", ".MAIN_DB_PREFIX."bank as b"; + $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."bank as b ON (b.fk_account = ba.rowid)"; $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."paiement as p ON p.fk_bank = b.rowid"; - $sql.= " WHERE ba.rowid = b.fk_account"; - $sql.= " AND ba.entity IN (".getEntity('bank_account', 1).")"; + $sql.= " WHERE ba.entity IN (".getEntity('bank_account', 1).")"; $sql.= " AND b.fk_type= 'CHQ'"; $sql.= " AND b.fk_bordereau = ".$object->id; $sql.= " ORDER BY $sortfield $sortorder"; @@ -625,6 +654,7 @@ else print_liste_field_titre($langs->trans("Bank"),$_SERVER["PHP_SELF"],"b.banque", "",$param,"",$sortfield,$sortorder); print_liste_field_titre($langs->trans("Amount"),$_SERVER["PHP_SELF"],"b.amount", "",$param,'align="right"',$sortfield,$sortorder); print_liste_field_titre($langs->trans("LineRecord"),$_SERVER["PHP_SELF"],"b.rowid", "",$param,'align="center"',$sortfield,$sortorder); + print_liste_field_titre($langs->trans("Payment"),$_SERVER["PHP_SELF"],"p.rowid", "",$param,'align="center"',$sortfield,$sortorder); print_liste_field_titre('','',''); print "\n"; $i=1; @@ -654,13 +684,31 @@ else print ' '; } print ''; + print ''; + $paymentstatic->id=$objp->pid; + $paymentstatic->ref=$objp->pid; + if ($paymentstatic->id) + { + print $paymentstatic->getNomUrl(1); + } + else + { + print ' '; + } + print ''; if ($object->statut == 0) { print 'rowid.'">'.img_delete().''; } else { - print ' '; + if($objp->statut == 0) + { + print 'rowid.'">'.img_picto($langs->trans("RejectCheck"),'disable').''; + } + else if($objp->statut == 2) { + print ''.img_picto($langs->trans('CheckRejected'),'statut8').''; + } } print ''; $var=!$var; diff --git a/htdocs/compta/paiement/cheque/class/remisecheque.class.php b/htdocs/compta/paiement/cheque/class/remisecheque.class.php index 2dc1efe04b4..82047342d35 100644 --- a/htdocs/compta/paiement/cheque/class/remisecheque.class.php +++ b/htdocs/compta/paiement/cheque/class/remisecheque.class.php @@ -25,6 +25,7 @@ * \brief File with class to manage cheque delivery receipts */ require_once DOL_DOCUMENT_ROOT .'/core/class/commonobject.class.php'; +require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; /** @@ -647,17 +648,82 @@ class RemiseCheque extends CommonObject $resql = $this->db->query($sql); if ($resql) - { - $this->updateAmount(); - } - else - { - $this->errno = -1032; - dol_syslog("RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)"); - } + { + $this->updateAmount(); + } + else + { + $this->errno = -1032; + dol_syslog("RemiseCheque::removeCheck ERREUR UPDATE ($this->errno)"); + } } return 0; } + + /** + * Check rejection management + * Reopen linked invoices and saves a new negative payment + * + * @param int $bank_id Id of bank line concerned + * @param date $rejection_date Date to use on the negative payment + * @return int + */ + function reject_check($bank_id, $rejection_date) + { + global $db, $user; + + $payment = new Paiement($db); + $payment->fetch(0,$bank_id); + + $bankaccount = $payment->fk_account; + + // Get invoice list to reopen them + $sql = 'SELECT pf.fk_facture, pf.amount'; + $sql.= ' FROM '.MAIN_DB_PREFIX.'paiement_facture as pf'; + $sql.= ' WHERE pf.fk_paiement = '.$payment->id; + + $resql=$db->query($sql); + if ($resql) + { + $rejectedPayment = new Paiement($db); + $rejectedPayment->amounts = array(); + $rejectedPayment->datepaye = $rejection_date; + $rejectedPayment->paiementid = 7; // type of payment: check + $rejectedPayment->num_paiement = $payment->numero; + + while($obj = $db->fetch_object($resql)) { + $invoice = new Facture($db); + $invoice->fetch($obj->fk_facture); + $invoice->set_unpaid($user); + + $rejectedPayment->amounts[$obj->fk_facture] = price2num($obj->amount) * -1; + } + + if ($rejectedPayment->create($user) > 0) + { + $result=$rejectedPayment->addPaymentToBank($user,'payment','(CheckRejected)',$bankaccount,'',''); + if ($result > 0) + { + $payment->reject(); + return $rejectedPayment->id; + } + else + { + return -1; + } + } + else + { + return -1; + } + } + else + { + $this->error=$this->db->error(); + return -1; + } + } + /** * Charge les proprietes ref_previous et ref_next * diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 7ec698f06bc..dc4be17aa07 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -116,7 +116,8 @@ class Paiement extends CommonObject $this->type_code = $obj->type_code; $this->statut = $obj->statut; - $this->bank_account = $obj->fk_account; + $this->bank_account = $obj->fk_account; // deprecated + $this->fk_account = $obj->fk_account; $this->bank_line = $obj->fk_bank; $this->db->free($result); @@ -669,6 +670,29 @@ class Paiement extends CommonObject return -1; } } + + /** + * Reject payment + * + * @return int <0 if KO, >0 if OK + */ + function reject() + { + $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element.' SET statut = 2 WHERE rowid = '.$this->id; + + dol_syslog(get_class($this).'::reject', LOG_DEBUG); + $result = $this->db->query($sql); + if ($result) + { + return 1; + } + else + { + $this->error=$this->db->lasterror(); + dol_syslog(get_class($this).'::reject '.$this->error); + return -1; + } + } /* * \brief Information sur l'objet diff --git a/htdocs/langs/en_US/banks.lang b/htdocs/langs/en_US/banks.lang index f363ffa56c6..3b36c59a0df 100644 --- a/htdocs/langs/en_US/banks.lang +++ b/htdocs/langs/en_US/banks.lang @@ -165,3 +165,8 @@ DeleteARib=Delete BAN record ConfirmDeleteRib=Are you sure you want to delete this BAN record ? StartDate=Start date EndDate=End date +RejectCheck=Check rejection +ConfirmRejectCheck=Are you sure you want to mark this check as rejected ? +RejectCheckDate=Check rejection date +CheckRejected=Check rejected +CheckRejectedAndInvoicesReopened=Check rejected and invoices reopened \ No newline at end of file