diff --git a/controllers/milerAppController.go b/controllers/milerAppController.go index 1aed20b..8015400 100644 --- a/controllers/milerAppController.go +++ b/controllers/milerAppController.go @@ -349,11 +349,33 @@ func MilerDeliverConsignment(c *fiber.Ctx) error { return utils.Internal(c, "failed to record delivery history") } + // riderkms is the distance actually ridden for this booking, measured from + // the pickup point to where the rider stood when they confirmed delivery + // (falling back to the booking's delivery coordinates if the app sent none). + // ridercharges is the order amount the tenant is billed, passed in at + // creation as finalprice and stored on the service option — Doormile does + // not compute it. Both were only ever read by GET /miler/earnings and never + // written, so every completed job reported zero distance and zero value. + dropLat, dropLon := req.Lat, req.Lon + if dropLat == 0 && dropLon == 0 { + dropLat, dropLon = consignment.Deliverylatitude, consignment.Deliverylongitude + } + riderKms := haversineKM(consignment.Pickuplatitude, consignment.Pickuplongitude, dropLat, dropLon) + + var serviceOpt models.BookingServiceOption + orderAmount := 0.0 + if tx.Where("bookingid = ?", booking.Bookingid). + Order("createdat DESC").First(&serviceOpt).Error == nil { + orderAmount = serviceOpt.Estimatedprice + } + if err := tx.Model(&models.BookingAssignment{}). Where("bookingid = ? AND mileruserid = ?", booking.Bookingid, milerUserID). Updates(map[string]interface{}{ "assignmentstatus": constants.AssignmentCompleted, "completedat": time.Now(), + "riderkms": riderKms, + "ridercharges": orderAmount, }).Error; err != nil { tx.Rollback() return utils.Internal(c, "failed to close assignment") @@ -503,7 +525,10 @@ func MilerGetEarnings(c *fiber.Ctx) error { period := c.Query("period", "daily") var start, end time.Time - now := time.Now() + // Database-local, not container-local — see utils.DBNow. The rest of the + // range logic already moved off the container clock; this branch would + // otherwise put a rider in the wrong month for 5h30m either side of it. + now := utils.DBNow() switch period { case "weekly":