From 1a27081b6e7be1787c09ac4ce66717f4987f46c9 Mon Sep 17 00:00:00 2001 From: Mohammad Date: Sat, 17 Oct 2026 15:57:22 +0330 Subject: [PATCH] fix(quick-sales): reset customer after completed sale --- .../lib/models/quick_sales_parked_sale.dart | 21 +++ .../lib/pages/business/quick_sales_page.dart | 124 +++++++++++++----- .../test/quick_sales_parked_sale_test.dart | 55 ++++++++ 3 files changed, 166 insertions(+), 34 deletions(-) diff --git a/hesabixUI/hesabix_ui/lib/models/quick_sales_parked_sale.dart b/hesabixUI/hesabix_ui/lib/models/quick_sales_parked_sale.dart index a1f98d82..c0ed3fce 100644 --- a/hesabixUI/hesabix_ui/lib/models/quick_sales_parked_sale.dart +++ b/hesabixUI/hesabix_ui/lib/models/quick_sales_parked_sale.dart @@ -337,6 +337,27 @@ class QuickSalesParkedSalesBundle { required this.sales, }); + /// فروش ثبت‌شده را می‌بندد، فروش‌های پارک‌شدهٔ واقعی را نگه می‌دارد و + /// یک فروش خالی تازه را فعال می‌کند. + QuickSalesParkedSalesBundle afterCompletedActiveSale({ + required QuickSalesParkedSale blankSale, + int? anonymousCustomerId, + }) { + final remainingSales = + sales + .where( + (sale) => + sale.id != activeId && + !sale.isBlank(anonymousCustomerId: anonymousCustomerId), + ) + .toList() + ..add(blankSale); + return QuickSalesParkedSalesBundle( + activeId: blankSale.id, + sales: remainingSales, + ); + } + Map toJson() => { 'version': version, 'active_id': activeId, diff --git a/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart b/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart index b079fb9c..9dddbea6 100755 --- a/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart +++ b/hesabixUI/hesabix_ui/lib/pages/business/quick_sales_page.dart @@ -91,6 +91,32 @@ enum _QuickSalesSaveAction { none, save, saveAndPrint } enum _StockShortageAction { cancel, park, correct, override } +class _QuickSalesShareSnapshot { + final bool onlinePaymentEnabled; + final bool hasGateways; + final int? gatewayId; + final int expiryHours; + final bool sendSms; + final bool sendEmail; + final bool useNativeShare; + final String customerName; + final String? customerMobile; + final String? customerEmail; + + const _QuickSalesShareSnapshot({ + required this.onlinePaymentEnabled, + required this.hasGateways, + required this.gatewayId, + required this.expiryHours, + required this.sendSms, + required this.sendEmail, + required this.useNativeShare, + required this.customerName, + required this.customerMobile, + required this.customerEmail, + }); +} + class QuickSalesPage extends StatefulWidget { final int businessId; final AuthStore authStore; @@ -808,6 +834,31 @@ class _QuickSalesPageState extends State _afterSaleSwitchUi(); } + void _openBlankSaleAfterSuccessfulSave() { + final completedSaleId = _activeSaleId; + final newSaleId = QuickSalesParkedSale.newId(); + setState(() { + _activeSaleId = newSaleId; + _applyBlankSaleState(); + _saveAction = _QuickSalesSaveAction.none; + + final nextBundle = + QuickSalesParkedSalesBundle( + activeId: completedSaleId, + sales: List.from(_sales), + ).afterCompletedActiveSale( + blankSale: _snapshotActiveSale(), + anonymousCustomerId: _anonymousCustomer?.id, + ); + _sales + ..clear() + ..addAll(nextBundle.sales); + _activeSaleId = nextBundle.activeId; + }); + unawaited(_persistParkedSalesNow()); + _afterSaleSwitchUi(); + } + void _selectParkedSale(String id) { if (_isSaving || id == _activeSaleId) return; QuickSalesParkedSale? target; @@ -1346,6 +1397,8 @@ class _QuickSalesPageState extends State required String? invoiceCode, required num totalAmount, required String channel, + required String customerName, + required String? customerMobile, }) async { final api = ApiClient(); final response = await api.post>( @@ -1355,15 +1408,15 @@ class _QuickSalesPageState extends State 'event_type': 'invoice_share_link', 'context': { 'share_link': shareLink, - 'customer_name': _selectedCustomer?.name ?? '', - 'customer_mobile': _selectedCustomerPhone ?? '', + 'customer_name': customerName, + 'customer_mobile': customerMobile ?? '', 'invoice_number': invoiceCode ?? '', 'invoice_code': invoiceCode ?? '', 'final_amount': totalAmount.toDouble(), }, 'channel': channel, - if (channel == 'sms' && _selectedCustomerPhone != null) - 'recipient_mobile': _selectedCustomerPhone, + if (channel == 'sms' && customerMobile != null) + 'recipient_mobile': customerMobile, }, ); return response.data; @@ -1374,6 +1427,7 @@ class _QuickSalesPageState extends State required String? invoiceCode, required int personId, required num totalAmount, + required _QuickSalesShareSnapshot shareSnapshot, }) async { final t = AppLocalizations.of(context); final notes = []; @@ -1383,12 +1437,12 @@ class _QuickSalesPageState extends State return notes; } - if (_shareOnlinePaymentEnabled) { - if (_shareGateways.isEmpty) { + if (shareSnapshot.onlinePaymentEnabled) { + if (!shareSnapshot.hasGateways) { notes.add(t.quickSalesShareNoGateway); return notes; } - if (_shareGatewayId == null) { + if (shareSnapshot.gatewayId == null) { notes.add(t.quickSalesShareGatewayRequired); return notes; } @@ -1398,10 +1452,10 @@ class _QuickSalesPageState extends State final link = await _invoiceService.createInvoiceShareLink( businessId: widget.businessId, invoiceId: invoiceId, - expiresInHours: _shareExpiryHours, - onlinePaymentEnabled: _shareOnlinePaymentEnabled, - onlinePaymentGatewayId: _shareOnlinePaymentEnabled - ? _shareGatewayId + expiresInHours: shareSnapshot.expiryHours, + onlinePaymentEnabled: shareSnapshot.onlinePaymentEnabled, + onlinePaymentGatewayId: shareSnapshot.onlinePaymentEnabled + ? shareSnapshot.gatewayId : null, ); final shortUrl = link['short_url']?.toString(); @@ -1416,8 +1470,8 @@ class _QuickSalesPageState extends State 'send', ); - if (_shareSendSms) { - if (_selectedCustomerPhone == null) { + if (shareSnapshot.sendSms) { + if (shareSnapshot.customerMobile == null) { notes.add(t.quickSalesShareSmsSkippedNoPhone); } else if (!canNotify) { notes.add(t.quickSalesShareNoNotificationPermission); @@ -1429,6 +1483,8 @@ class _QuickSalesPageState extends State invoiceCode: invoiceCode, totalAmount: totalAmount, channel: 'sms', + customerName: shareSnapshot.customerName, + customerMobile: shareSnapshot.customerMobile, ); if (!mounted) return notes; final result = response?['data'] as Map?; @@ -1453,8 +1509,8 @@ class _QuickSalesPageState extends State } } - if (_shareSendEmail) { - if (_selectedCustomerEmail == null) { + if (shareSnapshot.sendEmail) { + if (shareSnapshot.customerEmail == null) { notes.add(t.quickSalesShareEmailSkippedNoEmail); } else if (!canNotify) { notes.add(t.quickSalesShareNoNotificationPermission); @@ -1466,6 +1522,8 @@ class _QuickSalesPageState extends State invoiceCode: invoiceCode, totalAmount: totalAmount, channel: 'email', + customerName: shareSnapshot.customerName, + customerMobile: shareSnapshot.customerMobile, ); if (!mounted) return notes; final result = response?['data'] as Map?; @@ -1490,7 +1548,7 @@ class _QuickSalesPageState extends State } } - if (_shareViaNativeShare) { + if (shareSnapshot.useNativeShare) { await Share.share(shortUrl); } @@ -2722,6 +2780,18 @@ class _QuickSalesPageState extends State final shouldPrint = print || _autoPrint; final shouldShare = _shouldShareAfterSave; + final shareSnapshot = _QuickSalesShareSnapshot( + onlinePaymentEnabled: _shareOnlinePaymentEnabled, + hasGateways: _shareGateways.isNotEmpty, + gatewayId: _shareGatewayId, + expiryHours: _shareExpiryHours, + sendSms: _shareSendSms, + sendEmail: _shareSendEmail, + useNativeShare: _shareViaNativeShare, + customerName: _selectedCustomer?.name ?? '', + customerMobile: _selectedCustomerPhone, + customerEmail: _selectedCustomerEmail, + ); final savedShareAmount = _remainingAmount > 0 ? _remainingAmount : _paymentTargetAmount; @@ -2855,24 +2925,7 @@ class _QuickSalesPageState extends State final invoiceId = (result['id'] as num?)?.toInt(); final invoiceCode = result['code']?.toString(); - setState(() { - _cartItems.clear(); - _payments = []; - _changeResolution = null; - _cashFollowsTotal = false; - _applyCustomerBalance = false; - _shareRemainingEnabled = false; - _checkoutDiscountExpanded = false; - _productStocks.clear(); - _pendingStockProductIds.clear(); - _documentDate = DateTime.now(); - _documentDescriptionController.clear(); - _globalDiscountValueController.clear(); - _saveAction = _QuickSalesSaveAction.none; - }); - - _barcodeFocus.requestFocus(); - unawaited(_refreshCustomerBalance()); + _openBlankSaleAfterSuccessfulSave(); final successMsg = StringBuffer('فاکتور با موفقیت ثبت شد'); if (invoiceCode != null) successMsg.write(' ($invoiceCode)'); @@ -2887,6 +2940,7 @@ class _QuickSalesPageState extends State totalAmount: savedShareAmount, shouldPrint: shouldPrint, shouldShare: shouldShare, + shareSnapshot: shareSnapshot, ), ); } else if (shouldPrint && invoiceId == null) { @@ -2980,6 +3034,7 @@ class _QuickSalesPageState extends State required num totalAmount, required bool shouldPrint, required bool shouldShare, + required _QuickSalesShareSnapshot shareSnapshot, }) async { final followUpNotes = []; @@ -3000,6 +3055,7 @@ class _QuickSalesPageState extends State invoiceCode: invoiceCode, personId: personId, totalAmount: totalAmount, + shareSnapshot: shareSnapshot, ); followUpNotes.addAll(shareNotes); } catch (e) { diff --git a/hesabixUI/hesabix_ui/test/quick_sales_parked_sale_test.dart b/hesabixUI/hesabix_ui/test/quick_sales_parked_sale_test.dart index 68d2826c..30012f8e 100644 --- a/hesabixUI/hesabix_ui/test/quick_sales_parked_sale_test.dart +++ b/hesabixUI/hesabix_ui/test/quick_sales_parked_sale_test.dart @@ -79,6 +79,61 @@ void main() { expect(parsed?.sales, hasLength(1)); }); + test( + 'completed active sale is replaced by a blank default-customer sale', + () { + final now = DateTime.utc(2026, 10, 17); + const defaultCustomer = Customer(id: 1, name: 'مشتری عمومی'); + final completed = QuickSalesParkedSale( + id: 'completed', + createdAt: now, + updatedAt: now, + customer: const Customer(id: 42, name: 'زارع'), + documentDate: now, + documentDescription: 'فاکتور ثبت‌شده', + ); + final parked = QuickSalesParkedSale( + id: 'parked', + createdAt: now, + updatedAt: now, + customer: const Customer(id: 43, name: 'موسوی'), + documentDate: now, + documentDescription: 'فروش پارک‌شده', + ); + final staleBlank = QuickSalesParkedSale( + id: 'old-blank', + createdAt: now, + updatedAt: now, + customer: defaultCustomer, + documentDate: now, + ); + final newBlank = QuickSalesParkedSale( + id: 'new-blank', + createdAt: now, + updatedAt: now, + customer: defaultCustomer, + documentDate: now, + ); + + final result = + QuickSalesParkedSalesBundle( + activeId: completed.id, + sales: [completed, parked, staleBlank], + ).afterCompletedActiveSale( + blankSale: newBlank, + anonymousCustomerId: defaultCustomer.id, + ); + + expect(result.activeId, newBlank.id); + expect(result.sales.map((sale) => sale.id), ['parked', 'new-blank']); + expect(result.sales.last.customer?.id, defaultCustomer.id); + expect( + result.sales.last.isBlank(anonymousCustomerId: defaultCustomer.id), + isTrue, + ); + }, + ); + test('parked sale preserves split change resolution', () { final sale = QuickSalesParkedSale( id: 'change-sale', -- 2.43.0