openstamanager/plugins/statistiche_anagrafiche/info.php

242 lines
13 KiB
PHP
Raw Permalink Normal View History

2019-07-10 17:22:39 +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/>.
*/
2019-07-10 17:22:39 +02:00
include_once __DIR__.'/../../core.php';
2024-03-22 15:52:24 +01:00
use Models\Module;
2019-07-10 17:22:39 +02:00
use Modules\Anagrafiche\Anagrafica;
use Modules\Contratti\Contratto;
use Modules\DDT\DDT;
use Modules\Fatture\Fattura;
use Modules\Interventi\Components\Sessione;
2019-07-22 09:52:27 +02:00
use Modules\Interventi\Intervento;
2019-07-10 17:22:39 +02:00
use Modules\Ordini\Ordine;
use Modules\Preventivi\Preventivo;
$calendar_id = filter('calendar_id');
$start = filter('start');
$end = filter('end');
$anagrafica = Anagrafica::withTrashed()->find($id_record);
if (empty($anagrafica)) {
return;
}
2019-07-10 17:22:39 +02:00
// Preventivi
2019-10-11 12:52:05 +02:00
$preventivi = Preventivo::whereBetween('data_bozza', [$start, $end])
2019-07-10 17:22:39 +02:00
->where('idanagrafica', $id_record)
->where('default_revision', 1)
->get();
2019-07-11 17:44:42 +02:00
$totale_preventivi = $preventivi->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
// Contratti
2019-10-11 12:52:05 +02:00
$contratti = Contratto::whereBetween('data_bozza', [$start, $end])
2019-07-10 17:22:39 +02:00
->where('idanagrafica', $id_record)
->get();
2019-07-11 17:44:42 +02:00
$totale_contratti = $contratti->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
// Ordini cliente
$ordini_cliente = Ordine::whereBetween('data', [$start, $end])
->where('idanagrafica', $id_record)
->get();
2019-07-11 17:44:42 +02:00
$totale_ordini_cliente = $ordini_cliente->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
// Interventi e Ore lavorate
2019-07-19 17:20:35 +02:00
$interventi = [];
2019-07-10 17:22:39 +02:00
// Clienti
if ($anagrafica->isTipo('Cliente')) {
2019-10-11 17:54:11 +02:00
$interventi = $dbo->fetchArray('SELECT in_interventi.id FROM in_interventi WHERE in_interventi.idanagrafica='.prepare($id_record).' AND data_richiesta BETWEEN '.prepare($start).' AND '.prepare($end));
$sessioni = $dbo->fetchArray('SELECT in_interventi_tecnici.id FROM in_interventi_tecnici INNER JOIN in_interventi ON in_interventi.id = in_interventi_tecnici.idintervento WHERE in_interventi.idanagrafica='.prepare($id_record).' AND in_interventi_tecnici.orario_inizio BETWEEN '.prepare($start).' AND '.prepare($end));
2019-07-10 17:22:39 +02:00
}
// Tecnici
elseif ($anagrafica->isTipo('Tecnico')) {
2019-10-11 17:54:11 +02:00
$interventi = $dbo->fetchArray('SELECT in_interventi.id FROM in_interventi INNER JOIN in_interventi_tecnici ON in_interventi.id = in_interventi_tecnici.idintervento WHERE in_interventi_tecnici.idtecnico='.prepare($id_record).' AND data_richiesta BETWEEN '.prepare($start).' AND '.prepare($end));
$sessioni = $dbo->fetchArray('SELECT in_interventi_tecnici.id FROM in_interventi_tecnici WHERE in_interventi_tecnici.idtecnico='.prepare($id_record).' AND in_interventi_tecnici.orario_inizio BETWEEN '.prepare($start).' AND '.prepare($end));
2019-07-10 17:22:39 +02:00
}
2019-07-19 17:20:35 +02:00
2019-07-19 18:07:22 +02:00
$interventi = Intervento::whereIn('id', array_column($interventi, 'id'))->get();
$totale_interventi = $interventi->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
2024-02-06 17:25:50 +01:00
if ($sessioni) {
2024-02-05 17:07:33 +01:00
$sessioni = Sessione::whereIn('id', array_column($sessioni, 'id'))->get();
$totale_ore_lavorate = $sessioni->sum('ore');
}
2019-07-10 17:22:39 +02:00
// Ddt in uscita
$ddt_uscita = DDT::whereBetween('data', [$start, $end])
->where('idanagrafica', $id_record)
->whereHas('tipo', function ($query) {
$query->where('dt_tipiddt.dir', '=', 'entrata');
})
->get();
2019-07-11 17:44:42 +02:00
$totale_ddt_uscita = $ddt_uscita->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
// Fatture di vendita
2023-09-15 18:06:15 +02:00
$segmenti = $dbo->select('zz_segments', 'id', [], ['autofatture' => 0]);
2019-07-10 17:22:39 +02:00
$fatture_vendita = Fattura::whereBetween('data', [$start, $end])
->where('idanagrafica', $id_record)
2024-04-09 12:18:08 +02:00
->whereHas('tipo', fn ($query) => $query->where('co_tipidocumento.dir', '=', 'entrata')
2024-04-08 15:44:33 +02:00
->where('co_tipidocumento.reversed', '=', 0))
->whereIn('id_segment', array_column($segmenti, 'id'))
2019-07-10 17:22:39 +02:00
->get();
$note_credito = Fattura::whereBetween('data', [$start, $end])
->where('idanagrafica', $id_record)
2024-04-09 12:18:08 +02:00
->whereHas('tipo', fn ($query) => $query->where('co_tipidocumento.dir', '=', 'entrata')
2024-04-08 15:44:33 +02:00
->where('co_tipidocumento.reversed', '=', 1))
->get();
$totale_fatture_vendita = $fatture_vendita->sum('totale_imponibile') - $note_credito->sum('totale_imponibile');
2019-07-10 17:22:39 +02:00
echo '
2024-06-13 11:47:37 +02:00
<div class="card card-info" id="row-'.$calendar_id.'">
<div class="card-header">
<h3 class="card-title">'.tr('Dal _START_ al _END_', [
2024-03-22 15:52:24 +01:00
'_START_' => dateFormat($start),
'_END_' => dateFormat($end),
]).' - '.tr('Periodo _NUM_', [
'_NUM_' => $calendar_id,
]).'</h3>
2019-07-10 17:22:39 +02:00
</div>
2024-06-13 11:47:37 +02:00
<div class="card-body">
2019-07-10 17:22:39 +02:00
<div class="row">
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
2024-05-16 18:02:50 +02:00
<span class="info-box-icon bg-'.($preventivi->count() == 0 ? 'gray' : 'info').'"><i class="fa fa-question"></i></span>
2019-07-10 17:22:39 +02:00
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Preventivi').'
'.($preventivi->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Preventivi')->first()->id.'&search_Cliente='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>').'
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2019-07-19 18:07:22 +02:00
<big>'.$preventivi->count().'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_preventivi).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
<span class="info-box-icon bg-'.($contratti->count() == 0 ? 'gray' : 'purple').'"><i class="fa fa-refresh"></i></span>
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Contratti').'
'.($contratti->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Contratti')->first()->id.'&search_Cliente='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>').'
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2019-07-19 18:07:22 +02:00
<big>'.$contratti->count().'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_contratti).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
<span class="info-box-icon bg-'.($ordini_cliente->count() == 0 ? 'gray' : 'blue').'"><i class="fa fa-file-text"></i></span>
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Ordini cliente').'
'.($ordini_cliente->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Ordini cliente')->first()->id.'&search_Ragione-sociale='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>').'
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2019-07-19 18:07:22 +02:00
<big>'.$ordini_cliente->count().'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_ordini_cliente).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
2019-07-19 18:07:22 +02:00
<span class="info-box-icon bg-'.($interventi->count() == 0 ? 'gray' : 'red').'"><i class="fa fa-cog"></i></span>
2019-07-10 17:22:39 +02:00
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Attività');
2024-01-15 15:30:45 +01:00
if ($anagrafica->isTipo('Cliente')) {
echo '
'.($interventi->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Interventi')->first()->id.'&search_Ragione-sociale='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>');
2024-01-15 15:30:45 +01:00
} else {
echo '
'.($interventi->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Interventi')->first()->id.'&search_Tecnici='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>');
2024-01-15 15:30:45 +01:00
}
echo '
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2019-07-19 18:07:22 +02:00
<big>'.$interventi->count().'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_interventi).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
<span class="info-box-icon bg-'.($ddt_uscita->count() == 0 ? 'gray' : 'maroon').'"><i class="fa fa-truck"></i></span>
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Ddt in uscita').'
'.($ddt_uscita->count() > 0 ? '<a class="pull-right"href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Ddt in uscita')->first()->id.'&search_Ragione-sociale='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>').'
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2019-07-19 18:07:22 +02:00
<big>'.$ddt_uscita->count().'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_ddt_uscita).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3">
2019-07-10 17:22:39 +02:00
<div class="info-box">
2021-10-18 10:23:39 +02:00
<span class="info-box-icon bg-'.($fatture_vendita->count() + $note_credito->count() == 0 ? 'gray' : 'green').'"><i class="fa fa-money"></i></span>
2019-07-10 17:22:39 +02:00
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Fatture').'
'.($fatture_vendita->count() + $note_credito->count() > 0 ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Fatture di vendita')->first()->id.'&search_Ragione-sociale='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>').'
2019-07-10 17:22:39 +02:00
<br class="clearfix">
<span class="info-box-number">
2021-10-18 10:23:39 +02:00
<big>'.($fatture_vendita->count() + $note_credito->count()).'</big><br>
2019-07-10 17:22:39 +02:00
<small class="help-block">'.moneyFormat($totale_fatture_vendita).'</small>
</span>
</div>
</div>
</div>
2019-07-23 16:19:16 +02:00
2024-06-13 11:47:37 +02:00
<div class="col-md-3
">
<div class="info-box">
2024-05-16 18:02:50 +02:00
<span class="info-box-icon bg-'.(!empty($sessioni) ? 'warning' : 'gray').'"><i class="fa fa-wrench"></i></span>
<div class="info-box-content">
<span class="info-box-text pull-left">'.tr('Ore lavorate');
2024-01-15 15:30:45 +01:00
if ($anagrafica->isTipo('Cliente')) {
echo '
'.($sessioni ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Interventi')->first()->id.'&search_Ragione-sociale='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>');
2024-01-15 15:30:45 +01:00
} else {
echo '
'.($sessioni ? '<a class="pull-right" href="'.base_path().'/controller.php?id_module='.Module::where('name', 'Interventi')->first()->id.'&search_Tecnici='.rawurlencode((string) $anagrafica['ragione_sociale']).'">'.tr('Visualizza').' <i class="fa fa-chevron-circle-right"></i></a></span>' : '</span>');
2024-01-15 15:30:45 +01:00
}
echo '
<br class="clearfix">
<span class="info-box-number">
<big>'.numberFormat($totale_ore_lavorate, 0).'</big>
<br><br>
</span>
</div>
</div>
</div>
</div>
2019-07-10 17:22:39 +02:00
</div>
</div>';