clock->today(); $credit = $this->transactions->run(function () use ($saleId, $actorUserId, $today): QuickSale { $original = $this->sales->findById($saleId) ?? throw new QuickSaleNotFound($saleId); $this->cashDays->lockForDay($today)->ensureOpen(); if ($this->sales->isCredited($saleId)) { throw new QuickSaleAlreadyCredited; } $credit = QuickSale::creditFor($original, $actorUserId, $this->clock->now(), $today); return $this->sales->add($credit); }); if ($credit->receiptEmail() !== null) { $this->mailer->send((int) $credit->id()); } return (int) $credit->id(); } }