// Version: 1.5 - Sale モデル定義(売上) import '../services/database_helper.dart'; /// 売上項目クラス class SaleItem { int productId; String productName; double unitPrice; int quantity; SaleItem({ required this.productId, required this.productName, required this.unitPrice, this.quantity = 1, }); /// 小計金額(税込)を取得 double get subtotal => unitPrice * quantity; Map toMap() { return { 'productId': productId, 'productName': productName, 'unitPrice': unitPrice, 'quantity': quantity, 'subtotal': subtotal, }; } factory SaleItem.fromMap(Map map) { return SaleItem( productId: map['productId'] as int, productName: map['productName'] as String, unitPrice: (map['unitPrice'] as num).toDouble(), quantity: map['quantity'] as int? ?? 1, ); } static List fromMaps(List maps) { return maps.map((e) => SaleItem.fromMap(e as Map)).toList(); } } /// 売上モデル class Sale { int? id; String customerCode; // データベースでは product_code として保存(顧客コード) DateTime saleDate; double totalAmount; int taxRate; List items = []; DateTime createdAt; DateTime updatedAt; Sale({ this.id, required this.customerCode, required this.saleDate, this.totalAmount = 0.0, this.taxRate = 8, // デフォルト:8% DateTime? createdAt, DateTime? updatedAt, List? items, }) : createdAt = createdAt ?? DateTime.now(), updatedAt = updatedAt ?? DateTime.now(), items = items ?? []; /// マップから Sale オブジェクトへ変換 factory Sale.fromMap(Map map) { return Sale( id: map['id'] as int?, customerCode: map['customer_code'] as String, // 'product_code' を 'customer_code' として扱う saleDate: DateTime.parse(map['sale_date']), totalAmount: (map['total_amount'] as num).toDouble(), taxRate: map['tax_rate'] as int? ?? 8, createdAt: DateTime.parse(map['created_at']), updatedAt: DateTime.parse(map['updated_at']), ); } /// Map に変換 Map toMap() { return { 'id': id, 'customer_code': customerCode, 'sale_date': saleDate.toIso8601String(), 'total_amount': totalAmount, 'tax_rate': taxRate, 'product_items': items.map((item) => item.toMap()).toList(), 'created_at': createdAt.toIso8601String(), 'updated_at': updatedAt.toIso8601String(), }; } /// カピービルダ Sale copyWith({ int? id, String? customerCode, DateTime? saleDate, double? totalAmount, int? taxRate, DateTime? createdAt, DateTime? updatedAt, List? items, }) { return Sale( id: id ?? this.id, customerCode: customerCode ?? this.customerCode, saleDate: saleDate ?? this.saleDate, totalAmount: totalAmount ?? this.totalAmount, taxRate: taxRate ?? this.taxRate, createdAt: createdAt ?? this.createdAt, updatedAt: updatedAt ?? this.updatedAt, items: items ?? this.items, ); } /// 合計金額を再計算(items から集計) void recalculate() { totalAmount = items.fold(0, (sum, item) => sum + item.subtotal); } }