DeclarationTVA/declarationtva_line_details_ajax.php
Frank Cools 84017f33ce Improve CA-3 line details display with proper account grouping
Fixed:
- Grouped BASE and VAT accounts separately for lines 08, 09, 9B
- Added clear section headers: 'Base Accounts (Sales)' and 'VAT Accounts'
- Improved visual organization of account breakdown
- Removed debugging code
- Added translations for new section headers

This ensures users can clearly see which accounts contribute to base amounts vs VAT amounts for the special lines.
2025-10-02 23:28:07 +02:00

185 lines
6.5 KiB
PHP

<?php
/**
* AJAX endpoint for loading CA-3 line details
* Returns HTML content for inline dropdown display
*/
// Load Dolibarr environment
if (file_exists('../main.inc.php')) {
$res = @include '../main.inc.php';
} elseif (file_exists('../../main.inc.php')) {
$res = @include '../../main.inc.php';
} elseif (file_exists('../../../main.inc.php')) {
$res = @include '../../../main.inc.php';
} else {
die("Include of main fails");
}
// Load module classes
require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva.class.php';
require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva_config.class.php';
// Access control
if (!$user->hasRight("declarationtva", "declarationtva", "read")) {
http_response_code(403);
echo "Access denied";
exit;
}
// Get parameters
$declaration_id = GETPOST('declaration_id', 'int');
$ca3_line = GETPOST('ca3_line', 'alpha');
// Validate parameters
if (empty($declaration_id) || empty($ca3_line)) {
echo '<div class="error">' . $langs->trans("ErrorMissingParameters") . '</div>';
exit;
}
// Initialize objects
$declarationtva = new DeclarationTVA($db);
$config = new DeclarationTVA_Config($db);
// Get declaration info
if (!$declarationtva->fetch($declaration_id)) {
echo '<div class="error">' . $langs->trans("DeclarationNotFound") . '</div>';
exit;
}
// Get detailed breakdown
$line_details = $declarationtva->getCA3LineDetails($declaration_id, $ca3_line);
if (empty($line_details)) {
echo '<div class="error">' . $langs->trans("NoDataFoundForLine") . '</div>';
exit;
}
if (empty($line_details['account_details'])) {
echo '<div class="info">' . $langs->trans("NoDataFoundForLine") . '</div>';
exit;
}
// Get CA-3 line definition
$ca3_definitions = $config->getCA3LineDefinitions();
$line_definition = isset($ca3_definitions[$ca3_line]) ? $ca3_definitions[$ca3_line] : null;
// Group accounts by type for lines 08, 09, 9B
$base_accounts = array();
$vat_accounts = array();
$other_accounts = array();
foreach ($line_details['account_details'] as $account) {
if (strpos($account['mapping_type'], '_BASE') !== false) {
$base_accounts[] = $account;
} elseif (strpos($account['mapping_type'], '_VAT') !== false) {
$vat_accounts[] = $account;
} else {
$other_accounts[] = $account;
}
}
// Display account details table
echo '<table class="noborder centpercent">';
echo '<tr class="liste_titre">';
echo '<th>' . $langs->trans("AccountCode") . '</th>';
echo '<th>' . $langs->trans("AccountLabel") . '</th>';
echo '<th class="right">' . $langs->trans("BaseAmount") . '</th>';
echo '<th class="right">' . $langs->trans("VATAmount") . '</th>';
echo '<th class="right">' . $langs->trans("TotalAmount") . '</th>';
echo '<th>' . $langs->trans("MappingType") . '</th>';
echo '</tr>';
$total_base = 0;
$total_vat = 0;
$total_amount = 0;
// Display BASE accounts first (if any)
if (!empty($base_accounts)) {
echo '<tr class="liste_titre">';
echo '<td colspan="6"><strong>' . $langs->trans("BaseAccounts") . '</strong></td>';
echo '</tr>';
foreach ($base_accounts as $account) {
$total_base += $account['base_amount'];
$total_vat += $account['vat_amount'];
$total_amount += $account['total_amount'];
echo '<tr class="oddeven">';
echo '<td><strong>' . $account['account_code'] . '</strong></td>';
echo '<td>' . $account['account_label'] . '</td>';
echo '<td class="right">' . price($account['base_amount']) . '</td>';
echo '<td class="right">' . price($account['vat_amount']) . '</td>';
echo '<td class="right">' . price($account['total_amount']) . '</td>';
echo '<td>' . $account['mapping_type'] . '</td>';
echo '</tr>';
}
}
// Display VAT accounts second (if any)
if (!empty($vat_accounts)) {
echo '<tr class="liste_titre">';
echo '<td colspan="6"><strong>' . $langs->trans("VATAccounts") . '</strong></td>';
echo '</tr>';
foreach ($vat_accounts as $account) {
$total_base += $account['base_amount'];
$total_vat += $account['vat_amount'];
$total_amount += $account['total_amount'];
echo '<tr class="oddeven">';
echo '<td><strong>' . $account['account_code'] . '</strong></td>';
echo '<td>' . $account['account_label'] . '</td>';
echo '<td class="right">' . price($account['base_amount']) . '</td>';
echo '<td class="right">' . price($account['vat_amount']) . '</td>';
echo '<td class="right">' . price($account['total_amount']) . '</td>';
echo '<td>' . $account['mapping_type'] . '</td>';
echo '</tr>';
}
}
// Display other accounts (normal lines)
foreach ($other_accounts as $account) {
$total_base += $account['base_amount'];
$total_vat += $account['vat_amount'];
$total_amount += $account['total_amount'];
echo '<tr class="oddeven">';
echo '<td><strong>' . $account['account_code'] . '</strong></td>';
echo '<td>' . $account['account_label'] . '</td>';
echo '<td class="right">' . price($account['base_amount']) . '</td>';
echo '<td class="right">' . price($account['vat_amount']) . '</td>';
echo '<td class="right">' . price($account['total_amount']) . '</td>';
echo '<td>' . $account['mapping_type'] . '</td>';
echo '</tr>';
}
// Totals row
echo '<tr class="liste_titre">';
echo '<td colspan="2"><strong>' . $langs->trans("Total") . '</strong></td>';
echo '<td class="right"><strong>' . price($total_base) . '</strong></td>';
echo '<td class="right"><strong>' . price($total_vat) . '</strong></td>';
echo '<td class="right"><strong>' . price($total_amount) . '</strong></td>';
echo '<td></td>';
echo '</tr>';
echo '</table>';
// Additional info if calculated line exists
if ($line_details['calculated_line']) {
echo '<div class="info" style="margin-top: 10px;">';
echo '<strong>' . $langs->trans("Note") . ':</strong> ';
echo $langs->trans("CalculatedLineNote");
if (!empty($line_details['calculated_line']->line_label)) {
echo '<br><em>' . $line_details['calculated_line']->line_label . '</em>';
}
echo '</div>';
}
// Summary info
echo '<div class="info" style="margin-top: 10px;">';
echo '<strong>' . $langs->trans("Summary") . ':</strong> ';
echo $langs->trans("AccountCount") . ': ' . $line_details['account_count'] . ' | ';
echo $langs->trans("Period") . ': ' . dol_print_date($line_details['start_date'], 'day') . ' - ' . dol_print_date($line_details['end_date'], 'day');
echo '</div>';
?>