diff --git a/Controller/EditProyecto.php b/Controller/EditProyecto.php
index 8ad0afb..31487b0 100644
--- a/Controller/EditProyecto.php
+++ b/Controller/EditProyecto.php
@@ -644,16 +644,22 @@ protected function execPreviousAction($action)
return $this->unlinkUpAction(end($parts));
case 'add-file':
- return $this->addFileAction();
+ $result = $this->addFileAction();
+ $this->updateProjectNumDocs();
+ return $result;
case 'delete-file':
- return $this->deleteFileAction();
+ $result = $this->deleteFileAction();
+ $this->updateProjectNumDocs();
+ return $result;
case 'edit-file':
return $this->editFileAction();
case 'unlink-file':
- return $this->unlinkFileAction();
+ $result = $this->unlinkFileAction();
+ $this->updateProjectNumDocs();
+ return $result;
case 'sort-files':
return $this->sortFilesAction();
@@ -875,6 +881,21 @@ protected function rebuildStockAction(?int $idproyecto = null): void
Tools::log()->warning('project-stock-rebuild-error');
}
+ /**
+ * Actualiza el número de adjuntos del proyecto al añadir o eliminar
+ */
+ private function updateProjectNumDocs(): void
+ {
+ $project = new Proyecto();
+ if (false === $project->load($this->request->query('code'))) {
+ return;
+ }
+
+ if (false === $project->updateNumDocs()) {
+ Tools::log()->error('record-save-error');
+ }
+ }
+
private function tableColToNumber(string $name): string
{
return Tools::config('db_type') == 'postgresql' ?
diff --git a/Controller/ListProyecto.php b/Controller/ListProyecto.php
index 7333fba..65c732f 100644
--- a/Controller/ListProyecto.php
+++ b/Controller/ListProyecto.php
@@ -78,7 +78,8 @@ protected function createViewsProjects(string $viewName, string $label, string $
->addFilterNumber('totalcompras-gt', 'total-purchases', 'totalcompras', '>=')
->addFilterNumber('totalcompras-lt', 'total-purchases', 'totalcompras', '<=')
->addFilterNumber('totalventas-gt', 'total-sales', 'totalventas', '>=')
- ->addFilterNumber('totalventas-lt', 'total-sales', 'totalventas', '<=');
+ ->addFilterNumber('totalventas-lt', 'total-sales', 'totalventas', '<=')
+ ->addFilterCheckbox('numdocs', 'has-attachments', 'numdocs', '!=', 0);
$this->setProjectColors($viewName);
}
@@ -184,6 +185,7 @@ protected function createViewsProjectsClosed(string $viewName, string $label, st
->addFilterNumber('totalcompras-lt', 'total-purchases', 'totalcompras', '<=')
->addFilterNumber('totalventas-gt', 'total-sales', 'totalventas', '>=')
->addFilterNumber('totalventas-lt', 'total-sales', 'totalventas', '<=')
+ ->addFilterCheckbox('numdocs', 'has-attachments', 'numdocs', '!=', 0)
->setSettings('btnNew', false)
->setSettings('btnDelete', false);
diff --git a/Init.php b/Init.php
index 9c805c7..052adac 100644
--- a/Init.php
+++ b/Init.php
@@ -130,6 +130,9 @@ public function update(): void
new PresupuestoCliente();
new PresupuestoProveedor();
+ // Recalcular numero de archivos adjuntos
+ $this->updateNumDocsProjects();
+
$this->setupSettings();
$this->createRoleForPlugin();
$this->updateEmailNotifications();
@@ -213,6 +216,15 @@ private function setupSettings(): void
Tools::settingsSave();
}
+ private function updateNumDocsProjects(): void
+ {
+ foreach (Proyecto::all([], [], 0, 0) as $project) {
+ if (false === $project->updateNumDocs()) {
+ Tools::log()->error('record-save-error');
+ }
+ }
+ }
+
private function updateEmailNotifications(): void
{
$notificationModel = new EmailNotification();
diff --git a/Model/Proyecto.php b/Model/Proyecto.php
index 7c8df4f..e13d992 100644
--- a/Model/Proyecto.php
+++ b/Model/Proyecto.php
@@ -22,6 +22,7 @@
use FacturaScripts\Core\Where;
use FacturaScripts\Core\Template\ModelClass;
use FacturaScripts\Core\Template\ModelTrait;
+use FacturaScripts\Core\Model\AttachedFileRelation;
use FacturaScripts\Core\Model\User;
use FacturaScripts\Core\Session;
use FacturaScripts\Core\Tools;
@@ -72,6 +73,9 @@ class Proyecto extends ModelClass
/** @var string Nombre del proyecto. */
public $nombre;
+ /** @var int Número de archivos adjuntos al proyecto. */
+ public $numdocs;
+
/** @var bool Indica si el proyecto es privado y solo lo pueden ver los usuarios asignados. */
public $privado;
@@ -90,6 +94,7 @@ public function clear(): void
$this->editable = true;
$this->fecha = Tools::date();
$this->idempresa = Tools::settings('default', 'idempresa', 1);
+ $this->numdocs = 0;
$this->privado = false;
$this->totalcompras = 0.0;
$this->totalpendientefacturar = 0.0;
@@ -146,6 +151,19 @@ public function getTasks(): array
return TareaProyecto::all($where, [], 0, 0);
}
+ /**
+ * Recalcula el número de archivos adjuntos.
+ */
+ public function updateNumDocs(): bool
+ {
+ $this->numdocs = AttachedFileRelation::count([
+ Where::eq('model', 'Proyecto'),
+ Where::eq('modelid|modelcode', $this->idproyecto)
+ ]);
+
+ return $this->save();
+ }
+
public function install(): string
{
// needed dependencies
diff --git a/Table/proyectos.xml b/Table/proyectos.xml
index 244f0b1..2fae427 100644
--- a/Table/proyectos.xml
+++ b/Table/proyectos.xml
@@ -54,6 +54,11 @@
nombre
character varying(100)
+
+ numdocs
+ integer
+ 0
+
privado
boolean
diff --git a/XMLView/ListProyecto.xml b/XMLView/ListProyecto.xml
index ae7e13f..26ca2a4 100644
--- a/XMLView/ListProyecto.xml
+++ b/XMLView/ListProyecto.xml
@@ -43,6 +43,9 @@
+
+
+