Skip to content

Commit

Permalink
Modificada la forma de ordenar los valores de atributos al añadirlos …
Browse files Browse the repository at this point in the history
…a la descripción de un producto.
  • Loading branch information
NeoRazorX committed Feb 19, 2024
1 parent 51f7338 commit 5d3f223
Show file tree
Hide file tree
Showing 6 changed files with 150 additions and 54 deletions.
11 changes: 10 additions & 1 deletion Core/Model/Atributo.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2015-2023 Carlos Garcia Gomez <[email protected]>
* Copyright (C) 2015-2024 Carlos Garcia Gomez <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -49,6 +49,15 @@ public function clear()
$this->num_selector = 0;
}

public function getNewValue(string $value): AtributoValor
{
$attValue = new DinAtributoValor();
$attValue->codatributo = $this->codatributo;
$attValue->valor = $value;

return $attValue;
}

/**
* @return AtributoValor[]
*/
Expand Down
9 changes: 8 additions & 1 deletion Core/Model/AtributoValor.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2015-2023 Carlos Garcia Gomez <[email protected]>
* Copyright (C) 2015-2024 Carlos Garcia Gomez <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -86,6 +86,13 @@ public function codeModelAll(string $fieldCode = ''): array
return $results;
}

public function getAtributo(): Atributo
{
$atributo = new DinAtributo();
$atributo->loadFromCode($this->codatributo);
return $atributo;
}

