2025-08-03 00:35:00 +07:00

95 lines
2.1 KiB
Dart

import 'dart:convert';
import 'dart:developer';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
class ProductQuantity {
final Product product;
int quantity;
String notes;
ProductQuantity({
required this.product,
required this.quantity,
this.notes = '',
});
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ProductQuantity &&
other.product == product &&
other.quantity == quantity &&
other.notes == notes;
}
@override
int get hashCode => product.hashCode ^ quantity.hashCode ^ notes.hashCode;
Map<String, dynamic> toMap() {
return {
'product': product.toMap(),
'quantity': quantity,
'notes': notes,
};
}
Map<String, dynamic> toLocalMap(int orderId) {
log("OrderProductId: ${product.id}");
return {
'id_order': orderId,
'id_product': product.id,
'quantity': quantity,
'price': product.price,
'notes': notes,
};
}
Map<String, dynamic> toServerMap(int? orderId) {
log("toServerMap: ${product.id}");
return {
'id_order': orderId ?? 0,
'id_product': product.id,
'quantity': quantity,
'price': product.price,
'notes': notes,
};
}
factory ProductQuantity.fromMap(Map<String, dynamic> map) {
return ProductQuantity(
product: Product.fromMap(map['product']),
quantity: map['quantity']?.toInt() ?? 0,
notes: map['notes'] ?? '',
);
}
factory ProductQuantity.fromLocalMap(Map<String, dynamic> map) {
log("ProductQuantity: $map");
return ProductQuantity(
product: Product.fromOrderMap(map),
quantity: map['quantity']?.toInt() ?? 0,
notes: map['notes'] ?? '',
);
}
String toJson() => json.encode(toMap());
factory ProductQuantity.fromJson(String source) =>
ProductQuantity.fromMap(json.decode(source));
ProductQuantity copyWith({
Product? product,
int? quantity,
String? notes,
}) {
return ProductQuantity(
product: product ?? this.product,
quantity: quantity ?? this.quantity,
notes: notes ?? this.notes,
);
}
}