import 'purchase_order_item.dart'; import 'vendor.dart'; class PurchaseOrder { final int? id; final int? userId; final int? vendorId; final String poNumber; final DateTime issueDate; final DateTime? dueDate; final double subtotal; final double taxTotal; final double discountTotal; final double totalAmount; final String status; final String? notes; final DateTime? createdAt; final DateTime? updatedAt; final DateTime? poDate; final double amountPaid; final DateTime? nextPaymentDate; final String? vendorInvoiceUrl; final List items; final Vendor? vendor; PurchaseOrder({ this.id, this.userId, this.vendorId, required this.poNumber, required this.issueDate, this.dueDate, this.subtotal = 0.0, this.taxTotal = 0.0, this.discountTotal = 0.0, this.totalAmount = 0.0, this.status = 'PENDING', this.notes, this.createdAt, this.updatedAt, this.poDate, this.amountPaid = 0.0, this.nextPaymentDate, this.vendorInvoiceUrl, this.items = const [], this.vendor, }); factory PurchaseOrder.fromJson(Map json) { return PurchaseOrder( id: json['id'], userId: json['userId'], vendorId: json['vendorId'], poNumber: json['poNumber'] ?? '', issueDate: json['issueDate'] != null ? DateTime.parse(json['issueDate']) : DateTime.now(), dueDate: json['dueDate'] != null ? DateTime.parse(json['dueDate']) : null, subtotal: (json['subtotal'] ?? 0.0).toDouble(), taxTotal: (json['taxTotal'] ?? 0.0).toDouble(), discountTotal: (json['discountTotal'] ?? 0.0).toDouble(), totalAmount: (json['totalAmount'] ?? 0.0).toDouble(), status: json['status'] ?? 'PENDING', notes: json['notes'], createdAt: json['createdAt'] != null ? DateTime.parse(json['createdAt']) : null, updatedAt: json['updatedAt'] != null ? DateTime.parse(json['updatedAt']) : null, poDate: json['poDate'] != null ? DateTime.parse(json['poDate']) : null, amountPaid: (json['amountPaid'] ?? 0.0).toDouble(), nextPaymentDate: json['nextPaymentDate'] != null ? DateTime.parse(json['nextPaymentDate']) : null, vendorInvoiceUrl: json['vendorInvoiceUrl'], items: json['items'] != null ? (json['items'] as List).map((i) => PurchaseOrderItem.fromJson(i)).toList() : [], vendor: json['vendor'] != null ? Vendor.fromJson(json['vendor']) : null, ); } Map toJson() { return { 'id': id, 'userId': userId, 'vendorId': vendorId, 'poNumber': poNumber, 'issueDate': issueDate.toIso8601String(), 'dueDate': dueDate?.toIso8601String(), 'subtotal': subtotal, 'taxTotal': taxTotal, 'discountTotal': discountTotal, 'totalAmount': totalAmount, 'status': status, 'notes': notes, 'poDate': poDate?.toIso8601String(), 'amountPaid': amountPaid, 'nextPaymentDate': nextPaymentDate?.toIso8601String(), 'vendorInvoiceUrl': vendorInvoiceUrl, 'items': items.map((i) => i.toJson()).toList(), }; } PurchaseOrder copyWith({ int? id, int? userId, int? vendorId, String? poNumber, DateTime? issueDate, DateTime? dueDate, double? subtotal, double? taxTotal, double? discountTotal, double? totalAmount, String? status, String? notes, DateTime? poDate, double? amountPaid, DateTime? nextPaymentDate, String? vendorInvoiceUrl, List? items, Vendor? vendor, }) { return PurchaseOrder( id: id ?? this.id, userId: userId ?? this.userId, vendorId: vendorId ?? this.vendorId, poNumber: poNumber ?? this.poNumber, issueDate: issueDate ?? this.issueDate, dueDate: dueDate ?? this.dueDate, subtotal: subtotal ?? this.subtotal, taxTotal: taxTotal ?? this.taxTotal, discountTotal: discountTotal ?? this.discountTotal, totalAmount: totalAmount ?? this.totalAmount, status: status ?? this.status, notes: notes ?? this.notes, createdAt: createdAt, updatedAt: updatedAt, poDate: poDate ?? this.poDate, amountPaid: amountPaid ?? this.amountPaid, nextPaymentDate: nextPaymentDate ?? this.nextPaymentDate, vendorInvoiceUrl: vendorInvoiceUrl ?? this.vendorInvoiceUrl, items: items ?? this.items, vendor: vendor ?? this.vendor, ); } }