// Version: 1.5 - 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; // 仕入先マスタ情報フィールド(サプライヤーとの関連付け用) String? supplierContactName; String? supplierPhoneNumber; String? email; String? address; Product({ this.id, required this.productCode, required this.name, required this.unitPrice, this.quantity = 0, this.stock = 0, DateTime? createdAt, DateTime? updatedAt, this.supplierContactName, this.supplierPhoneNumber, this.email, this.address, }) : createdAt = createdAt ?? DateTime.now(), updatedAt = updatedAt ?? DateTime.now(); /// マップから Product オブジェクトへ変換 factory Product.fromMap(Map 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), supplierContactName: map['supplier_contact_name'] as String?, supplierPhoneNumber: map['supplier_phone_number'] as String?, email: map['email'] as String?, address: map['address'] as String?, ); } /// Map に変換 Map toMap() { return { 'id': id, 'product_code': productCode, 'name': name, 'unit_price': unitPrice, 'quantity': quantity, 'stock': stock, 'created_at': createdAt.toIso8601String(), 'updated_at': updatedAt.toIso8601String(), 'supplier_contact_name': supplierContactName ?? '', 'supplier_phone_number': supplierPhoneNumber ?? '', 'email': email ?? '', 'address': address ?? '', }; } /// カピービルダ Product copyWith({ int? id, String? productCode, String? name, double? unitPrice, int? quantity, int? stock, DateTime? createdAt, DateTime? updatedAt, String? supplierContactName, String? supplierPhoneNumber, String? email, String? address, }) { 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, supplierContactName: supplierContactName ?? this.supplierContactName, supplierPhoneNumber: supplierPhoneNumber ?? this.supplierPhoneNumber, email: email ?? this.email, address: address ?? this.address, ); } }