1#!/usr/bin/env php 2<?php 3/* 4 * Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org> 5 * Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net> 6 * Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 3 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 */ 21 22/** 23 * \file scripts/invoices/email_unpaid_invoices_to_representatives.php 24 * \ingroup facture 25 * \brief Script to send a mail to dolibarr users linked to companies with unpaid invoices 26 */ 27 28if (!defined('NOSESSION')) { 29 define('NOSESSION', '1'); 30} 31 32$sapi_type = php_sapi_name(); 33$script_file = basename(__FILE__); 34$path = __DIR__.'/'; 35 36// Test si mode batch 37$sapi_type = php_sapi_name(); 38if (substr($sapi_type, 0, 3) == 'cgi') { 39 echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; 40 exit(-1); 41} 42 43if (!isset($argv[1]) || !$argv[1] || !in_array($argv[1], array('test', 'confirm'))) { 44 print "Usage: $script_file (test|confirm) [delay]\n"; 45 print "\n"; 46 print "Send an email to users to remind all unpaid customer invoices user is sale representative for.\n"; 47 print "If you choose 'test' mode, no emails are sent.\n"; 48 print "If you add a delay (nb of days), only invoice with due date < today + delay are included.\n"; 49 exit(-1); 50} 51$mode = $argv[1]; 52 53require $path."../../htdocs/master.inc.php"; 54require_once DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php"; 55 56$langs->load('main'); 57 58// Global variables 59$version = DOL_VERSION; 60$error = 0; 61 62/* 63 * Main 64 */ 65 66@set_time_limit(0); 67print "***** ".$script_file." (".$version.") pid=".dol_getmypid()." *****\n"; 68dol_syslog($script_file." launched with arg ".join(',', $argv)); 69 70$now = dol_now('tzserver'); 71$duration_value = isset($argv[2]) ? $argv[2] : 'none'; 72 73print $script_file." launched with mode ".$mode." default lang=".$langs->defaultlang.(is_numeric($duration_value) ? " delay=".$duration_value : "")."\n"; 74 75if ($mode != 'confirm') { 76 $conf->global->MAIN_DISABLE_ALL_MAILS = 1; 77} 78 79$sql = "SELECT f.ref, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang,"; 80$sql .= " u.rowid as uid, u.lastname, u.firstname, u.email, u.lang"; 81$sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; 82$sql .= " , ".MAIN_DB_PREFIX."societe as s"; 83$sql .= " , ".MAIN_DB_PREFIX."societe_commerciaux as sc"; 84$sql .= " , ".MAIN_DB_PREFIX."user as u"; 85$sql .= " WHERE f.fk_statut = 1 AND f.paye = 0"; 86$sql .= " AND f.fk_soc = s.rowid"; 87if (is_numeric($duration_value)) { 88 $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'"; 89} 90$sql .= " AND sc.fk_soc = s.rowid"; 91$sql .= " AND sc.fk_user = u.rowid"; 92$sql .= " ORDER BY u.email ASC, s.rowid ASC, f.ref ASC"; // Order by email to allow one message per email 93 94// print $sql; 95$resql = $db->query($sql); 96if ($resql) { 97 $num = $db->num_rows($resql); 98 $i = 0; 99 $oldemail = 'none'; 100 $olduid = 0; 101 $oldlang = ''; 102 $total = 0; 103 $foundtoprocess = 0; 104 print "We found ".$num." couples (unpayed validated invoice - sale representative) qualified\n"; 105 dol_syslog("We found ".$num." couples (unpayed validated invoice - sale representative) qualified"); 106 $message = ''; 107 108 if ($num) { 109 while ($i < $num) { 110 $obj = $db->fetch_object($resql); 111 112 if (($obj->email != $oldemail || $obj->uid != $olduid) || $oldemail == 'none') { 113 // Break onto sales representative (new email or uid) 114 if (dol_strlen($oldemail) && $oldemail != 'none') { 115 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative); 116 } else { 117 if ($oldemail != 'none') { 118 print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n"; 119 } 120 } 121 $oldemail = $obj->email; 122 $olduid = $obj->uid; 123 $oldlang = $obj->lang; 124 $oldsalerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname); 125 $message = ''; 126 $total = 0; 127 $foundtoprocess = 0; 128 $salerepresentative = dolGetFirstLastname($obj->firstname, $obj->lastname); 129 if (empty($obj->email)) { 130 print "Warning: Sale representative ".$salerepresentative." has no email. Notice disabled.\n"; 131 } 132 } 133 134 // Define line content 135 $outputlangs = new Translate('', $conf); 136 $outputlangs->setDefaultLang(empty($obj->lang) ? $langs->defaultlang : $obj->lang); // By default language of sale representative 137 138 // Load translation files required by the page 139 $outputlangs->loadLangs(array("main", "bills")); 140 141 if (dol_strlen($obj->email)) { 142 $message .= $outputlangs->trans("Invoice")." ".$obj->ref." : ".price($obj->total_ttc, 0, $outputlangs, 0, 0, - 1, $conf->currency)." : ".$obj->name."\n"; 143 dol_syslog("email_unpaid_invoices_to_representatives.php: ".$obj->email); 144 $foundtoprocess++; 145 } 146 print "Unpaid invoice ".$obj->ref.", price ".price2num($obj->total_ttc).", due date ".dol_print_date($db->jdate($obj->due_date), 'day')." (linked to company ".$obj->name.", sale representative ".dolGetFirstLastname($obj->firstname, $obj->lastname).", email ".$obj->email.", lang ".$outputlangs->defaultlang."): "; 147 if (dol_strlen($obj->email)) { 148 print "qualified."; 149 } else { 150 print "disqualified (no email)."; 151 } 152 print "\n"; 153 154 unset($outputlangs); 155 156 $total += $obj->total_ttc; 157 $i++; 158 } 159 160 // Si il reste des envois en buffer 161 if ($foundtoprocess) { 162 if (dol_strlen($oldemail) && $oldemail != 'none') { // Break onto email (new email) 163 envoi_mail($mode, $oldemail, $message, $total, $oldlang, $oldsalerepresentative); 164 } else { 165 if ($oldemail != 'none') { 166 print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n"; 167 } 168 } 169 } 170 } else { 171 print "No unpaid invoices (for companies linked to a particular commercial dolibarr user) found\n"; 172 } 173 174 exit(0); 175} else { 176 dol_print_error($db); 177 dol_syslog("email_unpaid_invoices_to_representatives.php: Error"); 178 179 exit(-1); 180} 181 182/** 183 * Send email 184 * 185 * @param string $mode Mode (test | confirm) 186 * @param string $oldemail Old email 187 * @param string $message Message to send 188 * @param string $total Total amount of unpayed invoices 189 * @param string $userlang Code lang to use for email output. 190 * @param string $oldsalerepresentative Old sale representative 191 * @return int <0 if KO, >0 if OK 192 */ 193function envoi_mail($mode, $oldemail, $message, $total, $userlang, $oldsalerepresentative) 194{ 195 global $conf, $langs; 196 197 if (getenv('DOL_FORCE_EMAIL_TO')) { 198 $oldemail = getenv('DOL_FORCE_EMAIL_TO'); 199 } 200 201 $newlangs = new Translate('', $conf); 202 $newlangs->setDefaultLang(empty($userlang) ? (empty($conf->global->MAIN_LANG_DEFAULT) ? 'auto' : $conf->global->MAIN_LANG_DEFAULT) : $userlang); 203 $newlangs->load("main"); 204 $newlangs->load("bills"); 205 206 $subject = (empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT) ? $newlangs->trans("ListOfYourUnpaidInvoices") : $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_SUBJECT); 207 $sendto = $oldemail; 208 $from = empty($conf->global->MAIN_MAIL_EMAIL_FROM) ? '' : $conf->global->MAIN_MAIL_EMAIL_FROM; 209 $errorsto = empty($conf->global->MAIN_MAIL_ERRORS_TO) ? '' : $conf->global->MAIN_MAIL_ERRORS_TO; 210 $msgishtml = - 1; 211 212 print "- Send email for ".$oldsalerepresentative." (".$oldemail."), total: ".$total."\n"; 213 dol_syslog("email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail); 214 215 $usehtml = 0; 216 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER) && dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) { 217 $usehtml += 1; 218 } 219 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER) && dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) { 220 $usehtml += 1; 221 } 222 223 $allmessage = ''; 224 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER)) { 225 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_HEADER; 226 } else { 227 $allmessage .= $newlangs->transnoentities("ListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n").($usehtml ? "<br>\n" : "\n"); 228 $allmessage .= $newlangs->transnoentities("NoteListOfYourUnpaidInvoices").($usehtml ? "<br>\n" : "\n"); 229 } 230 $allmessage .= $message.($usehtml ? "<br>\n" : "\n"); 231 $allmessage .= $langs->trans("Total")." = ".price($total, 0, $newlangs, 0, 0, - 1, $conf->currency).($usehtml ? "<br>\n" : "\n"); 232 if (!empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) { 233 $allmessage .= $conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER; 234 if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) { 235 $usehtml += 1; 236 } 237 } 238 239 $mail = new CMailFile($subject, $sendto, $from, $allmessage, array(), array(), array(), '', '', 0, $msgishtml); 240 241 $mail->errors_to = $errorsto; 242 243 // Send or not email 244 if ($mode == 'confirm') { 245 $result = $mail->sendfile(); 246 if (!$result) { 247 print "Error sending email ".$mail->error."\n"; 248 dol_syslog("Error sending email ".$mail->error."\n"); 249 } 250 } else { 251 print "No email sent (test mode)\n"; 252 dol_syslog("No email sent (test mode)"); 253 $mail->dump_mail(); 254 $result = 1; 255 } 256 257 if ($result) { 258 return 1; 259 } else { 260 return -1; 261 } 262} 263