chore: refactor account model (#3570)

This commit is contained in:
Regis Freyd
2020-02-13 10:48:51 -05:00
committed by GitHub
parent 2e8a597c27
commit 5bf26262dc
45 changed files with 837 additions and 696 deletions
@@ -8,6 +8,7 @@ use Illuminate\Http\Request;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Payment;
use Illuminate\Http\Response;
use App\Helpers\AccountHelper;
use App\Helpers\InstanceHelper;
use App\Exceptions\StripeException;
use Illuminate\Support\Facades\App;
@@ -28,28 +29,30 @@ class SubscriptionsController extends Controller
*/
public function index()
{
$account = auth()->user()->account;
if (! config('monica.requires_subscription')) {
return redirect()->route('settings.index');
}
$subscription = auth()->user()->account->getSubscribedPlan();
if (! auth()->user()->account->isSubscribed() && (! $subscription || $subscription->ended())) {
$subscription = $account->getSubscribedPlan();
if (! $account->isSubscribed() && (! $subscription || $subscription->ended())) {
return view('settings.subscriptions.blank', [
'numberOfCustomers' => InstanceHelper::getNumberOfPaidSubscribers(),
]);
}
$planId = auth()->user()->account->getSubscribedPlanId();
$planId = $account->getSubscribedPlanId();
try {
$nextBillingDate = auth()->user()->account->getNextBillingDate();
$nextBillingDate = $account->getNextBillingDate();
} catch (StripeException $e) {
$nextBillingDate = trans('app.unknown');
}
$hasInvoices = auth()->user()->account->hasStripeId() && auth()->user()->account->hasInvoices();
$hasInvoices = $account->hasStripeId() && $account->hasInvoices();
$invoices = null;
if ($hasInvoices) {
$invoices = auth()->user()->account->invoices();
$invoices = $account->invoices();
}
return view('settings.subscriptions.account', [
@@ -58,6 +61,7 @@ class SubscriptionsController extends Controller
'subscription' => $subscription,
'hasInvoices' => $hasInvoices,
'invoices' => $invoices,
'accountHasLimitations' => AccountHelper::hasLimitations($account),
]);
}
@@ -168,15 +172,18 @@ class SubscriptionsController extends Controller
return redirect()->route('settings.index');
}
$subscription = auth()->user()->account->getSubscribedPlan();
if (! auth()->user()->account->isSubscribed() && ! $subscription) {
$subscription = $account->getSubscribedPlan();
if (! $account->isSubscribed() && ! $subscription) {
return redirect()->route('settings.index');
}
return view('settings.subscriptions.downgrade-checklist')
->with('numberOfActiveContacts', $account->contacts()->active()->count())
->with('numberOfPendingInvitations', $account->invitations()->count())
->with('numberOfUsers', $account->users()->count());
->with('numberOfUsers', $account->users()->count())
->with('accountHasLimitations', AccountHelper::hasLimitations($account))
->with('hasReachedContactLimit', AccountHelper::hasReachedContactLimit($account))
->with('canDowngrade', AccountHelper::canDowngrade($account));
}
/**
@@ -186,7 +193,7 @@ class SubscriptionsController extends Controller
*/
public function processDowngrade()
{
if (! auth()->user()->account->canDowngrade()) {
if (! AccountHelper::canDowngrade(auth()->user()->account)) {
return redirect()->route('settings.subscriptions.downgrade');
}
@@ -238,10 +245,10 @@ class SubscriptionsController extends Controller
/**
* Download the invoice as PDF.
*
* @param int $invoiceId
* @param mixed $invoiceId
* @return Response
*/
public function downloadInvoice(int $invoiceId)
public function downloadInvoice($invoiceId)
{
return auth()->user()->account->downloadInvoice($invoiceId, [
'vendor' => 'Monica',