New: Can send unpaid invoices to all contacts.
This commit is contained in:
parent
bbb843ec58
commit
b23d15c652
@ -36,9 +36,9 @@ if (substr($sapi_type, 0, 3) == 'cgi') {
|
||||
exit;
|
||||
}
|
||||
|
||||
if (! isset($argv[1]) || ! $argv[1] || ! in_array($argv[1],array('test','confirm')))
|
||||
if (! isset($argv[2]) || ! $argv[2] || ! in_array($argv[1],array('test','confirm')) || ! in_array($argv[2],array('thirdparties','contacts')))
|
||||
{
|
||||
print "Usage: $script_file [test|confirm] [delay]\n";
|
||||
print "Usage: $script_file (test|confirm) (thirdparties|contacts) [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";
|
||||
@ -46,6 +46,7 @@ if (! isset($argv[1]) || ! $argv[1] || ! in_array($argv[1],array('test','confirm
|
||||
exit;
|
||||
}
|
||||
$mode=$argv[1];
|
||||
$targettype=$argv[2];
|
||||
|
||||
|
||||
require($path."../../htdocs/master.inc.php");
|
||||
@ -67,18 +68,23 @@ $error=0;
|
||||
print "***** ".$script_file." (".$version.") *****\n";
|
||||
|
||||
$now=dol_now('tzserver');
|
||||
$duration_value=$argv[2];
|
||||
$duration_value=$argv[3];
|
||||
|
||||
$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
|
||||
$sql = "SELECT f.facnumber, f.total_ttc, f.date_lim_reglement as due_date,";
|
||||
$sql.= " s.rowid as sid, s.nom as name, s.email, s.default_lang";
|
||||
if ($targettype == 'contacts') $sql.= ", sp.rowid as cid, sp.firstname as cfirstname, sp.lastname as clastname, sp.email as cemail";
|
||||
$sql.= " FROM ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."societe as s";
|
||||
if ($targettype == 'contacts') $sql.= ", ".MAIN_DB_PREFIX."socpeople as sp";
|
||||
$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"))."'";
|
||||
if ($targettype == 'contacts') $sql.= " AND s.rowid = sp.fk_soc";
|
||||
$sql.= " ORDER BY";
|
||||
if ($targettype == 'contacts') $sql.= " sp.email, sp.rowid,";
|
||||
$sql.= " s.email ASC, s.rowid ASC"; // Order by email to allow one message per email
|
||||
|
||||
//print $sql;
|
||||
$resql=$db->query($sql);
|
||||
@ -86,10 +92,10 @@ if ($resql)
|
||||
{
|
||||
$num = $db->num_rows($resql);
|
||||
$i = 0;
|
||||
$oldemail = 'none'; $olduid = 0; $oldlang='';
|
||||
$oldemail = 'none'; $oldsid = 0; $oldcid = 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");
|
||||
print "We found ".$num." couples (unpayed validated invoices-".$targettype.") qualified\n";
|
||||
dol_syslog("We found ".$num." couples (unpayed validated invoices-".$targettype.") qualified");
|
||||
$message='';
|
||||
|
||||
if ($num)
|
||||
@ -98,36 +104,45 @@ if ($resql)
|
||||
{
|
||||
$obj = $db->fetch_object($resql);
|
||||
|
||||
if (($obj->email <> $oldemail || $obj->uid <> $olduid) || $oldemail == 'none')
|
||||
$newemail=empty($obj->cemail)?$obj->email:$obj->cemail;
|
||||
|
||||
// Check if this record is a break after previous one
|
||||
$startbreak=false;
|
||||
if ($newemail <> $oldemail || $oldemail == 'none') $startbreak=true;
|
||||
if ($obj->sid && $obj->sid <> $oldsid) $startbreak=true;
|
||||
if ($obj->cid && $obj->cid <> $oldcid) $startbreak=true;
|
||||
|
||||
if ($startbreak)
|
||||
{
|
||||
// Break onto sales representative (new email or uid)
|
||||
// Break onto sales representative (new email or cid)
|
||||
if (dol_strlen($oldemail) && $oldemail != 'none')
|
||||
{
|
||||
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer);
|
||||
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldtarget);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n";
|
||||
if ($oldemail != 'none') print "- No email sent for '".$oldtarget."', total: ".$total."\n";
|
||||
}
|
||||
$oldemail = $obj->email;
|
||||
$olduid = $obj->uid;
|
||||
$oldemail = $newemail;
|
||||
$oldsid = $obj->sid;
|
||||
$oldcid = $obj->cid;
|
||||
$oldlang = $obj->lang;
|
||||
$oldcustomer=$obj->name;
|
||||
$oldtarget=(empty($obj->cfirstname) && empty($obj->clastname))?$obj->name:($obj->clastname." ".$obj->cfirstname);
|
||||
$message = '';
|
||||
$total = 0;
|
||||
$total = 0;
|
||||
$foundtoprocess = 0;
|
||||
$customer=$obj->name;
|
||||
if (empty($obj->email)) print "Warning: Customer ".$customer." has no email. Notice disabled.\n";
|
||||
$target=(empty($obj->cfirstname) && empty($obj->clastname))?$obj->name:($obj->clastname." ".$obj->cfirstname);
|
||||
//if (empty($newemail)) print "Warning: Customer ".$target." has no email. Notice disabled.\n";
|
||||
}
|
||||
|
||||
if (dol_strlen($oldemail))
|
||||
if (dol_strlen($newemail))
|
||||
{
|
||||
$message .= $langs->trans("Invoice")." ".$obj->facnumber." : ".price($obj->total_ttc)." : ".$obj->name."\n";
|
||||
dol_syslog("email_unpaid_invoices_to_customers.php: ".$obj->email);
|
||||
$message .= $langs->trans("Invoice")." ".$obj->facnumber." : ".price($obj->total_ttc)."\n";
|
||||
dol_syslog("email_unpaid_invoices_to_customers.php: ".$newemail." ".$message);
|
||||
$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.";
|
||||
print "Unpaid invoice ".$obj->facnumber.", price ".price2num($obj->total_ttc).", due date ".dol_print_date($db->jdate($obj->due_date),'day')." customer ".$obj->sid." ".$obj->name.", ".($obj->cid?"contact ".$obj->cid." ".$obj->clastname." ".$obj->cfirstname.",":"")." email ".$newemail.": ";
|
||||
if (dol_strlen($newemail)) print "qualified.";
|
||||
else print "disqualified (no email).";
|
||||
print "\n";
|
||||
|
||||
@ -141,11 +156,11 @@ if ($resql)
|
||||
{
|
||||
if (dol_strlen($oldemail) && $oldemail != 'none') // Break onto email (new email)
|
||||
{
|
||||
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldcustomer);
|
||||
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldtarget);
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($oldemail != 'none') print "- No email sent for ".$oldcustomer.", total: ".$total."\n";
|
||||
if ($oldemail != 'none') print "- No email sent for '".$oldtarget."', total: ".$total."\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -165,14 +180,14 @@ else
|
||||
* Send email
|
||||
*
|
||||
* @param string $mode Mode (test | confirm)
|
||||
* @param string $oldemail Old email
|
||||
* @param string $oldemail Target 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
|
||||
* @param string $oldtarget Target name
|
||||
* @return int <0 if KO, >0 if OK
|
||||
*/
|
||||
function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldcustomer)
|
||||
function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldtarget)
|
||||
{
|
||||
global $conf,$langs;
|
||||
|
||||
@ -187,7 +202,7 @@ function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldcustomer)
|
||||
$errorsto = $conf->global->MAIN_MAIL_ERRORS_TO;
|
||||
$msgishtml = 0;
|
||||
|
||||
print "- Send email for ".$oldcustomer."(".$oldemail."), total: ".$total."\n";
|
||||
print "- Send email for '".$oldtarget."' (".$oldemail."), total: ".$total."\n";
|
||||
dol_syslog("email_unpaid_invoices_to_customers.php: send mail to ".$oldemail);
|
||||
|
||||
$usehtml=0;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user