feat: profit loss widget
This commit is contained in:
parent
c09822b470
commit
e20dc53fb8
@ -72,9 +72,13 @@ class ProfitLossData {
|
|||||||
dateTo: map['date_to'],
|
dateTo: map['date_to'],
|
||||||
groupBy: map['group_by'],
|
groupBy: map['group_by'],
|
||||||
summary: ProfitLossSummary.fromMap(map['summary']),
|
summary: ProfitLossSummary.fromMap(map['summary']),
|
||||||
data: List<ProfitLossItem>.from(
|
data: map['data'] == null
|
||||||
|
? []
|
||||||
|
: List<ProfitLossItem>.from(
|
||||||
map['data'].map((x) => ProfitLossItem.fromMap(x))),
|
map['data'].map((x) => ProfitLossItem.fromMap(x))),
|
||||||
productData: List<ProfitLossProduct>.from(
|
productData: map['product_data'] == null
|
||||||
|
? []
|
||||||
|
: List<ProfitLossProduct>.from(
|
||||||
map['product_data'].map((x) => ProfitLossProduct.fromMap(x))),
|
map['product_data'].map((x) => ProfitLossProduct.fromMap(x))),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -172,12 +172,17 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (previous == 0) return '+0.0%';
|
// Handle division by zero and invalid values
|
||||||
|
if (previous == 0 || previous.isNaN || previous.isInfinite) return '+0.0%';
|
||||||
|
if (current.isNaN || current.isInfinite) return '+0.0%';
|
||||||
|
|
||||||
final trendPercentage = ((current - previous) / previous) * 100;
|
final trendPercentage = ((current - previous) / previous) * 100;
|
||||||
final sign = trendPercentage >= 0 ? '+' : '';
|
|
||||||
|
|
||||||
return '$sign${trendPercentage.toStringAsFixed(1)}%';
|
// Check if trendPercentage is valid
|
||||||
|
if (trendPercentage.isNaN || trendPercentage.isInfinite) return '+0.0%';
|
||||||
|
|
||||||
|
final sign = trendPercentage >= 0 ? '+' : '';
|
||||||
|
return '$sign${trendPercentage.round()}%';
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildSummaryCards() {
|
Widget _buildSummaryCards() {
|
||||||
@ -201,8 +206,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
{
|
{
|
||||||
'title': 'Laba Kotor',
|
'title': 'Laba Kotor',
|
||||||
'value': _formatCurrency(data.summary.grossProfit),
|
'value': _formatCurrency(data.summary.grossProfit),
|
||||||
'subtitle':
|
'subtitle': '${_safeRound(data.summary.grossProfitMargin)}% margin',
|
||||||
'${data.summary.grossProfitMargin.toStringAsFixed(1)}% margin',
|
|
||||||
'icon': Icons.trending_up,
|
'icon': Icons.trending_up,
|
||||||
'color': AppColorProfitLoss.primary,
|
'color': AppColorProfitLoss.primary,
|
||||||
'trend': _calculateTrend('grossProfit'),
|
'trend': _calculateTrend('grossProfit'),
|
||||||
@ -210,8 +214,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
{
|
{
|
||||||
'title': 'Laba Bersih',
|
'title': 'Laba Bersih',
|
||||||
'value': _formatCurrency(data.summary.netProfit),
|
'value': _formatCurrency(data.summary.netProfit),
|
||||||
'subtitle':
|
'subtitle': '${_safeRound(data.summary.netProfitMargin)}% margin',
|
||||||
'${data.summary.netProfitMargin.toStringAsFixed(1)}% margin',
|
|
||||||
'icon': Icons.account_balance,
|
'icon': Icons.account_balance,
|
||||||
'color': AppColorProfitLoss.info,
|
'color': AppColorProfitLoss.info,
|
||||||
'trend': _calculateTrend('netProfit'),
|
'trend': _calculateTrend('netProfit'),
|
||||||
@ -360,13 +363,17 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
showTitles: true,
|
showTitles: true,
|
||||||
reservedSize: 50,
|
reservedSize: 50,
|
||||||
getTitlesWidget: (value, meta) {
|
getTitlesWidget: (value, meta) {
|
||||||
|
final kValue = (value / 1000);
|
||||||
|
if (kValue.isFinite) {
|
||||||
return Text(
|
return Text(
|
||||||
'${(value / 1000).toInt()}K',
|
'${kValue.toInt()}K',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
color: Colors.grey[600],
|
color: Colors.grey[600],
|
||||||
fontSize: 10,
|
fontSize: 10,
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
|
}
|
||||||
|
return const SizedBox();
|
||||||
},
|
},
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
@ -402,8 +409,9 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
// Garis Pendapatan
|
// Garis Pendapatan
|
||||||
LineChartBarData(
|
LineChartBarData(
|
||||||
spots: data.data.asMap().entries.map((entry) {
|
spots: data.data.asMap().entries.map((entry) {
|
||||||
|
final revenue = entry.value.revenue.toDouble();
|
||||||
return FlSpot(
|
return FlSpot(
|
||||||
entry.key.toDouble(), entry.value.revenue.toDouble());
|
entry.key.toDouble(), revenue.isFinite ? revenue : 0);
|
||||||
}).toList(),
|
}).toList(),
|
||||||
isCurved: true,
|
isCurved: true,
|
||||||
color: AppColorProfitLoss.info,
|
color: AppColorProfitLoss.info,
|
||||||
@ -412,8 +420,9 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
// Garis Biaya
|
// Garis Biaya
|
||||||
LineChartBarData(
|
LineChartBarData(
|
||||||
spots: data.data.asMap().entries.map((entry) {
|
spots: data.data.asMap().entries.map((entry) {
|
||||||
|
final cost = entry.value.cost.toDouble();
|
||||||
return FlSpot(
|
return FlSpot(
|
||||||
entry.key.toDouble(), entry.value.cost.toDouble());
|
entry.key.toDouble(), cost.isFinite ? cost : 0);
|
||||||
}).toList(),
|
}).toList(),
|
||||||
isCurved: true,
|
isCurved: true,
|
||||||
color: AppColorProfitLoss.danger,
|
color: AppColorProfitLoss.danger,
|
||||||
@ -422,8 +431,9 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
// Garis Laba Bersih
|
// Garis Laba Bersih
|
||||||
LineChartBarData(
|
LineChartBarData(
|
||||||
spots: data.data.asMap().entries.map((entry) {
|
spots: data.data.asMap().entries.map((entry) {
|
||||||
|
final netProfit = entry.value.netProfit.toDouble();
|
||||||
return FlSpot(entry.key.toDouble(),
|
return FlSpot(entry.key.toDouble(),
|
||||||
entry.value.netProfit.toDouble());
|
netProfit.isFinite ? netProfit : 0);
|
||||||
}).toList(),
|
}).toList(),
|
||||||
isCurved: true,
|
isCurved: true,
|
||||||
color: AppColorProfitLoss.success,
|
color: AppColorProfitLoss.success,
|
||||||
@ -506,9 +516,10 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Widget _buildProductItem(ProfitLossProduct product) {
|
Widget _buildProductItem(ProfitLossProduct product) {
|
||||||
final profitColor = product.grossProfitMargin >= 35
|
final profitMargin = _safeDouble(product.grossProfitMargin);
|
||||||
|
final profitColor = profitMargin >= 35
|
||||||
? AppColorProfitLoss.success
|
? AppColorProfitLoss.success
|
||||||
: product.grossProfitMargin >= 25
|
: profitMargin >= 25
|
||||||
? AppColorProfitLoss.warning
|
? AppColorProfitLoss.warning
|
||||||
: AppColorProfitLoss.danger;
|
: AppColorProfitLoss.danger;
|
||||||
|
|
||||||
@ -572,7 +583,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
borderRadius: BorderRadius.circular(6),
|
borderRadius: BorderRadius.circular(6),
|
||||||
),
|
),
|
||||||
child: Text(
|
child: Text(
|
||||||
'${product.grossProfitMargin.toStringAsFixed(1)}%',
|
'${_safeRound(profitMargin)}%',
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: 12,
|
fontSize: 12,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
@ -710,13 +721,17 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
startDegreeOffset: -90,
|
startDegreeOffset: -90,
|
||||||
sections: breakdownData.asMap().entries.map((entry) {
|
sections: breakdownData.asMap().entries.map((entry) {
|
||||||
final item = entry.value;
|
final item = entry.value;
|
||||||
|
final grossProfit = data.summary.grossProfit;
|
||||||
|
final value = item['value'] as int;
|
||||||
|
|
||||||
|
// Handle division by zero
|
||||||
final percentage =
|
final percentage =
|
||||||
(item['value'] as int) / data.summary.grossProfit * 100;
|
grossProfit > 0 ? (value / grossProfit * 100) : 0.0;
|
||||||
|
|
||||||
return PieChartSectionData(
|
return PieChartSectionData(
|
||||||
color: item['color'] as Color,
|
color: item['color'] as Color,
|
||||||
value: (item['value'] as int).toDouble(),
|
value: value.toDouble(),
|
||||||
title: '${percentage.toStringAsFixed(1)}%',
|
title: '${_safeRound(percentage)}%',
|
||||||
radius: 40,
|
radius: 40,
|
||||||
titleStyle: const TextStyle(
|
titleStyle: const TextStyle(
|
||||||
fontSize: 10,
|
fontSize: 10,
|
||||||
@ -799,7 +814,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
),
|
),
|
||||||
const SizedBox(height: 8),
|
const SizedBox(height: 8),
|
||||||
Text(
|
Text(
|
||||||
'${(data.summary.profitabilityRatio * 100).toStringAsFixed(1)}%',
|
'${_safeRound(data.summary.profitabilityRatio)}%',
|
||||||
style: const TextStyle(
|
style: const TextStyle(
|
||||||
fontSize: 24,
|
fontSize: 24,
|
||||||
fontWeight: FontWeight.bold,
|
fontWeight: FontWeight.bold,
|
||||||
@ -848,9 +863,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: _buildMetricCard(
|
child: _buildMetricCard(
|
||||||
'Nilai Rata-rata Pesanan',
|
'Nilai Rata-rata Pesanan',
|
||||||
_formatCurrency(
|
_formatCurrency(_safeCalculateAverageOrder()),
|
||||||
(data.summary.totalRevenue / data.summary.totalOrders)
|
|
||||||
.round()),
|
|
||||||
'Per transaksi',
|
'Per transaksi',
|
||||||
Icons.shopping_cart_outlined,
|
Icons.shopping_cart_outlined,
|
||||||
AppColorProfitLoss.info,
|
AppColorProfitLoss.info,
|
||||||
@ -870,7 +883,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
Expanded(
|
Expanded(
|
||||||
child: _buildMetricCard(
|
child: _buildMetricCard(
|
||||||
'Rasio Biaya',
|
'Rasio Biaya',
|
||||||
'${((data.summary.totalCost / data.summary.totalRevenue) * 100).toStringAsFixed(1)}%',
|
'${_safeCalculateCostRatio()}%',
|
||||||
'Dari total pendapatan',
|
'Dari total pendapatan',
|
||||||
Icons.pie_chart,
|
Icons.pie_chart,
|
||||||
AppColorProfitLoss.danger,
|
AppColorProfitLoss.danger,
|
||||||
@ -1026,7 +1039,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
borderRadius: BorderRadius.circular(4),
|
borderRadius: BorderRadius.circular(4),
|
||||||
),
|
),
|
||||||
child: Text(
|
child: Text(
|
||||||
'${item.netProfitMargin.toStringAsFixed(1)}%',
|
'${_safeRound(item.netProfitMargin)}%',
|
||||||
textAlign: TextAlign.center,
|
textAlign: TextAlign.center,
|
||||||
style: TextStyle(
|
style: TextStyle(
|
||||||
fontSize: 10,
|
fontSize: 10,
|
||||||
@ -1093,6 +1106,31 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Helper methods for safe calculations
|
||||||
|
int _safeRound(double value) {
|
||||||
|
if (value.isNaN || value.isInfinite) return 0;
|
||||||
|
return value.round();
|
||||||
|
}
|
||||||
|
|
||||||
|
double _safeDouble(double value) {
|
||||||
|
if (value.isNaN || value.isInfinite) return 0.0;
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
int _safeCalculateAverageOrder() {
|
||||||
|
if (data.summary.totalOrders == 0) return 0;
|
||||||
|
final average = data.summary.totalRevenue / data.summary.totalOrders;
|
||||||
|
if (average.isNaN || average.isInfinite) return 0;
|
||||||
|
return average.round();
|
||||||
|
}
|
||||||
|
|
||||||
|
int _safeCalculateCostRatio() {
|
||||||
|
if (data.summary.totalRevenue == 0) return 0;
|
||||||
|
final ratio = (data.summary.totalCost / data.summary.totalRevenue) * 100;
|
||||||
|
if (ratio.isNaN || ratio.isInfinite) return 0;
|
||||||
|
return ratio.round();
|
||||||
|
}
|
||||||
|
|
||||||
IconData _getProductIcon(String category) {
|
IconData _getProductIcon(String category) {
|
||||||
switch (category.toLowerCase()) {
|
switch (category.toLowerCase()) {
|
||||||
case 'coffee':
|
case 'coffee':
|
||||||
@ -1110,8 +1148,9 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Color _getMarginColor(double margin) {
|
Color _getMarginColor(double margin) {
|
||||||
if (margin >= 25) return AppColorProfitLoss.success;
|
final safeMargin = _safeDouble(margin);
|
||||||
if (margin >= 15) return AppColorProfitLoss.warning;
|
if (safeMargin >= 25) return AppColorProfitLoss.success;
|
||||||
|
if (safeMargin >= 15) return AppColorProfitLoss.warning;
|
||||||
return AppColorProfitLoss.danger;
|
return AppColorProfitLoss.danger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1126,7 +1165,7 @@ class ProfitLossWidget extends StatelessWidget {
|
|||||||
|
|
||||||
String _formatCurrencyShort(int amount) {
|
String _formatCurrencyShort(int amount) {
|
||||||
if (amount >= 1000000) {
|
if (amount >= 1000000) {
|
||||||
return 'Rp ${(amount / 1000000).toStringAsFixed(1)}M';
|
return 'Rp ${(amount / 1000000).round()}M';
|
||||||
} else if (amount >= 1000) {
|
} else if (amount >= 1000) {
|
||||||
return 'Rp ${(amount / 1000).toStringAsFixed(0)}K';
|
return 'Rp ${(amount / 1000).toStringAsFixed(0)}K';
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user