2016-07-25 21:50:41 +02:00
< ? php
2017-07-14 14:40:09 +02:00
/* Copyright ( C ) 2015 - 2017 Laurent Destailleur < eldy @ users . sourceforge . net >
2021-06-03 14:36:47 +02:00
* Copyright ( C ) 2018 - 2021 Nicolas ZABOURI < info @ inovea - conseil . com >
2018-05-17 19:05:25 +02:00
* Copyright ( C ) 2018 Juanjo Menent < jmenent @ 2 byte . es >
2019-01-04 10:52:10 +01:00
* Copyright ( C ) 2019 Ferran Marcet < fmarcet @ 2 byte . es >
2021-02-22 15:54:24 +01:00
* Copyright ( C ) 2019 - 2021 Frédéric France < frederic . france @ netlogic . fr >
2016-07-25 21:50:41 +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
* 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
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
* or see https :// www . gnu . org /
2016-07-25 21:50:41 +02:00
*/
/**
* \file htdocs / core / actions_massactions . inc . php
* \brief Code for actions done with massaction button ( send by email , merge pdf , delete , ... )
*/
// $massaction must be defined
2017-12-12 16:11:40 +01:00
// $objectclass and $objectlabel must be defined
2017-06-02 23:41:18 +02:00
// $parameters, $object, $action must be defined for the hook.
2019-11-04 15:45:50 +01:00
// $permissiontoread, $permissiontoadd, $permissiontodelete, $permissiontoclose may be defined
2022-06-14 17:53:17 +02:00
// $uploaddir may be defined (example to $conf->project->dir_output."/";)
2016-07-25 21:50:41 +02:00
// $toselect may be defined
2019-06-12 12:03:46 +02:00
// $diroutputmassaction may be defined
2016-07-25 21:50:41 +02:00
// Protection
2021-02-23 22:03:23 +01:00
if ( empty ( $objectclass ) || empty ( $uploaddir )) {
2019-08-28 13:11:51 +02:00
dol_print_error ( null , 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined' );
2017-10-13 13:28:26 +02:00
exit ;
2016-07-25 21:50:41 +02:00
}
2019-11-04 15:33:49 +01:00
// For backward compatibility
2021-02-23 22:03:23 +01:00
if ( ! empty ( $permtoread ) && empty ( $permissiontoread )) {
$permissiontoread = $permtoread ;
}
if ( ! empty ( $permtocreate ) && empty ( $permissiontoadd )) {
$permissiontoadd = $permtocreate ;
}
if ( ! empty ( $permtodelete ) && empty ( $permissiontodelete )) {
$permissiontodelete = $permtodelete ;
}
2019-11-04 15:33:49 +01:00
2016-07-25 21:50:41 +02:00
2017-06-02 23:41:18 +02:00
// Mass actions. Controls on number of lines checked.
2019-11-12 09:46:08 +01:00
$maxformassaction = ( empty ( $conf -> global -> MAIN_LIMIT_FOR_MASS_ACTIONS ) ? 1000 : $conf -> global -> MAIN_LIMIT_FOR_MASS_ACTIONS );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $massaction ) && is_array ( $toselect ) && count ( $toselect ) < 1 ) {
2017-10-13 13:28:26 +02:00
$error ++ ;
setEventMessages ( $langs -> trans ( " NoRecordSelected " ), null , " warnings " );
2016-07-25 21:50:41 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error && is_array ( $toselect ) && count ( $toselect ) > $maxformassaction ) {
2019-06-25 13:14:11 +02:00
setEventMessages ( $langs -> trans ( 'TooManyRecordForMassAction' , $maxformassaction ), null , 'errors' );
2017-10-13 13:28:26 +02:00
$error ++ ;
2016-07-25 21:50:41 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == 'confirm_presend' && ! GETPOST ( 'sendmail' )) { // If we do not choose button send (for example when we change template or limit), we must not send email, but keep on send email form
2019-11-12 09:46:08 +01:00
$massaction = 'presend' ;
2016-10-29 19:12:58 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == 'confirm_presend' ) {
2017-10-13 13:28:26 +02:00
$resaction = '' ;
$nbsent = 0 ;
$nbignored = 0 ;
$langs -> load ( " mails " );
include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2022-07-05 17:11:02 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/signature.lib.php' ;
2017-10-13 13:28:26 +02:00
2019-11-12 09:46:08 +01:00
$listofobjectid = array ();
$listofobjectthirdparties = array ();
2019-08-19 17:23:47 +02:00
$listofobjectcontacts = array ();
2019-11-12 09:46:08 +01:00
$listofobjectref = array ();
$contactidtosend = array ();
$attachedfilesThirdpartyObj = array ();
2022-01-21 13:44:40 +01:00
$oneemailperrecipient = ( GETPOST ( 'oneemailperrecipient' , 'int' ) ? 1 : 0 );
2017-10-13 13:28:26 +02:00
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2019-11-12 09:46:08 +01:00
$thirdparty = new Societe ( $db );
2017-10-13 13:28:26 +02:00
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db );
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'expensereport' ) {
$thirdparty = new User ( $db );
}
2022-05-10 16:19:00 +02:00
if ( $objecttmp -> element == 'partnership' && getDolGlobalString ( 'PARTNERSHIP_IS_MANAGED_FOR' ) == 'member' ) {
2021-04-16 10:54:11 +02:00
$thirdparty = new Adherent ( $db );
}
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'holiday' ) {
$thirdparty = new User ( $db );
}
2019-06-24 16:28:48 +02:00
2021-02-23 22:03:23 +01:00
foreach ( $toselect as $toselectid ) {
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db ); // we must create new instance because instance is saved into $listofobjectref array for future use
$result = $objecttmp -> fetch ( $toselectid );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2019-11-12 09:46:08 +01:00
$listofobjectid [ $toselectid ] = $toselectid ;
2019-06-24 16:28:48 +02:00
2019-11-12 09:46:08 +01:00
$thirdpartyid = ( $objecttmp -> fk_soc ? $objecttmp -> fk_soc : $objecttmp -> socid );
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'societe' ) {
$thirdpartyid = $objecttmp -> id ;
}
if ( $objecttmp -> element == 'expensereport' ) {
$thirdpartyid = $objecttmp -> fk_user_author ;
}
2022-05-10 16:19:00 +02:00
if ( $objecttmp -> element == 'partnership' && getDolGlobalString ( 'PARTNERSHIP_IS_MANAGED_FOR' ) == 'member' ) {
2021-04-16 10:54:11 +02:00
$thirdpartyid = $objecttmp -> fk_member ;
}
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'holiday' ) {
$thirdpartyid = $objecttmp -> fk_user ;
}
if ( empty ( $thirdpartyid )) {
$thirdpartyid = 0 ;
}
2019-06-24 16:28:48 +02:00
2019-08-19 17:23:47 +02:00
if ( $objectclass == 'Facture' ) {
$tmparraycontact = array ();
$tmparraycontact = $objecttmp -> liste_contact ( - 1 , 'external' , 0 , 'BILLING' );
if ( is_array ( $tmparraycontact ) && count ( $tmparraycontact ) > 0 ) {
foreach ( $tmparraycontact as $data_email ) {
$listofobjectcontacts [ $toselectid ][ $data_email [ 'id' ]] = $data_email [ 'email' ];
}
}
2022-04-27 10:09:22 +02:00
} elseif ( $objectclass == 'CommandeFournisseur' ) {
$tmparraycontact = array ();
$tmparraycontact = $objecttmp -> liste_contact ( - 1 , 'external' , 0 , 'CUSTOMER' );
if ( is_array ( $tmparraycontact ) && count ( $tmparraycontact ) > 0 ) {
foreach ( $tmparraycontact as $data_email ) {
$listofobjectcontacts [ $toselectid ][ $data_email [ 'id' ]] = $data_email [ 'email' ];
}
}
2019-08-19 17:23:47 +02:00
}
2019-04-29 17:40:59 +02:00
2020-10-31 14:32:18 +01:00
$listofobjectthirdparties [ $thirdpartyid ] = $thirdpartyid ;
$listofobjectref [ $thirdpartyid ][ $toselectid ] = $objecttmp ;
}
}
}
2017-10-13 13:28:26 +02:00
// Check mandatory parameters
2021-02-23 22:03:23 +01:00
if ( GETPOST ( 'fromtype' , 'alpha' ) === 'user' && empty ( $user -> email )) {
2017-10-13 13:28:26 +02:00
$error ++ ;
setEventMessages ( $langs -> trans ( " NoSenderEmailDefined " ), null , 'warnings' );
2019-11-12 09:46:08 +01:00
$massaction = 'presend' ;
2017-10-13 13:28:26 +02:00
}
2022-02-22 23:44:56 +01:00
$receiver = GETPOST ( 'receiver' , 'alphawithlgt' );
2021-02-23 22:03:23 +01:00
if ( ! is_array ( $receiver )) {
if ( empty ( $receiver ) || $receiver == '-1' ) {
$receiver = array ();
} else {
$receiver = array ( $receiver );
}
2017-10-13 13:28:26 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! trim ( $_POST [ 'sendto' ]) && count ( $receiver ) == 0 && count ( $listofobjectthirdparties ) == 1 ) { // if only one recipient, receiver is mandatory
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " Recipient " )), null , 'warnings' );
$massaction = 'presend' ;
2017-10-13 13:28:26 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! GETPOST ( 'subject' , 'restricthtml' )) {
2017-10-13 13:28:26 +02:00
$error ++ ;
setEventMessages ( $langs -> trans ( " ErrorFieldRequired " , $langs -> transnoentitiesnoconv ( " MailTopic " )), null , 'warnings' );
2019-11-12 09:46:08 +01:00
$massaction = 'presend' ;
2017-10-13 13:28:26 +02:00
}
// Loop on each recipient/thirdparty
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
foreach ( $listofobjectthirdparties as $thirdpartyid ) {
2017-10-13 13:28:26 +02:00
$result = $thirdparty -> fetch ( $thirdpartyid );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2017-10-13 13:28:26 +02:00
dol_print_error ( $db );
exit ;
}
2019-11-12 09:46:08 +01:00
$sendto = '' ;
$sendtocc = '' ;
$sendtobcc = '' ;
2017-10-13 13:28:26 +02:00
$sendtoid = array ();
// Define $sendto
2019-11-12 09:46:08 +01:00
$tmparray = array ();
2021-02-23 22:03:23 +01:00
if ( trim ( $_POST [ 'sendto' ])) {
2017-10-13 13:28:26 +02:00
// Recipients are provided into free text
2022-02-22 23:44:56 +01:00
$tmparray [] = trim ( GETPOST ( 'sendto' , 'alphawithlgt' ));
2017-10-13 13:28:26 +02:00
}
2021-02-23 22:03:23 +01:00
if ( count ( $receiver ) > 0 ) {
foreach ( $receiver as $key => $val ) {
2017-10-13 13:28:26 +02:00
// Recipient was provided from combo list
2021-02-23 22:03:23 +01:00
if ( $val == 'thirdparty' ) { // Id of third party or user
2017-10-13 13:28:26 +02:00
$tmparray [] = $thirdparty -> name . ' <' . $thirdparty -> email . '>' ;
2021-02-23 22:03:23 +01:00
} elseif ( $val && method_exists ( $thirdparty , 'contact_get_property' )) { // Id of contact
2019-06-25 13:14:11 +02:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2017-10-13 13:28:26 +02:00
$sendtoid [] = $val ;
}
}
}
2019-11-12 09:46:08 +01:00
$sendto = implode ( ',' , $tmparray );
2017-10-13 13:28:26 +02:00
// Define $sendtocc
2022-02-22 23:44:56 +01:00
$receivercc = GETPOST ( 'receivercc' , 'alphawithlgt' );
2021-02-23 22:03:23 +01:00
if ( ! is_array ( $receivercc )) {
if ( $receivercc == '-1' ) {
$receivercc = array ();
} else {
$receivercc = array ( $receivercc );
}
2017-10-13 13:28:26 +02:00
}
2019-11-12 09:46:08 +01:00
$tmparray = array ();
2021-02-23 22:03:23 +01:00
if ( trim ( $_POST [ 'sendtocc' ])) {
2022-02-22 23:44:56 +01:00
$tmparray [] = trim ( GETPOST ( 'sendtocc' , 'alphawithlgt' ));
2017-10-13 13:28:26 +02:00
}
2021-02-23 22:03:23 +01:00
if ( count ( $receivercc ) > 0 ) {
foreach ( $receivercc as $key => $val ) {
2017-10-13 13:28:26 +02:00
// Recipient was provided from combo list
2021-02-23 22:03:23 +01:00
if ( $val == 'thirdparty' ) { // Id of third party
2017-10-13 13:28:26 +02:00
$tmparray [] = $thirdparty -> name . ' <' . $thirdparty -> email . '>' ;
2021-02-23 22:03:23 +01:00
} elseif ( $val ) { // Id du contact
2019-06-25 14:30:48 +02:00
$tmparray [] = $thirdparty -> contact_get_property (( int ) $val , 'email' );
2017-10-13 13:28:26 +02:00
//$sendtoid[] = $val; TODO Add also id of contact in CC ?
}
}
}
2019-11-12 09:46:08 +01:00
$sendtocc = implode ( ',' , $tmparray );
2017-10-13 13:28:26 +02:00
//var_dump($listofobjectref);exit;
2019-11-12 09:46:08 +01:00
$listofqualifiedobj = array ();
$listofqualifiedref = array ();
$thirdpartywithoutemail = array ();
2017-10-13 13:28:26 +02:00
2021-02-23 22:03:23 +01:00
foreach ( $listofobjectref [ $thirdpartyid ] as $objectid => $objectobj ) {
2017-12-12 16:11:40 +01:00
//var_dump($thirdpartyid.' - '.$objectid.' - '.$objectobj->statut);
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Propal' && $objectobj -> statut == Propal :: STATUS_DRAFT ) {
2017-10-13 13:28:26 +02:00
$langs -> load ( " errors " );
$nbignored ++ ;
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="error">' . $langs -> trans ( 'ErrorOnlyProposalNotDraftCanBeSentInMassAction' , $objectobj -> ref ) . '</div><br>' ;
2017-10-13 13:28:26 +02:00
continue ; // Payment done or started or canceled
}
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Commande' && $objectobj -> statut == Commande :: STATUS_DRAFT ) {
2017-10-13 13:28:26 +02:00
$langs -> load ( " errors " );
$nbignored ++ ;
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="error">' . $langs -> trans ( 'ErrorOnlyOrderNotDraftCanBeSentInMassAction' , $objectobj -> ref ) . '</div><br>' ;
2017-10-13 13:28:26 +02:00
continue ;
}
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Facture' && $objectobj -> statut == Facture :: STATUS_DRAFT ) {
2017-10-13 13:28:26 +02:00
$langs -> load ( " errors " );
$nbignored ++ ;
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="error">' . $langs -> trans ( 'ErrorOnlyInvoiceValidatedCanBeSentInMassAction' , $objectobj -> ref ) . '</div><br>' ;
2017-10-13 13:28:26 +02:00
continue ; // Payment done or started or canceled
}
// Test recipient
2021-02-23 22:03:23 +01:00
if ( empty ( $sendto )) { // For the case, no recipient were set (multi thirdparties send)
if ( $objectobj -> element == 'societe' ) {
2020-04-05 15:26:16 +02:00
$sendto = $objectobj -> email ;
2021-02-23 22:03:23 +01:00
} elseif ( $objectobj -> element == 'expensereport' ) {
2017-09-16 22:03:25 +02:00
$fuser = new User ( $db );
2017-12-12 16:11:40 +01:00
$fuser -> fetch ( $objectobj -> fk_user_author );
2017-09-16 22:03:25 +02:00
$sendto = $fuser -> email ;
2022-05-10 16:19:00 +02:00
} elseif ( $objectobj -> element == 'partnership' && getDolGlobalString ( 'PARTNERSHIP_IS_MANAGED_FOR' ) == 'member' ) {
2021-04-16 10:54:11 +02:00
$fadherent = new Adherent ( $db );
$fadherent -> fetch ( $objectobj -> fk_member );
$sendto = $fadherent -> email ;
2021-02-23 22:03:23 +01:00
} elseif ( $objectobj -> element == 'holiday' ) {
2020-10-31 14:32:18 +01:00
$fuser = new User ( $db );
$fuser -> fetch ( $objectobj -> fk_user );
$sendto = $fuser -> email ;
2021-02-23 22:03:23 +01:00
} elseif ( $objectobj -> element == 'facture' && ! empty ( $listofobjectcontacts [ $objectid ])) {
2019-08-19 17:23:47 +02:00
$emails_to_sends = array ();
$objectobj -> fetch_thirdparty ();
2019-11-12 09:46:08 +01:00
$contactidtosend = array ();
2019-08-19 17:23:47 +02:00
foreach ( $listofobjectcontacts [ $objectid ] as $contactemailid => $contactemailemail ) {
$emails_to_sends [] = $objectobj -> thirdparty -> contact_get_property ( $contactemailid , 'email' );
if ( ! in_array ( $contactemailid , $contactidtosend )) {
$contactidtosend [] = $contactemailid ;
}
}
if ( count ( $emails_to_sends ) > 0 ) {
$sendto = implode ( ',' , $emails_to_sends );
}
2022-04-27 10:09:22 +02:00
} elseif ( $objectobj -> element == 'order_supplier' && ! empty ( $listofobjectcontacts [ $objectid ])) {
$emails_to_sends = array ();
$objectobj -> fetch_thirdparty ();
$contactidtosend = array ();
foreach ( $listofobjectcontacts [ $objectid ] as $contactemailid => $contactemailemail ) {
$emails_to_sends [] = $objectobj -> thirdparty -> contact_get_property ( $contactemailid , 'email' );
if ( ! in_array ( $contactemailid , $contactidtosend )) {
$contactidtosend [] = $contactemailid ;
}
}
if ( count ( $emails_to_sends ) > 0 ) {
$sendto = implode ( ',' , $emails_to_sends );
}
2020-05-21 15:05:19 +02:00
} else {
2017-12-12 16:11:40 +01:00
$objectobj -> fetch_thirdparty ();
$sendto = $objectobj -> thirdparty -> email ;
2017-10-13 13:28:26 +02:00
}
}
2019-01-30 20:02:41 +01:00
2021-02-23 22:03:23 +01:00
if ( empty ( $sendto )) {
2020-04-05 15:26:16 +02:00
if ( $objectobj -> element == 'societe' ) {
2020-04-10 10:59:32 +02:00
$objectobj -> thirdparty = $objectobj ; // Hack so following code is comaptible when objectobj is a thirdparty
2020-04-05 15:26:16 +02:00
}
2021-02-23 22:03:23 +01:00
//print "No recipient for thirdparty ".$objectobj->thirdparty->name;
$nbignored ++ ;
if ( empty ( $thirdpartywithoutemail [ $objectobj -> thirdparty -> id ])) {
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="error">' . $langs -> trans ( 'NoRecipientEmail' , $objectobj -> thirdparty -> name ) . '</div><br>' ;
2017-10-13 13:28:26 +02:00
}
2017-12-12 16:11:40 +01:00
dol_syslog ( 'No recipient for thirdparty: ' . $objectobj -> thirdparty -> name , LOG_WARNING );
2019-11-12 09:46:08 +01:00
$thirdpartywithoutemail [ $objectobj -> thirdparty -> id ] = 1 ;
2021-02-23 22:03:23 +01:00
continue ;
2017-10-13 13:28:26 +02:00
}
2019-01-30 20:02:41 +01:00
2022-02-22 23:44:56 +01:00
if ( GETPOST ( 'addmaindocfile' )) {
2017-12-12 16:11:40 +01:00
// TODO Use future field $objectobj->fullpathdoc to know where is stored default file
2021-02-08 18:30:09 +01:00
// TODO If not defined, use $objectobj->model_pdf (or defaut invoice config) to know what is template to use to regenerate doc.
2019-06-12 12:33:31 +02:00
$filename = dol_sanitizeFileName ( $objectobj -> ref ) . '.pdf' ;
$subdir = '' ;
2019-06-13 12:11:31 +02:00
// TODO Set subdir to be compatible with multi levels dir trees
2019-06-12 12:33:31 +02:00
// $subdir = get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element)
2019-11-12 09:46:08 +01:00
$filedir = $uploaddir . '/' . $subdir . dol_sanitizeFileName ( $objectobj -> ref );
2022-04-22 09:24:23 +02:00
$filepath = $filedir . '/' . $filename ;
2019-06-12 12:08:16 +02:00
2019-06-12 12:33:31 +02:00
// For supplier invoices, we use the file provided by supplier, not the one we generate
2021-02-23 22:03:23 +01:00
if ( $objectobj -> element == 'invoice_supplier' ) {
2019-11-12 09:46:08 +01:00
$fileparams = dol_most_recent_file ( $uploaddir . '/' . get_exdir ( $objectobj -> id , 2 , 0 , 0 , $objectobj , $objectobj -> element ) . $objectobj -> ref , preg_quote ( $objectobj -> ref , '/' ) . '([^\-])+' );
2022-04-22 09:24:23 +02:00
$filepath = $fileparams [ 'fullname' ];
2019-06-12 12:05:02 +02:00
}
2019-06-12 12:08:16 +02:00
2022-04-22 09:24:23 +02:00
// try to find other files generated for this object (last_main_doc)
$filename_found = '' ;
$filepath_found = '' ;
$file_check_list = array ();
$file_check_list [] = array (
'name' => $filename ,
'path' => $filepath ,
);
2022-05-02 10:50:48 +02:00
if ( ! empty ( $conf -> global -> MAIL_MASS_ACTION_ADD_LAST_IF_MAIN_DOC_NOT_FOUND ) && ! empty ( $objectobj -> last_main_doc )) {
2022-04-22 09:24:23 +02:00
$file_check_list [] = array (
'name' => basename ( $objectobj -> last_main_doc ),
'path' => DOL_DATA_ROOT . '/' . $objectobj -> last_main_doc ,
);
}
foreach ( $file_check_list as $file_check_arr ) {
if ( dol_is_file ( $file_check_arr [ 'path' ])) {
$filename_found = $file_check_arr [ 'name' ];
$filepath_found = $file_check_arr [ 'path' ];
break ;
}
}
2019-01-30 20:02:41 +01:00
2022-04-22 09:24:23 +02:00
if ( $filepath_found ) {
2019-06-20 10:32:32 +02:00
// Create form object
2019-11-12 09:46:08 +01:00
$attachedfilesThirdpartyObj [ $thirdpartyid ][ $objectid ] = array (
2022-04-22 09:24:23 +02:00
'paths' => array ( $filepath_found ),
'names' => array ( $filename_found ),
'mimes' => array ( dol_mimetype ( $filepath_found ))
2019-06-20 10:32:32 +02:00
);
2020-05-21 15:05:19 +02:00
} else {
2022-04-22 09:24:23 +02:00
$nbignored ++ ;
$langs -> load ( " errors " );
foreach ( $file_check_list as $file_check_arr ) {
$resaction .= '<div class="error">' . $langs -> trans ( 'ErrorCantReadFile' , $file_check_arr [ 'path' ]) . '</div><br>' ;
dol_syslog ( 'Failed to read file: ' . $file_check_arr [ 'path' ], LOG_WARNING );
}
continue ;
2017-10-13 13:28:26 +02:00
}
}
2019-06-24 16:12:20 +02:00
// Object of thirdparty qualified, we add it
2019-11-12 09:46:08 +01:00
$listofqualifiedobj [ $objectid ] = $objectobj ;
$listofqualifiedref [ $objectid ] = $objectobj -> ref ;
2017-10-13 13:28:26 +02:00
//var_dump($listofqualifiedref);
}
2019-06-24 16:12:20 +02:00
// Send email if there is at least one qualified object for current thirdparty
2021-02-23 22:03:23 +01:00
if ( count ( $listofqualifiedobj ) > 0 ) {
2017-10-13 13:28:26 +02:00
$langs -> load ( " commercial " );
2020-01-20 18:38:53 +01:00
$reg = array ();
2017-10-13 13:28:26 +02:00
$fromtype = GETPOST ( 'fromtype' );
if ( $fromtype === 'user' ) {
2022-02-22 23:44:56 +01:00
$from = dol_string_nospecial ( $user -> getFullName ( $langs ), ' ' , array ( " , " )) . ' <' . $user -> email . '>' ;
2020-05-21 15:05:19 +02:00
} elseif ( $fromtype === 'company' ) {
2019-11-12 09:46:08 +01:00
$from = $conf -> global -> MAIN_INFO_SOCIETE_NOM . ' <' . $conf -> global -> MAIN_INFO_SOCIETE_MAIL . '>' ;
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/user_aliases_(\d+)/' , $fromtype , $reg )) {
2019-11-12 09:46:08 +01:00
$tmp = explode ( ',' , $user -> email_aliases );
2017-10-13 13:28:26 +02:00
$from = trim ( $tmp [( $reg [ 1 ] - 1 )]);
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/global_aliases_(\d+)/' , $fromtype , $reg )) {
2019-11-12 09:46:08 +01:00
$tmp = explode ( ',' , $conf -> global -> MAIN_INFO_SOCIETE_MAIL_ALIASES );
2017-10-13 13:28:26 +02:00
$from = trim ( $tmp [( $reg [ 1 ] - 1 )]);
2020-05-21 15:05:19 +02:00
} elseif ( preg_match ( '/senderprofile_(\d+)_(\d+)/' , $fromtype , $reg )) {
2021-09-30 15:59:47 +02:00
$sql = " SELECT rowid, label, email FROM " . MAIN_DB_PREFIX . " c_email_senderprofile WHERE rowid = " . ( int ) $reg [ 1 ];
2017-10-13 13:28:26 +02:00
$resql = $db -> query ( $sql );
$obj = $db -> fetch_object ( $resql );
2021-02-23 22:03:23 +01:00
if ( $obj ) {
2022-02-22 23:44:56 +01:00
$from = dol_string_nospecial ( $obj -> label , ' ' , array ( " , " )) . ' <' . $obj -> email . '>' ;
2017-10-13 13:28:26 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2022-02-22 23:44:56 +01:00
$from = GETPOST ( 'fromname' ) . ' <' . GETPOST ( 'frommail' ) . '>' ;
2017-10-13 13:28:26 +02:00
}
$replyto = $from ;
2020-09-18 01:29:17 +02:00
$subject = GETPOST ( 'subject' , 'restricthtml' );
$message = GETPOST ( 'message' , 'restricthtml' );
2017-10-22 01:43:33 +02:00
$sendtobcc = GETPOST ( 'sendtoccc' );
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Propal' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_PROPOSAL_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_PROPOSAL_TO ));
}
if ( $objectclass == 'Commande' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_ORDER_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_ORDER_TO ));
}
if ( $objectclass == 'Facture' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_INVOICE_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_INVOICE_TO ));
}
if ( $objectclass == 'Supplier_Proposal' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO ));
}
if ( $objectclass == 'CommandeFournisseur' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO ));
}
if ( $objectclass == 'FactureFournisseur' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO ));
}
if ( $objectclass == 'Project' ) {
$sendtobcc .= ( empty ( $conf -> global -> MAIN_MAIL_AUTOCOPY_PROJECT_TO ) ? '' : (( $sendtobcc ? " , " : " " ) . $conf -> global -> MAIN_MAIL_AUTOCOPY_PROJECT_TO ));
}
2019-06-24 16:28:48 +02:00
2018-10-29 02:02:43 +01:00
// $listofqualifiedobj is array with key = object id and value is instance of qualified objects, for the current thirdparty (but thirdparty property is not loaded yet)
2019-06-24 16:12:20 +02:00
// $looparray will be an array with number of email to send for the current thirdparty (so 1 or n if n object for same thirdparty)
2019-11-12 09:46:08 +01:00
$looparray = array ();
2021-02-23 22:03:23 +01:00
if ( ! $oneemailperrecipient ) {
2017-12-12 16:11:40 +01:00
$looparray = $listofqualifiedobj ;
2021-02-23 22:03:23 +01:00
foreach ( $looparray as $key => $objecttmp ) {
2020-10-31 14:32:18 +01:00
$looparray [ $key ] -> thirdparty = $thirdparty ; // Force thirdparty on object
2018-10-29 02:02:43 +01:00
}
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$objectforloop = new $objectclass ( $db );
$objectforloop -> thirdparty = $thirdparty ; // Force thirdparty on object (even if object was not loaded)
$looparray [ 0 ] = $objectforloop ;
2017-10-13 13:28:26 +02:00
}
//var_dump($looparray);exit;
2020-10-31 14:32:18 +01:00
dol_syslog ( " We have set an array of " . count ( $looparray ) . " emails to send. oneemailperrecipient= " . $oneemailperrecipient );
//var_dump($oneemailperrecipient); var_dump($listofqualifiedobj); var_dump($listofqualifiedref);
2021-02-23 22:03:23 +01:00
foreach ( $looparray as $objectid => $objecttmp ) { // $objecttmp is a real object or an empty object if we choose to send one email per thirdparty instead of one per object
2017-10-13 13:28:26 +02:00
// Make substitution in email content
2022-06-14 17:53:17 +02:00
if ( ! empty ( $conf -> project -> enabled ) && method_exists ( $objecttmp , 'fetch_projet' ) && is_null ( $objecttmp -> project )) {
2019-09-23 13:04:22 +02:00
$objecttmp -> fetch_projet ();
}
2019-11-12 09:46:08 +01:00
$substitutionarray = getCommonSubstitutionArray ( $langs , 0 , null , $objecttmp );
2019-06-25 14:30:48 +02:00
$substitutionarray [ '__ID__' ] = ( $oneemailperrecipient ? join ( ', ' , array_keys ( $listofqualifiedobj )) : $objecttmp -> id );
$substitutionarray [ '__REF__' ] = ( $oneemailperrecipient ? join ( ', ' , $listofqualifiedref ) : $objecttmp -> ref );
2017-10-13 13:28:26 +02:00
$substitutionarray [ '__EMAIL__' ] = $thirdparty -> email ;
2021-05-11 21:13:52 +02:00
$substitutionarray [ '__CHECK_READ__' ] = '<img src="' . DOL_MAIN_URL_ROOT . '/public/emailing/mailing-read.php?tag=' . urlencode ( $thirdparty -> tag ) . '&securitykey=' . urlencode ( $conf -> global -> MAILING_EMAIL_UNSUBSCRIBE_KEY ) . '" width="1" height="1" style="width:1px;height:1px" border="0"/>' ;
2017-10-13 13:28:26 +02:00
2019-11-12 09:46:08 +01:00
$parameters = array ( 'mode' => 'formemail' );
2018-04-11 04:27:25 +02:00
2019-11-12 09:46:08 +01:00
if ( ! empty ( $listofobjectthirdparties )) {
2018-04-11 04:27:25 +02:00
$parameters [ 'listofobjectthirdparties' ] = $listofobjectthirdparties ;
}
2019-11-12 09:46:08 +01:00
if ( ! empty ( $listofobjectref )) {
2018-04-11 04:27:25 +02:00
$parameters [ 'listofobjectref' ] = $listofobjectref ;
}
2019-01-30 20:02:41 +01:00
2017-10-13 13:28:26 +02:00
complete_substitutions_array ( $substitutionarray , $langs , $objecttmp , $parameters );
2019-01-30 20:02:41 +01:00
2020-10-31 14:32:18 +01:00
$subjectreplaced = make_substitutions ( $subject , $substitutionarray );
$messagereplaced = make_substitutions ( $message , $substitutionarray );
2019-01-30 20:02:41 +01:00
2020-10-31 14:32:18 +01:00
$attachedfiles = array ( 'paths' => array (), 'names' => array (), 'mimes' => array ());
2021-02-23 22:03:23 +01:00
if ( $oneemailperrecipient ) {
2019-06-24 16:12:20 +02:00
// if "one email per recipient" is check we must collate $attachedfiles by thirdparty
2021-02-23 22:03:23 +01:00
if ( is_array ( $attachedfilesThirdpartyObj [ $thirdparty -> id ]) && count ( $attachedfilesThirdpartyObj [ $thirdparty -> id ])) {
foreach ( $attachedfilesThirdpartyObj [ $thirdparty -> id ] as $keyObjId => $objAttachedFiles ) {
2019-06-20 10:32:32 +02:00
// Create form object
2019-11-12 09:46:08 +01:00
$attachedfiles = array (
2019-06-20 10:32:32 +02:00
'paths' => array_merge ( $attachedfiles [ 'paths' ], $objAttachedFiles [ 'paths' ]),
'names' => array_merge ( $attachedfiles [ 'names' ], $objAttachedFiles [ 'names' ]),
'mimes' => array_merge ( $attachedfiles [ 'mimes' ], $objAttachedFiles [ 'mimes' ])
);
}
}
2020-05-21 15:05:19 +02:00
} elseif ( ! empty ( $attachedfilesThirdpartyObj [ $thirdparty -> id ][ $objectid ])) {
2019-06-20 10:32:32 +02:00
// Create form object
2019-06-20 11:09:37 +02:00
// if "one email per recipient" isn't check we must separate $attachedfiles by object
2019-11-12 09:46:08 +01:00
$attachedfiles = $attachedfilesThirdpartyObj [ $thirdparty -> id ][ $objectid ];
2019-06-20 10:32:32 +02:00
}
2017-10-13 13:28:26 +02:00
$filepath = $attachedfiles [ 'paths' ];
$filename = $attachedfiles [ 'names' ];
$mimetype = $attachedfiles [ 'mimes' ];
2019-06-24 16:12:20 +02:00
// Define the trackid when emails sent from the mass action
2021-02-23 22:03:23 +01:00
if ( $oneemailperrecipient ) {
2020-10-31 14:32:18 +01:00
$trackid = 'thi' . $thirdparty -> id ;
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'expensereport' ) {
$trackid = 'use' . $thirdparty -> id ;
}
if ( $objecttmp -> element == 'holiday' ) {
$trackid = 'use' . $thirdparty -> id ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
$trackid = strtolower ( get_class ( $objecttmp ));
2021-02-23 22:03:23 +01:00
if ( get_class ( $objecttmp ) == 'Contrat' ) {
$trackid = 'con' ;
}
if ( get_class ( $objecttmp ) == 'Propal' ) {
$trackid = 'pro' ;
}
if ( get_class ( $objecttmp ) == 'Commande' ) {
$trackid = 'ord' ;
}
if ( get_class ( $objecttmp ) == 'Facture' ) {
$trackid = 'inv' ;
}
if ( get_class ( $objecttmp ) == 'Supplier_Proposal' ) {
$trackid = 'spr' ;
}
if ( get_class ( $objecttmp ) == 'CommandeFournisseur' ) {
$trackid = 'sor' ;
}
if ( get_class ( $objecttmp ) == 'FactureFournisseur' ) {
$trackid = 'sin' ;
}
2020-10-31 14:32:18 +01:00
$trackid .= $objecttmp -> id ;
2019-06-24 16:12:20 +02:00
}
2017-10-13 13:28:26 +02:00
//var_dump($filepath);
2019-06-24 16:12:20 +02:00
//var_dump($trackid);exit;
//var_dump($subjectreplaced);
2019-06-24 16:28:48 +02:00
2021-02-23 22:03:23 +01:00
if ( empty ( $sendcontext )) {
$sendcontext = 'standard' ;
}
2020-04-05 15:39:53 +02:00
2017-10-13 13:28:26 +02:00
// Send mail (substitutionarray must be done just before this)
2020-10-31 14:32:18 +01:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
$mailfile = new CMailFile ( $subjectreplaced , $sendto , $from , $messagereplaced , $filepath , $mimetype , $filename , $sendtocc , $sendtobcc , $deliveryreceipt , - 1 , '' , '' , $trackid , '' , $sendcontext );
2021-02-23 22:03:23 +01:00
if ( $mailfile -> error ) {
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="error">' . $mailfile -> error . '</div>' ;
2020-05-21 15:05:19 +02:00
} else {
2019-11-12 09:46:08 +01:00
$result = $mailfile -> sendfile ();
2021-02-23 22:03:23 +01:00
if ( $result ) {
2019-11-12 09:46:08 +01:00
$resaction .= $langs -> trans ( 'MailSuccessfulySent' , $mailfile -> getValidAddress ( $from , 2 ), $mailfile -> getValidAddress ( $sendto , 2 )) . '<br>' ; // Must not contain "
2017-10-13 13:28:26 +02:00
2019-11-12 09:46:08 +01:00
$error = 0 ;
2017-10-13 13:28:26 +02:00
// Insert logs into agenda
2021-02-23 22:03:23 +01:00
foreach ( $listofqualifiedobj as $objid2 => $objectobj2 ) {
if (( ! $oneemailperrecipient ) && $objid2 != $objectid ) {
continue ; // We discard this pass to avoid duplicate with other pass in looparray at higher level
}
2019-01-30 20:02:41 +01:00
2020-10-31 14:32:18 +01:00
dol_syslog ( " Try to insert email event into agenda for objid= " . $objid2 . " => objectobj= " . get_class ( $objectobj2 ));
2019-01-30 20:02:41 +01:00
2017-10-13 13:28:26 +02:00
/* if ( $objectclass == 'Propale' ) $actiontypecode = 'AC_PROP' ;
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Commande' ) $actiontypecode = 'AC_COM' ;
if ( $objectclass == 'Facture' ) $actiontypecode = 'AC_FAC' ;
if ( $objectclass == 'Supplier_Proposal' ) $actiontypecode = 'AC_SUP_PRO' ;
if ( $objectclass == 'CommandeFournisseur' ) $actiontypecode = 'AC_SUP_ORD' ;
if ( $objectclass == 'FactureFournisseur' ) $actiontypecode = 'AC_SUP_INV' ; */
2017-09-22 16:31:05 +02:00
2019-11-12 09:46:08 +01:00
$actionmsg = $langs -> transnoentities ( 'MailSentBy' ) . ' ' . $from . ' ' . $langs -> transnoentities ( 'To' ) . ' ' . $sendto ;
2021-02-23 22:03:23 +01:00
if ( $message ) {
if ( $sendtocc ) {
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'Bcc' ) . " : " . $sendtocc );
}
2020-10-31 14:32:18 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'MailTopic' ) . " : " . $subjectreplaced );
2019-11-12 09:46:08 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $langs -> transnoentities ( 'TextUsedInTheMessageBody' ) . " : " );
2020-10-31 14:32:18 +01:00
$actionmsg = dol_concatdesc ( $actionmsg , $messagereplaced );
2017-10-13 13:28:26 +02:00
}
2019-11-12 09:46:08 +01:00
$actionmsg2 = '' ;
2019-01-30 20:02:41 +01:00
2020-10-31 14:32:18 +01:00
// Initialisation donnees
$objectobj2 -> sendtoid = ( empty ( $contactidtosend ) ? 0 : $contactidtosend );
$objectobj2 -> actionmsg = $actionmsg ; // Long text
$objectobj2 -> actionmsg2 = $actionmsg2 ; // Short text
$objectobj2 -> fk_element = $objid2 ;
$objectobj2 -> elementtype = $objectobj2 -> element ;
2019-01-30 20:02:41 +01:00
2019-11-12 09:46:08 +01:00
$triggername = strtoupper ( get_class ( $objectobj2 )) . '_SENTBYMAIL' ;
2021-02-23 22:03:23 +01:00
if ( $triggername == 'SOCIETE_SENTBYMAIL' ) {
$triggername = 'COMPANY_SENTBYMAIL' ;
}
if ( $triggername == 'CONTRAT_SENTBYMAIL' ) {
$triggername = 'CONTRACT_SENTBYMAIL' ;
}
if ( $triggername == 'COMMANDE_SENTBYMAIL' ) {
$triggername = 'ORDER_SENTBYMAIL' ;
}
if ( $triggername == 'FACTURE_SENTBYMAIL' ) {
$triggername = 'BILL_SENTBYMAIL' ;
}
if ( $triggername == 'EXPEDITION_SENTBYMAIL' ) {
$triggername = 'SHIPPING_SENTBYMAIL' ;
}
if ( $triggername == 'COMMANDEFOURNISSEUR_SENTBYMAIL' ) {
$triggername = 'ORDER_SUPPLIER_SENTBYMAIL' ;
}
if ( $triggername == 'FACTUREFOURNISSEUR_SENTBYMAIL' ) {
$triggername = 'BILL_SUPPLIER_SENTBYMAIL' ;
}
if ( $triggername == 'SUPPLIERPROPOSAL_SENTBYMAIL' ) {
$triggername = 'PROPOSAL_SUPPLIER_SENTBYMAIL' ;
}
if ( ! empty ( $triggername )) {
2020-04-02 12:03:29 +02:00
// Call trigger
$result = $objectobj2 -> call_trigger ( $triggername , $user );
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
$error ++ ;
}
2020-04-02 12:03:29 +02:00
// End call triggers
2019-01-29 10:51:34 +01:00
2021-02-23 22:03:23 +01:00
if ( $error ) {
2017-10-13 13:28:26 +02:00
setEventMessages ( $db -> lasterror (), $errors , 'errors' );
2018-08-02 16:05:54 +02:00
dol_syslog ( " Error in trigger " . $triggername . ' ' . $db -> lasterror (), LOG_ERR );
2017-10-13 13:28:26 +02:00
}
}
2019-01-29 10:51:34 +01:00
2019-11-12 09:46:08 +01:00
$nbsent ++ ; // Nb of object sent
2019-06-24 16:28:48 +02:00
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
$langs -> load ( " other " );
2021-02-23 22:03:23 +01:00
if ( $mailfile -> error ) {
2019-11-12 09:46:08 +01:00
$resaction .= $langs -> trans ( 'ErrorFailedToSendMail' , $from , $sendto );
$resaction .= '<br><div class="error">' . $mailfile -> error . '</div>' ;
2021-05-18 17:57:23 +02:00
} elseif ( ! empty ( $conf -> global -> MAIN_DISABLE_ALL_MAILS )) {
2019-11-12 09:46:08 +01:00
$resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>' ;
2021-05-18 17:57:23 +02:00
} else {
$resaction .= $langs -> trans ( 'ErrorFailedToSendMail' , $from , $sendto ) . '<br><div class="error">(unhandled error)</div>' ;
2017-10-13 13:28:26 +02:00
}
}
}
2017-09-22 16:31:05 +02:00
}
2017-10-13 13:28:26 +02:00
}
}
2019-11-12 09:46:08 +01:00
$resaction .= ( $resaction ? '<br>' : $resaction );
$resaction .= '<strong>' . $langs -> trans ( " ResultOfMailSending " ) . ':</strong><br>' . " \n " ;
$resaction .= $langs -> trans ( " NbSelected " ) . ': ' . count ( $toselect ) . " \n <br> " ;
$resaction .= $langs -> trans ( " NbIgnored " ) . ': ' . ( $nbignored ? $nbignored : 0 ) . " \n <br> " ;
$resaction .= $langs -> trans ( " NbSent " ) . ': ' . ( $nbsent ? $nbsent : 0 ) . " \n <br> " ;
2017-10-13 13:28:26 +02:00
2021-02-23 22:03:23 +01:00
if ( $nbsent ) {
2019-11-12 09:46:08 +01:00
$action = '' ; // Do not show form post if there was at least one successfull sent
2017-10-13 13:28:26 +02:00
//setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
setEventMessages ( $langs -> trans ( " EMailSentForNElements " , $nbsent . '/' . count ( $toselect )), null , 'mesgs' );
setEventMessages ( $resaction , null , 'mesgs' );
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
//setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
setEventMessages ( $resaction , null , 'warnings' );
}
2019-11-12 09:46:08 +01:00
$action = 'list' ;
$massaction = '' ;
2017-10-13 13:28:26 +02:00
}
2016-07-25 21:50:41 +02:00
}
2017-11-11 17:09:15 +01:00
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == 'cancelorders' ) {
2018-05-03 10:53:24 +02:00
$db -> begin ();
$nbok = 0 ;
2018-05-02 14:37:35 +02:00
$orders = GETPOST ( 'toselect' , 'array' );
2021-02-23 22:03:23 +01:00
foreach ( $orders as $id_order ) {
2018-05-02 14:37:35 +02:00
$cmd = new Commande ( $db );
2021-02-23 22:03:23 +01:00
if ( $cmd -> fetch ( $id_order ) <= 0 ) {
2018-05-02 14:37:35 +02:00
continue ;
2021-02-23 22:03:23 +01:00
}
2018-05-03 10:53:24 +02:00
2021-02-23 22:03:23 +01:00
if ( $cmd -> statut != Commande :: STATUS_VALIDATED ) {
2018-05-03 10:53:24 +02:00
$langs -> load ( 'errors' );
setEventMessages ( $langs -> trans ( " ErrorObjectMustHaveStatusValidToBeCanceled " , $cmd -> ref ), null , 'errors' );
$error ++ ;
break ;
2020-06-03 00:46:09 +02:00
} else {
// TODO We do not provide warehouse so no stock change here for the moment.
$result = $cmd -> cancel ();
}
2018-05-03 10:53:24 +02:00
2021-02-23 22:03:23 +01:00
if ( $result < 0 ) {
2018-05-03 10:53:24 +02:00
setEventMessages ( $cmd -> error , $cmd -> errors , 'errors' );
$error ++ ;
break ;
2021-02-23 22:03:23 +01:00
} else {
$nbok ++ ;
}
2018-05-03 10:53:24 +02:00
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbok ), null , 'mesgs' );
} else {
2018-05-03 10:53:24 +02:00
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbok ), null , 'mesgs' );
2021-02-23 22:03:23 +01:00
}
2018-05-03 10:53:24 +02:00
$db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2018-05-03 10:53:24 +02:00
$db -> rollback ();
2018-05-02 14:37:35 +02:00
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == " builddoc " && $permissiontoread && ! GETPOST ( 'button_search' )) {
if ( empty ( $diroutputmassaction )) {
2017-09-16 15:36:04 +02:00
dol_print_error ( null , 'include of actions_massactions.inc.php is done but var $diroutputmassaction was not defined' );
exit ;
}
2017-10-13 13:28:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/pdf.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php' ;
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db );
$listofobjectid = array ();
$listofobjectthirdparties = array ();
$listofobjectref = array ();
2021-02-23 22:03:23 +01:00
foreach ( $toselect as $toselectid ) {
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db ); // must create new instance because instance is saved into $listofobjectref array for future use
$result = $objecttmp -> fetch ( $toselectid );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2019-11-12 09:46:08 +01:00
$listofobjectid [ $toselectid ] = $toselectid ;
$thirdpartyid = $objecttmp -> fk_soc ? $objecttmp -> fk_soc : $objecttmp -> socid ;
$listofobjectthirdparties [ $thirdpartyid ] = $thirdpartyid ;
$listofobjectref [ $toselectid ] = $objecttmp -> ref ;
2017-10-13 13:28:26 +02:00
}
}
2019-11-12 09:46:08 +01:00
$arrayofinclusion = array ();
2021-02-23 22:03:23 +01:00
foreach ( $listofobjectref as $tmppdf ) {
$arrayofinclusion [] = '^' . preg_quote ( dol_sanitizeFileName ( $tmppdf ), '/' ) . '\.pdf$' ;
}
foreach ( $listofobjectref as $tmppdf ) {
2023-04-24 12:08:45 +02:00
$arrayofinclusion [] = '^' . preg_quote ( dol_sanitizeFileName ( $tmppdf ), '/' ) . '_[a-zA-Z0-9\-\_\'\&\.]+\.pdf$' ; // To include PDF generated from ODX files
2021-02-23 22:03:23 +01:00
}
2019-06-25 13:14:11 +02:00
$listoffiles = dol_dir_list ( $uploaddir , 'all' , 1 , implode ( '|' , $arrayofinclusion ), '\.meta$|\.png' , 'date' , SORT_DESC , 0 , true );
2017-10-13 13:28:26 +02:00
// build list of files with full path
$files = array ();
2021-02-23 22:03:23 +01:00
foreach ( $listofobjectref as $basename ) {
2017-12-11 14:47:07 +01:00
$basename = dol_sanitizeFileName ( $basename );
2021-02-23 22:03:23 +01:00
foreach ( $listoffiles as $filefound ) {
if ( strstr ( $filefound [ " name " ], $basename )) {
2017-10-13 13:28:26 +02:00
$files [] = $uploaddir . '/' . $basename . '/' . $filefound [ " name " ];
break ;
}
}
}
// Define output language (Here it is not used because we do only merging existing PDF)
$outputlangs = $langs ;
2019-11-12 09:46:08 +01:00
$newlang = '' ;
2021-02-23 22:03:23 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
2021-06-15 22:28:39 +02:00
//elseif ($conf->global->MAIN_MULTILANGS && empty($newlang) && is_object($objecttmp->thirdparty)) { // On massaction, we can have several values for $objecttmp->thirdparty
2021-06-15 22:17:53 +02:00
// $newlang = $objecttmp->thirdparty->default_lang;
//}
2019-11-12 09:46:08 +01:00
if ( ! empty ( $newlang )) {
2019-06-25 14:30:48 +02:00
$outputlangs = new Translate ( " " , $conf );
2017-10-13 13:28:26 +02:00
$outputlangs -> setDefaultLang ( $newlang );
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> USE_PDFTK_FOR_PDF_CONCAT )) {
2017-10-13 13:28:26 +02:00
// Create output dir if not exists
2017-08-18 23:16:40 +02:00
dol_mkdir ( $diroutputmassaction );
2017-09-16 03:13:44 +02:00
2017-08-18 23:16:40 +02:00
// Defined name of merged file
2019-11-12 09:46:08 +01:00
$filename = strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( $objectlabel )));
$filename = preg_replace ( '/\s/' , '_' , $filename );
2017-09-16 03:13:44 +02:00
2017-08-18 23:16:40 +02:00
// Save merged file
2021-02-23 22:03:23 +01:00
if ( in_array ( $objecttmp -> element , array ( 'facture' , 'facture_fournisseur' )) && $search_status == Facture :: STATUS_VALIDATED ) {
if ( $option == 'late' ) {
$filename .= '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Unpaid " ))) . '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Late " )));
} else {
$filename .= '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Unpaid " )));
}
}
if ( $year ) {
$filename .= '_' . $year ;
}
if ( $month ) {
$filename .= '_' . $month ;
2017-08-18 23:16:40 +02:00
}
2017-04-27 16:30:57 +02:00
2021-02-23 22:03:23 +01:00
if ( count ( $files ) > 0 ) {
2019-11-12 09:46:08 +01:00
$now = dol_now ();
$file = $diroutputmassaction . '/' . $filename . '_' . dol_print_date ( $now , 'dayhourlog' ) . '.pdf' ;
2017-10-13 13:28:26 +02:00
$input_files = '' ;
2019-11-12 09:46:08 +01:00
foreach ( $files as $f ) {
$input_files .= ' ' . escapeshellarg ( $f );
2017-10-13 13:28:26 +02:00
}
2020-06-03 01:06:27 +02:00
$cmd = 'pdftk ' . $input_files . ' cat output ' . escapeshellarg ( $file );
2017-10-13 13:28:26 +02:00
exec ( $cmd );
2020-06-01 17:12:02 +02:00
// check if pdftk is installed
if ( file_exists ( $file )) {
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2020-06-01 17:12:02 +02:00
@ chmod ( $file , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-23 22:03:23 +01:00
}
2017-10-13 13:28:26 +02:00
2020-06-01 17:12:02 +02:00
$langs -> load ( " exports " );
setEventMessages ( $langs -> trans ( 'FileSuccessfullyBuilt' , $filename . '_' . dol_print_date ( $now , 'dayhourlog' )), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( 'ErrorPDFTkOutputFileNotFound' ), null , 'errors' );
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
setEventMessages ( $langs -> trans ( 'NoPDFAvailableForDocGenAmongChecked' ), null , 'errors' );
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
// Create empty PDF
2019-11-12 09:46:08 +01:00
$formatarray = pdf_getFormat ();
2017-10-13 13:28:26 +02:00
$page_largeur = $formatarray [ 'width' ];
$page_hauteur = $formatarray [ 'height' ];
2019-06-25 13:14:11 +02:00
$format = array ( $page_largeur , $page_hauteur );
2017-10-13 13:28:26 +02:00
2019-11-12 09:46:08 +01:00
$pdf = pdf_getInstance ( $format );
2017-10-13 13:28:26 +02:00
2021-02-23 22:03:23 +01:00
if ( class_exists ( 'TCPDF' )) {
2017-08-30 16:10:39 +02:00
$pdf -> setPrintHeader ( false );
$pdf -> setPrintFooter ( false );
2017-10-13 13:28:26 +02:00
}
$pdf -> SetFont ( pdf_getPDFFont ( $outputlangs ));
2017-04-27 16:30:57 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_DISABLE_PDF_COMPRESSION )) {
$pdf -> SetCompression ( false );
}
2017-04-27 16:30:57 +02:00
2017-10-13 13:28:26 +02:00
// Add all others
2021-02-23 22:03:23 +01:00
foreach ( $files as $file ) {
2017-08-30 16:10:39 +02:00
// Charge un document PDF depuis un fichier.
$pagecount = $pdf -> setSourceFile ( $file );
2021-02-23 22:03:23 +01:00
for ( $i = 1 ; $i <= $pagecount ; $i ++ ) {
2017-10-13 13:28:26 +02:00
$tplidx = $pdf -> importPage ( $i );
$s = $pdf -> getTemplatesize ( $tplidx );
$pdf -> AddPage ( $s [ 'h' ] > $s [ 'w' ] ? 'P' : 'L' );
$pdf -> useTemplate ( $tplidx );
2017-08-30 16:10:39 +02:00
}
2017-10-13 13:28:26 +02:00
}
2017-04-27 16:30:57 +02:00
2017-10-13 13:28:26 +02:00
// Create output dir if not exists
dol_mkdir ( $diroutputmassaction );
2017-04-27 16:30:57 +02:00
2017-10-13 13:28:26 +02:00
// Defined name of merged file
2019-11-12 09:46:08 +01:00
$filename = strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( $objectlabel )));
$filename = preg_replace ( '/\s/' , '_' , $filename );
2017-06-02 23:41:18 +02:00
2017-10-13 13:28:26 +02:00
// Save merged file
2021-02-23 22:03:23 +01:00
if ( in_array ( $objecttmp -> element , array ( 'facture' , 'facture_fournisseur' )) && $search_status == Facture :: STATUS_VALIDATED ) {
if ( $option == 'late' ) {
$filename .= '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Unpaid " ))) . '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Late " )));
} else {
$filename .= '_' . strtolower ( dol_sanitizeFileName ( $langs -> transnoentities ( " Unpaid " )));
}
}
if ( $year ) {
$filename .= '_' . $year ;
2017-10-13 13:28:26 +02:00
}
2021-02-23 22:03:23 +01:00
if ( $month ) {
$filename .= '_' . $month ;
}
if ( $pagecount ) {
2019-11-12 09:46:08 +01:00
$now = dol_now ();
$file = $diroutputmassaction . '/' . $filename . '_' . dol_print_date ( $now , 'dayhourlog' ) . '.pdf' ;
2019-06-25 13:14:11 +02:00
$pdf -> Output ( $file , 'F' );
2021-02-23 22:03:23 +01:00
if ( ! empty ( $conf -> global -> MAIN_UMASK )) {
2017-10-13 13:28:26 +02:00
@ chmod ( $file , octdec ( $conf -> global -> MAIN_UMASK ));
2021-02-23 22:03:23 +01:00
}
2017-04-27 16:30:57 +02:00
2017-10-13 13:28:26 +02:00
$langs -> load ( " exports " );
2019-06-25 14:30:48 +02:00
setEventMessages ( $langs -> trans ( 'FileSuccessfullyBuilt' , $filename . '_' . dol_print_date ( $now , 'dayhourlog' )), null , 'mesgs' );
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
setEventMessages ( $langs -> trans ( 'NoPDFAvailableForDocGenAmongChecked' ), null , 'errors' );
2017-10-13 13:28:26 +02:00
}
}
2016-07-25 21:50:41 +02:00
}
// Remove a file from massaction area
2021-02-23 22:03:23 +01:00
if ( $action == 'remove_file' ) {
2017-10-13 13:28:26 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
$langs -> load ( " other " );
$upload_dir = $diroutputmassaction ;
2019-11-12 09:46:08 +01:00
$file = $upload_dir . '/' . GETPOST ( 'file' );
$ret = dol_delete_file ( $file );
2021-02-23 22:03:23 +01:00
if ( $ret ) {
setEventMessages ( $langs -> trans ( " FileWasRemoved " , GETPOST ( 'file' )), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " ErrorFailToDeleteFile " , GETPOST ( 'file' )), null , 'errors' );
}
2019-11-12 09:46:08 +01:00
$action = '' ;
2016-07-25 21:50:41 +02:00
}
2021-02-10 16:44:20 +01:00
2017-09-17 15:38:50 +02:00
// Validate records
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == 'validate' && $permissiontoadd ) {
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db );
2017-12-12 16:11:40 +01:00
2021-02-23 22:03:23 +01:00
if (( $objecttmp -> element == 'facture' || $objecttmp -> element == 'invoice' ) && ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_BILL )) {
2017-10-12 02:25:17 +02:00
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( 'ErrorMassValidationNotAllowedWhenStockIncreaseOnAction' ), null , 'errors' );
$error ++ ;
}
2021-02-23 22:03:23 +01:00
if ( $objecttmp -> element == 'invoice_supplier' && ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SUPPLIER_BILL )) {
2017-07-14 14:40:09 +02:00
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( 'ErrorMassValidationNotAllowedWhenStockIncreaseOnAction' ), null , 'errors' );
$error ++ ;
}
2022-03-02 10:16:48 +01:00
if ( $objecttmp -> element == 'facture' ) {
if ( ! empty ( $toselect ) && ! empty ( $conf -> global -> INVOICE_CHECK_POSTERIOR_DATE )) {
// order $toselect by date
2022-03-09 09:34:50 +01:00
$sql = " SELECT rowid FROM " . MAIN_DB_PREFIX . " facture " ;
$sql .= " WHERE rowid IN ( " . $db -> sanitize ( implode ( " , " , $toselect )) . " ) " ;
$sql .= " ORDER BY datef " ;
2022-03-02 10:16:48 +01:00
$resql = $db -> query ( $sql );
if ( $resql ) {
$toselectnew = [];
while ( ! empty ( $arr = $db -> fetch_row ( $resql ))) {
$toselectnew [] = $arr [ 0 ];
}
$toselect = ( empty ( $toselectnew )) ? $toselect : $toselectnew ;
} else {
dol_print_error ( $db );
$error ++ ;
}
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
2017-07-14 14:40:09 +02:00
$db -> begin ();
$nbok = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $toselect as $toselectid ) {
2019-11-12 09:46:08 +01:00
$result = $objecttmp -> fetch ( $toselectid );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2017-07-14 14:40:09 +02:00
$result = $objecttmp -> validate ( $user );
2021-02-22 15:54:24 +01:00
if ( $result == 0 ) {
2017-07-14 14:40:09 +02:00
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( " ErrorObjectMustHaveStatusDraftToBeValidated " , $objecttmp -> ref ), null , 'errors' );
$error ++ ;
break ;
2021-02-22 15:54:24 +01:00
} elseif ( $result < 0 ) {
2017-07-14 14:40:09 +02:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
2021-02-22 15:54:24 +01:00
} else {
2021-02-09 14:32:04 +01:00
// validate() rename pdf but do not regenerate
// Define output language
if ( empty ( $conf -> global -> MAIN_DISABLE_PDF_AUTOUPDATE )) {
$outputlangs = $langs ;
$newlang = '' ;
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang )) {
$newlang = $objecttmp -> thirdparty -> default_lang ;
}
if ( ! empty ( $newlang )) {
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
$outputlangs -> load ( 'products' );
}
$model = $objecttmp -> model_pdf ;
$ret = $objecttmp -> fetch ( $objecttmp -> id ); // Reload to get new records
// To be sure vars is defined
2022-04-01 16:52:59 +02:00
$hidedetails = ! empty ( $hidedetails ) ? $hidedetails : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS ) ? 1 : 0 );
$hidedesc = ! empty ( $hidedesc ) ? $hidedesc : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DESC ) ? 1 : 0 );
$hideref = ! empty ( $hideref ) ? $hideref : ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_REF ) ? 1 : 0 );
2021-02-09 14:32:04 +01:00
$moreparams = ! empty ( $moreparams ) ? $moreparams : null ;
$result = $objecttmp -> generateDocument ( $model , $outputlangs , $hidedetails , $hidedesc , $hideref );
if ( $result < 0 ) {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
}
}
$nbok ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-07-14 14:40:09 +02:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbok ), null , 'mesgs' );
} else {
2021-06-04 11:20:38 +02:00
setEventMessages ( $langs -> trans ( " RecordModifiedSuccessfully " ), null , 'mesgs' );
2021-02-23 22:03:23 +01:00
}
2017-07-14 14:40:09 +02:00
$db -> commit ();
2020-05-21 15:05:19 +02:00
} else {
2017-07-14 14:40:09 +02:00
$db -> rollback ();
}
//var_dump($listofobjectthirdparties);exit;
}
}
2019-11-04 15:33:49 +01:00
2020-07-11 12:21:53 +02:00
//var_dump($_POST);var_dump($massaction);exit;
2020-03-19 15:43:06 +01:00
2017-11-11 16:03:22 +01:00
// Delete record from mass action (massaction = 'delete' for direct delete, action/confirm='delete'/'yes' with a confirmation step before)
2021-02-23 22:03:23 +01:00
if ( ! $error && ( $massaction == 'delete' || ( $action == 'delete' && $confirm == 'yes' )) && $permissiontodelete ) {
2017-10-13 13:28:26 +02:00
$db -> begin ();
2019-11-12 09:46:08 +01:00
$objecttmp = new $objectclass ( $db );
2017-10-13 13:28:26 +02:00
$nbok = 0 ;
2021-09-07 16:46:48 +02:00
$TMsg = array ();
2021-02-23 22:03:23 +01:00
foreach ( $toselect as $toselectid ) {
2019-11-12 09:46:08 +01:00
$result = $objecttmp -> fetch ( $toselectid );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2018-05-05 14:22:18 +02:00
// Refuse deletion for some objects/status
2021-02-23 22:03:23 +01:00
if ( $objectclass == 'Facture' && empty ( $conf -> global -> INVOICE_CAN_ALWAYS_BE_REMOVED ) && $objecttmp -> status != Facture :: STATUS_DRAFT ) {
2018-05-05 19:39:58 +02:00
$langs -> load ( " errors " );
$nbignored ++ ;
2021-11-11 11:06:05 +01:00
$TMsg [] = '<div class="error">' . $langs -> trans ( 'ErrorOnlyDraftStatusCanBeDeletedInMassAction' , $objecttmp -> ref ) . '</div><br>' ;
continue ;
}
2021-11-11 14:37:25 +01:00
if ( method_exists ( $objecttmp , 'is_erasable' ) && $objecttmp -> is_erasable () <= 0 ) {
2021-11-11 11:06:05 +01:00
$langs -> load ( " errors " );
$nbignored ++ ;
$TMsg [] = '<div class="error">' . $langs -> trans ( 'ErrorRecordHasChildren' ) . ' ' . $objecttmp -> ref . '</div><br>' ;
2018-05-05 19:39:58 +02:00
continue ;
}
2021-11-26 18:22:31 +01:00
if ( $objectclass == 'Holiday' && ! in_array ( $objecttmp -> statut , array ( Holiday :: STATUS_DRAFT , Holiday :: STATUS_CANCELED , Holiday :: STATUS_REFUSED ))) {
2022-12-06 23:13:11 +01:00
$langs -> load ( " errors " );
2022-12-07 16:09:54 +01:00
$nbignored ++ ;
2022-12-06 23:13:11 +01:00
$TMsg [] = '<div class="error">' . $langs -> trans ( 'ErrorLeaveRequestMustBeDraftCanceledOrRefusedToBeDeleted' , $objecttmp -> ref ) . '</div><br>' ;
2021-11-26 18:22:31 +01:00
continue ;
}
2021-02-23 22:03:23 +01:00
if ( $objectclass == " Task " && $objecttmp -> hasChildren () > 0 ) {
2021-03-30 03:37:54 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " projet_task SET fk_task_parent = 0 WHERE fk_task_parent = " . (( int ) $objecttmp -> id );
2018-10-19 14:16:53 +02:00
$res = $db -> query ( $sql );
2021-02-23 22:03:23 +01:00
if ( ! $res ) {
2018-10-19 14:16:53 +02:00
setEventMessage ( 'ErrorRecordParentingNotModified' , 'errors' );
$error ++ ;
}
}
2021-02-23 22:03:23 +01:00
if ( in_array ( $objecttmp -> element , array ( 'societe' , 'member' ))) {
$result = $objecttmp -> delete ( $objecttmp -> id , $user , 1 );
2022-06-22 15:13:13 +02:00
} elseif ( in_array ( $objecttmp -> element , array ( 'action' ))) {
$result = $objecttmp -> delete ();
2021-02-23 22:03:23 +01:00
} else {
$result = $objecttmp -> delete ( $user );
}
2018-05-05 14:22:18 +02:00
2021-09-09 11:30:29 +02:00
if ( empty ( $result )) { // if delete returns 0, there is at least one object linked
2021-09-07 16:46:48 +02:00
$TMsg = array_merge ( $objecttmp -> errors , $TMsg );
2021-09-09 11:25:46 +02:00
} elseif ( $result < 0 ) { // if delete returns is < 0, there is an error, we break and rollback later
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
2020-10-31 14:32:18 +01:00
$error ++ ;
2021-09-09 11:25:46 +02:00
break ;
2021-02-23 22:03:23 +01:00
} else {
$nbok ++ ;
}
2020-05-21 15:05:19 +02:00
} else {
2017-10-13 13:28:26 +02:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
2021-09-09 11:25:46 +02:00
if ( empty ( $error )) {
// Message for elements well deleted
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsDeleted " , $nbok ), null , 'mesgs' );
2021-11-30 16:11:12 +01:00
} elseif ( $nbok > 0 ) {
setEventMessages ( $langs -> trans ( " RecordDeleted " , $nbok ), null , 'mesgs' );
2021-02-23 22:03:23 +01:00
} else {
2021-11-30 16:11:12 +01:00
setEventMessages ( $langs -> trans ( " NoRecordDeleted " ), null , 'mesgs' );
2021-09-09 11:25:46 +02:00
}
2021-09-07 16:46:48 +02:00
2021-09-09 11:25:46 +02:00
// Message for elements which can't be deleted
if ( ! empty ( $TMsg )) {
sort ( $TMsg );
setEventMessages ( '' , array_unique ( $TMsg ), 'warnings' );
}
$db -> commit ();
} else {
$db -> rollback ();
2017-10-13 13:28:26 +02:00
}
2021-09-09 11:25:46 +02:00
2017-10-13 13:28:26 +02:00
//var_dump($listofobjectthirdparties);exit;
2016-07-25 21:50:41 +02:00
}
2018-10-04 13:30:37 +02:00
// Generate document foreach object according to model linked to object
2019-12-11 19:11:13 +01:00
// @todo : propose model selection
2021-02-23 22:03:23 +01:00
if ( ! $error && $massaction == 'generate_doc' && $permissiontoread ) {
2020-10-31 14:32:18 +01:00
$db -> begin ();
$objecttmp = new $objectclass ( $db );
$nbok = 0 ;
2021-02-23 22:03:23 +01:00
foreach ( $toselect as $toselectid ) {
2020-10-31 14:32:18 +01:00
$result = $objecttmp -> fetch ( $toselectid );
2021-02-23 22:03:23 +01:00
if ( $result > 0 ) {
2020-10-31 14:32:18 +01:00
$outputlangs = $langs ;
$newlang = '' ;
2021-02-23 22:03:23 +01:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && GETPOST ( 'lang_id' , 'aZ09' )) {
$newlang = GETPOST ( 'lang_id' , 'aZ09' );
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && isset ( $objecttmp -> thirdparty -> default_lang )) {
$newlang = $objecttmp -> thirdparty -> default_lang ; // for proposal, order, invoice, ...
}
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && isset ( $objecttmp -> default_lang )) {
$newlang = $objecttmp -> default_lang ; // for thirdparty
}
2022-09-24 19:19:17 +02:00
if ( $conf -> global -> MAIN_MULTILANGS && empty ( $newlang ) && empty ( $objecttmp -> thirdparty )) { //load lang from thirdparty
2022-09-24 19:29:06 +02:00
$objecttmp -> fetch_thirdparty ();
$newlang = $objecttmp -> thirdparty -> default_lang ; // for proposal, order, invoice, ...
}
2021-02-23 22:03:23 +01:00
if ( ! empty ( $newlang )) {
2020-10-31 14:32:18 +01:00
$outputlangs = new Translate ( " " , $conf );
$outputlangs -> setDefaultLang ( $newlang );
}
// To be sure vars is defined
2021-02-23 22:03:23 +01:00
if ( empty ( $hidedetails )) {
2022-04-01 16:52:59 +02:00
$hidedetails = ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS ) ? 1 : 0 );
2021-02-23 22:03:23 +01:00
}
if ( empty ( $hidedesc )) {
2022-04-01 16:52:59 +02:00
$hidedesc = ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_DESC ) ? 1 : 0 );
2021-02-23 22:03:23 +01:00
}
if ( empty ( $hideref )) {
2022-04-01 16:52:59 +02:00
$hideref = ( ! empty ( $conf -> global -> MAIN_GENERATE_DOCUMENTS_HIDE_REF ) ? 1 : 0 );
2021-02-23 22:03:23 +01:00
}
if ( empty ( $moreparams )) {
$moreparams = null ;
}
2020-10-31 14:32:18 +01:00
2021-02-08 18:30:09 +01:00
$result = $objecttmp -> generateDocument ( $objecttmp -> model_pdf , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
2020-10-31 14:32:18 +01:00
2021-02-23 22:03:23 +01:00
if ( $result <= 0 ) {
2020-10-31 14:32:18 +01:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
2021-02-23 22:03:23 +01:00
} else {
$nbok ++ ;
}
2020-10-31 14:32:18 +01:00
} else {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsGenerated " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " RecordGenerated " , $nbok ), null , 'mesgs' );
}
2020-10-31 14:32:18 +01:00
$db -> commit ();
} else {
$db -> rollback ();
}
2018-10-04 13:30:37 +02:00
}
2020-12-23 23:32:50 +01:00
if ( ! $error && ( $action == 'affecttag' && $confirm == 'yes' ) && $permissiontoadd ) {
$db -> begin ();
2020-12-24 15:06:55 +01:00
$affecttag_type = GETPOST ( 'affecttag_type' , 'alpha' );
2020-12-23 23:32:50 +01:00
if ( ! empty ( $affecttag_type )) {
2020-12-24 15:06:55 +01:00
$affecttag_type_array = explode ( ',' , $affecttag_type );
2020-12-23 23:32:50 +01:00
} else {
2020-12-24 15:06:55 +01:00
setEventMessage ( 'CategTypeNotFound' , 'errors' );
2020-12-23 23:32:50 +01:00
}
if ( ! empty ( $affecttag_type_array )) {
//check if tag type submited exists into Tag Map categorie class
require_once DOL_DOCUMENT_ROOT . '/categories/class/categorie.class.php' ;
$categ = new Categorie ( $db );
$to_affecttag_type_array = array ();
$categ_type_array = $categ -> getMapList ();
2020-12-24 15:06:55 +01:00
foreach ( $categ_type_array as $categdef ) {
2020-12-23 23:32:50 +01:00
if ( in_array ( $categdef [ 'code' ], $affecttag_type_array )) {
$to_affecttag_type_array [] = $categdef [ 'code' ];
}
}
//For each valid categ type set common categ
$nbok = 0 ;
if ( ! empty ( $to_affecttag_type_array )) {
foreach ( $to_affecttag_type_array as $categ_type ) {
$contcats = GETPOST ( 'contcats_' . $categ_type , 'array' );
2020-12-23 23:44:38 +01:00
//var_dump($toselect);exit;
2020-12-23 23:32:50 +01:00
foreach ( $toselect as $toselectid ) {
$result = $object -> fetch ( $toselectid );
2020-12-23 23:44:38 +01:00
//var_dump($contcats);exit;
2020-12-23 23:32:50 +01:00
if ( $result > 0 ) {
$result = $object -> setCategoriesCommon ( $contcats , $categ_type , false );
if ( $result > 0 ) {
$nbok ++ ;
} else {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
}
} else {
setEventMessages ( $object -> error , $object -> errors , 'errors' );
$error ++ ;
break ;
}
}
}
}
}
2021-02-23 22:03:23 +01:00
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbok ), null );
} else {
setEventMessages ( $langs -> trans ( " RecordsModified " , $nbok ), null );
}
2020-12-23 23:32:50 +01:00
$db -> commit ();
$toselect = array ();
} else {
$db -> rollback ();
}
}
2021-07-13 10:05:38 +02:00
if ( ! $error && ( $massaction == 'enable' || ( $action == 'enable' && $confirm == 'yes' )) && $permissiontoadd ) {
$db -> begin ();
$objecttmp = new $objectclass ( $db );
$nbok = 0 ;
foreach ( $toselect as $toselectid ) {
$result = $objecttmp -> fetch ( $toselectid );
2021-07-13 10:08:01 +02:00
if ( $result > 0 ) {
if ( in_array ( $objecttmp -> element , array ( 'societe' ))) {
2021-07-13 10:05:38 +02:00
$result = $objecttmp -> setStatut ( 1 );
}
if ( $result <= 0 ) {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
} else {
$nbok ++ ;
}
2021-07-13 10:08:01 +02:00
} else {
2021-07-13 10:05:38 +02:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsEnabled " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " RecordEnabled " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
if ( ! $error && ( $massaction == 'disable' || ( $action == 'disable' && $confirm == 'yes' )) && $permissiontoadd ) {
$db -> begin ();
$objecttmp = new $objectclass ( $db );
$nbok = 0 ;
foreach ( $toselect as $toselectid ) {
$result = $objecttmp -> fetch ( $toselectid );
2021-07-13 10:08:01 +02:00
if ( $result > 0 ) {
if ( in_array ( $objecttmp -> element , array ( 'societe' ))) {
2021-07-13 10:05:38 +02:00
$result = $objecttmp -> setStatut ( 0 );
}
if ( $result <= 0 ) {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
} else {
$nbok ++ ;
}
2021-07-13 10:08:01 +02:00
} else {
2021-07-13 10:05:38 +02:00
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsDisabled " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " RecordDisabled " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
2022-04-29 15:10:15 +02:00
if ( ! $error && $action == 'confirm_edit_value_extrafields' && $confirm == 'yes' && $permissiontoadd ) {
2022-04-29 11:45:50 +02:00
$db -> begin ();
2022-04-29 15:38:12 +02:00
$objecttmp = new $objectclass ( $db );
$e = new ExtraFields ( $db ); // fetch optionals attributes and labels
$e -> fetch_name_optionals_label ( $objecttmp -> table_element );
2022-04-29 11:45:50 +02:00
2022-04-29 15:38:12 +02:00
$nbok = 0 ;
2022-05-04 17:24:37 +02:00
$extrafieldKeyToUpdate = GETPOST ( 'extrafield-key-to-update' );
2022-04-29 15:10:15 +02:00
2022-04-29 11:45:50 +02:00
foreach ( $toselect as $toselectid ) {
2022-04-29 15:10:15 +02:00
/** @var CommonObject $objecttmp */
2022-04-29 15:38:12 +02:00
$objecttmp = new $objectclass ( $db ); // to avoid ghost data
2022-04-29 11:45:50 +02:00
$result = $objecttmp -> fetch ( $toselectid );
if ( $result > 0 ) {
2022-04-29 15:10:15 +02:00
// Fill array 'array_options' with data from add form
$ret = $e -> setOptionalsFromPost ( null , $objecttmp , $extrafieldKeyToUpdate );
if ( $ret > 0 ) {
2022-05-04 17:24:37 +02:00
$objecttmp -> insertExtraFields ();
2022-04-29 15:10:15 +02:00
} else {
$error ++ ;
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
2022-04-29 11:45:50 +02:00
}
} else {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsDisabled " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " save " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
2022-04-12 13:57:00 +02:00
if ( ! $error && ( $massaction == 'affectcommercial' || ( $action == 'affectcommercial' && $confirm == 'yes' )) && $permissiontoadd ) {
$db -> begin ();
$objecttmp = new $objectclass ( $db );
$nbok = 0 ;
foreach ( $toselect as $toselectid ) {
$result = $objecttmp -> fetch ( $toselectid );
if ( $result > 0 ) {
if ( in_array ( $objecttmp -> element , array ( 'societe' ))) {
$result = $objecttmp -> setSalesRep ( GETPOST ( " commercial " , " alpha " ));
}
if ( $result <= 0 ) {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
} else {
$nbok ++ ;
}
} else {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " CommercialsAffected " , $nbok ), null , 'mesgs' );
} else {
setEventMessages ( $langs -> trans ( " CommercialAffected " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
2021-11-07 16:51:18 +01:00
// Approve for leave only
2021-08-02 15:45:55 +02:00
if ( ! $error && ( $massaction == 'approveleave' || ( $action == 'approveleave' && $confirm == 'yes' )) && $permissiontoapprove ) {
$db -> begin ();
$objecttmp = new $objectclass ( $db );
$nbok = 0 ;
foreach ( $toselect as $toselectid ) {
$result = $objecttmp -> fetch ( $toselectid );
2022-03-07 12:01:34 +01:00
if ( $result > 0 ) {
if ( $objecttmp -> statut != Holiday :: STATUS_VALIDATED ) {
setEventMessages ( $langs -> trans ( 'StatusOfRefMustBe' , $objecttmp -> ref , $langs -> transnoentitiesnoconv ( 'Validated' )), null , 'warnings' );
continue ;
}
if ( $user -> id == $objecttmp -> fk_validator ) {
2021-08-02 15:45:55 +02:00
$objecttmp -> oldcopy = dol_clone ( $objecttmp );
$objecttmp -> date_valid = dol_now ();
$objecttmp -> fk_user_valid = $user -> id ;
$objecttmp -> statut = Holiday :: STATUS_APPROVED ;
$verif = $objecttmp -> approve ( $user );
2022-03-07 12:01:34 +01:00
2021-08-02 15:45:55 +02:00
if ( $verif <= 0 ) {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
}
// If no SQL error, we redirect to the request form
if ( ! $error ) {
2022-12-06 19:56:49 +01:00
// Calculcate number of days consumed
2021-08-02 15:45:55 +02:00
$nbopenedday = num_open_day ( $objecttmp -> date_debut_gmt , $objecttmp -> date_fin_gmt , 0 , 1 , $objecttmp -> halfday );
$soldeActuel = $objecttmp -> getCpforUser ( $objecttmp -> fk_user , $objecttmp -> fk_type );
$newSolde = ( $soldeActuel - $nbopenedday );
// The modification is added to the LOG
$result = $objecttmp -> addLogCP ( $user -> id , $objecttmp -> fk_user , $langs -> transnoentitiesnoconv ( " Holidays " ), $newSolde , $objecttmp -> fk_type );
if ( $result < 0 ) {
$error ++ ;
setEventMessages ( null , $objecttmp -> errors , 'errors' );
}
// Update balance
$result = $objecttmp -> updateSoldeCP ( $objecttmp -> fk_user , $newSolde , $objecttmp -> fk_type );
if ( $result < 0 ) {
$error ++ ;
setEventMessages ( null , $objecttmp -> errors , 'errors' );
}
}
if ( ! $error ) {
// To
$destinataire = new User ( $db );
$destinataire -> fetch ( $objecttmp -> fk_user );
$emailTo = $destinataire -> email ;
if ( ! $emailTo ) {
dol_syslog ( " User that request leave has no email, so we redirect directly to finished page without sending email " );
} else {
// From
$expediteur = new User ( $db );
$expediteur -> fetch ( $objecttmp -> fk_validator );
//$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email.
$emailFrom = $conf -> global -> MAIN_MAIL_EMAIL_FROM ;
// Subject
$societeName = $conf -> global -> MAIN_INFO_SOCIETE_NOM ;
if ( ! empty ( $conf -> global -> MAIN_APPLICATION_TITLE )) {
$societeName = $conf -> global -> MAIN_APPLICATION_TITLE ;
}
$subject = $societeName . " - " . $langs -> transnoentitiesnoconv ( " HolidaysValidated " );
// Content
$message = $langs -> transnoentitiesnoconv ( " Hello " ) . " " . $destinataire -> firstname . " , \n " ;
$message .= " \n " ;
$message .= $langs -> transnoentities ( " HolidaysValidatedBody " , dol_print_date ( $objecttmp -> date_debut , 'day' ), dol_print_date ( $objecttmp -> date_fin , 'day' )) . " \n " ;
$message .= " - " . $langs -> transnoentitiesnoconv ( " ValidatedBy " ) . " : " . dolGetFirstLastname ( $expediteur -> firstname , $expediteur -> lastname ) . " \n " ;
$message .= " - " . $langs -> transnoentitiesnoconv ( " Link " ) . " : " . $dolibarr_main_url_root . " /holiday/card.php?id= " . $objecttmp -> id . " \n \n " ;
$message .= " \n " ;
$trackid = 'leav' . $objecttmp -> id ;
2022-05-18 14:47:12 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php' ;
2021-08-02 15:45:55 +02:00
$mail = new CMailFile ( $subject , $emailTo , $emailFrom , $message , array (), array (), array (), '' , '' , 0 , 0 , '' , '' , $trackid );
// Sending email
$result = $mail -> sendfile ();
if ( ! $result ) {
setEventMessages ( $mail -> error , $mail -> errors , 'warnings' ); // Show error, but do no make rollback, so $error is not set to 1
$action = '' ;
}
}
}
2022-03-07 12:01:34 +01:00
} else {
$langs -> load ( " errors " );
setEventMessages ( $langs -> trans ( 'ErrorNotApproverForHoliday' , $objecttmp -> ref ), null , 'errors' );
2021-08-02 15:45:55 +02:00
}
} else {
setEventMessages ( $objecttmp -> error , $objecttmp -> errors , 'errors' );
$error ++ ;
break ;
}
}
if ( ! $error ) {
if ( $nbok > 1 ) {
setEventMessages ( $langs -> trans ( " RecordsApproved " , $nbok ), null , 'mesgs' );
2022-03-07 12:01:34 +01:00
} elseif ( $nbok == 1 ) {
2021-08-02 15:45:55 +02:00
setEventMessages ( $langs -> trans ( " RecordAproved " ), null , 'mesgs' );
}
$db -> commit ();
} else {
$db -> rollback ();
}
}
2019-11-12 09:46:08 +01:00
$parameters [ 'toselect' ] = $toselect ;
$parameters [ 'uploaddir' ] = $uploaddir ;
$parameters [ 'massaction' ] = $massaction ;
2021-07-26 15:22:50 +02:00
$parameters [ 'diroutputmassaction' ] = isset ( $diroutputmassaction ) ? $diroutputmassaction : null ;
2017-06-02 23:41:18 +02:00
2019-11-12 09:46:08 +01:00
$reshook = $hookmanager -> executeHooks ( 'doMassActions' , $parameters , $object , $action ); // Note that $action and $object may have been modified by some hooks
2021-02-23 22:03:23 +01:00
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
}