Introduzione base per la sincronizzazione con applicazione
This commit is contained in:
parent
f02235a6e8
commit
5101eb9def
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Anagrafiche\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Anagrafiche\Anagrafica;
|
||||
|
||||
class Anagrafiche extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getDeleted('an_anagrafiche', 'idanagrafica');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$statement = Anagrafica::withTrashed()->select('idanagrafica')
|
||||
->whereHas('tipi', function (Builder $query) {
|
||||
$query->where('descrizione', '=', 'Cliente');
|
||||
});
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$statement = $statement->where('updated_at', '>', $last_sync);
|
||||
}
|
||||
|
||||
$results = $statement->get()
|
||||
->pluck('idanagrafica');
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT an_anagrafiche.idanagrafica AS id,
|
||||
an_anagrafiche.ragione_sociale,
|
||||
an_anagrafiche.piva AS partita_iva,
|
||||
an_anagrafiche.codice_fiscale,
|
||||
an_anagrafiche.indirizzo,
|
||||
an_anagrafiche.indirizzo2,
|
||||
an_anagrafiche.citta,
|
||||
an_anagrafiche.cap,
|
||||
an_anagrafiche.provincia,
|
||||
an_anagrafiche.km,
|
||||
IFNULL(an_anagrafiche.lat, 0.00) AS latitudine,
|
||||
IFNULL(an_anagrafiche.lng, 0.00) AS longitudine,
|
||||
an_nazioni.nome AS nazione,
|
||||
an_anagrafiche.fax,
|
||||
an_anagrafiche.telefono,
|
||||
an_anagrafiche.cellulare,
|
||||
an_anagrafiche.email,
|
||||
an_anagrafiche.sitoweb AS sito_web,
|
||||
an_anagrafiche.note,
|
||||
an_anagrafiche.deleted_at
|
||||
FROM an_anagrafiche
|
||||
LEFT OUTER JOIN an_nazioni ON an_anagrafiche.id_nazione = an_nazioni.id
|
||||
WHERE an_anagrafiche.idanagrafica = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Anagrafiche\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use API\Interfaces\RetrieveInterface;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class Referenti extends AppResource implements RetrieveInterface
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getMissingIDs('an_referenti', 'id');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$query = "SELECT DISTINCT(an_referenti.id) AS id FROM an_referenti
|
||||
INNER JOIN an_anagrafiche ON an_anagrafiche.idanagrafica = an_referenti.idanagrafica
|
||||
INNER JOIN an_tipianagrafiche_anagrafiche ON an_tipianagrafiche_anagrafiche.idanagrafica = an_anagrafiche.idanagrafica
|
||||
INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica = an_tipianagrafiche.idtipoanagrafica
|
||||
WHERE an_tipianagrafiche.descrizione = 'Cliente'";
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$query .= ' AND an_referenti.updated_at > '.prepare($last_sync);
|
||||
}
|
||||
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return array_column($records, 'id');
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT an_referenti.id,
|
||||
an_referenti.nome,
|
||||
an_referenti.mansione,
|
||||
an_referenti.telefono,
|
||||
an_referenti.email
|
||||
FROM an_referenti
|
||||
WHERE an_referenti.id = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,63 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Anagrafiche\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class Sedi extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getMissingIDs('an_sedi', 'id');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$query = "SELECT DISTINCT(an_sedi.id) AS id FROM an_sedi
|
||||
INNER JOIN an_anagrafiche ON an_anagrafiche.idanagrafica = an_sedi.idanagrafica
|
||||
INNER JOIN an_tipianagrafiche_anagrafiche ON an_tipianagrafiche_anagrafiche.idanagrafica = an_anagrafiche.idanagrafica
|
||||
INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica = an_tipianagrafiche.idtipoanagrafica
|
||||
WHERE an_tipianagrafiche.descrizione = 'Cliente'";
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$query .= ' AND an_sedi.updated_at > '.prepare($last_sync);
|
||||
}
|
||||
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return array_column($records, 'id');
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT an_sedi.id,
|
||||
an_sedi.idanagrafica AS id_anagrafica,
|
||||
an_sedi.nomesede AS nome,
|
||||
an_sedi.piva AS partita_iva,
|
||||
an_sedi.codice_fiscale,
|
||||
an_sedi.indirizzo,
|
||||
an_sedi.indirizzo2,
|
||||
an_sedi.citta,
|
||||
an_sedi.cap,
|
||||
an_sedi.provincia,
|
||||
an_sedi.km,
|
||||
IFNULL(an_sedi.lat, 0.00) AS latitudine,
|
||||
IFNULL(an_sedi.lng, 0.00) AS longitudine,
|
||||
an_nazioni.nome AS nazione,
|
||||
an_sedi.telefono,
|
||||
an_sedi.cellulare,
|
||||
an_sedi.fax,
|
||||
an_sedi.email
|
||||
FROM an_sedi
|
||||
LEFT OUTER JOIN an_nazioni ON an_sedi.id_nazione = an_nazioni.id
|
||||
WHERE an_sedi.id = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Articoli\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class Articoli extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getDeleted('mg_articoli', 'id');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$query = 'SELECT mg_articoli.id FROM mg_articoli';
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$query .= ' WHERE mg_articoli.updated_at > '.prepare($last_sync);
|
||||
}
|
||||
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return array_column($records, 'id');
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT mg_articoli.id AS id,
|
||||
mg_articoli.codice,
|
||||
mg_articoli.descrizione,
|
||||
mg_articoli.prezzo_vendita,
|
||||
mg_articoli.prezzo_acquisto,
|
||||
mg_articoli.qta,
|
||||
mg_articoli.um,
|
||||
(SELECT nome FROM mg_categorie WHERE id = mg_articoli.id_categoria) AS categoria,
|
||||
(SELECT nome FROM mg_categorie WHERE id = mg_articoli.id_sottocategoria) AS sottocategoria
|
||||
FROM mg_articoli
|
||||
WHERE mg_articoli.id = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\Impianti\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use Modules\Impianti\Impianto;
|
||||
|
||||
class Impianti extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getMissingIDs('my_impianti', 'id');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$statement = Impianto::select('id')
|
||||
->whereHas('anagrafica.tipi', function (Builder $query) {
|
||||
$query->where('descrizione', '=', 'Cliente');
|
||||
});
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$statement = $statement->where('updated_at', '>', $last_sync);
|
||||
}
|
||||
|
||||
$results = $statement->get()
|
||||
->pluck('id');
|
||||
|
||||
return $results;
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT my_impianti.id,
|
||||
my_impianti.idanagrafica AS id_anagrafica,
|
||||
my_impianti.matricola,
|
||||
my_impianti.nome,
|
||||
my_impianti.descrizione
|
||||
FROM my_impianti
|
||||
WHERE my_impianti.id = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -3,8 +3,15 @@
|
|||
namespace Modules\Impianti;
|
||||
|
||||
use Common\Model;
|
||||
use Modules\Anagrafiche\Anagrafica;
|
||||
|
||||
class Impianto extends Model
|
||||
{
|
||||
protected $table = 'my_impianti';
|
||||
|
||||
// Relazioni Eloquent
|
||||
public function anagrafica()
|
||||
{
|
||||
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,45 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\StatiIntervento\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class StatiIntervento extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getDeleted('in_statiintervento', 'idstatointervento');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$query = 'SELECT in_statiintervento.idstatointervento AS id FROM in_statiintervento';
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$query .= ' WHERE in_statiintervento.updated_at > '.prepare($last_sync);
|
||||
}
|
||||
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return array_column($records, 'id');
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT in_statiintervento.idstatointervento AS id,
|
||||
in_statiintervento.codice,
|
||||
in_statiintervento.descrizione,
|
||||
in_statiintervento.colore,
|
||||
in_statiintervento.is_completato
|
||||
FROM in_statiintervento
|
||||
WHERE in_statiintervento.idstatointervento = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
|
||||
namespace Modules\TipiIntervento\API\AppV1;
|
||||
|
||||
use API\AppResource;
|
||||
use Carbon\Carbon;
|
||||
|
||||
class TipiIntervento extends AppResource
|
||||
{
|
||||
protected function getCleanupData()
|
||||
{
|
||||
return $this->getMissingIDs('in_tipiintervento', 'idtipointervento');
|
||||
}
|
||||
|
||||
protected function getData($last_sync_at)
|
||||
{
|
||||
$query = 'SELECT in_tipiintervento.idtipointervento AS id FROM in_tipiintervento';
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$last_sync = new Carbon($last_sync_at);
|
||||
$query .= ' WHERE in_tipiintervento.updated_at > '.prepare($last_sync);
|
||||
}
|
||||
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return array_column($records, 'id');
|
||||
}
|
||||
|
||||
protected function getDetails($id)
|
||||
{
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$query = 'SELECT in_tipiintervento.idtipointervento AS id,
|
||||
in_tipiintervento.descrizione
|
||||
FROM in_tipiintervento
|
||||
WHERE in_tipiintervento.idtipointervento = '.prepare($id);
|
||||
|
||||
$record = database()->fetchOne($query);
|
||||
|
||||
return $record;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,118 @@
|
|||
<?php
|
||||
|
||||
namespace API;
|
||||
|
||||
use API\Interfaces\RetrieveInterface;
|
||||
|
||||
/**
|
||||
* Risorsa di base per la gestione delle operazioni standard di comunicazione con l'applicazione.
|
||||
*/
|
||||
abstract class AppResource extends Resource implements RetrieveInterface
|
||||
{
|
||||
public function retrieve($request)
|
||||
{
|
||||
$id = $request['id'];
|
||||
$last_sync_at = $request['last_sync_at'] == 'undefined' ? null : $request['last_sync_at'];
|
||||
|
||||
// Gestione delle operazioni di cleanup
|
||||
if (strpos($request['resource'], 'cleanup') !== false) {
|
||||
$list = $this->getCleanupData();
|
||||
|
||||
return [
|
||||
'records' => $list,
|
||||
];
|
||||
}
|
||||
|
||||
// Gestione dell'enumerazione dei record modificati
|
||||
if (!isset($id)) {
|
||||
$list = $this->getData($last_sync_at);
|
||||
|
||||
return [
|
||||
'records' => $list,
|
||||
];
|
||||
}
|
||||
|
||||
// Gestione della visualizzazione dei dettagli del record
|
||||
$details = $this->getDetails($id);
|
||||
|
||||
return [
|
||||
'record' => $details,
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table_name Tabella da analizzare
|
||||
* @param string $column Colonna di tipo AUTO_INCREMENT della tabella
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getMissingIDs($table_name, $column)
|
||||
{
|
||||
$database = database();
|
||||
$db_name = $database->getDatabaseName();
|
||||
|
||||
// Ottiene il valore successivo della colonna di tipo AUTO_INCREMENT
|
||||
$auto_inc = $database->fetchOne('SELECT AUTO_INCREMENT FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = '.prepare($table_name).' AND TABLE_SCHEMA = '.prepare($db_name))['AUTO_INCREMENT'];
|
||||
|
||||
// Ottiene i vuoti all'interno della sequenza AUTO_INCREMENT
|
||||
$steps = $database->fetchArray('SELECT (t1.'.$column.' + 1) as start, (SELECT MIN(t3.'.$column.') - 1 FROM '.$table_name.' t3 WHERE t3.'.$column.' > t1.'.$column.') as end FROM '.$table_name.' t1 WHERE NOT EXISTS (SELECT t2.'.$column.' FROM '.$table_name.' t2 WHERE t2.'.$column.' = t1.'.$column.' + 1) ORDER BY start');
|
||||
|
||||
$total = [];
|
||||
foreach ($steps as $step) {
|
||||
if ($step['end'] == null) {
|
||||
$step['end'] = $auto_inc - 1;
|
||||
}
|
||||
|
||||
if ($step['end'] >= $step['start']) {
|
||||
$total = array_merge($total, range($step['start'], $step['end']));
|
||||
}
|
||||
}
|
||||
|
||||
return $total;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $table_name Tabella da analizzare
|
||||
* @param string $column Colonna di tipo AUTO_INCREMENT della tabella
|
||||
*
|
||||
* @throws \Exception
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function getDeleted($table_name, $column)
|
||||
{
|
||||
$database = database();
|
||||
|
||||
$query = 'SELECT '.$column.' AS id FROM '.$table_name.' WHERE deleted_at IS NOT NULL';
|
||||
$results = $database->fetchArray($query);
|
||||
|
||||
return array_column($results, 'id');
|
||||
}
|
||||
|
||||
/**
|
||||
* Restituisce un array contenente gli ID dei record eliminati.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getCleanupData();
|
||||
|
||||
/**
|
||||
* Restituisce un array contenente gli ID dei record modificati e da sincronizzare.
|
||||
*
|
||||
* @param string $last_sync_at
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getData($last_sync_at);
|
||||
|
||||
/**
|
||||
* Restituisce i dettagli relativi a un singolo record identificato tramite ID.
|
||||
*
|
||||
* @param string $id
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
abstract protected function getDetails($id);
|
||||
}
|
|
@ -122,3 +122,27 @@ UPDATE `em_accounts` SET `connected_at` = NOW();
|
|||
|
||||
-- Aggiunta del flag is_importabile sulle causali per permettere/bloccare l'importazione dei DDT
|
||||
ALTER TABLE `dt_causalet` ADD `is_importabile` BOOLEAN DEFAULT TRUE AFTER `descrizione`;
|
||||
|
||||
-- Aggiunta risorse dedicate all'applicazione
|
||||
INSERT INTO `zz_api_resources` (`id`, `version`, `type`, `resource`, `class`, `enabled`) VALUES
|
||||
(NULL, 'app-v1', 'retrieve', 'anagrafiche', 'Modules\\Anagrafiche\\API\\AppV1\\Anagrafiche', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'anagrafiche-cleanup', 'Modules\\Anagrafiche\\API\\AppV1\\Anagrafiche', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'anagrafica', 'Modules\\Anagrafiche\\API\\AppV1\\Anagrafiche', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'sedi', 'Modules\\Anagrafiche\\API\\AppV1\\Sedi', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'sedi-cleanup', 'Modules\\Anagrafiche\\API\\AppV1\\Sedi', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'sede', 'Modules\\Anagrafiche\\API\\AppV1\\Sedi', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'referenti', 'Modules\\Anagrafiche\\API\\AppV1\\Referenti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'referenti-cleanup', 'Modules\\Anagrafiche\\API\\AppV1\\Referenti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'referente', 'Modules\\Anagrafiche\\API\\AppV1\\Referenti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'impianti', 'Modules\\Impianti\\API\\AppV1\\Impianti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'impianti-cleanup', 'Modules\\Impianti\\API\\AppV1\\Impianti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'impianto', 'Modules\\Impianti\\API\\AppV1\\Impianti', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'stati-intervento', 'Modules\\StatiIntervento\\API\\AppV1\\StatiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'stati-intervento-cleanup', 'Modules\\StatiIntervento\\API\\AppV1\\StatiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'stato-intervento', 'Modules\\StatiIntervento\\API\\AppV1\\StatiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'tipi-intervento', 'Modules\\TipiIntervento\\API\\AppV1\\TipiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'tipi-intervento-cleanup', 'Modules\\TipiIntervento\\API\\AppV1\\TipiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'tipo-intervento', 'Modules\\TipiIntervento\\API\\AppV1\\TipiIntervento', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'articoli', 'Modules\\Articoli\\API\\AppV1\\Articoli', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'articoli-cleanup', 'Modules\\Articoli\\API\\AppV1\\Articoli', '1'),
|
||||
(NULL, 'app-v1', 'retrieve', 'articolo', 'Modules\\Articoli\\API\\AppV1\\Articoli', '1');
|
||||
|
|
Loading…
Reference in New Issue