2099 lines
63 KiB
Dart
2099 lines
63 KiB
Dart
import 'package:file_picker/file_picker.dart';
|
||
import 'package:flutter/foundation.dart';
|
||
import 'package:flutter/material.dart';
|
||
import 'package:go_router/go_router.dart';
|
||
import 'package:http/http.dart' as http;
|
||
import 'package:nhance_partner/data/services/auth_service.dart';
|
||
import 'package:shared_preferences/shared_preferences.dart';
|
||
import 'dart:convert';
|
||
import '../../data/utils/toastNotification.dart';
|
||
import '../../presentation/screens/staff/enquiry_single_page/model/dropdown_option.dart';
|
||
import '../../presentation/screens/staff/enquiry_single_page/model/enquiry_model.dart';
|
||
import '../config/env.dart';
|
||
import 'package:universal_html/html.dart' as html;
|
||
import 'package:http/http.dart' as http;
|
||
|
||
import '../routing/routes.dart';
|
||
|
||
class ApiService {
|
||
late final BuildContext context;
|
||
|
||
String? _token;
|
||
|
||
Future<void> _initializeToken() async {
|
||
_token = await AuthService.getToken();
|
||
print("APISERTOKEN - $_token");
|
||
}
|
||
|
||
Future<void> clearLocalStorageAndRedirect() async {
|
||
// final prefs = await SharedPreferences.getInstance();
|
||
// await prefs.clear();
|
||
// Assuming you have access to the context
|
||
ToastHelper.showErrorToast(context, 'Session Out');
|
||
// Navigator.pushNamed(context, 'login');
|
||
AuthService.clearToken();
|
||
context.go(AppRoutes.login);
|
||
}
|
||
|
||
Future<Map<String, dynamic>> _makeGetRequest(
|
||
Uri url,
|
||
Map<String, String> headers,
|
||
) async {
|
||
final response = await http.get(url, headers: headers);
|
||
return _handleResponse(response);
|
||
}
|
||
|
||
Future<http.Response> _makeGethttpRequest(
|
||
Uri url,
|
||
Map<String, String> headers,
|
||
) async {
|
||
final response = await http.get(url, headers: headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
return response; // return full http.Response, not Map
|
||
} else if (response.statusCode == 401) {
|
||
await clearLocalStorageAndRedirect();
|
||
return http.Response('Forbidden', 401);
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
return http.Response('Forbidden', 403);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
throw Exception('Failed request: ${response.statusCode}');
|
||
}
|
||
}
|
||
|
||
Future<Map<String, dynamic>> _makePostRequestJson(
|
||
Uri url,
|
||
Map<String, dynamic> body,
|
||
Map<String, String> headers,
|
||
) async {
|
||
final response = await http.post(
|
||
url,
|
||
headers: headers,
|
||
body: jsonEncode(body), // ✅ convert to JSON
|
||
);
|
||
return _handleResponse(response);
|
||
}
|
||
|
||
// --------------------------- COMMON FILES __________________________________________
|
||
|
||
Future<Map<String, dynamic>> createUserData(data, path) async {
|
||
print('path - $path');
|
||
final url = Uri.parse('${Env.apiUrl}$path');
|
||
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchUploadEnquiryFiles(dynamic enqId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryFiles?enquiry_id=$enqId',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> logoutUsingAPI(agentId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}auth/logout');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
|
||
// if (response.statusCode == 200) {
|
||
// return jsonDecode(response.body);
|
||
// } else if (response.statusCode == 401 || response.statusCode == 403) {
|
||
// await clearLocalStorageAndRedirect();
|
||
// return {};
|
||
// } else {
|
||
// throw Exception('Failed to load data');
|
||
// }
|
||
// }
|
||
|
||
Future<Map<String, dynamic>> _handleResponse(http.Response response) async {
|
||
Map<String, dynamic> body = {};
|
||
|
||
// 1. Try to parse JSON, but don't let a parse error stop the 401 check
|
||
try {
|
||
body = jsonDecode(response.body);
|
||
} catch (e) {
|
||
debugPrint("Could not parse JSON body: $e");
|
||
}
|
||
|
||
// 2. SUCCESS
|
||
if (response.statusCode == 200) {
|
||
return body;
|
||
}
|
||
|
||
// 3. UNAUTHORIZED / FORBIDDEN (The 401/403 Fix)
|
||
// if ((response.statusCode == 401 && body['status'] == 401) ||
|
||
// (response.statusCode == 403 && body['status'] == 403)) {
|
||
// // Perform cleanup
|
||
// await AuthService.clearToken();
|
||
//
|
||
// // Redirect using global appRouter (avoids context errors)
|
||
// context.go(AppRoutes.login);
|
||
//
|
||
// // Return a structured error so the UI knows why it failed
|
||
// return {'status': 401, 'message': 'Session expired or invalid'};
|
||
// }
|
||
//
|
||
// if (response.statusCode == 401 || response.statusCode == 403) {
|
||
// await clearLocalStorageAndRedirect();
|
||
// return {};
|
||
// }
|
||
|
||
final message = (body['message'] ?? body['error'] ?? '')
|
||
.toString()
|
||
.toLowerCase();
|
||
|
||
// 2️⃣ TOKEN / AUTH FAILURE (message OR status based)
|
||
final isAuthError =
|
||
response.statusCode == 401 ||
|
||
response.statusCode == 403 ||
|
||
message.contains('token required') ||
|
||
message.contains('invalid or expired token');
|
||
|
||
if (isAuthError) {
|
||
await clearLocalStorageAndRedirect(); // handles navigation internally
|
||
|
||
// Stop further execution
|
||
throw Exception('Authentication error');
|
||
}
|
||
|
||
// 4. ALL OTHER ERRORS
|
||
throw Exception('Server Error: ${response.statusCode}');
|
||
}
|
||
|
||
Future<Map<String, dynamic>> CheckDuplicate(
|
||
BuildContext context,
|
||
String value,
|
||
) async {
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
String apiUrldata;
|
||
|
||
final response = await http.get(
|
||
Uri.parse('${Env.apiUrl}enquiry/checkVehicleDuplicate?reg_no=$value'),
|
||
headers: {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
},
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
try {
|
||
final data = json.decode(response.body);
|
||
|
||
// Updated to match actual response structure
|
||
if (data['status'] == "exists") {
|
||
final msg = data['message'] ?? "Already exists";
|
||
final field = data['field'] ?? "";
|
||
|
||
print("$field - $value : $msg");
|
||
print("EXIST REG");
|
||
|
||
return {"message": msg, "field": field};
|
||
} else {
|
||
print("NOT EXIST REG");
|
||
// print("$value : ${data['message'] ?? "is a new value"}");
|
||
return {};
|
||
}
|
||
} catch (e) {
|
||
throw Exception('Error parsing response: $e');
|
||
}
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
return {};
|
||
} else {
|
||
throw Exception(
|
||
'Failed to load checkDuplicate data. Status code: ${response.statusCode}',
|
||
);
|
||
}
|
||
}
|
||
|
||
Future<void> getPdfDownload1(path, id) async {
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/downloadAgentCertificateFile?agent_id=1',
|
||
// );
|
||
|
||
print('GetDownload');
|
||
final url = Uri.parse('https://venbait.in/nhance/partner/dev/$path');
|
||
print('GetDownload - $url');
|
||
await _initializeToken();
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGethttpRequest(url, headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
try {
|
||
print("PDf Dowloaded");
|
||
|
||
// Create a blob from the response body
|
||
final blob = html.Blob([response.bodyBytes]);
|
||
|
||
// Generate a download URL for the blob
|
||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||
|
||
// Create a link element to trigger the download
|
||
final anchor = html.AnchorElement(href: url)
|
||
..setAttribute('download', 'nhancePartner_$id.pdf')
|
||
..click();
|
||
|
||
// Revoke the download URL to free up resources
|
||
html.Url.revokeObjectUrl(url);
|
||
} catch (e) {
|
||
throw Exception('Error parsing response: $e');
|
||
}
|
||
} else if (response.statusCode == 404) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (BuildContext context) {
|
||
return AlertDialog(
|
||
title: Text('File not found.'),
|
||
// content: Text('File not found.'),
|
||
actions: [
|
||
TextButton(
|
||
child: Text('OK'),
|
||
onPressed: () {
|
||
Navigator.of(context).pop(); // Close the dialog
|
||
},
|
||
),
|
||
],
|
||
);
|
||
},
|
||
);
|
||
} else {
|
||
throw Exception('Failed to load plans');
|
||
}
|
||
}
|
||
|
||
String extractFileName(String? contentDisposition, String fallback) {
|
||
if (contentDisposition == null) return fallback;
|
||
|
||
// Check for extended filename first (RFC 5987)
|
||
final filenameStarMatch = RegExp(
|
||
r'filename\*\s*=\s*([^;]+)',
|
||
).firstMatch(contentDisposition);
|
||
if (filenameStarMatch != null) {
|
||
String value = filenameStarMatch.group(1)!;
|
||
// Remove UTF-8'' prefix if exists
|
||
value = value.replaceAll(RegExp(r"UTF-8''"), '');
|
||
return Uri.decodeFull(value.replaceAll('"', ''));
|
||
}
|
||
|
||
// Fallback to regular filename=
|
||
final filenameMatch = RegExp(
|
||
r'filename\s*=\s*"?([^";]+)"?',
|
||
).firstMatch(contentDisposition);
|
||
if (filenameMatch != null) {
|
||
return filenameMatch.group(1)!;
|
||
}
|
||
|
||
// Fallback: use the provided fallback string
|
||
return fallback;
|
||
}
|
||
|
||
Future<void> getPdfDownload(
|
||
// BuildContext context,
|
||
String path,
|
||
String id,
|
||
) async {
|
||
print('getPdfDownload 1 - $path');
|
||
|
||
// if (path.isEmpty) {
|
||
// ToastHelper.showInfoToast(context, 'No file path found.');
|
||
// return;
|
||
// }
|
||
final url = Uri.parse('https://venbait.in/nhance/partner/dev/$path');
|
||
// final url = Uri.parse('${Env.apiUrl}$path');
|
||
// final url = Uri.parse('${Env.apiUrl}$path');
|
||
print('getPdfDownload 2 - $url');
|
||
await _initializeToken();
|
||
|
||
if (_token == null) throw Exception('Token not found. Please log in.');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
// 'App-Signature': Env.App_Signature,
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGethttpRequest(url, headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
final contentType = response.headers['content-type'] ?? '';
|
||
if (contentType.contains('application/json')) {
|
||
final jsonResponse = jsonDecode(response.body);
|
||
if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
|
||
ToastHelper.show('File Not Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
}
|
||
return;
|
||
} else {
|
||
final blob = html.Blob([response.bodyBytes]);
|
||
final blobUrl = html.Url.createObjectUrlFromBlob(blob);
|
||
print('getPdfDownload $blobUrl');
|
||
// --- filename resolution ---
|
||
// String fileName = 'download_$id';
|
||
// print('getPdfDownloadfileName $fileName');
|
||
// final contentDisp = response.headers['content-disposition'];
|
||
// print('getPdfDownloadcontentDisp $contentDisp');
|
||
// if (contentDisp != null && contentDisp.contains('filename=')) {
|
||
// print('getPdfDownloadcontentDisp..1');
|
||
// fileName = contentDisp.split('filename=')[1].replaceAll('"', '');
|
||
// print('getPdfDownloadcontentDisp..2');
|
||
// } else {
|
||
// print('getPdfDownloadcontentDisp..3');
|
||
// // fallback: URL or id
|
||
// fileName = path.split('/').last;
|
||
// if (id != null) fileName = '${id}_$fileName';
|
||
// }
|
||
|
||
final contentDisp = response.headers['content-disposition'];
|
||
print('getPdfDownloadcontentDisp $contentDisp');
|
||
|
||
String fileName = extractFileName(contentDisp, path.split('/').last);
|
||
print('Resolved fileName: $fileName');
|
||
final anchor = html.AnchorElement(href: blobUrl)
|
||
..setAttribute('download', fileName)
|
||
..click();
|
||
|
||
html.Url.revokeObjectUrl(blobUrl);
|
||
}
|
||
} else if (response.statusCode == 404) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: const Text('File not found.'),
|
||
actions: [
|
||
TextButton(
|
||
child: const Text('OK'),
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
} else if (response.statusCode == 500) {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else if (response.statusCode == 302) {
|
||
print('getPdfDownload 3-PP-302');
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
throw Exception('Failed to download file');
|
||
}
|
||
}
|
||
|
||
Future<void> generateChartExcel(
|
||
// BuildContext context,
|
||
String path,
|
||
String id,
|
||
String month,
|
||
dynamic managerId,
|
||
) async {
|
||
print('getPdfDownload 1 - $path');
|
||
|
||
dynamic pathVal;
|
||
if (path == 'Insurer') {
|
||
pathVal =
|
||
'dashboard/downloadInsurerPoliciesExcel?manager_id=$managerId&month=$month&insurer_id=$id';
|
||
} else if (path == 'Broker') {
|
||
pathVal =
|
||
'dashboard/downloadBrokerPoliciesExcel?manager_id=$managerId&month=$month&broker_id=$id';
|
||
} else if (path == 'Product') {
|
||
pathVal =
|
||
'dashboard/downloadProductPoliciesExcel?manager_id=$managerId&month=$month&vehicle_type=$id';
|
||
} else if (path == 'PerformingTop50') {
|
||
pathVal =
|
||
'dashboard/downloadAgentMonthlyPoliciesExcel?manager_id=$managerId&agent_id=$id';
|
||
} else if (path == 'PerformingAgentAll50') {
|
||
pathVal = 'dashboard/downloadT50AgentPoliciesExcel?manager_id=$managerId';
|
||
} else if (path == 'NonPerformingBelow50K') {
|
||
pathVal = 'dashboard/downloadLowPremiumAgentExcel?manager_id=$managerId';
|
||
} else if (path == 'staff_pending_summary') {
|
||
pathVal = 'dashboard/downloadStaffPendingSummaryExcel?manager_id=$managerId';
|
||
} else if (path == 'staff_pending_summary_by_id') {
|
||
pathVal = 'dashboard/downloadStaffPendingSummaryExcelByID?manager_id=$managerId&staff_id=$id';
|
||
} else {
|
||
//NoBusiness
|
||
pathVal =
|
||
'dashboard/downloadAgentsWithoutPoliciesExcel?manager_id=$managerId';
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}$pathVal');
|
||
// final url = 'http://localhost/nhance_partner_be/dashboard/downloadAgentsWithoutPoliciesExcel?manager_id=1';
|
||
// final url = Uri.parse('https://venbait.in/nhance/partner/dev/$path');
|
||
print('getPdfDownload 2 - $url');
|
||
await _initializeToken();
|
||
|
||
if (_token == null) throw Exception('Token not found. Please log in.');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
// 'App-Signature': Env.App_Signature,
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGethttpRequest(url, headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
final contentType = response.headers['content-type'] ?? '';
|
||
if (contentType.contains('application/json')) {
|
||
final jsonResponse = jsonDecode(response.body);
|
||
if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
|
||
ToastHelper.show('File Not Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
}
|
||
return;
|
||
} else {
|
||
final blob = html.Blob([response.bodyBytes]);
|
||
final blobUrl = html.Url.createObjectUrlFromBlob(blob);
|
||
print('getPdfDownload $blobUrl');
|
||
final contentDisp = response.headers['content-disposition'];
|
||
print('getPdfDownloadcontentDisp $contentDisp');
|
||
|
||
String fileName = extractFileName(contentDisp, path.split('/').last);
|
||
print('Resolved fileName: $fileName');
|
||
final anchor = html.AnchorElement(href: blobUrl)
|
||
..setAttribute('download', fileName)
|
||
..click();
|
||
|
||
html.Url.revokeObjectUrl(blobUrl);
|
||
}
|
||
} else if (response.statusCode == 404) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: const Text('File not found.'),
|
||
actions: [
|
||
TextButton(
|
||
child: const Text('OK'),
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
} else if (response.statusCode == 500) {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else if (response.statusCode == 302) {
|
||
print('getPdfDownload 3-PP-302');
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
throw Exception('Failed to download file');
|
||
}
|
||
}
|
||
|
||
Future<void> generatePerformanceDashboardExcel(
|
||
// BuildContext context,
|
||
String path,
|
||
String id,
|
||
String salesId,
|
||
String month,
|
||
dynamic managerId,
|
||
) async {
|
||
print('getPdfDownload 1 - $path');
|
||
|
||
dynamic pathVal;
|
||
|
||
pathVal =
|
||
'dashboard/downloadStaffAndProductPoliciesExcel?manager_id=$managerId&month=$month&sales_executive_id=$salesId&vehicle_type=$id';
|
||
|
||
final url = Uri.parse('${Env.apiUrl}$pathVal');
|
||
// final url = Uri.parse('https://venbait.in/nhance/partner/dev/$path');
|
||
print('getPdfDownload 2 - $url');
|
||
await _initializeToken();
|
||
|
||
if (_token == null) throw Exception('Token not found. Please log in.');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
// 'App-Signature': Env.App_Signature,
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGethttpRequest(url, headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
final contentType = response.headers['content-type'] ?? '';
|
||
if (contentType.contains('application/json')) {
|
||
final jsonResponse = jsonDecode(response.body);
|
||
if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
|
||
ToastHelper.show('File Not Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
}
|
||
return;
|
||
} else {
|
||
final blob = html.Blob([response.bodyBytes]);
|
||
final blobUrl = html.Url.createObjectUrlFromBlob(blob);
|
||
print('getPdfDownload $blobUrl');
|
||
final contentDisp = response.headers['content-disposition'];
|
||
print('getPdfDownloadcontentDisp $contentDisp');
|
||
|
||
String fileName = extractFileName(contentDisp, path.split('/').last);
|
||
print('Resolved fileName: $fileName');
|
||
final anchor = html.AnchorElement(href: blobUrl)
|
||
..setAttribute('download', fileName)
|
||
..click();
|
||
|
||
html.Url.revokeObjectUrl(blobUrl);
|
||
}
|
||
} else if (response.statusCode == 404) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: const Text('File not found.'),
|
||
actions: [
|
||
TextButton(
|
||
child: const Text('OK'),
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
} else if (response.statusCode == 500) {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else if (response.statusCode == 302) {
|
||
print('getPdfDownload 3-PP-302');
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
throw Exception('Failed to download file');
|
||
}
|
||
}
|
||
|
||
Future<void> downloadFile({
|
||
required String? apiUrl,
|
||
required PlatformFile? localFile,
|
||
required String? fileName,
|
||
String? apiId, // optional if your API needs it
|
||
}) async {
|
||
try {
|
||
if (localFile != null) {
|
||
if (kIsWeb) {
|
||
final blob = html.Blob([localFile.bytes!]);
|
||
final url = html.Url.createObjectUrlFromBlob(blob);
|
||
|
||
final anchor = html.AnchorElement(href: url)
|
||
..setAttribute('download', localFile.name)
|
||
..click();
|
||
|
||
html.Url.revokeObjectUrl(url);
|
||
print("Download triggered successfully (web)!");
|
||
} else {
|
||
print("Local file path: ${localFile.path}");
|
||
// await OpenFilex.open(localFile.path!);
|
||
}
|
||
} else if (apiUrl != null) {
|
||
print("Download from API: $apiUrl");
|
||
print("Download from API: $apiId!");
|
||
await getPdfDownload(apiUrl, apiId!);
|
||
} else {
|
||
print("⚠️ No file available to download");
|
||
}
|
||
} catch (e) {
|
||
print("Error during download: $e");
|
||
}
|
||
}
|
||
|
||
Future<Map<String, dynamic>> updateStatus(id, status, role) async {
|
||
print('updateStatusupdateStatus');
|
||
final Map<String, dynamic> data = {
|
||
"id": int.parse(id),
|
||
"is_active": int.parse(status),
|
||
};
|
||
final url;
|
||
|
||
if (role == 'salesExecutive') {
|
||
url = Uri.parse('${Env.apiUrl}salesExecutive/changeExecutiveStatus');
|
||
} else if (role == 'staff') {
|
||
url = Uri.parse('${Env.apiUrl}staff/changeStaffStatus');
|
||
} else {
|
||
url = Uri.parse('${Env.apiUrl}agent/changeAgentStatus');
|
||
}
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> updateStatusMasters(
|
||
id,
|
||
status,
|
||
masterName,
|
||
userId,
|
||
) async {
|
||
print('updateStatusMaster $id $status $masterName $userId');
|
||
final Map<String, dynamic> data = {
|
||
"is_active": int.parse(status),
|
||
"updated_by": userId,
|
||
};
|
||
final url;
|
||
|
||
if (masterName == 'Broker') {
|
||
url = Uri.parse('${Env.apiUrl}master/updateBrokerStatus/$id');
|
||
}
|
||
else if (masterName == 'EndorsementType') {
|
||
url = Uri.parse('${Env.apiUrl}master/updateEndorsementStatus/$id');
|
||
}
|
||
else {
|
||
url = Uri.parse('${Env.apiUrl}master/updatePaymentModeStatus/$id');
|
||
}
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- Dashboard -------------------------------------------------
|
||
Future<Map<String, dynamic>> fetchDashboard(int id, role, userId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url;
|
||
|
||
if (role == 'manager') {
|
||
url = Uri.parse('${Env.apiUrl}dashboard/managerDashboard?manager_id=$id');
|
||
} else if (role == 'handler') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}dashboard/handlerDashboard?manager_id=$id&handler_id=$userId',
|
||
);
|
||
} else if (role == 'staff') {
|
||
url = Uri.parse('${Env.apiUrl}dashboard/staffDashboard?staff_id=$userId');
|
||
} else {
|
||
url = Uri.parse('${Env.apiUrl}dashboard/agentDashboard?agent_id=$userId');
|
||
}
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentList?manager_id=${managerId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findBusinessDashboardData(id, broker) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}dashboard/businessDashboard?manager_id=$id',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findPartnerDashboardData(id, broker) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}dashboard/partnerDashboard?manager_id=$id',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findProductivityDashboardData(id, broker) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}dashboard/productivityDashboard?manager_id=$id',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// -------------------------------- AGENT ----------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchAgentIncentiveList(agentId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}agent/agentIncentiveFileList?agent_id=${agentId}',
|
||
);
|
||
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentIncentiveFileList?agent_id=${agentId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchManagerIncentiveList(mangerId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}staff/managerIncentiveFileList?manager_id=$mangerId',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchAgentUserList(int managerId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final userId = '1';
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}agent/agentList?manager_id=${managerId}',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleAgentData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
// final url = Uri.parse('${Env.apiUrl}/api/agent/agentList');
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/findAgent?id=$id',
|
||
// );
|
||
|
||
final url = Uri.parse('${Env.apiUrl}agent/findAgent?id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> deleteAgentIncentiveFile(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
// final url = Uri.parse('${Env.apiUrl}/api/agent/agentList');
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/deleteAgentIncentiveFile?id=$id',
|
||
// );
|
||
|
||
final url = Uri.parse('${Env.apiUrl}agent/deleteAgentIncentiveFile?id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ------------------------------------ STAFF -----------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchStaffUserList(int managerId, role) async {
|
||
// print(_token);
|
||
|
||
print('rolemanagerId - $managerId');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/staff/staffList?manager_id=${managerId}',
|
||
// );
|
||
|
||
dynamic url;
|
||
if (role == 'manager') {
|
||
print('manager');
|
||
// url = Uri.parse('${Env.apiUrl}staff/staffList');
|
||
url = Uri.parse('${Env.apiUrl}staff/staffList?manager_id=${managerId}');
|
||
} else if (role == 'Accounts') {
|
||
print('manager');
|
||
// url = Uri.parse('${Env.apiUrl}staff/staffList');
|
||
url = Uri.parse('${Env.apiUrl}staff/staffList?manager_id=${managerId}');
|
||
} else {
|
||
print('handler');
|
||
url = Uri.parse('${Env.apiUrl}staff/staffList?handler_id=${managerId}');
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleStaffData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
// final url = Uri.parse('${Env.apiUrl}/api/staff/findStaff?id=$id');
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/staff/findStaff?id=$id',
|
||
// );
|
||
|
||
final url = Uri.parse('${Env.apiUrl}staff/findStaff?id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ------------------------------------ SALES EXECUTIVE -----------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchSalesExecutiveList(
|
||
int managerId,
|
||
role,
|
||
) async {
|
||
// print(_token);
|
||
|
||
print('rolemanagerId - $managerId');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
if (role == 'manager') {
|
||
print('manager');
|
||
// url = Uri.parse('${Env.apiUrl}staff/staffList');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}salesExecutive/executiveList?manager_id=${managerId}',
|
||
);
|
||
// url = Uri.parse('${Env.apiUrl}salesExecutive/executiveList');
|
||
} else if (role == 'Accounts') {
|
||
print('manager');
|
||
// url = Uri.parse('${Env.apiUrl}staff/staffList');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}salesExecutive/executiveList?manager_id=${managerId}',
|
||
);
|
||
} else {
|
||
print('handler');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}salesExecutive/executiveList?handler_id=${managerId}',
|
||
);
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleSalesExecutiveData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}salesExecutive/findExecutive?id=$id');
|
||
// final url = Uri.parse('${Env.apiUrl}staff/findStaff?id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- POS -------------------------------------------------
|
||
Future<Map<String, dynamic>> fetchPosList(int managerId, val) async {
|
||
// print(_token);
|
||
|
||
print('dropDown - $managerId');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
if (val == 'dropDown') {
|
||
print('dropDown');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}master/getAllPOS?is_active=1&manager_id=${managerId}',
|
||
);
|
||
} else {
|
||
print('List');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}master/getAllPOS?is_active=1&manager_id=${managerId}',
|
||
);
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- ENQUIRY -------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchEnquiryList(
|
||
int managerid,
|
||
int id,
|
||
String role, {
|
||
String? fromDate,
|
||
String? toDate,
|
||
String? selectedStatus,
|
||
String? selectedStaffId,
|
||
}) async {
|
||
print("selectedStatusselectedStatus - $selectedStatus");
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final String query;
|
||
|
||
// if (role == 'manager' || role == 'handler') {
|
||
// query = 'manager_id=$id';
|
||
// }
|
||
if (role == 'manager') {
|
||
query = 'manager_id=$id';
|
||
} else if (role == 'handler') {
|
||
query = 'handler_id=$id';
|
||
} else if (role == 'staff') {
|
||
query = 'staff_id=$id';
|
||
} else {
|
||
query = 'agent_id=$id';
|
||
}
|
||
|
||
// final url = Uri.parse(
|
||
// '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',
|
||
// );
|
||
final url;
|
||
|
||
if (role == 'staff') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&enquiry_status=$selectedStatus',
|
||
// '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus',
|
||
);
|
||
} else {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&enquiry_status=$selectedStatus&staff_id=$selectedStaffId',
|
||
// '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus&staff_id=$selectedStaffId',
|
||
);
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchAttndanceOFAllStaffList({
|
||
required String month,
|
||
required dynamic mangerId,
|
||
}) async {
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final String query;
|
||
|
||
// if (role == 'manager') {
|
||
// query = 'manager_id=$id';
|
||
// }
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}staff/monthlyLoginCount?month=${month ?? ''}&manager_id=$mangerId',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchAttndanceOfIndiviualStaffList({
|
||
String? month,
|
||
id,
|
||
}) async {
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final String query;
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}staff/staffLoggedDays?month=${month ?? ''}&staff_id=$id',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleEnquiryData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?enquiry_id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> updateEnquiryInProgress(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/updateEnquiryInProgress?enquiry_id=$id',
|
||
);
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findEnqQuotePolicyView(id) async {
|
||
print('findEnqQuotePolicyVie2w - $id');
|
||
if (_token == null) {
|
||
print('findEnqQuotePolicyVie2w 1');
|
||
await _initializeToken();
|
||
print('findEnqQuotePolicyVie2w 2');
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryQuotePolicyView?enquiry_id=$id',
|
||
);
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
print('findEnqQuotePolicyVie2w 3');
|
||
final response = await _makeGetRequest(url, headers);
|
||
|
||
if (response.containsKey('error')) {
|
||
print(
|
||
'❌ API returned invalid JSON or HTML. Raw response: ${response['raw']}',
|
||
);
|
||
return {}; // return empty map on error
|
||
}
|
||
print('findEnqQuotePolicyView 1');
|
||
print('findEnqQuotePolicyView 1 - $response');
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> deleteEnquiy(id, val) async {
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}/enquiry/updateEnquiryStatus?enquiry_id=$id&is_active=$val',
|
||
);
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
// final response = await _makePostRequestJson(url, data, headers);
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
//------------------------------------ Quotation ------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchQuickQuotationList(int managerId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/getQuickQuoteEnquiryList?manager_id=${managerId}',
|
||
);
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchQuotationList(int managerId) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/staff/staffList?manager_id=${managerId}',
|
||
// );
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}quotation/quotationList?manager_id=${managerId}',
|
||
);
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleQuotationData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse('${Env.apiUrl}quotation/findQuotation?id=$id');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- POLICY -------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchPolicyList(
|
||
int id,
|
||
role, {
|
||
String? fromDate,
|
||
String? toDate,
|
||
String? selectedStatus,
|
||
String? selectedStaffId,
|
||
}) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url;
|
||
|
||
final String query;
|
||
final String val = 'Proposal Created';
|
||
if (role == 'manager') {
|
||
query = 'manager_id=$id';
|
||
} else if (role == 'staff') {
|
||
query = 'staff_id=$id';
|
||
} else {
|
||
query = 'agent_id=$id';
|
||
}
|
||
|
||
// url = Uri.parse(
|
||
// '${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',
|
||
// );
|
||
|
||
if (role == 'staff') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus',
|
||
);
|
||
} else {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryList?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&status=$selectedStatus&staff_id=$selectedStaffId',
|
||
);
|
||
}
|
||
|
||
// if (role == 'manager') {
|
||
// url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?manager_id=$id');
|
||
// } else {
|
||
// url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?staff_id=$id');
|
||
// }
|
||
// else {
|
||
// url = Uri.parse('${Env.apiUrl}endorsement/endorsementList?agent_id=$id');
|
||
// }
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentList?manager_id=${managerId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchPolicyDataOnlyList(
|
||
managerId,
|
||
dynamic id,
|
||
role, {
|
||
String? fromDate,
|
||
String? toDate,
|
||
String? selectedStaffId,
|
||
}) async {
|
||
print(
|
||
"fetchPolicyDataOnlyListAPI - mangerID- $managerId - id - $id - role -$role ",
|
||
);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final userId = '1';
|
||
|
||
final String query;
|
||
|
||
if (role == 'manager') {
|
||
query = 'manager_id=$managerId';
|
||
} else if (role == 'Accounts') {
|
||
query = 'manager_id=$managerId';
|
||
} else if (role == 'Accounts') {
|
||
query = 'manager_id=$managerId';
|
||
} else if (role == 'staff') {
|
||
query = 'staff_id=$id';
|
||
} else if (role == 'handler') {
|
||
query = 'handler_id=$id';
|
||
} else {
|
||
query = 'agent_id=$id';
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}enquiry/enquiryList?$query&only_policy_data=true&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}&staff_id=$selectedStaffId',
|
||
);
|
||
|
||
// if (role == 'manager') {
|
||
// url = Uri.parse(
|
||
// '${Env.apiUrl}enquiry/enquiryList?manager_id=$id&only_policy_data=true',
|
||
// );
|
||
// } else if (role == 'staff') {
|
||
// url = Uri.parse(
|
||
// '${Env.apiUrl}enquiry/enquiryList?staff_id=$id&only_policy_data=true',
|
||
// );
|
||
// } else {
|
||
// url = Uri.parse(
|
||
// '${Env.apiUrl}enquiry/enquiryList?agent_id=$id&only_policy_data=true',
|
||
// );
|
||
// }
|
||
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentList?manager_id=${managerId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- CLAIMS -------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchPolicySearch(val) async {
|
||
// print(_token);
|
||
|
||
// print('rolemanagerId - $managerId');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
url = Uri.parse('${Env.apiUrl}policy/searchThePolicies?policy_number=$val');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchVehicleSearch(val) async {
|
||
// print(_token);
|
||
|
||
// print('rolemanagerId - $managerId');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}policy/searchThePolicies?vehicle_number=$val',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchClaimList(
|
||
managerId,
|
||
int userId,
|
||
role,
|
||
) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url;
|
||
|
||
if (role == 'manager') {
|
||
url = Uri.parse('${Env.apiUrl}claim/ClaimList?manager_id=$managerId');
|
||
} else if (role == 'Accounts') {
|
||
url = Uri.parse('${Env.apiUrl}claim/ClaimList?manager_id=$managerId');
|
||
} else if (role == 'agent') {
|
||
url = Uri.parse('${Env.apiUrl}claim/ClaimList?agent_id=$userId');
|
||
} else if (role == 'handler') {
|
||
url = Uri.parse('${Env.apiUrl}claim/ClaimList?handler_id=$userId');
|
||
} else {
|
||
url = Uri.parse('${Env.apiUrl}claim/ClaimList?staff_id=$userId');
|
||
}
|
||
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentList?manager_id=${managerId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleClaimData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse('${Env.apiUrl}claim/ClaimList?policy_number=$id');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// ----------------------------------- ENDORSEMENT -------------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchEndorsementList(
|
||
managerId,
|
||
int id,
|
||
role,
|
||
) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url;
|
||
|
||
if (role == 'manager') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}endorsement/endorsementList?manager_id=$managerId',
|
||
);
|
||
} else if (role == 'Accounts') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}endorsement/endorsementList?manager_id=$managerId',
|
||
);
|
||
} else if (role == 'agent') {
|
||
url = Uri.parse('${Env.apiUrl}endorsement/endorsementList?agent_id=$id');
|
||
} else if (role == 'handler') {
|
||
url = Uri.parse('${Env.apiUrl}enquiry/enquiryList?handler_id=$id');
|
||
} else {
|
||
url = Uri.parse('${Env.apiUrl}endorsement/endorsementList?staff_id=$id');
|
||
}
|
||
// final url = Uri.parse(
|
||
// 'https://venbait.in/nhance/partner/dev/api/agent/agentList?manager_id=${managerId}',
|
||
// );
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findSingleEnrosmentData(id) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}endorsement/endorsementList?policy_number=$id',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
// --------------------------------- MASTER DATA DROPDOWN----------------------------------------------
|
||
|
||
Future<Map<String, dynamic>> fetchMasterDropDown(String val) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
print('VAl - $val');
|
||
|
||
dynamic url;
|
||
|
||
if (val == 'staffRole') {
|
||
url = Uri.parse('${Env.apiUrl}master/getRoleMaster');
|
||
} else if (val == 'vehicleType') {
|
||
url = Uri.parse('${Env.apiUrl}master/getVehicleTypeMaster');
|
||
} else if (val == 'InsuranceType') {
|
||
url = Uri.parse('${Env.apiUrl}master/getInsurancePlanTypeMaster');
|
||
} else if (val == 'Insurers') {
|
||
url = Uri.parse('${Env.apiUrl}master/getInsurersMaster');
|
||
} else if (val == 'Staffs') {
|
||
url = Uri.parse('${Env.apiUrl}master/getStaffMaster');
|
||
} else if (val == 'Claim') {
|
||
url = Uri.parse('${Env.apiUrl}master/getClaimMaster');
|
||
} else if (val == 'Endorsement') {
|
||
url = Uri.parse('${Env.apiUrl}master/getEndorsementMaster');
|
||
} else if (val == 'Broker') {
|
||
url = Uri.parse('${Env.apiUrl}master/getAllBrokers');
|
||
} else if (val == 'PaymentMode') {
|
||
url = Uri.parse('${Env.apiUrl}master/getAllPaymentModelist');
|
||
} else if (val == 'EndorsementType') {
|
||
url = Uri.parse('${Env.apiUrl}master/getAllEndorsement');
|
||
}
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchHandlerNameDropDown(id) async {
|
||
print('fetchHandlerNameDropDown');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
print('fetchHandlerNameDropDown 1');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}staff/handlerListForStaffCreationDropdown?manager_id=$id',
|
||
);
|
||
print('fetchHandlerNameDropDown 2');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
print('fetchHandlerNameDropDown 3');
|
||
final response = await _makeGetRequest(url, headers);
|
||
print('fetchHandlerNameDropDown 4 - $response');
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchStaffListForEnquiryAssignDropDown(
|
||
managerId,
|
||
id,
|
||
role,
|
||
) async {
|
||
print('fetchHandlerNameDropDown');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
print('fetchHandlerNameDropDown 1');
|
||
|
||
if (role == 'manager') {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}staff/staffListForEnquiryAssignDropdown?manager_id=$managerId',
|
||
);
|
||
} else {
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}staff/staffListForEnquiryAssignDropdown?handler_id=$id',
|
||
);
|
||
}
|
||
print('fetchHandlerNameDropDown 2');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
print('fetchHandlerNameDropDown 3');
|
||
final response = await _makeGetRequest(url, headers);
|
||
print('fetchHandlerNameDropDown 4 - $response');
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchAgentNameDropDown(id) async {
|
||
print('fetchAGENTNameDropDown');
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
dynamic url;
|
||
print('fetchAGENTNameDropDown 1');
|
||
url = Uri.parse(
|
||
'${Env.apiUrl}agent/agentListForEnquiryCreationDropdown?manager_id=$id',
|
||
);
|
||
print('fetchAGENTNameDropDown 2');
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
print('fetchAGENTNameDropDown 3');
|
||
final response = await _makeGetRequest(url, headers);
|
||
print('fetchAGENTNameDropDown 4 - $response');
|
||
return response;
|
||
}
|
||
|
||
static late String _baseUrl;
|
||
|
||
static void initialize(String baseUrl) {
|
||
_baseUrl = Env.apiUrl;
|
||
}
|
||
|
||
// Fetch all enquiries
|
||
static Future<List<EnquiryModel>> fetchEnquiries() async {
|
||
try {
|
||
final response = await http.get(
|
||
Uri.parse('$_baseUrl/api/enquiries'),
|
||
headers: {'Content-Type': 'application/json'},
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final List<dynamic> data = json.decode(response.body);
|
||
return data.map((json) => EnquiryModel.fromJson(json)).toList();
|
||
} else {
|
||
throw Exception('Failed to load enquiries');
|
||
}
|
||
} catch (e) {
|
||
throw Exception('Error fetching enquiries: $e');
|
||
}
|
||
}
|
||
|
||
// Create new enquiry
|
||
static Future<EnquiryModel> createEnquiry(EnquiryModel enquiry) async {
|
||
try {
|
||
final response = await http.post(
|
||
Uri.parse('$_baseUrl/api/enquiries'),
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: json.encode(enquiry.toJson()),
|
||
);
|
||
|
||
if (response.statusCode == 200 || response.statusCode == 201) {
|
||
return EnquiryModel.fromJson(json.decode(response.body));
|
||
} else {
|
||
throw Exception('Failed to create enquiry');
|
||
}
|
||
} catch (e) {
|
||
throw Exception('Error creating enquiry: $e');
|
||
}
|
||
}
|
||
|
||
// Update enquiry
|
||
static Future<EnquiryModel> updateEnquiry(
|
||
String id,
|
||
EnquiryModel enquiry,
|
||
) async {
|
||
try {
|
||
final response = await http.put(
|
||
Uri.parse('$_baseUrl/api/enquiries/$id'),
|
||
headers: {'Content-Type': 'application/json'},
|
||
body: json.encode(enquiry.toJson()),
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
return EnquiryModel.fromJson(json.decode(response.body));
|
||
} else {
|
||
throw Exception('Failed to update enquiry');
|
||
}
|
||
} catch (e) {
|
||
throw Exception('Error updating enquiry: $e');
|
||
}
|
||
}
|
||
|
||
// Fetch dropdown options
|
||
static Future<List<DropdownOption>> fetchDropdownOptions(String type) async {
|
||
try {
|
||
final response = await http.get(
|
||
Uri.parse('$_baseUrl/api/options/$type'),
|
||
headers: {'Content-Type': 'application/json'},
|
||
);
|
||
|
||
if (response.statusCode == 200) {
|
||
final List<dynamic> data = json.decode(response.body);
|
||
return data.map((json) => DropdownOption.fromJson(json)).toList();
|
||
} else {
|
||
throw Exception('Failed to load options');
|
||
}
|
||
} catch (e) {
|
||
throw Exception('Error fetching options: $e');
|
||
}
|
||
}
|
||
|
||
// --------------------------------- PayOut Module----------------------------------------------
|
||
Future<Map<String, dynamic>> getCommissionRateList(data) async {
|
||
print("getCommissionRateList------- $data}");
|
||
final url = Uri.parse('${Env.apiUrl}/invoice/commission-rate-list');
|
||
print("getCommissionRateList 1");
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
// if (_token == null) {
|
||
// throw Exception('Token not found. Please log in.');
|
||
// }
|
||
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
print("getCommissionRateList 2");
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> getCreateOrUpdate(data) async {
|
||
final url = Uri.parse('${Env.apiUrl}invoice/create-or-update');
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> getInvoiceList() async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}invoice/list');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> deleteInvoice(ID) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}invoice/delete?id=$ID');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> delete(ID, path) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}$path');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> getInvoiceDetails(ID) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}invoice/details?id=$ID');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> findPolicyApi(ID) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse('${Env.apiUrl}policy/findPolicy?id=$ID');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> policyFilePathApi(ID, file_type) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}policy/PolicyFilePath?policy_id=$ID&file_type=$file_type',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> updatePolicyApi(data) async {
|
||
final url = Uri.parse('${Env.apiUrl}policy/updatePolicy');
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> calculateCommissionRequest(data) async {
|
||
final url = Uri.parse('${Env.apiUrl}policy/calculateCommissionRequest');
|
||
|
||
// final token = await getToken(); // Fetch token
|
||
|
||
if (_token == null) {
|
||
throw Exception('Token not found. Please log in.');
|
||
}
|
||
|
||
print("data------- $data}");
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
'Content-Type': 'application/json',
|
||
'app-signature': Env.App_Signature,
|
||
// 'app-signature': 'nhance-partner-2025-signature-35468846JRhH551HK',
|
||
};
|
||
|
||
final response = await _makePostRequestJson(url, data, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchPolicyMasterDropDown(String val) async {
|
||
// print(_token);
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
|
||
print('VAl - $val');
|
||
|
||
dynamic url;
|
||
|
||
if (val == 'fuelType') {
|
||
url = Uri.parse('${Env.apiUrl}policy/fuelTypeMaster');
|
||
} else if (val == 'vehicleType') {
|
||
url = Uri.parse('${Env.apiUrl}policy/vehicleTypeMaster');
|
||
}
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
final response = await _makeGetRequest(url, headers);
|
||
return response;
|
||
}
|
||
|
||
Future<void> generatePolicyExcel(managerId, fromDate, toDate) async {
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}reports/policy-excel?manager_id=$managerId&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',
|
||
);
|
||
|
||
print('getPdfDownload policy-excel - $url');
|
||
await _initializeToken();
|
||
|
||
if (_token == null) throw Exception('Token not found. Please log in.');
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token',
|
||
// 'App-Signature': Env.App_Signature,
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGethttpRequest(url, headers);
|
||
|
||
if (response.statusCode == 200) {
|
||
final contentType = response.headers['content-type'] ?? '';
|
||
if (contentType.contains('application/json')) {
|
||
final jsonResponse = jsonDecode(response.body);
|
||
if (jsonResponse['code'] == 404 || jsonResponse['code'] == 500) {
|
||
ToastHelper.show('File Not Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
}
|
||
return;
|
||
} else {
|
||
final blob = html.Blob([response.bodyBytes]);
|
||
final blobUrl = html.Url.createObjectUrlFromBlob(blob);
|
||
print('getPdfDownload $blobUrl');
|
||
final contentDisp = response.headers['content-disposition'];
|
||
print('getPdfDownloadcontentDisp $contentDisp');
|
||
|
||
String fileName = extractFileName(contentDisp, "policy_excel");
|
||
print('Resolved fileName: $fileName');
|
||
final anchor = html.AnchorElement(href: blobUrl)
|
||
..setAttribute('download', fileName)
|
||
..click();
|
||
|
||
html.Url.revokeObjectUrl(blobUrl);
|
||
}
|
||
} else if (response.statusCode == 404) {
|
||
showDialog(
|
||
context: context,
|
||
builder: (context) => AlertDialog(
|
||
title: const Text('File not found.'),
|
||
actions: [
|
||
TextButton(
|
||
child: const Text('OK'),
|
||
onPressed: () => Navigator.of(context).pop(),
|
||
),
|
||
],
|
||
),
|
||
);
|
||
} else if (response.statusCode == 403) {
|
||
await clearLocalStorageAndRedirect();
|
||
} else if (response.statusCode == 500) {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else if (response.statusCode == 302) {
|
||
print('getPdfDownload 3-PP-302');
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
} else {
|
||
ToastHelper.show('No File Found', Colors.red);
|
||
throw Exception('Failed to download file');
|
||
}
|
||
}
|
||
|
||
Future<Map<String, dynamic>> fetchAuditHistory(id) async {
|
||
print("fetchAuditHistory - $id ");
|
||
|
||
if (_token == null) {
|
||
await _initializeToken();
|
||
}
|
||
// final url = Uri.parse('${Env.apiUrl}audit/history?table_name=partner_policy&pk=80',);
|
||
final url = Uri.parse(
|
||
'${Env.apiUrl}audit/history?table_name=partner_policy&pk=${id ?? ''}',
|
||
);
|
||
|
||
final headers = {
|
||
'Authorization': 'Bearer $_token' ?? '',
|
||
'app-signature': Env.App_Signature,
|
||
};
|
||
|
||
final response = await _makeGetRequest(url, headers);
|
||
|
||
return response;
|
||
}
|
||
|
||
// Future<Map<String, dynamic>> fetchPolicyExcel(managerId,fromDate,toDate) async {
|
||
//
|
||
// print( "fetchPolicyExcel - mangerID- $managerId - Dated - $fromDate - $toDate " );
|
||
//
|
||
// if (_token == null) {
|
||
// await _initializeToken();
|
||
// }
|
||
//
|
||
// final String query;
|
||
//
|
||
// query = 'manager_id=$managerId';
|
||
//
|
||
// final url = Uri.parse('${Env.apiUrl}reports/policy-excel?$query&from_date=${fromDate ?? ''}&to_date=${toDate ?? ''}',);
|
||
//
|
||
// final headers = {
|
||
// 'Authorization': 'Bearer $_token' ?? '',
|
||
// 'app-signature': Env.App_Signature,
|
||
// };
|
||
//
|
||
// final response = await _makeGetRequest(url, headers);
|
||
//
|
||
// return response;
|
||
// }
|
||
}
|