2023-09-07 06:31:36 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Invoice Ninja (https://invoiceninja.com).
|
|
|
|
*
|
|
|
|
* @link https://github.com/invoiceninja/invoiceninja source repository
|
|
|
|
*
|
|
|
|
* @copyright Copyright (c) 2023. Invoice Ninja LLC (https://invoiceninja.com)
|
|
|
|
*
|
|
|
|
* @license https://www.elastic.co/licensing/elastic-license
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace App\Services\Quote;
|
|
|
|
|
2023-09-07 07:14:03 +02:00
|
|
|
use App\DataMapper\InvoiceItem;
|
2023-09-07 06:31:36 +02:00
|
|
|
use App\Models\Quote;
|
|
|
|
use App\Factory\ProjectFactory;
|
2023-09-07 07:14:03 +02:00
|
|
|
use App\Factory\TaskFactory;
|
2023-09-07 06:31:36 +02:00
|
|
|
use App\Models\Project;
|
2023-09-07 07:14:03 +02:00
|
|
|
use App\Repositories\TaskRepository;
|
2023-09-07 06:31:36 +02:00
|
|
|
use App\Utils\Traits\GeneratesCounter;
|
|
|
|
|
|
|
|
class ConvertQuoteToProject
|
|
|
|
{
|
|
|
|
use GeneratesCounter;
|
|
|
|
|
|
|
|
public function __construct(private Quote $quote)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
public function run(): Project
|
|
|
|
{
|
|
|
|
|
2023-09-07 07:14:03 +02:00
|
|
|
$quote_items = collect($this->quote->line_items)->filter(function ($item){
|
|
|
|
return $item->type_id == '2';
|
|
|
|
});
|
2023-09-07 06:31:36 +02:00
|
|
|
|
|
|
|
$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()}]";
|
|
|
|
$project->client_id = $this->quote->client_id;
|
|
|
|
$project->public_notes = $this->quote->public_notes;
|
|
|
|
$project->private_notes = $this->quote->private_notes;
|
|
|
|
$project->budgeted_hours = $quote_items->sum('quantity') ?? 0;
|
2023-09-07 07:14:03 +02:00
|
|
|
$project->task_rate = $this->quote->client->getSetting('default_task_rate');
|
2023-09-07 06:31:36 +02:00
|
|
|
$project->saveQuietly();
|
|
|
|
$project->number = $this->getNextProjectNumber($project);
|
|
|
|
$project->saveQuietly();
|
|
|
|
|
|
|
|
$this->quote->project_id = $project->id;
|
|
|
|
$this->quote->saveQuietly();
|
|
|
|
|
2023-09-07 07:14:03 +02:00
|
|
|
$task_status = $this->quote->company->task_statuses()
|
|
|
|
->whereNull('deleted_at')
|
|
|
|
->orderBy('id', 'asc')
|
|
|
|
->first();
|
|
|
|
|
|
|
|
|
|
|
|
$task_repo = new TaskRepository();
|
2023-09-07 06:31:36 +02:00
|
|
|
|
2023-09-07 07:14:03 +02:00
|
|
|
$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);
|
2023-09-07 06:31:36 +02:00
|
|
|
|
|
|
|
});
|
|
|
|
|
2023-09-07 07:14:03 +02:00
|
|
|
event('eloquent.created: App\Models\Project', $project);
|
|
|
|
|
|
|
|
return $project->fresh();
|
2023-09-07 06:31:36 +02:00
|
|
|
}
|
|
|
|
}
|