openstamanager/plugins/receiptFE/src/ReceiptHook.php

120 lines
3.5 KiB
PHP
Raw Normal View History

2019-05-10 06:32:06 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* 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/>.
*/
2019-05-10 06:32:06 +02:00
namespace Plugins\ReceiptFE;
use Hooks\Manager;
use Models\Cache;
use Modules;
2019-05-10 06:32:06 +02:00
/**
* Hook per l'importazione e il conteggio delle ricevute rilevate come da importare.
*
* @see MissingReceiptTask,ReceiptTask Per procedura più strutturata di importazione
*/
class ReceiptHook extends Manager
2019-05-10 06:32:06 +02:00
{
public function isSingleton()
2019-05-10 06:32:06 +02:00
{
return true;
2020-03-03 10:31:01 +01:00
}
2019-05-10 06:32:06 +02:00
public function needsExecution()
2020-03-03 10:31:01 +01:00
{
// Lettura cache
$todo_cache = Cache::pool('Ricevute Elettroniche');
return !$todo_cache->isValid() || !empty($todo_cache->content);
2019-05-10 06:32:06 +02:00
}
public function execute()
2019-05-10 06:32:06 +02:00
{
// Lettura cache
$todo_cache = Cache::pool('Ricevute Elettroniche');
$completed_cache = Cache::pool('Ricevute Elettroniche importate');
// Refresh cache
if (!$todo_cache->isValid()) {
$list = Interaction::getRemoteList();
$todo_cache->set($list);
$completed_cache->set([]);
return;
}
// Caricamento elenco di importazione
$todo = $todo_cache->content;
if (empty($todo)) {
return;
}
2019-08-29 11:25:13 +02:00
// Caricamento elenco di ricevute imporate
$completed = $completed_cache->content;
$count = count($todo);
2019-05-10 06:32:06 +02:00
// Esecuzione di 10 imporazioni
for ($i = 0; $i < 10 && $i < $count; ++$i) {
$element = $todo[$i];
2019-05-10 17:14:34 +02:00
// Importazione ricevuta
$name = $element['name'];
$fattura = Ricevuta::process($name);
if (!empty($fattura)) {
$completed[] = $element;
unset($todo[$i]);
}
2019-06-29 11:12:43 +02:00
}
2019-05-24 20:15:05 +02:00
// Aggiornamento cache
$todo_cache->set($todo);
$completed_cache->set($completed);
}
public function response()
{
// Lettura cache
$todo_cache = Cache::pool('Ricevute Elettroniche');
$completed_cache = Cache::pool('Ricevute Elettroniche importate');
$completed_number = count($completed_cache->content);
$total_number = $completed_number + count($todo_cache->content);
// Messaggio di importazione
$message = tr('Sono state importate _NUM_ ricevute su _TOT_', [
'_NUM_' => $completed_number,
'_TOT_' => $total_number,
2019-05-24 20:15:05 +02:00
]);
2019-05-16 04:52:16 +02:00
// Notifica sullo stato dell'importazione
$notify = $total_number != 0;
$color = $total_number == $completed_number ? 'success' : 'yellow';
$module = Modules::get('Fatture di vendita');
2019-05-16 04:52:16 +02:00
return [
'icon' => 'fa fa-ticket text-'.$color,
2019-05-16 04:52:16 +02:00
'message' => $message,
2019-08-27 15:42:13 +02:00
'show' => $notify,
'link' => base_path().'/controller.php?id_module='.$module->id,
2019-05-16 04:52:16 +02:00
];
2019-05-10 06:32:06 +02:00
}
}