h-1.flutter.4/@workspace/lib/models/customer.dart

82 lines
No EOL
2.3 KiB
Dart

// Version: 2.0 - Customer モデル定義(リッチフィールド対応拡張)
import '../services/database_helper.dart';
/// 得意先情報モデル(リッチ編集機能拡張)
class Customer {
int? id;
String? customerCode; // データベースでは 'customer_code' カラム
String name = '';
String? email;
String? phone;
String? address;
DateTime? createdAt;
bool? enableEmail; // メール通知可フラグ(拡張)
int? discountRate; // 割引率(%、拡張)
Customer({
this.id,
required this.customerCode,
required this.name,
this.email,
this.phone,
this.address,
DateTime? createdAt,
this.enableEmail = false,
this.discountRate,
}) : createdAt = createdAt ?? DateTime.now();
/// マップから Customer オブジェクトへ変換
factory Customer.fromMap(Map<String, dynamic> map) {
return Customer(
id: map['id'] as int?,
customerCode: map['customer_code'] as String? ?? '',
name: map['name'] as String? ?? '',
email: map['email'] as String?,
phone: map['phone'] as String?,
address: map['address'] as String?,
createdAt: DateTime.parse(map['created_at'] as String),
enableEmail: map['enable_email'] as bool?,
discountRate: map['discount_rate'] as int?,
);
}
/// Map に変換
Map<String, dynamic> toMap() {
return {
'id': id,
'customer_code': customerCode ?? '',
'name': name,
'email': email ?? '',
'phone': phone ?? '',
'address': address ?? '',
'created_at': createdAt?.toIso8601String(),
'enable_email': enableEmail ?? false,
'discount_rate': discountRate,
};
}
/// カピービルダ
Customer copyWith({
int? id,
String? customerCode,
String? name,
String? email,
String? phone,
String? address,
DateTime? createdAt,
bool? enableEmail,
int? discountRate,
}) {
return Customer(
id: id ?? this.id,
customerCode: customerCode ?? this.customerCode,
name: name ?? this.name,
email: email ?? this.email,
phone: phone ?? this.phone,
address: address ?? this.address,
createdAt: createdAt ?? this.createdAt,
enableEmail: enableEmail ?? this.enableEmail,
discountRate: discountRate ?? this.discountRate,
);
}
}