Skip to content

Commit 723505e

Browse files
committed
feat: Invio mail massivo da Azioni di gruppo, close #1713
1 parent 7522ea6 commit 723505e

1 file changed

Lines changed: 79 additions & 0 deletions

File tree

modules/ordini/bulk.php

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
use Modules\Fatture\Tipo as TipoFattura;
2929
use Modules\Ordini\Ordine;
3030
use Modules\Ordini\Tipo;
31+
use Modules\Emails\Template;
32+
use Models\OperationLog;
33+
use Modules\Emails\Mail;
34+
use Notifications\EmailNotification;
3135

3236
// Segmenti
3337
$id_modulo_fatture = Module::where('name', 'Fatture di vendita')->first()->id;
@@ -252,6 +256,70 @@
252256

253257
flash()->info(tr('Ordini duplicati correttamente!'));
254258

259+
break;
260+
261+
case 'send_mail':
262+
$template = Template::find(post('id_template'));
263+
264+
$list = [];
265+
foreach ($id_records as $id) {
266+
$ordine = Ordine::find($id);
267+
$id_anagrafica = $ordine->idanagrafica;
268+
269+
// Selezione destinatari e invio mail
270+
if (!empty($template)) {
271+
$creata_mail = false;
272+
$emails = [];
273+
274+
// Aggiungo email anagrafica
275+
if (!empty($ordine->anagrafica->email)) {
276+
$emails[] = $ordine->anagrafica->email;
277+
$mail = Mail::build(auth_osm()->getUser(), $template, $id);
278+
$mail->addReceiver($ordine->anagrafica->email);
279+
$creata_mail = true;
280+
}
281+
282+
// Aggiungo email referenti in base alla mansione impostata nel template
283+
$mansioni = $dbo->select('em_mansioni_template', 'idmansione', [], ['id_template' => $template->id]);
284+
foreach ($mansioni as $mansione) {
285+
$referenti = $dbo->table('an_referenti')->where('idmansione', $mansione['idmansione'])->where('idanagrafica', $id_anagrafica)->where('email', '!=', '')->get();
286+
if (!$referenti->isEmpty() && $creata_mail == false) {
287+
$mail = Mail::build(auth_osm()->getUser(), $template, $id);
288+
$creata_mail = true;
289+
}
290+
291+
foreach ($referenti as $referente) {
292+
if (!in_array($referente->email, $emails)) {
293+
$emails[] = $referente->email;
294+
$mail->addReceiver($referente->email);
295+
}
296+
}
297+
}
298+
if ($creata_mail == true) {
299+
$mail->save();
300+
// Invio mail istantaneo
301+
$email = EmailNotification::build($mail);
302+
$email_success = $email->send();
303+
304+
if ($email_success) {
305+
OperationLog::setInfo('id_email', $mail->id);
306+
OperationLog::setInfo('id_module', $id_module);
307+
OperationLog::setInfo('id_record', $mail->id_record);
308+
OperationLog::build('send-email');
309+
array_push($list, $ordine->codice);
310+
} else {
311+
$mail->delete();
312+
}
313+
}
314+
}
315+
}
316+
317+
if ($list) {
318+
flash()->info(tr('Mail inviata per gli ordini _LIST_ !', [
319+
'_LIST_' => implode(',', $list),
320+
]));
321+
}
322+
255323
break;
256324
}
257325

@@ -317,4 +385,15 @@
317385
}
318386
}
319387

388+
$operations['send_mail'] = [
389+
'text' => '<span><i class="fa fa-envelope"></i> '.tr('Invia mail').'</span>',
390+
'data' => [
391+
'title' => tr('Inviare mail?'),
392+
'msg' => tr('Per ciascun ordine selezionato, verrà inviata una mail').'<br><br>
393+
{[ "type": "select", "label": "'.tr('Template').'", "name": "id_template", "required": "1", "values": "query=SELECT `em_templates`.`id`, `em_templates_lang`.`title` AS descrizione FROM `em_templates` LEFT JOIN `em_templates_lang` ON (`em_templates`.`id` = `em_templates_lang`.`id_record` AND `em_templates_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') WHERE `id_module`='.prepare($id_module).' AND `deleted_at` IS NULL;" ]}',
394+
'button' => tr('Invia'),
395+
'class' => 'btn btn-lg btn-warning',
396+
],
397+
];
398+
320399
return $operations;

0 commit comments

Comments
 (0)