diff --git a/app/Services/Credit/CreditService.php b/app/Services/Credit/CreditService.php index 5970c42f2860..4c88f20e81a1 100644 --- a/app/Services/Credit/CreditService.php +++ b/app/Services/Credit/CreditService.php @@ -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() diff --git a/app/Services/Subscription/SubscriptionService.php b/app/Services/Subscription/SubscriptionService.php index e10e3ab261f3..940a3231e270 100644 --- a/app/Services/Subscription/SubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -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', diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 7af2a5d4b526..ad80141063d7 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -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').' '.$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')];