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;
|
|
|
|
use Modules\Ritenute\RitenutaAcconto;
|
|
|
|
use Modules\Ritenute\RivalsaINPS;
|
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
|
|
|
|
2018-12-25 11:32:19 +01:00
|
|
|
public static function make(Document $document, $bypass = false)
|
2018-09-25 16:47:44 +02:00
|
|
|
{
|
2018-12-25 11:33:15 +01:00
|
|
|
return parent::make($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()
|
|
|
|
{
|
|
|
|
return $this->imponibile - $this->sconto;
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:15:34 +01:00
|
|
|
/**
|
|
|
|
* Restituisce il totale (imponibile + iva + rivalsa_inps) dell'elemento.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
2018-09-25 16:47:44 +02:00
|
|
|
public function getTotaleAttribute()
|
|
|
|
{
|
2018-11-23 17:50:05 +01:00
|
|
|
return $this->imponibile_scontato + $this->iva + $this->rivalsa_inps;
|
|
|
|
}
|
|
|
|
|
2018-12-29 11:15:34 +01:00
|
|
|
/**
|
|
|
|
* Restituisce il netto a pagare (totale - ritenuta_acconto) dell'elemento.
|
|
|
|
*
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function getNettoAttribute()
|
|
|
|
{
|
|
|
|
return $this->totale - $this->ritenuta_acconto;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 getRivalsaINPSAttribute()
|
|
|
|
{
|
2019-01-02 09:22:57 +01:00
|
|
|
return ($this->imponibile_scontato) / 100 * $this->rivalsa->percentuale;
|
2018-11-23 17:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getRitenutaAccontoAttribute()
|
|
|
|
{
|
2019-01-02 09:22:57 +01:00
|
|
|
$result = $this->imponibile_scontato;
|
|
|
|
|
|
|
|
if ($this->calcolo_ritenuta_acconto == 'IMP+RIV') {
|
|
|
|
$result += $this->rivalsainps;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result / 100 * $this->ritenuta->percentuale;
|
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-02 09:22:57 +01:00
|
|
|
return ($this->imponibile_scontato + $this->rivalsa_inps) * $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 della Rivalsa INPS.
|
2018-09-26 15:37:46 +02:00
|
|
|
*
|
|
|
|
* @param int $value
|
|
|
|
*/
|
2018-09-25 16:47:44 +02:00
|
|
|
public function setIdRivalsaINPSAttribute($value)
|
|
|
|
{
|
|
|
|
$this->attributes['idrivalsainps'] = $value;
|
2019-01-02 09:22:57 +01:00
|
|
|
$this->load('rivalsa');
|
2018-10-05 10:49:46 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-12-29 11:15:34 +01:00
|
|
|
* Imposta l'identificatore della Ritenuta d'Acconto.
|
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 setIdRitenutaAccontoAttribute($value)
|
2018-09-25 16:47:44 +02:00
|
|
|
{
|
2018-12-29 11:15:34 +01:00
|
|
|
$this->attributes['idritenutaacconto'] = $value;
|
2019-01-02 09:22:57 +01:00
|
|
|
$this->load('ritenuta');
|
2018-09-25 16:47:44 +02:00
|
|
|
}
|
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Save the model to the database.
|
|
|
|
*
|
|
|
|
* @param array $options
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function save(array $options = [])
|
|
|
|
{
|
|
|
|
// Fix dei campi statici
|
|
|
|
$this->fixSubtotale();
|
|
|
|
$this->fixSconto();
|
|
|
|
|
|
|
|
$this->fixIva();
|
|
|
|
$this->fixRitenutaAcconto();
|
|
|
|
$this->fixRivalsaINPS();
|
|
|
|
|
|
|
|
return parent::save($options);
|
|
|
|
}
|
|
|
|
|
|
|
|
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-25 16:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:37:46 +02:00
|
|
|
/**
|
2018-12-29 11:15:34 +01:00
|
|
|
* Effettua i conti per la Rivalsa INPS.
|
2018-09-26 15:37:46 +02:00
|
|
|
*/
|
2018-12-29 11:15:34 +01:00
|
|
|
protected function fixRivalsaINPS()
|
2018-09-25 16:47:44 +02:00
|
|
|
{
|
2019-01-02 09:22:57 +01:00
|
|
|
$this->attributes['rivalsainps'] = $this->rivalsa_inps;
|
2018-09-25 16:47:44 +02:00
|
|
|
}
|
|
|
|
|
2018-09-26 15:37:46 +02:00
|
|
|
/**
|
2018-12-29 11:15:34 +01:00
|
|
|
* Effettua i conti per la Ritenuta d'Acconto, basandosi sul valore del campo calcolo_ritenuta_acconto.
|
2018-09-26 15:37:46 +02:00
|
|
|
*/
|
2018-12-29 11:15:34 +01:00
|
|
|
protected function fixRitenutaAcconto()
|
2018-09-25 16:47:44 +02:00
|
|
|
{
|
2019-01-02 09:22:57 +01:00
|
|
|
$this->attributes['ritenutaacconto'] = $this->ritenuta_acconto;
|
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-01-02 09:22:57 +01:00
|
|
|
|
|
|
|
public function aliquota()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(Aliquota::class, 'idiva');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function rivalsa()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(RivalsaINPS::class, 'idrivalsainps');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function ritenuta()
|
|
|
|
{
|
|
|
|
return $this->belongsTo(RitenutaAcconto::class, 'idritenutaacconto');
|
|
|
|
}
|
2018-09-25 16:47:44 +02:00
|
|
|
}
|