expense_detail_dialog.dart 38 KB

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