From 3591f9ad28a993870d3b3eecfc79b2d297bc7214 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 13:09:56 +1000 Subject: [PATCH 1/6] Fixes for tests --- app/Utils/Helpers.php | 5 +- composer.lock | 115 +++++++++++++++++++++--------------------- 2 files changed, 62 insertions(+), 58 deletions(-) diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index d3a731d92846..07966956a2c0 100644 --- a/app/Utils/Helpers.php +++ b/app/Utils/Helpers.php @@ -107,8 +107,11 @@ class Helpers * @param Client $client * @return null|string */ - public static function processReservedKeywords(string $value, Client $client): ?string + public static function processReservedKeywords(?string $value, Client $client): ?string { + if(!$value) + return ''; + Carbon::setLocale($client->locale()); $replacements = [ diff --git a/composer.lock b/composer.lock index b00b9a942a9d..0b0bdbc0caac 100644 --- a/composer.lock +++ b/composer.lock @@ -323,16 +323,16 @@ }, { "name": "aws/aws-sdk-php", - "version": "3.194.1", + "version": "3.194.2", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "67bdee05acef9e8ad60098090996690b49babd09" + "reference": "1f0a0cec5721b6346c968533fba9b44e462fc728" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/67bdee05acef9e8ad60098090996690b49babd09", - "reference": "67bdee05acef9e8ad60098090996690b49babd09", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/1f0a0cec5721b6346c968533fba9b44e462fc728", + "reference": "1f0a0cec5721b6346c968533fba9b44e462fc728", "shasum": "" }, "require": { @@ -408,9 +408,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "issues": "https://github.com/aws/aws-sdk-php/issues", - "source": "https://github.com/aws/aws-sdk-php/tree/3.194.1" + "source": "https://github.com/aws/aws-sdk-php/tree/3.194.2" }, - "time": "2021-09-17T18:15:42+00:00" + "time": "2021-09-21T18:14:06+00:00" }, { "name": "bacon/bacon-qr-code", @@ -2652,16 +2652,16 @@ }, { "name": "google/apiclient", - "version": "v2.10.1", + "version": "v2.11.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client.git", - "reference": "11871e94006ce7a419bb6124d51b6f9ace3f679b" + "reference": "7db9eb40c8ba887e81c0fe84f2888a967396cdfb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/11871e94006ce7a419bb6124d51b6f9ace3f679b", - "reference": "11871e94006ce7a419bb6124d51b6f9ace3f679b", + "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/7db9eb40c8ba887e81c0fe84f2888a967396cdfb", + "reference": "7db9eb40c8ba887e81c0fe84f2888a967396cdfb", "shasum": "" }, "require": { @@ -2669,8 +2669,8 @@ "google/apiclient-services": "~0.200", "google/auth": "^1.10", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", - "guzzlehttp/psr7": "^1.2", - "monolog/monolog": "^1.17|^2.0", + "guzzlehttp/psr7": "^1.7||^2.0.0", + "monolog/monolog": "^1.17||^2.0", "php": "^5.6|^7.0|^8.0", "phpseclib/phpseclib": "~2.0||^3.0.2" }, @@ -2679,10 +2679,12 @@ "composer/composer": "^1.10.22", "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "phpcompatibility/php-compatibility": "^9.2", - "phpunit/phpunit": "^5.7||^8.5.13", + "phpspec/prophecy-phpunit": "^1.1||^2.0", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0", "squizlabs/php_codesniffer": "~2.3", "symfony/css-selector": "~2.1", - "symfony/dom-crawler": "~2.1" + "symfony/dom-crawler": "~2.1", + "yoast/phpunit-polyfills": "^1.0" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" @@ -2715,22 +2717,22 @@ ], "support": { "issues": "https://github.com/googleapis/google-api-php-client/issues", - "source": "https://github.com/googleapis/google-api-php-client/tree/v2.10.1" + "source": "https://github.com/googleapis/google-api-php-client/tree/v2.11.0" }, - "time": "2021-06-25T14:25:44+00:00" + "time": "2021-09-20T21:15:55+00:00" }, { "name": "google/apiclient-services", - "version": "v0.212.0", + "version": "v0.213.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client-services.git", - "reference": "2c4bd512502ad9cdfec8ea711ea1592c79d345e5" + "reference": "260311821505438eb9208b068da0d849b8ea9baa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/2c4bd512502ad9cdfec8ea711ea1592c79d345e5", - "reference": "2c4bd512502ad9cdfec8ea711ea1592c79d345e5", + "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/260311821505438eb9208b068da0d849b8ea9baa", + "reference": "260311821505438eb9208b068da0d849b8ea9baa", "shasum": "" }, "require": { @@ -2759,9 +2761,9 @@ ], "support": { "issues": "https://github.com/googleapis/google-api-php-client-services/issues", - "source": "https://github.com/googleapis/google-api-php-client-services/tree/v0.212.0" + "source": "https://github.com/googleapis/google-api-php-client-services/tree/v0.213.0" }, - "time": "2021-09-12T11:18:27+00:00" + "time": "2021-09-19T11:18:26+00:00" }, { "name": "google/auth", @@ -5586,20 +5588,20 @@ }, { "name": "nette/utils", - "version": "v3.2.3", + "version": "v3.2.5", "source": { "type": "git", "url": "https://github.com/nette/utils.git", - "reference": "5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822" + "reference": "9cd80396ca58d7969ab44fc7afcf03624dfa526e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/utils/zipball/5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822", - "reference": "5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822", + "url": "https://api.github.com/repos/nette/utils/zipball/9cd80396ca58d7969ab44fc7afcf03624dfa526e", + "reference": "9cd80396ca58d7969ab44fc7afcf03624dfa526e", "shasum": "" }, "require": { - "php": ">=7.2 <8.1" + "php": ">=7.2 <8.2" }, "conflict": { "nette/di": "<3.0.6" @@ -5665,22 +5667,22 @@ ], "support": { "issues": "https://github.com/nette/utils/issues", - "source": "https://github.com/nette/utils/tree/v3.2.3" + "source": "https://github.com/nette/utils/tree/v3.2.5" }, - "time": "2021-08-16T21:05:00+00:00" + "time": "2021-09-20T10:50:11+00:00" }, { "name": "nikic/php-parser", - "version": "v4.12.0", + "version": "v4.13.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "6608f01670c3cc5079e18c1dab1104e002579143" + "reference": "50953a2691a922aa1769461637869a0a2faa3f53" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6608f01670c3cc5079e18c1dab1104e002579143", - "reference": "6608f01670c3cc5079e18c1dab1104e002579143", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/50953a2691a922aa1769461637869a0a2faa3f53", + "reference": "50953a2691a922aa1769461637869a0a2faa3f53", "shasum": "" }, "require": { @@ -5721,9 +5723,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.12.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.13.0" }, - "time": "2021-07-21T10:44:31+00:00" + "time": "2021-09-20T12:20:58+00:00" }, { "name": "nwidart/laravel-modules", @@ -12740,16 +12742,16 @@ }, { "name": "filp/whoops", - "version": "2.14.1", + "version": "2.14.3", "source": { "type": "git", "url": "https://github.com/filp/whoops.git", - "reference": "15ead64e9828f0fc90932114429c4f7923570cb1" + "reference": "89584ce67dd32307f1063cc43846674f4679feda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filp/whoops/zipball/15ead64e9828f0fc90932114429c4f7923570cb1", - "reference": "15ead64e9828f0fc90932114429c4f7923570cb1", + "url": "https://api.github.com/repos/filp/whoops/zipball/89584ce67dd32307f1063cc43846674f4679feda", + "reference": "89584ce67dd32307f1063cc43846674f4679feda", "shasum": "" }, "require": { @@ -12799,7 +12801,7 @@ ], "support": { "issues": "https://github.com/filp/whoops/issues", - "source": "https://github.com/filp/whoops/tree/2.14.1" + "source": "https://github.com/filp/whoops/tree/2.14.3" }, "funding": [ { @@ -12807,7 +12809,7 @@ "type": "github" } ], - "time": "2021-08-29T12:00:00+00:00" + "time": "2021-09-19T12:00:00+00:00" }, { "name": "friendsofphp/php-cs-fixer", @@ -13290,33 +13292,32 @@ }, { "name": "nunomaduro/collision", - "version": "v5.9.0", + "version": "v5.10.0", "source": { "type": "git", "url": "https://github.com/nunomaduro/collision.git", - "reference": "63456f5c3e8c4bc52bd573e5c85674d64d84fd43" + "reference": "3004cfa49c022183395eabc6d0e5207dfe498d00" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nunomaduro/collision/zipball/63456f5c3e8c4bc52bd573e5c85674d64d84fd43", - "reference": "63456f5c3e8c4bc52bd573e5c85674d64d84fd43", + "url": "https://api.github.com/repos/nunomaduro/collision/zipball/3004cfa49c022183395eabc6d0e5207dfe498d00", + "reference": "3004cfa49c022183395eabc6d0e5207dfe498d00", "shasum": "" }, "require": { "facade/ignition-contracts": "^1.0", - "filp/whoops": "^2.7.2", + "filp/whoops": "^2.14.3", "php": "^7.3 || ^8.0", "symfony/console": "^5.0" }, "require-dev": { "brianium/paratest": "^6.1", "fideloper/proxy": "^4.4.1", - "friendsofphp/php-cs-fixer": "^3.0", "fruitcake/laravel-cors": "^2.0.3", - "laravel/framework": "^8.0 || ^9.0", + "laravel/framework": "8.x-dev", "nunomaduro/larastan": "^0.6.2", "nunomaduro/mock-final-classes": "^1.0", - "orchestra/testbench": "^6.0 || ^7.0", + "orchestra/testbench": "^6.0", "phpstan/phpstan": "^0.12.64", "phpunit/phpunit": "^9.5.0" }, @@ -13374,7 +13375,7 @@ "type": "patreon" } ], - "time": "2021-08-26T15:32:09+00:00" + "time": "2021-09-20T15:06:32+00:00" }, { "name": "openlss/lib-array2xml", @@ -15274,16 +15275,16 @@ }, { "name": "swagger-api/swagger-ui", - "version": "v3.52.2", + "version": "v3.52.3", "source": { "type": "git", "url": "https://github.com/swagger-api/swagger-ui.git", - "reference": "e5611d72ff6b4affb373fa8859cc5feb6981f367" + "reference": "aa9f2e6733327b5f042f2529db76558d9c09bed2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swagger-api/swagger-ui/zipball/e5611d72ff6b4affb373fa8859cc5feb6981f367", - "reference": "e5611d72ff6b4affb373fa8859cc5feb6981f367", + "url": "https://api.github.com/repos/swagger-api/swagger-ui/zipball/aa9f2e6733327b5f042f2529db76558d9c09bed2", + "reference": "aa9f2e6733327b5f042f2529db76558d9c09bed2", "shasum": "" }, "type": "library", @@ -15329,9 +15330,9 @@ ], "support": { "issues": "https://github.com/swagger-api/swagger-ui/issues", - "source": "https://github.com/swagger-api/swagger-ui/tree/v3.52.2" + "source": "https://github.com/swagger-api/swagger-ui/tree/v3.52.3" }, - "time": "2021-09-13T12:46:28+00:00" + "time": "2021-09-20T12:12:56+00:00" }, { "name": "symfony/debug", @@ -15901,5 +15902,5 @@ "platform-dev": { "php": "^7.3|^7.4|^8.0" }, - "plugin-api-version": "2.1.0" + "plugin-api-version": "2.0.0" } From 8794753382939205622129d8f5842d930f09e189 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 18:47:57 +1000 Subject: [PATCH 2/6] Fixes for test data --- app/Console/Commands/CreateTestData.php | 8 +- .../ClientPortal/PaymentController.php | 4 +- app/Services/Ledger/LedgerService.php | 21 + tests/Feature/LoadTest.php | 472 ++++++++++++++++++ tests/Feature/RecurringInvoiceTest.php | 2 - 5 files changed, 502 insertions(+), 5 deletions(-) create mode 100644 tests/Feature/LoadTest.php diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 99ff15054929..b98205e70c2c 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -29,6 +29,7 @@ use App\Models\Expense; use App\Models\Product; use App\Models\Project; use App\Models\Quote; +use App\Models\RecurringInvoice; use App\Models\Task; use App\Models\User; use App\Models\Vendor; @@ -532,7 +533,9 @@ class CreateTestData extends Command $invoice->save(); $invoice->service()->createInvitations()->markSent(); - $this->invoice_repo->markSent($invoice); + if (rand(0, 1)) { + $this->invoice_repo->markSent($invoice); + } if (rand(0, 1)) { $invoice = $invoice->service()->markPaid()->save(); @@ -545,6 +548,9 @@ class CreateTestData extends Command 'documentable_id' => $invoice->id ]); + RecurringInvoice::factory()->create(['user_id' => $invoice->user->id, 'company_id' => $invoice->company->id, 'client_id' => $invoice->client_id]); + + event(new InvoiceWasCreated($invoice, $invoice->company, Ninja::eventVars())); } diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index ac451c505805..e4bed3334897 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -223,7 +223,7 @@ class PaymentController extends Controller $invoice_totals = $payable_invoices->sum('amount'); $first_invoice = $invoices->first(); $credit_totals = $first_invoice->client->getSetting('use_credits_payment') == 'always' ? $first_invoice->client->service()->getCreditBalance() : 0; - $starting_invoice_amount = $first_invoice->amount; + $starting_invoice_amount = $first_invoice->balance; if ($gateway) { $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); @@ -234,7 +234,7 @@ class PaymentController extends Controller * by adding it as a line item, and then subtract * the starting and finishing amounts of the invoice. */ - $fee_totals = $first_invoice->amount - $starting_invoice_amount; + $fee_totals = $first_invoice->balance - $starting_invoice_amount; if ($gateway) { $tokens = auth()->user()->client->gateway_tokens() diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 13498a41c7fd..f905eed815a0 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -28,6 +28,8 @@ class LedgerService { $balance = 0; + \DB::connection(config('database.default'))->beginTransaction(); + $company_ledger = $this->ledger(); if ($company_ledger) { @@ -44,6 +46,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } @@ -51,6 +55,8 @@ class LedgerService { $balance = 0; + \DB::connection(config('database.default'))->beginTransaction(); + /* Get the last record for the client and set the current balance*/ $company_ledger = $this->ledger(); @@ -68,12 +74,16 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } public function updateCreditBalance($adjustment, $notes = '') { $balance = 0; + + \DB::connection(config('database.default'))->beginTransaction(); $company_ledger = $this->ledger(); @@ -91,6 +101,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } @@ -99,6 +111,7 @@ class LedgerService return CompanyLedger::whereClientId($this->entity->client_id) ->whereCompanyId($this->entity->company_id) ->orderBy('id', 'DESC') + ->lockForUpdate() ->first(); } @@ -109,3 +122,11 @@ class LedgerService return $this->entity; } } + +/* + DB::connection(config('database.default'))->beginTransaction(); + + \DB::connection(config('database.default'))->commit(); + + +*/ diff --git a/tests/Feature/LoadTest.php b/tests/Feature/LoadTest.php new file mode 100644 index 000000000000..fb6a7b09240d --- /dev/null +++ b/tests/Feature/LoadTest.php @@ -0,0 +1,472 @@ +markTestSkipped('Skip test not needed in this environment'); + } + + public function testLoad() + { + + $account = Account::factory()->create(); + $company = Company::factory()->create([ + 'account_id' => $account->id, + 'slack_webhook_url' => config('ninja.notification.slack'), + ]); + + $account->default_company_id = $company->id; + $account->save(); + + $user = User::whereEmail('small@example.com')->first(); + + if (! $user) { + $user = User::factory()->create([ + 'account_id' => $account->id, + 'email' => 'small@example.com', + 'confirmation_code' => $this->createDbHash(config('database.default')), + ]); + } + + $company_token = new CompanyToken; + $company_token->user_id = $user->id; + $company_token->company_id = $company->id; + $company_token->account_id = $account->id; + $company_token->name = 'test token'; + $company_token->token = Str::random(64); + $company_token->is_system = true; + + $company_token->save(); + + $user->companies()->attach($company->id, [ + 'account_id' => $account->id, + 'is_owner' => 1, + 'is_admin' => 1, + 'is_locked' => 0, + 'notifications' => CompanySettings::notificationDefaults(), + // 'permissions' => '', + 'settings' => null, + ]); + + dispatch(function () use($user, $company){ + + Product::factory()->count(500)->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + ]); + + }); + + for ($x = 0; $x < $this->count * 100; $x++) { + $z = $x + 1; + + $this->createClient($company, $user); + + } + + do{ + sleep(3); + } + while($company->clients()->count() != 500); + + + for ($x = 0; $x < $this->count * 100 ; $x++) { + + $client = $company->clients->random(); + + $this->createInvoice($client); + + $client = $company->clients->random(); + + $this->createCredit($client); + + $client = $company->clients->random(); + + $this->createQuote($client); + + $client = $company->clients->random(); + + $this->createExpense($client); + + $client = $company->clients->random(); + + $this->createVendor($client); + + $client = $company->clients->random(); + + $this->createTask($client); + + $client = $company->clients->random(); + + $this->createProject($client); + } + + } + + + private function createClient($company, $user) + { + + + dispatch(function () use ($company, $user){ + + $client = Client::factory()->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'country_id' => 840, + ]); + + + Document::factory()->count(2)->create([ + 'user_id' => $user->id, + 'company_id' => $company->id, + 'documentable_type' => Client::class, + 'documentable_id' => $client->id + ]); + + ClientContact::factory()->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + 'is_primary' => 1, + ]); + + ClientContact::factory()->count(2)->create([ + 'user_id' => $user->id, + 'client_id' => $client->id, + 'company_id' => $company->id, + ]); + + $client->number = Str::random(28); + + $settings = $client->settings; + $settings->currency_id = (string) rand(1, 79); + $client->settings = $settings; + $client->save(); + + }); + + } + + private function createExpense($client) + { + dispatch(function () use($client){ + Expense::factory()->count(rand(1, 5))->create([ + 'user_id' => $client->user->id, + 'client_id' => $client->id, + 'company_id' => $client->company->id, + ]); + }); + } + + private function createVendor($client) + { + + dispatch(function () use($client){ + + $vendor = Vendor::factory()->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company->id, + ]); + + Document::factory()->count(2)->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company_id, + 'documentable_type' => Vendor::class, + 'documentable_id' => $vendor->id + ]); + + VendorContact::factory()->create([ + 'user_id' => $client->user->id, + 'vendor_id' => $vendor->id, + 'company_id' => $client->company->id, + 'is_primary' => 1, + ]); + + VendorContact::factory()->count(rand(1, 500))->create([ + 'user_id' => $client->user->id, + 'vendor_id' => $vendor->id, + 'company_id' => $client->company->id, + 'is_primary' => 0, + ]); + + }); + + } + + private function createTask($client) + { + + dispatch(function () use($client){ + + $vendor = Task::factory()->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company->id, + ]); + + + Document::factory()->count(5)->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company_id, + 'documentable_type' => Task::class, + 'documentable_id' => $vendor->id + ]); + + }); + } + + private function createProject($client) + { + + dispatch(function () use($client){ + + $vendor = Project::factory()->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company->id, + ]); + + Document::factory()->count(5)->create([ + 'user_id' => $client->user->id, + 'company_id' => $client->company_id, + 'documentable_type' => Project::class, + 'documentable_id' => $vendor->id + ]); + + }); + + } + + private function createInvoice($client) + { + + $faker = \Faker\Factory::create(); + + $invoice = InvoiceFactory::create($client->company->id, $client->user->id); //stub the company and user_id + $invoice->client_id = $client->id; + $dateable = \Carbon\Carbon::now()->subDays(rand(0, 90)); + $invoice->date = $dateable; + + $invoice->line_items = $this->buildLineItems(rand(1, 10)); + $invoice->uses_inclusive_taxes = false; + + if (rand(0, 1)) { + $invoice->tax_name1 = 'GST'; + $invoice->tax_rate1 = 10.00; + } + + if (rand(0, 1)) { + $invoice->tax_name2 = 'VAT'; + $invoice->tax_rate2 = 17.50; + } + + if (rand(0, 1)) { + $invoice->tax_name3 = 'CA Sales Tax'; + $invoice->tax_rate3 = 5; + } + + $invoice->custom_value1 = $faker->date; + $invoice->custom_value2 = rand(0, 1) ? 'yes' : 'no'; + + $invoice->save(); + + $invoice_calc = new InvoiceSum($invoice); + $invoice_calc->build(); + + $invoice = $invoice_calc->getInvoice(); + + $invoice->save(); + $invoice->service()->createInvitations()->markSent(); + + if (rand(0, 1)) { + $invoice_repo = new InvoiceRepository(); + $invoice_repo->markSent($invoice); + } + + if (rand(0, 1)) { + $invoice = $invoice->service()->markPaid()->save(); + } + + Document::factory()->count(5)->create([ + 'user_id' => $invoice->user->id, + 'company_id' => $invoice->company_id, + 'documentable_type' => Invoice::class, + 'documentable_id' => $invoice->id + ]); + + + + } + + + private function createCredit($client) + { + $faker = \Faker\Factory::create(); + + $credit = Credit::factory()->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]); + + $dateable = \Carbon\Carbon::now()->subDays(rand(0, 90)); + $credit->date = $dateable; + + $credit->line_items = $this->buildLineItems(rand(1, 10)); + $credit->uses_inclusive_taxes = false; + + if (rand(0, 1)) { + $credit->tax_name1 = 'GST'; + $credit->tax_rate1 = 10.00; + } + + if (rand(0, 1)) { + $credit->tax_name2 = 'VAT'; + $credit->tax_rate2 = 17.50; + } + + if (rand(0, 1)) { + $credit->tax_name3 = 'CA Sales Tax'; + $credit->tax_rate3 = 5; + } + + $credit->save(); + + $invoice_calc = new InvoiceSum($credit); + $invoice_calc->build(); + + $credit = $invoice_calc->getCredit(); + + $credit->save(); + $credit->service()->markSent()->save(); + $credit->service()->createInvitations(); + } + + private function createQuote($client) + { + $faker = \Faker\Factory::create(); + + //$quote = QuoteFactory::create($client->company->id, $client->user->id);//stub the company and user_id + $quote = Quote::factory()->create(['user_id' => $client->user->id, 'company_id' => $client->company->id, 'client_id' => $client->id]); + $quote->date = $faker->date(); + $quote->client_id = $client->id; + + $quote->setRelation('client', $client); + + $quote->line_items = $this->buildLineItems(rand(1, 10)); + $quote->uses_inclusive_taxes = false; + + if (rand(0, 1)) { + $quote->tax_name1 = 'GST'; + $quote->tax_rate1 = 10.00; + } + + if (rand(0, 1)) { + $quote->tax_name2 = 'VAT'; + $quote->tax_rate2 = 17.50; + } + + if (rand(0, 1)) { + $quote->tax_name3 = 'CA Sales Tax'; + $quote->tax_rate3 = 5; + } + + $quote->save(); + + $quote_calc = new InvoiceSum($quote); + $quote_calc->build(); + + $quote = $quote_calc->getQuote(); + + $quote->save(); + + $quote->service()->markSent()->save(); + $quote->service()->createInvitations(); + } + + private function buildLineItems($count = 1) + { + $line_items = []; + + for ($x = 0; $x < $count; $x++) { + $item = InvoiceItemFactory::create(); + $item->quantity = 1; + //$item->cost = 10; + + if (rand(0, 1)) { + $item->tax_name1 = 'GST'; + $item->tax_rate1 = 10.00; + } + + if (rand(0, 1)) { + $item->tax_name1 = 'VAT'; + $item->tax_rate1 = 17.50; + } + + if (rand(0, 1)) { + $item->tax_name1 = 'Sales Tax'; + $item->tax_rate1 = 5; + } + + $product = Product::all()->random(); + + $item->cost = (float) $product->cost; + $item->product_key = $product->product_key; + $item->notes = $product->notes; + $item->custom_value1 = $product->custom_value1; + $item->custom_value2 = $product->custom_value2; + $item->custom_value3 = $product->custom_value3; + $item->custom_value4 = $product->custom_value4; + + $line_items[] = $item; + } + + return $line_items; + } +} diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index 746b3a78878f..7661ce7b37a2 100644 --- a/tests/Feature/RecurringInvoiceTest.php +++ b/tests/Feature/RecurringInvoiceTest.php @@ -141,8 +141,6 @@ class RecurringInvoiceTest extends TestCase ])->put('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id), $RecurringInvoice_update) ->assertStatus(200); - - $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, From 449649b74840d2b40c51d68792b126d68eebc289 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 18:48:33 +1000 Subject: [PATCH 3/6] Minor fixes --- app/Services/Invoice/AutoBillInvoice.php | 10 +- app/Utils/Helpers.php | 167 +++++++++++++++++++++++ app/Utils/HtmlEngine.php | 6 +- app/Utils/Traits/MakesInvoiceValues.php | 166 +--------------------- composer.json | 2 +- composer.lock | 115 ++++++++-------- 6 files changed, 238 insertions(+), 228 deletions(-) diff --git a/app/Services/Invoice/AutoBillInvoice.php b/app/Services/Invoice/AutoBillInvoice.php index a64b568a988e..480ba0e7fb1c 100644 --- a/app/Services/Invoice/AutoBillInvoice.php +++ b/app/Services/Invoice/AutoBillInvoice.php @@ -70,7 +70,7 @@ class AutoBillInvoice extends AbstractService /* Determine $amount */ if ($this->invoice->partial > 0) { $is_partial = true; - $invoice_total = $this->invoice->amount; + $invoice_total = $this->invoice->balance; $amount = $this->invoice->partial; } elseif ($this->invoice->balance > 0) { $amount = $this->invoice->balance; @@ -94,10 +94,14 @@ class AutoBillInvoice extends AbstractService /* $gateway fee */ $this->invoice = $this->invoice->service()->addGatewayFee($gateway_token->gateway, $gateway_token->gateway_type_id, $amount)->save(); + //change from $this->invoice->amount to $this->invoice->balance if($is_partial) - $fee = $this->invoice->amount - $invoice_total; + $fee = $this->invoice->balance - $invoice_total; else - $fee = $this->invoice->amount - $amount; + $fee = $this->invoice->balance - $amount; + + if($fee > $amount) + $fee = 0; /* Build payment hash */ $payment_hash = PaymentHash::create([ diff --git a/app/Utils/Helpers.php b/app/Utils/Helpers.php index 789de53e6b5d..07966956a2c0 100644 --- a/app/Utils/Helpers.php +++ b/app/Utils/Helpers.php @@ -14,6 +14,8 @@ namespace App\Utils; use App\Models\Client; use App\Utils\Traits\MakesDates; +use Carbon\Carbon; +use Illuminate\Support\Str; use stdClass; class Helpers @@ -97,4 +99,169 @@ class Helpers return ''; } + + /** + * Process reserved keywords on PDF. + * + * @param string $value + * @param Client $client + * @return null|string + */ + public static function processReservedKeywords(?string $value, Client $client): ?string + { + if(!$value) + return ''; + + Carbon::setLocale($client->locale()); + + $replacements = [ + 'literal' => [ + ':MONTH' => Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F'), + ':YEAR' => now()->year, + ':QUARTER' => 'Q' . now()->quarter, + ':WEEK_BEFORE' => \sprintf( + '%s %s %s', + Carbon::now()->subDays(7)->translatedFormat($client->date_format()), + ctrans('texts.to'), + Carbon::now()->translatedFormat($client->date_format()) + ), + ':WEEK_AHEAD' => \sprintf( + '%s %s %s', + Carbon::now()->addDays(7)->translatedFormat($client->date_format()), + ctrans('texts.to'), + Carbon::now()->addDays(14)->translatedFormat($client->date_format()) + ), + ':WEEK' => \sprintf( + '%s %s %s', + Carbon::now()->translatedFormat($client->date_format()), + ctrans('texts.to'), + Carbon::now()->addDays(7)->translatedFormat($client->date_format()) + ), + ], + 'raw' => [ + ':MONTH' => now()->month, + ':YEAR' => now()->year, + ':QUARTER' => now()->quarter, + ], + 'ranges' => [ + 'MONTHYEAR' => Carbon::createFromDate(now()->year, now()->month), + ], + 'ranges_raw' => [ + 'MONTH' => now()->month, + 'YEAR' => now()->year, + ], + ]; + + // First case, with ranges. + preg_match_all('/\[(.*?)]/', $value, $ranges); + + $matches = array_shift($ranges); + + foreach ($matches as $match) { + if (!Str::contains($match, '|')) { + continue; + } + + if (Str::contains($match, '|')) { + $parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ] + + $left = substr($parts[0], 1); // 'MONTH' + $right = substr($parts[1], 0, -1); // MONTH+2 + + // If left side is not part of replacements, skip. + if (!array_key_exists($left, $replacements['ranges'])) { + continue; + } + + $_left = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y'); + $_right = ''; + + // If right side doesn't have any calculations, replace with raw ranges keyword. + if (!Str::contains($right, ['-', '+', '/', '*'])) { + $_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y'); + } + + // If right side contains one of math operations, calculate. + if (Str::contains($right, ['+'])) { + $operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $right, $_matches); + + $_operation = array_shift($_matches)[0]; // + - + + $_value = explode($_operation, $right); // [MONTHYEAR, 4] + + $_right = Carbon::createFromDate(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y'); + } + + $replacement = sprintf('%s to %s', $_left, $_right); + + $value = preg_replace( + sprintf('/%s/', preg_quote($match)), $replacement, $value, 1 + ); + } + } + + + // Second case with more common calculations. + preg_match_all('/:([^:\s]+)/', $value, $common); + + $matches = array_shift($common); + + foreach ($matches as $match) { + $matches = collect($replacements['literal'])->filter(function ($value, $key) use ($match) { + return Str::startsWith($match, $key); + }); + + if ($matches->count() === 0) { + continue; + } + + if (!Str::contains($match, ['-', '+', '/', '*'])) { + $value = preg_replace( + sprintf('/%s/', $matches->keys()->first()), $replacements['literal'][$matches->keys()->first()], $value, 1 + ); + } + + if (Str::contains($match, ['-', '+', '/', '*'])) { + $operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $match, $_matches); + + $_operation = array_shift($_matches)[0]; + + $_value = explode($_operation, $match); // [:MONTH, 4] + + $raw = strtr($matches->keys()->first(), $replacements['raw']); // :MONTH => 1 + + $number = $res = preg_replace("/[^0-9]/", '', $_value[1]); // :MONTH+1. || :MONTH+2! => 1 || 2 + + $target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1 + + $output = (int) $raw + (int)$_value[1]; + + if ($operation == '+') { + $output = (int) $raw + (int)$_value[1]; // 1 (:MONTH) + 4 + } + + if ($_operation == '-') { + $output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4 + } + + if ($_operation == '/' && (int)$_value[1] != 0) { + $output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4 + } + + if ($_operation == '*') { + $output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4 + } + + if ($matches->keys()->first() == ':MONTH') { + $output = \Carbon\Carbon::create()->month($output)->translatedFormat('F'); + } + + $value = preg_replace( + $target, $output, $value, 1 + ); + } + } + + return $value; + } } diff --git a/app/Utils/HtmlEngine.php b/app/Utils/HtmlEngine.php index 0401c14a3868..bf619dcd6eb6 100644 --- a/app/Utils/HtmlEngine.php +++ b/app/Utils/HtmlEngine.php @@ -127,7 +127,7 @@ class HtmlEngine $data['$entity'] = ['value' => '', 'label' => ctrans('texts.invoice')]; $data['$number'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number')]; $data['$number_short'] = ['value' => $this->entity->number ?: ' ', 'label' => ctrans('texts.invoice_number_short')]; - $data['$entity.terms'] = ['value' => $this->entity->terms ?: '', 'label' => ctrans('texts.invoice_terms')]; + $data['$entity.terms'] = ['value' => Helpers::processReservedKeywords($this->entity->terms, $this->client) ?: '', 'label' => ctrans('texts.invoice_terms')]; $data['$terms'] = &$data['$entity.terms']; $data['$view_link'] = ['value' => ''.ctrans('texts.view_invoice').'', 'label' => ctrans('texts.view_invoice')]; $data['$viewLink'] = &$data['$view_link']; @@ -237,7 +237,7 @@ class HtmlEngine $data['$invoice.custom2'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice2', $this->entity->custom_value2, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice2')]; $data['$invoice.custom3'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice3', $this->entity->custom_value3, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice3')]; $data['$invoice.custom4'] = ['value' => $this->helpers->formatCustomFieldValue($this->company->custom_fields, 'invoice4', $this->entity->custom_value4, $this->client) ?: ' ', 'label' => $this->helpers->makeCustomField($this->company->custom_fields, 'invoice4')]; - $data['$invoice.public_notes'] = ['value' => $this->entity->public_notes ?: '', 'label' => ctrans('texts.public_notes')]; + $data['$invoice.public_notes'] = ['value' => Helpers::processReservedKeywords($this->entity->public_notes, $this->client) ?: '', 'label' => ctrans('texts.public_notes')]; $data['$entity.public_notes'] = &$data['$invoice.public_notes']; $data['$public_notes'] = &$data['$invoice.public_notes']; $data['$notes'] = &$data['$public_notes']; @@ -435,7 +435,7 @@ class HtmlEngine $data['$description'] = ['value' => '', 'label' => ctrans('texts.description')]; //$data['$entity_footer'] = ['value' => $this->client->getSetting("{$this->entity_string}_footer"), 'label' => '']; - $data['$entity_footer'] = ['value' => $this->entity->footer, 'label' => '']; + $data['$entity_footer'] = ['value' => Helpers::processReservedKeywords($this->entity->footer, $this->client), 'label' => '']; $data['$page_size'] = ['value' => $this->settings->page_size, 'label' => '']; $data['$page_layout'] = ['value' => property_exists($this->settings, 'page_layout') ? $this->settings->page_layout : 'Portrait', 'label' => '']; diff --git a/app/Utils/Traits/MakesInvoiceValues.php b/app/Utils/Traits/MakesInvoiceValues.php index bf6b263ecf5c..d4a6ffca5748 100644 --- a/app/Utils/Traits/MakesInvoiceValues.php +++ b/app/Utils/Traits/MakesInvoiceValues.php @@ -294,8 +294,8 @@ trait MakesInvoiceValues $data[$key][$table_type.'.item'] = is_null(optional($item)->item) ? $item->product_key : $item->item; $data[$key][$table_type.'.service'] = is_null(optional($item)->service) ? $item->product_key : $item->service; - $data[$key][$table_type.'.notes'] = $this->processReservedKeywords($item->notes); - $data[$key][$table_type.'.description'] = $this->processReservedKeywords($item->notes); + $data[$key][$table_type.'.notes'] = Helpers::processReservedKeywords($item->notes, $this->client); + $data[$key][$table_type.'.description'] = Helpers::processReservedKeywords($item->notes, $this->client); /* need to test here as this is new - 18/09/2021*/ if(!array_key_exists($table_type.'.gross_line_total', $data[$key])) @@ -350,168 +350,6 @@ trait MakesInvoiceValues return $data; } - /** - * Process reserved words like :MONTH :YEAR :QUARTER - * as well as their operations. - * - * @param string $value - * @return string|null - */ - private function processReservedKeywords(string $value): ?string - { - Carbon::setLocale($this->client->locale()); - - $replacements = [ - 'literal' => [ - ':MONTH' => Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F'), - ':YEAR' => now()->year, - ':QUARTER' => 'Q' . now()->quarter, - ':WEEK_BEFORE' => \sprintf( - '%s %s %s', - Carbon::now()->subDays(7)->translatedFormat($this->client->date_format()), - ctrans('texts.to'), - Carbon::now()->translatedFormat($this->client->date_format()) - ), - ':WEEK_AHEAD' => \sprintf( - '%s %s %s', - Carbon::now()->addDays(7)->translatedFormat($this->client->date_format()), - ctrans('texts.to'), - Carbon::now()->addDays(14)->translatedFormat($this->client->date_format()) - ), - ':WEEK' => \sprintf( - '%s %s %s', - Carbon::now()->translatedFormat($this->client->date_format()), - ctrans('texts.to'), - Carbon::now()->addDays(7)->translatedFormat($this->client->date_format()) - ), - ], - 'raw' => [ - ':MONTH' => now()->month, - ':YEAR' => now()->year, - ':QUARTER' => now()->quarter, - ], - 'ranges' => [ - 'MONTHYEAR' => Carbon::createFromDate(now()->year, now()->month), - ], - 'ranges_raw' => [ - 'MONTH' => now()->month, - 'YEAR' => now()->year, - ], - ]; - - // First case, with ranges. - preg_match_all('/\[(.*?)]/', $value, $ranges); - - $matches = array_shift($ranges); - - foreach ($matches as $match) { - if (!Str::contains($match, '|')) { - continue; - } - - if (Str::contains($match, '|')) { - $parts = explode('|', $match); // [ '[MONTH', 'MONTH+2]' ] - - $left = substr($parts[0], 1); // 'MONTH' - $right = substr($parts[1], 0, -1); // MONTH+2 - - // If left side is not part of replacements, skip. - if (!array_key_exists($left, $replacements['ranges'])) { - continue; - } - - $_left = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y'); - $_right = ''; - - // If right side doesn't have any calculations, replace with raw ranges keyword. - if (!Str::contains($right, ['-', '+', '/', '*'])) { - $_right = Carbon::createFromDate(now()->year, now()->month)->translatedFormat('F Y'); - } - - // If right side contains one of math operations, calculate. - if (Str::contains($right, ['+'])) { - $operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $right, $_matches); - - $_operation = array_shift($_matches)[0]; // + - - - $_value = explode($_operation, $right); // [MONTHYEAR, 4] - - $_right = Carbon::createFromDate(now()->year, now()->month)->addMonths($_value[1])->translatedFormat('F Y'); - } - - $replacement = sprintf('%s to %s', $_left, $_right); - - $value = preg_replace( - sprintf('/%s/', preg_quote($match)), $replacement, $value, 1 - ); - } - } - - - // Second case with more common calculations. - preg_match_all('/:([^:\s]+)/', $value, $common); - - $matches = array_shift($common); - - foreach ($matches as $match) { - $matches = collect($replacements['literal'])->filter(function ($value, $key) use ($match) { - return Str::startsWith($match, $key); - }); - - if ($matches->count() === 0) { - continue; - } - - if (!Str::contains($match, ['-', '+', '/', '*'])) { - $value = preg_replace( - sprintf('/%s/', $matches->keys()->first()), $replacements['literal'][$matches->keys()->first()], $value, 1 - ); - } - - if (Str::contains($match, ['-', '+', '/', '*'])) { - $operation = preg_match_all('/(?!^-)[+*\/-](\s?-)?/', $match, $_matches); - - $_operation = array_shift($_matches)[0]; - - $_value = explode($_operation, $match); // [:MONTH, 4] - - $raw = strtr($matches->keys()->first(), $replacements['raw']); // :MONTH => 1 - - $number = $res = preg_replace("/[^0-9]/", '', $_value[1]); // :MONTH+1. || :MONTH+2! => 1 || 2 - - $target = "/{$matches->keys()->first()}\\{$_operation}{$number}/"; // /:$KEYWORD\\$OPERATION$VALUE => /:MONTH\\+1 - - $output = (int) $raw + (int)$_value[1]; - - if ($operation == '+') { - $output = (int) $raw + (int)$_value[1]; // 1 (:MONTH) + 4 - } - - if ($_operation == '-') { - $output = (int)$raw - (int)$_value[1]; // 1 (:MONTH) - 4 - } - - if ($_operation == '/' && (int)$_value[1] != 0) { - $output = (int)$raw / (int)$_value[1]; // 1 (:MONTH) / 4 - } - - if ($_operation == '*') { - $output = (int)$raw * (int)$_value[1]; // 1 (:MONTH) * 4 - } - - if ($matches->keys()->first() == ':MONTH') { - $output = \Carbon\Carbon::create()->month($output)->translatedFormat('F'); - } - - $value = preg_replace( - $target, $output, $value, 1 - ); - } - } - - return $value; - } - /** * Due to the way we are compiling the blade template we * have no ability to iterate, so in the case diff --git a/composer.json b/composer.json index 4e82661626a7..9e3eee567db6 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,7 @@ "league/flysystem-cached-adapter": "^1.1", "league/fractal": "^0.17.0", "league/omnipay": "^3.1", - "livewire/livewire": "^2.4", + "livewire/livewire": "^2.6", "maennchen/zipstream-php": "^1.2", "mollie/mollie-api-php": "^2.36", "nwidart/laravel-modules": "^8.0", diff --git a/composer.lock b/composer.lock index 5b07ef30132a..0b0bdbc0caac 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "54d84c4ecc41d25ece12b91b181e3431", + "content-hash": "96908a391244cbc96eefbb130bd7bed9", "packages": [ { "name": "apimatic/jsonmapper", @@ -323,16 +323,16 @@ }, { "name": "aws/aws-sdk-php", - "version": "3.194.1", + "version": "3.194.2", "source": { "type": "git", "url": "https://github.com/aws/aws-sdk-php.git", - "reference": "67bdee05acef9e8ad60098090996690b49babd09" + "reference": "1f0a0cec5721b6346c968533fba9b44e462fc728" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/67bdee05acef9e8ad60098090996690b49babd09", - "reference": "67bdee05acef9e8ad60098090996690b49babd09", + "url": "https://api.github.com/repos/aws/aws-sdk-php/zipball/1f0a0cec5721b6346c968533fba9b44e462fc728", + "reference": "1f0a0cec5721b6346c968533fba9b44e462fc728", "shasum": "" }, "require": { @@ -408,9 +408,9 @@ "support": { "forum": "https://forums.aws.amazon.com/forum.jspa?forumID=80", "issues": "https://github.com/aws/aws-sdk-php/issues", - "source": "https://github.com/aws/aws-sdk-php/tree/3.194.1" + "source": "https://github.com/aws/aws-sdk-php/tree/3.194.2" }, - "time": "2021-09-17T18:15:42+00:00" + "time": "2021-09-21T18:14:06+00:00" }, { "name": "bacon/bacon-qr-code", @@ -2652,16 +2652,16 @@ }, { "name": "google/apiclient", - "version": "v2.10.1", + "version": "v2.11.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client.git", - "reference": "11871e94006ce7a419bb6124d51b6f9ace3f679b" + "reference": "7db9eb40c8ba887e81c0fe84f2888a967396cdfb" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/11871e94006ce7a419bb6124d51b6f9ace3f679b", - "reference": "11871e94006ce7a419bb6124d51b6f9ace3f679b", + "url": "https://api.github.com/repos/googleapis/google-api-php-client/zipball/7db9eb40c8ba887e81c0fe84f2888a967396cdfb", + "reference": "7db9eb40c8ba887e81c0fe84f2888a967396cdfb", "shasum": "" }, "require": { @@ -2669,8 +2669,8 @@ "google/apiclient-services": "~0.200", "google/auth": "^1.10", "guzzlehttp/guzzle": "~5.3.3||~6.0||~7.0", - "guzzlehttp/psr7": "^1.2", - "monolog/monolog": "^1.17|^2.0", + "guzzlehttp/psr7": "^1.7||^2.0.0", + "monolog/monolog": "^1.17||^2.0", "php": "^5.6|^7.0|^8.0", "phpseclib/phpseclib": "~2.0||^3.0.2" }, @@ -2679,10 +2679,12 @@ "composer/composer": "^1.10.22", "dealerdirect/phpcodesniffer-composer-installer": "^0.7", "phpcompatibility/php-compatibility": "^9.2", - "phpunit/phpunit": "^5.7||^8.5.13", + "phpspec/prophecy-phpunit": "^1.1||^2.0", + "phpunit/phpunit": "^5.7.21 || ^6.0 || ^7.0 || ^8.0 || ^9.0", "squizlabs/php_codesniffer": "~2.3", "symfony/css-selector": "~2.1", - "symfony/dom-crawler": "~2.1" + "symfony/dom-crawler": "~2.1", + "yoast/phpunit-polyfills": "^1.0" }, "suggest": { "cache/filesystem-adapter": "For caching certs and tokens (using Google\\Client::setCache)" @@ -2715,22 +2717,22 @@ ], "support": { "issues": "https://github.com/googleapis/google-api-php-client/issues", - "source": "https://github.com/googleapis/google-api-php-client/tree/v2.10.1" + "source": "https://github.com/googleapis/google-api-php-client/tree/v2.11.0" }, - "time": "2021-06-25T14:25:44+00:00" + "time": "2021-09-20T21:15:55+00:00" }, { "name": "google/apiclient-services", - "version": "v0.212.0", + "version": "v0.213.0", "source": { "type": "git", "url": "https://github.com/googleapis/google-api-php-client-services.git", - "reference": "2c4bd512502ad9cdfec8ea711ea1592c79d345e5" + "reference": "260311821505438eb9208b068da0d849b8ea9baa" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/2c4bd512502ad9cdfec8ea711ea1592c79d345e5", - "reference": "2c4bd512502ad9cdfec8ea711ea1592c79d345e5", + "url": "https://api.github.com/repos/googleapis/google-api-php-client-services/zipball/260311821505438eb9208b068da0d849b8ea9baa", + "reference": "260311821505438eb9208b068da0d849b8ea9baa", "shasum": "" }, "require": { @@ -2759,9 +2761,9 @@ ], "support": { "issues": "https://github.com/googleapis/google-api-php-client-services/issues", - "source": "https://github.com/googleapis/google-api-php-client-services/tree/v0.212.0" + "source": "https://github.com/googleapis/google-api-php-client-services/tree/v0.213.0" }, - "time": "2021-09-12T11:18:27+00:00" + "time": "2021-09-19T11:18:26+00:00" }, { "name": "google/auth", @@ -5586,20 +5588,20 @@ }, { "name": "nette/utils", - "version": "v3.2.3", + "version": "v3.2.5", "source": { "type": "git", "url": "https://github.com/nette/utils.git", - "reference": "5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822" + "reference": "9cd80396ca58d7969ab44fc7afcf03624dfa526e" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nette/utils/zipball/5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822", - "reference": "5c36cc1ba9bb6abb8a9e425cf054e0c3fd5b9822", + "url": "https://api.github.com/repos/nette/utils/zipball/9cd80396ca58d7969ab44fc7afcf03624dfa526e", + "reference": "9cd80396ca58d7969ab44fc7afcf03624dfa526e", "shasum": "" }, "require": { - "php": ">=7.2 <8.1" + "php": ">=7.2 <8.2" }, "conflict": { "nette/di": "<3.0.6" @@ -5665,22 +5667,22 @@ ], "support": { "issues": "https://github.com/nette/utils/issues", - "source": "https://github.com/nette/utils/tree/v3.2.3" + "source": "https://github.com/nette/utils/tree/v3.2.5" }, - "time": "2021-08-16T21:05:00+00:00" + "time": "2021-09-20T10:50:11+00:00" }, { "name": "nikic/php-parser", - "version": "v4.12.0", + "version": "v4.13.0", "source": { "type": "git", "url": "https://github.com/nikic/PHP-Parser.git", - "reference": "6608f01670c3cc5079e18c1dab1104e002579143" + "reference": "50953a2691a922aa1769461637869a0a2faa3f53" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/6608f01670c3cc5079e18c1dab1104e002579143", - "reference": "6608f01670c3cc5079e18c1dab1104e002579143", + "url": "https://api.github.com/repos/nikic/PHP-Parser/zipball/50953a2691a922aa1769461637869a0a2faa3f53", + "reference": "50953a2691a922aa1769461637869a0a2faa3f53", "shasum": "" }, "require": { @@ -5721,9 +5723,9 @@ ], "support": { "issues": "https://github.com/nikic/PHP-Parser/issues", - "source": "https://github.com/nikic/PHP-Parser/tree/v4.12.0" + "source": "https://github.com/nikic/PHP-Parser/tree/v4.13.0" }, - "time": "2021-07-21T10:44:31+00:00" + "time": "2021-09-20T12:20:58+00:00" }, { "name": "nwidart/laravel-modules", @@ -12740,16 +12742,16 @@ }, { "name": "filp/whoops", - "version": "2.14.1", + "version": "2.14.3", "source": { "type": "git", "url": "https://github.com/filp/whoops.git", - "reference": "15ead64e9828f0fc90932114429c4f7923570cb1" + "reference": "89584ce67dd32307f1063cc43846674f4679feda" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/filp/whoops/zipball/15ead64e9828f0fc90932114429c4f7923570cb1", - "reference": "15ead64e9828f0fc90932114429c4f7923570cb1", + "url": "https://api.github.com/repos/filp/whoops/zipball/89584ce67dd32307f1063cc43846674f4679feda", + "reference": "89584ce67dd32307f1063cc43846674f4679feda", "shasum": "" }, "require": { @@ -12799,7 +12801,7 @@ ], "support": { "issues": "https://github.com/filp/whoops/issues", - "source": "https://github.com/filp/whoops/tree/2.14.1" + "source": "https://github.com/filp/whoops/tree/2.14.3" }, "funding": [ { @@ -12807,7 +12809,7 @@ "type": "github" } ], - "time": "2021-08-29T12:00:00+00:00" + "time": "2021-09-19T12:00:00+00:00" }, { "name": "friendsofphp/php-cs-fixer", @@ -13290,33 +13292,32 @@ }, { "name": "nunomaduro/collision", - "version": "v5.9.0", + "version": "v5.10.0", "source": { "type": "git", "url": "https://github.com/nunomaduro/collision.git", - "reference": "63456f5c3e8c4bc52bd573e5c85674d64d84fd43" + "reference": "3004cfa49c022183395eabc6d0e5207dfe498d00" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/nunomaduro/collision/zipball/63456f5c3e8c4bc52bd573e5c85674d64d84fd43", - "reference": "63456f5c3e8c4bc52bd573e5c85674d64d84fd43", + "url": "https://api.github.com/repos/nunomaduro/collision/zipball/3004cfa49c022183395eabc6d0e5207dfe498d00", + "reference": "3004cfa49c022183395eabc6d0e5207dfe498d00", "shasum": "" }, "require": { "facade/ignition-contracts": "^1.0", - "filp/whoops": "^2.7.2", + "filp/whoops": "^2.14.3", "php": "^7.3 || ^8.0", "symfony/console": "^5.0" }, "require-dev": { "brianium/paratest": "^6.1", "fideloper/proxy": "^4.4.1", - "friendsofphp/php-cs-fixer": "^3.0", "fruitcake/laravel-cors": "^2.0.3", - "laravel/framework": "^8.0 || ^9.0", + "laravel/framework": "8.x-dev", "nunomaduro/larastan": "^0.6.2", "nunomaduro/mock-final-classes": "^1.0", - "orchestra/testbench": "^6.0 || ^7.0", + "orchestra/testbench": "^6.0", "phpstan/phpstan": "^0.12.64", "phpunit/phpunit": "^9.5.0" }, @@ -13374,7 +13375,7 @@ "type": "patreon" } ], - "time": "2021-08-26T15:32:09+00:00" + "time": "2021-09-20T15:06:32+00:00" }, { "name": "openlss/lib-array2xml", @@ -15274,16 +15275,16 @@ }, { "name": "swagger-api/swagger-ui", - "version": "v3.52.2", + "version": "v3.52.3", "source": { "type": "git", "url": "https://github.com/swagger-api/swagger-ui.git", - "reference": "e5611d72ff6b4affb373fa8859cc5feb6981f367" + "reference": "aa9f2e6733327b5f042f2529db76558d9c09bed2" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/swagger-api/swagger-ui/zipball/e5611d72ff6b4affb373fa8859cc5feb6981f367", - "reference": "e5611d72ff6b4affb373fa8859cc5feb6981f367", + "url": "https://api.github.com/repos/swagger-api/swagger-ui/zipball/aa9f2e6733327b5f042f2529db76558d9c09bed2", + "reference": "aa9f2e6733327b5f042f2529db76558d9c09bed2", "shasum": "" }, "type": "library", @@ -15329,9 +15330,9 @@ ], "support": { "issues": "https://github.com/swagger-api/swagger-ui/issues", - "source": "https://github.com/swagger-api/swagger-ui/tree/v3.52.2" + "source": "https://github.com/swagger-api/swagger-ui/tree/v3.52.3" }, - "time": "2021-09-13T12:46:28+00:00" + "time": "2021-09-20T12:12:56+00:00" }, { "name": "symfony/debug", From a93baadd1a2ea9f062f92b400d907527b34b0c7d Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 18:50:19 +1000 Subject: [PATCH 4/6] Minor fixeS --- app/Console/Commands/CreateTestData.php | 8 ++++++- .../ClientPortal/PaymentController.php | 4 ++-- app/Services/Ledger/LedgerService.php | 21 +++++++++++++++++++ tests/Feature/RecurringInvoiceTest.php | 2 -- 4 files changed, 30 insertions(+), 5 deletions(-) diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index 99ff15054929..b98205e70c2c 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -29,6 +29,7 @@ use App\Models\Expense; use App\Models\Product; use App\Models\Project; use App\Models\Quote; +use App\Models\RecurringInvoice; use App\Models\Task; use App\Models\User; use App\Models\Vendor; @@ -532,7 +533,9 @@ class CreateTestData extends Command $invoice->save(); $invoice->service()->createInvitations()->markSent(); - $this->invoice_repo->markSent($invoice); + if (rand(0, 1)) { + $this->invoice_repo->markSent($invoice); + } if (rand(0, 1)) { $invoice = $invoice->service()->markPaid()->save(); @@ -545,6 +548,9 @@ class CreateTestData extends Command 'documentable_id' => $invoice->id ]); + RecurringInvoice::factory()->create(['user_id' => $invoice->user->id, 'company_id' => $invoice->company->id, 'client_id' => $invoice->client_id]); + + event(new InvoiceWasCreated($invoice, $invoice->company, Ninja::eventVars())); } diff --git a/app/Http/Controllers/ClientPortal/PaymentController.php b/app/Http/Controllers/ClientPortal/PaymentController.php index ac451c505805..e4bed3334897 100644 --- a/app/Http/Controllers/ClientPortal/PaymentController.php +++ b/app/Http/Controllers/ClientPortal/PaymentController.php @@ -223,7 +223,7 @@ class PaymentController extends Controller $invoice_totals = $payable_invoices->sum('amount'); $first_invoice = $invoices->first(); $credit_totals = $first_invoice->client->getSetting('use_credits_payment') == 'always' ? $first_invoice->client->service()->getCreditBalance() : 0; - $starting_invoice_amount = $first_invoice->amount; + $starting_invoice_amount = $first_invoice->balance; if ($gateway) { $first_invoice->service()->addGatewayFee($gateway, $payment_method_id, $invoice_totals)->save(); @@ -234,7 +234,7 @@ class PaymentController extends Controller * by adding it as a line item, and then subtract * the starting and finishing amounts of the invoice. */ - $fee_totals = $first_invoice->amount - $starting_invoice_amount; + $fee_totals = $first_invoice->balance - $starting_invoice_amount; if ($gateway) { $tokens = auth()->user()->client->gateway_tokens() diff --git a/app/Services/Ledger/LedgerService.php b/app/Services/Ledger/LedgerService.php index 13498a41c7fd..f905eed815a0 100644 --- a/app/Services/Ledger/LedgerService.php +++ b/app/Services/Ledger/LedgerService.php @@ -28,6 +28,8 @@ class LedgerService { $balance = 0; + \DB::connection(config('database.default'))->beginTransaction(); + $company_ledger = $this->ledger(); if ($company_ledger) { @@ -44,6 +46,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } @@ -51,6 +55,8 @@ class LedgerService { $balance = 0; + \DB::connection(config('database.default'))->beginTransaction(); + /* Get the last record for the client and set the current balance*/ $company_ledger = $this->ledger(); @@ -68,12 +74,16 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } public function updateCreditBalance($adjustment, $notes = '') { $balance = 0; + + \DB::connection(config('database.default'))->beginTransaction(); $company_ledger = $this->ledger(); @@ -91,6 +101,8 @@ class LedgerService $this->entity->company_ledger()->save($company_ledger); + \DB::connection(config('database.default'))->commit(); + return $this; } @@ -99,6 +111,7 @@ class LedgerService return CompanyLedger::whereClientId($this->entity->client_id) ->whereCompanyId($this->entity->company_id) ->orderBy('id', 'DESC') + ->lockForUpdate() ->first(); } @@ -109,3 +122,11 @@ class LedgerService return $this->entity; } } + +/* + DB::connection(config('database.default'))->beginTransaction(); + + \DB::connection(config('database.default'))->commit(); + + +*/ diff --git a/tests/Feature/RecurringInvoiceTest.php b/tests/Feature/RecurringInvoiceTest.php index 746b3a78878f..7661ce7b37a2 100644 --- a/tests/Feature/RecurringInvoiceTest.php +++ b/tests/Feature/RecurringInvoiceTest.php @@ -141,8 +141,6 @@ class RecurringInvoiceTest extends TestCase ])->put('/api/v1/recurring_invoices/'.$this->encodePrimaryKey($RecurringInvoice->id), $RecurringInvoice_update) ->assertStatus(200); - - $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, From 320646ae22cc44fa4e1596414fa2c6a4fe8c7475 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 19:12:17 +1000 Subject: [PATCH 5/6] remove back button from subscription purchase page --- .../livewire/billing-portal-purchase.blade.php | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php b/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php index 343444624d69..c02cc44ed202 100644 --- a/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php +++ b/resources/views/portal/ninja2020/components/livewire/billing-portal-purchase.blade.php @@ -69,19 +69,6 @@ - @if(auth('contact')->user()) - - - - - - - {{ ctrans('texts.client_portal') }} - - @endif - @if($subscription->service()->getPlans()->count() > 1)

From 37aec1689dd017e430430e16408adbf84879d268 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 22 Sep 2021 20:29:02 +1000 Subject: [PATCH 6/6] Selectively show recurring dates schedule --- app/Transformers/RecurringInvoiceTransformer.php | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/app/Transformers/RecurringInvoiceTransformer.php b/app/Transformers/RecurringInvoiceTransformer.php index 04611a3871c4..a845e447c105 100644 --- a/app/Transformers/RecurringInvoiceTransformer.php +++ b/app/Transformers/RecurringInvoiceTransformer.php @@ -64,7 +64,8 @@ class RecurringInvoiceTransformer extends EntityTransformer public function transform(RecurringInvoice $invoice) { - return [ + + $data = [ 'id' => $this->encodePrimaryKey($invoice->id), 'user_id' => $this->encodePrimaryKey($invoice->user_id), 'project_id' => $this->encodePrimaryKey($invoice->project_id), @@ -120,13 +121,19 @@ class RecurringInvoiceTransformer extends EntityTransformer 'entity_type' => 'recurringInvoice', 'frequency_id' => (string) $invoice->frequency_id, 'remaining_cycles' => (int) $invoice->remaining_cycles, - //'recurring_dates' => (array) $invoice->recurringDates(), 'recurring_dates' => [], 'auto_bill' => (string) $invoice->auto_bill, 'auto_bill_enabled' => (bool) $invoice->auto_bill_enabled, 'due_date_days' => (string) $invoice->due_date_days ?: '', 'paid_to_date' => (float) $invoice->paid_to_date, 'subscription_id' => (string)$this->encodePrimaryKey($invoice->subscription_id), + ]; + + + if(request()->has('show_dates') && request()->query('show_dates') == 'true') + $data['recurring_dates'] = (array) $invoice->recurringDates(); + + return $data; } }