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
99 changes: 14 additions & 85 deletions Core/Controller/ApiCreateFacturaRectificativaCliente.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@

namespace FacturaScripts\Core\Controller;

use FacturaScripts\Core\Lib\Calculator;
use FacturaScripts\Core\Params\RefundInvoiceParams;
use FacturaScripts\Core\Response;
use FacturaScripts\Core\Service\InvoiceManager;
use FacturaScripts\Core\Template\ApiController;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Model\FacturaCliente;
Expand Down Expand Up @@ -82,90 +83,18 @@ protected function newRefundAction(): ?FacturaCliente
}
}

// si no se especifican cantidades de las lineas,
// incluimos todas las lineas en la factura rectificativa.
if (empty($lines)) {
$lines = $invoiceLines;
}

$this->db()->beginTransaction();

if ($invoice->editable) {
foreach ($invoice->getAvailableStatus() as $status) {
if ($status->editable || !$status->activo) {
continue;
}

$invoice->idestado = $status->idestado;
if (false === $invoice->save()) {
$this->sendError('record-save-error', Response::HTTP_INTERNAL_SERVER_ERROR);
$this->db()->rollback();
return null;
}
}
}

$newRefund = new FacturaCliente();
$newRefund->loadFromData($invoice->toArray(), $invoice::dontCopyFields());
$newRefund->codigorect = $invoice->codigo;
$newRefund->codserie = $this->request->input('codserie') ?? $invoice->codserie;
$newRefund->idfacturarect = $invoice->idfactura;
$newRefund->nick = $this->request->input('nick');
$newRefund->observaciones = $this->request->input('observaciones');

$date = $this->request->input('fecha');
$hour = $this->request->input('hora');
if (false === $newRefund->setDate($date, $hour)) {
$this->sendError('error-set-date', Response::HTTP_BAD_REQUEST);
$this->db()->rollback();
return null;
}

if (false === $newRefund->save()) {
$this->sendError('record-save-error', Response::HTTP_INTERNAL_SERVER_ERROR);
$this->db()->rollback();
return null;
}

foreach ($lines as $line) {
$newLine = $newRefund->getNewLine($line->toArray());
$newLine->cantidad = 0 - (float)$this->request->input('refund_' . $line->id(), $line->cantidad);
$newLine->idlinearect = $line->idlinea;
if (false === $newLine->save()) {
$this->sendError('record-save-error', Response::HTTP_INTERNAL_SERVER_ERROR);
$this->db()->rollback();
return null;
}
}

$newLines = $newRefund->getLines();
$newRefund->idestado = $invoice->idestado;
if (false === Calculator::calculate($newRefund, $newLines, true)) {
$this->sendError('record-save-error', Response::HTTP_INTERNAL_SERVER_ERROR);
$this->db()->rollback();
return null;
}

// si la factura estaba pagada, marcamos los recibos de la nueva como pagados
if ($invoice->pagada) {
foreach ($newRefund->getReceipts() as $receipt) {
$receipt->pagado = true;
$receipt->save();
}
}

// asignamos el estado de la factura
$newRefund->idestado = $this->request->input('idestado');
if (false === $newRefund->save()) {
$this->sendError('record-save-error', Response::HTTP_INTERNAL_SERVER_ERROR);
$this->db()->rollback();
return null;
}

$this->db()->commit();
Tools::log()->notice('record-updated-correctly');

return $newRefund;
$params = new RefundInvoiceParams(
lines: $lines,
codserie: $this->request->input('codserie', ''),
fecha: $this->request->input('fecha'),
hora: $this->request->input('hora'),
observaciones: $this->request->input('observaciones', ''),
idestado: $this->request->input('idestado', ''),
nick: $this->request->input('nick', ''),
includeAllLinesIfEmpty: true
);

return InvoiceManager::createRefund($invoice, $params);
}

