DeclarationTVA/declarationtva_create.php
Frank Cools 9b7c460e61 Add automatic table creation to declaration create page
Database Fix:
- Added automatic table creation for missing tables
- Creates llx_declarationtva_declarations, llx_declarationtva_periods, llx_declarationtva_ca3_lines
- Matches the same approach used in setup_mvp.php
- Ensures all required tables exist before processing

This fixes the 'Table doesn't exist' error when creating declarations.
2025-10-02 18:19:09 +02:00

199 lines
7.6 KiB
PHP

<?php
/**
* DeclarationTVA Create Declaration
* French CA-3 VAT Declaration Module for Dolibarr
* MVP Version - Phase 1
*/
// 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';
} else {
$res = 0;
}
if (!$res) {
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';
require_once DOL_DOCUMENT_ROOT . '/custom/declarationtva/core/class/declarationtva_period.class.php';
// Access control
if (!$user->hasRight("declarationtva", "declarationtva", "write")) {
accessforbidden();
}
// Load language files
$langs->load("declarationtva@declarationtva");
// Ensure tables exist (create if missing)
$tables_to_check = array(
'declarationtva_declarations',
'declarationtva_periods',
'declarationtva_ca3_lines'
);
foreach ($tables_to_check as $table_name) {
$full_table_name = MAIN_DB_PREFIX . $table_name;
$check_table_sql = "SHOW TABLES LIKE '" . $full_table_name . "'";
$table_exists = $db->query($check_table_sql);
if (!$table_exists || $db->num_rows($table_exists) == 0) {
// Create the table based on schema
if ($table_name == 'declarationtva_declarations') {
$create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`entity` int(11) NOT NULL DEFAULT 1,
`period_id` int(11) NOT NULL DEFAULT 0,
`declaration_number` varchar(32) NOT NULL,
`declaration_name` varchar(255) DEFAULT NULL,
`start_date` date DEFAULT NULL,
`end_date` date DEFAULT NULL,
`status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted',
`total_vat_collected` decimal(15,2) DEFAULT 0.00,
`total_vat_deductible` decimal(15,2) DEFAULT 0.00,
`net_vat_due` decimal(15,2) DEFAULT 0.00,
`vat_credit` decimal(15,2) DEFAULT 0.00,
`submission_date` datetime DEFAULT NULL,
`created_date` datetime DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `uk_declaration_entity_number` (`entity`, `declaration_number`),
KEY `idx_period_id` (`period_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
} elseif ($table_name == 'declarationtva_periods') {
$create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`entity` int(11) NOT NULL DEFAULT 1,
`period_name` varchar(32) NOT NULL COMMENT 'Q1-2024, Q2-2024, etc.',
`start_date` date NOT NULL,
`end_date` date NOT NULL,
`status` varchar(32) DEFAULT 'draft' COMMENT 'draft, validated, submitted',
`created_date` datetime DEFAULT NULL,
PRIMARY KEY (`rowid`),
UNIQUE KEY `uk_period_entity_name` (`entity`, `period_name`),
KEY `idx_period_dates` (`start_date`, `end_date`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
} elseif ($table_name == 'declarationtva_ca3_lines') {
$create_table_sql = "CREATE TABLE IF NOT EXISTS `" . $full_table_name . "` (
`rowid` int(11) NOT NULL AUTO_INCREMENT,
`declaration_id` int(11) NOT NULL,
`ca3_line` varchar(8) NOT NULL,
`line_label` varchar(255) DEFAULT NULL,
`base_amount` decimal(15,2) DEFAULT 0.00,
`vat_amount` decimal(15,2) DEFAULT 0.00,
`total_amount` decimal(15,2) DEFAULT 0.00,
`created_date` datetime DEFAULT NULL,
PRIMARY KEY (`rowid`),
KEY `idx_declaration_id` (`declaration_id`),
KEY `idx_ca3_line` (`ca3_line`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci";
}
$db->query($create_table_sql);
}
}
// Initialize objects
$declarationtva = new DeclarationTVA($db, $conf->entity);
$config = new DeclarationTVA_Config($db, $conf->entity);
$period = new DeclarationTVA_Period($db, $conf->entity);
// Handle form submission
$action = GETPOST('action', 'alpha');
$declaration_name = GETPOST('declaration_name', 'alpha');
$start_date = GETPOST('start_date', 'alpha');
$end_date = GETPOST('end_date', 'alpha');
$error = '';
$success = '';
if ($action == 'create' && !empty($start_date) && !empty($end_date)) {
// Create the declaration with dates
$declaration_id = $declarationtva->createDeclarationWithDates($start_date, $end_date, $declaration_name);
if ($declaration_id > 0) {
$success = $langs->trans("DeclarationCreated");
// Redirect to view the created declaration
header("Location: declarationtva_view.php?id=" . $declaration_id);
exit;
} else {
$error = $langs->trans("ErrorCreatingDeclaration") . ": " . $declarationtva->error;
}
} elseif ($action == 'create' && (empty($start_date) || empty($end_date))) {
$error = $langs->trans("ErrorMissingDates");
}
// Page title
$title = $langs->trans("CreateDeclaration");
llxHeader('', $title);
// Print page header
print load_fiche_titre($title, '', 'title_accountancy');
// Display messages
if ($error) {
setEventMessages($error, null, 'errors');
}
if ($success) {
setEventMessages($success, null, 'mesgs');
}
// Print form
print '<div class="fiche">';
print '<div class="titre">' . $langs->trans("DeclarationDetails") . '</div>';
print '<form method="POST" action="' . $_SERVER['PHP_SELF'] . '">';
print '<input type="hidden" name="action" value="create">';
print '<input type="hidden" name="token" value="' . newToken() . '">';
print '<table class="noborder centpercent">';
// Date range (now mandatory and at the top)
print '<tr>';
print '<td class="fieldrequired">' . $langs->trans("SelectPeriod") . '</td>';
print '<td>';
print '<input type="date" name="start_date" class="flat" value="' . dol_escape_htmltag($start_date) . '" required>';
print ' - ';
print '<input type="date" name="end_date" class="flat" value="' . dol_escape_htmltag($end_date) . '" required>';
print '<br><small>' . $langs->trans("DateRangeHelp") . '</small>';
print '</td>';
print '</tr>';
// Declaration name
print '<tr>';
print '<td class="fieldrequired">' . $langs->trans("DeclarationName") . '</td>';
print '<td>';
print '<input type="text" name="declaration_name" class="flat" value="' . dol_escape_htmltag($declaration_name) . '" required>';
print '<br><small>' . $langs->trans("DeclarationNameHelp") . '</small>';
print '</td>';
print '</tr>';
print '</table>';
// Buttons
print '<div class="center">';
print '<input type="submit" class="button" value="' . $langs->trans("CreateDeclaration") . '">';
print '<a href="declarationtvaindex.php" class="button">' . $langs->trans("Cancel") . '</a>';
print '</div>';
print '</form>';
print '</div>';
// Print configuration section
print '<div class="fiche">';
print '<div class="titre">' . $langs->trans("DeclarationTVAConfiguration") . '</div>';
print '<div class="info">';
print $langs->trans("ConfigurationInfo") . ' ';
print '<a href="admin/setup_mvp.php" class="butAction">' . $langs->trans("ConfigurePCGAccounts") . '</a>';
print '</div>';
print '</div>';
// Print footer
llxFooter();
?>