Skip to content

Commit 0f81b15

Browse files
committed
feat: Registrazione richieste API nei log delle operazioni
1 parent 2a36a3d commit 0f81b15

29 files changed

Lines changed: 478 additions & 161 deletions

File tree

.!!InY/GeneratorTest.php

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
use Util\Generator;
4+
5+
class GeneratorTest extends \PHPUnit\Framework\TestCase
6+
{
7+
public function testNumbersWithPrefix()
8+
{
9+
$this->test(null, '|TEST');
10+
}
11+
12+
public function testNumbersWithSuffix()
13+
{
14+
$this->test('|TEST');
15+
}
16+
17+
public function testCommonNumbers()
18+
{
19+
$this->test();
20+
}
21+
22+
public function testDates()
23+
{
24+
$this->test('/YYYY');
25+
$this->test('/yy');
26+
27+
$this->test(null, 'YYYY-');
28+
$this->test(null, 'yy-');
29+
}
30+
31+
protected function test($prefix = null, $suffix = null)
32+
{
33+
$date = date('Y-m-d H:i:s');
34+
$info = Generator::dateToPattern($date);
35+
36+
// Individuazione valori relativi a suffisso e prefisso
37+
$prefix_value = Generator::complete($prefix, $info);
38+
$suffix_value = Generator::complete($suffix, $info);
39+
40+
$step = 3;
41+
42+
// Pattern di base con numero di caratteri incrementale
43+
$pattern = $prefix.'#'.$suffix;
44+
45+
$previous = null;
46+
for ($i = 0; $i < 10000; $i = $i + $step) {
47+
$value = $prefix_value.($i + 1).$suffix_value;
48+
$this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info));
49+
50+
$previous = $value;
51+
}
52+
53+
// Pattern con padding
54+
$length = 5;
55+
$pattern = $prefix.str_repeat('#', $length).$suffix;
56+
57+
$previous = null;
58+
for ($i = 0; $i < 10000; $i = $i + $step) {
59+
$value = $prefix_value.$this->pad($i + 1, $length).$suffix_value;
60+
$this->assertEquals($value, Generator::generate($pattern, $previous, $step, $info));
61+
62+
$previous = $value;
63+
}
64+
}
65+
66+
protected function pad($number, $length)
67+
{
68+
return str_pad($number, $length, '0', STR_PAD_LEFT);
69+
}
70+
}

.!!InY/_bootstrap.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
use Codeception\Util\Autoload;
4+
5+
// Caricamento delle dipendenze e delle librerie del progetto
6+
$namespaces = require_once __DIR__.'/../config/namespaces.php';
7+
foreach ($namespaces as $path => $namespace) {
8+
Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/custom/src');
9+
Autoload::addNamespace($namespace.'\\', __DIR__.'/../'.$path.'/src');
10+
}

api/index.php

Lines changed: 38 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
use API\Response;
22+
use Models\OperationLog;
2223

