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.
This commit is contained in:
parent
12c5329577
commit
9b7c460e61
@ -31,6 +31,73 @@ if (!$user->hasRight("declarationtva", "declarationtva", "write")) {
|
||||
// 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);
|
||||
|
||||
Loading…
Reference in New Issue
Block a user