feat: payment method report
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import 'dart:developer';
|
||||
|
||||
import 'package:dartz/dartz.dart';
|
||||
import 'package:dio/dio.dart';
|
||||
import 'package:enaklo_pos/core/constants/variables.dart';
|
||||
import 'package:enaklo_pos/core/network/dio_client.dart';
|
||||
import 'package:enaklo_pos/data/datasources/auth_local_datasource.dart';
|
||||
import 'package:enaklo_pos/data/models/response/payment_method_analytic_response_model.dart';
|
||||
import 'package:intl/intl.dart';
|
||||
|
||||
class AnalyticRemoteDatasource {
|
||||
final Dio dio = DioClient.instance;
|
||||
|
||||
Future<Either<String, PaymentMethodAnalyticResponseModel>> getPaymentMethod({
|
||||
required DateTime dateFrom,
|
||||
required DateTime dateTo,
|
||||
}) async {
|
||||
final authData = await AuthLocalDataSource().getAuthData();
|
||||
final headers = {
|
||||
'Authorization': 'Bearer ${authData.token}',
|
||||
'Accept': 'application/json',
|
||||
};
|
||||
|
||||
try {
|
||||
final response = await dio.get(
|
||||
'${Variables.baseUrl}/api/v1/analytics/payment-methods',
|
||||
queryParameters: {
|
||||
'date_from': DateFormat('dd-MM-yyyy').format(dateFrom),
|
||||
'date_to': DateFormat('dd-MM-yyyy').format(dateTo),
|
||||
},
|
||||
options: Options(headers: headers),
|
||||
);
|
||||
|
||||
if (response.statusCode == 200) {
|
||||
return right(PaymentMethodAnalyticResponseModel.fromMap(response.data));
|
||||
} else {
|
||||
return left(response.data.toString());
|
||||
}
|
||||
} on DioException catch (e) {
|
||||
log('Dio error: ${e.message}');
|
||||
return left(e.response?.data.toString() ?? e.message ?? 'Unknown error');
|
||||
} catch (e) {
|
||||
log('Unexpected error: $e');
|
||||
return left('Unexpected error occurred');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,170 @@
|
||||
class PaymentMethodAnalyticResponseModel {
|
||||
final bool success;
|
||||
final PaymentMethodAnalyticData data;
|
||||
final dynamic errors;
|
||||
|
||||
PaymentMethodAnalyticResponseModel({
|
||||
required this.success,
|
||||
required this.data,
|
||||
this.errors,
|
||||
});
|
||||
|
||||
factory PaymentMethodAnalyticResponseModel.fromJson(
|
||||
Map<String, dynamic> json) =>
|
||||
PaymentMethodAnalyticResponseModel.fromMap(json);
|
||||
|
||||
Map<String, dynamic> toJson() => toMap();
|
||||
|
||||
factory PaymentMethodAnalyticResponseModel.fromMap(Map<String, dynamic> map) {
|
||||
return PaymentMethodAnalyticResponseModel(
|
||||
success: map['success'],
|
||||
data: PaymentMethodAnalyticData.fromMap(map['data']),
|
||||
errors: map['errors'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'success': success,
|
||||
'data': data.toMap(),
|
||||
'errors': errors,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentMethodAnalyticData {
|
||||
final String organizationId;
|
||||
final String outletId;
|
||||
final DateTime dateFrom;
|
||||
final DateTime dateTo;
|
||||
final String groupBy;
|
||||
final PaymentSummary summary;
|
||||
final List<PaymentMethodAnalyticItem> data;
|
||||
|
||||
PaymentMethodAnalyticData({
|
||||
required this.organizationId,
|
||||
required this.outletId,
|
||||
required this.dateFrom,
|
||||
required this.dateTo,
|
||||
required this.groupBy,
|
||||
required this.summary,
|
||||
required this.data,
|
||||
});
|
||||
|
||||
factory PaymentMethodAnalyticData.fromJson(Map<String, dynamic> json) =>
|
||||
PaymentMethodAnalyticData.fromMap(json);
|
||||
|
||||
Map<String, dynamic> toJson() => toMap();
|
||||
|
||||
factory PaymentMethodAnalyticData.fromMap(Map<String, dynamic> map) {
|
||||
return PaymentMethodAnalyticData(
|
||||
organizationId: map['organization_id'],
|
||||
outletId: map['outlet_id'],
|
||||
dateFrom: DateTime.parse(map['date_from']),
|
||||
dateTo: DateTime.parse(map['date_to']),
|
||||
groupBy: map['group_by'],
|
||||
summary: PaymentSummary.fromMap(map['summary']),
|
||||
data: List<PaymentMethodAnalyticItem>.from(
|
||||
map['data']?.map((x) => PaymentMethodAnalyticItem.fromMap(x)) ?? [],
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'organization_id': organizationId,
|
||||
'outlet_id': outletId,
|
||||
'date_from': dateFrom.toIso8601String(),
|
||||
'date_to': dateTo.toIso8601String(),
|
||||
'group_by': groupBy,
|
||||
'summary': summary.toMap(),
|
||||
'data': data.map((x) => x.toMap()).toList(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentSummary {
|
||||
final int totalAmount;
|
||||
final int totalOrders;
|
||||
final int totalPayments;
|
||||
final double averageOrderValue;
|
||||
|
||||
PaymentSummary({
|
||||
required this.totalAmount,
|
||||
required this.totalOrders,
|
||||
required this.totalPayments,
|
||||
required this.averageOrderValue,
|
||||
});
|
||||
|
||||
factory PaymentSummary.fromJson(Map<String, dynamic> json) =>
|
||||
PaymentSummary.fromMap(json);
|
||||
|
||||
Map<String, dynamic> toJson() => toMap();
|
||||
|
||||
factory PaymentSummary.fromMap(Map<String, dynamic> map) {
|
||||
return PaymentSummary(
|
||||
totalAmount: map['total_amount'],
|
||||
totalOrders: map['total_orders'],
|
||||
totalPayments: map['total_payments'],
|
||||
averageOrderValue: (map['average_order_value'] as num).toDouble(),
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'total_amount': totalAmount,
|
||||
'total_orders': totalOrders,
|
||||
'total_payments': totalPayments,
|
||||
'average_order_value': averageOrderValue,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
class PaymentMethodAnalyticItem {
|
||||
final String paymentMethodId;
|
||||
final String paymentMethodName;
|
||||
final String paymentMethodType;
|
||||
final int totalAmount;
|
||||
final int orderCount;
|
||||
final int paymentCount;
|
||||
final int percentage;
|
||||
|
||||
PaymentMethodAnalyticItem({
|
||||
required this.paymentMethodId,
|
||||
required this.paymentMethodName,
|
||||
required this.paymentMethodType,
|
||||
required this.totalAmount,
|
||||
required this.orderCount,
|
||||
required this.paymentCount,
|
||||
required this.percentage,
|
||||
});
|
||||
|
||||
factory PaymentMethodAnalyticItem.fromJson(Map<String, dynamic> json) =>
|
||||
PaymentMethodAnalyticItem.fromMap(json);
|
||||
|
||||
Map<String, dynamic> toJson() => toMap();
|
||||
|
||||
factory PaymentMethodAnalyticItem.fromMap(Map<String, dynamic> map) {
|
||||
return PaymentMethodAnalyticItem(
|
||||
paymentMethodId: map['payment_method_id'],
|
||||
paymentMethodName: map['payment_method_name'],
|
||||
paymentMethodType: map['payment_method_type'],
|
||||
totalAmount: map['total_amount'],
|
||||
orderCount: map['order_count'],
|
||||
paymentCount: map['payment_count'],
|
||||
percentage: map['percentage'],
|
||||
);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toMap() {
|
||||
return {
|
||||
'payment_method_id': paymentMethodId,
|
||||
'payment_method_name': paymentMethodName,
|
||||
'payment_method_type': paymentMethodType,
|
||||
'total_amount': totalAmount,
|
||||
'order_count': orderCount,
|
||||
'payment_count': paymentCount,
|
||||
'percentage': percentage,
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user