Skip to content
Merged
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
19 changes: 19 additions & 0 deletions Controller/EditServicioAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
7 changes: 7 additions & 0 deletions Controller/ListServicioAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,20 @@ 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')
->addOrderBy(['nombre'], 'name')
->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);
Expand Down
18 changes: 15 additions & 3 deletions Controller/NewServicioAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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 .= '<tr class="clickableRow" data-idmaquina="' . $machine->idmaquina . '">'
Expand All @@ -270,15 +275,15 @@ protected function renderCustomerMachinesAction(): array
. '</tr>';
}

$whereAnonymous = [Where::isNull('codcliente')];
$whereAnonymous = [Where::isNull('codcliente'), Where::eq('activo', true)];
$anonymousMachines = MaquinaAT::all($whereAnonymous, $orderBy);
if (false === empty($anonymousMachines)) {
$html .= '<tr class="table-info"><td class="text-center" colspan="3">'
. Tools::trans('anonymous-machines')
. '</td></tr>';
}

$whereAnonymous = [Where::isNull('codcliente')];
$whereAnonymous = [Where::isNull('codcliente'), Where::eq('activo', true)];
foreach (MaquinaAT::all($whereAnonymous, $orderBy) as $machine) {
$html .= '<tr class="clickableRow" data-idmaquina="' . $machine->idmaquina . '">'
. '<td>' . $machine->nombre . '</td>'
Expand Down Expand Up @@ -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');
Expand All @@ -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,
Expand Down
4 changes: 4 additions & 0 deletions Model/MaquinaAT.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -64,6 +67,7 @@ class MaquinaAT extends ModelClass
public function clear(): void
{
parent::clear();
$this->activo = true;
$this->fecha = Tools::date();
}

Expand Down
5 changes: 5 additions & 0 deletions Table/serviciosat_maquinas.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@
Estructura de la tabla serviciosat_maquinas.
-->
<table>
<column>
<name>activo</name>
<type>boolean</type>
<default>true</default>
</column>
<column>
<name>codagente</name>
<type>character varying(10)</type>
Expand Down
22 changes: 22 additions & 0 deletions Test/main/MaquinaAtTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<br/>';
Expand Down
1 change: 1 addition & 0 deletions Translation/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
8 changes: 7 additions & 1 deletion View/NewServicioAT.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,10 @@
{{ trans('description') }}
<textarea name="description" class="form-control" maxlength="100"></textarea>
</div>
<div class="form-check mb-3">
<input type="checkbox" name="active" id="newMachineActive" class="form-check-input" checked/>
<label class="form-check-label" for="newMachineActive">{{ trans('active') }}</label>
</div>
{% for item in getIncludeViews('NewServicioAT', 'newMachineModal') %}
{% include item['path'] %}
{% endfor %}
Expand Down Expand Up @@ -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());
});
Expand Down
3 changes: 3 additions & 0 deletions XMLView/EditMaquinaAT.xml
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,9 @@
<values source="agentes" fieldcode="codagente" fieldtitle="nombre" />
</widget>
</column>
<column name="active" display="center" order="190">
<widget type="checkbox" fieldname="activo" />
</column>
</group>
</columns>
</view>
3 changes: 3 additions & 0 deletions XMLView/ListMaquinaAT.xml
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,8 @@
<column name="date" display="right" order="170">
<widget type="date" fieldname="fecha" />
</column>
<column name="active" display="center" order="180">
<widget type="checkbox" fieldname="activo" />
</column>
</columns>
</view>