From cb7f27462b410d8c9a9be007713e4c188256b011 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 24 Mar 2021 20:14:30 +1100 Subject: [PATCH] Client subscriptions + bug fixes (#5238) * Fixes for converting quote to invoice * Fixes for naming PDFs * Refresh entity prior to sending * Fixes for subscriptions * Add in required use * Fixes for notifications * Fixes for notifications --- app/DataMapper/CompanySettings.php | 3 +- app/Http/Controllers/EmailController.php | 2 +- app/Http/Controllers/InvoiceController.php | 2 ++ app/Http/Controllers/QuoteController.php | 2 +- .../Credit/CreditEmailedNotification.php | 2 +- .../Invoice/InvoiceEmailedNotification.php | 2 +- .../InvoiceFailedEmailNotification.php | 2 +- .../Quote/QuoteEmailedNotification.php | 2 +- app/Models/BaseModel.php | 4 ++- .../BillingSubscriptionService.php | 5 ++-- .../BillingSubscriptionTransformer.php | 4 ++- .../Traits/Notifications/UserNotifies.php | 4 ++- ...ble_constraint_to_recurring_invoice_id.php | 29 +++++++++++++++++++ 13 files changed, 51 insertions(+), 12 deletions(-) create mode 100644 database/migrations/2021_03_23_233844_add_nullable_constraint_to_recurring_invoice_id.php 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/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)); } 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/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/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/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); diff --git a/app/Services/BillingSubscription/BillingSubscriptionService.php b/app/Services/BillingSubscription/BillingSubscriptionService.php index 9cf918d99dc2..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(); @@ -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); } diff --git a/app/Transformers/BillingSubscriptionTransformer.php b/app/Transformers/BillingSubscriptionTransformer.php index 3dc50cd78235..74ac5484ffe3 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/app/Utils/Traits/Notifications/UserNotifies.php b/app/Utils/Traits/Notifications/UserNotifies.php index 91455c16ea5c..efff7001004f 100644 --- a/app/Utils/Traits/Notifications/UserNotifies.php +++ b/app/Utils/Traits/Notifications/UserNotifies.php @@ -34,10 +34,12 @@ trait UserNotifies 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) { + 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'); 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() + { + } +}