<?php
class AccountingEntry {
    private $conn;
    private $table_name = "accounting_entries";

    public $id;
    public $entry_date;
    public $description;
    public $entry_type;
    public $category;
    public $tax_free_amount;
    public $vat_percentage;
    public $vat_25_5;
    public $vat_14;
    public $vat_10;
    public $total_amount;
    public $net_amount;
    public $vat_amount;
    public $reference_number;

    public function __construct($db) {
        $this->conn = $db;
    }

    public function create() {
        $query = "INSERT INTO " . $this->table_name . " SET 
                  entry_date=:entry_date, 
                  description=:description, 
                  entry_type=:entry_type, 
                  category=:category, 
                  tax_free_amount=:tax_free_amount, 
                  vat_percentage=:vat_percentage, 
                  vat_25_5=:vat_25_5, 
                  vat_14=:vat_14, 
                  vat_10=:vat_10, 
                  total_amount=:total_amount, 
                  net_amount=:net_amount, 
                  vat_amount=:vat_amount, 
                  reference_number=:reference_number";

        $stmt = $this->conn->prepare($query);

        $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->entry_type = htmlspecialchars(strip_tags($this->entry_type));
        $this->category = htmlspecialchars(strip_tags($this->category));
        $this->tax_free_amount = htmlspecialchars(strip_tags($this->tax_free_amount));
        $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
        $this->vat_25_5 = htmlspecialchars(strip_tags($this->vat_25_5));
        $this->vat_14 = htmlspecialchars(strip_tags($this->vat_14));
        $this->vat_10 = htmlspecialchars(strip_tags($this->vat_10));
        $this->total_amount = htmlspecialchars(strip_tags($this->total_amount));
        $this->net_amount = htmlspecialchars(strip_tags($this->net_amount));
        $this->vat_amount = htmlspecialchars(strip_tags($this->vat_amount));
        $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));

        $stmt->bindParam(":entry_date", $this->entry_date);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":entry_type", $this->entry_type);
        $stmt->bindParam(":category", $this->category);
        $stmt->bindParam(":tax_free_amount", $this->tax_free_amount);
        $stmt->bindParam(":vat_percentage", $this->vat_percentage);
        $stmt->bindParam(":vat_25_5", $this->vat_25_5);
        $stmt->bindParam(":vat_14", $this->vat_14);
        $stmt->bindParam(":vat_10", $this->vat_10);
        $stmt->bindParam(":total_amount", $this->total_amount);
        $stmt->bindParam(":net_amount", $this->net_amount);
        $stmt->bindParam(":vat_amount", $this->vat_amount);
        $stmt->bindParam(":reference_number", $this->reference_number);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function read() {
        $query = "SELECT * FROM " . $this->table_name . " ORDER BY entry_date DESC, id DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->execute();

        return $stmt;
    }

    public function readOne() {
        $query = "SELECT * FROM " . $this->table_name . " WHERE id = ? LIMIT 0,1";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);
        $stmt->execute();

        $row = $stmt->fetch(PDO::FETCH_ASSOC);

        $this->entry_date = $row['entry_date'];
        $this->description = $row['description'];
        $this->entry_type = $row['entry_type'];
        $this->category = $row['category'];
        $this->tax_free_amount = $row['tax_free_amount'];
        $this->vat_percentage = $row['vat_percentage'];
        $this->vat_25_5 = $row['vat_25_5'];
        $this->vat_14 = $row['vat_14'];
        $this->vat_10 = $row['vat_10'];
        $this->total_amount = $row['total_amount'];
        $this->net_amount = $row['net_amount'];
        $this->vat_amount = $row['vat_amount'];
        $this->reference_number = $row['reference_number'];
    }

    public function update() {
        $query = "UPDATE " . $this->table_name . " SET 
                  entry_date=:entry_date, 
                  description=:description, 
                  entry_type=:entry_type, 
                  category=:category, 
                  tax_free_amount=:tax_free_amount, 
                  vat_percentage=:vat_percentage, 
                  vat_25_5=:vat_25_5, 
                  vat_14=:vat_14, 
                  vat_10=:vat_10, 
                  total_amount=:total_amount, 
                  net_amount=:net_amount, 
                  vat_amount=:vat_amount, 
                  reference_number=:reference_number 
                  WHERE id=:id";

        $stmt = $this->conn->prepare($query);

        $this->entry_date = htmlspecialchars(strip_tags($this->entry_date));
        $this->description = htmlspecialchars(strip_tags($this->description));
        $this->entry_type = htmlspecialchars(strip_tags($this->entry_type));
        $this->category = htmlspecialchars(strip_tags($this->category));
        $this->tax_free_amount = htmlspecialchars(strip_tags($this->tax_free_amount));
        $this->vat_percentage = htmlspecialchars(strip_tags($this->vat_percentage));
        $this->vat_25_5 = htmlspecialchars(strip_tags($this->vat_25_5));
        $this->vat_14 = htmlspecialchars(strip_tags($this->vat_14));
        $this->vat_10 = htmlspecialchars(strip_tags($this->vat_10));
        $this->total_amount = htmlspecialchars(strip_tags($this->total_amount));
        $this->net_amount = htmlspecialchars(strip_tags($this->net_amount));
        $this->vat_amount = htmlspecialchars(strip_tags($this->vat_amount));
        $this->reference_number = htmlspecialchars(strip_tags($this->reference_number));

        $stmt->bindParam(":entry_date", $this->entry_date);
        $stmt->bindParam(":description", $this->description);
        $stmt->bindParam(":entry_type", $this->entry_type);
        $stmt->bindParam(":category", $this->category);
        $stmt->bindParam(":tax_free_amount", $this->tax_free_amount);
        $stmt->bindParam(":vat_percentage", $this->vat_percentage);
        $stmt->bindParam(":vat_25_5", $this->vat_25_5);
        $stmt->bindParam(":vat_14", $this->vat_14);
        $stmt->bindParam(":vat_10", $this->vat_10);
        $stmt->bindParam(":total_amount", $this->total_amount);
        $stmt->bindParam(":net_amount", $this->net_amount);
        $stmt->bindParam(":vat_amount", $this->vat_amount);
        $stmt->bindParam(":id", $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function delete() {
        $query = "DELETE FROM " . $this->table_name . " WHERE id = ?";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $this->id);

        if($stmt->execute()) {
            return true;
        }

        return false;
    }

    public function search($search_term) {
        $query = "SELECT * FROM " . $this->table_name . " 
                  WHERE description LIKE ? OR 
                        category LIKE ? OR 
                        reference_number LIKE ? 
                  ORDER BY entry_date DESC, id DESC";

        $stmt = $this->conn->prepare($query);
        $search_term = "%{$search_term}%";
        $stmt->bindParam(1, $search_term);
        $stmt->bindParam(2, $search_term);
        $stmt->bindParam(3, $search_term);
        $stmt->execute();

        return $stmt;
    }

    public function getByDateRange($start_date, $end_date) {
        $query = "SELECT * FROM " . $this->table_name . " 
                  WHERE entry_date BETWEEN ? AND ? 
                  ORDER BY entry_date DESC, id DESC";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $start_date);
        $stmt->bindParam(2, $end_date);
        $stmt->execute();

        return $stmt;
    }

    public function getMonthlySummary($year, $month) {
        $query = "SELECT 
                    entry_type,
                    SUM(total_amount) as total,
                    SUM(vat_amount) as vat_total,
                    SUM(net_amount) as net_total,
                    COUNT(*) as count
                  FROM " . $this->table_name . " 
                  WHERE YEAR(entry_date) = ? AND MONTH(entry_date) = ?
                  GROUP BY entry_type";

        $stmt = $this->conn->prepare($query);
        $stmt->bindParam(1, $year);
        $stmt->bindParam(2, $month);
        $stmt->execute();

        return $stmt;
    }

    public function getEntryTypeBadge() {
        $badges = [
            'Tulo' => '<span style="background-color: #28a745; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Tulo</span>',
            'Kulu' => '<span style="background-color: #dc3545; color: white; padding: 2px 6px; border-radius: 4px; font-size: 12px;">Kulu</span>'
        ];
        
        return $badges[$this->entry_type] ?? $this->entry_type;
    }

    public function getEntryTypeName() {
        $types = [
            'Tulo' => 'Tulo',
            'Kulu' => 'Kulu'
        ];
        
        return $types[$this->entry_type] ?? $this->entry_type;
    }
}
?>
