openstamanager/plugins/receiptFE/src/Interaction.php

128 lines
3.4 KiB
PHP
Raw Normal View History

2018-12-14 12:50:44 +01: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/>.
*/
2018-12-14 12:50:44 +01:00
namespace Plugins\ReceiptFE;
2019-07-19 15:23:00 +02:00
use API\Services;
2020-03-03 10:31:01 +01:00
use Models\Cache;
2018-12-14 12:50:44 +01:00
/**
* Classe per la gestione delle API esterne per la gestione e l'importazione delle ricevute per le Fatture Elettroniche.
2018-12-14 12:50:44 +01:00
*
* @since 2.4.3
*/
2019-07-19 15:23:00 +02:00
class Interaction extends Services
2018-12-14 12:50:44 +01:00
{
public static function getReceiptList()
{
2019-07-24 17:54:50 +02:00
$list = self::getRemoteList();
2019-07-10 15:02:09 +02:00
2019-07-24 17:54:50 +02:00
// Ricerca fisica
2020-09-07 16:22:12 +02:00
$result = self::getFileList($list);
2019-07-24 17:54:50 +02:00
// Aggiornamento cache hook
Cache::pool('Ricevute Elettroniche')->set($result);
2019-07-24 17:54:50 +02:00
return $result;
2019-07-24 17:54:50 +02:00
}
public static function getRemoteList()
{
$list = [];
2019-07-10 15:02:09 +02:00
// Ricerca da remoto
if (self::isEnabled()) {
$response = static::request('POST', 'notifiche_da_importare');
$body = static::responseBody($response);
if ($body['status'] == '200') {
2019-07-24 17:54:50 +02:00
$results = $body['results'];
2019-07-10 15:02:09 +02:00
2019-07-24 17:54:50 +02:00
foreach ($results as $result) {
$list[] = [
'name' => $result,
];
2019-07-10 15:02:09 +02:00
}
}
}
2018-12-14 12:50:44 +01:00
2019-07-24 17:54:50 +02:00
return $list ?: [];
}
2019-07-24 17:17:54 +02:00
2020-02-24 11:25:18 +01:00
public static function getFileList($list = [])
2019-07-24 17:54:50 +02:00
{
2020-02-24 11:25:18 +01:00
$names = array_column($list, 'name');
2019-07-24 17:54:50 +02:00
// Ricerca fisica
$directory = Ricevuta::getImportDirectory();
$files = glob($directory.'/*.xml*');
foreach ($files as $id => $file) {
$name = basename($file);
2020-02-24 11:25:18 +01:00
$pos = array_search($name, $names);
2019-07-24 17:54:50 +02:00
2020-02-24 11:25:18 +01:00
if ($pos === false) {
2019-07-24 17:54:50 +02:00
$list[] = [
'id' => $id,
'name' => $name,
'file' => true,
];
2020-02-24 11:25:18 +01:00
} else {
$list[$pos]['id'] = $id;
2019-07-24 17:54:50 +02:00
}
}
2019-07-24 17:17:54 +02:00
return $list;
2018-12-14 12:50:44 +01:00
}
public static function getReceipt($name)
{
2019-02-20 16:07:42 +01:00
$directory = Ricevuta::getImportDirectory();
$file = $directory.'/'.$name;
if (!file_exists($file)) {
$response = static::request('POST', 'notifica_da_importare', [
'name' => $name,
]);
$body = static::responseBody($response);
2020-02-14 11:20:09 +01:00
if (!empty($body['content'])) {
Ricevuta::store($name, $body['content']);
}
2019-02-20 16:07:42 +01:00
}
2018-12-14 12:50:44 +01:00
2019-02-20 16:07:42 +01:00
return $name;
2019-02-20 12:39:28 +01:00
}
public static function processReceipt($filename)
{
$response = static::request('POST', 'notifica_xml_salvata', [
'filename' => $filename,
]);
$body = static::responseBody($response);
$result = true;
if ($body['status'] != '200') {
$result = $body['status'].' - '.$body['message'];
}
return $result;
2018-12-14 12:50:44 +01:00
}
}