2020-12-03 11:46:36 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
2021-01-03 22:54:54 +01:00
|
|
|
* @copyright Copyright (c) 2021. 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;
|
|
|
|
|
|
|
|
use App\Models\Invoice;
|
|
|
|
use App\Services\AbstractService;
|
2021-05-20 23:58:46 +02:00
|
|
|
use App\Utils\Ninja;
|
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
class HandleRestore extends AbstractService
|
|
|
|
{
|
2021-05-20 23:58:46 +02:00
|
|
|
use GeneratesCounter;
|
|
|
|
|
2020-12-03 11:46:36 +01:00
|
|
|
private $invoice;
|
|
|
|
|
2020-12-04 23:10:32 +01:00
|
|
|
private $payment_total = 0;
|
|
|
|
|
2020-12-03 11:46:36 +01:00
|
|
|
public function __construct(Invoice $invoice)
|
|
|
|
{
|
|
|
|
$this->invoice = $invoice;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run()
|
|
|
|
{
|
2020-12-05 14:24:21 +01: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
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
//determine whether we need to un-delete payments OR just modify the payment amount /applied balances.
|
|
|
|
|
|
|
|
foreach ($this->invoice->payments as $payment) {
|
2020-12-04 23:08:10 +01:00
|
|
|
//restore the payment record
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment->restore();
|
|
|
|
|
2020-12-04 23:08:10 +01:00
|
|
|
//determine the paymentable amount before paymentable restoration
|
|
|
|
$pre_restore_amount = $payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->sum(\DB::raw('amount'));
|
|
|
|
|
2021-07-31 11:59:04 +02:00
|
|
|
nlog("first pre restore amount = {$pre_restore_amount}");
|
|
|
|
|
|
|
|
$pre_restore_amount -= $payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->sum(\DB::raw('refunded'));
|
|
|
|
|
|
|
|
nlog("second pre restore amount = {$pre_restore_amount}");
|
|
|
|
|
|
|
|
|
2020-12-04 23:08:10 +01:00
|
|
|
//restore the paymentables
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->where('paymentable_id', $this->invoice->id)
|
|
|
|
->restore();
|
|
|
|
|
2020-12-04 23:08:10 +01:00
|
|
|
//determine the post restore paymentable amount (we need to increment the payment amount by the difference between pre and post)
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment_amount = $payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->sum(\DB::raw('amount'));
|
|
|
|
|
2021-07-31 11:59:04 +02:00
|
|
|
nlog("first payment_amount = {$payment_amount}");
|
|
|
|
|
|
|
|
$payment_amount -= $payment->paymentables()
|
|
|
|
->where('paymentable_type', '=', 'invoices')
|
|
|
|
->sum(\DB::raw('refunded'));
|
|
|
|
|
|
|
|
nlog("second payment_amount = {$payment_amount}");
|
|
|
|
|
|
|
|
nlog($payment->amount . " == " . $payment_amount);
|
2020-12-03 11:46:36 +01:00
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
if ($payment->amount == $payment_amount) {
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment->is_deleted = false;
|
|
|
|
$payment->save();
|
|
|
|
|
2020-12-04 23:10:32 +01:00
|
|
|
$this->payment_total += $payment_amount;
|
2020-12-05 14:24:21 +01:00
|
|
|
} else {
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment->is_deleted = false;
|
2020-12-04 23:08:10 +01:00
|
|
|
$payment->amount += ($payment_amount - $pre_restore_amount);
|
|
|
|
$payment->applied += ($payment_amount - $pre_restore_amount);
|
2020-12-03 11:46:36 +01:00
|
|
|
$payment->save();
|
2020-12-04 23:10:32 +01:00
|
|
|
|
|
|
|
$this->payment_total += ($payment_amount - $pre_restore_amount);
|
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
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
//adjust paid to dates
|
2020-12-04 23:10:32 +01:00
|
|
|
$this->invoice->client->service()->updatePaidToDate($this->payment_total)->save();
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
$this->invoice->client->service()->updateBalance($this->invoice->balance)->save();
|
|
|
|
|
2021-01-21 05:05:05 +01:00
|
|
|
// you only need to touch the ledger ONCE per transaction.
|
|
|
|
// $this->invoice->ledger()->updatePaymentBalance($this->payment_total*-1, "Restored payment for invoice {$this->invoice->number}")->save();
|
2020-12-03 11:46:36 +01:00
|
|
|
|
|
|
|
$this->windBackInvoiceNumber();
|
|
|
|
|
2020-12-05 14:24:21 +01:00
|
|
|
return $this->invoice;
|
2020-12-03 11:46:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private function windBackInvoiceNumber()
|
|
|
|
{
|
|
|
|
$findme = '_' . ctrans('texts.deleted');
|
|
|
|
|
|
|
|
$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
|
|
|
}
|
2020-12-17 21:11:31 +01:00
|
|
|
|
2020-12-03 11:46:36 +01:00
|
|
|
try {
|
2021-05-20 23:58:46 +02:00
|
|
|
|
|
|
|
$exists = Invoice::where(['company_id' => $this->invoice->company_id, 'number' => $new_invoice_number])->exists();
|
|
|
|
|
|
|
|
if($exists)
|
|
|
|
$this->invoice->number = $this->getNextInvoiceNumber($this->invoice->client, $this->invoice, $this->invoice->recurring_id);
|
|
|
|
else
|
2020-12-03 11:46:36 +01:00
|
|
|
$this->invoice->number = $new_invoice_number;
|
2021-05-20 23:58:46 +02:00
|
|
|
|
2021-10-14 08:54:38 +02:00
|
|
|
$this->invoice->saveQuietly();
|
2020-12-05 14:24:21 +01:00
|
|
|
} catch (\Exception $e) {
|
2021-05-20 23:58:46 +02:00
|
|
|
nlog("I could not wind back the invoice number");
|
|
|
|
|
|
|
|
if(Ninja::isHosted()){
|
|
|
|
\Sentry\captureMessage("I could not wind back the invoice number");
|
|
|
|
app('sentry')->captureException($e);
|
|
|
|
}
|
2020-12-03 11:46:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|