1919
2020namespace FacturaScripts \Plugins \Informes \Controller ;
2121
22+ use FacturaScripts \Core \DataSrc \Empresas ;
2223use FacturaScripts \Core \Template \Controller ;
2324use FacturaScripts \Core \Tools ;
2425use FacturaScripts \Dinamic \Model \Empresa ;
@@ -35,7 +36,7 @@ class ReportSuppliers extends Controller
3536 /** @var array todas las empresas a listar en el formulario [idEmpresa => nombre empresa] */
3637 public $ companies = [];
3738
38- /** @var int|string|null el idempresa sugerido por el usuario (puede ser 'all' ) */
39+ /** @var int empresa seleccionada (obligatoria ) */
3940 public $ idempresa ;
4041
4142 /** @var int Total de proveedores */
@@ -115,21 +116,15 @@ public function run(): void
115116
116117 protected function loadCompanies (): void
117118 {
118- $ this ->companies = ['all ' => Tools::trans ('all-companies ' )];
119- foreach (Empresa::all () as $ company ) {
119+ foreach (Empresas::all () as $ company ) {
120120 $ this ->companies [$ company ->idempresa ] = $ company ->nombrecorto ;
121121 }
122- // empresa seleccionada
123- $ this ->idempresa = $ this ->request ()->queryOrInput ('idempresa ' , null );
124- if (null === $ this ->idempresa ) {
125- // seleccionar todas por defecto si no hay nada
126- $ this ->idempresa = 'all ' ;
127- } elseif ($ this ->idempresa === 'all ' ) {
128- // seleccionar todas
129- $ this ->idempresa = 'all ' ;
130- } else {
131- // seleccionar sugerida
132- $ this ->idempresa = (int )$ this ->idempresa ;
122+
123+ $ requested = $ this ->request ()->queryOrInput ('idempresa ' , null );
124+ $ this ->idempresa = $ requested === null ? Empresas::default ()->idempresa : (int )$ requested ;
125+
126+ if (!isset ($ this ->companies [$ this ->idempresa ])) {
127+ $ this ->idempresa = Empresas::default ()->idempresa ;
133128 }
134129 }
135130
@@ -138,11 +133,9 @@ protected function loadData(): void
138133 $ this ->currentYear = date ('Y ' );
139134 $ this ->companyCountryCode = Tools::settings ('default ' , 'codpais ' );
140135
141- if ($ this ->idempresa !== 'all ' ) {
142- $ company = new Empresa ();
143- if ($ company ->load ($ this ->idempresa ) && !empty ($ company ->codpais )) {
144- $ this ->companyCountryCode = $ company ->codpais ;
145- }
136+ $ company = new Empresa ();
137+ if ($ company ->load ($ this ->idempresa ) && !empty ($ company ->codpais )) {
138+ $ this ->companyCountryCode = $ company ->codpais ;
146139 }
147140
148141 $ country = new Pais ();
@@ -152,10 +145,8 @@ protected function loadData(): void
152145 $ this ->companyCountry = $ this ->companyCountryCode ;
153146 }
154147
155- if ($ this ->idempresa !== 'all ' ) {
156- $ this ->whereEmpresaFacturasProv = " AND idempresa = " . $ this ->idempresa ;
157- $ this ->whereEmpresaProveedores = " WHERE codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . $ this ->idempresa . ") " ;
158- }
148+ $ this ->whereEmpresaFacturasProv = " AND idempresa = " . (int )$ this ->idempresa ;
149+ $ this ->whereEmpresaProveedores = " WHERE codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . (int )$ this ->idempresa . ") " ;
159150 }
160151
161152 protected function loadTotalSuppliers (): void
@@ -179,22 +170,15 @@ protected function loadActiveSupplierYear(): void
179170
180171 protected function loadInactiveSupplier (): void
181172 {
182- $ sqlInactive = "SELECT COUNT(*) as total FROM proveedores " ;
183- if ($ this ->idempresa !== 'all ' ) {
184- $ sqlInactive .= " WHERE debaja = true AND codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . $ this ->idempresa . ") " ;
185- } else {
186- $ sqlInactive .= " WHERE debaja = true " ;
187- }
173+ $ sqlInactive = "SELECT COUNT(*) as total FROM proveedores WHERE debaja = true "
174+ . " AND codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . (int )$ this ->idempresa . ") " ;
188175 $ this ->inactiveSuppliers = $ this ->db ()->select ($ sqlInactive )[0 ]['total ' ];
189176 }
190177
191178 protected function loadNewSuppliers30Days (): void
192179 {
193- $ sql = "SELECT COUNT(*) as total FROM proveedores WHERE fechaalta >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) " ;
194-
195- if ($ this ->idempresa !== 'all ' ) {
196- $ sql .= " AND codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . (int )$ this ->idempresa . ") " ;
197- }
180+ $ sql = "SELECT COUNT(*) as total FROM proveedores WHERE fechaalta >= DATE_SUB(CURDATE(), INTERVAL 30 DAY) "
181+ . " AND codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . (int )$ this ->idempresa . ") " ;
198182 $ this ->newSuppliers30Days = (int )$ this ->db ()->select ($ sql )[0 ]['total ' ];
199183 }
200184
@@ -211,15 +195,12 @@ protected function loadNewSuppliersByMonth(): void
211195 $ report ->addCustomFilter ('fechaalta ' , '>= ' , '{-1 year} ' );
212196 $ report ->addCustomFilter ('fechaalta ' , '<= ' , '{today} ' );
213197
214- // aplicamos el filtro de empresa si no se están mostrando todas
215- if ($ this ->idempresa !== 'all ' ) {
216- Report::activateAdvancedReport (true );
217- $ report ->addCustomFilter (
218- 'codproveedor ' ,
219- 'IN ' ,
220- 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
221- );
222- }
198+ Report::activateAdvancedReport (true );
199+ $ report ->addCustomFilter (
200+ 'codproveedor ' ,
201+ 'IN ' ,
202+ 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
203+ );
223204
224205 $ this ->charts ['reportTest ' ] = $ report ;
225206 }
@@ -235,28 +216,24 @@ protected function loadNewSuppliersByYear(): void
235216 $ report ->yoperation = 'COUNT ' ;
236217 $ report ->addFieldXName ('' );
237218
238- if ($ this ->idempresa !== 'all ' ) {
239- Report::activateAdvancedReport (true );
240- $ report ->addCustomFilter (
241- 'codproveedor ' ,
242- 'IN ' ,
243- 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
244- );
245- }
219+ Report::activateAdvancedReport (true );
220+ $ report ->addCustomFilter (
221+ 'codproveedor ' ,
222+ 'IN ' ,
223+ 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
224+ );
246225
247226 $ this ->charts ['newSuppliersByYear ' ] = $ report ;
248227 }
249228
250229 protected function loadSuppliersByCountry (): void
251230 {
252- $ sqlCountries = "SELECT c.codpais, p.codiso, p.nombre, COUNT(*) as total
253- FROM proveedores pv
254- LEFT JOIN contactos c ON pv.idcontacto = c.idcontacto
255- LEFT JOIN paises p ON c.codpais = p.codpais " ;
256- if ($ this ->idempresa !== 'all ' ) {
257- $ sqlCountries .= " WHERE pv.codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . $ this ->idempresa . ") " ;
258- }
259- $ sqlCountries .= " GROUP BY c.codpais, p.codiso, p.nombre ORDER BY total DESC " ;
231+ $ sqlCountries = "SELECT c.codpais, p.codiso, p.nombre, COUNT(*) as total
232+ FROM proveedores pv
233+ LEFT JOIN contactos c ON pv.idcontacto = c.idcontacto
234+ LEFT JOIN paises p ON c.codpais = p.codpais
235+ WHERE pv.codproveedor IN (SELECT codproveedor FROM facturasprov WHERE idempresa = " . (int )$ this ->idempresa . ")
236+ GROUP BY c.codpais, p.codiso, p.nombre ORDER BY total DESC " ;
260237 $ this ->suppliersByCountry = $ this ->db ()->select ($ sqlCountries );
261238 }
262239
@@ -276,14 +253,11 @@ protected function loadSuppliersByProvince(): void
276253 // filtramos por el país de la empresa para mostrar solo provincias del país
277254 $ report ->addCustomFilter ('c.codpais ' , '= ' , $ this ->companyCountryCode );
278255
279- // aplicamos el filtro de empresa si no se están mostrando todas
280- if ($ this ->idempresa !== 'all ' ) {
281- $ report ->addCustomFilter (
282- 'pv.codproveedor ' ,
283- 'IN ' ,
284- 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
285- );
286- }
256+ $ report ->addCustomFilter (
257+ 'pv.codproveedor ' ,
258+ 'IN ' ,
259+ 'SELECT codproveedor FROM facturasprov WHERE idempresa = ' . (int )$ this ->idempresa
260+ );
287261
288262 $ this ->charts ['suppliersByProvince ' ] = $ report ;
289263 }
@@ -302,21 +276,15 @@ protected function loadInvoicesByProvince(): void
302276 $ report ->addCustomJoin ('LEFT JOIN proveedores pv ON f.codproveedor = pv.codproveedor ' );
303277 $ report ->addCustomJoin ('LEFT JOIN contactos c ON pv.idcontacto = c.idcontacto ' );
304278
305- // aplicamos el filtro de empresa si no se están mostrando todas
306- if ($ this ->idempresa !== 'all ' ) {
307- $ report ->addCustomFilter ('f.idempresa ' , '= ' , (int )$ this ->idempresa );
308- }
279+ $ report ->addCustomFilter ('f.idempresa ' , '= ' , (int )$ this ->idempresa );
309280
310281 $ this ->charts ['invoicesByProvince ' ] = $ report ;
311282 }
312283
313284 protected function loadSuppliersWithPayables (): void
314285 {
315- $ sql = "SELECT COUNT(DISTINCT codproveedor) as total FROM facturasprov WHERE pagada = " . $ this ->db ()->var2str (false );
316-
317- if ($ this ->idempresa !== 'all ' ) {
318- $ sql .= " AND idempresa = " . $ this ->db ()->var2str ((int )$ this ->idempresa );
319- }
286+ $ sql = "SELECT COUNT(DISTINCT codproveedor) as total FROM facturasprov WHERE pagada = " . $ this ->db ()->var2str (false )
287+ . " AND idempresa = " . $ this ->db ()->var2str ((int )$ this ->idempresa );
320288
321289 $ this ->suppliersWithPayables = (int )$ this ->db ()->select ($ sql )[0 ]['total ' ];
322290 }
@@ -355,16 +323,13 @@ protected function getCreditorsSql(int $limit = 0): string
355323 {
356324 $ sql = "SELECT "
357325 . "f.codproveedor as codproveedor, "
358- . "COALESCE(NULLIF(f.nombre, ''), f.codproveedor, ' " . Tools::trans ('no-data ' ) . "') as xcol, "
326+ . "COALESCE(NULLIF(MAX( f.nombre) , ''), f.codproveedor, ' " . Tools::trans ('no-data ' ) . "') as xcol, "
359327 . "SUM(f.total) as ycol "
360328 . "FROM facturasprov f "
361- . "WHERE f.pagada = " . $ this ->db ()->var2str (false );
362-
363- if ($ this ->idempresa !== 'all ' ) {
364- $ sql .= " AND f.idempresa = " . $ this ->db ()->var2str ((int )$ this ->idempresa );
365- }
329+ . "WHERE f.pagada = " . $ this ->db ()->var2str (false )
330+ . " AND f.idempresa = " . $ this ->db ()->var2str ((int )$ this ->idempresa );
366331
367- $ sql .= " GROUP BY f.codproveedor, xcol ORDER BY ycol DESC, xcol ASC " ;
332+ $ sql .= " GROUP BY f.codproveedor ORDER BY ycol DESC, xcol ASC " ;
368333 if ($ limit > 0 ) {
369334 $ sql .= " LIMIT " . $ limit ;
370335 }
0 commit comments