Skip to content
Open
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
118 changes: 110 additions & 8 deletions Controller/Modelo130.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@
namespace FacturaScripts\Plugins\Modelo130\Controller;

use FacturaScripts\Core\Base\Controller;
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\DataSrc\Ejercicios;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Model\Asiento;
use FacturaScripts\Dinamic\Model\Ejercicio;
use FacturaScripts\Dinamic\Model\FacturaCliente;
Expand Down Expand Up @@ -59,6 +59,12 @@ class Modelo130 extends Controller
/** @var Subcuenta130 */
public $deductibleSubaccount;

/** @var Partida[] */
public $incomeEntries = [];

/** @var Subcuenta130 */
public $incomeSubaccount;

/** @var string */
public $period = 'T1';

Expand Down Expand Up @@ -123,6 +129,12 @@ public function getAllExercises(?int $idempresa): array
return $list;
}

public function getDeductibleSubaccounts(): array
{
$where = [new Where('tipo', Subcuenta130::TIPO_DEDUCIBLE)];
return (new Subcuenta130())->all($where, ['codsubcuenta' => 'ASC'], 0, 0);
}

/**
* @param string|null $codejercicio
* @return Ejercicio
Expand All @@ -135,6 +147,12 @@ public function getExercise(?string $codejercicio): Ejercicio
return $exercise;
}

public function getIncomeSubaccounts(): array
{
$where = [new Where('tipo', Subcuenta130::TIPO_INGRESO)];
return (new Subcuenta130())->all($where, ['codsubcuenta' => 'ASC'], 0, 0);
}

public function getPageData(): array
{
$data = parent::getPageData();
Expand All @@ -158,6 +176,7 @@ public function privateCore(&$response, $user, $permissions)
{
parent::privateCore($response, $user, $permissions);
$this->deductibleSubaccount = new Subcuenta130();
$this->incomeSubaccount = new Subcuenta130();

$this->paymentMethods = FormaPago::all();

Expand All @@ -173,13 +192,20 @@ public function privateCore(&$response, $user, $permissions)
case 'delete-deductible-subaccount':
return $this->deleteDeductibleSubaccount();

case 'add-income-subaccount':
return $this->addIncomeSubaccount();

case 'delete-income-subaccount':
return $this->deleteIncomeSubaccount();

case 'gen-accounting':
return $this->createAccountingEntry();
}

$this->loadDates();
$this->loadInvoices();
$this->loadAsientos();
$this->loadIncomeAsientos();
$this->loadResults();
}

Expand All @@ -202,6 +228,26 @@ protected function addDeductibleSubaccount(): bool
return true;
}

protected function addIncomeSubaccount(): bool
{
$this->activeTab = 'income-subaccount';

if (false === $this->validateFormToken()) {
return false;
}

$subaccount = new Subcuenta130();
$subaccount->codsubcuenta = $this->request->request->get('codsubcuenta');
$subaccount->tipo = Subcuenta130::TIPO_INGRESO;
if (false === $subaccount->save()) {
Tools::log()->error('record-save-error');
return false;
}

Tools::log()->notice('record-updated-correctly');
return true;
}

protected function autocompleteSubaccount(): void
{
$this->setTemplate(false);
Expand Down Expand Up @@ -248,6 +294,29 @@ protected function deleteDeductibleSubaccount(): bool
return false;
}

protected function deleteIncomeSubaccount(): bool
{
$this->activeTab = 'income-subaccount';

if (false === $this->validateFormToken()) {
return false;
}

$subaccount = new Subcuenta130();
if (false === $subaccount->load($this->request->request->get('id'))) {
Tools::log()->error('record-not-found');
return false;
}

if (false === $subaccount->delete()) {
Tools::log()->error('record-deleted-error');
return false;
}

Tools::log()->notice('record-deleted-correctly');
return false;
}

// Traemos del codejercicio y period elegido idempresa, dateStart y dateEnd
protected function loadDates(): void
{
Expand Down Expand Up @@ -289,20 +358,20 @@ protected function loadInvoices(): void

$whereFtrasProveedores = [
// Para buscar en el margen de fechas del periodo
new DataBaseWhere('fecha', date('Y-m-d', strtotime($this->dateStart)), '>='),
new DataBaseWhere('fecha', date('Y-m-d', strtotime($this->dateEnd)), '<='),
new Where('fecha', date('Y-m-d', strtotime($this->dateStart)), '>='),
new Where('fecha', date('Y-m-d', strtotime($this->dateEnd)), '<='),

// Para buscar ftras solo de la empresa/Ejercicio elegido
new DataBaseWhere('idempresa', $this->idempresa),
new Where('idempresa', $this->idempresa),
];

$whereFtrasClientes = [
// Para buscar en el margen de fechas del periodo
new DataBaseWhere('fecha', date('Y-m-d', strtotime($this->dateStart)), '>='),
new DataBaseWhere('fecha', date('Y-m-d', strtotime($this->dateEnd)), '<='),
new Where('fecha', date('Y-m-d', strtotime($this->dateStart)), '>='),
new Where('fecha', date('Y-m-d', strtotime($this->dateEnd)), '<='),

// Para buscar ftras solo de la empresa/Ejercicio elegido
new DataBaseWhere('idempresa', $this->idempresa),
new Where('idempresa', $this->idempresa),
];

// Preparamos el orderBy de como vamos a traer las facturas (fecha + numero ftra)
Expand Down Expand Up @@ -343,13 +412,42 @@ protected function getAccountingEntrySubaccounts(): array
{
$codsubs = [];
$subaccount130 = new Subcuenta130();
foreach ($subaccount130->all([], [], 0, 0) as $subaccount) {
$where = [new Where('tipo', Subcuenta130::TIPO_DEDUCIBLE)];
foreach ($subaccount130->all($where, [], 0, 0) as $subaccount) {
$codsubs[] = $subaccount->codsubcuenta;
}

return self::sanitizeSubaccountCodes($codsubs);
}

protected function loadIncomeAsientos(): void
{
$codsubs = [];
$sub = new Subcuenta130();
$where = [new Where('tipo', Subcuenta130::TIPO_INGRESO)];
foreach ($sub->all($where, [], 0, 0) as $subaccount) {
$codsubs[] = $subaccount->codsubcuenta;
}
$codsubs = self::sanitizeSubaccountCodes($codsubs);

if (empty($codsubs)) {
return;
}

$sql = 'SELECT * FROM ' . Partida::tableName() . ' as p'
. ' LEFT JOIN ' . Asiento::tableName() . ' as a ON p.idasiento = a.idasiento'
. ' WHERE ' . $this->getSqlValueCondition('a.idempresa', $this->idempresa)
. ' AND a.fecha BETWEEN ' . $this->dataBase->var2str(date('Y-m-d', strtotime($this->dateStart)))
. ' AND ' . $this->dataBase->var2str(date('Y-m-d', strtotime($this->dateEnd)))
. ' AND p.codsubcuenta IN (' . $this->getSqlValueList($codsubs) . ')'
. ' AND a.operacion IS ' . $this->dataBase->var2str(Asiento::OPERATION_GENERAL)
. ' ORDER BY numero ASC';

foreach ($this->dataBase->select($sql) as $row) {
$this->incomeEntries[] = new Partida($row);
}
}

protected function getSqlValueCondition(string $field, $value): string
{
if (null === $value) {
Expand Down Expand Up @@ -391,6 +489,10 @@ protected function loadResults(): void
$this->taxbaseRetenciones += $invoice->totalirpf;
}

foreach ($this->incomeEntries as $partida) {
$this->taxbaseIngresos += $partida->haber;
}

foreach ($this->supplierInvoices as $invoice) {
$this->taxbaseGastos += $invoice->neto;
}
Expand Down
19 changes: 17 additions & 2 deletions Model/Subcuenta130.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,18 +19,21 @@

namespace FacturaScripts\Plugins\Modelo130\Model;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Session;
use FacturaScripts\Core\Template\ModelClass;
use FacturaScripts\Core\Template\ModelTrait;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Model\Subcuenta;
use FacturaScripts\Dinamic\Model\User;

class Subcuenta130 extends ModelClass
{
use ModelTrait;

const TIPO_DEDUCIBLE = 'deducible';
const TIPO_INGRESO = 'ingreso';

/** @var string */
public $codsubcuenta;

