2017-08-04 16:28:16 +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 />.
*/
2017-08-04 16:28:16 +02:00
2023-11-08 16:51:25 +01:00
use Geocoder\Provider\GoogleMaps ;
2023-11-08 17:09:08 +01:00
use Ivory\HttpAdapter\CurlHttpAdapter ;
2020-09-03 15:00:52 +02:00
use Modules\Anagrafiche\Anagrafica ;
2020-11-27 15:10:27 +01:00
use Modules\Anagrafiche\Export\CSV ;
2024-07-10 16:55:57 +02:00
use Modules\Anagrafiche\Tipo ;
2020-09-03 15:00:52 +02:00
2017-08-04 16:28:16 +02:00
include_once __DIR__ . '/../../core.php' ;
switch ( post ( 'op' )) {
case 'delete-bulk' :
2024-07-10 16:55:57 +02:00
$id_tipo_azienda = Tipo :: where ( 'name' , 'Azienda' ) -> first () -> id ;
2018-06-26 14:30:26 +02:00
2019-05-16 04:52:16 +02:00
foreach ( $id_records as $id ) {
2024-07-10 16:55:57 +02:00
$anagrafica = $dbo -> fetchArray ( 'SELECT `an_tipianagrafiche`.`id` FROM `an_tipianagrafiche` 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 ) . ') INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_tipianagrafiche`.`id`=`an_tipianagrafiche_anagrafiche`.`idtipoanagrafica` WHERE `idanagrafica`=' . prepare ( $id ));
2019-05-16 04:52:16 +02:00
$tipi = array_column ( $anagrafica , 'idtipoanagrafica' );
2018-06-26 14:30:26 +02:00
2019-05-16 04:52:16 +02:00
// Se l'anagrafica non è di tipo Azienda
2020-09-03 15:00:52 +02:00
if ( ! in_array ( $id_tipo_azienda , $tipi )) {
2024-02-05 17:07:33 +01:00
$dbo -> query ( 'UPDATE `an_anagrafiche` SET `deleted_at` = NOW() WHERE `idanagrafica` = ' . prepare ( $id ) . Modules :: getAdditionalsQuery ( $id_module ));
2019-05-16 04:52:16 +02:00
}
}
2018-06-26 14:30:26 +02:00
2019-05-16 04:52:16 +02:00
flash () -> info ( tr ( 'Anagrafiche eliminate!' ));
2017-08-04 16:28:16 +02:00
2020-09-03 15:00:52 +02:00
break ;
case 'ricerca-coordinate' :
foreach ( $id_records as $id ) {
$anagrafica = Anagrafica :: find ( $id );
2024-07-10 17:26:44 +02:00
if ( ! empty ( $anagrafica -> sedeLegale -> indirizzo ) && ! empty ( $anagrafica -> sedeLegale -> citta ) && ! empty ( $anagrafica -> sedeLegale -> provincia )) {
$indirizzo = urlencode ( $anagrafica -> sedeLegale -> indirizzo . ', ' . $anagrafica -> sedeLegale -> citta . ', ' . $anagrafica -> sedeLegale -> provincia );
2023-09-25 16:54:00 +02:00
// TODO: da riscrivere con Guzzle e spostare su hook
$ch = curl_init ();
2023-12-18 15:01:50 +01:00
$url = 'https://nominatim.openstreetmap.org/search.php?q=' . $indirizzo . '&format=jsonv2&accept-language=' . $lang ;
2023-09-25 16:54:00 +02:00
$user_agent = 'traccar' ;
curl_setopt ( $ch , CURLOPT_USERAGENT , $user_agent );
curl_setopt ( $ch , CURLOPT_URL , $url );
curl_setopt ( $ch , CURLOPT_HEADER , 0 );
curl_setopt ( $ch , CURLOPT_RETURNTRANSFER , 1 );
$data = json_decode ( curl_exec ( $ch ));
curl_close ( $ch );
2020-09-08 14:25:38 +02:00
// Salvataggio informazioni
2023-09-25 16:54:00 +02:00
$anagrafica -> gaddress = $data [ 0 ] -> display_name ;
$anagrafica -> lat = $data [ 0 ] -> lat ;
$anagrafica -> lng = $data [ 0 ] -> lon ;
2020-09-08 14:25:38 +02:00
$anagrafica -> save ();
2020-09-03 15:00:52 +02:00
}
}
2017-08-04 16:28:16 +02:00
break ;
2020-11-24 17:25:07 +01:00
2023-11-08 16:51:25 +01:00
case 'ricerca-coordinate-google' :
$curl = new CurlHttpAdapter ();
2023-12-15 12:11:24 +01:00
$geocoder = new GoogleMaps ( $curl , 'IT-it' , null , true , $google );
2023-11-08 16:51:25 +01:00
foreach ( $id_records as $id ) {
$anagrafica = Anagrafica :: find ( $id );
if ( empty ( $anagrafica -> lat ) && empty ( $anagrafica -> lng ) && ! empty ( $anagrafica -> sedeLegale -> indirizzo ) && ! empty ( $anagrafica -> sedeLegale -> citta ) && ! empty ( $anagrafica -> sedeLegale -> cap )) {
$indirizzo = urlencode ( $anagrafica -> sedeLegale -> indirizzo . ' ' . $anagrafica -> sedeLegale -> citta . ' ' . $anagrafica -> sedeLegale -> cap );
2023-11-09 14:56:03 +01:00
try {
2023-11-08 16:51:25 +01:00
// Ricerca indirizzo
$address = $geocoder -> geocode ( $indirizzo ) -> first ();
$coordinates = $address -> getCoordinates ();
2023-11-09 14:56:03 +01:00
2023-11-08 16:51:25 +01:00
// Salvataggio informazioni
$anagrafica -> lat = $coordinates -> getLatitude ();
$anagrafica -> lng = $coordinates -> getLongitude ();
$anagrafica -> save ();
2024-05-23 11:36:25 +02:00
} catch ( Exception ) {
2023-11-08 16:51:25 +01:00
flash () -> error ( " Impossibile recuperare le coordinate dell'anagrafica " . $anagrafica -> ragione_sociale . " per l'indirizzo " . $anagrafica -> sedeLegale -> indirizzo . ' ' . $anagrafica -> sedeLegale -> citta . ' ' . $anagrafica -> sedeLegale -> cap );
}
}
}
break ;
2020-11-27 15:10:27 +01:00
case 'export-csv' :
$file = temp_file ();
$exporter = new CSV ( $file );
2020-11-24 17:25:07 +01:00
2020-11-27 15:10:27 +01:00
// Esportazione dei record selezionati
2020-12-04 16:37:07 +01:00
$anagrafiche = Anagrafica :: whereIn ( 'idanagrafica' , $id_records ) -> get ();
2020-11-27 15:10:27 +01:00
$exporter -> setRecords ( $anagrafiche );
$count = $exporter -> exportRecords ();
download ( $file , 'anagrafiche.csv' );
2020-11-24 17:25:07 +01:00
break ;
2023-01-13 18:57:44 +01:00
case 'cambia-relazione' :
$idrelazione = post ( 'idrelazione' );
foreach ( $id_records as $id ) {
$anagrafica = Anagrafica :: find ( $id );
$anagrafica -> idrelazione = $idrelazione ;
$anagrafica -> save ();
}
break ;
2023-12-28 17:14:26 +01:00
case 'aggiorna-listino' :
$id_listino = post ( 'id_listino' ) ? : 0 ;
foreach ( $id_records as $id ) {
$anagrafica = Anagrafica :: find ( $id );
if ( $anagrafica -> isTipo ( 'Cliente' )) {
$anagrafica -> id_listino = $id_listino ;
$anagrafica -> save ();
}
}
flash () -> info ( tr ( 'Listino aggiornato correttamente!' ));
break ;
2017-08-04 16:28:16 +02:00
}
2020-09-23 12:00:21 +02:00
$operations = [];
2024-07-10 17:26:44 +02:00
$operations [ 'delete-bulk' ] = [
'text' => '<span><i class="fa fa-trash"></i> ' . tr ( 'Elimina selezionati' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi davvero eliminare le anagrafiche selezionate?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-danger' ,
],
];
2021-09-20 13:33:45 +02:00
$operations [ 'export-csv' ] = [
'text' => '<span><i class="fa fa-download"></i> ' . tr ( 'Esporta selezionati' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi esportare un CSV con le anagrafiche selezionate?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-success' ,
'blank' => true ,
],
];
2024-07-10 17:26:44 +02:00
$operations [ 'ricerca-coordinate' ] = [
'text' => '<span><i class="fa fa-map"></i> ' . tr ( 'Ricerca coordinate' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Ricercare le coordinate per le anagrafiche selezionate senza latitudine e longitudine?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
2023-11-08 16:51:25 +01:00
2024-07-10 17:26:44 +02:00
if ( App :: debug ()) {
2023-11-08 16:51:25 +01:00
$operations [ 'ricerca-coordinate-google' ] = [
2024-07-10 17:26:44 +02:00
'text' => '<span><i class="fa fa-map"></i> ' . tr ( 'Ricerca coordinate (Google)' ) . '</span> <span class="badge badge-danger">beta</span>' ,
2020-09-03 15:00:52 +02:00
'data' => [
'msg' => tr ( 'Ricercare le coordinate per le anagrafiche selezionate senza latitudine e longitudine?' ),
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
}
2023-01-13 18:57:44 +01:00
$operations [ 'cambia-relazione' ] = [
'text' => '<span><i class="fa fa-copy"></i> ' . tr ( 'Cambia relazione' ) . '</span>' ,
'data' => [
'msg' => tr ( 'Vuoi davvero cambiare la relazione delle anagrafiche selezionate?' ) . '<br><br>{[ "type": "select", "label": "' . tr ( 'Relazione con il cliente' ) . '", "name": "idrelazione", "required": 1, "ajax-source": "relazioni"]}' ,
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
2023-12-28 17:14:26 +01:00
$operations [ 'aggiorna-listino' ] = [
2024-07-10 17:26:44 +02:00
'text' => '<span><i class="fa fa-refresh"></i> ' . tr ( 'Imposta listino' ) . '</span>' ,
2023-12-28 17:14:26 +01:00
'data' => [
2024-07-10 17:26:44 +02:00
'msg' => tr ( 'Vuoi impostare il listino cliente selezionato a queste anagrafiche?' ) . '<br><br>{[ "type": "select", "label": "' . tr ( 'Listino cliente' ) . '", "name": "id_listino", "required": 0, "ajax-source": "listini", "placeholder": "' . tr ( 'Nessun listino' ) . '" ]}' ,
2023-12-28 17:14:26 +01:00
'button' => tr ( 'Procedi' ),
'class' => 'btn btn-lg btn-warning' ,
],
];
2019-05-13 17:01:43 +02:00
return $operations ;