diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 758d22f..b2ac3a9 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -78,6 +78,45 @@ class DeclarationTVA return $declaration_id; } + /** + * Create a new declaration with specific dates + * + * @param string $start_date Start date (YYYY-MM-DD) + * @param string $end_date End date (YYYY-MM-DD) + * @param string $declaration_name Declaration name + * @return int Declaration ID or -1 if error + */ + public function createDeclarationWithDates($start_date, $end_date, $declaration_name = '') + { + global $user; + + $this->db->begin(); + + // Generate declaration number + $declaration_number = $this->generateDeclarationNumberFromDates($start_date, $end_date); + + // Create declaration record + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_declarations + (entity, period_id, declaration_number, declaration_name, start_date, end_date, status, created_date) + VALUES (" . $this->entity . ", 0, '" . $declaration_number . "', '" . $this->db->escape($declaration_name) . "', '" . $start_date . "', '" . $end_date . "', 'draft', NOW())"; + + $result = $this->db->query($sql); + if (!$result) { + $this->error = "Error creating declaration: " . $this->db->lasterror(); + $this->db->rollback(); + return -1; + } + + $declaration_id = $this->db->last_insert_id(MAIN_DB_PREFIX . "declarationtva_declarations"); + + // Calculate CA-3 amounts + $period = array('start_date' => $start_date, 'end_date' => $end_date); + $this->calculateCA3Amounts($declaration_id, $period); + + $this->db->commit(); + return $declaration_id; + } + /** * Calculate CA-3 amounts for a declaration * @@ -257,6 +296,21 @@ class DeclarationTVA return 'CA3-' . $year . '-Q' . $quarter . '-' . date('YmdHis'); } + /** + * Generate declaration number from dates + * + * @param string $start_date Start date + * @param string $end_date End date + * @return string Declaration number + */ + public function generateDeclarationNumberFromDates($start_date, $end_date) + { + $year = date('Y', strtotime($start_date)); + $quarter = ceil(date('n', strtotime($start_date)) / 3); + + return 'CA3-' . $year . '-Q' . $quarter . '-' . date('YmdHis'); + } + /** * Get declaration information * diff --git a/declarationtva_create.php b/declarationtva_create.php index aae441c..164c495 100644 --- a/declarationtva_create.php +++ b/declarationtva_create.php @@ -38,7 +38,6 @@ $period = new DeclarationTVA_Period($db, $conf->entity); // Handle form submission $action = GETPOST('action', 'alpha'); -$period_id = GETPOST('period_id', 'int'); $declaration_name = GETPOST('declaration_name', 'alpha'); $start_date = GETPOST('start_date', 'alpha'); $end_date = GETPOST('end_date', 'alpha'); @@ -46,9 +45,9 @@ $end_date = GETPOST('end_date', 'alpha'); $error = ''; $success = ''; -if ($action == 'create' && $period_id > 0) { - // Create the declaration - $declaration_id = $declarationtva->createDeclaration($period_id, $declaration_name); +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"); @@ -60,9 +59,6 @@ if ($action == 'create' && $period_id > 0) { } } -// Get available periods -$periods = $period->getAllPeriods(); - // Page title $title = $langs->trans("CreateDeclaration"); llxHeader('', $title); @@ -87,21 +83,14 @@ print ''; print '
| ' . $langs->trans("SelectPeriod") . ' | '; print '';
-print '';
+print '';
+print ' - ';
+print '';
+print ' ' . $langs->trans("DateRangeHelp") . ''; print ' | ';
print '
| ' . $langs->trans("DateRange") . ' | '; -print '';
-print '';
-print ' - ';
-print '';
-print ' ' . $langs->trans("DateRangeHelp") . ''; -print ' | ';
-print '