private function sendError(string $message, int $http_code): void
Expand Down
83 changes: 14 additions & 69 deletions Core/Controller/EditFacturaCliente.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\Lib\AjaxForms\SalesController;
use FacturaScripts\Core\Lib\Calculator;
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
use FacturaScripts\Core\Params\RefundInvoiceParams;
use FacturaScripts\Core\Service\InvoiceManager;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\Accounting\InvoiceToAccounting;
use FacturaScripts\Dinamic\Lib\ReceiptGenerator;
Expand Down Expand Up @@ -306,78 +307,22 @@ protected function newRefundAction(): bool
$lines[] = $line;
}
}
if (empty($lines)) {
Tools::log()->warning('no-selected-item');
return true;
}

$this->dataBase->beginTransaction();

if ($invoice->editable) {
foreach ($invoice->getAvailableStatus() as $status) {
if ($status->editable || !$status->activo) {
continue;
}

$invoice->idestado = $status->idestado;
if (false === $invoice->save()) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}
}
}

$newRefund = new FacturaCliente();
$newRefund->loadFromData($invoice->toArray(), $invoice::dontCopyFields());
$newRefund->codigorect = $invoice->codigo;
$newRefund->codserie = $this->request->input('codserie');
$newRefund->idfacturarect = $invoice->idfactura;
$newRefund->nick = $this->user->nick;
$newRefund->observaciones = $this->request->input('observaciones');
$newRefund->setDate($this->request->input('fecha'), date(Tools::HOUR_STYLE));
if (false === $newRefund->save()) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

foreach ($lines as $line) {
$newLine = $newRefund->getNewLine($line->toArray());
$newLine->cantidad = 0 - (float)$this->request->input('refund_' . $line->id(), '0');
$newLine->idlinearect = $line->idlinea;
if (false === $newLine->save()) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}
}

$newLines = $newRefund->getLines();
if (false === Calculator::calculate($newRefund, $newLines, true)) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

// si la factura estaba pagada, marcamos los recibos de la nueva como pagados
if ($invoice->pagada) {
foreach ($newRefund->getReceipts() as $receipt) {
$receipt->pagado = true;
$receipt->save();
}
}

// asignamos el estado de la factura
$newRefund->idestado = $this->request->input('idestado');
if (false === $newRefund->save()) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
$params = new RefundInvoiceParams(
lines: $lines,
codserie: $this->request->input('codserie', ''),
fecha: $this->request->input('fecha'),
observaciones: $this->request->input('observaciones', ''),
idestado: $this->request->input('idestado', ''),
nick: $this->user->nick,
includeAllLinesIfEmpty: false
);

$newRefund = InvoiceManager::createRefund($invoice, $params);
if ($newRefund === null) {
return true;
}

$this->dataBase->commit();
Tools::log()->notice('record-updated-correctly');
$this->redirect($newRefund->url() . '&action=save-ok');
return false;
}
Expand Down
45 changes: 45 additions & 0 deletions Core/Params/RefundInvoiceParams.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace FacturaScripts\Core\Params;

use FacturaScripts\Core\Model\LineaFacturaCliente;

class RefundInvoiceParams
{
/** @var LineaFacturaCliente[] */
public array $lines = [];

public string $codserie = '';

public string $fecha = '';

public ?string $hora = null;

public string $observaciones = '';

public string $idestado = '';

public string $nick = '';

public bool $includeAllLinesIfEmpty = false;

public function __construct(
array $lines = [],
string $codserie = '',
string $fecha = '',
?string $hora = null,
string $observaciones = '',
string $idestado = '',
string $nick = '',
bool $includeAllLinesIfEmpty = false
) {
$this->lines = $lines;
$this->codserie = $codserie;
$this->fecha = $fecha;
$this->hora = $hora;
$this->observaciones = $observaciones;
$this->idestado = $idestado;
$this->nick = $nick;
$this->includeAllLinesIfEmpty = $includeAllLinesIfEmpty;
}
}
Loading
Loading