2020-12-03 11:46:36 +01: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-12-03 11:46:36 +01:00
|
|
|
*
|
2021-06-16 08:58:16 +02:00
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
2020-12-03 11:46:36 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Invoice;
|
|
|
|
|
2022-10-29 03:14:25 +02:00
|
|
|
use App\Jobs\Inventory\AdjustProductInventory;
|
2020-12-03 11:46:36 +01:00
|
|
|
use App\Models\Invoice;
|
2022-10-28 01:37:58 +02:00
|
|
|
use App\Models\Paymentable;
|
2020-12-03 11:46:36 +01:00
|
|
|
use App\Services\AbstractService;
|
2021-05-20 23:58:46 +02:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2022-08-19 04:09:50 +02:00
|
|
|
use Illuminate\Support\Facades\DB;
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
class HandleRestore extends AbstractService
|
|
|
|
{
|
2021-05-20 23:58:46 +02:00
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-12-04 23:10:32 +01:00
|
|
|
private $payment_total = 0;
|
|
|
|
|
2022-08-19 04:48:58 +02:00
|
|
|
private $total_payments = 0;
|
|
|
|
|
|
|
|
private $adjustment_amount = 0;
|
|
|
|
|
2023-06-03 14:49:48 +02:00
|
|
|
public function __construct(private Invoice $invoice)
|
2020-12-03 11:46:36 +01:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2022-03-15 10:20:05 +01:00
|
|
|
$this->invoice->restore();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
|
|
|
if (! $this->invoice->is_deleted) {
|
2020-12-03 11:46:36 +01:00
|
|
|
return $this->invoice;
|
2020-12-05 14:24:21 +01:00
|
|
|
}
|
2020-12-03 11:46:36 +01:00
|
|
|
|
2022-11-30 05:36:30 +01:00
|
|
|
//cannot restore an invoice with a deleted payment
|
2020-12-05 14:24:21 +01:00
|
|
|
foreach ($this->invoice->payments as $payment) {
|
2023-02-16 02:36:09 +01:00
|
|
|
if (($this->invoice->paid_to_date == 0) && $payment->is_deleted) {
|
2023-07-16 09:42:34 +02:00
|
|
|
$this->invoice->delete();
|
2022-11-30 05:36:30 +01:00
|
|
|
return $this->invoice;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2020-12-03 11:46:36 +01:00
|
|
|
}
|
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
//adjust ledger balance
|
2021-01-21 05:05:05 +01:00
|
|
|
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Restored invoice {$this->invoice->number}")->save();
|
2020-12-03 11:46:36 +01:00
|
|
|
|
2022-08-19 04:48:58 +02:00
|
|
|
$this->invoice->client
|
|
|
|
->service()
|
2023-02-16 02:36:09 +01:00
|
|
|
->updateBalanceAndPaidToDate($this->invoice->balance, $this->invoice->paid_to_date)
|
2022-08-19 04:48:58 +02:00
|
|
|
->save();
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
$this->windBackInvoiceNumber();
|
|
|
|
|
2022-03-15 10:20:05 +01:00
|
|
|
$this->invoice->is_deleted = false;
|
|
|
|
$this->invoice->save();
|
2022-06-21 11:57:17 +02:00
|
|
|
|
2022-08-19 04:48:58 +02:00
|
|
|
$this->restorePaymentables()
|
|
|
|
->setAdjustmentAmount()
|
|
|
|
->adjustPayments();
|
|
|
|
|
2022-10-29 03:14:25 +02:00
|
|
|
if ($this->invoice->company->track_inventory) {
|
|
|
|
(new AdjustProductInventory($this->invoice->company, $this->invoice, []))->handleRestoredInvoice();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
return $this->invoice;
|
2020-12-03 11:46:36 +01:00
|
|
|
}
|
|
|
|
|
2022-08-19 04:48:58 +02:00
|
|
|
/* Touches all paymentables as deleted */
|
|
|
|
private function restorePaymentables()
|
|
|
|
{
|
|
|
|
$this->invoice->payments->each(function ($payment) {
|
2022-10-28 01:37:58 +02:00
|
|
|
Paymentable::query()
|
|
|
|
->withTrashed()
|
|
|
|
->where('payment_id', $payment->id)
|
2023-07-14 09:09:23 +02:00
|
|
|
// ->where('paymentable_type', '=', 'invoices')
|
|
|
|
// ->where('paymentable_id', $this->invoice->id)
|
2022-10-28 08:04:48 +02:00
|
|
|
->update(['deleted_at' => null]);
|
2022-08-19 04:48:58 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function setAdjustmentAmount()
|
|
|
|
{
|
|
|
|
foreach ($this->invoice->payments as $payment) {
|
|
|
|
$this->adjustment_amount += $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->sum(DB::raw('amount'));
|
|
|
|
|
|
|
|
$this->adjustment_amount += $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->sum(DB::raw('refunded'));
|
2023-07-14 09:09:23 +02:00
|
|
|
|
|
|
|
//14/07/2023 - do not include credits in the payment amount
|
|
|
|
$this->adjustment_amount -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'App\Models\Credit')
|
|
|
|
->sum(DB::raw('amount'));
|
|
|
|
|
2022-08-19 04:48:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->total_payments = $this->invoice->payments->sum('amount') - $this->invoice->payments->sum('refunded');
|
|
|
|
|
|
|
|
return $this;
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-08-19 04:48:58 +02:00
|
|
|
|
2022-08-19 04:09:50 +02:00
|
|
|
private function adjustPayments()
|
|
|
|
{
|
|
|
|
//if total payments = adjustment amount - that means we need to delete the payments as well.
|
|
|
|
|
|
|
|
if ($this->adjustment_amount == $this->total_payments) {
|
2022-08-19 04:48:58 +02:00
|
|
|
$this->invoice->payments()->update(['payments.deleted_at' => null, 'payments.is_deleted' => false]);
|
2023-02-16 02:36:09 +01:00
|
|
|
}
|
2022-08-19 04:09:50 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
//adjust payments down by the amount applied to the invoice payment.
|
2022-08-19 04:09:50 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$this->invoice->payments->fresh()->each(function ($payment) {
|
|
|
|
$payment_adjustment = $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->sum(DB::raw('amount'));
|
2022-08-19 04:09:50 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$payment_adjustment -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->sum(DB::raw('refunded'));
|
2022-08-19 04:09:50 +02:00
|
|
|
|
2023-07-14 09:09:23 +02:00
|
|
|
$payment_adjustment -= $payment->paymentables
|
|
|
|
->where('paymentable_type', '=', 'App\Models\Credit')
|
|
|
|
->sum(DB::raw('amount'));
|
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
$payment->amount += $payment_adjustment;
|
|
|
|
$payment->applied += $payment_adjustment;
|
|
|
|
$payment->is_deleted = false;
|
|
|
|
$payment->restore();
|
|
|
|
$payment->saveQuietly();
|
2023-07-14 09:09:23 +02:00
|
|
|
|
2023-02-16 02:36:09 +01:00
|
|
|
});
|
2022-11-30 04:39:45 +01:00
|
|
|
|
2022-08-19 04:09:50 +02:00
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-12-03 11:46:36 +01:00
|
|
|
private function windBackInvoiceNumber()
|
|
|
|
{
|
2022-06-21 11:57:17 +02:00
|
|
|
$findme = '_'.ctrans('texts.deleted');
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
$pos = strpos($this->invoice->number, $findme);
|
|
|
|
|
|
|
|
$new_invoice_number = substr($this->invoice->number, 0, $pos);
|
|
|
|
|
2020-12-21 11:46:46 +01:00
|
|
|
if (strlen($new_invoice_number) == 0) {
|
2020-12-17 21:11:31 +01:00
|
|
|
$new_invoice_number = null;
|
2020-12-21 11:46:46 +01:00
|
|
|
}
|
2021-05-20 23:58:46 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
try {
|
2021-05-20 23:58:46 +02:00
|
|
|
$exists = Invoice::where(['company_id' => $this->invoice->company_id, 'number' => $new_invoice_number])->exists();
|
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if ($exists) {
|
2021-05-20 23:58:46 +02:00
|
|
|
$this->invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice, $this->invoice->recurring_id);
|
2022-06-21 11:57:17 +02:00
|
|
|
} else {
|
|
|
|
$this->invoice->number = $new_invoice_number;
|
|
|
|
}
|
|
|
|
|
2021-10-14 08:54:38 +02:00
|
|
|
$this->invoice->saveQuietly();
|
2020-12-05 14:24:21 +01:00
|
|
|
} catch (\Exception $e) {
|
2022-06-21 11:57:17 +02:00
|
|
|
nlog('I could not wind back the invoice number');
|
2021-05-20 23:58:46 +02:00
|
|
|
|
2022-06-21 11:57:17 +02:00
|
|
|
if (Ninja::isHosted()) {
|
|
|
|
\Sentry\captureMessage('I could not wind back the invoice number');
|
2021-05-20 23:58:46 +02:00
|
|
|
app('sentry')->captureException($e);
|
|
|
|
}
|
2020-12-03 11:46:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|