fetchProductVariant function
DeliveryDoseCriteria?
fetchProductVariant(
- ProjectCycleDelivery? currentDelivery,
- IndividualModel? individualModel,
- HouseholdModel? householdModel
Implementation
DeliveryDoseCriteria? fetchProductVariant(ProjectCycleDelivery? currentDelivery,
IndividualModel? individualModel, HouseholdModel? householdModel) {
if (currentDelivery != null) {
var individualAgeInMonths = 0;
var gender;
var roomCount;
var memberCount;
if (individualModel != null) {
final individualAge = DigitDateUtils.calculateAge(
DigitDateUtils.getFormattedDateToDateTime(
individualModel.dateOfBirth!,
) ??
DateTime.now(),
);
individualAgeInMonths = individualAge.years * 12 + individualAge.months;
gender = individualModel.gender?.index;
}
if (householdModel != null && householdModel.additionalFields != null) {
memberCount = householdModel.memberCount;
roomCount = int.tryParse(householdModel.additionalFields?.fields
.where((h) => h.key == AdditionalFieldsType.noOfRooms.toValue())
.firstOrNull
?.value
.toString() ??
'1')!;
}
final filteredCriteria = currentDelivery.doseCriteria?.where((criteria) {
final condition = criteria.condition;
if (condition != null) {
final conditions = condition.split('and');
List expressionParser = [];
for (var element in conditions) {
final expression = FormulaParser(
element,
{
'age': individualAgeInMonths,
if (gender != null) 'gender': gender,
if (memberCount != null) 'memberCount': memberCount,
if (roomCount != null) 'roomCount': roomCount
},
);
final error = expression.parse;
expressionParser.add(error["value"]);
}
return expressionParser.where((element) => element == true).length ==
conditions.length;
}
return false;
}).toList();
return (filteredCriteria ?? []).isNotEmpty ? filteredCriteria?.first : null;
}
return null;
}