Files
app-grosir/lib/models/sale.dart
2026-07-23 22:12:32 +07:00

119 lines
3.4 KiB
Dart

class SaleItem {
final String productId;
final String name;
final double price;
final int qty;
final bool isReseller;
SaleItem({
required this.productId,
required this.name,
required this.price,
required this.qty,
this.isReseller = false,
});
double get subtotal => price * qty;
factory SaleItem.fromJson(Map<String, dynamic> json) {
return SaleItem(
productId: json['product_id'] as String? ?? '',
name: json['name'] as String,
price: (json['price'] as num).toDouble(),
qty: json['qty'] as int,
isReseller: json['isReseller'] as bool? ?? false,
);
}
Map<String, dynamic> toJson() => {
'product_id': productId,
'name': name,
'price': price,
'qty': qty,
'isReseller': isReseller,
};
}
class Sale {
final String id;
final DateTime date;
final String? userId;
final List<SaleItem> items;
final double subtotal;
final double discount;
final double total;
final double paid;
final double change;
final double totalCost;
final bool isCancelled;
final String paymentMethod;
final String paymentStatus;
final double dueAmount;
final String? customerPhone;
final Map<String, double>? splitPayment;
Sale({
required this.id,
required this.date,
this.userId,
required this.items,
required this.subtotal,
required this.discount,
required this.total,
required this.paid,
required this.change,
required this.totalCost,
this.isCancelled = false,
this.paymentMethod = 'tunai',
this.paymentStatus = 'paid',
this.dueAmount = 0.0,
this.customerPhone,
this.splitPayment,
});
factory Sale.fromJson(Map<String, dynamic> json) {
return Sale(
id: json['id'] as String,
date: DateTime.parse(json['created_at'] as String),
userId: json['user_id'] as String?,
items: (json['items'] as List)
.map((e) => SaleItem.fromJson(e as Map<String, dynamic>))
.toList(),
subtotal: (json['subtotal'] as num).toDouble(),
discount: (json['discount'] as num).toDouble(),
total: (json['total'] as num).toDouble(),
paid: (json['paid'] as num).toDouble(),
change: (json['change'] as num).toDouble(),
totalCost: (json['total_cost'] as num?)?.toDouble() ?? 0,
isCancelled: json['is_cancelled'] as bool? ?? false,
paymentMethod: json['payment_method'] as String? ?? 'tunai',
paymentStatus: json['payment_status'] as String? ?? 'paid',
dueAmount: (json['due_amount'] as num?)?.toDouble() ?? 0.0,
customerPhone: json['customer_phone'] as String?,
splitPayment: json['split_payment'] != null
? Map<String, double>.from(json['split_payment'] as Map)
: null,
);
}
Map<String, dynamic> toJson() => {
'id': id,
'created_at': date.toIso8601String(),
'user_id': userId,
'items': items.map((e) => e.toJson()).toList(),
'subtotal': subtotal,
'discount': discount,
'total': total,
'paid': paid,
'change': change,
'total_cost': totalCost,
'is_cancelled': isCancelled,
'payment_method': paymentMethod,
'payment_status': paymentStatus,
'due_amount': dueAmount,
'customer_phone': customerPhone,
if (splitPayment != null && splitPayment!.isNotEmpty)
'split_payment': splitPayment,
};
}