77 lines
No EOL
2.1 KiB
Dart
77 lines
No EOL
2.1 KiB
Dart
// Version: 1.4 - Product モデル定義(簡素化)
|
|
import '../services/database_helper.dart';
|
|
|
|
/// 商品情報モデル
|
|
class Product {
|
|
int? id;
|
|
String productCode; // データベースでは 'product_code' カラム
|
|
String name;
|
|
double unitPrice;
|
|
int quantity; // 管理用(未使用)
|
|
int stock; // 在庫管理用
|
|
DateTime createdAt;
|
|
DateTime updatedAt;
|
|
|
|
Product({
|
|
this.id,
|
|
required this.productCode,
|
|
required this.name,
|
|
required this.unitPrice,
|
|
this.quantity = 0,
|
|
this.stock = 0,
|
|
DateTime? createdAt,
|
|
DateTime? updatedAt,
|
|
}) : createdAt = createdAt ?? DateTime.now(),
|
|
updatedAt = updatedAt ?? DateTime.now();
|
|
|
|
/// マップから Product オブジェクトへ変換
|
|
factory Product.fromMap(Map<String, dynamic> map) {
|
|
return Product(
|
|
id: map['id'] as int?,
|
|
productCode: map['product_code'] as String,
|
|
name: map['name'] as String,
|
|
unitPrice: (map['unit_price'] as num).toDouble(),
|
|
quantity: map['quantity'] as int? ?? 0,
|
|
stock: map['stock'] 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,
|
|
'product_code': productCode,
|
|
'name': name,
|
|
'unit_price': unitPrice,
|
|
'quantity': quantity,
|
|
'stock': stock,
|
|
'created_at': createdAt.toIso8601String(),
|
|
'updated_at': updatedAt.toIso8601String(),
|
|
};
|
|
}
|
|
|
|
/// カピービルダ
|
|
Product copyWith({
|
|
int? id,
|
|
String? productCode,
|
|
String? name,
|
|
double? unitPrice,
|
|
int? quantity,
|
|
int? stock,
|
|
DateTime? createdAt,
|
|
DateTime? updatedAt,
|
|
}) {
|
|
return Product(
|
|
id: id ?? this.id,
|
|
productCode: productCode ?? this.productCode,
|
|
name: name ?? this.name,
|
|
unitPrice: unitPrice ?? this.unitPrice,
|
|
quantity: quantity ?? this.quantity,
|
|
stock: stock ?? this.stock,
|
|
createdAt: createdAt ?? this.createdAt,
|
|
updatedAt: updatedAt ?? this.updatedAt,
|
|
);
|
|
}
|
|
} |