2324
function serverError()
2425
{
@@ -41,19 +42,9 @@ function serverError()
4142
header('Access-Control-Allow-Origin: *');
4243
header('Access-Control-Allow-Methods: POST, GET, PUT, DELETE, OPTIONS');
4344

44-
$dbo->insert('zz_api_log', [
45-
'level' => 'warning',
46-
'message' => 'Richiesta API ricevuta',
47-
'name' => 'API '.get('resource'),
48-
'context' => json_encode([
49-
'token' => get('token'),
50-
'resource' => get('resource'),
51-
]),
52-
]);
53-
$id_log = $dbo->lastInsertedID();
54-
5545
try {
5646
$response = Response::manage();
47+
$info = Response::getInfo();
5748
} catch (Exception $e) {
5849
// Log dell'errore
5950
$logger = logger();
@@ -69,15 +60,42 @@ function serverError()
6960

7061
$result = json_decode((string) $response, true);
7162
$level = ($result['status'] == '200' ? 'info' : 'error');
72-
$dbo->update('zz_api_log', [
73-
'level' => $level,
74-
'message' => $result['message'],
75-
'context' => json_encode([
76-
'token' => get('token'),
77-
'resource' => get('resource'),
78-
'total-count' => $result['total-count'],
79-
]),
80-
], ['id' => $id_log]);
63+
$type = [ 'GET' => 'retrieve', 'POST' => 'create', 'PUT' => 'update', 'DELETE' => 'delete', ];
64+
65+
//Ricavo l'id della richiesta API
66+
$api = $dbo->table('zz_api_resources')
67+
->where('resource', $info['resource'])
68+
->where('type', $type[$_SERVER['REQUEST_METHOD']])
69+
->first();
70+
71+
//Salvataggio del log dell'operazione
72+
OperationLog::setInfo('id_module', $info['id_module']);
73+
OperationLog::setInfo('id_api', $api->id);
74+
OperationLog::setInfo('level', $level);
75+
76+
//Aggiungo il contenuto della richiesta
77+
$context = [
78+
'token' => get('token'),
79+
'resource' => get('resource'),
80+
'filter' => get('filter'),
81+
'total-count' => $result['total-count'],
82+
];
83+
OperationLog::setInfo('context', json_encode($context));
84+
85+
//Aggiungo al log il messaggio completo di risposta
86+
if( ($result['status']=='200' && setting('Log risposte API')=='debug') || $result['status']!='200' ){
87+
$message = json_encode($result);
88+
OperationLog::setInfo('message', $message);
89+
}
90+
91+
//Salvo l'id_record se presente nella risposta
92+
if( !empty($result['id']) ){
93+
OperationLog::setInfo('id_record', $result['id']);
94+
}
95+
96+
$op = ($result['op'] ?: $type[$_SERVER['REQUEST_METHOD']]);
97+
$result['op'] = ($op=='create' ? 'add' : ($op=='retrieve' ? 'read' : $op));
98+
OperationLog::build($result['op']);
8199

82100
json_decode((string) $response);
83101

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
"doctrine/sql-formatter": "^1.5",
3636
"dragonmantank/cron-expression": "^1.0",
3737
"endroid/qr-code": "^6.0",
38+
"ergebnis/json-printer": "^3.8",
3839
"ezyang/htmlpurifier": "^4.8",
3940
"filp/whoops": "^2.15.0",
4041
"greenlion/php-sql-parser": "^4.5",

editor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -449,7 +449,7 @@
449449
<div class="timeline-body py-2">
450450
<div class="d-flex align-items-center">
451451
<div class="mr-3 small">
452-
<i class="fa fa-user mr-1 text-'.$color.'"></i>
452+
<i class="'.($operation['id_api'] ? 'fa fa-plug' : 'fa fa-user').' mr-1 text-'.$color.'"></i>
453453
<span>'.$operation['username'].'</span>
454454
</div>';
455455

modules/anagrafiche/src/API/v1/Anagrafiche.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ public function create($request)
101101

102102
return [
103103
'id' => $id_record,
104+
'module' => 'Anagrafiche',
105+
'op' => 'create',
104106
];
105107
}
106108

@@ -109,6 +111,10 @@ public function delete($request)
109111
$anagrafica = Anagrafica::find($request['id']);
110112

111113
$anagrafica->delete();
114+
115+
return [
116+
'id' => $request['id'],
117+
];
112118
}
113119

114120
public function update($request)
@@ -155,5 +161,9 @@ public function update($request)
155161
}
156162

157163
$anagrafica->save();
164+
165+
return [
166+
'id' => $anagrafica->id,
167+
];
158168
}
159169
}

