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]>
144 lines
4.2 KiB
PHP
144 lines
4.2 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Modules\Customers\Domain\Entities;
|
|
|
|
use App\Modules\Customers\Domain\Enums\CustomerType;
|
|
use App\Modules\Customers\Domain\Exceptions\CompanyCannotHavePersonalIdentityCode;
|
|
use App\Modules\Customers\Domain\Exceptions\ConsumerCannotHaveBusinessId;
|
|
use App\Modules\Customers\Domain\Exceptions\InternalCustomerMustBeCompany;
|
|
use App\Modules\Customers\Domain\ValueObjects\CustomerName;
|
|
use App\Modules\Customers\Domain\ValueObjects\PersonalIdentityCode;
|
|
use App\Modules\Shared\Domain\ValueObjects\BusinessId;
|
|
|
|
/**
|
|
* A customer of the company: a business or a private consumer.
|
|
*
|
|
* Only the name is needed at job intake; other details are added later.
|
|
* A consumer who is invoiced needs a personal identity code.
|
|
*/
|
|
final class Customer
|
|
{
|
|
private function __construct(
|
|
private ?int $id,
|
|
private CustomerName $name,
|
|
private CustomerType $type,
|
|
private ?BusinessId $businessId,
|
|
private ?PersonalIdentityCode $personalIdentityCode,
|
|
private bool $internal = false,
|
|
) {
|
|
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
|
|
self::ensureInternalIsCompany($internal, $type);
|
|
}
|
|
|
|
/**
|
|
* A new customer that is not stored yet, so it has no id.
|
|
*/
|
|
public static function register(
|
|
CustomerName $name,
|
|
CustomerType $type,
|
|
?BusinessId $businessId = null,
|
|
?PersonalIdentityCode $personalIdentityCode = null,
|
|
): self {
|
|
return new self(null, $name, $type, $businessId, $personalIdentityCode);
|
|
}
|
|
|
|
/**
|
|
* Rebuilds a stored customer. Used only by repositories.
|
|
*/
|
|
public static function reconstitute(
|
|
int $id,
|
|
CustomerName $name,
|
|
CustomerType $type,
|
|
?BusinessId $businessId,
|
|
?PersonalIdentityCode $personalIdentityCode = null,
|
|
bool $internal = false,
|
|
): self {
|
|
return new self($id, $name, $type, $businessId, $personalIdentityCode, $internal);
|
|
}
|
|
|
|
/**
|
|
* Changes the details. A null personal identity code keeps the stored
|
|
* one, except that a company never keeps one.
|
|
*/
|
|
public function changeDetails(
|
|
CustomerName $name,
|
|
CustomerType $type,
|
|
?BusinessId $businessId,
|
|
?PersonalIdentityCode $personalIdentityCode = null,
|
|
): void {
|
|
if ($personalIdentityCode === null && $type === CustomerType::Consumer) {
|
|
$personalIdentityCode = $this->personalIdentityCode;
|
|
}
|
|
|
|
self::ensureIdentifiersFitType($type, $businessId, $personalIdentityCode);
|
|
self::ensureInternalIsCompany($this->internal, $type);
|
|
|
|
$this->name = $name;
|
|
$this->type = $type;
|
|
$this->businessId = $businessId;
|
|
$this->personalIdentityCode = $personalIdentityCode;
|
|
}
|
|
|
|
public function removePersonalIdentityCode(): void
|
|
{
|
|
$this->personalIdentityCode = null;
|
|
}
|
|
|
|
public function id(): ?int
|
|
{
|
|
return $this->id;
|
|
}
|
|
|
|
public function name(): CustomerName
|
|
{
|
|
return $this->name;
|
|
}
|
|
|
|
public function type(): CustomerType
|
|
{
|
|
return $this->type;
|
|
}
|
|
|
|
public function businessId(): ?BusinessId
|
|
{
|
|
return $this->businessId;
|
|
}
|
|
|
|
public function personalIdentityCode(): ?PersonalIdentityCode
|
|
{
|
|
return $this->personalIdentityCode;
|
|
}
|
|
|
|
/**
|
|
* The company itself, whose jobs hold internal work (administration,
|
|
* sales, training). Created by a migration, never through the UI.
|
|
*/
|
|
public function isInternal(): bool
|
|
{
|
|
return $this->internal;
|
|
}
|
|
|
|
private static function ensureInternalIsCompany(bool $internal, CustomerType $type): void
|
|
{
|
|
if ($internal && $type !== CustomerType::Company) {
|
|
throw new InternalCustomerMustBeCompany;
|
|
}
|
|
}
|
|
|
|
private static function ensureIdentifiersFitType(
|
|
CustomerType $type,
|
|
?BusinessId $businessId,
|
|
?PersonalIdentityCode $personalIdentityCode,
|
|
): void {
|
|
if ($type === CustomerType::Consumer && $businessId !== null) {
|
|
throw new ConsumerCannotHaveBusinessId;
|
|
}
|
|
|
|
if ($type === CustomerType::Company && $personalIdentityCode !== null) {
|
|
throw new CompanyCannotHavePersonalIdentityCode;
|
|
}
|
|
}
|
|
}
|