1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-22 01:11:34 +02:00
invoiceninja/app/Services/Payment/DeletePayment.php

203 lines
5.9 KiB
PHP
Raw Normal View History

2020-06-28 00:24:08 +02:00
<?php
2020-07-01 03:06:40 +02:00
/**
* Invoice Ninja (https://invoiceninja.com).
2020-07-01 03:06:40 +02:00
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2022-04-27 05:20:41 +02:00
* @copyright Copyright (c) 2022. Invoice Ninja LLC (https://invoiceninja.com)
2020-07-01 03:06:40 +02:00
*
2021-06-16 08:58:16 +02:00
* @license https://www.elastic.co/licensing/elastic-license
2020-07-01 03:06:40 +02:00
*/
2020-06-28 00:24:08 +02:00
namespace App\Services\Payment;
2022-03-10 01:32:04 +01:00
use App\Jobs\Ninja\TransactionLog;
2020-06-28 00:24:08 +02:00
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
2022-03-10 01:32:04 +01:00
use App\Models\TransactionEvent;
2020-06-28 00:24:08 +02:00
use App\Repositories\ActivityRepository;
class DeletePayment
{
public $payment;
private $activity_repository;
public function __construct($payment)
{
$this->payment = $payment;
$this->activity_repository = new ActivityRepository();
}
public function run()
{
2020-11-25 15:19:52 +01:00
if ($this->payment->is_deleted) {
2020-11-23 13:55:04 +01:00
return $this->payment;
2020-11-25 15:19:52 +01:00
}
2020-11-23 13:55:04 +01:00
2020-07-06 13:22:36 +02:00
return $this->setStatus(Payment::STATUS_CANCELLED) //sets status of payment
2020-06-28 00:24:08 +02:00
->updateCreditables() //return the credits first
->adjustInvoices()
2020-06-28 05:05:58 +02:00
->updateClient()
2020-11-15 22:23:20 +01:00
->deletePaymentables()
2020-11-23 13:55:04 +01:00
->cleanupPayment()
2020-06-28 00:24:08 +02:00
->save();
}
2020-11-23 13:55:04 +01:00
private function cleanupPayment()
{
$this->payment->is_deleted = true;
$this->payment->delete();
return $this;
}
2020-11-15 22:23:20 +01:00
private function deletePaymentables()
{
$this->payment->paymentables()->update(['deleted_at' => now()]);
return $this;
}
2020-06-28 05:05:58 +02:00
private function updateClient()
{
2021-02-11 23:01:39 +01:00
//$this->payment->client->service()->updatePaidToDate(-1 * $this->payment->amount)->save();
2020-06-28 05:05:58 +02:00
return $this;
}
private function adjustInvoices()
{
if ($this->payment->invoices()->exists()) {
$this->payment->invoices()->each(function ($paymentable_invoice) {
$net_deletable = $paymentable_invoice->pivot->amount - $paymentable_invoice->pivot->refunded;
2022-03-09 03:49:31 +01:00
$client = $this->payment->client->fresh();
2021-09-04 07:51:31 +02:00
nlog("net deletable amount - refunded = {$net_deletable}");
if(!$paymentable_invoice->is_deleted)
{
2022-03-14 10:52:38 +01:00
$paymentable_invoice->restore();
$paymentable_invoice->service()
->updateBalance($net_deletable)
->updatePaidToDate($net_deletable * -1)
->save();
$paymentable_invoice->ledger()
->updateInvoiceBalance($net_deletable, "Adjusting invoice {$paymentable_invoice->number} due to deletion of Payment {$this->payment->number}")
->save();
2022-03-09 03:49:31 +01:00
$client = $client->service()
->updateBalance($net_deletable)
->save();
if ($paymentable_invoice->balance == $paymentable_invoice->amount) {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_SENT)->save();
} else {
$paymentable_invoice->service()->setStatus(Invoice::STATUS_PARTIAL)->save();
}
2022-03-10 01:32:04 +01:00
}
else {
2022-03-14 10:52:38 +01:00
$paymentable_invoice->restore();
//If the invoice is deleted we only update the meta data on the invoice
//and reduce the clients paid to date
2021-09-06 07:08:41 +02:00
$paymentable_invoice->service()
->updatePaidToDate($net_deletable * -1)
->save();
}
2020-06-28 05:05:58 +02:00
2022-03-10 01:32:04 +01:00
$transaction = [
'invoice' => $paymentable_invoice->transaction_event(),
'payment' => $this->payment->transaction_event(),
'client' => $client->transaction_event(),
'credit' => [],
'metadata' => [],
];
TransactionLog::dispatch(TransactionEvent::PAYMENT_DELETED, $transaction, $paymentable_invoice->company->db);
2020-06-28 05:05:58 +02:00
});
}
2022-03-16 03:06:25 +01:00
$client = $this->payment->client->fresh();
$client
2022-03-10 01:32:04 +01:00
->service()
->updatePaidToDate(($this->payment->amount - $this->payment->refunded)*-1)
->save();
2021-10-26 20:01:10 +02:00
2022-03-10 01:32:04 +01:00
$transaction = [
'invoice' => [],
'payment' => [],
2022-03-16 03:06:25 +01:00
'client' => $client->transaction_event(),
2022-03-10 01:32:04 +01:00
'credit' => [],
'metadata' => [],
];
TransactionLog::dispatch(TransactionEvent::CLIENT_STATUS, $transaction, $this->payment->company->db);
2020-06-28 05:05:58 +02:00
2022-03-10 01:32:04 +01:00
2020-06-28 05:05:58 +02:00
return $this;
}
private function updateCreditables()
{
if ($this->payment->credits()->exists()) {
$this->payment->credits()->each(function ($paymentable_credit) {
2021-11-22 11:09:28 +01:00
$multiplier = 1;
2022-03-16 03:06:25 +01:00
if($paymentable_credit->pivot->amount < 0)
$multiplier = -1;
2021-11-22 11:09:28 +01:00
$paymentable_credit->service()
2022-03-01 11:25:18 +01:00
->updateBalance($paymentable_credit->pivot->amount*$multiplier*-1)
->updatePaidToDate($paymentable_credit->pivot->amount*$multiplier)
->setStatus(Credit::STATUS_SENT)
->save();
2022-03-01 11:25:18 +01:00
2022-03-16 03:06:25 +01:00
$client = $this->payment->client->fresh();
$client
->service()
->updatePaidToDate(($paymentable_credit->pivot->amount)*-1)
->save();
2020-06-28 05:05:58 +02:00
});
}
return $this;
}
private function setStatus($status)
{
2020-07-06 13:22:36 +02:00
$this->payment->status_id = Payment::STATUS_CANCELLED;
2020-06-28 05:05:58 +02:00
return $this;
}
2020-06-28 00:24:08 +02:00
/**
* Saves the payment.
*
2020-06-28 00:24:08 +02:00
* @return Payment $payment
*/
private function save()
{
$this->payment->save();
return $this->payment;
}
}