2020-01-29 03:03:47 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Utils\Traits\Payment;
|
|
|
|
|
|
|
|
use App\Factory\CreditFactory;
|
|
|
|
use App\Factory\InvoiceItemFactory;
|
|
|
|
use App\Models\Activity;
|
|
|
|
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;
|
|
|
|
|
|
|
|
trait Refundable
|
|
|
|
{
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
/**
|
|
|
|
* Entry point for processing of refunds
|
|
|
|
*/
|
2020-01-29 03:03:47 +01:00
|
|
|
public function processRefund(array $data)
|
|
|
|
{
|
|
|
|
|
2020-02-05 11:28:56 +01:00
|
|
|
if(isset($data['invoices']) && count($data['invoices']) > 0)
|
2020-01-30 12:19:51 +01:00
|
|
|
return $this->refundPaymentWithInvoices($data);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
return $this->refundPaymentWithNoInvoices($data);
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
private function refundPaymentWithNoInvoices(array $data)
|
2020-01-29 03:03:47 +01:00
|
|
|
{
|
|
|
|
//adjust payment refunded column amount
|
2020-01-30 05:50:45 +01:00
|
|
|
$this->refunded = $data['amount'];
|
2020-01-29 03:03:47 +01:00
|
|
|
|
2020-01-30 05:50:45 +01:00
|
|
|
if($data['amount'] == $this->amount)
|
2020-01-29 03:03:47 +01:00
|
|
|
$this->status_id = Payment::STATUS_REFUNDED;
|
|
|
|
else
|
|
|
|
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
|
|
|
|
|
2020-01-30 05:50:45 +01:00
|
|
|
$credit_note = $this->buildCreditNote($data);
|
2020-01-29 03:03:47 +01:00
|
|
|
|
|
|
|
$credit_line_item = InvoiceItemFactory::create();
|
|
|
|
$credit_line_item->quantity = 1;
|
2020-01-30 05:50:45 +01:00
|
|
|
$credit_line_item->cost = $data['amount'];
|
2020-01-29 03:03:47 +01:00
|
|
|
$credit_line_item->product_key = ctrans('texts.credit');
|
2020-01-30 05:50:45 +01:00
|
|
|
$credit_line_item->notes = ctrans('texts.credit_created_by', ['transaction_reference' => $this->number]);
|
|
|
|
$credit_line_item->line_total = $data['amount'];
|
2020-01-29 03:03:47 +01:00
|
|
|
$credit_line_item->date = $data['date'];
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
$line_items[] = $credit_line_item;
|
|
|
|
|
|
|
|
$credit_note->save();
|
|
|
|
|
|
|
|
$this->createActivity($data, $credit_note->id);
|
|
|
|
|
|
|
|
//determine if we need to refund via gateway
|
|
|
|
if($data['gateway_refund'] !== false)
|
|
|
|
{
|
2020-01-30 05:50:45 +01:00
|
|
|
//todo process gateway refund, on success, reduce the credit note balance to 0
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$this->save();
|
|
|
|
|
2020-01-30 05:50:45 +01:00
|
|
|
//$this->client->paid_to_date -= $data['amount'];
|
2020-01-29 03:03:47 +01:00
|
|
|
$this->client->save();
|
|
|
|
|
2020-02-05 11:28:56 +01:00
|
|
|
return $this->fresh();
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
private function refundPaymentWithInvoices($data)
|
2020-01-29 03:03:47 +01:00
|
|
|
{
|
2020-01-30 05:50:45 +01:00
|
|
|
|
|
|
|
$total_refund = 0;
|
|
|
|
|
|
|
|
foreach($data['invoices'] as $invoice)
|
|
|
|
$total_refund += $invoice['amount'];
|
|
|
|
|
|
|
|
$data['amount'] = $total_refund;
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
/* Set Payment Status*/
|
2020-01-30 05:50:45 +01:00
|
|
|
if($total_refund == $this->amount)
|
|
|
|
$this->status_id = Payment::STATUS_REFUNDED;
|
|
|
|
else
|
|
|
|
$this->status_id = Payment::STATUS_PARTIALLY_REFUNDED;
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
/* Build Credit Note*/
|
2020-01-30 05:50:45 +01:00
|
|
|
$credit_note = $this->buildCreditNote($data);
|
|
|
|
|
|
|
|
$line_items = [];
|
|
|
|
|
|
|
|
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'];
|
|
|
|
|
|
|
|
$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();
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
if($this->credits()->exists())
|
2020-01-30 05:50:45 +01:00
|
|
|
{
|
2020-01-30 12:19:51 +01:00
|
|
|
//Adjust credits first!!!
|
2020-01-30 05:50:45 +01:00
|
|
|
foreach($this->credits as $paymentable_credit)
|
|
|
|
{
|
2020-01-30 12:19:51 +01:00
|
|
|
$available_credit = $paymentable_credit->pivot->amount - $paymentable_credit->pivot->refunded;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-30 12:19:51 +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-01-30 12:19:51 +01:00
|
|
|
$paymentable_credit->balance += $total_refund;
|
|
|
|
$paymentable_credit->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
$total_refund = 0;
|
2020-01-30 05:50:45 +01:00
|
|
|
|
|
|
|
}
|
2020-01-30 12:19:51 +01:00
|
|
|
else {
|
|
|
|
$paymentable_credit->pivot->refunded += $available_credit;
|
|
|
|
$paymentable_credit->pivot->save();
|
|
|
|
|
|
|
|
$paymentable_credit->balance += $available_credit;
|
|
|
|
$paymentable_credit->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
$total_refund -= $available_credit;
|
|
|
|
}
|
2020-01-30 05:50:45 +01:00
|
|
|
|
2020-01-30 12:19:51 +01:00
|
|
|
if($total_refund == 0)
|
|
|
|
break;
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
2020-01-30 12:19:51 +01:00
|
|
|
|
2020-01-30 05:50:45 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$credit_note->line_items = $line_items;
|
|
|
|
$credit_note->save();
|
|
|
|
|
|
|
|
//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 12:19:51 +01:00
|
|
|
|
|
|
|
if($total_refund > 0)
|
|
|
|
$this->refunded += $total_refund;
|
|
|
|
|
2020-01-30 05:50:45 +01:00
|
|
|
$this->save();
|
|
|
|
|
|
|
|
$this->adjustInvoices($data);
|
|
|
|
|
|
|
|
$this->client->paid_to_date -= $data['amount'];
|
|
|
|
$this->client->save();
|
|
|
|
|
|
|
|
|
2020-01-29 05:25:08 +01:00
|
|
|
return $this;
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
$activity_repo->save($fields, $this);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-01-30 05:50:45 +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->number = $this->client->getNextCreditNumber($this->client);
|
|
|
|
$credit_note->status_id = Credit::STATUS_DRAFT;
|
|
|
|
$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) :void
|
|
|
|
{
|
|
|
|
foreach($data['invoices'] as $refunded_invoice)
|
|
|
|
{
|
|
|
|
$invoice = Invoice::find($refunded_invoice['invoice_id']);
|
|
|
|
|
2020-02-04 08:51:44 +01:00
|
|
|
$invoice->service()->updateBalance($refunded_invoice['amount'])->save();
|
2020-01-30 05:50:45 +01:00
|
|
|
|
|
|
|
if($invoice->amount == $invoice->balance)
|
2020-02-04 08:51:44 +01:00
|
|
|
$invoice->service()->setStatus(Invoice::STATUS_SENT);
|
2020-01-30 05:50:45 +01:00
|
|
|
else
|
2020-02-04 08:51:44 +01:00
|
|
|
$invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
|
2020-01-30 05:50:45 +01:00
|
|
|
|
|
|
|
$client = $invoice->client;
|
|
|
|
|
|
|
|
$client->balance += $refunded_invoice['amount'];
|
|
|
|
///$client->paid_to_date -= $refunded_invoice['amount'];
|
|
|
|
|
|
|
|
$client->save();
|
|
|
|
|
|
|
|
//todo adjust ledger balance here? or after and reference the credit and its total
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-01-29 03:03:47 +01:00
|
|
|
}
|