2020-09-23 12:52:54 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2023-01-28 23:21:40 +01:00
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
2020-09-23 12:52:54 +02:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-09-23 12:52:54 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2023-10-26 04:57:44 +02:00
|
|
|
use App\Jobs\Inventory\AdjustProductInventory;
|
2020-09-23 12:52:54 +02:00
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
2023-07-14 09:09:23 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2020-09-23 12:52:54 +02:00
|
|
|
|
|
|
|
class MarkInvoiceDeleted extends AbstractService
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-12-02 12:01:50 +01:00
|
|
|
private $adjustment_amount = 0;
|
|
|
|
|
2020-12-03 04:11:24 +01:00
|
|
|
private $total_payments = 0;
|
|
|
|
|
2021-08-29 13:03:00 +02:00
|
|
|
private $balance_adjustment = 0;
|
|
|
|
|
2023-06-03 14:49:48 +02:00
|
|
|
public function __construct(public Invoice $invoice)
|
2020-09-23 12:52:54 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2020-12-05 14:24:21 +01:00
|
|
|
if ($this->invoice->is_deleted) {
|
2020-12-02 12:01:50 +01:00
|
|
|
return $this->invoice;
|
2020-12-05 14:24:21 +01:00
|
|
|
}
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-10-29 03:14:25 +02:00
|
|
|
if ($this->invoice->company->track_inventory) {
|
|
|
|
(new AdjustProductInventory($this->invoice->company, $this->invoice, []))->handleDeletedInvoice();
|
|
|
|
}
|
|
|
|
|
2020-12-02 12:01:50 +01:00
|
|
|
$this->cleanup()
|
|
|
|
->setAdjustmentAmount()
|
2020-12-03 04:11:24 +01:00
|
|
|
->deletePaymentables()
|
|
|
|
->adjustPayments()
|
2022-09-06 12:51:42 +02:00
|
|
|
->adjustPaidToDateAndBalance()
|
2020-12-03 04:11:24 +01:00
|
|
|
->adjustLedger();
|
2020-12-03 04:56:00 +01:00
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
return $this->invoice;
|
2020-12-03 04:11:24 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private function adjustLedger()
|
|
|
|
{
|
2021-08-07 07:25:22 +02:00
|
|
|
// $this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1, 'Invoice Deleted - reducing ledger balance'); //reduces the payment balance by payment totals
|
2021-08-29 13:03:00 +02:00
|
|
|
$this->invoice->ledger()->updatePaymentBalance($this->balance_adjustment * -1, 'Invoice Deleted - reducing ledger balance'); //reduces the payment balance by payment totals
|
2020-12-03 04:56:00 +01:00
|
|
|
|
2020-12-03 04:11:24 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-09-06 12:51:42 +02:00
|
|
|
private function adjustPaidToDateAndBalance()
|
2020-12-03 04:11:24 +01:00
|
|
|
{
|
2022-09-06 12:51:42 +02:00
|
|
|
// 06-09-2022
|
|
|
|
$this->invoice
|
|
|
|
->client
|
|
|
|
->service()
|
|
|
|
->updateBalanceAndPaidToDate($this->balance_adjustment * -1, $this->adjustment_amount * -1)
|
|
|
|
->save(); //reduces the paid to date by the payment totals
|
2020-12-03 04:11:24 +01:00
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
return $this;
|
2020-12-02 12:01:50 +01:00
|
|
|
}
|
|
|
|
|
2021-01-21 05:05:05 +01:00
|
|
|
/* Adjust the payment amounts */
|
2020-12-03 04:11:24 +01:00
|
|
|
private function adjustPayments()
|
|
|
|
{
|
|
|
|
//if total payments = adjustment amount - that means we need to delete the payments as well.
|
2023-02-16 02:36:09 +01:00
|
|
|
if ($this->adjustment_amount == $this->total_payments) {
|
2020-12-03 04:56:00 +01:00
|
|
|
$this->invoice->payments()->update(['payments.deleted_at' => now(), 'payments.is_deleted' => true]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-11-30 04:39:45 +01:00
|
|
|
|
2021-01-21 05:05:05 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//adjust payments down by the amount applied to the invoice payment.
|
|
|
|
$this->invoice->payments->each(function ($payment) {
|
|
|
|
$payment_adjustment = $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
2023-09-04 02:27:28 +02:00
|
|
|
->sum('amount');
|
2020-12-03 04:11:24 +01:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$payment_adjustment -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
2023-09-04 02:27:28 +02:00
|
|
|
->sum('refunded');
|
2021-07-31 11:59:04 +02:00
|
|
|
|
2023-07-14 09:09:23 +02:00
|
|
|
//14-07-2023 - Do not include credits in the payment adjustment.
|
|
|
|
$payment_adjustment -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'App\Models\Credit')
|
2023-09-04 02:27:28 +02:00
|
|
|
->sum('amount');
|
2023-07-14 09:09:23 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$payment->amount -= $payment_adjustment;
|
|
|
|
$payment->applied -= $payment_adjustment;
|
|
|
|
$payment->save();
|
|
|
|
});
|
2022-11-30 04:39:45 +01:00
|
|
|
|
2020-12-03 04:11:24 +01:00
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-12-02 12:01:50 +01:00
|
|
|
|
2021-01-21 05:05:05 +01:00
|
|
|
/**
|
|
|
|
* Set the values of two variables
|
|
|
|
*
|
|
|
|
* $this->adjustment_amount - sum of the invoice paymentables
|
|
|
|
* $this->total_payments - sum of the invoice payments
|
|
|
|
*/
|
2020-12-02 12:01:50 +01:00
|
|
|
private function setAdjustmentAmount()
|
|
|
|
{
|
2020-12-05 14:24:21 +01:00
|
|
|
foreach ($this->invoice->payments as $payment) {
|
2020-12-02 12:01:50 +01:00
|
|
|
$this->adjustment_amount += $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
2023-09-04 02:27:28 +02:00
|
|
|
->sum('amount');
|
2021-07-31 11:59:04 +02:00
|
|
|
|
|
|
|
$this->adjustment_amount -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
2023-09-04 02:27:28 +02:00
|
|
|
->sum('refunded');
|
2020-12-02 12:01:50 +01:00
|
|
|
}
|
|
|
|
|
2021-08-29 13:03:00 +02:00
|
|
|
$this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded');
|
|
|
|
|
|
|
|
$this->balance_adjustment = $this->invoice->balance;
|
2020-12-03 04:11:24 +01:00
|
|
|
|
2020-12-02 12:01:50 +01:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
/*
|
2021-01-21 05:05:05 +01:00
|
|
|
*
|
|
|
|
* This sets the invoice number to _deleted
|
|
|
|
* and also removes the links to existing entities
|
2022-06-21 11:57:17 +02:00
|
|
|
*
|
2021-01-21 05:05:05 +01:00
|
|
|
*/
|
2020-12-02 12:01:50 +01:00
|
|
|
private function cleanup()
|
|
|
|
{
|
2020-11-25 15:19:52 +01:00
|
|
|
$check = false;
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
$x = 0;
|
2020-09-23 12:52:54 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
do {
|
|
|
|
$number = $this->calcNumber($x);
|
|
|
|
$check = $this->checkNumberAvailable(Invoice::class, $this->invoice, $number);
|
|
|
|
$x++;
|
2022-06-21 11:57:17 +02:00
|
|
|
} while (! $check);
|
2020-09-23 12:52:54 +02:00
|
|
|
|
|
|
|
$this->invoice->number = $number;
|
|
|
|
|
2020-11-01 08:53:43 +01:00
|
|
|
//wipe references to invoices from related entities.
|
|
|
|
$this->invoice->tasks()->update(['invoice_id' => null]);
|
|
|
|
$this->invoice->expenses()->update(['invoice_id' => null]);
|
|
|
|
|
2020-12-02 12:01:50 +01:00
|
|
|
return $this;
|
2020-09-23 12:52:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function calcNumber($x)
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($x == 0) {
|
|
|
|
$number = $this->invoice->number.'_'.ctrans('texts.deleted');
|
2020-11-25 15:19:52 +01:00
|
|
|
} else {
|
2022-06-21 11:57:17 +02:00
|
|
|
$number = $this->invoice->number.'_'.ctrans('texts.deleted').'_'.$x;
|
2020-11-25 15:19:52 +01:00
|
|
|
}
|
2020-09-23 12:52:54 +02:00
|
|
|
|
2020-11-25 15:19:52 +01:00
|
|
|
return $number;
|
2020-09-23 12:52:54 +02:00
|
|
|
}
|
2020-12-02 12:01:50 +01:00
|
|
|
|
2021-01-21 05:05:05 +01:00
|
|
|
/* Touches all paymentables as deleted */
|
2020-12-02 12:01:50 +01:00
|
|
|
private function deletePaymentables()
|
|
|
|
{
|
2020-12-05 14:24:21 +01:00
|
|
|
$this->invoice->payments->each(function ($payment) {
|
2020-12-02 12:01:50 +01:00
|
|
|
$payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->update(['deleted_at' => now()]);
|
|
|
|
});
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
2020-09-23 12:52:54 +02:00
|
|
|
}
|