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
36 changes: 35 additions & 1 deletion Core/Lib/ExtendedController/ComercialContactController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
use FacturaScripts\Core\Lib\InvoiceOperation;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator;
use FacturaScripts\Dinamic\Model\Ejercicio as DinEjercicio;

/**
* Controller for editing models that are related and show
Expand Down Expand Up @@ -143,6 +144,29 @@ protected function checkViesAction(): bool
return true;
}

protected function addSubaccountAction(): bool
{

$model = $this->getModel();
$primaryKey = $this->request->input($model->primaryColumn());
$code = $this->request->query('code', $primaryKey);
if (false === $model->loadFromCode($code)) {
Tools::log()->error('record-not-found');
return true;
}

$ejercicio = new DinEjercicio();
$ejercicio->idempresa = Tools::settings('default', 'idempresa');
if ($ejercicio->loadFromDate(Tools::date(), true, false)) {
$subAccount = $model->createSubaccount($ejercicio->codejercicio);
if ($subAccount->exists()) {
Tools::log()->notice('record-updated-correctly');
}
}

return true;
}

/**
* Add a Contact List View.
*
Expand Down Expand Up @@ -300,6 +324,9 @@ protected function execPreviousAction($action)
switch ($action) {
case 'add-file':
return $this->addFileAction();

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

case 'approve-document':
return $this->approveDocumentAction($codes, $model, $allowUpdate, $this->dataBase);
Expand Down Expand Up @@ -402,7 +429,14 @@ protected function loadData($viewName, $view)
$codsubcuenta = $this->getViewModelValue($mvn, 'codsubcuenta');
$where = [new DataBaseWhere('codsubcuenta', $codsubcuenta)];
$view->loadData('', $where);
$this->setSettings($viewName, 'active', $view->count > 0);
if ($view->count <= 0) {
$this->addButton($viewName, [
'action' => 'add-subaccount',
'color' => 'outline-success',
'icon' => 'fa-solid fa-plus',
'label' => 'add'
]);
}
break;

case 'ListEmailSent':
Expand Down
17 changes: 16 additions & 1 deletion Core/Model/Cliente.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use FacturaScripts\Dinamic\Model\Contacto as DinContacto;
use FacturaScripts\Dinamic\Model\CuentaBancoCliente as DinCuentaBancoCliente;
use FacturaScripts\Dinamic\Model\CuentaEspecial as DinCuentaEspecial;
use FacturaScripts\Dinamic\Model\Ejercicio as DinEjercicio;
use FacturaScripts\Dinamic\Model\FormaPago;
use FacturaScripts\Dinamic\Model\GrupoClientes as DinGrupoClientes;
use FacturaScripts\Dinamic\Model\Retencion;
Expand Down Expand Up @@ -355,6 +356,11 @@ public function test(): bool
return parent::test() && $this->testFiscalNumber() && $this->testEmailAndPhones() && $this->testDiasPago();
}

public function createSubaccount(string $codejercicio): Subcuenta
{
return $this->createSubcuenta($codejercicio);
}

protected function createSubcuenta(string $codejercicio): Subcuenta
{
// buscamos la cuenta especial
Expand Down Expand Up @@ -415,7 +421,16 @@ protected function saveInsert(): bool
$contact->tipoidfiscal = $this->tipoidfiscal;
if ($contact->save()) {
$this->idcontactofact = $contact->idcontacto;
return $this->save();
$return = $this->save();
}
}

// si no tiene subcuenta asignada, intentamos crearla si hay plan contable activo
if ($return && empty($this->codsubcuenta)) {
$ejercicio = new DinEjercicio();
$ejercicio->idempresa = Tools::settings('default', 'idempresa');
if ($ejercicio->loadFromDate(Tools::date(), true, false)) {
$this->createSubcuenta($ejercicio->codejercicio);
}
}

Expand Down
21 changes: 20 additions & 1 deletion Core/Model/Proveedor.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
use FacturaScripts\Dinamic\Model\Contacto as DinContacto;
use FacturaScripts\Dinamic\Model\CuentaBancoProveedor as DinCuentaBancoProveedor;
use FacturaScripts\Dinamic\Model\CuentaEspecial as DinCuentaEspecial;
use FacturaScripts\Dinamic\Model\Ejercicio as DinEjercicio;
use FacturaScripts\Dinamic\Model\Retencion;
use FacturaScripts\Dinamic\Model\Subcuenta as DinSubcuenta;

Expand Down Expand Up @@ -299,6 +300,14 @@ public function test(): bool
return parent::test() && $this->testEmailAndPhones() && $this->testFiscalNumber();
}

public function createSubaccount(string $codejercicio): Subcuenta
{
$specialAccount = $this->acreedor ?
static::SPECIAL_CREDITOR_ACCOUNT :
static::SPECIAL_ACCOUNT;
return $this->createSubcuenta($codejercicio, $specialAccount);
}

protected function createSubcuenta(string $codejercicio, string $specialAccount): Subcuenta
{
// buscamos la cuenta especial
Expand Down Expand Up @@ -355,7 +364,17 @@ protected function saveInsert(): bool
$contact->tipoidfiscal = $this->tipoidfiscal;
if ($contact->save()) {
$this->idcontacto = $contact->idcontacto;
return $this->save();
$return = $this->save();
}
}

// si no tiene subcuenta asignada, intentamos crearla si hay plan contable activo
if ($return && empty($this->codsubcuenta)) {
$ejercicio = new DinEjercicio();
$ejercicio->idempresa = Tools::settings('default', 'idempresa');
if ($ejercicio->loadFromDate(Tools::date(), true, false)) {
$specialAccount = $this->acreedor ? static::SPECIAL_CREDITOR_ACCOUNT : static::SPECIAL_ACCOUNT;
$this->createSubcuenta($ejercicio->codejercicio, $specialAccount);
}
}

Expand Down
15 changes: 15 additions & 0 deletions Test/Core/Lib/AccountingCreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

use FacturaScripts\Core\Base\DataBase;
use FacturaScripts\Core\DataSrc\Paises;
use FacturaScripts\Core\Where;
use FacturaScripts\Core\Lib\Accounting\AccountingAccounts;
use FacturaScripts\Core\Lib\Accounting\AccountingCreation;
use FacturaScripts\Core\Model\Cuenta;
Expand Down Expand Up @@ -63,6 +64,20 @@ public function testCreateCustomer(): void
$customersAccount = $accounts->getSpecialAccount(AccountingAccounts::SPECIAL_CUSTOMER_ACCOUNT);
$this->assertTrue($customersAccount->exists(), 'cant-get-customer-account');

// si al guardar el cliente se creó una subcuenta automáticamente, la registramos
// para limpiarla al final y reseteamos codsubcuenta para que el bucle pueda generar nuevas
if (!empty($customer->codsubcuenta)) {
$autoSubaccount = new Subcuenta();
$where = [
Where::eq('codsubcuenta', $customer->codsubcuenta),
Where::eq('codejercicio', $accounts->exercise->codejercicio),
];
if ($autoSubaccount->loadWhere($where)) {
self::$subaccounts[] = $autoSubaccount;
}
$customer->codsubcuenta = null;
}

// obtenemos una nueva subcuenta para el cliente, 1001 veces (solo España),
// para comprobar si en todos los casos se crea una nueva
$creator = new AccountingCreation();
Expand Down
Loading