Quick sales (pikamyynti) of active consumer products at the counter, in a new Sales module: - Product buttons add one to the sale's list per press; quantities can be lowered, raised or removed. Payment by cash or card, optional customer. - Product durations are booked as the seller's hours on the yearly "Pikamyynnit" job (internal customer, not billed) through Jobs' ProvideQuickSaleTask and TimeTracking's AddTaskHours. The sale stands even when the hours cannot be booked, and the seller is told. - Sales are never deleted: a credit records the lines negated on today's cash day, once per sale, by the seller or management. - Closing a cash day stores its balance by payment method and VAT rate and stops sales on it; only management reopens a day. - A printable A5 PDF receipt (barryvdh/laravel-dompdf) with the company's details, business ID, VAT number, lines and VAT breakdown. Company details are kept in the settings and edited by the admin only on the new Yritystiedot page. BusinessId moves from Customers to Shared so both use the same check. Co-Authored-By: Claude Opus 5.5 <[email protected]>
117 lines
4.7 KiB
PHP
117 lines
4.7 KiB
PHP
<?php
|
|
|
|
namespace App\Modules\Sales\Infrastructure\Persistence\Repositories;
|
|
|
|
use App\Modules\Customers\Application\Contracts\CustomerQueries;
|
|
use App\Modules\Sales\Application\Contracts\QuickSaleQueries;
|
|
use App\Modules\Sales\Application\DTOs\CashDaySummary;
|
|
use App\Modules\Sales\Application\DTOs\QuickSaleLineSummary;
|
|
use App\Modules\Sales\Application\DTOs\QuickSaleSummary;
|
|
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
|
|
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
|
|
use App\Modules\Sales\Domain\ValueObjects\CashDayBalance;
|
|
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\CashDayModel;
|
|
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleLineModel;
|
|
use App\Modules\Sales\Infrastructure\Persistence\Eloquent\QuickSaleModel;
|
|
use App\Modules\Users\Application\Contracts\UserAccountQueries;
|
|
use DateTimeImmutable;
|
|
use DateTimeInterface;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Collection;
|
|
|
|
class EloquentQuickSaleQueries implements QuickSaleQueries
|
|
{
|
|
public function __construct(
|
|
private CashDayRepository $cashDays,
|
|
private QuickSaleRepository $sales,
|
|
private UserAccountQueries $users,
|
|
private CustomerQueries $customers,
|
|
) {}
|
|
|
|
public function salesOn(string $day, ?int $sellerId = null): array
|
|
{
|
|
$models = QuickSaleModel::query()
|
|
->with(['lines', 'credit'])
|
|
->where('sold_on', $day)
|
|
->when($sellerId !== null, fn ($query) => $query->where('user_id', $sellerId))
|
|
->orderByDesc('sold_at')
|
|
->orderByDesc('id')
|
|
->get();
|
|
|
|
return $this->summaries($models);
|
|
}
|
|
|
|
public function findSale(int $id): ?QuickSaleSummary
|
|
{
|
|
$model = QuickSaleModel::query()->with(['lines', 'credit'])->find($id);
|
|
|
|
return $model === null ? null : $this->summaries(collect([$model]))[0];
|
|
}
|
|
|
|
public function cashDay(string $day): CashDaySummary
|
|
{
|
|
$date = new DateTimeImmutable($day);
|
|
$cashDay = $this->cashDays->forDay($date);
|
|
$names = $this->users->namesByIds(array_values(array_filter([$cashDay->closedBy(), $cashDay->reopenedBy()])));
|
|
|
|
return new CashDaySummary(
|
|
day: $day,
|
|
isClosed: $cashDay->isClosed(),
|
|
balance: $cashDay->balance() ?? CashDayBalance::of($this->sales->forDay($date)),
|
|
closedByName: $cashDay->closedBy() === null ? null : ($names[$cashDay->closedBy()] ?? ''),
|
|
closedAt: $this->localTime($cashDay->closedAt()),
|
|
reopenedByName: $cashDay->reopenedBy() === null ? null : ($names[$cashDay->reopenedBy()] ?? ''),
|
|
reopenedAt: $this->localTime($cashDay->reopenedAt()),
|
|
);
|
|
}
|
|
|
|
public function unclosedDaysBefore(string $day): array
|
|
{
|
|
$closed = CashDayModel::query()->whereNotNull('closed_at')->select('day');
|
|
|
|
return QuickSaleModel::query()
|
|
->where('sold_on', '<', $day)
|
|
->whereNotIn('sold_on', $closed)
|
|
->distinct()
|
|
->orderBy('sold_on')
|
|
->pluck('sold_on')
|
|
->map(fn ($value): string => substr((string) $value, 0, 10))
|
|
->values()
|
|
->all();
|
|
}
|
|
|
|
/**
|
|
* @param Collection<int, QuickSaleModel> $models
|
|
* @return list<QuickSaleSummary>
|
|
*/
|
|
private function summaries(Collection $models): array
|
|
{
|
|
$sellerNames = $this->users->namesByIds($models->pluck('user_id')->unique()->values()->all());
|
|
$customerNames = $this->customers->namesByIds($models->pluck('customer_id')->filter()->unique()->values()->all());
|
|
|
|
return $models->map(fn (QuickSaleModel $model): QuickSaleSummary => new QuickSaleSummary(
|
|
id: $model->id,
|
|
soldAt: (string) $this->localTime($model->sold_at),
|
|
sellerId: $model->user_id,
|
|
sellerName: $sellerNames[$model->user_id] ?? '',
|
|
customerName: $model->customer_id === null ? null : ($customerNames[$model->customer_id] ?? ''),
|
|
paymentMethod: $model->payment_method,
|
|
totalCents: $model->total_cents,
|
|
lines: $model->lines->map(fn (QuickSaleLineModel $line): QuickSaleLineSummary => new QuickSaleLineSummary(
|
|
$line->product_name,
|
|
$line->quantity,
|
|
$line->unit_price_cents,
|
|
$line->quantity * $line->unit_price_cents,
|
|
$line->vat_basis_points,
|
|
))->values()->all(),
|
|
creditsSaleId: $model->credits_quick_sale_id,
|
|
creditedBySaleId: $model->credit?->id,
|
|
))->values()->all();
|
|
}
|
|
|
|
private function localTime(?DateTimeInterface $moment): ?string
|
|
{
|
|
return $moment === null ? null : Carbon::instance($moment)->setTimezone((string) config('app.business_timezone'))->format('Y-m-d H:i');
|
|
}
|
|
}
|