diff --git a/app/Console/Commands/CreateTestData.php b/app/Console/Commands/CreateTestData.php index df22a2a24b90..7b79178e2bab 100644 --- a/app/Console/Commands/CreateTestData.php +++ b/app/Console/Commands/CreateTestData.php @@ -135,28 +135,44 @@ class CreateTestData extends Command $this->createClient($company, $user); } - foreach ($company->clients as $client) { + for($x=0; $x<$this->count; $x++) + { + $client = $company->clients->random(); + $this->info('creating invoice for client #'.$client->id); $this->createInvoice($client); + $client = $company->clients->random(); + $this->info('creating credit for client #'.$client->id); $this->createCredit($client); + $client = $company->clients->random(); + $this->info('creating quote for client #'.$client->id); $this->createQuote($client); + $client = $company->clients->random(); + $this->info('creating expense for client #'.$client->id); $this->createExpense($client); + $client = $company->clients->random(); + $this->info('creating vendor for client #'.$client->id); $this->createVendor($client); + $client = $company->clients->random(); + $this->info('creating task for client #'.$client->id); $this->createTask($client); + $client = $company->clients->random(); + $this->info('creating project for client #'.$client->id); $this->createProject($client); } + } private function createMediumAccount() @@ -217,49 +233,42 @@ class CreateTestData extends Command $this->createClient($company, $user); } - foreach ($company->clients as $client) { - $this->info('creating invoice for client #'.$client->id); + for($x=0; $x<$this->count; $x++) + { + $client = $company->clients->random(); - for ($i=0; $i<$this->count; $i++) { - $this->createInvoice($client); - } + $this->info('creating invoice for client #'.$client->id); + $this->createInvoice($client); + + $client = $company->clients->random(); $this->info('creating credit for client #'.$client->id); + $this->createCredit($client); - for ($i=0; $i<$this->count; $i++) { - $this->createCredit($client); - } - + $client = $company->clients->random(); $this->info('creating quote for client #'.$client->id); + $this->createQuote($client); - for ($i=0; $i<$this->count; $i++) { - $this->createQuote($client); - } + $client = $company->clients->random(); $this->info('creating expense for client #'.$client->id); - - for ($i=0; $i<$this->count; $i++) { - $this->createExpense($client); - } + $this->createExpense($client); + + $client = $company->clients->random(); $this->info('creating vendor for client #'.$client->id); - - for ($i=0; $i<$this->count; $i++) { - $this->createVendor($client); - } + $this->createVendor($client); + + $client = $company->clients->random(); $this->info('creating task for client #'.$client->id); - - for ($i=0; $i<$this->count; $i++) { - $this->createTask($client); - } + $this->createTask($client); + + $client = $company->clients->random(); $this->info('creating project for client #'.$client->id); - - for ($i=0; $i<$this->count; $i++) { - $this->createProject($client); - } + $this->createProject($client); } } @@ -322,25 +331,40 @@ class CreateTestData extends Command $this->createClient($company, $user); } - foreach ($company->clients as $client) { + for($x=0; $x<$this->count; $x++) + { + $client = $company->clients->random(); + $this->info('creating invoice for client #'.$client->id); $this->createInvoice($client); + $client = $company->clients->random(); + $this->info('creating credit for client #'.$client->id); $this->createCredit($client); + $client = $company->clients->random(); + $this->info('creating quote for client #'.$client->id); $this->createQuote($client); + $client = $company->clients->random(); + $this->info('creating expense for client #'.$client->id); $this->createExpense($client); + $client = $company->clients->random(); + $this->info('creating vendor for client #'.$client->id); $this->createVendor($client); + $client = $company->clients->random(); + $this->info('creating task for client #'.$client->id); $this->createTask($client); + $client = $company->clients->random(); + $this->info('creating project for client #'.$client->id); $this->createProject($client); } diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 9ecc45ddc161..cfcb8cdac667 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -533,14 +533,13 @@ class QuoteController extends BaseController $this->entity_type = Invoice::class; $this->entity_transformer = InvoiceTransformer::class; - $invoices = $quotes->map(function ($quote, $key) use ($action) { - if (auth()->user()->can('edit', $quote)) { - $invoice = $quote->service()->convertToInvoice(); - return $invoice->id; + $quotes->each(function ($quote, $key) use ($action) { + if (auth()->user()->can('edit', $quote) && $quote->service()->isConvertable()) { + $quote->service()->convertToInvoice(); } }); - return $this->listResponse(Invoice::withTrashed()->whereIn('id', $invoices)->company()); + return $this->listResponse(Quote::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()); } /* diff --git a/app/Services/Quote/QuoteService.php b/app/Services/Quote/QuoteService.php index b9d9b0ebdc70..71424e31c6b4 100644 --- a/app/Services/Quote/QuoteService.php +++ b/app/Services/Quote/QuoteService.php @@ -55,6 +55,7 @@ class QuoteService return $this; $convert_quote = new ConvertQuote($this->quote->client); + $this->invoice = $convert_quote->run($this->quote); $this->quote->fresh(); @@ -125,7 +126,7 @@ class QuoteService return $this; } - public function convertToInvoice() :Invoice + public function convertToInvoice() { //to prevent circular references we need to explicit call this here. @@ -137,6 +138,17 @@ class QuoteService return $this->invoice; } + public function isConvertable() :bool + { + if($this->quote->invoice_id) + return false; + + if($this->quote->status_id == Quote::STATUS_EXPIRED) + return false; + + return true; + } + /** * Saves the quote * @return Quote|null diff --git a/database/factories/ClientFactory.php b/database/factories/ClientFactory.php index 270b23dbdc2a..e00726d139dd 100644 --- a/database/factories/ClientFactory.php +++ b/database/factories/ClientFactory.php @@ -12,11 +12,11 @@ $factory->define(App\Models\Client::class, function (Faker $faker) { 'balance' => 0, 'paid_to_date' => 0, 'vat_number' => $faker->text(25), - 'id_number' => $faker->text(20), - 'custom_value1' => $faker->text(20), - 'custom_value2' => $faker->text(20), - 'custom_value3' => $faker->text(20), - 'custom_value4' => $faker->text(20), + 'id_number' => '', + 'custom_value1' => 'date|'.$faker->date('Y-m-d'), + 'custom_value2' => '', + 'custom_value3' => '', + 'custom_value4' => '', 'address1' => $faker->buildingNumber, 'address2' => $faker->streetAddress, 'city' => $faker->city,