Skip to content

Commit b6316b3

Browse files
committed
Fix tax summary query and test cleanup
1 parent 8453bda commit b6316b3

4 files changed

Lines changed: 111 additions & 12 deletions

File tree

Model/Join/PartidaImpuestoResumen.php

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function getFields(): array
3636
{
3737
return [
3838
'codsubcuenta' => 'partidas.codsubcuenta',
39+
'idsubcuenta' => 'partidas.idsubcuenta',
3940
'iva' => 'COALESCE(partidas.iva, 0)',
4041
'recargo' => 'COALESCE(partidas.recargo, 0)',
4142

@@ -44,10 +45,10 @@ protected function getFields(): array
4445
'codcuentaesp' => 'COALESCE(subcuentas.codcuentaesp, cuentas.codcuentaesp)',
4546
'tipo_desc' => 'cuentasesp.descripcion',
4647

47-
'baseimponible' => 'ROUND(SUM(partidas.baseimponible), 2)',
48-
'debe' => 'ROUND(SUM(partidas.debe), 2)',
49-
'haber' => 'ROUND(SUM(partidas.haber), 2)',
50-
'cuota' => 'ROUND(SUM(' . $this->sqlForCuota() . '), 2)',
48+
'baseimponible' => $this->sqlForRoundedSum('partidas.baseimponible'),
49+
'debe' => $this->sqlForRoundedSum('partidas.debe'),
50+
'haber' => $this->sqlForRoundedSum('partidas.haber'),
51+
'cuota' => $this->sqlForRoundedSum($this->sqlForCuota()),
5152
];
5253
}
5354

@@ -59,6 +60,7 @@ protected function getFields(): array
5960
protected function getGroupFields(): string
6061
{
6162
return 'partidas.codsubcuenta'
63+
. ', partidas.idsubcuenta'
6264
. ', partidas.iva'
6365
. ', partidas.recargo'
6466
. ', subcuentas.descripcion'
@@ -98,6 +100,19 @@ protected function getTables(): array
98100
];
99101
}
100102

103+
/**
104+
* Assign derived compatibility fields used by existing consumers.
105+
*
106+
* @param array $data
107+
*/
108+
protected function loadFromData(array $data)
109+
{
110+
parent::loadFromData($data);
111+
112+
$this->cuotaiva = $this->codcuentaesp === 'IVARRE' ? 0.0 : (float)$this->cuota;
113+
$this->cuotarecargo = $this->codcuentaesp === 'IVARRE' ? (float)$this->cuota : 0.0;
114+
}
115+
101116
/**
102117
* SQL snippet to calculate the cuota field.
103118
*
@@ -110,4 +125,16 @@ private function sqlForCuota(): string
110125
ELSE partidas.debe + partidas.haber
111126
END';
112127
}
128+
129+
/**
130+
* SQL snippet to round aggregated amounts on PostgreSQL and MySQL.
131+
*
132+
* @param string $field
133+
*
134+
* @return string
135+
*/
136+
private function sqlForRoundedSum(string $field): string
137+
{
138+
return 'ROUND(CAST(SUM(' . $field . ') AS DECIMAL(20, 6)), 2)';
139+
}
113140
}

