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
This commit is contained in:
Frank Cools 2025-10-02 20:42:29 +02:00
parent 66037a03f7
commit 56ed838601
4 changed files with 121 additions and 2 deletions

View File

@ -509,4 +509,102 @@ class DeclarationTVA
$this->db->rollback(); $this->db->rollback();
return false; 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);
}
} }

View File

@ -37,6 +37,18 @@ if (empty($id)) {
accessforbidden(); 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 // Initialize objects
$declarationtva = new DeclarationTVA($db, $conf->entity); $declarationtva = new DeclarationTVA($db, $conf->entity);
$config = new DeclarationTVA_Config($db, $conf->entity); $config = new DeclarationTVA_Config($db, $conf->entity);
@ -155,10 +167,13 @@ print '<div class="titre">' . $langs->trans("Actions") . '</div>';
print '<div class="center">'; print '<div class="center">';
// Recalculate button (always available)
print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&action=recalculate&token=' . newToken() . '" class="butAction">' . $langs->trans("Recalculate") . '</a> ';
if ($declarationtva->status == 'draft') { if ($declarationtva->status == 'draft') {
print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&action=validate" class="butAction">' . $langs->trans("Validate") . '</a>'; print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&action=validate" class="butAction">' . $langs->trans("Validate") . '</a> ';
} elseif ($declarationtva->status == 'validated') { } elseif ($declarationtva->status == 'validated') {
print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&action=submit" class="butAction">' . $langs->trans("Submit") . '</a>'; print '<a href="' . $_SERVER['PHP_SELF'] . '?id=' . $id . '&action=submit" class="butAction">' . $langs->trans("Submit") . '</a> ';
} }
print '<a href="declarationtvaindex.php" class="butAction">' . $langs->trans("BackToList") . '</a>'; print '<a href="declarationtvaindex.php" class="butAction">' . $langs->trans("BackToList") . '</a>';

View File

@ -426,3 +426,6 @@ CreatedDate = Created Date
CA3Amounts = CA-3 Amounts CA3Amounts = CA-3 Amounts
CA3Line = CA-3 Line CA3Line = CA-3 Line
BackToList = Back to List BackToList = Back to List
Recalculate = Recalculate
DeclarationRecalculated = Declaration recalculated successfully
ErrorRecalculatingDeclaration = Error recalculating declaration

View File

@ -415,3 +415,6 @@ CreatedDate = Date de création
CA3Amounts = Montants CA-3 CA3Amounts = Montants CA-3
CA3Line = Ligne CA-3 CA3Line = Ligne CA-3
BackToList = Retour à la liste BackToList = Retour à la liste
Recalculate = Recalculer
DeclarationRecalculated = Déclaration recalculée avec succès
ErrorRecalculatingDeclaration = Erreur lors du recalcul de la déclaration