subscription updates
parent
689460a78d
commit
d5745847bf
@ -0,0 +1,32 @@
|
||||
class BranchSelectionModel {
|
||||
int branchId;
|
||||
String branchName;
|
||||
bool isSelected;
|
||||
bool isOpend;
|
||||
List<UserSelectionModel> usersList;
|
||||
|
||||
BranchSelectionModel
|
||||
|
||||
({
|
||||
|
||||
required
|
||||
|
||||
this
|
||||
|
||||
.
|
||||
branchId,
|
||||
required this.branchName,
|
||||
required this.isSelected,
|
||||
this.isOpend = false,
|
||||
required this.usersList,
|
||||
});
|
||||
}
|
||||
|
||||
class UserSelectionModel {
|
||||
int userId;
|
||||
String userName;
|
||||
bool isSelected;
|
||||
|
||||
UserSelectionModel(
|
||||
{required this.userId, required this.userName, required this.isSelected});
|
||||
}
|
||||
@ -0,0 +1,165 @@
|
||||
import 'package:mc_common_app/api/api_client.dart';
|
||||
import 'package:mc_common_app/classes/app_state.dart';
|
||||
import 'package:mc_common_app/classes/consts.dart';
|
||||
import 'package:mc_common_app/config/dependencies.dart';
|
||||
import 'package:mc_common_app/models/general_models/generic_resp_model.dart';
|
||||
import 'package:mc_common_app/models/general_models/m_response.dart';
|
||||
import 'package:mc_common_app/models/provider_branches_models/branch_detail_model.dart';
|
||||
import 'package:mc_common_app/models/subscriptions_models/branch_user_selection_model.dart';
|
||||
import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart';
|
||||
import 'package:mc_common_app/models/user_models/branch_user.dart';
|
||||
|
||||
abstract class SubscriptionRepo {
|
||||
Future<SubscriptionModel> getAllSubscriptions(String? serviceProviderID);
|
||||
|
||||
Future<SubscriptionModel> getMySubscriptions(String? serviceProviderID);
|
||||
|
||||
Future<SubscriptionModel> getSubscriptionBySP(
|
||||
String? serviceProviderID, bool isRenew);
|
||||
|
||||
Future<MResponse> calculationUpgradePrice(
|
||||
String? serviceProviderID, String? newSubscription);
|
||||
|
||||
Future<MResponse> payForProviderSubscription(Map<String, dynamic> map);
|
||||
|
||||
Future<List<BranchSelectionModel>> getSPBranchUser_Get(
|
||||
Map<String, String> map);
|
||||
}
|
||||
|
||||
class SubscriptionRepoImp extends SubscriptionRepo {
|
||||
@override
|
||||
Future<SubscriptionModel> getAllSubscriptions(
|
||||
String? serviceProviderID) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
Map<String, String> queryParameters = {};
|
||||
if (serviceProviderID != null) {
|
||||
queryParameters = {
|
||||
"ID": serviceProviderID,
|
||||
};
|
||||
}
|
||||
|
||||
return await injector.get<ApiClient>().getJsonForObject(
|
||||
(json) => SubscriptionModel.fromJson(json),
|
||||
ApiConsts.getAllSubscriptions,
|
||||
token: t,
|
||||
queryParameters: queryParameters);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SubscriptionModel> getSubscriptionBySP(
|
||||
String? serviceProviderID, bool isRenew) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
Map<String, String> queryParameters = {};
|
||||
if (serviceProviderID != null) {
|
||||
queryParameters = {
|
||||
"ProviderID": serviceProviderID,
|
||||
"IsRenew": isRenew.toString(),
|
||||
};
|
||||
}
|
||||
|
||||
return await injector.get<ApiClient>().getJsonForObject(
|
||||
(json) => SubscriptionModel.fromJson(json),
|
||||
ApiConsts.getSubscriptionBySP,
|
||||
token: t,
|
||||
queryParameters: queryParameters,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MResponse> calculationUpgradePrice(
|
||||
String? serviceProviderID, String? newSubscription) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
Map<String, String> queryParameters = {};
|
||||
if (serviceProviderID != null) {
|
||||
queryParameters = {
|
||||
"ServiceProviderID": serviceProviderID,
|
||||
"NewSubscription": newSubscription!,
|
||||
};
|
||||
}
|
||||
|
||||
return await injector.get<ApiClient>().getJsonForObject(
|
||||
(json) => MResponse.fromJson(json),
|
||||
ApiConsts.calculationUpgradePrice,
|
||||
token: t,
|
||||
queryParameters: queryParameters,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<MResponse> payForProviderSubscription(Map<String, dynamic> map) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
|
||||
return await injector.get<ApiClient>().postJsonForObject(
|
||||
(json) => MResponse.fromJson(json),
|
||||
ApiConsts.payFortOrder_ProviderSubscription_Create,
|
||||
map,
|
||||
token: t,
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<BranchSelectionModel>> getSPBranchUser_Get(
|
||||
Map<String, String> map) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
|
||||
GenericRespModel genericRespModel =
|
||||
await injector.get<ApiClient>().getJsonForObject(
|
||||
(json) => GenericRespModel.fromJson(json),
|
||||
ApiConsts.getSPBranchUser_Get,
|
||||
token: t,
|
||||
queryParameters: map,
|
||||
);
|
||||
|
||||
List<BranchSelectionModel> branchList = [];
|
||||
|
||||
if (genericRespModel.data != null) {
|
||||
List<BranchDetailModel> branches = [];
|
||||
List<BranchUser> branchUsers = [];
|
||||
|
||||
// List<BranchUsersDatum>.from(json["data"]!.map((x) => BranchUsersDatum.fromJson(x))
|
||||
branches = List<BranchDetailModel>.from(genericRespModel.data["branches"]
|
||||
["data"]!
|
||||
.map((x) => BranchDetailModel.fromJson(x)));
|
||||
branchUsers = List<BranchUser>.from(genericRespModel.data["branchUsers"]
|
||||
["data"]!
|
||||
.map((x) => BranchUser.fromJson(x)));
|
||||
for (int i = 0; i < branches.length; i++) {
|
||||
List<UserSelectionModel> availableUsers = [];
|
||||
for (int j = 0; j < branchUsers.length; j++) {
|
||||
if (branches[i].id == branchUsers[j].serviceProviderBranchID) {
|
||||
availableUsers.add(UserSelectionModel(
|
||||
userId: branchUsers[j].id,
|
||||
userName:
|
||||
("${branchUsers[j].firstName ?? ""} ${branchUsers[j].lastName}"),
|
||||
isSelected: false));
|
||||
}
|
||||
}
|
||||
branchList.add(BranchSelectionModel(
|
||||
branchId: branches[i].id ?? 0,
|
||||
branchName: branches[i].branchName ?? "",
|
||||
isSelected: false,
|
||||
usersList: availableUsers));
|
||||
}
|
||||
}
|
||||
return branchList;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<SubscriptionModel> getMySubscriptions(
|
||||
String? serviceProviderID) async {
|
||||
String t = AppState().getUser.data!.accessToken ?? "";
|
||||
Map<String, String> queryParameters = {};
|
||||
if (serviceProviderID != null) {
|
||||
queryParameters = {
|
||||
"ID": serviceProviderID,
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
return await injector.get<ApiClient>().getJsonForObject(
|
||||
(json) => SubscriptionModel.fromJson(json),
|
||||
ApiConsts.getAllSubscriptions,
|
||||
token: t,
|
||||
queryParameters: queryParameters);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,182 @@
|
||||
import 'dart:convert';
|
||||
|
||||
|
||||
import 'package:mc_common_app/classes/app_state.dart';
|
||||
import 'package:mc_common_app/models/general_models/m_response.dart';
|
||||
import 'package:mc_common_app/models/subscriptions_models/branch_user_selection_model.dart';
|
||||
import 'package:mc_common_app/models/subscriptions_models/subscription_model.dart';
|
||||
import 'package:mc_common_app/utils/enums.dart';
|
||||
import 'package:mc_common_app/view_models/base_view_model.dart';
|
||||
import 'package:mc_common_app/widgets/dropdown/dropdow_field.dart';
|
||||
|
||||
import '../repositories/subscription_repo.dart';
|
||||
|
||||
class SubscriptionsVM extends BaseVM {
|
||||
final SubscriptionRepo subscriptionRepo;
|
||||
|
||||
SubscriptionsVM({required this.subscriptionRepo});
|
||||
|
||||
//All Subscriptions
|
||||
int selectedIndex = 0;
|
||||
late DropValue selectedMothlyTab;
|
||||
List<DropValue> monthlyTabs = [];
|
||||
late SubscriptionModel allSubscriptions;
|
||||
List<Subscription> tempSubscriptions = [];
|
||||
|
||||
//My Subscriptions
|
||||
|
||||
//All Subscriptions
|
||||
getAllAvailableSubscriptions(String? serviceProviderID) async {
|
||||
selectedIndex = 0;
|
||||
setState(ViewState.busy);
|
||||
allSubscriptions =
|
||||
await subscriptionRepo.getAllSubscriptions(serviceProviderID);
|
||||
if (allSubscriptions.messageStatus == 1) {
|
||||
monthlyTabs.clear();
|
||||
var idSet = <int>{};
|
||||
for (var d in allSubscriptions.data ?? []) {
|
||||
if (idSet.add(d.durationDays ?? 0)) {
|
||||
monthlyTabs.add(DropValue(
|
||||
d.durationDays, _convertDaysToMonths(d.durationDays ?? 0), ""));
|
||||
}
|
||||
}
|
||||
monthlyTabs.sort((a, b) => a.value.compareTo(b.value));
|
||||
selectedMothlyTab = monthlyTabs.first;
|
||||
filterSubscriptions();
|
||||
setState(ViewState.idle);
|
||||
} else {
|
||||
setState(ViewState.error);
|
||||
}
|
||||
}
|
||||
|
||||
getSubscriptionBySP(String serviceProviderID, bool isRenew) async {
|
||||
selectedIndex = 0;
|
||||
setState(ViewState.busy);
|
||||
allSubscriptions =
|
||||
await subscriptionRepo.getSubscriptionBySP(serviceProviderID, isRenew);
|
||||
if (allSubscriptions.messageStatus == 1) {
|
||||
monthlyTabs.clear();
|
||||
var idSet = <int>{};
|
||||
for (var d in allSubscriptions.data ?? []) {
|
||||
if (idSet.add(d.durationDays ?? 0)) {
|
||||
monthlyTabs.add(DropValue(
|
||||
d.durationDays, _convertDaysToMonths(d.durationDays ?? 0), ""));
|
||||
}
|
||||
}
|
||||
monthlyTabs.sort((a, b) => a.value.compareTo(b.value));
|
||||
selectedMothlyTab = monthlyTabs.first;
|
||||
filterSubscriptions();
|
||||
setState(ViewState.idle);
|
||||
} else {
|
||||
setState(ViewState.error);
|
||||
}
|
||||
}
|
||||
|
||||
String newPrice = "";
|
||||
|
||||
calculationUpgradePrice(
|
||||
String? serviceProviderID, String? newSubscription) async {
|
||||
setState(ViewState.busy);
|
||||
MResponse mResponse = await subscriptionRepo.calculationUpgradePrice(
|
||||
serviceProviderID, newSubscription);
|
||||
if (mResponse.messageStatus == 1) {
|
||||
setState(ViewState.idle);
|
||||
newPrice = mResponse.data.toString();
|
||||
} else {
|
||||
setState(ViewState.error);
|
||||
}
|
||||
}
|
||||
|
||||
Future<MResponse> payForSubscription(
|
||||
int subscriptionId, bool isStartNow, bool isReview, String amount,
|
||||
{bool isDegrade = false,
|
||||
List<int>? listOfBranches,
|
||||
List<int>? listOfUsers}) async {
|
||||
Map<String, dynamic> map;
|
||||
if (isDegrade) {
|
||||
map = {
|
||||
// "id": subscription.id.toString(),
|
||||
// "payFortOrderID": 0,
|
||||
"providerID":
|
||||
AppState().getUser.data?.userInfo?.providerId.toString() ?? "",
|
||||
"subscriptionID": subscriptionId.toString(),
|
||||
"isStartNow": isStartNow.toString(),
|
||||
"subscriptionAmount": amount,
|
||||
"isRenew": isReview.toString(),
|
||||
"listOfBranches": listOfBranches,
|
||||
"listOfUsers": listOfUsers
|
||||
};
|
||||
} else {
|
||||
map = {
|
||||
// "id": subscription.id.toString(),
|
||||
// "payFortOrderID": 0,
|
||||
"providerID":
|
||||
AppState().getUser.data?.userInfo?.providerId.toString() ?? "",
|
||||
"subscriptionID": subscriptionId.toString(),
|
||||
"isStartNow": isStartNow.toString(),
|
||||
"subscriptionAmount": amount,
|
||||
"isRenew": isReview.toString()
|
||||
// "listOfBranches": [],
|
||||
// "listOfUsers": []
|
||||
};
|
||||
}
|
||||
MResponse mResponse =
|
||||
await subscriptionRepo.payForProviderSubscription(map);
|
||||
return mResponse;
|
||||
}
|
||||
|
||||
List<BranchSelectionModel>? branchSelectionList;
|
||||
|
||||
getSPBranchUser_Get() async {
|
||||
branchSelectionList = null;
|
||||
Map<String, String> map = {
|
||||
// "id": subscription.id.toString(),
|
||||
// "payFortOrderID": 0,
|
||||
"providerID":
|
||||
AppState().getUser.data?.userInfo?.providerId.toString() ?? "",
|
||||
// "listOfBranches": [],
|
||||
// "listOfUsers": []
|
||||
};
|
||||
branchSelectionList = await subscriptionRepo.getSPBranchUser_Get(map);
|
||||
if (branchSelectionList!.isNotEmpty) {
|
||||
branchSelectionList!.first.isOpend = true;
|
||||
}
|
||||
setState(ViewState.idle);
|
||||
}
|
||||
|
||||
String _convertDaysToMonths(int days) {
|
||||
final int months = days ~/ 30;
|
||||
final int remainingDays = days % 30;
|
||||
|
||||
String _result = months > 0
|
||||
? '$months Month${months > 1 ? 's' : ''}${remainingDays > 0 ? ' & ' : ''}'
|
||||
: '';
|
||||
_result += remainingDays > 0
|
||||
? '$remainingDays Day${remainingDays > 1 ? 's' : ''}'
|
||||
: '';
|
||||
return _result;
|
||||
}
|
||||
|
||||
filterSubscriptions() {
|
||||
tempSubscriptions.clear();
|
||||
for (var element in allSubscriptions.data!) {
|
||||
if (selectedMothlyTab.id == element.durationDays) {
|
||||
tempSubscriptions.add(element);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//My Subscriptions
|
||||
getMySubscriptions(String? serviceProviderID) async {
|
||||
selectedIndex = 0;
|
||||
setState(ViewState.busy);
|
||||
// allSubscriptions = await subscriptionRepo.getAllSubscriptions(serviceProviderID);
|
||||
allSubscriptions = await subscriptionRepo.getMySubscriptions(serviceProviderID);
|
||||
if (allSubscriptions.messageStatus == 1) {
|
||||
// allSubscriptions.data!.sort((a, b) => a.value.compareTo(b.value));
|
||||
setState(ViewState.idle);
|
||||
} else {
|
||||
setState(ViewState.error);
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue