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

95 lines
No EOL
2.8 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.2 - Customer モデル定義
import '../services/database_helper.dart';
/// 顧客(得意先)情報モデル
class Customer {
int? id;
String customerCode; // データベースでは product_code として保存
String name;
String phoneNumber;
String email;
String address;
int? salesPersonId;
int taxRate; // デフォルト8%(標準税率)
int discountRate;
DateTime createdAt;
DateTime updatedAt;
Customer({
this.id,
required this.customerCode,
required this.name,
required this.phoneNumber,
required this.email,
required this.address,
this.salesPersonId,
this.taxRate = 8, // デフォルト8%
this.discountRate = 0,
DateTime? createdAt,
DateTime? updatedAt,
}) : createdAt = createdAt ?? DateTime.now(),
updatedAt = updatedAt ?? DateTime.now();
/// マップから Customer オブジェクトへ変換
factory Customer.fromMap(Map<String, dynamic> map) {
return Customer(
id: map['id'] as int?,
customerCode: map['customer_code'] as String, // 'product_code' を 'customer_code' として扱う
name: map['name'] as String,
phoneNumber: map['phone_number'] as String,
email: map['email'] as String,
address: map['address'] as String,
salesPersonId: map['sales_person_id'] as int?,
taxRate: map['tax_rate'] as int? ?? 8,
discountRate: map['discount_rate'] as int? ?? 0,
createdAt: DateTime.parse(map['created_at'] as String),
updatedAt: DateTime.parse(map['updated_at'] as String),
);
}
/// Map に変換
Map<String, dynamic> toMap() {
return {
'id': id,
'customer_code': customerCode,
'name': name,
'phone_number': phoneNumber,
'email': email,
'address': address,
'sales_person_id': salesPersonId,
'tax_rate': taxRate,
'discount_rate': discountRate,
'created_at': createdAt.toIso8601String(),
'updated_at': updatedAt.toIso8601String(),
};
}
/// カピービルダ
Customer copyWith({
int? id,
String? customerCode,
String? name,
String? phoneNumber,
String? email,
String? address,
int? salesPersonId,
int? taxRate,
int? discountRate,
DateTime? createdAt,
DateTime? updatedAt,
}) {
return Customer(
id: id ?? this.id,
customerCode: customerCode ?? this.customerCode,
name: name ?? this.name,
phoneNumber: phoneNumber ?? this.phoneNumber,
email: email ?? this.email,
address: address ?? this.address,
salesPersonId: salesPersonId ?? this.salesPersonId,
taxRate: taxRate ?? this.taxRate,
discountRate: discountRate ?? this.discountRate,
createdAt: createdAt ?? this.createdAt,
updatedAt: updatedAt ?? this.updatedAt,
);
}
}