where('customer_id', $customerId)->find($billingUnitId); return $model === null ? null : $this->toEntity($model); } public function save(BillingUnit $billingUnit): BillingUnit { $model = $billingUnit->id() === null ? new BillingUnitModel : BillingUnitModel::query()->findOrFail($billingUnit->id()); $model->fill([ 'customer_id' => $billingUnit->customerId(), 'name' => $billingUnit->name()->value, 'billing_address' => $billingUnit->billingAddress(), 'e_invoice_address' => $billingUnit->eInvoiceAddress()?->value, 'e_invoice_operator' => $billingUnit->eInvoiceOperator(), 'reference' => $billingUnit->reference(), ])->save(); return $this->toEntity($model); } public function delete(BillingUnit $billingUnit): void { BillingUnitModel::query()->whereKey($billingUnit->id())->delete(); } private function toEntity(BillingUnitModel $model): BillingUnit { return BillingUnit::reconstitute( $model->id, $model->customer_id, new BillingUnitName($model->name), $model->billing_address, $model->e_invoice_address === null ? null : new EInvoiceAddress($model->e_invoice_address), $model->e_invoice_operator, $model->reference, ); } }