54 lines
1.9 KiB
PHP
54 lines
1.9 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Sales\Infrastructure;
|
|
|
|
use App\Modules\Jobs\Application\UseCases\ProvideQuickSaleTask;
|
|
use App\Modules\Sales\Application\Contracts\QuickSaleHours;
|
|
use App\Modules\Shared\Domain\Exceptions\DomainException;
|
|
use App\Modules\TimeTracking\Application\UseCases\AddTaskHours;
|
|
use DateTimeImmutable;
|
|
|
|
/**
|
|
* Books quick sale hours with the Jobs module's yearly quick sale job and
|
|
* the TimeTracking module's task booking, so they are the seller's usual
|
|
* hours: in the weekly view, within 24 hours a day, approved as usual.
|
|
*/
|
|
class HoursForQuickSales implements QuickSaleHours
|
|
{
|
|
public function __construct(
|
|
private ProvideQuickSaleTask $tasks,
|
|
private AddTaskHours $addHours,
|
|
) {}
|
|
|
|
public function bookForOrder(int $userId, DateTimeImmutable $day, int $workTypeId, int $quarters, int $pickupNumber): bool
|
|
{
|
|
return $this->bookWithNote($userId, $day, $workTypeId, $quarters, __('sales.orders.hours_note', ['number' => $pickupNumber]));
|
|
}
|
|
|
|
public function book(int $sellerId, DateTimeImmutable $day, int $workTypeId, int $quarters, int $saleId): bool
|
|
{
|
|
return $this->bookWithNote($sellerId, $day, $workTypeId, $quarters, __('sales.hours_note', ['number' => $saleId]));
|
|
}
|
|
|
|
private function bookWithNote(int $sellerId, DateTimeImmutable $day, int $workTypeId, int $quarters, string $note): bool
|
|
{
|
|
$year = (int) $day->format('Y');
|
|
|
|
try {
|
|
$taskId = $this->tasks->execute($year, $workTypeId, $sellerId, __('sales.job_name', ['year' => $year]));
|
|
$this->addHours->execute(
|
|
$sellerId,
|
|
$taskId,
|
|
$day->format('Y-m-d'),
|
|
number_format($quarters / 4, 2, ',', ''),
|
|
$note,
|
|
);
|
|
} catch (DomainException) {
|
|
// Approved or locked hours, or a day over 24 hours: the sale stands without them.
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
}
|
|
}
|