$lines */ private function __construct( private ?int $id, private int $sellerId, private ?int $customerId, private PaymentMethod $paymentMethod, private DateTimeImmutable $soldAt, private DateTimeImmutable $soldOn, private array $lines, private ?int $creditsSaleId, private ?EmailAddress $receiptEmail = null, private ?int $orderId = null, ) { if ($lines === []) { throw new QuickSaleEmpty; } } /** * @param list $lines * @param DateTimeImmutable $soldOn The cash day, in the company's time zone. * * @throws QuickSaleEmpty */ public static function sell(int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, array $lines, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, ?EmailAddress $receiptEmail = null, ?int $orderId = null): self { return new self(null, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, null, $receiptEmail, $orderId); } /** * @throws CreditCannotBeCredited */ public static function creditFor(self $original, int $actorUserId, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn): self { if ($original->isCredit()) { throw new CreditCannotBeCredited; } return new self( null, $actorUserId, $original->customerId, $original->paymentMethod, $soldAt, $soldOn, array_map(fn (QuickSaleLine $line): QuickSaleLine => $line->negated(), $original->lines), $original->id, $original->receiptEmail, ); } /** * @param list $lines */ public static function reconstitute(int $id, int $sellerId, ?int $customerId, PaymentMethod $paymentMethod, DateTimeImmutable $soldAt, DateTimeImmutable $soldOn, array $lines, ?int $creditsSaleId, ?EmailAddress $receiptEmail = null, ?int $orderId = null): self { return new self($id, $sellerId, $customerId, $paymentMethod, $soldAt, $soldOn, $lines, $creditsSaleId, $receiptEmail, $orderId); } public function totalCents(): int { return array_sum(array_map(fn (QuickSaleLine $line): int => $line->totalCents(), $this->lines)); } public function isCredit(): bool { return $this->creditsSaleId !== null; } public function id(): ?int { return $this->id; } public function sellerId(): int { return $this->sellerId; } public function customerId(): ?int { return $this->customerId; } public function paymentMethod(): PaymentMethod { return $this->paymentMethod; } public function soldAt(): DateTimeImmutable { return $this->soldAt; } public function soldOn(): DateTimeImmutable { return $this->soldOn; } /** * @return list */ public function lines(): array { return $this->lines; } public function creditsSaleId(): ?int { return $this->creditsSaleId; } public function receiptEmail(): ?EmailAddress { return $this->receiptEmail; } /** * The quick sale order this sale paid, if any. */ public function orderId(): ?int { return $this->orderId; } }