apskel-pos-flutter/lib/data/models/response/profit_loss_response_model.dart
2025-09-19 19:12:35 +07:00

278 lines
7.5 KiB
Dart

class ProfitLossResponseModel {
final bool success;
final ProfitLossData? data;
final dynamic errors;
ProfitLossResponseModel({
required this.success,
required this.data,
this.errors,
});
/// Dari JSON ke model (misal response dari API)
factory ProfitLossResponseModel.fromJson(Map<String, dynamic> json) {
return ProfitLossResponseModel(
success: json['success'],
data: json['data'] == null ? null : ProfitLossData.fromMap(json['data']),
errors: json['errors'],
);
}
/// Dari model ke JSON (misal saat kirim ke API)
Map<String, dynamic> toJson() {
return {
'success': success,
'data': data?.toMap(),
'errors': errors,
};
}
/// Alias dari fromJson, kadang dibutuhkan untuk naming yang konsisten
factory ProfitLossResponseModel.fromMap(Map<String, dynamic> map) {
return ProfitLossResponseModel(
success: map['success'],
data: ProfitLossData.fromMap(map['data']),
errors: map['errors'],
);
}
/// Alias dari toJson
Map<String, dynamic> toMap() {
return {
'success': success,
'data': data?.toMap(),
'errors': errors,
};
}
}
class ProfitLossData {
final String organizationId;
final String dateFrom;
final String dateTo;
final String groupBy;
final ProfitLossSummary summary;
final List<ProfitLossItem> data;
final List<ProfitLossProduct> productData;
ProfitLossData({
required this.organizationId,
required this.dateFrom,
required this.dateTo,
required this.groupBy,
required this.summary,
required this.data,
required this.productData,
});
factory ProfitLossData.fromMap(Map<String, dynamic> map) {
return ProfitLossData(
organizationId: map['organization_id'],
dateFrom: map['date_from'],
dateTo: map['date_to'],
groupBy: map['group_by'],
summary: ProfitLossSummary.fromMap(map['summary']),
data: map['data'] == null
? []
: List<ProfitLossItem>.from(
map['data'].map((x) => ProfitLossItem.fromMap(x))),
productData: map['product_data'] == null
? []
: List<ProfitLossProduct>.from(
map['product_data'].map((x) => ProfitLossProduct.fromMap(x))),
);
}
Map<String, dynamic> toMap() {
return {
'organization_id': organizationId,
'date_from': dateFrom,
'date_to': dateTo,
'group_by': groupBy,
'summary': summary.toMap(),
'data': data.map((x) => x.toMap()).toList(),
'product_data': productData.map((x) => x.toMap()).toList(),
};
}
}
class ProfitLossSummary {
final int totalRevenue;
final num totalCost;
final num grossProfit;
final double grossProfitMargin;
final int totalTax;
final int totalDiscount;
final num netProfit;
final double netProfitMargin;
final int totalOrders;
final double averageProfit;
final double profitabilityRatio;
ProfitLossSummary({
required this.totalRevenue,
required this.totalCost,
required this.grossProfit,
required this.grossProfitMargin,
required this.totalTax,
required this.totalDiscount,
required this.netProfit,
required this.netProfitMargin,
required this.totalOrders,
required this.averageProfit,
required this.profitabilityRatio,
});
factory ProfitLossSummary.fromMap(Map<String, dynamic> map) {
return ProfitLossSummary(
totalRevenue: map['total_revenue'],
totalCost: map['total_cost'],
grossProfit: map['gross_profit'],
grossProfitMargin: (map['gross_profit_margin'] as num).toDouble(),
totalTax: map['total_tax'],
totalDiscount: map['total_discount'],
netProfit: map['net_profit'],
netProfitMargin: (map['net_profit_margin'] as num).toDouble(),
totalOrders: map['total_orders'],
averageProfit: (map['average_profit'] as num).toDouble(),
profitabilityRatio: (map['profitability_ratio'] as num).toDouble(),
);
}
Map<String, dynamic> toMap() {
return {
'total_revenue': totalRevenue,
'total_cost': totalCost,
'gross_profit': grossProfit,
'gross_profit_margin': grossProfitMargin,
'total_tax': totalTax,
'total_discount': totalDiscount,
'net_profit': netProfit,
'net_profit_margin': netProfitMargin,
'total_orders': totalOrders,
'average_profit': averageProfit,
'profitability_ratio': profitabilityRatio,
};
}
}
class ProfitLossItem {
final String date;
final int revenue;
final num cost;
final num grossProfit;
final double grossProfitMargin;
final int tax;
final int discount;
final num netProfit;
final double netProfitMargin;
final int orders;
ProfitLossItem({
required this.date,
required this.revenue,
required this.cost,
required this.grossProfit,
required this.grossProfitMargin,
required this.tax,
required this.discount,
required this.netProfit,
required this.netProfitMargin,
required this.orders,
});
factory ProfitLossItem.fromMap(Map<String, dynamic> map) {
return ProfitLossItem(
date: map['date'],
revenue: map['revenue'],
cost: map['cost'],
grossProfit: map['gross_profit'],
grossProfitMargin: (map['gross_profit_margin'] as num).toDouble(),
tax: map['tax'],
discount: map['discount'],
netProfit: map['net_profit'],
netProfitMargin: (map['net_profit_margin'] as num).toDouble(),
orders: map['orders'],
);
}
Map<String, dynamic> toMap() {
return {
'date': date,
'revenue': revenue,
'cost': cost,
'gross_profit': grossProfit,
'gross_profit_margin': grossProfitMargin,
'tax': tax,
'discount': discount,
'net_profit': netProfit,
'net_profit_margin': netProfitMargin,
'orders': orders,
};
}
}
class ProfitLossProduct {
final String productId;
final String productName;
final String categoryId;
final String categoryName;
final int quantitySold;
final int revenue;
final num cost;
final num grossProfit;
final double grossProfitMargin;
final int averagePrice;
final num averageCost;
final num profitPerUnit;
ProfitLossProduct({
required this.productId,
required this.productName,
required this.categoryId,
required this.categoryName,
required this.quantitySold,
required this.revenue,
required this.cost,
required this.grossProfit,
required this.grossProfitMargin,
required this.averagePrice,
required this.averageCost,
required this.profitPerUnit,
});
factory ProfitLossProduct.fromMap(Map<String, dynamic> map) {
return ProfitLossProduct(
productId: map['product_id'],
productName: map['product_name'],
categoryId: map['category_id'],
categoryName: map['category_name'],
quantitySold: map['quantity_sold'],
revenue: map['revenue'],
cost: map['cost'],
grossProfit: map['gross_profit'],
grossProfitMargin: (map['gross_profit_margin'] as num).toDouble(),
averagePrice: (map['average_price'] as num).toInt(),
averageCost: (map['average_cost'] as num).toInt(),
profitPerUnit: map['profit_per_unit'],
);
}
Map<String, dynamic> toMap() {
return {
'product_id': productId,
'product_name': productName,
'category_id': categoryId,
'category_name': categoryName,
'quantity_sold': quantitySold,
'revenue': revenue,
'cost': cost,
'gross_profit': grossProfit,
'gross_profit_margin': grossProfitMargin,
'average_price': averagePrice,
'average_cost': averageCost,
'profit_per_unit': profitPerUnit,
};
}
}