transactions->run(function () use ($actorUserId): int { if ($this->exports->openDraftId() !== null) { throw new DraftAlreadyOpen; } $export = BillingExport::startDraft($actorUserId, $this->clock->today()); $monthly = $this->jobs->monthly(); $snapshot = $this->snapshots->load($export->period(), [], $monthly); foreach ($monthly as $job) { if ($snapshot->reasonNotBillable($job) === null) { $export->addJob($job->toDraftJob()); } } return (int) $this->exports->save($export)->id(); }); } }