actions_massactions.inc.php 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529
  1. <?php
  2. /* Copyright (C) 2015-2017 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2018-2021 Nicolas ZABOURI <info@inovea-conseil.com>
  4. * Copyright (C) 2018 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2019 Ferran Marcet <fmarcet@2byte.es>
  6. * Copyright (C) 2019-2021 Frédéric France <frederic.france@netlogic.fr>
  7. *
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. * or see https://www.gnu.org/
  21. */
  22. /**
  23. * \file htdocs/core/actions_massactions.inc.php
  24. * \brief Code for actions done with massaction button (send by email, merge pdf, delete, ...)
  25. */
  26. // $massaction must be defined
  27. // $objectclass and $objectlabel must be defined
  28. // $parameters, $object, $action must be defined for the hook.
  29. // $permissiontoread, $permissiontoadd, $permissiontodelete, $permissiontoclose may be defined
  30. // $uploaddir may be defined (example to $conf->project->dir_output."/";)
  31. // $toselect may be defined
  32. // $diroutputmassaction may be defined
  33. // Protection
  34. if (empty($objectclass) || empty($uploaddir)) {
  35. dol_print_error(null, 'include of actions_massactions.inc.php is done but var $objectclass or $uploaddir was not defined');
  36. exit;
  37. }
  38. // For backward compatibility
  39. if (!empty($permtoread) && empty($permissiontoread)) {
  40. $permissiontoread = $permtoread;
  41. }
  42. if (!empty($permtocreate) && empty($permissiontoadd)) {
  43. $permissiontoadd = $permtocreate;
  44. }
  45. if (!empty($permtodelete) && empty($permissiontodelete)) {
  46. $permissiontodelete = $permtodelete;
  47. }
  48. // Mass actions. Controls on number of lines checked.
  49. $maxformassaction = (empty($conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS) ? 1000 : $conf->global->MAIN_LIMIT_FOR_MASS_ACTIONS);
  50. if (!empty($massaction) && is_array($toselect) && count($toselect) < 1) {
  51. $error++;
  52. setEventMessages($langs->trans("NoRecordSelected"), null, "warnings");
  53. }
  54. if (!$error && is_array($toselect) && count($toselect) > $maxformassaction) {
  55. setEventMessages($langs->trans('TooManyRecordForMassAction', $maxformassaction), null, 'errors');
  56. $error++;
  57. }
  58. 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
  59. $massaction = 'presend';
  60. }
  61. if (!$error && $massaction == 'confirm_presend') {
  62. $resaction = '';
  63. $nbsent = 0;
  64. $nbignored = 0;
  65. $langs->load("mails");
  66. include_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  67. include_once DOL_DOCUMENT_ROOT.'/core/lib/signature.lib.php';
  68. $listofobjectid = array();
  69. $listofobjectthirdparties = array();
  70. $listofobjectcontacts = array();
  71. $listofobjectref = array();
  72. $contactidtosend = array();
  73. $attachedfilesThirdpartyObj = array();
  74. $oneemailperrecipient = (GETPOST('oneemailperrecipient', 'int') ? 1 : 0);
  75. if (!$error) {
  76. $thirdparty = new Societe($db);
  77. $objecttmp = new $objectclass($db);
  78. if ($objecttmp->element == 'expensereport') {
  79. $thirdparty = new User($db);
  80. }
  81. if ($objecttmp->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
  82. $thirdparty = new Adherent($db);
  83. }
  84. if ($objecttmp->element == 'holiday') {
  85. $thirdparty = new User($db);
  86. }
  87. foreach ($toselect as $toselectid) {
  88. $objecttmp = new $objectclass($db); // we must create new instance because instance is saved into $listofobjectref array for future use
  89. $result = $objecttmp->fetch($toselectid);
  90. if ($result > 0) {
  91. $listofobjectid[$toselectid] = $toselectid;
  92. $thirdpartyid = ($objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid);
  93. if ($objecttmp->element == 'societe') {
  94. $thirdpartyid = $objecttmp->id;
  95. }
  96. if ($objecttmp->element == 'expensereport') {
  97. $thirdpartyid = $objecttmp->fk_user_author;
  98. }
  99. if ($objecttmp->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
  100. $thirdpartyid = $objecttmp->fk_member;
  101. }
  102. if ($objecttmp->element == 'holiday') {
  103. $thirdpartyid = $objecttmp->fk_user;
  104. }
  105. if (empty($thirdpartyid)) {
  106. $thirdpartyid = 0;
  107. }
  108. if ($objectclass == 'Facture') {
  109. $tmparraycontact = array();
  110. $tmparraycontact = $objecttmp->liste_contact(-1, 'external', 0, 'BILLING');
  111. if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
  112. foreach ($tmparraycontact as $data_email) {
  113. $listofobjectcontacts[$toselectid][$data_email['id']] = $data_email['email'];
  114. }
  115. }
  116. } elseif ($objectclass == 'CommandeFournisseur') {
  117. $tmparraycontact = array();
  118. $tmparraycontact = $objecttmp->liste_contact(-1, 'external', 0, 'CUSTOMER');
  119. if (is_array($tmparraycontact) && count($tmparraycontact) > 0) {
  120. foreach ($tmparraycontact as $data_email) {
  121. $listofobjectcontacts[$toselectid][$data_email['id']] = $data_email['email'];
  122. }
  123. }
  124. }
  125. $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
  126. $listofobjectref[$thirdpartyid][$toselectid] = $objecttmp;
  127. }
  128. }
  129. }
  130. // Check mandatory parameters
  131. if (GETPOST('fromtype', 'alpha') === 'user' && empty($user->email)) {
  132. $error++;
  133. setEventMessages($langs->trans("NoSenderEmailDefined"), null, 'warnings');
  134. $massaction = 'presend';
  135. }
  136. $receiver = GETPOST('receiver', 'alphawithlgt');
  137. if (!is_array($receiver)) {
  138. if (empty($receiver) || $receiver == '-1') {
  139. $receiver = array();
  140. } else {
  141. $receiver = array($receiver);
  142. }
  143. }
  144. if (!trim($_POST['sendto']) && count($receiver) == 0 && count($listofobjectthirdparties) == 1) { // if only one recipient, receiver is mandatory
  145. $error++;
  146. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Recipient")), null, 'warnings');
  147. $massaction = 'presend';
  148. }
  149. if (!GETPOST('subject', 'restricthtml')) {
  150. $error++;
  151. setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("MailTopic")), null, 'warnings');
  152. $massaction = 'presend';
  153. }
  154. // Loop on each recipient/thirdparty
  155. if (!$error) {
  156. foreach ($listofobjectthirdparties as $thirdpartyid) {
  157. $result = $thirdparty->fetch($thirdpartyid);
  158. if ($result < 0) {
  159. dol_print_error($db);
  160. exit;
  161. }
  162. $sendto = '';
  163. $sendtocc = '';
  164. $sendtobcc = '';
  165. $sendtoid = array();
  166. // Define $sendto
  167. $tmparray = array();
  168. if (trim($_POST['sendto'])) {
  169. // Recipients are provided into free text
  170. $tmparray[] = trim(GETPOST('sendto', 'alphawithlgt'));
  171. }
  172. if (count($receiver) > 0) {
  173. foreach ($receiver as $key => $val) {
  174. // Recipient was provided from combo list
  175. if ($val == 'thirdparty') { // Id of third party or user
  176. $tmparray[] = $thirdparty->name.' <'.$thirdparty->email.'>';
  177. } elseif ($val && method_exists($thirdparty, 'contact_get_property')) { // Id of contact
  178. $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
  179. $sendtoid[] = $val;
  180. }
  181. }
  182. }
  183. $sendto = implode(',', $tmparray);
  184. // Define $sendtocc
  185. $receivercc = GETPOST('receivercc', 'alphawithlgt');
  186. if (!is_array($receivercc)) {
  187. if ($receivercc == '-1') {
  188. $receivercc = array();
  189. } else {
  190. $receivercc = array($receivercc);
  191. }
  192. }
  193. $tmparray = array();
  194. if (trim($_POST['sendtocc'])) {
  195. $tmparray[] = trim(GETPOST('sendtocc', 'alphawithlgt'));
  196. }
  197. if (count($receivercc) > 0) {
  198. foreach ($receivercc as $key => $val) {
  199. // Recipient was provided from combo list
  200. if ($val == 'thirdparty') { // Id of third party
  201. $tmparray[] = $thirdparty->name.' <'.$thirdparty->email.'>';
  202. } elseif ($val) { // Id du contact
  203. $tmparray[] = $thirdparty->contact_get_property((int) $val, 'email');
  204. //$sendtoid[] = $val; TODO Add also id of contact in CC ?
  205. }
  206. }
  207. }
  208. $sendtocc = implode(',', $tmparray);
  209. //var_dump($listofobjectref);exit;
  210. $listofqualifiedobj = array();
  211. $listofqualifiedref = array();
  212. $thirdpartywithoutemail = array();
  213. foreach ($listofobjectref[$thirdpartyid] as $objectid => $objectobj) {
  214. //var_dump($thirdpartyid.' - '.$objectid.' - '.$objectobj->statut);
  215. if ($objectclass == 'Propal' && $objectobj->statut == Propal::STATUS_DRAFT) {
  216. $langs->load("errors");
  217. $nbignored++;
  218. $resaction .= '<div class="error">'.$langs->trans('ErrorOnlyProposalNotDraftCanBeSentInMassAction', $objectobj->ref).'</div><br>';
  219. continue; // Payment done or started or canceled
  220. }
  221. if ($objectclass == 'Commande' && $objectobj->statut == Commande::STATUS_DRAFT) {
  222. $langs->load("errors");
  223. $nbignored++;
  224. $resaction .= '<div class="error">'.$langs->trans('ErrorOnlyOrderNotDraftCanBeSentInMassAction', $objectobj->ref).'</div><br>';
  225. continue;
  226. }
  227. if ($objectclass == 'Facture' && $objectobj->statut == Facture::STATUS_DRAFT) {
  228. $langs->load("errors");
  229. $nbignored++;
  230. $resaction .= '<div class="error">'.$langs->trans('ErrorOnlyInvoiceValidatedCanBeSentInMassAction', $objectobj->ref).'</div><br>';
  231. continue; // Payment done or started or canceled
  232. }
  233. // Test recipient
  234. if (empty($sendto)) { // For the case, no recipient were set (multi thirdparties send)
  235. if ($objectobj->element == 'societe') {
  236. $sendto = $objectobj->email;
  237. } elseif ($objectobj->element == 'expensereport') {
  238. $fuser = new User($db);
  239. $fuser->fetch($objectobj->fk_user_author);
  240. $sendto = $fuser->email;
  241. } elseif ($objectobj->element == 'partnership' && getDolGlobalString('PARTNERSHIP_IS_MANAGED_FOR') == 'member') {
  242. $fadherent = new Adherent($db);
  243. $fadherent->fetch($objectobj->fk_member);
  244. $sendto = $fadherent->email;
  245. } elseif ($objectobj->element == 'holiday') {
  246. $fuser = new User($db);
  247. $fuser->fetch($objectobj->fk_user);
  248. $sendto = $fuser->email;
  249. } elseif ($objectobj->element == 'facture' && !empty($listofobjectcontacts[$objectid])) {
  250. $emails_to_sends = array();
  251. $objectobj->fetch_thirdparty();
  252. $contactidtosend = array();
  253. foreach ($listofobjectcontacts[$objectid] as $contactemailid => $contactemailemail) {
  254. $emails_to_sends[] = $objectobj->thirdparty->contact_get_property($contactemailid, 'email');
  255. if (!in_array($contactemailid, $contactidtosend)) {
  256. $contactidtosend[] = $contactemailid;
  257. }
  258. }
  259. if (count($emails_to_sends) > 0) {
  260. $sendto = implode(',', $emails_to_sends);
  261. }
  262. } elseif ($objectobj->element == 'order_supplier' && !empty($listofobjectcontacts[$objectid])) {
  263. $emails_to_sends = array();
  264. $objectobj->fetch_thirdparty();
  265. $contactidtosend = array();
  266. foreach ($listofobjectcontacts[$objectid] as $contactemailid => $contactemailemail) {
  267. $emails_to_sends[] = $objectobj->thirdparty->contact_get_property($contactemailid, 'email');
  268. if (!in_array($contactemailid, $contactidtosend)) {
  269. $contactidtosend[] = $contactemailid;
  270. }
  271. }
  272. if (count($emails_to_sends) > 0) {
  273. $sendto = implode(',', $emails_to_sends);
  274. }
  275. } else {
  276. $objectobj->fetch_thirdparty();
  277. $sendto = $objectobj->thirdparty->email;
  278. }
  279. }
  280. if (empty($sendto)) {
  281. if ($objectobj->element == 'societe') {
  282. $objectobj->thirdparty = $objectobj; // Hack so following code is comaptible when objectobj is a thirdparty
  283. }
  284. //print "No recipient for thirdparty ".$objectobj->thirdparty->name;
  285. $nbignored++;
  286. if (empty($thirdpartywithoutemail[$objectobj->thirdparty->id])) {
  287. $resaction .= '<div class="error">'.$langs->trans('NoRecipientEmail', $objectobj->thirdparty->name).'</div><br>';
  288. }
  289. dol_syslog('No recipient for thirdparty: '.$objectobj->thirdparty->name, LOG_WARNING);
  290. $thirdpartywithoutemail[$objectobj->thirdparty->id] = 1;
  291. continue;
  292. }
  293. if (GETPOST('addmaindocfile')) {
  294. // TODO Use future field $objectobj->fullpathdoc to know where is stored default file
  295. // TODO If not defined, use $objectobj->model_pdf (or defaut invoice config) to know what is template to use to regenerate doc.
  296. $filename = dol_sanitizeFileName($objectobj->ref).'.pdf';
  297. $subdir = '';
  298. // TODO Set subdir to be compatible with multi levels dir trees
  299. // $subdir = get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element)
  300. $filedir = $uploaddir.'/'.$subdir.dol_sanitizeFileName($objectobj->ref);
  301. $filepath = $filedir.'/'.$filename;
  302. // Added By MMI Mathieu Moulin iProspective
  303. // Fix search by filename
  304. if (!file_exists($filepath)) {
  305. $files = glob($filedir.'/'.dol_sanitizeFileName($objectobj->ref).'*.pdf');
  306. if (!empty($files))
  307. $filepath = $files[0];
  308. }
  309. // For supplier invoices, we use the file provided by supplier, not the one we generate
  310. if ($objectobj->element == 'invoice_supplier') {
  311. $fileparams = dol_most_recent_file($uploaddir.'/'.get_exdir($objectobj->id, 2, 0, 0, $objectobj, $objectobj->element).$objectobj->ref, preg_quote($objectobj->ref, '/').'([^\-])+');
  312. $filepath = $fileparams['fullname'];
  313. }
  314. // try to find other files generated for this object (last_main_doc)
  315. $filename_found = '';
  316. $filepath_found = '';
  317. $file_check_list = array();
  318. $file_check_list[] = array(
  319. 'name' => $filename,
  320. 'path' => $filepath,
  321. );
  322. if (!empty($conf->global->MAIL_MASS_ACTION_ADD_LAST_IF_MAIN_DOC_NOT_FOUND) && !empty($objectobj->last_main_doc)) {
  323. $file_check_list[] = array(
  324. 'name' => basename($objectobj->last_main_doc),
  325. 'path' => DOL_DATA_ROOT . '/' . $objectobj->last_main_doc,
  326. );
  327. }
  328. foreach ($file_check_list as $file_check_arr) {
  329. if (dol_is_file($file_check_arr['path'])) {
  330. $filename_found = $file_check_arr['name'];
  331. $filepath_found = $file_check_arr['path'];
  332. break;
  333. }
  334. }
  335. if ($filepath_found) {
  336. // Create form object
  337. $attachedfilesThirdpartyObj[$thirdpartyid][$objectid] = array(
  338. 'paths'=>array($filepath_found),
  339. 'names'=>array($filename_found),
  340. 'mimes'=>array(dol_mimetype($filepath_found))
  341. );
  342. } else {
  343. $nbignored++;
  344. $langs->load("errors");
  345. foreach ($file_check_list as $file_check_arr) {
  346. $resaction .= '<div class="error">'.$langs->trans('ErrorCantReadFile', $file_check_arr['path']).'</div><br>';
  347. dol_syslog('Failed to read file: '.$file_check_arr['path'], LOG_WARNING);
  348. }
  349. continue;
  350. }
  351. }
  352. // Object of thirdparty qualified, we add it
  353. $listofqualifiedobj[$objectid] = $objectobj;
  354. $listofqualifiedref[$objectid] = $objectobj->ref;
  355. //var_dump($listofqualifiedref);
  356. }
  357. // Send email if there is at least one qualified object for current thirdparty
  358. if (count($listofqualifiedobj) > 0) {
  359. $langs->load("commercial");
  360. $reg = array();
  361. $fromtype = GETPOST('fromtype');
  362. if ($fromtype === 'user') {
  363. $from = dol_string_nospecial($user->getFullName($langs), ' ', array(",")).' <'.$user->email.'>';
  364. } elseif ($fromtype === 'company') {
  365. $from = $conf->global->MAIN_INFO_SOCIETE_NOM.' <'.$conf->global->MAIN_INFO_SOCIETE_MAIL.'>';
  366. } elseif (preg_match('/user_aliases_(\d+)/', $fromtype, $reg)) {
  367. $tmp = explode(',', $user->email_aliases);
  368. $from = trim($tmp[($reg[1] - 1)]);
  369. } elseif (preg_match('/global_aliases_(\d+)/', $fromtype, $reg)) {
  370. $tmp = explode(',', $conf->global->MAIN_INFO_SOCIETE_MAIL_ALIASES);
  371. $from = trim($tmp[($reg[1] - 1)]);
  372. } elseif (preg_match('/senderprofile_(\d+)_(\d+)/', $fromtype, $reg)) {
  373. $sql = "SELECT rowid, label, email FROM ".MAIN_DB_PREFIX."c_email_senderprofile WHERE rowid = ".(int) $reg[1];
  374. $resql = $db->query($sql);
  375. $obj = $db->fetch_object($resql);
  376. if ($obj) {
  377. $from = dol_string_nospecial($obj->label, ' ', array(",")).' <'.$obj->email.'>';
  378. }
  379. } else {
  380. $from = GETPOST('fromname').' <'.GETPOST('frommail').'>';
  381. }
  382. $replyto = $from;
  383. $subject = GETPOST('subject', 'restricthtml');
  384. $message = GETPOST('message', 'restricthtml');
  385. $sendtobcc = GETPOST('sendtoccc');
  386. if ($objectclass == 'Propal') {
  387. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_PROPOSAL_TO));
  388. }
  389. if ($objectclass == 'Commande') {
  390. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_ORDER_TO));
  391. }
  392. if ($objectclass == 'Facture') {
  393. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_INVOICE_TO));
  394. }
  395. if ($objectclass == 'Supplier_Proposal') {
  396. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_PROPOSAL_TO));
  397. }
  398. if ($objectclass == 'CommandeFournisseur') {
  399. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_ORDER_TO));
  400. }
  401. if ($objectclass == 'FactureFournisseur') {
  402. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_SUPPLIER_INVOICE_TO));
  403. }
  404. if ($objectclass == 'Project') {
  405. $sendtobcc .= (empty($conf->global->MAIN_MAIL_AUTOCOPY_PROJECT_TO) ? '' : (($sendtobcc ? ", " : "").$conf->global->MAIN_MAIL_AUTOCOPY_PROJECT_TO));
  406. }
  407. // $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)
  408. // $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)
  409. $looparray = array();
  410. if (!$oneemailperrecipient) {
  411. $looparray = $listofqualifiedobj;
  412. foreach ($looparray as $key => $objecttmp) {
  413. $looparray[$key]->thirdparty = $thirdparty; // Force thirdparty on object
  414. }
  415. } else {
  416. $objectforloop = new $objectclass($db);
  417. $objectforloop->thirdparty = $thirdparty; // Force thirdparty on object (even if object was not loaded)
  418. $looparray[0] = $objectforloop;
  419. }
  420. //var_dump($looparray);exit;
  421. dol_syslog("We have set an array of ".count($looparray)." emails to send. oneemailperrecipient=".$oneemailperrecipient);
  422. //var_dump($oneemailperrecipient); var_dump($listofqualifiedobj); var_dump($listofqualifiedref);
  423. 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
  424. // Make substitution in email content
  425. if (!empty($conf->project->enabled) && method_exists($objecttmp, 'fetch_projet') && is_null($objecttmp->project)) {
  426. $objecttmp->fetch_projet();
  427. }
  428. $substitutionarray = getCommonSubstitutionArray($langs, 0, null, $objecttmp);
  429. $substitutionarray['__ID__'] = ($oneemailperrecipient ? join(', ', array_keys($listofqualifiedobj)) : $objecttmp->id);
  430. $substitutionarray['__REF__'] = ($oneemailperrecipient ? join(', ', $listofqualifiedref) : $objecttmp->ref);
  431. $substitutionarray['__EMAIL__'] = $thirdparty->email;
  432. $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"/>';
  433. $parameters = array('mode'=>'formemail');
  434. if (!empty($listofobjectthirdparties)) {
  435. $parameters['listofobjectthirdparties'] = $listofobjectthirdparties;
  436. }
  437. if (!empty($listofobjectref)) {
  438. $parameters['listofobjectref'] = $listofobjectref;
  439. }
  440. complete_substitutions_array($substitutionarray, $langs, $objecttmp, $parameters);
  441. $subjectreplaced = make_substitutions($subject, $substitutionarray);
  442. $messagereplaced = make_substitutions($message, $substitutionarray);
  443. $attachedfiles = array('paths'=>array(), 'names'=>array(), 'mimes'=>array());
  444. if ($oneemailperrecipient) {
  445. // if "one email per recipient" is check we must collate $attachedfiles by thirdparty
  446. if (is_array($attachedfilesThirdpartyObj[$thirdparty->id]) && count($attachedfilesThirdpartyObj[$thirdparty->id])) {
  447. foreach ($attachedfilesThirdpartyObj[$thirdparty->id] as $keyObjId => $objAttachedFiles) {
  448. // Create form object
  449. $attachedfiles = array(
  450. 'paths'=>array_merge($attachedfiles['paths'], $objAttachedFiles['paths']),
  451. 'names'=>array_merge($attachedfiles['names'], $objAttachedFiles['names']),
  452. 'mimes'=>array_merge($attachedfiles['mimes'], $objAttachedFiles['mimes'])
  453. );
  454. }
  455. }
  456. } elseif (!empty($attachedfilesThirdpartyObj[$thirdparty->id][$objectid])) {
  457. // Create form object
  458. // if "one email per recipient" isn't check we must separate $attachedfiles by object
  459. $attachedfiles = $attachedfilesThirdpartyObj[$thirdparty->id][$objectid];
  460. }
  461. $filepath = $attachedfiles['paths'];
  462. $filename = $attachedfiles['names'];
  463. $mimetype = $attachedfiles['mimes'];
  464. // Define the trackid when emails sent from the mass action
  465. if ($oneemailperrecipient) {
  466. $trackid = 'thi'.$thirdparty->id;
  467. if ($objecttmp->element == 'expensereport') {
  468. $trackid = 'use'.$thirdparty->id;
  469. }
  470. if ($objecttmp->element == 'holiday') {
  471. $trackid = 'use'.$thirdparty->id;
  472. }
  473. } else {
  474. $trackid = strtolower(get_class($objecttmp));
  475. if (get_class($objecttmp) == 'Contrat') {
  476. $trackid = 'con';
  477. }
  478. if (get_class($objecttmp) == 'Propal') {
  479. $trackid = 'pro';
  480. }
  481. if (get_class($objecttmp) == 'Commande') {
  482. $trackid = 'ord';
  483. }
  484. if (get_class($objecttmp) == 'Facture') {
  485. $trackid = 'inv';
  486. }
  487. if (get_class($objecttmp) == 'Supplier_Proposal') {
  488. $trackid = 'spr';
  489. }
  490. if (get_class($objecttmp) == 'CommandeFournisseur') {
  491. $trackid = 'sor';
  492. }
  493. if (get_class($objecttmp) == 'FactureFournisseur') {
  494. $trackid = 'sin';
  495. }
  496. $trackid .= $objecttmp->id;
  497. }
  498. //var_dump($filepath);
  499. //var_dump($trackid);exit;
  500. //var_dump($subjectreplaced);
  501. if (empty($sendcontext)) {
  502. $sendcontext = 'standard';
  503. }
  504. // Send mail (substitutionarray must be done just before this)
  505. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  506. $mailfile = new CMailFile($subjectreplaced, $sendto, $from, $messagereplaced, $filepath, $mimetype, $filename, $sendtocc, $sendtobcc, $deliveryreceipt, -1, '', '', $trackid, '', $sendcontext);
  507. if ($mailfile->error) {
  508. $resaction .= '<div class="error">'.$mailfile->error.'</div>';
  509. } else {
  510. $result = $mailfile->sendfile();
  511. if ($result) {
  512. $resaction .= $langs->trans('MailSuccessfulySent', $mailfile->getValidAddress($from, 2), $mailfile->getValidAddress($sendto, 2)).'<br>'; // Must not contain "
  513. $error = 0;
  514. // Insert logs into agenda
  515. foreach ($listofqualifiedobj as $objid2 => $objectobj2) {
  516. if ((!$oneemailperrecipient) && $objid2 != $objectid) {
  517. continue; // We discard this pass to avoid duplicate with other pass in looparray at higher level
  518. }
  519. dol_syslog("Try to insert email event into agenda for objid=".$objid2." => objectobj=".get_class($objectobj2));
  520. /*if ($objectclass == 'Propale') $actiontypecode='AC_PROP';
  521. if ($objectclass == 'Commande') $actiontypecode='AC_COM';
  522. if ($objectclass == 'Facture') $actiontypecode='AC_FAC';
  523. if ($objectclass == 'Supplier_Proposal') $actiontypecode='AC_SUP_PRO';
  524. if ($objectclass == 'CommandeFournisseur') $actiontypecode='AC_SUP_ORD';
  525. if ($objectclass == 'FactureFournisseur') $actiontypecode='AC_SUP_INV';*/
  526. $actionmsg = $langs->transnoentities('MailSentBy').' '.$from.' '.$langs->transnoentities('To').' '.$sendto;
  527. if ($message) {
  528. if ($sendtocc) {
  529. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('Bcc').": ".$sendtocc);
  530. }
  531. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('MailTopic').": ".$subjectreplaced);
  532. $actionmsg = dol_concatdesc($actionmsg, $langs->transnoentities('TextUsedInTheMessageBody').":");
  533. $actionmsg = dol_concatdesc($actionmsg, $messagereplaced);
  534. }
  535. $actionmsg2 = '';
  536. // Initialisation donnees
  537. $objectobj2->sendtoid = (empty($contactidtosend) ? 0 : $contactidtosend);
  538. $objectobj2->actionmsg = $actionmsg; // Long text
  539. $objectobj2->actionmsg2 = $actionmsg2; // Short text
  540. $objectobj2->fk_element = $objid2;
  541. $objectobj2->elementtype = $objectobj2->element;
  542. $triggername = strtoupper(get_class($objectobj2)).'_SENTBYMAIL';
  543. if ($triggername == 'SOCIETE_SENTBYMAIL') {
  544. $triggername = 'COMPANY_SENTBYMAIL';
  545. }
  546. if ($triggername == 'CONTRAT_SENTBYMAIL') {
  547. $triggername = 'CONTRACT_SENTBYMAIL';
  548. }
  549. if ($triggername == 'COMMANDE_SENTBYMAIL') {
  550. $triggername = 'ORDER_SENTBYMAIL';
  551. }
  552. if ($triggername == 'FACTURE_SENTBYMAIL') {
  553. $triggername = 'BILL_SENTBYMAIL';
  554. }
  555. if ($triggername == 'EXPEDITION_SENTBYMAIL') {
  556. $triggername = 'SHIPPING_SENTBYMAIL';
  557. }
  558. if ($triggername == 'COMMANDEFOURNISSEUR_SENTBYMAIL') {
  559. $triggername = 'ORDER_SUPPLIER_SENTBYMAIL';
  560. }
  561. if ($triggername == 'FACTUREFOURNISSEUR_SENTBYMAIL') {
  562. $triggername = 'BILL_SUPPLIER_SENTBYMAIL';
  563. }
  564. if ($triggername == 'SUPPLIERPROPOSAL_SENTBYMAIL') {
  565. $triggername = 'PROPOSAL_SUPPLIER_SENTBYMAIL';
  566. }
  567. if (!empty($triggername)) {
  568. // Call trigger
  569. $result = $objectobj2->call_trigger($triggername, $user);
  570. if ($result < 0) {
  571. $error++;
  572. }
  573. // End call triggers
  574. if ($error) {
  575. setEventMessages($db->lasterror(), $errors, 'errors');
  576. dol_syslog("Error in trigger ".$triggername.' '.$db->lasterror(), LOG_ERR);
  577. }
  578. }
  579. $nbsent++; // Nb of object sent
  580. }
  581. } else {
  582. $langs->load("other");
  583. if ($mailfile->error) {
  584. $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto);
  585. $resaction .= '<br><div class="error">'.$mailfile->error.'</div>';
  586. } elseif (!empty($conf->global->MAIN_DISABLE_ALL_MAILS)) {
  587. $resaction .= '<div class="warning">No mail sent. Feature is disabled by option MAIN_DISABLE_ALL_MAILS</div>';
  588. } else {
  589. $resaction .= $langs->trans('ErrorFailedToSendMail', $from, $sendto) . '<br><div class="error">(unhandled error)</div>';
  590. }
  591. }
  592. }
  593. }
  594. }
  595. }
  596. $resaction .= ($resaction ? '<br>' : $resaction);
  597. $resaction .= '<strong>'.$langs->trans("ResultOfMailSending").':</strong><br>'."\n";
  598. $resaction .= $langs->trans("NbSelected").': '.count($toselect)."\n<br>";
  599. $resaction .= $langs->trans("NbIgnored").': '.($nbignored ? $nbignored : 0)."\n<br>";
  600. $resaction .= $langs->trans("NbSent").': '.($nbsent ? $nbsent : 0)."\n<br>";
  601. if ($nbsent) {
  602. $action = ''; // Do not show form post if there was at least one successfull sent
  603. //setEventMessages($langs->trans("EMailSentToNRecipients", $nbsent.'/'.count($toselect)), null, 'mesgs');
  604. setEventMessages($langs->trans("EMailSentForNElements", $nbsent.'/'.count($toselect)), null, 'mesgs');
  605. setEventMessages($resaction, null, 'mesgs');
  606. } else {
  607. //setEventMessages($langs->trans("EMailSentToNRecipients", 0), null, 'warnings'); // May be object has no generated PDF file
  608. setEventMessages($resaction, null, 'warnings');
  609. }
  610. $action = 'list';
  611. $massaction = '';
  612. }
  613. }
  614. if (!$error && $massaction == 'cancelorders') {
  615. $db->begin();
  616. $nbok = 0;
  617. $orders = GETPOST('toselect', 'array');
  618. foreach ($orders as $id_order) {
  619. $cmd = new Commande($db);
  620. if ($cmd->fetch($id_order) <= 0) {
  621. continue;
  622. }
  623. if ($cmd->statut != Commande::STATUS_VALIDATED) {
  624. $langs->load('errors');
  625. setEventMessages($langs->trans("ErrorObjectMustHaveStatusValidToBeCanceled", $cmd->ref), null, 'errors');
  626. $error++;
  627. break;
  628. } else {
  629. // TODO We do not provide warehouse so no stock change here for the moment.
  630. $result = $cmd->cancel();
  631. }
  632. if ($result < 0) {
  633. setEventMessages($cmd->error, $cmd->errors, 'errors');
  634. $error++;
  635. break;
  636. } else {
  637. $nbok++;
  638. }
  639. }
  640. if (!$error) {
  641. if ($nbok > 1) {
  642. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  643. } else {
  644. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  645. }
  646. $db->commit();
  647. } else {
  648. $db->rollback();
  649. }
  650. }
  651. if (!$error && $massaction == "builddoc" && $permissiontoread && !GETPOST('button_search')) {
  652. if (empty($diroutputmassaction)) {
  653. dol_print_error(null, 'include of actions_massactions.inc.php is done but var $diroutputmassaction was not defined');
  654. exit;
  655. }
  656. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  657. require_once DOL_DOCUMENT_ROOT.'/core/lib/pdf.lib.php';
  658. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  659. $objecttmp = new $objectclass($db);
  660. $listofobjectid = array();
  661. $listofobjectthirdparties = array();
  662. $listofobjectref = array();
  663. foreach ($toselect as $toselectid) {
  664. $objecttmp = new $objectclass($db); // must create new instance because instance is saved into $listofobjectref array for future use
  665. $result = $objecttmp->fetch($toselectid);
  666. if ($result > 0) {
  667. $listofobjectid[$toselectid] = $toselectid;
  668. $thirdpartyid = $objecttmp->fk_soc ? $objecttmp->fk_soc : $objecttmp->socid;
  669. $listofobjectthirdparties[$thirdpartyid] = $thirdpartyid;
  670. $listofobjectref[$toselectid] = $objecttmp->ref;
  671. }
  672. }
  673. $arrayofinclusion = array();
  674. foreach ($listofobjectref as $tmppdf) {
  675. $arrayofinclusion[] = '^'.preg_quote(dol_sanitizeFileName($tmppdf), '/').'\.pdf$';
  676. }
  677. foreach ($listofobjectref as $tmppdf) {
  678. $arrayofinclusion[] = '^'.preg_quote(dol_sanitizeFileName($tmppdf), '/').'_[a-zA-Z0-9\-\_\']+\.pdf$'; // To include PDF generated from ODX files
  679. }
  680. $listoffiles = dol_dir_list($uploaddir, 'all', 1, implode('|', $arrayofinclusion), '\.meta$|\.png', 'date', SORT_DESC, 0, true);
  681. // build list of files with full path
  682. $files = array();
  683. foreach ($listofobjectref as $basename) {
  684. $basename = dol_sanitizeFileName($basename);
  685. foreach ($listoffiles as $filefound) {
  686. if (strstr($filefound["name"], $basename)) {
  687. $files[] = $uploaddir.'/'.$basename.'/'.$filefound["name"];
  688. break;
  689. }
  690. }
  691. }
  692. // Define output language (Here it is not used because we do only merging existing PDF)
  693. $outputlangs = $langs;
  694. $newlang = '';
  695. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  696. $newlang = GETPOST('lang_id', 'aZ09');
  697. }
  698. //elseif ($conf->global->MAIN_MULTILANGS && empty($newlang) && is_object($objecttmp->thirdparty)) { // On massaction, we can have several values for $objecttmp->thirdparty
  699. // $newlang = $objecttmp->thirdparty->default_lang;
  700. //}
  701. if (!empty($newlang)) {
  702. $outputlangs = new Translate("", $conf);
  703. $outputlangs->setDefaultLang($newlang);
  704. }
  705. if (!empty($conf->global->USE_PDFTK_FOR_PDF_CONCAT)) {
  706. // Create output dir if not exists
  707. dol_mkdir($diroutputmassaction);
  708. // Defined name of merged file
  709. $filename = strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
  710. $filename = preg_replace('/\s/', '_', $filename);
  711. // Save merged file
  712. if (in_array($objecttmp->element, array('facture', 'facture_fournisseur')) && $search_status == Facture::STATUS_VALIDATED) {
  713. if ($option == 'late') {
  714. $filename .= '_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
  715. } else {
  716. $filename .= '_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
  717. }
  718. }
  719. if ($year) {
  720. $filename .= '_'.$year;
  721. }
  722. if ($month) {
  723. $filename .= '_'.$month;
  724. }
  725. if (count($files) > 0) {
  726. $now = dol_now();
  727. $file = $diroutputmassaction.'/'.$filename.'_'.dol_print_date($now, 'dayhourlog').'.pdf';
  728. $input_files = '';
  729. foreach ($files as $f) {
  730. $input_files .= ' '.escapeshellarg($f);
  731. }
  732. $cmd = 'pdftk '.$input_files.' cat output '.escapeshellarg($file);
  733. exec($cmd);
  734. // check if pdftk is installed
  735. if (file_exists($file)) {
  736. if (!empty($conf->global->MAIN_UMASK)) {
  737. @chmod($file, octdec($conf->global->MAIN_UMASK));
  738. }
  739. $langs->load("exports");
  740. setEventMessages($langs->trans('FileSuccessfullyBuilt', $filename.'_'.dol_print_date($now, 'dayhourlog')), null, 'mesgs');
  741. } else {
  742. setEventMessages($langs->trans('ErrorPDFTkOutputFileNotFound'), null, 'errors');
  743. }
  744. } else {
  745. setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
  746. }
  747. } else {
  748. // Create empty PDF
  749. $formatarray = pdf_getFormat();
  750. $page_largeur = $formatarray['width'];
  751. $page_hauteur = $formatarray['height'];
  752. $format = array($page_largeur, $page_hauteur);
  753. $pdf = pdf_getInstance($format);
  754. if (class_exists('TCPDF')) {
  755. $pdf->setPrintHeader(false);
  756. $pdf->setPrintFooter(false);
  757. }
  758. $pdf->SetFont(pdf_getPDFFont($outputlangs));
  759. if (!empty($conf->global->MAIN_DISABLE_PDF_COMPRESSION)) {
  760. $pdf->SetCompression(false);
  761. }
  762. // Add all others
  763. foreach ($files as $file) {
  764. // Charge un document PDF depuis un fichier.
  765. $pagecount = $pdf->setSourceFile($file);
  766. for ($i = 1; $i <= $pagecount; $i++) {
  767. $tplidx = $pdf->importPage($i);
  768. $s = $pdf->getTemplatesize($tplidx);
  769. $pdf->AddPage($s['h'] > $s['w'] ? 'P' : 'L');
  770. $pdf->useTemplate($tplidx);
  771. }
  772. }
  773. // Create output dir if not exists
  774. dol_mkdir($diroutputmassaction);
  775. // Defined name of merged file
  776. $filename = strtolower(dol_sanitizeFileName($langs->transnoentities($objectlabel)));
  777. $filename = preg_replace('/\s/', '_', $filename);
  778. // Save merged file
  779. if (in_array($objecttmp->element, array('facture', 'facture_fournisseur')) && $search_status == Facture::STATUS_VALIDATED) {
  780. if ($option == 'late') {
  781. $filename .= '_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid"))).'_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Late")));
  782. } else {
  783. $filename .= '_'.strtolower(dol_sanitizeFileName($langs->transnoentities("Unpaid")));
  784. }
  785. }
  786. if ($year) {
  787. $filename .= '_'.$year;
  788. }
  789. if ($month) {
  790. $filename .= '_'.$month;
  791. }
  792. if ($pagecount) {
  793. $now = dol_now();
  794. $file = $diroutputmassaction.'/'.$filename.'_'.dol_print_date($now, 'dayhourlog').'.pdf';
  795. $pdf->Output($file, 'F');
  796. if (!empty($conf->global->MAIN_UMASK)) {
  797. @chmod($file, octdec($conf->global->MAIN_UMASK));
  798. }
  799. $langs->load("exports");
  800. setEventMessages($langs->trans('FileSuccessfullyBuilt', $filename.'_'.dol_print_date($now, 'dayhourlog')), null, 'mesgs');
  801. } else {
  802. setEventMessages($langs->trans('NoPDFAvailableForDocGenAmongChecked'), null, 'errors');
  803. }
  804. }
  805. }
  806. // Remove a file from massaction area
  807. if ($action == 'remove_file') {
  808. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  809. $langs->load("other");
  810. $upload_dir = $diroutputmassaction;
  811. $file = $upload_dir.'/'.GETPOST('file');
  812. $ret = dol_delete_file($file);
  813. if ($ret) {
  814. setEventMessages($langs->trans("FileWasRemoved", GETPOST('file')), null, 'mesgs');
  815. } else {
  816. setEventMessages($langs->trans("ErrorFailToDeleteFile", GETPOST('file')), null, 'errors');
  817. }
  818. $action = '';
  819. }
  820. // Validate records
  821. if (!$error && $massaction == 'validate' && $permissiontoadd) {
  822. $objecttmp = new $objectclass($db);
  823. if (($objecttmp->element == 'facture' || $objecttmp->element == 'invoice') && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_BILL)) {
  824. $langs->load("errors");
  825. setEventMessages($langs->trans('ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'), null, 'errors');
  826. $error++;
  827. }
  828. if ($objecttmp->element == 'invoice_supplier' && !empty($conf->stock->enabled) && !empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL)) {
  829. $langs->load("errors");
  830. setEventMessages($langs->trans('ErrorMassValidationNotAllowedWhenStockIncreaseOnAction'), null, 'errors');
  831. $error++;
  832. }
  833. if ($objecttmp->element == 'facture') {
  834. if (!empty($toselect) && !empty($conf->global->INVOICE_CHECK_POSTERIOR_DATE)) {
  835. // order $toselect by date
  836. $sql = "SELECT rowid FROM ".MAIN_DB_PREFIX."facture";
  837. $sql .= " WHERE rowid IN (".$db->sanitize(implode(",", $toselect)).")";
  838. $sql .= " ORDER BY datef";
  839. $resql = $db->query($sql);
  840. if ($resql) {
  841. $toselectnew = [];
  842. while ( !empty($arr = $db->fetch_row($resql))) {
  843. $toselectnew[] = $arr[0];
  844. }
  845. $toselect = (empty($toselectnew)) ? $toselect : $toselectnew;
  846. } else {
  847. dol_print_error($db);
  848. $error++;
  849. }
  850. }
  851. }
  852. if (!$error) {
  853. $db->begin();
  854. $nbok = 0;
  855. foreach ($toselect as $toselectid) {
  856. $result = $objecttmp->fetch($toselectid);
  857. if ($result > 0) {
  858. $result = $objecttmp->validate($user);
  859. if ($result == 0) {
  860. $langs->load("errors");
  861. setEventMessages($langs->trans("ErrorObjectMustHaveStatusDraftToBeValidated", $objecttmp->ref), null, 'errors');
  862. $error++;
  863. break;
  864. } elseif ($result < 0) {
  865. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  866. $error++;
  867. break;
  868. } else {
  869. // validate() rename pdf but do not regenerate
  870. // Define output language
  871. if (empty($conf->global->MAIN_DISABLE_PDF_AUTOUPDATE)) {
  872. $outputlangs = $langs;
  873. $newlang = '';
  874. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  875. $newlang = GETPOST('lang_id', 'aZ09');
  876. }
  877. if ($conf->global->MAIN_MULTILANGS && empty($newlang)) {
  878. $newlang = $objecttmp->thirdparty->default_lang;
  879. }
  880. if (!empty($newlang)) {
  881. $outputlangs = new Translate("", $conf);
  882. $outputlangs->setDefaultLang($newlang);
  883. $outputlangs->load('products');
  884. }
  885. $model = $objecttmp->model_pdf;
  886. $ret = $objecttmp->fetch($objecttmp->id); // Reload to get new records
  887. // To be sure vars is defined
  888. $hidedetails = !empty($hidedetails) ? $hidedetails : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
  889. $hidedesc = !empty($hidedesc) ? $hidedesc : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
  890. $hideref = !empty($hideref) ? $hideref : (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
  891. $moreparams = !empty($moreparams) ? $moreparams : null;
  892. $result = $objecttmp->generateDocument($model, $outputlangs, $hidedetails, $hidedesc, $hideref);
  893. if ($result < 0) {
  894. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  895. }
  896. }
  897. $nbok++;
  898. }
  899. } else {
  900. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  901. $error++;
  902. break;
  903. }
  904. }
  905. if (!$error) {
  906. if ($nbok > 1) {
  907. setEventMessages($langs->trans("RecordsModified", $nbok), null, 'mesgs');
  908. } else {
  909. setEventMessages($langs->trans("RecordModifiedSuccessfully"), null, 'mesgs');
  910. }
  911. $db->commit();
  912. } else {
  913. $db->rollback();
  914. }
  915. //var_dump($listofobjectthirdparties);exit;
  916. }
  917. }
  918. //var_dump($_POST);var_dump($massaction);exit;
  919. // Delete record from mass action (massaction = 'delete' for direct delete, action/confirm='delete'/'yes' with a confirmation step before)
  920. if (!$error && ($massaction == 'delete' || ($action == 'delete' && $confirm == 'yes')) && $permissiontodelete) {
  921. $db->begin();
  922. $objecttmp = new $objectclass($db);
  923. $nbok = 0;
  924. $TMsg = array();
  925. foreach ($toselect as $toselectid) {
  926. $result = $objecttmp->fetch($toselectid);
  927. if ($result > 0) {
  928. // Refuse deletion for some objects/status
  929. if ($objectclass == 'Facture' && empty($conf->global->INVOICE_CAN_ALWAYS_BE_REMOVED) && $objecttmp->status != Facture::STATUS_DRAFT) {
  930. $langs->load("errors");
  931. $nbignored++;
  932. $TMsg[] = '<div class="error">'.$langs->trans('ErrorOnlyDraftStatusCanBeDeletedInMassAction', $objecttmp->ref).'</div><br>';
  933. continue;
  934. }
  935. if (method_exists($objecttmp, 'is_erasable') && $objecttmp->is_erasable() <= 0) {
  936. $langs->load("errors");
  937. $nbignored++;
  938. $TMsg[] = '<div class="error">'.$langs->trans('ErrorRecordHasChildren').' '.$objecttmp->ref.'</div><br>';
  939. continue;
  940. }
  941. if ($objectclass == 'Holiday' && ! in_array($objecttmp->statut, array(Holiday::STATUS_DRAFT, Holiday::STATUS_CANCELED, Holiday::STATUS_REFUSED))) {
  942. $langs->load("errors");
  943. $nbignored++;
  944. $TMsg[] = '<div class="error">'.$langs->trans('ErrorLeaveRequestMustBeDraftCanceledOrRefusedToBeDeleted', $objecttmp->ref).'</div><br>';
  945. continue;
  946. }
  947. if ($objectclass == "Task" && $objecttmp->hasChildren() > 0) {
  948. $sql = "UPDATE ".MAIN_DB_PREFIX."projet_task SET fk_task_parent = 0 WHERE fk_task_parent = ".((int) $objecttmp->id);
  949. $res = $db->query($sql);
  950. if (!$res) {
  951. setEventMessage('ErrorRecordParentingNotModified', 'errors');
  952. $error++;
  953. }
  954. }
  955. if (in_array($objecttmp->element, array('societe', 'member'))) {
  956. $result = $objecttmp->delete($objecttmp->id, $user, 1);
  957. } elseif (in_array($objecttmp->element, array('action'))) {
  958. $result = $objecttmp->delete();
  959. } else {
  960. $result = $objecttmp->delete($user);
  961. }
  962. if (empty($result)) { // if delete returns 0, there is at least one object linked
  963. $TMsg = array_merge($objecttmp->errors, $TMsg);
  964. } elseif ($result < 0) { // if delete returns is < 0, there is an error, we break and rollback later
  965. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  966. $error++;
  967. break;
  968. } else {
  969. $nbok++;
  970. }
  971. } else {
  972. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  973. $error++;
  974. break;
  975. }
  976. }
  977. if (empty($error)) {
  978. // Message for elements well deleted
  979. if ($nbok > 1) {
  980. setEventMessages($langs->trans("RecordsDeleted", $nbok), null, 'mesgs');
  981. } elseif ($nbok > 0) {
  982. setEventMessages($langs->trans("RecordDeleted", $nbok), null, 'mesgs');
  983. } else {
  984. setEventMessages($langs->trans("NoRecordDeleted"), null, 'mesgs');
  985. }
  986. // Message for elements which can't be deleted
  987. if (!empty($TMsg)) {
  988. sort($TMsg);
  989. setEventMessages('', array_unique($TMsg), 'warnings');
  990. }
  991. $db->commit();
  992. } else {
  993. $db->rollback();
  994. }
  995. //var_dump($listofobjectthirdparties);exit;
  996. }
  997. // Generate document foreach object according to model linked to object
  998. // @todo : propose model selection
  999. if (!$error && $massaction == 'generate_doc' && $permissiontoread) {
  1000. $db->begin();
  1001. $objecttmp = new $objectclass($db);
  1002. $nbok = 0;
  1003. foreach ($toselect as $toselectid) {
  1004. $result = $objecttmp->fetch($toselectid);
  1005. if ($result > 0) {
  1006. $outputlangs = $langs;
  1007. $newlang = '';
  1008. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && GETPOST('lang_id', 'aZ09')) {
  1009. $newlang = GETPOST('lang_id', 'aZ09');
  1010. }
  1011. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($objecttmp->thirdparty->default_lang)) {
  1012. $newlang = $objecttmp->thirdparty->default_lang; // for proposal, order, invoice, ...
  1013. }
  1014. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && isset($objecttmp->default_lang)) {
  1015. $newlang = $objecttmp->default_lang; // for thirdparty
  1016. }
  1017. if ($conf->global->MAIN_MULTILANGS && empty($newlang) && empty($objecttmp->thirdparty)) { //load lang from thirdparty
  1018. $objecttmp->fetch_thirdparty();
  1019. $newlang = $objecttmp->thirdparty->default_lang; // for proposal, order, invoice, ...
  1020. }
  1021. if (!empty($newlang)) {
  1022. $outputlangs = new Translate("", $conf);
  1023. $outputlangs->setDefaultLang($newlang);
  1024. }
  1025. // To be sure vars is defined
  1026. if (empty($hidedetails)) {
  1027. $hidedetails = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DETAILS) ? 1 : 0);
  1028. }
  1029. if (empty($hidedesc)) {
  1030. $hidedesc = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_DESC) ? 1 : 0);
  1031. }
  1032. if (empty($hideref)) {
  1033. $hideref = (!empty($conf->global->MAIN_GENERATE_DOCUMENTS_HIDE_REF) ? 1 : 0);
  1034. }
  1035. if (empty($moreparams)) {
  1036. $moreparams = null;
  1037. }
  1038. $result = $objecttmp->generateDocument($objecttmp->model_pdf, $outputlangs, $hidedetails, $hidedesc, $hideref, $moreparams);
  1039. if ($result <= 0) {
  1040. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1041. $error++;
  1042. break;
  1043. } else {
  1044. $nbok++;
  1045. }
  1046. } else {
  1047. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1048. $error++;
  1049. break;
  1050. }
  1051. }
  1052. if (!$error) {
  1053. if ($nbok > 1) {
  1054. setEventMessages($langs->trans("RecordsGenerated", $nbok), null, 'mesgs');
  1055. } else {
  1056. setEventMessages($langs->trans("RecordGenerated", $nbok), null, 'mesgs');
  1057. }
  1058. $db->commit();
  1059. } else {
  1060. $db->rollback();
  1061. }
  1062. }
  1063. if (!$error && ($action == 'affecttag' && $confirm == 'yes') && $permissiontoadd) {
  1064. $db->begin();
  1065. $affecttag_type=GETPOST('affecttag_type', 'alpha');
  1066. if (!empty($affecttag_type)) {
  1067. $affecttag_type_array=explode(',', $affecttag_type);
  1068. } else {
  1069. setEventMessage('CategTypeNotFound', 'errors');
  1070. }
  1071. if (!empty($affecttag_type_array)) {
  1072. //check if tag type submited exists into Tag Map categorie class
  1073. require_once DOL_DOCUMENT_ROOT.'/categories/class/categorie.class.php';
  1074. $categ = new Categorie($db);
  1075. $to_affecttag_type_array=array();
  1076. $categ_type_array=$categ->getMapList();
  1077. foreach ($categ_type_array as $categdef) {
  1078. if (in_array($categdef['code'], $affecttag_type_array)) {
  1079. $to_affecttag_type_array[] = $categdef['code'];
  1080. }
  1081. }
  1082. //For each valid categ type set common categ
  1083. $nbok = 0;
  1084. if (!empty($to_affecttag_type_array)) {
  1085. foreach ($to_affecttag_type_array as $categ_type) {
  1086. $contcats = GETPOST('contcats_' . $categ_type, 'array');
  1087. //var_dump($toselect);exit;
  1088. foreach ($toselect as $toselectid) {
  1089. $result = $object->fetch($toselectid);
  1090. //var_dump($contcats);exit;
  1091. if ($result > 0) {
  1092. $result = $object->setCategoriesCommon($contcats, $categ_type, false);
  1093. if ($result > 0) {
  1094. $nbok++;
  1095. } else {
  1096. setEventMessages($object->error, $object->errors, 'errors');
  1097. }
  1098. } else {
  1099. setEventMessages($object->error, $object->errors, 'errors');
  1100. $error++;
  1101. break;
  1102. }
  1103. }
  1104. }
  1105. }
  1106. }
  1107. if (!$error) {
  1108. if ($nbok > 1) {
  1109. setEventMessages($langs->trans("RecordsModified", $nbok), null);
  1110. } else {
  1111. setEventMessages($langs->trans("RecordsModified", $nbok), null);
  1112. }
  1113. $db->commit();
  1114. $toselect=array();
  1115. } else {
  1116. $db->rollback();
  1117. }
  1118. }
  1119. if (!$error && ($massaction == 'enable' || ($action == 'enable' && $confirm == 'yes')) && $permissiontoadd) {
  1120. $db->begin();
  1121. $objecttmp = new $objectclass($db);
  1122. $nbok = 0;
  1123. foreach ($toselect as $toselectid) {
  1124. $result = $objecttmp->fetch($toselectid);
  1125. if ($result>0) {
  1126. if (in_array($objecttmp->element, array('societe'))) {
  1127. $result =$objecttmp->setStatut(1);
  1128. }
  1129. if ($result <= 0) {
  1130. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1131. $error++;
  1132. break;
  1133. } else {
  1134. $nbok++;
  1135. }
  1136. } else {
  1137. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1138. $error++;
  1139. break;
  1140. }
  1141. }
  1142. if (!$error) {
  1143. if ($nbok > 1) {
  1144. setEventMessages($langs->trans("RecordsEnabled", $nbok), null, 'mesgs');
  1145. } else {
  1146. setEventMessages($langs->trans("RecordEnabled"), null, 'mesgs');
  1147. }
  1148. $db->commit();
  1149. } else {
  1150. $db->rollback();
  1151. }
  1152. }
  1153. if (!$error && ($massaction == 'disable' || ($action == 'disable' && $confirm == 'yes')) && $permissiontoadd) {
  1154. $db->begin();
  1155. $objecttmp = new $objectclass($db);
  1156. $nbok = 0;
  1157. foreach ($toselect as $toselectid) {
  1158. $result = $objecttmp->fetch($toselectid);
  1159. if ($result>0) {
  1160. if (in_array($objecttmp->element, array('societe'))) {
  1161. $result =$objecttmp->setStatut(0);
  1162. }
  1163. if ($result <= 0) {
  1164. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1165. $error++;
  1166. break;
  1167. } else {
  1168. $nbok++;
  1169. }
  1170. } else {
  1171. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1172. $error++;
  1173. break;
  1174. }
  1175. }
  1176. if (!$error) {
  1177. if ($nbok > 1) {
  1178. setEventMessages($langs->trans("RecordsDisabled", $nbok), null, 'mesgs');
  1179. } else {
  1180. setEventMessages($langs->trans("RecordDisabled"), null, 'mesgs');
  1181. }
  1182. $db->commit();
  1183. } else {
  1184. $db->rollback();
  1185. }
  1186. }
  1187. if (!$error && $action == 'confirm_edit_value_extrafields' && $confirm == 'yes' && $permissiontoadd) {
  1188. $db->begin();
  1189. $objecttmp = new $objectclass($db);
  1190. $e = new ExtraFields($db);// fetch optionals attributes and labels
  1191. $e->fetch_name_optionals_label($objecttmp->table_element);
  1192. $nbok = 0;
  1193. $extrafieldKeyToUpdate = GETPOST('extrafield-key-to-update');
  1194. foreach ($toselect as $toselectid) {
  1195. /** @var CommonObject $objecttmp */
  1196. $objecttmp = new $objectclass($db); // to avoid ghost data
  1197. $result = $objecttmp->fetch($toselectid);
  1198. if ($result>0) {
  1199. // Fill array 'array_options' with data from add form
  1200. $ret = $e->setOptionalsFromPost(null, $objecttmp, $extrafieldKeyToUpdate);
  1201. if ($ret > 0) {
  1202. $objecttmp->insertExtraFields();
  1203. } else {
  1204. $error++;
  1205. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1206. }
  1207. } else {
  1208. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1209. $error++;
  1210. break;
  1211. }
  1212. }
  1213. if (!$error) {
  1214. if ($nbok > 1) {
  1215. setEventMessages($langs->trans("RecordsDisabled", $nbok), null, 'mesgs');
  1216. } else {
  1217. setEventMessages($langs->trans("save"), null, 'mesgs');
  1218. }
  1219. $db->commit();
  1220. } else {
  1221. $db->rollback();
  1222. }
  1223. }
  1224. if (!$error && ($massaction == 'affectcommercial' || ($action == 'affectcommercial' && $confirm == 'yes')) && $permissiontoadd) {
  1225. $db->begin();
  1226. $objecttmp = new $objectclass($db);
  1227. $nbok = 0;
  1228. foreach ($toselect as $toselectid) {
  1229. $result = $objecttmp->fetch($toselectid);
  1230. if ($result>0) {
  1231. if (in_array($objecttmp->element, array('societe'))) {
  1232. $result = $objecttmp->setSalesRep(GETPOST("commercial", "alpha"));
  1233. }
  1234. if ($result <= 0) {
  1235. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1236. $error++;
  1237. break;
  1238. } else {
  1239. $nbok++;
  1240. }
  1241. } else {
  1242. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1243. $error++;
  1244. break;
  1245. }
  1246. }
  1247. if (!$error) {
  1248. if ($nbok > 1) {
  1249. setEventMessages($langs->trans("CommercialsAffected", $nbok), null, 'mesgs');
  1250. } else {
  1251. setEventMessages($langs->trans("CommercialAffected"), null, 'mesgs');
  1252. }
  1253. $db->commit();
  1254. } else {
  1255. $db->rollback();
  1256. }
  1257. }
  1258. // Approve for leave only
  1259. if (!$error && ($massaction == 'approveleave' || ($action == 'approveleave' && $confirm == 'yes')) && $permissiontoapprove) {
  1260. $db->begin();
  1261. $objecttmp = new $objectclass($db);
  1262. $nbok = 0;
  1263. foreach ($toselect as $toselectid) {
  1264. $result = $objecttmp->fetch($toselectid);
  1265. if ($result > 0) {
  1266. if ($objecttmp->statut != Holiday::STATUS_VALIDATED) {
  1267. setEventMessages($langs->trans('StatusOfRefMustBe', $objecttmp->ref, $langs->transnoentitiesnoconv('Validated')), null, 'warnings');
  1268. continue;
  1269. }
  1270. if ($user->id == $objecttmp->fk_validator) {
  1271. $objecttmp->oldcopy = dol_clone($objecttmp);
  1272. $objecttmp->date_valid = dol_now();
  1273. $objecttmp->fk_user_valid = $user->id;
  1274. $objecttmp->statut = Holiday::STATUS_APPROVED;
  1275. $verif = $objecttmp->approve($user);
  1276. if ($verif <= 0) {
  1277. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1278. $error++;
  1279. }
  1280. // If no SQL error, we redirect to the request form
  1281. if (!$error) {
  1282. // Calculcate number of days consumed
  1283. $nbopenedday = num_open_day($objecttmp->date_debut_gmt, $objecttmp->date_fin_gmt, 0, 1, $objecttmp->halfday);
  1284. $soldeActuel = $objecttmp->getCpforUser($objecttmp->fk_user, $objecttmp->fk_type);
  1285. $newSolde = ($soldeActuel - $nbopenedday);
  1286. // The modification is added to the LOG
  1287. $result = $objecttmp->addLogCP($user->id, $objecttmp->fk_user, $langs->transnoentitiesnoconv("Holidays"), $newSolde, $objecttmp->fk_type);
  1288. if ($result < 0) {
  1289. $error++;
  1290. setEventMessages(null, $objecttmp->errors, 'errors');
  1291. }
  1292. // Update balance
  1293. $result = $objecttmp->updateSoldeCP($objecttmp->fk_user, $newSolde, $objecttmp->fk_type);
  1294. if ($result < 0) {
  1295. $error++;
  1296. setEventMessages(null, $objecttmp->errors, 'errors');
  1297. }
  1298. }
  1299. if (!$error) {
  1300. // To
  1301. $destinataire = new User($db);
  1302. $destinataire->fetch($objecttmp->fk_user);
  1303. $emailTo = $destinataire->email;
  1304. if (!$emailTo) {
  1305. dol_syslog("User that request leave has no email, so we redirect directly to finished page without sending email");
  1306. } else {
  1307. // From
  1308. $expediteur = new User($db);
  1309. $expediteur->fetch($objecttmp->fk_validator);
  1310. //$emailFrom = $expediteur->email; Email of user can be an email into another company. Sending will fails, we must use the generic email.
  1311. $emailFrom = $conf->global->MAIN_MAIL_EMAIL_FROM;
  1312. // Subject
  1313. $societeName = $conf->global->MAIN_INFO_SOCIETE_NOM;
  1314. if (!empty($conf->global->MAIN_APPLICATION_TITLE)) {
  1315. $societeName = $conf->global->MAIN_APPLICATION_TITLE;
  1316. }
  1317. $subject = $societeName." - ".$langs->transnoentitiesnoconv("HolidaysValidated");
  1318. // Content
  1319. $message = $langs->transnoentitiesnoconv("Hello")." ".$destinataire->firstname.",\n";
  1320. $message .= "\n";
  1321. $message .= $langs->transnoentities("HolidaysValidatedBody", dol_print_date($objecttmp->date_debut, 'day'), dol_print_date($objecttmp->date_fin, 'day'))."\n";
  1322. $message .= "- ".$langs->transnoentitiesnoconv("ValidatedBy")." : ".dolGetFirstLastname($expediteur->firstname, $expediteur->lastname)."\n";
  1323. $message .= "- ".$langs->transnoentitiesnoconv("Link")." : ".$dolibarr_main_url_root."/holiday/card.php?id=".$objecttmp->id."\n\n";
  1324. $message .= "\n";
  1325. $trackid = 'leav'.$objecttmp->id;
  1326. require_once DOL_DOCUMENT_ROOT.'/core/class/CMailFile.class.php';
  1327. $mail = new CMailFile($subject, $emailTo, $emailFrom, $message, array(), array(), array(), '', '', 0, 0, '', '', $trackid);
  1328. // Sending email
  1329. $result = $mail->sendfile();
  1330. if (!$result) {
  1331. setEventMessages($mail->error, $mail->errors, 'warnings'); // Show error, but do no make rollback, so $error is not set to 1
  1332. $action = '';
  1333. }
  1334. }
  1335. }
  1336. } else {
  1337. $langs->load("errors");
  1338. setEventMessages($langs->trans('ErrorNotApproverForHoliday', $objecttmp->ref), null, 'errors');
  1339. }
  1340. } else {
  1341. setEventMessages($objecttmp->error, $objecttmp->errors, 'errors');
  1342. $error++;
  1343. break;
  1344. }
  1345. }
  1346. if (!$error) {
  1347. if ($nbok > 1) {
  1348. setEventMessages($langs->trans("RecordsApproved", $nbok), null, 'mesgs');
  1349. } elseif ($nbok == 1) {
  1350. setEventMessages($langs->trans("RecordAproved"), null, 'mesgs');
  1351. }
  1352. $db->commit();
  1353. } else {
  1354. $db->rollback();
  1355. }
  1356. }
  1357. $parameters['toselect'] = $toselect;
  1358. $parameters['uploaddir'] = $uploaddir;
  1359. $parameters['massaction'] = $massaction;
  1360. $parameters['diroutputmassaction'] = isset($diroutputmassaction) ? $diroutputmassaction : null;
  1361. $reshook = $hookmanager->executeHooks('doMassActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  1362. if ($reshook < 0) {
  1363. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  1364. }