openstamanager/plugins/importFE/src/Interaction.php

103 lines
2.4 KiB
PHP
Raw Normal View History

2018-11-30 15:33:25 +01:00
<?php
namespace Plugins\ImportFE;
2019-07-19 15:23:00 +02:00
use API\Services;
2018-11-30 15:33:25 +01:00
/**
* Classe per l'interazione con API esterne.
*
* @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
2019-11-29 18:44:08 +01:00
InvoiceHook::update($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()
{
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'];
}
}
2019-07-24 17:17:54 +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
}