From e254a9ad73e9a7541a8b5fa2a4a1b8d5bf26cee0 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 May 2021 10:35:39 +1000 Subject: [PATCH 1/4] Refactor reminders --- app/Jobs/Util/ReminderJob.php | 2 +- app/Observers/InvoiceObserver.php | 3 + app/Services/Invoice/InvoiceService.php | 8 ++ app/Services/Invoice/MarkSent.php | 3 +- app/Services/Invoice/UpdateReminder.php | 131 +++++++++++++++++++++++ app/Utils/Traits/MakesReminders.php | 115 +------------------- tests/Feature/ReminderTest.php | 6 +- tests/Integration/CheckRemindersTest.php | 12 +-- 8 files changed, 155 insertions(+), 125 deletions(-) create mode 100644 app/Services/Invoice/UpdateReminder.php diff --git a/app/Jobs/Util/ReminderJob.php b/app/Jobs/Util/ReminderJob.php index c0f4a7bcfefe..5988076a16ee 100644 --- a/app/Jobs/Util/ReminderJob.php +++ b/app/Jobs/Util/ReminderJob.php @@ -68,7 +68,7 @@ class ReminderJob implements ShouldQueue event(new InvoiceWasEmailed($invoice->invitations->first(), $invoice->company, Ninja::eventVars(), $reminder_template)); } - $invoice->setReminder(); + $invoice->service()->setReminder()->save(); } else { $invoice->next_send_date = null; diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index db104603dbf6..d3f425c3f577 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -52,6 +52,9 @@ class InvoiceObserver WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); } + if($invoice->isDirty('date') || $invoice->isDirty('due_date')) + $invoice->service()->setReminder()->save(); + // UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->numberFormatter().'.pdf'); } diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index adb0c4e5c5ea..f93d5a88db30 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -21,6 +21,7 @@ use App\Models\Invoice; use App\Models\Payment; use App\Models\Task; use App\Services\Client\ClientService; +use App\Services\Invoice\UpdateReminder; use App\Utils\Ninja; use App\Utils\Traits\MakesHash; use Illuminate\Support\Carbon; @@ -425,6 +426,13 @@ class InvoiceService return $this; } + public function setReminder($settings = null) + { + $this->invoice = (new UpdateReminder($this->invoice, $settings))->run(); + + return $this; + } + /** * Saves the invoice. * @return Invoice object diff --git a/app/Services/Invoice/MarkSent.php b/app/Services/Invoice/MarkSent.php index 11e36d590503..07dd66e817eb 100644 --- a/app/Services/Invoice/MarkSent.php +++ b/app/Services/Invoice/MarkSent.php @@ -46,10 +46,9 @@ class MarkSent extends AbstractService ->setDueDate() ->updateBalance($this->invoice->amount) ->deletePdf() + ->setReminder() ->save(); - $this->invoice->setReminder(); - $this->client->service()->updateBalance($this->invoice->balance)->save(); $this->invoice->ledger()->updateInvoiceBalance($this->invoice->balance, "Invoice {$this->invoice->number} marked as sent."); diff --git a/app/Services/Invoice/UpdateReminder.php b/app/Services/Invoice/UpdateReminder.php new file mode 100644 index 000000000000..c9a0078e6d7a --- /dev/null +++ b/app/Services/Invoice/UpdateReminder.php @@ -0,0 +1,131 @@ +invoice = $invoice; + $this->settings = $settings; + } + + public function run() + { + + if (! $this->settings) { + $this->settings = $this->invoice->client->getMergedSettings(); + } + + if (! $this->invoice->isPayable()) { + $this->invoice->next_send_date = null; + $this->invoice->save(); + + return; //exit early + } + + $date_collection = collect(); + + if (is_null($this->invoice->reminder1_sent) && + $this->settings->schedule_reminder1 == 'after_invoice_date' && + $this->settings->num_days_reminder1 > 0) { + $reminder_date = Carbon::parse($this->invoice->date)->addDays($this->settings->num_days_reminder1); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder1_sent) && + $this->settings->schedule_reminder1 == 'before_due_date' && + $this->settings->num_days_reminder1 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->subDays($this->settings->num_days_reminder1); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder1_sent) && + $this->settings->schedule_reminder1 == 'after_due_date' && + $this->settings->num_days_reminder1 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->addDays($this->settings->num_days_reminder1); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder2_sent) && + $this->settings->schedule_reminder2 == 'after_invoice_date' && + $this->settings->num_days_reminder2 > 0) { + $reminder_date = Carbon::parse($this->invoice->date)->addDays($this->settings->num_days_reminder2); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder2_sent) && + $this->settings->schedule_reminder2 == 'before_due_date' && + $this->settings->num_days_reminder2 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->subDays($this->settings->num_days_reminder2); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder2_sent) && + $this->settings->schedule_reminder2 == 'after_due_date' && + $this->settings->num_days_reminder2 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->addDays($this->settings->num_days_reminder2); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder3_sent) && + $this->settings->schedule_reminder3 == 'after_invoice_date' && + $this->settings->num_days_reminder3 > 0) { + $reminder_date = Carbon::parse($this->invoice->date)->addDays($this->settings->num_days_reminder3); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder3_sent) && + $this->settings->schedule_reminder3 == 'before_due_date' && + $this->settings->num_days_reminder3 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->subDays($this->settings->num_days_reminder3); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + if (is_null($this->invoice->reminder3_sent) && + $this->settings->schedule_reminder3 == 'after_due_date' && + $this->settings->num_days_reminder3 > 0) { + $reminder_date = Carbon::parse($this->invoice->due_date)->addDays($this->settings->num_days_reminder3); + + if ($reminder_date->gt(Carbon::parse($this->invoice->next_send_date))); + $date_collection->push($reminder_date->format('Y-m-d')); + } + + $this->invoice->next_send_date = $date_collection->sort()->first(); + + return $this->invoice; + } +} \ No newline at end of file diff --git a/app/Utils/Traits/MakesReminders.php b/app/Utils/Traits/MakesReminders.php index b650de23b474..d0457321f141 100644 --- a/app/Utils/Traits/MakesReminders.php +++ b/app/Utils/Traits/MakesReminders.php @@ -19,116 +19,7 @@ use Illuminate\Support\Carbon; */ trait MakesReminders { - public function setReminder($settings = null) - { - if (! $settings) { - $settings = $this->client->getMergedSettings(); - } - - if (! $this->isPayable()) { - $this->next_send_date = null; - $this->save(); - - return; //exit early - } - - $date_collection = collect(); - - if (is_null($this->reminder1_sent) && - $settings->schedule_reminder1 == 'after_invoice_date' && - $settings->num_days_reminder1 > 0) { - $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder1); - nlog("reminder 1 = after invoice date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_invoice_date pushed to collection'); - } - - if (is_null($this->reminder1_sent) && - $settings->schedule_reminder1 == 'before_due_date' && - $settings->num_days_reminder1 > 0) { - $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder1); - nlog("reminder 1 = before_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('before_due_date pushed to collection'); - } - - if (is_null($this->reminder1_sent) && - $settings->schedule_reminder1 == 'after_due_date' && - $settings->num_days_reminder1 > 0) { - $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder1); - nlog("reminder 1 = after_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_due_date pushed to collection'); - } - - if (is_null($this->reminder2_sent) && - $settings->schedule_reminder2 == 'after_invoice_date' && - $settings->num_days_reminder2 > 0) { - $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder2); - nlog("reminder 2 = after_invoice_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_invoice_date pushed to collection'); - } - - if (is_null($this->reminder2_sent) && - $settings->schedule_reminder2 == 'before_due_date' && - $settings->num_days_reminder2 > 0) { - $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder2); - nlog("reminder 2 = before_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('before_due_date pushed to collection'); - } - - if (is_null($this->reminder2_sent) && - $settings->schedule_reminder2 == 'after_due_date' && - $settings->num_days_reminder2 > 0) { - $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder2); - nlog("reminder 2 = after_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_due_date pushed to collection'); - } - - if (is_null($this->reminder3_sent) && - $settings->schedule_reminder3 == 'after_invoice_date' && - $settings->num_days_reminder3 > 0) { - $reminder_date = Carbon::parse($this->date)->addDays($settings->num_days_reminder3); - nlog("reminder 3 = after_invoice_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_invoice_date pushed to collection'); - } - - if (is_null($this->reminder3_sent) && - $settings->schedule_reminder3 == 'before_due_date' && - $settings->num_days_reminder3 > 0) { - $reminder_date = Carbon::parse($this->due_date)->subDays($settings->num_days_reminder3); - nlog("reminder 3 = before_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('before_due_date pushed to collection'); - } - - if (is_null($this->reminder3_sent) && - $settings->schedule_reminder3 == 'after_due_date' && - $settings->num_days_reminder3 > 0) { - $reminder_date = Carbon::parse($this->due_date)->addDays($settings->num_days_reminder3); - nlog("reminder 3 = after_due_date = {$reminder_date}"); - - if ($reminder_date->gt(Carbon::parse($this->next_send_date))); - $date_collection->push($reminder_date->format('Y-m-d')); nlog('after_due_date pushed to collection'); - } - - $this->next_send_date = $date_collection->sort()->first(); - - $this->save(); - } - + public function inReminderWindow($schedule_reminder, $num_days_reminder) { switch ($schedule_reminder) { @@ -195,11 +86,9 @@ trait MakesReminders private function addTimeInterval($date, $endless_reminder_frequency_id) :?Carbon { - if (!$date) { + if (!$date) return null; - } - switch ($endless_reminder_frequency_id) { case RecurringInvoice::FREQUENCY_WEEKLY: return Carbon::parse($date)->addWeek()->startOfDay(); diff --git a/tests/Feature/ReminderTest.php b/tests/Feature/ReminderTest.php index 8ed4c6bea559..3651be8c7f5b 100644 --- a/tests/Feature/ReminderTest.php +++ b/tests/Feature/ReminderTest.php @@ -75,7 +75,7 @@ class ReminderTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals($this->invoice->next_send_date, Carbon::now()->addDays(7)->format('Y-m-d')); @@ -102,7 +102,7 @@ class ReminderTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->invoice->fresh(); @@ -132,7 +132,7 @@ class ReminderTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->invoice->fresh(); diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index b8f2cbb265f8..f58cc42339fa 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -52,7 +52,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals(0, Carbon::now()->addDays(7)->diffInDays($this->invoice->next_send_date)); } @@ -75,7 +75,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent()->setStatus(Invoice::STATUS_PAID); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals($this->invoice->next_send_date, null); } @@ -98,7 +98,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->subDays(29)->diffInDays($this->invoice->next_send_date)); } @@ -118,7 +118,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals(0, Carbon::parse($this->invoice->due_date)->addDays(1)->diffInDays($this->invoice->next_send_date)); } @@ -141,7 +141,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals($this->invoice->next_send_date, null); } @@ -164,7 +164,7 @@ class CheckRemindersTest extends TestCase $this->company->settings = $settings; $this->invoice->service()->markSent(); - $this->invoice->setReminder($settings); + $this->invoice->service()->setReminder($settings)->save(); $this->assertEquals($this->invoice->next_send_date, null); } From 0079e52b858ca62f78ac7cdc66eb161ea743fe4f Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 May 2021 10:37:59 +1000 Subject: [PATCH 2/4] Refactor for account signups --- app/Http/Controllers/AccountController.php | 2 +- app/Jobs/Account/CreateAccount.php | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/AccountController.php b/app/Http/Controllers/AccountController.php index 53950f1f108f..b4df8ca6b5a2 100644 --- a/app/Http/Controllers/AccountController.php +++ b/app/Http/Controllers/AccountController.php @@ -142,7 +142,7 @@ class AccountController extends BaseController */ public function store(CreateAccountRequest $request) { - $account = CreateAccount::dispatchNow($request->all()); + $account = CreateAccount::dispatchNow($request->all(), $request->getClientIp()); if (! ($account instanceof Account)) { return $account; diff --git a/app/Jobs/Account/CreateAccount.php b/app/Jobs/Account/CreateAccount.php index b175e4365e32..6b41005b229b 100644 --- a/app/Jobs/Account/CreateAccount.php +++ b/app/Jobs/Account/CreateAccount.php @@ -42,9 +42,12 @@ class CreateAccount protected $request; - public function __construct(array $sp660339) + protected $client_ip; + + public function __construct(array $sp660339, $client_ip) { $this->request = $sp660339; + $this->client_ip = $client_ip; } public function handle() @@ -113,7 +116,7 @@ class CreateAccount private function processSettings($settings) { - if(Ninja::isHosted() && Cache::get('currencies') && $data = unserialize(@file_get_contents('http://www.geoplugin.net/php.gp?ip=' . request()->getClientIp()))) + if(Ninja::isHosted() && Cache::get('currencies') && $data = unserialize(@file_get_contents('http://www.geoplugin.net/php.gp?ip=' . $this->client_ip))) { $currency_code = strtolower($data['geoplugin_currencyCode']); From ba672f6fdd4cb573185f879c78cefbdf9a4e9cc9 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 May 2021 11:32:01 +1000 Subject: [PATCH 3/4] Fixes for test --- app/Http/Requests/User/UpdateUserRequest.php | 3 --- app/Http/ValidationRules/UniqueUserRule.php | 8 +------- app/Http/ValidationRules/User/AttachableUser.php | 12 ++++++++++-- app/Http/ValidationRules/ValidUserForCompany.php | 9 +-------- app/Jobs/Util/Import.php | 3 ++- app/Libraries/MultiDB.php | 4 ++-- app/Services/Invoice/UpdateReminder.php | 2 +- resources/lang/en/texts.php | 2 ++ tests/Integration/CheckRemindersTest.php | 1 - tests/MockAccountData.php | 8 ++++---- 10 files changed, 23 insertions(+), 29 deletions(-) diff --git a/app/Http/Requests/User/UpdateUserRequest.php b/app/Http/Requests/User/UpdateUserRequest.php index 966d8b24d5d3..eda9afb9fc74 100644 --- a/app/Http/Requests/User/UpdateUserRequest.php +++ b/app/Http/Requests/User/UpdateUserRequest.php @@ -45,9 +45,6 @@ class UpdateUserRequest extends Request { $input = $this->all(); - // if (isset($input['company_user']) && ! auth()->user()->isAdmin()) { - // unset($input['company_user']); - // } $this->replace($input); } diff --git a/app/Http/ValidationRules/UniqueUserRule.php b/app/Http/ValidationRules/UniqueUserRule.php index e6eb7be90782..b9bca3a2e5bc 100644 --- a/app/Http/ValidationRules/UniqueUserRule.php +++ b/app/Http/ValidationRules/UniqueUserRule.php @@ -60,12 +60,6 @@ class UniqueUserRule implements Rule */ private function checkIfEmailExists($email) : bool { - $current_db = config('database.default'); - - $result = MultiDB::checkUserEmailExists($email); - - MultiDB::setDb($current_db); - - return $result; + return MultiDB::checkUserEmailExists($email); } } diff --git a/app/Http/ValidationRules/User/AttachableUser.php b/app/Http/ValidationRules/User/AttachableUser.php index 226d67b702cb..db7cec97a3e3 100644 --- a/app/Http/ValidationRules/User/AttachableUser.php +++ b/app/Http/ValidationRules/User/AttachableUser.php @@ -20,6 +20,7 @@ use Illuminate\Contracts\Validation\Rule; */ class AttachableUser implements Rule { + public $message; public function __construct() { @@ -39,7 +40,7 @@ class AttachableUser implements Rule */ public function message() { - return "Cannot add the same user to the same company"; + return $this->message; } /** @@ -63,9 +64,16 @@ class AttachableUser implements Rule ->where('company_id', auth()->user()->company()->id) ->exists(); - if($user_already_attached) + //If the user is already attached or isn't link to this account - return false + if($user_already_attached) { + $this->message = ctrans('texts.user_duplicate_error'); return false; + } + if($user->account_id != auth()->user()->account_id){ + $this->message = ctrans('texts.user_cross_linked_error'); + return false; + } return true; } diff --git a/app/Http/ValidationRules/ValidUserForCompany.php b/app/Http/ValidationRules/ValidUserForCompany.php index cb3825704606..967ca56591d7 100644 --- a/app/Http/ValidationRules/ValidUserForCompany.php +++ b/app/Http/ValidationRules/ValidUserForCompany.php @@ -26,14 +26,7 @@ class ValidUserForCompany implements Rule */ public function passes($attribute, $value) { - $current_db = config('database.default'); - - $result = MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id); - - - MultiDB::setDb($current_db); - - return $result; + return MultiDB::checkUserAndCompanyCoExist($value, auth()->user()->company()->company_key, auth()->user()->company()->id); } /** diff --git a/app/Jobs/Util/Import.php b/app/Jobs/Util/Import.php index 074716d9468e..f21a9817840b 100644 --- a/app/Jobs/Util/Import.php +++ b/app/Jobs/Util/Import.php @@ -423,7 +423,8 @@ class Import implements ShouldQueue $rules = [ '*.first_name' => ['string'], '*.last_name' => ['string'], - '*.email' => ['distinct'], + //'*.email' => ['distinct'], + '*.email' => ['distinct', 'email', new ValidUserForCompany(), new AttachableUser()], ]; // if (config('ninja.db.multi_db_enabled')) { diff --git a/app/Libraries/MultiDB.php b/app/Libraries/MultiDB.php index 83519d6c46e4..51afc34ae331 100644 --- a/app/Libraries/MultiDB.php +++ b/app/Libraries/MultiDB.php @@ -106,8 +106,8 @@ class MultiDB $current_db = config('database.default'); foreach (self::$dbs as $db) { - if (User::on($db)->where(['email' => $email])->get()->count() >= 1) { // if user already exists, validation will fail - if (Company::on($db)->where(['company_key' => $company_key])->get()->count() >= 1) { + if (User::on($db)->where(['email' => $email])->exists()) { + if (Company::on($db)->where(['company_key' => $company_key])->exists()) { self::setDb($current_db); return true; } else { diff --git a/app/Services/Invoice/UpdateReminder.php b/app/Services/Invoice/UpdateReminder.php index c9a0078e6d7a..60faac74f108 100644 --- a/app/Services/Invoice/UpdateReminder.php +++ b/app/Services/Invoice/UpdateReminder.php @@ -38,7 +38,7 @@ class UpdateReminder extends AbstractService $this->invoice->next_send_date = null; $this->invoice->save(); - return; //exit early + return $this->invoice; //exit early } $date_collection = collect(); diff --git a/resources/lang/en/texts.php b/resources/lang/en/texts.php index 09ecf7a16aee..e329553bd1f9 100644 --- a/resources/lang/en/texts.php +++ b/resources/lang/en/texts.php @@ -4252,6 +4252,8 @@ $LANG = array( 'contact_details' => 'Contact Details', 'download_backup_subject' => 'Your company backup is ready for download', 'account_passwordless_login' => 'Account passwordless login', + 'user_duplicate_error' => 'Cannot add the same user to the same company', + 'user_cross_linked_error' => 'User exists but cannot be crossed linked to multiple accounts', ); return $LANG; diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index f58cc42339fa..78c5ab89f483 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -25,7 +25,6 @@ class CheckRemindersTest extends TestCase { use MockAccountData; use DatabaseTransactions; - use MakesReminders; public function setUp() :void { diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index f20b0a4737fa..ad5225270ddc 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -192,10 +192,10 @@ trait MockAccountData if (! $user) { $user = User::factory()->create([ - 'account_id' => $this->account->id, - 'confirmation_code' => $this->createDbHash(config('database.default')), - 'email' => 'user@example.com', - ]); + 'account_id' => $this->account->id, + 'confirmation_code' => $this->createDbHash(config('database.default')), + 'email' => 'user@example.com', + ]); } $user->password = Hash::make('ALongAndBriliantPassword'); From 7618666c4b8497af0bf79f850b01e12b972fa6a8 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 26 May 2021 12:37:16 +1000 Subject: [PATCH 4/4] Fixes for tests --- app/Console/Commands/CheckData.php | 2 +- app/Console/Kernel.php | 3 ++- app/Observers/CompanyObserver.php | 4 ---- app/Observers/InvoiceObserver.php | 4 ++-- app/Repositories/BaseRepository.php | 10 +++++++--- app/Services/Invoice/InvoiceService.php | 14 +++++++------- app/Services/Invoice/UpdateReminder.php | 2 +- resources/views/email/migration/failed.blade.php | 4 ++++ tests/Integration/CheckRemindersTest.php | 3 ++- 9 files changed, 26 insertions(+), 20 deletions(-) diff --git a/app/Console/Commands/CheckData.php b/app/Console/Commands/CheckData.php index 0b8ad079ea92..6a68546d6e18 100644 --- a/app/Console/Commands/CheckData.php +++ b/app/Console/Commands/CheckData.php @@ -65,7 +65,7 @@ class CheckData extends Command /** * @var string */ - protected $name = 'ninja:check-data'; + protected $signature = 'ninja:check-data {--database=} {--fix=}'; /** * @var string diff --git a/app/Console/Kernel.php b/app/Console/Kernel.php index 4edd36a1c3ec..f0ac9d77857f 100644 --- a/app/Console/Kernel.php +++ b/app/Console/Kernel.php @@ -46,7 +46,7 @@ class Kernel extends ConsoleKernel $schedule->job(new VersionCheck)->daily(); - $schedule->command('ninja:check-data')->daily()->withoutOverlapping(); + $schedule->command('ninja:check-data --database=db-ninja-01')->daily()->withoutOverlapping(); $schedule->job(new ReminderJob)->daily()->withoutOverlapping(); @@ -65,6 +65,7 @@ class Kernel extends ConsoleKernel $schedule->job(new AdjustEmailQuota)->daily()->withoutOverlapping(); $schedule->job(new SendFailedEmails)->daily()->withoutOverlapping(); + $schedule->command('ninja:check-data --database=db-ninja-02')->daily()->withoutOverlapping(); } diff --git a/app/Observers/CompanyObserver.php b/app/Observers/CompanyObserver.php index 8ae111afa0e3..05a0ee3c3329 100644 --- a/app/Observers/CompanyObserver.php +++ b/app/Observers/CompanyObserver.php @@ -39,10 +39,6 @@ class CompanyObserver if(Ninja::isHosted() && $company->portal_mode == 'domain' && $company->isDirty('portal_domain')) { - nlog('company observer - updated'); - nlog($company->portal_domain); - nlog($company->getOriginal('portal_domain')); - //fire event to build new custom portal domain \Modules\Admin\Jobs\Domain\CustomDomain::dispatch($company->getOriginal('portal_domain'), $company)->onQueue('domain'); } diff --git a/app/Observers/InvoiceObserver.php b/app/Observers/InvoiceObserver.php index d3f425c3f577..b91436d2edcf 100644 --- a/app/Observers/InvoiceObserver.php +++ b/app/Observers/InvoiceObserver.php @@ -52,8 +52,8 @@ class InvoiceObserver WebhookHandler::dispatch(Webhook::EVENT_UPDATE_INVOICE, $invoice, $invoice->company); } - if($invoice->isDirty('date') || $invoice->isDirty('due_date')) - $invoice->service()->setReminder()->save(); + // if($invoice->isDirty('date') || $invoice->isDirty('due_date')) + // $invoice->service()->setReminder()->save(); // UnlinkFile::dispatchNow(config('filesystems.default'), $invoice->client->invoice_filepath() . $invoice->numberFormatter().'.pdf'); diff --git a/app/Repositories/BaseRepository.php b/app/Repositories/BaseRepository.php index b5062a061b40..bfe250ea26c8 100644 --- a/app/Repositories/BaseRepository.php +++ b/app/Repositories/BaseRepository.php @@ -169,10 +169,14 @@ class BaseRepository */ protected function alternativeSave($data, $model) { - - if (array_key_exists('client_id', $data)) //forces the client_id if it doesn't exist + //forces the client_id if it doesn't exist + if(array_key_exists('client_id', $data)) $model->client_id = $data['client_id']; + //pickup changes here to recalculate reminders + if($model instanceof Invoice && ($model->isDirty('date') || $model->isDirty('due_date'))) + $model->service()->setReminder()->save(); + $client = Client::where('id', $model->client_id)->withTrashed()->first(); $state = []; @@ -189,7 +193,7 @@ class BaseRepository $data = array_merge($company_defaults, $data); } - $tmp_data = $data; //preserves the $data arrayss + $tmp_data = $data; //preserves the $data array /* We need to unset some variable as we sometimes unguard the model */ if (isset($tmp_data['invitations'])) diff --git a/app/Services/Invoice/InvoiceService.php b/app/Services/Invoice/InvoiceService.php index f93d5a88db30..f0cf40cb6411 100644 --- a/app/Services/Invoice/InvoiceService.php +++ b/app/Services/Invoice/InvoiceService.php @@ -245,6 +245,13 @@ class InvoiceService return $this; } + + public function setReminder($settings = null) + { + $this->invoice = (new UpdateReminder($this->invoice, $settings))->run(); + + return $this; + } public function setStatus($status) { @@ -425,13 +432,6 @@ class InvoiceService return $this; } - - public function setReminder($settings = null) - { - $this->invoice = (new UpdateReminder($this->invoice, $settings))->run(); - - return $this; - } /** * Saves the invoice. diff --git a/app/Services/Invoice/UpdateReminder.php b/app/Services/Invoice/UpdateReminder.php index 60faac74f108..99a826ddfc22 100644 --- a/app/Services/Invoice/UpdateReminder.php +++ b/app/Services/Invoice/UpdateReminder.php @@ -21,7 +21,7 @@ class UpdateReminder extends AbstractService public $settings; - public function __construct($invoice, $settings = null) + public function __construct(Invoice $invoice, $settings = null) { $this->invoice = $invoice; $this->settings = $settings; diff --git a/resources/views/email/migration/failed.blade.php b/resources/views/email/migration/failed.blade.php index 41c6421e7c97..0b84e5af48dd 100644 --- a/resources/views/email/migration/failed.blade.php +++ b/resources/views/email/migration/failed.blade.php @@ -7,7 +7,11 @@

Looks like your migration failed. Here's the error message:

+    	@if(Ninja::isHosted())
+    		There was a problem with your migration, please send us an email contact@invoiceninja.com
+    	@else
         {!! $exception->getMessage() !!}
         {!! $content !!}
+        @endif
     
@endcomponent diff --git a/tests/Integration/CheckRemindersTest.php b/tests/Integration/CheckRemindersTest.php index 78c5ab89f483..31d1dd7f217d 100644 --- a/tests/Integration/CheckRemindersTest.php +++ b/tests/Integration/CheckRemindersTest.php @@ -19,7 +19,6 @@ use Tests\TestCase; /** * @test - * @covers App\Utils\Traits\MakesReminders */ class CheckRemindersTest extends TestCase { @@ -50,6 +49,8 @@ class CheckRemindersTest extends TestCase $settings->num_days_reminder3 = 1; $this->company->settings = $settings; + $this->company->save(); + $this->invoice->service()->markSent(); $this->invoice->service()->setReminder($settings)->save();