expense_create_controller.dart 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. await _api.submit(state.expense.copyWith(status: 'pending').toJson());
  155. await DraftStorage.delete(expenseDraftKey);
  156. return true;
  157. } catch (_) {
  158. return false;
  159. } finally {
  160. state = state.copyWith(isSubmitting: false);
  161. }
  162. }
  163. Future<bool> saveDraft() async {
  164. state = state.copyWith(isSubmitting: true);
  165. try {
  166. final data = state.expense.toJson();
  167. data.remove('deptId');
  168. data.remove('deptName');
  169. data.remove('applicantId');
  170. data.remove('applicantName');
  171. await DraftStorage.save(expenseDraftKey, data);
  172. return true;
  173. } catch (_) {
  174. return false;
  175. } finally {
  176. state = state.copyWith(isSubmitting: false);
  177. }
  178. }
  179. static Future<ExpenseModel?> loadDraft() async {
  180. final data = await DraftStorage.load(expenseDraftKey);
  181. if (data == null) return null;
  182. try {
  183. return ExpenseModel.fromJson(data);
  184. } catch (_) {
  185. return null;
  186. }
  187. }
  188. static Future<bool> hasDraft() => DraftStorage.has(expenseDraftKey);
  189. static Future<void> deleteDraft() => DraftStorage.delete(expenseDraftKey);
  190. }
  191. final expenseCreateProvider = StateNotifierProvider.autoDispose
  192. .family<ExpenseCreateController, ExpenseCreateState, String?>((
  193. ref,
  194. editId,
  195. ) {
  196. final api = ref.watch(expenseApiProvider);
  197. return ExpenseCreateController(api);
  198. });