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,