1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-11 05:32:39 +01:00
invoiceninja/app/Services/Invoice/MarkInvoiceDeleted.php

160 lines
4.4 KiB
PHP
Raw Normal View History

<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. 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 App\Utils\Traits\GeneratesCounter;
2020-12-03 04:56:00 +01:00
use Illuminate\Support\Facades\DB;
class MarkInvoiceDeleted extends AbstractService
{
use GeneratesCounter;
private $invoice;
2020-12-02 12:01:50 +01:00
private $adjustment_amount = 0;
2020-12-03 04:11:24 +01:00
private $total_payments = 0;
public function __construct(Invoice $invoice)
{
$this->invoice = $invoice;
}
public function run()
{
if ($this->invoice->is_deleted) {
2020-12-02 12:01:50 +01:00
return $this->invoice;
}
2020-12-02 12:01:50 +01:00
// if(in_array($this->invoice->status_id, ['currencies', 'industries', 'languages', 'countries', 'banks']))
// return $this->
$this->cleanup()
->setAdjustmentAmount()
2020-12-03 04:11:24 +01:00
->deletePaymentables()
->adjustPayments()
->adjustPaidToDate()
->adjustBalance()
2020-12-03 04:11:24 +01:00
->adjustLedger();
2020-12-03 04:56:00 +01:00
return $this->invoice;
2020-12-03 04:11:24 +01:00
}
private function adjustLedger()
{
$this->invoice->ledger()->updatePaymentBalance($this->adjustment_amount * -1);
2020-12-03 04:56:00 +01:00
2020-12-03 04:11:24 +01:00
return $this;
}
private function adjustPaidToDate()
{
$this->invoice->client->service()->updatePaidToDate($this->adjustment_amount * -1)->save();
return $this;
2020-12-02 12:01:50 +01:00
}
private function adjustBalance()
{
$this->invoice->client->service()->updateBalance($this->invoice->balance * -1)->save();
return $this;
}
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.
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]);
} else {
2020-12-03 04:11:24 +01:00
//adjust payments down by the amount applied to the invoice payment.
$this->invoice->payments->each(function ($payment) {
2020-12-03 04:11:24 +01:00
$payment_adjustment = $payment->paymentables
->where('paymentable_type', '=', 'invoices')
->where('paymentable_id', $this->invoice->id)
->sum(DB::raw('amount'));
$payment->amount -= $payment_adjustment;
$payment->applied -= $payment_adjustment;
$payment->save();
});
}
return $this;
}
2020-12-02 12:01:50 +01:00
private function setAdjustmentAmount()
{
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)
->sum(DB::raw('amount'));
}
2020-12-03 04:11:24 +01:00
$this->total_payments = $this->invoice->payments->sum('amount');
2020-12-02 12:01:50 +01:00
return $this;
}
private function cleanup()
{
2020-11-25 15:19:52 +01:00
$check = false;
2020-12-02 12:01:50 +01:00
2020-11-25 15:19:52 +01:00
$x=0;
2020-11-25 15:19:52 +01:00
do {
$number = $this->calcNumber($x);
$check = $this->checkNumberAvailable(Invoice::class, $this->invoice, $number);
$x++;
} while (!$check);
$this->invoice->number = $number;
//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;
}
private function calcNumber($x)
{
2020-11-25 15:19:52 +01:00
if ($x==0) {
$number = $this->invoice->number . '_' . ctrans('texts.deleted');
} else {
$number = $this->invoice->number . '_' . ctrans('texts.deleted') . '_'. $x;
}
2020-11-25 15:19:52 +01:00
return $number;
}
2020-12-02 12:01:50 +01:00
private function deletePaymentables()
{
$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;
}
}