1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2025-02-14 18:40:36 +01:00

121 lines
3.4 KiB
PHP
Raw Normal View History

2019-07-31 18:22:35 +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-31 18:22:35 +02:00
namespace Modules\PrimaNota;
use Common\SimpleModelTrait;
use Illuminate\Database\Eloquent\Model;
2019-07-31 18:22:35 +02:00
use Modules\Fatture\Fattura;
use Modules\Scadenzario\Scadenza;
class Movimento extends Model
{
use SimpleModelTrait;
2019-07-31 18:22:35 +02:00
protected $table = 'co_movimenti';
protected $appends = [
'id_conto',
'avere',
'dare',
];
2024-03-22 15:52:24 +01:00
public static function build(?Mastrino $mastrino = null, $id_conto = null, ?Fattura $documento = null, ?Scadenza $scadenza = null)
2019-07-31 18:22:35 +02:00
{
$model = new static();
2019-07-31 18:22:35 +02:00
2020-08-10 10:20:22 +02:00
// Informazioni dipendenti dal mastrino
2019-08-01 10:08:34 +02:00
$model->idmastrino = $mastrino->idmastrino;
$model->data = $mastrino->data;
$model->descrizione = $mastrino->descrizione;
$model->note = $mastrino->note;
2019-08-01 10:08:34 +02:00
$model->primanota = $mastrino->primanota;
$model->is_insoluto = $mastrino->is_insoluto;
$model->id_anagrafica = $mastrino->id_anagrafica;
2020-11-06 10:46:42 +01:00
2020-08-10 10:20:22 +02:00
// Conto associato
$model->idconto = $id_conto;
2019-07-31 18:22:35 +02:00
2020-08-10 10:20:22 +02:00
// Associazione al documento indicato
$documento_scadenza = $scadenza ? $scadenza->documento : null;
$documento = $documento ?: $documento_scadenza;
2019-07-31 18:22:35 +02:00
if (!empty($documento)) {
$model->id_anagrafica = $documento->idanagrafica;
2020-08-10 10:20:22 +02:00
$model->iddocumento = $documento->id;
2019-07-31 18:22:35 +02:00
}
2020-08-10 10:20:22 +02:00
// Associazione alla scadenza indicata
$model->id_scadenza = $scadenza ? $scadenza->id : null;
2019-07-31 18:22:35 +02:00
$model->save();
return $model;
}
public function setTotale($avere, $dare)
{
if (!empty($avere)) {
$totale = -$avere;
} else {
$totale = $dare;
}
$this->totale = $totale;
}
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');
}
}