2018-07-17 12:05:21 +02:00
< ? php
/**
* Funzioni globali utilizzate per il funzionamento dei componenti indipendenti del progetto ( moduli , plugin , stampe , ... ) .
*
* @ since 2.4 . 2
*/
/**
* Esegue una somma precisa tra due interi / array .
*
* @ param array | float $first
* @ param array | float $second
* @ param int $decimals
*
* @ since 2.3
*
* @ return float
*/
function sum ( $first , $second = null , $decimals = 4 )
{
$first = ( array ) $first ;
$second = ( array ) $second ;
$array = array_merge ( $first , $second );
$result = 0 ;
2018-09-20 12:05:22 +02:00
$decimals = is_numeric ( $decimals ) ? $decimals : formatter () -> getPrecision ();
2018-07-17 12:05:21 +02:00
$bcadd = function_exists ( 'bcadd' );
foreach ( $array as $value ) {
$value = round ( $value , $decimals );
if ( $bcadd ) {
$result = bcadd ( $result , $value , $decimals );
} else {
$result += $value ;
}
}
return floatval ( $result );
}
2020-02-14 18:31:07 +01:00
/**
* @ param $field
* @ param $id_riga
* @ param $old_qta
* @ param $new_qta
* @ param $dir
*
* @ throws Exception
*
* @ return bool
*
* @ deprecated
*/
2018-07-17 12:05:21 +02:00
function controlla_seriali ( $field , $id_riga , $old_qta , $new_qta , $dir )
{
2018-09-20 12:05:22 +02:00
$dbo = database ();
2018-07-17 12:05:21 +02:00
$new_qta = abs ( $new_qta );
$old_qta = abs ( $old_qta );
if ( $old_qta >= $new_qta ) {
// Controllo sulla possibilità di rimuovere i seriali (se non utilizzati da documenti di vendita)
if ( $dir == 'uscita' && $new_qta < count ( seriali_non_rimuovibili ( $field , $id_riga , $dir ))) {
return false ;
} else {
// Controllo sul numero di seriali effettivi da rimuovere
$count = $dbo -> fetchArray ( 'SELECT COUNT(*) AS tot FROM mg_prodotti WHERE ' . $field . '=' . prepare ( $id_riga ))[ 0 ][ 'tot' ];
if ( $new_qta < $count ) {
$deletes = $dbo -> fetchArray ( " SELECT id FROM mg_prodotti WHERE serial NOT IN (SELECT serial FROM mg_prodotti WHERE dir = 'entrata' AND " . $field . '!=' . prepare ( $id_riga ) . ') AND ' . $field . '=' . prepare ( $id_riga ) . ' ORDER BY serial DESC LIMIT ' . abs ( $count - $new_qta ));
// Rimozione
foreach ( $deletes as $delete ) {
$dbo -> query ( 'DELETE FROM mg_prodotti WHERE id = ' . prepare ( $delete [ 'id' ]));
}
}
}
}
return true ;
}
/**
* Individua i seriali non rimuovibili poichè utilizzati in documenti rilasciati .
*
* @ param string $field
* @ param int $id_riga
* @ param string $dir
*
* @ return array
2020-02-14 18:31:07 +01:00
*
* @ deprecated
2018-07-17 12:05:21 +02:00
*/
function seriali_non_rimuovibili ( $field , $id_riga , $dir )
{
2018-09-20 12:05:22 +02:00
$dbo = database ();
2018-07-17 12:05:21 +02:00
$results = [];
if ( $dir == 'uscita' ) {
$results = $dbo -> fetchArray ( " SELECT serial FROM mg_prodotti WHERE serial IN (SELECT serial FROM mg_prodotti WHERE dir = 'entrata') AND " . $field . '=' . prepare ( $id_riga ));
}
return $results ;
}
/**
* Calcola gli sconti in modo automatico .
*
* @ param array $data
*
* @ return float
*/
function calcola_sconto ( $data )
{
if ( $data [ 'tipo' ] == 'PRC' ) {
$result = 0 ;
$price = floatval ( $data [ 'prezzo' ]);
$percentages = explode ( '+' , $data [ 'sconto' ]);
foreach ( $percentages as $percentage ) {
$discount = $price / 100 * floatval ( $percentage );
$result += $discount ;
$price -= $discount ;
}
} else {
$result = floatval ( $data [ 'sconto' ]);
}
if ( ! empty ( $data [ 'qta' ])) {
$result = $result * $data [ 'qta' ];
}
return $result ;
}
/**
* Restistuisce le informazioni sull ' eventuale riferimento ai documenti .
*
2019-02-01 18:19:13 +01:00
* @ param $info
* @ param $dir
* @ param array $ignore
*
* @ throws Exception
2018-07-17 12:05:21 +02:00
*
* @ return array
*/
function doc_references ( $info , $dir , $ignore = [])
{
2018-09-20 12:05:22 +02:00
$dbo = database ();
2018-07-17 12:05:21 +02:00
// Rimozione valori da non controllare
foreach ( $ignore as $field ) {
if ( isset ( $info [ $field ])) {
unset ( $info [ $field ]);
}
}
$module = null ;
$id = null ;
// DDT
2019-03-01 15:33:22 +01:00
if ( ! empty ( $info [ 'idddt' ])) {
2018-07-17 12:05:21 +02:00
$data = $dbo -> fetchArray ( " SELECT IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM dt_ddt WHERE id= " . prepare ( $info [ 'idddt' ]));
$module = ( $dir == 'entrata' ) ? 'Ddt di vendita' : 'Ddt di acquisto' ;
$id = $info [ 'idddt' ];
$document = tr ( 'Ddt' );
}
2019-03-01 15:33:22 +01:00
// Ordine
elseif ( ! empty ( $info [ 'idordine' ])) {
$data = $dbo -> fetchArray ( " SELECT IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM or_ordini WHERE id= " . prepare ( $info [ 'idordine' ]));
$module = ( $dir == 'entrata' ) ? 'Ordini cliente' : 'Ordini fornitore' ;
$id = $info [ 'idordine' ];
$document = tr ( 'Ordine' );
}
2018-07-17 12:05:21 +02:00
// Preventivo
elseif ( ! empty ( $info [ 'idpreventivo' ])) {
$data = $dbo -> fetchArray ( 'SELECT numero, data_bozza AS data FROM co_preventivi WHERE id=' . prepare ( $info [ 'idpreventivo' ]));
$module = 'Preventivi' ;
$id = $info [ 'idpreventivo' ];
$document = tr ( 'Preventivo' );
}
// Contratto
elseif ( ! empty ( $info [ 'idcontratto' ])) {
$data = $dbo -> fetchArray ( 'SELECT numero, data_bozza AS data FROM co_contratti WHERE id=' . prepare ( $info [ 'idcontratto' ]));
$module = 'Contratti' ;
$id = $info [ 'idcontratto' ];
$document = tr ( 'Contratto' );
}
// Intervento
elseif ( ! empty ( $info [ 'idintervento' ])) {
$data = $dbo -> fetchArray ( 'SELECT codice AS numero, IFNULL( (SELECT MIN(orario_inizio) FROM in_interventi_tecnici WHERE in_interventi_tecnici.idintervento=in_interventi.id), data_richiesta) AS data FROM in_interventi WHERE id=' . prepare ( $info [ 'idintervento' ]));
$module = 'Interventi' ;
$id = $info [ 'idintervento' ];
$document = tr ( 'Intervento' );
}
// Testo relativo
if ( ! empty ( $module ) && ! empty ( $id )) {
$document = Stringy\Stringy :: create ( $document ) -> toLowerCase ();
if ( ! empty ( $data )) {
$description = tr ( 'Rif. _DOC_ num. _NUM_ del _DATE_' , [
'_DOC_' => $document ,
'_NUM_' => $data [ 0 ][ 'numero' ],
'_DATE_' => Translator :: dateToLocale ( $data [ 0 ][ 'data' ]),
]);
} else {
$description = tr ( '_DOC_ di riferimento _ID_ eliminato' , [
'_DOC_' => $document -> upperCaseFirst (),
'_ID_' => $id ,
]);
}
return [
'module' => $module ,
'id' => $id ,
'description' => $description ,
];
}
return [];
}
2018-09-20 16:42:35 +02:00
function months ()
{
return [
1 => tr ( 'Gennaio' ),
2 => tr ( 'Febbraio' ),
3 => tr ( 'Marzo' ),
4 => tr ( 'Aprile' ),
5 => tr ( 'Maggio' ),
6 => tr ( 'Giugno' ),
7 => tr ( 'Luglio' ),
8 => tr ( 'Agosto' ),
9 => tr ( 'Settembre' ),
10 => tr ( 'Ottobre' ),
11 => tr ( 'Novembre' ),
12 => tr ( 'Dicembre' ),
];
}
2018-09-24 10:51:48 +02:00
function orderValue ( $table , $field , $id )
{
return database () -> fetchOne ( 'SELECT IFNULL(MAX(`order`) + 1, 0) AS value FROM ' . $table . ' WHERE ' . $field . ' = ' . prepare ( $id ))[ 'value' ];
}
2020-02-14 18:31:07 +01:00
/**
* Visualizza le informazioni relative allo sconto presente su una riga .
*
* @ param bool $mostra_maggiorazione
*
* @ return string | null
*/
function discountInfo ( \Common\Components\Row $riga , $mostra_maggiorazione = true )
{
if ( empty ( $riga -> sconto_unitario ) || ( ! $mostra_maggiorazione && $riga -> sconto_unitario < 0 )) {
return null ;
}
$text = $riga -> sconto_unitario > 0 ? tr ( 'sconto _TOT_ _TYPE_' ) : tr ( 'maggiorazione _TOT_ _TYPE_' );
return replace ( $text , [
'_TOT_' => Translator :: numberToLocale ( ! empty ( $riga -> sconto_percentuale ) ? $riga -> sconto_percentuale : $riga -> sconto_unitario ),
'_TYPE_' => ( ! empty ( $riga -> sconto_percentuale ) ? '%' : currency ()),
]);
}