openstamanager/modules/statistiche/edit.php

803 lines
33 KiB
PHP
Raw Normal View History

<?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/>.
*/
include_once __DIR__.'/../../core.php';
2024-03-05 16:01:45 +01:00
use Models\Plugin;
2024-03-22 15:52:24 +01:00
use Modules\Statistiche\Stats;
2019-07-05 17:26:58 +02:00
echo '
2022-07-07 15:31:53 +02:00
<script src="'.base_path().'/assets/dist/js/chartjs/chart.min.js"></script>
<script src="'.$structure->fileurl('js/functions.js').'"></script>
<script src="'.$structure->fileurl('js/calendar.js').'"></script>
<script src="'.$structure->fileurl('js/manager.js').'"></script>
<script src="'.$structure->fileurl('js/stat.js').'"></script>
<script src="'.$structure->fileurl('js/stats/line_chart.js').'"></script>';
$start = $_SESSION['period_start'];
$end = $_SESSION['period_end'];
2024-07-02 12:39:01 +02:00
$translated_months = [tr('Gennaio'), tr('Febbraio'), tr('Marzo'), tr('Aprile'), tr('Maggio'), tr('Giugno'), tr('Luglio'), tr('Agosto'), tr('Settembre'), tr('Ottobre'), tr('Novembre'), tr('Dicembre')];
2024-05-17 16:42:59 +02:00
// Fatturato
echo '
2024-05-17 16:42:59 +02:00
<div class="card card-info">
2024-05-16 18:02:50 +02:00
<div class="card-header">
2024-05-17 16:42:59 +02:00
<h4 class="card-title">'.tr('Vendite e acquisti').'</h4>
2024-05-16 18:02:50 +02:00
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
2019-07-11 12:04:22 +02:00
<i class="fa fa-minus"></i>
</button>
</div>
</div>
2024-05-17 16:42:59 +02:00
<div class="card-body">
<div class="card card-warning collapsed-card">
<div class="card-header">
<h4 class="card-title">'.tr('Periodi temporali').'</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-plus"></i>
</button>
</div>
</div>
2024-05-17 16:42:59 +02:00
<div class="card-body" id="calendars">
2024-06-25 17:52:27 +02:00
<div class="row">
<button class="btn btn-warning btn-sm" onclick="add_calendar()">
<i class="fa fa-plus"></i> '.tr('Aggiungi periodo').'
</button>
</div>
<br>
2024-05-17 16:42:59 +02:00
</div>
</div>
2024-05-17 16:42:59 +02:00
<canvas id="fatturato" height="50"></canvas>
</div>
</div>';
2024-05-17 16:42:59 +02:00
// Script per il grafico del fatturato
echo '
<script>
2019-07-11 12:04:22 +02:00
start = moment("'.$start.'");
end = moment("'.$end.'");
var chart_options = {
type: "line",
data: {
labels: [],
datasets: [],
},
options: {
responsive: true,
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataset = data.datasets[tooltipItem.datasetIndex];
var label = dataset.labels ? dataset.labels[tooltipItem.index] : "";
if (label) {
label += ": ";
}
2019-07-11 12:04:22 +02:00
label += tooltipItem.yLabel;
2019-07-11 12:04:22 +02:00
return label;
}
2019-07-11 12:04:22 +02:00
}
2024-05-16 18:02:50 +02:00
},
2020-07-20 09:15:44 +02:00
elements: {
line: {
tension: 0
}
},
2020-07-09 18:50:10 +02:00
annotation: {
annotations: [{
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: 0,
label: {
2024-06-25 17:52:27 +02:00
enabled: true,
2020-07-09 18:50:10 +02:00
}
}]
},
hover: {
mode: "nearest",
intersect: false
},
2019-07-11 12:04:22 +02:00
scales: {
2023-09-06 17:37:44 +02:00
x: {
2020-07-09 18:50:10 +02:00
display: true,
2024-05-16 18:02:50 +02:00
title: {
2020-07-09 18:50:10 +02:00
display: true,
2024-05-16 18:02:50 +02:00
text: "'.tr('Periodo').'"
2020-07-09 18:50:10 +02:00
}
2023-09-06 17:37:44 +02:00
},
y: {
2020-07-09 18:50:10 +02:00
display: true,
2024-05-16 18:02:50 +02:00
title: {
2020-07-09 18:50:10 +02:00
display: true,
2024-05-16 18:02:50 +02:00
text: "'.tr('Andamento').'"
2020-07-09 18:50:10 +02:00
},
2019-07-11 12:04:22 +02:00
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
return \''.html_entity_decode(currency()).' \' + value;
2019-07-11 12:04:22 +02:00
}
}
2023-09-06 17:37:44 +02:00
}
2019-07-11 12:04:22 +02:00
},
}
};
var info = {
url: "'.str_replace('edit.php', '', $structure->fileurl('edit.php')).'",
id_module: globals.id_module,
id_record: globals.id_record,
start_date: globals.start_date,
end_date: globals.end_date,
}
var manager = new Manager(info);
var chart_fatturato, chart_acquisti;
$(document).ready(function() {
2019-07-11 12:04:22 +02:00
var fatturato_canvas = document.getElementById("fatturato").getContext("2d");
2019-07-11 12:04:22 +02:00
chart_fatturato = new Chart(fatturato_canvas, chart_options);
2019-07-11 12:04:22 +02:00
add_calendar();
});
2019-07-11 12:04:22 +02:00
function init_calendar(calendar) {
var fatturato = new LineChart(calendar, "actions.php", {op: "fatturato"}, chart_fatturato);
var acquisti = new LineChart(calendar, "actions.php", {op: "acquisti"}, chart_fatturato);
calendar.addElement(fatturato);
calendar.addElement(acquisti);
}
</script>';
// Clienti top
$clienti = $dbo->fetchArray('SELECT
SUM(IF(`reversed`=1, - (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))) AS totale,
(SELECT
COUNT(*)
FROM
`co_documenti`
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
WHERE
`co_documenti`.`idanagrafica` = `an_anagrafiche`.`idanagrafica` AND `co_documenti`.`data` BETWEEN '.prepare($start).' AND '.prepare($end)." AND `co_tipidocumento`.`dir`='entrata' AND `zz_segments`.`autofatture`=0) AS qta,
`an_anagrafiche`.`idanagrafica`,
`an_anagrafiche`.`ragione_sociale`
FROM
`co_documenti`
INNER JOIN `co_statidocumento` ON `co_statidocumento`.`id` = `co_documenti`.`idstatodocumento`
2024-03-22 15:52:24 +01:00
LEFT JOIN `co_statidocumento_lang` ON (`co_statidocumento_lang`.`id_record` = `co_statidocumento`.`id` AND `co_statidocumento_lang`.`id_lang` = ".prepare(Models\Locale::getDefault()->id).")
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
INNER JOIN `an_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`co_documenti`.`idanagrafica`
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
WHERE
`co_tipidocumento`.`dir`='entrata'
2024-04-18 17:44:05 +02:00
AND `co_statidocumento_lang`.`title` IN('Pagato', 'Parzialmente pagato', 'Emessa')
AND `co_documenti`.`data` BETWEEN ".prepare($start).' AND '.prepare($end).'
AND `zz_segments`.`autofatture`=0
GROUP BY
`an_anagrafiche`.`idanagrafica`
ORDER BY
`totale` DESC LIMIT 20');
2024-02-13 15:50:26 +01:00
$totale = $dbo->fetchArray('SELECT
SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))) AS totale
FROM
`co_documenti`
INNER JOIN `co_statidocumento` ON `co_statidocumento`.`id` = `co_documenti`.`idstatodocumento`
2024-03-22 15:52:24 +01:00
LEFT JOIN `co_statidocumento_lang` ON (`co_statidocumento_lang`.`id_record` = `co_statidocumento`.`id` AND `co_statidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).")
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
WHERE
2024-04-18 17:44:05 +02:00
`co_statidocumento_lang`.`title` IN ('Pagato', 'Parzialmente pagato', 'Emessa')
AND `co_tipidocumento`.`dir`='entrata'
AND `co_documenti`.`data` BETWEEN ".prepare($start).' AND '.prepare($end).'
AND `zz_segments`.`autofatture`=0');
2017-09-18 18:11:19 +02:00
echo '
<div class="row">
2018-02-20 16:46:23 +01:00
<div class="col-md-6">
2024-05-17 16:42:59 +02:00
<div class="card card-info">
2024-05-16 18:02:50 +02:00
<div class="card-header">
<h4 class="card-title">'.tr('I 20 clienti TOP per il periodo').': '.Translator::dateToLocale($start).' - '.Translator::dateToLocale($end).'</h4>
2024-05-16 18:02:50 +02:00
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
2024-05-16 18:02:50 +02:00
<div class="card-body" height="500">';
if (!empty($clienti)) {
echo '
2017-09-18 18:11:19 +02:00
<table class="table table-striped">
<tr>
<th>'.tr('Ragione sociale').'</th>
2022-06-30 11:34:26 +02:00
<th class="text-right" width="100">'.tr('N. fatture').'</th>
<th class="text-right" width="120">'.tr('Totale').'<span class="tip" title="'.tr('Valori iva esclusa').'"> <i class="fa fa-question-circle-o" aria-hidden="true"></i></span></th>
<th class="text-right" width="120">'.tr('Percentuale').'<span class="tip" title="'.tr('Incidenza sul fatturato').'">&nbsp;<i class="fa fa-question-circle-o" aria-hidden="true"></i></span></th>
2017-09-18 18:11:19 +02:00
</tr>';
foreach ($clienti as $cliente) {
echo '
2017-09-18 18:11:19 +02:00
<tr>
<td>'.Modules::link('Anagrafiche', $cliente['idanagrafica'], $cliente['ragione_sociale']).'</td>
<td class="text-right">'.intval($cliente['qta']).'</td>
<td class="text-right">'.moneyFormat($cliente['totale'], 2).'</td>
2023-12-05 10:39:26 +01:00
<td class="text-right">'.Translator::numberToLocale($cliente['totale'] * 100 / ($totale[0]['totale'] != 0 ? $totale[0]['totale'] : 1), 2).' %</td>
2017-09-18 18:11:19 +02:00
</tr>';
}
echo '
2017-09-18 18:11:19 +02:00
</table>';
} else {
echo '
2019-06-26 11:42:11 +02:00
<p>'.tr('Nessuna vendita').'...</p>';
}
echo '
</div>
</div>
</div>';
// Articoli più venduti
2024-04-09 12:18:08 +02:00
$articoli = $dbo->fetchArray('SELECT
SUM(IF(`reversed`=1, -`co_righe_documenti`.`qta`, `co_righe_documenti`.`qta`)) AS qta,
SUM(IF(`reversed`=1, -(`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))) AS totale,
`mg_articoli`.`id`,
`mg_articoli`.`codice`,
2024-06-27 09:13:40 +02:00
`mg_articoli_lang`.`title` as descrizione,
`mg_articoli`.`um`
FROM
`co_documenti`
INNER JOIN `co_statidocumento` ON `co_statidocumento`.`id` = `co_documenti`.`idstatodocumento`
2024-04-09 12:18:08 +02:00
LEFT JOIN `co_statidocumento_lang` ON `co_statidocumento_lang`.`id_record` = `co_statidocumento`.`id` AND `co_statidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).'
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
INNER JOIN `mg_articoli` ON `mg_articoli`.`id`=`co_righe_documenti`.`idarticolo`
2024-04-09 12:18:08 +02:00
LEFT JOIN `mg_articoli_lang` ON (`mg_articoli_lang`.`id_record`=`mg_articoli`.`id` AND `mg_articoli_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).")
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
WHERE
`co_tipidocumento`.`dir`='entrata'
2024-04-18 17:44:05 +02:00
AND `co_statidocumento_lang`.`title` IN ('Pagato', 'Parzialmente pagato', 'Emessa')
2024-04-09 12:18:08 +02:00
AND `co_documenti`.`data` BETWEEN ".prepare($start).' AND '.prepare($end).'
AND `zz_segments`.`autofatture`=0
GROUP BY
`co_righe_documenti`.`idarticolo`
ORDER BY
2024-04-09 12:18:08 +02:00
`qta` DESC LIMIT 20');
2024-04-09 12:18:08 +02:00
$totale = $dbo->fetchArray('SELECT
SUM(IF(`reversed`=1, - `co_righe_documenti`.`qta`, `co_righe_documenti`.`qta`)) AS totale_qta,
SUM(IF(`reversed`=1, - (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`), (`co_righe_documenti`.`subtotale` - `co_righe_documenti`.`sconto`))) AS totale
FROM
`co_documenti`
INNER JOIN `co_statidocumento` ON `co_statidocumento`.`id` = `co_documenti`.`idstatodocumento`
2024-04-09 12:18:08 +02:00
LEFT JOIN `co_statidocumento_lang` ON `co_statidocumento_lang`.`id_record` = `co_statidocumento`.`id` AND `co_statidocumento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id)."
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `co_righe_documenti` ON `co_righe_documenti`.`iddocumento`=`co_documenti`.`id`
INNER JOIN `mg_articoli` ON `mg_articoli`.`id`=`co_righe_documenti`.`idarticolo`
INNER JOIN `zz_segments` ON `co_documenti`.`id_segment`=`zz_segments`.`id`
WHERE
`co_tipidocumento`.`dir`='entrata'
2024-04-18 17:44:05 +02:00
AND `co_statidocumento_lang`.`title` IN ('Pagato', 'Parzialmente pagato', 'Emessa')
2024-04-09 12:18:08 +02:00
AND `co_documenti`.`data` BETWEEN ".prepare($start).' AND '.prepare($end).'
AND `zz_segments`.`autofatture`=0');
echo '
2018-02-20 16:46:23 +01:00
<div class="col-md-6">
2024-05-17 16:42:59 +02:00
<div class="card card-info">
2024-05-16 18:02:50 +02:00
<div class="card-header">
<h4 class="card-title">'.tr('I 20 articoli più venduti per il periodo').': '.Translator::dateToLocale($start).' - '.Translator::dateToLocale($end).'</h4>
2024-05-16 18:02:50 +02:00
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
2024-05-16 18:02:50 +02:00
<div class="card-body" height="500">';
if (!empty($articoli)) {
echo '
2017-09-18 18:11:19 +02:00
<table class="table table-striped">
<tr>
<th>'.tr('Articolo').'</th>
2024-05-16 18:02:50 +02:00
<th class="text-right" width="100">'.tr('N. articoli').'<span class="tip" title="'.tr('Numero di articoli venduti').'"> <i class="fa fa-question-circle" aria-hidden="true"></i></span></th>
<th class="text-right" width="120">'.tr('Percentuale').'<span class="tip" title="'.tr('Incidenza sul numero di articoli').'"> <i class="fa fa-question-circle-o" aria-hidden="true"></i></span></th>
2022-06-30 11:34:26 +02:00
<th class="text-right" width="120">'.tr('Totale').'<span class="tip" title="'.tr('Valori iva esclusa').'"> <i class="fa fa-question-circle-o" aria-hidden="true"></i></span></th>
2017-09-18 18:11:19 +02:00
</tr>';
foreach ($articoli as $articolo) {
echo '
2017-09-18 18:11:19 +02:00
<tr>
2022-06-30 11:34:26 +02:00
<td><div class="shorten"> '.Modules::link('Articoli', $articolo['id'], $articolo['codice'].' - '.$articolo['descrizione']).'</div></td>
<td class="text-right">'.Translator::numberToLocale($articolo['qta'], 'qta').' '.$articolo['um'].'</td>
2023-12-05 10:39:26 +01:00
<td class="text-right">'.Translator::numberToLocale($articolo['qta'] * 100 / ($totale[0]['totale_qta'] != 0 ? $totale[0]['totale_qta'] : 1), 2).' %</td>
<td class="text-right">'.moneyFormat($articolo['totale'], 2).'</td>
2017-09-18 18:11:19 +02:00
</tr>';
}
echo '
2017-09-18 18:11:19 +02:00
</table>';
2023-08-04 14:54:28 +02:00
2024-06-11 16:58:27 +02:00
echo "<br><p class='float-right' >".Modules::link('Articoli', null, tr('Vedi tutto...'), null, null, false, 'tab_'.Plugin::where('name', 'Statistiche vendita')->first()->id).'</p>';
} else {
echo '
2022-06-30 11:34:26 +02:00
<p>'.tr('Nessun articolo venduto').'...</p>';
}
echo '
</div>
</div>
</div>
</div>';
2022-01-04 22:22:16 +01:00
// Numero interventi per tipologia
$tipi = $dbo->fetchArray('SELECT *, in_tipiintervento.id AS idtipointervento FROM `in_tipiintervento` LEFT JOIN `in_tipiintervento_lang` ON (`in_tipiintervento`.`id` = `in_tipiintervento_lang`.`id_record` AND `in_tipiintervento_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')');
$dataset = '';
2017-12-05 01:19:40 +01:00
foreach ($tipi as $tipo) {
2024-04-22 09:44:07 +02:00
$interventi = $dbo->fetchArray('
SELECT
COUNT(`in_interventi`.`id`) AS result,
YEAR(`sessioni`.`orario_fine`) AS `year`,
MONTH(`sessioni`.`orario_fine`) AS `month`
2022-10-11 10:28:25 +02:00
FROM
`in_interventi`
2024-04-22 09:44:07 +02:00
LEFT JOIN(SELECT `in_interventi_tecnici`.`idintervento`, MAX(`orario_fine`) AS orario_fine FROM `in_interventi_tecnici` GROUP BY `idintervento`) sessioni ON `in_interventi`.`id` = `sessioni`.`idintervento`
WHERE
`in_interventi`.`idtipointervento` = '.prepare($tipo['idtipointervento']).'
2024-04-22 09:44:07 +02:00
AND `sessioni`.`orario_fine` BETWEEN '.prepare($start).' AND '.prepare($end).'
2022-10-11 10:28:25 +02:00
GROUP BY
YEAR(`sessioni`.`orario_fine`),
MONTH(`sessioni`.`orario_fine`)
ORDER BY
YEAR(`sessioni`.`orario_fine`) ASC,
MONTH(`sessioni`.`orario_fine`) ASC');
2019-07-05 17:26:58 +02:00
$interventi = Stats::monthly($interventi, $start, $end);
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
2017-12-05 01:19:40 +01:00
$dataset .= '{
2024-05-16 18:02:50 +02:00
label: "'.$tipo['title'].'",
2017-12-05 01:19:40 +01:00
backgroundColor: "'.$background.'",
data: [
2019-07-11 12:04:22 +02:00
'.implode(',', array_column($interventi, 'result')).'
]
},';
}
echo '
2024-05-17 16:42:59 +02:00
<div class="row">
<div class="col-md-6">
<div class="card card-info">
<div class="card-header">
<h4 class="card-title">'.tr('Numero interventi per tipologia').'</h4>
2024-05-17 16:42:59 +02:00
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<canvas id="interventi_n_tipologia"></canvas>
</div>
</div>
2024-05-17 16:42:59 +02:00
</div>';
2024-05-16 18:02:50 +02:00
// Script for the chart displaying the number of interventions by type
echo '
<script>
$(document).ready(function() {
2024-07-02 12:39:01 +02:00
var translatedMonths = '.json_encode($translated_months).';
2022-01-04 22:22:16 +01:00
new Chart(document.getElementById("interventi_n_tipologia").getContext("2d"), {
type: "bar",
data: {
labels: translatedMonths,
2022-01-04 22:22:16 +01:00
datasets: [
'.$dataset.'
]
},
});
});
</script>';
// Ore interventi per tipologia
$dataset = '';
foreach ($tipi as $tipo) {
$interventi = $dbo->fetchArray('SELECT ROUND(SUM(in_interventi_tecnici.ore), 2) AS result, YEAR(in_interventi_tecnici.orario_fine) AS year, MONTH(in_interventi_tecnici.orario_fine) AS month FROM in_interventi INNER JOIN in_interventi_tecnici ON in_interventi.id=in_interventi_tecnici.idintervento WHERE in_interventi.idtipointervento = '.prepare($tipo['idtipointervento']).' AND in_interventi.data_richiesta BETWEEN '.prepare($start).' AND '.prepare($end).' AND in_interventi_tecnici.orario_fine BETWEEN '.prepare($start).' AND '.prepare($end).' GROUP BY YEAR(in_interventi_tecnici.orario_fine), MONTH(in_interventi_tecnici.orario_fine) ORDER BY YEAR(in_interventi_tecnici.orario_fine) ASC, MONTH(in_interventi_tecnici.orario_fine) ASC');
2022-01-04 22:22:16 +01:00
$interventi = Stats::monthly($interventi, $start, $end);
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
2022-01-04 22:22:16 +01:00
$dataset .= '{
2024-05-16 18:02:50 +02:00
label: "'.$tipo['title'].'",
2022-01-04 22:22:16 +01:00
backgroundColor: "'.$background.'",
data: [
'.implode(',', array_column($interventi, 'result')).'
]
},';
}
echo '
2024-05-17 16:42:59 +02:00
<div class="col-md-6">
<div class="card card-info">
<div class="card-header">
<h4 class="card-title">'.tr('Ore interventi per tipologia').'</h4>
2022-01-04 22:22:16 +01:00
2024-05-17 16:42:59 +02:00
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<canvas id="interventi_ore_tipologia"></canvas>
</div>
2022-01-04 22:22:16 +01:00
</div>
</div>
</div>';
// Script per il grafico delle ore interventi per tipologia
echo '
<script>
$(document).ready(function() {
2024-07-02 12:39:01 +02:00
var translatedMonths = '.json_encode($translated_months).';
2022-01-04 22:22:16 +01:00
new Chart(document.getElementById("interventi_ore_tipologia").getContext("2d"), {
type: "bar",
data: {
labels: translatedMonths,
datasets: [
'.$dataset.'
]
},
});
});
</script>';
// Interventi per tecnico
2024-02-05 17:07:33 +01:00
$tecnici = $dbo->fetchArray('SELECT `an_anagrafiche`.`idanagrafica` AS id, `ragione_sociale`, `colore`
FROM
`an_anagrafiche`
INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`an_tipianagrafiche_anagrafiche`.`idanagrafica`
INNER JOIN `an_tipianagrafiche` ON `an_tipianagrafiche_anagrafiche`.`idtipoanagrafica`=`an_tipianagrafiche`.`id`
2024-03-22 15:52:24 +01:00
LEFT JOIN `an_tipianagrafiche_lang` ON (`an_tipianagrafiche_lang`.`id_record` = `an_tipianagrafiche`.`id` AND `an_tipianagrafiche_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).")
2024-02-05 17:07:33 +01:00
LEFT JOIN `in_interventi_tecnici` ON `in_interventi_tecnici`.`idtecnico` = `an_anagrafiche`.`idanagrafica`
INNER JOIN `in_interventi` ON `in_interventi_tecnici`.`idintervento`=`in_interventi`.`id`
WHERE
2024-04-18 17:44:05 +02:00
`an_anagrafiche`.`deleted_at` IS NULL AND `an_tipianagrafiche_lang`.`title`='Tecnico'
2024-02-05 17:07:33 +01:00
GROUP BY
`an_anagrafiche`.`idanagrafica`
ORDER BY
`ragione_sociale` ASC");
$dataset = '';
if (is_array($_SESSION['superselect']['idtipiintervento'])) {
2024-05-23 11:36:25 +02:00
$where = '`in_interventi_tecnici`.`id` IN('.implode(',', (array) json_decode((string) $_SESSION['superselect']['idtipiintervento'])).')';
2024-04-10 10:28:30 +02:00
} else {
$where = '1=1';
}
foreach ($tecnici as $tecnico) {
$sessioni = $dbo->fetchArray('SELECT SUM(`in_interventi_tecnici`.`ore`) AS result, CONCAT(CAST(SUM(`in_interventi_tecnici`.`ore`) AS char(20)),\' ore\') AS ore_lavorate, YEAR(`in_interventi_tecnici`.`orario_inizio`) AS year, MONTH(`in_interventi_tecnici`.`orario_inizio`) AS month FROM `in_interventi_tecnici` INNER JOIN `in_interventi` ON `in_interventi_tecnici`.`idintervento` = `in_interventi`.`id` LEFT JOIN `in_statiintervento` ON `in_interventi`.`idstatointervento`=`in_statiintervento`.`id` WHERE `in_interventi_tecnici`.`idtecnico` = '.prepare($tecnico['id']).' AND `in_interventi_tecnici`.`orario_inizio` BETWEEN '.prepare($start).' AND '.prepare($end).' AND `in_statiintervento`.`is_completato` AND '.$where.' GROUP BY YEAR(`in_interventi_tecnici`.`orario_inizio`), MONTH(`in_interventi_tecnici`.`orario_inizio`) ORDER BY YEAR(`in_interventi_tecnici`.`orario_inizio`) ASC, MONTH(`in_interventi_tecnici`.`orario_inizio`) ASC');
2021-02-18 18:48:44 +01:00
$sessioni = Stats::monthly($sessioni, $start, $end);
2021-02-18 18:48:44 +01:00
2024-01-15 15:30:45 +01:00
// Colore tecnico
2024-05-23 11:36:25 +02:00
$background = strtoupper((string) $tecnico['colore']);
2021-02-18 18:48:44 +01:00
if (empty($background) || $background == '#FFFFFF') {
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
}
$dataset .= '{
label: "'.$tecnico['ragione_sociale'].'",
backgroundColor: "'.$background.'",
data: [
'.implode(',', array_column($sessioni, 'result')).'
],
},';
}
echo '
2024-05-17 16:42:59 +02:00
<div class="row">
<div class="col-md-6">
<div class="card card-info">
<div class="card-header">
<h4 class="card-title">'.tr('Ore di lavoro per tecnico').'</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<div class="row">
<div class="col-md-4">
{["type": "select", "multiple": "1", "label": "'.tr('Tipi attività').'", "name": "idtipiintervento[]", "ajax-source": "tipiintervento", "value": "'.implode(',', (array) json_decode((string) $_SESSION['superselect']['idtipiintervento'])).'", "placeholder": "Tutti" ]}
</div>
</div>
2024-05-17 16:42:59 +02:00
<canvas id="sessioni"></canvas>
</div>
</div>
2024-05-17 16:42:59 +02:00
</div>';
// Script per il grafico ore interventi per tecnico
echo '
<script>
$(document).ready(function() {
2024-07-02 12:39:01 +02:00
var translatedMonths = '.json_encode($translated_months).';
new Chart(document.getElementById("sessioni").getContext("2d"), {
type: "bar",
data: {
labels: translatedMonths,
datasets: [
2023-08-04 14:54:28 +02:00
'.($dataset ?: '{ label: "", backgroundColor: "transparent", data: [ 0,0,0,0,0,0,0,0,0,0,0,0 ] }').'
]
},
options: {
responsive: true,
indexAxis: "y",
scales: {
2023-09-06 17:37:44 +02:00
x: {
ticks: {
// Include a dollar sign in the ticks
callback: function(value, index, values) {
var text = "";
if (value<=1 && value!=0){
text = " ora";
}else{
text = " ore";
}
return value + text;
}
}
2023-09-06 17:37:44 +02:00
}
},
tooltips: {
callbacks: {
label: function(tooltipItem, data) {
var dataset = data.datasets[tooltipItem.datasetIndex];
var label = dataset.labels ? dataset.labels[tooltipItem.index] : "";
if (label) {
label += ": ";
}
label += tooltipItem.xLabel;
if (tooltipItem.xLabel<=1) {
label += " ora ";
}else{
label += " ore ";
}
label += "(in attività completate)";
return label;
}
}
},
}
});
});
2021-02-18 18:48:44 +01:00
</script>';
$dataset = '';
2024-02-05 17:07:33 +01:00
$nuovi_clienti = $dbo->fetchArray('SELECT
COUNT(*) AS result,
GROUP_CONCAT(`an_anagrafiche`.`ragione_sociale`, "<br>") AS ragioni_sociali,
YEAR(`an_anagrafiche`.`created_at`) AS year,
MONTH(`an_anagrafiche`.`created_at`) AS month
FROM
`an_anagrafiche`
INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`an_tipianagrafiche_anagrafiche`.`idanagrafica`
INNER JOIN `an_tipianagrafiche` ON `an_tipianagrafiche_anagrafiche`.`idtipoanagrafica`=`an_tipianagrafiche`.`id`
2024-03-22 15:52:24 +01:00
LEFT JOIN `an_tipianagrafiche_lang` ON (`an_tipianagrafiche`.`id` = `an_tipianagrafiche_lang`.`id_record` AND `an_tipianagrafiche_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
2024-02-05 17:07:33 +01:00
WHERE
2024-04-18 17:44:05 +02:00
`an_tipianagrafiche_lang`.`title` = "Cliente" AND `deleted_at` IS NULL AND `an_anagrafiche`.`created_at` BETWEEN '.prepare($start).' AND '.prepare($end).' GROUP BY YEAR(`an_anagrafiche`.`created_at`), MONTH(`an_anagrafiche`.`created_at`) ORDER BY YEAR(`an_anagrafiche`.`created_at`) ASC, MONTH(`an_anagrafiche`.`created_at`) ASC');
2024-02-05 17:07:33 +01:00
$nuovi_fornitori = $dbo->fetchArray('SELECT
COUNT(*) AS result,
GROUP_CONCAT(`an_anagrafiche`.`ragione_sociale`, "<br>") AS ragioni_sociali,
YEAR(`an_anagrafiche`.`created_at`) AS year,
MONTH(`an_anagrafiche`.`created_at`) AS month
FROM
`an_anagrafiche`
INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`an_tipianagrafiche_anagrafiche`.`idanagrafica`
INNER JOIN `an_tipianagrafiche` ON `an_tipianagrafiche_anagrafiche`.`idtipoanagrafica`=`an_tipianagrafiche`.`id`
2024-03-22 15:52:24 +01:00
LEFT JOIN `an_tipianagrafiche_lang` ON (`an_tipianagrafiche`.`id` = `an_tipianagrafiche_lang`.`id_record` AND `an_tipianagrafiche_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
2024-02-05 17:07:33 +01:00
WHERE
2024-04-18 17:44:05 +02:00
`an_tipianagrafiche_lang`.`title` = "Fornitore" AND `deleted_at` IS NULL AND `an_anagrafiche`.`created_at` BETWEEN '.prepare($start).' AND '.prepare($end).'
2024-02-05 17:07:33 +01:00
GROUP BY
YEAR(`an_anagrafiche`.`created_at`), MONTH(`an_anagrafiche`.`created_at`)
ORDER BY
YEAR(`an_anagrafiche`.`created_at`) ASC, MONTH(`an_anagrafiche`.`created_at`) ASC');
2024-01-15 15:30:45 +01:00
// Nuovi clienti per i quali ho emesso almeno una fattura di vendita
2024-02-05 17:07:33 +01:00
$clienti_acquisiti = $dbo->fetchArray('SELECT
COUNT(*) AS result,
GROUP_CONCAT(`an_anagrafiche`.`ragione_sociale`, "<br>") AS ragioni_sociali,
YEAR(`an_anagrafiche`.`created_at`) AS year,
MONTH(`an_anagrafiche`.`created_at`) AS month
FROM
`an_anagrafiche`
INNER JOIN `co_documenti` ON `an_anagrafiche`.`idanagrafica` = `co_documenti`.`idanagrafica`
INNER JOIN `co_tipidocumento` ON `co_documenti`.`idtipodocumento`=`co_tipidocumento`.`id`
INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`an_tipianagrafiche_anagrafiche`.`idanagrafica`
INNER JOIN `an_tipianagrafiche` ON `an_tipianagrafiche_anagrafiche`.`idtipoanagrafica`=`an_tipianagrafiche`.`id`
2024-03-22 15:52:24 +01:00
LEFT JOIN `an_tipianagrafiche_lang` ON (`an_tipianagrafiche`.`id` = `an_tipianagrafiche_lang`.`id_record` AND `an_tipianagrafiche_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).')
2024-02-05 17:07:33 +01:00
WHERE
2024-04-18 17:44:05 +02:00
`an_tipianagrafiche_lang`.`title` = "Cliente" AND
2024-02-26 15:53:39 +01:00
`co_tipidocumento`.`dir` = "entrata" AND
`an_anagrafiche`.`created_at` BETWEEN '.prepare($start).' AND '.prepare($end).'
2024-02-05 17:07:33 +01:00
GROUP BY
YEAR(`an_anagrafiche`.`created_at`), MONTH(`an_anagrafiche`.`created_at`)
ORDER BY
YEAR(`an_anagrafiche`.`created_at`) ASC, MONTH(`an_anagrafiche`.`created_at`) ASC');
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
$dataset .= '{
2023-08-04 14:54:28 +02:00
label: "'.tr('Nuovi clienti').'",
backgroundColor: "'.$background.'",
data: [
'.implode(',', array_column($nuovi_clienti, 'result')).'
]
},';
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
$dataset .= '{
2023-08-04 14:54:28 +02:00
label: "'.tr('Clienti acquisiti').'",
backgroundColor: "'.$background.'",
data: [
'.implode(',', array_column($clienti_acquisiti, 'result')).'
]
},';
2024-01-15 15:30:45 +01:00
// Random color
2024-04-08 15:44:33 +02:00
$background = '#'.dechex(random_int(256, 16777215));
$dataset .= '{
2023-08-04 14:54:28 +02:00
label: "'.tr('Nuovi fornitori').'",
backgroundColor: "'.$background.'",
data: [
'.implode(',', array_column($nuovi_fornitori, 'result')).'
]
},';
echo '
2024-05-17 16:42:59 +02:00
<div class="col-md-6">
<div class="card card-info">
<div class="card-header">
<h4 class="card-title">'.tr('Nuove anagrafiche').'</h4>
<div class="card-tools">
<button type="button" class="btn btn-tool" data-card-widget="collapse">
<i class="fa fa-minus"></i>
</button>
</div>
</div>
<div class="card-body">
<canvas id="n_anagrafiche"></canvas>
</div>
</div>
</div>
</div>';
// Script per il grafico dei nuovi clienti per mese
echo '
<script>
$(document).ready(function() {
2024-07-02 12:39:01 +02:00
var translatedMonths = '.json_encode($translated_months).';
new Chart(document.getElementById("n_anagrafiche").getContext("2d"), {
type: "line",
data: {
labels: translatedMonths,
datasets: [
'.$dataset.'
]
},
options: {
responsive: true,
elements: {
line: {
tension: 0
}
},
annotation: {
annotations: [{
type: "line",
mode: "horizontal",
scaleID: "y-axis-0",
value: 0,
label: {
enabled: false,
}
}]
},
hover: {
mode: "nearest",
intersect: false
},
scales: {
2023-09-06 17:37:44 +02:00
x: {
display: true,
scaleLabel: {
display: true,
labelString: "'.tr('Periodo').'"
}
2023-09-06 17:37:44 +02:00
},
y: {
display: true,
scaleLabel: {
display: true,
labelString: "'.tr('Numero').'"
},
ticks: {
callback: function(value, index, values) {
return value;
}
}
2023-09-06 17:37:44 +02:00
}
},
}
});
});
2022-06-30 11:34:26 +02:00
</script>
<script type="text/javascript">
$(".shorten").shorten({
moreText: "'.tr('Mostra tutto').'",
lessText: "'.tr('Comprimi').'",
showChars : 70
});
$("#idtipiintervento").change(function(){
let tipi = "";
if( $(this).val() ){
idtipi = JSON.stringify($(this).val());
}
session_set("superselect,idtipiintervento",idtipi,0);
location.reload();
});
2023-08-04 14:54:28 +02:00
</script>';