codex/quick-sales-reset-customer-after-save #19
3 changed files with 166 additions and 34 deletions
|
|
@ -337,6 +337,27 @@ class QuickSalesParkedSalesBundle {
|
||||||
required this.sales,
|
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<String, dynamic> toJson() => {
|
Map<String, dynamic> toJson() => {
|
||||||
'version': version,
|
'version': version,
|
||||||
'active_id': activeId,
|
'active_id': activeId,
|
||||||
|
|
|
||||||
|
|
@ -91,6 +91,32 @@ enum _QuickSalesSaveAction { none, save, saveAndPrint }
|
||||||
|
|
||||||
enum _StockShortageAction { cancel, park, correct, override }
|
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 {
|
class QuickSalesPage extends StatefulWidget {
|
||||||
final int businessId;
|
final int businessId;
|
||||||
final AuthStore authStore;
|
final AuthStore authStore;
|
||||||
|
|
@ -808,6 +834,31 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
_afterSaleSwitchUi();
|
_afterSaleSwitchUi();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void _openBlankSaleAfterSuccessfulSave() {
|
||||||
|
final completedSaleId = _activeSaleId;
|
||||||
|
final newSaleId = QuickSalesParkedSale.newId();
|
||||||
|
setState(() {
|
||||||
|
_activeSaleId = newSaleId;
|
||||||
|
_applyBlankSaleState();
|
||||||
|
_saveAction = _QuickSalesSaveAction.none;
|
||||||
|
|
||||||
|
final nextBundle =
|
||||||
|
QuickSalesParkedSalesBundle(
|
||||||
|
activeId: completedSaleId,
|
||||||
|
sales: List<QuickSalesParkedSale>.from(_sales),
|
||||||
|
).afterCompletedActiveSale(
|
||||||
|
blankSale: _snapshotActiveSale(),
|
||||||
|
anonymousCustomerId: _anonymousCustomer?.id,
|
||||||
|
);
|
||||||
|
_sales
|
||||||
|
..clear()
|
||||||
|
..addAll(nextBundle.sales);
|
||||||
|
_activeSaleId = nextBundle.activeId;
|
||||||
|
});
|
||||||
|
unawaited(_persistParkedSalesNow());
|
||||||
|
_afterSaleSwitchUi();
|
||||||
|
}
|
||||||
|
|
||||||
void _selectParkedSale(String id) {
|
void _selectParkedSale(String id) {
|
||||||
if (_isSaving || id == _activeSaleId) return;
|
if (_isSaving || id == _activeSaleId) return;
|
||||||
QuickSalesParkedSale? target;
|
QuickSalesParkedSale? target;
|
||||||
|
|
@ -1346,6 +1397,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
required String? invoiceCode,
|
required String? invoiceCode,
|
||||||
required num totalAmount,
|
required num totalAmount,
|
||||||
required String channel,
|
required String channel,
|
||||||
|
required String customerName,
|
||||||
|
required String? customerMobile,
|
||||||
}) async {
|
}) async {
|
||||||
final api = ApiClient();
|
final api = ApiClient();
|
||||||
final response = await api.post<Map<String, dynamic>>(
|
final response = await api.post<Map<String, dynamic>>(
|
||||||
|
|
@ -1355,15 +1408,15 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
'event_type': 'invoice_share_link',
|
'event_type': 'invoice_share_link',
|
||||||
'context': {
|
'context': {
|
||||||
'share_link': shareLink,
|
'share_link': shareLink,
|
||||||
'customer_name': _selectedCustomer?.name ?? '',
|
'customer_name': customerName,
|
||||||
'customer_mobile': _selectedCustomerPhone ?? '',
|
'customer_mobile': customerMobile ?? '',
|
||||||
'invoice_number': invoiceCode ?? '',
|
'invoice_number': invoiceCode ?? '',
|
||||||
'invoice_code': invoiceCode ?? '',
|
'invoice_code': invoiceCode ?? '',
|
||||||
'final_amount': totalAmount.toDouble(),
|
'final_amount': totalAmount.toDouble(),
|
||||||
},
|
},
|
||||||
'channel': channel,
|
'channel': channel,
|
||||||
if (channel == 'sms' && _selectedCustomerPhone != null)
|
if (channel == 'sms' && customerMobile != null)
|
||||||
'recipient_mobile': _selectedCustomerPhone,
|
'recipient_mobile': customerMobile,
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
return response.data;
|
return response.data;
|
||||||
|
|
@ -1374,6 +1427,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
required String? invoiceCode,
|
required String? invoiceCode,
|
||||||
required int personId,
|
required int personId,
|
||||||
required num totalAmount,
|
required num totalAmount,
|
||||||
|
required _QuickSalesShareSnapshot shareSnapshot,
|
||||||
}) async {
|
}) async {
|
||||||
final t = AppLocalizations.of(context);
|
final t = AppLocalizations.of(context);
|
||||||
final notes = <String>[];
|
final notes = <String>[];
|
||||||
|
|
@ -1383,12 +1437,12 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
return notes;
|
return notes;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_shareOnlinePaymentEnabled) {
|
if (shareSnapshot.onlinePaymentEnabled) {
|
||||||
if (_shareGateways.isEmpty) {
|
if (!shareSnapshot.hasGateways) {
|
||||||
notes.add(t.quickSalesShareNoGateway);
|
notes.add(t.quickSalesShareNoGateway);
|
||||||
return notes;
|
return notes;
|
||||||
}
|
}
|
||||||
if (_shareGatewayId == null) {
|
if (shareSnapshot.gatewayId == null) {
|
||||||
notes.add(t.quickSalesShareGatewayRequired);
|
notes.add(t.quickSalesShareGatewayRequired);
|
||||||
return notes;
|
return notes;
|
||||||
}
|
}
|
||||||
|
|
@ -1398,10 +1452,10 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
final link = await _invoiceService.createInvoiceShareLink(
|
final link = await _invoiceService.createInvoiceShareLink(
|
||||||
businessId: widget.businessId,
|
businessId: widget.businessId,
|
||||||
invoiceId: invoiceId,
|
invoiceId: invoiceId,
|
||||||
expiresInHours: _shareExpiryHours,
|
expiresInHours: shareSnapshot.expiryHours,
|
||||||
onlinePaymentEnabled: _shareOnlinePaymentEnabled,
|
onlinePaymentEnabled: shareSnapshot.onlinePaymentEnabled,
|
||||||
onlinePaymentGatewayId: _shareOnlinePaymentEnabled
|
onlinePaymentGatewayId: shareSnapshot.onlinePaymentEnabled
|
||||||
? _shareGatewayId
|
? shareSnapshot.gatewayId
|
||||||
: null,
|
: null,
|
||||||
);
|
);
|
||||||
final shortUrl = link['short_url']?.toString();
|
final shortUrl = link['short_url']?.toString();
|
||||||
|
|
@ -1416,8 +1470,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
'send',
|
'send',
|
||||||
);
|
);
|
||||||
|
|
||||||
if (_shareSendSms) {
|
if (shareSnapshot.sendSms) {
|
||||||
if (_selectedCustomerPhone == null) {
|
if (shareSnapshot.customerMobile == null) {
|
||||||
notes.add(t.quickSalesShareSmsSkippedNoPhone);
|
notes.add(t.quickSalesShareSmsSkippedNoPhone);
|
||||||
} else if (!canNotify) {
|
} else if (!canNotify) {
|
||||||
notes.add(t.quickSalesShareNoNotificationPermission);
|
notes.add(t.quickSalesShareNoNotificationPermission);
|
||||||
|
|
@ -1429,6 +1483,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
invoiceCode: invoiceCode,
|
invoiceCode: invoiceCode,
|
||||||
totalAmount: totalAmount,
|
totalAmount: totalAmount,
|
||||||
channel: 'sms',
|
channel: 'sms',
|
||||||
|
customerName: shareSnapshot.customerName,
|
||||||
|
customerMobile: shareSnapshot.customerMobile,
|
||||||
);
|
);
|
||||||
if (!mounted) return notes;
|
if (!mounted) return notes;
|
||||||
final result = response?['data'] as Map<String, dynamic>?;
|
final result = response?['data'] as Map<String, dynamic>?;
|
||||||
|
|
@ -1453,8 +1509,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_shareSendEmail) {
|
if (shareSnapshot.sendEmail) {
|
||||||
if (_selectedCustomerEmail == null) {
|
if (shareSnapshot.customerEmail == null) {
|
||||||
notes.add(t.quickSalesShareEmailSkippedNoEmail);
|
notes.add(t.quickSalesShareEmailSkippedNoEmail);
|
||||||
} else if (!canNotify) {
|
} else if (!canNotify) {
|
||||||
notes.add(t.quickSalesShareNoNotificationPermission);
|
notes.add(t.quickSalesShareNoNotificationPermission);
|
||||||
|
|
@ -1466,6 +1522,8 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
invoiceCode: invoiceCode,
|
invoiceCode: invoiceCode,
|
||||||
totalAmount: totalAmount,
|
totalAmount: totalAmount,
|
||||||
channel: 'email',
|
channel: 'email',
|
||||||
|
customerName: shareSnapshot.customerName,
|
||||||
|
customerMobile: shareSnapshot.customerMobile,
|
||||||
);
|
);
|
||||||
if (!mounted) return notes;
|
if (!mounted) return notes;
|
||||||
final result = response?['data'] as Map<String, dynamic>?;
|
final result = response?['data'] as Map<String, dynamic>?;
|
||||||
|
|
@ -1490,7 +1548,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_shareViaNativeShare) {
|
if (shareSnapshot.useNativeShare) {
|
||||||
await Share.share(shortUrl);
|
await Share.share(shortUrl);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -2722,6 +2780,18 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
|
|
||||||
final shouldPrint = print || _autoPrint;
|
final shouldPrint = print || _autoPrint;
|
||||||
final shouldShare = _shouldShareAfterSave;
|
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
|
final savedShareAmount = _remainingAmount > 0
|
||||||
? _remainingAmount
|
? _remainingAmount
|
||||||
: _paymentTargetAmount;
|
: _paymentTargetAmount;
|
||||||
|
|
@ -2855,24 +2925,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
final invoiceId = (result['id'] as num?)?.toInt();
|
final invoiceId = (result['id'] as num?)?.toInt();
|
||||||
final invoiceCode = result['code']?.toString();
|
final invoiceCode = result['code']?.toString();
|
||||||
|
|
||||||
setState(() {
|
_openBlankSaleAfterSuccessfulSave();
|
||||||
_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());
|
|
||||||
|
|
||||||
final successMsg = StringBuffer('فاکتور با موفقیت ثبت شد');
|
final successMsg = StringBuffer('فاکتور با موفقیت ثبت شد');
|
||||||
if (invoiceCode != null) successMsg.write(' ($invoiceCode)');
|
if (invoiceCode != null) successMsg.write(' ($invoiceCode)');
|
||||||
|
|
@ -2887,6 +2940,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
totalAmount: savedShareAmount,
|
totalAmount: savedShareAmount,
|
||||||
shouldPrint: shouldPrint,
|
shouldPrint: shouldPrint,
|
||||||
shouldShare: shouldShare,
|
shouldShare: shouldShare,
|
||||||
|
shareSnapshot: shareSnapshot,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
} else if (shouldPrint && invoiceId == null) {
|
} else if (shouldPrint && invoiceId == null) {
|
||||||
|
|
@ -2980,6 +3034,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
required num totalAmount,
|
required num totalAmount,
|
||||||
required bool shouldPrint,
|
required bool shouldPrint,
|
||||||
required bool shouldShare,
|
required bool shouldShare,
|
||||||
|
required _QuickSalesShareSnapshot shareSnapshot,
|
||||||
}) async {
|
}) async {
|
||||||
final followUpNotes = <String>[];
|
final followUpNotes = <String>[];
|
||||||
|
|
||||||
|
|
@ -3000,6 +3055,7 @@ class _QuickSalesPageState extends State<QuickSalesPage>
|
||||||
invoiceCode: invoiceCode,
|
invoiceCode: invoiceCode,
|
||||||
personId: personId,
|
personId: personId,
|
||||||
totalAmount: totalAmount,
|
totalAmount: totalAmount,
|
||||||
|
shareSnapshot: shareSnapshot,
|
||||||
);
|
);
|
||||||
followUpNotes.addAll(shareNotes);
|
followUpNotes.addAll(shareNotes);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|
|
||||||
|
|
@ -79,6 +79,61 @@ void main() {
|
||||||
expect(parsed?.sales, hasLength(1));
|
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', () {
|
test('parked sale preserves split change resolution', () {
|
||||||
final sale = QuickSalesParkedSale(
|
final sale = QuickSalesParkedSale(
|
||||||
id: 'change-sale',
|
id: 'change-sale',
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue