|
|
@@ -24,48 +24,67 @@ try {
|
|
|
$period = $_GET['period'] ?? 'current';
|
|
|
$month = $_GET['month'] ?? null;
|
|
|
$year = $_GET['year'] ?? null;
|
|
|
+ $report_type = $_GET['report_type'] ?? 'standard'; // 'standard' or 'monthly_summary'
|
|
|
|
|
|
// Determine date range based on period
|
|
|
$dateConditions = getDateConditions($period, $month, $year);
|
|
|
|
|
|
- // Fetch accounting entries for VAT calculation
|
|
|
- $sql = "SELECT
|
|
|
- ae.*,
|
|
|
- ac.category_name,
|
|
|
- ac.category_type,
|
|
|
- ac.vat_percentage as category_vat
|
|
|
- FROM accounting_entries ae
|
|
|
- LEFT JOIN accounting_categories ac ON ae.category = ac.category_code
|
|
|
- WHERE ae.entry_date BETWEEN :start_date AND :end_date
|
|
|
- ORDER BY ae.entry_date";
|
|
|
-
|
|
|
- $stmt = $conn->prepare($sql);
|
|
|
- $stmt->bindParam(':start_date', $dateConditions['start_date']);
|
|
|
- $stmt->bindParam(':end_date', $dateConditions['end_date']);
|
|
|
- $stmt->execute();
|
|
|
-
|
|
|
- $entries = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
-
|
|
|
- // Calculate VAT breakdown
|
|
|
- $vatBreakdown = calculateVATBreakdown($entries);
|
|
|
-
|
|
|
- // Get previous period data for comparison
|
|
|
- $previousDateConditions = getPreviousDateConditions($period, $month, $year);
|
|
|
- $previousData = getPreviousPeriodVATData($conn, $previousDateConditions);
|
|
|
-
|
|
|
- // Combine current and previous data
|
|
|
- $result = [
|
|
|
- 'period' => $period,
|
|
|
- 'date_range' => $dateConditions,
|
|
|
- 'vat_breakdown' => $vatBreakdown,
|
|
|
- 'previous_period' => $previousData,
|
|
|
- 'totals' => calculateVATTotals($vatBreakdown)
|
|
|
- ];
|
|
|
-
|
|
|
- echo json_encode([
|
|
|
- 'success' => true,
|
|
|
- 'data' => $result
|
|
|
- ]);
|
|
|
+ if ($report_type === 'monthly_summary') {
|
|
|
+ // Generate monthly summary report
|
|
|
+ error_log("Generating monthly summary for period: " . $period . " from " . $dateConditions['start_date'] . " to " . $dateConditions['end_date']);
|
|
|
+ $monthlyData = generateMonthlyVATSummary($conn, $dateConditions);
|
|
|
+ error_log("Monthly summary data: " . json_encode($monthlyData));
|
|
|
+
|
|
|
+ echo json_encode([
|
|
|
+ 'success' => true,
|
|
|
+ 'data' => [
|
|
|
+ 'report_type' => 'monthly_summary',
|
|
|
+ 'period' => $period,
|
|
|
+ 'date_range' => $dateConditions,
|
|
|
+ 'monthly_summary' => $monthlyData
|
|
|
+ ]
|
|
|
+ ]);
|
|
|
+ } else {
|
|
|
+ // Standard VAT calculation
|
|
|
+ // Fetch accounting entries for VAT calculation
|
|
|
+ $sql = "SELECT
|
|
|
+ ae.*,
|
|
|
+ ac.category_name,
|
|
|
+ ac.category_type,
|
|
|
+ ac.vat_percentage as category_vat
|
|
|
+ FROM accounting_entries ae
|
|
|
+ LEFT JOIN accounting_categories ac ON ae.category = ac.category_code
|
|
|
+ WHERE ae.entry_date BETWEEN :start_date AND :end_date
|
|
|
+ ORDER BY ae.entry_date";
|
|
|
+
|
|
|
+ $stmt = $conn->prepare($sql);
|
|
|
+ $stmt->bindParam(':start_date', $dateConditions['start_date']);
|
|
|
+ $stmt->bindParam(':end_date', $dateConditions['end_date']);
|
|
|
+ $stmt->execute();
|
|
|
+
|
|
|
+ $entries = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
+
|
|
|
+ // Calculate VAT breakdown
|
|
|
+ $vatBreakdown = calculateVATBreakdown($entries);
|
|
|
+
|
|
|
+ // Get previous period data for comparison
|
|
|
+ $previousDateConditions = getPreviousDateConditions($period, $month, $year);
|
|
|
+ $previousData = getPreviousPeriodVATData($conn, $previousDateConditions);
|
|
|
+
|
|
|
+ // Combine current and previous data
|
|
|
+ $result = [
|
|
|
+ 'period' => $period,
|
|
|
+ 'date_range' => $dateConditions,
|
|
|
+ 'vat_breakdown' => $vatBreakdown,
|
|
|
+ 'previous_period' => $previousData,
|
|
|
+ 'totals' => calculateVATTotals($vatBreakdown)
|
|
|
+ ];
|
|
|
+
|
|
|
+ echo json_encode([
|
|
|
+ 'success' => true,
|
|
|
+ 'data' => $result
|
|
|
+ ]);
|
|
|
+ }
|
|
|
|
|
|
} catch (Exception $e) {
|
|
|
http_response_code(500);
|
|
|
@@ -86,7 +105,7 @@ function getDateConditions($period, $month, $year) {
|
|
|
'end_date' => date('Y-m-t', strtotime("$currentYear-$currentMonth-01"))
|
|
|
];
|
|
|
case 'quarter':
|
|
|
- $quarter = ceil($currentMonth / 3);
|
|
|
+ $quarter = isset($_GET['quarter']) ? intval($_GET['quarter']) : ceil($currentMonth / 3);
|
|
|
$startMonth = ($quarter - 1) * 3 + 1;
|
|
|
$endMonth = $quarter * 3;
|
|
|
return [
|
|
|
@@ -279,4 +298,51 @@ function getPreviousPeriodVATData($conn, $dateConditions) {
|
|
|
$entries = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
return calculateVATBreakdown($entries);
|
|
|
}
|
|
|
+
|
|
|
+function generateMonthlyVATSummary($conn, $dateConditions) {
|
|
|
+ $monthlySummary = [];
|
|
|
+
|
|
|
+ // Get all months within the date range
|
|
|
+ $start = new DateTime($dateConditions['start_date']);
|
|
|
+ $end = new DateTime($dateConditions['end_date']);
|
|
|
+ $interval = new DateInterval('P1M');
|
|
|
+ $period = new DatePeriod($start, $interval, $end->modify('+1 month'));
|
|
|
+
|
|
|
+ foreach ($period as $month) {
|
|
|
+ $monthStart = $month->format('Y-m-01');
|
|
|
+ $monthEnd = $month->format('Y-m-t');
|
|
|
+
|
|
|
+ // Fetch accounting entries for this month
|
|
|
+ $sql = "SELECT
|
|
|
+ ae.*,
|
|
|
+ ac.category_name,
|
|
|
+ ac.category_type,
|
|
|
+ ac.vat_percentage as category_vat
|
|
|
+ FROM accounting_entries ae
|
|
|
+ LEFT JOIN accounting_categories ac ON ae.category = ac.category_code
|
|
|
+ WHERE ae.entry_date BETWEEN :start_date AND :end_date
|
|
|
+ ORDER BY ae.entry_date";
|
|
|
+
|
|
|
+ $stmt = $conn->prepare($sql);
|
|
|
+ $stmt->bindParam(':start_date', $monthStart);
|
|
|
+ $stmt->bindParam(':end_date', $monthEnd);
|
|
|
+ $stmt->execute();
|
|
|
+
|
|
|
+ $entries = $stmt->fetchAll(PDO::FETCH_ASSOC);
|
|
|
+ $vatBreakdown = calculateVATBreakdown($entries);
|
|
|
+ $totals = calculateVATTotals($vatBreakdown);
|
|
|
+
|
|
|
+ $monthlySummary[] = [
|
|
|
+ 'month' => $month->format('Y-m'),
|
|
|
+ 'month_name' => $month->format('F Y'),
|
|
|
+ 'payable' => $totals['payable_vat'] ?? 0,
|
|
|
+ 'deductible' => $totals['deductible_vat'] ?? 0,
|
|
|
+ 'net' => $totals['net_vat'] ?? 0,
|
|
|
+ 'turnover' => $totals['taxable_sales'] ?? 0,
|
|
|
+ 'vat_breakdown' => $vatBreakdown
|
|
|
+ ];
|
|
|
+ }
|
|
|
+
|
|
|
+ return $monthlySummary;
|
|
|
+}
|
|
|
?>
|