Files
Kifi/kifi-app/lib/features/sales/domain/invoice.dart

251 lines
8.0 KiB
Dart

class InvoiceItem {
final int? id;
final int? invoiceId;
final int? productId;
final int? inventoryItemId;
final String? sku;
final String? hsnCode;
final String? description;
final double quantity;
final double unitPrice;
final double taxRate;
final double discount;
final double makingCharge;
final double otherCharges;
final double total;
InvoiceItem({
this.id,
this.invoiceId,
this.productId,
this.inventoryItemId,
this.hsnCode,
this.sku,
this.description,
required this.quantity,
required this.unitPrice,
this.taxRate = 0.0,
this.discount = 0.0,
this.makingCharge = 0.0,
this.otherCharges = 0.0,
required this.total,
});
InvoiceItem copyWith({
int? id,
int? invoiceId,
int? productId,
int? inventoryItemId,
String? hsnCode,
String? sku,
String? description,
double? quantity,
double? unitPrice,
double? taxRate,
double? discount,
double? makingCharge,
double? otherCharges,
double? total,
}) {
return InvoiceItem(
id: id ?? this.id,
invoiceId: invoiceId ?? this.invoiceId,
productId: productId ?? this.productId,
inventoryItemId: inventoryItemId ?? this.inventoryItemId,
hsnCode: hsnCode ?? this.hsnCode,
sku: sku ?? this.sku,
description: description ?? this.description,
quantity: quantity ?? this.quantity,
unitPrice: unitPrice ?? this.unitPrice,
taxRate: taxRate ?? this.taxRate,
discount: discount ?? this.discount,
makingCharge: makingCharge ?? this.makingCharge,
otherCharges: otherCharges ?? this.otherCharges,
total: total ?? this.total,
);
}
factory InvoiceItem.fromJson(Map<String, dynamic> json) {
return InvoiceItem(
id: json['id'],
invoiceId: json['invoiceId'],
productId: json['productId'],
inventoryItemId: json['inventoryItemId'],
hsnCode: json['hsnCode'] ?? json['hsn_code'],
sku: json['sku'],
description: json['description'],
quantity: json['quantity'].toDouble(),
unitPrice: json['unitPrice'].toDouble(),
taxRate: json['taxRate']?.toDouble() ?? 0.0,
discount: json['discount']?.toDouble() ?? 0.0,
makingCharge: json['makingCharge']?.toDouble() ?? 0.0,
otherCharges: json['otherCharges']?.toDouble() ?? 0.0,
total: json['total'].toDouble(),
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
if (id != null) data['id'] = id;
if (invoiceId != null) data['invoiceId'] = invoiceId;
if (productId != null) data['productId'] = productId;
if (inventoryItemId != null) data['inventoryItemId'] = inventoryItemId;
if (hsnCode != null) data['hsnCode'] = hsnCode;
if (sku != null) data['sku'] = sku;
if (description != null) data['description'] = description;
data['quantity'] = quantity;
data['unitPrice'] = unitPrice;
data['taxRate'] = taxRate;
data['discount'] = discount;
data['makingCharge'] = makingCharge;
data['otherCharges'] = otherCharges;
data['total'] = total;
return data;
}
}
class Invoice {
final int? id;
final int? customerId;
final String invoiceNumber;
final DateTime issueDate;
final DateTime? dueDate;
final double subtotal;
final double taxTotal;
final double discountTotal;
final double totalAmount;
final double amountPaid;
final DateTime? nextPaymentDate;
final String? paymentMethod;
final int? paymentWalletId;
final String status;
final String? notes;
final bool isEmi;
final double? emiAmount;
final String? emiCycle;
final DateTime? emiStartDate;
final List<InvoiceItem> items;
final List<InvoicePayment>? payments;
Invoice({
this.id,
this.customerId,
required this.invoiceNumber,
required this.issueDate,
this.dueDate,
required this.subtotal,
this.taxTotal = 0.0,
this.discountTotal = 0.0,
required this.totalAmount,
this.amountPaid = 0.0,
this.nextPaymentDate,
this.paymentMethod,
this.paymentWalletId,
this.status = 'DRAFT',
this.notes,
this.isEmi = false,
this.emiAmount,
this.emiCycle,
this.emiStartDate,
this.items = const [],
this.payments,
});
factory Invoice.fromJson(Map<String, dynamic> json) {
return Invoice(
id: json['id'],
customerId: json['customerId'],
invoiceNumber: json['invoiceNumber'],
issueDate: DateTime.parse(json['issueDate']),
dueDate: json['dueDate'] != null ? DateTime.parse(json['dueDate']) : null,
subtotal: json['subtotal'].toDouble(),
taxTotal: json['taxTotal']?.toDouble() ?? 0.0,
discountTotal: json['discountTotal']?.toDouble() ?? 0.0,
totalAmount: json['totalAmount'].toDouble(),
amountPaid: json['amountPaid']?.toDouble() ?? 0.0,
nextPaymentDate: json['nextPaymentDate'] != null ? DateTime.parse(json['nextPaymentDate']) : null,
paymentMethod: json['paymentMethod'],
paymentWalletId: json['paymentWalletId'],
status: json['status'] ?? 'DRAFT',
notes: json['notes'],
isEmi: json['isEmi'] ?? false,
emiAmount: json['emiAmount']?.toDouble(),
emiCycle: json['emiCycle'],
emiStartDate: json['emiStartDate'] != null ? DateTime.parse(json['emiStartDate']) : null,
items: json['items'] != null ? (json['items'] as List).map((i) => InvoiceItem.fromJson(i)).toList() : [],
payments: json['payments'] != null ? (json['payments'] as List).map((i) => InvoicePayment.fromJson(i)).toList() : null,
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
if (id != null) data['id'] = id;
if (customerId != null) data['customerId'] = customerId;
data['invoiceNumber'] = invoiceNumber;
data['issueDate'] = issueDate.toIso8601String().split('T')[0];
if (dueDate != null) data['dueDate'] = dueDate!.toIso8601String().split('T')[0];
data['subtotal'] = subtotal;
data['taxTotal'] = taxTotal;
data['discountTotal'] = discountTotal;
data['totalAmount'] = totalAmount;
if (amountPaid > 0) data['amountPaid'] = amountPaid;
if (nextPaymentDate != null) data['nextPaymentDate'] = nextPaymentDate!.toIso8601String().split('T')[0];
if (paymentMethod != null) data['paymentMethod'] = paymentMethod;
if (paymentWalletId != null) data['paymentWalletId'] = paymentWalletId;
data['status'] = status;
if (notes != null) data['notes'] = notes;
data['isEmi'] = isEmi;
if (emiAmount != null) data['emiAmount'] = emiAmount;
if (emiCycle != null) data['emiCycle'] = emiCycle;
if (emiStartDate != null) data['emiStartDate'] = emiStartDate!.toIso8601String().split('T')[0];
data['items'] = items.map((i) => i.toJson()).toList();
if (payments != null) data['payments'] = payments!.map((i) => i.toJson()).toList();
return data;
}
}
class InvoicePayment {
final int? id;
final int? invoiceId;
final double amount;
final DateTime? paymentDate;
final String paymentMethod;
final int? emiInstallmentNumber;
final int? walletId;
InvoicePayment({
this.id,
this.invoiceId,
required this.amount,
this.paymentDate,
required this.paymentMethod,
this.emiInstallmentNumber,
this.walletId,
});
factory InvoicePayment.fromJson(Map<String, dynamic> json) {
return InvoicePayment(
id: json['id'],
invoiceId: json['invoiceId'],
amount: json['amount'].toDouble(),
paymentDate: json['paymentDate'] != null ? DateTime.parse(json['paymentDate']) : null,
paymentMethod: json['paymentMethod'] ?? 'Cash',
emiInstallmentNumber: json['emiInstallmentNumber'],
walletId: json['walletId'],
);
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = {};
if (id != null) data['id'] = id;
if (invoiceId != null) data['invoiceId'] = invoiceId;
data['amount'] = amount;
if (paymentDate != null) data['paymentDate'] = paymentDate!.toIso8601String().split('T')[0];
data['paymentMethod'] = paymentMethod;
if (emiInstallmentNumber != null) data['emiInstallmentNumber'] = emiInstallmentNumber;
if (walletId != null) data['walletId'] = walletId;
return data;
}
}