openstamanager/modules/interventi/src/Intervento.php

254 lines
6.2 KiB
PHP
Raw Normal View History

2018-08-29 18:06:51 +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/>.
*/
2018-08-29 18:06:51 +02:00
namespace Modules\Interventi;
2018-12-14 09:46:35 +01:00
use Common\Document;
2020-08-06 16:22:32 +02:00
use Illuminate\Database\Eloquent\SoftDeletes;
2018-08-29 18:06:51 +02:00
use Modules\Anagrafiche\Anagrafica;
2019-02-14 17:49:58 +01:00
use Modules\Contratti\Contratto;
2019-01-11 08:32:08 +01:00
use Modules\Preventivi\Preventivo;
2019-07-08 12:24:59 +02:00
use Modules\TipiIntervento\Tipo as TipoSessione;
use Traits\RecordTrait;
use Traits\ReferenceTrait;
2019-07-08 12:25:51 +02:00
use Util\Generator;
2018-08-29 18:06:51 +02:00
2018-12-14 09:46:35 +01:00
class Intervento extends Document
2018-08-29 18:06:51 +02:00
{
use ReferenceTrait;
2020-08-06 16:22:32 +02:00
//use SoftDeletes;
use RecordTrait;
2018-08-29 18:06:51 +02:00
protected $table = 'in_interventi';
2019-07-15 11:16:12 +02:00
protected $info = [];
protected $dates = [
'data_richiesta',
'data_scadenza',
];
2019-01-11 08:32:08 +01:00
/**
2020-01-03 16:45:44 +01:00
* Crea un nuovo intervento.
2019-01-11 08:32:08 +01:00
*
* @param string $data_richiesta
2019-01-11 08:32:08 +01:00
*
* @return self
*/
public static function build(Anagrafica $anagrafica, TipoSessione $tipo_sessione, Stato $stato, $data_richiesta, $id_segment = null)
2019-01-11 08:32:08 +01:00
{
$model = new static();
2019-01-11 08:32:08 +01:00
$id_segment = $id_segment ?: getSegmentPredefined($model->getModule()->id);
2019-01-11 08:32:08 +01:00
$model->anagrafica()->associate($anagrafica);
$model->stato()->associate($stato);
2021-07-15 18:14:17 +02:00
$model->tipo()->associate($tipo_sessione);
2019-01-11 08:32:08 +01:00
$model->codice = static::getNextCodice($data_richiesta, $id_segment);
2019-01-11 08:32:08 +01:00
$model->data_richiesta = $data_richiesta;
$model->id_segment = $id_segment;
2019-01-11 08:32:08 +01:00
$model->save();
return $model;
}
2019-07-12 17:35:14 +02:00
public function getOreTotaliAttribute()
{
2019-07-15 11:16:12 +02:00
if (!isset($this->info['ore_totali'])) {
$sessioni = $this->sessioni;
$this->info['ore_totali'] = $sessioni->sum('ore');
}
return $this->info['ore_totali'];
}
public function getKmTotaliAttribute()
{
if (!isset($this->info['km_totali'])) {
$sessioni = $this->sessioni;
$this->info['km_totali'] = $sessioni->sum('km');
}
return $this->info['km_totali'];
}
public function getInizioAttribute()
{
if (!isset($this->info['inizio'])) {
$sessioni = $this->sessioni;
$this->info['inizio'] = $sessioni->min('orario_inizio');
}
return $this->info['inizio'];
}
public function getFineAttribute()
{
if (!isset($this->info['fine'])) {
$sessioni = $this->sessioni;
2019-07-12 17:35:14 +02:00
2019-07-15 11:16:12 +02:00
$this->info['fine'] = $sessioni->max('orario_fine');
}
2019-07-12 17:35:14 +02:00
2019-07-15 11:16:12 +02:00
return $this->info['fine'];
2019-07-12 17:35:14 +02:00
}
public function getModuleAttribute()
{
return 'Interventi';
}
public function getDirezioneAttribute()
{
return 'entrata';
}
2019-07-12 17:35:14 +02:00
/**
* Restituisce la collezione di righe e articoli con valori rilevanti per i conti.
*
* @return iterable
*/
public function getRigheContabili()
{
$results = parent::getRigheContabili();
2020-01-11 13:36:43 +01:00
return $this->mergeCollections($results, $this->sessioni);
2019-07-12 17:35:14 +02:00
}
// Relazioni Eloquent
2018-08-29 18:06:51 +02:00
public function anagrafica()
{
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
}
2019-01-11 08:32:08 +01:00
public function preventivo()
{
2019-02-14 17:49:58 +01:00
return $this->belongsTo(Preventivo::class, 'id_preventivo');
}
public function contratto()
{
return $this->belongsTo(Contratto::class, 'id_contratto');
2019-01-11 08:32:08 +01:00
}
2018-08-29 18:06:51 +02:00
public function stato()
{
return $this->belongsTo(Stato::class, 'idstatointervento');
}
2020-01-03 16:45:44 +01:00
public function tipo()
2019-01-11 08:32:08 +01:00
{
return $this->belongsTo(TipoSessione::class, 'idtipointervento');
}
2018-08-29 18:06:51 +02:00
public function articoli()
{
return $this->hasMany(Components\Articolo::class, 'idintervento');
2018-08-29 18:06:51 +02:00
}
public function righe()
{
return $this->hasMany(Components\Riga::class, 'idintervento');
2018-08-29 18:06:51 +02:00
}
2018-12-25 11:32:19 +01:00
2019-04-04 17:12:32 +02:00
public function sconti()
{
return $this->hasMany(Components\Sconto::class, 'idintervento');
}
2018-12-25 11:33:15 +01:00
public function descrizioni()
{
return $this->hasMany(Components\Descrizione::class, 'idintervento');
2018-12-25 11:32:19 +01:00
}
2019-01-11 08:32:08 +01:00
public function sessioni()
{
return $this->hasMany(Components\Sessione::class, 'idintervento');
}
public function toArray()
{
$array = parent::toArray();
$result = array_merge($array, [
'ore_totali' => $this->ore_totali,
'km_totali' => $this->km_totali,
]);
return $result;
}
2019-01-11 08:32:08 +01:00
// Metodi statici
/**
* Calcola il nuovo codice di intervento.
*
* @param string $data
*
2019-01-11 08:32:08 +01:00
* @return string
*/
public static function getNextCodice($data, $id_segment)
2019-01-11 08:32:08 +01:00
{
$maschera = Generator::getMaschera($id_segment);
2019-01-11 08:32:08 +01:00
//$ultimo = Generator::getPreviousFrom($maschera, 'in_interventi', 'codice');
if ((strpos($maschera, 'YYYY') == false) or (strpos($maschera, 'yy') == false)) {
$ultimo = Generator::getPreviousFrom($maschera, 'in_interventi', 'codice', [
'YEAR(data_richiesta) = '.prepare(date('Y', strtotime($data))),
2021-01-13 17:36:35 +01:00
], $data);
2020-02-05 14:51:39 +01:00
} else {
$ultimo = Generator::getPreviousFrom($maschera, 'in_interventi', 'codice');
}
2021-01-13 17:36:35 +01:00
$numero = Generator::generate($maschera, $ultimo, $quantity = 1, $values = [], $data);
2019-01-11 08:32:08 +01:00
return $numero;
}
// Opzioni di riferimento
public function getReferenceName()
{
2020-11-09 14:40:10 +01:00
return 'Attivit&agrave;';
}
public function getReferenceNumber()
{
return $this->codice;
}
public function getReferenceDate()
{
2021-04-08 18:49:06 +02:00
return $this->fine;
}
public function getReferenceRagioneSociale()
{
return $this->anagrafica->ragione_sociale;
}
2018-08-29 18:06:51 +02:00
}