DeclarationTVA/core/class/declarationtva_period.class.php
Frank Cools 8e71a12000 Phase 1 MVP - Core foundation complete
MVP Database Schema:
- Simplified database schema for MVP development
- Core tables: config, account_mappings, periods, declarations, ca3_lines
- Basic indexes for performance
- Initial configuration data

Core PHP Classes:
- DeclarationTVA: Main class for CA-3 processing
- DeclarationTVA_Config: Configuration management
- DeclarationTVA_Period: Period management
- Complete CRUD operations for all entities

Main Interface:
- declarationtvaindex.php: Main module interface
- Period management and declaration creation
- Status tracking (draft, validated, submitted)
- Basic action handling

Configuration Interface:
- setup_mvp.php: Simplified configuration page
- PCG account mapping for all CA-3 lines
- Account selection from Dolibarr chart of accounts
- VAT rate configuration

Key Features Implemented:
- Basic CA-3 form generation
- PCG account mapping (one account per line for MVP)
- Period management (quarterly)
- Declaration status tracking
- Configuration interface
- Account validation against Dolibarr

Next Steps:
- CA-3 form generation logic
- PDF export functionality
- Testing with sample data

MVP Progress: 60% complete
Core foundation ready for testing and refinement
2025-10-02 16:16:15 +02:00

293 lines
8.6 KiB
PHP

