1
0
mirror of https://github.com/devcode-it/openstamanager.git synced 2024-12-08 14:36:05 +01:00
openstamanager/lib/common.php

211 lines
5.3 KiB
PHP
Raw Normal View History

2018-07-17 12:05:21 +02: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-07-17 12:05:21 +02:00
2020-11-06 10:46:42 +01:00
/*
2018-07-17 12:05:21 +02:00
* Funzioni globali utilizzate per il funzionamento dei componenti indipendenti del progetto (moduli, plugin, stampe, ...).
*
* @since 2.4.2
*/
use Common\Components\Accounting;
2018-07-17 12:05:21 +02:00
/**
* 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);
}
/**
* 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;
2021-10-06 15:03:29 +02:00
$price -= $discount;
2018-07-17 12:05:21 +02:00
}
} else {
$result = floatval($data['sconto']);
}
if (!empty($data['qta'])) {
$result = $result * $data['qta'];
}
return $result;
}
2020-03-09 09:12:41 +01:00
/**
* Individua il valore della colonna order per i nuovi elementi di una tabella.
*
* @param $table
* @param $field
* @param $id
*
* @return mixed
*/
2018-09-24 10:51:48 +02:00
function orderValue($table, $field, $id)
{
return database()->fetchOne('SELECT IFNULL(MAX(`order`) + 1, 1) AS value FROM '.$table.' WHERE '.$field.' = '.prepare($id))['value'];
}
/**
* Ricalcola il riordinamento righe di una tabella.
*
* @param $table
*
* @return mixed
*/
function reorderRows($table, $field, $id)
{
$righe = database()->select($table, 'id', [$field => $id], ['order' => 'ASC']);
2021-02-23 11:40:27 +01:00
$i = 1;
2021-02-23 11:40:27 +01:00
foreach ($righe as $riga) {
database()->query('UPDATE '.$table.' SET `order`='.$i.' WHERE id='.prepare($riga['id']));
2021-02-23 11:40:27 +01:00
++$i;
}
2018-09-24 10:51:48 +02:00
}
/**
* Visualizza le informazioni relative allo sconto presente su una riga.
*
* @param bool $mostra_maggiorazione
*
* @return string|null
*/
function discountInfo(Accounting $riga, $mostra_maggiorazione = true)
{
if (empty($riga->sconto_unitario) || (!$mostra_maggiorazione && $riga->sconto_unitario < 0)) {
return null;
}
2020-07-02 10:11:33 +02:00
$text = $riga->sconto_unitario > 0 ? tr('sconto _TOT_ _TYPE_') : tr('maggiorazione _TOT__TYPE_');
2020-09-02 10:05:32 +02:00
$totale = !empty($riga->sconto_percentuale) ? $riga->sconto_percentuale : $riga->sconto_unitario_corrente;
return replace($text, [
2020-09-02 10:05:32 +02:00
'_TOT_' => Translator::numberToLocale(abs($totale)),
'_TYPE_' => !empty($riga->sconto_percentuale) ? '%' : currency(),
]);
}
2020-03-09 09:12:41 +01:00
/**
* Genera i riferimenti ai documenti del gestionale, attraverso l'interfaccia Common\ReferenceInterface.
*
* @param $document
* @param string $text Formato "Contenuto descrittivo _DOCUMENT_"
2020-03-09 09:12:41 +01:00
*
* @return string
*/
function reference($document, $text = null)
{
if (!empty($document) && !($document instanceof \Common\ReferenceInterface)) {
return null;
}
$extra = '';
$module_id = null;
$document_id = null;
if (empty($document)) {
$content = tr('non disponibile');
$extra = 'class="disabled"';
} else {
$module_id = $document->module;
$document_id = $document->id;
$content = $document->getReference();
}
2020-09-14 18:38:58 +02:00
$description = $text ?: tr('Rif. _DOCUMENT_', [
'_DOCUMENT_' => strtolower($content),
]);
return Modules::link($module_id, $document_id, $description, $description, $extra);
}
2020-03-09 09:12:41 +01:00
/**
* Funzione che gestisce il parsing di uno sconto combinato e la relativa trasformazione in sconto fisso.
* Esempio: (40 + 10) % = 44 %.
*
* @param $combinato
*
* @return float|int
*/
function parseScontoCombinato($combinato)
{
$sign = substr($combinato, 0, 1);
$original = $sign != '+' && $sign != '-' ? '+'.$combinato : $combinato;
$pieces = preg_split('/[+,-]+/', $original);
unset($pieces[0]);
$result = 1;
$text = $original;
foreach ($pieces as $piece) {
$sign = substr($text, 0, 1);
$text = substr($text, 1 + strlen($piece));
$result *= 1 - floatval($sign.$piece) / 100;
}
return (1 - $result) * 100;
}