Add money permissions, a read-only price list and VAT rates
- Admins give employees and interns personal money permissions (prices, expenses, quotes, billing) on the user form. Other modules check them through the money.* abilities; admins and management see all amounts, subcontractors none. - Employees and interns get a read-only price list: consumer products with VAT always, hourly rates, other products and the mileage rate only with the price permission. Subcontractors cannot open it. - VAT rates are maintained on the price list page, with one default rate that always stays in use. Products get a VAT rate; changing a rate's percentage applies to every product using it. - Consumer product prices are entered and stored with VAT, so cash prices stay round; the price without VAT is calculated. The product form shows the other price as you type. - Shared VatPercentage value object and Money::addVat()/removeVat(), rounding half up to the cent in integer arithmetic. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
+58
-2
@@ -8,13 +8,17 @@ use App\Modules\Catalog\Application\DTOs\HourlyRateGroup;
|
||||
use App\Modules\Catalog\Application\DTOs\HourlyRateRow;
|
||||
use App\Modules\Catalog\Application\DTOs\ProductSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\ServiceUnitNode;
|
||||
use App\Modules\Catalog\Application\DTOs\VatRateSummary;
|
||||
use App\Modules\Catalog\Application\DTOs\WorkTypeSummary;
|
||||
use App\Modules\Catalog\Application\UseCases\SetMileageRate;
|
||||
use App\Modules\Catalog\Domain\Enums\ProductUnit;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\ProductDuration;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\ProductModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\VatRateModel;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\WorkTypeModel;
|
||||
use App\Modules\Shared\Application\Contracts\Settings;
|
||||
use App\Modules\Shared\Domain\ValueObjects\Money;
|
||||
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
|
||||
|
||||
class EloquentPriceListQueries implements PriceListQueries
|
||||
{
|
||||
@@ -59,7 +63,7 @@ class EloquentPriceListQueries implements PriceListQueries
|
||||
public function products(bool $includeInactive = true): array
|
||||
{
|
||||
return ProductModel::query()
|
||||
->with('workType')
|
||||
->with(['workType', 'vatRate'])
|
||||
->when(! $includeInactive, fn ($query) => $query->where('is_active', true))
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
@@ -70,11 +74,49 @@ class EloquentPriceListQueries implements PriceListQueries
|
||||
|
||||
public function findProduct(int $id): ?ProductSummary
|
||||
{
|
||||
$model = ProductModel::query()->with('workType')->find($id);
|
||||
$model = ProductModel::query()->with(['workType', 'vatRate'])->find($id);
|
||||
|
||||
return $model === null ? null : $this->toSummary($model);
|
||||
}
|
||||
|
||||
public function consumerProducts(): array
|
||||
{
|
||||
return ProductModel::query()
|
||||
->with(['workType', 'vatRate'])
|
||||
->where('is_active', true)
|
||||
->where('is_consumer_product', true)
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->map(fn (ProductModel $model): ProductSummary => $this->toSummary($model))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function vatRates(bool $includeInactive = true): array
|
||||
{
|
||||
return VatRateModel::query()
|
||||
->when(! $includeInactive, fn ($query) => $query->where('is_active', true))
|
||||
->orderBy('sort_order')
|
||||
->get()
|
||||
->map(fn (VatRateModel $model): VatRateSummary => $this->toVatRateSummary($model))
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
|
||||
public function findVatRate(int $id): ?VatRateSummary
|
||||
{
|
||||
$model = VatRateModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toVatRateSummary($model);
|
||||
}
|
||||
|
||||
public function defaultVatRate(): ?VatRateSummary
|
||||
{
|
||||
$model = VatRateModel::query()->where('is_default', true)->first();
|
||||
|
||||
return $model === null ? null : $this->toVatRateSummary($model);
|
||||
}
|
||||
|
||||
public function mileageRateCents(): ?int
|
||||
{
|
||||
$value = $this->settings->get(SetMileageRate::SETTING_KEY);
|
||||
@@ -82,13 +124,27 @@ class EloquentPriceListQueries implements PriceListQueries
|
||||
return $value === null ? null : (int) $value;
|
||||
}
|
||||
|
||||
private function toVatRateSummary(VatRateModel $model): VatRateSummary
|
||||
{
|
||||
return new VatRateSummary($model->id, $model->name, $model->basis_points, $model->is_default, $model->is_active);
|
||||
}
|
||||
|
||||
private function toSummary(ProductModel $model): ProductSummary
|
||||
{
|
||||
$price = Money::fromCents($model->price_cents);
|
||||
$vat = new VatPercentage($model->vatRate->basis_points ?? 0);
|
||||
$includesVat = $model->is_consumer_product;
|
||||
|
||||
return new ProductSummary(
|
||||
$model->id,
|
||||
$model->name,
|
||||
ProductUnit::from($model->unit),
|
||||
$model->price_cents,
|
||||
$includesVat,
|
||||
$includesVat ? $price->removeVat($vat)->cents : $price->cents,
|
||||
$includesVat ? $price->cents : $price->addVat($vat)->cents,
|
||||
(int) $model->vat_rate_id,
|
||||
$vat->basisPoints,
|
||||
$model->is_consumer_product,
|
||||
ProductDuration::fromDecimal((float) $model->auto_hours)->quarters,
|
||||
$model->work_type_id,
|
||||
|
||||
+2
@@ -28,6 +28,7 @@ class EloquentProductRepository implements ProductRepository
|
||||
'name' => $product->name()->value,
|
||||
'unit' => $product->unit()->value,
|
||||
'price_cents' => $product->price()->cents,
|
||||
'vat_rate_id' => $product->vatRateId(),
|
||||
'is_consumer_product' => $product->isConsumerProduct(),
|
||||
'auto_hours' => $product->duration()->decimal(),
|
||||
'work_type_id' => $product->workTypeId(),
|
||||
@@ -68,6 +69,7 @@ class EloquentProductRepository implements ProductRepository
|
||||
new CatalogName($model->name),
|
||||
ProductUnit::from($model->unit),
|
||||
Money::fromCents($model->price_cents),
|
||||
(int) $model->vat_rate_id,
|
||||
$model->is_consumer_product,
|
||||
ProductDuration::fromDecimal((float) $model->auto_hours),
|
||||
$model->work_type_id,
|
||||
|
||||
+77
@@ -0,0 +1,77 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Repositories;
|
||||
|
||||
use App\Modules\Catalog\Domain\Contracts\VatRateRepository;
|
||||
use App\Modules\Catalog\Domain\Entities\VatRate;
|
||||
use App\Modules\Catalog\Domain\Enums\MoveDirection;
|
||||
use App\Modules\Catalog\Domain\ValueObjects\CatalogName;
|
||||
use App\Modules\Catalog\Infrastructure\Persistence\Eloquent\VatRateModel;
|
||||
use App\Modules\Shared\Domain\ValueObjects\VatPercentage;
|
||||
|
||||
class EloquentVatRateRepository implements VatRateRepository
|
||||
{
|
||||
public function findById(int $id): ?VatRate
|
||||
{
|
||||
$model = VatRateModel::query()->find($id);
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function findDefault(): ?VatRate
|
||||
{
|
||||
$model = VatRateModel::query()->where('is_default', true)->lockForUpdate()->first();
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function save(VatRate $vatRate): VatRate
|
||||
{
|
||||
$model = $vatRate->id() === null ? new VatRateModel : VatRateModel::query()->findOrFail($vatRate->id());
|
||||
|
||||
$model->fill([
|
||||
'name' => $vatRate->name()->value,
|
||||
'basis_points' => $vatRate->percentage()->basisPoints,
|
||||
'is_default' => $vatRate->isDefault(),
|
||||
'is_active' => $vatRate->isActive(),
|
||||
'sort_order' => $vatRate->sortOrder(),
|
||||
])->save();
|
||||
|
||||
return $this->toEntity($model);
|
||||
}
|
||||
|
||||
public function isNameTaken(string $name, ?int $exceptId = null): bool
|
||||
{
|
||||
return VatRateModel::query()
|
||||
->whereRaw('LOWER(name) = ?', [mb_strtolower($name)])
|
||||
->when($exceptId !== null, fn ($query) => $query->whereKeyNot($exceptId))
|
||||
->exists();
|
||||
}
|
||||
|
||||
public function nextSortOrder(): int
|
||||
{
|
||||
return (int) VatRateModel::query()->max('sort_order') + 1;
|
||||
}
|
||||
|
||||
public function neighbour(VatRate $vatRate, MoveDirection $direction): ?VatRate
|
||||
{
|
||||
$model = VatRateModel::query()
|
||||
->where('sort_order', $direction === MoveDirection::Up ? '<' : '>', $vatRate->sortOrder())
|
||||
->orderBy('sort_order', $direction === MoveDirection::Up ? 'desc' : 'asc')
|
||||
->first();
|
||||
|
||||
return $model === null ? null : $this->toEntity($model);
|
||||
}
|
||||
|
||||
private function toEntity(VatRateModel $model): VatRate
|
||||
{
|
||||
return VatRate::reconstitute(
|
||||
$model->id,
|
||||
new CatalogName($model->name),
|
||||
new VatPercentage($model->basis_points),
|
||||
$model->is_default,
|
||||
$model->is_active,
|
||||
$model->sort_order,
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user