1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-21 08:51:34 +02:00
invoiceninja/app/Services/Payment/RefundPayment.php

345 lines
11 KiB
PHP
Raw Normal View History

2020-05-29 00:21:47 +02:00
<?php
namespace App\Services\Payment;
2020-05-29 08:24:41 +02:00
use App\Exceptions\PaymentRefundFailed;
2020-05-29 00:21:47 +02:00
use App\Factory\CreditFactory;
use App\Factory\InvoiceItemFactory;
2020-06-01 00:29:53 +02:00
use App\Models\Activity;
2020-05-29 08:24:41 +02:00
use App\Models\CompanyGateway;
2020-05-29 00:21:47 +02:00
use App\Models\Credit;
2020-06-01 05:16:06 +02:00
use App\Models\Invoice;
2020-05-29 00:21:47 +02:00
use App\Models\Payment;
2020-06-01 00:29:53 +02:00
use App\Repositories\ActivityRepository;
2020-05-29 00:21:47 +02:00
class RefundPayment
{
public $payment;
2020-06-01 00:29:53 +02:00
public $refund_data;
2020-05-29 00:21:47 +02:00
private $credit_note;
2020-05-29 08:24:41 +02:00
private $total_refund;
private $gateway_refund_status;
private $activity_repository;
2020-05-29 00:21:47 +02:00
public function __construct($payment, $refund_data)
{
$this->payment = $payment;
$this->refund_data = $refund_data;
2020-05-29 08:24:41 +02:00
$this->total_refund = 0;
$this->gateway_refund_status = false;
$this->activity_repository = new ActivityRepository();
2020-05-29 00:21:47 +02:00
}
public function run()
{
return $this->calculateTotalRefund() //sets amount for the refund (needed if we are refunding multiple invoices in one payment)
->setStatus() //sets status of payment
2020-06-27 02:05:31 +02:00
//->reversePayment()
//->buildCreditNote() //generate the credit note
//->buildCreditLineItems() //generate the credit note items
->updateCreditables() //return the credits first
->updatePaymentables() //update the paymentable items
->adjustInvoices()
->processGatewayRefund() //process the gateway refund if needed
->save();
2020-05-29 08:24:41 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Process the refund through the gateway
*
* @return $this
*/
2020-05-29 08:24:41 +02:00
private function processGatewayRefund()
{
if ($this->refund_data['gateway_refund'] !== false && $this->total_refund > 0) {
2020-06-27 02:05:31 +02:00
if ($this->payment->company_gateway) {
2020-06-01 00:29:53 +02:00
$response = $gateway->driver($this->payment->client)->refund($this->payment, $this->total_refund);
2020-05-29 08:24:41 +02:00
if ($response['success']) {
2020-05-29 08:24:41 +02:00
throw new PaymentRefundFailed();
}
2020-06-01 05:16:06 +02:00
2020-06-27 02:05:31 +02:00
$this->payment->refunded += $this->total_refund;
$this
->createActivity($gateway)
->updateCreditNoteBalance();
2020-05-29 08:24:41 +02:00
}
} else {
2020-06-01 05:16:06 +02:00
$this->payment->refunded += $this->total_refund;
}
2020-05-29 08:24:41 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Create the payment activity
*
* @param json $notes gateway_transaction
* @return $this
*/
private function createActivity($notes)
2020-05-29 08:24:41 +02:00
{
2020-06-01 00:29:53 +02:00
$fields = new \stdClass;
$activity_repo = new ActivityRepository();
$fields->payment_id = $this->payment->id;
$fields->user_id = $this->payment->user_id;
$fields->company_id = $this->payment->company_id;
$fields->activity_type_id = Activity::REFUNDED_PAYMENT;
$fields->credit_id = $this->credit_note->id;
$fields->notes = json_encode($notes);
2020-06-01 00:29:53 +02:00
if (isset($this->refund_data['invoices'])) {
foreach ($this->refund_data['invoices'] as $invoice) {
2020-06-01 05:16:06 +02:00
$fields->invoice_id = $invoice['invoice_id'];
2020-06-01 00:29:53 +02:00
$activity_repo->save($fields, $this->payment);
}
} else {
$activity_repo->save($fields, $this->payment);
}
2020-05-29 08:24:41 +02:00
2020-06-01 00:29:53 +02:00
return $this;
2020-05-29 00:21:47 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Determine the amount of refund
*
* @return $this
*/
2020-05-29 00:21:47 +02:00
private function calculateTotalRefund()
{
2020-06-27 02:05:31 +02:00
if (array_key_exists('invoices', $this->refund_data) && count($this->refund_data['invoices']) > 0)
2020-06-01 05:16:06 +02:00
$this->total_refund = collect($this->refund_data['invoices'])->sum('amount');
2020-06-27 02:05:31 +02:00
else
2020-05-29 08:24:41 +02:00
$this->total_refund = $this->refund_data['amount'];
2020-05-29 00:21:47 +02:00
return $this;
2020-06-27 02:05:31 +02:00
2020-05-29 00:21:47 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Set the payment status
*/
2020-05-29 00:21:47 +02:00
private function setStatus()
{
2020-06-27 02:05:31 +02:00
if ($this->refund_data['amount'] == $this->payment->amount)
2020-05-29 00:21:47 +02:00
$this->payment->status_id = Payment::STATUS_REFUNDED;
2020-06-27 02:05:31 +02:00
else
2020-05-29 00:21:47 +02:00
$this->payment->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
2020-05-29 08:24:41 +02:00
return $this;
2020-05-29 00:21:47 +02:00
}
2020-06-27 02:05:31 +02:00
/**
* Update the paymentable records
*
* @return $this
*/
2020-05-29 08:24:41 +02:00
private function updatePaymentables()
2020-05-29 00:21:47 +02:00
{
if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) {
2020-05-29 08:24:41 +02:00
$this->payment->invoices->each(function ($paymentable_invoice) {
2020-05-29 00:21:47 +02:00
collect($this->refund_data['invoices'])->each(function ($refunded_invoice) use ($paymentable_invoice) {
2020-05-29 00:21:47 +02:00
if ($refunded_invoice['invoice_id'] == $paymentable_invoice->id) {
2020-05-29 08:24:41 +02:00
$paymentable_invoice->pivot->refunded += $refunded_invoice['amount'];
$paymentable_invoice->pivot->save();
}
});
});
2020-05-29 00:21:47 +02:00
}
2020-05-29 08:24:41 +02:00
return $this;
}
2020-05-29 00:21:47 +02:00
2020-06-27 02:05:31 +02:00
/**
* If credits have been bundled in this payment, we
* need to reverse these
*
* @return $this
*/
2020-05-29 08:24:41 +02:00
private function updateCreditables()
{
2020-05-29 00:21:47 +02:00
2020-05-29 08:24:41 +02:00
if ($this->payment->credits()->exists()) {
2020-05-29 00:21:47 +02:00
//Adjust credits first!!!
2020-05-29 08:24:41 +02:00
foreach ($this->payment->credits as $paymentable_credit) {
2020-05-29 00:21:47 +02:00
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
2020-05-29 08:24:41 +02:00
if ($available_credit > $this->total_refund) {
$paymentable_credit->pivot->refunded += $this->total_refund;
2020-05-29 00:21:47 +02:00
$paymentable_credit->pivot->save();
2020-05-29 08:24:41 +02:00
$paymentable_credit->balance += $this->total_refund;
2020-06-27 03:43:45 +02:00
$paymentable_credit->service()->setStatus(Credit::STATUS_SENT)->save();
//$paymentable_credit->save();
2020-05-29 00:21:47 +02:00
2020-05-29 08:24:41 +02:00
$this->total_refund = 0;
2020-05-29 00:21:47 +02:00
} else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $available_credit;
2020-06-27 03:43:45 +02:00
$paymentable_credit->service()->setStatus(Credit::STATUS_SENT)->save();
// $paymentable_credit->save();
2020-05-29 00:21:47 +02:00
2020-05-29 08:24:41 +02:00
$this->total_refund -= $available_credit;
2020-05-29 00:21:47 +02:00
}
2020-05-29 08:24:41 +02:00
if ($this->total_refund == 0) {
2020-05-29 00:21:47 +02:00
break;
}
}
}
2020-05-29 08:24:41 +02:00
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Reverse the payments made on invoices
*
* @return $this
*/
2020-06-01 06:18:33 +02:00
private function adjustInvoices()
{
$adjustment_amount = 0;
if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) {
2020-06-27 02:05:31 +02:00
foreach ($this->refund_data['invoices'] as $refunded_invoice)
{
2020-06-01 06:18:33 +02:00
$invoice = Invoice::find($refunded_invoice['invoice_id']);
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
2020-06-27 03:43:45 +02:00
$invoice->ledger()->updateInvoiceBalance($refunded_invoice['amount'], "Refund of payment # {$this->payment->number}")->save();
2020-06-01 06:18:33 +02:00
2020-06-27 02:05:31 +02:00
if ($invoice->amount == $invoice->balance)
2020-06-01 06:18:33 +02:00
$invoice->service()->setStatus(Invoice::STATUS_SENT);
2020-06-27 02:05:31 +02:00
else
2020-06-01 06:18:33 +02:00
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
2020-06-27 02:05:31 +02:00
$invoice->save();
2020-06-01 06:18:33 +02:00
$client = $invoice->client;
$adjustment_amount += $refunded_invoice['amount'];
$client->balance += $refunded_invoice['amount'];
$client->save();
//todo adjust ledger balance here? or after and reference the credit and its total
}
2020-06-27 02:20:27 +02:00
// $ledger_string = "Refund for Invoice {$invoice->number} for amount " . $refunded_invoice['amount']; //todo
2020-06-01 06:18:33 +02:00
2020-06-27 02:20:27 +02:00
// $this->credit_note->ledger()->updateCreditBalance($adjustment_amount, $ledger_string);
2020-06-28 05:05:58 +02:00
2020-06-27 02:05:31 +02:00
$client = $this->payment->client->fresh();
2020-06-28 05:05:58 +02:00
$client->service()->updatePaidToDate(-1*$this->total_refund)->save();
2020-06-01 06:18:33 +02:00
}
return $this;
}
2020-06-27 02:05:31 +02:00
/**
* Saves the payment
*
* @return Payment $payment
*/
2020-05-29 08:24:41 +02:00
private function save()
{
$this->payment->save();
return $this->payment;
}
2020-06-27 02:05:31 +02:00
// public function updateCreditNoteBalance()
// {
// $this->credit_note->balance -= $this->total_refund;
// $this->credit_note->status_id = Credit::STATUS_APPLIED;
// $this->credit_note->balance === 0
// ? $this->credit_note->status_id = Credit::STATUS_APPLIED
// : $this->credit_note->status_id = Credit::STATUS_PARTIAL;
// $this->credit_note->save();
// return $this;
// }
// private function buildCreditNote()
// {
// $this->credit_note = CreditFactory::create($this->payment->company_id, $this->payment->user_id);
// $this->credit_note->assigned_user_id = isset($this->payment->assigned_user_id) ?: null;
// $this->credit_note->date = $this->refund_data['date'];
// $this->credit_note->status_id = Credit::STATUS_SENT;
// $this->credit_note->client_id = $this->payment->client->id;
// $this->credit_note->amount = $this->total_refund;
// $this->credit_note->balance = $this->total_refund;
// $this->credit_note->save();
// $this->credit_note->number = $this->payment->client->getNextCreditNumber($this->payment->client);
// $this->credit_note->save();
// return $this;
// }
// private function buildCreditLineItems()
// {
// $ledger_string = '';
// if (isset($this->refund_data['invoices']) && count($this->refund_data['invoices']) > 0) {
// foreach ($this->refund_data['invoices'] as $invoice) {
// $inv = Invoice::find($invoice['invoice_id']);
// $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' => $invoice['amount'], 'invoice_number' => $inv->number]);
// $credit_line_item->line_total = $invoice['amount'];
// $credit_line_item->date = $this->refund_data['date'];
// $ledger_string .= $credit_line_item->notes . ' ';
// $line_items[] = $credit_line_item;
// }
// } else {
// $credit_line_item = InvoiceItemFactory::create();
// $credit_line_item->quantity = 1;
// $credit_line_item->cost = $this->refund_data['amount'];
// $credit_line_item->product_key = ctrans('texts.credit');
// $credit_line_item->notes = ctrans('texts.credit_created_by', ['transaction_reference' => $this->payment->number]);
// $credit_line_item->line_total = $this->refund_data['amount'];
// $credit_line_item->date = $this->refund_data['date'];
// $line_items = [];
// $line_items[] = $credit_line_item;
// }
// $this->credit_note->line_items = $line_items;
// $this->credit_note->save();
// return $this;
// }
2020-05-29 08:24:41 +02:00
}