import 'dart:convert'; import 'package:enaklo_pos/data/models/response/product_response_model.dart'; import 'package:enaklo_pos/presentation/home/models/product_quantity.dart'; class OrderDetailResponseModel { final bool? success; final Order? data; final dynamic errors; OrderDetailResponseModel({ this.success, this.data, this.errors, }); factory OrderDetailResponseModel.fromJson(String str) => OrderDetailResponseModel.fromMap(json.decode(str)); String toJson() => json.encode(toMap()); factory OrderDetailResponseModel.fromMap(Map json) => OrderDetailResponseModel( success: json["success"], data: json["data"] == null ? null : Order.fromMap(json["data"]), errors: json["errors"], ); Map toMap() => { "success": success, "data": data?.toMap(), "errors": errors, }; } class OrderResponseModel { final bool? success; final OrderData? data; OrderResponseModel({ this.success, this.data, }); factory OrderResponseModel.fromJson(String str) => OrderResponseModel.fromMap(json.decode(str)); String toJson() => json.encode(toMap()); factory OrderResponseModel.fromMap(Map map) { return OrderResponseModel( success: map['success'] ?? false, data: OrderData.fromMap(map['data']), ); } Map toMap() { return { 'success': success, 'data': data?.toMap(), }; } } class OrderData { final List? orders; final List? payments; final int? totalCount; final int? page; final int? limit; final int? totalPages; OrderData({ required this.orders, required this.payments, required this.totalCount, required this.page, required this.limit, required this.totalPages, }); factory OrderData.fromMap(Map map) { return OrderData( orders: map["orders"] == null ? [] : List.from(map['orders']?.map((x) => Order.fromMap(x))), payments: map["payments"] == null ? [] : List.from(map['payments']?.map((x) => Payment.fromMap(x))), totalCount: map['total_count'], page: map['page'], limit: map['limit'], totalPages: map['total_pages'], ); } Map toMap() { return { 'orders': orders?.map((x) => x.toMap()).toList(), 'payments': payments?.map((x) => x.toMap()).toList(), 'total_count': totalCount, 'page': page, 'limit': limit, 'total_pages': totalPages, }; } } class Order { final String? id; final String? orderNumber; final String? outletId; final String? userId; final String? tableNumber; final String? orderType; final String? status; final int? subtotal; final int? taxAmount; final int? discountAmount; final int? totalAmount; final num? totalCost; final int? remainingAmount; final String? paymentStatus; final int? refundAmount; final bool? isVoid; final bool? isRefund; final String? notes; final Map? metadata; final DateTime? createdAt; final DateTime? updatedAt; final List? orderItems; final List? payments; final int? totalPaid; final int? paymentCount; final String? splitType; Order({ this.id, this.orderNumber, this.outletId, this.userId, this.tableNumber, this.orderType, this.status, this.subtotal, this.taxAmount, this.discountAmount, this.totalAmount, this.totalCost, this.remainingAmount, this.paymentStatus, this.refundAmount, this.isVoid, this.isRefund, this.notes, this.metadata, this.createdAt, this.updatedAt, this.orderItems, this.payments, this.totalPaid, this.paymentCount, this.splitType, }); factory Order.fromMap(Map map) { return Order( id: map['id'], orderNumber: map['order_number'], outletId: map['outlet_id'], userId: map['user_id'], tableNumber: map['table_number'], orderType: map['order_type'], status: map['status'], subtotal: map['subtotal'], taxAmount: map['tax_amount'], discountAmount: map['discount_amount'], totalAmount: map['total_amount'], totalCost: map['total_cost'], remainingAmount: map['remaining_amount'], paymentStatus: map['payment_status'], refundAmount: map['refund_amount'], isVoid: map['is_void'], isRefund: map['is_refund'], notes: map['notes'], metadata: map['metadata'] ?? {}, createdAt: DateTime.parse(map['created_at']), updatedAt: DateTime.parse(map['updated_at']), orderItems: map["order_items"] == null ? [] : List.from( map['order_items'].map((x) => OrderItem.fromMap(x))), payments: map["payments"] == null ? [] : List.from(map['payments'].map((x) => Payment.fromMap(x))), totalPaid: map['total_paid'], paymentCount: map['payment_count'], splitType: map['split_type'] ?? "", ); } Map toMap() { return { 'id': id, 'order_number': orderNumber, 'outlet_id': outletId, 'user_id': userId, 'table_number': tableNumber, 'order_type': orderType, 'status': status, 'subtotal': subtotal, 'tax_amount': taxAmount, 'discount_amount': discountAmount, 'total_amount': totalAmount, 'total_cost': totalCost, 'remaining_amount': remainingAmount, 'payment_status': paymentStatus, 'refund_amount': refundAmount, 'is_void': isVoid, 'is_refund': isRefund, 'notes': notes, 'metadata': metadata, 'created_at': createdAt?.toIso8601String(), 'updated_at': updatedAt?.toIso8601String(), 'order_items': orderItems?.map((x) => x.toMap()).toList(), 'payments': payments?.map((x) => x.toMap()).toList(), 'total_paid': totalPaid, 'payment_count': paymentCount, 'split_type': splitType, }; } } class OrderItem { String? id; String? orderId; String? productId; String? productName; String? productVariantId; String? productVariantName; int? quantity; int? unitPrice; int? totalPrice; List? modifiers; String? notes; String? status; DateTime? createdAt; DateTime? updatedAt; String? printerType; int? paidQuantity; OrderItem({ this.id, this.orderId, this.productId, this.productName, this.productVariantId, this.productVariantName, this.quantity, this.unitPrice, this.totalPrice, this.modifiers, this.notes, this.status, this.createdAt, this.updatedAt, this.printerType, this.paidQuantity, }); factory OrderItem.fromMap(Map map) { return OrderItem( id: map['id'], orderId: map['order_id'], productId: map['product_id'], productName: map['product_name'], productVariantId: map['product_variant_id'], productVariantName: map['product_variant_name'], quantity: map['quantity'], unitPrice: map['unit_price'], totalPrice: map['total_price'], modifiers: map['modifiers'] == null ? [] : List.from(map['modifiers']), notes: map['notes'], status: map['status'], createdAt: DateTime.parse(map['created_at']), updatedAt: DateTime.parse(map['updated_at']), printerType: map['printer_type'], paidQuantity: map['paid_quantity'], ); } Map toMap() { return { 'id': id, 'order_id': orderId, 'product_id': productId, 'product_name': productName, 'product_variant_id': productVariantId, 'product_variant_name': productVariantName, 'quantity': quantity, 'unit_price': unitPrice, 'total_price': totalPrice, 'modifiers': modifiers, 'notes': notes, 'status': status, 'created_at': createdAt?.toIso8601String(), 'updated_at': updatedAt?.toIso8601String(), 'printer_type': printerType, 'paid_quantity': paidQuantity, }; } } class Payment { final String? id; final String? orderId; final String? paymentMethodId; final String? paymentMethodName; final String? paymentMethodType; final int? amount; final String? status; final int? splitNumber; final int? splitTotal; final String? splitDescription; final int? refundAmount; final Map? metadata; final DateTime? createdAt; final DateTime? updatedAt; Payment({ this.id, this.orderId, this.paymentMethodId, this.paymentMethodName, this.paymentMethodType, this.amount, this.status, this.splitNumber, this.splitTotal, this.splitDescription, this.refundAmount, this.metadata, this.createdAt, this.updatedAt, }); factory Payment.fromMap(Map map) { return Payment( id: map['id'], orderId: map['order_id'], paymentMethodId: map['payment_method_id'], paymentMethodName: map['payment_method_name'], paymentMethodType: map['payment_method_type'], amount: map['amount'], status: map['status'], splitNumber: map['split_number'], splitTotal: map['split_total'], splitDescription: map['split_description'], refundAmount: map['refund_amount'], metadata: map['metadata'] ?? {}, createdAt: DateTime.parse(map['created_at']), updatedAt: DateTime.parse(map['updated_at']), ); } Map toMap() { return { 'id': id, 'order_id': orderId, 'payment_method_id': paymentMethodId, 'payment_method_name': paymentMethodName, 'payment_method_type': paymentMethodType, 'amount': amount, 'status': status, 'split_number': splitNumber, 'split_total': splitTotal, 'split_description': splitDescription, 'refund_amount': refundAmount, 'metadata': metadata, 'created_at': createdAt?.toIso8601String(), 'updated_at': updatedAt?.toIso8601String(), }; } } extension OrderItemListExtension on List { List toProductQuantities() => map((e) => ProductQuantity( product: Product( id: e.productId, name: e.productName, price: e.unitPrice, printerType: e.printerType, ), variant: ProductVariant( id: e.productVariantId, name: e.productVariantName, productId: e.productId, ), notes: e.notes ?? '', quantity: e.quantity ?? 0, )).toList(); } extension OrderCopyWith on Order { Order copyWith({ String? id, String? orderNumber, String? outletId, String? userId, String? tableNumber, String? orderType, String? status, int? subtotal, int? taxAmount, int? discountAmount, int? totalAmount, num? totalCost, int? remainingAmount, String? paymentStatus, int? refundAmount, bool? isVoid, bool? isRefund, String? notes, Map? metadata, DateTime? createdAt, DateTime? updatedAt, List? orderItems, List? payments, int? totalPaid, int? paymentCount, String? splitType, }) { return Order( id: id ?? this.id, orderNumber: orderNumber ?? this.orderNumber, outletId: outletId ?? this.outletId, userId: userId ?? this.userId, tableNumber: tableNumber ?? this.tableNumber, orderType: orderType ?? this.orderType, status: status ?? this.status, subtotal: subtotal ?? this.subtotal, taxAmount: taxAmount ?? this.taxAmount, discountAmount: discountAmount ?? this.discountAmount, totalAmount: totalAmount ?? this.totalAmount, totalCost: totalCost ?? this.totalCost, remainingAmount: remainingAmount ?? this.remainingAmount, paymentStatus: paymentStatus ?? this.paymentStatus, refundAmount: refundAmount ?? this.refundAmount, isVoid: isVoid ?? this.isVoid, isRefund: isRefund ?? this.isRefund, notes: notes ?? this.notes, metadata: metadata ?? this.metadata, createdAt: createdAt ?? this.createdAt, updatedAt: updatedAt ?? this.updatedAt, orderItems: orderItems ?? this.orderItems, payments: payments ?? this.payments, totalPaid: totalPaid ?? this.totalPaid, paymentCount: paymentCount ?? this.paymentCount, splitType: splitType ?? this.splitType, ); } } extension OrderItemCopyWith on OrderItem { OrderItem copyWith({ String? id, String? orderId, String? productId, String? productName, String? productVariantId, String? productVariantName, int? quantity, int? unitPrice, int? totalPrice, List? modifiers, String? notes, String? status, DateTime? createdAt, DateTime? updatedAt, String? printerType, int? paidQuantity, }) { return OrderItem( id: id ?? this.id, orderId: orderId ?? this.orderId, productId: productId ?? this.productId, productName: productName ?? this.productName, productVariantId: productVariantId ?? this.productVariantId, productVariantName: productVariantName ?? this.productVariantName, quantity: quantity ?? this.quantity, unitPrice: unitPrice ?? this.unitPrice, totalPrice: totalPrice ?? this.totalPrice, modifiers: modifiers ?? this.modifiers, notes: notes ?? this.notes, status: status ?? this.status, createdAt: createdAt ?? this.createdAt, updatedAt: updatedAt ?? this.updatedAt, printerType: printerType ?? this.printerType, paidQuantity: paidQuantity ?? this.paidQuantity, ); } }