Individual and Jwellery Account Setup done
This commit is contained in:
@@ -11,8 +11,10 @@ import '../domain/invoice.dart';
|
||||
import '../providers/customers_provider.dart';
|
||||
import '../domain/customer.dart';
|
||||
import '../../inventory/providers/products_provider.dart';
|
||||
import '../../inventory/providers/inventory_items_provider.dart';
|
||||
import '../../projects/providers/project_mode_provider.dart';
|
||||
import '../../inventory/domain/product.dart';
|
||||
import '../../inventory/domain/inventory_item.dart';
|
||||
import 'add_customer_sheet.dart';
|
||||
import '../../transactions/providers/providers.dart';
|
||||
|
||||
@@ -309,9 +311,49 @@ class _InvoiceBuilderScreenState extends ConsumerState<InvoiceBuilderScreen> {
|
||||
);
|
||||
if (barcode != null && barcode.isNotEmpty) {
|
||||
final products = ref.read(productsProvider).value ?? [];
|
||||
final inventoryItems = ref.read(inventoryItemsProvider).value ?? [];
|
||||
final businessFeature = ref.read(businessFeatureProvider).value;
|
||||
final bool useBarcodeField = businessFeature?.barcodeSource == 'BARCODE';
|
||||
|
||||
// 1. Try to match by HUID first
|
||||
final invItem = inventoryItems.where((i) => i.huid?.toLowerCase() == barcode.toLowerCase()).firstOrNull;
|
||||
|
||||
if (invItem != null) {
|
||||
final p = products.where((p) => p.id == invItem.productId).firstOrNull;
|
||||
setState(() {
|
||||
final existingIndex = _items.indexWhere((item) => item.inventoryItemId == invItem.id);
|
||||
if (existingIndex >= 0) {
|
||||
// HUIDs are unique, so this shouldn't normally increment qty, but for safety:
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(const SnackBar(content: Text('Item with this HUID is already in the invoice')));
|
||||
}
|
||||
} else {
|
||||
final price = invItem.purchaseCost ?? p?.sellingPrice ?? 0; // Ideally use selling price logic, but keeping simple
|
||||
final tax = invItem.tax ?? p?.gstRate ?? 0;
|
||||
final making = invItem.makingCharges ?? p?.makingCharges ?? 0;
|
||||
final total = price + (price * (tax / 100)) + making;
|
||||
_items.add(InvoiceItem(
|
||||
productId: p?.id,
|
||||
inventoryItemId: invItem.id,
|
||||
sku: invItem.huid ?? invItem.sku ?? p?.sku,
|
||||
description: p?.name ?? 'Inventory Item',
|
||||
quantity: 1,
|
||||
unitPrice: price,
|
||||
taxRate: tax,
|
||||
makingCharge: making,
|
||||
otherCharges: 0,
|
||||
discount: 0,
|
||||
total: total,
|
||||
));
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Added HUID item to invoice')));
|
||||
}
|
||||
}
|
||||
});
|
||||
return;
|
||||
}
|
||||
|
||||
// 2. Fallback to matching by Product SKU or Barcode
|
||||
final p = products.where((p) {
|
||||
if (useBarcodeField) {
|
||||
return p.barcode?.toLowerCase() == barcode.toLowerCase();
|
||||
@@ -322,7 +364,7 @@ class _InvoiceBuilderScreenState extends ConsumerState<InvoiceBuilderScreen> {
|
||||
|
||||
if (p != null) {
|
||||
setState(() {
|
||||
final existingIndex = _items.indexWhere((item) => item.productId == p.id);
|
||||
final existingIndex = _items.indexWhere((item) => item.productId == p.id && item.inventoryItemId == null);
|
||||
if (existingIndex >= 0) {
|
||||
// Increment qty
|
||||
final item = _items[existingIndex];
|
||||
@@ -354,7 +396,7 @@ class _InvoiceBuilderScreenState extends ConsumerState<InvoiceBuilderScreen> {
|
||||
}
|
||||
} else {
|
||||
if (context.mounted) {
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Product not found for barcode: $barcode')));
|
||||
ScaffoldMessenger.of(context).showSnackBar(SnackBar(content: Text('Product/Item not found for barcode: $barcode')));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user