From 08aa825fe823305404823f57c459d6e3e318c7bb Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Wed, 8 Oct 2025 22:26:02 +0200 Subject: [PATCH] Simplify Bookkeeping creation following working example - Remove excessive debugging and field validation - Use minimal field set like working example - Add try/catch block for proper error handling - Use dol_now() for doc_date like working example - Simplify to essential fields only: doc_date, doc_ref, code_journal, numero_compte, label_compte, montant, sens, fk_doc, fk_user_author --- core/class/declarationtva.class.php | 156 +++++++++------------------- 1 file changed, 49 insertions(+), 107 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 1d3078b..70580c8 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -1229,119 +1229,61 @@ class DeclarationTVA $this->db->begin(); error_log("DEBUG: Database transaction started"); - foreach ($entries as $entry) { - error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); - - // Prepare amounts - $debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0; - $credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0; - - // Create debit entry if amount > 0 - if ($debit_amount > 0) { - $debit = new Bookkeeping($this->db); - $debit->doc_date = $declaration->end_date; - $debit->doc_ref = $declaration->declaration_name; - $debit->code_journal = 'OD'; - $debit->numero_compte = $entry['account_code']; - $debit->label_compte = $entry['account_label']; - $debit->montant = $debit_amount; - $debit->sens = 'D'; - $debit->fk_doc = $declaration->rowid; - $debit->fk_docdet = 0; - $debit->societe_type = 1; - $debit->fk_user_author = $user->id; - $debit->code_tiers = ''; - $debit->piece_num = ''; - $debit->import_key = ''; - $debit->entity = $this->entity; + try { + foreach ($entries as $entry) { + error_log("DEBUG: Creating accounting entry for account: " . $entry['account_code'] . ", debit: " . $entry['debit'] . ", credit: " . $entry['credit']); - // Add additional required fields that might be missing - $debit->date_creation = dol_now(); - $debit->tms = dol_now(); - $debit->fk_user_creation = $user->id; - $debit->fk_user_modification = $user->id; + // Prepare amounts + $debit_amount = !empty($entry['debit']) ? (float)$entry['debit'] : 0; + $credit_amount = !empty($entry['credit']) ? (float)$entry['credit'] : 0; - // Debug: Log all field values before create - error_log("DEBUG: Debit entry fields - doc_date: " . $debit->doc_date . ", doc_ref: " . $debit->doc_ref . ", code_journal: " . $debit->code_journal . ", numero_compte: " . $debit->numero_compte . ", montant: " . $debit->montant . ", sens: " . $debit->sens . ", fk_doc: " . $debit->fk_doc . ", entity: " . $debit->entity); - - // Check if journal code exists - $journal_check_sql = "SELECT code FROM " . MAIN_DB_PREFIX . "accounting_journal WHERE code = 'OD' AND entity = " . $this->entity; - $journal_check_result = $this->db->query($journal_check_sql); - if ($journal_check_result && $this->db->num_rows($journal_check_result) > 0) { - error_log("DEBUG: Journal code 'OD' exists in system"); - } else { - error_log("DEBUG: Journal code 'OD' NOT FOUND in system - this might be the issue"); + // Create debit entry if amount > 0 + if ($debit_amount > 0) { + $debit = new Bookkeeping($this->db); + $debit->doc_date = dol_now(); + $debit->doc_ref = $declaration->declaration_name; + $debit->code_journal = 'OD'; + $debit->numero_compte = $entry['account_code']; + $debit->label_compte = $entry['account_label']; + $debit->montant = $debit_amount; + $debit->sens = 'D'; + $debit->fk_doc = $declaration->rowid; + $debit->fk_user_author = $user->id; + + $result = $debit->create($user); + error_log("DEBUG: Debit entry create result: " . $result . " (type: " . gettype($result) . ")"); + + if ($result <= 0) { + throw new Exception('Failed to create debit entry: ' . $debit->error); + } } - // Check if account number exists - $account_check_sql = "SELECT account_number FROM " . MAIN_DB_PREFIX . "accounting_account WHERE account_number = '" . $this->db->escape($entry['account_code']) . "' AND entity = " . $this->entity; - $account_check_result = $this->db->query($account_check_sql); - if ($account_check_result && $this->db->num_rows($account_check_result) > 0) { - error_log("DEBUG: Account number '" . $entry['account_code'] . "' exists in system"); - } else { - error_log("DEBUG: Account number '" . $entry['account_code'] . "' NOT FOUND in system - this might be the issue"); - } - - // Check existing accounting entries to see what fields they have - $existing_sql = "SELECT * FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping WHERE entity = " . $this->entity . " LIMIT 1"; - $existing_result = $this->db->query($existing_sql); - if ($existing_result && $this->db->num_rows($existing_result) > 0) { - $existing_obj = $this->db->fetch_object($existing_result); - error_log("DEBUG: Existing entry fields - doc_date: " . $existing_obj->doc_date . ", doc_ref: " . $existing_obj->doc_ref . ", code_journal: " . $existing_obj->code_journal . ", numero_compte: " . $existing_obj->numero_compte . ", montant: " . $existing_obj->montant . ", sens: " . $existing_obj->sens); - } else { - error_log("DEBUG: No existing accounting entries found in system"); - } - - $result = $debit->create($user); - error_log("DEBUG: Debit entry create result: " . $result . " (type: " . gettype($result) . ")"); - - if ($result > 0) { - error_log("DEBUG: Debit entry created successfully with ID: " . $result); - } else { - error_log("DEBUG: Debit entry creation failed - result: " . $result . ", error: " . $debit->error); - $this->db->rollback(); - $this->error = 'Failed to create debit entry: ' . $debit->error; - return false; - } - } - - // Create credit entry if amount > 0 - if ($credit_amount > 0) { - $credit = new Bookkeeping($this->db); - $credit->doc_date = $declaration->end_date; - $credit->doc_ref = $declaration->declaration_name; - $credit->code_journal = 'OD'; - $credit->numero_compte = $entry['account_code']; - $credit->label_compte = $entry['account_label']; - $credit->montant = $credit_amount; - $credit->sens = 'C'; - $credit->fk_doc = $declaration->rowid; - $credit->fk_docdet = 0; - $credit->societe_type = 1; - $credit->fk_user_author = $user->id; - $credit->code_tiers = ''; - $credit->piece_num = ''; - $credit->import_key = ''; - $credit->entity = $this->entity; - - // Add additional required fields that might be missing - $credit->date_creation = dol_now(); - $credit->tms = dol_now(); - $credit->fk_user_creation = $user->id; - $credit->fk_user_modification = $user->id; - - $result = $credit->create($user); - error_log("DEBUG: Credit entry create result: " . $result . " (type: " . gettype($result) . ")"); - - if ($result > 0) { - error_log("DEBUG: Credit entry created successfully with ID: " . $result); - } else { - error_log("DEBUG: Credit entry creation failed - result: " . $result . ", error: " . $credit->error); - $this->db->rollback(); - $this->error = 'Failed to create credit entry: ' . $credit->error; - return false; + // Create credit entry if amount > 0 + if ($credit_amount > 0) { + $credit = new Bookkeeping($this->db); + $credit->doc_date = dol_now(); + $credit->doc_ref = $declaration->declaration_name; + $credit->code_journal = 'OD'; + $credit->numero_compte = $entry['account_code']; + $credit->label_compte = $entry['account_label']; + $credit->montant = $credit_amount; + $credit->sens = 'C'; + $credit->fk_doc = $declaration->rowid; + $credit->fk_user_author = $user->id; + + $result = $credit->create($user); + error_log("DEBUG: Credit entry create result: " . $result . " (type: " . gettype($result) . ")"); + + if ($result <= 0) { + throw new Exception('Failed to create credit entry: ' . $credit->error); + } } } + } catch (Exception $e) { + $this->db->rollback(); + error_log("DEBUG: Exception caught: " . $e->getMessage()); + $this->error = $e->getMessage(); + return false; } // Commit the transaction