Files
rasadyar_application/packages/chicken/lib/presentation/pages/steward/segmentation/logic.dart

310 lines
9.7 KiB
Dart
Raw Normal View History

2025-07-13 16:17:17 +03:30
import 'package:flutter/cupertino.dart';
2025-07-15 09:03:11 +03:30
import 'package:flutter/material.dart';
import 'package:rasadyar_chicken/data/models/response/broadcast_price/broadcast_price.dart';
2025-07-21 09:16:13 +03:30
import 'package:rasadyar_chicken/data/models/response/guild/guild_model.dart';
2025-07-13 16:17:17 +03:30
import 'package:rasadyar_chicken/data/models/response/roles_products/roles_products.dart';
import 'package:rasadyar_chicken/data/models/response/segmentation_model/segmentation_model.dart';
2025-11-02 17:27:45 +03:30
import 'package:rasadyar_chicken/data/models/response/steward_remain_weight/steward_remain_weight.dart';
import 'package:rasadyar_chicken/presentation/pages/steward/root/logic.dart';
2025-07-13 16:17:17 +03:30
import 'package:rasadyar_chicken/presentation/utils/utils.dart';
import 'package:rasadyar_core/core.dart';
class SegmentationLogic extends GetxController {
StewardRootLogic rootLogic = Get.find<StewardRootLogic>();
2025-07-13 16:17:17 +03:30
RxBool isLoadingMoreAllocationsMade = false.obs;
RxInt currentPage = 1.obs;
late List<String> routesName;
RxInt selectedSegmentIndex = 0.obs;
RxBool isExpanded = false.obs;
2025-10-12 12:48:20 +03:30
RxInt expandedListIndex = (-1).obs;
2025-07-13 16:17:17 +03:30
Rx<Jalali> fromDateFilter = Jalali.now().obs;
Rx<Jalali> toDateFilter = Jalali.now().obs;
RxnString searchedValue = RxnString();
2025-10-06 10:57:46 +03:30
RxInt segmentType = 1.obs;
RxInt priceType = 2.obs;
2025-11-02 15:45:31 +03:30
RxInt quotaType = 2.obs;
2025-07-13 16:17:17 +03:30
GlobalKey<FormState> formKey = GlobalKey<FormState>();
2025-07-14 12:35:12 +03:30
TextEditingController weightController = TextEditingController(text: '0');
RxBool isSubmitButtonEnabled = false.obs;
2025-07-21 09:16:13 +03:30
Rxn<GuildModel> selectedGuildModel = Rxn<GuildModel>();
2025-07-13 16:17:17 +03:30
Rxn<ProductModel> selectedProduct = Rxn<ProductModel>();
Rxn<SegmentationModel> selectedSegment = Rxn<SegmentationModel>();
Rxn<BroadcastPrice> broadcastPrice = Rxn<BroadcastPrice>();
2025-07-13 16:17:17 +03:30
Rx<Resource<PaginationModel<SegmentationModel>>> segmentationList =
Resource<PaginationModel<SegmentationModel>>.loading().obs;
2025-07-21 09:16:13 +03:30
RxList<GuildModel> guildsModel = <GuildModel>[].obs;
Rx<Jalali> saleDate = Jalali.now().obs;
RxInt weight = 0.obs;
Rxn<Jalali> productionDate = Rxn(null);
Rxn<int> remainingStock = Rxn(null);
2025-11-02 15:45:31 +03:30
Map<String, DayData> freeProductionDateData = {};
Map<String, DayData> governmentalProductionDateData = {};
2025-07-13 16:17:17 +03:30
@override
void onInit() {
super.onInit();
routesName = ['قطعه‌بندی'].toList();
2025-07-14 12:35:12 +03:30
once(rootLogic.rolesProductsModel, (callback) => selectedProduct.value = callback.first);
2025-07-13 16:17:17 +03:30
getAllSegmentation();
2025-07-21 09:16:13 +03:30
getGuilds();
2025-11-02 15:45:31 +03:30
ever(quotaType, (_) {
remainingStock.value = null;
productionDate.value = null;
});
2025-11-02 15:45:31 +03:30
_updateGovernmentalProductionDateData();
_updateFreeProductionDateData();
2025-11-02 17:27:45 +03:30
ever(rootLogic.stewardRemainWeight, (callback) {
2025-11-02 15:45:31 +03:30
_updateGovernmentalProductionDateData();
_updateFreeProductionDateData();
});
}
void _updateGovernmentalProductionDateData() {
2025-11-02 17:27:45 +03:30
List<RemainWeightDay> dates = rootLogic.stewardRemainWeight.value?.governmental ?? [];
2025-11-02 15:45:31 +03:30
governmentalProductionDateData = {
2025-11-02 17:27:45 +03:30
for (var element in dates)
element.day.toString().toJalali.formatCompactDate(): DayData(
value: element.amount?.toInt(),
),
2025-11-02 15:45:31 +03:30
};
}
void _updateFreeProductionDateData() {
2025-11-02 17:27:45 +03:30
var dates = rootLogic.stewardRemainWeight.value?.free ?? [];
2025-11-02 15:45:31 +03:30
freeProductionDateData = {
2025-11-02 17:27:45 +03:30
for (var element in dates)
element.day.toString().toJalali.formatCompactDate(): DayData(
value: element.amount?.toInt(),
),
2025-11-02 15:45:31 +03:30
};
2025-07-13 16:17:17 +03:30
}
@override
void onReady() {
super.onReady();
setUpListener();
}
void setSearchValue(String? value) {
searchedValue.value = value?.trim();
}
void setUpListener() {
debounce(
searchedValue,
(callback) => getAllSegmentation(),
time: Duration(milliseconds: timeDebounce),
);
2025-11-02 15:45:31 +03:30
everAll([selectedSegment, quotaType, priceType], (_) {
2025-07-14 12:35:12 +03:30
validateForm();
});
weightController.addListener(() => validateForm());
2025-07-13 16:17:17 +03:30
}
void setEditData(SegmentationModel item) {
selectedSegment.value = item;
weightController.text = item.weight.toString();
}
void clearForm() {
2025-07-14 12:35:12 +03:30
weightController.text = '0';
2025-07-13 16:17:17 +03:30
selectedSegment.value = null;
2025-07-21 12:04:43 +03:30
selectedGuildModel.value = null;
productionDate.value = null;
2025-10-06 10:57:46 +03:30
segmentType.value = 1;
priceType.value = 2;
2025-10-06 10:57:46 +03:30
quotaType.value = 1;
remainingStock.value = null;
2025-07-13 16:17:17 +03:30
}
2025-07-14 12:35:12 +03:30
void validateForm() {
var weight = int.tryParse(weightController.text.trim().clearComma) ?? 0;
var hasWeight = (remainingStock.value ?? 0) > weight;
2025-07-16 18:31:47 +03:30
isSubmitButtonEnabled.value =
2025-07-21 12:04:43 +03:30
selectedProduct.value != null &&
weightController.text.isNotEmpty &&
2025-11-02 15:45:31 +03:30
hasWeight &&
productionDate.value != null &&
weight > 0 &&
2025-10-12 12:48:20 +03:30
(segmentType.value == 1 || (segmentType.value == 2 && selectedGuildModel.value != null));
2025-07-14 12:35:12 +03:30
}
2025-07-13 16:17:17 +03:30
Future<void> getAllSegmentation([bool isLoadingMore = false]) async {
if (isLoadingMore) {
isLoadingMoreAllocationsMade.value = true;
} else {
segmentationList.value = Resource<PaginationModel<SegmentationModel>>.loading();
}
if (searchedValue.value != null &&
searchedValue.value!.trim().isNotEmpty &&
currentPage.value > 1) {
currentPage.value = 1; // Reset to first page if search value is set
}
await safeCall(
showError: true,
2025-07-13 16:17:17 +03:30
call: () async => await rootLogic.chickenRepository.getSegmentation(
token: rootLogic.tokenService.accessToken.value!,
queryParameters: buildQueryParams(
pageSize: 20,
page: currentPage.value,
search: 'filter',
role: 'Steward',
value: searchedValue.value,
fromDate: fromDateFilter.value.toDateTime(),
toDate: toDateFilter.value.toDateTime(),
),
),
onSuccess: (result) {
if ((result?.count ?? 0) == 0) {
segmentationList.value = Resource<PaginationModel<SegmentationModel>>.empty();
} else {
segmentationList.value = Resource<PaginationModel<SegmentationModel>>.success(
PaginationModel<SegmentationModel>(
count: result?.count ?? 0,
next: result?.next,
previous: result?.previous,
results: [
...(segmentationList.value.data?.results ?? []),
...(result?.results ?? []),
],
),
);
isLoadingMoreAllocationsMade.value = false;
}
},
);
}
Future<void> deleteSegmentation(String key) async {
await safeCall(
showError: true,
showSuccess: true,
2025-07-13 16:17:17 +03:30
call: () => rootLogic.chickenRepository.deleteSegmentation(
token: rootLogic.tokenService.accessToken.value!,
key: key,
),
);
}
Future<bool> editSegment() async {
var res = true;
safeCall(
2025-07-15 09:03:11 +03:30
showError: true,
2025-07-13 16:17:17 +03:30
call: () async => await rootLogic.chickenRepository.editSegmentation(
token: rootLogic.tokenService.accessToken.value!,
model: SegmentationModel(
key: selectedSegment.value?.key,
2025-07-17 15:44:40 +03:30
weight: int.tryParse(weightController.text.clearComma) ?? 0,
2025-11-02 15:45:31 +03:30
productionDate: productionDate.value?.toDateTime().formattedDashedGregorian,
2025-07-13 16:17:17 +03:30
),
),
onSuccess: (result) {
res = true;
2025-10-12 12:48:20 +03:30
onRefresh();
2025-07-13 16:17:17 +03:30
},
onError: (error, stacktrace) {
res = false;
},
);
return res;
}
Future<bool> createSegment() async {
var res = true;
2025-07-17 15:44:40 +03:30
SegmentationModel segmentationModel = SegmentationModel(
productKey: selectedProduct.value?.key,
weight: int.tryParse(weightController.text.clearComma) ?? 0,
saleType: priceType.value == 1 ? 'governmental' : 'free',
2025-10-06 10:57:46 +03:30
quota: quotaType.value == 1 ? 'governmental' : 'free',
2025-07-17 15:44:40 +03:30
);
2025-10-06 10:57:46 +03:30
if (segmentType.value == 2) {
2025-07-21 12:04:43 +03:30
segmentationModel = segmentationModel.copyWith(guildKey: selectedGuildModel.value?.key);
2025-07-21 09:32:29 +03:30
}
2025-11-02 15:45:31 +03:30
segmentationModel = segmentationModel.copyWith(
productionDate: productionDate.value?.toDateTime().formattedDashedGregorian,
);
2025-07-21 12:04:43 +03:30
await safeCall(
showError: true,
2025-07-13 16:17:17 +03:30
call: () async => await rootLogic.chickenRepository.createSegmentation(
token: rootLogic.tokenService.accessToken.value!,
2025-07-17 15:44:40 +03:30
model: segmentationModel,
2025-07-13 16:17:17 +03:30
),
2025-11-02 15:45:31 +03:30
onSuccess: (result) async {
2025-07-13 16:17:17 +03:30
res = true;
2025-11-02 15:45:31 +03:30
isSubmitButtonEnabled.value = false;
2025-10-12 12:48:20 +03:30
onRefresh();
2025-11-02 15:45:31 +03:30
Future.delayed(
Duration(seconds: 1),
() => defaultShowSuccessMessage("قطعه‌بندی با موفقیت ثبت شد!"),
);
Get.back();
2025-07-13 16:17:17 +03:30
},
onError: (error, stacktrace) {
res = false;
},
);
return res;
}
2025-07-21 09:16:13 +03:30
Future<void> getGuilds() async {
safeCall(
2025-07-21 09:32:29 +03:30
call: () async => await rootLogic.chickenRepository.getGuilds(
2025-07-21 09:16:13 +03:30
token: rootLogic.tokenService.accessToken.value!,
2025-07-21 09:32:29 +03:30
queryParameters: buildQueryParams(queryParams: {'all': true}, role: 'Steward'),
2025-07-21 09:16:13 +03:30
),
onSuccess: (result) {
if (result != null) {
guildsModel.clear();
guildsModel.addAll(result);
}
},
onError: (error, stacktrace) {},
);
}
2025-10-07 14:21:09 +03:30
Future<void> onRefresh() async {
2025-10-12 12:48:20 +03:30
toggleExpansion();
2025-10-07 14:21:09 +03:30
currentPage.value = 1;
await rootLogic.onRefresh();
2025-10-07 14:21:09 +03:30
await getAllSegmentation();
await getBroadcastPrice();
2025-11-02 15:45:31 +03:30
_updateFreeProductionDateData();
_updateGovernmentalProductionDateData();
2025-10-07 14:21:09 +03:30
}
2025-10-12 12:48:20 +03:30
Future<void> getBroadcastPrice() async {
safeCall(
call: () async => await rootLogic.chickenRepository.getBroadcastPrice(
token: rootLogic.tokenService.accessToken.value!,
),
onSuccess: (result) {
broadcastPrice.value = result;
if (broadcastPrice.value?.active == true) {
priceType.value = 2;
}
},
onError: (error, stacktrace) {},
);
}
2025-10-12 12:48:20 +03:30
void toggleExpansion({int? index}) {
if (expandedListIndex.value == index || index == null) {
expandedListIndex.value = -1;
} else {
expandedListIndex.value = index;
}
}
2025-07-13 16:17:17 +03:30
}