openstamanager/modules/interventi/src/API/v1/Interventi.php

124 lines
3.7 KiB
PHP
Raw Normal View History

2019-07-19 15:23:00 +02:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright (C) DevCode s.r.l.
2020-09-07 15:04:06 +02:00
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
2019-07-19 15:23:00 +02:00
namespace Modules\Interventi\API\v1;
use API\Interfaces\CreateInterface;
use API\Interfaces\RetrieveInterface;
use API\Interfaces\UpdateInterface;
2019-07-19 16:51:52 +02:00
use API\Resource;
2019-07-19 15:23:00 +02:00
use Modules\Anagrafiche\Anagrafica;
use Modules\Interventi\Intervento;
use Modules\Interventi\Stato;
use Modules\TipiIntervento\Tipo as TipoSessione;
2019-07-19 16:51:52 +02:00
class Interventi extends Resource implements RetrieveInterface, CreateInterface, UpdateInterface
2019-07-19 15:23:00 +02:00
{
public function retrieve($request)
{
// Periodo per selezionare interventi
2024-01-15 15:30:45 +01:00
$user = \Auth::user();
2019-07-19 15:23:00 +02:00
2023-09-21 11:59:43 +02:00
$table = 'in_interventi';
2023-09-21 11:59:43 +02:00
$select = [
'in_interventi.*',
'MAX(in_interventi_tecnici.orario_fine) as data',
'GROUP_CONCAT(DISTINCT b.ragione_sociale SEPARATOR \', \') AS tecnici',
2023-09-21 17:34:30 +02:00
'in_statiintervento.descrizione AS stato',
2023-09-21 11:59:43 +02:00
];
$joins[] = [
'in_statiintervento',
'in_interventi.idstatointervento',
2023-09-21 17:34:30 +02:00
'in_statiintervento.idstatointervento',
2023-09-21 11:59:43 +02:00
];
$joins[] = [
'an_anagrafiche',
'in_interventi.idanagrafica',
2023-09-21 17:34:30 +02:00
'an_anagrafiche.idanagrafica',
2023-09-21 11:59:43 +02:00
];
2023-09-21 11:59:43 +02:00
$joins[] = [
'in_interventi_tecnici',
'in_interventi_tecnici.idintervento',
2023-09-21 17:34:30 +02:00
'in_interventi.id',
2023-09-21 11:59:43 +02:00
];
2020-06-30 13:26:15 +02:00
2023-09-21 11:59:43 +02:00
$joins[] = [
'an_anagrafiche as b',
'in_interventi_tecnici.idtecnico',
2023-09-21 17:34:30 +02:00
'b.ragione_sociale',
2019-07-19 15:23:00 +02:00
];
2020-05-07 18:21:49 +02:00
2023-09-21 11:59:43 +02:00
$where = [];
2023-09-21 17:34:30 +02:00
if (!$user->is_admin) {
2023-09-21 11:59:43 +02:00
$where[] = ['in_interventi_tecnici.idtecnico', '=', $user->idanagrafica];
}
$whereraw = [];
2023-09-21 11:59:43 +02:00
$group = 'in_interventi.id';
2020-06-30 13:26:15 +02:00
2019-07-19 15:23:00 +02:00
return [
2023-09-21 11:59:43 +02:00
'table' => $table,
'select' => $select,
'joins' => $joins,
'where' => $where,
'whereraw' => $whereraw,
'group' => $group,
2019-07-19 15:23:00 +02:00
];
}
public function create($request)
{
$data = $request['data'];
$anagrafica = Anagrafica::find($data['id_anagrafica']);
$tipo = TipoSessione::find($data['id_tipo_intervento']);
$stato = Stato::find($data['id_stato_intervento']);
$intervento = Intervento::build($anagrafica, $tipo, $stato, $data['data_richiesta']);
$intervento->richiesta = $data['richiesta'];
$intervento->descrizione = $data['descrizione'];
$intervento->informazioniaggiuntive = $data['informazioni_aggiuntive'];
$intervento->save();
return [
'id' => $intervento->id,
'codice' => $intervento->codice,
];
}
public function update($request)
{
$data = $request['data'];
$intervento = Intervento::find($data['id']);
$intervento->idstatointervento = $data['id_stato_intervento'];
$intervento->descrizione = $data['descrizione'];
$intervento->informazioniaggiuntive = $data['informazioni_aggiuntive'];
$intervento->save();
}
}