expense_create_controller.dart 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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(expense: draft);
  69. }
  70. void reset() {
  71. state = ExpenseCreateState(
  72. expense: ExpenseModel(
  73. id: '',
  74. expenseNo: '',
  75. createTime: DateTime.now(),
  76. updateTime: DateTime.now(),
  77. ),
  78. );
  79. }
  80. void updatePurpose(String purpose) {
  81. state = state.copyWith(expense: state.expense.copyWith(purpose: purpose));
  82. }
  83. ExpenseCreateState get currentState => state;
  84. void updateAttachments(List<String> paths) {
  85. state = state.copyWith(expense: state.expense.copyWith(attachments: paths));
  86. }
  87. void updatePaymentMethod(String method) {
  88. state = state.copyWith(
  89. expense: state.expense.copyWith(paymentMethod: method),
  90. );
  91. }
  92. void updateRemark(String remark) {
  93. state = state.copyWith(expense: state.expense.copyWith(remark: remark));
  94. }
  95. void setGenerateVoucher(bool value) {
  96. state = state.copyWith(
  97. expense: state.expense.copyWith(isGenerateVoucher: value),
  98. );
  99. }
  100. void updateDept(String deptId, String deptName) {
  101. state = state.copyWith(
  102. expense: state.expense.copyWith(deptId: deptId, deptName: deptName),
  103. );
  104. }
  105. void addDetail(ExpenseDetailModel detail) {
  106. final details = [...state.expense.details, detail];
  107. state = state.copyWith(expense: state.expense.copyWith(details: details));
  108. }
  109. void removeDetail(int index) {
  110. final details = [...state.expense.details]..removeAt(index);
  111. state = state.copyWith(expense: state.expense.copyWith(details: details));
  112. }
  113. void removeDetailsByAeNo(String aeNo) {
  114. final details = state.expense.details.where((d) => d.aeNo != aeNo).toList();
  115. state = state.copyWith(expense: state.expense.copyWith(details: details));
  116. }
  117. void updateDetail(int index, ExpenseDetailModel detail) {
  118. final details = [...state.expense.details];
  119. details[index] = detail;
  120. state = state.copyWith(expense: state.expense.copyWith(details: details));
  121. }
  122. void recalculateAmount() {
  123. var totalAmount = 0.0;
  124. var approvedAmount = 0.0;
  125. for (final d in state.expense.details) {
  126. totalAmount += d.totalAmount;
  127. approvedAmount += d.approvedAmount;
  128. }
  129. state = state.copyWith(
  130. expense: state.expense.copyWith(
  131. totalAmount: totalAmount,
  132. approvedAmount: approvedAmount,
  133. ),
  134. );
  135. }
  136. Future<bool> submit() async {
  137. state = state.copyWith(isSubmitting: true);
  138. try {
  139. await _api.submit(state.expense.copyWith(status: 'pending').toJson());
  140. await DraftStorage.delete(expenseDraftKey);
  141. return true;
  142. } catch (_) {
  143. return false;
  144. } finally {
  145. state = state.copyWith(isSubmitting: false);
  146. }
  147. }
  148. Future<bool> saveDraft() async {
  149. state = state.copyWith(isSubmitting: true);
  150. try {
  151. await DraftStorage.save(expenseDraftKey, state.expense.toJson());
  152. return true;
  153. } catch (_) {
  154. return false;
  155. } finally {
  156. state = state.copyWith(isSubmitting: false);
  157. }
  158. }
  159. static Future<ExpenseModel?> loadDraft() async {
  160. final data = await DraftStorage.load(expenseDraftKey);
  161. if (data == null) return null;
  162. try {
  163. return ExpenseModel.fromJson(data);
  164. } catch (_) {
  165. return null;
  166. }
  167. }
  168. static Future<bool> hasDraft() => DraftStorage.has(expenseDraftKey);
  169. static Future<void> deleteDraft() => DraftStorage.delete(expenseDraftKey);
  170. }
  171. final expenseCreateProvider = StateNotifierProvider.autoDispose
  172. .family<ExpenseCreateController, ExpenseCreateState, String?>((
  173. ref,
  174. editId,
  175. ) {
  176. final api = ref.watch(expenseApiProvider);
  177. return ExpenseCreateController(api);
  178. });