h-1.flutter.4/lib/models/sale.dart

123 lines
No EOL
3.3 KiB
Dart
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// 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<String, dynamic> toMap() {
return {
'productId': productId,
'productName': productName,
'unitPrice': unitPrice,
'quantity': quantity,
'subtotal': subtotal,
};
}
factory SaleItem.fromMap(Map<dynamic, dynamic> 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<SaleItem> fromMaps(List<dynamic> 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<SaleItem> 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<SaleItem>? items,
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now(),
items = items ?? [];
/// マップから Sale オブジェクトへ変換
factory Sale.fromMap(Map<String, dynamic> 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<String, dynamic> 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<SaleItem>? 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);
}
}