diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index d4dcbf8..0e0249b 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -297,8 +297,15 @@ class DeclarationTVA if ($result && $this->db->num_rows($result) > 0) { $obj = $this->db->fetch_object($result); - // For VAT accounts, we need the absolute value since credit side contains VAT amounts - $total_amount = abs($obj->total_debit - $obj->total_credit); + // For VAT accounts, we need to use the credit side (VAT collected) or debit side (VAT paid) + // If debit = credit, the account is balanced, so we use the credit amount (VAT collected) + if ($obj->total_debit == $obj->total_credit) { + // Account is balanced, use the credit amount (VAT collected) + $total_amount = $obj->total_credit; + } else { + // Account has imbalance, use the absolute difference + $total_amount = abs($obj->total_debit - $obj->total_credit); + } // Log successful query for debugging error_log("DeclarationTVA: Found data with query: " . substr($sql, 0, 100) . "... Debit: " . $obj->total_debit . ", Credit: " . $obj->total_credit . ", Amount: $total_amount");