diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index e77c9a5addd8..dbb954d35a60 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -40,7 +40,7 @@ class TaskRepository extends BaseRepository if ($task->id) { $this->new_task = false; } -nlog($data); + $task->fill($data); $task->saveQuietly(); @@ -49,7 +49,6 @@ nlog($data); } if($this->new_task && (!$task->rate || $task->rate <= 0)) { - nlog($task->getRate()); $task->rate = $task->getRate(); } @@ -58,7 +57,7 @@ nlog($data); if (isset($data['description'])) { $task->description = trim($data['description']); } -nlog($task->toArray()); + //todo i can't set it - i need to calculate it. if (isset($data['status_order'])) { $task->status_order = $data['status_order']; diff --git a/app/Services/Quote/ConvertQuoteToProject.php b/app/Services/Quote/ConvertQuoteToProject.php index d15aabd13562..d74b05f49ebd 100644 --- a/app/Services/Quote/ConvertQuoteToProject.php +++ b/app/Services/Quote/ConvertQuoteToProject.php @@ -12,9 +12,12 @@ namespace App\Services\Quote; +use App\DataMapper\InvoiceItem; use App\Models\Quote; use App\Factory\ProjectFactory; +use App\Factory\TaskFactory; use App\Models\Project; +use App\Repositories\TaskRepository; use App\Utils\Traits\GeneratesCounter; class ConvertQuoteToProject @@ -28,7 +31,9 @@ class ConvertQuoteToProject public function run(): Project { - $quote_items = collect($this->quote->line_items); + $quote_items = collect($this->quote->line_items)->filter(function ($item){ + return $item->type_id == '2'; + }); $project = ProjectFactory::create($this->quote->company_id, $this->quote->user_id); $project->name = ctrans('texts.quote_number_short'). " " . $this->quote->number . "[{$this->quote->client->present()->name()}]"; @@ -36,7 +41,7 @@ class ConvertQuoteToProject $project->public_notes = $this->quote->public_notes; $project->private_notes = $this->quote->private_notes; $project->budgeted_hours = $quote_items->sum('quantity') ?? 0; - $project->task_rate = ($this->quote->amount / $project->budgeted_hours) ?? 0; + $project->task_rate = $this->quote->client->getSetting('default_task_rate'); $project->saveQuietly(); $project->number = $this->getNextProjectNumber($project); $project->saveQuietly(); @@ -44,12 +49,28 @@ class ConvertQuoteToProject $this->quote->project_id = $project->id; $this->quote->saveQuietly(); - event('eloquent.created: App\Models\Project', $project); + $task_status = $this->quote->company->task_statuses() + ->whereNull('deleted_at') + ->orderBy('id', 'asc') + ->first(); - $quote_items->each(function($item){ + + $task_repo = new TaskRepository(); + + $quote_items->each(function($item) use($task_repo, $task_status){ + + $task = TaskFactory::create($this->quote->company_id, $this->quote->user_id); + $task->client_id = $this->quote->client_id; + $task->project_id = $this->quote->project_id; + $task->description = $item->notes; + $task->status_id = $task_status->id; + $task->rate = $item->unit_cost; + $task_repo->save([], $task); }); - return $project; + event('eloquent.created: App\Models\Project', $project); + + return $project->fresh(); } } \ No newline at end of file diff --git a/tests/Feature/QuoteTest.php b/tests/Feature/QuoteTest.php index 320a4f1149d0..fd1847da6007 100644 --- a/tests/Feature/QuoteTest.php +++ b/tests/Feature/QuoteTest.php @@ -13,10 +13,12 @@ namespace Tests\Feature; use Tests\TestCase; use App\Models\Quote; +use App\Models\Client; use App\Models\Project; use Tests\MockAccountData; use App\Models\ClientContact; use App\Utils\Traits\MakesHash; +use App\DataMapper\ClientSettings; use App\Exceptions\QuoteConversion; use Illuminate\Database\Eloquent\Model; use Illuminate\Support\Facades\Session; @@ -52,6 +54,72 @@ class QuoteTest extends TestCase ); } + public function testQuoteToProjectConversion2() + { + $settings = ClientSettings::defaults(); + $settings->default_task_rate = 41; + + $c = Client::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'settings' => $settings, + ]); + + $q = Quote::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'client_id' => $c->id, + 'status_id' => 2, + 'date' => now(), + 'line_items' =>[ + [ + 'type_id' => 2, + 'unit_cost' => 200, + 'quantity' => 1, + 'notes' => 'Test200', + ], + [ + 'type_id' => 2, + 'unit_cost' => 100, + 'quantity' => 1, + 'notes' => 'Test100', + ], + [ + 'type_id' => 1, + 'unit_cost' => 10, + 'quantity' => 1, + 'notes' => 'Test', + ], + + ], + ]); + + $q->calc()->getQuote(); + $q->fresh(); + + $p = $q->service()->convertToProject(); + + $this->assertEquals(2, $p->budgeted_hours); + $this->assertEquals(2, $p->tasks()->count()); + + $t = $p->tasks()->where('description', 'Test200')->first(); + + $this->assertEquals(200, $t->rate); + + $t = $p->tasks()->where('description', 'Test100')->first(); + + $this->assertEquals(100, $t->rate); + + + } + + public function testQuoteToProjectConversion() + { + $project = $this->quote->service()->convertToProject(); + + $this->assertInstanceOf('\App\Models\Project', $project); + } + public function testQuoteConversion() { $invoice = $this->quote->service()->convertToInvoice(); @@ -62,7 +130,6 @@ class QuoteTest extends TestCase $invoice = $this->quote->service()->convertToInvoice(); - } public function testQuoteDownloadPDF()