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