Email quick sale receipts and send them again from the sales list

Co-Authored-By: Claude Sonnet 5.5 <[email protected]>
This commit is contained in:
2026-11-26 16:00:15 +02:00
co-authored by Claude Sonnet 5.5
parent 3faa4cfbc6
commit d160854b79
29 changed files with 679 additions and 34 deletions
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Modules\Sales\Application\UseCases;
use App\Modules\Sales\Application\Contracts\ReceiptMailer;
use App\Modules\Sales\Domain\Contracts\CashDayRepository;
use App\Modules\Sales\Domain\Contracts\QuickSaleRepository;
use App\Modules\Sales\Domain\Entities\QuickSale;
@@ -18,13 +19,16 @@ use App\Modules\Users\Application\Contracts\Clock;
* Credits a quick sale (hyvitys): records the sale's lines negated on
* today's cash day, so a sale of a closed day can be credited too. Hours
* stay as they are: the work was done, and a wrong amount is corrected in
* the weekly view. Who may credit is checked by the caller.
* the weekly view. When the original's receipt went to an email address,
* the credit's receipt is emailed there too. Who may credit is checked by
* the caller.
*/
final class CreditQuickSale
{
public function __construct(
private QuickSaleRepository $sales,
private CashDayRepository $cashDays,
private ReceiptMailer $mailer,
private TransactionManager $transactions,
private Clock $clock,
) {}
@@ -41,7 +45,7 @@ final class CreditQuickSale
{
$today = $this->clock->today();
return $this->transactions->run(function () use ($saleId, $actorUserId, $today): int {
$credit = $this->transactions->run(function () use ($saleId, $actorUserId, $today): QuickSale {
$original = $this->sales->findById($saleId) ?? throw new QuickSaleNotFound($saleId);
$this->cashDays->lockForDay($today)->ensureOpen();
@@ -51,7 +55,13 @@ final class CreditQuickSale
$credit = QuickSale::creditFor($original, $actorUserId, $this->clock->now(), $today);
return (int) $this->sales->add($credit)->id();
return $this->sales->add($credit);
});
if ($credit->receiptEmail() !== null) {
$this->mailer->send((int) $credit->id());
}
return (int) $credit->id();
}
}