modules/articoli/src/API/v1/Articoli.php

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ public function update($request)
111111
{
112112
$data = $request['data'];
113113

114-
$articolo = Articolo::find($request['id']);
114+
$articolo = Articolo::find($data['id']);
115115
[$categoria, $sottocategoria] = $this->gestioneCategorie($data['categoria'], $data['sottocategoria']);
116116

117117
// Gestione categoria
118118
if (!empty($categoria)) {
119-
$articolo->categoria()->associate($categoria);
119+
$articolo->id_categoria = post('categoria_edit') ?: post('categoria');
120120
}
121121
if (!empty($sottocategoria)) {
122122
$articolo->sottocategoria()->associate($sottocategoria);
@@ -126,15 +126,19 @@ public function update($request)
126126
$articolo->setPrezzoVendita($data['prezzo_vendita'], $articolo->idiva_vendita);
127127

128128
$articolo->save();
129+
130+
return [
131+
'id' => $articolo->id,
132+
];
129133
}
130134

131135
protected function gestioneCategorie($nome_categoria, $nome_sottocategoria)
132136
{
133137
$sottocategoria = null;
134138

135139
// Gestione categoria
136-
$categoria = Categoria::where('title', '=', $nome_categoria)
137-
->first();
140+
$categoria = (new Categoria())->getByField('title', $nome_categoria);
141+
$categoria = Categoria::find($categoria);
138142
if (empty($categoria) && !empty($nome_categoria)) {
139143
$categoria = Categoria::build();
140144
$categoria->setTranslation('title', $nome_categoria);
@@ -147,10 +151,9 @@ protected function gestioneCategorie($nome_categoria, $nome_sottocategoria)
147151
}
148152

149153
// Gestione sotto-categoria
150-
$sottocategoria = Categoria::where('title', '=', $nome_sottocategoria)
151-
->where('parent', '=', $categoria->id)
152-
->first();
153-
if (empty($sottocategoria) && !empty($nome_sottocategoria)) {
154+
$sottocategoria = (new Categoria())->getByField('title', $nome_sottocategoria);
155+
$sottocategoria = Categoria::find($sottocategoria);
156+
if ( (empty($sottocategoria) && !empty($nome_sottocategoria)) || (!empty($nome_sottocategoria) && $sottocategoria->parent != $categoria->id) ){
154157
$sottocategoria = Categoria::build();
155158
$sottocategoria->setTranslation('title', $nome_sottocategoria);
156159
$sottocategoria->parent = $categoria->id;

modules/articoli/src/API/v1/Movimenti.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,10 @@ public function create($request)
3232

3333
$articolo = Articolo::find($data['id_articolo']);
3434
$articolo->movimenta($data['qta'], $data['descrizione'], $data['data'], true);
35+
36+
return [
37+
'id' => $articolo->id,
38+
'op' => 'add-movimento',
39+
];
3540
}
3641
}

modules/ddt/actions.php

Lines changed: 0 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -857,72 +857,5 @@
857857
flash()->warning(tr('Nessuna aliquota IVA modificata!'));
858858
}
859859

860-
break;
861-
862-
case 'add_ddt_rientrato':
863-
$class = post('class');
864-
$id_documento = post('id_documento');
865-
866-
// Individuazione del documento originale
867-
if (!is_subclass_of($class, Common\Document::class)) {
868-
return;
869-
}
870-
$documento = $class::find($id_documento);
871-
872-
$tipo = Tipo::where('dir', '!=', $documento->direzione)->first();
873-
$stato = Stato::where('name', 'Evaso')->first()->id;
874-
875-
// Duplicazione documento
876-
$id_segment = post('id_segment');
877-
if (get('id_segment')) {
878-
$id_segment = get('id_segment');
879-
}
880-
881-
if (post('create_document') == 'on') {
882-
$ddt = DDT::build($documento->anagrafica, $tipo, $documento->data, $id_segment);
883-
$ddt->stato()->associate($stato);
884-
$ddt->idcausalet = post('id_causale_trasporto');
885-
$ddt->idpagamento = $documento->idpagamento;
886-
$ddt->idsede_partenza = $documento->idsede_destinazione;
887-
$ddt->idsede_destinazione = $documento->idsede_partenza;
888-
$ddt->save();
889-
890-
$id_record = $ddt->id;
891-
}
892-
893-
$evadi_qta_parent = true;
894-
$righe = $documento->getRighe();
895-
foreach ($righe as $riga) {
896-
if (post('evadere')[$riga->id] == 'on' and !empty(post('qta_da_evadere')[$riga->id])) {
897-
$qta = post('qta_da_evadere')[$riga->id];
898-
899-
$copia = $riga->copiaIn($ddt, $qta, $evadi_qta_parent);
900-
$id_iva = ($ddt->anagrafica->idiva_acquisti ?: setting('Iva predefinita'));
901-
$copia->setPrezzoUnitario(0, $id_iva);
902-
903-
// Aggiornamento seriali dalla riga dell'ordine
904-
if ($copia->isArticolo()) {
905-
$serials = is_array(post('serial')[$riga->id]) ? post('serial')[$riga->id] : [];
906-
$copia->serials = $serials;
907-
}
908-
909-
$copia->save();
910-
}
911-
}
912-
913-
// Modifica finale dello stato
914-
if (post('create_document') == 'on') {
915-
$ddt->idstatoddt = post('id_stato');
916-
$ddt->save();
917-
}
918-
919-
ricalcola_costiagg_ddt($id_record);
920-
921-
// Messaggio informativo
922-
$message = tr('_DOC_ aggiunto!', [
923-
'_DOC_' => $ddt->getReference(),
924-
]);
925-
flash()->info($message);
926-
927860
break;
928861
}

modules/ddt/buttons.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,11 @@
1919
*/
2020

2121
use Models\Module;
22-
use Modules\DDT\Causale;
2322

2423
include_once __DIR__.'/../../core.php';
2524

2625
$id_module_collegamento = $ddt->direzione == 'entrata' ? Module::where('name', 'Ddt in entrata')->first()->id : Module::where('name', 'Ddt in uscita')->first()->id;
2726

28-
$causale = Causale::find($ddt->idcausalet);
29-
if ($causale->is_rientrabile && $ddt->direzione == 'entrata') {
30-
echo '
31-
<div class="tip" data-widget="tooltip" title="'.tr("Questo è un DDT rientrabile: per completare la movimentazione, è necessario generare un DDT in entrata tramite questo pulsante").'.">
32-
<button class="btn btn-warning '.($ddt->isImportabile() ? '' : 'disabled').'" data-href="'.$structure->fileurl('crea_documento.php').'?id_module='.$id_module.'&id_record='.$id_record.'&documento=ddt" data-widget="modal" data-title="'.tr('Crea ddt in entrata').'">
33-
<i class="fa fa-truck"></i> '.tr('Crea ddt di rientro').'
34-
</button>
35-
</div>';
36-
}
37-
3827
// Informazioni sui movimenti interni
3928
if (!empty($ddt->id_ddt_trasporto_interno)) {
4029
echo '

0 commit comments

Comments
 (0)