Test/main/ComprobarFechaDevengoTest.php

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,41 @@ class ComprobarFechaDevengoTest extends Modelo303TestCase
2424
public function testComprobarFechaDevengo()
2525
{
2626
$invoiceFechaHoy = $this->getRandomCustomerInvoice();
27+
$this->addCleanup(static function () use ($invoiceFechaHoy) {
28+
if ($invoiceFechaHoy->exists()) {
29+
$invoiceFechaHoy->delete();
30+
}
31+
$subject = $invoiceFechaHoy->getSubject();
32+
if ($subject->id()) {
33+
$subject->delete();
34+
}
35+
});
2736

2837
// creamos una factura con fecha de devengo anterior
2938
$invoiceFechaDevengoAnterior = $this->getRandomCustomerInvoice();
39+
$this->addCleanup(static function () use ($invoiceFechaDevengoAnterior) {
40+
if ($invoiceFechaDevengoAnterior->exists()) {
41+
$invoiceFechaDevengoAnterior->delete();
42+
}
43+
$subject = $invoiceFechaDevengoAnterior->getSubject();
44+
if ($subject->id()) {
45+
$subject->delete();
46+
}
47+
});
3048
$invoiceFechaDevengoAnterior->fechadevengo = Tools::date('-1 month');
3149
$this->assertTrue($invoiceFechaDevengoAnterior->save());
3250

3351
// creamos una factura con fecha de devengo posterior
3452
$invoiceFechaDevengoPosterior = $this->getRandomCustomerInvoice();
53+
$this->addCleanup(static function () use ($invoiceFechaDevengoPosterior) {
54+
if ($invoiceFechaDevengoPosterior->exists()) {
55+
$invoiceFechaDevengoPosterior->delete();
56+
}
57+
$subject = $invoiceFechaDevengoPosterior->getSubject();
58+
if ($subject->id()) {
59+
$subject->delete();
60+
}
61+
});
3562
$invoiceFechaDevengoPosterior->fechadevengo = Tools::date('+1 month');
3663
$this->assertTrue($invoiceFechaDevengoPosterior->save());
3764

@@ -65,7 +92,7 @@ public function testComprobarFechaDevengo()
6592
$this->assertEquals($invoiceFechaDevengoPosterior->totaliva, $partida->haber);
6693

6794
// comprobamos que solo obtiene los resultados de todas las facturas
68-
$totalIVAFacturasCliente = FacturaCliente::table()->sum('totaliva');
95+
$totalIVAFacturasCliente = FacturaCliente::table()->sum('totaliva', 2);
6996

7097
$partidaImpuestoResumen = new PartidaImpuestoResumen();
7198
$partida = $partidaImpuestoResumen->all([

Test/main/Modelo303TestCase.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ class Modelo303TestCase extends TestCase
1212
use DefaultSettingsTrait;
1313
use LogErrorsTrait;
1414

15+
/**
16+
* Cleanup callbacks to run after each test in reverse order.
17+
*
18+
* @var array<int, callable>
19+
*/
20+
private array $cleanupCallbacks = [];
21+
1522
public function setUp(): void
1623
{
1724
// inicializamos los modelos para que se creen las
@@ -28,8 +35,21 @@ public function setUp(): void
2835
self::installAccountingPlan();
2936
}
3037

38+
protected function addCleanup(callable $callback): void
39+
{
40+
$this->cleanupCallbacks[] = $callback;
41+
}
42+
3143
protected function tearDown(): void
3244
{
45+
while ($callback = array_pop($this->cleanupCallbacks)) {
46+
try {
47+
$callback();
48+
} catch (\Throwable $th) {
49+
error_log($th->getMessage());
50+
}
51+
}
52+
3353
$this->logErrors();
3454
}
3555
}

Test/main/VatRegularizationToAccountingTest.php

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,52 @@ public function testCreate()
4141
{
4242
// creamos una factura de proveedor
4343
$supplierInvoice = $this->getRandomSupplierInvoice('now');
44+
$this->addCleanup(static function () use ($supplierInvoice) {
45+
if ($supplierInvoice->exists()) {
46+
$supplierInvoice->delete();
47+
}
48+
$subject = $supplierInvoice->getSubject();
49+
if ($subject->id()) {
50+
$subject->delete();
51+
}
52+
});
4453
$this->assertTrue($supplierInvoice->save());
4554

4655
// creamos dos facturas de cliente
4756
$customerInvoice1 = $this->getRandomCustomerInvoice('+1 day');
57+
$this->addCleanup(static function () use ($customerInvoice1) {
58+
if ($customerInvoice1->exists()) {
59+
$customerInvoice1->delete();
60+
}
61+
$subject = $customerInvoice1->getSubject();
62+
if ($subject->id()) {
63+
$subject->delete();
64+
}
65+
});
4866
$this->assertTrue($customerInvoice1->save());
4967

5068
$customerInvoice2 = $this->getRandomCustomerInvoice('+2 day');
69+
$this->addCleanup(static function () use ($customerInvoice2) {
70+
if ($customerInvoice2->exists()) {
71+
$customerInvoice2->delete();
72+
}
73+
$subject = $customerInvoice2->getSubject();
74+
if ($subject->id()) {
75+
$subject->delete();
76+
}
77+
});
5178
$this->assertTrue($customerInvoice2->save());
5279

5380
// creamos una regularización
5481
$reg = new RegularizacionImpuesto();
5582
$reg->codejercicio = $supplierInvoice->codejercicio;
5683
$reg->periodo = 'T1';
5784
$this->assertTrue($reg->save());
85+
$this->addCleanup(static function () use ($reg) {
86+
if ($reg->exists()) {
87+
$reg->delete();
88+
}
89+
});
5890

5991
// generamos el asiento contable
6092
$generator = new VatRegularizationToAccounting();
@@ -68,13 +100,6 @@ public function testCreate()
68100

69101
// comprobamos que se ha eliminado el asiento contable
70102
$this->assertFalse($reg->getAccountingEntry()->exists());
71-
72-
$this->assertTrue($supplierInvoice->delete());
73-
$this->assertTrue($supplierInvoice->getSubject()->delete());
74-
$this->assertTrue($customerInvoice1->delete());
75-
$this->assertTrue($customerInvoice2->delete());
76-
$this->assertTrue($customerInvoice1->getSubject()->delete());
77-
$this->assertTrue($customerInvoice2->getSubject()->delete());
78103
}
79104

80105
protected function tearDown(): void

0 commit comments

Comments
 (0)