|
| 1 | +<?php |
| 2 | +/* |
| 3 | + * OpenSTAManager: il software gestionale open source per l'assistenza tecnica e la fatturazione |
| 4 | + * Copyright (C) DevCode s.r.l. |
| 5 | + * |
| 6 | + * This program is free software: you can redistribute it and/or modify |
| 7 | + * it under the terms of the GNU General Public License as published by |
| 8 | + * the Free Software Foundation, either version 3 of the License, or |
| 9 | + * (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | + * GNU General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU General Public License |
| 17 | + * along with this program. If not, see <https://www.gnu.org/licenses/>. |
| 18 | + */ |
| 19 | + |
| 20 | +include_once __DIR__.'/../../../core.php'; |
| 21 | +use Models\Module; |
| 22 | + |
| 23 | +$link_id = Module::where('name', 'Contratti')->first()->id; |
| 24 | + |
| 25 | +$results = []; |
| 26 | + |
| 27 | +$fields = [ |
| 28 | + 'Numero' => 'co_contratti.numero', |
| 29 | + 'Nome' => 'co_contratti.nome', |
| 30 | + 'Descrizione' => 'co_contratti.descrizione', |
| 31 | + 'Data accettazione' => 'co_contratti.data_accettazione', |
| 32 | + 'Righe' => 'righe.descrizione', |
| 33 | +]; |
| 34 | + |
| 35 | +$query = 'SELECT co_contratti.*'; |
| 36 | + |
| 37 | +foreach ($fields as $name => $value) { |
| 38 | + $query .= ', '.$value." AS '".str_replace("'", "\'", $name)."'"; |
| 39 | +} |
| 40 | + |
| 41 | +$query .= ' FROM co_contratti LEFT JOIN (SELECT GROUP_CONCAT(`descrizione` SEPARATOR " -- ") AS "descrizione", `idcontratto`, SUM(`qta`) AS "totale_quantita", SUM(`subtotale`) AS "totale_vendita" FROM co_righe_contratti GROUP BY `idcontratto`) righe ON `righe`.`idcontratto`=`co_contratti`.`id` WHERE 1=0 '; |
| 42 | + |
| 43 | +foreach ($fields as $name => $value) { |
| 44 | + $query .= ' OR '.$value.' LIKE '.prepare('%'.$term.'%'); |
| 45 | +} |
| 46 | + |
| 47 | +// Ricerca anche per anagrafica se il termine corrisponde a una ragione sociale |
| 48 | +$query .= ' OR co_contratti.idanagrafica IN (SELECT idanagrafica FROM an_anagrafiche WHERE ragione_sociale LIKE '.prepare('%'.$term.'%').')'; |
| 49 | + |
| 50 | +// Ricerca anche negli articoli associati al contratto |
| 51 | +$query .= ' OR co_contratti.id IN ( |
| 52 | + SELECT DISTINCT co_righe_contratti.idcontratto |
| 53 | + FROM co_righe_contratti |
| 54 | + LEFT JOIN mg_articoli ON co_righe_contratti.idarticolo = mg_articoli.id |
| 55 | + LEFT JOIN mg_articoli_lang ON (mg_articoli.id = mg_articoli_lang.id_record AND mg_articoli_lang.id_lang = '.prepare(Models\Locale::getDefault()->id).') |
| 56 | + WHERE mg_articoli.codice LIKE '.prepare('%'.$term.'%').' |
| 57 | + OR mg_articoli_lang.title LIKE '.prepare('%'.$term.'%').' |
| 58 | +)'; |
| 59 | + |
| 60 | +$query .= Modules::getAdditionalsQuery(Module::where('name', 'Contratti')->first()->id); |
| 61 | + |
| 62 | +$rs = $dbo->fetchArray($query); |
| 63 | + |
| 64 | +foreach ($rs as $r) { |
| 65 | + $result = []; |
| 66 | + |
| 67 | + $result['link'] = base_path().'/editor.php?id_module='.$link_id.'&id_record='.$r['id']; |
| 68 | + $result['title'] = 'Contratto '.$r['numero']; |
| 69 | + |
| 70 | + if ($r['data_accettazione'] && $r['data_accettazione'] != '0000-00-00') { |
| 71 | + $result['title'] .= ' del '.Translator::dateToLocale($r['data_accettazione']); |
| 72 | + } |
| 73 | + |
| 74 | + $result['category'] = 'Contratti'; |
| 75 | + |
| 76 | + // Campi da evidenziare |
| 77 | + $result['labels'] = []; |
| 78 | + foreach ($fields as $name => $value) { |
| 79 | + if (string_contains($r[$name], $term)) { |
| 80 | + $text = $r[$name]; |
| 81 | + |
| 82 | + // Formattazione speciale per la data di accettazione |
| 83 | + if ($name == 'Data accettazione' && $text != '0000-00-00' && !empty($text)) { |
| 84 | + $text = Translator::dateToLocale($text); |
| 85 | + } |
| 86 | + |
| 87 | + $text = str_replace($term, "<span class='highlight'>".$term.'</span>', $text); |
| 88 | + $result['labels'][] = $name.': '.$text.'<br/>'; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + // Aggiunta nome anagrafica |
| 93 | + $anagrafica_query = 'SELECT ragione_sociale FROM an_anagrafiche WHERE idanagrafica = '.prepare($r['idanagrafica']); |
| 94 | + $anagrafica_rs = $dbo->fetchOne($anagrafica_query); |
| 95 | + if (!empty($anagrafica_rs['ragione_sociale'])) { |
| 96 | + $result['labels'][] = 'Anagrafica: '.$anagrafica_rs['ragione_sociale'].'<br/>'; |
| 97 | + } |
| 98 | + |
| 99 | + // Recupero solo gli articoli che corrispondono al termine di ricerca con quantità e valori |
| 100 | + $articoli_query = 'SELECT CONCAT(COALESCE(`mg_articoli`.`codice`, ""), IF(`mg_articoli`.`codice` IS NOT NULL AND `mg_articoli_lang`.`title` IS NOT NULL, " - ", ""), COALESCE(`mg_articoli_lang`.`title`, "")) AS articolo, `co_righe_contratti`.`qta`, `co_righe_contratti`.`prezzo_unitario`, `co_righe_contratti`.`sconto`, `co_righe_contratti`.`subtotale` FROM co_righe_contratti LEFT JOIN `mg_articoli` ON `co_righe_contratti`.`idarticolo` = `mg_articoli`.`id` LEFT JOIN `mg_articoli_lang` ON (`mg_articoli`.`id` = `mg_articoli_lang`.`id_record` AND `mg_articoli_lang`.`id_lang` = '.prepare(Models\Locale::getDefault()->id).') WHERE `co_righe_contratti`.`idcontratto` = '.prepare($r['id']).' AND `mg_articoli`.`id` IS NOT NULL AND (CONCAT(COALESCE(`mg_articoli`.`codice`, ""), " - ", COALESCE(`mg_articoli_lang`.`title`, "")) LIKE "%'.$term.'%" OR `mg_articoli`.`codice` LIKE "%'.$term.'%" OR `mg_articoli_lang`.`title` LIKE "%'.$term.'%")'; |
| 101 | + $articoli_rs = $dbo->fetchArray($articoli_query); |
| 102 | + |
| 103 | + $articoli = []; |
| 104 | + $quantita_totale = 0; |
| 105 | + $valore_totale = 0; |
| 106 | + |
| 107 | + foreach ($articoli_rs as $articolo) { |
| 108 | + if (!empty(trim($articolo['articolo']))) { |
| 109 | + $articoli[] = $articolo['articolo']; |
| 110 | + $quantita_totale += $articolo['qta']; |
| 111 | + |
| 112 | + // Calcolo del valore per contratti (sempre vendita) |
| 113 | + $valore_totale += $articolo['prezzo_unitario'] * $articolo['qta'] - $articolo['sconto']; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Aggiunta solo degli articoli che corrispondono alla ricerca |
| 118 | + if (!empty($articoli)) { |
| 119 | + $result['labels'][] = implode(', ', $articoli).'<br/>'; |
| 120 | + |
| 121 | + // Aggiunta quantità dell'articolo cercato |
| 122 | + if ($quantita_totale > 0) { |
| 123 | + $result['labels'][] = 'Quantità: '.numberFormat($quantita_totale, setting('Cifre decimali per quantità')).'<br/>'; |
| 124 | + } |
| 125 | + |
| 126 | + // Aggiunta valore dell'articolo cercato |
| 127 | + if ($valore_totale > 0) { |
| 128 | + $result['labels'][] = 'Valore: '.moneyFormat($valore_totale).'<br/>'; |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + $results[] = $result; |
| 133 | +} |
0 commit comments