109 lines
2.6 KiB
Dart
Raw Normal View History

2025-07-30 22:38:44 +07:00
import 'dart:convert';
import 'dart:developer';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
class ProductQuantity {
final Product product;
2025-08-04 17:42:39 +07:00
ProductVariant? variant;
2025-07-30 22:38:44 +07:00
int quantity;
String notes;
ProductQuantity({
required this.product,
required this.quantity,
this.notes = '',
2025-08-04 17:42:39 +07:00
this.variant,
2025-07-30 22:38:44 +07:00
});
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is ProductQuantity &&
other.product == product &&
other.quantity == quantity &&
2025-08-04 17:42:39 +07:00
other.variant == variant &&
2025-07-30 22:38:44 +07:00
other.notes == notes;
}
@override
2025-08-04 17:42:39 +07:00
int get hashCode =>
product.hashCode ^ quantity.hashCode ^ variant.hashCode ^ notes.hashCode;
2025-07-30 22:38:44 +07:00
Map<String, dynamic> toMap() {
return {
'product': product.toMap(),
'quantity': quantity,
'notes': notes,
2025-08-04 17:42:39 +07:00
'variant': variant?.toMap(),
2025-07-30 22:38:44 +07:00
};
}
Map<String, dynamic> toLocalMap(int orderId) {
log("OrderProductId: ${product.id}");
return {
'id_order': orderId,
2025-08-03 00:35:00 +07:00
'id_product': product.id,
2025-07-30 22:38:44 +07:00
'quantity': quantity,
'price': product.price,
'notes': notes,
2025-08-04 17:42:39 +07:00
'variant': variant?.toMap(),
2025-07-30 22:38:44 +07:00
};
}
Map<String, dynamic> toServerMap(int? orderId) {
2025-08-03 00:35:00 +07:00
log("toServerMap: ${product.id}");
2025-07-30 22:38:44 +07:00
return {
'id_order': orderId ?? 0,
2025-08-03 00:35:00 +07:00
'id_product': product.id,
2025-07-30 22:38:44 +07:00
'quantity': quantity,
'price': product.price,
'notes': notes,
2025-08-04 17:42:39 +07:00
'variant': variant?.toMap(),
2025-07-30 22:38:44 +07:00
};
}
factory ProductQuantity.fromMap(Map<String, dynamic> map) {
return ProductQuantity(
product: Product.fromMap(map['product']),
quantity: map['quantity']?.toInt() ?? 0,
notes: map['notes'] ?? '',
2025-08-04 17:42:39 +07:00
variant: map['variant'] != null
? ProductVariant.fromMap(map['variant'])
: null,
2025-07-30 22:38:44 +07:00
);
}
factory ProductQuantity.fromLocalMap(Map<String, dynamic> map) {
log("ProductQuantity: $map");
return ProductQuantity(
product: Product.fromOrderMap(map),
quantity: map['quantity']?.toInt() ?? 0,
notes: map['notes'] ?? '',
2025-08-04 17:42:39 +07:00
variant: map['variant'] != null
? ProductVariant.fromMap(map['variant'])
: null,
2025-07-30 22:38:44 +07:00
);
}
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,
2025-08-04 17:42:39 +07:00
variant: variant,
2025-07-30 22:38:44 +07:00
);
}
}