feat(expenses): add tax tracking and reporting (#737)

This commit is contained in:
Darko Gjorgjijoski
2026-08-02 13:01:01 +02:00
committed by GitHub
parent 3455ceb594
commit 885042f13a
38 changed files with 1466 additions and 78 deletions

View File

@@ -3,10 +3,24 @@
namespace App\Http\Requests;
use App\Models\CompanySetting;
use App\Models\TaxType;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class ExpenseRequest extends FormRequest
{
protected function prepareForValidation(): void
{
if (is_string($this->input('taxes'))) {
$taxes = json_decode($this->input('taxes'), true);
$this->merge([
'taxes' => json_last_error() === JSON_ERROR_NONE ? $taxes : null,
]);
}
}
/**
* Determine if the user is authorized to make this request.
*/
@@ -42,6 +56,8 @@ class ExpenseRequest extends FormRequest
],
'amount' => [
'required',
'integer',
'min:0',
],
'customer_id' => [
'nullable',
@@ -58,6 +74,28 @@ class ExpenseRequest extends FormRequest
'mimes:jpg,png,pdf,doc,docx,xls,xlsx,ppt,pptx',
'max:20000',
],
'taxes' => [
'sometimes',
'array',
],
'taxes.*' => [
'required',
'array:tax_type_id,amount',
],
'taxes.*.tax_type_id' => [
'required',
'integer',
'distinct',
Rule::exists('tax_types', 'id')
->where('company_id', $this->header('company'))
->where('type', TaxType::TYPE_GENERAL)
->where('transaction_type', TaxType::TRANSACTION_TYPE_PURCHASES),
],
'taxes.*.amount' => [
'required',
'integer',
'min:0',
],
];
if ($companyCurrency && $this->currency_id) {
@@ -71,6 +109,25 @@ class ExpenseRequest extends FormRequest
return $rules;
}
public function withValidator(Validator $validator): void
{
$validator->after(function (Validator $validator): void {
$taxes = $this->input('taxes');
if (! is_array($taxes) || $validator->errors()->has('taxes') || $validator->errors()->has('taxes.*')) {
return;
}
$totalTaxAmount = collect($taxes)->sum(
fn (mixed $tax): int => is_array($tax) ? (int) ($tax['amount'] ?? 0) : 0
);
if ($totalTaxAmount > (int) $this->input('amount')) {
$validator->errors()->add('taxes', 'The total tax amount may not exceed the expense amount.');
}
});
}
public function getExpensePayload()
{
$company_currency = CompanySetting::getSetting('currency', $this->header('company'));
@@ -78,6 +135,7 @@ class ExpenseRequest extends FormRequest
$exchange_rate = $company_currency != $current_currency ? $this->exchange_rate : 1;
return collect($this->validated())
->except('taxes')
->merge([
'creator_id' => $this->user()->id,
'company_id' => $this->header('company'),