From f0a3057221c230a38172ab656fe826123f6cc871 Mon Sep 17 00:00:00 2001 From: aspangaro Date: Sun, 21 Jun 2015 08:07:06 +0200 Subject: [PATCH] Fix: ER button set_paid & merge with old option setPaid --- htdocs/expensereport/card.php | 215 ++++++++---------- .../class/expensereport.class.php | 45 +--- 2 files changed, 96 insertions(+), 164 deletions(-) diff --git a/htdocs/expensereport/card.php b/htdocs/expensereport/card.php index 1a52f0dc194..d5dd3098080 100644 --- a/htdocs/expensereport/card.php +++ b/htdocs/expensereport/card.php @@ -666,123 +666,6 @@ if ($action == "confirm_cancel" && GETPOST('confirm')=="yes" && GETPOST('detail_ } } -if ($action == "confirm_paid" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->to_paid) -{ - $object = new ExpenseReport($db); - $object->fetch($id); - - $result = $object->setPaid($user); - - if ($result > 0) - { - // Define output language - if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) - { - $outputlangs = $langs; - $newlang = ''; - if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha'); - if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang; - if (! empty($newlang)) { - $outputlangs = new Translate("", $conf); - $outputlangs->setDefaultLang($newlang); - } - $model=$object->modelpdf; - $ret = $object->fetch($id); // Reload to get new records - - $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref); - } - } - - if ($result > 0) - { - if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) - { - // Send mail - - // TO - $destinataire = new User($db); - $destinataire->fetch($object->fk_user_author); - $emailTo = $destinataire->email; - - // FROM - $expediteur = new User($db); - $expediteur->fetch($object->fk_user_paid); - $emailFrom = $expediteur->email; - - // SUBJECT - $subject = "' ERP - Note de frais payée"; - - // CONTENT - $message = "Bonjour {$destinataire->firstname},\n\n"; - $message.= "Votre note de frais \"{$object->ref}\" vient d'être payée.\n"; - $message.= "- Payeur : {$expediteur->firstname} {$expediteur->lastname}\n"; - $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n"; - $message.= "Bien cordialement,\n' SI"; - - // Génération du pdf avant attachement - $object->setDocModel($user,""); - $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs); - - // PREPARE SEND - $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message); - - if(!$mailfile->error): - - // SEND - $result=$mailfile->sendfile(); - if ($result): - // Insert écriture dans le compte courant - $idTrip = $id; - $idAccount = 1; - - $object = new ExpenseReport($db); - $object->fetch($idTrip); - - $datePaiement = explode("-",$object->date_paiement); - - $dateop = dol_mktime(12,0,0,$datePaiement[1],$datePaiement[2],$datePaiement[0]); - $operation = $object->code_paiement; - $label = "Règlement ".$object->ref; - $amount = - price2num($object->total_ttc); - $num_chq = ''; - $cat1 = ''; - - $user = new User($db); - $user->fetch($object->fk_user_paid); - - $acct=new Account($db,$idAccount); - $insertid = $acct->addline($dateop, $operation, $label, $amount, $num_chq, $cat1, $user); - - if ($insertid > 0): - $sql = " UPDATE ".MAIN_DB_PREFIX."expensereport as d"; - $sql.= " SET integration_compta = 1, fk_bank_account = $idAccount"; - $sql.= " WHERE rowid = $idTrip"; - $resql=$db->query($sql); - if($result): - Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id); - exit; - else: - dol_print_error($db); - endif; - else: - dol_print_error($db,$acct->error); - endif; - endif; - - else: - - $mesg="Impossible d'envoyer l'email."; - - endif; - // END - Send mail - } - } - else - { - setEventMessages($object->error, $object->errors, 'errors'); - } -} - if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 && $user->rights->expensereport->creer) { $object = new ExpenseReport($db); @@ -827,16 +710,96 @@ if ($action == "confirm_brouillonner" && GETPOST('confirm')=="yes" && $id > 0 && } } -if ($action == 'set_paid') +if ($action == 'set_paid' && $id > 0 && $user->rights->expensereport->to_paid) { - if ($object->set_paid($id) >= 0) + $object = new ExpenseReport($db); + $object->fetch($id); + + $result = $object->set_paid($id, $user) + + if ($result > 0) { - header("Location: ".$_SERVER['PHP_SELF']."?id=".$id); - exit; + // Define output language + if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) + { + $outputlangs = $langs; + $newlang = ''; + if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id')) $newlang = GETPOST('lang_id','alpha'); + if ($conf->global->MAIN_MULTILANGS && empty($newlang)) $newlang = $object->thirdparty->default_lang; + if (! empty($newlang)) { + $outputlangs = new Translate("", $conf); + $outputlangs->setDefaultLang($newlang); + } + $model=$object->modelpdf; + $ret = $object->fetch($id); // Reload to get new records + + $object->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref); + } + } + + if ($result > 0) + { + if (! empty($conf->global->DEPLACEMENT_TO_CLEAN)) + { + // Send mail + + // TO + $destinataire = new User($db); + $destinataire->fetch($object->fk_user_author); + $emailTo = $destinataire->email; + + // FROM + $expediteur = new User($db); + $expediteur->fetch($object->fk_user_paid); + $emailFrom = $expediteur->email; + + // SUBJECT + $subject = "'ERP - Note de frais payée"; + + // CONTENT + $message = "Bonjour {$destinataire->firstname},\n\n"; + $message.= "Votre note de frais \"{$object->ref}\" vient d'être payée.\n"; + $message.= "- Payeur : {$expediteur->firstname} {$expediteur->lastname}\n"; + $message.= "- Lien : {$dolibarr_main_url_root}/expensereport/card.php?id={$object->id}\n\n"; + $message.= "Bien cordialement,\n' SI"; + + // Generate pdf before attachment + $object->setDocModel($user,""); + $resultPDF = expensereport_pdf_create($db,$object,'',"",$langs); + + // PREPARE SEND + $mailfile = new CMailFile($subject,$emailTo,$emailFrom,$message); + + if(!$mailfile->error): + + // SEND + $result=$mailfile->sendfile(); + if ($result): + + // Retour + if($result): + Header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id); + exit; + else: + dol_print_error($db); + endif; + else: + dol_print_error($db,$acct->error); + endif; + endif; + + else: + + $mesg="Impossible d'envoyer l'email."; + + endif; + // END - Send mail + } + } + else + { + setEventMessages($object->error, $object->errors, 'errors'); } - else { - setEventMessage($object->error, 'errors'); - } } if ($action == "addline") @@ -2001,9 +1964,9 @@ if ($action != 'create' && $action != 'edit') print '
' . $langs->trans('DoPayment') . '
'; } - if ($object->statut == 1 && round($remaintopay) == 0 && $object->paid == 0 && $user->rights->don->creer) + if (round($remaintopay) == 0 && $object->paid == 0) { - print '
'.$langs->trans("ClassifyPaid")."
"; + print '
'.$langs->trans("ClassifyPaid")."
"; } // Cancel diff --git a/htdocs/expensereport/class/expensereport.class.php b/htdocs/expensereport/class/expensereport.class.php index 914ff34043d..10b96d7b013 100644 --- a/htdocs/expensereport/class/expensereport.class.php +++ b/htdocs/expensereport/class/expensereport.class.php @@ -1,6 +1,7 @@ * Copyright (C) 2015 Laurent Destailleur + * Copyright (C) 2015 Alexandre Spangaro * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -376,13 +377,16 @@ class ExpenseReport extends CommonObject * Classify the expense report as paid * * @param int $id id of expense report - * @return int <0 if KO, >0 if OK + * @param user $fuser User + * @return int <0 if KO, >0 if OK */ - function set_paid($id) + function set_paid($id, $fuser) { - $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport SET fk_statut = 6"; + $sql = "UPDATE ".MAIN_DB_PREFIX."expensereport"; + $sql.= " SET fk_statut = 6, fk_user_paid = ".$fuser->id.","; $sql.= " WHERE rowid = $id AND fk_statut = 5"; + dol_syslog(get_class($this)."::set_paid sql=".$sql, LOG_DEBUG); $resql=$this->db->query($sql); if ($resql) { @@ -1018,41 +1022,6 @@ class ExpenseReport extends CommonObject } } - /** - * setPaid - * - * @param User $fuser User - * @return int <0 if KO, >0 if OK - */ - function setPaid($fuser) - { - $now= dol_now(); - - $this->date_paiement = $this->db->idate($now); - if ($this->fk_statut != 6) - { - $sql = 'UPDATE '.MAIN_DB_PREFIX.$this->table_element; - $sql.= " SET fk_statut = 6, fk_user_paid = ".$fuser->id.","; - $sql.= " date_paiement='".$this->db->idate($this->date_paiement)."'"; - $sql.= ' WHERE rowid = '.$this->id; - - dol_syslog(get_class($this)."::setPaid sql=".$sql, LOG_DEBUG); - if ($this->db->query($sql)) - { - return 1; - } - else - { - $this->error=$this->db->error(); - return -1; - } - } - else - { - dol_syslog(get_class($this)."::set_paid expensereport already with paid status", LOG_WARNING); - } - } - /** * set_unpaid *