import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import 'dart:convert'; import '../models/customer.dart'; import '../models/product.dart'; import '../models/estimate.dart'; class DatabaseHelper { static final DatabaseHelper instance = DatabaseHelper._init(); static Database? _database; DatabaseHelper._init(); Future get database async { if (_database != null) return _database!; _database = await _initDB('customer_assist.db'); return _database!; } Future _initDB(String filePath) async { final dbPath = await getDatabasesPath(); final path = join(dbPath, filePath); return await openDatabase( path, version: 1, onCreate: _createDB, ); } Future _createDB(Database db, int version) async { await db.execute('CREATE TABLE customers (id INTEGER PRIMARY KEY AUTOINCREMENT, customer_code TEXT NOT NULL, name TEXT NOT NULL, phone_number TEXT, email TEXT NOT NULL, address TEXT, sales_person_id INTEGER, tax_rate INTEGER DEFAULT 8, discount_rate INTEGER DEFAULT 0, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE employees (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, position TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE warehouses (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, description TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE suppliers (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL, address TEXT, phone_number TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE products (id INTEGER PRIMARY KEY AUTOINCREMENT, product_code TEXT NOT NULL, name TEXT NOT NULL, unit_price INTEGER NOT NULL, quantity INTEGER DEFAULT 0, stock INTEGER DEFAULT 0, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE sales (id INTEGER PRIMARY KEY AUTOINCREMENT, customer_id INTEGER NOT NULL, sale_date TEXT NOT NULL, total_amount INTEGER NOT NULL, tax_rate INTEGER DEFAULT 8, product_items TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE estimates (id INTEGER PRIMARY KEY AUTOINCREMENT, customer_code TEXT NOT NULL, estimate_number TEXT NOT NULL, product_items TEXT, total_amount INTEGER NOT NULL, tax_rate INTEGER DEFAULT 8, status TEXT DEFAULT "open", expiry_date TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE inventory (id INTEGER PRIMARY KEY AUTOINCREMENT, product_code TEXT UNIQUE NOT NULL, name TEXT NOT NULL, unit_price INTEGER NOT NULL, stock INTEGER DEFAULT 0, min_stock INTEGER DEFAULT 0, max_stock INTEGER DEFAULT 1000, supplier_name TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); await db.execute('CREATE TABLE invoices (id INTEGER PRIMARY KEY AUTOINCREMENT, customer_code TEXT NOT NULL, invoice_number TEXT NOT NULL, sale_date TEXT NOT NULL, total_amount INTEGER NOT NULL, tax_rate INTEGER DEFAULT 8, status TEXT DEFAULT "paid", product_items TEXT, created_at TEXT NOT NULL, updated_at TEXT NOT NULL)'); print('Database created with version: 1'); } // Customer API Future insertCustomer(Customer customer) async { final db = await database; return await db.insert('customers', customer.toMap()); } Future getCustomer(int id) async { final db = await database; final results = await db.query('customers', where: 'id = ?', whereArgs: [id]); if (results.isEmpty) return null; return Customer.fromMap(results.first); } Future> getCustomers() async { final db = await database; final results = await db.query('customers'); return results.map((e) => Customer.fromMap(e)).toList(); } Future updateCustomer(Customer customer) async { final db = await database; return await db.update('customers', customer.toMap(), where: 'id = ?', whereArgs: [customer.id]); } Future deleteCustomer(int id) async { final db = await database; return await db.delete('customers', where: 'id = ?', whereArgs: [id]); } // Product API Future insertProduct(Product product) async { final db = await database; return await db.insert('products', product.toMap()); } Future getProduct(int id) async { final db = await database; final results = await db.query('products', where: 'id = ?', whereArgs: [id]); if (results.isEmpty) return null; return Product.fromMap(results.first); } Future> getProducts() async { final db = await database; final results = await db.query('products'); return results.map((e) => Product.fromMap(e)).toList(); } Future updateProduct(Product product) async { final db = await database; return await db.update('products', product.toMap(), where: 'id = ?', whereArgs: [product.id]); } Future deleteProduct(int id) async { final db = await database; return await db.delete('products', where: 'id = ?', whereArgs: [id]); } // Sales API Future insertSales(Map salesData) async { final db = await database; return await db.insert('sales', salesData); } Future>> getSales() async { final db = await database; return await db.query('sales'); } Future updateSales(Map salesData) async { final db = await database; return await db.update('sales', salesData, where: 'id = ?', whereArgs: [salesData['id'] as int]); } Future deleteSales(int id) async { final db = await database; return await db.delete('sales', where: 'id = ?', whereArgs: [id]); } // Estimate API(単純化) Future insertEstimate(Map estimateData) async { final db = await database; return await db.insert('estimates', estimateData); } Future>> getEstimates() async { final db = await database; return await db.query('estimates'); } Future updateEstimate(Map estimateData) async { final db = await database; return await db.update('estimates', estimateData, where: 'id = ?', whereArgs: [estimateData['id'] as int]); } Future deleteEstimate(int id) async { final db = await database; return await db.delete('estimates', where: 'id = ?', whereArgs: [id]); } // Invoice API Future insertInvoice(Map invoiceData) async { final db = await database; return await db.insert('invoices', invoiceData); } Future>> getInvoices() async { final db = await database; return await db.query('invoices'); } Future updateInvoice(Map invoiceData) async { final db = await database; return await db.update('invoices', invoiceData, where: 'id = ?', whereArgs: [invoiceData['id'] as int]); } Future deleteInvoice(int id) async { final db = await database; return await db.delete('invoices', where: 'id = ?', whereArgs: [id]); } // Inventory API Future insertInventory(Map inventoryData) async { final db = await database; return await db.insert('inventory', inventoryData); } Future>> getInventory() async { final db = await database; return await db.query('inventory'); } Future updateInventory(Map inventoryData) async { final db = await database; return await db.update('inventory', inventoryData, where: 'id = ?', whereArgs: [inventoryData['id'] as int]); } Future deleteInventory(int id) async { final db = await database; return await db.delete('inventory', where: 'id = ?', whereArgs: [id]); } // Employee API Future insertEmployee(Map employeeData) async { final db = await database; return await db.insert('employees', employeeData); } Future>> getEmployees() async { final db = await database; return await db.query('employees'); } Future updateEmployee(Map employeeData) async { final db = await database; return await db.update('employees', employeeData, where: 'id = ?', whereArgs: [employeeData['id'] as int]); } Future deleteEmployee(int id) async { final db = await database; return await db.delete('employees', where: 'id = ?', whereArgs: [id]); } // Warehouse API Future insertWarehouse(Map warehouseData) async { final db = await database; return await db.insert('warehouses', warehouseData); } Future>> getWarehouses() async { final db = await database; return await db.query('warehouses'); } Future updateWarehouse(Map warehouseData) async { final db = await database; return await db.update('warehouses', warehouseData, where: 'id = ?', whereArgs: [warehouseData['id'] as int]); } Future deleteWarehouse(int id) async { final db = await database; return await db.delete('warehouses', where: 'id = ?', whereArgs: [id]); } // Supplier API Future insertSupplier(Map supplierData) async { final db = await database; return await db.insert('suppliers', supplierData); } Future>> getSuppliers() async { final db = await database; return await db.query('suppliers'); } Future updateSupplier(Map supplierData) async { final db = await database; return await db.update('suppliers', supplierData, where: 'id = ?', whereArgs: [supplierData['id'] as int]); } Future deleteSupplier(int id) async { final db = await database; return await db.delete('suppliers', where: 'id = ?', whereArgs: [id]); } Future close() async { final db = await database; db.close(); } }