From 56ed83860177c953ff85aebf86b48326ff810320 Mon Sep 17 00:00:00 2001 From: Frank Cools Date: Thu, 2 Oct 2025 20:42:29 +0200 Subject: [PATCH] Implement CA-3 calculation system and recalculate functionality Calculation System: - Added getAccountMappings() to retrieve PCG account mappings - Added createCA3Line() to create CA-3 line records - Added updateDeclarationTotals() to update declaration totals - Added recalculateCA3Amounts() for recalculation functionality - Enhanced getAccountAmounts() to query Dolibarr accounting entries Recalculate Button: - Added recalculate button to declaration view page - Added action handling for recalculate functionality - Added success/error messages for recalculation - Added translations for recalculate functionality The calculation system now: - Queries actual accounting data from Dolibarr - Creates detailed CA-3 line records - Updates declaration totals automatically - Allows manual recalculation of amounts --- core/class/declarationtva.class.php | 98 +++++++++++++++++++++++++++++ declarationtva_view.php | 19 +++++- langs/en_US/declarationtva.lang | 3 + langs/fr_FR/declarationtva.lang | 3 + 4 files changed, 121 insertions(+), 2 deletions(-) diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 7e75f07..9d2d132 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -509,4 +509,102 @@ class DeclarationTVA $this->db->rollback(); return false; } + + /** + * Get account mappings for CA-3 lines + * + * @return array Account mappings + */ + public function getAccountMappings() + { + $sql = "SELECT ca3_line, account_code, account_label + FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings + WHERE entity = " . $this->entity . " AND is_active = 1 + ORDER BY ca3_line, account_code"; + + $result = $this->db->query($sql); + $mappings = array(); + + if ($result) { + while ($obj = $this->db->fetch_object($result)) { + $mappings[] = array( + 'ca3_line' => $obj->ca3_line, + 'account_code' => $obj->account_code, + 'account_label' => $obj->account_label + ); + } + } + + return $mappings; + } + + /** + * Create CA-3 line record + * + * @param int $declaration_id Declaration ID + * @param string $ca3_line CA-3 line code + * @param string $line_label Line label + * @param array $amounts Amounts + * @return bool Success + */ + public function createCA3Line($declaration_id, $ca3_line, $line_label, $amounts) + { + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + (declaration_id, ca3_line, line_label, base_amount, vat_amount, total_amount, created_date) + VALUES (" . $declaration_id . ", '" . $this->db->escape($ca3_line) . "', + '" . $this->db->escape($line_label) . "', " . $amounts['base_amount'] . ", + " . $amounts['vat_amount'] . ", " . $amounts['total_amount'] . ", NOW())"; + + return $this->db->query($sql); + } + + /** + * Update declaration totals + * + * @param int $declaration_id Declaration ID + * @param float $total_vat_collected Total VAT collected + * @param float $total_vat_deductible Total VAT deductible + * @param float $net_vat_due Net VAT due + * @param float $vat_credit VAT credit + * @return bool Success + */ + public function updateDeclarationTotals($declaration_id, $total_vat_collected, $total_vat_deductible, $net_vat_due, $vat_credit) + { + $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations + SET total_vat_collected = " . $total_vat_collected . ", + total_vat_deductible = " . $total_vat_deductible . ", + net_vat_due = " . $net_vat_due . ", + vat_credit = " . $vat_credit . " + WHERE rowid = " . $declaration_id; + + return $this->db->query($sql); + } + + /** + * Recalculate CA-3 amounts for a declaration + * + * @param int $declaration_id Declaration ID + * @return bool Success + */ + public function recalculateCA3Amounts($declaration_id) + { + // Get declaration info + $declaration = $this->getDeclarationInfo($declaration_id); + if (!$declaration) { + return false; + } + + // Clear existing CA-3 lines + $sql = "DELETE FROM " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + WHERE declaration_id = " . $declaration_id; + $this->db->query($sql); + + // Recalculate using declaration dates + $period = array( + 'start_date' => $declaration['start_date'], + 'end_date' => $declaration['end_date'] + ); + + return $this->calculateCA3Amounts($declaration_id, $period); + } } diff --git a/declarationtva_view.php b/declarationtva_view.php index 6b94eed..338b586 100644 --- a/declarationtva_view.php +++ b/declarationtva_view.php @@ -37,6 +37,18 @@ if (empty($id)) { accessforbidden(); } +// Handle actions +$action = GETPOST('action', 'alpha'); +$token = GETPOST('token', 'alpha'); + +if ($action == 'recalculate' && $token) { + if ($declarationtva->recalculateCA3Amounts($id)) { + setEventMessages($langs->trans("DeclarationRecalculated"), null, 'mesgs'); + } else { + setEventMessages($langs->trans("ErrorRecalculatingDeclaration"), null, 'errors'); + } +} + // Initialize objects $declarationtva = new DeclarationTVA($db, $conf->entity); $config = new DeclarationTVA_Config($db, $conf->entity); @@ -155,10 +167,13 @@ print '
' . $langs->trans("Actions") . '
'; print '
'; +// Recalculate button (always available) +print '' . $langs->trans("Recalculate") . ' '; + if ($declarationtva->status == 'draft') { - print '' . $langs->trans("Validate") . ''; + print '' . $langs->trans("Validate") . ' '; } elseif ($declarationtva->status == 'validated') { - print '' . $langs->trans("Submit") . ''; + print '' . $langs->trans("Submit") . ' '; } print '' . $langs->trans("BackToList") . ''; diff --git a/langs/en_US/declarationtva.lang b/langs/en_US/declarationtva.lang index d361d43..ed29357 100644 --- a/langs/en_US/declarationtva.lang +++ b/langs/en_US/declarationtva.lang @@ -426,3 +426,6 @@ CreatedDate = Created Date CA3Amounts = CA-3 Amounts CA3Line = CA-3 Line BackToList = Back to List +Recalculate = Recalculate +DeclarationRecalculated = Declaration recalculated successfully +ErrorRecalculatingDeclaration = Error recalculating declaration diff --git a/langs/fr_FR/declarationtva.lang b/langs/fr_FR/declarationtva.lang index ee6c395..3232a88 100644 --- a/langs/fr_FR/declarationtva.lang +++ b/langs/fr_FR/declarationtva.lang @@ -415,3 +415,6 @@ CreatedDate = Date de création CA3Amounts = Montants CA-3 CA3Line = Ligne CA-3 BackToList = Retour à la liste +Recalculate = Recalculer +DeclarationRecalculated = Déclaration recalculée avec succès +ErrorRecalculatingDeclaration = Erreur lors du recalcul de la déclaration