1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 07:02:34 +01:00
invoiceninja/app/Utils/Traits/Payment/Refundable.php
David Bomba ba75a44eb8
Laravel 7.x Shift (#40)
* Adopt Laravel coding style

The Laravel framework adopts the PSR-2 coding style with some additions.
Laravel apps *should* adopt this coding style as well.

However, Shift allows you to customize the adopted coding style by
adding your own [PHP CS Fixer][1] `.php_cs` config to your project.

You may use [Shift's .php_cs][2] file as a base.

[1]: https://github.com/FriendsOfPHP/PHP-CS-Fixer
[2]: https://gist.github.com/laravel-shift/cab527923ed2a109dda047b97d53c200

* Shift bindings

PHP 5.5.9+ adds the new static `class` property which provides the fully qualified class name. This is preferred over using class name strings as these references are checked by the parser.

* Shift core files

* Shift to Throwable

* Add laravel/ui dependency

* Unindent vendor mail templates

* Shift config files

* Default config files

In an effort to make upgrading the constantly changing config files
easier, Shift defaulted them so you can review the commit diff for
changes. Moving forward, you should use ENV variables or create a
separate config file to allow the core config files to remain
automatically upgradeable.

* Shift Laravel dependencies

* Shift cleanup

* Upgrade to Laravel 7

Co-authored-by: Laravel Shift <shift@laravelshift.com>
2020-09-06 19:38:10 +10:00

258 lines
8.2 KiB
PHP

<?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\Utils\Traits\Payment;
use App\Exceptions\PaymentRefundFailed;
use App\Factory\CreditFactory;
use App\Factory\InvoiceItemFactory;
use App\Models\Activity;
use App\Models\CompanyGateway;
use App\Models\Credit;
use App\Models\Invoice;
use App\Models\Payment;
use App\Repositories\ActivityRepository;
use App\Utils\Ninja;
trait Refundable
{
/**
* Entry point for processing of refunds.
*/
public function processRefund(array $data)
{
if (isset($data['invoices']) && count($data['invoices']) > 0) {
return $this->refundPaymentWithInvoices($data);
}
return $this->refundPaymentWithNoInvoices($data);
}
private function refundPaymentWithNoInvoices(array $data)
{
//adjust payment refunded column amount
$this->refunded = $data['amount'];
if ($data['amount'] == $this->amount) {
$this->status_id = Payment::STATUS_REFUNDED;
} else {
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
}
$credit_note = $this->buildCreditNote($data);
$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'];
$line_items = [];
$line_items[] = $credit_line_item;
$credit_note->save();
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->save();
$this->createActivity($data, $credit_note->id);
//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
}
$this->save();
//$this->client->paid_to_date -= $data['amount'];
$this->client->save();
return $this->fresh();
}
private function refundPaymentWithInvoices($data)
{
$total_refund = 0;
foreach ($data['invoices'] as $invoice) {
$total_refund += $invoice['amount'];
}
$data['amount'] = $total_refund;
/* Set Payment Status*/
if ($total_refund == $this->amount) {
$this->status_id = Payment::STATUS_REFUNDED;
} else {
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
}
/* Build Credit Note*/
$credit_note = $this->buildCreditNote($data);
$line_items = [];
$ledger_string = '';
foreach ($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' => $data['amount'], 'invoice_number' => $inv->number]);
$credit_line_item->line_total = $invoice['amount'];
$credit_line_item->date = $data['date'];
$ledger_string .= $credit_line_item->notes.' ';
$line_items[] = $credit_line_item;
}
/* 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();
}
}
}
if ($this->credits()->exists()) {
//Adjust credits first!!!
foreach ($this->credits as $paymentable_credit) {
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
if ($available_credit > $total_refund) {
$paymentable_credit->pivot->refunded += $total_refund;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $total_refund;
$paymentable_credit->save();
$total_refund = 0;
} else {
$paymentable_credit->pivot->refunded += $available_credit;
$paymentable_credit->pivot->save();
$paymentable_credit->balance += $available_credit;
$paymentable_credit->save();
$total_refund -= $available_credit;
}
if ($total_refund == 0) {
break;
}
}
}
$credit_note->line_items = $line_items;
$credit_note->save();
$credit_note->number = $this->client->getNextCreditNumber($this->client);
$credit_note->save();
if ($data['gateway_refund'] !== false && $total_refund > 0) {
$gateway = CompanyGateway::find($this->company_gateway_id);
if ($gateway) {
$response = $gateway->driver($this->client)->refund($this, $total_refund);
if (! $response) {
throw new PaymentRefundFailed();
}
}
}
if ($total_refund > 0) {
$this->refunded += $total_refund;
}
$this->save();
$client_balance_adjustment = $this->adjustInvoices($data);
$credit_note->ledger()->updateCreditBalance($client_balance_adjustment, $ledger_string);
$this->client->paid_to_date -= $data['amount'];
$this->client->save();
return $this;
}
private function createActivity(array $data, int $credit_id)
{
$fields = new \stdClass;
$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;
if (isset($data['invoices'])) {
foreach ($data['invoices'] as $invoice) {
$fields->invoice_id = $invoice->id;
$activity_repo->save($fields, $this, Ninja::eventVars());
}
} else {
$activity_repo->save($fields, $this, Ninja::eventVars());
}
}
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'];
return $credit_note;
}
private function adjustInvoices(array $data)
{
$adjustment_amount = 0;
foreach ($data['invoices'] as $refunded_invoice) {
$invoice = Invoice::find($refunded_invoice['invoice_id']);
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
if ($invoice->amount == $invoice->balance) {
$invoice->service()->setStatus(Invoice::STATUS_SENT);
} else {
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
}
$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
}
return $adjustment_amount;
}
}