openstamanager/modules/ddt/src/DDT.php

270 lines
6.8 KiB
PHP
Raw Normal View History

2018-12-29 14:24:27 +01:00
<?php
namespace Modules\DDT;
use Auth;
use Common\Components\Description;
2018-12-29 14:24:27 +01:00
use Common\Document;
use Modules\Anagrafiche\Anagrafica;
use Traits\RecordTrait;
use Traits\ReferenceTrait;
2018-12-29 14:24:27 +01:00
use Util\Generator;
class DDT extends Document
{
use ReferenceTrait;
2018-12-29 14:24:27 +01:00
use RecordTrait;
protected $table = 'dt_ddt';
protected $casts = [
'bollo' => 'float',
'peso' => 'float',
'volume' => 'float',
];
protected $with = [
'tipo',
];
2018-12-29 14:24:27 +01:00
/**
* Crea un nuovo ddt.
*
* @param string $data
2018-12-29 14:24:27 +01:00
*
* @return self
*/
2019-01-02 14:15:16 +01:00
public static function build(Anagrafica $anagrafica, Tipo $tipo_documento, $data)
2018-12-29 14:24:27 +01:00
{
2019-01-02 14:15:16 +01:00
$model = parent::build();
2018-12-29 14:24:27 +01:00
$user = Auth::user();
2018-12-29 14:24:27 +01:00
$stato_documento = Stato::where('descrizione', 'Bozza')->first();
$id_anagrafica = $anagrafica->id;
$direzione = $tipo_documento->dir;
$database = database();
if ($direzione == 'entrata') {
$conto = 'vendite';
} else {
$conto = 'acquisti';
}
// Tipo di pagamento e banca predefinite dall'anagrafica
$id_pagamento = $database->fetchOne('SELECT id FROM co_pagamenti WHERE id = :id_pagamento', [
':id_pagamento' => $anagrafica['idpagamento_'.$conto],
2018-12-29 14:24:27 +01:00
])['id'];
// Se il ddt è un ddt cliente e non è stato associato un pagamento predefinito al cliente leggo il pagamento dalle impostazioni
if ($direzione == 'entrata' && empty($id_pagamento)) {
$id_pagamento = setting('Tipo di pagamento predefinito');
}
$model->anagrafica()->associate($anagrafica);
$model->tipo()->associate($tipo_documento);
$model->stato()->associate($stato_documento);
$model->save();
// Salvataggio delle informazioni
$model->data = $data;
if (!empty($id_pagamento)) {
$model->idpagamento = $id_pagamento;
}
$model->numero = static::getNextNumero($data, $direzione);
$model->numero_esterno = static::getNextNumeroSecondario($data, $direzione);
// Imposto, come sede aziendale, la prima sede disponibile come utente
if ($direzione == 'entrata') {
$model->idsede_partenza = $user->sedi[0];
} else {
$model->idsede_destinazione = $user->sedi[0];
}
2018-12-29 14:24:27 +01:00
$model->save();
return $model;
}
/**
* Restituisce il nome del modulo a cui l'oggetto è collegato.
*
* @return string
*/
public function getModuleAttribute()
{
2019-03-29 12:46:17 +01:00
return $this->direzione == 'entrata' ? 'Ddt di vendita' : 'DDT di acquisto';
2018-12-29 14:24:27 +01:00
}
2019-03-29 12:46:17 +01:00
public function getDirezioneAttribute()
2018-12-29 14:24:27 +01:00
{
2019-03-29 12:46:17 +01:00
return $this->tipo->dir;
2018-12-29 14:24:27 +01:00
}
/**
* Restituisce il peso calcolato sulla base degli articoli del documento.
*
* @return float
*/
public function getPesoCalcolatoAttribute()
{
$righe = $this->getRighe();
$peso_lordo = $righe->sum(function ($item) {
return $item->isArticolo() ? $item->articolo->peso_lordo * $item->qta : 0;
});
return $peso_lordo;
}
/**
* Restituisce il volume calcolato sulla base degli articoli del documento.
*
* @return float
*/
public function getVolumeCalcolatoAttribute()
{
$righe = $this->getRighe();
$volume = $righe->sum(function ($item) {
return $item->isArticolo() ? $item->articolo->volume * $item->qta : 0;
});
return $volume;
}
2018-12-29 14:24:27 +01:00
public function anagrafica()
{
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
}
public function tipo()
{
return $this->belongsTo(Tipo::class, 'idtipoddt');
}
public function stato()
{
return $this->belongsTo(Stato::class, 'idstatoddt');
}
public function articoli()
{
return $this->hasMany(Components\Articolo::class, 'idddt');
}
public function righe()
{
return $this->hasMany(Components\Riga::class, 'idddt');
}
2019-04-04 17:12:32 +02:00
public function sconti()
{
return $this->hasMany(Components\Sconto::class, 'idddt');
}
2018-12-29 14:24:27 +01:00
public function descrizioni()
{
return $this->hasMany(Components\Descrizione::class, 'idddt');
}
/**
* Effettua un controllo sui campi del documento.
* Viene richiamatp dalle modifiche alle righe del documento.
*/
2019-09-13 10:07:54 +02:00
public function triggerEvasione(Description $trigger)
{
2019-09-13 10:07:54 +02:00
parent::triggerEvasione($trigger);
if (setting('Cambia automaticamente stato ddt fatturati')) {
$righe = $this->getRighe();
$qta_evasa = $righe->sum('qta_evasa');
$qta = $righe->sum('qta');
$parziale = $qta != $qta_evasa;
// Impostazione del nuovo stato
if ($qta_evasa == 0) {
$descrizione = 'Bozza';
} else {
$descrizione = $parziale ? 'Parzialmente fatturato' : 'Fatturato';
}
$stato = Stato::where('descrizione', $descrizione)->first();
$this->stato()->associate($stato);
$this->save();
}
}
2018-12-29 14:24:27 +01:00
// Metodi statici
/**
* Calcola il nuovo numero di ddt.
*
* @param string $data
* @param string $direzione
* @param int $id_segment
*
* @return string
*/
public static function getNextNumero($data, $direzione)
{
2019-01-10 20:10:47 +01:00
$maschera = '#';
2018-12-29 14:24:27 +01:00
2019-01-10 20:10:47 +01:00
$ultimo = Generator::getPreviousFrom($maschera, 'dt_ddt', 'numero', [
'YEAR(data) = '.prepare(date('Y', strtotime($data))),
'idtipoddt IN (SELECT id FROM dt_tipiddt WHERE dir = '.prepare($direzione).')',
2018-12-29 14:24:27 +01:00
]);
2019-01-10 20:10:47 +01:00
$numero = Generator::generate($maschera, $ultimo);
2018-12-29 14:24:27 +01:00
2019-01-10 20:10:47 +01:00
return $numero;
2018-12-29 14:24:27 +01:00
}
/**
* Calcola il nuovo numero secondario di ddt.
*
* @param string $data
* @param string $direzione
*
* @return string
*/
public static function getNextNumeroSecondario($data, $direzione)
{
if ($direzione == 'uscita') {
return '';
}
$maschera = setting('Formato numero secondario ddt');
2019-01-10 20:10:47 +01:00
$ultimo = Generator::getPreviousFrom($maschera, 'dt_ddt', 'numero_esterno', [
'YEAR(data) = '.prepare(date('Y', strtotime($data))),
'idtipoddt IN (SELECT id FROM dt_tipiddt WHERE dir = '.prepare($direzione).')',
2018-12-29 14:24:27 +01:00
]);
2019-01-10 20:10:47 +01:00
$numero = Generator::generate($maschera, $ultimo, 1, Generator::dateToPattern($data));
2018-12-29 14:24:27 +01:00
2019-01-10 20:10:47 +01:00
return $numero;
2018-12-29 14:24:27 +01:00
}
// Opzioni di riferimento
public function getReferenceName()
{
return $this->tipo->descrizione;
}
public function getReferenceNumber()
{
return $this->numero_esterno ?: $this->numero;
}
public function getReferenceDate()
{
return $this->data;
}
2018-12-29 14:24:27 +01:00
}