mirror of
https://github.com/invoiceninja/invoiceninja.git
synced 2024-11-10 13:12:50 +01:00
Working on invoice delete restore refactor
This commit is contained in:
parent
d30ef031a2
commit
fceaab9e40
@ -102,7 +102,6 @@ class InvoiceController extends BaseController
|
||||
* response=422,
|
||||
* description="Validation error",
|
||||
* @OA\JsonContent(ref="#/components/schemas/ValidationError"),
|
||||
|
||||
* ),
|
||||
* @OA\Response(
|
||||
* response="default",
|
||||
@ -691,7 +690,7 @@ class InvoiceController extends BaseController
|
||||
break;
|
||||
case 'delete':
|
||||
//need to make sure the invoice is cancelled first!!
|
||||
$invoice->service()->handleCancellation()->save();
|
||||
//$invoice->service()->handleCancellation()->save();
|
||||
|
||||
$this->invoice_repo->delete($invoice);
|
||||
|
||||
|
@ -94,7 +94,7 @@ class InvoiceRepository extends BaseRepository
|
||||
}
|
||||
|
||||
// reversed delete invoice actions
|
||||
$invoice = $invoice->service()->handeRestore()->save()
|
||||
$invoice = $invoice->service()->handleRestore()->save();
|
||||
|
||||
parent::restore($invoice);
|
||||
|
||||
|
@ -1,42 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Services\Invoice;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Services\AbstractService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class HandleInvoiceRestore extends AbstractService
|
||||
{
|
||||
|
||||
private $invoice;
|
||||
|
||||
public function __construct(Invoice $invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
|
||||
//determine whether we need to un-delete payments OR just modify the payment amount /applied balances.
|
||||
|
||||
//adjust ledger balance
|
||||
|
||||
//adjust paid to dates
|
||||
|
||||
//restore paymentables
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
111
app/Services/Invoice/HandleRestore.php
Normal file
111
app/Services/Invoice/HandleRestore.php
Normal file
@ -0,0 +1,111 @@
|
||||
<?php
|
||||
/**
|
||||
* Invoice Ninja (https://invoiceninja.com).
|
||||
*
|
||||
* @link https://github.com/invoiceninja/invoiceninja source repository
|
||||
*
|
||||
* @copyright Copyright (c) 2020. Invoice Ninja LLC (https://invoiceninja.com)
|
||||
*
|
||||
* @license https://opensource.org/licenses/AAL
|
||||
*/
|
||||
|
||||
namespace App\Services\Invoice;
|
||||
|
||||
use App\Models\Invoice;
|
||||
use App\Services\AbstractService;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class HandleRestore extends AbstractService
|
||||
{
|
||||
|
||||
private $invoice;
|
||||
|
||||
private $payment_total = 0;
|
||||
|
||||
public function __construct(Invoice $invoice)
|
||||
{
|
||||
$this->invoice = $invoice;
|
||||
}
|
||||
|
||||
public function run()
|
||||
{
|
||||
|
||||
if(!$this->invoice->is_deleted)
|
||||
return $this->invoice;
|
||||
|
||||
//determine whether we need to un-delete payments OR just modify the payment amount /applied balances.
|
||||
|
||||
foreach($this->invoice->payments as $payment)
|
||||
{
|
||||
|
||||
$payment->restore();
|
||||
|
||||
$payment->paymentables()
|
||||
->where('paymentable_type', '=', 'invoices')
|
||||
->where('paymentable_id', $this->invoice->id)
|
||||
->restore();
|
||||
|
||||
$payment_amount = $payment->paymentables()
|
||||
->where('paymentable_type', '=', 'invoices')
|
||||
->where('paymentable_id', $this->invoice->id)
|
||||
->sum(\DB::raw('amount'));
|
||||
|
||||
info($payment->amount . " == " . $payment_amount);
|
||||
|
||||
if($payment->amount == $payment_amount) {
|
||||
|
||||
$payment->is_deleted = false;
|
||||
$payment->save();
|
||||
|
||||
}
|
||||
else {
|
||||
|
||||
$payment->is_deleted = false;
|
||||
$payment->amount += $this->payment_total;
|
||||
$payment->applied += $this->payment_total;
|
||||
$payment->save();
|
||||
}
|
||||
|
||||
$this->payment_total += $payment_amount;
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
//adjust ledger balance
|
||||
$this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, 'Restored invoice {$this->invoice->number}')->save();
|
||||
|
||||
//adjust paid to dates
|
||||
$this->invoice->client->service()->updatePaidToDate($this->payment_total)->save();
|
||||
|
||||
$this->invoice->client->service()->updateBalance($this->invoice->balance)->save();
|
||||
|
||||
$this->invoice->ledger()->updatePaymentBalance($this->payment_total, 'Restored payment for invoice {$this->invoice->number}')->save();
|
||||
|
||||
$this->windBackInvoiceNumber();
|
||||
|
||||
return $this->invoice;
|
||||
}
|
||||
|
||||
|
||||
private function windBackInvoiceNumber()
|
||||
{
|
||||
|
||||
$findme = '_' . ctrans('texts.deleted');
|
||||
|
||||
$pos = strpos($this->invoice->number, $findme);
|
||||
|
||||
$new_invoice_number = substr($this->invoice->number, 0, $pos);
|
||||
|
||||
try {
|
||||
$this->invoice->number = $new_invoice_number;
|
||||
$this->invoice->save();
|
||||
}
|
||||
catch(\Exception $e){
|
||||
info("I could not wind back the invoice number");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -164,9 +164,9 @@ class InvoiceService
|
||||
return $this;
|
||||
}
|
||||
|
||||
public function handeRestore()
|
||||
public function handleRestore()
|
||||
{
|
||||
$this->invoice = (new HandleInvoiceRestore($this->invoice))->run();
|
||||
$this->invoice = (new HandleRestore($this->invoice))->run();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
@ -44,6 +44,7 @@ class MarkInvoiceDeleted extends AbstractService
|
||||
->deletePaymentables()
|
||||
->adjustPayments()
|
||||
->adjustPaidToDate()
|
||||
->adjustBalance()
|
||||
->adjustLedger();
|
||||
|
||||
return $this->invoice;
|
||||
@ -63,6 +64,13 @@ class MarkInvoiceDeleted extends AbstractService
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function adjustBalance()
|
||||
{
|
||||
$this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save();
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
private function adjustPayments()
|
||||
{
|
||||
//if total payments = adjustment amount - that means we need to delete the payments as well.
|
||||
|
Loading…
Reference in New Issue
Block a user