fix: populate riderkms and ridercharges on delivery completion

riderkms, ridercharges and bonuspoints appeared in exactly three places in the
codebase — all three GET /miler/earnings reading them. Nothing ever wrote them,
so every completed job reported zero distance and zero value and the earnings
screen was permanently empty.

On delivery completion:
- riderkms is the distance ridden for the booking, pickup point to where the
  rider confirmed delivery, falling back to the booking's delivery coordinates
  when the app sends no position.
- ridercharges is the order amount the tenant is billed, supplied at creation
  as finalprice and already stored on the booking service option. Doormile does
  not compute it; this is pass-through.
- bonuspoints deliberately left at zero pending a decision on what earns them.

Also moves the monthly earnings window onto utils.DBNow, so it doesn't put a
rider in the wrong month for 5h30m either side of a month boundary.

Note historical rows keep whatever completedat they were written with before
the image gained TZ=Asia/Kolkata, so figures spanning today are mixed; not
backfilled.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
Suriya
2026-08-05 18:33:01 +05:30
parent 39dcba3d80
commit 9b3ec886ce

View File

@@ -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":