You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
432 lines
17 KiB
Dart
432 lines
17 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:flutter/material.dart';
|
|
import 'package:mohem_flutter_app/api/api_client.dart';
|
|
import 'package:mohem_flutter_app/app_state/app_state.dart';
|
|
import 'package:mohem_flutter_app/classes/consts.dart';
|
|
import 'package:mohem_flutter_app/models/offers_and_discounts/get_categories_list.dart';
|
|
import 'package:mohem_flutter_app/models/offers_and_discounts/get_offers_list.dart';
|
|
import 'package:mohem_flutter_app/models/offers_and_discounts/wala_plus_category_model.dart';
|
|
import 'package:mohem_flutter_app/models/offers_and_discounts/wala_plus_retailer_model.dart';
|
|
|
|
class OffersAndDiscountsApiClient {
|
|
static final OffersAndDiscountsApiClient _instance = OffersAndDiscountsApiClient._internal();
|
|
|
|
OffersAndDiscountsApiClient._internal();
|
|
|
|
factory OffersAndDiscountsApiClient() => _instance;
|
|
|
|
Future<List<GetCategoriesList>> getSaleCategories() async {
|
|
List<GetCategoriesList> getSaleCategoriesList = [];
|
|
|
|
String url = "${ApiConsts.cocRest}Mohemm_ITG_GetCategories";
|
|
Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgPageSize": 100, "ItgPageNo": 1, "ItgIsActive": true};
|
|
|
|
postParams.addAll(AppState().postParamsJson);
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
var body = json.decode(response['Mohemm_ITG_ResponseItem']);
|
|
|
|
GetCategoriesList getSaleCategoriesListObj = GetCategoriesList();
|
|
getSaleCategoriesListObj.id = 0;
|
|
getSaleCategoriesListObj.categoryNameEn = "All";
|
|
getSaleCategoriesListObj.categoryNameAr = "الجميع";
|
|
getSaleCategoriesListObj.isActive = true;
|
|
getSaleCategoriesListObj.content =
|
|
'<svg xmlns="http://www.w3.org/2000/svg" width="26.213" height="26.213" viewBox="0 0 26.213 26.213"> <g id="More_Select" data-name="More Select" transform="translate(-1 -1)"> <path id="Path_4860" data-name="Path 4860" d="M26.844,1H5.874A.874.874,0,0,0,5,1.874v.874H23.349A2.621,2.621,0,0,1,25.97,5.369V23.718h.874a.874.874,0,0,0,.874-.874V1.874A.874.874,0,0,0,26.844,1Z" transform="translate(-0.505)" fill="#2bb8a6"/> <path id="Path_4861" data-name="Path 4861" d="M22.844,5H1.874A.874.874,0,0,0,1,5.874v20.97a.874.874,0,0,0,.874.874h20.97a.874.874,0,0,0,.874-.874V5.874A.874.874,0,0,0,22.844,5ZM18.7,13.164,11.07,20.789a.874.874,0,0,1-1.236,0L6.022,16.977a.874.874,0,1,1,1.236-1.236l3.2,3.195,7.008-7.007A.874.874,0,1,1,18.7,13.165Z" transform="translate(0 -0.505)" fill="#125765"/> </g> </svg>';
|
|
|
|
getSaleCategoriesList.add(getSaleCategoriesListObj);
|
|
|
|
body['result']['data'].forEach((v) {
|
|
getSaleCategoriesList.add(GetCategoriesList.fromJson(v));
|
|
});
|
|
return getSaleCategoriesList;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
|
|
Future<List<OffersListModel>> getOffersList(int categoryID, int pageSize) async {
|
|
List<OffersListModel> getSaleCategoriesList = [];
|
|
|
|
String url = "${ApiConsts.cocRest}GetOfferDiscountsConfigData";
|
|
Map<String, dynamic> postParams = {"EmployeeNumber": AppState().memberInformationList?.eMPLOYEENUMBER, "ItgPageSize": pageSize, "ItgPageNo": 1, "ItgCategoryID": categoryID};
|
|
|
|
postParams.addAll(AppState().postParamsJson);
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
var body = json.decode(response['Mohemm_ITG_ResponseItem']);
|
|
|
|
var bodyData = body['result']['data'];
|
|
|
|
if (bodyData != null) {
|
|
bodyData.forEach((v) {
|
|
getSaleCategoriesList.add(OffersListModel.fromJson(v));
|
|
});
|
|
}
|
|
return getSaleCategoriesList;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
|
|
String _resolveUserName() {
|
|
String? pUserName = AppState().getUserName;
|
|
if (pUserName == null || pUserName.isEmpty) {
|
|
pUserName = AppState().postParamsObject?.userName ??
|
|
AppState().postParamsObject?.pUserName ??
|
|
AppState().memberInformationList?.eMPLOYEENUMBER;
|
|
}
|
|
return pUserName ?? "";
|
|
}
|
|
|
|
String _resolveDepNationalId() {
|
|
if (!ApiConsts.isDependetLogin) {
|
|
return "";
|
|
}
|
|
return AppState().getUserName ?? "";
|
|
}
|
|
|
|
String _resolveMobileNumber({String? overrideMobile}) {
|
|
if (overrideMobile != null && overrideMobile.trim().isNotEmpty) {
|
|
return overrideMobile.trim();
|
|
}
|
|
String? mobile = AppState().postParamsObject?.mobileNumber;
|
|
if (mobile != null && mobile.trim().isNotEmpty) {
|
|
return mobile.trim();
|
|
}
|
|
mobile = AppState().memberInformationList?.eMPLOYEEMOBILENUMBER;
|
|
if (mobile != null && mobile.trim().isNotEmpty) {
|
|
return mobile.trim();
|
|
}
|
|
mobile = AppState().walaplusDepInfoModel?.pEmpMobileNumber;
|
|
if (mobile != null && mobile.trim().isNotEmpty) {
|
|
return mobile.trim();
|
|
}
|
|
mobile = AppState().memberLoginList?.pMOBILENUMBER;
|
|
if (mobile != null && mobile.trim().isNotEmpty) {
|
|
return mobile.trim();
|
|
}
|
|
mobile = AppState().memberInformationList?.mobileNumberWithZipCode;
|
|
if (mobile != null && mobile.trim().isNotEmpty) {
|
|
return mobile.trim();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
|
|
Map<String, dynamic> _getBaseWalaPlusParams() {
|
|
Map<String, dynamic> base = {};
|
|
base.addAll(AppState().postParamsJson);
|
|
if (ApiConsts.isDependetLogin) {
|
|
String? token = AppState().postParamsObject?.tokenID ?? AppState().authenticationTokenID;
|
|
if (token != null && token.isNotEmpty) {
|
|
base["TokenID"] = token;
|
|
}
|
|
String? logInToken = AppState().postParamsObject?.logInTokenID;
|
|
if (logInToken != null && logInToken.isNotEmpty) {
|
|
base["LogInTokenID"] = logInToken;
|
|
}
|
|
}
|
|
_cleanWalaPlusParams(base);
|
|
return base;
|
|
}
|
|
|
|
void _cleanWalaPlusParams(Map<String, dynamic> map) {
|
|
map.remove("P_EMAIL_ADDRESS");
|
|
map.remove("P_SESSION_ID");
|
|
map.remove("PayrollCodeStr");
|
|
map.remove("LegislationCodeStr");
|
|
map.remove("P_SELECTED_EMPLOYEE_NUMBER");
|
|
map.removeWhere((key, value) => value == null);
|
|
}
|
|
|
|
Future<List<WalaPlusCategoryModel>> getWalaPlusCategories({BuildContext? context, String? mobileNumber}) async {
|
|
String url = ApiConsts.walaPlusCategories;
|
|
String pUserName = _resolveUserName();
|
|
String pMobileNumber = _resolveMobileNumber(overrideMobile: mobileNumber);
|
|
String pDepNationalId = _resolveDepNationalId();
|
|
|
|
Map<String, dynamic> postParams = _getBaseWalaPlusParams();
|
|
postParams["UserName"] = pUserName;
|
|
postParams["P_USER_NAME"] = pUserName;
|
|
postParams["P_DEP_NATIONAL_ID"] = pDepNationalId;
|
|
if (pMobileNumber.isNotEmpty) {
|
|
postParams["MobileNumber"] = pMobileNumber;
|
|
}
|
|
postParams["MemberType"] = ApiConsts.isDependetLogin ? 2 : 1;
|
|
postParams["CompanyID"] = 1;
|
|
_cleanWalaPlusParams(postParams);
|
|
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
List<WalaPlusCategoryModel> categories = [];
|
|
if (response != null) {
|
|
dynamic cats = response['WalaPlus_Categories'] ??
|
|
response['walaPlus_Categories'] ??
|
|
response['WalaPlusCategories'] ??
|
|
response['result'];
|
|
if (cats is String) {
|
|
try {
|
|
cats = json.decode(cats);
|
|
} catch (e) {
|
|
debugPrint("Error decoding WalaPlus_Categories: $e");
|
|
}
|
|
}
|
|
if (cats is List) {
|
|
cats.forEach((v) {
|
|
if (v is Map<String, dynamic>) {
|
|
categories.add(WalaPlusCategoryModel.fromJson(v));
|
|
} else if (v is Map) {
|
|
categories.add(WalaPlusCategoryModel.fromJson(Map<String, dynamic>.from(v)));
|
|
}
|
|
});
|
|
}
|
|
}
|
|
return categories;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
|
|
Future<WalaPlusRetailersResponse?> getWalaPlusRetailers({
|
|
String? categoryId,
|
|
int page = 1,
|
|
int perPage = 10,
|
|
String? geoLocation,
|
|
BuildContext? context,
|
|
String? mobileNumber,
|
|
}) async {
|
|
String url = ApiConsts.walaPlusRetailers;
|
|
String pUserName = _resolveUserName();
|
|
String pMobileNumber = _resolveMobileNumber(overrideMobile: mobileNumber);
|
|
String pDepNationalId = _resolveDepNationalId();
|
|
String effectiveCatId = (categoryId == "all" || categoryId == "0" || categoryId == null) ? "" : categoryId;
|
|
|
|
Map<String, dynamic> postParams = _getBaseWalaPlusParams();
|
|
postParams["UserName"] = pUserName;
|
|
postParams["P_USER_NAME"] = pUserName;
|
|
postParams["P_DEP_NATIONAL_ID"] = pDepNationalId;
|
|
if (pMobileNumber.isNotEmpty) {
|
|
postParams["MobileNumber"] = pMobileNumber;
|
|
}
|
|
postParams["MemberType"] = ApiConsts.isDependetLogin ? 2 : 1;
|
|
postParams["CompanyID"] = 1;
|
|
postParams["WP_CategoryID"] = effectiveCatId;
|
|
postParams["WP_Page"] = page;
|
|
postParams["WP_Per_page"] = perPage;
|
|
|
|
if (geoLocation != null && geoLocation.isNotEmpty) {
|
|
postParams["WP_Geo_Location"] = geoLocation;
|
|
}
|
|
_cleanWalaPlusParams(postParams);
|
|
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
if (response != null) {
|
|
dynamic val = response['WalaPlus_Retailers'] ??
|
|
response['walaPlus_Retailers'] ??
|
|
response['WalaPlusRetailers'] ??
|
|
response['WP_Retailers'] ??
|
|
response['result'];
|
|
if (val is String) {
|
|
try {
|
|
val = json.decode(val);
|
|
} catch (e) {
|
|
debugPrint("Error decoding WalaPlus_Retailers: $e");
|
|
}
|
|
}
|
|
if (val is Map<String, dynamic>) {
|
|
return WalaPlusRetailersResponse.fromJson(val);
|
|
} else if (val is Map) {
|
|
return WalaPlusRetailersResponse.fromJson(Map<String, dynamic>.from(val));
|
|
} else if (val is List) {
|
|
List<WalaPlusRetailerModel> retailers = [];
|
|
for (var v in val) {
|
|
if (v is Map<String, dynamic>) {
|
|
retailers.add(WalaPlusRetailerModel.fromJson(v));
|
|
} else if (v is Map) {
|
|
retailers.add(WalaPlusRetailerModel.fromJson(Map<String, dynamic>.from(v)));
|
|
}
|
|
}
|
|
return WalaPlusRetailersResponse(retailers: retailers, page: 1, pagesCount: 1, perPage: retailers.length);
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
|
|
Future<WalaPlusRetailerModel?> getWalaPlusRetailerById({
|
|
required String retailerId,
|
|
BuildContext? context,
|
|
String? mobileNumber,
|
|
}) async {
|
|
String url = ApiConsts.walaPlusRetailersByID;
|
|
String pUserName = _resolveUserName();
|
|
String pMobileNumber = _resolveMobileNumber(overrideMobile: mobileNumber);
|
|
String pDepNationalId = _resolveDepNationalId();
|
|
|
|
Map<String, dynamic> postParams = _getBaseWalaPlusParams();
|
|
postParams["WP_RetailerID"] = retailerId;
|
|
postParams["RetailerID"] = retailerId;
|
|
postParams["UserName"] = pUserName;
|
|
postParams["P_USER_NAME"] = pUserName;
|
|
postParams["P_DEP_NATIONAL_ID"] = pDepNationalId;
|
|
if (pMobileNumber.isNotEmpty) {
|
|
postParams["MobileNumber"] = pMobileNumber;
|
|
}
|
|
postParams["MemberType"] = ApiConsts.isDependetLogin ? 2 : 1;
|
|
postParams["CompanyID"] = 1;
|
|
_cleanWalaPlusParams(postParams);
|
|
|
|
debugPrint("getWalaPlusRetailerById: calling $url with WP_RetailerID: $retailerId");
|
|
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
if (response != null) {
|
|
debugPrint("getWalaPlusRetailerById response keys: ${response is Map ? response.keys.toList() : 'non-map'}");
|
|
dynamic val = response['WalaPlus_RetailersByID'] ??
|
|
response['walaPlus_RetailersByID'] ??
|
|
response['WalaPlusRetailersByID'] ??
|
|
response['WP_RetailersByID'] ??
|
|
response['result'];
|
|
if (val is String) {
|
|
try {
|
|
val = json.decode(val);
|
|
} catch (e) {
|
|
debugPrint("Error decoding WalaPlus_RetailersByID JSON string: $e");
|
|
}
|
|
}
|
|
WalaPlusRetailerModel? model;
|
|
if (val is List && val.isNotEmpty) {
|
|
var item = val.first;
|
|
if (item is Map<String, dynamic>) {
|
|
model = WalaPlusRetailerModel.fromJson(item);
|
|
} else if (item is Map) {
|
|
model = WalaPlusRetailerModel.fromJson(Map<String, dynamic>.from(item));
|
|
}
|
|
} else if (val is Map) {
|
|
Map<String, dynamic> map = (val is Map<String, dynamic>) ? val : Map<String, dynamic>.from(val);
|
|
if (map.containsKey('retailer')) {
|
|
var byIdResponse = WalaPlusRetailersByIDResponse.fromJson(map);
|
|
model = byIdResponse.primaryRetailer ?? WalaPlusRetailerModel.fromJson(map);
|
|
} else {
|
|
model = WalaPlusRetailerModel.fromJson(map);
|
|
}
|
|
if ((model?.offers == null || model!.offers!.isEmpty) && (map.containsKey('offers') || map.containsKey('Offers'))) {
|
|
model?.offers = WalaPlusRetailerModel.parseOffersList(map['offers'] ?? map['Offers']);
|
|
}
|
|
if ((model?.locations == null || model!.locations!.isEmpty) &&
|
|
(map.containsKey('locations') || map.containsKey('Locations') || map.containsKey('location') || map.containsKey('Location'))) {
|
|
model?.locations = WalaPlusRetailerModel.parseLocationsList(
|
|
map['locations'] ?? map['Locations'] ?? map['location'] ?? map['Location'],
|
|
);
|
|
}
|
|
} else if (response is Map<String, dynamic> && response.containsKey('name')) {
|
|
model = WalaPlusRetailerModel.fromJson(response);
|
|
}
|
|
|
|
if (model != null) {
|
|
if (response is Map) {
|
|
if ((model.offers == null || model.offers!.isEmpty) && (response.containsKey('offers') || response.containsKey('Offers'))) {
|
|
model.offers = WalaPlusRetailerModel.parseOffersList(response['offers'] ?? response['Offers']);
|
|
}
|
|
if ((model.locations == null || model.locations!.isEmpty) &&
|
|
(response.containsKey('locations') || response.containsKey('Locations') || response.containsKey('location') || response.containsKey('Location'))) {
|
|
model.locations = WalaPlusRetailerModel.parseLocationsList(
|
|
response['locations'] ?? response['Locations'] ?? response['location'] ?? response['Location'],
|
|
);
|
|
}
|
|
}
|
|
debugPrint("getWalaPlusRetailerById: parsed retailer '${model.name}', offers: ${model.offers?.length ?? 0}, locations: ${model.locations?.length ?? 0}");
|
|
return model;
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
|
|
String _resolveUserReference({String? overrideUserReference}) {
|
|
if (overrideUserReference != null && overrideUserReference.trim().isNotEmpty) {
|
|
return overrideUserReference.trim();
|
|
}
|
|
String? ref = AppState().memberInformationList?.eMPLOYEENUMBER?.toString();
|
|
if (ref != null && ref.trim().isNotEmpty) {
|
|
return ref.trim();
|
|
}
|
|
String? pUserName = AppState().getUserName;
|
|
if (pUserName != null && pUserName.trim().isNotEmpty) {
|
|
return pUserName.trim();
|
|
}
|
|
return "";
|
|
}
|
|
|
|
Future<Map<String, dynamic>?> redeemWalaPlusOffer({
|
|
required String offerId,
|
|
String? offerType,
|
|
String? userReference,
|
|
BuildContext? context,
|
|
}) async {
|
|
String url = ApiConsts.walaPlusOfferRedeem;
|
|
String pUserName = _resolveUserName();
|
|
String pUserReference = _resolveUserReference(overrideUserReference: userReference);
|
|
|
|
Map<String, dynamic> postParams = _getBaseWalaPlusParams();
|
|
postParams["WP_User_Reference"] = pUserReference;
|
|
postParams["WP_Offer_Id"] = offerId;
|
|
postParams["WP_Offer_Type"] = (offerType != null && offerType.trim().isNotEmpty) ? offerType.trim() : "promo";
|
|
postParams["UserName"] = pUserName;
|
|
postParams["P_USER_NAME"] = pUserName;
|
|
|
|
if (ApiConsts.isDependetLogin) {
|
|
String pDepNationalId = _resolveDepNationalId();
|
|
if (pDepNationalId.isNotEmpty) {
|
|
postParams["P_DEP_NATIONAL_ID"] = pDepNationalId;
|
|
}
|
|
postParams["MemberType"] = 2;
|
|
} else {
|
|
postParams["P_DEP_NATIONAL_ID"] = "";
|
|
postParams["MemberType"] = 1;
|
|
}
|
|
_cleanWalaPlusParams(postParams);
|
|
|
|
debugPrint("redeemWalaPlusOffer calling $url with params: $postParams");
|
|
|
|
return await ApiClient().postJsonForObject(
|
|
(response) {
|
|
if (response != null) {
|
|
debugPrint("redeemWalaPlusOffer response: $response");
|
|
dynamic val = response['WalaPlus_OfferRedeem'] ??
|
|
response['walaPlus_OfferRedeem'] ??
|
|
response['WalaPlusOfferRedeem'] ??
|
|
response['result'];
|
|
if (val is String) {
|
|
try {
|
|
val = json.decode(val);
|
|
} catch (e) {
|
|
debugPrint("Error decoding WalaPlus_OfferRedeem: $e");
|
|
}
|
|
}
|
|
if (val is Map<String, dynamic>) {
|
|
return val;
|
|
} else if (val is Map) {
|
|
return Map<String, dynamic>.from(val);
|
|
}
|
|
}
|
|
return null;
|
|
},
|
|
url,
|
|
postParams,
|
|
);
|
|
}
|
|
}
|