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
49 changes: 42 additions & 7 deletions Core/Controller/EditFacturaCliente.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace FacturaScripts\Core\Controller;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\DataSrc\FormasPago;
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\Accounting\InvoiceToAccounting;
Expand All @@ -30,6 +31,13 @@ public function getModelClassName(): string
return 'FacturaCliente';
}

public function paymentMethods(): array
{
return array_filter(FormasPago::all(), function ($method) {
return $method->activa;
});
}

public function getPageData(): array
{
$data = parent::getPageData();
Expand Down Expand Up @@ -361,28 +369,55 @@ protected function newRefundAction(): bool
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();
}
if (false === $this->updateRefundInvoicePayment(
$invoice,
$this->request->input('codpago_original', $invoice->codpago),
$this->request->input('pagada_original', false)
)) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

// asignamos el estado de la factura
$codpagoRectificativa = $this->request->input('codpago_rectificativa', $newRefund->codpago);
$newRefund->idestado = $this->request->input('idestado');

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

if (false === $this->updateRefundInvoicePayment($newRefund, $codpagoRectificativa, $this->request->input('pagada_rectificativa', false))) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

$this->dataBase->commit();
Tools::log()->notice('record-updated-correctly');
$this->redirect($newRefund->url() . '&action=save-ok');
return false;
}

private function updateRefundInvoicePayment(FacturaCliente $invoice, string $codpago, bool $paid): bool
{
if (false === $invoice->savePaymentMethod($codpago)) {
return false;
}

foreach ($invoice->getReceipts() as $receipt) {
$receipt->setPaymentMethod($codpago);
$receipt->pagado = $paid;
if (false === $receipt->save()) {
return false;
}
}

$invoice->reload();
return true;
}

