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
1 change: 1 addition & 0 deletions Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ private function setupSettings(): void
{
Tools::settings('proyectos', 'patron', 'PR-{ANYO}-{NUM}');
Tools::settings('proyectos', 'longnumero', 6);
Tools::settings('proyectos', 'reiniciarpatronanualmente', 0);
Tools::settingsSave();
}
}
49 changes: 48 additions & 1 deletion Lib/ProjectCodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Tools;
use FacturaScripts\Plugins\Proyectos\Model\Proyecto;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;

/**
* Description of ProjectCodeGenerator
Expand All @@ -37,9 +38,55 @@ public static function new(&$project)
{
$patron = Tools::settings('proyectos', 'patron', 'PR-{ANYO}-{NUM}');
$long_numero = Tools::settings('proyectos', 'longnumero', 6);
$reset = (bool) Tools::settings('proyectos', 'reiniciarpatronanualmente', 0);

$proyecto = new Proyecto();
$numero = 1 + $proyecto->count();

if (!$reset) {
// default behaviour: keep existing global counting
$numero = 1 + $proyecto->count();
} else {
// build prefix replacing date placeholders but leaving numeric placeholders empty
$replacements = [
'{ANYO}' => date('Y'),
'{ANYO2}' => date('y'),
'{MES}' => date('m'),
'{DIA}' => date('d'),
'{NUM}' => '',
'{0NUM}' => ''
];

$prefix = strtr($patron, $replacements);

// find projects that start with the same prefix and belong to the same company, limited to current year
$year = date('Y');
$where = [
new DataBaseWhere('nombre', $prefix . '%', 'LIKE'),
new DataBaseWhere('idempresa', $project->idempresa),
new DataBaseWhere('fecha', $year . '-01-01', '>='),
new DataBaseWhere('fecha', $year . '-12-31', '<=')
];
$projects = Proyecto::all($where, [], 0, 0);

$max = 0;
foreach ($projects as $p) {
if (empty($p->fecha)) {
continue;
}
$pYear = date('Y', strtotime($p->fecha));
if ($pYear !== $year) {
continue;
}
if (preg_match('/(\\d+)$/', $p->nombre, $m)) {
$num = intval($m[1]);
if ($num > $max) {
$max = $num;
}
}
}

$numero = $max + 1;
}

$project->nombre = strtr($patron, [
'{ANYO}' => date('Y'),
Expand Down
65 changes: 65 additions & 0 deletions Test/main/ProyectoPatternResetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace FacturaScripts\Test\Plugins;

use FacturaScripts\Plugins\Proyectos\Model\Proyecto;
use FacturaScripts\Core\Tools;
use FacturaScripts\Test\Traits\DefaultSettingsTrait;
use FacturaScripts\Test\Traits\LogErrorsTrait;
use PHPUnit\Framework\TestCase;

final class ProyectoPatternResetTest extends TestCase
{
use LogErrorsTrait;
use DefaultSettingsTrait;

public static function setUpBeforeClass(): void
{
self::setDefaultSettings();
self::installAccountingPlan();
self::removeTaxRegularization();
}

public function testAnnualPatternReset(): void
{
// activar ajuste
Tools::settings('proyectos', 'reiniciarpatronanualmente', 1);
Tools::settingsSave();

$prevYear = date('Y') - 1;
$patron = Tools::settings('proyectos', 'patron', 'PR-{ANYO}-{NUM}');
$long = Tools::settings('proyectos', 'longnumero', 6);

// crear proyecto del año anterior
$old = new Proyecto();
$old->nombre = strtr($patron, [
'{ANYO}' => (string)$prevYear,
'{ANYO2}' => substr((string)$prevYear, 2),
'{MES}' => '01',
'{DIA}' => '01',
'{NUM}' => '5',
'{0NUM}' => str_pad('5', $long, '0', STR_PAD_LEFT)
]);
$old->fecha = $prevYear . '-01-01';
$old->descripcion = 'old';
$this->assertTrue($old->save());

// crear proyecto nuevo (debe generarse y empezar en 1)
$new = new Proyecto();
$new->descripcion = 'nuevo';
$this->assertTrue($new->save());

$this->assertMatchesRegularExpression('/(\\d+)$/', $new->nombre, 'El nombre generado debe terminar en dígitos');
preg_match('/(\\d+)$/', $new->nombre, $m);
$this->assertEquals(1, intval($m[1]), 'Se esperaba que la numeración empiece en 1 para el ejercicio actual');

// limpieza
$this->assertTrue($new->delete());
$this->assertTrue($old->delete());
}

protected function tearDown(): void
{
$this->logErrors();
}
}
3 changes: 2 additions & 1 deletion Translation/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"phases": "Phases",
"project-stock-rebuild-error": "Error rebuilding project stock",
"project-stock-rebuild-ok": "Project stock rebuilt successfully",
"reiniciar-patron-anualmente": "Reset pattern annually",
"show-project-stock": "Show project stock",
"tasks-imported-correctly": "Tasks imported correctly",
"total-pending-invoice": "Pending invoice",
"unlink-document": "Unlink..."
}
}
3 changes: 2 additions & 1 deletion Translation/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
"phases": "Fases",
"project-stock-rebuild-error": "Error al regenerar el stock",
"project-stock-rebuild-ok": "Stock regenerado correctamente",
"reiniciar-patron-anualmente": "Reiniciar patrón anualmente",
"show-project-stock": "Mostrar stock del proyecto",
"tasks-imported-correctly": "Tareas importadas correctamente",
"total-pending-invoice": "Pendiente facturar",
"unlink-document": "Desvincular..."
}
}
5 changes: 4 additions & 1 deletion XMLView/ConfigProyectos.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
<column name="pattern" numcolumns="3" order="100">
<widget type="text" fieldname="patron" required="true"/>
</column>
<column name="reset-pattern" numcolumns="12" order="115" title="reiniciar-patron-anualmente">
<widget type="checkbox" fieldname="reiniciarpatronanualmente"/>
</column>
<column name="number-length" numcolumns="3" order="110">
<widget type="number" fieldname="longnumero" decimal="0" min="1" required="true"/>
</column>
Expand All @@ -21,4 +24,4 @@
</column>
</group>
</columns>
</view>
</view>
Loading