| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960 |
- <?php
- /* Copyright (C) 2015-2017 Laurent Destailleur <eldy@users.sourceforge.net>
- * Copyright (C) 2018-2021 Nicolas ZABOURI <info@inovea-conseil.com>
- * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
- * Copyright (C) 2019 Ferran Marcet <fmarcet@2byte.es>
- * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
- *
- * 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 <https://www.gnu.org/licenses/>.
- * or see https://www.gnu.org/
- */
- /**
- * \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
- // $objectclass and $objectlabel must be defined
- // $parameters, $object, $action must be defined for the hook.
- // $permissiontoread, $permissiontoadd, $permissiontodelete, $permissiontoclose may be defined
- // $uploaddir may be defined (example to $conf->project->dir_output."/";)
- // $toselect may be defined
- // $diroutputmassaction may be defined
- $statusesrowidAndlabel = $stasusesObj->getStatusidsAndRowids();
- // Protection
- if (empty($objectclass) || empty($uploaddir)) {
- dol_print_error(null, 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
- exit;
- }
- if (empty($massaction)) {
- $massaction = '';
- }
- $error = 0;
- // For backward compatibility
- if (!empty($permtoread) && empty($permissiontoread)) {
- $permissiontoread = $permtoread;
- }
- if (!empty($permtocreate) && empty($permissiontoadd)) {
- $permissiontoadd = $permtocreate;
- }
- if (!empty($permtodelete) && empty($permissiontodelete)) {
- $permissiontodelete = $permtodelete;
- }
- // Mass actions. Controls on number of lines checked.
- $maxformassaction = (empty($conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS) ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
- if ($massaction && is_array($toselect) && count($toselect) < 1) {
- $error++;
- setEventMessages($langs->trans("NoRecordSelected"), null, "warnings");
- }
- if (!$error && isset($toselect) && is_array($toselect) && count($toselect) > $maxformassaction) {
- setEventMessages($langs->trans('TooManyRecordForMassAction', $maxformassaction), null, 'errors');
- $error++;
- }
- 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
- $massaction = 'presend';
- }
- if (!$error && $massaction == 'confirm_presend') {
- $resaction = '';
- $nbsent = 0;
- $nbignored = 0;
- $langs->load("mails");
- include_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
- include_once DOL_DOCUMENT_ROOT . '/core/lib/signature.lib.php';
- $listofobjectid = array();
- $listofobjectthirdparties = array();
- $listofobjectcontacts = array();
- $listofobjectref = array();
- $contactidtosend = array();
- $attachedfilesThirdpartyObj = array();
- $oneemailperrecipient = (GETPOST('oneemailperrecipient', 'int') ? 1 : 0);
- if (!$error) {
- $thirdparty = new Societe($db);
- $objecttmp = new $objectclass($db);
- if ($objecttmp->element == 'expensereport') {
- $thirdparty = new User($db);
- }
- if ($objecttmp->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
- $thirdparty = new Adherent($db);
- }
- if ($objecttmp->element == 'holiday') {
- $thirdparty = new User($db);
- }
- foreach ($toselect as $toselectid) {
- $objecttmp = new $objectclass($db); // we must create new instance because instance is saved into $listofobjectref array for future use
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- $listofobjectid[$toselectid] = $toselectid;
- $thirdpartyid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid);
- if ($objecttmp->element == 'societe') {
- $thirdpartyid = $objecttmp->id;
- }
- if ($objecttmp->element == 'expensereport') {
- $thirdpartyid = $objecttmp->fk_user_author;
- }
- if ($objecttmp->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
- $thirdpartyid = $objecttmp->fk_member;
- }
- if ($objecttmp->element == 'holiday') {
- $thirdpartyid = $objecttmp->fk_user;
- }
- if (empty($thirdpartyid)) {
- $thirdpartyid = 0;
- }
- 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'];
- }
- }
- } 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'];
- }
- }
- }
- $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
- $listofobjectref[$thirdpartyid][$toselectid] = $objecttmp;
- }
- }
- }
- // Check mandatory parameters
- if (GETPOST('fromtype', 'alpha') === 'user' && empty($user->email)) {
- $error++;
- setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings');
- $massaction = 'presend';
- }
- $receiver = GETPOST('receiver', 'alphawithlgt');
- if (!is_array($receiver)) {
- if (empty($receiver) || $receiver == '-1') {
- $receiver = array();
- } else {
- $receiver = array($receiver);
- }
- }
- 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';
- }
- if (!GETPOST('subject', 'restricthtml')) {
- $error++;
- setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTopic")), null, 'warnings');
- $massaction = 'presend';
- }
- // Loop on each recipient/thirdparty
- if (!$error) {
- foreach ($listofobjectthirdparties as $thirdpartyid) {
- $result = $thirdparty->fetch($thirdpartyid);
- if ($result < 0) {
- dol_print_error($db);
- exit;
- }
- $sendto = '';
- $sendtocc = '';
- $sendtobcc = '';
- $sendtoid = array();
- // Define $sendto
- $tmparray = array();
- if (trim($_POST['sendto'])) {
- // Recipients are provided into free text
- $tmparray[] = trim(GETPOST('sendto', 'alphawithlgt'));
- }
- if (count($receiver) > 0) {
- foreach ($receiver as $key => $val) {
- // Recipient was provided from combo list
- if ($val == 'thirdparty') { // Id of third party or user
- $tmparray[] = $thirdparty->name . ' <' . $thirdparty->email . '>';
- } elseif ($val && method_exists($thirdparty, 'contact_get_property')) { // Id of contact
- $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
- $sendtoid[] = $val;
- }
- }
- }
- $sendto = implode(',', $tmparray);
- // Define $sendtocc
- $receivercc = GETPOST('receivercc', 'alphawithlgt');
- if (!is_array($receivercc)) {
- if ($receivercc == '-1') {
- $receivercc = array();
- } else {
- $receivercc = array($receivercc);
- }
- }
- $tmparray = array();
- if (trim($_POST['sendtocc'])) {
- $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
- }
- if (count($receivercc) > 0) {
- foreach ($receivercc as $key => $val) {
- // Recipient was provided from combo list
- if ($val == 'thirdparty') { // Id of third party
- $tmparray[] = $thirdparty->name . ' <' . $thirdparty->email . '>';
- } elseif ($val) { // Id du contact
- $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
- //$sendtoid[] = $val; TODO Add also id of contact in CC ?
- }
- }
- }
- $sendtocc = implode(',', $tmparray);
- //var_dump($listofobjectref);exit;
- $listofqualifiedobj = array();
- $listofqualifiedref = array();
- $thirdpartywithoutemail = array();
- foreach ($listofobjectref[$thirdpartyid] as $objectid => $objectobj) {
- //var_dump($thirdpartyid.' - '.$objectid.' - '.$objectobj->statut);
- if ($objectclass == 'Propal' && $objectobj->statut == Propal::STATUS_DRAFT) {
- $langs->load("errors");
- $nbignored++;
- $resaction .= '<div class="error">' . $langs->trans('ErrorOnlyProposalNotDraftCanBeSentInMassAction', $objectobj->ref) . '</div><br>';
- continue; // Payment done or started or canceled
- }
- if ($objectclass == 'Commande' && $objectobj->statut == Commande::STATUS_DRAFT) {
- $langs->load("errors");
- $nbignored++;
- $resaction .= '<div class="error">' . $langs->trans('ErrorOnlyOrderNotDraftCanBeSentInMassAction', $objectobj->ref) . '</div><br>';
- continue;
- }
- if ($objectclass == 'Facture' && $objectobj->statut == Facture::STATUS_DRAFT) {
- $langs->load("errors");
- $nbignored++;
- $resaction .= '<div class="error">' . $langs->trans('ErrorOnlyInvoiceValidatedCanBeSentInMassAction', $objectobj->ref) . '</div><br>';
- continue; // Payment done or started or canceled
- }
- // Test recipient
- if (empty($sendto)) { // For the case, no recipient were set (multi thirdparties send)
- if ($objectobj->element == 'societe') {
- $sendto = $objectobj->email;
- } elseif ($objectobj->element == 'expensereport') {
- $fuser = new User($db);
- $fuser->fetch($objectobj->fk_user_author);
- $sendto = $fuser->email;
- } elseif ($objectobj->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
- $fadherent = new Adherent($db);
- $fadherent->fetch($objectobj->fk_member);
- $sendto = $fadherent->email;
- } elseif ($objectobj->element == 'holiday') {
- $fuser = new User($db);
- $fuser->fetch($objectobj->fk_user);
- $sendto = $fuser->email;
- } elseif ($objectobj->element == 'facture' && !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);
- }
- } 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);
- }
- } else {
- $objectobj->fetch_thirdparty();
- $sendto = $objectobj->thirdparty->email;
- }
- }
- if (empty($sendto)) {
- if ($objectobj->element == 'societe') {
- $objectobj->thirdparty = $objectobj; // Hack so following code is comaptible when objectobj is a thirdparty
- }
- //print "No recipient for thirdparty ".$objectobj->thirdparty->name;
- $nbignored++;
- if (empty($thirdpartywithoutemail[$objectobj->thirdparty->id])) {
- $resaction .= '<div class="error">' . $langs->trans('NoRecipientEmail', $objectobj->thirdparty->name) . '</div><br>';
- }
- dol_syslog('No recipient for thirdparty: ' . $objectobj->thirdparty->name, LOG_WARNING);
- $thirdpartywithoutemail[$objectobj->thirdparty->id] = 1;
- continue;
- }
- if (GETPOST('addmaindocfile')) {
- // TODO Use future field $objectobj->fullpathdoc to know where is stored default file
- // TODO If not defined, use $objectobj->model_pdf (or defaut invoice config) to know what is template to use to regenerate doc.
- $filename = dol_sanitizeFileName($objectobj->ref) . '.pdf';
- $subdir = '';
- // TODO Set subdir to be compatible with multi levels dir trees
- // $subdir = get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element)
- $filedir = $uploaddir . '/' . $subdir . dol_sanitizeFileName($objectobj->ref);
- $filepath = $filedir . '/' . $filename;
- // For supplier invoices, we use the file provided by supplier, not the one we generate
- if ($objectobj->element == 'invoice_supplier') {
- $fileparams = dol_most_recent_file($uploaddir . '/' . get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element) . $objectobj->ref, preg_quote($objectobj->ref, '/') . '([^\-])+');
- $filepath = $fileparams['fullname'];
- }
- // 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,
- );
- if (!empty($conf->global->MAIL_MASS_ACTION_ADD_LAST_IF_MAIN_DOC_NOT_FOUND) && !empty($objectobj->last_main_doc)) {
- $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;
- }
- }
- if ($filepath_found) {
- // Create form object
- $attachedfilesThirdpartyObj[$thirdpartyid][$objectid] = array(
- 'paths' => array($filepath_found),
- 'names' => array($filename_found),
- 'mimes' => array(dol_mimetype($filepath_found))
- );
- } else {
- $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;
- }
- }
- // Object of thirdparty qualified, we add it
- $listofqualifiedobj[$objectid] = $objectobj;
- $listofqualifiedref[$objectid] = $objectobj->ref;
- //var_dump($listofqualifiedref);
- }
- // Send email if there is at least one qualified object for current thirdparty
- if (count($listofqualifiedobj) > 0) {
- $langs->load("commercial");
- $reg = array();
- $fromtype = GETPOST('fromtype');
- if ($fromtype === 'user') {
- $from = dol_string_nospecial($user->getFullName($langs), ' ', array(",")) . ' <' . $user->email . '>';
- } elseif ($fromtype === 'company') {
- $from = $conf->global->MAIN_INFO_SOCIETE_NOM . ' <' . $conf->global->MAIN_INFO_SOCIETE_MAIL . '>';
- } elseif (preg_match('/user_aliases_(\d+)/', $fromtype, $reg)) {
- $tmp = explode(',', $user->email_aliases);
- $from = trim($tmp[($reg[1] - 1)]);
- } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
- $tmp = explode(',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
- $from = trim($tmp[($reg[1] - 1)]);
- } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
- $sql = "SELECT rowid, label, email FROM " . MAIN_DB_PREFIX . "c_email_senderprofile WHERE rowid = " . (int) $reg[1];
- $resql = $db->query($sql);
- $obj = $db->fetch_object($resql);
- if ($obj) {
- $from = dol_string_nospecial($obj->label, ' ', array(",")) . ' <' . $obj->email . '>';
- }
- } else {
- $from = GETPOST('fromname') . ' <' . GETPOST('frommail') . '>';
- }
- $replyto = $from;
- $subject = GETPOST('subject', 'restricthtml');
- $message = GETPOST('message', 'restricthtml');
- $sendtobcc = GETPOST('sendtoccc');
- 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));
- }
- // $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)
- // $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)
- $looparray = array();
- if (!$oneemailperrecipient) {
- $looparray = $listofqualifiedobj;
- foreach ($looparray as $key => $objecttmp) {
- $looparray[$key]->thirdparty = $thirdparty; // Force thirdparty on object
- }
- } else {
- $objectforloop = new $objectclass($db);
- $objectforloop->thirdparty = $thirdparty; // Force thirdparty on object (even if object was not loaded)
- $looparray[0] = $objectforloop;
- }
- //var_dump($looparray);exit;
- dol_syslog("We have set an array of " . count($looparray) . " emails to send. oneemailperrecipient=" . $oneemailperrecipient);
- //var_dump($oneemailperrecipient); var_dump($listofqualifiedobj); var_dump($listofqualifiedref);
- 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
- // Make substitution in email content
- if (isModEnabled('project') && method_exists($objecttmp, 'fetch_projet') && is_null($objecttmp->project)) {
- $objecttmp->fetch_projet();
- }
- $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $objecttmp);
- $substitutionarray['__ID__'] = ($oneemailperrecipient ? join(', ', array_keys($listofqualifiedobj)) : $objecttmp->id);
- $substitutionarray['__REF__'] = ($oneemailperrecipient ? join(', ', $listofqualifiedref) : $objecttmp->ref);
- $substitutionarray['__EMAIL__'] = $thirdparty->email;
- $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"/>';
- $parameters = array('mode' => 'formemail');
- if (!empty($listofobjectthirdparties)) {
- $parameters['listofobjectthirdparties'] = $listofobjectthirdparties;
- }
- if (!empty($listofobjectref)) {
- $parameters['listofobjectref'] = $listofobjectref;
- }
- complete_substitutions_array($substitutionarray, $langs, $objecttmp, $parameters);
- $subjectreplaced = make_substitutions($subject, $substitutionarray);
- $messagereplaced = make_substitutions($message, $substitutionarray);
- $attachedfiles = array('paths' => array(), 'names' => array(), 'mimes' => array());
- if ($oneemailperrecipient) {
- // if "one email per recipient" is check we must collate $attachedfiles by thirdparty
- if (is_array($attachedfilesThirdpartyObj[$thirdparty->id]) && count($attachedfilesThirdpartyObj[$thirdparty->id])) {
- foreach ($attachedfilesThirdpartyObj[$thirdparty->id] as $keyObjId => $objAttachedFiles) {
- // Create form object
- $attachedfiles = array(
- 'paths' => array_merge($attachedfiles['paths'], $objAttachedFiles['paths']),
- 'names' => array_merge($attachedfiles['names'], $objAttachedFiles['names']),
- 'mimes' => array_merge($attachedfiles['mimes'], $objAttachedFiles['mimes'])
- );
- }
- }
- } elseif (!empty($attachedfilesThirdpartyObj[$thirdparty->id][$objectid])) {
- // Create form object
- // if "one email per recipient" isn't check we must separate $attachedfiles by object
- $attachedfiles = $attachedfilesThirdpartyObj[$thirdparty->id][$objectid];
- }
- $filepath = $attachedfiles['paths'];
- $filename = $attachedfiles['names'];
- $mimetype = $attachedfiles['mimes'];
- // Define the trackid when emails sent from the mass action
- if ($oneemailperrecipient) {
- $trackid = 'thi' . $thirdparty->id;
- if ($objecttmp->element == 'expensereport') {
- $trackid = 'use' . $thirdparty->id;
- }
- if ($objecttmp->element == 'holiday') {
- $trackid = 'use' . $thirdparty->id;
- }
- } else {
- $trackid = strtolower(get_class($objecttmp));
- 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';
- }
- $trackid .= $objecttmp->id;
- }
- //var_dump($filepath);
- //var_dump($trackid);exit;
- //var_dump($subjectreplaced);
- if (empty($sendcontext)) {
- $sendcontext = 'standard';
- }
- // Send mail (substitutionarray must be done just before this)
- 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);
- if ($mailfile->error) {
- $resaction .= '<div class="error">' . $mailfile->error . '</div>';
- } else {
- $result = $mailfile->sendfile();
- if ($result) {
- $resaction .= $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2)) . '<br>'; // Must not contain "
- $error = 0;
- // Insert logs into agenda
- 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
- }
- dol_syslog("Try to insert email event into agenda for objid=" . $objid2 . " => objectobj=" . get_class($objectobj2));
- /*if ($objectclass == 'Propale') $actiontypecode='AC_PROP';
- 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';*/
- $actionmsg = $langs->transnoentities('MailSentBy') . ' ' . $from . ' ' . $langs->transnoentities('To') . ' ' . $sendto;
- if ($message) {
- if ($sendtocc) {
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc') . ": " . $sendtocc);
- }
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic') . ": " . $subjectreplaced);
- $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody') . ":");
- $actionmsg = dol_concatdesc($actionmsg, $messagereplaced);
- }
- $actionmsg2 = '';
- // 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;
- if (!empty($conf->global->MAIN_MAIL_REPLACE_EVENT_TITLE_BY_EMAIL_SUBJECT)) {
- $objectobj2->actionmsg2 = $subjectreplaced; // Short text
- }
- $triggername = strtoupper(get_class($objectobj2)) . '_SENTBYMAIL';
- 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 ($triggername == 'PROJET_SENTBYMAIL') {
- $triggername = 'PROJECT_SENTBYMAIL';
- }
- if (!empty($triggername)) {
- // Call trigger
- $result = $objectobj2->call_trigger($triggername, $user);
- if ($result < 0) {
- $error++;
- }
- // End call triggers
- if ($error) {
- setEventMessages($db->lasterror(), $errors, 'errors');
- dol_syslog("Error in trigger " . $triggername . ' ' . $db->lasterror(), LOG_ERR);
- }
- }
- $nbsent++; // Nb of object sent
- }
- } else {
- $langs->load("other");
- if ($mailfile->error) {
- $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto);
- $resaction .= '<br><div class="error">' . $mailfile->error . '</div>';
- } elseif (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
- $resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
- } else {
- $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto) . '<br><div class="error">(unhandled error)</div>';
- }
- }
- }
- }
- }
- }
- $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>";
- if ($nbsent) {
- $action = ''; // Do not show form post if there was at least one successfull sent
- //setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
- setEventMessages($langs->trans("EMailSentForNElements", $nbsent . '/' . count($toselect)), null, 'mesgs');
- setEventMessages($resaction, null, 'mesgs');
- } else {
- //setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
- setEventMessages($resaction, null, 'warnings');
- }
- $action = 'list';
- $massaction = '';
- }
- }
- if (!$error && $massaction == 'cancelorders') {
- $db->begin();
- $nbok = 0;
- $orders = GETPOST('toselect', 'array');
- foreach ($orders as $id_order) {
- $cmd = new Commande($db);
- if ($cmd->fetch($id_order) <= 0) {
- continue;
- }
- if ($cmd->statut != Commande::STATUS_VALIDATED) {
- $langs->load('errors');
- setEventMessages($langs->trans("ErrorObjectMustHaveStatusValidToBeCanceled", $cmd->ref), null, 'errors');
- $error++;
- break;
- } else {
- // TODO We do not provide warehouse so no stock change here for the moment.
- $result = $cmd->cancel();
- }
- if ($result < 0) {
- setEventMessages($cmd->error, $cmd->errors, 'errors');
- $error++;
- break;
- } else {
- $nbok++;
- }
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- }
- if (!$error && $massaction == "builddoc" && $permissiontoread && !GETPOST('button_search')) {
- if (empty($diroutputmassaction)) {
- dol_print_error(null, 'include of actions_massactions.inc.php is done but var $diroutputmassaction was not defined');
- exit;
- }
- 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';
- $objecttmp = new $objectclass($db);
- $listofobjectid = array();
- $listofobjectthirdparties = array();
- $listofobjectref = array();
- foreach ($toselect as $toselectid) {
- $objecttmp = new $objectclass($db); // must create new instance because instance is saved into $listofobjectref array for future use
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- $listofobjectid[$toselectid] = $toselectid;
- $thirdpartyid = $objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid;
- $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
- $listofobjectref[$toselectid] = $objecttmp->ref;
- }
- }
- $arrayofinclusion = array();
- foreach ($listofobjectref as $tmppdf) {
- $arrayofinclusion[] = '^' . preg_quote(dol_sanitizeFileName($tmppdf), '/') . '\.pdf$';
- }
- foreach ($listofobjectref as $tmppdf) {
- $arrayofinclusion[] = '^' . preg_quote(dol_sanitizeFileName($tmppdf), '/') . '_[a-zA-Z0-9\-\_\']+\.pdf$'; // To include PDF generated from ODX files
- }
- $listoffiles = dol_dir_list($uploaddir, 'all', 1, implode('|', $arrayofinclusion), '\.meta$|\.png', 'date', SORT_DESC, 0, true);
- // build list of files with full path
- $files = array();
- foreach ($listofobjectref as $basename) {
- $basename = dol_sanitizeFileName($basename);
- foreach ($listoffiles as $filefound) {
- if (strstr($filefound["name"], $basename)) {
- $files[] = $uploaddir . '/' . $basename . '/' . $filefound["name"];
- break;
- }
- }
- }
- // Define output language (Here it is not used because we do only merging existing PDF)
- $outputlangs = $langs;
- $newlang = '';
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
- $newlang = GETPOST('lang_id', 'aZ09');
- }
- //elseif (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && is_object($objecttmp->thirdparty)) { // On massaction, we can have several values for $objecttmp->thirdparty
- // $newlang = $objecttmp->thirdparty->default_lang;
- //}
- if (!empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- if (!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT)) {
- // Create output dir if not exists
- dol_mkdir($diroutputmassaction);
- // Defined name of merged file
- $filename = strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
- $filename = preg_replace('/\s/', '_', $filename);
- // Save merged file
- 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;
- }
- if (count($files) > 0) {
- $now = dol_now();
- $file = $diroutputmassaction . '/' . $filename . '_' . dol_print_date($now, 'dayhourlog') . '.pdf';
- $input_files = '';
- foreach ($files as $f) {
- $input_files .= ' ' . escapeshellarg($f);
- }
- $cmd = 'pdftk ' . $input_files . ' cat output ' . escapeshellarg($file);
- exec($cmd);
- // check if pdftk is installed
- if (file_exists($file)) {
- if (!empty($conf->global->MAIN_UMASK)) {
- @chmod($file, octdec($conf->global->MAIN_UMASK));
- }
- $langs->load("exports");
- setEventMessages($langs->trans('FileSuccessfullyBuilt', $filename . '_' . dol_print_date($now, 'dayhourlog')), null, 'mesgs');
- } else {
- setEventMessages($langs->trans('ErrorPDFTkOutputFileNotFound'), null, 'errors');
- }
- } else {
- setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
- }
- } else {
- // Create empty PDF
- $formatarray = pdf_getFormat();
- $page_largeur = $formatarray['width'];
- $page_hauteur = $formatarray['height'];
- $format = array($page_largeur, $page_hauteur);
- $pdf = pdf_getInstance($format);
- if (class_exists('TCPDF')) {
- $pdf->setPrintHeader(false);
- $pdf->setPrintFooter(false);
- }
- $pdf->SetFont(pdf_getPDFFont($outputlangs));
- if (getDolGlobalString('MAIN_DISABLE_PDF_COMPRESSION')) {
- $pdf->SetCompression(false);
- }
- // Add all others
- foreach ($files as $file) {
- // Charge un document PDF depuis un fichier.
- $pagecount = $pdf->setSourceFile($file);
- for ($i = 1; $i <= $pagecount; $i++) {
- $tplidx = $pdf->importPage($i);
- $s = $pdf->getTemplatesize($tplidx);
- $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
- $pdf->useTemplate($tplidx);
- }
- }
- // Create output dir if not exists
- dol_mkdir($diroutputmassaction);
- // Defined name of merged file
- $filename = strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
- $filename = preg_replace('/\s/', '_', $filename);
- // Save merged file
- 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;
- }
- if ($pagecount) {
- $now = dol_now();
- $file = $diroutputmassaction . '/' . $filename . '_' . dol_print_date($now, 'dayhourlog') . '.pdf';
- $pdf->Output($file, 'F');
- if (!empty($conf->global->MAIN_UMASK)) {
- @chmod($file, octdec($conf->global->MAIN_UMASK));
- }
- $langs->load("exports");
- setEventMessages($langs->trans('FileSuccessfullyBuilt', $filename . '_' . dol_print_date($now, 'dayhourlog')), null, 'mesgs');
- } else {
- setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
- }
- }
- }
- // Remove a file from massaction area
- if ($action == 'remove_file') {
- require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php';
- $langs->load("other");
- $upload_dir = $diroutputmassaction;
- $file = $upload_dir . '/' . GETPOST('file');
- $ret = dol_delete_file($file);
- if ($ret) {
- setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('file')), null, 'errors');
- }
- $action = '';
- }
- // Validate records
- if (!$error && $massaction == 'validate' && $permissiontoadd) {
- $objecttmp = new $objectclass($db);
- if (($objecttmp->element == 'facture' || $objecttmp->element == 'invoice') && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
- $langs->load("errors");
- setEventMessages($langs->trans('ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'), null, 'errors');
- $error++;
- }
- if ($objecttmp->element == 'invoice_supplier' && isModEnabled('stock') && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
- $langs->load("errors");
- setEventMessages($langs->trans('ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'), null, 'errors');
- $error++;
- }
- if ($objecttmp->element == 'facture') {
- if (!empty($toselect) && !empty($conf->global->INVOICE_CHECK_POSTERIOR_DATE)) {
- // order $toselect by date
- $sql = "SELECT rowid FROM " . MAIN_DB_PREFIX . "facture";
- $sql .= " WHERE rowid IN (" . $db->sanitize(implode(",", $toselect)) . ")";
- $sql .= " ORDER BY datef";
- $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++;
- }
- }
- }
- if (!$error) {
- $db->begin();
- $nbok = 0;
- foreach ($toselect as $toselectid) {
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- if (method_exists($objecttmp, 'validate')) {
- $result = $objecttmp->validate($user);
- } elseif (method_exists($objecttmp, 'setValid')) {
- $result = $objecttmp->setValid($user);
- } else {
- $objecttmp->error = 'No method validate or setValid on this object';
- $result = -1;
- }
- if ($result == 0) {
- $langs->load("errors");
- setEventMessages($langs->trans("ErrorObjectMustHaveStatusDraftToBeValidated", $objecttmp->ref), null, 'errors');
- $error++;
- break;
- } elseif ($result < 0) {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- break;
- } else {
- // validate() rename pdf but do not regenerate
- // Define output language
- if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
- $outputlangs = $langs;
- $newlang = '';
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
- $newlang = GETPOST('lang_id', 'aZ09');
- }
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && property_exists($objecttmp, 'thirdparty')) {
- if ((property_exists($objecttmp, 'socid') || property_exists($objecttmp, 'fk_soc')) && empty($objecttmp->thirdparty)) {
- $objecttmp->fetch_thirdparty();
- }
- if (!empty($objecttmp->thirdparty)) {
- $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
- $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);
- $moreparams = !empty($moreparams) ? $moreparams : null;
- $result = $objecttmp->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
- if ($result < 0) {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- }
- }
- $nbok++;
- }
- } else {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- break;
- }
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- //var_dump($listofobjectthirdparties);exit;
- }
- }
- function getInventoryIdFromMassaction($massaction){
- $resultInventory = explode('_', $massaction);
- return $resultInventory[1];
- }
- function createRollerHistoryObj($obj, $selectedIdFromInventory, $inventory_from, $inventory_to, $status_from, $status_id){
- $obj->id = $selectedIdFromInventory;
- $obj->egyebAdatok['warehouse_from'] = $inventory_from;
- $obj->fk_warehouse = $inventory_to;
- $obj->egyebAdatok['status_from'] = $status_from;
- $obj->status = $status_id;
- return $obj;
- }
- if (!$error && strpos($massaction, 'towarehouse_') !== false && $permissiontoadd) {
- $objForHistory = new stdClass();
- $status_id = '113';
- $inventory_from = $inventory_id;
- $inventory_to = getInventoryIdFromMassaction($massaction);
- $db->begin();
- if (!is_array($toselect) || empty($toselect)) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- $db->rollback();
- }
- foreach ($toselect as $toselectid) {
- $selectedArray = explode('_', $toselectid);
- $selectedStatusFromInventory = $selectedArray[0];
- $selectedIdFromInventory = $selectedArray[1];
- if ($selectedStatusFromSelector !== $status_id) {
- $result = $object->fetch($selectedArray[0]);
- if ($result > 0) {
- $objForHistory = createRollerHistoryObj($objForHistory, $selectedIdFromInventory, $inventory_from, $inventory_to, $statusesrowidAndlabel[$selectedStatusFromInventory], $status_id);
- $res = $modRollerstorage->updateStatusAndWarehouse($db, $object, $selectedIdFromInventory, $objForHistory);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && strpos($massaction, "preout") !== false && $permissiontoadd) {
- $objForHistory = new stdClass();
- $status_id = '112';
- $inventory_from = $inventory_to = getInventoryIdFromMassaction($massaction);
- $db->begin();
- if (!is_array($toselect) || empty($toselect)) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- $db->rollback();
- }
- foreach ($toselect as $toselectid) {
- $selectedArray = explode('_', $toselectid);
- $selectedStatusFromInventory = $selectedArray[0];
- $selectedIdFromInventory = $selectedArray[1];
- if ($selectedStatusFromSelector !== $status_id) {
- $result = $object->fetch($selectedArray[0]);
- if ($result > 0) {
- $objForHistory = createRollerHistoryObj($objForHistory, $selectedIdFromInventory, $inventory_from, $inventory_to, $statusesrowidAndlabel[$selectedStatusFromInventory], $status_id);
- $res = $modRollerstorage->updateStatus($db, $object, $selectedIdFromInventory, $objForHistory);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && strpos($massaction, "prewarehouse") !== false && $permissiontoadd) {
- $objForHistory = new stdClass();
- $status_id = '110';
- $inventory_from = $inventory_to = getInventoryIdFromMassaction($massaction);
- $db->begin();
- if (!is_array($toselect) || empty($toselect)) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- $db->rollback();
- }
- foreach ($toselect as $toselectid) {
- $selectedArray = explode('_', $toselectid);
- $selectedStatusFromInventory = $selectedArray[0];
- $selectedIdFromInventory = $selectedArray[1];
- if ($selectedStatusFromSelector !== $status_id) {
- $result = $object->fetch($selectedArray[0]);
- if ($result > 0) {
- $objForHistory = createRollerHistoryObj($objForHistory, $selectedIdFromInventory, $inventory_from, $inventory_to, $statusesrowidAndlabel[$selectedStatusFromInventory], $status_id);
- $res = $modRollerstorage->updateStatus($db, $object, $selectedIdFromInventory, $objForHistory);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && strpos($massaction, "prefaulty") !== false && $permissiontoadd) {
- $objForHistory = new stdClass();
- $status_id = '114';
- $inventory_from = $inventory_to = getInventoryIdFromMassaction($massaction);
- $db->begin();
- if (!is_array($toselect) || empty($toselect)) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- $db->rollback();
- }
- foreach ($toselect as $toselectid) {
- $selectedArray = explode('_', $toselectid);
- $selectedStatusFromInventory = $selectedArray[0];
- $selectedIdFromInventory = $selectedArray[1];
- if ($selectedStatusFromSelector !== $status_id) {
- $result = $object->fetch($selectedArray[0]);
- if ($result > 0) {
- $objForHistory = createRollerHistoryObj($objForHistory, $selectedIdFromInventory, $inventory_from, $inventory_to, $statusesrowidAndlabel[$selectedStatusFromInventory], $status_id);
- $res = $modRollerstorage->updateStatus($db, $object, $selectedIdFromInventory, $objForHistory);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && strpos($massaction, "preservice") !== false && $permissiontoadd) {
- $objForHistory = new stdClass();
- $status_id = '111';
- $inventory_from = $inventory_to = getInventoryIdFromMassaction($massaction);
- $db->begin();
- if (!is_array($toselect) || empty($toselect)) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- $db->rollback();
- }
- foreach ($toselect as $toselectid) {
- $selectedArray = explode('_', $toselectid);
- $selectedStatusFromInventory = $selectedArray[0];
- $selectedIdFromInventory = $selectedArray[1];
- if ($selectedStatusFromSelector !== $status_id) {
- $result = $object->fetch($selectedArray[0]);
- if ($result > 0) {
- $objForHistory = createRollerHistoryObj($objForHistory, $selectedIdFromInventory, $inventory_from, $inventory_to, $statusesrowidAndlabel[$selectedStatusFromInventory], $status_id);
- $res = $modRollerstorage->updateStatus($db, $object, $selectedIdFromInventory, $objForHistory);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- // Delete record from mass action (massaction = 'delete' for direct delete, action/confirm='delete'/'yes' with a confirmation step before)
- if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == 'yes')) && $permissiontodelete) {
- $db->begin();
- $objecttmp = new $objectclass($db);
- $nbok = 0;
- $TMsg = array();
- foreach ($toselect as $toselectid) {
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- // Refuse deletion for some objects/status
- if ($objectclass == 'Facture' && empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $objecttmp->status != Facture::STATUS_DRAFT) {
- $langs->load("errors");
- $nbignored++;
- $TMsg[] = '<div class="error">' . $langs->trans('ErrorOnlyDraftStatusCanBeDeletedInMassAction', $objecttmp->ref) . '</div><br>';
- continue;
- }
- if (method_exists($objecttmp, 'is_erasable') && $objecttmp->is_erasable() <= 0) {
- $langs->load("errors");
- $nbignored++;
- $TMsg[] = '<div class="error">' . $langs->trans('ErrorRecordHasChildren') . ' ' . $objecttmp->ref . '</div><br>';
- continue;
- }
- if ($objectclass == 'Holiday' && !in_array($objecttmp->statut, array(Holiday::STATUS_DRAFT, Holiday::STATUS_CANCELED, Holiday::STATUS_REFUSED))) {
- $langs->load("errors");
- $nbignored++;
- $TMsg[] = '<div class="error">' . $langs->trans('ErrorLeaveRequestMustBeDraftCanceledOrRefusedToBeDeleted', $objecttmp->ref) . '</div><br>';
- continue;
- }
- if ($objectclass == "Task" && $objecttmp->hasChildren() > 0) {
- $sql = "UPDATE " . MAIN_DB_PREFIX . "projet_task SET fk_task_parent = 0 WHERE fk_task_parent = " . ((int) $objecttmp->id);
- $res = $db->query($sql);
- if (!$res) {
- setEventMessage('ErrorRecordParentingNotModified', 'errors');
- $error++;
- }
- }
- if (in_array($objecttmp->element, array('societe', 'member'))) {
- $result = $objecttmp->delete($objecttmp->id, $user, 1);
- } elseif (in_array($objecttmp->element, array('action'))) {
- $result = $objecttmp->delete();
- } else {
- $result = $objecttmp->delete($user);
- }
- if (empty($result)) { // if delete returns 0, there is at least one object linked
- $TMsg = array_merge($objecttmp->errors, $TMsg);
- } elseif ($result < 0) { // if delete returns is < 0, there is an error, we break and rollback later
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- break;
- } else {
- $nbok++;
- }
- } else {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- break;
- }
- }
- if (empty($error)) {
- // Message for elements well deleted
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
- } elseif ($nbok > 0) {
- setEventMessages($langs->trans("RecordDeleted", $nbok), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("NoRecordDeleted"), null, 'mesgs');
- }
- // Message for elements which can't be deleted
- if (!empty($TMsg)) {
- sort($TMsg);
- setEventMessages('', array_unique($TMsg), 'warnings');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- //var_dump($listofobjectthirdparties);exit;
- }
- // Generate document foreach object according to model linked to object
- // @todo : propose model selection
- if (!$error && $massaction == 'generate_doc' && $permissiontoread) {
- $db->begin();
- $objecttmp = new $objectclass($db);
- $nbok = 0;
- foreach ($toselect as $toselectid) {
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- $outputlangs = $langs;
- $newlang = '';
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
- $newlang = GETPOST('lang_id', 'aZ09');
- }
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($objecttmp->thirdparty->default_lang)) {
- $newlang = $objecttmp->thirdparty->default_lang; // for proposal, order, invoice, ...
- }
- if (getDolGlobalInt('MAIN_MULTILANGS') && empty($newlang) && isset($objecttmp->default_lang)) {
- $newlang = $objecttmp->default_lang; // for thirdparty
- }
- if ($conf->global->MAIN_MULTILANGS && empty($newlang) && empty($objecttmp->thirdparty)) { //load lang from thirdparty
- $objecttmp->fetch_thirdparty();
- $newlang = $objecttmp->thirdparty->default_lang; // for proposal, order, invoice, ...
- }
- if (!empty($newlang)) {
- $outputlangs = new Translate("", $conf);
- $outputlangs->setDefaultLang($newlang);
- }
- // To be sure vars is defined
- if (empty($hidedetails)) {
- $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
- }
- if (empty($hidedesc)) {
- $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
- }
- if (empty($hideref)) {
- $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
- }
- if (empty($moreparams)) {
- $moreparams = null;
- }
- $result = $objecttmp->generateDocument($objecttmp->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
- 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("RecordsGenerated", $nbok), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("RecordGenerated", $nbok), null, 'mesgs');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- }
- if (!$error && ($action == 'affecttag' && $confirm == 'yes') && $permissiontoadd) {
- $db->begin();
- $affecttag_type = GETPOST('affecttag_type', 'alpha');
- if (!empty($affecttag_type)) {
- $affecttag_type_array = explode(',', $affecttag_type);
- } else {
- setEventMessage('CategTypeNotFound', 'errors');
- }
- 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();
- foreach ($categ_type_array as $categdef) {
- 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');
- //var_dump($toselect);exit;
- foreach ($toselect as $toselectid) {
- $result = $object->fetch($toselectid);
- //var_dump($contcats);exit;
- 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;
- }
- }
- }
- }
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- } else {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && ($action == 'updateprice' && $confirm == 'yes') && $permissiontoadd) {
- $db->begin();
- if (GETPOSTISSET('pricerate')) {
- $pricepercentage = GETPOST('pricerate', 'int');
- if ($pricepercentage == 0) {
- setEventMessages($langs->trans("RecordsModified", 0), null);
- } else {
- foreach ($toselect as $toselectid) {
- $result = $object->fetch($toselectid);
- //var_dump($contcats);exit;
- if ($result > 0) {
- if ($obj->price_base_type == 'TTC') {
- $newprice = $object->price_ttc * (100 + $pricepercentage) / 100;
- $minprice = $object->price_min_ttc;
- } else {
- $newprice = $object->price * (100 + $pricepercentage) / 100;
- $minprice = $object->price_min;
- }
- $res = $object->updatePrice($newprice, $obj->price_base_type, $user, $object->tva_tx, $minprice, 0, $object->tva_npr, 0, 0, array(), $object->default_vat_code);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- }
- }
- if (!$error) {
- if ($nbok > 0) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && ($action == 'setsupervisor' && $confirm == 'yes') && $permissiontoadd) {
- $db->begin();
- $supervisortoset = GETPOST('supervisortoset');
- if (!empty($supervisortoset)) {
- foreach ($toselect as $toselectid) {
- $result = $object->fetch($toselectid);
- //var_dump($contcats);exit;
- if ($result > 0) {
- $object->fk_user = $supervisortoset;
- $res = $object->update($user);
- if ($res > 0) {
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- } else {
- setEventMessage('UserNotFound', 'errors');
- $error++;
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- } else {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- if (!$error && ($action == 'affectuser' && $confirm == 'yes') && $permissiontoadd) {
- $db->begin();
- $usertoaffect = GETPOST('usertoaffect');
- $projectrole = GETPOST('projectrole');
- $tasksrole = GETPOST('tasksrole');
- if (!empty($usertoaffect)) {
- foreach ($toselect as $toselectid) {
- $result = $object->fetch($toselectid);
- //var_dump($contcats);exit;
- if ($result > 0) {
- $res = $object->add_contact($usertoaffect, $projectrole, 'internal');
- if ($res >= 0) {
- $taskstatic = new Task($db);
- $task_array = $taskstatic->getTasksArray(0, 0, $object->id, 0, 0);
- foreach ($task_array as $task) {
- $tasksToAffect = new Task($db);
- $result = $tasksToAffect->fetch($task->id);
- if ($result > 0) {
- $res = $tasksToAffect->add_contact($usertoaffect, $tasksrole, 'internal');
- if ($res < 0) {
- setEventMessages($tasksToAffect->error, $tasksToAffect->errors, 'errors');
- }
- }
- }
- $nbok++;
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- }
- } else {
- setEventMessages($object->error, $object->errors, 'errors');
- $error++;
- break;
- }
- }
- } else {
- setEventMessage('UserNotFound', 'errors');
- $error++;
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- } else {
- setEventMessages($langs->trans("RecordsModified", $nbok), null);
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- 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);
- if ($result > 0) {
- if (in_array($objecttmp->element, array('societe'))) {
- $result = $objecttmp->setStatut(1);
- }
- 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("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);
- if ($result > 0) {
- if (in_array($objecttmp->element, array('societe'))) {
- $result = $objecttmp->setStatut(0);
- }
- 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("RecordsDisabled", $nbok), null, 'mesgs');
- } else {
- setEventMessages($langs->trans("RecordDisabled"), null, 'mesgs');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- }
- if (!$error && $action == 'confirm_edit_value_extrafields' && $confirm == 'yes' && $permissiontoadd) {
- $db->begin();
- $objecttmp = new $objectclass($db);
- $e = new ExtraFields($db); // fetch optionals attributes and labels
- $e->fetch_name_optionals_label($objecttmp->table_element);
- $nbok = 0;
- $extrafieldKeyToUpdate = GETPOST('extrafield-key-to-update');
- foreach ($toselect as $toselectid) {
- /** @var CommonObject $objecttmp */
- $objecttmp = new $objectclass($db); // to avoid ghost data
- $result = $objecttmp->fetch($toselectid);
- if ($result > 0) {
- // Fill array 'array_options' with data from add form
- $ret = $e->setOptionalsFromPost(null, $objecttmp, $extrafieldKeyToUpdate);
- if ($ret > 0) {
- $objecttmp->insertExtraFields();
- } else {
- $error++;
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- }
- } 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();
- }
- }
- 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();
- }
- }
- // Approve for leave only
- 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);
- 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) {
- $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);
- if ($verif <= 0) {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- }
- // If no SQL error, we redirect to the request form
- if (!$error) {
- // Calculcate number of days consumed
- $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;
- require_once DOL_DOCUMENT_ROOT . '/core/class/CMailFile.class.php';
- $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 = '';
- }
- }
- }
- } else {
- $langs->load("errors");
- setEventMessages($langs->trans('ErrorNotApproverForHoliday', $objecttmp->ref), null, 'errors');
- }
- } else {
- setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
- $error++;
- break;
- }
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("RecordsApproved", $nbok), null, 'mesgs');
- } elseif ($nbok == 1) {
- setEventMessages($langs->trans("RecordAproved"), null, 'mesgs');
- }
- $db->commit();
- } else {
- $db->rollback();
- }
- }
- if (!$error && ($massaction == 'increaseholiday' || ($action == 'increaseholiday' && $confirm == 'yes')) && $permissiontoapprove) {
- $db->begin();
- $objecttmp = new $objectclass($db);
- $nbok = 0;
- $typeholiday = GETPOST('typeholiday', 'alpha');
- $nbdaysholidays = GETPOST('nbdaysholidays', 'double');
- if ($nbdaysholidays <= 0) {
- setEventMessages($langs->trans("WrongAmount"), "", 'errors');
- $error++;
- }
- if (!$error) {
- foreach ($toselect as $toselectid) {
- $balancecpuser = $objecttmp->getCPforUser($toselectid, $typeholiday);
- if (!empty($balancecpuser)) {
- $newnbdaysholidays = $nbdaysholidays + $balancecpuser;
- } else {
- $newnbdaysholidays = $nbdaysholidays;
- }
- $result = $holiday->addLogCP($user->id, $toselectid, $langs->transnoentitiesnoconv('ManualUpdate'), $newnbdaysholidays, $typeholiday);
- if ($result <= 0) {
- setEventMessages($holiday->error, $holiday->errors, 'errors');
- $error++;
- break;
- }
- $objecttmp->updateSoldeCP($toselectid, $newnbdaysholidays, $typeholiday);
- if ($result > 0) {
- $nbok++;
- } else {
- setEventMessages("", $langs->trans("ErrorUpdatingUsersCP"), 'errors');
- $error++;
- break;
- }
- }
- }
- if (!$error) {
- if ($nbok > 1) {
- setEventMessages($langs->trans("HolidayRecordsIncreased", $nbok), null, 'mesgs');
- } elseif ($nbok == 1) {
- setEventMessages($langs->trans("HolidayRecordIncreased"), null, 'mesgs');
- }
- $db->commit();
- $toselect = array();
- } else {
- $db->rollback();
- }
- }
- $parameters['toselect'] = (empty($toselect) ? array() : $toselect);
- $parameters['uploaddir'] = $uploaddir;
- $parameters['massaction'] = $massaction;
- $parameters['diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction : null;
- $reshook = $hookmanager->executeHooks('doMassActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
- if ($reshook < 0) {
- setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
- }
|