2018-02-03 15:03:48 +01: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 />.
*/
2018-02-03 15:03:48 +01:00
2022-03-07 15:41:04 +01:00
use Carbon\Carbon ;
2018-02-03 16:29:53 +01:00
include_once __DIR__ . '/../../../core.php' ;
2023-01-18 16:07:48 +01:00
$filter_agente = Auth :: user ()[ 'gruppo' ] == 'Agenti' ;
2018-02-03 15:03:48 +01:00
switch ( $resource ) {
case 'clienti' :
2022-07-07 17:28:18 +02:00
$id_azienda = setting ( 'Azienda predefinita' );
2022-12-31 19:28:46 +01:00
$query = " SELECT
an_anagrafiche . idanagrafica AS id ,
an_anagrafiche . lat ,
an_anagrafiche . lng ,
is_bloccata ,
2023-01-27 15:17:13 +01:00
CONCAT ( ragione_sociale , IF ( citta IS NULL OR citta = '' , '' , CONCAT ( ' (' , citta , ')' )), IF ( an_anagrafiche . deleted_at IS NULL , '' , ' (".tr(' eliminata ').")' ), IF ( is_bloccata = 1 , CONCAT ( ' (' , an_relazioni . descrizione , ')' ), '' ), ' - ' , an_anagrafiche . codice ) AS descrizione ,
2022-12-31 19:28:46 +01:00
idtipointervento_default AS idtipointervento ,
in_tipiintervento . descrizione AS idtipointervento_descrizione ,
an_anagrafiche . idzona ,
contratto . id AS id_contratto ,
contratto . descrizione AS descrizione_contratto ,
co_pagamenti . id AS id_pagamento ,
co_pagamenti . descrizione AS desc_pagamento ,
banca_vendite . id AS id_banca_vendite ,
CONCAT ( banca_vendite . nome , ' - ' , banca_vendite . iban ) AS descrizione_banca_vendite
FROM
an_anagrafiche
INNER JOIN ( an_tipianagrafiche_anagrafiche
INNER JOIN an_tipianagrafiche
ON an_tipianagrafiche_anagrafiche . idtipoanagrafica = an_tipianagrafiche . idtipoanagrafica )
ON an_anagrafiche . idanagrafica = an_tipianagrafiche_anagrafiche . idanagrafica LEFT JOIN in_tipiintervento ON an_anagrafiche . idtipointervento_default = in_tipiintervento . idtipointervento
LEFT JOIN
an_relazioni
ON an_anagrafiche . idrelazione = an_relazioni . id
LEFT JOIN
( SELECT co_contratti . id , idanagrafica , CONCAT ( 'Contratto ' , numero , ' del ' , DATE_FORMAT ( data_bozza , '%d/%m/%Y' ), ' - ' , co_contratti . nome , ' [' , `co_staticontratti` . `descrizione` , ']' ) AS descrizione FROM co_contratti LEFT JOIN co_staticontratti ON co_contratti . idstato = co_staticontratti . id WHERE co_contratti . predefined = 1 AND is_pianificabile = 1 ) AS contratto
ON an_anagrafiche . idanagrafica = contratto . idanagrafica
LEFT JOIN
co_pagamenti
ON an_anagrafiche . idpagamento_vendite = co_pagamenti . id
LEFT JOIN
co_banche banca_vendite
ON co_pagamenti . idconto_vendite = banca_vendite . id_pianodeiconti3 AND banca_vendite . id_anagrafica = " .prepare( $id_azienda ). " AND banca_vendite . deleted_at IS NULL AND banca_vendite . predefined = 1
| where |
ORDER BY
ragione_sociale " ;
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-02-03 15:03:48 +01:00
2020-07-24 08:33:28 +02:00
$where [] = " an_tipianagrafiche.descrizione='Cliente' " ;
2019-08-29 15:09:01 +02:00
if ( empty ( $filter )) {
2022-07-07 17:28:18 +02:00
$where [] = 'an_anagrafiche.deleted_at IS NULL' ;
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
2023-01-18 16:07:48 +01:00
if ( empty ( ! $filter_agente )) {
$where [] = 'idagente = ' . Auth :: user ()[ 'idanagrafica' ];
}
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
2022-02-06 10:06:48 +01:00
$data = AJAX :: selectResults ( $query , $where , $filter , $search_fields , $limit , $custom );
$rs = $data [ 'results' ];
foreach ( $rs as $k => $r ) {
$rs [ $k ] = array_merge ( $r , [
'text' => $r [ 'descrizione' ],
'disabled' => $r [ 'is_bloccata' ],
]);
}
$results = [
'results' => $rs ,
'recordsFiltered' => $data [ 'recordsFiltered' ],
];
2018-02-03 15:03:48 +01:00
break ;
case 'fornitori' :
2022-07-07 17:28:18 +02:00
$id_azienda = setting ( 'Azienda predefinita' );
2023-01-27 15:17:13 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')')), IF(an_anagrafiche.deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, idtipointervento_default AS idtipointervento, co_pagamenti.id AS id_pagamento, co_pagamenti.descrizione AS desc_pagamento, banca_acquisti.id AS id_banca_acquisti, CONCAT(banca_acquisti.nome, ' - ', banca_acquisti.iban) AS descrizione_banca_acquisti FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica LEFT JOIN co_pagamenti ON an_anagrafiche.idpagamento_acquisti=co_pagamenti.id LEFT JOIN co_banche banca_acquisti ON co_pagamenti.idconto_acquisti = banca_acquisti.id_pianodeiconti3 AND banca_acquisti.id_anagrafica = " . prepare ( $id_azienda ) . " AND banca_acquisti.deleted_at IS NULL AND banca_acquisti.predefined = 1 |where| ORDER BY ragione_sociale " ;
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-02-03 15:03:48 +01:00
2022-07-07 17:28:18 +02:00
$where [] = " an_tipianagrafiche.descrizione='Fornitore' " ;
2019-08-29 15:09:01 +02:00
if ( empty ( $filter )) {
2022-07-07 17:28:18 +02:00
$where [] = 'an_anagrafiche.deleted_at IS NULL' ;
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
break ;
2018-11-30 16:10:15 +01:00
case 'vettori' :
2023-01-27 15:17:13 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')')), IF(deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, idtipointervento_default AS idtipointervento FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
2018-11-27 11:43:22 +01:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-11-27 11:43:22 +01:00
2019-08-29 15:09:01 +02:00
$where [] = " descrizione='Vettore' " ;
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2018-11-27 11:43:22 +01:00
break ;
2018-11-30 16:10:15 +01:00
2020-08-14 09:38:17 +02:00
/*
* Opzioni utilizzate :
* - idanagrafica
*/
2018-02-03 15:03:48 +01:00
case 'agenti' :
2023-01-27 15:17:13 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')')), IF(deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
$where [] = " descrizione='Agente' " ;
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
$results = AJAX :: selectResults ( $query , $where , $filter , $search , $limit , $custom );
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
// Evidenzia l'agente di default
if ( $superselect [ 'idanagrafica' ]) {
$rsa = $dbo -> fetchArray ( 'SELECT idagente FROM an_anagrafiche WHERE idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]));
$idagente_default = $rsa [ 0 ][ 'idagente' ];
} else {
$idagente_default = 0 ;
}
2018-02-03 15:03:48 +01:00
2022-05-18 16:18:04 +02:00
$ids = array_column ( $results [ 'results' ], 'id' );
2019-08-29 15:09:01 +02:00
$pos = array_search ( $idagente_default , $ids );
if ( $pos !== false ) {
2022-05-26 15:39:29 +02:00
$results [ 'results' ][ $pos ][ '_bgcolor_' ] = '#ffff00' ;
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
break ;
case 'tecnici' :
2023-01-27 15:17:13 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT(ragione_sociale, IF(citta IS NULL OR citta = '', '', CONCAT(' (', citta, ')')), IF(deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, idtipointervento_default FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY ragione_sociale " ;
2018-06-26 10:25:50 +02:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
$where [] = " descrizione='Tecnico' " ;
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
2018-06-26 10:25:50 +02:00
2019-12-13 10:08:17 +01:00
if ( setting ( 'Permetti inserimento sessioni degli altri tecnici' )) {
} else {
2019-12-09 17:47:07 +01:00
//come tecnico posso aprire attività solo a mio nome
$user = Auth :: user ();
if ( $user [ 'gruppo' ] == 'Tecnici' && ! empty ( $user [ 'idanagrafica' ])) {
$where [] = 'an_anagrafiche.idanagrafica=' . $user [ 'idanagrafica' ];
}
2018-02-03 15:03:48 +01:00
}
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2018-02-03 15:03:48 +01:00
break ;
2018-11-22 15:34:44 +01:00
2019-08-29 15:09:01 +02:00
case 'clienti_fornitori' :
2023-01-27 15:17:13 +01:00
$query = " SELECT `an_anagrafiche`.`idanagrafica` AS id, CONCAT_WS('', ragione_sociale, IF(citta !='' OR provincia != '', CONCAT(' (', citta, IF(provincia!='', CONCAT(' ', provincia), ''), ')'), ''), IF(deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, `an_tipianagrafiche`.`descrizione` AS optgroup, idtipointervento_default, an_tipianagrafiche.idtipoanagrafica FROM `an_tipianagrafiche` INNER JOIN `an_tipianagrafiche_anagrafiche` ON `an_tipianagrafiche`.`idtipoanagrafica`=`an_tipianagrafiche_anagrafiche`.`idtipoanagrafica` INNER JOIN `an_anagrafiche` ON `an_anagrafiche`.`idanagrafica`=`an_tipianagrafiche_anagrafiche`.`idanagrafica` |where| ORDER BY `optgroup` ASC, ragione_sociale ASC " ;
2018-11-20 11:53:58 +01:00
2018-11-22 15:34:44 +01:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-11-22 10:05:45 +01:00
$where = [];
2018-11-22 15:34:44 +01:00
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
2020-04-27 12:26:27 +02:00
$where [] = " an_tipianagrafiche_anagrafiche.idtipoanagrafica IN (SELECT idtipoanagrafica FROM an_tipianagrafiche WHERE descrizione = 'Cliente' OR descrizione = 'Fornitore' OR descrizione = 'Azienda') " ;
2018-11-22 15:34:44 +01:00
}
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2018-11-22 15:34:44 +01:00
}
// Aggiunta filtri di ricerca
2018-11-22 10:05:45 +01:00
if ( ! empty ( $search_fields )) {
$where [] = '(' . implode ( ' OR ' , $search_fields ) . ')' ;
}
2018-11-22 15:34:44 +01:00
2018-11-22 10:05:45 +01:00
if ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
2018-11-22 15:34:44 +01:00
2018-11-22 10:05:45 +01:00
$query = str_replace ( '|where|' , ! empty ( $where ) ? 'WHERE ' . implode ( ' AND ' , $where ) : '' , $query );
2018-11-20 11:53:58 +01:00
$rs = $dbo -> fetchArray ( $query );
foreach ( $rs as $r ) {
if ( $prev != $r [ 'optgroup' ]) {
$results [] = [ 'text' => $r [ 'optgroup' ], 'children' => []];
$prev = $r [ 'optgroup' ];
}
$results [ count ( $results ) - 1 ][ 'children' ][] = [
'id' => $r [ 'id' ],
'text' => $r [ 'descrizione' ],
'descrizione' => $r [ 'descrizione' ],
];
}
2018-02-03 15:03:48 +01:00
2018-11-20 11:53:58 +01:00
break ;
2018-11-22 15:34:44 +01:00
2018-02-03 15:03:48 +01:00
// Nota Bene: nel campo id viene specificato idtipoanagrafica-idanagrafica -> modulo Utenti e permessi, creazione nuovo utente
case 'anagrafiche' :
2023-01-27 15:17:13 +01:00
$query = " SELECT an_anagrafiche.idanagrafica AS id, CONCAT_WS('', ragione_sociale, IF(citta !='' OR provincia != '', CONCAT(' (', citta, IF(provincia!='', CONCAT(' ', provincia), ''), ')'), ''), IF(deleted_at IS NULL, '', ' ( " . tr ( 'eliminata' ) . " )'),' - ', an_anagrafiche.codice) AS descrizione, `an_tipianagrafiche`.`descrizione` AS optgroup FROM an_anagrafiche INNER JOIN (an_tipianagrafiche_anagrafiche INNER JOIN an_tipianagrafiche ON an_tipianagrafiche_anagrafiche.idtipoanagrafica=an_tipianagrafiche.idtipoanagrafica) ON an_anagrafiche.idanagrafica=an_tipianagrafiche_anagrafiche.idanagrafica |where| ORDER BY `optgroup` ASC, ragione_sociale ASC " ;
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
foreach ( $elements as $element ) {
$filter [] = 'an_anagrafiche.idanagrafica=' . prepare ( $element );
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
}
2018-02-03 15:03:48 +01:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'ragione_sociale LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'provincia LIKE ' . prepare ( '%' . $search . '%' );
2023-01-27 15:17:13 +01:00
$search_fields [] = 'an_anagrafiche.codice LIKE ' . prepare ( '%' . $search . '%' );
2019-08-29 15:09:01 +02:00
}
2019-08-27 17:25:52 +02:00
2019-08-29 15:09:01 +02:00
// Aggiunta filtri di ricerca
if ( ! empty ( $search_fields )) {
$where [] = '(' . implode ( ' OR ' , $search_fields ) . ')' ;
}
2019-08-27 17:25:52 +02:00
2019-08-29 15:09:01 +02:00
if ( ! empty ( $filter )) {
$where [] = '(' . implode ( ' OR ' , $filter ) . ')' ;
}
2019-08-27 17:25:52 +02:00
2019-08-29 15:09:01 +02:00
$query = str_replace ( '|where|' , ! empty ( $where ) ? 'WHERE ' . implode ( ' AND ' , $where ) : '' , $query );
2019-08-27 17:25:52 +02:00
2019-08-29 15:09:01 +02:00
$rs = $dbo -> fetchArray ( $query );
foreach ( $rs as $r ) {
if ( $prev != $r [ 'optgroup' ]) {
$results [] = [ 'text' => $r [ 'optgroup' ], 'children' => []];
$prev = $r [ 'optgroup' ];
2019-08-27 17:25:52 +02:00
}
2019-08-29 15:09:01 +02:00
$results [ count ( $results ) - 1 ][ 'children' ][] = [
'id' => $r [ 'id' ],
'text' => $r [ 'descrizione' ],
'descrizione' => $r [ 'descrizione' ],
];
}
2018-02-03 15:03:48 +01:00
break ;
2020-08-14 09:38:17 +02:00
/*
2020-08-17 10:57:51 +02:00
* Opzioni utilizzate :
2020-08-14 09:38:17 +02:00
* - idanagrafica
*/
2018-02-03 15:03:48 +01:00
case 'sedi' :
2018-02-03 16:29:53 +01:00
if ( isset ( $superselect [ 'idanagrafica' ])) {
2022-12-31 19:28:46 +01:00
$query = "
SELECT
*
FROM
( SELECT '0' AS id , ( SELECT lat FROM an_anagrafiche | where | ) AS lat , ( SELECT lng FROM an_anagrafiche | where | ) AS lng , ( SELECT idzona FROM an_anagrafiche | where | ) AS idzona , CONCAT_WS ( ' - ' , \ " " . tr ( 'Sede legale' ) . " \" , (SELECT CONCAT (citta, IF(indirizzo!='',CONCAT(' (', indirizzo, ')'), ''), ' (',ragione_sociale,')') FROM an_anagrafiche |where|)) AS descrizione
UNION
SELECT
id ,
lat ,
lng ,
idzona ,
CONCAT_WS ( ' - ' , nomesede , CONCAT ( citta , IF ( indirizzo != '' , CONCAT ( ' (' , indirizzo , ')' ), '' )) ) FROM an_sedi | where | ) AS tab
HAVING
descrizione LIKE " .prepare('%'. $search .'%').'
ORDER BY
descrizione ' ;
2018-02-03 15:03:48 +01:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
$where [] = 'idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
2020-11-25 17:35:06 +01:00
/*
2019-05-29 19:17:57 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
}
2020-11-25 17:35:06 +01:00
*/
2019-05-29 19:17:57 +02:00
}
break ;
case 'sedi_azienda' :
2020-07-31 14:25:50 +02:00
$user = Auth :: user ();
$id_azienda = setting ( 'Azienda predefinita' );
2019-05-29 19:17:57 +02:00
2021-06-10 17:44:21 +02:00
$query = " SELECT * FROM (SELECT '0' AS id, CONCAT_WS(' - ', \" " . tr ( 'Sede legale' ) . " \" , (SELECT CONCAT (citta, IF(indirizzo!='',CONCAT(' (', indirizzo, ')'), ''),' (', ragione_sociale,')') FROM an_anagrafiche |where|)) AS descrizione UNION SELECT id, CONCAT_WS(' - ', nomesede, CONCAT(citta, IF(indirizzo!='',CONCAT(' (', indirizzo, ')'), '')) ) FROM an_sedi |where|) AS tab |filter| ORDER BY descrizione " ;
2019-06-29 11:01:26 +02:00
2020-07-31 14:25:50 +02:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
2019-05-29 19:17:57 +02:00
2020-07-31 14:25:50 +02:00
$where [] = 'idanagrafica=' . prepare ( $id_azienda );
//admin o utente senza una sede prefissata, avrà accesso a tutte le sedi
if ( ! empty ( $user -> sedi ) and ! $user -> is_admin ) {
$where [] = 'id IN(' . implode ( ',' , $user -> sedi ) . ')' ;
}
2019-05-29 19:17:57 +02:00
2020-07-31 14:25:50 +02:00
if ( ! empty ( $search )) {
$search_fields [] = 'nomesede LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'citta LIKE ' . prepare ( '%' . $search . '%' );
2018-02-03 15:03:48 +01:00
}
2019-06-29 11:01:26 +02:00
2018-02-03 15:03:48 +01:00
break ;
2020-08-14 09:38:17 +02:00
/*
2020-08-17 10:57:51 +02:00
* Opzioni utilizzate :
* - idanagrafica
*/
2018-02-03 15:03:48 +01:00
case 'referenti' :
2018-02-03 16:29:53 +01:00
if ( isset ( $superselect [ 'idanagrafica' ])) {
2021-11-22 11:35:20 +01:00
$query = 'SELECT an_referenti.id, an_referenti.nome AS descrizione, an_mansioni.nome AS optgroup FROM an_referenti LEFT JOIN an_mansioni ON an_referenti.idmansione=an_mansioni.id |where| ORDER BY optgroup, an_referenti.nome' ;
2018-02-03 15:03:48 +01:00
foreach ( $elements as $element ) {
2021-11-22 11:35:20 +01:00
$filter [] = 'an_referenti.id=' . prepare ( $element );
2018-02-03 15:03:48 +01:00
}
2021-07-07 07:57:10 +02:00
if ( isset ( $superselect [ 'idclientefinale' ])) {
2021-06-22 17:31:10 +02:00
$where [] = '(idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]) . ' OR idanagrafica=' . prepare ( $superselect [ 'idclientefinale' ]) . ')' ;
2021-07-07 07:57:10 +02:00
} else {
2021-06-22 17:31:10 +02:00
$where [] = 'idanagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
}
2018-02-03 15:03:48 +01:00
2023-01-13 09:35:27 +01:00
if ( isset ( $superselect [ 'idsede_destinazione' ])) {
$where [] = 'idsede=' . prepare ( $superselect [ 'idsede_destinazione' ]);
}
2018-02-03 15:03:48 +01:00
if ( ! empty ( $search )) {
2021-11-22 11:35:20 +01:00
$search_fields [] = 'an_referenti.nome LIKE ' . prepare ( '%' . $search . '%' );
2018-02-03 15:03:48 +01:00
}
}
break ;
2019-05-04 00:32:28 +02:00
case 'nazioni' :
2020-02-03 15:21:34 +01:00
$query = 'SELECT id AS id, iso2, CONCAT_WS(\' - \', iso2, nome) AS descrizione FROM an_nazioni |where| ORDER BY CASE WHEN iso2=\'IT\' THEN -1 ELSE iso2 END' ;
2019-05-04 00:32:28 +02:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
if ( ! empty ( $search )) {
$search_fields [] = 'nome LIKE ' . prepare ( '%' . $search . '%' );
2020-02-03 15:21:34 +01:00
$search_fields [] = 'iso2 LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'CONCAT_WS(\' - \', iso2, nome) LIKE ' . prepare ( '%' . $search . '%' );
2019-05-04 00:32:28 +02:00
}
2019-09-13 12:27:29 +02:00
break ;
2022-04-22 19:08:53 +02:00
case 'regioni' :
if ( isset ( $superselect [ 'id_nazione' ])) {
$query = 'SELECT an_regioni.id AS id, an_regioni.iso2, CONCAT(CONCAT_WS(\' - \', an_regioni.iso2, an_regioni.nome), \' (\', an_nazioni.iso2, \')\') AS descrizione FROM an_regioni INNER JOIN an_nazioni ON an_regioni.id_nazione = an_nazioni.id |where| ORDER BY an_regioni.nome' ;
foreach ( $elements as $element ) {
$filter [] = 'an_regioni.id=' . prepare ( $element );
}
$where [] = 'an_regioni.id_nazione=' . prepare ( $superselect [ 'id_nazione' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'an_regioni.nome LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'an_regioni.iso2 LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'CONCAT_WS(\' - \', an_regioni.iso2, an_regioni.nome) LIKE ' . prepare ( '%' . $search . '%' );
}
}
break ;
2020-01-22 19:39:56 +01:00
case 'relazioni' :
2020-02-17 14:51:39 +01:00
$query = 'SELECT id, descrizione, colore AS bgcolor FROM an_relazioni |where| ORDER BY descrizione' ;
2020-01-22 19:39:56 +01:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
break ;
2022-06-10 17:33:35 +02:00
case 'provenienze' :
$query = 'SELECT id, descrizione, colore AS bgcolor FROM an_provenienze |where| ORDER BY descrizione' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
break ;
2020-01-22 19:39:56 +01:00
2022-06-10 19:07:12 +02:00
case 'settori' :
$query = 'SELECT id, descrizione FROM an_settori |where| ORDER BY descrizione' ;
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
if ( ! empty ( $search )) {
$search_fields [] = 'descrizione LIKE ' . prepare ( '%' . $search . '%' );
}
break ;
2020-08-14 09:38:17 +02:00
/*
* Opzioni utilizzate :
* - idanagrafica
*/
2022-03-07 12:47:58 +01:00
2020-08-17 10:57:51 +02:00
case 'dichiarazioni_intento' :
2019-09-13 12:27:29 +02:00
2022-03-07 15:41:04 +01:00
if ( isset ( $superselect [ 'idanagrafica' ]) && isset ( $superselect [ 'data' ])) {
//$query = "SELECT id, CONCAT('N. prot. ', numero_protocollo, ' (periodo dal ', DATE_FORMAT(data_inizio, '%d/%m/%Y'), ' al ' ,DATE_FORMAT(data_fine, '%d/%m/%Y'),') (utilizzati ',REPLACE(REPLACE(REPLACE(FORMAT(SUM(totale),2), ',', '#'), '.', ','), '#', '.'), ' su ' , REPLACE(REPLACE(REPLACE(FORMAT(SUM(massimale),2), ',', '#'), '.', ','), '#', '.'), ' €)' ) AS descrizione, data_inizio, data_fine FROM co_dichiarazioni_intento |where| ORDER BY `data`, `id`";
2019-09-13 12:27:29 +02:00
2022-03-07 15:41:04 +01:00
$query = " SELECT id, numero_protocollo, data_inizio, data_fine, massimale, totale FROM co_dichiarazioni_intento |where| ORDER BY data " ;
2021-02-18 18:48:44 +01:00
2019-09-13 12:27:29 +02:00
2022-03-07 15:41:04 +01:00
foreach ( $elements as $element ) {
$filter [] = 'id=' . prepare ( $element );
}
//$where[] = '( '.prepare($superselect['data']).' BETWEEN data_inizio AND data_fine)';
//$where[] = 'data_inizio < NOW()';
//$where[] = 'data_fine > NOW()';
if ( empty ( $filter )) {
$where [] = 'deleted_at IS NULL' ;
}
$where [] = 'id_anagrafica=' . prepare ( $superselect [ 'idanagrafica' ]);
if ( ! empty ( $search )) {
$search_fields [] = 'numero_protocollo LIKE ' . prepare ( '%' . $search . '%' );
$search_fields [] = 'numero_progressivo LIKE ' . prepare ( '%' . $search . '%' );
}
$data = AJAX :: selectResults ( $query , $where , $filter , $search_fields , $limit , $custom );
$rs = $data [ 'results' ];
foreach ( $rs as $k => $r ) {
$currentDate = date ( 'Y-m-d' , strtotime ( $superselect [ 'data' ]));
$startDate = date ( 'Y-m-d' , strtotime ( $r [ 'data_inizio' ]));
$endDate = date ( 'Y-m-d' , strtotime ( $r [ 'data_fine' ]));
$rs [ $k ] = array_merge ( $r , [
'text' => tr ( 'N. prot.' ) . ' ' . $r [ 'numero_protocollo' ] . ' - ' . Translator :: numberToLocale ( $r [ 'totale' ]) . '/' . Translator :: numberToLocale ( $r [ 'massimale' ]) . ' € [' . Translator :: dateToLocale ( $r [ 'data_fine' ]) . ']' ,
'disabled' => (( $currentDate < $startDate ) || ( $currentDate > $endDate )),
]);
}
$results = [
'results' => $rs ,
'recordsFiltered' => $data [ 'recordsFiltered' ],
];
2019-09-13 12:27:29 +02:00
}
2019-04-30 16:16:25 +02:00
break ;
2018-02-03 15:03:48 +01:00
}