2019-01-01 11:39:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\Preventivi;
|
|
|
|
|
2019-01-10 18:41:25 +01:00
|
|
|
use Carbon\Carbon;
|
2019-01-01 11:39:20 +01:00
|
|
|
use Common\Document;
|
|
|
|
use Modules\Anagrafiche\Anagrafica;
|
2019-02-14 17:49:58 +01:00
|
|
|
use Modules\Interventi\Intervento;
|
2019-01-02 10:08:08 +01:00
|
|
|
use Modules\Interventi\TipoSessione;
|
2019-01-01 11:39:20 +01:00
|
|
|
use Traits\RecordTrait;
|
|
|
|
use Util\Generator;
|
|
|
|
|
|
|
|
class Preventivo extends Document
|
|
|
|
{
|
|
|
|
use RecordTrait;
|
|
|
|
|
|
|
|
protected $table = 'co_preventivi';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Crea un nuovo preventivo.
|
|
|
|
*
|
2019-01-11 08:32:08 +01:00
|
|
|
* @param Anagrafica $anagrafica
|
|
|
|
* @param TipoSessione $tipo_sessione
|
|
|
|
* @param string $nome
|
2019-01-01 11:39:20 +01:00
|
|
|
*
|
|
|
|
* @return self
|
|
|
|
*/
|
2019-01-02 14:15:16 +01:00
|
|
|
public static function build(Anagrafica $anagrafica, TipoSessione $tipo_sessione, $nome)
|
2019-01-01 11:39:20 +01:00
|
|
|
{
|
2019-01-02 14:15:16 +01:00
|
|
|
$model = parent::build();
|
2019-01-01 11:39:20 +01:00
|
|
|
|
|
|
|
$stato_documento = Stato::where('descrizione', 'Bozza')->first();
|
|
|
|
|
|
|
|
$id_anagrafica = $anagrafica->id;
|
|
|
|
$id_agente = $anagrafica->idagente;
|
|
|
|
$id_pagamento = $anagrafica->idpagamento_vendite;
|
|
|
|
|
2019-01-02 10:08:08 +01:00
|
|
|
$costo_orario = $tipo_sessione['costo_orario'];
|
|
|
|
$costo_diritto_chiamata = $tipo_sessione['costo_diritto_chiamata'];
|
2019-01-01 11:39:20 +01:00
|
|
|
|
|
|
|
$id_iva = setting('Iva predefinita');
|
|
|
|
if (empty($id_pagamento)) {
|
|
|
|
$id_pagamento = setting('Tipo di pagamento predefinito');
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->anagrafica()->associate($anagrafica);
|
|
|
|
$model->stato()->associate($stato_documento);
|
2019-01-02 10:08:08 +01:00
|
|
|
$model->tipoSessione()->associate($tipo_sessione);
|
2019-01-01 11:39:20 +01:00
|
|
|
|
|
|
|
$model->numero = static::getNextNumero();
|
|
|
|
|
|
|
|
// Salvataggio delle informazioni
|
|
|
|
$model->nome = $nome;
|
|
|
|
$model->data_bozza = Carbon::now();
|
|
|
|
$model->data_conclusione = Carbon::now()->addMonth();
|
|
|
|
|
|
|
|
if (!empty($id_agente)) {
|
|
|
|
$model->idagente = $id_agente;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!empty($id_iva)) {
|
|
|
|
$model->idiva = $id_iva;
|
|
|
|
}
|
|
|
|
if (!empty($id_pagamento)) {
|
|
|
|
$model->idpagamento = $id_pagamento;
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
// Gestione delle revisioni
|
|
|
|
$model->master_revision = $model->id;
|
|
|
|
$model->default_revision = 1;
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Restituisce il nome del modulo a cui l'oggetto è collegato.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getModuleAttribute()
|
|
|
|
{
|
|
|
|
return 'Preventivi';
|
|
|
|
}
|
|
|
|
|
2019-03-29 12:46:17 +01:00
|
|
|
public function getDirezioneAttribute()
|
2019-01-01 11:39:20 +01:00
|
|
|
{
|
2019-03-29 12:46:17 +01:00
|
|
|
return 'entrata';
|
2019-01-01 11:39:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function anagrafica()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function stato()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Stato::class, 'idstato');
|
|
|
|
}
|
|
|
|
|
2019-01-02 10:08:08 +01:00
|
|
|
public function tipoSessione()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(TipoSessione::class, 'idtipointervento');
|
|
|
|
}
|
|
|
|
|
2019-01-01 11:39:20 +01:00
|
|
|
public function articoli()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Articolo::class, 'idpreventivo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function righe()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Riga::class, 'idpreventivo');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function descrizioni()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Components\Descrizione::class, 'idpreventivo');
|
|
|
|
}
|
|
|
|
|
2019-02-14 17:49:58 +01:00
|
|
|
public function interventi()
|
|
|
|
{
|
|
|
|
return $this->hasMany(Intervento::class, 'id_preventivo');
|
|
|
|
}
|
|
|
|
|
2019-01-01 11:39:20 +01:00
|
|
|
// Metodi statici
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calcola il nuovo numero di preventivo.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public static function getNextNumero()
|
|
|
|
{
|
|
|
|
$maschera = setting('Formato codice preventivi');
|
|
|
|
|
2019-01-10 20:10:47 +01:00
|
|
|
$ultimo = Generator::getPreviousFrom($maschera, 'co_preventivi', 'numero');
|
|
|
|
$numero = Generator::generate($maschera, $ultimo);
|
2019-01-01 11:39:20 +01:00
|
|
|
|
|
|
|
return $numero;
|
|
|
|
}
|
|
|
|
}
|