openstamanager/src/Common/Document.php

271 lines
6.3 KiB
PHP
Raw Normal View History

2018-12-14 09:46:35 +01:00
<?php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione
* Copyright (C) DevCode s.n.c.
*
* 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-14 09:46:35 +01:00
namespace Common;
use Common\Components\Component;
use Illuminate\Database\Eloquent\Model as Model;
abstract class Document extends Model implements ReferenceInterface, DocumentInterface
2018-12-14 09:46:35 +01:00
{
/**
* Abilita la movimentazione automatica degli Articoli, finalizzata alla gestione interna del magazzino.
*
* @var bool
*/
public static $movimenta_magazzino = true;
/**
* Restituisce il valore della variabile statica $movimenta_magazzino per il documento.
*
* @return bool
*/
public function getMovimentaMagazzinoAttribute()
{
return static::$movimenta_magazzino;
}
2018-12-23 16:16:59 +01:00
public function getRighe()
2018-12-14 09:46:35 +01:00
{
2020-01-11 13:35:34 +01:00
$results = $this->mergeCollections($this->descrizioni, $this->righe, $this->articoli, $this->sconti);
2018-12-23 16:16:59 +01:00
return $results->sortBy(function ($item) {
return [$item->order, $item->id];
});
2018-12-23 16:16:59 +01:00
}
2020-01-17 17:31:07 +01:00
public function getRiga($type, $id)
{
$righe = $this->getRighe();
2020-01-17 17:31:46 +01:00
return $righe->first(function ($item) use ($type, $id) {
return $item instanceof $type && $item->id == $id;
2020-01-17 17:31:07 +01:00
});
}
2019-05-04 06:19:08 +02:00
public function getRigheRaggruppate()
{
$righe = $this->getRighe();
$groups = $righe->groupBy(function ($item, $key) {
if (!$item->hasOriginalComponent()) {
2019-05-04 06:19:08 +02:00
return null;
}
$parent = $item->getOriginalComponent()->getDocument();
2019-05-04 06:19:08 +02:00
return serialize($parent);
});
return $groups;
}
2018-12-23 16:16:59 +01:00
abstract public function righe();
abstract public function articoli();
abstract public function descrizioni();
2019-04-04 17:12:32 +02:00
abstract public function sconti();
abstract public function getDirezioneAttribute();
public function triggerEvasione(Component $trigger)
{
$this->setRelations([]);
}
public function triggerComponent(Component $trigger)
{
$this->setRelations([]);
}
2018-12-14 09:46:35 +01:00
/**
2019-02-15 12:12:44 +01:00
* Calcola l'imponibile del documento.
2018-12-14 09:46:35 +01:00
*
* @return float
*/
public function getImponibileAttribute()
{
return $this->calcola('imponibile');
2018-12-14 09:46:35 +01:00
}
/**
2019-02-15 12:12:44 +01:00
* Calcola lo sconto totale del documento.
2018-12-14 09:46:35 +01:00
*
* @return float
*/
public function getScontoAttribute()
{
return $this->calcola('sconto');
2018-12-14 09:46:35 +01:00
}
/**
2019-07-11 17:44:42 +02:00
* Calcola il totale imponibile del documento.
2018-12-14 09:46:35 +01:00
*
* @return float
*/
2019-07-11 17:44:42 +02:00
public function getTotaleImponibileAttribute()
2018-12-14 09:46:35 +01:00
{
2019-07-11 17:44:42 +02:00
return $this->calcola('totale_imponibile');
2018-12-14 09:46:35 +01:00
}
/**
2019-02-15 12:12:44 +01:00
* Calcola l'IVA totale del documento.
2018-12-14 09:46:35 +01:00
*
* @return float
*/
public function getIvaAttribute()
{
2019-02-15 12:12:44 +01:00
return $this->calcola('iva');
2018-12-14 09:46:35 +01:00
}
/**
2019-02-15 12:12:44 +01:00
* Calcola il totale del documento.
2018-12-14 09:46:35 +01:00
*
* @return float
*/
public function getTotaleAttribute()
{
return $this->calcola('totale');
2018-12-14 09:46:35 +01:00
}
2018-12-23 16:16:59 +01:00
/**
* Calcola la spesa totale relativa alla fattura.
*
* @return float
*/
public function getSpesaAttribute()
{
return $this->calcola('spesa');
2018-12-23 16:16:59 +01:00
}
/**
2019-09-12 09:31:55 +02:00
* Calcola il margine del documento.
2018-12-23 16:16:59 +01:00
*
* @return float
*/
2019-09-12 09:31:55 +02:00
public function getMargineAttribute()
2018-12-23 16:16:59 +01:00
{
2019-09-12 09:31:55 +02:00
return $this->calcola('margine');
}
/**
* Restituisce il margine percentuale del documento.
*
* @return float
*/
public function getMarginePercentualeAttribute()
{
return $this->imponibile ? (1 - ($this->spesa / $this->totale_imponibile)) * 100 : 100;
2018-12-14 09:46:35 +01:00
}
2018-12-29 12:03:22 +01:00
2019-05-04 06:19:08 +02:00
public function delete()
{
$righe = $this->getRighe();
$can_delete = true;
foreach ($righe as $riga) {
$can_delete &= $riga->canDelete();
}
if (!$can_delete) {
throw new \InvalidArgumentException();
}
2019-05-04 06:19:08 +02:00
foreach ($righe as $riga) {
$riga->delete();
}
return parent::delete();
}
public function toArray()
{
$array = parent::toArray();
$result = array_merge($array, [
'spesa' => $this->spesa,
'imponibile' => $this->imponibile,
'sconto' => $this->sconto,
'totale_imponibile' => $this->totale_imponibile,
'iva' => $this->iva,
'totale' => $this->totale,
]);
return $result;
}
2020-01-11 13:35:34 +01:00
/**
* Costruisce una nuova collezione Laravel a partire da quelle indicate.
*
* @param array<\Illuminate\Support\Collection> ...$args
*
* @return \Illuminate\Support\Collection
*/
protected function mergeCollections(...$args)
{
$collection = collect($args);
return $collection->collapse();
}
/**
* Calcola la somma degli attributi indicati come parametri.
* Il metodo **non** deve essere adattato per ulteriori funzionalità: deve esclusivamente calcolare la somma richiesta in modo esplicito dagli argomenti.
*
* @param mixed ...$args
*
* @return float
*/
protected function calcola(...$args)
{
$result = 0;
foreach ($args as $arg) {
$result += $this->getRigheContabili()->sum($arg);
}
return $this->round($result);
}
2018-12-29 12:03:22 +01:00
/**
* Restituisce la collezione di righe e articoli con valori rilevanti per i conti.
*
* @return iterable
*/
protected function getRigheContabili()
{
2019-07-12 17:35:14 +02:00
return $this->getRighe();
2018-12-29 12:03:22 +01:00
}
/**
* Funzione per l'arrotondamento degli importi.
*
* @param float $value
*
* @return float
*/
protected function round($value)
{
$decimals = 2;
return round($value, $decimals);
}
2018-12-14 09:46:35 +01:00
}