<?php
class Database {
    private $db;
    private $dbPath;

    public function __construct() {
        $this->dbPath = __DIR__ . '/invoices.db'; // Путь к файлу базы данных
        $this->db = new PDO('sqlite:' . $this->dbPath);
        $this->initialize();
    }

    private function initialize() {
    $this->db->exec("CREATE TABLE IF NOT EXISTS invoices (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        customer TEXT NOT NULL,
        description TEXT,
        amount REAL NOT NULL,
        date TEXT NOT NULL,
        prepuesto TEXT
    )");
}


    private function databaseExists() {
        // Проверка существования файла базы данных
        return file_exists($this->dbPath);
    }

     public function addInvoice($customer, $description, $amount, $date, $prepuesto) {
        $stmt = $this->db->prepare("INSERT INTO invoices (customer, description, amount, date, prepuesto) VALUES (?, ?, ?, ?, ?)");
        $stmt->execute([$customer, $description, $amount, $date, $prepuesto]);
    }

    public function getAllInvoices() {
        $stmt = $this->db->query("SELECT * FROM invoices");
        return $stmt->fetchAll(PDO::FETCH_ASSOC);
    }
}
?>
