You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

58 rivejä
1.7 KiB

  1. import 'dart:io';
  2. import 'package:path_provider/path_provider.dart';
  3. import 'package:dio/dio.dart';
  4. import 'package:qadirneyriz/config/config.dart';
  5. import 'package:qadirneyriz/setting/setting.dart';
  6. import 'package:qadirneyriz/utils/result/result.dart';
  7. class ReportApi {
  8. Future<Result?> downloadReport(
  9. {String? fromDate,
  10. String? toDate,
  11. int? location,
  12. int? subject,
  13. int? meetingManager,
  14. int? status,
  15. String format = 'xlsx'}) async {
  16. try {
  17. final Map<String, String> headers = {"Accept": "application/json"};
  18. String dataToken = setting.userLocalDb.getUser().token!;
  19. if (dataToken != '') {
  20. headers['Authorization'] = "Bearer $dataToken";
  21. }
  22. final Directory tempDir = await getApplicationDocumentsDirectory();
  23. final String tempPath = tempDir.path;
  24. final String savePath = '$tempPath/report.$format';
  25. final res = await Dio().download(
  26. '${config.network.baseUrl}statistic',
  27. savePath,
  28. queryParameters: {
  29. 'date_meeting_az': fromDate,
  30. 'date_meeting_ta': toDate,
  31. 'location': location,
  32. 'subject': subject,
  33. 'meeting_manager': meetingManager,
  34. 'status': status,
  35. },
  36. options: Options(headers: headers),
  37. );
  38. if (res.statusCode == 200 || res.statusCode == 201) {
  39. return Result(isOk: true, message: savePath);
  40. } else {
  41. return Result(
  42. isOk: false, message: 'Failed with status code: ${res.statusCode}');
  43. }
  44. } on DioException catch (e) {
  45. return Result(
  46. isOk: false,
  47. errors: e.response?.data['errors'],
  48. message:
  49. e.response?.data['message'] ?? 'An error occurred during download.',
  50. );
  51. }
  52. }
  53. }