<?php
/**
* DeclarationTVA_Period Class
* Period management for DeclarationTVA module
* MVP Version - Phase 1
*/
class DeclarationTVA_Period
{
/**
* @var DoliDB Database handler
*/
public $db;
/**
* @var int Entity ID
*/
public $entity;
/**
* @var string Error message
*/
public $error;
/**
* Constructor
*
* @param DoliDB $db Database handler
* @param int $entity Entity ID
*/
public function __construct($db, $entity = 1)
{
$this->db = $db;
$this->entity = $entity;
}
/**
* Create a new period
*
* @param string $period_name Period name (e.g., Q1-2024)
* @param string $start_date Start date (YYYY-MM-DD)
* @param string $end_date End date (YYYY-MM-DD)
* @return int Period ID or -1 if error
*/
public function createPeriod($period_name, $start_date, $end_date)
{
$this->db->begin();
// Check if period already exists
if ($this->periodExists($period_name)) {
$this->error = "Period already exists";
$this->db->rollback();
return -1;
}
// Create period
$sql = "INSERT INTO " . MAIN_DB_PREFIX . "declarationtva_periods
(entity, period_name, start_date, end_date, status, created_date)
VALUES (" . $this->entity . ", '" . $this->db->escape($period_name) . "',
'" . $this->db->escape($start_date) . "', '" . $this->db->escape($end_date) . "',
'draft', NOW())";
$result = $this->db->query($sql);
if (!$result) {
$this->error = "Error creating period: " . $this->db->lasterror();
$this->db->rollback();
return -1;
}
$period_id = $this->db->last_insert_id(MAIN_DB_PREFIX . "declarationtva_periods");
$this->db->commit();
return $period_id;
}
/**
* Check if period exists
*
* @param string $period_name Period name
* @return bool Exists
*/
public function periodExists($period_name)
{
$sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE entity = " . $this->entity . " AND period_name = '" . $this->db->escape($period_name) . "'";
$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;
}
/**
* Get period information
*
* @param int $period_id Period ID
* @return array|false Period information or false if not found
*/
public function getPeriod($period_id)
{
$sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE rowid = " . $period_id . " AND entity = " . $this->entity;
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
return $this->db->fetch_array($result);
}
return false;
}
/**
* Get all periods
*
* @return array Periods
*/
public function getAllPeriods()
{
$sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE entity = " . $this->entity . "
ORDER BY start_date DESC";
$result = $this->db->query($sql);
$periods = array();
if ($result) {
while ($obj = $this->db->fetch_object($result)) {
$periods[] = array(
'rowid' => $obj->rowid,
'period_name' => $obj->period_name,
'start_date' => $obj->start_date,
'end_date' => $obj->end_date,
'status' => $obj->status,
'created_date' => $obj->created_date
);
}
}
return $periods;
}
/**
* Update period status
*
* @param int $period_id Period ID
* @param string $status New status
* @return bool Success
*/
public function updatePeriodStatus($period_id, $status)
{
$sql = "UPDATE " . MAIN_DB_PREFIX . "declarationtva_periods
SET status = '" . $this->db->escape($status) . "'
WHERE rowid = " . $period_id . " AND entity = " . $this->entity;
$result = $this->db->query($sql);
return $result !== false;
}
/**
* Generate quarterly periods for a year
*
* @param int $year Year
* @return array Period IDs
*/
public function generateYearlyPeriods($year)
{
$periods = array();
$quarters = array(
'Q1' => array('start' => $year . '-01-01', 'end' => $year . '-03-31'),
'Q2' => array('start' => $year . '-04-01', 'end' => $year . '-06-30'),
'Q3' => array('start' => $year . '-07-01', 'end' => $year . '-09-30'),
'Q4' => array('start' => $year . '-10-01', 'end' => $year . '-12-31')
);
foreach ($quarters as $quarter => $dates) {
$period_name = $quarter . '-' . $year;
if (!$this->periodExists($period_name)) {
$period_id = $this->createPeriod($period_name, $dates['start'], $dates['end']);
if ($period_id > 0) {
$periods[] = $period_id;
}
}
}
return $periods;
}
/**
* Get current period (based on today's date)
*
* @return array|false Current period or false if not found
*/
public function getCurrentPeriod()
{
$today = date('Y-m-d');
$sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE entity = " . $this->entity . "
AND start_date <= '" . $today . "'
AND end_date >= '" . $today . "'
ORDER BY start_date DESC
LIMIT 1";
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
return $this->db->fetch_array($result);
}
return false;
}
/**
* Get period by name
*
* @param string $period_name Period name
* @return array|false Period information or false if not found
*/
public function getPeriodByName($period_name)
{
$sql = "SELECT * FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE entity = " . $this->entity . " AND period_name = '" . $this->db->escape($period_name) . "'";
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
return $this->db->fetch_array($result);
}
return false;
}
/**
* Delete period (only if no declarations exist)
*
* @param int $period_id Period ID
* @return bool Success
*/
public function deletePeriod($period_id)
{
// Check if declarations exist for this period
$sql = "SELECT COUNT(*) as count FROM " . MAIN_DB_PREFIX . "declarationtva_declarations
WHERE period_id = " . $period_id . " AND entity = " . $this->entity;
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
$obj = $this->db->fetch_object($result);
if ($obj->count > 0) {
$this->error = "Cannot delete period with existing declarations";
return false;
}
}
$sql = "DELETE FROM " . MAIN_DB_PREFIX . "declarationtva_periods
WHERE rowid = " . $period_id . " AND entity = " . $this->entity;
$result = $this->db->query($sql);
return $result !== false;
}
/**
* Get period statistics
*
* @param int $period_id Period ID
* @return array Statistics
*/
public function getPeriodStatistics($period_id)
{
$sql = "SELECT COUNT(*) as declaration_count,
SUM(total_vat_collected) as total_vat_collected,
SUM(total_vat_deductible) as total_vat_deductible,
SUM(net_vat_due) as net_vat_due,
SUM(vat_credit) as vat_credit
FROM " . MAIN_DB_PREFIX . "declarationtva_declarations
WHERE period_id = " . $period_id . " AND entity = " . $this->entity;
$result = $this->db->query($sql);
if ($result && $this->db->num_rows($result) > 0) {
return $this->db->fetch_array($result);
}
return array(
'declaration_count' => 0,
'total_vat_collected' => 0,
'total_vat_deductible' => 0,
'net_vat_due' => 0,
'vat_credit' => 0
);
}
}