Watch
1
0
Fork
You've already forked arc
0
forked from hesabix/arc
arc/hesabixUI/hesabix_ui/lib/pages/business/print_settings_page.dart

884 lines
32 KiB
Dart
Executable file
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:flutter/material.dart';
import 'package:hesabix_ui/l10n/app_localizations.dart';
import 'package:hesabix_ui/services/business_api_service.dart';
import '../../utils/error_extractor.dart';
import '../../utils/snackbar_helper.dart';
import '../../widgets/business_subpage_back_leading.dart';
class BusinessPrintSettingsPage extends StatefulWidget {
final int businessId;
const BusinessPrintSettingsPage({super.key, required this.businessId});
@override
State<BusinessPrintSettingsPage> createState() => _BusinessPrintSettingsPageState();
}
class _BusinessPrintSettingsPageState extends State<BusinessPrintSettingsPage> {
bool _loading = true;
bool _saving = false;
String? _error;
final Map<String, _PrintConfig> _configs = {};
String _selectedDocType = 'all';
final List<_DocTypeDef> _docTypes = const [
_DocTypeDef('all', 'تنظیمات عمومی (همه فاکتورها)'),
_DocTypeDef('invoice_sales', 'فاکتور فروش'),
_DocTypeDef('invoice_sales_return', 'برگشت از فروش'),
_DocTypeDef('invoice_purchase', 'فاکتور خرید'),
_DocTypeDef('invoice_purchase_return', 'برگشت از خرید'),
_DocTypeDef('invoice_direct_consumption', 'مصرف مستقیم'),
_DocTypeDef('invoice_production', 'تولید'),
_DocTypeDef('invoice_waste', 'ضایعات'),
];
final _footerController = TextEditingController();
@override
void initState() {
super.initState();
_load();
}
@override
void dispose() {
_footerController.dispose();
super.dispose();
}
Future<void> _load() async {
setState(() {
_loading = true;
_error = null;
});
try {
final data = await BusinessApiService.getPrintSettings(widget.businessId);
_configs.clear();
final defaultJson = (data['default'] as Map?)?.cast<String, dynamic>() ?? const <String, dynamic>{};
_configs['all'] = _PrintConfig.fromJson(defaultJson);
final perTypeJson = (data['per_type'] as Map?)?.cast<String, dynamic>() ?? const <String, dynamic>{};
perTypeJson.forEach((key, value) {
if (value is Map) {
_configs[key] = _PrintConfig.fromJson(value.cast<String, dynamic>());
}
});
_selectedDocType = 'all';
_syncFormWithConfig();
} catch (e) {
if (!mounted) return;
_error = ErrorExtractor.forContext(e, context);
} finally {
if (!mounted) return;
setState(() {
_loading = false;
});
}
}
_PrintConfig _currentConfig() {
return _configs[_selectedDocType] ?? _configs['all'] ?? _PrintConfig.initial();
}
bool _hasCustomConfigForCurrentType() {
if (_selectedDocType == 'all') return true;
return _configs.containsKey(_selectedDocType);
}
void _syncFormWithConfig() {
final cfg = _currentConfig();
_footerController.text = cfg.footerNote ?? '';
}
void _updateCurrentConfig(_PrintConfig cfg) {
if (_selectedDocType == 'all') {
_configs['all'] = cfg;
} else {
// اگر برای این نوع تنظیم اختصاصی نداریم، آن را ایجاد می‌کنیم
_configs[_selectedDocType] = cfg;
}
}
Future<void> _save() async {
setState(() {
_saving = true;
_error = null;
});
try {
// آخرین تغییرات فیلد متن را روی مدل اعمال کنیم
final current = _currentConfig().copyWith(
footerNote: _footerController.text.trim().isEmpty ? null : _footerController.text.trim(),
);
_updateCurrentConfig(current);
final defaultCfg = _configs['all'] ?? _PrintConfig.initial();
final Map<String, dynamic> payload = {
'default': defaultCfg.toJson(),
'per_type': <String, dynamic>{},
};
final perType = payload['per_type'] as Map<String, dynamic>;
_configs.forEach((key, value) {
if (key == 'all') return;
perType[key] = value.toJson();
});
final saved = await BusinessApiService.updatePrintSettings(widget.businessId, payload);
// بعد از ذخیره، از پاسخ سرور دوباره هم‌خوان‌سازی می‌کنیم
final defaultJson = (saved['default'] as Map?)?.cast<String, dynamic>() ?? const <String, dynamic>{};
final perTypeJson = (saved['per_type'] as Map?)?.cast<String, dynamic>() ?? const <String, dynamic>{};
_configs.clear();
_configs['all'] = _PrintConfig.fromJson(defaultJson);
perTypeJson.forEach((key, value) {
if (value is Map) {
_configs[key] = _PrintConfig.fromJson(value.cast<String, dynamic>());
}
});
_syncFormWithConfig();
if (!mounted) return;
final t = AppLocalizations.of(context);
SnackBarHelper.show(context, message: t.savedSuccessfully);
} catch (e) {
if (!mounted) return;
setState(() {
_error = ErrorExtractor.forContext(e, context);
});
} finally {
if (!mounted) return;
setState(() {
_saving = false;
});
}
}
@override
Widget build(BuildContext context) {
final t = AppLocalizations.of(context);
final cs = Theme.of(context).colorScheme;
return Scaffold(
appBar: AppBar(
title: Text(t.printSettings),
leading: businessSubpageBackLeading(context, widget.businessId),
backgroundColor: cs.surface,
foregroundColor: cs.onSurface,
),
body: _loading
? const Center(child: CircularProgressIndicator())
: _error != null
? Center(child: Text(_error!))
: Padding(
padding: const EdgeInsets.all(16),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.printDocuments,
style: TextStyle(
fontWeight: FontWeight.w600,
color: cs.onSurface,
),
),
const SizedBox(height: 8),
DropdownButtonFormField<String>(
value: _selectedDocType,
decoration: const InputDecoration(
border: OutlineInputBorder(),
),
items: _docTypes
.map(
(d) => DropdownMenuItem<String>(
value: d.key,
child: Text(d.label),
),
)
.toList(),
onChanged: (val) {
if (val == null) return;
setState(() {
// قبل از سوییچ، متن فوتر را روی کانفیگ قبلی ذخیره کنیم
final prev = _currentConfig().copyWith(
footerNote: _footerController.text.trim().isEmpty
? null
: _footerController.text.trim(),
);
_updateCurrentConfig(prev);
_selectedDocType = val;
_syncFormWithConfig();
});
},
),
const SizedBox(height: 16),
if (_selectedDocType != 'all') ...[
SwitchListTile(
title: Text(t.printSettings),
subtitle: const Text(
'در صورت خاموش بودن، از تنظیمات عمومی (همه فاکتورها) استفاده می‌شود',
),
value: _hasCustomConfigForCurrentType(),
onChanged: (v) {
setState(() {
if (v) {
// ساخت تنظیم اختصاصی بر اساس عمومی
_configs[_selectedDocType] = _configs[_selectedDocType] ?? _configs['all'] ?? _PrintConfig.initial();
} else {
// حذف تنظیم اختصاصی → استفاده از عمومی
_configs.remove(_selectedDocType);
}
_syncFormWithConfig();
});
},
),
const Divider(),
],
Expanded(
child: SingleChildScrollView(
child: AbsorbPointer(
absorbing: _selectedDocType != 'all' && !_hasCustomConfigForCurrentType(),
child: _buildConfigForm(context),
),
),
),
const SizedBox(height: 12),
Row(
children: [
ElevatedButton.icon(
onPressed: _saving ? null : _save,
icon: _saving
? const SizedBox(
width: 16,
height: 16,
child: CircularProgressIndicator(strokeWidth: 2),
)
: const Icon(Icons.save),
label: Text(t.save),
),
const SizedBox(width: 8),
TextButton.icon(
onPressed: _loading ? null : _load,
icon: const Icon(Icons.refresh),
label: Text(t.reload),
),
],
),
],
),
),
);
}
Widget _buildConfigForm(BuildContext context) {
final cfg = _currentConfig();
final t = AppLocalizations.of(context);
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
SwitchListTile(
title: Text(t.printDocuments),
subtitle: const Text('نمایش لوگوی کسب‌وکار در بالای فاکتور'),
value: cfg.showLogo,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showLogo: v));
});
},
),
SwitchListTile(
title: const Text('نمایش مهر / امضای شرکت'),
subtitle: const Text('درج تصویر مهر و امضای ثبت‌شده داخل بلوک فروشنده (در صورت خاموشی، تصویر بارگذاری نمی‌شود)'),
value: cfg.showStamp,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showStamp: v));
});
},
),
if (cfg.showStamp) ...[
const SizedBox(height: 4),
Padding(
padding: const EdgeInsetsDirectional.only(start: 16, end: 8),
child: Text(
'اندازه مهر در PDF',
style: Theme.of(context).textTheme.titleSmall,
),
),
const SizedBox(height: 4),
Padding(
padding: const EdgeInsetsDirectional.only(start: 8, end: 8),
child: _ScalePresetRow(
value: cfg.stampScalePercent,
onChanged: (v) {
setState(() {
final current = _currentConfig();
_updateCurrentConfig(current.copyWith(stampScalePercent: v));
});
},
),
),
if (_isCustomScale(cfg.stampScalePercent))
Padding(
padding: const EdgeInsetsDirectional.only(start: 8, end: 8),
child: _ScaleSlider(
label: 'مقیاس مهر',
value: cfg.stampScalePercent,
onChanged: (v) {
setState(() {
final current = _currentConfig();
_updateCurrentConfig(current.copyWith(stampScalePercent: v));
});
},
),
),
const SizedBox(height: 8),
Padding(
padding: const EdgeInsetsDirectional.only(start: 16, end: 8),
child: Text(
'اندازه امضا در PDF',
style: Theme.of(context).textTheme.titleSmall,
),
),
const SizedBox(height: 4),
Padding(
padding: const EdgeInsetsDirectional.only(start: 8, end: 8),
child: _ScalePresetRow(
value: cfg.signatureScalePercent,
onChanged: (v) {
setState(() {
final current = _currentConfig();
_updateCurrentConfig(current.copyWith(signatureScalePercent: v));
});
},
),
),
if (_isCustomScale(cfg.signatureScalePercent))
Padding(
padding: const EdgeInsetsDirectional.only(start: 8, end: 8),
child: _ScaleSlider(
label: 'مقیاس امضا',
value: cfg.signatureScalePercent,
onChanged: (v) {
setState(() {
final current = _currentConfig();
_updateCurrentConfig(current.copyWith(signatureScalePercent: v));
});
},
),
),
const SizedBox(height: 8),
],
SwitchListTile(
title: const Text('نمایش بلوک امضا / مهر فروشنده'),
subtitle: const Text('کل بخش امضای فروشنده در انتهای صفحهٔ PDF؛ می‌توانید بدون خاموش کردن بقیهٔ فاکتور مخفی کنید'),
value: cfg.showSellerSignatureArea,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showSellerSignatureArea: v));
});
},
),
SwitchListTile(
title: const Text('نمایش بلوک امضای خریدار'),
subtitle: const Text('قاب خالی امضای خریدار در پایین چاپ فاکتور'),
value: cfg.showBuyerSignatureArea,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showBuyerSignatureArea: v));
});
},
),
SwitchListTile(
title: const Text('نمایش تراز و وضعیت حساب طرف سند'),
subtitle: const Text(
'برای پیش‌فاکتور: تراز فعلی و تراز احتمالی؛ برای فاکتور قطعی: تراز حساب شخص (در یک کادر جدا از پرداخت‌ها)',
),
value: cfg.showCustomerBalance,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showCustomerBalance: v));
});
},
),
SwitchListTile(
title: const Text('نمایش تراکنش‌های پرداخت مرتبط'),
subtitle: const Text('جدول رسیدها / پرداخت‌های متصل به فاکتور در پایین صفحه'),
value: cfg.showPayments,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showPayments: v));
});
},
),
SwitchListTile(
title: const Text('نمایش برنامه اقساط'),
subtitle: const Text('در صورت فروش اقساطی، برنامه اقساط در صفحه جداگانه چاپ شود'),
value: cfg.showInstallmentPlan,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showInstallmentPlan: v));
});
},
),
SwitchListTile(
title: const Text('QR نمایش آنلاین / اعتبارسنجی'),
subtitle: const Text('امکان درج QR در فاکتور چاپی برای مشاهدهٔ آنلاین (در صدور/چاپ موردی قابل خاموش)'),
value: cfg.showShareQr,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showShareQr: v));
});
},
),
SwitchListTile(
title: const Text('نمایش زمان چاپ در پاورقی صفحهٔ PDF'),
subtitle: const Text('متن «زمان چاپ» در نوار پایین هر صفحهٔ خروجی PDF فاکتور'),
value: cfg.showFooterPrintTime,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showFooterPrintTime: v));
});
},
),
SwitchListTile(
title: const Text('نمایش تهیه‌کننده در پاورقی صفحهٔ PDF'),
subtitle: const Text('نام کاربری که فاکتور را ثبت کرده در کنار زمان چاپ (نوار پایین صفحه)'),
value: cfg.showFooterPreparer,
onChanged: (v) {
setState(() {
_updateCurrentConfig(cfg.copyWith(showFooterPreparer: v));
});
},
),
const SizedBox(height: 16),
Text(
'نمایش مبالغ مالیات و تخفیف',
style: Theme.of(context).textTheme.titleMedium,
),
const SizedBox(height: 4),
Text(
'حالت «هوشمند»: اگر مقدار مربوطه در فاکتور صفر باشد، در خروجی PDF نمایش داده نمی‌شود.',
style: Theme.of(context).textTheme.bodySmall,
),
const SizedBox(height: 8),
_TaxDiscountDisplayModeTile(
title: 'ستون تخفیف (جدول اقلام)',
value: cfg.lineDiscountDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(lineDiscountDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ستون مالیات (جدول اقلام)',
value: cfg.lineTaxDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(lineTaxDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ستون جمع بدون تخفیف (جدول اقلام)',
value: cfg.lineAmountBeforeDiscountDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(lineAmountBeforeDiscountDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ستون جمع بدون مالیات (جدول اقلام)',
value: cfg.lineAmountBeforeTaxDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(lineAmountBeforeTaxDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ردیف تخفیف (خلاصه مالی)',
value: cfg.summaryDiscountDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(summaryDiscountDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ردیف مبلغ بدون مالیات (خلاصه مالی)',
value: cfg.summaryAmountWithoutTaxDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(summaryAmountWithoutTaxDisplay: v));
}),
),
_TaxDiscountDisplayModeTile(
title: 'ردیف مالیات (خلاصه مالی)',
value: cfg.summaryTaxDisplay,
onChanged: (v) => setState(() {
_updateCurrentConfig(cfg.copyWith(summaryTaxDisplay: v));
}),
),
const SizedBox(height: 12),
TextFormField(
controller: _footerController,
maxLines: 4,
decoration: const InputDecoration(
labelText: 'متن ثابت انتهای فاکتور (پاورقی)',
hintText: 'مثال: این فاکتور بر اساس قوانین و مقررات مالیاتی جاری صادر شده است.',
border: OutlineInputBorder(),
),
),
],
);
}
}
class _PrintConfig {
static const String displaySmart = 'smart';
static const String displayAlways = 'always';
static const String displayNever = 'never';
final bool showLogo;
final bool showStamp;
final bool showSellerSignatureArea;
final bool showBuyerSignatureArea;
final bool showCustomerBalance;
final bool showPayments;
final bool showInstallmentPlan;
final bool showShareQr;
final bool showFooterPrintTime;
final bool showFooterPreparer;
final String? footerNote;
/// مقیاس مهر نسبت به اندازه پایه قالب (۵۰ تا ۲۰۰؛ ۱۰۰ = پیش‌فرض)
final int stampScalePercent;
/// مقیاس امضا نسبت به اندازه پایه قالب (۵۰ تا ۲۰۰؛ ۱۰۰ = پیش‌فرض)
final int signatureScalePercent;
final String lineDiscountDisplay;
final String lineTaxDisplay;
final String lineAmountBeforeDiscountDisplay;
final String lineAmountBeforeTaxDisplay;
final String summaryDiscountDisplay;
final String summaryTaxDisplay;
final String summaryAmountWithoutTaxDisplay;
const _PrintConfig({
required this.showLogo,
required this.showStamp,
required this.showSellerSignatureArea,
required this.showBuyerSignatureArea,
required this.showCustomerBalance,
required this.showPayments,
required this.showInstallmentPlan,
required this.showShareQr,
required this.showFooterPrintTime,
required this.showFooterPreparer,
required this.footerNote,
required this.stampScalePercent,
required this.signatureScalePercent,
required this.lineDiscountDisplay,
required this.lineTaxDisplay,
required this.lineAmountBeforeDiscountDisplay,
required this.lineAmountBeforeTaxDisplay,
required this.summaryDiscountDisplay,
required this.summaryTaxDisplay,
required this.summaryAmountWithoutTaxDisplay,
});
factory _PrintConfig.initial() {
return const _PrintConfig(
showLogo: true,
showStamp: true,
showSellerSignatureArea: true,
showBuyerSignatureArea: true,
showCustomerBalance: true,
showPayments: true,
showInstallmentPlan: true,
showShareQr: false,
showFooterPrintTime: true,
showFooterPreparer: true,
footerNote: null,
stampScalePercent: 100,
signatureScalePercent: 100,
lineDiscountDisplay: displaySmart,
lineTaxDisplay: displaySmart,
lineAmountBeforeDiscountDisplay: displaySmart,
lineAmountBeforeTaxDisplay: displaySmart,
summaryDiscountDisplay: displaySmart,
summaryTaxDisplay: displaySmart,
summaryAmountWithoutTaxDisplay: displaySmart,
);
}
static String _displayMode(String? raw, {String fallback = displaySmart}) {
final mode = (raw ?? '').trim().toLowerCase();
if (mode == displayAlways || mode == displayNever || mode == displaySmart) {
return mode;
}
return fallback;
}
factory _PrintConfig.fromJson(Map<String, dynamic> json) {
bool _b(String key, bool def) {
final v = json[key];
if (v is bool) return v;
if (v is num) return v != 0;
if (v is String) {
final s = v.trim().toLowerCase();
if (s == 'true' || s == '1' || s == 'yes' || s == 'on') return true;
if (s == 'false' || s == '0' || s == 'no' || s == 'off') return false;
}
return def;
}
return _PrintConfig(
showLogo: _b('show_logo', true),
showStamp: _b('show_stamp', true),
showSellerSignatureArea: _b('show_seller_signature_area', true),
showBuyerSignatureArea: _b('show_buyer_signature_area', true),
showCustomerBalance: _b('show_customer_balance', true),
showPayments: _b('show_payments', true),
showInstallmentPlan: _b('show_installment_plan', true),
showShareQr: _b('show_share_qr', false),
showFooterPrintTime: _b('show_footer_print_time', true),
showFooterPreparer: _b('show_footer_preparer', true),
footerNote: (json['footer_note'] as String?)?.trim().isEmpty == true
? null
: json['footer_note'] as String?,
stampScalePercent: _clampScale(json['stamp_scale_percent'], 100),
signatureScalePercent: _clampScale(json['signature_scale_percent'], 100),
lineDiscountDisplay: _displayMode(json['line_discount_display'] as String?),
lineTaxDisplay: _displayMode(json['line_tax_display'] as String?),
lineAmountBeforeDiscountDisplay:
_displayMode(json['line_amount_before_discount_display'] as String?),
lineAmountBeforeTaxDisplay:
_displayMode(json['line_amount_before_tax_display'] as String?),
summaryDiscountDisplay: _displayMode(json['summary_discount_display'] as String?),
summaryTaxDisplay: _displayMode(json['summary_tax_display'] as String?),
summaryAmountWithoutTaxDisplay:
_displayMode(json['summary_amount_without_tax_display'] as String?),
);
}
Map<String, dynamic> toJson() {
return {
'show_logo': showLogo,
'show_stamp': showStamp,
'show_seller_signature_area': showSellerSignatureArea,
'show_buyer_signature_area': showBuyerSignatureArea,
'show_customer_balance': showCustomerBalance,
'show_payments': showPayments,
'show_installment_plan': showInstallmentPlan,
'show_share_qr': showShareQr,
'show_footer_print_time': showFooterPrintTime,
'show_footer_preparer': showFooterPreparer,
'footer_note': footerNote,
'stamp_scale_percent': stampScalePercent,
'signature_scale_percent': signatureScalePercent,
'line_discount_display': lineDiscountDisplay,
'line_tax_display': lineTaxDisplay,
'line_amount_before_discount_display': lineAmountBeforeDiscountDisplay,
'line_amount_before_tax_display': lineAmountBeforeTaxDisplay,
'summary_discount_display': summaryDiscountDisplay,
'summary_tax_display': summaryTaxDisplay,
'summary_amount_without_tax_display': summaryAmountWithoutTaxDisplay,
};
}
_PrintConfig copyWith({
bool? showLogo,
bool? showStamp,
bool? showSellerSignatureArea,
bool? showBuyerSignatureArea,
bool? showCustomerBalance,
bool? showPayments,
bool? showInstallmentPlan,
bool? showShareQr,
bool? showFooterPrintTime,
bool? showFooterPreparer,
String? footerNote,
int? stampScalePercent,
int? signatureScalePercent,
String? lineDiscountDisplay,
String? lineTaxDisplay,
String? lineAmountBeforeDiscountDisplay,
String? lineAmountBeforeTaxDisplay,
String? summaryDiscountDisplay,
String? summaryTaxDisplay,
String? summaryAmountWithoutTaxDisplay,
}) {
return _PrintConfig(
showLogo: showLogo ?? this.showLogo,
showStamp: showStamp ?? this.showStamp,
showSellerSignatureArea: showSellerSignatureArea ?? this.showSellerSignatureArea,
showBuyerSignatureArea: showBuyerSignatureArea ?? this.showBuyerSignatureArea,
showCustomerBalance: showCustomerBalance ?? this.showCustomerBalance,
showPayments: showPayments ?? this.showPayments,
showInstallmentPlan: showInstallmentPlan ?? this.showInstallmentPlan,
showShareQr: showShareQr ?? this.showShareQr,
showFooterPrintTime: showFooterPrintTime ?? this.showFooterPrintTime,
showFooterPreparer: showFooterPreparer ?? this.showFooterPreparer,
footerNote: footerNote ?? this.footerNote,
stampScalePercent: stampScalePercent ?? this.stampScalePercent,
signatureScalePercent: signatureScalePercent ?? this.signatureScalePercent,
lineDiscountDisplay: lineDiscountDisplay ?? this.lineDiscountDisplay,
lineTaxDisplay: lineTaxDisplay ?? this.lineTaxDisplay,
lineAmountBeforeDiscountDisplay:
lineAmountBeforeDiscountDisplay ?? this.lineAmountBeforeDiscountDisplay,
lineAmountBeforeTaxDisplay:
lineAmountBeforeTaxDisplay ?? this.lineAmountBeforeTaxDisplay,
summaryDiscountDisplay: summaryDiscountDisplay ?? this.summaryDiscountDisplay,
summaryTaxDisplay: summaryTaxDisplay ?? this.summaryTaxDisplay,
summaryAmountWithoutTaxDisplay:
summaryAmountWithoutTaxDisplay ?? this.summaryAmountWithoutTaxDisplay,
);
}
}
class _TaxDiscountDisplayModeTile extends StatelessWidget {
final String title;
final String value;
final ValueChanged<String> onChanged;
const _TaxDiscountDisplayModeTile({
required this.title,
required this.value,
required this.onChanged,
});
static const _labels = <String, String>{
_PrintConfig.displaySmart: 'هوشمند',
_PrintConfig.displayAlways: 'همیشه',
_PrintConfig.displayNever: 'هرگز',
};
@override
Widget build(BuildContext context) {
return ListTile(
contentPadding: const EdgeInsetsDirectional.only(start: 8, end: 8),
title: Text(title),
trailing: DropdownButton<String>(
value: _labels.containsKey(value) ? value : _PrintConfig.displaySmart,
underline: const SizedBox.shrink(),
items: _labels.entries
.map(
(e) => DropdownMenuItem<String>(
value: e.key,
child: Text(e.value),
),
)
.toList(),
onChanged: (v) {
if (v != null) onChanged(v);
},
),
);
}
}
bool _isCustomScale(int value) => value != 70 && value != 100 && value != 140;
int _clampScale(dynamic raw, int fallback) {
int n = fallback;
if (raw is int) {
n = raw;
} else if (raw is num) {
n = raw.round();
} else if (raw is String) {
n = int.tryParse(raw.trim()) ?? fallback;
}
if (n < 50) return 50;
if (n > 200) return 200;
return n;
}
class _ScalePresetRow extends StatelessWidget {
final int value;
final ValueChanged<int> onChanged;
const _ScalePresetRow({
required this.value,
required this.onChanged,
});
@override
Widget build(BuildContext context) {
final selected = _isCustomScale(value)
? 'custom'
: (value == 70
? 'small'
: (value == 140 ? 'large' : 'medium'));
return Wrap(
spacing: 8,
runSpacing: 8,
children: [
ChoiceChip(
label: const Text('کوچک'),
selected: selected == 'small',
onSelected: (_) => onChanged(70),
),
ChoiceChip(
label: const Text('متوسط'),
selected: selected == 'medium',
onSelected: (_) => onChanged(100),
),
ChoiceChip(
label: const Text('بزرگ'),
selected: selected == 'large',
onSelected: (_) => onChanged(140),
),
ChoiceChip(
label: Text(_isCustomScale(value) ? 'سفارشی ($value٪)' : 'سفارشی'),
selected: selected == 'custom',
onSelected: (_) {
if (!_isCustomScale(value)) {
onChanged(120);
}
},
),
],
);
}
}
class _ScaleSlider extends StatelessWidget {
final String label;
final int value;
final ValueChanged<int> onChanged;
const _ScaleSlider({
required this.label,
required this.value,
required this.onChanged,
});
@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Row(
children: [
Expanded(child: Text('$label: $value٪')),
],
),
Slider(
value: value.toDouble().clamp(50, 200),
min: 50,
max: 200,
divisions: 30,
label: '$value٪',
onChanged: (v) => onChanged(v.round()),
),
],
);
}
}
class _DocTypeDef {
final String key;
final String label;
const _DocTypeDef(this.key, this.label);
}