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:
@@ -17,15 +17,17 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
||||
* @property string $name
|
||||
* @property string $unit
|
||||
* @property int $price_cents
|
||||
* @property int|null $vat_rate_id
|
||||
* @property bool $is_consumer_product
|
||||
* @property string $auto_hours
|
||||
* @property int|null $work_type_id
|
||||
* @property bool $is_active
|
||||
* @property int $sort_order
|
||||
* @property-read WorkTypeModel|null $workType
|
||||
* @property-read VatRateModel|null $vatRate
|
||||
*/
|
||||
#[Table('products')]
|
||||
#[Fillable(['name', 'unit', 'price_cents', 'is_consumer_product', 'auto_hours', 'work_type_id', 'is_active', 'sort_order'])]
|
||||
#[Fillable(['name', 'unit', 'price_cents', 'vat_rate_id', 'is_consumer_product', 'auto_hours', 'work_type_id', 'is_active', 'sort_order'])]
|
||||
#[UseFactory(ProductFactory::class)]
|
||||
class ProductModel extends Model
|
||||
{
|
||||
@@ -40,6 +42,14 @@ class ProductModel extends Model
|
||||
return $this->belongsTo(WorkTypeModel::class, 'work_type_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return BelongsTo<VatRateModel, $this>
|
||||
*/
|
||||
public function vatRate(): BelongsTo
|
||||
{
|
||||
return $this->belongsTo(VatRateModel::class, 'vat_rate_id');
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
@@ -47,6 +57,7 @@ class ProductModel extends Model
|
||||
{
|
||||
return [
|
||||
'price_cents' => 'integer',
|
||||
'vat_rate_id' => 'integer',
|
||||
'is_consumer_product' => 'boolean',
|
||||
'auto_hours' => 'decimal:2',
|
||||
'work_type_id' => 'integer',
|
||||
|
||||
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Modules\Catalog\Infrastructure\Persistence\Eloquent;
|
||||
|
||||
use Database\Factories\VatRateFactory;
|
||||
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
||||
use Illuminate\Database\Eloquent\Attributes\Table;
|
||||
use Illuminate\Database\Eloquent\Attributes\UseFactory;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
/**
|
||||
* Persistence model, used only inside the Catalog Infrastructure layer.
|
||||
*
|
||||
* @property int $id
|
||||
* @property string $name
|
||||
* @property int $basis_points
|
||||
* @property bool $is_default
|
||||
* @property bool $is_active
|
||||
* @property int $sort_order
|
||||
*/
|
||||
#[Table('vat_rates')]
|
||||
#[Fillable(['name', 'basis_points', 'is_default', 'is_active', 'sort_order'])]
|
||||
#[UseFactory(VatRateFactory::class)]
|
||||
class VatRateModel extends Model
|
||||
{
|
||||
/** @use HasFactory<VatRateFactory> */
|
||||
use HasFactory;
|
||||
|
||||
/**
|
||||
* @return array<string, string>
|
||||
*/
|
||||
protected function casts(): array
|
||||
{
|
||||
return [
|
||||
'basis_points' => 'integer',
|
||||
'is_default' => 'boolean',
|
||||
'is_active' => 'boolean',
|
||||
'sort_order' => 'integer',
|
||||
];
|
||||
}
|
||||
}
|
||||
+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