Skip to content

Commit 1d5a609

Browse files
committed
feat: invio pdf fatture da bulk
1 parent 88ecc65 commit 1d5a609

1 file changed

Lines changed: 133 additions & 0 deletions

File tree

modules/fatture/bulk.php

Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@
3030
use Plugins\ExportFE\FatturaElettronica;
3131
use Plugins\ExportFE\Interaction;
3232
use Plugins\ReceiptFE\Ricevuta;
33+
use Modules\Emails\Template;
34+
use Notifications\EmailNotification;
35+
use Models\OperationLog;
3336
use Util\Zip;
3437

3538
$anagrafica_azienda = Anagrafica::find(setting('Azienda predefinita'));
@@ -575,6 +578,125 @@
575578

576579
break;
577580

581+
case 'send-invoices':
582+
$list = [];
583+
$user = Auth::user();
584+
$fatture = Fattura::vendita()
585+
->whereIn('id', $id_records)
586+
->orderBy('data')
587+
->get();
588+
589+
// Template email predefinito
590+
$template = Template::where('id_module', $id_module)
591+
->where('predefined', 1)
592+
->first();
593+
594+
if (empty($template)) {
595+
flash()->error(tr('Nessun template email predefinito trovato per il modulo Fatture!'));
596+
break;
597+
}
598+
599+
$module = $template->module;
600+
$success_count = 0;
601+
$failed_count = 0;
602+
$failed_emails = [];
603+
604+
foreach ($fatture as $fattura) {
605+
$mail = Modules\Emails\Mail::build($user, $template, $fattura->id);
606+
607+
// Destinatari
608+
$emails = [];
609+
if (!empty($fattura->anagrafica->email)) {
610+
$emails[] = $fattura->anagrafica->email;
611+
}
612+
613+
// Aggiungo email referenti in base alla mansione impostata nel template
614+
$mansioni = $dbo->select('em_mansioni_template', ['idmansione'], [], ['id_template' => $template->id]);
615+
foreach ($mansioni as $mansione) {
616+
$referenti = $dbo->table('an_referenti')
617+
->where('idmansione', $mansione['idmansione'])
618+
->where('idanagrafica', $fattura->idanagrafica)
619+
->where('email', '!=', '')
620+
->get();
621+
622+
foreach ($referenti as $referente) {
623+
if (!in_array($referente->email, $emails)) {
624+
$emails[] = $referente->email;
625+
}
626+
}
627+
}
628+
629+
// Se non ci sono destinatari, salta questa fattura
630+
if (empty($emails)) {
631+
$failed_count++;
632+
$failed_emails[] = $fattura->numero_esterno;
633+
continue;
634+
}
635+
636+
// Aggiungi tutti i destinatari all'email
637+
foreach ($emails as $receiver) {
638+
$mail->addReceiver($receiver);
639+
}
640+
641+
// Contenuti
642+
$placeholder_options = ['is_pec' => intval($mail->account->pec ?? 0)];
643+
$mail->content = $template->getTranslation('body');
644+
$mail->subject = $template->getTranslation('subject');
645+
646+
// Conferma di lettura
647+
$mail->read_notify = $template->read_notify;
648+
649+
// Prima rimuoviamo eventuali stampe predefinite per evitare duplicati
650+
$mail->resetPrints();
651+
652+
// Stampe da allegare
653+
$selected_prints = $dbo->fetchArray('SELECT id_print FROM em_print_template WHERE id_template = '.prepare($template['id']));
654+
$prints = array_column($selected_prints, 'id_print');
655+
656+
// Aggiungi le stampe selezionate come allegati SOLO per questa fattura
657+
foreach ($prints as $print_id) {
658+
// Passa l'ID della fattura corrente per allegare solo questa fattura
659+
$mail->addPrint($print_id, $fattura->id);
660+
}
661+
662+
// Salvataggio email nella coda di invio
663+
$mail->save();
664+
665+
// Invio mail istantaneo
666+
$email = EmailNotification::build($mail);
667+
$email_success = $email->send();
668+
669+
if ($email_success) {
670+
OperationLog::setInfo('id_email', $mail->id);
671+
$list[] = $fattura->numero_esterno;
672+
$success_count++;
673+
} else {
674+
$mail->delete();
675+
$failed_count++;
676+
$failed_emails[] = $fattura->numero_esterno;
677+
}
678+
}
679+
680+
// Mostra messaggi di riepilogo
681+
if ($success_count > 0) {
682+
flash()->info(tr('Inviate con successo _COUNT_ email per le fatture _LIST_', [
683+
'_COUNT_' => $success_count,
684+
'_LIST_' => implode(', ', $list),
685+
]));
686+
}
687+
688+
if ($failed_count > 0) {
689+
flash()->error(tr('Impossibile inviare _COUNT_ email per le fatture _LIST_', [
690+
'_COUNT_' => $failed_count,
691+
'_LIST_' => implode(', ', $failed_emails),
692+
]));
693+
694+
// Aggiungi suggerimento per verificare gli indirizzi email
695+
flash()->warning(tr('Verificare che gli indirizzi email dei destinatari siano corretti e che i domini esistano.'));
696+
}
697+
698+
break;
699+
578700
case 'verify_notifications':
579701
foreach ($id_records as $id) {
580702
$documento = Fattura::find($id);
@@ -767,6 +889,17 @@
767889
];
768890
}
769891

892+
$operations['send-invoices'] = [
893+
'text' => '<span><i class="fa fa-envelope"></i> '.tr('Invia fatture').'</span>',
894+
'data' => [
895+
'title' => tr('Invia fatture'),
896+
'msg' => tr('Vuoi inviare le fatture PDF ai contatti email predefiniti in anagrafica?'),
897+
'button' => tr('Procedi'),
898+
'class' => 'btn btn-lg btn-warning',
899+
],
900+
];
901+
902+
770903
$operations['registrazione_contabile'] = [
771904
'text' => '<span><i class="fa fa-calculator"></i> '.tr('Registrazione contabile').'</span>',
772905
'data' => [

0 commit comments

Comments
 (0)