Compare commits

..

No commits in common. "88a3a9d6e013d5dc4718a14ebc26b296b579b857" and "551637f0ecb81ac51424da589794dc2ec3c1df39" have entirely different histories.

2 changed files with 6 additions and 74 deletions

View file

@ -56,22 +56,6 @@ struct ChatMessagesResult {
let chatClosed: Bool let chatClosed: Bool
} }
/// Backend-authoritative cash completion result.
/// All monetary values are formatted strings (e.g. "12.50") from the server.
struct CashCompletionResult {
let cashReceived: String
let orderTotal: String
let change: String
let customerFee: String?
let businessFee: String?
let cashRoutedTo: String // "worker" or "business"
let balanceApplied: String?
var changeDollars: Double {
Double(change) ?? 0
}
}
// MARK: - API Service // MARK: - API Service
actor APIService { actor APIService {
@ -490,9 +474,7 @@ actor APIService {
return arr.map { WorkTask(json: $0) } return arr.map { WorkTask(json: $0) }
} }
/// Complete a task. Returns CashCompletionResult when cashReceivedCents is provided. func completeTask(taskId: Int, workerRating: [String: Bool]? = nil, cashReceivedCents: Int? = nil, cancelOrder: Bool = false) async throws {
@discardableResult
func completeTask(taskId: Int, workerRating: [String: Bool]? = nil, cashReceivedCents: Int? = nil, cancelOrder: Bool = false) async throws -> CashCompletionResult? {
var payload: [String: Any] = [ var payload: [String: Any] = [
"TaskID": taskId, "TaskID": taskId,
"UserID": userId ?? 0 "UserID": userId ?? 0
@ -515,20 +497,6 @@ actor APIService {
} }
throw APIError.serverError("Failed to complete task: \(errorMsg)") throw APIError.serverError("Failed to complete task: \(errorMsg)")
} }
// Parse cash completion response from backend (authoritative values)
if let _ = cashReceivedCents, json["CashProcessed"] as? Bool == true {
return CashCompletionResult(
cashReceived: json["CashReceived"] as? String ?? "0.00",
orderTotal: json["OrderTotal"] as? String ?? "0.00",
change: json["Change"] as? String ?? "0.00",
customerFee: json["CustomerFee"] as? String,
businessFee: json["BusinessFee"] as? String,
cashRoutedTo: json["CashRoutedTo"] as? String ?? "worker",
balanceApplied: json["BalanceApplied"] as? String
)
}
return nil
} }
func closeChat(taskId: Int) async throws { func closeChat(taskId: Int) async throws {

View file

@ -834,7 +834,6 @@ struct CashCollectionSheet: View {
@State private var cashReceivedText = "" @State private var cashReceivedText = ""
@State private var isProcessing = false @State private var isProcessing = false
@State private var errorMessage: String? @State private var errorMessage: String?
@State private var completionResult: CashCompletionResult?
private var isAdmin: Bool { roleId >= 2 } private var isAdmin: Bool { roleId >= 2 }
@ -845,8 +844,7 @@ struct CashCollectionSheet: View {
return Int(round(dollars * 100)) return Int(round(dollars * 100))
} }
/// Client-side estimate shown before confirmation (preview only) private var changeDue: Double? {
private var estimatedChangeDue: Double? {
guard let receivedCents = cashReceivedCents else { return nil } guard let receivedCents = cashReceivedCents else { return nil }
let change = Double(receivedCents - orderTotalCents) / 100 let change = Double(receivedCents - orderTotalCents) / 100
return change >= 0 ? change : nil return change >= 0 ? change : nil
@ -890,35 +888,8 @@ struct CashCollectionSheet: View {
.cornerRadius(12) .cornerRadius(12)
} }
// Change display backend-authoritative after completion, estimate before // Change display
if let result = completionResult { if let change = changeDue {
// Show confirmed change from backend
VStack(spacing: 8) {
HStack {
Image(systemName: "checkmark.circle.fill")
.foregroundColor(.payfritGreen)
Text("Change Due:")
.font(.body.weight(.medium))
Spacer()
Text("$\(result.change)")
.font(.title3.bold())
.foregroundColor(.payfritGreen)
}
if let balanceApplied = result.balanceApplied {
HStack {
Text("Balance applied:")
.font(.caption)
.foregroundColor(.secondary)
Spacer()
Text("$\(balanceApplied)")
.font(.caption.weight(.medium))
}
}
}
.padding(16)
.background(Color.payfritGreen.opacity(0.15))
.cornerRadius(12)
} else if let change = estimatedChangeDue {
HStack { HStack {
Image(systemName: "arrow.uturn.left.circle.fill") Image(systemName: "arrow.uturn.left.circle.fill")
.foregroundColor(.payfritGreen) .foregroundColor(.payfritGreen)
@ -983,7 +954,7 @@ struct CashCollectionSheet: View {
} }
.buttonStyle(.borderedProminent) .buttonStyle(.borderedProminent)
.tint(Color(red: 0.13, green: 0.55, blue: 0.13)) .tint(Color(red: 0.13, green: 0.55, blue: 0.13))
.disabled(!isValid || isProcessing || completionResult != nil) .disabled(!isValid || isProcessing)
.padding(.bottom, 16) .padding(.bottom, 16)
} }
.padding(.horizontal, 24) .padding(.horizontal, 24)
@ -1005,14 +976,7 @@ struct CashCollectionSheet: View {
Task { Task {
do { do {
let result = try await APIService.shared.completeTask(taskId: taskId, cashReceivedCents: cents) try await APIService.shared.completeTask(taskId: taskId, cashReceivedCents: cents)
if let result = result {
// Show backend-authoritative change before dismissing
completionResult = result
isProcessing = false
// Brief pause so worker can see the confirmed change amount
try? await Task.sleep(nanoseconds: 2_000_000_000)
}
dismiss() dismiss()
onComplete() onComplete()
} catch { } catch {