2020namespace FacturaScripts \Plugins \Servicios \Controller ;
2121
2222use DateTime ;
23+ use FacturaScripts \Core \DataSrc \Empresas ;
2324use FacturaScripts \Core \Template \Controller ;
25+ use FacturaScripts \Dinamic \Model \Empresa ;
2426
2527/**
2628 * Informe de servicios: abiertos totales, del último mes, del último año y desglose por estado.
@@ -50,6 +52,12 @@ class ReportServicioAT extends Controller
5052 /** @var array */
5153 public $ servicesByYear = [];
5254
55+ /** @var array */
56+ public $ amountsByMonth = [];
57+
58+ /** @var array */
59+ public $ amountsByYear = [];
60+
5361 /** @var int */
5462 public $ openServices ;
5563
@@ -62,6 +70,12 @@ class ReportServicioAT extends Controller
6270 /** @var int */
6371 public $ totalServices ;
6472
73+ /** @var int */
74+ public $ idempresa ;
75+
76+ /** @var Empresa[] */
77+ public $ empresas = [];
78+
6579 public function getPageData (): array
6680 {
6781 $ data = parent ::getPageData ();
@@ -75,6 +89,9 @@ public function run(): void
7589 {
7690 parent ::run ();
7791
92+ $ this ->empresas = Empresas::all ();
93+ $ this ->idempresa = (int )$ this ->request ()->get ('idempresa ' , Empresas::default ()->idempresa );
94+
7895 $ this ->loadTotalServices ();
7996 $ this ->loadOpenServices ();
8097 $ this ->loadOpenServicesLastMonth ();
@@ -92,7 +109,8 @@ public function run(): void
92109
93110 protected function loadTotalServices (): void
94111 {
95- $ sql = 'SELECT COUNT(*) as total FROM serviciosat ' ;
112+ $ sql = 'SELECT COUNT(*) as total FROM serviciosat '
113+ . ' WHERE idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa );
96114 $ result = $ this ->db ()->select ($ sql );
97115 $ this ->totalServices = (int )($ result [0 ]['total ' ] ?? 0 );
98116 }
@@ -101,6 +119,7 @@ protected function loadServicesByNick(): void
101119 {
102120 $ sql = 'SELECT nick, COUNT(*) as total '
103121 . ' FROM serviciosat '
122+ . ' WHERE idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
104123 . ' GROUP BY nick '
105124 . ' ORDER BY total DESC ' ;
106125 $ this ->servicesByNick = $ this ->db ()->select ($ sql );
@@ -111,6 +130,7 @@ protected function loadServicesByAgent(): void
111130 $ sql = 'SELECT s.codagente, a.nombre, COUNT(s.idservicio) as total '
112131 . ' FROM serviciosat s '
113132 . ' LEFT JOIN agentes a ON a.codagente = s.codagente '
133+ . ' WHERE s.idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
114134 . ' GROUP BY s.codagente, a.nombre '
115135 . ' ORDER BY total DESC ' ;
116136 $ this ->servicesByAgent = $ this ->db ()->select ($ sql );
@@ -120,6 +140,7 @@ protected function loadServicesByAssigned(): void
120140 {
121141 $ sql = 'SELECT asignado, COUNT(*) as total '
122142 . ' FROM serviciosat '
143+ . ' WHERE idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
123144 . ' GROUP BY asignado '
124145 . ' ORDER BY total DESC ' ;
125146 $ this ->servicesByAssigned = $ this ->db ()->select ($ sql );
@@ -130,6 +151,7 @@ protected function loadServicesByClient(): void
130151 $ sql = 'SELECT s.codcliente, c.nombre, COUNT(s.idservicio) as total '
131152 . ' FROM serviciosat s '
132153 . ' LEFT JOIN clientes c ON c.codcliente = s.codcliente '
154+ . ' WHERE s.idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
133155 . ' GROUP BY s.codcliente, c.nombre '
134156 . ' ORDER BY total DESC ' ;
135157 $ this ->servicesByClient = $ this ->db ()->select ($ sql );
@@ -138,23 +160,26 @@ protected function loadServicesByClient(): void
138160 protected function loadOpenServices (): void
139161 {
140162 $ sql = 'SELECT COUNT(*) as total FROM serviciosat '
141- . ' WHERE editable = ' . $ this ->db ()->var2str (true );
163+ . ' WHERE editable = ' . $ this ->db ()->var2str (true )
164+ . ' AND idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa );
142165 $ result = $ this ->db ()->select ($ sql );
143166 $ this ->openServices = (int )($ result [0 ]['total ' ] ?? 0 );
144167 }
145168
146169 protected function loadOpenServicesLastMonth (): void
147170 {
148171 $ since = date ('Y-m-d ' , strtotime ('-1 month ' ));
149- $ sql = "SELECT COUNT(*) as total FROM serviciosat WHERE fecha >= ' " . $ since . "' " ;
172+ $ sql = "SELECT COUNT(*) as total FROM serviciosat WHERE fecha >= ' " . $ since . "' "
173+ . ' AND idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa );
150174 $ result = $ this ->db ()->select ($ sql );
151175 $ this ->openServicesLastMonth = (int )($ result [0 ]['total ' ] ?? 0 );
152176 }
153177
154178 protected function loadOpenServicesLastYear (): void
155179 {
156180 $ since = date ('Y-m-d ' , strtotime ('-1 year ' ));
157- $ sql = "SELECT COUNT(*) as total FROM serviciosat WHERE fecha >= ' " . $ since . "' " ;
181+ $ sql = "SELECT COUNT(*) as total FROM serviciosat WHERE fecha >= ' " . $ since . "' "
182+ . ' AND idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa );
158183 $ result = $ this ->db ()->select ($ sql );
159184 $ this ->openServicesLastYear = (int )($ result [0 ]['total ' ] ?? 0 );
160185 }
@@ -167,29 +192,36 @@ protected function loadServicesByMonth(): void
167192 $ date = clone $ now ;
168193 $ date ->modify ("- $ i months " );
169194 $ this ->servicesByMonth [$ date ->format ('Y-m ' )] = 0 ;
195+ $ this ->amountsByMonth [$ date ->format ('Y-m ' )] = 0.0 ;
170196 }
171197
172198 $ since = (clone $ now )->modify ('-11 months ' )->format ('Y-m-01 ' );
173- $ sql = "SELECT DATE_FORMAT(fecha, '%Y-%m') as periodo, COUNT(*) as total "
199+ $ sql = "SELECT DATE_FORMAT(fecha, '%Y-%m') as periodo, COUNT(*) as total, "
200+ . ' COALESCE(SUM(neto), 0) as neto '
174201 . ' FROM serviciosat '
175202 . " WHERE fecha >= ' " . $ since . "' "
203+ . ' AND idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
176204 . " GROUP BY DATE_FORMAT(fecha, '%Y-%m') "
177205 . ' ORDER BY periodo ASC ' ;
178206 foreach ($ this ->db ()->select ($ sql ) as $ row ) {
179207 if (isset ($ this ->servicesByMonth [$ row ['periodo ' ]])) {
180208 $ this ->servicesByMonth [$ row ['periodo ' ]] = (int )$ row ['total ' ];
209+ $ this ->amountsByMonth [$ row ['periodo ' ]] = (float )$ row ['neto ' ];
181210 }
182211 }
183212 }
184213
185214 protected function loadServicesByYear (): void
186215 {
187- $ sql = 'SELECT YEAR(fecha) as periodo, COUNT(*) as total '
216+ $ sql = 'SELECT YEAR(fecha) as periodo, COUNT(*) as total, '
217+ . ' COALESCE(SUM(neto), 0) as neto '
188218 . ' FROM serviciosat '
219+ . ' WHERE idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
189220 . ' GROUP BY YEAR(fecha) '
190221 . ' ORDER BY periodo ASC ' ;
191222 foreach ($ this ->db ()->select ($ sql ) as $ row ) {
192223 $ this ->servicesByYear [(string )$ row ['periodo ' ]] = (int )$ row ['total ' ];
224+ $ this ->amountsByYear [(string )$ row ['periodo ' ]] = (float )$ row ['neto ' ];
193225 }
194226 }
195227
@@ -199,6 +231,7 @@ protected function loadServicesByStatus(): void
199231 . ' COALESCE(SUM(s.neto), 0) as neto '
200232 . ' FROM serviciosat_estados e '
201233 . ' LEFT JOIN serviciosat s ON s.idestado = e.id '
234+ . ' AND s.idempresa = ' . $ this ->db ()->var2str ($ this ->idempresa )
202235 . ' GROUP BY e.id, e.nombre, e.color, e.editable '
203236 . ' ORDER BY total DESC ' ;
204237 $ this ->servicesByStatus = $ this ->db ()->select ($ sql );
0 commit comments