import 'dart:io'; import 'package:path_provider/path_provider.dart'; import 'package:sqflite/sqflite.dart'; import 'package:path/path.dart'; import '../models/warehouse_mode.dart'; class SqfliteDatabaseHelper { SqfliteDatabaseHelper.internal(); static final SqfliteDatabaseHelper instance = SqfliteDatabaseHelper.internal(); factory SqfliteDatabaseHelper() => instance; static final warehouseTable = 'm_warehouses'; static final typePetiTable = 'type_petis'; static final conditionPetiTable = 'kondisi_petis'; static final customerTable = 'customers'; static final petiTable = 'petis'; static final transferPetiTable = 'transfers'; static final disposalTable = 'disposals'; static final asset_statusesTable = 'asset_statuses'; // static final assetLocalTable = 'asset_statuses_local'; static final peminjamanTable = 'peminjamans'; static final ipDomainTable = 'ipdomain'; static final pengembalianTable = 'pengembalians'; static final _version = 1; // Versi database ditingkatkan Database? _db; Future get db async { if (_db != null) { return _db; } _db = await initDb(); return _db; } Future saveWarehouseList(List warehouseList) async { final dbClient = await db; try { await dbClient!.transaction((txn) async { var batch = txn.batch(); for (var warehouse in warehouseList) { batch.insert( warehouseTable, warehouse.toJson(), conflictAlgorithm: ConflictAlgorithm.replace, ); } await batch.commit(); }); } catch (e) { print('Error saving warehouse list to SQLite: $e'); } } Future initDb() async { final Directory directory = await getApplicationDocumentsDirectory(); String dbPath = join(directory.path, 'siopas.db'); print(dbPath); var openDb = await openDatabase(dbPath, version: _version, onCreate: (Database db, int version) async { await db.execute(""" CREATE TABLE $ipDomainTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, ipOrDomain TEXT ); """); await db.execute(""" CREATE TABLE $peminjamanTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id INTEGER NULL, peti_id INTEGER NULL, customer_id INTEGER NULL, warehouse_id INTEGER NULL, exit_at DATETIME NULL, est_pengembalian DATETIME NULL, exit_pic TEXT NULL, enter_at DATETIME NULL, enter_pic TEXT NULL, enter_warehouse INTEGER NULL, kondisi_peti_id INTEGER NULL, status INTEGER NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $pengembalianTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id INTEGER NULL, peti_id INTEGER NULL, customer_id INTEGER NULL, warehouse_id INTEGER NULL, exit_at DATETIME NULL, est_pengembalian DATETIME NULL, exit_pic TEXT NULL, enter_at DATETIME NULL, enter_pic TEXT NULL, enter_warehouse INTEGER NULL, kondisi_peti_id INTEGER NULL, status INTEGER NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $asset_statusesTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id INTEGER NOT NULL, peti_id INTEGER NULL, customer_id INTEGER NULL, warehouse_id INTEGER NULL, exit_at DATETIME NULL, est_pengembalian DATETIME NULL, exit_pic TEXT NULL, enter_at DATETIME NULL, enter_pic TEXT NULL, enter_warehouse INTEGER NULL, kondisi_peti_id INTEGER NULL, status INTEGER NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $warehouseTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id TEXT NULL, name TEXT NULL, description TEXT NULL, address TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $typePetiTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, type TEXT NULL, size_peti TEXT NULL, description TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $conditionPetiTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, nama_kondisi TEXT NULL, deskripsi_kondisi TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $customerTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NULL, code_customer TEXT NULL, lot_no TEXT NULL, no_tlp TEXT NULL, address TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $petiTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, fix_lot TEXT NULL, customer_id INTEGER NULL, warehouse_id INTEGER NULL, kondisipeti_id INTEGER NULL, status TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, FOREIGN KEY (customer_id) REFERENCES $customerTable(id), FOREIGN KEY (warehouse_id) REFERENCES $warehouseTable(id) ); """); await db.execute(""" CREATE TABLE $transferPetiTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id TEXT NULL, peti_id INTEGER NULL, name_customer INTEGER NULL, source_warehouse INTEGER NULL, destination_warehouse INTEGER NULL, date DATETIME NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); await db.execute(""" CREATE TABLE $disposalTable ( id INTEGER PRIMARY KEY AUTOINCREMENT, mobile_id TEXT NULL, peti_id INTEGER NULL, customer_id INTEGER NULL, warehouse_id INTEGER NULL, date_disposal DATETIME NULL, description LONGTEXT NULL, status_disposal TEXT NULL, created_at TIMESTAMP NULL, updated_at TIMESTAMP NULL, deleted_at TIMESTAMP NULL, created_by TEXT NULL, updated_by TEXT NULL ); """); // Tambahkan CREATE TABLE untuk tabel-tabel lainnya di sini }, onUpgrade: (Database db, int oldversion, int newversion) async { if (oldversion < newversion) { print("Version Upgrade"); // Tambahkan pernyataan ALTER TABLE jika diperlukan } }); print('db initialize'); return openDb; } }