add auth, format

This commit is contained in:
Phuoc Nguyen
2025-11-07 11:52:06 +07:00
parent 24a8508fce
commit 3803bd26e0
173 changed files with 8505 additions and 7116 deletions

View File

@@ -27,7 +27,9 @@ class InvoicesLocalDataSource {
.map((json) => InvoiceModel.fromJson(json as Map<String, dynamic>))
.toList();
debugPrint('[InvoicesLocalDataSource] Loaded ${invoices.length} invoices');
debugPrint(
'[InvoicesLocalDataSource] Loaded ${invoices.length} invoices',
);
return invoices;
} catch (e, stackTrace) {
debugPrint('[InvoicesLocalDataSource] Error loading invoices: $e');
@@ -65,8 +67,11 @@ class InvoicesLocalDataSource {
final filtered = allInvoices
.where(
(invoice) =>
invoice.invoiceNumber.toLowerCase().contains(query.toLowerCase()) ||
(invoice.orderId?.toLowerCase().contains(query.toLowerCase()) ?? false),
invoice.invoiceNumber.toLowerCase().contains(
query.toLowerCase(),
) ||
(invoice.orderId?.toLowerCase().contains(query.toLowerCase()) ??
false),
)
.toList();
@@ -89,7 +94,9 @@ class InvoicesLocalDataSource {
orElse: () => throw Exception('Invoice not found: $invoiceId'),
);
debugPrint('[InvoicesLocalDataSource] Found invoice: ${invoice.invoiceNumber}');
debugPrint(
'[InvoicesLocalDataSource] Found invoice: ${invoice.invoiceNumber}',
);
return invoice;
} catch (e) {
debugPrint('[InvoicesLocalDataSource] Error getting invoice: $e');
@@ -105,10 +112,14 @@ class InvoicesLocalDataSource {
.where((invoice) => invoice.isOverdue)
.toList();
debugPrint('[InvoicesLocalDataSource] Found ${overdue.length} overdue invoices');
debugPrint(
'[InvoicesLocalDataSource] Found ${overdue.length} overdue invoices',
);
return overdue;
} catch (e) {
debugPrint('[InvoicesLocalDataSource] Error getting overdue invoices: $e');
debugPrint(
'[InvoicesLocalDataSource] Error getting overdue invoices: $e',
);
rethrow;
}
}
@@ -118,10 +129,14 @@ class InvoicesLocalDataSource {
try {
final allInvoices = await getAllInvoices();
final unpaid = allInvoices
.where((invoice) => invoice.status.name == 'issued' && !invoice.isPaid)
.where(
(invoice) => invoice.status.name == 'issued' && !invoice.isPaid,
)
.toList();
debugPrint('[InvoicesLocalDataSource] Found ${unpaid.length} unpaid invoices');
debugPrint(
'[InvoicesLocalDataSource] Found ${unpaid.length} unpaid invoices',
);
return unpaid;
} catch (e) {
debugPrint('[InvoicesLocalDataSource] Error getting unpaid invoices: $e');

View File

@@ -6,37 +6,84 @@ part 'invoice_model.g.dart';
@HiveType(typeId: HiveTypeIds.invoiceModel)
class InvoiceModel extends HiveObject {
InvoiceModel({required this.invoiceId, required this.invoiceNumber, required this.userId, this.orderId, required this.invoiceType, required this.issueDate, required this.dueDate, required this.currency, required this.subtotalAmount, required this.taxAmount, required this.discountAmount, required this.shippingAmount, required this.totalAmount, required this.amountPaid, required this.amountRemaining, required this.status, this.paymentTerms, this.notes, this.erpnextInvoice, required this.createdAt, this.updatedAt, this.lastReminderSent});
@HiveField(0) final String invoiceId;
@HiveField(1) final String invoiceNumber;
@HiveField(2) final String userId;
@HiveField(3) final String? orderId;
@HiveField(4) final InvoiceType invoiceType;
@HiveField(5) final DateTime issueDate;
@HiveField(6) final DateTime dueDate;
@HiveField(7) final String currency;
@HiveField(8) final double subtotalAmount;
@HiveField(9) final double taxAmount;
@HiveField(10) final double discountAmount;
@HiveField(11) final double shippingAmount;
@HiveField(12) final double totalAmount;
@HiveField(13) final double amountPaid;
@HiveField(14) final double amountRemaining;
@HiveField(15) final InvoiceStatus status;
@HiveField(16) final String? paymentTerms;
@HiveField(17) final String? notes;
@HiveField(18) final String? erpnextInvoice;
@HiveField(19) final DateTime createdAt;
@HiveField(20) final DateTime? updatedAt;
@HiveField(21) final DateTime? lastReminderSent;
InvoiceModel({
required this.invoiceId,
required this.invoiceNumber,
required this.userId,
this.orderId,
required this.invoiceType,
required this.issueDate,
required this.dueDate,
required this.currency,
required this.subtotalAmount,
required this.taxAmount,
required this.discountAmount,
required this.shippingAmount,
required this.totalAmount,
required this.amountPaid,
required this.amountRemaining,
required this.status,
this.paymentTerms,
this.notes,
this.erpnextInvoice,
required this.createdAt,
this.updatedAt,
this.lastReminderSent,
});
@HiveField(0)
final String invoiceId;
@HiveField(1)
final String invoiceNumber;
@HiveField(2)
final String userId;
@HiveField(3)
final String? orderId;
@HiveField(4)
final InvoiceType invoiceType;
@HiveField(5)
final DateTime issueDate;
@HiveField(6)
final DateTime dueDate;
@HiveField(7)
final String currency;
@HiveField(8)
final double subtotalAmount;
@HiveField(9)
final double taxAmount;
@HiveField(10)
final double discountAmount;
@HiveField(11)
final double shippingAmount;
@HiveField(12)
final double totalAmount;
@HiveField(13)
final double amountPaid;
@HiveField(14)
final double amountRemaining;
@HiveField(15)
final InvoiceStatus status;
@HiveField(16)
final String? paymentTerms;
@HiveField(17)
final String? notes;
@HiveField(18)
final String? erpnextInvoice;
@HiveField(19)
final DateTime createdAt;
@HiveField(20)
final DateTime? updatedAt;
@HiveField(21)
final DateTime? lastReminderSent;
factory InvoiceModel.fromJson(Map<String, dynamic> json) => InvoiceModel(
invoiceId: json['invoice_id'] as String,
invoiceNumber: json['invoice_number'] as String,
userId: json['user_id'] as String,
orderId: json['order_id'] as String?,
invoiceType: InvoiceType.values.firstWhere((e) => e.name == json['invoice_type']),
invoiceType: InvoiceType.values.firstWhere(
(e) => e.name == json['invoice_type'],
),
issueDate: DateTime.parse(json['issue_date']?.toString() ?? ''),
dueDate: DateTime.parse(json['due_date']?.toString() ?? ''),
currency: json['currency'] as String? ?? 'VND',
@@ -52,8 +99,12 @@ class InvoiceModel extends HiveObject {
notes: json['notes'] as String?,
erpnextInvoice: json['erpnext_invoice'] as String?,
createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
updatedAt: json['updated_at'] != null ? DateTime.parse(json['updated_at']?.toString() ?? '') : null,
lastReminderSent: json['last_reminder_sent'] != null ? DateTime.parse(json['last_reminder_sent']?.toString() ?? '') : null,
updatedAt: json['updated_at'] != null
? DateTime.parse(json['updated_at']?.toString() ?? '')
: null,
lastReminderSent: json['last_reminder_sent'] != null
? DateTime.parse(json['last_reminder_sent']?.toString() ?? '')
: null,
);
Map<String, dynamic> toJson() => {
@@ -81,6 +132,7 @@ class InvoiceModel extends HiveObject {
'last_reminder_sent': lastReminderSent?.toIso8601String(),
};
bool get isOverdue => DateTime.now().isAfter(dueDate) && status != InvoiceStatus.paid;
bool get isOverdue =>
DateTime.now().isAfter(dueDate) && status != InvoiceStatus.paid;
bool get isPaid => status == InvoiceStatus.paid;
}

View File

@@ -5,16 +5,33 @@ part 'order_item_model.g.dart';
@HiveType(typeId: HiveTypeIds.orderItemModel)
class OrderItemModel extends HiveObject {
OrderItemModel({required this.orderItemId, required this.orderId, required this.productId, required this.quantity, required this.unitPrice, required this.discountPercent, required this.subtotal, this.notes});
@HiveField(0) final String orderItemId;
@HiveField(1) final String orderId;
@HiveField(2) final String productId;
@HiveField(3) final double quantity;
@HiveField(4) final double unitPrice;
@HiveField(5) final double discountPercent;
@HiveField(6) final double subtotal;
@HiveField(7) final String? notes;
OrderItemModel({
required this.orderItemId,
required this.orderId,
required this.productId,
required this.quantity,
required this.unitPrice,
required this.discountPercent,
required this.subtotal,
this.notes,
});
@HiveField(0)
final String orderItemId;
@HiveField(1)
final String orderId;
@HiveField(2)
final String productId;
@HiveField(3)
final double quantity;
@HiveField(4)
final double unitPrice;
@HiveField(5)
final double discountPercent;
@HiveField(6)
final double subtotal;
@HiveField(7)
final String? notes;
factory OrderItemModel.fromJson(Map<String, dynamic> json) => OrderItemModel(
orderItemId: json['order_item_id'] as String,

View File

@@ -94,15 +94,25 @@ class OrderModel extends HiveObject {
taxAmount: (json['tax_amount'] as num).toDouble(),
shippingFee: (json['shipping_fee'] as num).toDouble(),
finalAmount: (json['final_amount'] as num).toDouble(),
shippingAddress: json['shipping_address'] != null ? jsonEncode(json['shipping_address']) : null,
billingAddress: json['billing_address'] != null ? jsonEncode(json['billing_address']) : null,
expectedDeliveryDate: json['expected_delivery_date'] != null ? DateTime.parse(json['expected_delivery_date']?.toString() ?? '') : null,
actualDeliveryDate: json['actual_delivery_date'] != null ? DateTime.parse(json['actual_delivery_date']?.toString() ?? '') : null,
shippingAddress: json['shipping_address'] != null
? jsonEncode(json['shipping_address'])
: null,
billingAddress: json['billing_address'] != null
? jsonEncode(json['billing_address'])
: null,
expectedDeliveryDate: json['expected_delivery_date'] != null
? DateTime.parse(json['expected_delivery_date']?.toString() ?? '')
: null,
actualDeliveryDate: json['actual_delivery_date'] != null
? DateTime.parse(json['actual_delivery_date']?.toString() ?? '')
: null,
notes: json['notes'] as String?,
cancellationReason: json['cancellation_reason'] as String?,
erpnextSalesOrder: json['erpnext_sales_order'] as String?,
createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
updatedAt: json['updated_at'] != null ? DateTime.parse(json['updated_at']?.toString() ?? '') : null,
updatedAt: json['updated_at'] != null
? DateTime.parse(json['updated_at']?.toString() ?? '')
: null,
);
}
@@ -116,8 +126,12 @@ class OrderModel extends HiveObject {
'tax_amount': taxAmount,
'shipping_fee': shippingFee,
'final_amount': finalAmount,
'shipping_address': shippingAddress != null ? jsonDecode(shippingAddress!) : null,
'billing_address': billingAddress != null ? jsonDecode(billingAddress!) : null,
'shipping_address': shippingAddress != null
? jsonDecode(shippingAddress!)
: null,
'billing_address': billingAddress != null
? jsonDecode(billingAddress!)
: null,
'expected_delivery_date': expectedDeliveryDate?.toIso8601String(),
'actual_delivery_date': actualDeliveryDate?.toIso8601String(),
'notes': notes,

View File

@@ -6,41 +6,79 @@ part 'payment_line_model.g.dart';
@HiveType(typeId: HiveTypeIds.paymentLineModel)
class PaymentLineModel extends HiveObject {
PaymentLineModel({required this.paymentLineId, required this.invoiceId, required this.paymentNumber, required this.paymentDate, required this.amount, required this.paymentMethod, this.bankName, this.bankAccount, this.referenceNumber, this.notes, required this.status, this.receiptUrl, this.erpnextPaymentEntry, required this.createdAt, this.processedAt});
@HiveField(0) final String paymentLineId;
@HiveField(1) final String invoiceId;
@HiveField(2) final String paymentNumber;
@HiveField(3) final DateTime paymentDate;
@HiveField(4) final double amount;
@HiveField(5) final PaymentMethod paymentMethod;
@HiveField(6) final String? bankName;
@HiveField(7) final String? bankAccount;
@HiveField(8) final String? referenceNumber;
@HiveField(9) final String? notes;
@HiveField(10) final PaymentStatus status;
@HiveField(11) final String? receiptUrl;
@HiveField(12) final String? erpnextPaymentEntry;
@HiveField(13) final DateTime createdAt;
@HiveField(14) final DateTime? processedAt;
PaymentLineModel({
required this.paymentLineId,
required this.invoiceId,
required this.paymentNumber,
required this.paymentDate,
required this.amount,
required this.paymentMethod,
this.bankName,
this.bankAccount,
this.referenceNumber,
this.notes,
required this.status,
this.receiptUrl,
this.erpnextPaymentEntry,
required this.createdAt,
this.processedAt,
});
factory PaymentLineModel.fromJson(Map<String, dynamic> json) => PaymentLineModel(
paymentLineId: json['payment_line_id'] as String,
invoiceId: json['invoice_id'] as String,
paymentNumber: json['payment_number'] as String,
paymentDate: DateTime.parse(json['payment_date']?.toString() ?? ''),
amount: (json['amount'] as num).toDouble(),
paymentMethod: PaymentMethod.values.firstWhere((e) => e.name == json['payment_method']),
bankName: json['bank_name'] as String?,
bankAccount: json['bank_account'] as String?,
referenceNumber: json['reference_number'] as String?,
notes: json['notes'] as String?,
status: PaymentStatus.values.firstWhere((e) => e.name == json['status']),
receiptUrl: json['receipt_url'] as String?,
erpnextPaymentEntry: json['erpnext_payment_entry'] as String?,
createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
processedAt: json['processed_at'] != null ? DateTime.parse(json['processed_at']?.toString() ?? '') : null,
);
@HiveField(0)
final String paymentLineId;
@HiveField(1)
final String invoiceId;
@HiveField(2)
final String paymentNumber;
@HiveField(3)
final DateTime paymentDate;
@HiveField(4)
final double amount;
@HiveField(5)
final PaymentMethod paymentMethod;
@HiveField(6)
final String? bankName;
@HiveField(7)
final String? bankAccount;
@HiveField(8)
final String? referenceNumber;
@HiveField(9)
final String? notes;
@HiveField(10)
final PaymentStatus status;
@HiveField(11)
final String? receiptUrl;
@HiveField(12)
final String? erpnextPaymentEntry;
@HiveField(13)
final DateTime createdAt;
@HiveField(14)
final DateTime? processedAt;
factory PaymentLineModel.fromJson(Map<String, dynamic> json) =>
PaymentLineModel(
paymentLineId: json['payment_line_id'] as String,
invoiceId: json['invoice_id'] as String,
paymentNumber: json['payment_number'] as String,
paymentDate: DateTime.parse(json['payment_date']?.toString() ?? ''),
amount: (json['amount'] as num).toDouble(),
paymentMethod: PaymentMethod.values.firstWhere(
(e) => e.name == json['payment_method'],
),
bankName: json['bank_name'] as String?,
bankAccount: json['bank_account'] as String?,
referenceNumber: json['reference_number'] as String?,
notes: json['notes'] as String?,
status: PaymentStatus.values.firstWhere(
(e) => e.name == json['status'],
),
receiptUrl: json['receipt_url'] as String?,
erpnextPaymentEntry: json['erpnext_payment_entry'] as String?,
createdAt: DateTime.parse(json['created_at']?.toString() ?? ''),
processedAt: json['processed_at'] != null
? DateTime.parse(json['processed_at']?.toString() ?? '')
: null,
);
Map<String, dynamic> toJson() => {
'payment_line_id': paymentLineId,