Add quick sales with cash days, credits and PDF receipts
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]>
This commit is contained in:
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shared\Application\DTOs;
|
||||
|
||||
/**
|
||||
* The company's own details, printed on receipts and other documents.
|
||||
* Empty optional values are null.
|
||||
*/
|
||||
final readonly class CompanyDetails
|
||||
{
|
||||
public function __construct(
|
||||
public string $name,
|
||||
public string $businessId,
|
||||
public ?string $address = null,
|
||||
public ?string $phone = null,
|
||||
public ?string $email = null,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* The EU VAT number of a Finnish business ID, e.g. FI07375462.
|
||||
*/
|
||||
public function vatNumber(): string
|
||||
{
|
||||
return 'FI'.str_replace('-', '', $this->businessId);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,83 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shared\Application\UseCases;
|
||||
|
||||
use App\Modules\Shared\Application\Contracts\Settings;
|
||||
use App\Modules\Shared\Application\DTOs\CompanyDetails;
|
||||
use App\Modules\Shared\Domain\Exceptions\CompanyNameRequired;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
|
||||
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
|
||||
use App\Modules\Shared\Domain\ValueObjects\EmailAddress;
|
||||
|
||||
/**
|
||||
* Reads and saves the company's details (Asetukset → Yritystiedot), kept
|
||||
* in the settings under `shared.company_*`. Who may save is checked by
|
||||
* the caller (admin).
|
||||
*/
|
||||
final class CompanyDetailsStore
|
||||
{
|
||||
private const array KEYS = [
|
||||
'name' => 'shared.company_name',
|
||||
'business_id' => 'shared.company_business_id',
|
||||
'address' => 'shared.company_address',
|
||||
'phone' => 'shared.company_phone',
|
||||
'email' => 'shared.company_email',
|
||||
];
|
||||
|
||||
public function __construct(private Settings $settings) {}
|
||||
|
||||
/**
|
||||
* Null until the details have been saved.
|
||||
*/
|
||||
public function get(): ?CompanyDetails
|
||||
{
|
||||
$name = $this->settings->get(self::KEYS['name']);
|
||||
$businessId = $this->settings->get(self::KEYS['business_id']);
|
||||
|
||||
if ($name === null || $businessId === null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new CompanyDetails(
|
||||
$name,
|
||||
$businessId,
|
||||
$this->settings->get(self::KEYS['address']),
|
||||
$this->settings->get(self::KEYS['phone']),
|
||||
$this->settings->get(self::KEYS['email']),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws CompanyNameRequired
|
||||
* @throws InvalidBusinessId
|
||||
* @throws InvalidEmailAddress
|
||||
*/
|
||||
public function save(CompanyDetails $details): void
|
||||
{
|
||||
$name = trim($details->name);
|
||||
|
||||
if ($name === '' || mb_strlen($name) > 255) {
|
||||
throw new CompanyNameRequired;
|
||||
}
|
||||
|
||||
$businessId = new BusinessId($details->businessId);
|
||||
$email = self::text($details->email);
|
||||
$email = $email === null ? null : (new EmailAddress($email))->value;
|
||||
|
||||
$this->settings->put(self::KEYS['name'], $name);
|
||||
$this->settings->put(self::KEYS['business_id'], $businessId->value);
|
||||
$this->settings->put(self::KEYS['address'], self::text($details->address));
|
||||
$this->settings->put(self::KEYS['phone'], self::text($details->phone));
|
||||
$this->settings->put(self::KEYS['email'], $email);
|
||||
}
|
||||
|
||||
private static function text(?string $value): ?string
|
||||
{
|
||||
$value = $value === null ? '' : trim($value);
|
||||
|
||||
return $value === '' ? null : $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shared\Domain\Exceptions;
|
||||
|
||||
final class CompanyNameRequired extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The company needs a name of at most 255 characters.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,13 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shared\Domain\Exceptions;
|
||||
|
||||
final class InvalidBusinessId extends DomainException
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct('The business ID is not a valid Finnish business ID.');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Modules\Shared\Domain\ValueObjects;
|
||||
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
|
||||
|
||||
/**
|
||||
* Finnish business ID (Y-tunnus), e.g. 0737546-2.
|
||||
*
|
||||
* Seven digits, a hyphen and a check digit. Old six-digit IDs are
|
||||
* accepted and padded with a leading zero.
|
||||
*/
|
||||
final readonly class BusinessId
|
||||
{
|
||||
private const array WEIGHTS = [7, 9, 10, 5, 8, 4, 2];
|
||||
|
||||
public string $value;
|
||||
|
||||
public function __construct(string $value)
|
||||
{
|
||||
$value = trim($value);
|
||||
|
||||
if (preg_match('/^(\d{6,7})-(\d)$/', $value, $matches) !== 1) {
|
||||
throw new InvalidBusinessId;
|
||||
}
|
||||
|
||||
$digits = str_pad($matches[1], 7, '0', STR_PAD_LEFT);
|
||||
|
||||
if (self::checkDigitFor($digits) !== (int) $matches[2]) {
|
||||
throw new InvalidBusinessId;
|
||||
}
|
||||
|
||||
$this->value = "{$digits}-{$matches[2]}";
|
||||
}
|
||||
|
||||
public function equals(self $other): bool
|
||||
{
|
||||
return $this->value === $other->value;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns null when no valid check digit exists (remainder 1).
|
||||
*/
|
||||
private static function checkDigitFor(string $digits): ?int
|
||||
{
|
||||
$sum = 0;
|
||||
|
||||
foreach (str_split($digits) as $index => $digit) {
|
||||
$sum += (int) $digit * self::WEIGHTS[$index];
|
||||
}
|
||||
|
||||
$remainder = $sum % 11;
|
||||
|
||||
return match ($remainder) {
|
||||
0 => 0,
|
||||
1 => null,
|
||||
default => 11 - $remainder,
|
||||
};
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Modules\Shared\Infrastructure\Providers;
|
||||
|
||||
use App\Models\User;
|
||||
use App\Modules\Shared\Application\Contracts\EventDispatcher;
|
||||
use App\Modules\Shared\Application\Contracts\Settings;
|
||||
use App\Modules\Shared\Application\Contracts\TransactionManager;
|
||||
@@ -9,8 +10,11 @@ use App\Modules\Shared\Infrastructure\Events\LaravelEventDispatcher;
|
||||
use App\Modules\Shared\Infrastructure\Persistence\DatabaseSettings;
|
||||
use App\Modules\Shared\Infrastructure\Persistence\LaravelTransactionManager;
|
||||
use App\Modules\Shared\Presentation\Http\Middleware\SetSecurityHeaders;
|
||||
use App\Modules\Users\Domain\Enums\Role;
|
||||
use Illuminate\Contracts\Http\Kernel;
|
||||
use Illuminate\Support\Facades\Gate;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Livewire\Livewire;
|
||||
|
||||
class SharedServiceProvider extends ServiceProvider
|
||||
{
|
||||
@@ -26,5 +30,16 @@ class SharedServiceProvider extends ServiceProvider
|
||||
public function boot(): void
|
||||
{
|
||||
$this->app->make(Kernel::class)->pushMiddleware(SetSecurityHeaders::class);
|
||||
|
||||
$presentationPath = dirname(__DIR__, 2).'/Presentation';
|
||||
|
||||
Livewire::addNamespace('shared', viewPath: $presentationPath.'/Livewire');
|
||||
|
||||
if (! $this->app->routesAreCached()) {
|
||||
$this->loadRoutesFrom($presentationPath.'/Routes/web.php');
|
||||
}
|
||||
|
||||
// Settings, such as the company's details, are the admin's (spec 2).
|
||||
Gate::define('settings.manage', fn (User $user): bool => $user->role === Role::Admin);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
use App\Modules\Shared\Application\DTOs\CompanyDetails;
|
||||
use App\Modules\Shared\Application\UseCases\CompanyDetailsStore;
|
||||
use App\Modules\Shared\Domain\Exceptions\CompanyNameRequired;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidBusinessId;
|
||||
use App\Modules\Shared\Domain\Exceptions\InvalidEmailAddress;
|
||||
use Livewire\Attributes\Locked;
|
||||
use Livewire\Component;
|
||||
|
||||
/**
|
||||
* The company's own details (Yritystiedot) for receipts and other
|
||||
* documents. Only admins change settings (spec 2).
|
||||
*/
|
||||
new class extends Component
|
||||
{
|
||||
public string $name = '';
|
||||
|
||||
public string $business_id = '';
|
||||
|
||||
public string $address = '';
|
||||
|
||||
public string $phone = '';
|
||||
|
||||
public string $email = '';
|
||||
|
||||
#[Locked]
|
||||
public string $statusMessage = '';
|
||||
|
||||
public function mount(CompanyDetailsStore $store): void
|
||||
{
|
||||
$this->authorize('settings.manage');
|
||||
|
||||
$details = $store->get();
|
||||
|
||||
if ($details !== null) {
|
||||
$this->name = $details->name;
|
||||
$this->business_id = $details->businessId;
|
||||
$this->address = $details->address ?? '';
|
||||
$this->phone = $details->phone ?? '';
|
||||
$this->email = $details->email ?? '';
|
||||
}
|
||||
}
|
||||
|
||||
public function save(CompanyDetailsStore $store): void
|
||||
{
|
||||
$this->authorize('settings.manage');
|
||||
$this->statusMessage = '';
|
||||
|
||||
$this->validate([
|
||||
'name' => ['required', 'string', 'max:255'],
|
||||
'business_id' => ['required', 'string', 'max:20'],
|
||||
'address' => ['nullable', 'string', 'max:500'],
|
||||
'phone' => ['nullable', 'string', 'max:50'],
|
||||
'email' => ['nullable', 'string', 'max:255'],
|
||||
], [
|
||||
'name.required' => __('company.errors.name'),
|
||||
'name.max' => __('company.errors.name'),
|
||||
'business_id.required' => __('company.errors.business_id'),
|
||||
'business_id.max' => __('company.errors.business_id'),
|
||||
'address.max' => __('company.errors.address'),
|
||||
'phone.max' => __('company.errors.phone'),
|
||||
'email.max' => __('company.errors.email'),
|
||||
]);
|
||||
|
||||
try {
|
||||
$store->save(new CompanyDetails($this->name, $this->business_id, $this->address, $this->phone, $this->email));
|
||||
} catch (CompanyNameRequired) {
|
||||
$this->addError('name', __('company.errors.name'));
|
||||
|
||||
return;
|
||||
} catch (InvalidBusinessId) {
|
||||
$this->addError('business_id', __('company.errors.business_id'));
|
||||
|
||||
return;
|
||||
} catch (InvalidEmailAddress) {
|
||||
$this->addError('email', __('company.errors.email'));
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
$this->business_id = (string) $store->get()?->businessId;
|
||||
$this->statusMessage = __('company.saved');
|
||||
}
|
||||
|
||||
public function render(): mixed
|
||||
{
|
||||
return $this->view()->title(__('company.title'));
|
||||
}
|
||||
};
|
||||
?>
|
||||
|
||||
<div class="mx-auto max-w-lg space-y-6 p-4 sm:p-6">
|
||||
<div class="space-y-1">
|
||||
<h1 class="text-2xl font-semibold">{{ __('company.title') }}</h1>
|
||||
<p class="text-sm text-zinc-600 dark:text-zinc-300">{{ __('company.intro') }}</p>
|
||||
</div>
|
||||
|
||||
<p role="status" aria-live="polite" class="text-sm font-medium">{{ $statusMessage }}</p>
|
||||
|
||||
<form wire:submit="save" class="space-y-6" novalidate>
|
||||
<x-input wire:model="name" :label="__('company.fields.name')" autocomplete="organization" required />
|
||||
<x-input wire:model="business_id" :label="__('company.fields.business_id')" :hint="__('company.fields.business_id_hint')" autocomplete="off" required />
|
||||
<x-textarea wire:model="address" :label="__('company.fields.address')" :hint="__('company.fields.address_hint')" rows="3" autocomplete="street-address" />
|
||||
<x-input wire:model="phone" type="tel" :label="__('company.fields.phone')" autocomplete="tel" />
|
||||
<x-input wire:model="email" type="email" :label="__('company.fields.email')" autocomplete="email" />
|
||||
|
||||
<x-button submit :text="__('company.save')" />
|
||||
</form>
|
||||
</div>
|
||||
@@ -0,0 +1,10 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
|
||||
Route::middleware(['web', 'auth', 'can:settings.manage'])
|
||||
->prefix('settings')
|
||||
->name('settings.')
|
||||
->group(function () {
|
||||
Route::livewire('/company', 'shared::company')->name('company');
|
||||
});
|
||||
Reference in New Issue
Block a user