-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
/
email_unpaid_invoices_to_customers.php
executable file
·292 lines (252 loc) · 10.6 KB
/
email_unpaid_invoices_to_customers.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/php
<?php
/*
* Copyright (C) 2005 Rodolphe Quiedeville <rodolphe@quiedeville.org>
* Copyright (C) 2005-2013 Laurent Destailleur <eldy@users.sourceforge.net>
* Copyright (C) 2013 Juanjo Menent <jmenent@2byte.es>
*
* 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 <http://www.gnu.org/licenses/>.
*/
/**
* \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[2]) || ! $argv[2] || ! in_array($argv[1],array('test','confirm')) || ! in_array($argv[2],array('thirdparties','contacts')))
{
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";
print "If you add a delay (nb of days), only invoice with due date < today + delay are included.\n";
exit;
}
$mode=$argv[1];
$targettype=$argv[2];
require($path."../../htdocs/master.inc.php");
require_once (DOL_DOCUMENT_ROOT."/core/class/CMailFile.class.php");
$langs->load('main');
// Global variables
$version=DOL_VERSION;
$error=0;
/*
* Main
*/
@set_time_limit(0);
print "***** ".$script_file." (".$version.") pid=".getmypid()." *****\n";
$now=dol_now('tzserver');
$duration_value=isset($argv[3])?$argv[3]:'none';
$error = 0;
print $script_file." launched with mode ".$mode.(is_numeric($duration_value)?" delay=".$duration_value:"")."\n";
if ($mode != 'confirm') $conf->global->MAIN_DISABLE_ALL_MAILS=1;
$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 (is_numeric($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, f.facnumber 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'; $oldsid = 0; $oldcid = 0; $oldlang='';
$total = 0; $foundtoprocess = 0;
$trackthirdpartiessent = array();
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)
{
while ($i < $num)
{
$obj = $db->fetch_object($resql);
$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 cid)
if (dol_strlen($oldemail) && $oldemail != 'none' && empty($trackthirdpartiessent[$oldsid.'|'.$oldemail]))
{
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldtarget);
$trackthirdpartiessent[$oldsid.'|'.$oldemail]='contact id '.$oldcid;
}
else
{
if ($oldemail != 'none')
{
if (empty($trackthirdpartiessent[$oldsid.'|'.$oldemail])) print "- No email sent for '".$oldtarget."', total: ".$total."\n";
else print "- No email sent for '".$oldtarget."', total: ".$total." (already sent to ".$trackthirdpartiessent[$oldsid.'|'.$oldemail].")\n";
}
}
$oldemail = $newemail;
$oldsid = $obj->sid;
$oldcid = $obj->cid;
$oldlang = $obj->lang;
$oldtarget=(empty($obj->cfirstname) && empty($obj->clastname))?$obj->name:($obj->clastname." ".$obj->cfirstname);
$message = '';
$total = 0;
$foundtoprocess = 0;
$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($newemail))
{
$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 id ".$obj->sid." ".$obj->name.", ".($obj->cid?"contact id ".$obj->cid." ".$obj->clastname." ".$obj->cfirstname.",":"")." email ".$newemail.": ";
if (dol_strlen($newemail)) 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' && empty($trackthirdpartiessent[$oldsid.'|'.$oldemail])) // Break onto email (new email)
{
envoi_mail($mode,$oldemail,$message,$total,$oldlang,$oldtarget);
$trackthirdpartiessent[$oldsid.'|'.$oldemail]='contact id '.$oldcid;
}
else
{
if ($oldemail != 'none')
{
if (empty($trackthirdpartiessent[$oldsid.'|'.$oldemail])) print "- No email sent for '".$oldtarget."', total: ".$total."\n";
else print "- No email sent for '".$oldtarget."', total: ".$total." (already sent to ".$trackthirdpartiessent[$oldsid.'|'.$oldemail].")\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 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 $oldtarget Target name
* @return int <0 if KO, >0 if OK
*/
function envoi_mail($mode,$oldemail,$message,$total,$userlang,$oldtarget)
{
global $conf,$langs;
if (getenv('DOL_FORCE_EMAIL_TO')) $oldemail=getenv('DOL_FORCE_EMAIL_TO');
$newlangs=new Translate('',$conf);
$newlangs->setDefaultLang(empty($userlang)?(empty($conf->global->MAIN_LANG_DEFAULT)?'auto':$conf->global->MAIN_LANG_DEFAULT):$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_MAIL_EMAIL_FROM;
$errorsto = $conf->global->MAIN_MAIL_ERRORS_TO;
$msgishtml = -1;
print "- Send email for '".$oldtarget."' (".$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_CUSTOMERS_FOOTER)) $usehtml+=1;
if (dol_textishtml($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER)) $usehtml+=1;
$allmessage='';
if (! empty($conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER))
{
$allmessage.=$conf->global->SCRIPT_EMAIL_UNPAID_INVOICES_CUSTOMERS_HEADER;
}
else
{
$allmessage.= "Dear customer".($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
$allmessage.= "Please, find a summary of the bills with pending payments from you.".($usehtml?"<br>\n":"\n").($usehtml?"<br>\n":"\n");
$allmessage.= "Note: This list contains only unpaid invoices.".($usehtml?"<br>\n":"\n");
}
$allmessage.= $message.($usehtml?"<br>\n":"\n");
$allmessage.= $langs->trans("Total")." = ".price($total).($usehtml?"<br>\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();
if (! $result)
{
print "Error sending email ".$mail->error."\n";
dol_syslog("Error sending email ".$mail->error."\n");
}
}
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;
}
}
?>