2015-12-11 05:08:32 +01:00
#!/usr/bin/env php
2013-05-07 12:52:14 +02:00
< ? php
/*
2019-05-15 13:20:02 +02:00
* Copyright ( C ) 2005 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
* Copyright ( C ) 2005 - 2013 Laurent Destailleur < eldy @ users . sourceforge . net >
* Copyright ( C ) 2013 Juanjo Menent < jmenent @ 2 byte . es >
2013-05-07 12:52:14 +02:00
*
* 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
2019-05-15 13:20:02 +02:00
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
2013-05-07 12:52:14 +02:00
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2013-05-07 12:52:14 +02:00
*/
/**
2019-05-15 13:20:02 +02:00
* \file scripts / contracts / email_expire_services_to_representatives . php
* \ingroup contracts
* \brief Script to send a mail to dolibarr users linked to companies with services to expire
2013-05-07 12:52:14 +02:00
*/
2020-10-27 01:44:00 +01:00
2021-03-01 00:22:36 +01:00
if ( ! defined ( 'NOSESSION' )) {
define ( 'NOSESSION' , '1' );
}
2020-10-27 01:44:00 +01:00
2013-05-07 12:52:14 +02:00
$sapi_type = php_sapi_name ();
$script_file = basename ( __FILE__ );
2020-04-10 10:59:32 +02:00
$path = __DIR__ . '/' ;
2013-05-07 12:52:14 +02:00
// Test si mode batch
$sapi_type = php_sapi_name ();
if ( substr ( $sapi_type , 0 , 3 ) == 'cgi' ) {
2020-04-10 10:59:32 +02:00
echo " Error: You are using PHP for CGI. To execute " . $script_file . " from command line, you must use PHP for CLI mode. \n " ;
exit ( - 1 );
2013-05-07 12:52:14 +02:00
}
2020-04-10 10:59:32 +02:00
if ( ! isset ( $argv [ 1 ]) || ! $argv [ 1 ] || ! in_array ( $argv [ 1 ], array ( 'test' , 'confirm' ))) {
2013-07-31 12:33:18 +02:00
print " Usage: $script_file (test|confirm) [delay] \n " ;
2013-05-07 12:52:14 +02:00
print " \n " ;
2013-05-07 13:18:16 +02:00
print " Send an email to remind all contracts services to expire, to users that are sale representative for. \n " ;
2013-05-07 12:52:14 +02:00
print " If you choose 'test' mode, no emails are sent. \n " ;
2013-05-07 13:18:16 +02:00
print " If you add a delay (nb of days), only services with expired date < today + delay are included. \n " ;
2020-04-10 10:59:32 +02:00
exit ( - 1 );
2013-05-07 12:52:14 +02:00
}
2019-05-15 13:20:02 +02:00
$mode = $argv [ 1 ];
2013-05-07 12:52:14 +02:00
2020-04-10 10:59:32 +02:00
require $path . " ../../htdocs/master.inc.php " ;
require_once DOL_DOCUMENT_ROOT . " /core/class/CMailFile.class.php " ;
2013-05-07 12:52:14 +02:00
2020-04-10 10:59:32 +02:00
$langs -> loadLangs ( array ( 'main' , 'contracts' ));
2013-05-07 12:52:14 +02:00
2013-06-05 16:12:07 +02:00
// Global variables
2019-05-15 13:20:02 +02:00
$version = DOL_VERSION ;
$error = 0 ;
2013-06-05 16:12:07 +02:00
2013-05-07 12:52:14 +02:00
/*
* Main
*/
2013-06-05 16:12:07 +02:00
@ set_time_limit ( 0 );
2020-04-10 10:59:32 +02:00
print " ***** " . $script_file . " ( " . $version . " ) pid= " . dol_getmypid () . " ***** \n " ;
dol_syslog ( $script_file . " launched with arg " . join ( ',' , $argv ));
2019-05-15 13:20:02 +02:00
$now = dol_now ( 'tzserver' );
$duration_value = isset ( $argv [ 2 ]) ? $argv [ 2 ] : 'none' ;
2020-04-10 10:59:32 +02:00
print $script_file . " launched with mode " . $mode . " default lang= " . $langs -> defaultlang . ( is_numeric ( $duration_value ) ? " delay= " . $duration_value : " " ) . " \n " ;
2019-05-15 13:20:02 +02:00
2021-01-02 17:43:02 +01:00
if ( $mode != 'confirm' ) {
2019-05-15 13:20:02 +02:00
$conf -> global -> MAIN_DISABLE_ALL_MAILS = 1 ;
2021-01-02 17:43:02 +01:00
}
2019-05-15 13:20:02 +02:00
$sql = " SELECT DISTINCT c.ref, c.fk_soc, cd.date_fin_validite, cd.total_ttc, cd.description as description, p.label as plabel, s.rowid, s.nom as name, s.email, s.default_lang, " ;
$sql .= " u.rowid as uid, u.lastname, u.firstname, u.email, u.lang " ;
2020-04-10 10:59:32 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " societe AS s, " . MAIN_DB_PREFIX . " contrat AS c, " . MAIN_DB_PREFIX . " contratdet AS cd " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product AS p ON p.rowid = cd.fk_product, " . MAIN_DB_PREFIX . " societe_commerciaux AS sc, " . MAIN_DB_PREFIX . " user AS u " ;
2019-05-15 13:20:02 +02:00
$sql .= " WHERE s.rowid = c.fk_soc AND c.rowid = cd.fk_contrat AND c.statut > 0 AND cd.statut<5 " ;
2021-03-01 00:22:36 +01:00
if ( is_numeric ( $duration_value )) {
2020-04-10 10:59:32 +02:00
$sql .= " AND cd.date_fin_validite < ' " . $db -> idate ( dol_time_plus_duree ( $now , $duration_value , " d " )) . " ' " ;
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
$sql .= " AND sc.fk_soc = s.rowid AND sc.fk_user = u.rowid " ;
$sql .= " ORDER BY u.email ASC, s.rowid ASC, c.ref 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' ;
2021-04-26 15:44:24 +02:00
$oldsalerepresentative = '' ;
2019-05-15 13:20:02 +02:00
$olduid = 0 ;
$oldlang = '' ;
$total = 0 ;
$foundtoprocess = 0 ;
2020-04-10 10:59:32 +02:00
print " We found " . $num . " couples (services to expire - sale representative) qualified \n " ;
dol_syslog ( " We found " . $num . " couples (services to expire - sale representative) qualified " );
2019-05-15 13:20:02 +02:00
$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 , $oldsalerepresentative , $duration_value );
} else {
2021-03-01 00:22:36 +01:00
if ( $oldemail != 'none' ) {
2020-04-10 10:59:32 +02:00
print " - No email sent for " . $oldsalerepresentative . " , total: " . $total . " \n " ;
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
}
$oldemail = $obj -> email ;
$olduid = $obj -> uid ;
$oldlang = $obj -> lang ;
$oldsalerepresentative = dolGetFirstLastname ( $obj -> firstname , $obj -> lastname );
$message = '' ;
$total = 0 ;
$foundtoprocess = 0 ;
$salerepresentative = dolGetFirstLastname ( $obj -> firstname , $obj -> lastname );
2021-03-01 00:22:36 +01:00
if ( empty ( $obj -> email )) {
2020-04-10 10:59:32 +02:00
print " Warning: Sale representative " . $salerepresentative . " has no email. Notice disabled. \n " ;
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
}
// Define line content
$outputlangs = new Translate ( '' , $conf );
$outputlangs -> setDefaultLang ( empty ( $obj -> lang ) ? $langs -> defaultlang : $obj -> lang ); // By default language of sale representative
// Load translation files required by the page
2020-04-10 10:59:32 +02:00
$outputlangs -> loadLangs ( array ( " main " , " contracts " , " bills " , " products " ));
2019-05-15 13:20:02 +02:00
if ( dol_strlen ( $obj -> email )) {
2020-04-10 10:59:32 +02:00
$message .= $outputlangs -> trans ( " Contract " ) . " " . $obj -> ref . " : " . $langs -> trans ( " Service " ) . " " . dol_concatdesc ( $obj -> plabel , $obj -> description ) . " ( " . price ( $obj -> total_ttc , 0 , $outputlangs , 0 , 0 , - 1 , $conf -> currency ) . " ) " . $obj -> name . " , " . $outputlangs -> trans ( " DateEndPlannedShort " ) . " " . dol_print_date ( $db -> jdate ( $obj -> date_fin_validite ), 'day' ) . " \n \n " ;
dol_syslog ( " email_expire_services_to_representatives.php: " . $obj -> email );
$foundtoprocess ++ ;
2019-05-15 13:20:02 +02:00
}
2020-04-10 10:59:32 +02:00
print " Service to expire " . $obj -> ref . " , label " . dol_concatdesc ( $obj -> plabel , $obj -> description ) . " , due date " . dol_print_date ( $db -> jdate ( $obj -> date_fin_validite ), 'day' ) . " (linked to company " . $obj -> name . " , sale representative " . dolGetFirstLastname ( $obj -> firstname , $obj -> lastname ) . " , email " . $obj -> email . " ): " ;
2021-03-01 00:22:36 +01:00
if ( dol_strlen ( $obj -> email )) {
2019-05-15 13:20:02 +02:00
print " qualified. " ;
2021-03-01 00:22:36 +01:00
} else {
print " disqualified (no email). " ;
}
2013-05-07 12:52:14 +02:00
print " \n " ;
2013-07-31 12:33:18 +02:00
unset ( $outputlangs );
$total += $obj -> total_ttc ;
2020-04-10 10:59:32 +02:00
$i ++ ;
2019-05-15 13:20:02 +02:00
}
2013-06-09 14:40:33 +02:00
2019-05-15 13:20:02 +02:00
// Si il reste des envois en buffer
if ( $foundtoprocess ) {
2021-03-01 00:22:36 +01:00
if ( dol_strlen ( $oldemail ) && $oldemail != 'none' ) { // Break onto email (new email)
2019-05-15 13:20:02 +02:00
envoi_mail ( $mode , $oldemail , $message , $total , $oldlang , $oldsalerepresentative , $duration_value );
} else {
2021-03-01 00:22:36 +01:00
if ( $oldemail != 'none' ) {
2020-04-10 10:59:32 +02:00
print " - No email sent for " . $oldsalerepresentative . " , total: " . $total . " \n " ;
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
}
}
} else {
print " No services to expire (for companies linked to a particular commercial dolibarr user) found \n " ;
}
exit ( 0 );
} else {
dol_print_error ( $db );
dol_syslog ( " email_expire_services_to_representatives.php: Error " );
2020-04-10 10:59:32 +02:00
exit ( - 1 );
2013-05-07 12:52:14 +02:00
}
/**
2019-05-15 13:20:02 +02:00
* Send email
2013-05-07 12:52:14 +02:00
*
2019-05-16 14:38:01 +02:00
* @ 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 $oldsalerepresentative Old sale representative
* @ param int $duration_value Duration value
* @ return int < 0 if KO , > 0 if OK
2013-05-07 12:52:14 +02:00
*/
2019-01-27 15:20:16 +01:00
function envoi_mail ( $mode , $oldemail , $message , $total , $userlang , $oldsalerepresentative , $duration_value )
2013-05-07 12:52:14 +02:00
{
2019-05-15 13:20:02 +02:00
global $conf , $langs ;
2021-03-01 00:22:36 +01:00
if ( getenv ( 'DOL_FORCE_EMAIL_TO' )) {
2019-05-15 13:20:02 +02:00
$oldemail = getenv ( 'DOL_FORCE_EMAIL_TO' );
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
$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 ( " contracts " );
if ( $duration_value ) {
2021-01-02 17:43:02 +01:00
if ( $duration_value > 0 ) {
2019-05-15 13:20:02 +02:00
$title = $newlangs -> transnoentities ( " ListOfServicesToExpireWithDuration " , $duration_value );
2021-01-02 17:43:02 +01:00
} else {
$title = $newlangs -> transnoentities ( " ListOfServicesToExpireWithDurationNeg " , $duration_value );
}
} else {
$title = $newlangs -> transnoentities ( " ListOfServicesToExpire " );
}
2019-05-15 13:20:02 +02:00
$subject = ( empty ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_SUBJECT ) ? $title : $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_SUBJECT );
$sendto = $oldemail ;
2021-01-02 17:43:02 +01:00
$from = empty ( $conf -> global -> MAIN_MAIL_EMAIL_FROM ) ? '' : $conf -> global -> MAIN_MAIL_EMAIL_FROM ;
$errorsto = empty ( $conf -> global -> MAIN_MAIL_ERRORS_TO ) ? '' : $conf -> global -> MAIN_MAIL_ERRORS_TO ;
2019-05-15 13:20:02 +02:00
$msgishtml = - 1 ;
2020-04-10 10:59:32 +02:00
print " - Send email for " . $oldsalerepresentative . " ( " . $oldemail . " ), total: " . $total . " \n " ;
dol_syslog ( " email_expire_services_to_representatives.php: send mail to " . $oldemail );
2019-05-15 13:20:02 +02:00
$usehtml = 0 ;
2021-01-02 17:43:02 +01:00
if ( ! empty ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER ) && dol_textishtml ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER )) {
2019-05-15 13:20:02 +02:00
$usehtml += 1 ;
2021-01-02 17:43:02 +01:00
}
if ( ! empty ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_HEADER ) && dol_textishtml ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_HEADER )) {
2019-05-15 13:20:02 +02:00
$usehtml += 1 ;
2021-01-02 17:43:02 +01:00
}
2019-05-15 13:20:02 +02:00
$allmessage = '' ;
2020-04-10 10:59:32 +02:00
if ( ! empty ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_HEADER )) {
2019-05-15 13:20:02 +02:00
$allmessage .= $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_HEADER ;
} else {
2020-04-10 10:59:32 +02:00
$allmessage .= $title . ( $usehtml ? " <br> \n " : " \n " ) . ( $usehtml ? " <br> \n " : " \n " );
$allmessage .= $newlangs -> transnoentities ( " NoteListOfYourExpiredServices " ) . ( $usehtml ? " <br> \n " : " \n " ) . ( $usehtml ? " <br> \n " : " \n " );
2019-05-15 13:20:02 +02:00
}
2020-04-10 10:59:32 +02:00
$allmessage .= $message . ( $usehtml ? " <br> \n " : " \n " );
$allmessage .= $langs -> trans ( " Total " ) . " = " . price ( $total , 0 , $userlang , 0 , 0 , - 1 , $conf -> currency ) . ( $usehtml ? " <br> \n " : " \n " );
if ( ! empty ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER )) {
2019-05-15 13:20:02 +02:00
$allmessage .= $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER ;
2021-03-01 00:22:36 +01:00
if ( dol_textishtml ( $conf -> global -> SCRIPT_EMAIL_EXPIRE_SERVICES_SALESREPRESENTATIVES_FOOTER )) {
2019-05-15 13:20:02 +02:00
$usehtml += 1 ;
2021-03-01 00:22:36 +01:00
}
2019-05-15 13:20:02 +02:00
}
$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 ();
2020-04-10 10:59:32 +02:00
if ( ! $result ) {
print " Error sending email " . $mail -> error . " \n " ;
dol_syslog ( " Error sending email " . $mail -> error . " \n " );
2019-05-15 13:20:02 +02:00
}
} 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 {
2020-04-10 10:59:32 +02:00
return - 1 ;
2019-05-15 13:20:02 +02:00
}
2013-05-07 12:52:14 +02:00
}