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

110 lines
No EOL
2.8 KiB
Dart
Raw 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.0.0
import 'dart:convert';
/// 見積Estimateモデル
class Estimate {
final int id;
final String estimateNo; // 見積書 No.
final int? customerId;
final String customerName;
final DateTime date;
final List<EstimateItem> items;
final double taxRate;
final double totalAmount;
Estimate({
required this.id,
required this.estimateNo,
this.customerId,
required this.customerName,
required this.date,
required this.items,
required this.taxRate,
required this.totalAmount,
});
/// 引数の ID が重複しているか確認するnull 許容)
bool hasDuplicateId(int? id) {
if (id == null) return false;
// items に productId が重複していないか確認
final itemIds = items.map((item) => item.productId).toSet();
return !itemIds.contains(id);
}
Map<String, dynamic> toMap() {
return {
'id': id,
'estimateNo': estimateNo,
'customerId': customerId,
'customerName': customerName,
'date': date.toIso8601String(),
'itemsJson': jsonEncode(items.map((e) => e.toMap()).toList()),
'taxRate': taxRate,
'totalAmount': totalAmount,
};
}
factory Estimate.fromMap(Map<String, dynamic> map) {
final items = (map['itemsJson'] as String?) != null
? ((jsonDecode(map['itemsJson']) as List)
.map((e) => EstimateItem.fromMap(e as Map))
.toList())
: [];
return Estimate(
id: map['id'] as int,
estimateNo: map['estimateNo'] as String,
customerId: map['customerId'] as int?,
customerName: map['customerName'] as String,
date: DateTime.parse(map['date'] as String),
items: items,
taxRate: (map['taxRate'] as num).toDouble(),
totalAmount: (map['totalAmount'] as num).toDouble(),
);
}
String toJson() => jsonEncode(toMap());
}
/// 見積行EstimateItemモデル
class EstimateItem {
final int id;
final int? productId;
final String productName;
final int quantity;
final double unitPrice;
final double total;
EstimateItem({
required this.id,
this.productId,
required this.productName,
required this.quantity,
required this.unitPrice,
required this.total,
});
Map<String, dynamic> toMap() {
return {
'id': id,
'productId': productId,
'productName': productName,
'quantity': quantity,
'unitPrice': unitPrice,
'total': total,
};
}
factory EstimateItem.fromMap(Map<String, dynamic> map) {
return EstimateItem(
id: map['id'] as int,
productId: map['productId'] as int?,
productName: map['productName'] as String,
quantity: map['quantity'] as int,
unitPrice: (map['unitPrice'] as num).toDouble(),
total: (map['total'] as num).toDouble(),
);
}
String toJson() => jsonEncode(toMap());
}