Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 24 additions & 3 deletions Controller/EditProyecto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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' ?
Expand Down
4 changes: 3 additions & 1 deletion Controller/ListProyecto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);

Expand Down
12 changes: 12 additions & 0 deletions Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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();
Expand Down
18 changes: 18 additions & 0 deletions Model/Proyecto.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions Table/proyectos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,11 @@
<name>nombre</name>
<type>character varying(100)</type>
</column>
<column>
<name>numdocs</name>
<type>integer</type>
<default>0</default>
</column>
<column>
<name>privado</name>
<type>boolean</type>
Expand Down
3 changes: 3 additions & 0 deletions XMLView/ListProyecto.xml
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@
<column name="total-sales" display="right" order="210">
<widget type="money" fieldname="totalventas"/>
</column>
<column name="number-attachments" display="center" order="215">
<widget type="number" fieldname="numdocs" decimal="0"/>
</column>
<column name="date" display="right" order="220">
<widget type="date" fieldname="fecha"/>
</column>
Expand Down
Loading