diff --git a/core/class/declarationtva.class.php b/core/class/declarationtva.class.php index 7e75f07..9d2d132 100644 --- a/core/class/declarationtva.class.php +++ b/core/class/declarationtva.class.php @@ -509,4 +509,102 @@ class DeclarationTVA $this->db->rollback(); return false; } + + /** + * Get account mappings for CA-3 lines + * + * @return array Account mappings + */ + public function getAccountMappings() + { + $sql = "SELECT ca3_line, account_code, account_label + FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings + WHERE entity = " . $this->entity . " AND is_active = 1 + ORDER BY ca3_line, account_code"; + + $result = $this->db->query($sql); + $mappings = array(); + + if ($result) { + while ($obj = $this->db->fetch_object($result)) { + $mappings[] = array( + 'ca3_line' => $obj->ca3_line, + 'account_code' => $obj->account_code, + 'account_label' => $obj->account_label + ); + } + } + + return $mappings; + } + + /** + * Create CA-3 line record + * + * @param int $declaration_id Declaration ID + * @param string $ca3_line CA-3 line code + * @param string $line_label Line label + * @param array $amounts Amounts + * @return bool Success + */ + public function createCA3Line($declaration_id, $ca3_line, $line_label, $amounts) + { + $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + (declaration_id, ca3_line, line_label, base_amount, vat_amount, total_amount, created_date) + VALUES (" . $declaration_id . ", '" . $this->db->escape($ca3_line) . "', + '" . $this->db->escape($line_label) . "', " . $amounts['base_amount'] . ", + " . $amounts['vat_amount'] . ", " . $amounts['total_amount'] . ", NOW())"; + + return $this->db->query($sql); + } + + /** + * Update declaration totals + * + * @param int $declaration_id Declaration ID + * @param float $total_vat_collected Total VAT collected + * @param float $total_vat_deductible Total VAT deductible + * @param float $net_vat_due Net VAT due + * @param float $vat_credit VAT credit + * @return bool Success + */ + public function updateDeclarationTotals($declaration_id, $total_vat_collected, $total_vat_deductible, $net_vat_due, $vat_credit) + { + $sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_declarations + SET total_vat_collected = " . $total_vat_collected . ", + total_vat_deductible = " . $total_vat_deductible . ", + net_vat_due = " . $net_vat_due . ", + vat_credit = " . $vat_credit . " + WHERE rowid = " . $declaration_id; + + return $this->db->query($sql); + } + + /** + * Recalculate CA-3 amounts for a declaration + * + * @param int $declaration_id Declaration ID + * @return bool Success + */ + public function recalculateCA3Amounts($declaration_id) + { + // Get declaration info + $declaration = $this->getDeclarationInfo($declaration_id); + if (!$declaration) { + return false; + } + + // Clear existing CA-3 lines + $sql = "DELETE FROM " . MAIN_DB_PREFIX . "declarationtva_ca3_lines + WHERE declaration_id = " . $declaration_id; + $this->db->query($sql); + + // Recalculate using declaration dates + $period = array( + 'start_date' => $declaration['start_date'], + 'end_date' => $declaration['end_date'] + ); + + return $this->calculateCA3Amounts($declaration_id, $period); + } } diff --git a/declarationtva_view.php b/declarationtva_view.php index 6b94eed..338b586 100644 --- a/declarationtva_view.php +++ b/declarationtva_view.php @@ -37,6 +37,18 @@ if (empty($id)) { accessforbidden(); } +// Handle actions +$action = GETPOST('action', 'alpha'); +$token = GETPOST('token', 'alpha'); + +if ($action == 'recalculate' && $token) { + if ($declarationtva->recalculateCA3Amounts($id)) { + setEventMessages($langs->trans("DeclarationRecalculated"), null, 'mesgs'); + } else { + setEventMessages($langs->trans("ErrorRecalculatingDeclaration"), null, 'errors'); + } +} + // Initialize objects $declarationtva = new DeclarationTVA($db, $conf->entity); $config = new DeclarationTVA_Config($db, $conf->entity); @@ -155,10 +167,13 @@ print '