openstamanager/plugins/importFE/src/Interaction.php

123 lines
3.2 KiB
PHP
Raw Normal View History

2018-11-30 15:33:25 +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-11-30 15:33:25 +01:00
namespace Plugins\ImportFE;
2019-07-19 15:23:00 +02:00
use API\Services;
2020-03-03 10:31:01 +01:00
use Models\Cache;
2018-11-30 15:33:25 +01:00
/**
* Classe per la gestione delle API esterne per l'importazione delle Fatture Elettroniche di acquisto.
2018-11-30 15:33:25 +01:00
*
* @since 2.4.3
*/
2019-07-19 15:23:00 +02:00
class Interaction extends Services
2018-11-30 15:33:25 +01:00
{
2019-07-24 17:17:54 +02:00
public static function getInvoiceList()
2018-11-30 15:33:25 +01:00
{
2019-07-24 17:17:54 +02:00
$list = self::getRemoteList();
// Ricerca fisica
2019-11-29 18:44:08 +01:00
$result = self::getFileList($list);
2019-07-24 17:17:54 +02:00
// Aggiornamento cache hook
Cache::pool('Fatture Elettroniche')->set($result);
2019-07-24 17:17:54 +02:00
2019-11-29 18:44:08 +01:00
return $result;
2019-07-24 17:17:54 +02:00
}
2018-11-30 15:33:25 +01:00
2019-07-24 17:17:54 +02:00
public static function getRemoteList()
{
2020-09-23 12:00:21 +02:00
$list = [];
2019-02-12 11:42:48 +01:00
// Ricerca da remoto
if (self::isEnabled()) {
2019-02-19 15:59:27 +01:00
$response = static::request('POST', 'fatture_da_importare');
2019-02-12 11:42:48 +01:00
$body = static::responseBody($response);
2018-11-30 15:33:25 +01:00
2019-02-19 15:59:27 +01:00
if ($body['status'] == '200') {
2019-07-22 09:52:27 +02:00
$list = $body['results'];
}
}
2020-09-23 12:00:21 +02:00
return $list;
}
2019-11-29 18:44:08 +01:00
public static function getFileList($list = [])
{
2019-11-29 18:44:08 +01:00
$names = array_column($list, 'name');
2019-07-22 09:52:27 +02:00
// Ricerca fisica
2019-07-22 11:35:44 +02:00
$directory = FatturaElettronica::getImportDirectory();
2019-07-22 09:52:27 +02:00
$files = glob($directory.'/*.xml*');
2019-07-24 17:39:11 +02:00
foreach ($files as $id => $file) {
2019-07-22 09:52:27 +02:00
$name = basename($file);
2019-11-29 18:44:08 +01:00
$pos = array_search($name, $names);
2019-01-25 11:02:36 +01:00
2019-11-29 18:44:08 +01:00
if ($pos === false) {
2019-07-22 09:52:27 +02:00
$list[] = [
2019-07-24 17:39:11 +02:00
'id' => $id,
2019-07-22 09:52:27 +02:00
'name' => $name,
2019-07-24 17:39:11 +02:00
'file' => true,
2019-07-22 09:52:27 +02:00
];
2019-11-29 18:44:08 +01:00
} else {
$list[$pos]['id'] = $id;
2019-01-22 08:59:11 +01:00
}
}
2019-02-12 11:42:48 +01:00
2019-07-22 09:52:27 +02:00
return $list;
2018-11-30 15:33:25 +01:00
}
2019-07-24 17:17:54 +02:00
public static function getInvoiceFile($name)
2018-11-30 15:33:25 +01:00
{
$directory = FatturaElettronica::getImportDirectory();
$file = $directory.'/'.$name;
if (!file_exists($file)) {
2019-02-19 15:59:27 +01:00
$response = static::request('POST', 'fattura_da_importare', [
2018-11-30 15:33:25 +01:00
'name' => $name,
]);
2018-12-14 12:50:44 +01:00
$body = static::responseBody($response);
2018-11-30 15:33:25 +01:00
2019-10-18 15:28:44 +02:00
if (!empty($body['content'])) {
FatturaElettronica::store($name, $body['content']);
}
2018-11-30 15:33:25 +01:00
}
return $name;
}
2019-01-22 08:59:11 +01:00
2019-07-24 17:17:54 +02:00
public static function processInvoice($filename)
2019-01-22 08:59:11 +01:00
{
2019-02-19 15:59:27 +01:00
$response = static::request('POST', 'fattura_xml_salvata', [
2019-09-16 11:59:26 +02:00
'filename' => $filename,
]);
2019-01-22 08:59:11 +01:00
2019-01-25 11:02:36 +01:00
$body = static::responseBody($response);
2019-01-22 08:59:11 +01:00
2019-02-20 16:07:42 +01:00
$message = '';
2019-02-19 15:59:27 +01:00
if ($body['status'] != '200') {
$message = $body['status'].' - '.$body['message'];
2019-01-25 11:02:36 +01:00
}
2019-01-22 08:59:11 +01:00
return $message;
}
2018-11-30 15:33:25 +01:00
}