<?php
session_start();
include 'php/db_connection.php';
require('pdf/fpdf.php');

if(!isset($_SESSION['user_id'])){
    header("Location: login.php");
    exit();
}

date_default_timezone_set('Asia/Dhaka');
/** ---------------
$days = isset($_GET['days']) ? intval($_GET['days']) : 30;

$start_date = date('Y-m-d', strtotime("-$days days"));
$end_date = date('Y-m-d');

$start_date = '2000-01-01';
$end_date = date('Y-m-d');
-----------*/

if(isset($_GET['preset']) && $_GET['preset'] != 'custom'){
    $days = intval($_GET['preset']);
    $start_date = date('Y-m-d', strtotime("-$days days"));
    $end_date = date('Y-m-d');
} elseif(isset($_GET['start_date'], $_GET['end_date'])){
    $start_date = $_GET['start_date'];
    $end_date = $_GET['end_date'];
} else {
    // Default 30 days
    $start_date = date('Y-m-d', strtotime('-30 days'));
    $end_date = date('Y-m-d');
}

$sql = "
SELECT 
sales_data.sale_date,
sales_data.total_sales_count,
sales_data.grand_total,
sales_data.total_collected,
sales_data.total_discount,
sales_data.total_due,
IFNULL(profit_data.daily_profit,0) as daily_profit

FROM

(
    SELECT 
    DATE(created_at) as sale_date,
    COUNT(id) as total_sales_count,
    SUM(grand_total) as grand_total,
    SUM(collected) as total_collected,
    SUM(discount) as total_discount,
    SUM(final_due) as total_due
    FROM sales
    WHERE created_at BETWEEN '$start_date 00:00:00' AND '$end_date 23:59:59'
    GROUP BY DATE(created_at)
) sales_data

LEFT JOIN

(
    SELECT 
    DATE(s.created_at) as sale_date,
    SUM(si.Quantity * (si.price - pc.purchase_price)) as daily_profit
    FROM sales_items si
    LEFT JOIN sales s ON si.sales_id = s.id
    LEFT JOIN product_cat pc ON si.Product_id = pc.Product_id
    WHERE s.created_at BETWEEN '$start_date 00:00:00' AND '$end_date 23:59:59'
    GROUP BY DATE(s.created_at)
) profit_data

ON sales_data.sale_date = profit_data.sale_date

ORDER BY sales_data.sale_date ASC
";

$result = $con->query($sql);

// Summary calculation
$total_sales = 0;
$total_amount = 0;
$total_collected = 0;
$total_discount = 0;
$total_due = 0;
$total_profit = 0;

$data = [];

while($row = $result->fetch_assoc()){
    $data[] = $row;

    $total_sales += $row['total_sales_count'];
    $total_amount += $row['grand_total'];
    $total_collected += $row['total_collected'];
    $total_discount += $row['total_discount'];
    $total_due += $row['total_due'];
    $total_profit += $row['daily_profit'];
}

class PDF extends FPDF{

function Header(){

$this->SetFont('Arial','B',16);
$this->Cell(0,10,'M/S Taqwa Medicine Shop',0,1,'C');

$this->SetFont('Arial','B',14);
$this->Cell(0,8,'Sales Report',0,1,'C');

$this->SetFont('Arial','',10);
$this->Cell(0,6,'Generated: '.date('d-m-Y h:i A'),0,1,'C');

$this->Ln(5);
}

}

$pdf = new PDF();
$pdf->AddPage();




// ================= DAILY TABLE =================

$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,8,'Daily Breakdown',0,1);

$pdf->SetFont('Arial','B',10);

$pdf->Cell(30,8,'Date',1,0,'C');
$pdf->Cell(25,8,'Sales',1,0,'C');
$pdf->Cell(35,8,'Total',1,0,'C');
$pdf->Cell(35,8,'Collected',1,0,'C');
$pdf->Cell(35,8,'discount',1,0,'C');
$pdf->Cell(35,8,'Profit',1,1,'C');

$pdf->SetFont('Arial','',9);

foreach($data as $row){

$pdf->Cell(30,7,date('d/m/Y', strtotime($row['sale_date'])),1,0,'C');

$pdf->Cell(25,7,$row['total_sales_count'],1,0,'C');

$pdf->Cell(35,7,number_format($row['grand_total'],2),1,0,'R');

$pdf->Cell(35,7,number_format($row['total_collected'],2),1,0,'R');
$pdf->Cell(35,7,number_format($row['total_discount'],2),1,0,'R');

if (($row['daily_profit'] - $row['total_discount']) >= 0) {
    $pdf->SetTextColor(0,128,0);
} else {
    $pdf->SetTextColor(255,0,0);
}

$pdf->Cell(
    35,
    7,
    number_format($row['daily_profit'] - $row['total_discount'], 2),
    1,
    1,
    'R'
);

$pdf->SetTextColor(0,0,0);

}



// ================= SUMMARY TABLE =================

$pdf->SetFont('Arial','B',12);
$pdf->Cell(0,8,'Summary',0,1);

$pdf->SetFont('Arial','B',10);

$pdf->Cell(38,8,'Total Sales',1,0,'C');
$pdf->Cell(38,8,'Total Amount',1,0,'C');
$pdf->Cell(38,8,'Collected',1,0,'C');
$pdf->Cell(38,8,'discount',1,0,'C');
$pdf->Cell(43,8,'Profit',1,1,'C');

$pdf->SetFont('Arial','',10);

$pdf->Cell(38,8,$total_sales,1,0,'C');
$pdf->Cell(38,8,number_format($total_amount,2),1,0,'R');
$pdf->Cell(38,8,number_format($total_collected,2),1,0,'R');
$pdf->Cell(38,8,number_format($total_discount,2),1,0,'R');

if($total_profit >= 0){
$pdf->SetTextColor(0,128,0);
}else{
$pdf->SetTextColor(255,0,0);
}

$pdf->Cell(43,8,number_format($total_profit -  $total_discount ,2),1,1,'R');

$pdf->SetTextColor(0,0,0);

$pdf->Ln(8);


$pdf->Output('I','daily_sales_report.pdf');

?>