Some checks are pending
Flutter CI / build (push) Waiting to run
Co-authored-by: aider (ollama_chat/7b) <aider@aider.chat>
63 lines
1.5 KiB
Dart
63 lines
1.5 KiB
Dart
import 'package:sqflite/sqflite.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'dart:io' as io;
|
|
|
|
class DbHelper {
|
|
static Database? _database;
|
|
|
|
Future<Database> get database async {
|
|
if (_database != null) return _database!;
|
|
_database = await initDb();
|
|
return _database!;
|
|
}
|
|
|
|
initDb() async {
|
|
io.Directory documentsDirectory = await getApplicationDocumentsDirectory();
|
|
String path = '${documentsDirectory.path}/invoice.db';
|
|
var db = await openDatabase(path, version: 1, onCreate: _onCreate);
|
|
return db;
|
|
}
|
|
|
|
void _onCreate(Database db, int version) async {
|
|
await db.execute('''
|
|
CREATE TABLE customers (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT,
|
|
phone TEXT,
|
|
address TEXT,
|
|
email TEXT
|
|
)
|
|
''');
|
|
|
|
await db.execute('''
|
|
CREATE TABLE products (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
name TEXT,
|
|
unit_price REAL,
|
|
discount REAL
|
|
)
|
|
''');
|
|
|
|
await db.execute('''
|
|
CREATE TABLE invoices (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
customer_id INTEGER,
|
|
date TEXT,
|
|
total REAL,
|
|
tax REAL,
|
|
discount_total REAL
|
|
)
|
|
''');
|
|
|
|
await db.execute('''
|
|
CREATE TABLE invoice_items (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
invoice_id INTEGER,
|
|
product_id INTEGER,
|
|
quantity INTEGER,
|
|
unit_price REAL,
|
|
discount REAL
|
|
)
|
|
''');
|
|
}
|
|
}
|