Add duplicate entry prevention for accounting entries

- Check if accounting entries already exist for the declaration
- Delete existing entries before creating new ones to prevent BookkeepingRecordAlreadyExists error
- Allow resubmission of declarations without duplicate entry errors
- Add debug logging to track existing entries and deletion
This commit is contained in:
Frank Cools 2025-10-09 00:49:11 +02:00
parent b5da8910ee
commit c06c427478

View File

@ -902,6 +902,30 @@ class DeclarationTVA
return false;
}
// Check if accounting entries already exist for this declaration
$existing_entries_sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping
WHERE doc_ref = '" . $this->db->escape($this->declaration_name) . "'
AND entity = " . $this->entity;
$existing_result = $this->db->query($existing_entries_sql);
if ($existing_result && $this->db->num_rows($existing_result) > 0) {
$existing_obj = $this->db->fetch_object($existing_result);
if ($existing_obj->count > 0) {
error_log("DEBUG: Found " . $existing_obj->count . " existing accounting entries for declaration " . $this->declaration_name);
error_log("DEBUG: Deleting existing entries to prevent duplicates");
// Delete existing entries to prevent duplicates
$delete_sql = "DELETE FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping
WHERE doc_ref = '" . $this->db->escape($this->declaration_name) . "'
AND entity = " . $this->entity;
$delete_result = $this->db->query($delete_sql);
if ($delete_result) {
error_log("DEBUG: Successfully deleted " . $existing_obj->count . " existing entries");
} else {
error_log("DEBUG: Failed to delete existing entries: " . $this->db->lasterror());
}
}
}
// Create a declaration object with the fetched data
$declaration = new stdClass();
$declaration->rowid = $this->rowid;