mirror of
https://github.com/devcode-it/openstamanager.git
synced 2025-02-16 19:40:44 +01:00
Correzioni per sincronizzazione da applicazione
This commit is contained in:
parent
b268093d54
commit
065486539a
@ -20,7 +20,6 @@
|
||||
namespace API\App\v1;
|
||||
|
||||
use API\App\AppResource;
|
||||
use Auth;
|
||||
use Modules\Anagrafiche\Anagrafica;
|
||||
|
||||
class Clienti extends AppResource
|
||||
@ -43,28 +42,19 @@ class Clienti extends AppResource
|
||||
|
||||
$sincronizza_lavorati = setting('Sincronizza solo i Clienti per cui il Tecnico ha lavorato in passato');
|
||||
if (!empty($sincronizza_lavorati)) {
|
||||
// Elenco di interventi di interesse
|
||||
$risorsa_interventi = $this->getRisorsaInterventi();
|
||||
$interventi = $risorsa_interventi->getModifiedRecords(null);
|
||||
if (empty($interventi)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$id_interventi = array_keys($interventi);
|
||||
$query .= '
|
||||
AND an_anagrafiche.idanagrafica IN (
|
||||
SELECT idanagrafica FROM in_interventi
|
||||
INNER JOIN in_interventi_tecnici ON in_interventi_tecnici.idintervento = in_interventi.id
|
||||
WHERE in_interventi_tecnici.orario_fine BETWEEN :period_start AND :period_end
|
||||
AND in_interventi_tecnici.idtecnico = :id_tecnico
|
||||
|
||||
UNION
|
||||
|
||||
SELECT idanagrafica FROM in_interventi
|
||||
WHERE in_interventi.id NOT IN (
|
||||
SELECT idintervento FROM in_interventi_tecnici
|
||||
)
|
||||
WHERE in_interventi.id IN ('.implode(',', $id_interventi).')
|
||||
)';
|
||||
|
||||
$date = (new Interventi())->getDateDiInteresse();
|
||||
$id_tecnico = Auth::user()->id_anagrafica;
|
||||
$parameters = [
|
||||
':period_start' => $date['start'],
|
||||
':period_end' => $date['end'],
|
||||
':id_tecnico' => $id_tecnico,
|
||||
];
|
||||
}
|
||||
|
||||
// Filtro per data
|
||||
@ -148,6 +138,11 @@ class Clienti extends AppResource
|
||||
return [];
|
||||
}
|
||||
|
||||
protected function getRisorsaInterventi()
|
||||
{
|
||||
return new Interventi();
|
||||
}
|
||||
|
||||
protected function aggiornaRecord($record, $data)
|
||||
{
|
||||
$database = database();
|
||||
|
@ -50,7 +50,7 @@ class Intervento extends Resource implements UpdateInterface
|
||||
foreach ($request[$sezione] as $record) {
|
||||
$records[] = [$record, $risorse[$sezione]];
|
||||
}
|
||||
} else {
|
||||
} elseif (!empty($request[$sezione])) {
|
||||
$records[] = [$request[$sezione], $risorse[$sezione]];
|
||||
}
|
||||
}
|
||||
@ -58,9 +58,9 @@ class Intervento extends Resource implements UpdateInterface
|
||||
// Controlli sui conflitti
|
||||
$conflict = false;
|
||||
foreach ($records as $key => [$record, $risorsa]) {
|
||||
$record['updated_at'] = new Carbon($record['updated_at']);
|
||||
$record['last_sync_at'] = new Carbon($record['last_sync_at']);
|
||||
if (!$record['updated_at']->greaterThan($record['last_sync_at'])) {
|
||||
$ultima_modifica = new Carbon($record['updated_at']);
|
||||
$ultima_sincronizzazione = new Carbon($record['last_sync_at']);
|
||||
if (!empty($record['last_sync_at']) && !$ultima_modifica->greaterThan($ultima_sincronizzazione)) {
|
||||
unset($records[$key]);
|
||||
continue;
|
||||
}
|
||||
|
@ -43,25 +43,27 @@ class RigheInterventi extends AppResource
|
||||
$user = Auth::user();
|
||||
$id_tecnico = $user->id_anagrafica;
|
||||
|
||||
$query = 'SELECT in_righe_interventi.id FROM in_righe_interventi WHERE in_righe_interventi.idintervento IN (
|
||||
SELECT in_interventi.id FROM in_interventi WHERE
|
||||
deleted_at IS NOT NULL
|
||||
OR EXISTS(
|
||||
SELECT orario_fine FROM in_interventi_tecnici WHERE
|
||||
in_interventi_tecnici.idintervento = in_interventi.id
|
||||
AND orario_fine NOT BETWEEN :period_start AND :period_end
|
||||
AND idtecnico = :id_tecnico
|
||||
)
|
||||
)';
|
||||
$records = database()->fetchArray($query, [
|
||||
':period_end' => $end,
|
||||
':period_start' => $start,
|
||||
':id_tecnico' => $id_tecnico,
|
||||
]);
|
||||
// Elenco di interventi di interesse
|
||||
$risorsa_interventi = $this->getRisorsaInterventi();
|
||||
$interventi = $risorsa_interventi->getCleanupData($last_sync_at);
|
||||
|
||||
// Elenco sessioni degli interventi da rimuovere
|
||||
$da_interventi = [];
|
||||
if (!empty($interventi)) {
|
||||
$query = 'SELECT in_righe_interventi.id
|
||||
FROM in_righe_interventi
|
||||
INNER JOIN in_interventi ON in_interventi_tecnici.idintervento = in_interventi.id
|
||||
WHERE
|
||||
in_interventi.id IN ('.implode(',', $interventi).')
|
||||
OR (orario_fine NOT BETWEEN :period_start AND :period_end)';
|
||||
$records = database()->fetchArray($query, [
|
||||
':period_end' => $end,
|
||||
':period_start' => $start,
|
||||
]);
|
||||
$da_interventi = array_column($records, 'id');
|
||||
}
|
||||
|
||||
$da_interventi = array_column($records, 'id');
|
||||
$mancanti = $this->getMissingIDs('in_righe_interventi', 'id', $last_sync_at);
|
||||
|
||||
$results = array_unique(array_merge($da_interventi, $mancanti));
|
||||
|
||||
return $results;
|
||||
@ -78,26 +80,21 @@ class RigheInterventi extends AppResource
|
||||
$user = Auth::user();
|
||||
$id_tecnico = $user->id_anagrafica;
|
||||
|
||||
$query = 'SELECT in_righe_interventi.id, in_righe_interventi.updated_at FROM in_righe_interventi WHERE in_righe_interventi.idintervento IN (
|
||||
SELECT in_interventi.id FROM in_interventi WHERE
|
||||
in_interventi.id IN (
|
||||
SELECT idintervento FROM in_interventi_tecnici
|
||||
WHERE in_interventi_tecnici.idintervento = in_interventi.id
|
||||
AND in_interventi_tecnici.orario_fine BETWEEN :period_start AND :period_end
|
||||
AND in_interventi_tecnici.idtecnico = :id_tecnico
|
||||
)
|
||||
AND deleted_at IS NULL
|
||||
)';
|
||||
// Elenco di interventi di interesse
|
||||
$risorsa_interventi = $this->getRisorsaInterventi();
|
||||
$interventi = $risorsa_interventi->getModifiedRecords(null);
|
||||
if (empty($interventi)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$id_interventi = array_keys($interventi);
|
||||
$query = 'SELECT in_righe_interventi.id, in_righe_interventi.updated_at FROM in_righe_interventi WHERE in_righe_interventi.idintervento IN ('.implode(',', $id_interventi).')';
|
||||
|
||||
// Filtro per data
|
||||
if ($last_sync_at) {
|
||||
$query .= ' AND in_righe_interventi.updated_at > '.prepare($last_sync_at);
|
||||
}
|
||||
$records = database()->fetchArray($query, [
|
||||
':period_start' => $start,
|
||||
':period_end' => $end,
|
||||
':id_tecnico' => $id_tecnico,
|
||||
]);
|
||||
$records = database()->fetchArray($query);
|
||||
|
||||
return $this->mapModifiedRecords($records);
|
||||
}
|
||||
@ -182,6 +179,11 @@ class RigheInterventi extends AppResource
|
||||
$riga->delete();
|
||||
}
|
||||
|
||||
protected function getRisorsaInterventi()
|
||||
{
|
||||
return new Interventi();
|
||||
}
|
||||
|
||||
protected function getRecord($id)
|
||||
{
|
||||
// Individuazione delle caratteristiche del record
|
||||
|
@ -32,7 +32,7 @@ class Tecnici extends AppResource
|
||||
|
||||
public function getModifiedRecords($last_sync_at)
|
||||
{
|
||||
$statement = Anagrafica::select('idanagrafica as id', 'updated_at')
|
||||
$statement = Anagrafica::select('idanagrafica', 'updated_at')
|
||||
->whereHas('tipi', function (Builder $query) {
|
||||
$query->where('descrizione', '=', 'Tecnico');
|
||||
});
|
||||
|
Loading…
x
Reference in New Issue
Block a user