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
|
|
|
/**
|
|
|
|
* Restituisce l'imponibile scontato dell'elemento.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2018-11-23 17:50:05 +01:00
|
|
|
public function getImponibileScontatoAttribute()
|
|
|
|
{
|
2019-02-12 17:21:27 +01:00
|
|
|
$result = $this->prezzo_unitario_vendita > 0 ? $this->imponibile : -$this->imponibile;
|
|
|
|
|
|
|
|
$result -= $this->sconto;
|
|
|
|
|
2019-03-18 12:16:55 +01:00
|
|
|
return $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-02-15 12:12:44 +01:00
|
|
|
return $this->imponibile_scontato + $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
|
|
|
/**
|
|
|
|
* Restituisce il gaudagno totale (imponibile_scontato - spesa) relativo all'elemento.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2018-12-23 16:16:59 +01:00
|
|
|
public function getGuadagnoAttribute()
|
|
|
|
{
|
|
|
|
return $this->imponibile_scontato - $this->spesa;
|
|
|
|
}
|
|
|
|
|
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-01-05 12:06:17 +01:00
|
|
|
return ($this->imponibile_scontato) * $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,
|
2018-11-21 17:45:06 +01:00
|
|
|
'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
|
|
|
/**
|
2018-12-29 11:15:34 +01:00
|
|
|
* Imposta il costo 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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-29 11:15:34 +01:00
|
|
|
* Restituisce il costo 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
|
|
|
}
|
|
|
|
|
2018-12-29 11:15:34 +01:00
|
|
|
return $this->prezzo_unitario_vendita_riga;
|
2018-09-25 16:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-12-29 12:03:22 +01:00
|
|
|
/**
|
2019-02-12 17:21:27 +01:00
|
|
|
* Salva la riga, impostando i campi dipendenti dai parametri singoli.
|
2018-12-29 12:03:22 +01:00
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
// Fix dei campi statici
|
|
|
|
$this->fixSubtotale();
|
|
|
|
$this->fixSconto();
|
|
|
|
|
|
|
|
$this->fixIva();
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
2019-01-06 14:18:48 +01:00
|
|
|
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);
|
|
|
|
|
|
|
|
if (!$bypass) {
|
|
|
|
static::addGlobalScope('rows', function (Builder $builder) {
|
|
|
|
$builder->whereNull('idarticolo')->orWhere('idarticolo', '=', 0);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|