db = $db; $this->entity = $entity; } /** * Get configuration value * * @param string $key Configuration key * @param mixed $default Default value * @return mixed Configuration value */ public function get($key, $default = null) { $sql = "SELECT config_value FROM " . MAIN_DB_PREFIX . "declarationtva_config WHERE entity = " . $this->entity . " AND config_key = '" . $this->db->escape($key) . "'"; $result = $this->db->query($sql); if ($result && $this->db->num_rows($result) > 0) { $obj = $this->db->fetch_object($result); return $obj->config_value; } return $default; } /** * Set configuration value * * @param string $key Configuration key * @param mixed $value Configuration value * @return bool Success */ public function set($key, $value) { $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_config (entity, config_key, config_value, created_date) VALUES (" . $this->entity . ", '" . $this->db->escape($key) . "', '" . $this->db->escape($value) . "', NOW()) ON DUPLICATE KEY UPDATE config_value = '" . $this->db->escape($value) . "'"; $result = $this->db->query($sql); return $result !== false; } /** * Get account mapping for a CA-3 line * * @param string $ca3_line CA-3 line code * @return array|false Account mapping or false if not found */ public function getAccountMapping($ca3_line) { $sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings WHERE entity = " . $this->entity . " AND ca3_line = '" . $this->db->escape($ca3_line) . "'"; $result = $this->db->query($sql); if ($result && $this->db->num_rows($result) > 0) { return $this->db->fetch_array($result); } return false; } /** * Update account mapping * * @param string $ca3_line CA-3 line code * @param string $account_code Account code * @param string $account_label Account label * @param float $vat_rate VAT rate * @return bool Success */ public function updateAccountMapping($ca3_line, $account_code, $account_label, $vat_rate) { $sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_account_mappings (entity, ca3_line, account_code, account_label, vat_rate, is_active, created_date) VALUES (" . $this->entity . ", '" . $this->db->escape($ca3_line) . "', '" . $this->db->escape($account_code) . "', '" . $this->db->escape($account_label) . "', " . $vat_rate . ", 1, NOW()) ON DUPLICATE KEY UPDATE account_code = '" . $this->db->escape($account_code) . "', account_label = '" . $this->db->escape($account_label) . "', vat_rate = " . $vat_rate; $result = $this->db->query($sql); return $result !== false; } /** * Get all account mappings * * @return array Account mappings */ public function getAllAccountMappings() { $sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_account_mappings WHERE entity = " . $this->entity . " AND is_active = 1 ORDER BY ca3_line"; $result = $this->db->query($sql); $mappings = array(); if ($result) { while ($obj = $this->db->fetch_object($result)) { $mappings[] = array( 'rowid' => $obj->rowid, 'ca3_line' => $obj->ca3_line, 'account_code' => $obj->account_code, 'account_label' => $obj->account_label, 'vat_rate' => $obj->vat_rate, 'is_active' => $obj->is_active ); } } return $mappings; } /** * Get available accounting accounts from Dolibarr * * @return array Accounting accounts */ public function getAccountingAccounts() { $sql = "SELECT account_number, label FROM " . MAIN_DB_PREFIX . "accounting_account WHERE active = 1 ORDER BY account_number"; $result = $this->db->query($sql); $accounts = array(); if ($result) { while ($obj = $this->db->fetch_object($result)) { $accounts[] = array( 'account_number' => $obj->account_number, 'label' => $obj->label ); } } return $accounts; } /** * Get CA-3 line definitions * * @return array CA-3 line definitions */ public function getCA3LineDefinitions() { return array( // A. Opérations imposables (Taxable Operations) 'A1' => array('label' => 'Montant hors TVA des opérations imposables', 'type' => 'base', 'section' => 'A'), 'A2' => array('label' => 'Opérations imposables mais ne relevant pas du CA courant', 'type' => 'base', 'section' => 'A'), 'B1' => array('label' => 'Répartition 20% (base + taxe)', 'type' => 'vat', 'section' => 'A'), 'B2' => array('label' => 'Répartition 10% (base + taxe)', 'type' => 'vat', 'section' => 'A'), 'B3' => array('label' => 'Répartition 5,5% (base + taxe)', 'type' => 'vat', 'section' => 'A'), 'B4' => array('label' => 'Répartition 2,1% (base + taxe)', 'type' => 'vat', 'section' => 'A'), // A. Opérations intracommunautaires (Intra-EU Operations) '05' => array('label' => 'Livraisons intracommunautaires (B2B)', 'type' => 'base', 'section' => 'A'), '06' => array('label' => 'Prestations intracommunautaires (B2B)', 'type' => 'base', 'section' => 'A'), // B. TVA due (VAT Due) '17' => array('label' => 'TVA due au titre des acquisitions intracommunautaires', 'type' => 'vat', 'section' => 'B'), // C. TVA déductible (Deductible VAT) '20' => array('label' => 'TVA déductible sur immobilisations', 'type' => 'vat', 'section' => 'C'), '21' => array('label' => 'TVA déductible sur autres biens et services', 'type' => 'vat', 'section' => 'C'), // D. Résultat (Result) '22' => array('label' => 'Crédit de TVA reportable', 'type' => 'vat', 'section' => 'D'), '28' => array('label' => 'TVA nette à payer', 'type' => 'vat', 'section' => 'D'), '29' => array('label' => 'Crédit de TVA à reporter ou remboursement', 'type' => 'vat', 'section' => 'D') ); } /** * Get CA-3 section headers * * @return array Section headers */ public function getCA3SectionHeaders() { return array( 'A' => array( 'title' => 'A. Opérations imposables (Taxable Operations)', 'description' => 'Base amounts and VAT for domestic taxable operations' ), 'B' => array( 'title' => 'B. TVA due (VAT Due)', 'description' => 'VAT amounts due from intra-EU acquisitions and other sources' ), 'C' => array( 'title' => 'C. TVA déductible (Deductible VAT)', 'description' => 'VAT amounts that can be deducted from purchases and expenses' ), 'D' => array( 'title' => 'D. Résultat (Result)', 'description' => 'Final calculation: net VAT to pay or credit to carry forward' ) ); } /** * Get supported VAT rates * * @return array VAT rates */ public function getVATRates() { return array( '20.00' => '20%', '10.00' => '10%', '5.50' => '5.5%', '2.10' => '2.1%', '0.00' => '0%' ); } /** * Validate account mapping * * @param string $ca3_line CA-3 line code * @param string $account_code Account code * @return bool Valid */ public function validateAccountMapping($ca3_line, $account_code) { // Check if account exists in Dolibarr $sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "accounting_account WHERE account_number = '" . $this->db->escape($account_code) . "' AND active = 1"; $result = $this->db->query($sql); if ($result && $this->db->num_rows($result) > 0) { $obj = $this->db->fetch_object($result); return $obj->count > 0; } return false; } }