diff --git a/Controller/EditServicioAT.php b/Controller/EditServicioAT.php index 0b395d0..c76e1f3 100644 --- a/Controller/EditServicioAT.php +++ b/Controller/EditServicioAT.php @@ -60,6 +60,25 @@ public function getPageData(): array return $data; } + protected function autocompleteAction(): array + { + $data = $this->requestGet(['source', 'fieldcode', 'fieldtitle', 'term']); + if ($data['source'] !== 'serviciosat_maquinas') { + return parent::autocompleteAction(); + } + + $where = [Where::eq('activo', true)]; + + $result = []; + foreach ($this->codeModel->search($data['source'], $data['fieldcode'], $data['fieldtitle'], $data['term'], $where) as $value) { + $result[] = ['key' => Tools::fixHtml($value->code), 'value' => Tools::fixHtml($value->description)]; + } + + return empty($result) + ? [['key' => null, 'value' => Tools::trans('no-data')]] + : $result; + } + /** * Calculate the number of hours worked. * diff --git a/Controller/ListServicioAT.php b/Controller/ListServicioAT.php index 9823274..5812a18 100644 --- a/Controller/ListServicioAT.php +++ b/Controller/ListServicioAT.php @@ -58,6 +58,12 @@ protected function createViewsMachines(string $viewName = 'ListMaquinaAT'): void $manufacturers = $this->codeModel->all('fabricantes', 'codfabricante', 'nombre'); $agents = $this->codeModel->all('agentes', 'codagente', 'nombre'); + $valuesWhere = [ + ['label' => Tools::trans('all'), 'where' => []], + ['label' => Tools::trans('only-active'), 'where' => [Where::eq('activo', true)]], + ['label' => Tools::trans('inactive'), 'where' => [Where::eq('activo', false)]], + ]; + $this->addView($viewName, 'MaquinaAT', 'machines', 'fa-solid fa-laptop-medical') ->addOrderBy(['idmaquina'], 'code', 2) ->addOrderBy(['fecha'], 'date') @@ -65,6 +71,7 @@ protected function createViewsMachines(string $viewName = 'ListMaquinaAT'): void ->addOrderBy(['referencia'], 'reference') ->addSearchFields(['descripcion', 'idmaquina', 'nombre', 'numserie', 'referencia']) ->addFilterPeriod('fecha', 'date', 'fecha') + ->addFilterSelectWhere('activo', $valuesWhere) ->addFilterSelect('codfabricante', 'manufacturer', 'codfabricante', $manufacturers) ->addFilterAutocomplete('codcliente', 'customer', 'codcliente', 'clientes', 'codcliente', 'nombre') ->addFilterSelect('codagente', 'agent', 'codagente', $agents); diff --git a/Controller/NewServicioAT.php b/Controller/NewServicioAT.php index 6558c95..d18bc18 100644 --- a/Controller/NewServicioAT.php +++ b/Controller/NewServicioAT.php @@ -203,6 +203,11 @@ protected function checkMachine(): bool return false; } + // no permitimos crear servicios sobre máquinas desactivadas + if (false === (bool)$machine->activo) { + return false; + } + // si no hay cliente, usamos el cliente de la máquina if (empty($this->codcliente)) { $this->codcliente = $machine->codcliente; @@ -260,7 +265,7 @@ protected function renderCustomerMachinesAction(): array ]; } - $whereCustomer = [Where::eq('codcliente', $customer->codcliente)]; + $whereCustomer = [Where::eq('codcliente', $customer->codcliente), Where::eq('activo', true)]; $customerMachines = MaquinaAT::all($whereCustomer, $orderBy); foreach ($customerMachines as $machine) { $html .= '' @@ -270,7 +275,7 @@ protected function renderCustomerMachinesAction(): array . ''; } - $whereAnonymous = [Where::isNull('codcliente')]; + $whereAnonymous = [Where::isNull('codcliente'), Where::eq('activo', true)]; $anonymousMachines = MaquinaAT::all($whereAnonymous, $orderBy); if (false === empty($anonymousMachines)) { $html .= '' @@ -278,7 +283,7 @@ protected function renderCustomerMachinesAction(): array . ''; } - $whereAnonymous = [Where::isNull('codcliente')]; + $whereAnonymous = [Where::isNull('codcliente'), Where::eq('activo', true)]; foreach (MaquinaAT::all($whereAnonymous, $orderBy) as $machine) { $html .= '' . '' . $machine->nombre . '' @@ -349,6 +354,7 @@ protected function saveNewMachineAction(): array } $machine = new MaquinaAT(); + $machine->activo = (bool)$this->request->get('active', true); $machine->codcliente = $this->request->get('codcliente'); $machine->nombre = $this->request->get('name'); $machine->numserie = $this->request->get('serial_number'); @@ -369,6 +375,12 @@ protected function saveNewMachineAction(): array return ['saveNewMachine' => false]; } + // una máquina desactivada no puede asignarse al servicio que se está creando + if (false === $machine->activo) { + Tools::log()->warning('inactive-machine-cant-be-selected'); + return ['saveNewMachine' => false]; + } + return [ 'saveNewMachine' => true, 'idmaquina' => $machine->idmaquina, diff --git a/Model/MaquinaAT.php b/Model/MaquinaAT.php index ffb955e..3aed0b8 100644 --- a/Model/MaquinaAT.php +++ b/Model/MaquinaAT.php @@ -34,6 +34,9 @@ class MaquinaAT extends ModelClass { use ModelTrait; + /** @var bool Indica si la máquina está activa y puede seleccionarse en nuevos servicios. */ + public $activo; + /** @var string Código del agente relacionado con la máquina. */ public $codagente; @@ -64,6 +67,7 @@ class MaquinaAT extends ModelClass public function clear(): void { parent::clear(); + $this->activo = true; $this->fecha = Tools::date(); } diff --git a/Table/serviciosat_maquinas.xml b/Table/serviciosat_maquinas.xml index a57f5c9..f5a7983 100644 --- a/Table/serviciosat_maquinas.xml +++ b/Table/serviciosat_maquinas.xml @@ -5,6 +5,11 @@ Estructura de la tabla serviciosat_maquinas. --> + + activo + boolean + true + codagente character varying(10) diff --git a/Test/main/MaquinaAtTest.php b/Test/main/MaquinaAtTest.php index 0478e87..d1c7e74 100644 --- a/Test/main/MaquinaAtTest.php +++ b/Test/main/MaquinaAtTest.php @@ -50,6 +50,28 @@ public function testCreate(): void $this->assertTrue($customer->delete()); } + public function testActive(): void + { + // una máquina nueva está activa por defecto + $machine = new MaquinaAT(); + $this->assertTrue($machine->activo); + + $machine->nombre = 'Test active machine'; + $this->assertTrue($machine->save()); + + // la desactivamos + $machine->activo = false; + $this->assertTrue($machine->save()); + + // recargamos y comprobamos que sigue desactivada + $reloaded = new MaquinaAT(); + $this->assertTrue($reloaded->load($machine->idmaquina)); + $this->assertFalse((bool)$reloaded->activo); + + // eliminamos + $this->assertTrue($machine->delete()); + } + public function testEscapeHtml(): void { $html = '
'; diff --git a/Translation/es_ES.json b/Translation/es_ES.json index 099dd5f..3b4cb59 100644 --- a/Translation/es_ES.json +++ b/Translation/es_ES.json @@ -18,6 +18,7 @@ "customer-machines": "Máquinas del cliente", "deleted-service": "Servicio eliminado", "generated-services": "%quantity% servicios generados.", + "inactive-machine-cant-be-selected": "La máquina se ha guardado como inactiva, por lo que no puede asignarse al servicio.", "make-delivery-note": "Hacer albarán", "make-estimation": "Hacer presupuesto", "material": "Material", diff --git a/View/NewServicioAT.html.twig b/View/NewServicioAT.html.twig index e2d7bd3..ddea456 100644 --- a/View/NewServicioAT.html.twig +++ b/View/NewServicioAT.html.twig @@ -314,6 +314,10 @@ {{ trans('description') }} +
+ + +
{% for item in getIncludeViews('NewServicioAT', 'newMachineModal') %} {% include item['path'] %} {% endfor %} @@ -429,10 +433,12 @@ let formData = new FormData(); formData.append('codcliente', codcliente); - $('#newMachineModal input').each(function () { + $('#newMachineModal input:not([type=checkbox])').each(function () { formData.append($(this).attr('name'), $(this).val()); }); + formData.append('active', $('#newMachineActive').is(':checked') ? '1' : '0'); + $('#newMachineModal select').each(function () { formData.append($(this).attr('name'), $(this).find('option:selected').val()); }); diff --git a/XMLView/EditMaquinaAT.xml b/XMLView/EditMaquinaAT.xml index ef282b8..f46566d 100644 --- a/XMLView/EditMaquinaAT.xml +++ b/XMLView/EditMaquinaAT.xml @@ -54,6 +54,9 @@
+ + + \ No newline at end of file diff --git a/XMLView/ListMaquinaAT.xml b/XMLView/ListMaquinaAT.xml index 1421265..d1db464 100644 --- a/XMLView/ListMaquinaAT.xml +++ b/XMLView/ListMaquinaAT.xml @@ -48,5 +48,8 @@ + + + \ No newline at end of file