diff --git a/scripts/invoices/email_unpaid_invoices_to_customers.php b/scripts/invoices/email_unpaid_invoices_to_customers.php new file mode 100755 index 00000000000..0c069d04683 --- /dev/null +++ b/scripts/invoices/email_unpaid_invoices_to_customers.php @@ -0,0 +1,247 @@ +#!/usr/bin/php + + * Copyright (C) 2005-2013 Laurent Destailleur + * + * 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 + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +/** + * \file scripts/invoices/email_unpaid_invoices_to_customers.php + * \ingroup facture + * \brief Script to send a mail to customers with unpaid invoices + */ + +$sapi_type = php_sapi_name(); +$script_file = basename(__FILE__); +$path=dirname(__FILE__).'/'; + +// Test si mode batch +$sapi_type = php_sapi_name(); +if (substr($sapi_type, 0, 3) == 'cgi') { + echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; + exit; +} + +if (! isset($argv[1]) || ! $argv[1] || ! in_array($argv[1],array('test','confirm'))) +{ + print "Usage: $script_file [test|confirm] [delay]\n"; + print "\n"; + print "Send an email to customers to remind all unpaid customer invoices.\n"; + print "If you choose 'test' mode, no emails are sent.\n"; + print "If you add a delay (nb of days), only invoice with due date < today + delay are included.\n"; + exit; +} +$mode=$argv[1]; + + +require($path."../../htdocs/master.inc.php"); +require_once (DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php"); + +$langs->load('main'); + + +/* + * Main + */ + +$now=dol_now('tzserver'); +$duration_value=$argv[2]; + +$error = 0; +print $script_file." launched with mode ".$mode.($duration_value?" delay=".$duration_value:"")."\n"; + +$sql = "SELECT f.facnumber, f.total_ttc, f.date_lim_reglement as due_date, s.nom as name, s.email, s.default_lang"; +$sql .= " FROM ".MAIN_DB_PREFIX."facture as f"; +$sql .= " , ".MAIN_DB_PREFIX."societe as s"; +$sql .= " WHERE f.fk_statut != 0 AND f.paye = 0"; +$sql .= " AND f.fk_soc = s.rowid"; +if ($duration_value) $sql .= " AND f.date_lim_reglement < '".$db->idate(dol_time_plus_duree($now, $duration_value, "d"))."'"; +$sql .= " ORDER BY s.email ASC, s.rowid ASC"; // Order by email to allow one message per email + +//print $sql; +$resql=$db->query($sql); +if ($resql) +{ + $num = $db->num_rows($resql); + $i = 0; + $oldemail = 'none'; $olduid = 0; $oldlang=''; + $total = 0; $foundtoprocess = 0; + print "We found ".$num." couples (unpayed validated invoice - customer) qualified\n"; + dol_syslog("We found ".$num." couples (unpayed validated invoice - customer) qualified"); + $message=''; + + if ($num) + { + while ($i < $num) + { + $obj = $db->fetch_object($resql); + + if (($obj->email <> $oldemail || $obj->uid <> $olduid) || $oldemail == 'none') + { + // Break onto sales representative (new email or uid) + if (dol_strlen($oldemail) && $oldemail != 'none') + { + envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer); + } + else + { + if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n"; + } + $oldemail = $obj->email; + $olduid = $obj->uid; + $oldlang = $obj->lang; + $oldcustomer=$obj->name; + $message = ''; + $total = 0; + $foundtoprocess = 0; + $customer=$obj->name; + if (empty($obj->email)) print "Warning: Customer ".$customer." has no email. Notice disabled.\n"; + } + + if (dol_strlen($oldemail)) + { + $message .= $langs->trans("Invoice")." ".$obj->facnumber." : ".price($obj->total_ttc)." : ".$obj->name."\n"; + dol_syslog("email_unpaid_invoices_to_customers.php: ".$obj->email); + $foundtoprocess++; + } + print "Unpaid invoice ".$obj->facnumber.", price ".price2num($obj->total_ttc).", due date ".dol_print_date($db->jdate($obj->due_date),'day')." customer ".$obj->name.", email ".$obj->email.": "; + if (dol_strlen($obj->email)) print "qualified."; + else print "disqualified (no email)."; + print "\n"; + + $total += $obj->total_ttc; + + $i++; + } + + // Si il reste des envois en buffer + if ($foundtoprocess) + { + if (dol_strlen($oldemail) && $oldemail != 'none') // Break onto email (new email) + { + envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer); + } + else + { + if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n"; + } + } + } + else + { + print "No unpaid invoices found\n"; + } +} +else +{ + dol_print_error($db); + dol_syslog("email_unpaid_invoices_to_customers.php: Error"); +} + + +/** + * Send email + * + * @param string $mode Mode (test | confirm) + * @param string $oldemail Old email + * @param string $message Message to send + * @param string $total Total amount of unpayed invoices + * @param string $userlang Code lang to use for email output. + * @param string $oldcustomer Old customer + * @return int <0 if KO, >0 if OK + */ +function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldcustomer) +{ + global $conf,$langs; + + $newlangs=new Translate('',$conf); + $newlangs->setDefaultLang($userlang); + $newlangs->load("main"); + $newlangs->load("bills"); + + $subject = "[".(empty($conf->global->MAIN_APPLICATION_TITLE)?'Dolibarr':$conf->global->MAIN_APPLICATION_TITLE)."] ".$newlangs->trans("ListOfYourUnpaidInvoices"); + $sendto = $oldemail; + $from = $conf->global->MAIN_EMAIL_FROM; + $errorsto = $conf->global->MAIN_MAIL_ERRORS_TO; + $msgishtml = 0; + + print "- Send email for ".$oldcustomer."(".$oldemail."), total: ".$total."\n"; + dol_syslog("email_unpaid_invoices_to_customers.php: send mail to ".$oldemail); + + $usehtml=0; + if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_FOOTER)) $usehtml+=1; + if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_HEADER)) $usehtml+=1; + + $allmessage=''; + if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_HEADER)) + { + $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_HEADER; + } + else + { + $allmessage.= "Dear customer".($usehtml?"
\n":"\n").($usehtml?"
\n":"\n"); + $allmessage.= "Please, find a summary of the bills with pending payments from you, with an attachment of invoices.".($usehtml?"
\n":"\n").($usehtml?"
\n":"\n"); + $allmessage.= "Note: This list contains only unpaid invoices.".($usehtml?"
\n":"\n"); + } + $allmessage.= $message.($usehtml?"
\n":"\n"); + $allmessage.= $langs->trans("Total")." = ".price($total).($usehtml?"
\n":"\n"); + if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER)) + { + $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER; + if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_FOOTER)) $usehtml+=1; + } + + $mail = new CMailFile( + $subject, + $sendto, + $from, + $allmessage, + array(), + array(), + array(), + '', + '', + 0, + $msgishtml + ); + + $mail->errors_to = $errorsto; + + // Send or not email + if ($mode == 'confirm') + { + $result=$mail->sendfile(); + } + else + { + print "No email sent (test mode)\n"; + dol_syslog("No email sent (test mode)"); + $mail->dump_mail(); + $result=1; + } + + if ($result) + { + return 1; + } + else + { + return -1; + } +} + + +?> diff --git a/scripts/invoices/email_unpaid_invoices_to_representatives.php b/scripts/invoices/email_unpaid_invoices_to_representatives.php index 2484bb2e541..77bd9ee6402 100755 --- a/scripts/invoices/email_unpaid_invoices_to_representatives.php +++ b/scripts/invoices/email_unpaid_invoices_to_representatives.php @@ -31,7 +31,7 @@ $path=dirname(__FILE__).'/'; // Test si mode batch $sapi_type = php_sapi_name(); if (substr($sapi_type, 0, 3) == 'cgi') { - echo "Erreur: Vous utilisez l'interpreteur PHP pour le mode CGI. Pour executer mailing-send.php en ligne de commande, vous devez utiliser l'interpreteur PHP pour le mode CLI.\n"; + echo "Error: You are using PHP for CGI. To execute ".$script_file." from command line, you must use PHP for CLI mode.\n"; exit; } @@ -82,9 +82,9 @@ if ($resql) $num = $db->num_rows($resql); $i = 0; $oldemail = 'none'; $olduid = 0; $oldlang=''; - $total = 0; - print "We found ".$num." couples (unpayed validated invoice/sale representative) qualified\n"; - dol_syslog("We found ".$num." couples (unpayed validated invoice/sale representative) qualified"); + $total = 0; $foundtoprocess = 0; + print "We found ".$num." couples (unpayed validated invoice - sale representative) qualified\n"; + dol_syslog("We found ".$num." couples (unpayed validated invoice - sale representative) qualified"); $message=''; if ($num) @@ -98,34 +98,49 @@ if ($resql) // Break onto sales representative (new email or uid) if (dol_strlen($oldemail) && $oldemail != 'none') { - envoi_mail($mode,$oldemail,$message,$total,$oldlang); + envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldsalerepresentative); + } + else + { + if ($oldemail != 'none') print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n"; } $oldemail = $obj->email; $olduid = $obj->uid; $oldlang = $obj->lang; + $oldsalerepresentative=dolGetFirstLastname($obj->firstname, $obj->lastname);; $message = ''; $total = 0; - if (empty($obj->email)) print "Warning: Sal representative ".dolGetFirstLastname($obj->firstname, $obj->lastname)." has no email. Notice disabled.\n"; + $foundtoprocess = 0; + $salerepresentative=dolGetFirstLastname($obj->firstname, $obj->lastname); + if (empty($obj->email)) print "Warning: Sal representative ".$salerepresentative." has no email. Notice disabled.\n"; } - if (dol_strlen($oldemail)) + if (dol_strlen($obj->email)) { $message .= $langs->trans("Invoice")." ".$obj->facnumber." : ".price($obj->total_ttc)." : ".$obj->name."\n"; - print "Unpaid invoice ".$obj->facnumber.", 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.") qualified.\n"; dol_syslog("email_unpaid_invoices_to_representatives.php: ".$obj->email); + $foundtoprocess++; } + print "Unpaid invoice ".$obj->facnumber.", 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."): "; + if (dol_strlen($obj->email)) print "qualified."; + else print "disqualified (no email)."; + print "\n"; $total += $obj->total_ttc; $i++; } // Si il reste des envois en buffer - if ($total) + if ($foundtoprocess) { if (dol_strlen($oldemail) && $oldemail != 'none') // Break onto email (new email) { - envoi_mail($mode,$oldemail,$message,$total,$oldlang); + envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldsalerepresentative); } + else + { + if ($oldemail != 'none') print "- No email sent for ".$oldsalerepresentative.", total: ".$total."\n"; + } } } else @@ -143,14 +158,15 @@ else /** * Send email * - * @param string $mode Mode (test | confirm) - * @param string $oldemail Old email - * @param string $message Message to send - * @param string $total Total amount of unpayed invoices - * @param string $userlang Code lang to use for email output. - * @return int <0 if KO, >0 if OK + * @param string $mode Mode (test | confirm) + * @param string $oldemail Old email + * @param string $message Message to send + * @param string $total Total amount of unpayed invoices + * @param string $userlang Code lang to use for email output. + * @param string $oldcustomer Old customer + * @return int <0 if KO, >0 if OK */ -function envoi_mail($mode,$oldemail,$message,$total,$userlang) +function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldsalerepresentative) { global $conf,$langs; @@ -165,7 +181,7 @@ function envoi_mail($mode,$oldemail,$message,$total,$userlang) $errorsto = $conf->global->MAIN_MAIL_ERRORS_TO; $msgishtml = 0; - print "Send email for ".$oldemail.", total: ".$total."\n"; + print "- Send email for ".$oldsalerepresentative." (".$oldemail."), total: ".$total."\n"; dol_syslog("email_unpaid_invoices_to_representatives.php: send mail to ".$oldemail); $usehtml=0; @@ -179,16 +195,15 @@ function envoi_mail($mode,$oldemail,$message,$total,$userlang) } else { - $allmessage.= "List of unpaid invoices\n\n"; - $allmessage.= "This list contains only invoices for third parties you are linked to as a sales representative.\n"; - $allmessage.= "\n"; + $allmessage.= "List of unpaid invoices".($usehtml?"
\n":"\n").($usehtml?"
\n":"\n"); + $allmessage.= "Note: This list contains only invoices for third parties you are linked to as a sale representative.".($usehtml?"
\n":"\n"); } $allmessage.= $message.($usehtml?"
\n":"\n"); $allmessage.= $langs->trans("Total")." = ".price($total).($usehtml?"
\n":"\n"); - if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_FOOTER)) + if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) { - $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_FOOTER; - if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_FOOTER)) $usehtml+=1; + $allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER; + if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_SALESREPRESENTATIVES_FOOTER)) $usehtml+=1; } $mail = new CMailFile(