public function install(): string
{
// needed dependency
Expand Down
24 changes: 20 additions & 4 deletions Core/Model/Variante.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2018-2023 Carlos García Gómez <[email protected]>
* Copyright (C) 2018-2024 Carlos García Gómez <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
Expand Down Expand Up @@ -206,19 +206,35 @@ public function delete(): bool
*/
protected function getAttributeDescription($idAttVal1, $idAttVal2, $idAttVal3, $idAttVal4, $description = '', $separator1 = "\n", $separator2 = ', '): string
{
// obtenemos las descripciones de los atributos
$attributeValue = new DinAtributoValor();
$extra = [];
$attDesc = [];
foreach ([$idAttVal1, $idAttVal2, $idAttVal3, $idAttVal4] as $id) {
if (!empty($id) && $attributeValue->loadFromCode($id)) {
$extra[] = $attributeValue->descripcion;
$attribute = $attributeValue->getAtributo();
$attDesc[] = [
'position' => empty($attribute->num_selector) ? 99 : $attribute->num_selector,
'value' => $attributeValue->descripcion
];
}
}

// compose text
// ordenamos por posición
usort($attDesc, function ($a, $b) {
return $a['position'] <=> $b['position'];
});

$extra = [];
foreach ($attDesc as $item) {
$extra[] = $item['value'];
}

// devolvemos la descripción
if (empty($description)) {
return implode($separator2, $extra);
}

// combinamos la descripción con los atributos
return empty($extra) ? $description : implode($separator1, [$description, implode($separator2, $extra)]);
}

Expand Down
29 changes: 17 additions & 12 deletions Test/Core/Model/AtributoTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2021 Carlos Garcia Gomez <[email protected]>
* Copyright (C) 2017-2024 Carlos Garcia Gomez <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand All @@ -28,7 +28,7 @@ final class AtributoTest extends TestCase
{
use LogErrorsTrait;

public function testCreate()
public function testCreate(): void
{
$attribute = $this->getTestAttribute();
$this->assertTrue($attribute->save(), 'attribute-cant-save');
Expand All @@ -37,34 +37,39 @@ public function testCreate()
$this->assertTrue($attribute->delete(), 'attribute-cant-delete');
}

public function testCreateWithNoCode()
public function testCreateWithNoCode(): void
{
$attribute = new Atributo();
$attribute->nombre = 'Test Attribute with new code';
$this->assertTrue($attribute->save(), 'attribute-cant-save');
$this->assertTrue($attribute->delete(), 'attribute-cant-delete');
}

public function testAttributeValues()
public function testAttributeValues(): void
{
// creamos el atributo
$attribute = $this->getTestAttribute();
$this->assertTrue($attribute->save(), 'attribute-cant-save');

// creamos un valor
$attributeValue = new AtributoValor();
$attributeValue->codatributo = $attribute->codatributo;
$attributeValue->valor = 'Value 1';
$this->assertTrue($attributeValue->save(), 'attribute-value-cant-save');
$attValue = new AtributoValor();
$attValue->codatributo = $attribute->codatributo;
$attValue->valor = 'Value 1';
$this->assertTrue($attValue->save(), 'attribute-value-cant-save');

// creamos otro valor, pero con getNewValue
$attValue2 = $attribute->getNewValue('Value 2');
$this->assertTrue($attValue2->save(), 'attribute-value-cant-save');

// eliminamos el atributo
$this->assertTrue($attribute->delete(), 'attribute-value-cant-delete');

// se debe haber eliminado el valor
$this->assertFalse($attributeValue->exists(), 'attribute-value-still-persist');
// se deben haber eliminado los valores
$this->assertFalse($attValue->exists(), 'attribute-value-still-persist');
$this->assertFalse($attValue2->exists(), 'attribute-value-still-persist');
}

public function testValueNoAttribute()
public function testValueNoAttribute(): void
{
$attributeValue = new AtributoValor();
$attributeValue->valor = 'Value 1';
Expand All @@ -75,7 +80,7 @@ private function getTestAttribute(): Atributo
{
$attribute = new Atributo();
$attribute->codatributo = 'Test';
$attribute->nombre = 'Test Atribute';
$attribute->nombre = 'Test Attribute';
return $attribute;
}

Expand Down
70 changes: 35 additions & 35 deletions Test/Core/Model/ContactoTest.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2017-2023 Carlos Garcia Gomez <[email protected]>
* Copyright (C) 2017-2024 Carlos Garcia Gomez <[email protected]>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
Expand Down Expand Up @@ -212,22 +212,22 @@ public function testVies(): void
$this->assertTrue($contact->checkVies());
}

public function testAlias()
public function testAlias(): void
{
$contacto = new Contacto();
$contacto->idcontacto = 999;

$result = $contacto->alias();

static::assertEquals('999', $result);
$this->assertEquals('999', $result);

$contacto->email = '[email protected]';
$result = $contacto->alias();
static::assertEquals('noreply_999', $result);
$this->assertEquals('noreply_999', $result);

$contacto->email = '[email protected]';
$result = $contacto->alias();
static::assertEquals('example_999', $result);
$this->assertEquals('example_999', $result);
}

public function testCodeModelSearch(): void
Expand All @@ -242,60 +242,60 @@ public function testCodeModelSearch(): void
$query = '';
$fieldCode = '';
$results = (new Contacto())->codeModelSearch($query, $fieldCode, []);
static::assertCount(count((new Contacto())->all()), $results);
$this->assertCount(count((new Contacto())->all()), $results);

// Pasando el nombre del primer contacto debe devolver solo un registro
$query = $contact1->nombre;
$fieldCode = '';
$results = (new Contacto())->codeModelSearch($query, $fieldCode, []);
static::assertCount(1, $results);
static::assertEquals($contact1->descripcion, trim($results[0]->description));
$this->assertCount(1, $results);
$this->assertEquals($contact1->descripcion, trim($results[0]->description));

// Pasando un valor que no existe no devuelve ningún contacto
$query = 'dummy-text';
$fieldCode = '';
$results = (new Contacto())->codeModelSearch($query, $fieldCode, []);
static::assertCount(0, $results);
$this->assertCount(0, $results);

// Pasando una clausula where devuelve el resultado de la consulta
$query = '';
$fieldCode = '';
$where = [new DataBaseWhere('empresa', $contact2->empresa)];
$results = (new Contacto())->codeModelSearch($query, $fieldCode, $where);
static::assertCount(1, $results);
static::assertEquals($contact2->descripcion, trim($results[0]->description));
$this->assertCount(1, $results);
$this->assertEquals($contact2->descripcion, trim($results[0]->description));

$contact1->delete();
$contact2->delete();
}

public function testCountry()
public function testCountry(): void
{
$contacto = new Contacto();
$contacto->codpais = 'ESP';

static::assertEquals('España', $contacto->country());
$this->assertEquals('España', $contacto->country());

$contacto->codpais = 'ABW';
static::assertEquals('Aruba', $contacto->country());
$this->assertEquals('Aruba', $contacto->country());

$contacto->codpais = 'WRONG-COD-PAIS';
static::assertEquals('WRONG-COD-PAIS', $contacto->country());
$this->assertEquals('WRONG-COD-PAIS', $contacto->country());
}

public function testGetCustomer()
public function testGetCustomer(): void
{
$contacto = $this->getRandomContact();

// Como no existe Cliente asociado al contacto devuelve un Cliente vacío(todos los campos a null)
// ya que indicamos por parametro que no cree ningún cliente
$result = $contacto->getCustomer(false);
static::assertNull($result->codcliente);
$this->assertNull($result->codcliente);

// Crea un Cliente con los mismos campos del contacto ya que no tiene ningún Cliente asociado
// y hemos indicado por parametro que se cree un Cliente
$result = $contacto->getCustomer(true);
static::assertEquals($result->codcliente, $contacto->codcliente);
$this->assertEquals($result->codcliente, $contacto->codcliente);

// Creamos un Cliente y lo asociamos al Contacto. Debe devolver el Cliente asociado
$cliente = $this->getRandomCustomer();
Expand All @@ -306,22 +306,22 @@ public function testGetCustomer()

$result = $contacto->getCustomer(true);

static::assertEquals($cliente->codcliente, $result->codcliente);
$this->assertEquals($cliente->codcliente, $result->codcliente);
}

public function testGetSupplier()
public function testGetSupplier(): void
{
$contacto = $this->getRandomContact();

// Como no existe Proveedor asociado al contacto devuelve un Proveedor vacío(todos los campos a null)
// ya que indicamos por parametro que no cree ningún cliente
$result = $contacto->getSupplier(false);
static::assertNull($result->codproveedor);
$this->assertNull($result->codproveedor);

// Crea un Proveedor con los mismos campos del contacto ya que no tiene ningún Proveedor asociado
// y hemos indicado por parametro que se cree un Proveedor
$result = $contacto->getSupplier(true);
static::assertEquals($result->codproveedor, $contacto->codproveedor);
$this->assertEquals($result->codproveedor, $contacto->codproveedor);

// Creamos un Proveedor y lo asociamos al Contacto. Debe devolver el Proveedor asociado
$cliente = $this->getRandomSupplier();
Expand All @@ -332,56 +332,56 @@ public function testGetSupplier()

$result = $contacto->getSupplier(true);

static::assertEquals($cliente->codproveedor, $result->codproveedor);
$this->assertEquals($cliente->codproveedor, $result->codproveedor);
}

public function testInstall()
public function testInstall(): void
{
$contacto = new Contacto();
$result = $contacto->install();
static::assertEquals('', $result);
$this->assertEquals('', $result);
}

public function testNewLogkey()
public function testNewLogkey(): void
{
$fakeIP = '192.192.192.192';

$contacto = new Contacto();

$result = $contacto->newLogkey($fakeIP);

static::assertEquals($fakeIP, $contacto->lastip);
static::assertEquals($contacto->logkey, $result);
$this->assertEquals($fakeIP, $contacto->lastip);
$this->assertEquals($contacto->logkey, $result);
}

public function testPrimaryDescriptionColumn()
public function testPrimaryDescriptionColumn(): void
{
$contacto = new Contacto();

$result = $contacto->primaryDescriptionColumn();

static::assertEquals('descripcion', $result);
$this->assertEquals('descripcion', $result);
}

public function testUrl()
public function testUrl(): void
{
$contacto = new Contacto();

$result = $contacto->url();

static::assertEquals('ListCliente?activetab=ListContacto', $result);
$this->assertEquals('ListCliente?activetab=ListContacto', $result);
}

public function testVerifyLogkey()
public function testVerifyLogkey(): void
{
$contacto = new Contacto();
$contacto->logkey = 'fake-logkey';

$result = $contacto->verifyLogkey('fake-logkey');
static::assertTrue($result);
$this->assertTrue($result);

$result = $contacto->verifyLogkey('fake-logkey-2');
static::assertFalse($result);
$this->assertFalse($result);
}

protected function tearDown(): void
Expand Down
Loading

0 comments on commit 5d3f223

Please sign in to comment.