feat: sales page
This commit is contained in:
@@ -7,7 +7,6 @@ import 'package:enaklo_pos/data/models/response/table_model.dart';
|
||||
import 'package:enaklo_pos/presentation/home/models/order_model.dart';
|
||||
import 'package:enaklo_pos/presentation/table/models/draft_order_item.dart';
|
||||
import 'package:enaklo_pos/presentation/table/models/draft_order_model.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
import 'package:sqflite/sqflite.dart';
|
||||
|
||||
import '../../presentation/home/models/product_quantity.dart';
|
||||
@@ -145,7 +144,7 @@ class ProductLocalDatasource {
|
||||
Future<Database> _initDB(String filePath) async {
|
||||
final dbPath = await getDatabasesPath();
|
||||
final path = dbPath + filePath;
|
||||
|
||||
|
||||
// Force delete existing database to ensure new schema
|
||||
try {
|
||||
final dbExists = await databaseExists(path);
|
||||
@@ -156,20 +155,21 @@ class ProductLocalDatasource {
|
||||
} catch (e) {
|
||||
log("Error deleting database: $e");
|
||||
}
|
||||
|
||||
|
||||
return await openDatabase(
|
||||
path,
|
||||
version: 2,
|
||||
path,
|
||||
version: 2,
|
||||
onCreate: _createDb,
|
||||
onUpgrade: _onUpgrade,
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
Future<void> _onUpgrade(Database db, int oldVersion, int newVersion) async {
|
||||
if (oldVersion < 2) {
|
||||
// Add order_type column to orders table if it doesn't exist
|
||||
try {
|
||||
await db.execute('ALTER TABLE $tableOrder ADD COLUMN order_type TEXT DEFAULT "DINE IN"');
|
||||
await db.execute(
|
||||
'ALTER TABLE $tableOrder ADD COLUMN order_type TEXT DEFAULT "DINE IN"');
|
||||
log("Added order_type column to orders table");
|
||||
} catch (e) {
|
||||
log("order_type column might already exist: $e");
|
||||
@@ -186,11 +186,11 @@ class ProductLocalDatasource {
|
||||
//save order
|
||||
Future<int> saveOrder(OrderModel order) async {
|
||||
final db = await instance.database;
|
||||
|
||||
|
||||
// Since we're forcing database recreation, order_type column should exist
|
||||
final orderMap = order.toMap(includeOrderType: true);
|
||||
log("Final orderMap for insertion: $orderMap");
|
||||
|
||||
|
||||
int id = await db.insert(tableOrder, orderMap,
|
||||
conflictAlgorithm: ConflictAlgorithm.replace);
|
||||
|
||||
@@ -238,6 +238,31 @@ class ProductLocalDatasource {
|
||||
});
|
||||
}
|
||||
|
||||
Future<List<OrderModel>> getAllOrderByRange(
|
||||
DateTime start, DateTime end) async {
|
||||
final db = await instance.database;
|
||||
|
||||
// Format ke ISO 8601 untuk range, hasil: yyyy-MM-ddTHH:mm:ss
|
||||
final startIso = start.toIso8601String();
|
||||
final endIso = end.toIso8601String();
|
||||
|
||||
final startDateYYYYMMDD = startIso.substring(0, 10);
|
||||
final endDateYYYYMMDD = endIso.substring(0, 10);
|
||||
|
||||
final List<Map<String, dynamic>> maps = await db.query(
|
||||
tableOrder,
|
||||
where: 'substr(transaction_time, 1, 10) BETWEEN ? AND ?',
|
||||
whereArgs: [startDateYYYYMMDD, endDateYYYYMMDD],
|
||||
orderBy: 'transaction_time DESC',
|
||||
);
|
||||
log("Get All Order By Range: $startDateYYYYMMDD $endDateYYYYMMDD");
|
||||
|
||||
return List.generate(maps.length, (i) {
|
||||
log("Save save OrderModel: ${OrderModel.fromMap(maps[i])}");
|
||||
return OrderModel.fromMap(maps[i]);
|
||||
});
|
||||
}
|
||||
|
||||
//get order item by order id
|
||||
Future<List<ProductQuantity>> getOrderItemByOrderId(int orderId) async {
|
||||
final db = await instance.database;
|
||||
@@ -402,12 +427,12 @@ class ProductLocalDatasource {
|
||||
Future<List<TableModel>> getTableByStatus(String status) async {
|
||||
final db = await instance.database;
|
||||
List<Map<String, dynamic>> maps;
|
||||
|
||||
|
||||
if (status == 'all') {
|
||||
// Get all tables
|
||||
maps = await db.query(tableManagement);
|
||||
log("Getting all tables, found: ${maps.length}");
|
||||
|
||||
|
||||
// If no tables exist, create some default tables
|
||||
if (maps.isEmpty) {
|
||||
log("No tables found, creating default tables...");
|
||||
@@ -428,19 +453,19 @@ class ProductLocalDatasource {
|
||||
final tables = List.generate(maps.length, (i) {
|
||||
return TableModel.fromMap(maps[i]);
|
||||
});
|
||||
|
||||
|
||||
log("Returning ${tables.length} tables");
|
||||
tables.forEach((table) {
|
||||
log("Table: ${table.tableName} (ID: ${table.id}, Status: ${table.status})");
|
||||
});
|
||||
|
||||
|
||||
return tables;
|
||||
}
|
||||
|
||||
// Create default tables if none exist
|
||||
Future<void> _createDefaultTables() async {
|
||||
final db = await instance.database;
|
||||
|
||||
|
||||
// Create 5 default tables
|
||||
for (int i = 1; i <= 5; i++) {
|
||||
await db.insert(tableManagement, {
|
||||
@@ -463,7 +488,7 @@ class ProductLocalDatasource {
|
||||
await db.update(tableManagement, table.toMap(),
|
||||
where: 'id = ?', whereArgs: [table.id]);
|
||||
log("Success Update Status Table: ${table.toMap()}");
|
||||
|
||||
|
||||
// Verify the update
|
||||
final updatedTable = await db.query(
|
||||
tableManagement,
|
||||
@@ -474,7 +499,7 @@ class ProductLocalDatasource {
|
||||
log("Verified table update: ${updatedTable.first}");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Debug method to reset all tables to available status
|
||||
Future<void> resetAllTablesToAvailable() async {
|
||||
log("Resetting all tables to available status...");
|
||||
@@ -564,7 +589,7 @@ class ProductLocalDatasource {
|
||||
//update draft order
|
||||
Future<void> updateDraftOrder(DraftOrderModel draftOrder) async {
|
||||
final db = await instance.database;
|
||||
|
||||
|
||||
// Update the draft order
|
||||
await db.update(
|
||||
'draft_orders',
|
||||
@@ -572,13 +597,14 @@ class ProductLocalDatasource {
|
||||
where: 'id = ?',
|
||||
whereArgs: [draftOrder.id],
|
||||
);
|
||||
|
||||
|
||||
// Remove existing items and add new ones
|
||||
await db.delete('draft_order_items',
|
||||
where: 'id_draft_order = ?', whereArgs: [draftOrder.id]);
|
||||
|
||||
|
||||
for (var orderItem in draftOrder.orders) {
|
||||
await db.insert('draft_order_items', orderItem.toMapForLocal(draftOrder.id!));
|
||||
await db.insert(
|
||||
'draft_order_items', orderItem.toMapForLocal(draftOrder.id!));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user