Add payment number when generating credit payment from subscription

This commit is contained in:
David Bomba 2023-02-04 15:51:39 +11:00
parent 9b9f73aed2
commit 5e68199396
3 changed files with 24 additions and 5 deletions

View File

@ -271,6 +271,12 @@ class CreditService
public function restoreCredit()
{
// $this->credit->payments()->each(function ($payment){
// $payment->pivot->update(['deleted_at' => null]);
// });
$this->credit
->client
->service()

View File

@ -59,6 +59,8 @@ class SubscriptionService
/** @var subscription */
private $subscription;
private float $credit_payments = 0;
public function __construct(Subscription $subscription)
{
$this->subscription = $subscription;
@ -531,10 +533,17 @@ class SubscriptionService
->orderBy('id', 'desc')
->first();
if($this->calculateProRataRefundForSubscription($last_invoice) > 0)
$credit = $this->createCredit($last_invoice, $target_subscription, false);
//if last payment was created from a credit, do not generate a new credit, refund the old one.
if($last_invoice) {
$last_invoice->payments->each(function ($payment){
$this->credit_payments += $payment->credits->sum('amount');
});
$invoice_repo = new InvoiceRepository();
$invoice_repo->delete($last_invoice);
@ -547,6 +556,10 @@ class SubscriptionService
}
if($this->calculateProRataRefundForSubscription($last_invoice) > 0 && $this->credit_payments == 0)
$credit = $this->createCredit($last_invoice, $target_subscription, false);
$new_recurring_invoice = $this->createNewRecurringInvoice($recurring_invoice);
$invoice = $this->changePlanInvoice($target_subscription, $recurring_invoice->client_id);
@ -560,7 +573,7 @@ class SubscriptionService
$payment->is_manual = true;
$payment->save();
$payment->service()->applyCreditsToInvoice($invoice);
$payment->service()->applyNumber()->applyCreditsToInvoice($invoice);
$context = [
'context' => 'change_plan',

View File

@ -296,7 +296,7 @@ class HtmlEngine
$data['$portal_url'] = ['value' => $this->invitation->getPortalLink(), 'label' =>''];
$data['$entity_number'] = &$data['$number'];
$data['$invoice.discount'] = ['value' => Number::formatMoney($this->entity_calc->getTotalDiscount(), $this->client) ?: ' ', 'label' => ($this->entity->is_amount_discount) ? ctrans('texts.discount') : ctrans('texts.discount').'&nbsp'.$this->entity->discount.'%'];
$data['$invoice.discount'] = ['value' => Number::formatMoney($this->entity_calc->getTotalDiscount(), $this->client) ?: ' ', 'label' => ($this->entity->is_amount_discount) ? ctrans('texts.discount') : ctrans('texts.discount').' '.$this->entity->discount.'%'];
$data['$discount'] = &$data['$invoice.discount'];
$data['$subtotal'] = ['value' => Number::formatMoney($this->entity_calc->getSubTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.subtotal')];
$data['$gross_subtotal'] = ['value' => Number::formatMoney($this->entity_calc->getGrossSubTotal(), $this->client) ?: ' ', 'label' => ctrans('texts.subtotal')];