Add journal entry table to PDF with OD journal entries
- Added addJournalEntryTable method to generate journal entries table - Table includes columns: Code compte, Libellé compte, Libellé écriture, Débit, Crédit - Extracts VAT accounts from line 8 (debit side, non-zero only) - Extracts accounts from line 20 (credit side, non-zero only) - Adds VAT result on account 4456700 (debit if < 0, credit if >= 0) - Adds rounding difference on 658000 (if < 0) or 758000 (if > 0) - Journal table starts on page 2 of the detailed PDF - Uses proper French accounting terminology and formatting
This commit is contained in:
parent
1aa6cbc705
commit
5256d21389
@ -945,6 +945,9 @@ class DeclarationTVA_PDF
|
|||||||
foreach ($lines_with_data as $line_code) {
|
foreach ($lines_with_data as $line_code) {
|
||||||
$this->addLineDetailPage($pdf, $declaration, $line_code);
|
$this->addLineDetailPage($pdf, $declaration, $line_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Add journal entry table starting on page 2
|
||||||
|
$this->addJournalEntryTable($pdf, $declaration, $ca3_data);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -1754,4 +1757,261 @@ class DeclarationTVA_PDF
|
|||||||
|
|
||||||
return $result;
|
return $result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add journal entry table to PDF
|
||||||
|
*
|
||||||
|
* @param TCPDF $pdf PDF object
|
||||||
|
* @param DeclarationTVA $declaration Declaration object
|
||||||
|
* @param array $ca3_data CA-3 line data
|
||||||
|
*/
|
||||||
|
private function addJournalEntryTable($pdf, $declaration, $ca3_data)
|
||||||
|
{
|
||||||
|
// Start a new page for the journal table
|
||||||
|
$pdf->AddPage();
|
||||||
|
|
||||||
|
// Title
|
||||||
|
$pdf->SetFont('helvetica', 'B', 16);
|
||||||
|
$pdf->Cell(0, 10, 'Écritures du Journal OD', 0, 1, 'C');
|
||||||
|
$pdf->Ln(10);
|
||||||
|
|
||||||
|
// Create lookup array for CA-3 data
|
||||||
|
$ca3_lookup = array();
|
||||||
|
foreach ($ca3_data as $line_data) {
|
||||||
|
$ca3_lookup[$line_data['ca3_line']] = $line_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Get journal entries
|
||||||
|
$journal_entries = $this->generateJournalEntries($declaration, $ca3_lookup);
|
||||||
|
|
||||||
|
if (empty($journal_entries)) {
|
||||||
|
$pdf->SetFont('helvetica', '', 12);
|
||||||
|
$pdf->Cell(0, 8, 'Aucune écriture à générer.', 0, 1);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Table header
|
||||||
|
$pdf->SetFont('helvetica', 'B', 10);
|
||||||
|
$pdf->SetFillColor(240, 240, 240);
|
||||||
|
|
||||||
|
// Column widths
|
||||||
|
$col_widths = array(25, 50, 60, 25, 25);
|
||||||
|
$col_headers = array('Code compte', 'Libellé compte', 'Libellé écriture', 'Débit', 'Crédit');
|
||||||
|
|
||||||
|
// Draw header
|
||||||
|
for ($i = 0; $i < count($col_headers); $i++) {
|
||||||
|
$pdf->Cell($col_widths[$i], 8, $col_headers[$i], 1, 0, 'C', true);
|
||||||
|
}
|
||||||
|
$pdf->Ln();
|
||||||
|
|
||||||
|
// Table content
|
||||||
|
$pdf->SetFont('helvetica', '', 9);
|
||||||
|
$pdf->SetFillColor(255, 255, 255);
|
||||||
|
|
||||||
|
foreach ($journal_entries as $entry) {
|
||||||
|
$pdf->Cell($col_widths[0], 6, $entry['account_code'], 1, 0, 'C');
|
||||||
|
$pdf->Cell($col_widths[1], 6, $entry['account_label'], 1, 0, 'L');
|
||||||
|
$pdf->Cell($col_widths[2], 6, $entry['entry_label'], 1, 0, 'L');
|
||||||
|
$pdf->Cell($col_widths[3], 6, $entry['debit'], 1, 0, 'R');
|
||||||
|
$pdf->Cell($col_widths[4], 6, $entry['credit'], 1, 0, 'R');
|
||||||
|
$pdf->Ln();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Generate journal entries based on CA-3 data
|
||||||
|
*
|
||||||
|
* @param DeclarationTVA $declaration Declaration object
|
||||||
|
* @param array $ca3_lookup CA-3 data lookup array
|
||||||
|
* @return array Journal entries
|
||||||
|
*/
|
||||||
|
private function generateJournalEntries($declaration, $ca3_lookup)
|
||||||
|
{
|
||||||
|
$entries = array();
|
||||||
|
|
||||||
|
// Get line 8 VAT accounts (debit side, non-zero only)
|
||||||
|
$line8_entries = $this->getLine8VATAccounts($declaration, $ca3_lookup);
|
||||||
|
$entries = array_merge($entries, $line8_entries);
|
||||||
|
|
||||||
|
// Get line 20 accounts (credit side, non-zero only)
|
||||||
|
$line20_entries = $this->getLine20Accounts($declaration, $ca3_lookup);
|
||||||
|
$entries = array_merge($entries, $line20_entries);
|
||||||
|
|
||||||
|
// Add VAT result on account 4456700
|
||||||
|
$vat_result_entry = $this->getVATResultEntry($ca3_lookup);
|
||||||
|
if ($vat_result_entry) {
|
||||||
|
$entries[] = $vat_result_entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Add rounding difference
|
||||||
|
$rounding_entry = $this->getRoundingEntry($ca3_lookup);
|
||||||
|
if ($rounding_entry) {
|
||||||
|
$entries[] = $rounding_entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get line 8 VAT accounts for debit side
|
||||||
|
*
|
||||||
|
* @param DeclarationTVA $declaration Declaration object
|
||||||
|
* @param array $ca3_lookup CA-3 data lookup array
|
||||||
|
* @return array Journal entries
|
||||||
|
*/
|
||||||
|
private function getLine8VATAccounts($declaration, $ca3_lookup)
|
||||||
|
{
|
||||||
|
$entries = array();
|
||||||
|
|
||||||
|
if (!isset($ca3_lookup['08'])) {
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
$line8_data = $ca3_lookup['08'];
|
||||||
|
$line8_details = $declaration->getCA3LineDetails($declaration->rowid, '08');
|
||||||
|
|
||||||
|
if (empty($line8_details) || empty($line8_details['account_details'])) {
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($line8_details['account_details'] as $account) {
|
||||||
|
if ($account['vat_amount'] > 0) {
|
||||||
|
$entries[] = array(
|
||||||
|
'account_code' => $account['account_number'],
|
||||||
|
'account_label' => $account['account_label'],
|
||||||
|
'entry_label' => 'TVA due - Ligne 08',
|
||||||
|
'debit' => $this->formatAmount($account['vat_amount']),
|
||||||
|
'credit' => ''
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get line 20 accounts for credit side
|
||||||
|
*
|
||||||
|
* @param DeclarationTVA $declaration Declaration object
|
||||||
|
* @param array $ca3_lookup CA-3 data lookup array
|
||||||
|
* @return array Journal entries
|
||||||
|
*/
|
||||||
|
private function getLine20Accounts($declaration, $ca3_lookup)
|
||||||
|
{
|
||||||
|
$entries = array();
|
||||||
|
|
||||||
|
if (!isset($ca3_lookup['20'])) {
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
$line20_data = $ca3_lookup['20'];
|
||||||
|
$line20_details = $declaration->getCA3LineDetails($declaration->rowid, '20');
|
||||||
|
|
||||||
|
if (empty($line20_details) || empty($line20_details['account_details'])) {
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($line20_details['account_details'] as $account) {
|
||||||
|
if ($account['vat_amount'] > 0) {
|
||||||
|
$entries[] = array(
|
||||||
|
'account_code' => $account['account_number'],
|
||||||
|
'account_label' => $account['account_label'],
|
||||||
|
'entry_label' => 'TVA déductible - Ligne 20',
|
||||||
|
'debit' => '',
|
||||||
|
'credit' => $this->formatAmount($account['vat_amount'])
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entries;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get VAT result entry on account 4456700
|
||||||
|
*
|
||||||
|
* @param array $ca3_lookup CA-3 data lookup array
|
||||||
|
* @return array|null Journal entry
|
||||||
|
*/
|
||||||
|
private function getVATResultEntry($ca3_lookup)
|
||||||
|
{
|
||||||
|
// Calculate VAT result (line 28 - line 29)
|
||||||
|
$line28_amount = isset($ca3_lookup['28']) ? $ca3_lookup['28']['vat_amount'] : 0;
|
||||||
|
$line29_amount = isset($ca3_lookup['29']) ? $ca3_lookup['29']['vat_amount'] : 0;
|
||||||
|
$vat_result = $line28_amount - $line29_amount;
|
||||||
|
|
||||||
|
if ($vat_result == 0) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
$entry = array(
|
||||||
|
'account_code' => '4456700',
|
||||||
|
'account_label' => 'TVA à payer',
|
||||||
|
'entry_label' => 'Résultat TVA (L28-L29)',
|
||||||
|
'debit' => '',
|
||||||
|
'credit' => ''
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($vat_result < 0) {
|
||||||
|
$entry['debit'] = $this->formatAmount(abs($vat_result));
|
||||||
|
} else {
|
||||||
|
$entry['credit'] = $this->formatAmount($vat_result);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get rounding difference entry
|
||||||
|
*
|
||||||
|
* @param array $ca3_lookup CA-3 data lookup array
|
||||||
|
* @return array|null Journal entry
|
||||||
|
*/
|
||||||
|
private function getRoundingEntry($ca3_lookup)
|
||||||
|
{
|
||||||
|
// Calculate rounding difference
|
||||||
|
$total_vat_due = 0;
|
||||||
|
$total_vat_deductible = 0;
|
||||||
|
|
||||||
|
// Sum all VAT due amounts
|
||||||
|
foreach ($ca3_lookup as $line_data) {
|
||||||
|
if (in_array($line_data['ca3_line'], array('08', '09', '9B', '17'))) {
|
||||||
|
$total_vat_due += $line_data['vat_amount'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum all VAT deductible amounts
|
||||||
|
foreach ($ca3_lookup as $line_data) {
|
||||||
|
if (in_array($line_data['ca3_line'], array('19', '20', '21', '22'))) {
|
||||||
|
$total_vat_deductible += $line_data['vat_amount'];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$vat_result = $total_vat_due - $total_vat_deductible;
|
||||||
|
$rounded_result = round($vat_result);
|
||||||
|
$rounding_diff = $vat_result - $rounded_result;
|
||||||
|
|
||||||
|
if (abs($rounding_diff) < 0.01) {
|
||||||
|
return null; // No rounding difference
|
||||||
|
}
|
||||||
|
|
||||||
|
$entry = array(
|
||||||
|
'account_code' => '',
|
||||||
|
'account_label' => '',
|
||||||
|
'entry_label' => 'Différence d\'arrondi',
|
||||||
|
'debit' => '',
|
||||||
|
'credit' => ''
|
||||||
|
);
|
||||||
|
|
||||||
|
if ($rounding_diff < 0) {
|
||||||
|
$entry['account_code'] = '658000';
|
||||||
|
$entry['account_label'] = 'Charges exceptionnelles';
|
||||||
|
$entry['debit'] = $this->formatAmount(abs($rounding_diff));
|
||||||
|
} else {
|
||||||
|
$entry['account_code'] = '758000';
|
||||||
|
$entry['account_label'] = 'Produits exceptionnels';
|
||||||
|
$entry['credit'] = $this->formatAmount($rounding_diff);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $entry;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user