import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import '../models/customer.dart'; import '../models/product.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_id INTEGER 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)'); print('Database created with version: 1'); } 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]); } 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]); } String encodeToJson(Object? data) { try { if (data == null) return ''; if (data is String) return data; final json = StringBuffer('{'); var first = true; if (data is Map) { for (var key in data.keys) { if (!first) json.write(','); first = false; json.write('"${key}":"${data[key]}"'); } } else if (data is List) { for (var item in data) { if (!first) json.write(','); first = false; json.write('{"val":"$item"}'); } } json.write('}'); return json.toString(); } catch (e) { return ''; } } Future insertSales(Map salesData) async { final db = await database; if (salesData['product_items'] != null && salesData['product_items'] is List) { salesData['product_items'] = encodeToJson(salesData['product_items']); } 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; if (salesData['product_items'] != null && salesData['product_items'] is List) { salesData['product_items'] = encodeToJson(salesData['product_items']); } 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]); } Future insertEstimate(Map estimateData) async { final db = await database; if (estimateData['product_items'] != null && estimateData['product_items'] is List) { estimateData['product_items'] = encodeToJson(estimateData['product_items']); } 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; if (estimateData['product_items'] != null && estimateData['product_items'] is List) { estimateData['product_items'] = encodeToJson(estimateData['product_items']); } 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]); } 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]); } 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]); } 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]); } 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(); } }