diff --git a/Core/Lib/ExtendedController/ComercialContactController.php b/Core/Lib/ExtendedController/ComercialContactController.php index 3626b7bc5a..2597b4a621 100644 --- a/Core/Lib/ExtendedController/ComercialContactController.php +++ b/Core/Lib/ExtendedController/ComercialContactController.php @@ -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 @@ -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. * @@ -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); @@ -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': diff --git a/Core/Model/Cliente.php b/Core/Model/Cliente.php index ed5097ab8f..826cb02c76 100644 --- a/Core/Model/Cliente.php +++ b/Core/Model/Cliente.php @@ -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; @@ -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 @@ -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); } } diff --git a/Core/Model/Proveedor.php b/Core/Model/Proveedor.php index 28d5e8a32c..e6441ef0cb 100644 --- a/Core/Model/Proveedor.php +++ b/Core/Model/Proveedor.php @@ -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; @@ -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 @@ -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); } } diff --git a/Test/Core/Lib/AccountingCreationTest.php b/Test/Core/Lib/AccountingCreationTest.php index 34896e8e81..94a2b21d7d 100644 --- a/Test/Core/Lib/AccountingCreationTest.php +++ b/Test/Core/Lib/AccountingCreationTest.php @@ -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; @@ -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();