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
|
|
|
|
* Copyright (C) DevCode s.n.c.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
*/
|
2020-09-22 20:28:37 +02:00
|
|
|
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;
|
|
|
|
$price -= $discount;
|
|
|
|
}
|
|
|
|
} 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, 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
|
|
|
|
*/
|
2020-09-22 20:28:37 +02:00
|
|
|
function discountInfo(Accounting $riga, $mostra_maggiorazione = true)
|
2020-02-14 18:31:07 +01:00
|
|
|
{
|
|
|
|
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;
|
2020-02-14 18:31:07 +01:00
|
|
|
|
|
|
|
return replace($text, [
|
2020-09-02 10:05:32 +02:00
|
|
|
'_TOT_' => Translator::numberToLocale(abs($totale)),
|
|
|
|
'_TYPE_' => !empty($riga->sconto_percentuale) ? '%' : currency(),
|
2020-02-14 18:31:07 +01:00
|
|
|
]);
|
|
|
|
}
|
2020-03-03 10:33:32 +01:00
|
|
|
|
2020-03-09 09:12:41 +01:00
|
|
|
/**
|
|
|
|
* Genera i riferimenti ai documenti del gestionale, attraverso l'interfaccia Common\ReferenceInterface.
|
|
|
|
*
|
|
|
|
* @param $document
|
2020-07-06 13:19:20 +02:00
|
|
|
* @param string $text Formato "Contenuto descrittivo _DOCUMENT_"
|
2020-03-09 09:12:41 +01:00
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
2020-07-06 13:19:20 +02:00
|
|
|
function reference($document, $text = null)
|
2020-03-03 10:33:32 +01:00
|
|
|
{
|
|
|
|
if (!empty($document) && !($document instanceof \Common\ReferenceInterface)) {
|
2020-07-02 15:27:18 +02:00
|
|
|
return null;
|
2020-03-03 10:33:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$extra = '';
|
|
|
|
$module_id = null;
|
|
|
|
$document_id = null;
|
|
|
|
|
|
|
|
if (empty($document)) {
|
2020-07-02 15:27:18 +02:00
|
|
|
$content = tr('non disponibile');
|
2020-03-03 10:33:32 +01:00
|
|
|
$extra = 'class="disabled"';
|
|
|
|
} else {
|
|
|
|
$module_id = $document->module;
|
|
|
|
$document_id = $document->id;
|
|
|
|
|
2020-07-02 15:27:18 +02:00
|
|
|
$content = $document->getReference();
|
2020-03-03 10:33:32 +01:00
|
|
|
}
|
|
|
|
|
2020-09-14 18:38:58 +02:00
|
|
|
$description = $text ?: tr('Rif. _DOCUMENT_', [
|
2020-07-02 15:27:18 +02:00
|
|
|
'_DOCUMENT_' => strtolower($content),
|
|
|
|
]);
|
|
|
|
|
2020-03-03 10:33:32 +01:00
|
|
|
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;
|
|
|
|
}
|