Convert quote to project

This commit is contained in:
David Bomba 2023-09-07 15:14:03 +10:00
parent 83b0f55729
commit d403da6cd9
3 changed files with 96 additions and 9 deletions

View File

@ -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'];

View File

@ -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();
}
}

View File

@ -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()