feat: Implement hub and role matching for order creation and remove payment, savings, and export sections from the billing page.

This commit is contained in:
Achintha Isuru
2026-02-27 21:32:12 -05:00
parent 95175536a0
commit bba4054143
4 changed files with 159 additions and 131 deletions

View File

@@ -9,7 +9,6 @@ import '../blocs/billing_bloc.dart';
import '../blocs/billing_event.dart'; import '../blocs/billing_event.dart';
import '../blocs/billing_state.dart'; import '../blocs/billing_state.dart';
import '../widgets/invoice_history_section.dart'; import '../widgets/invoice_history_section.dart';
import '../widgets/payment_method_card.dart';
import '../widgets/pending_invoices_section.dart'; import '../widgets/pending_invoices_section.dart';
import '../widgets/spending_breakdown_card.dart'; import '../widgets/spending_breakdown_card.dart';
@@ -106,14 +105,14 @@ class _BillingViewState extends State<BillingView> {
), ),
title: Text( title: Text(
t.client_billing.title, t.client_billing.title,
style: UiTypography.headline3b.copyWith(color: UiColors.white), style: UiTypography.headline3b.copyWith(
color: UiColors.white,
),
), ),
centerTitle: false, centerTitle: false,
flexibleSpace: FlexibleSpaceBar( flexibleSpace: FlexibleSpaceBar(
background: Padding( background: Padding(
padding: const EdgeInsets.only( padding: const EdgeInsets.only(bottom: UiConstants.space8),
bottom: UiConstants.space8,
),
child: Column( child: Column(
mainAxisAlignment: MainAxisAlignment.end, mainAxisAlignment: MainAxisAlignment.end,
children: <Widget>[ children: <Widget>[
@@ -224,90 +223,16 @@ class _BillingViewState extends State<BillingView> {
if (state.pendingInvoices.isNotEmpty) ...<Widget>[ if (state.pendingInvoices.isNotEmpty) ...<Widget>[
PendingInvoicesSection(invoices: state.pendingInvoices), PendingInvoicesSection(invoices: state.pendingInvoices),
], ],
const PaymentMethodCard(), // const PaymentMethodCard(),
const SpendingBreakdownCard(), const SpendingBreakdownCard(),
_buildSavingsCard(state.savings),
if (state.invoiceHistory.isNotEmpty) if (state.invoiceHistory.isNotEmpty)
InvoiceHistorySection(invoices: state.invoiceHistory), InvoiceHistorySection(invoices: state.invoiceHistory),
const SizedBox(height: UiConstants.space16),
_buildExportButton(),
const SizedBox(height: UiConstants.space12),
], ],
), ),
); );
} }
Widget _buildSavingsCard(double amount) {
return Container(
padding: const EdgeInsets.all(UiConstants.space4),
decoration: BoxDecoration(
color: const Color(0xFFFFFBEB),
borderRadius: UiConstants.radiusLg,
border: Border.all(color: UiColors.accent.withOpacity(0.5)),
),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Container(
padding: const EdgeInsets.all(UiConstants.space2),
decoration: BoxDecoration(
color: UiColors.accent,
borderRadius: UiConstants.radiusMd,
),
child: const Icon(UiIcons.trendingDown, size: 18, color: UiColors.accentForeground),
),
const SizedBox(width: UiConstants.space3),
Expanded(
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Text(
t.client_billing.rate_optimization_title,
style: UiTypography.body2b.textPrimary,
),
const SizedBox(height: 4),
Text.rich(
TextSpan(
style: UiTypography.footnote2r.textSecondary,
children: [
TextSpan(text: t.client_billing.rate_optimization_save),
TextSpan(
text: t.client_billing.rate_optimization_amount(amount: amount.toStringAsFixed(0)),
style: UiTypography.footnote2b.textPrimary,
),
TextSpan(text: t.client_billing.rate_optimization_shifts),
],
),
),
const SizedBox(height: UiConstants.space3),
SizedBox(
height: 32,
child: UiButton.primary(
text: t.client_billing.view_details,
onPressed: () {},
size: UiButtonSize.small,
),
),
],
),
),
],
),
);
}
Widget _buildExportButton() {
return SizedBox(
width: double.infinity,
child: UiButton.secondary(
text: t.client_billing.export_button,
leadingIcon: UiIcons.download,
onPressed: () {},
size: UiButtonSize.large,
),
);
}
Widget _buildEmptyState(BuildContext context) { Widget _buildEmptyState(BuildContext context) {
return Center( return Center(
child: Column( child: Column(
@@ -361,11 +286,15 @@ class _InvoicesReadyBanner extends StatelessWidget {
children: [ children: [
Text( Text(
t.client_billing.invoices_ready_title, t.client_billing.invoices_ready_title,
style: UiTypography.body1b.copyWith(color: UiColors.success), style: UiTypography.body1b.copyWith(
color: UiColors.success,
),
), ),
Text( Text(
t.client_billing.invoices_ready_subtitle, t.client_billing.invoices_ready_subtitle,
style: UiTypography.footnote2r.copyWith(color: UiColors.success), style: UiTypography.footnote2r.copyWith(
color: UiColors.success,
),
), ),
], ],
), ),

View File

@@ -14,33 +14,11 @@ class InvoiceHistorySection extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return Column( return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[ children: <Widget>[
Row( Text(
mainAxisAlignment: MainAxisAlignment.spaceBetween, t.client_billing.invoice_history,
children: <Widget>[ style: UiTypography.title2b.textPrimary,
Text(
t.client_billing.invoice_history,
style: UiTypography.title2b.textPrimary,
),
TextButton(
onPressed: () {},
style: TextButton.styleFrom(
padding: EdgeInsets.zero,
minimumSize: Size.zero,
tapTargetSize: MaterialTapTargetSize.shrinkWrap,
),
child: Row(
children: [
Text(
t.client_billing.view_all,
style: UiTypography.body2b.copyWith(color: UiColors.primary),
),
const SizedBox(width: 4),
const Icon(UiIcons.chevronRight, size: 16, color: UiColors.primary),
],
),
),
],
), ),
const SizedBox(height: UiConstants.space3), const SizedBox(height: UiConstants.space3),
Container( Container(
@@ -129,12 +107,6 @@ class _InvoiceItem extends StatelessWidget {
_StatusBadge(status: invoice.status), _StatusBadge(status: invoice.status),
], ],
), ),
const SizedBox(width: UiConstants.space4),
Icon(
UiIcons.download,
size: 20,
color: UiColors.iconSecondary.withOpacity(0.3),
),
], ],
), ),
); );

View File

@@ -21,18 +21,11 @@ class PendingInvoicesSection extends StatelessWidget {
return GestureDetector( return GestureDetector(
onTap: () => Modular.to.toAwaitingApproval(), onTap: () => Modular.to.toAwaitingApproval(),
child: Container( child: Container(
padding: const EdgeInsets.all(UiConstants.space5), padding: const EdgeInsets.all(UiConstants.space4),
decoration: BoxDecoration( decoration: BoxDecoration(
color: UiColors.white, color: UiColors.white,
borderRadius: UiConstants.radiusLg, borderRadius: UiConstants.radiusLg,
border: Border.all(color: UiColors.border.withOpacity(0.5)), border: Border.all(color: UiColors.border.withOpacity(0.5)),
boxShadow: [
BoxShadow(
color: UiColors.black.withOpacity(0.04),
blurRadius: 10,
offset: const Offset(0, 4),
),
],
), ),
child: Row( child: Row(
children: <Widget>[ children: <Widget>[

View File

@@ -382,29 +382,82 @@ class ClientCreateOrderRepositoryImpl
); );
final RapidOrderParsedData data = response.parsed; final RapidOrderParsedData data = response.parsed;
// Fetch Business ID
final String businessId = await _service.getBusinessId();
// 1. Hub Matching
final OperationResult<
dc.ListTeamHubsByOwnerIdData,
dc.ListTeamHubsByOwnerIdVariables
>
hubResult = await _service.connector
.listTeamHubsByOwnerId(ownerId: businessId)
.execute();
final List<dc.ListTeamHubsByOwnerIdTeamHubs> hubs = hubResult.data.teamHubs;
final dc.ListTeamHubsByOwnerIdTeamHubs? bestHub = _findBestHub(
hubs,
data.locationHint,
);
// 2. Roles Matching
// We fetch vendors to get the first one as a context for role matching.
final OperationResult<dc.ListVendorsData, void> vendorResult =
await _service.connector.listVendors().execute();
final List<dc.ListVendorsVendors> vendors = vendorResult.data.vendors;
String? selectedVendorId;
List<dc.ListRolesByVendorIdRoles> availableRoles =
<dc.ListRolesByVendorIdRoles>[];
if (vendors.isNotEmpty) {
selectedVendorId = vendors.first.id;
final OperationResult<
dc.ListRolesByVendorIdData,
dc.ListRolesByVendorIdVariables
>
roleResult = await _service.connector
.listRolesByVendorId(vendorId: selectedVendorId)
.execute();
availableRoles = roleResult.data.roles;
}
final DateTime startAt = final DateTime startAt =
DateTime.tryParse(data.startAt ?? '') ?? DateTime.now(); DateTime.tryParse(data.startAt ?? '') ?? DateTime.now();
final DateTime endAt = final DateTime endAt =
DateTime.tryParse(data.endAt ?? '') ?? DateTime.tryParse(data.endAt ?? '') ??
startAt.add(const Duration(hours: 8)); startAt.add(const Duration(hours: 8));
final String startTimeStr = DateFormat('hh:mm a').format(startAt); final String startTimeStr = DateFormat('hh:mm a').format(startAt.toLocal());
final String endTimeStr = DateFormat('hh:mm a').format(endAt); final String endTimeStr = DateFormat('hh:mm a').format(endAt.toLocal());
return domain.OneTimeOrder( return domain.OneTimeOrder(
date: startAt, date: startAt,
location: data.locationHint ?? '', location: bestHub?.hubName ?? data.locationHint ?? '',
eventName: data.notes ?? '', eventName: data.notes ?? '',
hub: data.locationHint != null vendorId: selectedVendorId,
hub: bestHub != null
? domain.OneTimeOrderHubDetails( ? domain.OneTimeOrderHubDetails(
id: '', id: bestHub.id,
name: data.locationHint!, name: bestHub.hubName,
address: '', address: bestHub.address,
placeId: bestHub.placeId,
latitude: bestHub.latitude ?? 0,
longitude: bestHub.longitude ?? 0,
city: bestHub.city,
state: bestHub.state,
street: bestHub.street,
country: bestHub.country,
zipCode: bestHub.zipCode,
) )
: null, : null,
positions: data.positions.map((RapidOrderPosition p) { positions: data.positions.map((RapidOrderPosition p) {
final dc.ListRolesByVendorIdRoles? matchedRole = _findBestRole(
availableRoles,
p.role,
);
return domain.OneTimeOrderPosition( return domain.OneTimeOrderPosition(
role: p.role, role: matchedRole?.id ?? p.role,
count: p.count, count: p.count,
startTime: startTimeStr, startTime: startTimeStr,
endTime: endTimeStr, endTime: endTimeStr,
@@ -656,4 +709,85 @@ class ClientCreateOrderRepositoryImpl
} }
return domain.OrderType.oneTime; return domain.OrderType.oneTime;
} }
dc.ListTeamHubsByOwnerIdTeamHubs? _findBestHub(
List<dc.ListTeamHubsByOwnerIdTeamHubs> hubs,
String? hint,
) {
if (hint == null || hint.isEmpty || hubs.isEmpty) return null;
final String normalizedHint = hint.toLowerCase();
dc.ListTeamHubsByOwnerIdTeamHubs? bestMatch;
double highestScore = -1;
for (final dc.ListTeamHubsByOwnerIdTeamHubs hub in hubs) {
final String name = hub.hubName.toLowerCase();
final String address = hub.address.toLowerCase();
double score = 0;
if (name == normalizedHint || address == normalizedHint) {
score = 100;
} else if (name.contains(normalizedHint) ||
address.contains(normalizedHint)) {
score = 80;
} else if (normalizedHint.contains(name) ||
normalizedHint.contains(address)) {
score = 60;
} else {
final List<String> hintWords = normalizedHint.split(RegExp(r'\s+'));
final List<String> hubWords = ('$name $address').split(RegExp(r'\s+'));
int overlap = 0;
for (final String word in hintWords) {
if (word.length > 2 && hubWords.contains(word)) overlap++;
}
score = overlap * 10.0;
}
if (score > highestScore) {
highestScore = score;
bestMatch = hub;
}
}
return (highestScore >= 10) ? bestMatch : null;
}
dc.ListRolesByVendorIdRoles? _findBestRole(
List<dc.ListRolesByVendorIdRoles> roles,
String? hint,
) {
if (hint == null || hint.isEmpty || roles.isEmpty) return null;
final String normalizedHint = hint.toLowerCase();
dc.ListRolesByVendorIdRoles? bestMatch;
double highestScore = -1;
for (final dc.ListRolesByVendorIdRoles role in roles) {
final String name = role.name.toLowerCase();
double score = 0;
if (name == normalizedHint) {
score = 100;
} else if (name.contains(normalizedHint)) {
score = 80;
} else if (normalizedHint.contains(name)) {
score = 60;
} else {
final List<String> hintWords = normalizedHint.split(RegExp(r'\s+'));
final List<String> roleWords = name.split(RegExp(r'\s+'));
int overlap = 0;
for (final String word in hintWords) {
if (word.length > 2 && roleWords.contains(word)) overlap++;
}
score = overlap * 10.0;
}
if (score > highestScore) {
highestScore = score;
bestMatch = role;
}
}
return (highestScore >= 10) ? bestMatch : null;
}
} }