openstamanager/modules/fatture/src/Components/RelationTrait.php

242 lines
6.6 KiB
PHP
Raw Normal View History

2018-12-25 11:32:19 +01: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-12-25 11:32:19 +01:00
namespace Modules\Fatture\Components;
use Illuminate\Database\Eloquent\Builder;
2018-12-25 11:32:19 +01:00
use Modules\Fatture\Fattura;
2019-02-15 12:12:44 +01:00
use Modules\Ritenute\RitenutaAcconto;
use Modules\Rivalse\RivalsaINPS;
2018-12-25 11:32:19 +01:00
trait RelationTrait
{
public function getDocumentID()
2018-12-25 11:32:19 +01:00
{
return 'iddocumento';
}
public function document()
2018-12-25 11:32:19 +01:00
{
return $this->belongsTo(Fattura::class, $this->getDocumentID());
2018-12-25 11:32:19 +01:00
}
public function fattura()
{
return $this->document();
2018-12-25 11:32:19 +01:00
}
2018-12-29 11:15:34 +01:00
/**
* Restituisce i dati aggiuntivi per la fattura elettronica dell'elemento.
*
* @return array
*/
public function getDatiAggiuntiviFEAttribute()
{
2024-04-12 09:12:45 +02:00
$result = $this->attributes['dati_aggiuntivi_fe'] ? json_decode($this->attributes['dati_aggiuntivi_fe'], true) : '';
return (array) $result;
}
/**
* Imposta i dati aggiuntivi per la fattura elettronica dell'elemento.
*/
public function setDatiAggiuntiviFEAttribute($values)
{
$values = (array) $values;
$dati = array_deep_clean($values);
$this->attributes['dati_aggiuntivi_fe'] = json_encode($dati);
}
2019-02-15 12:12:44 +01:00
/**
* Restituisce il totale (imponibile + iva + rivalsa_inps + iva_rivalsainps) dell'elemento.
*
* @return float
*/
public function getTotaleAttribute()
{
2019-07-11 17:44:42 +02:00
return $this->totale_imponibile + $this->iva + $this->rivalsa_inps + $this->iva_rivalsa_inps;
2019-02-15 12:12:44 +01:00
}
public function getRivalsaINPSAttribute()
{
2019-07-11 17:44:42 +02:00
return $this->totale_imponibile / 100 * $this->rivalsa->percentuale;
2019-02-15 12:12:44 +01:00
}
public function getIvaRivalsaINPSAttribute()
{
return $this->rivalsa_inps / 100 * $this->aliquota->percentuale;
}
public function getRitenutaAccontoAttribute()
{
2019-07-11 17:44:42 +02:00
$result = $this->totale_imponibile;
2019-02-15 12:12:44 +01:00
if ($this->calcolo_ritenuta_acconto == 'IMP+RIV') {
$result += $this->rivalsainps;
}
$ritenuta = $this->ritenuta;
$result = $result * $ritenuta->percentuale_imponibile / 100;
return $result / 100 * $ritenuta->percentuale;
2019-02-15 12:12:44 +01:00
}
public function getRitenutaContributiAttribute()
{
if ($this->attributes['ritenuta_contributi']) {
2019-07-11 17:44:42 +02:00
$result = $this->totale_imponibile;
$ritenuta = $this->getDocument()->ritenutaContributi;
2019-02-15 12:12:44 +01:00
$result = $result * $ritenuta->percentuale_imponibile / 100;
return $result / 100 * $ritenuta->percentuale;
}
return 0;
}
/**
* Imposta l'identificatore della Rivalsa INPS.
*
* @param int $value
*/
public function setIdRivalsaINPSAttribute($value)
{
$this->attributes['idrivalsainps'] = $value;
$this->load('rivalsa');
}
/**
* Imposta l'identificatore della Ritenuta d'Acconto.
*
* @param int $value
*/
public function setIdRitenutaAccontoAttribute($value)
{
$this->attributes['idritenutaacconto'] = $value;
$this->load('ritenuta');
}
2019-02-15 10:37:08 +01:00
public function getIdContoAttribute()
{
return $this->attributes['idconto'];
2019-02-15 10:37:08 +01:00
}
public function setIdContoAttribute($value)
{
2019-02-15 12:12:44 +01:00
$this->attributes['idconto'] = $value;
}
public function rivalsa()
{
return $this->belongsTo(RivalsaINPS::class, 'idrivalsainps');
}
public function ritenuta()
{
return $this->belongsTo(RitenutaAcconto::class, 'idritenutaacconto');
}
/**
* Salva la riga, impostando i campi dipendenti dai parametri singoli.
*
* @return bool
*/
public function save(array $options = [])
{
$this->fixRitenutaAcconto();
$this->fixRivalsaINPS();
return parent::save($options);
}
public function delete()
{
$result = parent::delete();
2021-10-18 10:23:39 +02:00
if (!empty($this->idintervento) && $this->fattura->getRighe()->where('idintervento', $this->idintervento)->count() == 1) {
database()->query("UPDATE `in_interventi` SET `idstatointervento` = (SELECT `id` FROM `in_statiintervento` WHERE `codice` = 'OK') WHERE `id`=".prepare($this->idintervento));
}
return $result;
}
/**
* Modifica la quantità del componente.
* Se la fattura è una Nota di credito/debito, risale al secondo livello di origine del componente e corregge di conseguenza le quantità evase.
*
* @param float $value
*
* @return float
*/
public function setQtaAttribute($value)
{
2024-04-08 15:44:33 +02:00
[$qta, $diff] = $this->parseQta($value);
parent::setQtaAttribute($value);
// Individuazione fattura corrente (fix in caso di creazione diretta)
$fattura = $this->fattura;
if (isset($fattura) && $fattura->isNota() && $this->hasOriginalComponent()) {
$source = $this->getOriginalComponent();
// Aggiornamento della quantità evasa di origine
if ($source->hasOriginalComponent()) {
$target = $source->getOriginalComponent();
$target->qta_evasa -= $diff;
$target->save();
}
}
return $diff;
}
2019-02-15 12:12:44 +01:00
/**
* Effettua i conti per la Rivalsa INPS.
*/
protected function fixRivalsaINPS()
{
$this->attributes['rivalsainps'] = $this->rivalsa_inps;
}
/**
* Effettua i conti per la Ritenuta d'Acconto, basandosi sul valore del campo calcolo_ritenuta_acconto.
*/
protected function fixRitenutaAcconto()
{
$this->attributes['ritenutaacconto'] = $this->ritenuta_acconto;
2019-02-15 10:37:08 +01:00
}
2019-04-04 17:44:28 +02:00
protected static function boot($bypass = false)
2019-04-04 17:44:28 +02:00
{
parent::boot($bypass);
2019-04-04 17:44:28 +02:00
// Precaricamento Rivalsa INPS
static::addGlobalScope('rivalsa', function (Builder $builder) {
$builder->with('rivalsa');
});
2019-04-04 17:44:28 +02:00
// Precaricamento Ritenuta d'Acconto
static::addGlobalScope('ritenuta', function (Builder $builder) {
$builder->with('ritenuta');
});
2019-04-04 17:44:28 +02:00
}
2018-12-25 11:32:19 +01:00
}