|
| 1 | +<?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 | + */ |
| 19 | + |
| 20 | +namespace FacturaScripts\Plugins\Informes\Controller; |
| 21 | + |
| 22 | +use FacturaScripts\Core\Base\Controller; |
| 23 | +use FacturaScripts\Core\Plugins; |
| 24 | +use FacturaScripts\Plugins\Informes\Model\Report; |
| 25 | + |
| 26 | +class ReportAgentes extends Controller |
| 27 | +{ |
| 28 | + /** @var array lista de agentes [codagente => nombre] */ |
| 29 | + public $agents = []; |
| 30 | + |
| 31 | + /** @var Report gráfica de albaranes por agente */ |
| 32 | + public $albaranes; |
| 33 | + |
| 34 | + /** @var Report gráfica de comisiones por mes */ |
| 35 | + public $comisionesByMonth; |
| 36 | + |
| 37 | + /** @var Report gráfica de comisiones por año */ |
| 38 | + public $comisionesByYear; |
| 39 | + |
| 40 | + /** @var bool indica si el plugin Comisiones está activo */ |
| 41 | + public $comisionesEnabled = false; |
| 42 | + |
| 43 | + /** @var Report gráfica de liquidaciones por mes */ |
| 44 | + public $liquidacionesByMonth; |
| 45 | + |
| 46 | + /** @var Report gráfica de liquidaciones por año */ |
| 47 | + public $liquidacionesByYear; |
| 48 | + |
| 49 | + /** @var Report gráfica de albaranes por mes (último año) */ |
| 50 | + public $albaranesByMonth; |
| 51 | + |
| 52 | + /** @var Report gráfica de albaranes por año */ |
| 53 | + public $albaranesByYear; |
| 54 | + |
| 55 | + /** @var Report gráfica de facturas por agente */ |
| 56 | + public $facturas; |
| 57 | + |
| 58 | + /** @var Report gráfica de facturas por mes (último año) */ |
| 59 | + public $facturasByMonth; |
| 60 | + |
| 61 | + /** @var Report gráfica de facturas por año */ |
| 62 | + public $facturasByYear; |
| 63 | + |
| 64 | + /** @var Report gráfica de pedidos por agente */ |
| 65 | + public $pedidos; |
| 66 | + |
| 67 | + /** @var Report gráfica de pedidos por mes (último año) */ |
| 68 | + public $pedidosByMonth; |
| 69 | + |
| 70 | + /** @var Report gráfica de pedidos por año */ |
| 71 | + public $pedidosByYear; |
| 72 | + |
| 73 | + /** @var Report gráfica de presupuestos por agente */ |
| 74 | + public $presupuestos; |
| 75 | + |
| 76 | + /** @var Report gráfica de presupuestos por mes (último año) */ |
| 77 | + public $presupuestosByMonth; |
| 78 | + |
| 79 | + /** @var Report gráfica de presupuestos por año */ |
| 80 | + public $presupuestosByYear; |
| 81 | + |
| 82 | + public function getPageData(): array |
| 83 | + { |
| 84 | + $data = parent::getPageData(); |
| 85 | + $data['menu'] = 'reports'; |
| 86 | + $data['title'] = 'agents-report'; |
| 87 | + $data['icon'] = 'fa-solid fa-user-tie'; |
| 88 | + return $data; |
| 89 | + } |
| 90 | + |
| 91 | + public function privateCore(&$response, $user, $permissions) |
| 92 | + { |
| 93 | + parent::privateCore($response, $user, $permissions); |
| 94 | + |
| 95 | + $this->comisionesEnabled = Plugins::isEnabled('Comisiones'); |
| 96 | + |
| 97 | + $this->loadAgentes(); |
| 98 | + $this->loadAlbaranes(); |
| 99 | + $this->loadAlbaranesByMonth(); |
| 100 | + $this->loadAlbaranesByYear(); |
| 101 | + $this->loadFacturas(); |
| 102 | + $this->loadFacturasByMonth(); |
| 103 | + $this->loadFacturasByYear(); |
| 104 | + $this->loadPedidos(); |
| 105 | + $this->loadPedidosByMonth(); |
| 106 | + $this->loadPedidosByYear(); |
| 107 | + $this->loadPresupuestos(); |
| 108 | + $this->loadPresupuestosByMonth(); |
| 109 | + $this->loadPresupuestosByYear(); |
| 110 | + |
| 111 | + if ($this->comisionesEnabled) { |
| 112 | + $this->loadComisionesByMonth(); |
| 113 | + $this->loadComisionesByYear(); |
| 114 | + $this->loadLiquidacionesByMonth(); |
| 115 | + $this->loadLiquidacionesByYear(); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + protected function loadAgentes(): void |
| 120 | + { |
| 121 | + $rows = $this->dataBase->select("SELECT codagente, nombre FROM agentes WHERE debaja = false ORDER BY nombre ASC"); |
| 122 | + foreach ($rows as $row) { |
| 123 | + $this->agents[$row['codagente']] = $row['nombre']; |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + protected function loadAlbaranes(): void |
| 128 | + { |
| 129 | + $report = new Report(); |
| 130 | + $report->type = Report::TYPE_PIE; |
| 131 | + $report->table = 'albaranescli f'; |
| 132 | + $report->xcolumn = 'COALESCE(a.nombre, f.codagente)'; |
| 133 | + $report->ycolumn = '*'; |
| 134 | + $report->yoperation = 'COUNT'; |
| 135 | + |
| 136 | + Report::activateAdvancedReport(true); |
| 137 | + $report->addCustomJoin('LEFT JOIN agentes a ON f.codagente = a.codagente'); |
| 138 | + $report->addCustomFilter('f.codagente', 'IS NOT NULL', ''); |
| 139 | + |
| 140 | + $this->albaranes = $report; |
| 141 | + } |
| 142 | + |
| 143 | + protected function loadAlbaranesByMonth(): void |
| 144 | + { |
| 145 | + $report = new Report(); |
| 146 | + $report->type = Report::TYPE_BAR; |
| 147 | + $report->table = 'albaranescli'; |
| 148 | + $report->xcolumn = 'fecha'; |
| 149 | + $report->ycolumn = 'idalbaran'; |
| 150 | + $report->xoperation = 'MONTHS'; |
| 151 | + $report->yoperation = 'COUNT'; |
| 152 | + $report->addFieldXName(''); |
| 153 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 154 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 155 | + |
| 156 | + $this->albaranesByMonth = $report; |
| 157 | + } |
| 158 | + |
| 159 | + protected function loadAlbaranesByYear(): void |
| 160 | + { |
| 161 | + $report = new Report(); |
| 162 | + $report->type = Report::TYPE_BAR; |
| 163 | + $report->table = 'albaranescli'; |
| 164 | + $report->xcolumn = 'fecha'; |
| 165 | + $report->ycolumn = 'idalbaran'; |
| 166 | + $report->xoperation = 'YEAR'; |
| 167 | + $report->yoperation = 'COUNT'; |
| 168 | + $report->addFieldXName(''); |
| 169 | + |
| 170 | + $this->albaranesByYear = $report; |
| 171 | + } |
| 172 | + |
| 173 | + protected function loadComisionesByMonth(): void |
| 174 | + { |
| 175 | + $report = new Report(); |
| 176 | + $report->type = Report::TYPE_BAR; |
| 177 | + $report->table = 'facturascli'; |
| 178 | + $report->xcolumn = 'fecha'; |
| 179 | + $report->ycolumn = 'totalcomision'; |
| 180 | + $report->xoperation = 'MONTHS'; |
| 181 | + $report->yoperation = 'SUM'; |
| 182 | + $report->addFieldXName(''); |
| 183 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 184 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 185 | + $report->addCustomFilter('codagente', 'IS NOT NULL', ''); |
| 186 | + |
| 187 | + $this->comisionesByMonth = $report; |
| 188 | + } |
| 189 | + |
| 190 | + protected function loadComisionesByYear(): void |
| 191 | + { |
| 192 | + $report = new Report(); |
| 193 | + $report->type = Report::TYPE_BAR; |
| 194 | + $report->table = 'facturascli'; |
| 195 | + $report->xcolumn = 'fecha'; |
| 196 | + $report->ycolumn = 'totalcomision'; |
| 197 | + $report->xoperation = 'YEAR'; |
| 198 | + $report->yoperation = 'SUM'; |
| 199 | + $report->addFieldXName(''); |
| 200 | + $report->addCustomFilter('codagente', 'IS NOT NULL', ''); |
| 201 | + |
| 202 | + $this->comisionesByYear = $report; |
| 203 | + } |
| 204 | + |
| 205 | + protected function loadLiquidacionesByMonth(): void |
| 206 | + { |
| 207 | + $report = new Report(); |
| 208 | + $report->type = Report::TYPE_BAR; |
| 209 | + $report->table = 'liquidacionescomisiones'; |
| 210 | + $report->xcolumn = 'fecha'; |
| 211 | + $report->ycolumn = 'total'; |
| 212 | + $report->xoperation = 'MONTHS'; |
| 213 | + $report->yoperation = 'SUM'; |
| 214 | + $report->addFieldXName(''); |
| 215 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 216 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 217 | + |
| 218 | + $this->liquidacionesByMonth = $report; |
| 219 | + } |
| 220 | + |
| 221 | + protected function loadLiquidacionesByYear(): void |
| 222 | + { |
| 223 | + $report = new Report(); |
| 224 | + $report->type = Report::TYPE_BAR; |
| 225 | + $report->table = 'liquidacionescomisiones'; |
| 226 | + $report->xcolumn = 'fecha'; |
| 227 | + $report->ycolumn = 'total'; |
| 228 | + $report->xoperation = 'YEAR'; |
| 229 | + $report->yoperation = 'SUM'; |
| 230 | + $report->addFieldXName(''); |
| 231 | + |
| 232 | + $this->liquidacionesByYear = $report; |
| 233 | + } |
| 234 | + |
| 235 | + protected function loadFacturas(): void |
| 236 | + { |
| 237 | + $report = new Report(); |
| 238 | + $report->type = Report::TYPE_PIE; |
| 239 | + $report->table = 'facturascli f'; |
| 240 | + $report->xcolumn = 'COALESCE(a.nombre, f.codagente)'; |
| 241 | + $report->ycolumn = '*'; |
| 242 | + $report->yoperation = 'COUNT'; |
| 243 | + |
| 244 | + Report::activateAdvancedReport(true); |
| 245 | + $report->addCustomJoin('LEFT JOIN agentes a ON f.codagente = a.codagente'); |
| 246 | + $report->addCustomFilter('f.codagente', 'IS NOT NULL', ''); |
| 247 | + |
| 248 | + $this->facturas = $report; |
| 249 | + } |
| 250 | + |
| 251 | + protected function loadFacturasByMonth(): void |
| 252 | + { |
| 253 | + $report = new Report(); |
| 254 | + $report->type = Report::TYPE_BAR; |
| 255 | + $report->table = 'facturascli'; |
| 256 | + $report->xcolumn = 'fecha'; |
| 257 | + $report->ycolumn = 'idfactura'; |
| 258 | + $report->xoperation = 'MONTHS'; |
| 259 | + $report->yoperation = 'COUNT'; |
| 260 | + $report->addFieldXName(''); |
| 261 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 262 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 263 | + |
| 264 | + $this->facturasByMonth = $report; |
| 265 | + } |
| 266 | + |
| 267 | + protected function loadFacturasByYear(): void |
| 268 | + { |
| 269 | + $report = new Report(); |
| 270 | + $report->type = Report::TYPE_BAR; |
| 271 | + $report->table = 'facturascli'; |
| 272 | + $report->xcolumn = 'fecha'; |
| 273 | + $report->ycolumn = 'idfactura'; |
| 274 | + $report->xoperation = 'YEAR'; |
| 275 | + $report->yoperation = 'COUNT'; |
| 276 | + $report->addFieldXName(''); |
| 277 | + |
| 278 | + $this->facturasByYear = $report; |
| 279 | + } |
| 280 | + |
| 281 | + protected function loadPedidos(): void |
| 282 | + { |
| 283 | + $report = new Report(); |
| 284 | + $report->type = Report::TYPE_PIE; |
| 285 | + $report->table = 'pedidoscli f'; |
| 286 | + $report->xcolumn = 'COALESCE(a.nombre, f.codagente)'; |
| 287 | + $report->ycolumn = '*'; |
| 288 | + $report->yoperation = 'COUNT'; |
| 289 | + |
| 290 | + Report::activateAdvancedReport(true); |
| 291 | + $report->addCustomJoin('LEFT JOIN agentes a ON f.codagente = a.codagente'); |
| 292 | + $report->addCustomFilter('f.codagente', 'IS NOT NULL', ''); |
| 293 | + |
| 294 | + $this->pedidos = $report; |
| 295 | + } |
| 296 | + |
| 297 | + protected function loadPedidosByMonth(): void |
| 298 | + { |
| 299 | + $report = new Report(); |
| 300 | + $report->type = Report::TYPE_BAR; |
| 301 | + $report->table = 'pedidoscli'; |
| 302 | + $report->xcolumn = 'fecha'; |
| 303 | + $report->ycolumn = 'idpedido'; |
| 304 | + $report->xoperation = 'MONTHS'; |
| 305 | + $report->yoperation = 'COUNT'; |
| 306 | + $report->addFieldXName(''); |
| 307 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 308 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 309 | + |
| 310 | + $this->pedidosByMonth = $report; |
| 311 | + } |
| 312 | + |
| 313 | + protected function loadPedidosByYear(): void |
| 314 | + { |
| 315 | + $report = new Report(); |
| 316 | + $report->type = Report::TYPE_BAR; |
| 317 | + $report->table = 'pedidoscli'; |
| 318 | + $report->xcolumn = 'fecha'; |
| 319 | + $report->ycolumn = 'idpedido'; |
| 320 | + $report->xoperation = 'YEAR'; |
| 321 | + $report->yoperation = 'COUNT'; |
| 322 | + $report->addFieldXName(''); |
| 323 | + |
| 324 | + $this->pedidosByYear = $report; |
| 325 | + } |
| 326 | + |
| 327 | + protected function loadPresupuestos(): void |
| 328 | + { |
| 329 | + $report = new Report(); |
| 330 | + $report->type = Report::TYPE_PIE; |
| 331 | + $report->table = 'presupuestoscli f'; |
| 332 | + $report->xcolumn = 'COALESCE(a.nombre, f.codagente)'; |
| 333 | + $report->ycolumn = '*'; |
| 334 | + $report->yoperation = 'COUNT'; |
| 335 | + |
| 336 | + Report::activateAdvancedReport(true); |
| 337 | + $report->addCustomJoin('LEFT JOIN agentes a ON f.codagente = a.codagente'); |
| 338 | + $report->addCustomFilter('f.codagente', 'IS NOT NULL', ''); |
| 339 | + |
| 340 | + $this->presupuestos = $report; |
| 341 | + } |
| 342 | + |
| 343 | + protected function loadPresupuestosByMonth(): void |
| 344 | + { |
| 345 | + $report = new Report(); |
| 346 | + $report->type = Report::TYPE_BAR; |
| 347 | + $report->table = 'presupuestoscli'; |
| 348 | + $report->xcolumn = 'fecha'; |
| 349 | + $report->ycolumn = 'idpresupuesto'; |
| 350 | + $report->xoperation = 'MONTHS'; |
| 351 | + $report->yoperation = 'COUNT'; |
| 352 | + $report->addFieldXName(''); |
| 353 | + $report->addCustomFilter('fecha', '>=', '{-1 year}'); |
| 354 | + $report->addCustomFilter('fecha', '<=', '{today}'); |
| 355 | + |
| 356 | + $this->presupuestosByMonth = $report; |
| 357 | + } |
| 358 | + |
| 359 | + protected function loadPresupuestosByYear(): void |
| 360 | + { |
| 361 | + $report = new Report(); |
| 362 | + $report->type = Report::TYPE_BAR; |
| 363 | + $report->table = 'presupuestoscli'; |
| 364 | + $report->xcolumn = 'fecha'; |
| 365 | + $report->ycolumn = 'idpresupuesto'; |
| 366 | + $report->xoperation = 'YEAR'; |
| 367 | + $report->yoperation = 'COUNT'; |
| 368 | + $report->addFieldXName(''); |
| 369 | + |
| 370 | + $this->presupuestosByYear = $report; |
| 371 | + } |
| 372 | +} |
0 commit comments