From 9d5c181634710e289d93b4e73f0dae568811bf4d Mon Sep 17 00:00:00 2001 From: = Date: Mon, 15 Aug 2022 13:49:47 +1000 Subject: [PATCH] Convert quote to project tests --- app/Factory/CloneQuoteToProjectFactory.php | 3 +- app/Http/Controllers/QuoteController.php | 36 +++++++++++++--------- tests/Feature/QuoteTest.php | 22 +++++++++++++ 3 files changed, 46 insertions(+), 15 deletions(-) diff --git a/app/Factory/CloneQuoteToProjectFactory.php b/app/Factory/CloneQuoteToProjectFactory.php index 6f304b2e39..7c95e19b25 100644 --- a/app/Factory/CloneQuoteToProjectFactory.php +++ b/app/Factory/CloneQuoteToProjectFactory.php @@ -22,7 +22,8 @@ class CloneQuoteToProjectFactory $project->company_id = $quote->company_id; $project->user_id = $user_id; - + $project->client_id = $quote->client_id; + $project->public_notes = $quote->public_notes; $project->private_notes = $quote->private_notes; $project->budgeted_hours = 0; diff --git a/app/Http/Controllers/QuoteController.php b/app/Http/Controllers/QuoteController.php index 19967d0c87..de549bdccc 100644 --- a/app/Http/Controllers/QuoteController.php +++ b/app/Http/Controllers/QuoteController.php @@ -561,6 +561,28 @@ class QuoteController extends BaseController return $this->listResponse(Quote::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()); } + + if($action == 'convert_to_project') + { + + $quotes->each(function ($quote, $key) use ($action) { + if (auth()->user()->can('edit', $quote)) + { + $project = CloneQuoteToProjectFactory::create($quote, auth()->user()->id); + + if (empty($project->number)) { + $project->number = $this->getNextProjectNumber($project); + + } + $project->save(); + $quote->project_id = $project->id; + $quote->save(); + } + }); + + return $this->listResponse(Quote::withTrashed()->whereIn('id', $this->transformKeys($ids))->company()); + } + /* * Send the other actions to the switch */ @@ -667,20 +689,6 @@ class QuoteController extends BaseController break; - case 'convert_to_project': - - $this->entity_type = Project::class; - $this->entity_transformer = ProjectTransformer::class; - - $project = CloneQuoteToProjectFactory::create($quote, auth()->user()->id); - - if (empty($project->number)) { - $project->number = $this->getNextProjectNumber($project); - $project->save(); - } - - return $this->itemResponse($project); - case 'clone_to_invoice': $this->entity_type = Invoice::class; diff --git a/tests/Feature/QuoteTest.php b/tests/Feature/QuoteTest.php index 81bbe46e19..a6072ade4f 100644 --- a/tests/Feature/QuoteTest.php +++ b/tests/Feature/QuoteTest.php @@ -13,6 +13,7 @@ namespace Tests\Feature; use App\Models\Client; use App\Models\ClientContact; +use App\Models\Project; use App\Models\Quote; use App\Utils\Traits\MakesHash; use Illuminate\Database\Eloquent\Model; @@ -49,6 +50,26 @@ class QuoteTest extends TestCase ); } + + + public function testQuoteConvertToProject() + { + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->post('/api/v1/quotes/bulk',['action' => 'convert_to_project', 'ids' => [$this->quote->hashed_id]]); + + $response->assertStatus(200); + + $res = $response->json(); + + $this->assertNotNull($res['data'][0]['project_id']); + + $project = Project::find($this->decodePrimaryKey($res['data'][0]['project_id'])); + + $this->assertEquals($project->name, ctrans('texts.quote_number_short') . " " . $this->quote->number); + } + public function testQuoteList() { $response = $this->withHeaders([ @@ -139,4 +160,5 @@ class QuoteTest extends TestCase $response->assertStatus(200); } + }