2019-01-19 10:33:57 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Contratti;
|
|
|
|
|
|
|
|
use Carbon\Carbon;
|
2019-07-22 18:35:13 +02:00
|
|
|
use Common\Components\Description;
|
2019-01-19 10:33:57 +01:00
|
|
|
use Common\Document;
|
|
|
|
use Modules\Anagrafiche\Anagrafica;
|
2019-02-14 17:49:58 +01:00
|
|
|
use Modules\Interventi\Intervento;
|
2019-07-08 12:24:59 +02:00
|
|
|
use Modules\TipiIntervento\Tipo as TipoSessione;
|
2020-02-17 18:43:49 +01:00
|
|
|
use Plugins\PianificazioneFatturazione\Pianificazione;
|
2020-02-17 13:18:25 +01:00
|
|
|
use Plugins\PianificazioneInterventi\Promemoria;
|
2019-01-19 10:33:57 +01:00
|
|
|
use Traits\RecordTrait;
|
2020-03-03 10:33:32 +01:00
|
|
|
use Traits\ReferenceTrait;
|
2019-01-19 10:33:57 +01:00
|
|
|
use Util\Generator;
|
|
|
|
|
|
|
|
class Contratto extends Document
|
|
|
|
{
|
2020-03-03 10:33:32 +01:00
|
|
|
use ReferenceTrait;
|
2019-01-19 10:33:57 +01:00
|
|
|
use RecordTrait;
|
|
|
|
|
|
|
|
protected $table = 'co_contratti';
|
|
|
|
|
2019-11-12 16:48:06 +01:00
|
|
|
/**
|
|
|
|
* The attributes that should be mutated to dates.
|
|
|
|
*
|
|
|
|
* @var array
|
|
|
|
*/
|
|
|
|
protected $dates = [
|
|
|
|
'data_conclusione',
|
|
|
|
'data_accettazione',
|
|
|
|
];
|
|
|
|
|
2019-01-19 10:33:57 +01:00
|
|
|
/**
|
|
|
|
* Crea un nuovo contratto.
|
|
|
|
*
|
2020-02-11 11:43:59 +01:00
|
|
|
* @param string $nome
|
2019-01-19 10:33:57 +01:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
|
|
|
public static function build(Anagrafica $anagrafica, $nome)
|
|
|
|
{
|
|
|
|
$model = parent::build();
|
|
|
|
|
|
|
|
$stato_documento = Stato::where('descrizione', 'Bozza')->first();
|
|
|
|
|
|
|
|
$id_anagrafica = $anagrafica->id;
|
|
|
|
$id_agente = $anagrafica->idagente;
|
|
|
|
|
|
|
|
$id_pagamento = $anagrafica->idpagamento_vendite;
|
|
|
|
if (empty($id_pagamento)) {
|
|
|
|
$id_pagamento = setting('Tipo di pagamento predefinito');
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->anagrafica()->associate($anagrafica);
|
|
|
|
$model->stato()->associate($stato_documento);
|
|
|
|
|
|
|
|
$model->numero = static::getNextNumero();
|
|
|
|
|
|
|
|
$model->nome = $nome;
|
|
|
|
$model->data_bozza = Carbon::now();
|
|
|
|
|
|
|
|
if (!empty($id_agente)) {
|
|
|
|
$model->idagente = $id_agente;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($id_pagamento)) {
|
|
|
|
$model->idpagamento = $id_pagamento;
|
|
|
|
}
|
|
|
|
|
2019-07-08 12:24:59 +02:00
|
|
|
// Salvataggio delle informazioni
|
2019-01-19 10:33:57 +01:00
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fixTipiSessioni()
|
|
|
|
{
|
|
|
|
$database = database();
|
2019-07-08 12:24:59 +02:00
|
|
|
|
|
|
|
$presenti = $database->fetchArray('SELECT idtipointervento AS id FROM co_contratti_tipiintervento WHERE idcontratto = '.prepare($this->id));
|
2019-01-19 10:33:57 +01:00
|
|
|
|
|
|
|
// Aggiunta associazioni costi unitari al contratto
|
2019-07-08 12:24:59 +02:00
|
|
|
$tipi = TipoSessione::whereNotIn('idtipointervento', array_column($presenti, 'id'))->get();
|
2019-01-19 10:33:57 +01:00
|
|
|
|
|
|
|
foreach ($tipi as $tipo) {
|
|
|
|
$database->insert('co_contratti_tipiintervento', [
|
|
|
|
'idcontratto' => $this->id,
|
|
|
|
'idtipointervento' => $tipo->id,
|
|
|
|
'costo_ore' => $tipo->costo_orario,
|
|
|
|
'costo_km' => $tipo->costo_km,
|
|
|
|
'costo_dirittochiamata' => $tipo->costo_diritto_chiamata,
|
|
|
|
'costo_ore_tecnico' => $tipo->costo_orario_tecnico,
|
|
|
|
'costo_km_tecnico' => $tipo->costo_km_tecnico,
|
|
|
|
'costo_dirittochiamata_tecnico' => $tipo->costo_diritto_chiamata_tecnico,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il nome del modulo a cui l'oggetto è collegato.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModuleAttribute()
|
|
|
|
{
|
|
|
|
return 'Contratti';
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:46:17 +01:00
|
|
|
public function getDirezioneAttribute()
|
2019-01-19 10:33:57 +01:00
|
|
|
{
|
2019-03-29 12:46:17 +01:00
|
|
|
return 'entrata';
|
2019-01-19 10:33:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function anagrafica()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stato()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Stato::class, 'idstato');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function articoli()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Articolo::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function righe()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Riga::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
2019-04-04 17:12:32 +02:00
|
|
|
public function sconti()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Sconto::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
2019-01-19 10:33:57 +01:00
|
|
|
public function descrizioni()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Descrizione::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:49:58 +01:00
|
|
|
public function interventi()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Intervento::class, 'id_contratto');
|
|
|
|
}
|
|
|
|
|
2020-02-17 13:18:25 +01:00
|
|
|
public function promemoria()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Promemoria::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
2020-02-17 18:43:49 +01:00
|
|
|
public function pianificazioni()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Pianificazione::class, 'idcontratto');
|
|
|
|
}
|
|
|
|
|
2019-05-04 06:19:08 +02:00
|
|
|
public function fixBudget()
|
|
|
|
{
|
2019-09-05 18:40:39 +02:00
|
|
|
$this->budget = $this->totale_imponibile ?: 0;
|
2019-05-04 06:19:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
$this->fixBudget();
|
|
|
|
|
2019-07-18 09:42:51 +02:00
|
|
|
$result = parent::save($options);
|
|
|
|
|
|
|
|
$this->fixTipiSessioni();
|
|
|
|
|
|
|
|
return $result;
|
2019-05-04 06:19:08 +02:00
|
|
|
}
|
|
|
|
|
2019-07-22 18:35:13 +02:00
|
|
|
/**
|
|
|
|
* Effettua un controllo sui campi del documento.
|
|
|
|
* Viene richiamatp dalle modifiche alle righe del documento.
|
|
|
|
*/
|
2019-09-13 10:07:54 +02:00
|
|
|
public function triggerEvasione(Description $trigger)
|
2019-07-22 18:35:13 +02:00
|
|
|
{
|
2019-09-13 10:07:54 +02:00
|
|
|
parent::triggerEvasione($trigger);
|
2019-07-22 18:35:13 +02:00
|
|
|
|
|
|
|
$righe = $this->getRighe();
|
|
|
|
|
|
|
|
$qta_evasa = $righe->sum('qta_evasa');
|
|
|
|
$qta = $righe->sum('qta');
|
|
|
|
$parziale = $qta != $qta_evasa;
|
|
|
|
|
|
|
|
// Impostazione del nuovo stato
|
|
|
|
if ($qta_evasa == 0) {
|
|
|
|
$descrizione = 'In lavorazione';
|
2020-02-05 09:25:40 +01:00
|
|
|
$codice_intervento = 'OK';
|
2019-07-22 18:35:13 +02:00
|
|
|
} else {
|
|
|
|
$descrizione = $parziale ? 'Parzialmente fatturato' : 'Fatturato';
|
2020-02-05 09:25:40 +01:00
|
|
|
$codice_intervento = 'FAT';
|
2019-07-22 18:35:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$stato = Stato::where('descrizione', $descrizione)->first();
|
|
|
|
$this->stato()->associate($stato);
|
|
|
|
$this->save();
|
|
|
|
|
|
|
|
// Trasferimento degli interventi collegati
|
|
|
|
$interventi = $this->interventi;
|
2020-02-05 09:25:40 +01:00
|
|
|
$stato_intervento = \Modules\Interventi\Stato::where('codice', $codice_intervento)->first();
|
2019-07-22 18:35:13 +02:00
|
|
|
foreach ($interventi as $intervento) {
|
|
|
|
$intervento->stato()->associate($stato_intervento);
|
|
|
|
$intervento->save();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-19 10:33:57 +01:00
|
|
|
// Metodi statici
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calcola il nuovo numero di contratto.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getNextNumero()
|
|
|
|
{
|
|
|
|
$maschera = setting('Formato codice contratti');
|
|
|
|
|
|
|
|
$ultimo = Generator::getPreviousFrom($maschera, 'co_contratti', 'numero');
|
|
|
|
$numero = Generator::generate($maschera, $ultimo);
|
|
|
|
|
|
|
|
return $numero;
|
|
|
|
}
|
2020-03-03 10:33:32 +01:00
|
|
|
|
|
|
|
// Opzioni di riferimento
|
|
|
|
|
|
|
|
public function getReferenceName()
|
|
|
|
{
|
|
|
|
return 'Contratto';
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReferenceNumber()
|
|
|
|
{
|
|
|
|
return $this->numero;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getReferenceDate()
|
|
|
|
{
|
|
|
|
return $this->data_bozza;
|
|
|
|
}
|
2020-04-01 16:42:19 +02:00
|
|
|
|
|
|
|
public function setStatoAttribute($stato)
|
|
|
|
{
|
|
|
|
$this->idstato = Stato::where('descrizione', $stato)->first()['id'];
|
|
|
|
}
|
2019-01-19 10:33:57 +01:00
|
|
|
}
|