expense_detail_dialog.dart 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250
  1. // ignore_for_file: use_build_context_synchronously
  2. import 'package:flutter/material.dart';
  3. import 'package:flutter/services.dart';
  4. import 'package:tdesign_flutter/tdesign_flutter.dart';
  5. import '../../../core/i18n/app_localizations.dart';
  6. import '../../../core/theme/app_colors.dart';
  7. import '../../../core/theme/app_colors_extension.dart';
  8. import '../../../core/utils/amount_utils.dart';
  9. import '../../../shared/models/bill_file_rights.dart';
  10. import '../expense_api.dart';
  11. import '../../../shared/widgets/attachment_picker.dart';
  12. import '../../../shared/widgets/searchable_picker_sheet.dart';
  13. /// 报销明细输入数据。
  14. class ExpenseDetailInputData {
  15. final String category;
  16. final String categoryName;
  17. final String acctSubjectId;
  18. final String acctSubjectName;
  19. final String purpose;
  20. final double amount;
  21. final double taxRate;
  22. final String projectId;
  23. final String projectName;
  24. final String costDeptId;
  25. final String costDeptName;
  26. final String customerVendorId;
  27. final String customerVendorName;
  28. final double approvedAmount;
  29. final double offsetAmount;
  30. final String bankName;
  31. final String bankAccountName;
  32. final String bankAccount;
  33. final String remark;
  34. final List<String> attachmentPaths;
  35. final String sqMan;
  36. final String sqManName;
  37. final String aeNo;
  38. final String aeDd;
  39. const ExpenseDetailInputData({
  40. required this.category,
  41. required this.categoryName,
  42. required this.acctSubjectId,
  43. required this.acctSubjectName,
  44. required this.purpose,
  45. required this.amount,
  46. required this.taxRate,
  47. this.projectId = '',
  48. this.projectName = '',
  49. this.costDeptId = '',
  50. this.costDeptName = '',
  51. this.customerVendorId = '',
  52. this.customerVendorName = '',
  53. this.approvedAmount = 0.0,
  54. this.offsetAmount = 0.0,
  55. this.bankName = '',
  56. this.bankAccountName = '',
  57. this.bankAccount = '',
  58. this.remark = '',
  59. this.attachmentPaths = const [],
  60. this.sqMan = '',
  61. this.sqManName = '',
  62. this.aeNo = '',
  63. this.aeDd = '',
  64. });
  65. }
  66. /// 报销明细编辑弹窗。
  67. ///
  68. /// 使用 [TDSlidePopupRoute] 从底部滑出,卡片化展示表单字段。
  69. /// 参照 ExpenseApplyCreatePage 的 ExpenseDetailDialog 样式。
  70. class ExpenseDetailDialog extends StatefulWidget {
  71. final ExpenseApi api;
  72. final AppLocalizations l10n;
  73. final ExpenseDetailInputData? initialData;
  74. final Future<bool> Function()? checkAttachHealth;
  75. final bool showAttachments;
  76. final bool canEditApprovedAmount;
  77. final BillFileRights? billFileRights;
  78. final dynamic acctTree;
  79. const ExpenseDetailDialog({
  80. super.key,
  81. required this.api,
  82. required this.l10n,
  83. this.initialData,
  84. this.checkAttachHealth,
  85. this.showAttachments = true,
  86. this.canEditApprovedAmount = false,
  87. this.billFileRights,
  88. required this.acctTree,
  89. });
  90. /// 显示弹窗,返回 [ExpenseDetailInputData] 或 `null`(取消时)。
  91. static Future<ExpenseDetailInputData?> show(
  92. BuildContext context, {
  93. required ExpenseApi api,
  94. required AppLocalizations l10n,
  95. ExpenseDetailInputData? initialData,
  96. Future<bool> Function()? checkAttachHealth,
  97. bool showAttachments = true,
  98. bool canEditApprovedAmount = false,
  99. BillFileRights? billFileRights,
  100. required dynamic acctTree,
  101. }) {
  102. FocusScope.of(context).unfocus();
  103. return Navigator.push<ExpenseDetailInputData>(
  104. context,
  105. TDSlidePopupRoute<ExpenseDetailInputData>(
  106. slideTransitionFrom: SlideTransitionFrom.bottom,
  107. isDismissible: true,
  108. builder: (_) => ExpenseDetailDialog(
  109. api: api,
  110. l10n: l10n,
  111. initialData: initialData,
  112. checkAttachHealth: checkAttachHealth,
  113. showAttachments: showAttachments,
  114. canEditApprovedAmount: canEditApprovedAmount,
  115. billFileRights: billFileRights,
  116. acctTree: acctTree,
  117. ),
  118. ),
  119. );
  120. }
  121. @override
  122. State<ExpenseDetailDialog> createState() => _ExpenseDetailDialogState();
  123. }
  124. class _ExpenseDetailDialogState extends State<ExpenseDetailDialog> {
  125. late CostProjectItem _selCat;
  126. late TextEditingController _descCtrl;
  127. late TextEditingController _amountCtrl;
  128. CustomerItem? _selCustomer;
  129. late TextEditingController _approvedAmountCtrl;
  130. late TextEditingController _remarkCtrl;
  131. late TextEditingController _bankNameCtrl;
  132. late TextEditingController _bankAccountNameCtrl;
  133. late TextEditingController _bankAccountCtrl;
  134. double _taxRate = 0;
  135. ProjectCodeItem? _selProject;
  136. DepartmentItem? _selDept;
  137. EmployeeItem? _selEmployee;
  138. late final AttachmentPickerController _attachmentCtrl;
  139. final ScrollController _scrollCtrl = ScrollController();
  140. final _remarkFocus = FocusNode();
  141. final _bankNameFocus = FocusNode();
  142. final _bankAccountNameFocus = FocusNode();
  143. final _bankAccountFocus = FocusNode();
  144. bool _attachAvailable = false;
  145. void _ensureVisible(FocusNode node) {
  146. if (!node.hasFocus) return;
  147. _doEnsureVisible(node, 0, -1);
  148. }
  149. void _doEnsureVisible(FocusNode node, int attempt, double lastInsets) {
  150. if (attempt >= 15) return;
  151. WidgetsBinding.instance.addPostFrameCallback((_) {
  152. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  153. final insets = MediaQuery.of(context).viewInsets.bottom;
  154. if (insets != lastInsets) {
  155. _doEnsureVisible(node, attempt + 1, insets);
  156. return;
  157. }
  158. final ctx = node.context;
  159. if (ctx == null) return;
  160. Future.delayed(const Duration(milliseconds: 500), () {
  161. if (!mounted || !node.hasFocus || !_scrollCtrl.hasClients) return;
  162. Scrollable.ensureVisible(
  163. ctx,
  164. alignment: 0.5,
  165. duration: const Duration(milliseconds: 300),
  166. );
  167. });
  168. });
  169. }
  170. static const _taxOptions = [0, 6, 9, 13];
  171. static const _taxLabels = ['0%', '6%', '9%', '13%'];
  172. AppLocalizations get _l10n => widget.l10n;
  173. bool get _isEdit => widget.initialData != null;
  174. @override
  175. void initState() {
  176. super.initState();
  177. final d = widget.initialData;
  178. _selCat = CostProjectItem(
  179. idx1: d?.category ?? '',
  180. name: d?.categoryName ?? '',
  181. accNo: d?.acctSubjectId ?? '',
  182. accName: d?.acctSubjectName ?? '',
  183. );
  184. _descCtrl = TextEditingController(text: d?.purpose ?? '');
  185. _amountCtrl = TextEditingController(
  186. text: d != null && d.amount > 0 ? formatAmountNumeric(d.amount) : '',
  187. );
  188. _selCustomer = (d != null && d.customerVendorId.isNotEmpty)
  189. ? CustomerItem(cusNo: '', name: '')
  190. : null;
  191. _approvedAmountCtrl = TextEditingController(
  192. text: d != null && d.approvedAmount > 0
  193. ? formatAmountNumeric(d.approvedAmount)
  194. : '',
  195. );
  196. _remarkCtrl = TextEditingController(text: d?.remark ?? '');
  197. _bankNameCtrl = TextEditingController(text: d?.bankName ?? '');
  198. _bankAccountNameCtrl = TextEditingController(
  199. text: d?.bankAccountName ?? '',
  200. );
  201. _bankAccountCtrl = TextEditingController(text: d?.bankAccount ?? '');
  202. _taxRate = d?.taxRate ?? 0;
  203. _selEmployee = (d != null && d.sqMan.isNotEmpty)
  204. ? EmployeeItem(salNo: d.sqMan, name: d.sqManName)
  205. : null;
  206. _selProject = (d != null && d.projectId.isNotEmpty)
  207. ? ProjectCodeItem(objNo: d.projectId, name: d.projectName)
  208. : null;
  209. _selDept = (d != null && d.costDeptId.isNotEmpty)
  210. ? DepartmentItem(dep: d.costDeptId, name: d.costDeptName)
  211. : null;
  212. if (d != null && d.attachmentPaths.isNotEmpty) {
  213. WidgetsBinding.instance.addPostFrameCallback((_) {
  214. _attachmentCtrl.restoreFromPaths(d.attachmentPaths);
  215. });
  216. }
  217. _attachmentCtrl = AttachmentPickerController(maxCount: 9)
  218. ..addListener(() => setState(() {}));
  219. _remarkFocus.addListener(() => _ensureVisible(_remarkFocus));
  220. _bankNameFocus.addListener(() => _ensureVisible(_bankNameFocus));
  221. _bankAccountNameFocus.addListener(
  222. () => _ensureVisible(_bankAccountNameFocus),
  223. );
  224. _bankAccountFocus.addListener(() => _ensureVisible(_bankAccountFocus));
  225. _checkAttachHealth();
  226. }
  227. Future<void> _checkAttachHealth() async {
  228. if (widget.checkAttachHealth == null) return;
  229. if (mounted) setState(() => _attachAvailable = false);
  230. try {
  231. final ok = await widget.checkAttachHealth!();
  232. if (mounted) setState(() => _attachAvailable = ok);
  233. } catch (_) {
  234. if (mounted) setState(() => _attachAvailable = false);
  235. }
  236. }
  237. @override
  238. void dispose() {
  239. _descCtrl.dispose();
  240. _amountCtrl.dispose();
  241. _approvedAmountCtrl.dispose();
  242. _remarkCtrl.dispose();
  243. _bankNameCtrl.dispose();
  244. _bankAccountNameCtrl.dispose();
  245. _bankAccountCtrl.dispose();
  246. _attachmentCtrl.dispose();
  247. _scrollCtrl.dispose();
  248. _remarkFocus.dispose();
  249. _bankNameFocus.dispose();
  250. _bankAccountNameFocus.dispose();
  251. _bankAccountFocus.dispose();
  252. super.dispose();
  253. }
  254. void _confirm() {
  255. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  256. if (amount <= 0) {
  257. TDToast.showText(_l10n.get('amountPositive'), context: context);
  258. return;
  259. }
  260. Navigator.pop(
  261. context,
  262. ExpenseDetailInputData(
  263. category: _selCat.idx1,
  264. categoryName: _selCat.name,
  265. acctSubjectId: _selCat.accNo,
  266. acctSubjectName: _selCat.accName,
  267. purpose: '',
  268. amount: amount,
  269. taxRate: _taxRate,
  270. projectId: _selProject?.objNo ?? '',
  271. projectName: _selProject?.name ?? '',
  272. costDeptId: _selDept?.dep ?? '',
  273. costDeptName: _selDept?.name ?? '',
  274. customerVendorId: _selCustomer?.cusNo ?? '',
  275. customerVendorName: _selCustomer?.name ?? '',
  276. approvedAmount: double.tryParse(_approvedAmountCtrl.text) ?? 0,
  277. bankName: _bankNameCtrl.text.trim(),
  278. bankAccountName: _bankAccountNameCtrl.text.trim(),
  279. bankAccount: _bankAccountCtrl.text.trim(),
  280. remark: _remarkCtrl.text.trim(),
  281. attachmentPaths: _attachmentCtrl.toPathList(),
  282. sqMan: _selEmployee?.salNo ?? '',
  283. sqManName: _selEmployee?.name ?? '',
  284. aeNo: widget.initialData?.aeNo ?? '',
  285. aeDd: widget.initialData?.aeDd ?? '',
  286. ),
  287. );
  288. }
  289. double get _amountExclTax => _taxRate > 0
  290. ? (double.tryParse(_amountCtrl.text) ?? 0) / (1 + _taxRate / 100)
  291. : (double.tryParse(_amountCtrl.text) ?? 0);
  292. double get _taxAmount =>
  293. (double.tryParse(_amountCtrl.text) ?? 0) - _amountExclTax;
  294. @override
  295. Widget build(BuildContext context) {
  296. final colors = Theme.of(context).extension<AppColorsExtension>()!;
  297. return AnimatedPadding(
  298. padding: EdgeInsets.only(
  299. bottom: MediaQuery.of(context).viewInsets.bottom,
  300. ),
  301. duration: const Duration(milliseconds: 200),
  302. child: SafeArea(
  303. child: ConstrainedBox(
  304. constraints: BoxConstraints(
  305. maxHeight: MediaQuery.of(context).size.height * 0.8,
  306. ),
  307. child: Container(
  308. decoration: BoxDecoration(
  309. color: colors.bgPage,
  310. borderRadius: const BorderRadius.vertical(
  311. top: Radius.circular(16),
  312. ),
  313. ),
  314. child: Column(
  315. mainAxisSize: MainAxisSize.min,
  316. crossAxisAlignment: CrossAxisAlignment.stretch,
  317. children: [
  318. _buildHeader(colors),
  319. Flexible(
  320. child: GestureDetector(
  321. onTap: () => FocusScope.of(context).unfocus(),
  322. behavior: HitTestBehavior.translucent,
  323. child: SingleChildScrollView(
  324. controller: _scrollCtrl,
  325. keyboardDismissBehavior:
  326. ScrollViewKeyboardDismissBehavior.manual,
  327. padding: const EdgeInsets.fromLTRB(16, 0, 16, 12),
  328. child: Column(
  329. mainAxisSize: MainAxisSize.min,
  330. crossAxisAlignment: CrossAxisAlignment.stretch,
  331. children: [
  332. if (_isEdit &&
  333. widget.initialData!.aeNo.isNotEmpty) ...[
  334. _buildAeInfoCard(colors),
  335. const SizedBox(height: 12),
  336. ],
  337. _buildCategoryCard(colors),
  338. const SizedBox(height: 12),
  339. _buildAcctSubjectCard(colors),
  340. const SizedBox(height: 12),
  341. _buildAmountCard(),
  342. const SizedBox(height: 12),
  343. _buildTaxRateCard(colors),
  344. if ((double.tryParse(_amountCtrl.text) ?? 0) > 0) ...[
  345. const SizedBox(height: 12),
  346. _buildCalcInfo(colors),
  347. ],
  348. const SizedBox(height: 12),
  349. _buildApprovedAmountCard(),
  350. const SizedBox(height: 12),
  351. _buildProjectCard(colors),
  352. const SizedBox(height: 12),
  353. _buildCostDeptCard(colors),
  354. const SizedBox(height: 12),
  355. _buildCustomerCard(colors),
  356. const SizedBox(height: 12),
  357. _buildEmployeeCard(colors),
  358. const SizedBox(height: 12),
  359. _buildBankInfoCard(colors),
  360. const SizedBox(height: 12),
  361. _buildRemarkInput(colors),
  362. if (widget.showAttachments) ...[
  363. const SizedBox(height: 12),
  364. _buildAttachmentCard(colors),
  365. ],
  366. ],
  367. ),
  368. ),
  369. ),
  370. ),
  371. Container(
  372. padding: const EdgeInsets.fromLTRB(16, 12, 16, 16),
  373. decoration: BoxDecoration(
  374. color: colors.bgCard,
  375. border: Border(
  376. top: BorderSide(color: colors.border, width: 0.5),
  377. ),
  378. ),
  379. child: _buildActions(),
  380. ),
  381. ],
  382. ),
  383. ),
  384. ),
  385. ),
  386. );
  387. }
  388. // ── 标题栏 ──
  389. Widget _buildHeader(AppColorsExtension colors) {
  390. return Column(
  391. mainAxisSize: MainAxisSize.min,
  392. children: [
  393. Center(
  394. child: Container(
  395. margin: const EdgeInsets.only(top: 8, bottom: 4),
  396. width: 36,
  397. height: 4,
  398. decoration: BoxDecoration(
  399. color: colors.border,
  400. borderRadius: BorderRadius.circular(2),
  401. ),
  402. ),
  403. ),
  404. Padding(
  405. padding: const EdgeInsets.fromLTRB(20, 8, 12, 16),
  406. child: Row(
  407. children: [
  408. const SizedBox(width: 24),
  409. Expanded(
  410. child: Center(
  411. child: Text(
  412. _l10n.get('addExpenseDetail'),
  413. style: TextStyle(
  414. fontSize: AppFontSizes.title,
  415. fontWeight: FontWeight.w600,
  416. color: colors.textPrimary,
  417. ),
  418. ),
  419. ),
  420. ),
  421. GestureDetector(
  422. onTap: () => Navigator.pop(context),
  423. child: Padding(
  424. padding: const EdgeInsets.all(4),
  425. child: Icon(
  426. Icons.close,
  427. size: 20,
  428. color: colors.textSecondary,
  429. ),
  430. ),
  431. ),
  432. ],
  433. ),
  434. ),
  435. ],
  436. );
  437. }
  438. // ── picker 卡片 ──
  439. Widget _pickerCard({
  440. required String label,
  441. required bool required,
  442. required String currentLabel,
  443. required bool hasValue,
  444. required VoidCallback onTap,
  445. VoidCallback? onClear,
  446. }) {
  447. final tdTheme = TDTheme.of(context);
  448. return GestureDetector(
  449. onTap: () {
  450. FocusManager.instance.primaryFocus?.unfocus();
  451. onTap();
  452. },
  453. child: Container(
  454. padding: const EdgeInsets.only(
  455. left: 16,
  456. right: 10,
  457. top: 12,
  458. bottom: 12,
  459. ),
  460. decoration: BoxDecoration(
  461. color: tdTheme.bgColorContainer,
  462. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  463. border: Border.all(color: tdTheme.componentStrokeColor),
  464. ),
  465. child: Row(
  466. children: [
  467. TDText(
  468. label,
  469. maxLines: 1,
  470. overflow: TextOverflow.visible,
  471. font: tdTheme.fontBodyLarge,
  472. fontWeight: FontWeight.w400,
  473. style: const TextStyle(letterSpacing: 0),
  474. ),
  475. if (required)
  476. Padding(
  477. padding: const EdgeInsets.only(left: 4),
  478. child: TDText(
  479. '*',
  480. font: tdTheme.fontBodyLarge,
  481. fontWeight: FontWeight.w400,
  482. style: TextStyle(color: tdTheme.errorColor6),
  483. ),
  484. ),
  485. const SizedBox(width: 12),
  486. Expanded(
  487. child: Row(
  488. mainAxisAlignment: MainAxisAlignment.end,
  489. mainAxisSize: MainAxisSize.max,
  490. children: [
  491. Flexible(
  492. child: TDText(
  493. currentLabel,
  494. maxLines: 1,
  495. overflow: TextOverflow.ellipsis,
  496. font: tdTheme.fontBodyLarge,
  497. fontWeight: FontWeight.w400,
  498. textColor: hasValue ? tdTheme.textColorPrimary : tdTheme.textColorPlaceholder,
  499. textAlign: TextAlign.end,
  500. ),
  501. ),
  502. const SizedBox(width: 4),
  503. SizedBox(
  504. width: 18,
  505. height: 18,
  506. child: onClear != null
  507. ? GestureDetector(
  508. onTap: onClear,
  509. child: Icon(
  510. Icons.close,
  511. size: 18,
  512. color: tdTheme.textColorPlaceholder,
  513. ),
  514. )
  515. : Icon(
  516. Icons.chevron_right,
  517. size: 18,
  518. color: tdTheme.textColorPlaceholder,
  519. ),
  520. ),
  521. ],
  522. ),
  523. ),
  524. ],
  525. ),
  526. ),
  527. );
  528. }
  529. // ── 费用项目 ──
  530. Widget _buildCategoryCard(AppColorsExtension colors) {
  531. return _pickerCard(
  532. label: _l10n.get('expenseCategory'),
  533. required: true,
  534. hasValue: _selCat.idx1.isNotEmpty,
  535. currentLabel: _selCat.idx1.isNotEmpty
  536. ? '${_selCat.idx1}/${_selCat.name}'
  537. : _l10n.get('pleaseSelect'),
  538. onTap: () async {
  539. final result = await showSearchablePicker<CostProjectItem>(
  540. context,
  541. title: '${_l10n.get('select')}${_l10n.get('expenseCategory')}',
  542. searchHint: _l10n.get('search'),
  543. loader: (keyword, page) => widget.api
  544. .getCostProjects(keyword: keyword, page: page, size: 20)
  545. .then((list) => list.cast<CostProjectItem>()),
  546. labelBuilder: (c) => '${c.idx1}/${c.name}',
  547. onRefresh: () => widget.api.clearRefCache(),
  548. );
  549. if (result != null && mounted) {
  550. setState(() => _selCat = result);
  551. }
  552. },
  553. );
  554. }
  555. // ── 输入卡片(对齐 pickerCard 样式) ──
  556. Widget _inputCard({
  557. required String label,
  558. required bool required,
  559. required TextEditingController controller,
  560. required String hintText,
  561. required AppColorsExtension colors,
  562. TextInputType? keyboardType,
  563. List<TextInputFormatter>? inputFormatters,
  564. FocusNode? focusNode,
  565. }) {
  566. final tdTheme = TDTheme.of(context);
  567. final hasValue = controller.text.isNotEmpty;
  568. return Container(
  569. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  570. decoration: BoxDecoration(
  571. color: tdTheme.bgColorContainer,
  572. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  573. border: Border.all(color: tdTheme.componentStrokeColor),
  574. ),
  575. child: Row(
  576. children: [
  577. TDText(
  578. label,
  579. maxLines: 1,
  580. overflow: TextOverflow.visible,
  581. font: tdTheme.fontBodyLarge,
  582. fontWeight: FontWeight.w400,
  583. style: const TextStyle(letterSpacing: 0),
  584. ),
  585. if (required)
  586. Padding(
  587. padding: const EdgeInsets.only(left: 4),
  588. child: TDText(
  589. '*',
  590. font: tdTheme.fontBodyLarge,
  591. fontWeight: FontWeight.w400,
  592. style: TextStyle(color: tdTheme.errorColor6),
  593. ),
  594. ),
  595. const SizedBox(width: 12),
  596. Expanded(
  597. child: Row(
  598. mainAxisAlignment: MainAxisAlignment.end,
  599. mainAxisSize: MainAxisSize.max,
  600. children: [
  601. Flexible(
  602. child: TextField(
  603. controller: controller,
  604. focusNode: focusNode,
  605. textAlign: TextAlign.end,
  606. keyboardType: keyboardType,
  607. inputFormatters: inputFormatters,
  608. style: TextStyle(fontSize: 16, color: colors.textPrimary),
  609. decoration: InputDecoration(
  610. hintText: hintText,
  611. hintStyle: TextStyle(
  612. fontSize: 16,
  613. color: colors.textPlaceholder,
  614. ),
  615. border: InputBorder.none,
  616. isDense: true,
  617. contentPadding: EdgeInsets.zero,
  618. ),
  619. onChanged: (_) => setState(() {}),
  620. ),
  621. ),
  622. const SizedBox(width: 4),
  623. SizedBox(
  624. width: 18,
  625. height: 18,
  626. child: hasValue
  627. ? GestureDetector(
  628. onTap: () {
  629. controller.clear();
  630. setState(() {});
  631. },
  632. child: Icon(
  633. Icons.close,
  634. size: 18,
  635. color: tdTheme.textColorPlaceholder,
  636. ),
  637. )
  638. : null,
  639. ),
  640. ],
  641. ),
  642. ),
  643. ],
  644. ),
  645. );
  646. }
  647. // ── 含税金额 ──
  648. Widget _buildAmountCard() {
  649. return _inputCard(
  650. label: _l10n.get('amountInclTax'),
  651. required: true,
  652. controller: _amountCtrl,
  653. hintText: '>0',
  654. colors: Theme.of(context).extension<AppColorsExtension>()!,
  655. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  656. inputFormatters: [
  657. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  658. ],
  659. );
  660. }
  661. // ── 税率 ──
  662. Widget _buildTaxRateCard(AppColorsExtension colors) {
  663. final currentLabel = '${_taxRate.toStringAsFixed(0)}%';
  664. final labels = _taxLabels.toList();
  665. return _pickerCard(
  666. label: _l10n.get('taxRate'),
  667. required: false,
  668. hasValue: true,
  669. currentLabel: currentLabel,
  670. onTap: () {
  671. FocusManager.instance.primaryFocus?.unfocus();
  672. TDPicker.showMultiPicker(
  673. context,
  674. title: _l10n.get('taxRate'),
  675. backgroundColor: colors.bgCard,
  676. data: [labels],
  677. onConfirm: (selected) {
  678. if (selected.isNotEmpty && selected[0] is int) {
  679. final idx = selected[0] as int;
  680. if (idx >= 0 && idx < labels.length) {
  681. Navigator.of(context).pop();
  682. setState(() => _taxRate = _taxOptions[idx].toDouble());
  683. }
  684. }
  685. },
  686. );
  687. },
  688. );
  689. }
  690. // ── 计算信息 ──
  691. Widget _buildCalcInfo(AppColorsExtension colors) {
  692. final amount = double.tryParse(_amountCtrl.text) ?? 0;
  693. if (amount <= 0) return const SizedBox.shrink();
  694. return Container(
  695. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 10),
  696. decoration: BoxDecoration(
  697. color: colors.primaryLight,
  698. borderRadius: BorderRadius.circular(8),
  699. ),
  700. child: Row(
  701. children: [
  702. Expanded(
  703. child: Text(
  704. '${_l10n.get('amountExcludingTax')}: ${formatAmount(_amountExclTax)}',
  705. style: TextStyle(
  706. fontSize: AppFontSizes.body,
  707. color: colors.textSecondary,
  708. ),
  709. ),
  710. ),
  711. Text(
  712. '${_l10n.get('taxAmount')}: ${formatAmount(_taxAmount)}',
  713. style: TextStyle(
  714. fontSize: AppFontSizes.body,
  715. color: colors.textSecondary,
  716. ),
  717. ),
  718. ],
  719. ),
  720. );
  721. }
  722. // ── 导入单据信息 ──
  723. Widget _buildAeInfoCard(AppColorsExtension colors) {
  724. final d = widget.initialData!;
  725. final tdTheme = TDTheme.of(context);
  726. final dateStr = d.aeDd.isNotEmpty ? _formatDate(d.aeDd) : d.aeDd;
  727. return Container(
  728. padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 12),
  729. decoration: _cardDecoration(tdTheme),
  730. child: Column(
  731. children: [
  732. _readOnlyRow(tdTheme, _l10n.get('expenseApplyNo'), d.aeNo),
  733. const SizedBox(height: 8),
  734. _readOnlyRow(tdTheme, _l10n.get('applyDate'), dateStr),
  735. ],
  736. ),
  737. );
  738. }
  739. String _formatDate(String raw) {
  740. final dt = DateTime.tryParse(raw);
  741. if (dt == null) return raw;
  742. return '${dt.year}-${dt.month.toString().padLeft(2, '0')}-${dt.day.toString().padLeft(2, '0')}';
  743. }
  744. Widget _readOnlyRow(TDThemeData tdTheme, String label, String value) {
  745. return Row(
  746. children: [
  747. TDText(
  748. label,
  749. font: tdTheme.fontBodyLarge,
  750. fontWeight: FontWeight.w400,
  751. style: const TextStyle(letterSpacing: 0),
  752. ),
  753. const SizedBox(width: 12),
  754. Expanded(
  755. child: TDText(
  756. value,
  757. maxLines: 1,
  758. overflow: TextOverflow.ellipsis,
  759. font: tdTheme.fontBodyLarge,
  760. fontWeight: FontWeight.w400,
  761. textColor: tdTheme.textColorPrimary,
  762. textAlign: TextAlign.end,
  763. ),
  764. ),
  765. ],
  766. );
  767. }
  768. BoxDecoration _cardDecoration(TDThemeData tdTheme) => BoxDecoration(
  769. color: tdTheme.bgColorContainer,
  770. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  771. border: Border.all(color: tdTheme.componentStrokeColor),
  772. );
  773. // ── 申请人 ──
  774. Widget _buildEmployeeCard(AppColorsExtension colors) {
  775. return _pickerCard(
  776. label: _l10n.get('applicant'),
  777. required: false,
  778. hasValue: _selEmployee != null,
  779. currentLabel: _selEmployee != null
  780. ? '${_selEmployee!.salNo}/${_selEmployee!.name}'
  781. : _l10n.get('pleaseSelect'),
  782. onTap: () async {
  783. final result = await showSearchablePicker<EmployeeItem>(
  784. context,
  785. title: '${_l10n.get('select')}${_l10n.get('applicant')}',
  786. searchHint: _l10n.get('search'),
  787. loader: (keyword, page) =>
  788. widget.api.getEmployees(keyword: keyword, page: page, size: 20),
  789. labelBuilder: (e) =>
  790. e.name.isEmpty ? e.salNo : '${e.salNo} ${e.name}',
  791. onRefresh: () => widget.api.clearRefCache(),
  792. );
  793. if (result != null && mounted) {
  794. setState(() {
  795. _selEmployee = result;
  796. _bankNameCtrl.text = result.bnkNo;
  797. _bankAccountNameCtrl.text = result.accName;
  798. _bankAccountCtrl.text = result.bnkId;
  799. });
  800. }
  801. },
  802. onClear: _selEmployee != null
  803. ? () => setState(() {
  804. _selEmployee = null;
  805. _bankNameCtrl.clear();
  806. _bankAccountNameCtrl.clear();
  807. _bankAccountCtrl.clear();
  808. })
  809. : null,
  810. );
  811. }
  812. // ── 客户/厂商 ──
  813. Widget _buildCustomerCard(AppColorsExtension colors) {
  814. return _pickerCard(
  815. label: _l10n.get('customerVendor'),
  816. required: false,
  817. hasValue: _selCustomer != null,
  818. currentLabel: _selCustomer != null
  819. ? '${_selCustomer!.cusNo}/${_selCustomer!.name}'
  820. : _l10n.get('pleaseSelect'),
  821. onTap: () async {
  822. final result = await showSearchablePicker<CustomerItem>(
  823. context,
  824. title: '${_l10n.get('select')}${_l10n.get('customerVendor')}',
  825. searchHint: _l10n.get('search'),
  826. loader: (keyword, page) =>
  827. widget.api.getCustomers(keyword: keyword, page: page, size: 20),
  828. labelBuilder: (v) =>
  829. v.name.isEmpty ? v.cusNo : '${v.cusNo} ${v.name}',
  830. onRefresh: () => widget.api.clearRefCache(),
  831. );
  832. if (result != null && mounted) {
  833. setState(() => _selCustomer = result);
  834. }
  835. },
  836. onClear: _selCustomer != null
  837. ? () => setState(() => _selCustomer = null)
  838. : null,
  839. );
  840. }
  841. // ── 核准金额 ──
  842. Widget _buildApprovedAmountCard() {
  843. if (!widget.canEditApprovedAmount) {
  844. final value = _approvedAmountCtrl.text.isNotEmpty
  845. ? formatAmount(double.tryParse(_approvedAmountCtrl.text) ?? 0)
  846. : '-';
  847. return _readOnlyCard(
  848. label: _l10n.get('approvedAmount'),
  849. value: value,
  850. colors: Theme.of(context).extension<AppColorsExtension>()!,
  851. );
  852. }
  853. return _inputCard(
  854. label: _l10n.get('approvedAmount'),
  855. required: false,
  856. controller: _approvedAmountCtrl,
  857. hintText: '0',
  858. colors: Theme.of(context).extension<AppColorsExtension>()!,
  859. keyboardType: const TextInputType.numberWithOptions(decimal: true),
  860. inputFormatters: [
  861. FilteringTextInputFormatter.allow(RegExp(r'^\d*\.?\d{0,2}$')),
  862. ],
  863. );
  864. }
  865. // ── 备注 ──
  866. Widget _buildRemarkInput(AppColorsExtension colors) {
  867. final tdTheme = TDTheme.of(context);
  868. return TDTextarea(
  869. controller: _remarkCtrl,
  870. focusNode: _remarkFocus,
  871. label: _l10n.get('remark'),
  872. hintText: _l10n.get('enterRemark'),
  873. maxLines: 3,
  874. minLines: 1,
  875. maxLength: 500,
  876. indicator: true,
  877. decoration: BoxDecoration(
  878. color: tdTheme.bgColorContainer,
  879. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  880. border: Border.all(color: tdTheme.componentStrokeColor),
  881. ),
  882. onChanged: (_) => setState(() {}),
  883. );
  884. }
  885. // ── 操作按钮 ──
  886. Widget _buildAttachmentCard(AppColorsExtension colors) {
  887. final tdTheme = TDTheme.of(context);
  888. Widget? hint;
  889. if (!_attachAvailable) {
  890. hint = TDText(
  891. _l10n.get('attachServiceUnavailable'),
  892. font: tdTheme.fontBodySmall,
  893. fontWeight: FontWeight.w400,
  894. textColor: tdTheme.textColorPlaceholder,
  895. );
  896. } else if (widget.billFileRights != null &&
  897. !widget.billFileRights!.canManageAttachments) {
  898. hint = TDText(
  899. _l10n.get('noAttachmentPermission'),
  900. font: tdTheme.fontBodySmall,
  901. fontWeight: FontWeight.w400,
  902. textColor: tdTheme.textColorPlaceholder,
  903. );
  904. }
  905. return Column(
  906. crossAxisAlignment: CrossAxisAlignment.start,
  907. children: [
  908. Padding(
  909. padding: const EdgeInsets.only(left: 4),
  910. child: TDText(
  911. _l10n.get('attachmentUpload'),
  912. font: tdTheme.fontBodyLarge,
  913. fontWeight: FontWeight.w400,
  914. style: const TextStyle(letterSpacing: 0),
  915. ),
  916. ),
  917. const SizedBox(height: 4),
  918. if (hint != null) ...[
  919. Padding(padding: const EdgeInsets.only(left: 4), child: hint),
  920. ] else ...[
  921. Padding(
  922. padding: const EdgeInsets.only(left: 4, top: 4),
  923. child: TDText(
  924. _l10n.get('maxAttachment'),
  925. font: tdTheme.fontBodySmall,
  926. fontWeight: FontWeight.w400,
  927. textColor: tdTheme.textColorPlaceholder,
  928. ),
  929. ),
  930. const SizedBox(height: 4),
  931. AttachmentPicker(
  932. controller: _attachmentCtrl,
  933. maxImageSizeMB: 10,
  934. maxFileSizeMB: 20,
  935. allowedExtensions: const [
  936. 'pdf',
  937. 'doc',
  938. 'docx',
  939. 'xls',
  940. 'xlsx',
  941. 'ppt',
  942. 'pptx',
  943. 'txt',
  944. ],
  945. onFileRejected: (file, reason) {
  946. if (context.mounted) TDToast.showText(reason, context: context);
  947. },
  948. ),
  949. ],
  950. ],
  951. );
  952. }
  953. // ── 会计科目(级联选择器,支持手动修改) ──
  954. Widget _buildAcctSubjectCard(AppColorsExtension colors) {
  955. final tdTheme = TDTheme.of(context);
  956. final hasValue = _selCat.accNo.isNotEmpty;
  957. final displayValue = hasValue
  958. ? (_selCat.accName.startsWith(_selCat.accNo)
  959. ? _selCat.accName
  960. : (_selCat.accName.toString().isNotEmpty
  961. ? '${_selCat.accNo}/${_selCat.accName}'
  962. : _selCat.accNo))
  963. : _l10n.get('pleaseSelect');
  964. return GestureDetector(
  965. onTap: () {
  966. if (widget.acctTree == null) {
  967. TDToast.showText(_l10n.get('loading'), context: context);
  968. return;
  969. }
  970. FocusManager.instance.primaryFocus?.unfocus();
  971. TDCascader.showMultiCascader(
  972. context,
  973. title: _l10n.get('acctSubject'),
  974. data: widget.acctTree as List<Map>,
  975. theme: 'step',
  976. onChange: (List<MultiCascaderListModel> selected) {
  977. setState(() {
  978. _selCat = CostProjectItem(
  979. idx1: _selCat.idx1,
  980. name: _selCat.name,
  981. accNo: (selected.last.value ?? '').replaceFirst('self:', ''),
  982. accName: (selected.last.label ?? '')
  983. .replaceFirst(RegExp(r'^> '), '')
  984. .replaceFirst('${selected.last.value ?? ''}/', ''),
  985. );
  986. });
  987. },
  988. );
  989. },
  990. child: Container(
  991. padding: const EdgeInsets.only(left: 16, right: 10, top: 12, bottom: 12),
  992. decoration: BoxDecoration(
  993. color: tdTheme.bgColorContainer,
  994. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  995. border: Border.all(color: tdTheme.componentStrokeColor),
  996. ),
  997. child: Row(
  998. children: [
  999. TDText(
  1000. _l10n.get('acctSubject'),
  1001. maxLines: 1,
  1002. overflow: TextOverflow.visible,
  1003. font: tdTheme.fontBodyLarge,
  1004. fontWeight: FontWeight.w400,
  1005. style: const TextStyle(letterSpacing: 0),
  1006. ),
  1007. const SizedBox(width: 12),
  1008. Expanded(
  1009. child: Row(
  1010. mainAxisAlignment: MainAxisAlignment.end,
  1011. mainAxisSize: MainAxisSize.max,
  1012. children: [
  1013. Flexible(
  1014. child: TDText(
  1015. displayValue,
  1016. maxLines: 1,
  1017. overflow: TextOverflow.ellipsis,
  1018. font: tdTheme.fontBodyLarge,
  1019. fontWeight: FontWeight.w400,
  1020. textColor: hasValue ? tdTheme.textColorPrimary : tdTheme.textColorPlaceholder,
  1021. textAlign: TextAlign.end,
  1022. ),
  1023. ),
  1024. const SizedBox(width: 4),
  1025. SizedBox(
  1026. width: 18,
  1027. height: 18,
  1028. child: hasValue
  1029. ? GestureDetector(
  1030. onTap: () {
  1031. setState(() {
  1032. _selCat = CostProjectItem(
  1033. idx1: _selCat.idx1,
  1034. name: _selCat.name,
  1035. accNo: '',
  1036. accName: '',
  1037. );
  1038. });
  1039. },
  1040. child: Icon(Icons.close, size: 18, color: tdTheme.textColorPlaceholder),
  1041. )
  1042. : Icon(Icons.chevron_right, size: 18, color: tdTheme.textColorPlaceholder),
  1043. ),
  1044. ],
  1045. ),
  1046. ),
  1047. ],
  1048. ),
  1049. ),
  1050. );
  1051. }
  1052. Widget _readOnlyCard({
  1053. required String label,
  1054. required String value,
  1055. required AppColorsExtension colors,
  1056. }) {
  1057. final tdTheme = TDTheme.of(context);
  1058. return Container(
  1059. padding: const EdgeInsets.only(left: 16, right: 16, top: 12, bottom: 12),
  1060. decoration: BoxDecoration(
  1061. color: tdTheme.bgColorContainer,
  1062. borderRadius: BorderRadius.circular(tdTheme.radiusDefault),
  1063. border: Border.all(color: tdTheme.componentStrokeColor),
  1064. ),
  1065. child: Row(
  1066. children: [
  1067. TDText(
  1068. label,
  1069. maxLines: 1,
  1070. overflow: TextOverflow.visible,
  1071. font: tdTheme.fontBodyLarge,
  1072. fontWeight: FontWeight.w400,
  1073. style: const TextStyle(letterSpacing: 0),
  1074. ),
  1075. const SizedBox(width: 12),
  1076. Expanded(
  1077. child: TDText(
  1078. value,
  1079. maxLines: 1,
  1080. overflow: TextOverflow.ellipsis,
  1081. font: tdTheme.fontBodyLarge,
  1082. fontWeight: FontWeight.w400,
  1083. textColor: tdTheme.textColorPrimary,
  1084. textAlign: TextAlign.end,
  1085. ),
  1086. ),
  1087. ],
  1088. ),
  1089. );
  1090. }
  1091. // ── 关联项目 ──
  1092. Widget _buildProjectCard(AppColorsExtension colors) {
  1093. return _pickerCard(
  1094. label: _l10n.get('relatedProject'),
  1095. required: false,
  1096. hasValue: _selProject != null,
  1097. currentLabel: _selProject != null
  1098. ? '${_selProject!.objNo}/${_selProject!.name}'
  1099. : _l10n.get('pleaseSelect'),
  1100. onTap: () async {
  1101. final result = await showSearchablePicker<ProjectCodeItem>(
  1102. context,
  1103. title: '${_l10n.get('select')}${_l10n.get('relatedProject')}',
  1104. searchHint: _l10n.get('search'),
  1105. loader: (keyword, page) => widget.api
  1106. .getProjectCodes(keyword: keyword, page: page, size: 20)
  1107. .then((list) => list.cast<ProjectCodeItem>()),
  1108. labelBuilder: (p) => '${p.objNo}/${p.name}',
  1109. onRefresh: () => widget.api.clearRefCache(),
  1110. );
  1111. if (result != null && mounted) {
  1112. setState(() => _selProject = result);
  1113. }
  1114. },
  1115. onClear: _selProject != null
  1116. ? () => setState(() => _selProject = null)
  1117. : null,
  1118. );
  1119. }
  1120. // ── 费用承担部门 ──
  1121. Widget _buildCostDeptCard(AppColorsExtension colors) {
  1122. return _pickerCard(
  1123. label: _l10n.get('costDept'),
  1124. required: false,
  1125. hasValue: _selDept != null,
  1126. currentLabel: _selDept != null
  1127. ? '${_selDept!.dep}/${_selDept!.name}'
  1128. : _l10n.get('pleaseSelect'),
  1129. onTap: () async {
  1130. final result = await showSearchablePicker<DepartmentItem>(
  1131. context,
  1132. title: '${_l10n.get('select')}${_l10n.get('costDept')}',
  1133. searchHint: _l10n.get('search'),
  1134. loader: (keyword, page) =>
  1135. widget.api.getDepartments(keyword: keyword, page: page, size: 20),
  1136. labelBuilder: (d) => d.name.isEmpty ? d.dep : '${d.dep} ${d.name}',
  1137. onRefresh: () => widget.api.clearRefCache(),
  1138. );
  1139. if (result != null && mounted) {
  1140. setState(() => _selDept = result);
  1141. }
  1142. },
  1143. onClear: _selDept != null ? () => setState(() => _selDept = null) : null,
  1144. );
  1145. }
  1146. // ── 收款银行信息 ──
  1147. Widget _buildBankInfoCard(AppColorsExtension colors) {
  1148. return Column(
  1149. crossAxisAlignment: CrossAxisAlignment.start,
  1150. children: [
  1151. _inputCard(
  1152. label: _l10n.get('bankName'),
  1153. required: false,
  1154. controller: _bankNameCtrl,
  1155. hintText: _l10n.get('pleaseEnter'),
  1156. colors: colors,
  1157. focusNode: _bankNameFocus,
  1158. ),
  1159. const SizedBox(height: 12),
  1160. _inputCard(
  1161. label: _l10n.get('bankAccountName'),
  1162. required: false,
  1163. controller: _bankAccountNameCtrl,
  1164. hintText: _l10n.get('pleaseEnter'),
  1165. colors: colors,
  1166. focusNode: _bankAccountNameFocus,
  1167. ),
  1168. const SizedBox(height: 12),
  1169. _inputCard(
  1170. label: _l10n.get('bankAccount'),
  1171. required: false,
  1172. controller: _bankAccountCtrl,
  1173. hintText: _l10n.get('pleaseEnter'),
  1174. colors: colors,
  1175. focusNode: _bankAccountFocus,
  1176. ),
  1177. ],
  1178. );
  1179. }
  1180. Widget _buildActions() {
  1181. return Row(
  1182. children: [
  1183. Expanded(
  1184. child: TDButton(
  1185. text: _l10n.get('cancel'),
  1186. size: TDButtonSize.large,
  1187. type: TDButtonType.outline,
  1188. shape: TDButtonShape.rectangle,
  1189. theme: TDButtonTheme.defaultTheme,
  1190. onTap: () => Navigator.pop(context),
  1191. ),
  1192. ),
  1193. const SizedBox(width: 12),
  1194. Expanded(
  1195. child: TDButton(
  1196. text: _isEdit ? _l10n.get('confirmEdit') : _l10n.get('add'),
  1197. size: TDButtonSize.large,
  1198. type: TDButtonType.fill,
  1199. shape: TDButtonShape.rectangle,
  1200. theme: TDButtonTheme.primary,
  1201. onTap: _confirm,
  1202. ),
  1203. ),
  1204. ],
  1205. );
  1206. }
  1207. }