diff --git a/lib/services/database_helper.dart b/lib/services/database_helper.dart index 5b3f42f..09a2e81 100644 --- a/lib/services/database_helper.dart +++ b/lib/services/database_helper.dart @@ -343,6 +343,47 @@ class DatabaseHelper { //_estimate_snapshots テーブルも必要に応じて追加可 } + // ========== Sales CRUD ====== + + Future insertSales(String saleNo, String customerName, DateTime date, List items) async { + final db = await instance.database; + + // 売上番号の重複チェック + final existing = await db.query('sales', where: 'sale_no = ?', whereArgs: [saleNo]); + if (existing.isNotEmpty) { + throw ArgumentError('売上番号「$saleNo」は既に存在します'); + } + + final itemsJson = jsonEncode(items); + final totalAmount = items.fold(0, (sum, item) => sum + (item['amount'] as num).toDouble()); + + return await db.insert('sales', { + 'sale_no': saleNo, + 'customer_name': customerName, + 'date': date.toIso8601String(), + 'total_amount': totalAmount, + 'tax_rate': 10, + 'items_json': itemsJson, + 'created_at': DateTime.now().toIso8601String(), + 'updated_at': DateTime.now().toIso8601String(), + }); + } + + Future> getSales() async { + final db = await instance.database; + final List> maps = await db.query('sales', orderBy: 'created_at DESC'); + + return (maps as List).map((json) => json); + } + + Future getSales(int id) async { + final db = await instance.database; + final maps = await db.query('sales', where: 'id = ?', whereArgs: [id]); + + if (maps.isEmpty) return null; + return maps.first; + } + // ========== Product CRUD ====== Future insert(Product product) async {