Expand All @@ -52,10 +55,19 @@ class Subcuenta130 extends ModelClass
/** @var string */
public $nick;

/** @var string */
public $tipo = self::TIPO_DEDUCIBLE;

public function clear(): void
{
parent::clear();
$this->tipo = self::TIPO_DEDUCIBLE;
}

public function getSubcuenta(): Subcuenta
{
$subcuenta = new Subcuenta();
$where = [new DataBaseWhere('codsubcuenta', $this->codsubcuenta)];
$where = [new Where('codsubcuenta', $this->codsubcuenta)];
$subcuenta->loadWhere($where);
return $subcuenta;
}
Expand Down Expand Up @@ -89,6 +101,9 @@ public function test(): bool

$this->codsubcuenta = trim(Tools::noHtml((string)$this->codsubcuenta));
$this->name = Tools::noHtml($this->name);
$this->tipo = in_array($this->tipo, [self::TIPO_DEDUCIBLE, self::TIPO_INGRESO], true)
? $this->tipo
: self::TIPO_DEDUCIBLE;

if (strlen($this->codsubcuenta) < 1 || strlen($this->codsubcuenta) > 15) {
Tools::log()->warning('invalid-column-lenght', [
Expand Down
6 changes: 6 additions & 0 deletions Table/subcuentas_130.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
<name>nick</name>
<type>character varying(50)</type>
</column>
<column>
<name>tipo</name>
<type>character varying(20)</type>
<default>'deducible'</default>
<null>NO</null>
</column>
<constraint>
<name>subcuentas_130_pkey</name>
<type>PRIMARY KEY (id)</type>
Expand Down
62 changes: 62 additions & 0 deletions Test/main/Model/Subcuenta130Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,68 @@ public function testCreateEmptySubcuenta130Fails(): void
$this->assertFalse($subcuenta130->save(), 'empty-subcuenta130-should-fail');
}

public function testSubcuenta130TieneTipoDeduciblePorDefecto(): void
{
$sub130 = new Subcuenta130();
$this->assertSame(Subcuenta130::TIPO_DEDUCIBLE, $sub130->tipo);
}

public function testTipoInvalidoFuerzaTipoDeducible(): void
{
$exercise = $this->getRandomExercise();

$account = new Cuenta();
$account->codcuenta = '9998';
$account->codejercicio = $exercise->codejercicio;
$account->descripcion = 'Test tipo invalido';
$this->assertTrue($account->save(), 'cant-save-account');

$subaccount = new Subcuenta();
$subaccount->codcuenta = $account->codcuenta;
$subaccount->codejercicio = $exercise->codejercicio;
$subaccount->codsubcuenta = '9998000000';
$subaccount->descripcion = 'Test tipo invalido';
$this->assertTrue($subaccount->save(), 'cant-save-subaccount');

$sub130 = new Subcuenta130();
$sub130->codsubcuenta = $subaccount->codsubcuenta;
$sub130->tipo = 'tipo_invalido_xyz';
$this->assertTrue($sub130->save(), 'cant-save-subcuenta130');
$this->assertSame(Subcuenta130::TIPO_DEDUCIBLE, $sub130->tipo, 'invalid-tipo-should-fallback-to-deducible');

$this->assertTrue($sub130->delete());
$this->assertTrue($subaccount->delete());
$this->assertTrue($account->delete());
}

public function testCreateSubcuenta130ConTipoIngreso(): void
{
$exercise = $this->getRandomExercise();

$account = new Cuenta();
$account->codcuenta = '9997';
$account->codejercicio = $exercise->codejercicio;
$account->descripcion = 'Test ingreso';
$this->assertTrue($account->save(), 'cant-save-account');

$subaccount = new Subcuenta();
$subaccount->codcuenta = $account->codcuenta;
$subaccount->codejercicio = $exercise->codejercicio;
$subaccount->codsubcuenta = '9997000000';
$subaccount->descripcion = 'Test ingreso';
$this->assertTrue($subaccount->save(), 'cant-save-subaccount');

$sub130 = new Subcuenta130();
$sub130->codsubcuenta = $subaccount->codsubcuenta;
$sub130->tipo = Subcuenta130::TIPO_INGRESO;
$this->assertTrue($sub130->save(), 'cant-save-subcuenta130-ingreso');
$this->assertSame(Subcuenta130::TIPO_INGRESO, $sub130->tipo, 'tipo-should-be-ingreso');

$this->assertTrue($sub130->delete());
$this->assertTrue($subaccount->delete());
$this->assertTrue($account->delete());
}

protected function getRandomExercise(): Ejercicio
{
$model = new Ejercicio();
Expand Down
Loading
Loading