2020-01-29 03:03:47 +01:00
|
|
|
<?php
|
2020-03-18 10:40:15 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
2020-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @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-03-18 10:40:15 +01:00
|
|
|
*
|
|
|
|
* @license https://opensource.org/licenses/AAL
|
|
|
|
*/
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
namespace App\Utils\Traits\Payment;
|
|
|
|
|
2020-05-14 03:04:23 +02:00
|
|
|
use App\Exceptions\PaymentRefundFailed;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
|
|
|
use App\Models\Activity;
|
2020-05-14 03:04:23 +02:00
|
|
|
use App\Models\CompanyGateway;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Models\Credit;
|
2020-01-30 05:50:45 +01:00
|
|
|
use App\Models\Invoice;
|
2020-01-29 03:03:47 +01:00
|
|
|
use App\Models\Payment;
|
|
|
|
use App\Repositories\ActivityRepository;
|
2020-07-17 11:47:17 +02:00
|
|
|
use App\Utils\Ninja;
|
2020-10-28 11:10:49 +01:00
|
|
|
use stdClass;
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
trait Refundable
|
|
|
|
{
|
2020-03-21 06:37:30 +01:00
|
|
|
/**
|
2020-09-06 11:38:10 +02:00
|
|
|
* Entry point for processing of refunds.
|
2020-10-28 11:10:49 +01:00
|
|
|
* @param array $data
|
|
|
|
* @return Refundable
|
|
|
|
* @throws PaymentRefundFailed
|
2020-03-21 06:37:30 +01:00
|
|
|
*/
|
|
|
|
public function processRefund(array $data)
|
|
|
|
{
|
|
|
|
if (isset($data['invoices']) && count($data['invoices']) > 0) {
|
|
|
|
return $this->refundPaymentWithInvoices($data);
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this->refundPaymentWithNoInvoices($data);
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private function refundPaymentWithNoInvoices(array $data)
|
|
|
|
{
|
|
|
|
//adjust payment refunded column amount
|
|
|
|
$this->refunded = $data['amount'];
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($data['amount'] == $this->amount) {
|
|
|
|
$this->status_id = Payment::STATUS_REFUNDED;
|
|
|
|
} else {
|
|
|
|
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_note = $this->buildCreditNote($data);
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_line_item = InvoiceItemFactory::create();
|
|
|
|
$credit_line_item->quantity = 1;
|
|
|
|
$credit_line_item->cost = $data['amount'];
|
|
|
|
$credit_line_item->product_key = ctrans('texts.credit');
|
|
|
|
$credit_line_item->notes = ctrans('texts.credit_created_by', ['transaction_reference' => $this->number]);
|
|
|
|
$credit_line_item->line_total = $data['amount'];
|
|
|
|
$credit_line_item->date = $data['date'];
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items = [];
|
|
|
|
$line_items[] = $credit_line_item;
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_note->save();
|
|
|
|
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
|
|
|
$credit_note->save();
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->createActivity($data, $credit_note->id);
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//determine if we need to refund via gateway
|
|
|
|
if ($data['gateway_refund'] !== false) {
|
|
|
|
//todo process gateway refund, on success, reduce the credit note balance to 0
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//$this->client->paid_to_date -= $data['amount'];
|
|
|
|
$this->client->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this->fresh();
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private function refundPaymentWithInvoices($data)
|
|
|
|
{
|
|
|
|
$total_refund = 0;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($data['invoices'] as $invoice) {
|
|
|
|
$total_refund += $invoice['amount'];
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$data['amount'] = $total_refund;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Set Payment Status*/
|
|
|
|
if ($total_refund == $this->amount) {
|
|
|
|
$this->status_id = Payment::STATUS_REFUNDED;
|
|
|
|
} else {
|
|
|
|
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Build Credit Note*/
|
|
|
|
$credit_note = $this->buildCreditNote($data);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items = [];
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-04-11 13:19:05 +02:00
|
|
|
$ledger_string = '';
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($data['invoices'] as $invoice) {
|
|
|
|
$inv = Invoice::find($invoice['invoice_id']);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_line_item = InvoiceItemFactory::create();
|
|
|
|
$credit_line_item->quantity = 1;
|
|
|
|
$credit_line_item->cost = $invoice['amount'];
|
|
|
|
$credit_line_item->product_key = ctrans('texts.invoice');
|
|
|
|
$credit_line_item->notes = ctrans('texts.refund_body', ['amount' => $data['amount'], 'invoice_number' => $inv->number]);
|
|
|
|
$credit_line_item->line_total = $invoice['amount'];
|
|
|
|
$credit_line_item->date = $data['date'];
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
$ledger_string .= $credit_line_item->notes.' ';
|
2020-04-11 13:19:05 +02:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$line_items[] = $credit_line_item;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
/* Update paymentable record */
|
|
|
|
foreach ($this->invoices as $paymentable_invoice) {
|
|
|
|
foreach ($data['invoices'] as $refunded_invoice) {
|
|
|
|
if ($refunded_invoice['invoice_id'] == $paymentable_invoice->id) {
|
|
|
|
$paymentable_invoice->pivot->refunded += $refunded_invoice['amount'];
|
|
|
|
$paymentable_invoice->pivot->save();
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($this->credits()->exists()) {
|
|
|
|
//Adjust credits first!!!
|
|
|
|
foreach ($this->credits as $paymentable_credit) {
|
|
|
|
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($available_credit > $total_refund) {
|
|
|
|
$paymentable_credit->pivot->refunded += $total_refund;
|
|
|
|
$paymentable_credit->pivot->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$paymentable_credit->balance += $total_refund;
|
|
|
|
$paymentable_credit->save();
|
2020-01-30 12:19:51 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$total_refund = 0;
|
|
|
|
} else {
|
|
|
|
$paymentable_credit->pivot->refunded += $available_credit;
|
|
|
|
$paymentable_credit->pivot->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$paymentable_credit->balance += $available_credit;
|
|
|
|
$paymentable_credit->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$total_refund -= $available_credit;
|
|
|
|
}
|
2020-01-30 12:19:51 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($total_refund == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_note->line_items = $line_items;
|
|
|
|
$credit_note->save();
|
2020-03-04 05:06:27 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$credit_note->number = $this->client->getNextCreditNumber($this->client);
|
|
|
|
$credit_note->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-05-14 13:33:29 +02:00
|
|
|
if ($data['gateway_refund'] !== false && $total_refund > 0) {
|
2020-05-14 03:04:23 +02:00
|
|
|
$gateway = CompanyGateway::find($this->company_gateway_id);
|
|
|
|
|
|
|
|
if ($gateway) {
|
2020-05-14 13:33:29 +02:00
|
|
|
$response = $gateway->driver($this->client)->refund($this, $total_refund);
|
2020-01-30 12:19:51 +01:00
|
|
|
|
2020-09-06 11:38:10 +02:00
|
|
|
if (! $response) {
|
2020-05-14 03:04:23 +02:00
|
|
|
throw new PaymentRefundFailed();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-30 12:19:51 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($total_refund > 0) {
|
|
|
|
$this->refunded += $total_refund;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-04-11 13:19:05 +02:00
|
|
|
$client_balance_adjustment = $this->adjustInvoices($data);
|
|
|
|
|
2021-02-03 13:29:44 +01:00
|
|
|
// $credit_note->ledger()->updateCreditBalance($client_balance_adjustment, $ledger_string);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$this->client->paid_to_date -= $data['amount'];
|
|
|
|
$this->client->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $this;
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private function createActivity(array $data, int $credit_id)
|
|
|
|
{
|
2020-10-28 11:10:49 +01:00
|
|
|
$fields = new stdClass;
|
2020-01-29 03:03:47 +01:00
|
|
|
$activity_repo = new ActivityRepository();
|
|
|
|
|
|
|
|
$fields->payment_id = $this->id;
|
|
|
|
$fields->user_id = $this->user_id;
|
|
|
|
$fields->company_id = $this->company_id;
|
|
|
|
$fields->activity_type_id = Activity::REFUNDED_PAYMENT;
|
|
|
|
$fields->credit_id = $credit_id;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if (isset($data['invoices'])) {
|
|
|
|
foreach ($data['invoices'] as $invoice) {
|
|
|
|
$fields->invoice_id = $invoice->id;
|
2020-09-06 11:38:10 +02:00
|
|
|
|
2020-07-17 11:47:17 +02:00
|
|
|
$activity_repo->save($fields, $this, Ninja::eventVars());
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
|
|
|
} else {
|
2020-07-17 11:47:17 +02:00
|
|
|
$activity_repo->save($fields, $this, Ninja::eventVars());
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
private function buildCreditNote(array $data) :?Credit
|
|
|
|
{
|
|
|
|
$credit_note = CreditFactory::create($this->company_id, $this->user_id);
|
|
|
|
$credit_note->assigned_user_id = isset($this->assigned_user_id) ?: null;
|
|
|
|
$credit_note->date = $data['date'];
|
|
|
|
$credit_note->status_id = Credit::STATUS_SENT;
|
|
|
|
$credit_note->client_id = $this->client->id;
|
|
|
|
$credit_note->amount = $data['amount'];
|
|
|
|
$credit_note->balance = $data['amount'];
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
return $credit_note;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-04-15 02:30:52 +02:00
|
|
|
private function adjustInvoices(array $data)
|
2020-03-21 06:37:30 +01:00
|
|
|
{
|
2020-04-11 13:19:05 +02:00
|
|
|
$adjustment_amount = 0;
|
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
foreach ($data['invoices'] as $refunded_invoice) {
|
|
|
|
$invoice = Invoice::find($refunded_invoice['invoice_id']);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
if ($invoice->amount == $invoice->balance) {
|
|
|
|
$invoice->service()->setStatus(Invoice::STATUS_SENT);
|
|
|
|
} else {
|
|
|
|
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$client = $invoice->client;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-04-11 13:19:05 +02:00
|
|
|
$adjustment_amount += $refunded_invoice['amount'];
|
2020-03-21 06:37:30 +01:00
|
|
|
$client->balance += $refunded_invoice['amount'];
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
$client->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-03-21 06:37:30 +01:00
|
|
|
//todo adjust ledger balance here? or after and reference the credit and its total
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
2020-04-11 13:19:05 +02:00
|
|
|
|
|
|
|
return $adjustment_amount;
|
2020-03-21 06:37:30 +01:00
|
|
|
}
|
|
|
|
}
|