openstamanager/modules/interventi/bulk.php

278 lines
12 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
* 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/>.
*/
2019-11-08 15:09:05 +01:00
include_once __DIR__.'/../../core.php';
2018-12-29 12:03:22 +01:00
use Modules\Anagrafiche\Anagrafica;
use Modules\Fatture\Fattura;
use Modules\Fatture\Tipo;
use Modules\Interventi\Intervento;
use Modules\Interventi\Stato;
2018-12-29 12:03:22 +01:00
use Util\Zip;
2018-09-19 16:51:37 +02:00
// Segmenti
$id_fatture = Modules::get('Fatture di vendita')['id'];
if (!isset($_SESSION['module_'.$id_fatture]['id_segment'])) {
$segments = Modules::getSegments($id_fatture);
$_SESSION['module_'.$id_fatture]['id_segment'] = isset($segments[0]['id']) ? $segments[0]['id'] : null;
}
$id_segment = $_SESSION['module_'.$id_fatture]['id_segment'];
switch (post('op')) {
case 'export-bulk':
$dir = DOCROOT.'/files/export_interventi/';
directory($dir.'tmp/');
// Rimozione dei contenuti precedenti
$files = glob($dir.'/*.zip');
foreach ($files as $file) {
delete($file);
}
2018-07-18 15:20:10 +02:00
// Selezione degli interventi da stampare
$interventi = $dbo->fetchArray('SELECT in_interventi.id, in_interventi.codice, data_richiesta, ragione_sociale FROM in_interventi INNER JOIN an_anagrafiche ON in_interventi.idanagrafica=an_anagrafiche.idanagrafica WHERE in_interventi.id IN('.implode(',', $id_records).')');
2018-07-18 15:20:10 +02:00
if (!empty($interventi)) {
foreach ($interventi as $r) {
2018-09-26 10:49:38 +02:00
$print = Prints::getModulePredefinedPrint($id_module);
2018-02-18 19:53:23 +01:00
Prints::render($print['id'], $r['id'], $dir.'tmp/');
}
$dir = slashes($dir);
$file = slashes($dir.'interventi_'.time().'.zip');
// Creazione zip
if (extension_loaded('zip')) {
2018-09-19 16:51:37 +02:00
Zip::create($dir.'tmp/', $file);
// Invio al browser dello zip
download($file);
// Rimozione dei contenuti
delete($dir.'tmp/');
}
}
2018-03-22 15:40:20 +01:00
break;
case 'crea_fattura':
$id_documento_cliente = [];
$n_interventi = 0;
2018-03-22 15:40:20 +01:00
2018-03-08 17:29:32 +01:00
$data = date('Y-m-d');
$dir = 'entrata';
$tipo_documento = Tipo::where('descrizione', 'Fattura immediata di vendita')->first();
2018-03-08 17:29:32 +01:00
2018-09-03 16:01:05 +02:00
$id_iva = setting('Iva predefinita');
$id_conto = setting('Conto predefinito fatture di vendita');
2018-03-08 17:29:32 +01:00
$accodare = post('accodare');
$id_segment = post('id_segment');
2018-03-08 17:29:32 +01:00
$interventi = $dbo->fetchArray('SELECT *, IFNULL((SELECT MIN(orario_inizio) FROM in_interventi_tecnici WHERE in_interventi_tecnici.idintervento = in_interventi.id), in_interventi.data_richiesta) AS data, in_statiintervento.descrizione AS stato, in_interventi.codice AS codice_intervento FROM in_interventi INNER JOIN in_statiintervento ON in_interventi.idstatointervento=in_statiintervento.idstatointervento WHERE in_statiintervento.is_completato=1 AND in_interventi.id NOT IN (SELECT idintervento FROM co_righe_documenti WHERE idintervento IS NOT NULL) AND in_interventi.id_preventivo IS NULL AND in_interventi.id NOT IN (SELECT idintervento FROM co_promemoria WHERE idintervento IS NOT NULL) AND in_interventi.id IN ('.implode(',', $id_records).')');
2018-09-24 10:51:48 +02:00
// Lettura righe selezionate
foreach ($interventi as $intervento) {
$id_anagrafica = $intervento['idanagrafica'];
2018-03-22 15:40:20 +01:00
$id_documento = $id_documento_cliente[$id_anagrafica];
2018-03-22 15:40:20 +01:00
// Se non c'è già una fattura appena creata per questo cliente, creo una fattura nuova
if (empty($id_documento)) {
if (!empty($accodare)) {
$documento = $dbo->fetchOne('SELECT co_documenti.id FROM co_documenti INNER JOIN co_statidocumento ON co_documenti.idstatodocumento = co_statidocumento.id WHERE co_statidocumento.descrizione = \'Bozza\' AND idanagrafica = '.prepare($id_anagrafica));
2018-03-22 15:40:20 +01:00
$id_documento = $documento['id'];
$id_documento_cliente[$id_anagrafica] = $id_documento;
2018-03-22 15:40:20 +01:00
}
if (empty($id_documento)) {
$anagrafica = Anagrafica::find($id_anagrafica);
2019-01-02 14:15:16 +01:00
$fattura = Fattura::build($anagrafica, $tipo_documento, $data, $id_segment);
$id_documento = $fattura->id;
$id_documento_cliente[$id_anagrafica] = $id_documento;
2018-03-22 15:40:20 +01:00
}
}
2018-03-22 15:40:20 +01:00
$descrizione = tr('Intervento numero _NUM_ del _DATE_ [_STATE_]', [
2020-01-24 11:52:36 +01:00
'_NUM_' => $intervento['codice_intervento'],
'_DATE_' => Translator::dateToLocale($intervento['data']),
'_STATE_' => $intervento['stato'],
]);
2018-03-22 15:40:20 +01:00
aggiungi_intervento_in_fattura($intervento['id'], $id_documento, $descrizione, $id_iva, $id_conto);
2018-09-03 16:49:43 +02:00
++$n_interventi;
2018-03-22 15:40:20 +01:00
}
if ($n_interventi > 0) {
2018-09-03 16:01:05 +02:00
flash()->info(tr('_NUM_ interventi fatturati.', [
2018-03-22 15:40:20 +01:00
'_NUM_' => $n_interventi,
]));
2018-03-22 15:40:20 +01:00
} else {
2018-07-19 17:29:21 +02:00
flash()->warning(tr('Nessuna attività fatturata!'));
2018-03-08 17:29:32 +01:00
}
2018-03-22 15:40:20 +01:00
break;
case 'cambia_stato':
$id_stato = post('id_stato');
$n_interventi = 0;
$stato = Stato::find($id_stato);
// Lettura righe selezionate
foreach ($id_records as $id) {
$intervento = Intervento::find($id);
if (!$intervento->stato->is_completato) {
$intervento->stato()->associate($stato);
$intervento->save();
++$n_interventi;
}
}
if ($n_interventi > 0) {
flash()->info(tr('Stato cambiato a _NUM_ attività!', [
'_NUM_' => $n_interventi,
]));
} else {
flash()->warning(tr('Nessuna attività modificata!'));
}
2020-06-09 13:08:36 +02:00
break;
2020-06-09 16:59:26 +02:00
2020-08-07 17:37:39 +02:00
case 'copy-bulk':
2020-09-22 13:59:50 +02:00
$id_stato = post('idstatointervento');
2020-06-09 13:08:36 +02:00
$data_richiesta = post('data_richiesta');
$copia_sessioni = post('sessioni');
$copia_righe = post('righe');
foreach ($id_records as $idintervento) {
$intervento = Intervento::find($idintervento);
$new = $intervento->replicate();
2020-09-22 13:59:50 +02:00
$new->idstatointervento = $id_stato;
2020-06-09 13:08:36 +02:00
2020-09-22 13:59:50 +02:00
// Calcolo del nuovo codice sulla base della data di richiesta
2020-06-09 13:08:36 +02:00
$new->codice = Intervento::getNextCodice($data_richiesta);
$new->save();
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$id_record = $new->id;
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
// Copio le righe
if (!empty($copia_righe)) {
$righe = $intervento->getRighe();
foreach ($righe as $riga) {
$new_riga = $riga->replicate();
$new_riga->setParent($new);
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
$new_riga->qta_evasa = 0;
$new_riga->save();
2020-06-09 13:08:36 +02:00
}
}
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
// Copia delle sessioni
$numero_sessione = 0;
if (!empty($copia_sessioni)) {
2020-06-09 13:08:36 +02:00
$sessioni = $intervento->sessioni;
foreach ($sessioni as $sessione) {
2020-09-22 13:59:50 +02:00
// Se è la prima sessione che copio importo la data con quella della richiesta
if ($numero_sessione == 0) {
2020-06-09 16:59:26 +02:00
$orario_inizio = date('Y-m-d', strtotime($data_richiesta)).' '.date('H:i:s', strtotime($sessione->orario_inizio));
} else {
2020-06-09 13:08:36 +02:00
$diff = strtotime($sessione->orario_inizio) - strtotime($inizio_old);
2020-09-22 13:59:50 +02:00
$orario_inizio = date('Y-m-d H:i:s', (strtotime($sessione->orario_inizio) + $diff));
2020-06-09 13:08:36 +02:00
}
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$diff_fine = strtotime($sessione->orario_fine) - strtotime($sessione->orario_inizio);
2020-06-09 16:59:26 +02:00
$orario_fine = date('Y-m-d H:i:s', (strtotime($orario_inizio) + $diff_fine));
2020-06-09 13:08:36 +02:00
$new_sessione = $sessione->replicate();
$new_sessione->idintervento = $new->id;
2020-06-09 16:59:26 +02:00
2020-06-09 13:08:36 +02:00
$new_sessione->orario_inizio = $orario_inizio;
$new_sessione->orario_fine = $orario_fine;
$new_sessione->save();
2020-06-09 16:59:26 +02:00
2020-09-22 13:59:50 +02:00
++$numero_sessione;
2020-06-09 13:08:36 +02:00
$inizio_old = $sessione->orario_inizio;
}
}
}
flash()->info(tr('Attività duplicate correttamente!'));
break;
}
return [
2018-03-22 15:40:20 +01:00
'export-bulk' => [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-file-archive-o"></i> '.tr('Esporta stampe'),
'data' => [
'title' => tr('Vuoi davvero esportare queste stampe in un archivio?'),
2018-07-23 13:47:09 +02:00
'msg' => '',
'button' => tr('Crea archivio'),
'class' => 'btn btn-lg btn-warning',
'blank' => true,
],
],
2018-03-22 15:40:20 +01:00
'crea_fattura' => [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-file-code-o"></i> '.tr('Fattura documenti'),
2018-03-08 17:29:32 +01:00
'data' => [
2018-07-23 13:47:09 +02:00
'title' => tr('Vuoi davvero generare le fatture per questi interventi?'),
'msg' => tr('Verranno fatturati gli interventi completati non inseriti in preventivi e contratti').'.<br>{[ "type": "checkbox", "placeholder": "'.tr('Aggiungere alle fatture esistenti non ancora emesse?').'", "name": "accodare" ]}
<br>{[ "type": "select", "label": "'.tr('Sezionale').'", "name": "id_segment", "required": 1, "values": "query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module=\''.$id_fatture.'\' AND is_fiscale = 1 ORDER BY name", "value": "'.$id_segment.'" ]}',
'button' => tr('Crea fatture'),
2018-03-08 17:29:32 +01:00
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
],
'cambia_stato' => [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-refresh"></i> '.tr('Cambia stato'),
'data' => [
'title' => tr('Vuoi davvero cambinare le stato per questi interventi?'),
'msg' => tr('Seleziona lo stato in cui spostare tutti gli interventi non completati').'.<br>
<br>{[ "type": "select", "label": "'.tr('Stato').'", "name": "id_stato", "required": 1, "values": "query=SELECT idstatointervento AS id, descrizione, colore AS _bgcolor_ FROM in_statiintervento WHERE deleted_at IS NULL" ]}',
'button' => tr('Sposta'),
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
],
2020-06-09 16:59:26 +02:00
2020-08-07 17:37:39 +02:00
'copy-bulk' => [
2020-07-09 18:50:10 +02:00
'text' => '<span><i class="fa fa-clone"></i> '.tr('Duplica attività'),
2020-06-09 13:08:36 +02:00
'data' => [
'title' => tr('Vuoi davvero fare una copia degli interventi selezionati?'),
'msg' => '<br>{[ "type": "timestamp", "label": "'.tr('Data/ora richiesta').'", "name": "data_richiesta", "required": 0, "value": "-now-", "required":1 ]}
<br>{[ "type": "select", "label": "'.tr('Stato').'", "name": "idstatointervento", "required": 1, "values": "query=SELECT idstatointervento AS id, descrizione, colore AS _bgcolor_ FROM in_statiintervento WHERE deleted_at IS NULL", "value": "" ]}
<br>{[ "type":"checkbox", "label":"'.tr('Duplica righe').'", "name":"righe", "value":"" ]}
<br>{[ "type":"checkbox", "label":"'.tr('Duplica sessioni').'", "name":"sessioni", "value":"" ]}',
'button' => tr('Duplica attività'),
'class' => 'btn btn-lg btn-warning',
'blank' => false,
],
],
];