third commit

This commit is contained in:
2026-07-31 17:06:52 +05:30
parent d9886880cc
commit 9891a69a5f
32 changed files with 2083 additions and 557 deletions

View File

@@ -16,6 +16,7 @@ import '../../../core/utils/formatters.dart';
import '../../../core/widgets/glass_card.dart';
import '../../../core/widgets/primary_button.dart';
import '../../../domain/entities/transaction.dart';
import '../../modules/providers/printer_settings.dart';
import '../../pos/providers/cart_controller.dart';
import '../widgets/receipt_preview.dart';
@@ -37,6 +38,30 @@ class _ReceiptScreenState extends ConsumerState<ReceiptScreen> {
@override
void initState() {
super.initState();
// Auto-print is opt-in and only fires when a printer was actually
// selected, so it can never fail invisibly on a terminal with none.
WidgetsBinding.instance.addPostFrameCallback((_) async {
final settings = ref.read(printerSettingsProvider);
if (!settings.autoPrint || !settings.hasPrinter) return;
final ok = await ref.read(receiptServiceProvider).printDirect(
widget.transaction,
printerUrl: settings.printerUrl,
);
if (!ok && mounted) {
_cancelAutoReturn();
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(
backgroundColor: AppColors.danger,
content: Text(
'Printer did not respond — use Print to send it again.',
),
));
}
});
_timer = Timer.periodic(const Duration(seconds: 1), (t) {
if (!mounted) return;
setState(() => _seconds--);
@@ -182,31 +207,85 @@ class _ReceiptScreenState extends ConsumerState<ReceiptScreen> {
),
const SizedBox(height: AppSpacing.xxl),
Row(children: [
Expanded(
child: PrimaryButton(
label: 'Reprint',
icon: Icons.print_outlined,
tone: ButtonTone.neutral,
onPressed: () {
_cancelAutoReturn();
ref.read(receiptServiceProvider).printWithDialog(txn);
},
// Wrap, so three actions never overflow a narrow terminal.
Wrap(
spacing: AppSpacing.md,
runSpacing: AppSpacing.md,
children: [
SizedBox(
width: 150,
child: PrimaryButton(
label: 'Print',
icon: Icons.print_outlined,
tone: ButtonTone.neutral,
onPressed: () async {
_cancelAutoReturn();
final settings = ref.read(printerSettingsProvider);
final service = ref.read(receiptServiceProvider);
// Straight to the roll when one is configured; otherwise
// the system preview, which works with no hardware.
final printed = settings.hasPrinter &&
await service.printDirect(
txn,
printerUrl: settings.printerUrl,
);
if (!printed) await service.printPreview(txn);
},
),
),
SizedBox(
width: 170,
child: PrimaryButton(
label: 'WhatsApp',
icon: Icons.chat_rounded,
tone: txn.customer == null
? ButtonTone.neutral
: ButtonTone.ghost,
onPressed: txn.customer == null
? null
: () async {
_cancelAutoReturn();
final sent = await ref
.read(receiptServiceProvider)
.sendToWhatsApp(txn);
if (!context.mounted || sent) return;
ScaffoldMessenger.of(context)
..hideCurrentSnackBar()
..showSnackBar(const SnackBar(
content: Text(
'Could not open WhatsApp on this device.',
),
));
},
),
),
SizedBox(
width: 170,
child: PrimaryButton(
label: 'Share PDF',
icon: Icons.ios_share_rounded,
tone: ButtonTone.neutral,
onPressed: () {
_cancelAutoReturn();
ref.read(receiptServiceProvider).sharePdf(txn);
},
),
),
],
),
if (txn.customer == null)
const Padding(
padding: EdgeInsets.only(top: AppSpacing.sm),
child: Text(
'No mobile number captured, so WhatsApp is unavailable for '
'this bill.',
style: TextStyle(
fontSize: 12,
color: AppColors.textTertiary,
),
),
),
const SizedBox(width: AppSpacing.md),
Expanded(
child: PrimaryButton(
label: 'Share',
icon: Icons.ios_share_rounded,
tone: ButtonTone.neutral,
onPressed: () {
_cancelAutoReturn();
ref.read(receiptServiceProvider).share(txn);
},
),
),
]),
const SizedBox(height: AppSpacing.md),
PrimaryButton(
label: _seconds > 0

View File

@@ -5,6 +5,7 @@ import '../../../core/theme/app_colors.dart';
import '../../../core/theme/app_dimens.dart';
import '../../../core/theme/app_typography.dart';
import '../../../core/utils/formatters.dart';
import '../../../domain/entities/cart.dart';
import '../../../domain/entities/transaction.dart';
/// Paper-like preview of what the thermal printer produced.
@@ -13,10 +14,33 @@ class ReceiptPreview extends StatelessWidget {
final SaleTransaction transaction;
/// Groups the bill by GST rate, mirroring how the printed invoice lists it.
List<_PreviewSlab> _slabs(SaleTransaction txn) {
final cart = txn.cart;
final factor = cart.subtotal <= 0 ? 1.0 : cart.netAmount / cart.subtotal;
final grouped = <double, List<CartLine>>{};
for (final line in cart.lines) {
grouped.putIfAbsent(line.product.gstRate, () => []).add(line);
}
final rates = grouped.keys.toList()..sort();
return [
for (var i = 0; i < rates.length; i++)
_PreviewSlab(i + 1, rates[i], grouped[rates[i]]!, factor),
];
}
double _gross(SaleTransaction txn) => txn.cart.lines
.fold(0.0, (s, l) => s + (l.product.mrp ?? l.product.price) * l.quantity);
@override
Widget build(BuildContext context) {
final txn = transaction;
final cart = txn.cart;
final slabs = _slabs(txn);
final gross = _gross(txn);
final discount = gross - cart.netAmount;
return Container(
decoration: BoxDecoration(
@@ -29,14 +53,15 @@ class ReceiptPreview extends StatelessWidget {
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(
horizontal: AppSpacing.xxl,
vertical: AppSpacing.xl,
horizontal: AppSpacing.xl,
vertical: AppSpacing.lg,
),
child: DefaultTextStyle(
style: AppTypography.mono(11.5, color: AppColors.textPrimary),
style: AppTypography.mono(10, color: AppColors.textPrimary),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
// ------------------------------------------------ header
Center(
child: Column(children: [
Text(
@@ -44,117 +69,251 @@ class ReceiptPreview extends StatelessWidget {
style: AppTypography.mono(15)
.copyWith(fontWeight: FontWeight.w700),
),
const SizedBox(height: 3),
Text(AppConstants.storeLegalName,
style: AppTypography.mono(8.5)),
const SizedBox(height: 2),
Text(AppConstants.storeAddress,
textAlign: TextAlign.center,
style: AppTypography.mono(9.5)),
Text('GSTIN: ${AppConstants.storeGstin}',
style: AppTypography.mono(9.5)),
const SizedBox(height: AppSpacing.sm),
Text('TAX INVOICE',
style: AppTypography.mono(11.5)
.copyWith(fontWeight: FontWeight.w700)),
style: AppTypography.mono(8.5)),
Text('Customer care : ${AppConstants.storePhone}',
style: AppTypography.mono(8.5)),
Text('CIN No : ${AppConstants.storeCin}',
style: AppTypography.mono(8.5)),
Text('GSTIN : ${AppConstants.storeGstin}',
style: AppTypography.mono(8.5)),
Text('FSSAI Lic No : ${AppConstants.storeFssai}',
style: AppTypography.mono(8.5)),
]),
),
const _Dashes(),
_row('Invoice', txn.invoiceNumber),
_row('Date', Formatters.receiptStamp(txn.createdAt)),
_row('Cashier', txn.cashierName),
_row('Customer', txn.customer?.name ?? 'Walk-in'),
const _Dashes(),
Row(children: [
Expanded(flex: 5, child: _bold('Item')),
Expanded(
flex: 2,
child: _bold('Qty', align: TextAlign.center),
),
Expanded(
flex: 3,
child: _bold('Amount', align: TextAlign.right),
),
]),
const SizedBox(height: AppSpacing.xs),
...cart.lines.map((line) => Padding(
padding: const EdgeInsets.symmetric(vertical: 2.5),
child: Row(children: [
Expanded(flex: 5, child: Text(line.product.name)),
Expanded(
flex: 2,
child: Text(
line.quantity % 1 == 0
? line.quantity.toStringAsFixed(0)
: line.quantity.toStringAsFixed(2),
textAlign: TextAlign.center,
),
),
Expanded(
flex: 3,
child: Text(
line.payable.toStringAsFixed(2),
textAlign: TextAlign.right,
),
),
]),
)),
const _Dashes(),
_row('Subtotal', cart.subtotal.toStringAsFixed(2)),
if (cart.billDiscountTotal > 0)
_row('Discount',
'-${cart.billDiscountTotal.toStringAsFixed(2)}'),
if (cart.loyaltyRedemptionValue > 0)
_row('Points redeemed',
'-${cart.loyaltyRedemptionValue.toStringAsFixed(2)}'),
_row('CGST', cart.cgst.toStringAsFixed(2)),
_row('SGST', cart.sgst.toStringAsFixed(2)),
if (cart.roundOff != 0)
_row('Round off', cart.roundOff.toStringAsFixed(2)),
const _Dashes(),
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text('TOTAL',
style: AppTypography.mono(15).copyWith(
fontWeight: FontWeight.w800,
color: AppColors.textPrimary,
)),
Text(
Formatters.money(txn.total),
style: AppTypography.mono(15).copyWith(
fontWeight: FontWeight.w800,
if (discount > 0) ...[
const SizedBox(height: AppSpacing.sm),
Center(
child: Text(
'You have saved Rs.${discount.toStringAsFixed(2)}',
style: AppTypography.mono(11).copyWith(
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
],
),
),
],
const _Dashes(),
...txn.payments.map((p) =>
_row(p.method.label, p.amount.toStringAsFixed(2))),
if (txn.changeDue > 0)
_row('Change', txn.changeDue.toStringAsFixed(2)),
Center(
child: Column(children: [
Text('TAX INVOICE',
style: AppTypography.mono(11)
.copyWith(fontWeight: FontWeight.w700)),
Text('xxxxxx Original for Recipient xxxxxx',
style: AppTypography.mono(8)),
]),
),
const SizedBox(height: AppSpacing.sm),
if (txn.customer != null) ...[
const _Dashes(),
_row('Points earned', '+${txn.pointsEarned}'),
_row('Membership', txn.customer!.tier.label),
// -------------------------------------------------- meta
_plain('Place of Supply & State Code: '
'${AppConstants.stateCode} ${AppConstants.stateName}'),
_plain('Customer Type: '
'${txn.customer == null ? 'URD' : 'REG'}'),
_row('Date:${Formatters.receiptStamp(txn.createdAt)}',
'Bill No:${txn.invoiceNumber.split('-').last}'),
_row(
'Store:${AppConstants.storeCode} '
'Cashier:${txn.cashierName}',
'Pos:${AppConstants.posNumber}',
),
if (txn.customer != null)
_plain('Customer: ${txn.customer!.name} '
'${txn.customer!.mobile}'),
const _Dashes(),
// ------------------------------------------------- items
Row(children: [
Expanded(flex: 12, child: _bold('HSN')),
Expanded(flex: 34, child: _bold('Item Description')),
Expanded(
flex: 16,
child: _bold('Net Price', align: TextAlign.right)),
Expanded(
flex: 8, child: _bold('Qty', align: TextAlign.right)),
Expanded(
flex: 18,
child: _bold('Value', align: TextAlign.right)),
]),
const SizedBox(height: 3),
for (final slab in slabs) ...[
Padding(
padding: const EdgeInsets.only(top: 5, bottom: 2),
child: Text(
'${slab.index}) CGST @ ${slab.halfPercent} '
'SGST @ ${slab.halfPercent}',
style: AppTypography.mono(9).copyWith(
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
),
),
for (final line in slab.lines)
Padding(
padding: const EdgeInsets.symmetric(vertical: 1.5),
child: Row(children: [
Expanded(
flex: 12,
child: _cell(line.product.hsnCode ?? '-')),
Expanded(
flex: 34,
child:
_cell(line.product.name.toUpperCase())),
Expanded(
flex: 16,
child: _cell(
line.product.price.toStringAsFixed(2),
align: TextAlign.right),
),
Expanded(
flex: 8,
child: _cell(_qty(line.quantity),
align: TextAlign.right),
),
Expanded(
flex: 18,
child: _cell(line.payable.toStringAsFixed(2),
align: TextAlign.right),
),
]),
),
],
const _Dashes(),
// ------------------------------------------------ totals
_row('Items:${cart.lineCount}',
'Qty:${_qty(cart.totalQuantity)} '
'${cart.netAmount.toStringAsFixed(2)}'),
const SizedBox(height: 3),
_amount('Gross Sales Value', gross),
if (discount > 0) _amount('Total Discount', discount),
_amount(
'Net Sales Value (Inclusive of GST)', cart.netAmount),
if (cart.roundOff != 0)
_amount('Round Off', cart.roundOff),
_amount('Total Amount Paid', txn.total, bold: true),
for (final p in txn.payments)
_amount(p.method.label.toUpperCase(), p.amount),
if (txn.changeDue > 0)
_amount('Change Returned', txn.changeDue),
Text('(AMOUNT INCLUSIVE OF APPLICABLE TAXES)',
style: AppTypography.mono(8)),
const SizedBox(height: AppSpacing.md),
// ------------------------------------------- gst breakup
Center(
child: Text('------GST Breakup Details------ Amount (INR)',
style: AppTypography.mono(8.5)),
),
const SizedBox(height: 4),
Row(children: [
Expanded(flex: 10, child: _bold('GST')),
Expanded(
flex: 20,
child: _bold('Taxable', align: TextAlign.right)),
Expanded(
flex: 16,
child: _bold('CGST', align: TextAlign.right)),
Expanded(
flex: 16,
child: _bold('SGST', align: TextAlign.right)),
Expanded(
flex: 14,
child: _bold('CESS', align: TextAlign.right)),
Expanded(
flex: 20,
child: _bold('Total', align: TextAlign.right)),
]),
const SizedBox(height: 2),
for (final s in slabs)
Padding(
padding: const EdgeInsets.symmetric(vertical: 1),
child: Row(children: [
Expanded(flex: 10, child: _cell('${s.index}')),
Expanded(
flex: 20,
child: _cell(s.taxable.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 16,
child: _cell(s.cgst.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 16,
child: _cell(s.sgst.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 14,
child: _cell('0.00', align: TextAlign.right)),
Expanded(
flex: 20,
child: _cell(s.total.toStringAsFixed(2),
align: TextAlign.right)),
]),
),
const _Dashes(),
Row(children: [
Expanded(flex: 10, child: _bold('Total')),
Expanded(
flex: 20,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.taxable)
.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 16,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.cgst)
.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 16,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.sgst)
.toStringAsFixed(2),
align: TextAlign.right)),
Expanded(
flex: 14,
child: _bold('0.00', align: TextAlign.right)),
Expanded(
flex: 20,
child: _bold(
slabs
.fold(0.0, (a, s) => a + s.total)
.toStringAsFixed(2),
align: TextAlign.right)),
]),
const _Dashes(),
_plain('TaxInvoice# ${txn.invoiceNumber}'),
if (txn.customer != null)
_plain('Loyalty Pts: earned ${txn.pointsEarned} '
'Bal: ${txn.customer!.loyaltyPoints - txn.pointsRedeemed + txn.pointsEarned}'),
_plain('Terms & Conditions Apply'),
const SizedBox(height: AppSpacing.lg),
Center(
child: Column(children: [
_FakeBarcode(value: txn.invoiceNumber),
const SizedBox(height: AppSpacing.sm),
Text('Thank you for shopping with us!',
style: AppTypography.mono(11)
Text('* Thank You for Shopping with us *',
style: AppTypography.mono(10)
.copyWith(fontWeight: FontWeight.w700)),
const SizedBox(height: 2),
Text('Powered by Nearle POS',
style: AppTypography.mono(9)),
style: AppTypography.mono(8)),
]),
),
],
@@ -167,22 +326,97 @@ class ReceiptPreview extends StatelessWidget {
);
}
Widget _row(String label, String value) => Padding(
padding: const EdgeInsets.symmetric(vertical: 1.5),
String _qty(double q) =>
q % 1 == 0 ? q.toStringAsFixed(0) : q.toStringAsFixed(3);
Widget _plain(String text) => Padding(
padding: const EdgeInsets.symmetric(vertical: 0.8),
child: Text(text, style: AppTypography.mono(9)),
);
Widget _row(String left, String right) => Padding(
padding: const EdgeInsets.symmetric(vertical: 0.8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [Text(label), Text(value)],
children: [
Flexible(
child: Text(left,
overflow: TextOverflow.ellipsis,
style: AppTypography.mono(9)),
),
const SizedBox(width: AppSpacing.sm),
Text(right, style: AppTypography.mono(9)),
],
),
);
Widget _amount(String label, double value, {bool bold = false}) => Padding(
padding: const EdgeInsets.symmetric(vertical: 1),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Flexible(
child: Text(
label,
overflow: TextOverflow.ellipsis,
style: AppTypography.mono(9.5).copyWith(
fontWeight: bold ? FontWeight.w700 : FontWeight.w400,
color: AppColors.textPrimary,
),
),
),
const SizedBox(width: AppSpacing.sm),
Text(
value.toStringAsFixed(2),
style: AppTypography.mono(9.5).copyWith(
fontWeight: bold ? FontWeight.w700 : FontWeight.w400,
color: AppColors.textPrimary,
),
),
],
),
);
Widget _bold(String text, {TextAlign align = TextAlign.left}) => Text(
text,
textAlign: align,
style: AppTypography.mono(11.5).copyWith(
maxLines: 1,
overflow: TextOverflow.clip,
style: AppTypography.mono(8.5).copyWith(
fontWeight: FontWeight.w700,
color: AppColors.textPrimary,
),
);
Widget _cell(String text, {TextAlign align = TextAlign.left}) => Text(
text,
textAlign: align,
maxLines: 1,
overflow: TextOverflow.ellipsis,
style: AppTypography.mono(8.5),
);
}
/// One GST rate's slice of the bill, for the preview.
class _PreviewSlab {
const _PreviewSlab(this.index, this.rate, this.lines, this.factor);
final int index;
final double rate;
final List<CartLine> lines;
final double factor;
String get halfPercent => '${(rate * 100 / 2).toStringAsFixed(2)}%';
double get total => lines.fold(0.0, (s, l) => s + l.payable * factor);
double get taxAmount => lines.fold(0.0, (s, l) => s + l.taxAmount * factor);
double get taxable => total - taxAmount;
double get cgst => taxAmount / 2;
double get sgst => taxAmount - cgst;
}
class _Dashes extends StatelessWidget {