Tests around the handling of verifactu credit amounts

This commit is contained in:
David Bomba 2025-08-12 14:33:52 +10:00
parent 1a3badf748
commit 81ec3986ca
4 changed files with 21 additions and 23 deletions

View File

@ -27,22 +27,33 @@ class RestoreDisabledRule implements ValidationRule
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (empty($value) ||!in_array($value, ['delete', 'restore'])) {
$user = auth()->user();
$company = $user->company();
if (empty($value) || !$company->verifactuEnabled()) {
return;
}
$user = auth()->user();
$company = $user->company();
$base_query = Invoice::withTrashed()
->whereIn('id', $this->transformKeys(request()->ids))
->company();
$restore_query = clone $base_query;
$delete_query = clone $base_query;
$mutated_query = $delete_query->where(function ($q){
$q->whereNotNull('backup->parent_invoice_id')->orWhere('backup->child_invoice_ids', '!=', []);
});
/** For verifactu, we do not allow restores of deleted invoices */
if($company->verifactuEnabled() && $value == 'restore' &&Invoice::withTrashed()->whereIn('id', $this->transformKeys(request()->ids))->where('company_id', $company->id)->where('is_deleted', true)->exists()) {
if($value == 'restore' && $restore_query->where('is_deleted', true)->exists()) {
$fail(ctrans('texts.restore_disabled_verifactu'));
}
if ($company->verifactuEnabled() && $value == 'delete' && Invoice::withTrashed()->whereIn('id', $this->transformKeys(request()->ids))->where('company_id', $company->id)->where('status_id', Invoice::STATUS_CANCELLED)->exists()) {
elseif(in_array($value, ['delete', 'cancel']) && $delete_query->exists()) {
nlog($delete_query->pluck('backup')->toArray()); // any verifactu invoices that have a parent can NEVER be deleted. The parent can also NEVER be deleted
$fail(ctrans('texts.delete_disabled_verifactu'));
}
}
}

View File

@ -38,8 +38,6 @@ class VerifactuAmountCheck implements ValidationRule
$company = $user->company();
$start = microtime(true);
/** For verifactu, we do not allow restores of deleted invoices */
if ($company->verifactuEnabled()) {
$invoice = false;
@ -48,16 +46,13 @@ class VerifactuAmountCheck implements ValidationRule
$child_invoice_count = 0;
if(isset($this->input['modified_invoice_id'])) {
$invoice = Invoice::withTrashed()->find($this->decodePrimaryKey($this->input['modified_invoice_id']));
$invoice = Invoice::withTrashed()->where('id', $this->decodePrimaryKey($this->input['modified_invoice_id']))->company()->firstOrFail();
$child_invoices = Invoice::withTrashed()
->whereIn('id', $this->transformKeys($invoice->backup->child_invoice_ids->toArray()))
->get();
$child_invoice_totals = round($child_invoices->sum('amount'), 2);
$child_invoice_count = $child_invoices->count();
// if($child_invoice_totals + $invoice->amount < 0) {
// $fail("Negative invoices can only be linked to existing invoices");
// }
}
$items = collect($this->input['line_items'])->map(function ($item) use($company){
@ -89,12 +84,6 @@ class VerifactuAmountCheck implements ValidationRule
$total = $items->sum() - $total_discount;
nlog("total " . $total);
nlog(!$invoice);
nlog($total);
nlog($child_invoice_totals);
nlog($invoice->amount ?? 0);
if($total < 0 && !$invoice) {
$fail("Negative invoices {$total} can only be linked to existing invoices");
}
@ -104,6 +93,5 @@ nlog($invoice->amount ?? 0);
}
}
nlog(microtime(true) - $start);
}
}

View File

@ -5574,7 +5574,7 @@ $lang = array(
'selected_products' => 'Selected Products',
'create_company_error_unauthorized' => 'You are not authorized to create a company. Only the account owner can create a company.',
'restore_disabled_verifactu' => 'You cannot restore an invoice once it has been deleted',
'delete_disabled_verifactu' => 'You cannot delete an invoice once it has been cancelled',
'delete_disabled_verifactu' => 'You cannot delete an invoice once it has been cancelled or modified',
);
return $lang;

View File

@ -632,7 +632,6 @@ class VerifactuApiTest extends TestCase
$this->company->settings = $settings;
$this->company->save();
$data = [
'action' => 'cancel',
'ids' => [$invoice->hashed_id],