openstamanager/include/src/Components/Row.php

248 lines
5.8 KiB
PHP
Raw Normal View History

2018-09-25 16:47:44 +02:00
<?php
2018-12-25 11:32:19 +01:00
namespace Common\Components;
2018-09-25 16:47:44 +02:00
2018-12-25 11:32:19 +01:00
use Common\Document;
2018-12-29 11:42:53 +01:00
use Illuminate\Database\Eloquent\Builder;
2019-01-02 09:22:57 +01:00
use Modules\Iva\Aliquota;
2018-09-25 17:13:23 +02:00
2018-09-26 12:06:24 +02:00
abstract class Row extends Description
2018-09-25 16:47:44 +02:00
{
2018-12-07 09:20:49 +01:00
protected $prezzo_unitario_vendita_riga = null;
2018-09-26 15:37:46 +02:00
2019-02-14 17:49:58 +01:00
protected $casts = [
'qta' => 'float',
//'qta_evasa' => 'float',
];
2019-01-02 14:15:16 +01:00
public static function build(Document $document, $bypass = false)
2018-09-25 16:47:44 +02:00
{
2019-01-02 14:15:16 +01:00
return parent::build($document, true);
2018-09-25 16:47:44 +02:00
}
2018-12-29 11:15:34 +01:00
// Attributi di contabilità
2018-11-23 17:50:05 +01:00
2018-12-29 11:15:34 +01:00
/**
* Restituisce l'imponibile dell'elemento.
*
* @return float
*/
2018-11-23 17:50:05 +01:00
public function getImponibileAttribute()
{
2018-12-29 11:15:34 +01:00
return $this->prezzo_unitario_vendita * $this->qta;
2018-11-23 17:50:05 +01:00
}
2018-12-29 11:15:34 +01:00
/**
2019-07-11 17:44:42 +02:00
* Restituisce il totale imponibile dell'elemento.
2018-12-29 11:15:34 +01:00
*
* @return float
*/
2019-07-11 17:44:42 +02:00
public function getTotaleImponibileAttribute()
2018-11-23 17:50:05 +01:00
{
$result = $this->prezzo_unitario_vendita >= 0 ? $this->imponibile : -$this->imponibile;
2019-02-12 17:21:27 +01:00
$result -= $this->sconto;
return $this->prezzo_unitario_vendita >= 0 ? $result : -$result;
2018-11-23 17:50:05 +01:00
}
2018-12-29 11:15:34 +01:00
/**
2019-02-15 12:12:44 +01:00
* Restituisce il totale (imponibile + iva) dell'elemento.
2018-12-29 11:15:34 +01:00
*
* @return float
*/
2018-09-25 16:47:44 +02:00
public function getTotaleAttribute()
{
2019-07-11 17:44:42 +02:00
return $this->totale_imponibile + $this->iva;
2018-12-29 11:15:34 +01:00
}
/**
* Restituisce la spesa (prezzo_unitario_acquisto * qta) relativa all'elemento.
*
* @return float
*/
2018-12-23 16:16:59 +01:00
public function getSpesaAttribute()
{
return $this->prezzo_unitario_acquisto * $this->qta;
}
2018-12-29 11:15:34 +01:00
/**
2019-09-12 09:31:55 +02:00
* Restituisce il margine totale (imponibile - spesa) relativo all'elemento.
2018-12-29 11:15:34 +01:00
*
* @return float
*/
2019-09-12 09:31:55 +02:00
public function getMargineAttribute()
2018-12-23 16:16:59 +01:00
{
return $this->totale_imponibile - $this->spesa;
2019-09-12 09:31:55 +02:00
}
/**
* Restituisce il margine percentuale relativo all'elemento.
*
* @return float
*/
public function getMarginePercentualeAttribute()
{
return (1 - ($this->spesa / $this->imponibile)) * 100;
2018-12-23 16:16:59 +01:00
}
2018-12-29 11:15:34 +01:00
// Attributi della componente
2018-11-23 17:50:05 +01:00
public function getIvaIndetraibileAttribute()
{
2019-01-02 09:22:57 +01:00
return $this->iva / 100 * $this->aliquota->indetraibile;
2018-11-23 17:50:05 +01:00
}
public function getIvaAttribute()
{
2019-07-11 17:44:42 +02:00
return ($this->totale_imponibile) * $this->aliquota->percentuale / 100;
2018-11-23 17:50:05 +01:00
}
public function getIvaDetraibileAttribute()
{
return $this->iva - $this->iva_indetraibile;
2018-09-25 16:47:44 +02:00
}
2018-12-29 11:15:34 +01:00
public function getSubtotaleAttribute()
{
return $this->imponibile;
}
2018-09-26 15:37:46 +02:00
/**
* Restituisce lo sconto della riga corrente in euro.
*
* @return float
*/
2018-09-25 16:47:44 +02:00
public function getScontoAttribute()
{
return calcola_sconto([
'sconto' => $this->sconto_unitario,
'prezzo' => $this->prezzo_unitario_vendita,
2018-09-25 16:47:44 +02:00
'tipo' => $this->tipo_sconto,
'qta' => $this->qta,
]);
}
2018-09-26 15:37:46 +02:00
/**
2019-01-02 09:22:57 +01:00
* Imposta l'identificatore dell'IVA.
2018-09-26 15:37:46 +02:00
*
2018-12-29 11:15:34 +01:00
* @param int $value
2018-09-26 15:37:46 +02:00
*/
2018-12-29 11:15:34 +01:00
public function setIdIvaAttribute($value)
2018-09-25 16:47:44 +02:00
{
2018-12-29 11:15:34 +01:00
$this->attributes['idiva'] = $value;
2019-01-02 09:22:57 +01:00
$this->load('aliquota');
2018-09-25 16:47:44 +02:00
}
2018-09-26 15:37:46 +02:00
/**
2019-07-12 17:35:14 +02:00
* Imposta il prezzo unitario della riga.
2018-09-26 15:37:46 +02:00
*
2018-12-29 11:15:34 +01:00
* @param float $value
2018-09-26 15:37:46 +02:00
*/
2018-12-29 11:15:34 +01:00
public function setPrezzoUnitarioVenditaAttribute($value)
2018-09-25 16:47:44 +02:00
{
2018-12-29 11:15:34 +01:00
$this->prezzo_unitario_vendita_riga = $value;
2018-09-26 15:37:46 +02:00
}
/**
2019-07-12 17:35:14 +02:00
* Restituisce il prezzo unitario della riga.
2018-09-26 15:37:46 +02:00
*/
2018-12-29 11:15:34 +01:00
public function getPrezzoUnitarioVenditaAttribute()
2018-09-26 15:37:46 +02:00
{
2018-12-29 11:15:34 +01:00
if (!isset($this->prezzo_unitario_vendita_riga)) {
$this->prezzo_unitario_vendita_riga = $this->attributes['subtotale'] / $this->qta;
2018-09-25 16:47:44 +02:00
}
2019-04-19 20:52:02 +02:00
return !is_nan($this->prezzo_unitario_vendita_riga) ? $this->prezzo_unitario_vendita_riga : 0;
2018-09-25 16:47:44 +02:00
}
2018-12-29 12:03:22 +01:00
/**
2019-04-19 20:47:55 +02:00
* Salva la riga, impostando i campi dipendenti dai singoli parametri.
2018-12-29 12:03:22 +01:00
*
* @return bool
*/
public function save(array $options = [])
{
// Fix dei campi statici
$this->fixSubtotale();
$this->fixSconto();
$this->fixIva();
return parent::save($options);
}
public function aliquota()
{
return $this->belongsTo(Aliquota::class, 'idiva');
}
2018-12-29 12:03:22 +01:00
protected static function boot($bypass = false)
{
parent::boot(true);
2019-05-04 03:28:33 +02:00
$table = parent::getTableName();
2018-12-29 12:03:22 +01:00
if (!$bypass) {
2019-05-04 03:28:33 +02:00
static::addGlobalScope('rows', function (Builder $builder) use ($table) {
$builder->whereNull($table.'.idarticolo')->orWhere($table.'.idarticolo', '=', 0);
2018-12-29 12:03:22 +01:00
});
2019-04-04 17:12:32 +02:00
2019-05-04 03:28:33 +02:00
static::addGlobalScope('not_discounts', function (Builder $builder) use ($table) {
$builder->where($table.'.is_sconto', '=', 0);
2019-04-04 17:12:32 +02:00
});
2018-12-29 12:03:22 +01:00
}
}
2018-09-26 15:37:46 +02:00
/**
2018-12-29 11:15:34 +01:00
* Effettua i conti per il subtotale della riga.
2018-09-26 15:37:46 +02:00
*/
2018-12-29 11:15:34 +01:00
protected function fixSubtotale()
2018-09-25 16:47:44 +02:00
{
2018-12-29 11:15:34 +01:00
$this->attributes['subtotale'] = $this->imponibile;
2018-09-26 15:37:46 +02:00
}
/**
* Effettua i conti per l'IVA.
*/
2018-09-26 12:06:24 +02:00
protected function fixIva()
2018-09-25 16:47:44 +02:00
{
2019-01-02 09:22:57 +01:00
$this->attributes['iva'] = $this->iva;
2018-09-25 16:47:44 +02:00
2019-01-02 09:22:57 +01:00
$descrizione = $this->aliquota->descrizione;
2018-12-29 11:42:53 +01:00
if (!empty($descrizione)) {
$this->attributes['desc_iva'] = $descrizione;
}
2018-09-26 15:37:46 +02:00
$this->fixIvaIndetraibile();
}
2018-09-26 12:06:24 +02:00
2018-09-26 15:37:46 +02:00
/**
* Effettua i conti per l'IVA indetraibile.
*/
protected function fixIvaIndetraibile()
{
2019-01-02 09:22:57 +01:00
$this->attributes['iva_indetraibile'] = $this->iva_indetraibile;
2018-09-26 12:06:24 +02:00
}
2018-09-26 15:37:46 +02:00
/**
2018-12-29 11:15:34 +01:00
* Effettua i conti per lo sconto totale.
2018-09-26 15:37:46 +02:00
*/
2018-12-29 11:15:34 +01:00
protected function fixSconto()
2018-09-26 12:06:24 +02:00
{
2018-12-29 11:15:34 +01:00
$this->attributes['sconto'] = $this->sconto;
2018-09-25 16:47:44 +02:00
}
2019-02-12 17:21:27 +01:00
/**
* Azione personalizzata per la copia dell'oggetto (dopo la copia).
*
* @param $original
*/
protected function customAfterDataCopiaIn($original)
{
$this->prezzo_unitario_vendita = $original->prezzo_unitario_vendita;
parent::customAfterDataCopiaIn($original);
}
2018-09-25 16:47:44 +02:00
}