11<?php
2+ /**
3+ * This file is part of Informes plugin for FacturaScripts
4+ * Copyright (C) 2026 Carlos Garcia Gomez <carlos@facturascripts.com>
5+ *
6+ * This program is free software: you can redistribute it and/or modify
7+ * it under the terms of the GNU Lesser General Public License as
8+ * published by the Free Software Foundation, either version 3 of the
9+ * License, or (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 Lesser General Public License for more details.
15+ *
16+ * You should have received a copy of the GNU Lesser General Public License
17+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
18+ */
219
320namespace FacturaScripts \Plugins \Informes \Controller ;
421
522use FacturaScripts \Core \Base \Controller ;
623use FacturaScripts \Core \Plugins ;
724use FacturaScripts \Core \Tools ;
25+ use FacturaScripts \Dinamic \Model \Pais ;
826use FacturaScripts \Plugins \Informes \Model \Report ;
927use FacturaScripts \Plugins \Informes \Lib \Informes \ContactsReport ;
1028
@@ -15,6 +33,8 @@ class ReportContacts extends Controller
1533 public $ charts = [];
1634 public $ totals = [];
1735 public $ countries = [];
36+ public $ companyCountry = '' ;
37+ public $ companyCountryCode = '' ;
1838
1939 // extra data for view
2040 public $ sources_periods = [];
@@ -45,6 +65,7 @@ public function privateCore(&$response, $user, $permissions)
4565 // charts
4666 $ this ->loadNewContactsByMonth ();
4767 $ this ->loadNewContactsByYear ();
68+ $ this ->loadContactsByProvince ();
4869
4970 if ($ this ->isEnabledCRM ) {
5071 $ this ->loadSourcesChart ();
@@ -61,7 +82,14 @@ public function privateCore(&$response, $user, $permissions)
6182
6283 protected function loadData (): void
6384 {
64- // nothing special for now
85+ $ this ->companyCountryCode = Tools::settings ('default ' , 'codpais ' , 'ESP ' );
86+
87+ $ country = new Pais ();
88+ if ($ country ->load ($ this ->companyCountryCode )) {
89+ $ this ->companyCountry = $ country ->nombre ;
90+ } else {
91+ $ this ->companyCountry = $ this ->companyCountryCode ;
92+ }
6593 }
6694
6795 protected function loadNewContactsByMonth (): void
@@ -94,17 +122,32 @@ protected function loadNewContactsByYear(): void
94122 $ this ->charts ['newByYears ' ] = $ report ;
95123 }
96124
125+ protected function loadContactsByProvince (): void
126+ {
127+ $ report = new Report ();
128+ $ report ->type = Report::TYPE_TREE_MAP ;
129+ $ report ->table = 'contactos c ' ;
130+ $ report ->xcolumn = "COALESCE(NULLIF(c.provincia, ''), ' " . Tools::trans ('no-data ' ) . "') " ;
131+ $ report ->ycolumn = 'c.idcontacto ' ;
132+ $ report ->yoperation = 'COUNT ' ;
133+
134+ Report::activateAdvancedReport (true );
135+ $ report ->addCustomFilter ('c.codpais ' , '= ' , $ this ->companyCountryCode );
136+
137+ $ this ->charts ['contactsByProvince ' ] = $ report ;
138+ }
139+
97140 protected function loadSourcesChart (): void
98141 {
99142 $ report = new Report ();
100143 $ report ->type = Report::TYPE_DOUGHNUT ;
101- $ report ->table = 'crm_fuentes2 f ' ;
102- $ report ->xcolumn = "COALESCE(f.nombre, ' " . Tools::trans ('no-data ' ) . "') " ;
144+ $ report ->table = 'contactos c ' ;
145+ $ report ->xcolumn = "COALESCE(NULLIF( f.nombre, '') , ' " . Tools::trans ('no-data ' ) . "') " ;
103146 $ report ->ycolumn = 'c.idcontacto ' ;
104147 $ report ->yoperation = 'COUNT ' ;
105148
106149 Report::activateAdvancedReport (true );
107- $ report ->addCustomJoin ('LEFT JOIN contactos c ON c.idfuente = f.id ' );
150+ $ report ->addCustomJoin ('LEFT JOIN crm_fuentes2 f ON c.idfuente = f.id ' );
108151
109152 $ this ->charts ['sources ' ] = $ report ;
110153 }
@@ -113,13 +156,14 @@ protected function loadInterestsChart(): void
113156 {
114157 $ report = new Report ();
115158 $ report ->type = Report::TYPE_DOUGHNUT ;
116- $ report ->table = 'crm_intereses i ' ;
117- $ report ->xcolumn = "COALESCE(i.nombre, ' " . Tools::trans ('no-data ' ) . "') " ;
118- $ report ->ycolumn = 'ic.id ' ;
159+ $ report ->table = 'contactos c ' ;
160+ $ report ->xcolumn = "COALESCE(NULLIF( i.nombre, '') , ' " . Tools::trans ('no-data ' ) . "') " ;
161+ $ report ->ycolumn = 'c.idcontacto ' ;
119162 $ report ->yoperation = 'COUNT ' ;
120163
121164 Report::activateAdvancedReport (true );
122- $ report ->addCustomJoin ('LEFT JOIN crm_intereses_contactos ic ON ic.idinteres = i.id ' );
165+ $ report ->addCustomJoin ('LEFT JOIN crm_intereses_contactos ic ON ic.idcontacto = c.idcontacto ' );
166+ $ report ->addCustomJoin ('LEFT JOIN crm_intereses i ON ic.idinteres = i.id ' );
123167
124168 $ this ->charts ['interests ' ] = $ report ;
125169 }
0 commit comments