/**
* Adds a warning message if the sum of the receipts is not equal
* to the total of the invoice.
Expand Down
49 changes: 42 additions & 7 deletions Core/Controller/EditFacturaProveedor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
namespace FacturaScripts\Core\Controller;

use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
use FacturaScripts\Core\DataSrc\FormasPago;
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
use FacturaScripts\Core\Tools;
use FacturaScripts\Dinamic\Lib\Accounting\InvoiceToAccounting;
Expand All @@ -31,6 +32,13 @@ public function getModelClassName(): string
return 'FacturaProveedor';
}

public function paymentMethods(): array
{
return array_filter(FormasPago::all(), function ($method) {
return $method->activa;
});
}

public function getPageData(): array
{
$data = parent::getPageData();
Expand Down Expand Up @@ -364,28 +372,55 @@ protected function newRefundAction(): bool
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();
}
if (false === $this->updateRefundInvoicePayment(
$invoice,
$this->request->input('codpago_original', $invoice->codpago),
$this->request->input('pagada_original', false)
)) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

// asignamos el estado de la factura
$codpagoRectificativa = $this->request->input('codpago_rectificativa', $newRefund->codpago);
$newRefund->idestado = $this->request->input('idestado');

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

if (false === $this->updateRefundInvoicePayment($newRefund, $codpagoRectificativa, $this->request->input('pagada_rectificativa', false))) {
Tools::log()->error('record-save-error');
$this->dataBase->rollback();
return true;
}

$this->dataBase->commit();
Tools::log()->notice('record-updated-correctly');
$this->redirect($newRefund->url() . '&action=save-ok');
return false;
}

private function updateRefundInvoicePayment(FacturaProveedor $invoice, string $codpago, bool $paid): bool
{
if (false === $invoice->savePaymentMethod($codpago)) {
return false;
}

foreach ($invoice->getReceipts() as $receipt) {
$receipt->setPaymentMethod($codpago);
$receipt->pagado = $paid;
if (false === $receipt->save()) {
return false;
}
}

$invoice->reload();
return true;
}

/**
* Adds a warning message if the sum of the receipts is not equal
* to the total of the invoice.
Expand Down
14 changes: 14 additions & 0 deletions Core/Model/FacturaCliente.php
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,20 @@ public static function tableName(): string
return 'facturascli';
}

public function savePaymentMethod(string $codpago): bool
{
$formaPago = new FormaPago();
if (false === $formaPago->load($codpago)) {
return false;
}

if (false === $this->update(['codpago' => $codpago])) {
return false;
}

return $this->reload();
}

protected function saveInsert(): bool
{
return $this->testDate() && parent::saveInsert();
Expand Down
14 changes: 14 additions & 0 deletions Core/Model/FacturaProveedor.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,20 @@ public static function tableName(): string
return 'facturasprov';
}

public function savePaymentMethod(string $codpago): bool
{
$formaPago = new FormaPago();
if (false === $formaPago->load($codpago)) {
return false;
}

if (false === $this->update(['codpago' => $codpago])) {
return false;
}

return $this->reload();
}

protected function testDate(): bool
{
return true;
Expand Down
4 changes: 3 additions & 1 deletion Core/Translation/es_ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,8 @@
"mailer": "Controlador de correo",
"main-address": "Dirección principal",
"make-invoice": "Hacer factura",
"mark-original-invoice-as-paid": "Marcar la original como pagada",
"mark-rectifying-invoice-as-paid": "Marcar la rectificativa como pagada",
"manage": "Administrar",
"manage-installation": "Administrar",
"manual": "Manual",
Expand Down Expand Up @@ -2061,4 +2063,4 @@
"zip-error-wrong-structure": "La estructura del zip es incorrecta. Debe contener la carpeta del plugin y solamente un plugin.",
"zone": "Zona",
"zones": "Zonas"
}
}
28 changes: 28 additions & 0 deletions Core/View/Tab/RefundFacturaCliente.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@
<div class="card-body">
<p>{{ trans('rectified-invoice-p') }}</p>
<div class="row g-2 align-items-end">
<div class="col bg-light border rounded">
<div class="form-check mb-2">
<input type="checkbox" name="pagada_original" id="pagada_original" value="true" class="form-check-input"{{ firstView.model.pagada ? ' checked' : '' }}>
<label class="form-check-label" for="pagada_original">{{ trans('mark-original-invoice-as-paid') }}</label>
</div>
<div class="mb-3">
{{ trans('payment-method') }}
<select name="codpago_original" class="form-select">
{% for paymethod in fsc.paymentMethods() %}
<option value="{{ paymethod.codpago }}"{{ paymethod.codpago == firstView.model.codpago ? ' selected' : '' }}>{{ paymethod.descripcion }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col bg-light border rounded">
<div class="form-check mb-2">
<input type="checkbox" name="pagada_rectificativa" id="pagada_rectificativa" value="true" class="form-check-input">
<label class="form-check-label" for="pagada_rectificativa">{{ trans('mark-rectifying-invoice-as-paid') }}</label>
</div>
<div class="mb-3">
{{ trans('payment-method') }}
<select name="codpago_rectificativa" class="form-select">
{% for paymethod in fsc.paymentMethods() %}
<option value="{{ paymethod.codpago }}"{{ paymethod.codpago == firstView.model.codpago ? ' selected' : '' }}>{{ paymethod.descripcion }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm-6 col-md-4 col-lg">
<div class="mb-3">
{{ trans('serie') }}
Expand Down
28 changes: 28 additions & 0 deletions Core/View/Tab/RefundFacturaProveedor.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,34 @@
<div class="card-body">
<p>{{ trans('rectified-invoice-p') }}</p>
<div class="row g-2 align-items-end">
<div class="col bg-light border rounded">
<div class="form-check mb-2">
<input type="checkbox" name="pagada_original" id="pagada_original" value="true" class="form-check-input"{{ firstView.model.pagada ? ' checked' : '' }}>
<label class="form-check-label" for="pagada_original">{{ trans('mark-original-invoice-as-paid') }}</label>
</div>
<div class="mb-3">
{{ trans('payment-method') }}
<select name="codpago_original" class="form-select">
{% for paymethod in fsc.paymentMethods() %}
<option value="{{ paymethod.codpago }}"{{ paymethod.codpago == firstView.model.codpago ? ' selected' : '' }}>{{ paymethod.descripcion }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col bg-light border rounded">
<div class="form-check mb-2">
<input type="checkbox" name="pagada_rectificativa" id="pagada_rectificativa" value="true" class="form-check-input">
<label class="form-check-label" for="pagada_rectificativa">{{ trans('mark-rectifying-invoice-as-paid') }}</label>
</div>
<div class="mb-3">
{{ trans('payment-method') }}
<select name="codpago_rectificativa" class="form-select">
{% for paymethod in fsc.paymentMethods() %}
<option value="{{ paymethod.codpago }}"{{ paymethod.codpago == firstView.model.codpago ? ' selected' : '' }}>{{ paymethod.descripcion }}</option>
{% endfor %}
</select>
</div>
</div>
<div class="col-sm-6 col-md-3 col-lg">
<div class="mb-3">
{{ trans('serie') }}
Expand Down
Loading
Loading