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
213 changes: 54 additions & 159 deletions Controller/Modelo130.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@
use FacturaScripts\Core\DataSrc\Ejercicios;
use FacturaScripts\Core\Response;
use FacturaScripts\Core\Tools;
use FacturaScripts\Core\Where;
use FacturaScripts\Dinamic\Model\Ejercicio;
use FacturaScripts\Dinamic\Model\Empresa;
use FacturaScripts\Dinamic\Model\FormaPago;
use FacturaScripts\Dinamic\Model\Subcuenta130;
use FacturaScripts\Dinamic\Lib\Modelo130 as DinModelo130;
use FacturaScripts\Dinamic\Lib\Modelo130Export as DinModelo130Export;

Expand All @@ -41,24 +39,15 @@
*/
class Modelo130 extends Controller
{
/** @var string */
public $activeTab = '';

/** bool */
public $applyGastosJustificacion = false;

/** @var float */
public $gastosJustificacionPct = 7.0;
public $gastosJustificacionPct = 5.0;

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

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

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

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

Expand All @@ -84,13 +73,8 @@ public function getAllExercises(?int $idempresa): array
$list[] = $exercise;
}
}
return $list;
}

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

/**
Expand All @@ -104,12 +88,6 @@ public function getExercise(?string $codejercicio): Ejercicio
return $exercise;
}

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

public function getPageData(): array
{
$data = parent::getPageData();
Expand Down Expand Up @@ -138,43 +116,33 @@ public function getPeriodsForComboBoxHtml(): array
public function privateCore(&$response, $user, $permissions)
{
parent::privateCore($response, $user, $permissions);
$this->deductibleSubaccount = new Subcuenta130();
$this->incomeSubaccount = new Subcuenta130();

$action = $this->request->request->get('action', $this->request->input('action'));
switch ($action) {
case 'autocomplete-subaccount':
$this->autocompleteSubaccount();
return;

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

case 'delete-deductible-subaccount':
$this->deleteDeductibleSubaccount();
return;

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

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

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

$this->codejercicio = $this->request->request->get('codejercicio', '');
$this->period = $this->request->request->get('period', $this->period);
$this->applyGastosJustificacion = (bool)$this->request->request->get('applyGastosJustificacion', false);
$this->applyGastosJustificacion = (bool)$this->request->request->get(
'applyGastosJustificacion',
false
);
$this->todeduct = (float)$this->request->request->get('todeduct', 20.0);
$this->gastosJustificacionPct = (float)$this->request->request->get('gastosJustificacionPct', 7.0);

$this->result = DinModelo130::generate($this->codejercicio, $this->period, $this->applyGastosJustificacion, $this->todeduct, $this->gastosJustificacionPct);
$this->gastosJustificacionPct = (float)$this->request->request->get(
'gastosJustificacionPct',
5.0
);

$this->result = DinModelo130::generate(
$this->codejercicio,
$this->period,
$this->applyGastosJustificacion,
$this->todeduct,
$this->gastosJustificacionPct
);

if ($action === 'download') {
$this->downloadFile($response);
Expand Down Expand Up @@ -212,78 +180,40 @@ protected function downloadFile(Response $response): void
return;
}

$content = DinModelo130Export::generate($this->result, $empresa, $this->period, $year);
$content = DinModelo130Export::generate(
$this->result,
$empresa,
$this->period,
$year
);

if (strlen($content) !== DinModelo130Export::FILE_LENGTH) {
Tools::log()->error('aeat-file-invalid-length');
return;
}

$filename = 'modelo130_' . $year . '_' . DinModelo130Export::getPeriodNumber($this->period) . '.txt';
$filename = 'modelo130_'
. $year
. '_'
. DinModelo130Export::getPeriodNumber($this->period)
. '.txt';

$response->headers->set('Content-Type', 'text/plain; charset=ISO-8859-1');
$response->headers->set('Content-Disposition', 'attachment; filename="' . $filename . '"');
$response->setContent(mb_convert_encoding($content, 'ISO-8859-1', 'UTF-8'));
$response->send();
exit;
}
$response->headers->set(
'Content-Type',
'text/plain; charset=ISO-8859-1'
);

protected function addDeductibleSubaccount(): void
{
$this->activeTab = 'deductible-subaccount';
$response->headers->set(
'Content-Disposition',
'attachment; filename="' . $filename . '"'
);

if (false === $this->validateFormToken()) {
return;
}
$response->setContent(
mb_convert_encoding($content, 'ISO-8859-1', 'UTF-8')
);

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

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

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

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

$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;
}

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

protected function autocompleteSubaccount(): void
{
$this->setTemplate(false);

$list = [];
$term = $this->request->get('term');
$sql = 'SELECT DISTINCT codsubcuenta, descripcion FROM subcuentas WHERE codsubcuenta LIKE "' . $term . '%";';

foreach ($this->dataBase->select($sql) as $value) {
$list[] = [
'key' => Tools::fixHtml($value['codsubcuenta']),
'value' => Tools::fixHtml($value['descripcion'])
];
}

if (empty($list)) {
$list[] = ['key' => null, 'value' => Tools::lang()->trans('no-data')];
}

$this->response->setContent(json_encode($list));
$response->send();
exit;
}

protected function createAccountingEntry(): void
Expand All @@ -299,52 +229,17 @@ protected function createAccountingEntry(): void
$amount = (float)$this->request->request->get('amount');
$paymentMethodId = (int)$this->request->request->get('paymentMethod');

if (DinModelo130::generateEntries($idempresa, $codejercicio, $period, $date, $amount, $paymentMethodId)) {
if (
DinModelo130::generateEntries(
$idempresa,
$codejercicio,
$period,
$date,
$amount,
$paymentMethodId
)
) {
Tools::log()->notice('record-updated-correctly');
}
}

protected function deleteDeductibleSubaccount(): void
{
$this->activeTab = 'deductible-subaccount';

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

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

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

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

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

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

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

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

Tools::log()->notice('record-deleted-correctly');
}
}
}
3 changes: 0 additions & 3 deletions Data/Lang/ES/subcuentas_130.csv

This file was deleted.

28 changes: 0 additions & 28 deletions Init.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,33 +33,5 @@ public function uninstall(): void

public function update(): void
{
$this->cleanInvalidUsers();
}

private function cleanInvalidUsers(): void
{
// ver si existe la tabla subcuentas o usuario
$db = new DataBase();
if (false === $db->tableExists('subcuentas_130') ||
false === $db->tableExists('users')) {
return;
}

// consulta compatible con mysql y postgresql
// elimina el registro foráneo si no existe en la tabla original
$templateSql = "UPDATE subcuentas_130
SET REPLACE_COLUMN = NULL
WHERE REPLACE_COLUMN IS NOT NULL
AND NOT EXISTS (
SELECT 1
FROM users
WHERE users.nick = subcuentas_130.REPLACE_COLUMN
);";

foreach (['nick', 'last_nick'] as $column) {
// reemplazar la columna de user en la tabla subcuentas_130 y ejecutar
$sql = str_replace("REPLACE_COLUMN", $column, $templateSql);
$db->exec($sql);
}
}
}
Loading
Loading