category === ExpenseCategory::Mileage->value; } public function fillFrom(ExpenseSummary $expense): void { $this->user_id = (string) $expense->userId; $this->category = $expense->category->value; $this->paid_by = $expense->paidBy->value; $this->spent_on = $expense->spentOn; $this->description = $expense->description ?? ''; $this->cost = $expense->category === ExpenseCategory::Mileage ? '' : Money::fromCents($expense->costCents)->inputValue(); $this->kilometers = $expense->kilometerTenths === null ? '' : (new Kilometers($expense->kilometerTenths))->format(); $this->destination = $expense->destination ?? ''; $this->purpose = $expense->purpose ?? ''; $this->is_billable = $expense->isBillable; $this->markup = $expense->markupBasisPoints === null ? '' : (new MarkupPercentage($expense->markupBasisPoints))->inputValue(); $this->billed_amount = $expense->billedAmountCents === null ? '' : Money::fromCents($expense->billedAmountCents)->inputValue(); } public function toData(): ExpenseData { $this->validate(); $mileage = $this->isMileage(); return new ExpenseData( userId: (int) $this->user_id, category: ExpenseCategory::from($this->category), paidBy: ExpensePayer::from($this->paid_by), spentOn: $this->spent_on, description: $this->description, cost: $mileage ? '' : $this->cost, kilometers: $mileage ? $this->kilometers : '', destination: $mileage ? $this->destination : '', purpose: $mileage ? $this->purpose : '', isBillable: $this->is_billable, markup: $this->is_billable ? $this->markup : '', billedAmount: $this->is_billable ? $this->billed_amount : '', ); } /** * Shows a broken business rule as an error on the field it concerns. */ public function showDomainError(DomainException $exception): void { [$field, $key] = match ($exception::class) { ExpenseDescriptionRequired::class => ['description', 'expenses.errors.description_required'], ExpenseCostRequired::class => ['cost', 'expenses.errors.cost_required'], InvalidMoneyAmount::class => [$this->invalidAmountField(), 'expenses.errors.invalid_amount'], InvalidKilometers::class => ['kilometers', 'expenses.errors.invalid_kilometers'], MileageDetailsRequired::class => [$this->firstMissingMileageField(), 'expenses.errors.mileage_details_required'], MileageRateMissing::class => ['kilometers', 'expenses.errors.mileage_rate_missing'], InvalidMarkup::class => ['markup', 'expenses.errors.invalid_markup'], default => throw $exception, }; $this->addError($field, __($key)); } /** * @return array> */ protected function rules(): array { return [ 'user_id' => ['required', 'integer'], 'category' => ['required', Rule::enum(ExpenseCategory::class)], 'paid_by' => ['required', Rule::enum(ExpensePayer::class)], 'spent_on' => ['required', 'date_format:Y-m-d'], 'description' => ['nullable', 'string', 'max:255'], 'cost' => ['nullable', 'string', 'max:20'], 'kilometers' => ['nullable', 'string', 'max:12'], 'destination' => ['nullable', 'string', 'max:255'], 'purpose' => ['nullable', 'string', 'max:255'], 'is_billable' => ['boolean'], 'markup' => ['nullable', 'string', 'max:10'], 'billed_amount' => ['nullable', 'string', 'max:20'], ]; } /** * @return array */ protected function messages(): array { return [ 'user_id.required' => __('expenses.errors.user_required'), 'user_id.integer' => __('expenses.errors.user_required'), 'category.*' => __('expenses.errors.category_required'), 'paid_by.*' => __('expenses.errors.payer_required'), 'spent_on.required' => __('expenses.errors.date_required'), 'spent_on.date_format' => __('expenses.errors.invalid_date'), 'description.max' => __('expenses.errors.text_too_long'), 'destination.max' => __('expenses.errors.text_too_long'), 'purpose.max' => __('expenses.errors.text_too_long'), 'cost.max' => __('expenses.errors.invalid_amount'), 'billed_amount.max' => __('expenses.errors.invalid_amount'), 'kilometers.max' => __('expenses.errors.invalid_kilometers'), 'markup.max' => __('expenses.errors.invalid_markup'), ]; } /** * Which of the two amount fields holds the amount that does not parse. */ private function invalidAmountField(): string { if ($this->isMileage()) { return 'billed_amount'; } try { Money::parse($this->cost); } catch (InvalidMoneyAmount) { return 'cost'; } return 'billed_amount'; } private function firstMissingMileageField(): string { foreach (['kilometers', 'destination', 'purpose'] as $field) { if (trim($this->{$field}) === '') { return $field; } } return 'kilometers'; } }