2019-07-31 18:22:35 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Modules\PrimaNota;
|
|
|
|
|
|
|
|
use Common\Model;
|
|
|
|
use Modules\Fatture\Fattura;
|
|
|
|
use Modules\Scadenzario\Scadenza;
|
|
|
|
|
|
|
|
class Movimento extends Model
|
|
|
|
{
|
|
|
|
protected $table = 'co_movimenti';
|
|
|
|
|
|
|
|
protected $appends = [
|
|
|
|
'id_conto',
|
|
|
|
'avere',
|
|
|
|
'dare',
|
|
|
|
];
|
|
|
|
|
2019-08-01 10:08:34 +02:00
|
|
|
public static function build(Mastrino $mastrino, $id_conto, Scadenza $scadenza = null)
|
2019-07-31 18:22:35 +02:00
|
|
|
{
|
|
|
|
$model = parent::build();
|
|
|
|
|
2019-08-01 10:08:34 +02:00
|
|
|
$model->idmastrino = $mastrino->idmastrino;
|
|
|
|
$model->data = $mastrino->data;
|
|
|
|
$model->descrizione = $mastrino->descrizione;
|
|
|
|
$model->primanota = $mastrino->primanota;
|
|
|
|
$model->is_insoluto = $mastrino->is_insoluto;
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2020-08-05 17:58:54 +02:00
|
|
|
// Associazione al documento del mastrino
|
|
|
|
if (!empty($mastrino->iddocumento)) {
|
|
|
|
$model->iddocumento = $mastrino->iddocumento;
|
|
|
|
}
|
2019-07-31 18:22:35 +02:00
|
|
|
|
2020-08-05 17:58:54 +02:00
|
|
|
// Associazione alla scadenza indicata
|
|
|
|
$model->id_scadenza = $scadenza ? $scadenza->id : null;
|
2019-07-31 18:22:35 +02:00
|
|
|
$documento = $scadenza ? $scadenza->documento : null;
|
|
|
|
if (!empty($documento)) {
|
|
|
|
$model->iddocumento = $documento->id;
|
|
|
|
$model->idanagrafica = $documento->idanagrafica;
|
|
|
|
}
|
|
|
|
|
|
|
|
$model->idconto = $id_conto;
|
|
|
|
|
|
|
|
$model->save();
|
|
|
|
|
|
|
|
return $model;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function setTotale($avere, $dare)
|
|
|
|
{
|
|
|
|
if (!empty($avere)) {
|
|
|
|
$totale = -$avere;
|
|
|
|
} else {
|
|
|
|
$totale = $dare;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->totale = $totale;
|
|
|
|
}
|
|
|
|
|
2020-08-05 17:58:54 +02:00
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
// Aggiornamento automatico di totale_reddito
|
|
|
|
$conto = database()->fetchOne('SELECT * FROM co_pianodeiconti3 WHERE id = '.prepare($this->id_conto));
|
|
|
|
$percentuale = floatval($conto['percentuale_deducibile']);
|
|
|
|
$this->totale_reddito = $this->totale * $percentuale / 100;
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
2019-07-31 18:22:35 +02:00
|
|
|
// Attributi
|
|
|
|
|
|
|
|
public function getIdContoAttribute()
|
|
|
|
{
|
|
|
|
return $this->attributes['idconto'];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getAvereAttribute()
|
|
|
|
{
|
|
|
|
return $this->totale < 0 ? abs($this->totale) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getDareAttribute()
|
|
|
|
{
|
|
|
|
return $this->totale > 0 ? abs($this->totale) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Relazioni Eloquent
|
|
|
|
|
|
|
|
public function scadenza()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Scadenza::class, 'id_scadenza');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function documento()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Fattura::class, 'iddocumento');
|
|
|
|
}
|
|
|
|
}
|