expense_create_controller.dart 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. import 'package:flutter_riverpod/flutter_riverpod.dart';
  2. import '../../core/storage/draft_storage.dart';
  3. import 'expense_model.dart';
  4. import 'expense_api.dart';
  5. const expenseDraftKey = 'expense';
  6. class ExpenseCreateState {
  7. final ExpenseModel expense;
  8. final bool isSubmitting;
  9. const ExpenseCreateState({required this.expense, this.isSubmitting = false});
  10. ExpenseCreateState copyWith({ExpenseModel? expense, bool? isSubmitting}) {
  11. return ExpenseCreateState(
  12. expense: expense ?? this.expense,
  13. isSubmitting: isSubmitting ?? this.isSubmitting,
  14. );
  15. }
  16. }
  17. class ExpenseCreateController extends StateNotifier<ExpenseCreateState> {
  18. final ExpenseApi _api;
  19. ExpenseCreateController(this._api, {ExpenseModel? initial})
  20. : super(
  21. ExpenseCreateState(
  22. expense:
  23. initial ??
  24. ExpenseModel(
  25. id: '',
  26. expenseNo: '',
  27. createTime: DateTime.now(),
  28. updateTime: DateTime.now(),
  29. ),
  30. ),
  31. );
  32. void updateCurrencyCode(String code, double excRto) {
  33. state = state.copyWith(
  34. expense: state.expense.copyWith(currencyCode: code, excRto: excRto),
  35. );
  36. }
  37. void updateApprovedAmount(double amount) {
  38. state = state.copyWith(
  39. expense: state.expense.copyWith(approvedAmount: amount),
  40. );
  41. }
  42. void updateDetailApprovedAmount(int index, double amount) {
  43. final details = [...state.expense.details];
  44. details[index] = details[index].copyWith(approvedAmount: amount);
  45. state = state.copyWith(expense: state.expense.copyWith(details: details));
  46. }
  47. void updateDetailCustomerVendor(int index, String name) {
  48. final details = [...state.expense.details];
  49. details[index] = details[index].copyWith(customerVendorName: name);
  50. state = state.copyWith(expense: state.expense.copyWith(details: details));
  51. }
  52. void updateDetailProjectId(int index, String id) {
  53. final details = [...state.expense.details];
  54. details[index] = details[index].copyWith(projectId: id);
  55. state = state.copyWith(expense: state.expense.copyWith(details: details));
  56. }
  57. void updateDetailAcctSubjectId(int index, String id) {
  58. final details = [...state.expense.details];
  59. details[index] = details[index].copyWith(acctSubjectId: id);
  60. state = state.copyWith(expense: state.expense.copyWith(details: details));
  61. }
  62. void updateDetailOffsetAmount(int index, double amount) {
  63. final details = [...state.expense.details];
  64. details[index] = details[index].copyWith(offsetAmount: amount);
  65. state = state.copyWith(expense: state.expense.copyWith(details: details));
  66. }
  67. void restoreFromDraft(ExpenseModel draft, ExpenseApi api) {
  68. state = ExpenseCreateState(
  69. expense: draft.copyWith(
  70. deptId: '',
  71. deptName: '',
  72. applicantId: '',
  73. applicantName: '',
  74. ),
  75. );
  76. }
  77. void reset() {
  78. state = ExpenseCreateState(
  79. expense: ExpenseModel(
  80. id: '',
  81. expenseNo: '',
  82. createTime: DateTime.now(),
  83. updateTime: DateTime.now(),
  84. ),
  85. );
  86. }
  87. void updatePurpose(String purpose) {
  88. state = state.copyWith(expense: state.expense.copyWith(purpose: purpose));
  89. }
  90. ExpenseCreateState get currentState => state;
  91. void updateAttachments(List<String> paths) {
  92. state = state.copyWith(expense: state.expense.copyWith(attachments: paths));
  93. }
  94. void updatePaymentMethod(String method) {
  95. state = state.copyWith(
  96. expense: state.expense.copyWith(paymentMethod: method),
  97. );
  98. }
  99. void updateRemark(String remark) {
  100. state = state.copyWith(expense: state.expense.copyWith(remark: remark));
  101. }
  102. void setGenerateVoucher(bool value) {
  103. state = state.copyWith(
  104. expense: state.expense.copyWith(isGenerateVoucher: value),
  105. );
  106. }
  107. void updateDept(String deptId, String deptName) {
  108. state = state.copyWith(
  109. expense: state.expense.copyWith(deptId: deptId, deptName: deptName),
  110. );
  111. }
  112. void updateEmployee(String applicantId, String applicantName) {
  113. state = state.copyWith(
  114. expense: state.expense.copyWith(
  115. applicantId: applicantId,
  116. applicantName: applicantName,
  117. ),
  118. );
  119. }
  120. void addDetail(ExpenseDetailModel detail) {
  121. final details = [...state.expense.details, detail];
  122. state = state.copyWith(expense: state.expense.copyWith(details: details));
  123. }
  124. void removeDetail(int index) {
  125. final details = [...state.expense.details]..removeAt(index);
  126. state = state.copyWith(expense: state.expense.copyWith(details: details));
  127. }
  128. void removeDetailsByAeNo(String aeNo) {
  129. final details = state.expense.details.where((d) => d.aeNo != aeNo).toList();
  130. state = state.copyWith(expense: state.expense.copyWith(details: details));
  131. }
  132. void updateDetail(int index, ExpenseDetailModel detail) {
  133. final details = [...state.expense.details];
  134. details[index] = detail;
  135. state = state.copyWith(expense: state.expense.copyWith(details: details));
  136. }
  137. void recalculateAmount() {
  138. var totalAmount = 0.0;
  139. var approvedAmount = 0.0;
  140. for (final d in state.expense.details) {
  141. totalAmount += d.totalAmount;
  142. approvedAmount += d.approvedAmount;
  143. }
  144. state = state.copyWith(
  145. expense: state.expense.copyWith(
  146. totalAmount: totalAmount,
  147. approvedAmount: approvedAmount,
  148. ),
  149. );
  150. }
  151. Future<bool> submit() async {
  152. state = state.copyWith(isSubmitting: true);
  153. try {
  154. final expense = state.expense.copyWith(status: 'pending');
  155. // 手动拼 Map(提交流程专用,保持 camelCase key,与 BillSave 接口一致)
  156. final data = <String, dynamic>{
  157. 'id': expense.id,
  158. 'expenseNo': expense.expenseNo,
  159. 'expenseDate': expense.expenseDate?.toIso8601String(),
  160. 'applicantId': expense.applicantId,
  161. 'applicantName': expense.applicantName,
  162. 'deptId': expense.deptId,
  163. 'deptName': expense.deptName,
  164. 'currencyCode': expense.currencyCode,
  165. 'excRto': expense.excRto,
  166. 'isGenerateVoucher': expense.isGenerateVoucher,
  167. 'voucherNo': expense.voucherNo,
  168. 'approvedAmount': expense.approvedAmount,
  169. 'totalAmount': expense.totalAmount,
  170. 'totalAmtnSh': expense.totalAmtnSh,
  171. 'purpose': expense.purpose,
  172. 'remark': expense.remark,
  173. 'paymentMethod': expense.paymentMethod,
  174. 'status': expense.status,
  175. 'approvalInstanceId': expense.approvalInstanceId,
  176. 'previousInstanceIds': expense.previousInstanceIds,
  177. 'effectiveDate': expense.effectiveDate?.toIso8601String(),
  178. 'auditorId': expense.auditorId,
  179. 'version': expense.version,
  180. 'createTime': expense.createTime.toIso8601String(),
  181. 'updateTime': expense.updateTime.toIso8601String(),
  182. 'details': expense.details.map((d) => d.toJson()).toList(),
  183. 'attachments': expense.attachments,
  184. };
  185. await _api.submit(data);
  186. await DraftStorage.delete(expenseDraftKey);
  187. return true;
  188. } catch (_) {
  189. return false;
  190. } finally {
  191. state = state.copyWith(isSubmitting: false);
  192. }
  193. }
  194. Future<bool> saveDraft() async {
  195. state = state.copyWith(isSubmitting: true);
  196. try {
  197. final data = state.expense.toJson();
  198. data.remove('dept');
  199. data.remove('deptName');
  200. data.remove('usrNo');
  201. data.remove('applicantName');
  202. await DraftStorage.save(expenseDraftKey, data);
  203. return true;
  204. } catch (_) {
  205. return false;
  206. } finally {
  207. state = state.copyWith(isSubmitting: false);
  208. }
  209. }
  210. static Future<ExpenseModel?> loadDraft() async {
  211. final data = await DraftStorage.load(expenseDraftKey);
  212. if (data == null) return null;
  213. try {
  214. return ExpenseModel.fromJson(data);
  215. } catch (_) {
  216. return null;
  217. }
  218. }
  219. static Future<bool> hasDraft() => DraftStorage.has(expenseDraftKey);
  220. static Future<void> deleteDraft() => DraftStorage.delete(expenseDraftKey);
  221. }
  222. final expenseCreateProvider = StateNotifierProvider.autoDispose
  223. .family<ExpenseCreateController, ExpenseCreateState, String?>((
  224. ref,
  225. editId,
  226. ) {
  227. final api = ref.watch(expenseApiProvider);
  228. return ExpenseCreateController(api);
  229. });