2018-12-29 14:41:32 +01:00
< ? php
2020-09-07 15:04:06 +02:00
/*
* OpenSTAManager : il software gestionale open source per l ' assistenza tecnica e la fatturazione
2021-01-20 15:08:51 +01:00
* Copyright ( C ) DevCode s . r . l .
2020-09-07 15:04:06 +02:00
*
* 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-29 14:41:32 +01:00
namespace Modules\Ordini ;
2020-09-22 20:28:37 +02:00
use Common\Components\Component ;
2018-12-29 14:41:32 +01:00
use Common\Document ;
use Modules\Anagrafiche\Anagrafica ;
2019-07-22 18:35:13 +02:00
use Modules\DDT\DDT ;
2021-03-01 16:01:19 +01:00
use Modules\Interventi\Intervento ;
2024-02-08 13:10:46 +01:00
use Modules\Pagamenti\Pagamento ;
2018-12-29 14:41:32 +01:00
use Traits\RecordTrait ;
2020-03-03 10:33:32 +01:00
use Traits\ReferenceTrait ;
2018-12-29 14:41:32 +01:00
use Util\Generator ;
class Ordine extends Document
{
2020-03-03 10:33:32 +01:00
use ReferenceTrait ;
2018-12-29 14:41:32 +01:00
use RecordTrait ;
2020-09-22 09:44:35 +02:00
/**
* @ var bool Disabilita movimentazione automatica
*/
public static $movimenta_magazzino = false ;
2018-12-29 14:41:32 +01:00
protected $table = 'or_ordini' ;
2020-03-09 13:57:13 +01:00
protected $with = [
'tipo' ,
];
2018-12-29 14:41:32 +01:00
/**
* Crea un nuovo ordine .
*
2020-02-11 11:43:59 +01:00
* @ param string $data
2018-12-29 14:41:32 +01:00
*
* @ return self
*/
2022-11-28 09:27:25 +01:00
public static function build ( Anagrafica $anagrafica , Tipo $tipo_documento , $data , $id_segment = null )
2018-12-29 14:41:32 +01:00
{
2020-09-22 20:28:37 +02:00
$model = new static ();
2018-12-29 14:41:32 +01:00
2024-04-18 17:44:05 +02:00
$stato_documento = ( new Stato ()) -> getByField ( 'title' , 'Bozza' , \Models\Locale :: getPredefined () -> id );
2018-12-29 14:41:32 +01:00
$direzione = $tipo_documento -> dir ;
2022-11-28 09:27:25 +01:00
$id_segment = $id_segment ? : getSegmentPredefined ( $model -> getModule () -> id );
2018-12-29 14:41:32 +01:00
if ( $direzione == 'entrata' ) {
$conto = 'vendite' ;
} else {
$conto = 'acquisti' ;
}
// Tipo di pagamento e banca predefinite dall'anagrafica
2024-02-08 13:10:46 +01:00
$id_pagamento = Pagamento :: find ( $anagrafica [ 'idpagamento_' . $conto ]) -> id_record ;
2018-12-29 14:41:32 +01:00
// Se il ordine è un ordine 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 );
2022-11-28 09:27:25 +01:00
$model -> id_segment = $id_segment ;
2023-04-27 15:37:15 +02:00
$model -> idagente = $anagrafica -> idagente ;
2024-03-25 12:27:34 +01:00
2018-12-29 14:41:32 +01:00
// Salvataggio delle informazioni
$model -> data = $data ;
if ( ! empty ( $id_pagamento )) {
$model -> idpagamento = $id_pagamento ;
}
2023-02-16 14:53:42 +01:00
$model -> numero = static :: getNextNumero ( $data , $direzione , $id_segment );
2022-11-28 09:27:25 +01:00
$model -> numero_esterno = static :: getNextNumeroSecondario ( $data , $direzione , $id_segment );
2018-12-29 14:41:32 +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' ? 'Ordini cliente' : 'Ordini fornitore' ;
2018-12-29 14:41:32 +01:00
}
2019-03-29 12:46:17 +01:00
public function getDirezioneAttribute ()
2018-12-29 14:41:32 +01:00
{
2019-03-29 12:46:17 +01:00
return $this -> tipo -> dir ;
2018-12-29 14:41:32 +01:00
}
public function anagrafica ()
{
return $this -> belongsTo ( Anagrafica :: class , 'idanagrafica' );
}
public function tipo ()
{
return $this -> belongsTo ( Tipo :: class , 'idtipoordine' );
}
public function stato ()
{
return $this -> belongsTo ( Stato :: class , 'idstatoordine' );
}
public function articoli ()
{
return $this -> hasMany ( Components\Articolo :: class , 'idordine' );
}
public function righe ()
{
return $this -> hasMany ( Components\Riga :: class , 'idordine' );
}
2019-04-04 17:12:32 +02:00
public function sconti ()
2019-04-04 17:30:58 +02:00
{
return $this -> hasMany ( Components\Sconto :: class , 'idordine' );
}
2019-04-04 17:12:32 +02:00
2018-12-29 14:41:32 +01:00
public function descrizioni ()
{
return $this -> hasMany ( Components\Descrizione :: class , 'idordine' );
}
2021-03-01 15:01:02 +01:00
public function interventi ()
{
return $this -> hasMany ( Intervento :: class , 'id_ordine' );
}
2019-07-22 18:35:13 +02:00
/**
* Effettua un controllo sui campi del documento .
2020-07-14 13:30:25 +02:00
* Viene richiamato dalle modifiche alle righe del documento .
2019-07-22 18:35:13 +02:00
*/
2020-09-22 20:28:37 +02:00
public function triggerEvasione ( Component $trigger )
2019-07-22 18:35:13 +02:00
{
2019-09-13 10:07:54 +02:00
parent :: triggerEvasione ( $trigger );
2019-07-22 18:35:13 +02:00
if ( setting ( 'Cambia automaticamente stato ordini fatturati' )) {
$righe = $this -> getRighe ();
$qta_evasa = $righe -> sum ( 'qta_evasa' );
$qta = $righe -> sum ( 'qta' );
$parziale = $qta != $qta_evasa ;
2019-07-23 15:39:00 +02:00
$stato_attuale = $this -> stato ;
2019-07-22 18:35:13 +02:00
// Impostazione del nuovo stato
if ( $qta_evasa == 0 ) {
2020-12-21 12:53:19 +01:00
$descrizione = 'Accettato' ;
2024-04-18 17:44:05 +02:00
} elseif ( ! in_array ( $stato_attuale -> getTranslation ( 'title' , \Models\Locale :: getPredefined () -> id ), [ 'Parzialmente fatturato' , 'Fatturato' ]) && $trigger -> getDocument () instanceof DDT ) {
2019-07-22 18:35:13 +02:00
$descrizione = $parziale ? 'Parzialmente evaso' : 'Evaso' ;
} else {
$descrizione = $parziale ? 'Parzialmente fatturato' : 'Fatturato' ;
}
2024-04-18 17:44:05 +02:00
$stato = ( new Stato ()) -> getByField ( 'title' , $descrizione , \Models\Locale :: getPredefined () -> id );
2019-07-22 18:35:13 +02:00
$this -> stato () -> associate ( $stato );
$this -> save ();
}
}
2018-12-29 14:41:32 +01:00
// Metodi statici
/**
* Calcola il nuovo numero di ordine .
*
* @ param string $data
* @ param string $direzione
* @ param int $id_segment
*
* @ return string
*/
2023-02-16 14:53:42 +01:00
public static function getNextNumero ( $data , $direzione , $id_segment )
2018-12-29 14:41:32 +01:00
{
2023-08-04 14:54:28 +02:00
if ( $direzione == 'entrata' ) {
2023-02-16 14:53:42 +01:00
$maschera = '#' ;
} else {
$maschera = Generator :: getMaschera ( $id_segment );
2024-05-23 11:36:25 +02:00
if ( str_contains ( $maschera , 'm' )) {
2023-02-16 14:53:42 +01:00
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
'MONTH(data) = ' . prepare ( date ( 'm' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2023-02-16 14:53:42 +01:00
]);
2024-05-24 09:17:38 +02:00
} elseif ( str_contains ( $maschera , 'YYYY' ) or str_contains ( $maschera , 'yy' )) {
2023-02-16 14:53:42 +01:00
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2023-02-16 14:53:42 +01:00
]);
} else {
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2023-02-16 14:53:42 +01:00
]);
}
}
2023-08-04 14:54:28 +02:00
2019-01-10 20:10:47 +01:00
$numero = Generator :: generate ( $maschera , $ultimo );
2018-12-29 14:41:32 +01:00
2019-01-10 20:10:47 +01:00
return $numero ;
2018-12-29 14:41:32 +01:00
}
/**
* Calcola il nuovo numero secondario di ordine .
*
* @ param string $data
* @ param string $direzione
*
* @ return string
*/
2022-11-28 09:27:25 +01:00
public static function getNextNumeroSecondario ( $data , $direzione , $id_segment )
2018-12-29 14:41:32 +01:00
{
if ( $direzione == 'uscita' ) {
return '' ;
}
2022-11-28 09:27:25 +01:00
$maschera = Generator :: getMaschera ( $id_segment );
2018-12-29 14:41:32 +01:00
2024-05-23 11:36:25 +02:00
if ( str_contains ( $maschera , 'm' )) {
2022-02-08 16:06:15 +01:00
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero_esterno' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
'MONTH(data) = ' . prepare ( date ( 'm' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2022-02-08 16:06:15 +01:00
]);
2024-05-24 09:17:38 +02:00
} elseif ( str_contains ( $maschera , 'YYYY' ) or str_contains ( $maschera , 'yy' )) {
2022-02-08 16:06:15 +01:00
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero_esterno' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2022-02-08 16:06:15 +01:00
]);
} else {
$ultimo = Generator :: getPreviousFrom ( $maschera , 'or_ordini' , 'numero_esterno' , [
'YEAR(data) = ' . prepare ( date ( 'Y' , strtotime ( $data ))),
2024-03-01 11:49:21 +01:00
'idtipoordine IN (SELECT `id` FROM `or_tipiordine` WHERE `dir` = ' . prepare ( $direzione ) . ')' ,
2022-02-08 16:06:15 +01:00
]);
}
$numero = Generator :: generate ( $maschera , $ultimo );
2018-12-29 14:41:32 +01:00
2019-01-10 20:10:47 +01:00
return $numero ;
2018-12-29 14:41:32 +01:00
}
2020-03-03 10:33:32 +01:00
// Opzioni di riferimento
public function getReferenceName ()
{
2024-04-18 17:44:05 +02:00
return $this -> tipo -> getTranslation ( 'title' );
2020-03-03 10:33:32 +01:00
}
public function getReferenceNumber ()
{
2024-04-12 13:22:14 +02:00
return setting ( 'Visualizza numero ordine cliente' ) ? ( $this -> numero_cliente ? : ( $this -> numero_esterno ? : $this -> numero )) : ( $this -> numero_esterno ? : $this -> numero );
2020-03-03 10:33:32 +01:00
}
public function getReferenceDate ()
{
2024-04-12 13:22:14 +02:00
return setting ( 'Visualizza numero ordine cliente' ) ? ( $this -> data_cliente ? : $this -> data ) : $this -> data ;
2020-03-03 10:33:32 +01:00
}
2020-10-15 17:00:43 +02:00
public function getReferenceRagioneSociale ()
{
return $this -> anagrafica -> ragione_sociale ;
}
2018-12-29 14:41:32 +01:00
}