diff --git a/modules/backups/src/BackupHook.php b/modules/backups/src/BackupHook.php index 1313fa066..bac7f9a34 100644 --- a/modules/backups/src/BackupHook.php +++ b/modules/backups/src/BackupHook.php @@ -26,8 +26,8 @@ class BackupHook extends Manager public function response() { - $show = boolval(setting('Backup automatico')); - $message = $show && !Backup::isDailyComplete() ? tr('Backup in corso...') : tr('Backup automatico completato!'); + $show = boolval(setting('Backup automatico')) && !Backup::isDailyComplete(); + $message = $show ? tr('Backup in corso...') : tr('Backup automatico completato!'); return [ 'icon' => 'fa fa-file-o text-success', diff --git a/modules/emails/src/EmailHook.php b/modules/emails/src/EmailHook.php index 8303a4414..6478a5049 100644 --- a/modules/emails/src/EmailHook.php +++ b/modules/emails/src/EmailHook.php @@ -80,8 +80,10 @@ class EmailHook extends Manager $current = Mail::whereDate('sent_at', '>', $yesterday) ->where('created_by', $user->id) ->count(); - $total = Mail::whereDate('sent_at', '>', $yesterday) - ->orWhereNull('sent_at') + $total = Mail::where(function ($query) use ($yesterday) { + $query->whereDate('sent_at', '>', $yesterday) + ->orWhereNull('sent_at'); + }) ->where('created_by', $user->id) ->count(); @@ -91,7 +93,7 @@ class EmailHook extends Manager return [ 'icon' => 'fa fa-envelope text-info', 'message' => $message, - 'show' => true, + 'show' => ($total != $current), 'progress' => [ 'current' => $current, 'total' => $total, diff --git a/modules/fatture/bulk.php b/modules/fatture/bulk.php index 345e32485..ad1bac9a0 100644 --- a/modules/fatture/bulk.php +++ b/modules/fatture/bulk.php @@ -4,6 +4,7 @@ include_once __DIR__.'/../../core.php'; use Modules\Fatture\Fattura; use Plugins\ExportFE\FatturaElettronica; +use Plugins\ExportFE\Interaction; use Util\Zip; switch (post('op')) { @@ -89,6 +90,23 @@ switch (post('op')) { break; + case 'hook-send': + foreach ($id_records as $id) { + $fattura = Fattura::find($id); + + $fe = new \Plugins\ExportFE\FatturaElettronica($fattura->id); + if ($fe->isGenerated() && $fattura->codice_stato_fe == 'GEN') { + $fattura->codice_stato_fe = 'QUEUE'; + $fattura->data_stato_fe = date('Y-m-d H:i:s'); + $fattura->hook_send = true; + $fattura->save(); + } + } + + flash()->info(tr('Le fatture elettroniche sono state aggiunte alla coda di invio')); + + break; + case 'export-xml-bulk': $dir = DOCROOT.'/files/export_fatture/'; directory($dir.'tmp/'); @@ -219,4 +237,16 @@ $operations['export-xml-bulk'] = [ ], ]; +if (Interaction::isEnabled()) { + $operations['hook-send'] = [ + 'text' => ' '.tr('Coda di invio FE').'', + 'data' => [ + 'title' => '', + 'msg' => tr('Vuoi davvero aggiungere queste fatture alla coda di invio per le fatture elettroniche?'), + 'button' => tr('Procedi'), + 'class' => 'btn btn-lg btn-warning', + ], + ]; +} + return $operations; diff --git a/plugins/exportFE/actions.php b/plugins/exportFE/actions.php index 6a15bd3d1..8906ef5ae 100644 --- a/plugins/exportFE/actions.php +++ b/plugins/exportFE/actions.php @@ -27,14 +27,6 @@ switch (filter('op')) { case 'send': $result = Interaction::sendInvoice($id_record); - // Aggiornamento dello stato - if ($result['code'] == 200) { - database()->update('co_documenti', [ - 'codice_stato_fe' => 'WAIT', - 'data_stato_fe' => date('Y-m-d H:i:s'), - ], ['id' => $id_record]); - } - echo json_encode($result); break; diff --git a/plugins/exportFE/src/Interaction.php b/plugins/exportFE/src/Interaction.php index fdcdcc99e..e7e0e71db 100644 --- a/plugins/exportFE/src/Interaction.php +++ b/plugins/exportFE/src/Interaction.php @@ -23,6 +23,19 @@ class Interaction extends Services ]); $body = static::responseBody($response); + // Aggiornamento dello stato + if ($body['status'] == 200) { + database()->update('co_documenti', [ + 'codice_stato_fe' => 'WAIT', + 'data_stato_fe' => date('Y-m-d H:i:s'), + ], ['id' => $id_record]); + } elseif ($body['status'] == 405) { + database()->update('co_documenti', [ + 'codice_stato_fe' => 'ERR', + 'data_stato_fe' => date('Y-m-d H:i:s'), + ], ['id' => $id_record]); + } + return [ 'code' => $body['status'], 'message' => $body['message'], diff --git a/plugins/exportFE/src/InvoiceHook.php b/plugins/exportFE/src/InvoiceHook.php new file mode 100644 index 000000000..0486a4ff3 --- /dev/null +++ b/plugins/exportFE/src/InvoiceHook.php @@ -0,0 +1,69 @@ +where('codice_stato_fe', 'ERR') + ->count(); + + return !empty($remaining); + } + + public function execute() + { + $fattura = Fattura::where('hook_send', 1) + ->where('codice_stato_fe', 'ERR') + ->first(); + + $result = Interaction::sendInvoice($fattura->id); + + if ($result['code'] == 200) { + $fattura->hook_send = false; + $fattura->save(); + } + + return $result; + } + + public function response() + { + $completed = !$this->needsExecution(); + $message = tr('Invio fatture elettroniche in corso...'); + $icon = 'text-info'; + + $errors = Fattura::where('hook_send', 1) + ->where('codice_stato_fe', 'ERR') + ->count(); + + if ($completed) { + if (empty($errors)) { + $message = tr('Invio fatture elettroniche completato!'); + } else { + $message = tr('Invio fatture elettroniche completato con errori'); + $icon = 'text-danger'; + } + } + + return [ + 'icon' => 'fa fa-envelope '.$icon, + 'message' => $message, + 'show' => !$completed || !empty($errors), + ]; + } +} diff --git a/update/2_4_11.sql b/update/2_4_11.sql index f3d0333c5..4ca931f43 100644 --- a/update/2_4_11.sql +++ b/update/2_4_11.sql @@ -434,3 +434,11 @@ ALTER TABLE `em_templates` CHANGE `id_smtp` `id_account` INT(11) NOT NULL; ALTER TABLE `em_print_template` CHANGE `id_email` `id_template` INT(11) NOT NULL; ALTER TABLE `em_accounts` ADD `timeout` INT(11) NOT NULL DEFAULT 1000; ALTER TABLE `an_anagrafiche` ADD `enable_newsletter` BOOLEAN DEFAULT TRUE; + +-- Aggiunta coda di invio per le Fatture Elettroniche +ALTER TABLE `co_documenti` ADD `hook_send` BOOLEAN DEFAULT FALSE; +INSERT INTO `zz_hooks` (`id`, `name`, `class`, `frequency`, `id_module`) VALUES (NULL, 'Fatture Elettroniche', 'Plugins\\ExportFE\\InvoiceHook', '1 minute', NULL); + +INSERT INTO `fe_stati_documento` (`codice`, `descrizione`, `icon`) VALUES +('ERR', 'Trasmissione non riuscita', 'fa fa-close'), +('QUEUE', 'In coda di elaborazione', 'fa fa-spinner');