From fb9bb7e269d8f59214651e857fd419a79751cefe Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 09:28:12 +1100 Subject: [PATCH 01/12] Fixes for converting quote to invoice --- app/Http/Controllers/QuoteController.php | 2 +- app/Services/BillingSubscription/BillingSubscriptionService.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index e4cb852c767b..02045ce2897f 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -532,7 +532,7 @@ class QuoteController extends BaseController return response()->json(['message' => ctrans('texts.sent_message')], 200); } - if ($action == 'convert') { + if ($action == 'convert' || $action == 'convert_to_invoice') { $this->entity_type = Quote::class; $this->entity_transformer = QuoteTransformer::class; diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index 9cf918d99dc2..8b6591219b73 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/BillingSubscription/BillingSubscriptionService.php @@ -192,6 +192,7 @@ class BillingSubscriptionService throw new \Exception("Could not match an invoice for payment of billing subscription"); //todo - need to remove the promo code - if it exists + return InvoiceToRecurringInvoiceFactory::create($invoice); } From 89598d44ef8fb5eaf7977c9c3d68288e0fd590e1 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 24 Mar 2021 09:28:42 +1100 Subject: [PATCH 02/12] Fixes for naming PDFs --- app/Http/Controllers/InvoiceController.php | 2 ++ app/Models/BaseModel.php | 4 +++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/InvoiceController.php b/app/Http/Controllers/InvoiceController.php index b9e99002f42d..9c037e92286d 100644 --- a/app/Http/Controllers/InvoiceController.php +++ b/app/Http/Controllers/InvoiceController.php @@ -795,6 +795,8 @@ class InvoiceController extends BaseController $file_path = $invoice->service()->getInvoicePdf($contact); +nlog($file_path); + return response()->download($file_path, basename($file_path)); } diff --git a/app/Models/BaseModel.php b/app/Models/BaseModel.php index 8d818643d6bb..ab0e0ee4b88f 100644 --- a/app/Models/BaseModel.php +++ b/app/Models/BaseModel.php @@ -191,7 +191,9 @@ class BaseModel extends Model public function numberFormatter() { - $formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $this->number); + $number = strlen($this->number) > 1 ? $this->number : class_basename($this); + + $formatted_number = mb_ereg_replace("([^\w\s\d\-_~,;\[\]\(\).])", '', $number); // Remove any runs of periods (thanks falstro!) $formatted_number = mb_ereg_replace("([\.]{2,})", '', $formatted_number); From 90540d64038b42258b2cc29c56f12eb5f5e115f7 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 09:47:05 +1100 Subject: [PATCH 03/12] Refresh entity prior to sending --- app/Http/Controllers/EmailController.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Http/Controllers/EmailController.php b/app/Http/Controllers/EmailController.php index f53f47348ef8..cdd13ea89094 100644 --- a/app/Http/Controllers/EmailController.php +++ b/app/Http/Controllers/EmailController.php @@ -131,7 +131,7 @@ class EmailController extends BaseController $entity_obj->service()->markSent()->save(); - EmailEntity::dispatch($invitation, $invitation->company, $template, $data) + EmailEntity::dispatch($invitation->fresh(), $invitation->company, $template, $data) ->delay(now()->addSeconds(5)); } From 946ab58f135ac69fa8cb4fb9a1a7711c73069312 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 10:43:21 +1100 Subject: [PATCH 04/12] Fixes for subscriptions --- .../BillingSubscriptionService.php | 4 +-- .../BillingSubscriptionTransformer.php | 4 ++- ...ble_constraint_to_recurring_invoice_id.php | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) create mode 100644 database/migrations/2021_03_23_233844_add_nullable_constraint_to_recurring_invoice_id.php diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index 8b6591219b73..acb39acf4635 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/BillingSubscription/BillingSubscriptionService.php @@ -46,7 +46,7 @@ class BillingSubscriptionService public function completePurchase(PaymentHash $payment_hash) { - if (!property_exists($payment_hash, 'billing_context')) { + if (!property_exists($payment_hash->data, 'billing_context')) { throw new \Exception("Illegal entrypoint into method, payload must contain billing context"); } @@ -79,7 +79,7 @@ class BillingSubscriptionService $cs->subscription_id = $this->billing_subscription->id; $cs->company_id = $this->billing_subscription->company_id; $cs->trial_started = time(); - $cs->trial_duration = time() + $this->billing_subscription->trial_duration; + $cs->trial_ends = time() + $this->billing_subscription->trial_duration; $cs->quantity = $data['quantity']; $cs->client_id = $contact->client->id; $cs->save(); diff --git a/app/Transformers/BillingSubscriptionTransformer.php b/app/Transformers/BillingSubscriptionTransformer.php index 3dc50cd78235..919889dd96a6 100644 --- a/app/Transformers/BillingSubscriptionTransformer.php +++ b/app/Transformers/BillingSubscriptionTransformer.php @@ -34,6 +34,8 @@ class BillingSubscriptionTransformer extends EntityTransformer public function transform(BillingSubscription $billing_subscription): array { + $std = new stdClass; + return [ 'id' => $this->encodePrimaryKey($billing_subscription->id), 'user_id' => $this->encodePrimaryKey($billing_subscription->user_id), @@ -56,7 +58,7 @@ class BillingSubscriptionTransformer extends EntityTransformer 'allow_plan_changes' => (bool)$billing_subscription->allow_plan_changes, 'plan_map' => (string)$billing_subscription->plan_map, 'refund_period' => (int)$billing_subscription->refund_period, - 'webhook_configuration' => (string)$billing_subscription->webhook_configuration, + 'webhook_configuration' => $billing_subscription->webhook_configuration ?: $std, 'purchase_page' => (string)route('client.subscription.purchase', $billing_subscription->hashed_id), 'is_deleted' => (bool)$billing_subscription->is_deleted, 'created_at' => (int)$billing_subscription->created_at, diff --git a/database/migrations/2021_03_23_233844_add_nullable_constraint_to_recurring_invoice_id.php b/database/migrations/2021_03_23_233844_add_nullable_constraint_to_recurring_invoice_id.php new file mode 100644 index 000000000000..f1224c7cdb1a --- /dev/null +++ b/database/migrations/2021_03_23_233844_add_nullable_constraint_to_recurring_invoice_id.php @@ -0,0 +1,29 @@ +unsignedInteger('recurring_invoice_id')->nullable()->change(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +} From db4f8946e4f0c11d1b528164da183cb6a0b9ba9e Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 12:12:37 +1100 Subject: [PATCH 05/12] Add in required use --- app/Transformers/BillingSubscriptionTransformer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/Transformers/BillingSubscriptionTransformer.php b/app/Transformers/BillingSubscriptionTransformer.php index 919889dd96a6..74ac5484ffe3 100644 --- a/app/Transformers/BillingSubscriptionTransformer.php +++ b/app/Transformers/BillingSubscriptionTransformer.php @@ -34,7 +34,7 @@ class BillingSubscriptionTransformer extends EntityTransformer public function transform(BillingSubscription $billing_subscription): array { - $std = new stdClass; + $std = new \stdClass; return [ 'id' => $this->encodePrimaryKey($billing_subscription->id), From 41de6a4062944e6e93a4547688d2ffc244acef84 Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 14:20:51 +1100 Subject: [PATCH 06/12] Fixes for notifications --- app/DataMapper/CompanySettings.php | 3 ++- .../Traits/Notifications/UserNotifies.php | 22 +++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index ce9b344e323e..ea342769bd7e 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -574,7 +574,8 @@ class CompanySettings extends BaseSettings public static function notificationDefaults() :stdClass { $notification = new stdClass; - $notification->email = ['all_notifications']; + $notification->email = []; + // $notification->email = ['all_notifications']; return $notification; } diff --git a/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index 91455c16ea5c..9710ee0282b7 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -30,14 +30,22 @@ trait UserNotifies $notifications = $company_user->notifications; //if a user owns this record or is assigned to it, they are attached the permission for notification. - if ($invitation->{$entity_name}->user_id == $company_user->_user_id || $invitation->{$entity_name}->assigned_user_id == $company_user->user_id) { - array_push($required_permissions, 'all_user_notifications'); - } + // if ($invitation->{$entity_name}->user_id == $company_user->_user_id || $invitation->{$entity_name}->assigned_user_id == $company_user->user_id) { + // array_push($required_permissions, 'all_user_notifications'); + // } +nlog($notifications); +nlog($required_permissions); +nlog($notifications->email); +nlog(count(array_intersect($required_permissions, $notifications->email))); +nlog(count(array_intersect(['all_user_notifications'], $notifications->email))); +nlog(count(array_intersect(['all_notifications'],$notifications->email))); - if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect($required_permissions, ['all_user_notifications'])) >= 1 || count(array_intersect($required_permissions, ['all_notifications'])) >= 1) { + if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect(['all_user_notifications'],$notifications->email)) >= 1 || count(array_intersect(['all_notifications'],$notifications->email)) >= 1) { array_push($notifiable_methods, 'mail'); } +nlog($notifiable_methods); + // if(count(array_intersect($required_permissions, $notifications->slack)) >=1) // array_push($notifiable_methods, 'slack'); @@ -57,9 +65,9 @@ trait UserNotifies return []; } - if ($entity->user_id == $company_user->_user_id || $entity->assigned_user_id == $company_user->user_id) { - array_push($required_permissions, 'all_user_notifications'); - } + // if ($entity->user_id == $company_user->_user_id || $entity->assigned_user_id == $company_user->user_id) { + // array_push($required_permissions, 'all_user_notifications'); + // } if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect($required_permissions, ['all_user_notifications'])) >= 1 || count(array_intersect($required_permissions, ['all_notifications'])) >= 1) { array_push($notifiable_methods, 'mail'); From f1ced66226e551f85841bd2e4872d39b4e0d15aa Mon Sep 17 00:00:00 2001 From: = Date: Wed, 24 Mar 2021 14:26:51 +1100 Subject: [PATCH 07/12] Fixes for notifications --- .../Credit/CreditEmailedNotification.php | 2 +- .../Invoice/InvoiceEmailedNotification.php | 2 +- .../InvoiceFailedEmailNotification.php | 2 +- .../Quote/QuoteEmailedNotification.php | 2 +- .../Traits/Notifications/UserNotifies.php | 20 +++++++------------ 5 files changed, 11 insertions(+), 17 deletions(-) diff --git a/app/Listeners/Credit/CreditEmailedNotification.php b/app/Listeners/Credit/CreditEmailedNotification.php index dd7f3bd5e06f..48dcf314623e 100644 --- a/app/Listeners/Credit/CreditEmailedNotification.php +++ b/app/Listeners/Credit/CreditEmailedNotification.php @@ -54,7 +54,7 @@ class CreditEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'credit'); - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'credit', ['all_notifications', 'credit_sent']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'credit', ['all_notifications', 'credit_sent', 'credit_sent_all']); if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); diff --git a/app/Listeners/Invoice/InvoiceEmailedNotification.php b/app/Listeners/Invoice/InvoiceEmailedNotification.php index 1bffd1d37649..4a3b7f77e64f 100644 --- a/app/Listeners/Invoice/InvoiceEmailedNotification.php +++ b/app/Listeners/Invoice/InvoiceEmailedNotification.php @@ -60,7 +60,7 @@ class InvoiceEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'invoice'); /* Returns an array of notification methods */ - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all']); /* If one of the methods is email then we fire the EntitySentMailer */ if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { diff --git a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php index aeda48fbfb62..d235b4a95cc2 100644 --- a/app/Listeners/Invoice/InvoiceFailedEmailNotification.php +++ b/app/Listeners/Invoice/InvoiceFailedEmailNotification.php @@ -56,7 +56,7 @@ class InvoiceFailedEmailNotification // $notification = new EntitySentNotification($event->invitation, 'invoice'); - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'invoice', ['all_notifications', 'invoice_sent', 'invoice_sent_all']); if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); diff --git a/app/Listeners/Quote/QuoteEmailedNotification.php b/app/Listeners/Quote/QuoteEmailedNotification.php index 7cb0995f638d..43ec02df255c 100644 --- a/app/Listeners/Quote/QuoteEmailedNotification.php +++ b/app/Listeners/Quote/QuoteEmailedNotification.php @@ -55,7 +55,7 @@ class QuoteEmailedNotification implements ShouldQueue // $notification = new EntitySentNotification($event->invitation, 'quote'); - $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'quote', ['all_notifications', 'quote_sent']); + $methods = $this->findUserNotificationTypes($event->invitation, $company_user, 'quote', ['all_notifications', 'quote_sent', 'quote_sent_all']); if (($key = array_search('mail', $methods)) !== false && $first_notification_sent === true) { unset($methods[$key]); diff --git a/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index 9710ee0282b7..efff7001004f 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -30,17 +30,11 @@ trait UserNotifies $notifications = $company_user->notifications; //if a user owns this record or is assigned to it, they are attached the permission for notification. - // if ($invitation->{$entity_name}->user_id == $company_user->_user_id || $invitation->{$entity_name}->assigned_user_id == $company_user->user_id) { - // array_push($required_permissions, 'all_user_notifications'); - // } -nlog($notifications); -nlog($required_permissions); -nlog($notifications->email); -nlog(count(array_intersect($required_permissions, $notifications->email))); -nlog(count(array_intersect(['all_user_notifications'], $notifications->email))); -nlog(count(array_intersect(['all_notifications'],$notifications->email))); + if ($invitation->{$entity_name}->user_id == $company_user->_user_id || $invitation->{$entity_name}->assigned_user_id == $company_user->user_id) { + array_push($required_permissions, 'all_user_notifications'); + } - if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect(['all_user_notifications'],$notifications->email)) >= 1 || count(array_intersect(['all_notifications'],$notifications->email)) >= 1) { + if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect(['all_user_notifications'], $notifications->email)) >= 1 || count(array_intersect(['all_notifications'],$notifications->email)) >= 1) { array_push($notifiable_methods, 'mail'); } @@ -65,9 +59,9 @@ nlog($notifiable_methods); return []; } - // if ($entity->user_id == $company_user->_user_id || $entity->assigned_user_id == $company_user->user_id) { - // array_push($required_permissions, 'all_user_notifications'); - // } + if ($entity->user_id == $company_user->_user_id || $entity->assigned_user_id == $company_user->user_id) { + array_push($required_permissions, 'all_user_notifications'); + } if (count(array_intersect($required_permissions, $notifications->email)) >= 1 || count(array_intersect($required_permissions, ['all_user_notifications'])) >= 1 || count(array_intersect($required_permissions, ['all_notifications'])) >= 1) { array_push($notifiable_methods, 'mail'); From 07eb89acfe9c59a0c71d6b74ef0e5a6873bf012b Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 15:37:32 +1100 Subject: [PATCH 08/12] Add with trasheD --- app/Http/Controllers/UserController.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/UserController.php b/app/Http/Controllers/UserController.php index 202354eb4c54..c9386d194d0b 100644 --- a/app/Http/Controllers/UserController.php +++ b/app/Http/Controllers/UserController.php @@ -612,7 +612,9 @@ class UserController extends BaseController return response()->json(['message', 'Cannot detach owner.'],400); $company_user = CompanyUser::whereUserId($user->id) - ->whereCompanyId(auth()->user()->companyId())->first(); + ->whereCompanyId(auth()->user()->companyId()) + ->withTrashed() + ->first(); $token = $company_user->token->where('company_id', $company_user->company_id)->where('user_id', $company_user->user_id)->first(); From 103fa1b974acdb79b93709516a7c2e8a43a91e15 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 19:48:54 +1100 Subject: [PATCH 09/12] Rename BillingSubscriptions to Subscriptions --- app/Console/Kernel.php | 4 +- ...pper.php => SubscriptionContextMapper.php} | 6 +- app/DataMapper/CompanySettings.php | 2 +- .../SubscriptionWasCreated.php} | 14 +- ...ionFactory.php => SubscriptionFactory.php} | 9 +- .../BillingSubscriptionPurchaseController.php | 8 +- .../OpenAPI/BillingSubscription.php | 2 +- ...troller.php => SubscriptionController.php} | 194 +++++++++--------- app/Http/Livewire/BillingPortalPurchase.php | 30 +-- .../CreateSubscriptionRequest.php} | 8 +- .../DestroySubscriptionRequest.php} | 6 +- .../EditSubscriptionRequest.php} | 6 +- .../ShowSubscriptionRequest.php} | 6 +- .../StoreSubscriptionRequest.php} | 8 +- .../UpdateSubscriptionRequest.php} | 6 +- ...scriptionCron.php => SubscriptionCron.php} | 15 +- app/Models/Company.php | 11 +- ...llingSubscription.php => Subscription.php} | 6 +- ...nObserver.php => SubscriptionObserver.php} | 24 +-- app/PaymentDrivers/BaseDriver.php | 6 +- ...ptionPolicy.php => SubscriptionPolicy.php} | 6 +- app/Providers/AppServiceProvider.php | 6 +- app/Providers/AuthServiceProvider.php | 8 +- ...ository.php => SubscriptionRepository.php} | 12 +- .../SubscriptionService.php} | 54 ++--- .../BillingSubscriptionTransformer.php | 76 ------- app/Transformers/CompanyTransformer.php | 17 +- app/Transformers/SubscriptionTransformer.php | 69 +++++++ ...ionFactory.php => SubscriptionFactory.php} | 6 +- ...2025_refactor_billing_scriptions_table.php | 29 +++ routes/api.php | 2 +- ...ionApiTest.php => SubscriptionApiTest.php} | 39 ++-- 32 files changed, 353 insertions(+), 342 deletions(-) rename app/DataMapper/Billing/{BillingContextMapper.php => SubscriptionContextMapper.php} (85%) rename app/Events/{BillingSubscription/BillingSubscriptionWasCreated.php => Subscription/SubscriptionWasCreated.php} (72%) rename app/Factory/{BillingSubscriptionFactory.php => SubscriptionFactory.php} (77%) rename app/Http/Controllers/{BillingSubscriptionController.php => SubscriptionController.php} (61%) rename app/Http/Requests/{BillingSubscription/CreateBillingSubscriptionRequest.php => Subscription/CreateSubscriptionRequest.php} (75%) rename app/Http/Requests/{BillingSubscription/EditBillingSubscriptionRequest.php => Subscription/DestroySubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/DestroyBillingSubscriptionRequest.php => Subscription/EditSubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/ShowBillingSubscriptionRequest.php => Subscription/ShowSubscriptionRequest.php} (79%) rename app/Http/Requests/{BillingSubscription/StoreBillingSubscriptionRequest.php => Subscription/StoreSubscriptionRequest.php} (88%) rename app/Http/Requests/{BillingSubscription/UpdateBillingSubscriptionRequest.php => Subscription/UpdateSubscriptionRequest.php} (80%) rename app/Jobs/Cron/{BillingSubscriptionCron.php => SubscriptionCron.php} (76%) rename app/Models/{BillingSubscription.php => Subscription.php} (91%) rename app/Observers/{BillingSubscriptionObserver.php => SubscriptionObserver.php} (56%) rename app/Policies/{BillingSubscriptionPolicy.php => SubscriptionPolicy.php} (79%) rename app/Repositories/{BillingSubscriptionRepository.php => SubscriptionRepository.php} (58%) rename app/Services/{BillingSubscription/BillingSubscriptionService.php => Subscription/SubscriptionService.php} (78%) delete mode 100644 app/Transformers/BillingSubscriptionTransformer.php create mode 100644 app/Transformers/SubscriptionTransformer.php rename database/factories/{BillingSubscriptionFactory.php => SubscriptionFactory.php} (81%) create mode 100644 database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php rename tests/Feature/{BillingSubscriptionApiTest.php => SubscriptionApiTest.php} (68%) diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 0c908d25a7ee..2cf0371b23e4 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -11,7 +11,7 @@ namespace App\Console; -use App\Jobs\Cron\BillingSubscriptionCron; +use App\Jobs\Cron\SubscriptionCron; use App\Jobs\Cron\RecurringInvoicesCron; use App\Jobs\Ninja\AdjustEmailQuota; use App\Jobs\Ninja\CompanySizeCheck; @@ -54,7 +54,7 @@ class Kernel extends ConsoleKernel $schedule->job(new UpdateExchangeRates)->daily()->withoutOverlapping(); - $schedule->job(new BillingSubscriptionCron)->daily()->withoutOverlapping(); + $schedule->job(new SubscriptionCron)->daily()->withoutOverlapping(); $schedule->job(new RecurringInvoicesCron)->hourly()->withoutOverlapping(); diff --git a/app/DataMapper/Billing/BillingContextMapper.php b/app/DataMapper/Billing/SubscriptionContextMapper.php similarity index 85% rename from app/DataMapper/Billing/BillingContextMapper.php rename to app/DataMapper/Billing/SubscriptionContextMapper.php index 3d878f38841e..fa5aa18a34c9 100644 --- a/app/DataMapper/Billing/BillingContextMapper.php +++ b/app/DataMapper/Billing/SubscriptionContextMapper.php @@ -13,12 +13,12 @@ namespace App\DataMapper\Billing; -class BillingContextMapper +class SubscriptionContextMapper { /** * @var int */ - public $billing_subscription_id; + public $subscription_id; /** * @var string @@ -39,7 +39,7 @@ class BillingContextMapper * @var string[] */ public $casts = [ - 'billing_subscription_id' => 'integer', + 'subscription_id' => 'integer', 'email' => 'string', 'client_id' => 'integer', 'invoice_id' => 'integer', diff --git a/app/DataMapper/CompanySettings.php b/app/DataMapper/CompanySettings.php index ea342769bd7e..049f807d5743 100644 --- a/app/DataMapper/CompanySettings.php +++ b/app/DataMapper/CompanySettings.php @@ -109,7 +109,7 @@ class CompanySettings extends BaseSettings public $shared_invoice_quote_counter = false; //@implemented public $shared_invoice_credit_counter = false; //@implemented - public $recurring_number_prefix = 'R'; //@implemented + public $recurring_number_prefix = ''; //@implemented public $reset_counter_frequency_id = '0'; //@implemented public $reset_counter_date = ''; //@implemented public $counter_padding = 4; //@implemented diff --git a/app/Events/BillingSubscription/BillingSubscriptionWasCreated.php b/app/Events/Subscription/SubscriptionWasCreated.php similarity index 72% rename from app/Events/BillingSubscription/BillingSubscriptionWasCreated.php rename to app/Events/Subscription/SubscriptionWasCreated.php index 73fc2d1f2f87..67d061f6738d 100644 --- a/app/Events/BillingSubscription/BillingSubscriptionWasCreated.php +++ b/app/Events/Subscription/SubscriptionWasCreated.php @@ -1,8 +1,8 @@ billing_subscription = $billing_subscription; + $this->subscription = $subscription; $this->company = $company; $this->event_vars = $event_vars; } diff --git a/app/Factory/BillingSubscriptionFactory.php b/app/Factory/SubscriptionFactory.php similarity index 77% rename from app/Factory/BillingSubscriptionFactory.php rename to app/Factory/SubscriptionFactory.php index f9f051293e9c..6e9c912bb776 100644 --- a/app/Factory/BillingSubscriptionFactory.php +++ b/app/Factory/SubscriptionFactory.php @@ -11,14 +11,13 @@ namespace App\Factory; +use App\Models\Subscription; -use App\Models\BillingSubscription; - -class BillingSubscriptionFactory +class SubscriptionFactory { - public static function create(int $company_id, int $user_id): BillingSubscription + public static function create(int $company_id, int $user_id): Subscription { - $billing_subscription = new BillingSubscription(); + $billing_subscription = new Subscription(); $billing_subscription->company_id = $company_id; $billing_subscription->user_id = $user_id; diff --git a/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php b/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php index b6a545e96b53..d39d8ca36111 100644 --- a/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php +++ b/app/Http/Controllers/ClientPortal/BillingSubscriptionPurchaseController.php @@ -13,23 +13,23 @@ namespace App\Http\Controllers\ClientPortal; use App\Http\Controllers\Controller; -use App\Models\BillingSubscription; +use App\Models\Subscription; use Illuminate\Http\Request; use Illuminate\Support\Facades\App; use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\DB; use Illuminate\Support\Str; -class BillingSubscriptionPurchaseController extends Controller +class SubscriptionPurchaseController extends Controller { - public function index(BillingSubscription $billing_subscription, Request $request) + public function index(Subscription $subscription, Request $request) { if ($request->has('locale')) { $this->setLocale($request->query('locale')); } return view('billing-portal.purchase', [ - 'billing_subscription' => $billing_subscription, + 'subscription' => $subscription, 'hash' => Str::uuid()->toString(), 'request_data' => $request->all(), ]); diff --git a/app/Http/Controllers/OpenAPI/BillingSubscription.php b/app/Http/Controllers/OpenAPI/BillingSubscription.php index fad9b1eac32f..c088160781d6 100644 --- a/app/Http/Controllers/OpenAPI/BillingSubscription.php +++ b/app/Http/Controllers/OpenAPI/BillingSubscription.php @@ -1,7 +1,7 @@ billing_subscription_repo = $billing_subscription_repo; + $this->subscription_repo = $subscription_repo; } /** - * Show the list of BillingSubscriptions. + * Show the list of Subscriptions. * * @return Response * * @OA\Get( - * path="/api/v1/billing_subscriptions", - * operationId="getBillingSubscriptions", - * tags={"billing_subscriptions"}, - * summary="Gets a list of billing_subscriptions", - * description="Lists billing_subscriptions.", + * path="/api/v1/subscriptions", + * operationId="getSubscriptions", + * tags={"subscriptions"}, + * summary="Gets a list of subscriptions", + * description="Lists subscriptions.", * * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), @@ -58,11 +58,11 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="A list of billing_subscriptions", + * description="A list of subscriptions", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -79,24 +79,24 @@ class BillingSubscriptionController extends BaseController public function index(): \Illuminate\Http\Response { - $billing_subscriptions = BillingSubscription::query()->company(); + $subscriptions = Subscription::query()->company(); - return $this->listResponse($billing_subscriptions); + return $this->listResponse($subscriptions); } /** * Show the form for creating a new resource. * - * @param CreateBillingSubscriptionRequest $request The request + * @param CreateSubscriptionRequest $request The request * * @return Response * * * @OA\Get( - * path="/api/v1/billing_subscriptions/create", - * operationId="getBillingSubscriptionsCreate", - * tags={"billing_subscriptions"}, - * summary="Gets a new blank billing_subscriptions object", + * path="/api/v1/subscriptions/create", + * operationId="getSubscriptionsCreate", + * tags={"subscriptions"}, + * summary="Gets a new blank subscriptions object", * description="Returns a blank object with default values", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), @@ -104,11 +104,11 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="A blank billing_subscriptions object", + * description="A blank subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -123,38 +123,38 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function create(CreateBillingSubscriptionRequest $request): \Illuminate\Http\Response + public function create(CreateSubscriptionRequest $request): \Illuminate\Http\Response { - $billing_subscription = BillingSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); + $subscription = SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Store a newly created resource in storage. * - * @param StoreBillingSubscriptionRequest $request The request + * @param StoreSubscriptionRequest $request The request * * @return Response * * * @OA\Post( - * path="/api/v1/billing_subscriptions", - * operationId="storeBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Adds a billing_subscriptions", - * description="Adds an billing_subscriptions to the system", + * path="/api/v1/subscriptions", + * operationId="storeSubscription", + * tags={"subscriptions"}, + * summary="Adds a subscriptions", + * description="Adds an subscriptions to the system", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), * @OA\Parameter(ref="#/components/parameters/include"), * @OA\Response( * response=200, - * description="Returns the saved billing_subscriptions object", + * description="Returns the saved subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -169,30 +169,30 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function store(StoreBillingSubscriptionRequest $request): \Illuminate\Http\Response + public function store(StoreSubscriptionRequest $request): \Illuminate\Http\Response { - $billing_subscription = $this->billing_subscription_repo->save($request->all(), BillingSubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id)); + $subscription = $this->subscription_repo->save($request->all(), SubscriptionFactory::create(auth()->user()->company()->id, auth()->user()->id)); - event(new BillingsubscriptionWasCreated($billing_subscription, $billing_subscription->company, Ninja::eventVars())); + event(new SubscriptionWasCreated($subscription, $subscription->company, Ninja::eventVars())); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Display the specified resource. * - * @param ShowBillingSubscriptionRequest $request The request - * @param Invoice $billing_subscription The invoice + * @param ShowSubscriptionRequest $request The request + * @param Invoice $subscription The invoice * * @return Response * * * @OA\Get( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="showBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Shows an billing_subscriptions", - * description="Displays an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="showSubscription", + * tags={"subscriptions"}, + * summary="Shows an subscriptions", + * description="Displays an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -200,7 +200,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -210,11 +210,11 @@ class BillingSubscriptionController extends BaseController * ), * @OA\Response( * response=200, - * description="Returns the BillingSubscription object", + * description="Returns the Subscription object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -229,25 +229,25 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function show(ShowBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function show(ShowSubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Show the form for editing the specified resource. * - * @param EditBillingSubscriptionRequest $request The request - * @param Invoice $billing_subscription The invoice + * @param EditSubscriptionRequest $request The request + * @param Invoice $subscription The invoice * * @return Response * * @OA\Get( - * path="/api/v1/billing_subscriptions/{id}/edit", - * operationId="editBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Shows an billing_subscriptions for editting", - * description="Displays an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}/edit", + * operationId="editSubscription", + * tags={"subscriptions"}, + * summary="Shows an subscriptions for editting", + * description="Displays an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -255,7 +255,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -269,7 +269,7 @@ class BillingSubscriptionController extends BaseController * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -284,26 +284,26 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function edit(EditBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function edit(EditSubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Update the specified resource in storage. * - * @param UpdateBillingSubscriptionRequest $request The request - * @param BillingSubscription $billing_subscription The invoice + * @param UpdateSubscriptionRequest $request The request + * @param Subscription $subscription The invoice * * @return Response * * * @OA\Put( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="updateBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Updates an billing_subscriptions", - * description="Handles the updating of an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="updateSubscription", + * tags={"subscriptions"}, + * summary="Updates an subscriptions", + * description="Handles the updating of an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -311,7 +311,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -321,11 +321,11 @@ class BillingSubscriptionController extends BaseController * ), * @OA\Response( * response=200, - * description="Returns the billing_subscriptions object", + * description="Returns the subscriptions object", * @OA\Header(header="X-MINIMUM-CLIENT-VERSION", ref="#/components/headers/X-MINIMUM-CLIENT-VERSION"), * @OA\Header(header="X-RateLimit-Remaining", ref="#/components/headers/X-RateLimit-Remaining"), * @OA\Header(header="X-RateLimit-Limit", ref="#/components/headers/X-RateLimit-Limit"), - * @OA\JsonContent(ref="#/components/schemas/BillingSubscription"), + * @OA\JsonContent(ref="#/components/schemas/Subscription"), * ), * @OA\Response( * response=422, @@ -340,32 +340,32 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function update(UpdateBillingSubscriptionRequest $request, BillingSubscription $billing_subscription) + public function update(UpdateSubscriptionRequest $request, Subscription $subscription) { - if ($request->entityIsDeleted($billing_subscription)) { + if ($request->entityIsDeleted($subscription)) { return $request->disallowUpdate(); } - $billing_subscription = $this->billing_subscription_repo->save($request->all(), $billing_subscription); + $subscription = $this->subscription_repo->save($request->all(), $subscription); - return $this->itemResponse($billing_subscription); + return $this->itemResponse($subscription); } /** * Remove the specified resource from storage. * - * @param DestroyBillingSubscriptionRequest $request - * @param BillingSubscription $invoice + * @param DestroySubscriptionRequest $request + * @param Subscription $invoice * * @return Response * * @throws \Exception * @OA\Delete( - * path="/api/v1/billing_subscriptions/{id}", - * operationId="deleteBillingSubscription", - * tags={"billing_subscriptions"}, - * summary="Deletes a billing_subscriptions", - * description="Handles the deletion of an billing_subscriptions by id", + * path="/api/v1/subscriptions/{id}", + * operationId="deleteSubscription", + * tags={"subscriptions"}, + * summary="Deletes a subscriptions", + * description="Handles the deletion of an subscriptions by id", * @OA\Parameter(ref="#/components/parameters/X-Api-Secret"), * @OA\Parameter(ref="#/components/parameters/X-Api-Token"), * @OA\Parameter(ref="#/components/parameters/X-Requested-With"), @@ -373,7 +373,7 @@ class BillingSubscriptionController extends BaseController * @OA\Parameter( * name="id", * in="path", - * description="The BillingSubscription Hashed ID", + * description="The Subscription Hashed ID", * example="D2J234DFA", * required=true, * @OA\Schema( @@ -401,10 +401,10 @@ class BillingSubscriptionController extends BaseController * ), * ) */ - public function destroy(DestroyBillingSubscriptionRequest $request, BillingSubscription $billing_subscription): \Illuminate\Http\Response + public function destroy(DestroySubscriptionRequest $request, Subscription $subscription): \Illuminate\Http\Response { - $this->billing_subscription_repo->delete($billing_subscription); + $this->subscription_repo->delete($subscription); - return $this->itemResponse($billing_subscription->fresh()); + return $this->itemResponse($subscription->fresh()); } } diff --git a/app/Http/Livewire/BillingPortalPurchase.php b/app/Http/Livewire/BillingPortalPurchase.php index 6529c4cff065..ab4dc76cb341 100644 --- a/app/Http/Livewire/BillingPortalPurchase.php +++ b/app/Http/Livewire/BillingPortalPurchase.php @@ -12,7 +12,7 @@ namespace App\Http\Livewire; use App\Factory\ClientFactory; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\ClientContact; use App\Models\Invoice; use App\Repositories\ClientContactRepository; @@ -55,11 +55,11 @@ class BillingPortalPurchase extends Component public $password; /** - * Instance of billing subscription. + * Instance of subscription. * - * @var BillingSubscription + * @var Subscription */ - public $billing_subscription; + public $subscription; /** * Instance of client contact. @@ -186,8 +186,8 @@ class BillingPortalPurchase extends Component */ protected function createBlankClient() { - $company = $this->billing_subscription->company; - $user = $this->billing_subscription->user; + $company = $this->subscription->company; + $user = $this->subscription->user; $client_repo = new ClientRepository(new ClientContactRepository()); @@ -224,7 +224,7 @@ class BillingPortalPurchase extends Component */ protected function getPaymentMethods(ClientContact $contact): self { - if ($this->billing_subscription->trial_enabled) { + if ($this->subscription->trial_enabled) { $this->heading_text = ctrans('texts.plan_trial'); $this->steps['show_start_trial'] = true; @@ -274,11 +274,11 @@ class BillingPortalPurchase extends Component 'client_contact_id' => $this->contact->hashed_id, ]], 'user_input_promo_code' => $this->coupon, - 'coupon' => empty($this->billing_subscription->promo_code) ? '' : $this->coupon, + 'coupon' => empty($this->subscription->promo_code) ? '' : $this->coupon, 'quantity' => $this->quantity, ]; - $this->invoice = $this->billing_subscription + $this->invoice = $this->subscription ->service() ->createInvoice($data) ->service() @@ -287,12 +287,12 @@ class BillingPortalPurchase extends Component ->save(); Cache::put($this->hash, [ - 'billing_subscription_id' => $this->billing_subscription->id, + 'subscription_id' => $this->subscription->id, 'email' => $this->email ?? $this->contact->email, 'client_id' => $this->contact->client->id, 'invoice_id' => $this->invoice->id, 'quantity' => $this->quantity, - 'subscription_id' => $this->billing_subscription->id, + 'subscription_id' => $this->subscription->id, now()->addMinutes(60)] ); @@ -306,7 +306,7 @@ class BillingPortalPurchase extends Component */ public function handleTrial() { - return $this->billing_subscription->service()->startTrial([ + return $this->subscription->service()->startTrial([ 'email' => $this->email ?? $this->contact->email, 'quantity' => $this->quantity, 'contact_id' => $this->contact->id, @@ -325,17 +325,17 @@ class BillingPortalPurchase extends Component return $this->quantity; } - if ($this->quantity >= $this->billing_subscription->max_seats_limit && $option == 'increment') { + if ($this->quantity >= $this->subscription->max_seats_limit && $option == 'increment') { return $this->quantity; } if ($option == 'increment') { $this->quantity++; - return $this->price = (int)$this->price + $this->billing_subscription->product->price; + return $this->price = (int)$this->price + $this->subscription->product->price; } $this->quantity--; - $this->price = (int)$this->price - $this->billing_subscription->product->price; + $this->price = (int)$this->price - $this->subscription->product->price; return 0; } diff --git a/app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/CreateSubscriptionRequest.php similarity index 75% rename from app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/CreateSubscriptionRequest.php index f4dfbe25d4b0..c3572cb7be1f 100644 --- a/app/Http/Requests/BillingSubscription/CreateBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/CreateSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class CreateBillingSubscriptionRequest extends Request +class CreateSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class CreateBillingSubscriptionRequest extends Request */ public function authorize(): bool { - return auth()->user()->can('create', BillingSubscription::class); + return auth()->user()->can('create', Subscription::class); } /** diff --git a/app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/DestroySubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/DestroySubscriptionRequest.php index 94d285770b65..7cb4e16e51c4 100644 --- a/app/Http/Requests/BillingSubscription/EditBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/DestroySubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class EditBillingSubscriptionRequest extends Request +class DestroySubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class EditBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/EditSubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/EditSubscriptionRequest.php index 399cdceb2f0b..aae8d1ea10e5 100644 --- a/app/Http/Requests/BillingSubscription/DestroyBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/EditSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class DestroyBillingSubscriptionRequest extends Request +class EditSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class DestroyBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/ShowSubscriptionRequest.php similarity index 79% rename from app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/ShowSubscriptionRequest.php index 18ba6ab68f7d..1b24c7779018 100644 --- a/app/Http/Requests/BillingSubscription/ShowBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/ShowSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; -class ShowBillingSubscriptionRequest extends Request +class ShowSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class ShowBillingSubscriptionRequest extends Request */ public function authorize() : bool { - return auth()->user()->can('view', $this->billing_subscription); + return auth()->user()->can('view', $this->subscription); } /** diff --git a/app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php similarity index 88% rename from app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/StoreSubscriptionRequest.php index 0c5a64c2ead6..e142d4578f1b 100644 --- a/app/Http/Requests/BillingSubscription/StoreBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/StoreSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class StoreBillingSubscriptionRequest extends Request +class StoreSubscriptionRequest extends Request { /** * Determine if the user is authorized to make this request. @@ -23,7 +23,7 @@ class StoreBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('create', BillingSubscription::class); + return auth()->user()->can('create', Subscription::class); } /** diff --git a/app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php b/app/Http/Requests/Subscription/UpdateSubscriptionRequest.php similarity index 80% rename from app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php rename to app/Http/Requests/Subscription/UpdateSubscriptionRequest.php index 78d41deb35e6..4b7a0c4bbe46 100644 --- a/app/Http/Requests/BillingSubscription/UpdateBillingSubscriptionRequest.php +++ b/app/Http/Requests/Subscription/UpdateSubscriptionRequest.php @@ -9,12 +9,12 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Http\Requests\BillingSubscription; +namespace App\Http\Requests\Subscription; use App\Http\Requests\Request; use App\Utils\Traits\ChecksEntityStatus; -class UpdateBillingSubscriptionRequest extends Request +class UpdateSubscriptionRequest extends Request { use ChecksEntityStatus; @@ -25,7 +25,7 @@ class UpdateBillingSubscriptionRequest extends Request */ public function authorize() { - return auth()->user()->can('edit', $this->billing_subscription); + return auth()->user()->can('edit', $this->subscription); } /** diff --git a/app/Jobs/Cron/BillingSubscriptionCron.php b/app/Jobs/Cron/SubscriptionCron.php similarity index 76% rename from app/Jobs/Cron/BillingSubscriptionCron.php rename to app/Jobs/Cron/SubscriptionCron.php index 3aef58c290cd..1fdd1812d82f 100644 --- a/app/Jobs/Cron/BillingSubscriptionCron.php +++ b/app/Jobs/Cron/SubscriptionCron.php @@ -12,11 +12,10 @@ namespace App\Jobs\Cron; use App\Libraries\MultiDB; -use App\Models\ClientSubscription; use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Support\Carbon; -class BillingSubscriptionCron +class SubscriptionCron { use Dispatchable; @@ -52,11 +51,13 @@ class BillingSubscriptionCron private function loopSubscriptions() { - $client_subs = ClientSubscription::whereNull('deleted_at') - ->cursor() - ->each(function ($cs){ - $this->processSubscription($cs); - }); + //looop recurring invoices with subscription id + + // $client_subs = ClientSubscription::whereNull('deleted_at') + // ->cursor() + // ->each(function ($cs){ + // $this->processSubscription($cs); + // }); } /* Our daily cron should check diff --git a/app/Models/Company.php b/app/Models/Company.php index 1982f6a3c72c..e2edb0d28314 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -159,15 +159,10 @@ class Company extends BaseModel { return $this->hasMany(ExpenseCategory::class)->withTrashed(); } - - public function client_subscriptions() + + public function subscriptions() { - return $this->hasMany(ClientSubscription::class)->withTrashed(); - } - - public function billing_subscriptions() - { - return $this->hasMany(BillingSubscription::class)->withTrashed(); + return $this->hasMany(Subscription::class)->withTrashed(); } public function task_statuses() diff --git a/app/Models/BillingSubscription.php b/app/Models/Subscription.php similarity index 91% rename from app/Models/BillingSubscription.php rename to app/Models/Subscription.php index 50b68eae796f..7b588bddfc97 100644 --- a/app/Models/BillingSubscription.php +++ b/app/Models/Subscription.php @@ -11,12 +11,12 @@ namespace App\Models; -use App\Services\BillingSubscription\BillingSubscriptionService; +use App\Services\Subscription\SubscriptionService; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\SoftDeletes; -class BillingSubscription extends BaseModel +class Subscription extends BaseModel { use HasFactory, SoftDeletes; @@ -56,7 +56,7 @@ class BillingSubscription extends BaseModel public function service() { - return new BillingSubscriptionService($this); + return new SubscriptionService($this); } public function company(): \Illuminate\Database\Eloquent\Relations\BelongsTo diff --git a/app/Observers/BillingSubscriptionObserver.php b/app/Observers/SubscriptionObserver.php similarity index 56% rename from app/Observers/BillingSubscriptionObserver.php rename to app/Observers/SubscriptionObserver.php index dc813c732544..7afa2f4c8da1 100644 --- a/app/Observers/BillingSubscriptionObserver.php +++ b/app/Observers/SubscriptionObserver.php @@ -11,17 +11,17 @@ namespace App\Observers; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class BillingSubscriptionObserver +class SubscriptionObserver { /** * Handle the billing_subscription "created" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function created(BillingSubscription $billing_subscription) + public function created(Subscription $subscription) { // } @@ -29,10 +29,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "updated" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function updated(BillingSubscription $billing_subscription) + public function updated(Subscription $subscription) { // } @@ -40,10 +40,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "deleted" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function deleted(BillingSubscription $billing_subscription) + public function deleted(Subscription $subscription) { // } @@ -51,10 +51,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "restored" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function restored(BillingSubscription $billing_subscription) + public function restored(Subscription $subscription) { // } @@ -62,10 +62,10 @@ class BillingSubscriptionObserver /** * Handle the billing_subscription "force deleted" event. * - * @param BillingSubscription $billing_subscription + * @param Subscription $billing_subscription * @return void */ - public function forceDeleted(BillingSubscription $billing_subscription) + public function forceDeleted(Subscription $subscription) { // } diff --git a/app/PaymentDrivers/BaseDriver.php b/app/PaymentDrivers/BaseDriver.php index cb639b5e219f..18f7cb08542e 100644 --- a/app/PaymentDrivers/BaseDriver.php +++ b/app/PaymentDrivers/BaseDriver.php @@ -30,7 +30,7 @@ use App\Models\Invoice; use App\Models\Payment; use App\Models\PaymentHash; use App\Models\SystemLog; -use App\Services\BillingSubscription\BillingSubscriptionService; +use App\Services\Subscription\SubscriptionService; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use App\Utils\Traits\SystemLogTrait; @@ -242,9 +242,9 @@ class BaseDriver extends AbstractPaymentDriver event(new PaymentWasCreated($payment, $payment->company, Ninja::eventVars())); if (property_exists($this->payment_hash->data, 'billing_context')) { - $billing_subscription = \App\Models\BillingSubscription::find($this->payment_hash->data->billing_context->billing_subscription_id); + $billing_subscription = \App\Models\Subscription::find($this->payment_hash->data->billing_context->billing_subscription_id); - (new BillingSubscriptionService($billing_subscription))->completePurchase($this->payment_hash); + (new SubscriptionService($billing_subscription))->completePurchase($this->payment_hash); } return $payment->service()->applyNumber()->save(); diff --git a/app/Policies/BillingSubscriptionPolicy.php b/app/Policies/SubscriptionPolicy.php similarity index 79% rename from app/Policies/BillingSubscriptionPolicy.php rename to app/Policies/SubscriptionPolicy.php index aee6b5e190de..82f3b160651b 100644 --- a/app/Policies/BillingSubscriptionPolicy.php +++ b/app/Policies/SubscriptionPolicy.php @@ -14,9 +14,9 @@ namespace App\Policies; use App\Models\User; /** - * Class BillingSubscriptionPolicy. + * Class SubscriptionPolicy. */ -class BillingSubscriptionPolicy extends EntityPolicy +class SubscriptionPolicy extends EntityPolicy { /** * Checks if the user has create permissions. @@ -26,6 +26,6 @@ class BillingSubscriptionPolicy extends EntityPolicy */ public function create(User $user) : bool { - return $user->isAdmin() || $user->hasPermission('create_billing_subscription') || $user->hasPermission('create_all'); + return $user->isAdmin() || $user->hasPermission('create_subscription') || $user->hasPermission('create_all'); } } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index c017572692c3..058ec2835aa0 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -12,7 +12,7 @@ namespace App\Providers; use App\Models\Account; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; use App\Models\ClientSubscription; use App\Models\Company; @@ -28,7 +28,7 @@ use App\Models\Quote; use App\Models\Task; use App\Models\User; use App\Observers\AccountObserver; -use App\Observers\BillingSubscriptionObserver; +use App\Observers\SubscriptionObserver; use App\Observers\ClientObserver; use App\Observers\ClientSubscriptionObserver; use App\Observers\CompanyGatewayObserver; @@ -80,7 +80,7 @@ class AppServiceProvider extends ServiceProvider Schema::defaultStringLength(191); Account::observe(AccountObserver::class); - BillingSubscription::observe(BillingSubscriptionObserver::class); + Subscription::observe(SubscriptionObserver::class); Client::observe(ClientObserver::class); ClientSubscription::observe(ClientSubscriptionObserver::class); Company::observe(CompanyObserver::class); diff --git a/app/Providers/AuthServiceProvider.php b/app/Providers/AuthServiceProvider.php index d71a5ae1435f..5057e976c64b 100644 --- a/app/Providers/AuthServiceProvider.php +++ b/app/Providers/AuthServiceProvider.php @@ -12,9 +12,8 @@ namespace App\Providers; use App\Models\Activity; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; -use App\Models\ClientSubscription; use App\Models\Company; use App\Models\CompanyGateway; use App\Models\CompanyToken; @@ -39,7 +38,7 @@ use App\Models\User; use App\Models\Vendor; use App\Models\Webhook; use App\Policies\ActivityPolicy; -use App\Policies\BillingSubscriptionPolicy; +use App\Policies\SubscriptionPolicy; use App\Policies\ClientPolicy; use App\Policies\ClientSubscriptionPolicy; use App\Policies\CompanyGatewayPolicy; @@ -77,9 +76,8 @@ class AuthServiceProvider extends ServiceProvider */ protected $policies = [ Activity::class => ActivityPolicy::class, - BillingSubscription::class => BillingSubscriptionPolicy::class, + Subscription::class => SubscriptionPolicy::class, Client::class => ClientPolicy::class, - ClientSubscription::class => ClientSubscriptionPolicy::class, Company::class => CompanyPolicy::class, CompanyToken::class => CompanyTokenPolicy::class, CompanyGateway::class => CompanyGatewayPolicy::class, diff --git a/app/Repositories/BillingSubscriptionRepository.php b/app/Repositories/SubscriptionRepository.php similarity index 58% rename from app/Repositories/BillingSubscriptionRepository.php rename to app/Repositories/SubscriptionRepository.php index 68be2473468d..4a7a5469facc 100644 --- a/app/Repositories/BillingSubscriptionRepository.php +++ b/app/Repositories/SubscriptionRepository.php @@ -13,16 +13,16 @@ namespace App\Repositories; -use App\Models\BillingSubscription; +use App\Models\Subscription; -class BillingSubscriptionRepository extends BaseRepository +class SubscriptionRepository extends BaseRepository { - public function save($data, BillingSubscription $billing_subscription): ?BillingSubscription + public function save($data, Subscription $subscription): ?Subscription { - $billing_subscription + $subscription ->fill($data) ->save(); - return $billing_subscription; + return $subscription; } -} +} \ No newline at end of file diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/Subscription/SubscriptionService.php similarity index 78% rename from app/Services/BillingSubscription/BillingSubscriptionService.php rename to app/Services/Subscription/SubscriptionService.php index acb39acf4635..74b83b4e667b 100644 --- a/app/Services/BillingSubscription/BillingSubscriptionService.php +++ b/app/Services/Subscription/SubscriptionService.php @@ -9,13 +9,13 @@ * @license https://opensource.org/licenses/AAL */ -namespace App\Services\BillingSubscription; +namespace App\Services\Subscription; use App\DataMapper\InvoiceItem; use App\Factory\InvoiceFactory; use App\Factory\InvoiceToRecurringInvoiceFactory; use App\Jobs\Util\SystemLogger; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\ClientContact; use App\Models\ClientSubscription; use App\Models\Invoice; @@ -27,20 +27,20 @@ use App\Utils\Traits\CleanLineItems; use App\Utils\Traits\MakesHash; use GuzzleHttp\RequestOptions; -class BillingSubscriptionService +class SubscriptionService { use MakesHash; use CleanLineItems; - /** @var billing_subscription */ - private $billing_subscription; + /** @var subscription */ + private $subscription; /** @var client_subscription */ private $client_subscription; - public function __construct(BillingSubscription $billing_subscription) + public function __construct(Subscription $subscription) { - $this->billing_subscription = $billing_subscription; + $this->subscription = $subscription; } public function completePurchase(PaymentHash $payment_hash) @@ -70,16 +70,16 @@ class BillingSubscriptionService { // Redirects from here work just fine. Livewire will respect it. - if(!$this->billing_subscription->trial_enabled) + if(!$this->subscription->trial_enabled) return new \Exception("Trials are disabled for this product"); $contact = ClientContact::with('client')->find($data['contact_id']); $cs = new ClientSubscription(); - $cs->subscription_id = $this->billing_subscription->id; - $cs->company_id = $this->billing_subscription->company_id; + $cs->subscription_id = $this->subscription->id; + $cs->company_id = $this->subscription->company_id; $cs->trial_started = time(); - $cs->trial_ends = time() + $this->billing_subscription->trial_duration; + $cs->trial_ends = time() + $this->subscription->trial_duration; $cs->quantity = $data['quantity']; $cs->client_id = $contact->client->id; $cs->save(); @@ -89,8 +89,8 @@ class BillingSubscriptionService //execute any webhooks $this->triggerWebhook(); - if(strlen($this->billing_subscription->webhook_configuration->post_purchase_url) >=1) - return redirect($this->billing_subscription->webhook_configuration->post_purchase_url); + if(strlen($this->subscription->webhook_configuration->post_purchase_url) >=1) + return redirect($this->subscription->webhook_configuration->post_purchase_url); return redirect('/client/subscription/'.$cs->hashed_id); } @@ -102,7 +102,7 @@ class BillingSubscriptionService $data['line_items'] = $this->cleanItems($this->createLineItems($data)); - return $invoice_repo->save($data, InvoiceFactory::create($this->billing_subscription->company_id, $this->billing_subscription->user_id)); + return $invoice_repo->save($data, InvoiceFactory::create($this->subscription->company_id, $this->subscription->user_id)); } @@ -115,7 +115,7 @@ class BillingSubscriptionService $line_items = []; - $product = $this->billing_subscription->product; + $product = $this->subscription->product; $item = new InvoiceItem; $item->quantity = $data['quantity']; @@ -139,7 +139,7 @@ class BillingSubscriptionService //do we have a promocode? enter this as a line item. - if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->billing_subscription->promo_code) && $this->billing_subscription->promo_discount > 0) + if(strlen($data['coupon']) >=1 && ($data['coupon'] == $this->subscription->promo_code) && $this->subscription->promo_discount > 0) $line_items[] = $this->createPromoLine($data); return $line_items; @@ -153,16 +153,16 @@ class BillingSubscriptionService private function createPromoLine($data) { - $product = $this->billing_subscription->product; + $product = $this->subscription->product; $discounted_amount = 0; $discount = 0; $amount = $data['quantity'] * $product->cost; - if ($this->billing_subscription->is_amount_discount == true) { - $discount = $this->billing_subscription->promo_discount; + if ($this->subscription->is_amount_discount == true) { + $discount = $this->subscription->promo_discount; } else { - $discount = round($amount * ($this->billing_subscription->promo_discount / 100), 2); + $discount = round($amount * ($this->subscription->promo_discount / 100), 2); } $discounted_amount = $amount - $discount; @@ -203,8 +203,8 @@ class BillingSubscriptionService //is this a recurring or one off subscription. $cs = new ClientSubscription(); - $cs->subscription_id = $this->billing_subscription->id; - $cs->company_id = $this->billing_subscription->company_id; + $cs->subscription_id = $this->subscription->id; + $cs->company_id = $this->subscription->company_id; $cs->invoice_id = $payment_hash->billing_context->invoice_id; $cs->client_id = $payment_hash->billing_context->client_id; @@ -212,10 +212,10 @@ class BillingSubscriptionService //if is_recurring //create recurring invoice from invoice - if($this->billing_subscription->is_recurring) + if($this->subscription->is_recurring) { $recurring_invoice = $this->convertInvoiceToRecurring($payment_hash); - $recurring_invoice->frequency_id = $this->billing_subscription->frequency_id; + $recurring_invoice->frequency_id = $this->subscription->frequency_id; $recurring_invoice->next_send_date = $recurring_invoice->nextDateByFrequency(now()->format('Y-m-d')); $recurring_invoice->save(); $cs->recurring_invoice_id = $recurring_invoice->id; @@ -236,15 +236,15 @@ class BillingSubscriptionService //hit the webhook to after a successful onboarding $body = [ - 'billing_subscription' => $this->billing_subscription, + 'subscription' => $this->subscription, 'client_subscription' => $this->client_subscription, 'client' => $this->client_subscription->client->toArray(), ]; - $client = new \GuzzleHttp\Client(['headers' => $this->billing_subscription->webhook_configuration->post_purchase_headers]); + $client = new \GuzzleHttp\Client(['headers' => $this->subscription->webhook_configuration->post_purchase_headers]); - $response = $client->{$this->billing_subscription->webhook_configuration->post_purchase_rest_method}($this->billing_subscription->post_purchase_url,[ + $response = $client->{$this->subscription->webhook_configuration->post_purchase_rest_method}($this->subscription->post_purchase_url,[ RequestOptions::JSON => ['body' => $body] ]); diff --git a/app/Transformers/BillingSubscriptionTransformer.php b/app/Transformers/BillingSubscriptionTransformer.php deleted file mode 100644 index 74ac5484ffe3..000000000000 --- a/app/Transformers/BillingSubscriptionTransformer.php +++ /dev/null @@ -1,76 +0,0 @@ - $this->encodePrimaryKey($billing_subscription->id), - 'user_id' => $this->encodePrimaryKey($billing_subscription->user_id), - 'product_id' => $this->encodePrimaryKey($billing_subscription->product_id), - 'assigned_user_id' => $this->encodePrimaryKey($billing_subscription->assigned_user_id), - 'company_id' => $this->encodePrimaryKey($billing_subscription->company_id), - 'is_recurring' => (bool)$billing_subscription->is_recurring, - 'frequency_id' => (string)$billing_subscription->frequency_id, - 'auto_bill' => (string)$billing_subscription->auto_bill, - 'promo_code' => (string)$billing_subscription->promo_code, - 'promo_discount' => (float)$billing_subscription->promo_discount, - 'is_amount_discount' => (bool)$billing_subscription->is_amount_discount, - 'allow_cancellation' => (bool)$billing_subscription->allow_cancellation, - 'per_seat_enabled' => (bool)$billing_subscription->per_set_enabled, - 'min_seats_limit' => (int)$billing_subscription->min_seats_limit, - 'max_seats_limit' => (int)$billing_subscription->max_seats_limit, - 'trial_enabled' => (bool)$billing_subscription->trial_enabled, - 'trial_duration' => (int)$billing_subscription->trial_duration, - 'allow_query_overrides' => (bool)$billing_subscription->allow_query_overrides, - 'allow_plan_changes' => (bool)$billing_subscription->allow_plan_changes, - 'plan_map' => (string)$billing_subscription->plan_map, - 'refund_period' => (int)$billing_subscription->refund_period, - 'webhook_configuration' => $billing_subscription->webhook_configuration ?: $std, - 'purchase_page' => (string)route('client.subscription.purchase', $billing_subscription->hashed_id), - 'is_deleted' => (bool)$billing_subscription->is_deleted, - 'created_at' => (int)$billing_subscription->created_at, - 'updated_at' => (int)$billing_subscription->updated_at, - 'archived_at' => (int)$billing_subscription->deleted_at, - ]; - } - - public function includeProduct(BillingSubscription $billing_subscription): \League\Fractal\Resource\Item - { - $transformer = new ProductTransformer($this->serializer); - - return $this->includeItem($billing_subscription->product, $transformer, Product::class); - } -} diff --git a/app/Transformers/CompanyTransformer.php b/app/Transformers/CompanyTransformer.php index 8620fefd7b2a..5fd5857f5973 100644 --- a/app/Transformers/CompanyTransformer.php +++ b/app/Transformers/CompanyTransformer.php @@ -13,7 +13,7 @@ namespace App\Transformers; use App\Models\Account; use App\Models\Activity; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Client; use App\Models\ClientSubscription; use App\Models\Company; @@ -92,7 +92,7 @@ class CompanyTransformer extends EntityTransformer 'system_logs', 'expense_categories', 'task_statuses', - 'client_subscriptions', + 'billing_subscriptions', ]; /** @@ -362,17 +362,10 @@ class CompanyTransformer extends EntityTransformer return $this->includeCollection($company->system_logs, $transformer, SystemLog::class); } - public function includeClientSubscriptions(Company $company) + public function includeSubscriptions(Company $company) { - $transformer = new ClientSubscriptionTransformer($this->serializer); + $transformer = new SubscriptionTransformer($this->serializer); - return $this->includeCollection($company->client_subscriptions, $transformer, ClientSubscription::class); - } - - public function includeBillingSubscriptions(Company $company) - { - $transformer = new BillingSubscriptionTransformer($this->serializer); - - return $this->includeCollection($company->billing_subscriptions, $transformer, BillingSubscription::class); + return $this->includeCollection($company->billing_subscriptions, $transformer, Subscription::class); } } diff --git a/app/Transformers/SubscriptionTransformer.php b/app/Transformers/SubscriptionTransformer.php new file mode 100644 index 000000000000..4c0b59d2118b --- /dev/null +++ b/app/Transformers/SubscriptionTransformer.php @@ -0,0 +1,69 @@ + $this->encodePrimaryKey($subscription->id), + 'user_id' => $this->encodePrimaryKey($subscription->user_id), + 'product_id' => $this->encodePrimaryKey($subscription->product_id), + 'assigned_user_id' => $this->encodePrimaryKey($subscription->assigned_user_id), + 'company_id' => $this->encodePrimaryKey($subscription->company_id), + 'is_recurring' => (bool)$subscription->is_recurring, + 'frequency_id' => (string)$subscription->frequency_id, + 'auto_bill' => (string)$subscription->auto_bill, + 'promo_code' => (string)$subscription->promo_code, + 'promo_discount' => (float)$subscription->promo_discount, + 'is_amount_discount' => (bool)$subscription->is_amount_discount, + 'allow_cancellation' => (bool)$subscription->allow_cancellation, + 'per_seat_enabled' => (bool)$subscription->per_set_enabled, + 'min_seats_limit' => (int)$subscription->min_seats_limit, + 'max_seats_limit' => (int)$subscription->max_seats_limit, + 'trial_enabled' => (bool)$subscription->trial_enabled, + 'trial_duration' => (int)$subscription->trial_duration, + 'allow_query_overrides' => (bool)$subscription->allow_query_overrides, + 'allow_plan_changes' => (bool)$subscription->allow_plan_changes, + 'plan_map' => (string)$subscription->plan_map, + 'refund_period' => (int)$subscription->refund_period, + 'webhook_configuration' => $subscription->webhook_configuration ?: $std, + 'purchase_page' => (string)route('client.subscription.purchase', $subscription->hashed_id), + 'is_deleted' => (bool)$subscription->is_deleted, + 'created_at' => (int)$subscription->created_at, + 'updated_at' => (int)$subscription->updated_at, + 'archived_at' => (int)$subscription->deleted_at, + ]; + } + +} diff --git a/database/factories/BillingSubscriptionFactory.php b/database/factories/SubscriptionFactory.php similarity index 81% rename from database/factories/BillingSubscriptionFactory.php rename to database/factories/SubscriptionFactory.php index 2fdf320cd5f4..a9b008244c7f 100644 --- a/database/factories/BillingSubscriptionFactory.php +++ b/database/factories/SubscriptionFactory.php @@ -12,17 +12,17 @@ namespace Database\Factories; -use App\Models\BillingSubscription; +use App\Models\Subscription; use Illuminate\Database\Eloquent\Factories\Factory; -class BillingSubscriptionFactory extends Factory +class SubscriptionFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ - protected $model = BillingSubscription::class; + protected $model = Subscription::class; /** * Define the model's default state. diff --git a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php new file mode 100644 index 000000000000..67a74f1f45ee --- /dev/null +++ b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php @@ -0,0 +1,29 @@ + ['api_db', 'token_auth', 'locale'], 'prefix' => 'a // Route::post('hooks', 'SubscriptionController@subscribe')->name('hooks.subscribe'); // Route::delete('hooks/{subscription_id}', 'SubscriptionController@unsubscribe')->name('hooks.unsubscribe'); - Route::resource('billing_subscriptions', 'BillingSubscriptionController'); + Route::resource('subscriptions', 'SubscriptionController'); Route::resource('cliente_subscriptions', 'ClientSubscriptionController'); }); diff --git a/tests/Feature/BillingSubscriptionApiTest.php b/tests/Feature/SubscriptionApiTest.php similarity index 68% rename from tests/Feature/BillingSubscriptionApiTest.php rename to tests/Feature/SubscriptionApiTest.php index 54c6162ef5ff..c09e3c13c424 100644 --- a/tests/Feature/BillingSubscriptionApiTest.php +++ b/tests/Feature/SubscriptionApiTest.php @@ -11,7 +11,7 @@ namespace Tests\Feature; -use App\Models\BillingSubscription; +use App\Models\Subscription; use App\Models\Product; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; @@ -24,9 +24,9 @@ use Tests\TestCase; /** * @test - * @covers App\Http\Controllers\BillingSubscriptionController + * @covers App\Http\Controllers\SubscriptionController */ -class BillingSubscriptionApiTest extends TestCase +class SubscriptionApiTest extends TestCase { use MakesHash; use DatabaseTransactions; @@ -45,27 +45,30 @@ class BillingSubscriptionApiTest extends TestCase Model::reguard(); } - public function testBillingSubscriptionsGet() + public function testSubscriptionsGet() { $product = Product::factory()->create([ 'company_id' => $this->company->id, 'user_id' => $this->user->id, ]); - $billing_subscription = BillingSubscription::factory()->create([ + $billing_subscription = Subscription::factory()->create([ 'product_id' => $product->id, 'company_id' => $this->company->id, ]); + $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->get('/api/v1/billing_subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)); - + ])->get('/api/v1/subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)); + + nlog($response); + $response->assertStatus(200); } - public function testBillingSubscriptionsPost() + public function testSubscriptionsPost() { $product = Product::factory()->create([ 'company_id' => $this->company->id, @@ -75,12 +78,12 @@ class BillingSubscriptionApiTest extends TestCase $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/billing_subscriptions', ['product_id' => $product->id, 'allow_cancellation' => true]); + ])->post('/api/v1/subscriptions', ['product_id' => $product->id, 'allow_cancellation' => true]); $response->assertStatus(200); } - public function testBillingSubscriptionPut() + public function testSubscriptionPut() { $product = Product::factory()->create([ 'company_id' => $this->company->id, @@ -89,13 +92,13 @@ class BillingSubscriptionApiTest extends TestCase $response1 = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) - ->post('/api/v1/billing_subscriptions', ['product_id' => $product->id]) + ->post('/api/v1/subscriptions', ['product_id' => $product->id]) ->assertStatus(200) ->json(); $response2 = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'),'X-API-TOKEN' => $this->token]) - ->put('/api/v1/billing_subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true]) + ->put('/api/v1/subscriptions/' . $response1['data']['id'], ['allow_cancellation' => true]) ->assertStatus(200) ->json(); @@ -103,15 +106,15 @@ class BillingSubscriptionApiTest extends TestCase } /* - TypeError : Argument 1 passed to App\Transformers\BillingSubscriptionTransformer::transform() must be an instance of App\Models\BillingSubscription, bool given, called in /var/www/html/vendor/league/fractal/src/Scope.php on line 407 - /var/www/html/app/Transformers/BillingSubscriptionTransformer.php:35 + TypeError : Argument 1 passed to App\Transformers\SubscriptionTransformer::transform() must be an instance of App\Models\Subscription, bool given, called in /var/www/html/vendor/league/fractal/src/Scope.php on line 407 + /var/www/html/app/Transformers/SubscriptionTransformer.php:35 /var/www/html/vendor/league/fractal/src/Scope.php:407 /var/www/html/vendor/league/fractal/src/Scope.php:349 /var/www/html/vendor/league/fractal/src/Scope.php:235 /var/www/html/app/Http/Controllers/BaseController.php:395 - /var/www/html/app/Http/Controllers/BillingSubscriptionController.php:408 + /var/www/html/app/Http/Controllers/SubscriptionController.php:408 */ - public function testBillingSubscriptionDeleted() + public function testSubscriptionDeleted() { $product = Product::factory()->create([ @@ -119,14 +122,14 @@ class BillingSubscriptionApiTest extends TestCase 'user_id' => $this->user->id, ]); - $billing_subscription = BillingSubscription::factory()->create([ + $billing_subscription = Subscription::factory()->create([ 'product_id' => $product->id, 'company_id' => $this->company->id, ]); $response = $this ->withHeaders(['X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token]) - ->delete('/api/v1/billing_subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)) + ->delete('/api/v1/subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)) ->assertStatus(200) ->json(); From 31eac0a761deeb1d77b5e77538b7934c2bca50ac Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 21:55:35 +1100 Subject: [PATCH 10/12] Refactoring subscriptions --- ...21_03_25_082025_refactor_billing_scriptions_table.php | 9 +++++++++ tests/Feature/SubscriptionApiTest.php | 2 +- 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php index 67a74f1f45ee..c982bb2f0eaa 100644 --- a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php +++ b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php @@ -15,6 +15,15 @@ class RefactorBillingScriptionsTable extends Migration { Schema::rename('billing_subscriptions', 'subscriptions'); + Schema::table('subscriptions', function (Blueprint $table) { + $table->text('product_id')->change(); + $table->text('recurring_product_ids'); + }); + + Schema::table('subscriptions', function (Blueprint $table) { + $table->renameColumn('product_id', 'product_ids'); + }); + } /** diff --git a/tests/Feature/SubscriptionApiTest.php b/tests/Feature/SubscriptionApiTest.php index c09e3c13c424..49ca0d007987 100644 --- a/tests/Feature/SubscriptionApiTest.php +++ b/tests/Feature/SubscriptionApiTest.php @@ -63,7 +63,7 @@ class SubscriptionApiTest extends TestCase 'X-API-TOKEN' => $this->token, ])->get('/api/v1/subscriptions/' . $this->encodePrimaryKey($billing_subscription->id)); - nlog($response); + // nlog($response); $response->assertStatus(200); } From b70eac484a900806600d69997ee4e169ba9de588 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 22:00:51 +1100 Subject: [PATCH 11/12] Add name to subscription --- .../2021_03_25_082025_refactor_billing_scriptions_table.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php index c982bb2f0eaa..4a8c10856ec8 100644 --- a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php +++ b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php @@ -18,6 +18,8 @@ class RefactorBillingScriptionsTable extends Migration Schema::table('subscriptions', function (Blueprint $table) { $table->text('product_id')->change(); $table->text('recurring_product_ids'); + $table->string('name'); + $table->unique(['company_id', 'name']); }); Schema::table('subscriptions', function (Blueprint $table) { From 951af497ed3b32b7aa859981c97323769820267e Mon Sep 17 00:00:00 2001 From: David Bomba Date: Thu, 25 Mar 2021 22:27:52 +1100 Subject: [PATCH 12/12] Refactoring subscriptions --- .../2021_03_25_082025_refactor_billing_scriptions_table.php | 1 + 1 file changed, 1 insertion(+) diff --git a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php index 4a8c10856ec8..f944dc5596de 100644 --- a/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php +++ b/database/migrations/2021_03_25_082025_refactor_billing_scriptions_table.php @@ -13,6 +13,7 @@ class RefactorBillingScriptionsTable extends Migration */ public function up() { + Schema::rename('billing_subscriptions', 'subscriptions'); Schema::table('subscriptions', function (Blueprint $table) {