Skip to content

Commit 546d39e

Browse files
committed
feat: aggiunta api per la sincronizzazione dei movimenti fatti dal tecnico
1 parent d17a395 commit 546d39e

3 files changed

Lines changed: 69 additions & 7 deletions

File tree

modules/articoli/src/Articolo.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,14 +168,15 @@ public function registra($qta, $descrizone = null, $data = null, $manuale = fals
168168
// Movimento il magazzino solo se l'articolo non è un servizio
169169
if (empty($this->servizio)) {
170170
// Registrazione della movimentazione
171-
database()->insert('mg_movimenti', array_merge($array, [
171+
// I valori in $array hanno priorità (es. idutente passato dall'API)
172+
database()->insert('mg_movimenti', array_merge([
172173
'idarticolo' => $this->id,
173174
'qta' => $qta,
174175
'movimento' => $descrizone,
175176
'data' => $data,
176177
'manuale' => $manuale,
177178
'idutente' => $user->id,
178-
]));
179+
], $array));
179180
}
180181
$id = database()->lastInsertedID();
181182

src/API/App/v1/MovimentiManuali.php

Lines changed: 62 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,28 +28,86 @@ class MovimentiManuali extends AppResource
2828
{
2929
public function getCleanupData($last_sync_at)
3030
{
31-
return [];
31+
// Recupera l'ID utente loggato
32+
$user = auth_osm()->getUser();
33+
$id_utente = $user->id;
34+
35+
// Restituisce i movimenti eliminati o non più appartenenti all'utente
36+
$query = 'SELECT `mg_movimenti`.`id`
37+
FROM `mg_movimenti`
38+
WHERE `mg_movimenti`.`idutente` = '.prepare($id_utente);
39+
40+
if ($last_sync_at) {
41+
$query .= ' AND `mg_movimenti`.`updated_at` > '.prepare($last_sync_at);
42+
}
43+
44+
$records = database()->fetchArray($query);
45+
$ids = array_column($records, 'id');
46+
47+
return $this->getMissingIDs('mg_movimenti', 'id', $last_sync_at, $ids);
3248
}
3349

3450
public function getModifiedRecords($last_sync_at)
3551
{
36-
return [];
52+
// Recupera l'ID utente loggato
53+
$user = auth_osm()->getUser();
54+
$id_utente = $user->id;
55+
56+
// Calcola le date dell'ultimo giorno (00:00 - 23:59)
57+
$oggi = Carbon::now();
58+
$inizio_giorno = $oggi->copy()->startOfDay()->format('Y-m-d H:i:s');
59+
$fine_giorno = $oggi->copy()->endOfDay()->format('Y-m-d H:i:s');
60+
61+
// Query per recuperare i movimenti dell'utente loggato nell'ultimo giorno
62+
$query = 'SELECT `mg_movimenti`.`id`, `mg_movimenti`.`updated_at`
63+
FROM `mg_movimenti`
64+
WHERE `mg_movimenti`.`idutente` = '.prepare($id_utente).'
65+
AND `mg_movimenti`.`data` BETWEEN '.prepare($inizio_giorno).' AND '.prepare($fine_giorno);
66+
67+
$records = database()->fetchArray($query);
68+
69+
return $this->mapModifiedRecords($records);
3770
}
3871

3972
public function retrieveRecord($id)
4073
{
41-
return [];
74+
// Recupera l'ID utente loggato per sicurezza
75+
$user = auth_osm()->getUser();
76+
$id_utente = $user->id;
77+
78+
// Query per recuperare il dettaglio del movimento
79+
$query = 'SELECT
80+
`mg_movimenti`.`id`,
81+
`mg_movimenti`.`idarticolo` AS id_articolo,
82+
`mg_movimenti`.`qta`,
83+
`mg_movimenti`.`movimento` AS descrizione,
84+
`mg_movimenti`.`data`,
85+
`mg_movimenti`.`idsede` AS id_sede_azienda,
86+
`mg_movimenti`.`idutente` AS id_utente,
87+
`mg_movimenti`.`manuale`
88+
FROM
89+
`mg_movimenti`
90+
LEFT JOIN `mg_articoli` ON `mg_movimenti`.`idarticolo` = `mg_articoli`.`id`
91+
LEFT JOIN `mg_articoli_lang` ON (`mg_articoli`.`id` = `mg_articoli_lang`.`id_record` AND `mg_articoli_lang`.`id_lang` = '.prepare(\Models\Locale::getDefault()->id).')
92+
WHERE
93+
`mg_movimenti`.`id` = '.prepare($id).'
94+
AND `mg_movimenti`.`idutente` = '.prepare($id_utente);
95+
96+
$record = database()->fetchOne($query);
97+
98+
return $record;
4299
}
43100

44101
public function createRecord($data)
45102
{
46103
$articolo = Articolo::find($data['id_articolo']);
47-
$data_movimento = new Carbon($data['created_at']);
104+
$data_movimento = new Carbon($data['data']);
48105

49106
$id_sede = isset($data['id_sede_azienda']) && $data['id_sede_azienda'] !== null ? $data['id_sede_azienda'] : 0;
50107

51108
$id_movimento = $articolo->movimenta($data['qta'], $data['descrizione'], $data_movimento, true, [
52109
'idsede' => $id_sede,
110+
'idutente' => auth_osm()->getUser()->id,
53111
]);
54112

55113
return [

update/2_10.sql

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,7 @@ ALTER TABLE `zz_modules` DROP COLUMN `use_notes`;
200200
ALTER TABLE `zz_modules` DROP COLUMN `use_checklists`;
201201

202202
-- Aggiunta colonna session_token per gestione sessione singola
203-
ALTER TABLE `zz_users` ADD `session_token` VARCHAR(64) NULL DEFAULT NULL AFTER `password`;
203+
ALTER TABLE `zz_users` ADD `session_token` VARCHAR(64) NULL DEFAULT NULL AFTER `password`;
204+
205+
-- Aggiunta risorsa API per la gestione dei movimenti manuali
206+
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`, `created_at`, `updated_at`) VALUES (NULL, 'app-v1', 'retrieve', 'movimento-manuale', 'API\\App\\v1\\MovimentiManuali', '1', NULL, NULL)

0 commit comments

Comments
 (0)