Subversion Repositories SmartDukaan

Rev

Rev 36821 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
36618 ranu 1
package com.spice.profitmandi.web.controller;
2
 
3
import com.google.gson.Gson;
4
import com.google.gson.reflect.TypeToken;
5
import com.spice.profitmandi.common.exception.ProfitMandiBusinessException;
6
import com.spice.profitmandi.common.model.CustomRetailer;
36763 ranu 7
import com.spice.profitmandi.common.model.ProfitMandiConstants;
36618 ranu 8
import com.spice.profitmandi.common.web.util.ResponseSender;
9
import com.spice.profitmandi.dao.entity.auth.AuthUser;
10
import com.spice.profitmandi.dao.entity.fofo.FofoStore;
11
import com.spice.profitmandi.dao.entity.logistics.PublicHolidays;
36644 ranu 12
import com.spice.profitmandi.dao.entity.user.*;
36618 ranu 13
import com.spice.profitmandi.dao.enumuration.cs.EscalationType;
14
import com.spice.profitmandi.dao.repository.auth.AuthRepository;
15
import com.spice.profitmandi.dao.repository.cs.CsService;
16
import com.spice.profitmandi.dao.repository.dtr.*;
17
import com.spice.profitmandi.dao.repository.logistics.PublicHolidaysRepository;
18
import com.spice.profitmandi.service.user.RetailerService;
19
import com.spice.profitmandi.web.model.LoginDetails;
20
import com.spice.profitmandi.web.util.CookiesProcessor;
21
import org.apache.logging.log4j.LogManager;
22
import org.apache.logging.log4j.Logger;
23
import org.springframework.beans.factory.annotation.Autowired;
24
import org.springframework.http.ResponseEntity;
25
import org.springframework.stereotype.Controller;
26
import org.springframework.transaction.annotation.Transactional;
27
import org.springframework.ui.Model;
28
import org.springframework.web.bind.annotation.GetMapping;
29
import org.springframework.web.bind.annotation.PostMapping;
30
import org.springframework.web.bind.annotation.RequestParam;
31
 
32
import javax.servlet.http.HttpServletRequest;
33
import java.lang.reflect.Type;
34
import java.time.DayOfWeek;
35
import java.time.LocalDate;
36
import java.time.LocalDateTime;
37
import java.time.YearMonth;
38
import java.time.format.DateTimeFormatter;
39
import java.util.*;
40
import java.util.stream.Collectors;
41
 
42
@Controller
43
@Transactional(rollbackFor = Throwable.class)
44
public class BeatPlanController {
45
	private static final Logger LOGGER = LogManager.getLogger(BeatPlanController.class);
46
	private static final String[] BEAT_COLORS = {
47
			"#3498DB", "#E74C3C", "#2ECC71", "#9B59B6", "#F39C12",
48
			"#1ABC9C", "#E67E22", "#34495E", "#16A085", "#C0392B"
49
	};
50
	@Autowired
51
	private CsService csService;
52
	@Autowired
53
	private AuthRepository authRepository;
36686 ranu 54
	// Emails that bypass hierarchy and role gates — single source of truth.
55
	private static final Set<String> SUPER_ADMIN_EMAILS = new HashSet<>(Arrays.asList(
56
			"tarun.verma@smartdukaan.com"
57
	));
36618 ranu 58
	@Autowired
36686 ranu 59
	private com.spice.profitmandi.service.AuthService authService;
36618 ranu 60
	@Autowired
36686 ranu 61
	private com.spice.profitmandi.dao.repository.cs.PositionRepository positionRepository;
62
	@Autowired
36618 ranu 63
	private RetailerService retailerService;
64
	@Autowired
36644 ranu 65
	private BeatRepository beatRepository;
36618 ranu 66
	@Autowired
36644 ranu 67
	private BeatRouteRepository beatRouteRepository;
36618 ranu 68
	@Autowired
36644 ranu 69
	private BeatScheduleRepository beatScheduleRepository;
70
	@Autowired
71
	private LeadRouteRepository leadRouteRepository;
72
	@Autowired
36650 ranu 73
	private com.spice.profitmandi.dao.service.BeatPlanQueryService beatPlanQueryService;
74
	@Autowired
36618 ranu 75
	private AuthUserLocationRepository authUserLocationRepository;
76
	@Autowired
77
	private LeadRepository leadRepository;
78
	@Autowired
79
	private PublicHolidaysRepository publicHolidaysRepository;
80
	@Autowired
81
	private com.spice.profitmandi.service.GeocodingService geocodingService;
82
	@Autowired
83
	private CookiesProcessor cookiesProcessor;
84
	@Autowired
85
	private ResponseSender responseSender;
36686 ranu 86
	@Autowired
87
	private FofoStoreRepository fofoStoreRepository;
36811 ranu 88
	@Autowired
89
	private com.spice.profitmandi.dao.repository.logistics.CompanyOfficeRepository companyOfficeRepository;
36618 ranu 90
 
91
	@GetMapping(value = "/beatPlan")
36686 ranu 92
	public String beatPlan(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
93
		model.addAttribute("escalationTypes", visibleLevelsFor(request));
36618 ranu 94
		return "beat-plan";
95
	}
96
 
36650 ranu 97
	@Autowired
98
	private com.spice.profitmandi.dao.repository.dtr.LeadLiveLocationRepository leadLiveLocationRepositoryAuto;
36651 ranu 99
	@Autowired
100
	private com.spice.profitmandi.dao.repository.dtr.LeadActivityRepository leadActivityRepositoryAuto;
36663 ranu 101
	@Autowired
102
	private com.spice.profitmandi.dao.repository.dtr.UserRepository userRepositoryAuto;
103
	@Autowired
104
	private com.spice.profitmandi.common.web.client.RestClient restClientAuto;
105
	@Autowired
106
	private com.spice.profitmandi.dao.repository.auth.LocationTrackingRepository locationTrackingRepositoryAuto;
36740 ranu 107
	@Autowired
108
	private com.spice.profitmandi.dao.repository.dtr.BeatDeferredVisitRepository beatDeferredVisitRepository;
36650 ranu 109
 
36655 ranu 110
	private static Double parseDoubleOrNull(String s) {
111
		if (s == null || s.trim().isEmpty()) return null;
112
		try {
113
			return Double.parseDouble(s.trim());
114
		} catch (NumberFormatException e) {
115
			return null;
116
		}
117
	}
118
 
119
	private static double haversineKm(double lat1, double lng1, double lat2, double lng2) {
120
		double R = 6371;
121
		double dLat = Math.toRadians(lat2 - lat1);
122
		double dLng = Math.toRadians(lng2 - lng1);
123
		double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
124
				+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
125
				* Math.sin(dLng / 2) * Math.sin(dLng / 2);
126
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
127
		return R * c;
128
	}
129
 
36711 ranu 130
    // Mirrors the JS recalcDay() formula. Used by schedule/repeat endpoints
131
    // which create fresh BeatSchedule rows — they need to fill totals from the
132
    // existing beat_route table, not from anything the client posted.
133
    // Returns {totalKm, totalMins}.
134
    private double[] computeDayTotals(int beatId, int dayNumber, String endAction) {
135
        Beat beat = beatRepository.selectById(beatId);
136
        if (beat == null) return new double[]{0d, 0d};
137
 
138
        List<BeatRoute> dayRoutes = beatRouteRepository.selectByBeatId(beatId).stream()
139
                .filter(r -> r.getDayNumber() == dayNumber)
140
                .sorted(java.util.Comparator.comparingInt(BeatRoute::getSequenceOrder))
141
                .collect(Collectors.toList());
142
        if (dayRoutes.isEmpty()) return new double[]{0d, 0d};
143
 
144
        List<Integer> fofoIds = dayRoutes.stream().map(BeatRoute::getFofoId).distinct().collect(Collectors.toList());
145
        Map<Integer, FofoStore> storeMap = new HashMap<>();
146
        try {
147
            for (FofoStore fs : fofoStoreRepository.selectByRetailerIds(fofoIds)) {
148
                storeMap.put(fs.getId(), fs);
149
            }
150
        } catch (Exception ignored) { /* fall through with empty map */ }
151
 
152
        double ROAD_FACTOR = 1.3;
153
        double AVG_SPEED = 30.0; // km/h
154
        int VISIT_MINS = 30;
155
 
156
        Double prevLat = parseDoubleOrNull(beat.getStartLatitude());
157
        Double prevLng = parseDoubleOrNull(beat.getStartLongitude());
158
 
159
        double totalKm = 0d;
160
        for (BeatRoute r : dayRoutes) {
161
            FofoStore fs = storeMap.get(r.getFofoId());
162
            if (fs == null) continue;
163
            Double curLat = parseDoubleOrNull(fs.getLatitude());
164
            Double curLng = parseDoubleOrNull(fs.getLongitude());
165
            if (prevLat != null && prevLng != null && curLat != null && curLng != null) {
166
                totalKm += haversineKm(prevLat, prevLng, curLat, curLng) * ROAD_FACTOR;
167
            }
168
            if (curLat != null && curLng != null) {
169
                prevLat = curLat;
170
                prevLng = curLng;
171
            }
172
        }
173
 
174
        // Return-home leg only when end_action='HOME'
175
        if ("HOME".equalsIgnoreCase(endAction)) {
176
            Double homeLat = parseDoubleOrNull(beat.getStartLatitude());
177
            Double homeLng = parseDoubleOrNull(beat.getStartLongitude());
178
            if (prevLat != null && prevLng != null && homeLat != null && homeLng != null) {
179
                totalKm += haversineKm(prevLat, prevLng, homeLat, homeLng) * ROAD_FACTOR;
180
            }
181
        }
182
 
183
        double totalMins = (totalKm / AVG_SPEED) * 60.0 + dayRoutes.size() * VISIT_MINS;
184
        return new double[]{Math.round(totalKm * 1000d) / 1000d, Math.round(totalMins)};
185
    }
186
 
36663 ranu 187
	// ====================== ASSIGN VISIT ======================
188
	// Day View "Assign Visit" — lets an admin pick parties (stores) for a specific
189
	// auth user on a specific date and pushes them as visit tasks to the v2
190
	// /profitmandi-web/v2/beat-tracking/batch endpoint.
191
 
36716 ranu 192
	// List of parties (stores) assigned to this auth user + their dtr.users.id.
193
	// When date+beatId are passed, each party is also tagged with:
194
	//   inBeat        = is this store part of the scheduled beat's route on that date
195
	//   existingAgendas[] = agendas already saved for this store on that date (so the
196
	//                       modal can pre-fill them and let the user refill rather than re-assign)
36663 ranu 197
	@GetMapping(value = "/beatPlan/assignVisit/parties")
36716 ranu 198
	public ResponseEntity<?> assignVisitParties(
199
			@RequestParam int authUserId,
200
			@RequestParam(required = false) String date,
201
			@RequestParam(required = false) Integer beatId) throws Exception {
36663 ranu 202
		AuthUser au = authRepository.selectById(authUserId);
203
		if (au == null) return responseSender.badRequest("Auth user not found");
204
 
205
		// Map auth_user → dtr.users via email
206
		Integer dtrUserId = null;
207
		try {
208
			com.spice.profitmandi.dao.entity.dtr.User dtrUser =
209
					userRepositoryAuto.selectByEmailId(au.getEmailId());
210
			if (dtrUser != null) dtrUserId = dtrUser.getId();
211
		} catch (Exception ignored) {
212
		}
213
 
36716 ranu 214
		// Parse optional date
215
		LocalDate parsedDate = null;
216
		if (date != null && !date.isEmpty()) {
217
			try {
218
				parsedDate = LocalDate.parse(date);
219
			} catch (Exception ignored) {
220
			}
221
		}
222
 
223
		// Build (fofoId → dayNumber) of partners already in the beat's scheduled route for this date
224
		Set<Integer> inBeatFofoIds = new HashSet<>();
225
		if (beatId != null && parsedDate != null) {
226
			final LocalDate dateF = parsedDate; // capture for lambda (parsedDate is reassigned earlier so not effectively final)
227
			List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(beatId);
228
			BeatSchedule match = schedules.stream()
229
					.filter(s -> s.getStartDate() != null && s.getStartDate().equals(dateF))
230
					.findFirst().orElse(null);
231
			if (match != null) {
232
				List<BeatRoute> routes = beatRouteRepository.selectByBeatId(beatId);
233
				routes.stream()
234
						.filter(r -> r.getDayNumber() == match.getDayNumber() && r.isActive())
235
						.forEach(r -> inBeatFofoIds.add(r.getFofoId()));
236
			}
237
		}
238
 
239
		// Build (fofoId → existingAgendas) and (fofoId → existingDescription) from
240
		// any already-saved location_tracking rows for this user on this date.
241
		// Agenda is stored as task_name = "agenda1, agenda2 | OutletName"
242
		// so we split on " | " to peel the outlet suffix off, then split agendas by ", ".
243
		// Description is stored on task_description (free text).
244
		Map<Integer, List<String>> existingAgendaByFofo = new HashMap<>();
245
		Map<Integer, String> existingDescByFofo = new HashMap<>();
246
		Map<Integer, Integer> existingTrackingIdByFofo = new HashMap<>();
247
		if (dtrUserId != null && parsedDate != null) {
248
			List<com.spice.profitmandi.dao.entity.auth.LocationTracking> existing =
249
					locationTrackingRepositoryAuto.findByUserAndDate(dtrUserId, parsedDate);
250
			for (com.spice.profitmandi.dao.entity.auth.LocationTracking lt : existing) {
251
				if (!"franchisee-visit".equals(lt.getTaskType())) continue;
252
				if (existingAgendaByFofo.containsKey(lt.getTaskId())) continue; // first wins
253
				String taskName = lt.getTaskName() == null ? "" : lt.getTaskName();
254
				String agendaPart = taskName;
255
				int pipeIdx = taskName.lastIndexOf(" | ");
256
				if (pipeIdx > 0) agendaPart = taskName.substring(0, pipeIdx);
257
				List<String> agendas = new ArrayList<>();
258
				for (String a : agendaPart.split(",")) {
259
					String trimmed = a.trim();
260
					if (!trimmed.isEmpty()) agendas.add(trimmed);
261
				}
262
				existingAgendaByFofo.put(lt.getTaskId(), agendas);
263
				existingDescByFofo.put(lt.getTaskId(), lt.getTaskDescription() != null ? lt.getTaskDescription() : "");
264
				existingTrackingIdByFofo.put(lt.getTaskId(), lt.getId());
265
			}
266
		}
267
 
36663 ranu 268
		Map<Integer, List<Integer>> mapping = csService.getAuthUserIdPartnerIdMapping();
269
		List<Integer> fofoIds = mapping.get(authUserId);
270
 
271
		List<Map<String, Object>> parties = new ArrayList<>();
272
		if (fofoIds != null && !fofoIds.isEmpty()) {
273
			List<FofoStore> stores = fofoStoreRepository.selectByRetailerIds(fofoIds);
274
			Map<Integer, CustomRetailer> retailerMap = retailerService.getFofoRetailers(fofoIds);
275
			for (FofoStore store : stores) {
276
				if (!store.isActive() || store.isClosed()) continue;
277
				CustomRetailer retailer = retailerMap.get(store.getId());
278
				Map<String, Object> p = new HashMap<>();
279
				p.put("fofoStoreId", store.getId());
280
				p.put("code", store.getCode());
281
				p.put("outletName", store.getOutletName() != null ? store.getOutletName()
282
						: (retailer != null ? retailer.getBusinessName() : "Store #" + store.getId()));
283
				p.put("latitude", store.getLatitude());
284
				p.put("longitude", store.getLongitude());
285
				p.put("city", retailer != null && retailer.getAddress() != null ? retailer.getAddress().getCity() : null);
36716 ranu 286
				p.put("inBeat", inBeatFofoIds.contains(store.getId()));
287
				p.put("existingAgendas", existingAgendaByFofo.getOrDefault(store.getId(), new ArrayList<>()));
288
				p.put("existingDescription", existingDescByFofo.getOrDefault(store.getId(), ""));
289
				p.put("existingTrackingId", existingTrackingIdByFofo.get(store.getId()));
36663 ranu 290
				parties.add(p);
291
			}
36716 ranu 292
			// In-beat first, then by code
293
			parties.sort((a, b) -> {
294
				boolean ai = Boolean.TRUE.equals(a.get("inBeat"));
295
				boolean bi = Boolean.TRUE.equals(b.get("inBeat"));
296
				if (ai != bi) return ai ? -1 : 1;
297
				return String.valueOf(a.get("code")).compareToIgnoreCase(String.valueOf(b.get("code")));
298
			});
36663 ranu 299
		}
300
 
301
		Map<String, Object> result = new HashMap<>();
302
		result.put("dtrUserId", dtrUserId);
303
		result.put("authUserId", authUserId);
304
		result.put("userName", au.getFirstName() + " " + au.getLastName());
305
		result.put("parties", parties);
36716 ranu 306
		result.put("agendaOptions", com.spice.profitmandi.dao.enumuration.dtr.VisitAgenda.labels());
36663 ranu 307
		return responseSender.ok(result);
308
	}
309
 
310
	// Submit assignment — accepts a JSON body, builds the v2 payload, posts it
311
	@PostMapping(value = "/beatPlan/assignVisit/submit")
312
	public ResponseEntity<?> assignVisitSubmit(
313
			HttpServletRequest request,
314
			@org.springframework.web.bind.annotation.RequestBody Map<String, Object> body) throws Exception {
315
 
316
		Integer authUserId = body.get("authUserId") != null ? ((Number) body.get("authUserId")).intValue() : null;
317
		String planDate = (String) body.get("planDate");
318
		List<Map<String, Object>> selected = (List<Map<String, Object>>) body.get("parties");
319
		if (authUserId == null || planDate == null || selected == null || selected.isEmpty()) {
320
			return responseSender.badRequest("authUserId, planDate and parties are required");
321
		}
322
 
323
		AuthUser au = authRepository.selectById(authUserId);
324
		if (au == null) return responseSender.badRequest("Auth user not found");
325
 
326
		// Map auth → dtr.users.id (this is the userId the v2 endpoint expects)
327
		com.spice.profitmandi.dao.entity.dtr.User dtrUser;
328
		try {
329
			dtrUser = userRepositoryAuto.selectByEmailId(au.getEmailId());
330
		} catch (Exception e) {
331
			return responseSender.badRequest("Failed to look up dtr.users for this auth user");
332
		}
333
		if (dtrUser == null) {
334
			return responseSender.badRequest("No dtr.users record found for auth user " + authUserId);
335
		}
336
		int dtrUserId = dtrUser.getId();
337
 
338
		// Persist directly via the shared DAO — mirrors BeatTrackingController.createBatch
339
		// in profitmandi-web. We can't autowire a controller across WARs, but
340
		// LocationTrackingRepository lives in profitmandi-dao and is shared.
341
		LocalDate taskDate;
342
		try {
343
			taskDate = LocalDate.parse(planDate);
344
		} catch (Exception e) {
345
			return responseSender.badRequest("Invalid planDate (expected yyyy-MM-dd): " + planDate);
346
		}
347
 
36716 ranu 348
		// Existing rows for this user on this date — keyed by fofoStoreId.
349
		// If a row already exists for a party, we UPDATE its agenda instead of
350
		// creating a duplicate (this is the "refill agenda" path for already-
351
		// assigned parties).
352
		Map<Integer, com.spice.profitmandi.dao.entity.auth.LocationTracking> existingByFofo = new HashMap<>();
353
		for (com.spice.profitmandi.dao.entity.auth.LocationTracking lt :
354
				locationTrackingRepositoryAuto.findByUserAndDate(dtrUserId, taskDate)) {
355
			if (!"franchisee-visit".equals(lt.getTaskType())) continue;
356
			existingByFofo.putIfAbsent(lt.getTaskId(), lt);
357
		}
358
 
359
		// Defence-in-depth: Assign Visit is only valid for today's run. Hiding the
360
		// button on the UI isn't enough — block at the API too.
361
		if (!taskDate.equals(LocalDate.now())) {
362
			return responseSender.badRequest("Visits can only be assigned for today's date (" + LocalDate.now() + ")");
363
		}
364
 
36663 ranu 365
		LocalDateTime now = LocalDateTime.now();
36716 ranu 366
		int createdCount = 0, updatedCount = 0;
36663 ranu 367
 
368
		for (Map<String, Object> p : selected) {
369
			Integer fofoStoreId = ((Number) p.get("fofoStoreId")).intValue();
370
			String outletName = (String) p.get("outletName");
371
			String lat = (String) p.get("latitude");
372
			String lng = (String) p.get("longitude");
36716 ranu 373
			String description = (String) p.get("description");
374
			if (description != null) description = description.trim();
375
			if (description == null) description = "";
36663 ranu 376
 
36716 ranu 377
			// Multi-agenda: accept agendas[] (new format) or fall back to agenda (legacy single)
378
			List<String> agendas = new ArrayList<>();
379
			Object rawAgendas = p.get("agendas");
380
			if (rawAgendas instanceof List) {
381
				for (Object o : (List<?>) rawAgendas) {
382
					if (o != null) {
383
						String s = String.valueOf(o).trim();
384
						if (!s.isEmpty()) agendas.add(s);
385
					}
386
				}
387
			}
388
			if (agendas.isEmpty()) {
389
				String single = (String) p.get("agenda");
390
				if (single != null && !single.trim().isEmpty()) agendas.add(single.trim());
391
			}
392
			if (agendas.isEmpty()) agendas.add("Visit");
393
			String agendaJoined = String.join(", ", agendas);
394
 
36663 ranu 395
			String visitLocation = (lat != null && lng != null && !lat.isEmpty() && !lng.isEmpty())
396
					? (lat + "," + lng) : "0.0000,0.0000";
397
 
398
			String displayName = (outletName != null && !outletName.isEmpty()) ? outletName : ("Store #" + fofoStoreId);
36716 ranu 399
			String newTaskName = agendaJoined + " | " + displayName;
36663 ranu 400
 
36716 ranu 401
			com.spice.profitmandi.dao.entity.auth.LocationTracking existing = existingByFofo.get(fofoStoreId);
402
			if (existing != null) {
403
				// Refill — agenda (task_name), description, and visit location change
404
				existing.setTaskName(newTaskName);
405
				existing.setTaskDescription(description);
406
				existing.setVisitLocation(visitLocation);
407
				existing.setUpdatedTimestamp(now);
408
				locationTrackingRepositoryAuto.persist(existing);
409
				updatedCount++;
410
				continue;
411
			}
412
 
36663 ranu 413
			com.spice.profitmandi.dao.entity.auth.LocationTracking row =
414
					new com.spice.profitmandi.dao.entity.auth.LocationTracking();
415
			row.setUserId(dtrUserId);
416
			row.setDeviceId("0");
417
			row.setTaskId(fofoStoreId);
418
			row.setTaskDate(taskDate);
36716 ranu 419
			row.setTaskName(newTaskName);
420
			row.setTaskDescription(description);
36663 ranu 421
			row.setTaskType("franchisee-visit");
36764 ranu 422
			row.setMarkType(String.valueOf(ProfitMandiConstants.MARK_TYPE.PENDING));
36663 ranu 423
			row.setAddress("");
424
			row.setVisitLocation(visitLocation);
425
			row.setCheckInLatLng("0.0000,0.0000");
426
			row.setCheckOutLatLng("0.0000,0.0000");
427
			row.setCheckInTime(java.time.LocalTime.MIDNIGHT);
428
			row.setCheckOutTime(java.time.LocalTime.MIDNIGHT);
429
			row.setTransitTime(java.time.LocalTime.MIDNIGHT);
430
			row.setTimeSpent(java.time.LocalTime.MIDNIGHT);
431
			row.setEstimatedTime(java.time.LocalTime.MIDNIGHT);
432
			row.setSessionStartTime(java.time.LocalTime.MIDNIGHT);
433
			row.setSessionEndTime(java.time.LocalTime.MIDNIGHT);
434
			row.setTotalDistance("0.0");
435
			row.setStatus(false);
436
			row.setCreatedTimestamp(now);
437
			row.setUpdatedTimestamp(now);
438
 
439
			// Do NOT try/catch this — if persist throws, let it propagate so
440
			// @Transactional(rollbackFor = Throwable.class) rolls back cleanly.
441
			locationTrackingRepositoryAuto.persist(row);
36716 ranu 442
			createdCount++;
36663 ranu 443
		}
36716 ranu 444
		LOGGER.info("assignVisit dtrUserId={} created={} updated={}", dtrUserId, createdCount, updatedCount);
36663 ranu 445
 
446
		Map<String, Object> result = new HashMap<>();
447
		result.put("status", true);
36716 ranu 448
		result.put("createdCount", createdCount);
449
		result.put("updatedCount", updatedCount);
36663 ranu 450
		result.put("dtrUserId", dtrUserId);
36716 ranu 451
		StringBuilder msg = new StringBuilder();
452
		if (createdCount > 0)
453
			msg.append(createdCount).append(" new visit").append(createdCount == 1 ? "" : "s").append(" assigned");
454
		if (updatedCount > 0) {
455
			if (msg.length() > 0) msg.append(", ");
456
			msg.append(updatedCount).append(" existing agenda").append(updatedCount == 1 ? "" : "s").append(" refilled");
457
		}
458
		msg.append(" for ").append(au.getFirstName()).append(" ").append(au.getLastName());
459
		result.put("message", msg.toString());
36663 ranu 460
		return responseSender.ok(result);
461
	}
462
 
36740 ranu 463
	// ====================== DEFERRED PARTNERS ======================
464
	// Heads review partners that weren't visited on their planned day and act on
465
	// them. The deferral lifecycle lives in user.beat_deferred_visit (separate
466
	// from the raw location_tracking event log). Detection = explicit
467
	// (mark_type='DEFERRED') + derived (planned beat_route minus completed visits).
468
 
469
	// Page
470
	@GetMapping(value = "/beatPlan/deferredView")
471
	public String deferredView(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
472
		model.addAttribute("escalationTypes", visibleLevelsFor(request));
473
		return "beat-plan-deferred";
474
	}
475
 
476
	// List (syncs the table first, then returns the head's downline deferrals).
477
	@GetMapping(value = "/beatPlan/deferred")
478
	public ResponseEntity<?> deferredList(
479
			HttpServletRequest request,
480
			@RequestParam(required = false) String startDate,
481
			@RequestParam(required = false) String endDate) throws Exception {
482
 
483
		LocalDate start, end;
484
		try {
485
			start = (startDate == null || startDate.isEmpty()) ? LocalDate.now().minusDays(7) : LocalDate.parse(startDate);
486
			end = (endDate == null || endDate.isEmpty()) ? LocalDate.now() : LocalDate.parse(endDate);
487
		} catch (Exception e) {
488
			return responseSender.badRequest("Invalid date — expected yyyy-MM-dd");
489
		}
490
 
491
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
492
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
493
		if (me == null) return responseSender.unauthorized("Not logged in");
494
 
495
		// PURE READ. Deferrals are persisted at the write point (BeatTrackingController,
496
		// when mark_type='DEFERRED' is recorded) — this endpoint never writes.
497
		List<BeatDeferredVisit> source;
498
		if (isSuperAdmin(me)) {
499
			source = beatDeferredVisitRepository.selectByDateRange(start, end);
500
		} else {
501
			Set<Integer> downline = new HashSet<>(authService.getAllReportees(me.getId()));
502
			downline.add(me.getId());
503
			source = beatDeferredVisitRepository.selectByAuthUserIdsAndDateRange(new ArrayList<>(downline), start, end);
504
		}
505
		List<BeatDeferredVisit> rows = source.stream()
506
				.filter(r -> "DEFERRED".equals(r.getStatus()))
507
				.collect(Collectors.toList());
508
 
509
		// ---- nextScheduledDate (info only: does a future run already cover it?) ----
510
		// Only meaningful for partner visits (leads aren't on beat_route). Purely a
511
		// hint — the row stays actionable even when auto-covered, since the next run
512
		// could be far off.
513
		Map<Integer, Map<Integer, LocalDate>> coverCache = new HashMap<>();
514
		Map<Integer, LocalDate> nextByRowId = new HashMap<>();
515
		for (BeatDeferredVisit r : rows) {
516
			if (!"franchisee-visit".equals(r.getTaskType())) continue;
517
			Map<Integer, LocalDate> cover = coverCache.computeIfAbsent(r.getAuthUserId(), this::computeFutureCover);
518
			LocalDate next = cover.get(r.getFofoId());
519
			if (next != null) nextByRowId.put(r.getId(), next);
520
		}
521
 
522
		// ---- resolve user names (display name + type already denormalized on the row) ----
523
		Set<Integer> authIds = rows.stream().map(BeatDeferredVisit::getAuthUserId).collect(Collectors.toSet());
524
		Map<Integer, AuthUser> userMap = new HashMap<>();
525
		if (!authIds.isEmpty())
526
			authRepository.selectByIds(new ArrayList<>(authIds)).forEach(u -> userMap.put(u.getId(), u));
527
 
528
		List<Map<String, Object>> out = new ArrayList<>();
529
		for (BeatDeferredVisit r : rows) {
530
			AuthUser u = userMap.get(r.getAuthUserId());
531
			boolean isLead = "lead".equalsIgnoreCase(r.getTaskType());
36811 ranu 532
			boolean isOffice = "office-visit".equalsIgnoreCase(r.getTaskType());
36740 ranu 533
			Map<String, Object> row = new HashMap<>();
534
			row.put("id", r.getId());
535
			row.put("authUserId", r.getAuthUserId());
536
			row.put("userName", u != null ? (u.getFirstName() + " " + u.getLastName()) : ("User #" + r.getAuthUserId()));
537
			row.put("fofoStoreId", r.getFofoId());
538
			row.put("name", r.getDisplayName() != null ? r.getDisplayName() : ("#" + r.getFofoId()));
36811 ranu 539
			row.put("type", isLead ? "Lead" : (isOffice ? "Office" : "Visit"));
36740 ranu 540
			row.put("deferredDate", r.getDeferredDate() != null ? r.getDeferredDate().toString() : null);
541
			row.put("reason", r.getReason());
542
			row.put("status", r.getStatus());
543
			LocalDate next = nextByRowId.get(r.getId());
544
			row.put("nextScheduledDate", next != null ? next.toString() : null);
545
			out.add(row);
546
		}
547
		out.sort((a, c) -> String.valueOf(a.get("deferredDate")).compareTo(String.valueOf(c.get("deferredDate"))));
548
 
549
		Map<String, Object> result = new HashMap<>();
550
		result.put("rows", out);
551
		result.put("startDate", start.toString());
552
		result.put("endDate", end.toString());
553
		return responseSender.ok(result);
554
	}
555
 
556
	// Head action on a deferral: reschedule (one-off visit, or into an existing
557
	// beat-day) or cancel. Never edits the beat template.
558
	@PostMapping(value = "/beatPlan/deferred/action")
559
	public ResponseEntity<?> deferredAction(
560
			HttpServletRequest request,
561
			@org.springframework.web.bind.annotation.RequestBody Map<String, Object> body) throws Exception {
562
 
563
		Integer deferredId = body.get("deferredId") != null ? ((Number) body.get("deferredId")).intValue() : null;
564
		String action = (String) body.get("action");
565
		String toDateStr = (String) body.get("toDate");
566
		if (deferredId == null || action == null)
567
			return responseSender.badRequest("deferredId and action are required");
568
 
569
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
570
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
571
		if (me == null) return responseSender.unauthorized("Not logged in");
572
 
573
		BeatDeferredVisit d = beatDeferredVisitRepository.selectById(deferredId);
574
		if (d == null) return responseSender.badRequest("Deferred record not found");
575
 
576
		LocalDateTime now = LocalDateTime.now();
577
 
578
		if ("cancel".equalsIgnoreCase(action)) {
36763 ranu 579
			d.setStatus(String.valueOf(ProfitMandiConstants.MARK_TYPE.CANCELLED));
36740 ranu 580
			d.setActionBy(me.getId());
581
			d.setUpdatedTimestamp(now);
36792 ranu 582
			// Optional cancel reason — overlay onto the reason column. Original
583
			// deferred-reason is preserved as a suffix so we keep the audit trail.
584
			String cancelReason = body.get("reason") != null ? String.valueOf(body.get("reason")).trim() : "";
585
			if (!cancelReason.isEmpty()) {
586
				String prev = d.getReason() != null ? d.getReason() : "";
587
				d.setReason("Cancelled: " + cancelReason + (prev.isEmpty() ? "" : " | Original: " + prev));
588
			}
36740 ranu 589
			beatDeferredVisitRepository.persist(d);
590
			Map<String, Object> ok = new HashMap<>();
591
			ok.put("status", true);
592
			ok.put("message", "Deferred visit cancelled");
593
			return responseSender.ok(ok);
594
		}
595
 
596
		// reschedule_oneoff | reschedule_beat
597
		if (toDateStr == null || toDateStr.isEmpty())
598
			return responseSender.badRequest("toDate is required to reschedule");
599
		LocalDate toDate;
600
		try {
601
			toDate = LocalDate.parse(toDateStr);
602
		} catch (Exception e) {
603
			return responseSender.badRequest("Invalid toDate (yyyy-MM-dd)");
604
		}
36821 ranu 605
		// Reschedule must land on a STRICTLY future date — today is already in progress.
606
		if (!toDate.isAfter(LocalDate.now()))
607
			return responseSender.badRequest("Reschedule date must be in the future (today is already in progress).");
36740 ranu 608
 
609
		if ("reschedule_beat".equalsIgnoreCase(action)) {
610
			boolean hasBeat = beatRepository.selectActiveByAuthUserId(d.getAuthUserId()).stream()
611
					.flatMap(b -> beatScheduleRepository.selectByBeatId(b.getId()).stream())
612
					.anyMatch(s -> s.getStartDate() != null && s.getStartDate().equals(toDate));
613
			if (!hasBeat)
614
				return responseSender.badRequest("No beat is scheduled for this user on " + toDateStr + ". Pick another date or use a one-off visit.");
615
		}
616
 
617
		// Resolve dtr user, then create a PENDING task on toDate. Reuse the
618
		// denormalized name + type (works for both partner visits and leads — for
619
		// leads, looking up fofo_store would be the wrong id space). For visits we
620
		// still try to pull lat/lng for the visit location.
621
		Integer dtrId = resolveDtrId(d.getAuthUserId(), new HashMap<>());
622
		if (dtrId == null) return responseSender.badRequest("No dtr.users record for this sales person");
623
		boolean isLead = "lead".equalsIgnoreCase(d.getTaskType());
36811 ranu 624
		boolean isOffice = "office-visit".equalsIgnoreCase(d.getTaskType());
36740 ranu 625
		String visitLocation = "0.0000,0.0000";
36811 ranu 626
		if (isOffice) {
627
			// Office stops resolve lat/lng from logistics.company_office.
36740 ranu 628
			try {
36811 ranu 629
				com.spice.profitmandi.dao.entity.logistics.CompanyOffice o = companyOfficeRepository.selectById(d.getFofoId());
630
				if (o != null) visitLocation = o.getLat() + "," + o.getLng();
631
			} catch (Exception ignored) {
632
			}
633
		} else if (!isLead) {
634
			try {
36740 ranu 635
				List<FofoStore> ss = fofoStoreRepository.selectByRetailerIds(java.util.Collections.singletonList(d.getFofoId()));
636
				if (!ss.isEmpty()) {
637
					FofoStore fs = ss.get(0);
638
					if (fs.getLatitude() != null && fs.getLongitude() != null
639
							&& !fs.getLatitude().isEmpty() && !fs.getLongitude().isEmpty()) {
640
						visitLocation = fs.getLatitude() + "," + fs.getLongitude();
641
					}
642
				}
643
			} catch (Exception ignored) {
644
			}
645
		}
646
		String taskName = d.getDisplayName() != null ? d.getDisplayName()
647
				: ((d.getReason() != null ? d.getReason() : "Rescheduled") + " | #" + d.getFofoId());
648
 
649
		com.spice.profitmandi.dao.entity.auth.LocationTracking row = new com.spice.profitmandi.dao.entity.auth.LocationTracking();
650
		row.setUserId(dtrId);
651
		row.setDeviceId("0");
652
		row.setTaskId(d.getFofoId());
653
		row.setTaskDate(toDate);
654
		row.setTaskName(taskName);
655
		row.setTaskDescription("Rescheduled from " + d.getDeferredDate());
656
		row.setTaskType(d.getTaskType() != null ? d.getTaskType() : "franchisee-visit");
36763 ranu 657
		row.setMarkType(String.valueOf(ProfitMandiConstants.MARK_TYPE.PENDING));
36740 ranu 658
		row.setAddress("");
659
		row.setVisitLocation(visitLocation);
660
		row.setCheckInLatLng("0.0000,0.0000");
661
		row.setCheckOutLatLng("0.0000,0.0000");
662
		row.setCheckInTime(java.time.LocalTime.MIDNIGHT);
663
		row.setCheckOutTime(java.time.LocalTime.MIDNIGHT);
664
		row.setTransitTime(java.time.LocalTime.MIDNIGHT);
665
		row.setTimeSpent(java.time.LocalTime.MIDNIGHT);
666
		row.setEstimatedTime(java.time.LocalTime.MIDNIGHT);
667
		row.setSessionStartTime(java.time.LocalTime.MIDNIGHT);
668
		row.setSessionEndTime(java.time.LocalTime.MIDNIGHT);
669
		row.setTotalDistance("0.0");
670
		row.setStatus(false);
671
		row.setCreatedTimestamp(now);
672
		row.setUpdatedTimestamp(now);
673
		locationTrackingRepositoryAuto.persist(row);
674
 
36763 ranu 675
		d.setStatus(String.valueOf(ProfitMandiConstants.MARK_TYPE.RESCHEDULED));
36740 ranu 676
		d.setRescheduledToDate(toDate);
677
		d.setActionBy(me.getId());
678
		d.setUpdatedTimestamp(now);
679
		beatDeferredVisitRepository.persist(d);
680
 
681
		Map<String, Object> ok = new HashMap<>();
682
		ok.put("status", true);
683
		ok.put("message", "Visit rescheduled to " + toDateStr);
684
		return responseSender.ok(ok);
685
	}
686
 
687
	// Drop a deferred item into a specific upcoming BEAT run (chosen from the beat
688
	// calendar). Lead → a lead_route row on that beat/date (renders as a lead stop).
689
	// Partner visit → appended to that beat's route for the date's day_number.
690
	// The beat plan calendar then shows it. Marks the deferral RESCHEDULED.
691
	@PostMapping(value = "/beatPlan/deferred/assignToBeat")
692
	public ResponseEntity<?> deferredAssignToBeat(
693
			HttpServletRequest request,
694
			@org.springframework.web.bind.annotation.RequestBody Map<String, Object> body) throws Exception {
695
 
696
		Integer deferredId = body.get("deferredId") != null ? ((Number) body.get("deferredId")).intValue() : null;
697
		Integer beatId = body.get("beatId") != null ? ((Number) body.get("beatId")).intValue() : null;
698
		String dateStr = (String) body.get("date");
699
		if (deferredId == null || beatId == null || dateStr == null)
700
			return responseSender.badRequest("deferredId, beatId and date are required");
701
 
702
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
703
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
704
		if (me == null) return responseSender.unauthorized("Not logged in");
705
 
706
		LocalDate date;
707
		try {
708
			date = LocalDate.parse(dateStr);
709
		} catch (Exception e) {
710
			return responseSender.badRequest("Invalid date (yyyy-MM-dd)");
711
		}
36821 ranu 712
		// Drop-onto-beat must land on a STRICTLY future date — today is already in progress.
713
		if (!date.isAfter(LocalDate.now()))
714
			return responseSender.badRequest("Pick a future date — today is already in progress.");
36740 ranu 715
 
716
		BeatDeferredVisit d = beatDeferredVisitRepository.selectById(deferredId);
717
		if (d == null) return responseSender.badRequest("Deferred record not found");
718
 
719
		// A deferral can only move FORWARD — never onto the day it was deferred or earlier.
720
		if (d.getDeferredDate() != null && !date.isAfter(d.getDeferredDate())) {
721
			return responseSender.badRequest("A deferred item can only be moved to a date after "
722
					+ d.getDeferredDate() + " (it was deferred that day).");
723
		}
724
 
725
		Beat beat = beatRepository.selectById(beatId);
726
		if (beat == null) return responseSender.badRequest("Beat not found");
727
 
728
		// The beat must actually run on the chosen date — get that run's day number.
729
		BeatSchedule sched = beatScheduleRepository.selectByBeatId(beatId).stream()
730
				.filter(s -> s.getStartDate() != null && s.getStartDate().equals(date))
731
				.findFirst().orElse(null);
732
		if (sched == null) return responseSender.badRequest("That beat is not scheduled on " + dateStr);
733
 
734
		LocalDateTime now = LocalDateTime.now();
735
		boolean isLead = "lead".equalsIgnoreCase(d.getTaskType());
736
 
737
		if (isLead) {
738
			// Avoid duplicating the same lead on the same beat/date
739
			boolean exists = leadRouteRepository.selectByBeatId(beatId).stream()
740
					.anyMatch(lr -> lr.getLeadId() == d.getFofoId()
741
							&& date.equals(lr.getScheduleDate())
742
							&& !"CANCELLED".equals(lr.getStatus()));
743
			if (!exists) {
744
				LeadRoute lr = new LeadRoute();
745
				lr.setBeatId(beatId);
746
				lr.setLeadId(d.getFofoId());
747
				lr.setScheduleDate(date);
748
				lr.setSequenceOrder(9999); // append; planner can reorder
749
				lr.setStatus("APPROVED");
750
				lr.setApprovedBy(me.getId());
751
				lr.setApprovedTimestamp(now);
752
				lr.setCreatedTimestamp(now);
753
				lr.setUpdatedTimestamp(now);
754
				leadRouteRepository.persist(lr);
755
			}
756
		} else {
757
			// Partner visit → append to that beat's route for the date's day number,
758
			// if not already present on that day.
759
			boolean exists = beatRouteRepository.selectByBeatId(beatId).stream()
760
					.anyMatch(r -> r.getFofoId() == d.getFofoId() && r.getDayNumber() == sched.getDayNumber() && r.isActive());
761
			if (!exists) {
762
				int nextSeq = beatRouteRepository.selectByBeatId(beatId).stream()
763
						.filter(r -> r.getDayNumber() == sched.getDayNumber())
764
						.mapToInt(BeatRoute::getSequenceOrder).max().orElse(-1) + 1;
765
				BeatRoute br = new BeatRoute();
766
				br.setBeatId(beatId);
767
				br.setFofoId(d.getFofoId());
36811 ranu 768
				br.setVisitType(com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.PARTNER);
36740 ranu 769
				br.setDayNumber(sched.getDayNumber());
770
				br.setSequenceOrder(nextSeq);
771
				br.setActive(true);
772
				beatRouteRepository.persist(br);
773
			}
774
		}
775
 
36763 ranu 776
		d.setStatus(String.valueOf(ProfitMandiConstants.MARK_TYPE.RESCHEDULED));
36740 ranu 777
		d.setRescheduledToDate(date);
778
		d.setActionBy(me.getId());
779
		d.setUpdatedTimestamp(now);
780
		beatDeferredVisitRepository.persist(d);
781
 
782
		Map<String, Object> ok = new HashMap<>();
783
		ok.put("status", true);
784
		ok.put("message", (isLead ? "Lead" : "Partner") + " added to beat '" + beat.getName() + "' on " + dateStr);
785
		return responseSender.ok(ok);
786
	}
787
 
788
	// authUserId -> dtr.users id (via shared email), memoized in the passed cache.
789
	// Used by the reschedule action to create the new PENDING location_tracking row.
790
	private Integer resolveDtrId(int authUserId, Map<Integer, Integer> cache) {
791
		if (cache.containsKey(authUserId)) return cache.get(authUserId);
792
		Integer dtrId = null;
793
		try {
794
			AuthUser au = authRepository.selectById(authUserId);
795
			if (au != null && au.getEmailId() != null) {
796
				com.spice.profitmandi.dao.entity.dtr.User u = userRepositoryAuto.selectByEmailId(au.getEmailId());
797
				if (u != null) dtrId = u.getId();
798
			}
799
		} catch (Exception ignored) {
800
		}
801
		cache.put(authUserId, dtrId);
802
		return dtrId;
803
	}
804
 
805
	// For an auth user: fofoId -> earliest upcoming (>= today) scheduled date where
806
	// an active beat's route still includes that partner (the "Next Scheduled" hint).
807
	private Map<Integer, LocalDate> computeFutureCover(int authUserId) {
808
		LocalDate today = LocalDate.now();
809
		Map<Integer, LocalDate> cover = new HashMap<>();
810
		for (Beat b : beatRepository.selectActiveByAuthUserId(authUserId)) {
811
			LocalDate earliest = null;
812
			for (BeatSchedule s : beatScheduleRepository.selectByBeatId(b.getId())) {
813
				LocalDate dt = s.getStartDate();
814
				if (dt != null && dt.getYear() != 9999 && !dt.isBefore(today)) {
815
					if (earliest == null || dt.isBefore(earliest)) earliest = dt;
816
				}
817
			}
818
			if (earliest == null) continue;
819
			for (BeatRoute rt : beatRouteRepository.selectByBeatId(b.getId())) {
820
				if (!rt.isActive()) continue;
821
				LocalDate cur = cover.get(rt.getFofoId());
822
				if (cur == null || earliest.isBefore(cur)) cover.put(rt.getFofoId(), earliest);
823
			}
824
		}
825
		return cover;
826
	}
827
 
36686 ranu 828
	@GetMapping(value = "/beatPlanWindow")
829
	public String beatPlanWindow(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
830
		model.addAttribute("escalationTypes", visibleLevelsFor(request));
831
		return "beat-plan-window";
36655 ranu 832
	}
833
 
36668 ranu 834
	// Helpers for XLSX bulk upload
835
	private static String readCell(org.apache.poi.ss.usermodel.Cell cell) {
836
		if (cell == null) return null;
837
		switch (cell.getCellType()) {
838
			case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_STRING:
839
				return cell.getStringCellValue();
840
			case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_NUMERIC:
841
				if (org.apache.poi.ss.usermodel.DateUtil.isCellDateFormatted(cell)) {
842
					return cell.getDateCellValue().toInstant()
843
							.atZone(java.time.ZoneId.systemDefault()).toLocalDate().toString();
844
				}
845
				double n = cell.getNumericCellValue();
846
				return (n == Math.floor(n)) ? String.valueOf((long) n) : String.valueOf(n);
847
			case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_BOOLEAN:
848
				return String.valueOf(cell.getBooleanCellValue());
849
			case org.apache.poi.ss.usermodel.Cell.CELL_TYPE_FORMULA:
850
				return cell.getCellFormula();
851
			default:
852
				return null;
36655 ranu 853
		}
854
	}
855
 
856
	// ====================== ONE-TIME LAT/LNG MIGRATION ======================
857
	// For each active fofo_store, compare its stored lat/lng with the geocoded
858
	// address lat/lng (cached in Redis). If the gap is > thresholdKm (default 5)
859
	// OR the store has no lat/lng yet, update the store with the geocoded
860
	// coordinates. Otherwise keep the existing values.
861
	//
862
	// Usage:
863
	//   GET /beatPlan/migrateStoreLatLng              -> dry run, default 5km, all
864
	//   GET /beatPlan/migrateStoreLatLng?apply=true   -> actually update
865
	//   ?thresholdKm=3      -> use a different threshold
866
	//   ?limit=100          -> process only N stores (for staged runs)
867
	@GetMapping(value = "/beatPlan/migrateStoreLatLng")
868
	public ResponseEntity<?> migrateStoreLatLng(
869
			@RequestParam(required = false, defaultValue = "false") boolean apply,
870
			@RequestParam(required = false, defaultValue = "5") double thresholdKm,
36660 ranu 871
			@RequestParam(required = false, defaultValue = "0") int limit,
36727 ranu 872
			@RequestParam(required = false, defaultValue = "0") int offset,
873
			@RequestParam(required = false, defaultValue = "40") int maxSeconds) throws ProfitMandiBusinessException {
36655 ranu 874
 
36660 ranu 875
		List<FofoStore> all = fofoStoreRepository.selectActiveStores();
876
		int totalAvailable = all.size();
877
		int from = Math.max(0, Math.min(offset, totalAvailable));
36655 ranu 878
 
36727 ranu 879
		// Hard cap (if limit given), else go to the end of the list.
880
		int hardTo = limit > 0 ? Math.min(from + limit, totalAvailable) : totalAvailable;
881
 
882
		// Time budget: stop processing once we approach the gateway timeout and
883
		// return nextOffset so the caller can resume. Geocoding is the slow part
884
		// (network/cache), so a fixed batch size could still time out on a cache-miss
885
		// run — a wall-clock budget is safer. maxSeconds defaults to 40 (< typical 60s gateway).
886
		long deadlineMs = System.currentTimeMillis() + Math.max(5, maxSeconds) * 1000L;
887
 
888
		List<FofoStore> stores = all.subList(from, hardTo);
36655 ranu 889
		List<Integer> ids = stores.stream().map(FofoStore::getId).collect(Collectors.toList());
890
		Map<Integer, CustomRetailer> retailerMap = retailerService.getFofoRetailers(ids);
891
 
36727 ranu 892
		int total = 0;            // stores actually processed this call
36655 ranu 893
		int updated = 0, kept = 0, noAddress = 0, noGeocode = 0, errored = 0;
36727 ranu 894
		boolean stoppedOnTime = false;
895
		int nextIndex = from;     // absolute index of next unprocessed store
36655 ranu 896
		List<Map<String, Object>> changes = new ArrayList<>();
897
 
898
		for (FofoStore store : stores) {
36727 ranu 899
			// Stop before doing more slow geocoding work if we've spent our budget.
900
			if (System.currentTimeMillis() >= deadlineMs) {
901
				stoppedOnTime = true;
902
				break;
903
			}
904
			total++;
905
			nextIndex++;
36655 ranu 906
			try {
907
				CustomRetailer retailer = retailerMap.get(store.getId());
908
				if (retailer == null || retailer.getAddress() == null) {
909
					noAddress++;
910
					continue;
911
				}
912
 
913
				String geoAddr = com.spice.profitmandi.service.GeocodingService.buildGeoAddress(
914
						retailer.getAddress().getLine1(), retailer.getAddress().getCity(),
915
						retailer.getAddress().getState(), retailer.getAddress().getPinCode());
916
				if (geoAddr == null || geoAddr.isEmpty()) {
917
					noAddress++;
918
					continue;
919
				}
920
 
921
				double[] coords = geocodingService.geocodeAddress(geoAddr);
922
				if (coords == null) {
923
					noGeocode++;
924
					continue;
925
				}
926
 
927
				Double existingLat = parseDoubleOrNull(store.getLatitude());
928
				Double existingLng = parseDoubleOrNull(store.getLongitude());
929
 
930
				boolean shouldUpdate;
931
				double distKm = -1;
932
				String reason;
933
				if (existingLat == null || existingLng == null) {
934
					shouldUpdate = true;
935
					reason = "missing existing lat/lng";
936
				} else {
937
					distKm = haversineKm(existingLat, existingLng, coords[0], coords[1]);
938
					shouldUpdate = distKm > thresholdKm;
939
					reason = shouldUpdate
940
							? "gap " + Math.round(distKm * 10.0) / 10.0 + "km > " + thresholdKm + "km"
941
							: "gap " + Math.round(distKm * 10.0) / 10.0 + "km within " + thresholdKm + "km";
942
				}
943
 
944
				if (shouldUpdate) {
945
					if (apply) {
946
						store.setLatitude(String.valueOf(coords[0]));
947
						store.setLongitude(String.valueOf(coords[1]));
36727 ranu 948
						store.setLatLngUpdatedTimestamp(LocalDateTime.now());
36655 ranu 949
						fofoStoreRepository.persist(store);
950
					}
951
					updated++;
952
					Map<String, Object> ch = new HashMap<>();
953
					ch.put("storeId", store.getId());
954
					ch.put("code", store.getCode());
955
					ch.put("oldLat", existingLat);
956
					ch.put("oldLng", existingLng);
957
					ch.put("newLat", coords[0]);
958
					ch.put("newLng", coords[1]);
959
					ch.put("distKm", distKm >= 0 ? Math.round(distKm * 10.0) / 10.0 : null);
960
					ch.put("reason", reason);
961
					changes.add(ch);
962
				} else {
36727 ranu 963
					// Verified-kept: lat/lng was already within threshold. Still stamp it
964
					// so "processed vs pending" can be told from lat_lng_updated_timestamp.
965
					if (apply) {
966
						store.setLatLngUpdatedTimestamp(LocalDateTime.now());
967
						fofoStoreRepository.persist(store);
968
					}
36655 ranu 969
					kept++;
970
				}
971
			} catch (Exception e) {
972
				errored++;
973
				LOGGER.warn("Geocode/migrate failed for fofoId={}: {}", store.getId(), e.getMessage());
974
			}
975
		}
976
 
977
		Map<String, Object> result = new HashMap<>();
978
		result.put("mode", apply ? "APPLIED" : "DRY RUN — pass &apply=true to actually update");
979
		result.put("thresholdKm", thresholdKm);
36727 ranu 980
		result.put("totalAvailable", totalAvailable);   // total active stores in DB
36660 ranu 981
		result.put("offset", from);
36727 ranu 982
		result.put("processed", total);                  // stores processed this call
983
		result.put("nextOffset", nextIndex);             // resume here next call
984
		result.put("done", nextIndex >= totalAvailable); // true when nothing left
985
		result.put("stoppedOnTimeBudget", stoppedOnTime);// true if we paused for time, not because we finished
36655 ranu 986
		result.put("updated", updated);
987
		result.put("kept", kept);
988
		result.put("noAddress", noAddress);
989
		result.put("noGeocode", noGeocode);
990
		result.put("errored", errored);
991
		// Limit changes preview to avoid huge responses
992
		result.put("changes", changes.size() > 200 ? changes.subList(0, 200) : changes);
993
		result.put("changesShownCount", Math.min(changes.size(), 200));
994
		return responseSender.ok(result);
995
	}
996
 
36651 ranu 997
	// ====================== EDIT BEAT ======================
998
	// Update an existing beat — name + partner stops (routes).
999
	// Schedules are NOT touched here; manage them via calendar drag-drop.
1000
	@PostMapping(value = "/beatPlan/updateBeat")
1001
	public ResponseEntity<?> updateBeat(
1002
			HttpServletRequest request,
1003
			@RequestParam int beatId,
1004
			@RequestParam String planData) throws Exception {
1005
 
1006
		Beat beat = beatRepository.selectById(beatId);
1007
		if (beat == null) return responseSender.badRequest("Beat not found");
1008
 
36821 ranu 1009
		// Refuse edits while the beat is "live" — i.e., a schedule row covers today.
1010
		// The salesperson is already on-route; mutating the plan mid-day would
1011
		// break their location_tracking timeline. Edits resume tomorrow.
1012
		LocalDate today = LocalDate.now();
1013
		boolean runningToday = beatScheduleRepository.selectByBeatId(beatId).stream()
1014
				.anyMatch(s -> s.getStartDate() != null && s.getStartDate().equals(today));
1015
		if (runningToday) {
1016
			return responseSender.badRequest(
1017
					"This beat is scheduled to run today — editing is locked until tomorrow. "
1018
							+ "Use the Deferred panel for today's adjustments.");
1019
		}
1020
 
36651 ranu 1021
		Gson gson = new Gson();
1022
		Type type = new TypeToken<Map<String, Object>>() {
1023
		}.getType();
1024
		Map<String, Object> plan = gson.fromJson(planData, type);
1025
 
1026
		List<Map<String, Object>> days = (List<Map<String, Object>>) plan.get("days");
1027
		if (days == null || days.isEmpty()) return responseSender.badRequest("No days provided");
1028
 
1029
		// Update name if changed (and not colliding with another beat)
1030
		String newName = plan.get("beatName") != null ? ((String) plan.get("beatName")).trim() : beat.getName();
1031
		if (newName != null && !newName.equalsIgnoreCase(beat.getName())) {
36698 ranu 1032
			// Make sure no other ACTIVE beat for this user already uses this name.
1033
			// Soft-deleted beats keep their name in the table; we don't want them
1034
			// to block a legitimate rename.
1035
			boolean collides = beatRepository.selectActiveByAuthUserId(beat.getAuthUserId()).stream()
36651 ranu 1036
					.anyMatch(b -> b.getId() != beat.getId()
1037
							&& b.getName() != null
1038
							&& newName.equalsIgnoreCase(b.getName().trim()));
1039
			if (collides) return responseSender.badRequest("Another beat with this name already exists");
1040
			beat.setName(newName);
1041
		}
1042
 
1043
		// Update start location from first day if present
1044
		Map<String, Object> firstDay = days.get(0);
1045
		if (firstDay.get("startLocationName") != null)
1046
			beat.setStartLocationName((String) firstDay.get("startLocationName"));
1047
		if (firstDay.get("startLatitude") != null) beat.setStartLatitude((String) firstDay.get("startLatitude"));
1048
		if (firstDay.get("startLongitude") != null) beat.setStartLongitude((String) firstDay.get("startLongitude"));
1049
 
36681 ranu 1050
		int oldTotalDays = beat.getTotalDays();
1051
		int newTotalDays = days.size();
1052
 
1053
		// Hard rule: you cannot grow the number of days on an existing beat.
1054
		// If you need more days, create a new beat. (Shrinking is allowed and
1055
		// the schedules for dropped day numbers are cleaned below.)
1056
		if (newTotalDays > oldTotalDays) {
1057
			return responseSender.badRequest(
1058
					"Cannot increase the number of days on an existing beat. "
1059
							+ "Original: " + oldTotalDays + " day(s), tried: " + newTotalDays + " day(s). "
1060
							+ "Please create a new beat for additional days.");
1061
		}
1062
		beat.setTotalDays(newTotalDays);
1063
 
1064
		// Replace routes (partner stops). Schedules stay intact (except for
36711 ranu 1065
        // dayNumber > newTotalDays cleanup + total km/min refresh below).
36651 ranu 1066
		beatRouteRepository.deleteByBeatId(beatId);
36681 ranu 1067
		// Collect lead IDs the user kept on the plan
36651 ranu 1068
		Set<Integer> keptLeadIds = new HashSet<>();
1069
		for (int d = 0; d < days.size(); d++) {
1070
			Map<String, Object> day = days.get(d);
1071
			int dayNumber = d + 1;
1072
			List<Map<String, Object>> visits = (List<Map<String, Object>>) day.get("visits");
1073
			if (visits == null) continue;
1074
			int partnerSeq = 0;
1075
			for (int i = 0; i < visits.size(); i++) {
1076
				Map<String, Object> v = visits.get(i);
1077
				if ("lead".equals(v.get("type"))) {
1078
					keptLeadIds.add(((Number) v.get("id")).intValue());
1079
					continue; // leads live in lead_route, handled below
1080
				}
1081
				BeatRoute route = new BeatRoute();
1082
				route.setBeatId(beatId);
1083
				route.setFofoId(((Number) v.get("id")).intValue());
36811 ranu 1084
				route.setVisitType("office".equals(v.get("type"))
1085
						? com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.OFFICE
1086
						: com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.PARTNER);
36651 ranu 1087
				route.setSequenceOrder(partnerSeq++);
1088
				route.setDayNumber(dayNumber);
1089
				route.setActive(true);
36711 ranu 1090
                if (v.get("distanceFromPrevKm") != null)
1091
                    route.setDistanceFromPrevKm(((Number) v.get("distanceFromPrevKm")).doubleValue());
1092
                if (v.get("timeFromPrevMins") != null)
1093
                    route.setTimeFromPrevMins(((Number) v.get("timeFromPrevMins")).intValue());
36651 ranu 1094
				beatRouteRepository.persist(route);
1095
			}
1096
		}
1097
 
36681 ranu 1098
		// If the beat shrank, drop schedule rows for day numbers that no longer exist
1099
		if (newTotalDays < oldTotalDays) {
1100
			List<BeatSchedule> currentSchedules = beatScheduleRepository.selectByBeatId(beatId);
1101
			for (BeatSchedule s : currentSchedules) {
1102
				if (s.getDayNumber() > newTotalDays) beatScheduleRepository.delete(s);
1103
			}
1104
		}
1105
 
36711 ranu 1106
        // Refresh the day-level totals on every remaining schedule row so they
1107
        // reflect the post-edit route. Previously updateBeat left these stale
1108
        // (or NULL, for beats created before this fix), which is what the user
1109
        // reported. Keyed by dayNumber so multi-instance beats all get updated.
1110
        Map<Integer, Map<String, Object>> dayByNumber = new HashMap<>();
1111
        for (int d = 0; d < days.size(); d++) {
1112
            dayByNumber.put(d + 1, days.get(d));
1113
        }
1114
        List<BeatSchedule> allSchedules = beatScheduleRepository.selectByBeatId(beatId);
1115
        for (BeatSchedule s : allSchedules) {
1116
            Map<String, Object> day = dayByNumber.get(s.getDayNumber());
1117
            if (day == null) continue;
1118
            if (day.get("totalDistanceKm") != null)
1119
                s.setTotalDistanceKm(((Number) day.get("totalDistanceKm")).doubleValue());
1120
            if (day.get("totalTimeMins") != null)
1121
                s.setTotalTimeMins(((Number) day.get("totalTimeMins")).intValue());
1122
        }
1123
 
36681 ranu 1124
		// Process per-lead actions sent from the editor's removed-leads popup.
1125
		// Each entry: {leadId, action: "cancel"|"reschedule", toDate?: "yyyy-MM-dd"}.
1126
		// - cancel: mark the lead's current APPROVED row for this beat as CANCELLED.
1127
		// - reschedule: cancel here, then create a fresh APPROVED LeadRoute on
1128
		//   whichever beat this user has scheduled on toDate. If no beat exists
1129
		//   on toDate, the whole update fails (so the caller can prompt again).
1130
		int leadsCancelled = 0, leadsRescheduled = 0;
1131
		List<String> leadFailures = new ArrayList<>();
1132
		String removedLeadActionsJson = (String) plan.get("removedLeadActions");
1133
		if (removedLeadActionsJson != null && !removedLeadActionsJson.isEmpty()) {
1134
			Type listType = new TypeToken<List<Map<String, Object>>>() {
1135
			}.getType();
1136
			List<Map<String, Object>> actions = gson.fromJson(removedLeadActionsJson, listType);
1137
 
1138
			List<LeadRoute> beatLeads = leadRouteRepository.selectByBeatId(beatId);
1139
 
1140
			for (Map<String, Object> act : actions) {
1141
				int leadId = ((Number) act.get("leadId")).intValue();
1142
				String mode = (String) act.get("action");
1143
 
1144
				// Find this lead's most-recent APPROVED row on this beat
1145
				LeadRoute current = beatLeads.stream()
1146
						.filter(r -> r.getLeadId() == leadId && "APPROVED".equals(r.getStatus()))
1147
						.findFirst().orElse(null);
1148
				if (current == null) continue; // already removed/cancelled; nothing to do
1149
 
1150
				if ("reschedule".equalsIgnoreCase(mode)) {
1151
					String toDateStr = (String) act.get("toDate");
1152
					if (toDateStr == null || toDateStr.isEmpty()) {
1153
						leadFailures.add("Lead " + leadId + ": reschedule date missing");
1154
						continue;
1155
					}
1156
					LocalDate toDate = LocalDate.parse(toDateStr);
1157
 
1158
					// Find ANY beat this user has scheduled on toDate
1159
					Beat targetBeat = null;
1160
					Integer targetDayNumber = null;
1161
					List<Beat> userBeats = beatRepository.selectActiveByAuthUserId(beat.getAuthUserId());
1162
					for (Beat b : userBeats) {
1163
						List<BeatSchedule> sl = beatScheduleRepository.selectByBeatId(b.getId());
1164
						for (BeatSchedule s : sl) {
1165
							if (s.getStartDate() != null && s.getStartDate().equals(toDate)) {
1166
								targetBeat = b;
1167
								targetDayNumber = s.getDayNumber();
1168
								break;
1169
							}
1170
						}
1171
						if (targetBeat != null) break;
1172
					}
1173
					if (targetBeat == null) {
1174
						return responseSender.badRequest(
1175
								"No beat is scheduled for this user on " + toDateStr
1176
										+ ". Pick a different date for lead " + leadId
1177
										+ ", or choose Cancel for it.");
1178
					}
1179
 
1180
					// Cancel the current attachment to this beat
1181
					current.setStatus("CANCELLED");
1182
					current.setUpdatedTimestamp(LocalDateTime.now());
1183
 
1184
					// Create the new attachment on the target beat/date
1185
					LeadRoute fresh = new LeadRoute();
1186
					fresh.setBeatId(targetBeat.getId());
1187
					fresh.setLeadId(leadId);
1188
					fresh.setNearestStoreId(current.getNearestStoreId());
1189
					fresh.setScheduleDate(toDate);
1190
					fresh.setSequenceOrder(9999); // append; the planner can reorder
1191
					fresh.setStatus("APPROVED");
1192
					fresh.setRequestedBy(current.getRequestedBy());
1193
					fresh.setApprovedBy(current.getApprovedBy());
1194
					fresh.setApprovedTimestamp(LocalDateTime.now());
1195
					fresh.setCreatedTimestamp(LocalDateTime.now());
1196
					fresh.setUpdatedTimestamp(LocalDateTime.now());
1197
					leadRouteRepository.persist(fresh);
1198
 
1199
					LeadActivity la = new LeadActivity();
1200
					la.setLeadId(leadId);
1201
					la.setRemark("Rescheduled from beat '" + beat.getName() + "' to '"
1202
							+ targetBeat.getName() + "' on " + toDateStr + " (day " + targetDayNumber + ")");
1203
					la.setAuthId(0);
1204
					la.setCreatedTimestamp(LocalDateTime.now());
1205
					leadActivityRepositoryAuto.persist(la);
1206
					leadsRescheduled++;
1207
				} else {
1208
					// cancel (default)
1209
					current.setStatus("CANCELLED");
1210
					current.setUpdatedTimestamp(LocalDateTime.now());
1211
 
1212
					LeadActivity la = new LeadActivity();
1213
					la.setLeadId(leadId);
1214
					la.setRemark("Cancelled from beat '" + beat.getName() + "' during edit");
1215
					la.setAuthId(0);
1216
					la.setCreatedTimestamp(LocalDateTime.now());
1217
					leadActivityRepositoryAuto.persist(la);
1218
					leadsCancelled++;
36651 ranu 1219
				}
1220
			}
1221
		}
1222
 
1223
		Map<String, Object> response = new HashMap<>();
1224
		response.put("status", true);
1225
		response.put("planGroupId", String.valueOf(beat.getId()));
36681 ranu 1226
		response.put("leadsCancelled", leadsCancelled);
1227
		response.put("leadsRescheduled", leadsRescheduled);
1228
		response.put("leadFailures", leadFailures);
1229
		response.put("message", "Beat updated successfully"
1230
				+ (leadsCancelled > 0 ? " (" + leadsCancelled + " lead(s) cancelled)" : "")
1231
				+ (leadsRescheduled > 0 ? " (" + leadsRescheduled + " lead(s) rescheduled)" : ""));
36651 ranu 1232
		return responseSender.ok(response);
1233
	}
1234
 
36681 ranu 1235
	// Used by the edit-mode "removed leads" popup so the date picker can warn
1236
	// upfront when the user picks a date that has no beat for them.
1237
	@GetMapping(value = "/beatPlan/userBeatsOnDate")
1238
	public ResponseEntity<?> userBeatsOnDate(
1239
			@RequestParam int authUserId,
1240
			@RequestParam String date) {
1241
		LocalDate target;
1242
		try {
1243
			target = LocalDate.parse(date);
1244
		} catch (Exception e) {
1245
			return responseSender.badRequest("Invalid date");
1246
		}
1247
 
1248
		List<Map<String, Object>> hits = new ArrayList<>();
1249
		List<Beat> userBeats = beatRepository.selectActiveByAuthUserId(authUserId);
1250
		for (Beat b : userBeats) {
1251
			List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(b.getId());
1252
			for (BeatSchedule s : schedules) {
1253
				if (s.getStartDate() != null && s.getStartDate().equals(target)) {
1254
					Map<String, Object> m = new HashMap<>();
1255
					m.put("beatId", b.getId());
1256
					m.put("beatName", b.getName());
1257
					m.put("dayNumber", s.getDayNumber());
1258
					hits.add(m);
1259
				}
1260
			}
1261
		}
1262
		Map<String, Object> result = new HashMap<>();
1263
		result.put("date", date);
1264
		result.put("authUserId", authUserId);
1265
		result.put("beats", hits);
1266
		return responseSender.ok(result);
1267
	}
1268
 
36686 ranu 1269
	// ====================== BASE LOCATION MANAGEMENT ======================
1270
	// Inline page that lets Sales L3+ pick a user and set their base (home)
1271
	// location via map. Reads use the existing /beatPlan/getBaseLocation, writes
1272
	// go through the L3+-guarded endpoint below.
1273
	@GetMapping(value = "/beatPlan/baseLocationPage")
1274
	public String baseLocationPage(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
1275
		model.addAttribute("escalationTypes", visibleLevelsFor(request));
1276
		return "beat-plan-base-location";
36650 ranu 1277
	}
1278
 
1279
	// Tabular JSON: one row per (beat, scheduled date) in [startDate, endDate].
36821 ranu 1280
	// Hierarchy-scoped: a manager sees only their downline + self; super-admins see all.
1281
	// Optional categoryId + escalationType further narrow the listing to users at
1282
	// a specific level (e.g. all Sales L1 in scope).
36650 ranu 1283
	@GetMapping(value = "/beatPlan/scheduledList")
1284
	public ResponseEntity<?> scheduledList(
36821 ranu 1285
			HttpServletRequest request,
36650 ranu 1286
			@RequestParam(required = false) String startDate,
36821 ranu 1287
			@RequestParam(required = false) String endDate,
1288
			@RequestParam(required = false) Integer categoryId,
1289
			@RequestParam(required = false) com.spice.profitmandi.dao.enumuration.cs.EscalationType escalationType) throws ProfitMandiBusinessException {
36650 ranu 1290
 
1291
		LocalDate start, end;
1292
		try {
1293
			start = (startDate == null || startDate.isEmpty()) ? LocalDate.now() : LocalDate.parse(startDate);
1294
			end = (endDate == null || endDate.isEmpty()) ? start.plusDays(7) : LocalDate.parse(endDate);
1295
		} catch (Exception e) {
1296
			return responseSender.badRequest("Invalid date — expected yyyy-MM-dd");
1297
		}
1298
 
36821 ranu 1299
		// ---- Scope: which auth users does the caller get to see ----
1300
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
1301
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
1302
		Set<Integer> visible = null;
1303
		if (me != null && !isSuperAdmin(me)) {
1304
			visible = new HashSet<>(authService.getAllReportees(me.getId()));
1305
			visible.add(me.getId());
1306
		}
1307
		// If a (category, level) was picked, further restrict to that population.
1308
		Set<Integer> levelFilter = null;
1309
		if (categoryId != null && escalationType != null) {
1310
			levelFilter = csService.getAuthUserByCategoryId(categoryId, escalationType).stream()
1311
					.filter(AuthUser::getActive)
1312
					.map(AuthUser::getId)
1313
					.collect(java.util.stream.Collectors.toSet());
1314
		}
1315
 
36650 ranu 1316
		List<com.spice.profitmandi.dao.model.BeatDayDetails> beats =
1317
				beatPlanQueryService.getAllScheduledBeats(start, end);
1318
 
36821 ranu 1319
		final Set<Integer> visibleF = visible;
1320
		final Set<Integer> levelF = levelFilter;
1321
		beats = beats.stream()
1322
				.filter(b -> visibleF == null || visibleF.contains(b.getAuthUserId()))
1323
				.filter(b -> levelF == null || levelF.contains(b.getAuthUserId()))
1324
				.collect(java.util.stream.Collectors.toList());
1325
 
36650 ranu 1326
		// Resolve user names in bulk
1327
		Set<Integer> userIds = beats.stream()
1328
				.map(com.spice.profitmandi.dao.model.BeatDayDetails::getAuthUserId)
1329
				.collect(java.util.stream.Collectors.toSet());
1330
		Map<Integer, AuthUser> userMap = new HashMap<>();
1331
		if (!userIds.isEmpty()) {
1332
			authRepository.selectByIds(new ArrayList<>(userIds))
1333
					.forEach(u -> userMap.put(u.getId(), u));
1334
		}
1335
 
1336
		List<Map<String, Object>> rows = new ArrayList<>();
1337
		for (com.spice.profitmandi.dao.model.BeatDayDetails b : beats) {
1338
			AuthUser u = userMap.get(b.getAuthUserId());
1339
			Map<String, Object> row = new HashMap<>();
1340
			row.put("authUserId", b.getAuthUserId());
1341
			row.put("userName", u != null ? (u.getFirstName() + " " + u.getLastName()) : "User #" + b.getAuthUserId());
1342
			row.put("scheduleDate", b.getScheduleDate().toString());
1343
			row.put("dayNumber", b.getDayNumber());
1344
			row.put("beatId", b.getBeatId());
1345
			row.put("beatName", b.getBeatName());
1346
			row.put("beatColor", b.getBeatColor());
1347
			row.put("partnerCount", b.getPartnerStops().size());
1348
			row.put("leadCount", b.getLeadStops().size());
1349
			row.put("visitCount", b.getPartnerStops().size() + b.getLeadStops().size());
1350
			rows.add(row);
1351
		}
1352
 
1353
		Map<String, Object> result = new HashMap<>();
1354
		result.put("rows", rows);
1355
		result.put("startDate", start.toString());
1356
		result.put("endDate", end.toString());
1357
		return responseSender.ok(result);
1358
	}
1359
 
1360
	// JSON: beats running for (authUserId, date) — enriched with partner/lead names & coords
1361
	@GetMapping(value = "/beatPlan/dayViewData")
1362
	public ResponseEntity<?> beatPlanDayViewData(
1363
			@RequestParam int authUserId,
1364
			@RequestParam String date) throws ProfitMandiBusinessException {
1365
 
1366
		LocalDate localDate;
1367
		try {
1368
			localDate = LocalDate.parse(date);
1369
		} catch (Exception e) {
1370
			return responseSender.badRequest("Invalid date — expected yyyy-MM-dd");
1371
		}
1372
 
1373
		List<com.spice.profitmandi.dao.model.BeatDayDetails> beats =
1374
				beatPlanQueryService.getBeatsForUserOnDate(authUserId, localDate);
1375
 
1376
		// Collect all partner & lead IDs to fetch metadata in bulk
1377
		Set<Integer> partnerIds = new HashSet<>();
1378
		Set<Integer> leadIds = new HashSet<>();
1379
		for (com.spice.profitmandi.dao.model.BeatDayDetails b : beats) {
1380
			b.getPartnerStops().forEach(s -> partnerIds.add((Integer) s.get("fofoId")));
1381
			b.getLeadStops().forEach(s -> leadIds.add((Integer) s.get("leadId")));
1382
		}
1383
 
1384
		// Partners: name + geocoded lat/lng (geocoder is cached in Redis)
1385
		Map<Integer, CustomRetailer> retailerMap = partnerIds.isEmpty()
1386
				? new HashMap<>()
1387
				: retailerService.getFofoRetailers(new ArrayList<>(partnerIds));
1388
		Map<Integer, FofoStore> storeMap = new HashMap<>();
1389
		if (!partnerIds.isEmpty()) {
1390
			fofoStoreRepository.selectByRetailerIds(new ArrayList<>(partnerIds))
1391
					.forEach(fs -> storeMap.put(fs.getId(), fs));
1392
		}
1393
 
1394
		// Leads: name + geo
1395
		Map<Integer, com.spice.profitmandi.dao.entity.user.Lead> leadMap = new HashMap<>();
1396
		Map<Integer, com.spice.profitmandi.dao.entity.user.LeadLiveLocation> leadGeoMap = new HashMap<>();
1397
		for (int leadId : leadIds) {
1398
			com.spice.profitmandi.dao.entity.user.Lead l = leadRepository.selectById(leadId);
1399
			if (l != null) leadMap.put(leadId, l);
1400
			com.spice.profitmandi.dao.entity.user.LeadLiveLocation lg =
1401
					leadLiveLocationRepositoryAuto.selectApprovedByLeadId(leadId);
1402
			if (lg != null) leadGeoMap.put(leadId, lg);
1403
		}
1404
 
1405
		// Enrich each stop
1406
		List<Map<String, Object>> out = new ArrayList<>();
1407
		for (com.spice.profitmandi.dao.model.BeatDayDetails b : beats) {
1408
			Map<String, Object> beatJson = new HashMap<>();
1409
			beatJson.put("beatId", b.getBeatId());
1410
			beatJson.put("beatName", b.getBeatName());
1411
			beatJson.put("beatColor", b.getBeatColor());
1412
			beatJson.put("dayNumber", b.getDayNumber());
1413
			beatJson.put("scheduleDate", b.getScheduleDate().toString());
1414
			beatJson.put("endAction", b.getEndAction());
1415
			beatJson.put("totalDistanceKm", b.getTotalDistanceKm());
1416
			beatJson.put("totalTimeMins", b.getTotalTimeMins());
1417
			beatJson.put("startLocationName", b.getStartLocationName());
1418
			beatJson.put("startLatitude", b.getStartLatitude());
1419
			beatJson.put("startLongitude", b.getStartLongitude());
1420
 
1421
			List<Map<String, Object>> stops = new ArrayList<>();
1422
			// Partners
1423
			for (Map<String, Object> ps : b.getPartnerStops()) {
1424
				int fofoId = (Integer) ps.get("fofoId");
1425
				Map<String, Object> stop = new HashMap<>();
1426
				stop.put("type", "partner");
1427
				stop.put("id", fofoId);
1428
				stop.put("sequenceOrder", ps.get("sequenceOrder"));
1429
				FofoStore fs = storeMap.get(fofoId);
1430
				CustomRetailer cr = retailerMap.get(fofoId);
1431
				stop.put("code", fs != null ? fs.getCode() : null);
1432
				stop.put("name", fs != null && fs.getOutletName() != null ? fs.getOutletName()
1433
						: (cr != null ? cr.getBusinessName() : "Store #" + fofoId));
36655 ranu 1434
				// Use FofoStore lat/lng directly (no geocoding needed after migration)
1435
				if (fs != null && fs.getLatitude() != null && fs.getLongitude() != null
1436
						&& !fs.getLatitude().isEmpty() && !fs.getLongitude().isEmpty()) {
36650 ranu 1437
					try {
36655 ranu 1438
						stop.put("lat", Double.parseDouble(fs.getLatitude()));
1439
						stop.put("lng", Double.parseDouble(fs.getLongitude()));
1440
					} catch (NumberFormatException ignored) {
36650 ranu 1441
					}
1442
				}
36655 ranu 1443
				if (cr != null && cr.getAddress() != null) {
1444
					stop.put("address", cr.getAddress().getAddressString());
1445
				}
36650 ranu 1446
				stops.add(stop);
1447
			}
1448
			// Leads
1449
			for (Map<String, Object> ls : b.getLeadStops()) {
1450
				int leadId = (Integer) ls.get("leadId");
1451
				Map<String, Object> stop = new HashMap<>();
1452
				stop.put("type", "lead");
1453
				stop.put("id", leadId);
1454
				stop.put("sequenceOrder", ls.get("sequenceOrder"));
1455
				stop.put("nearestStoreId", ls.get("nearestStoreId"));
1456
				com.spice.profitmandi.dao.entity.user.Lead l = leadMap.get(leadId);
1457
				stop.put("name", l != null ? l.getFirstName() + " " + l.getLastName() : "Lead #" + leadId);
1458
				stop.put("mobile", l != null ? l.getLeadMobile() : null);
1459
				stop.put("city", l != null ? l.getCity() : null);
1460
				com.spice.profitmandi.dao.entity.user.LeadLiveLocation lg = leadGeoMap.get(leadId);
1461
				if (lg != null) {
1462
					stop.put("lat", lg.getLatitude());
1463
					stop.put("lng", lg.getLongitude());
1464
				}
1465
				stops.add(stop);
1466
			}
1467
			beatJson.put("stops", stops);
1468
			beatJson.put("partnerCount", b.getPartnerStops().size());
1469
			beatJson.put("leadCount", b.getLeadStops().size());
1470
			out.add(beatJson);
1471
		}
1472
 
1473
		Map<String, Object> result = new HashMap<>();
1474
		result.put("beats", out);
1475
		return responseSender.ok(result);
1476
	}
1477
 
36686 ranu 1478
	// ====================== DAY VIEW ======================
1479
	// Inline page (loaded into dashboard #main-content): tabular list of all beats
1480
	// scheduled in a date range across all users. Each row has a View button that
1481
	// opens that user's calendar in a modal.
1482
	@GetMapping(value = "/beatPlan/dayView")
1483
	public String beatPlanDayView(HttpServletRequest request, Model model) throws ProfitMandiBusinessException {
1484
		model.addAttribute("escalationTypes", visibleLevelsFor(request));
36821 ranu 1485
		// Matches /beatPlan/getAuthUsers and the Beat Report — default Sales category.
1486
		model.addAttribute("categoryId", com.spice.profitmandi.common.model.ProfitMandiConstants.TICKET_CATEGORY_SALES);
36686 ranu 1487
		return "beat-plan-day-view";
36618 ranu 1488
	}
1489
 
36644 ranu 1490
	// Returns visits for a beat.
1491
	// - Partner stops (beat_route) belong to the beat template — always returned.
1492
	// - Lead stops (lead_route) belong to a specific run — returned ONLY when planDate
1493
	//   is given and matches the lead's schedule_date. (No planDate = template view.)
36632 ranu 1494
	@GetMapping(value = "/beatPlan/getBeatVisits")
36644 ranu 1495
	public ResponseEntity<?> getBeatVisits(
1496
			@RequestParam String planGroupId,
1497
			@RequestParam(required = false) String planDate) {
1498
 
1499
		int beatId;
1500
		try {
1501
			beatId = Integer.parseInt(planGroupId);
1502
		} catch (NumberFormatException e) {
1503
			return responseSender.ok(new ArrayList<>());
1504
		}
1505
 
1506
		List<BeatRoute> routes = beatRouteRepository.selectByBeatId(beatId);
1507
		List<Map<String, Object>> result = new ArrayList<>();
1508
 
36811 ranu 1509
		// Stops — partner OR office, dispatched by visit_type. Partners are
1510
		// enriched on the client from the partner map (already in scope);
1511
		// offices are enriched here because the client has no office map.
36644 ranu 1512
		for (BeatRoute r : routes) {
36632 ranu 1513
			Map<String, Object> map = new HashMap<>();
36644 ranu 1514
			map.put("fofoId", r.getFofoId());
1515
			map.put("dayNumber", r.getDayNumber());
1516
			map.put("sequenceOrder", r.getSequenceOrder());
36811 ranu 1517
			if (r.getVisitType() == com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.OFFICE) {
1518
				map.put("visitType", "office");
1519
				try {
1520
					com.spice.profitmandi.dao.entity.logistics.CompanyOffice o =
1521
							companyOfficeRepository.selectById(r.getFofoId());
1522
					if (o != null) {
1523
						map.put("code", o.getCode());
1524
						map.put("name", o.getName());
1525
						map.put("latitude", String.valueOf(o.getLat()));
1526
						map.put("longitude", String.valueOf(o.getLng()));
1527
					}
1528
				} catch (Exception ignored) {
1529
				}
1530
			} else {
1531
				map.put("visitType", "partner");
1532
			}
36644 ranu 1533
			result.add(map);
1534
		}
1535
 
1536
		// Lead stops — only for the requested run date
1537
		if (planDate != null && !planDate.isEmpty()) {
1538
			LocalDate date = LocalDate.parse(planDate);
1539
			List<LeadRoute> leads = leadRouteRepository.selectByBeatId(beatId);
1540
			for (LeadRoute lr : leads) {
1541
				if ("APPROVED".equals(lr.getStatus())
1542
						&& lr.getScheduleDate() != null
1543
						&& lr.getScheduleDate().equals(date)) {
1544
					Map<String, Object> map = new HashMap<>();
1545
					map.put("fofoId", lr.getLeadId());
1546
					map.put("dayNumber", 1);
1547
					map.put("sequenceOrder", lr.getSequenceOrder() != null ? lr.getSequenceOrder() : 999);
1548
					map.put("visitType", "lead");
1549
					result.add(map);
1550
				}
1551
			}
1552
		}
1553
 
1554
		// Sort by dayNumber then sequenceOrder
1555
		result.sort((a, b) -> {
1556
			int cmp = Integer.compare((int) a.get("dayNumber"), (int) b.get("dayNumber"));
1557
			return cmp != 0 ? cmp : Integer.compare((int) a.get("sequenceOrder"), (int) b.get("sequenceOrder"));
1558
		});
1559
 
36632 ranu 1560
		return responseSender.ok(result);
1561
	}
1562
 
36681 ranu 1563
	// Returns the user's DEFAULT base location. Falls back to most-recent for
1564
	// legacy users who pre-date the is_default column.
36618 ranu 1565
	@GetMapping(value = "/beatPlan/getBaseLocation")
1566
	public ResponseEntity<?> getBaseLocation(@RequestParam int authUserId) {
36681 ranu 1567
		AuthUserLocation baseLoc = authUserLocationRepository.selectDefaultByAuthUserIdAndType(authUserId, "BASE");
36618 ranu 1568
		if (baseLoc == null) {
1569
			return responseSender.ok(new HashMap<>());
1570
		}
1571
		Map<String, Object> result = new HashMap<>();
1572
		result.put("id", baseLoc.getId());
1573
		result.put("locationName", baseLoc.getLocationName());
1574
		result.put("latitude", baseLoc.getLatitude());
1575
		result.put("longitude", baseLoc.getLongitude());
1576
		result.put("address", baseLoc.getAddress());
36681 ranu 1577
		result.put("isDefault", baseLoc.isDefault());
36618 ranu 1578
		return responseSender.ok(result);
1579
	}
1580
 
36681 ranu 1581
	// Returns ALL BASE locations for a user, default first.
1582
	@GetMapping(value = "/beatPlan/listBaseLocations")
1583
	public ResponseEntity<?> listBaseLocations(@RequestParam int authUserId) {
1584
		List<AuthUserLocation> all = authUserLocationRepository.selectAllByAuthUserIdAndType(authUserId, "BASE");
1585
		// Default at the top, then by created desc (the repo already returns desc).
1586
		all.sort((a, b) -> {
1587
			if (a.isDefault() && !b.isDefault()) return -1;
1588
			if (!a.isDefault() && b.isDefault()) return 1;
1589
			return 0;
1590
		});
1591
		List<Map<String, Object>> rows = new ArrayList<>();
1592
		for (AuthUserLocation l : all) {
1593
			Map<String, Object> row = new HashMap<>();
1594
			row.put("id", l.getId());
1595
			row.put("locationName", l.getLocationName());
1596
			row.put("latitude", l.getLatitude());
1597
			row.put("longitude", l.getLongitude());
1598
			row.put("address", l.getAddress());
1599
			row.put("isDefault", l.isDefault());
1600
			row.put("createdTimestamp", l.getCreatedTimestamp() != null ? l.getCreatedTimestamp().toString() : null);
1601
			rows.add(row);
1602
		}
1603
		Map<String, Object> result = new HashMap<>();
1604
		result.put("authUserId", authUserId);
1605
		result.put("locations", rows);
1606
		return responseSender.ok(result);
1607
	}
1608
 
1609
	// Flip the default flag — set this id default, clear all others.
1610
	@PostMapping(value = "/beatPlan/setDefaultBaseLocation")
1611
	public ResponseEntity<?> setDefaultBaseLocation(
1612
			HttpServletRequest request,
1613
			@RequestParam int id) throws ProfitMandiBusinessException {
1614
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
1615
		AuthUser me = authRepository.selectByEmailOrMobile(ld.getEmailId());
1616
		if (me == null) return responseSender.unauthorized("Not logged in");
1617
		if (!isBaseLocationManager(me)) {
1618
			return responseSender.forbidden("You are not authorized for this action. Only Sales L3 and above can manage base locations.");
1619
		}
1620
 
1621
		AuthUserLocation target = authUserLocationRepository.selectById(id);
1622
		if (target == null) return responseSender.badRequest("Location not found");
1623
 
1624
		List<AuthUserLocation> all = authUserLocationRepository.selectAllByAuthUserIdAndType(target.getAuthUserId(), "BASE");
1625
		for (AuthUserLocation l : all) {
1626
			boolean shouldBeDefault = (l.getId() == id);
1627
			if (l.isDefault() != shouldBeDefault) {
1628
				l.setDefault(shouldBeDefault);
1629
				authUserLocationRepository.persist(l); // saveOrUpdate
1630
			}
1631
		}
1632
 
1633
		Map<String, Object> result = new HashMap<>();
1634
		result.put("status", true);
1635
		result.put("id", id);
1636
		result.put("message", "Default base location updated");
1637
		return responseSender.ok(result);
1638
	}
1639
 
1640
	// Delete a base location. The DEFAULT one cannot be deleted — user must
1641
	// first pick another row as default.
1642
	@PostMapping(value = "/beatPlan/deleteBaseLocation")
1643
	public ResponseEntity<?> deleteBaseLocation(
1644
			HttpServletRequest request,
1645
			@RequestParam int id) throws ProfitMandiBusinessException {
1646
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
1647
		AuthUser me = authRepository.selectByEmailOrMobile(ld.getEmailId());
1648
		if (me == null) return responseSender.unauthorized("Not logged in");
1649
		if (!isBaseLocationManager(me)) {
1650
			return responseSender.forbidden("You are not authorized for this action. Only Sales L3 and above can manage base locations.");
1651
		}
1652
 
1653
		AuthUserLocation target = authUserLocationRepository.selectById(id);
1654
		if (target == null) return responseSender.badRequest("Location not found");
1655
		if (target.isDefault()) {
1656
			return responseSender.badRequest("Default base location cannot be removed. Set another location as default first.");
1657
		}
1658
 
1659
		authUserLocationRepository.delete(target);
1660
 
1661
		Map<String, Object> result = new HashMap<>();
1662
		result.put("status", true);
1663
		result.put("message", "Base location removed");
1664
		return responseSender.ok(result);
1665
	}
1666
 
36686 ranu 1667
	@GetMapping(value = "/beatPlan/getAuthUsers")
1668
	public ResponseEntity<?> getAuthUsers(
1669
			HttpServletRequest request,
1670
			@RequestParam int categoryId,
1671
			@RequestParam EscalationType escalationType) throws ProfitMandiBusinessException {
1672
 
1673
		// Hierarchy filter: a manager only sees users in their downline
1674
		// (themselves + every reportee under them, recursively). Super-admin
1675
		// emails bypass the filter and see everyone. Downline is computed by
1676
		// AuthService.getAllReportees (existing recursive walker).
1677
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
1678
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
1679
 
1680
		final Set<Integer> visible;
1681
		if (me == null || isSuperAdmin(me)) {
1682
			visible = null; // null = no filter
1683
		} else {
1684
			visible = new HashSet<>(authService.getAllReportees(me.getId()));
1685
			visible.add(me.getId()); // include self
1686
		}
1687
 
1688
		List<AuthUser> authUsers = csService.getAuthUserByCategoryId(categoryId, escalationType);
1689
		List<Map<String, Object>> result = authUsers.stream()
1690
				.filter(au -> au.getActive())
1691
				.filter(au -> visible == null || visible.contains(au.getId()))
1692
				.map(au -> {
1693
					Map<String, Object> map = new HashMap<>();
1694
					map.put("id", au.getId());
1695
					map.put("name", au.getFirstName() + " " + au.getLastName());
1696
					return map;
1697
				})
1698
				.collect(Collectors.toList());
1699
		return responseSender.ok(result);
1700
	}
1701
 
1702
	private boolean isSuperAdmin(AuthUser me) {
36681 ranu 1703
		String myEmail = me.getEmailId() != null ? me.getEmailId().toLowerCase() : "";
36686 ranu 1704
		return SUPER_ADMIN_EMAILS.contains(myEmail);
1705
	}
36681 ranu 1706
 
36686 ranu 1707
	// Returns the user's highest escalation level across all positions.
1708
	// Mirrors OrderController.getSalesEscalationLevel but category-agnostic.
1709
	private EscalationType getHighestEscalation(int authUserId) {
1710
		EscalationType highest = null;
1711
		List<com.spice.profitmandi.dao.entity.cs.Position> positions = positionRepository.selectPositionByAuthId(authUserId);
1712
		for (com.spice.profitmandi.dao.entity.cs.Position p : positions) {
1713
			if (highest == null || p.getEscalationType().isGreaterThanEqualTo(highest)) {
1714
				highest = p.getEscalationType();
1715
			}
1716
		}
1717
		return highest;
1718
	}
1719
 
1720
	// Returns the escalation levels a user can manage — strictly below their own.
1721
	// L3 → [L1, L2]; L4 → [L1, L2, L3]; Final → all levels. Super-admin → all levels.
1722
	private List<EscalationType> getVisibleEscalationLevels(AuthUser me) {
1723
		if (isSuperAdmin(me)) return EscalationType.escalations;
1724
		EscalationType mine = getHighestEscalation(me.getId());
1725
		if (mine == null) return java.util.Collections.emptyList();
1726
		List<EscalationType> below = new ArrayList<>();
1727
		for (EscalationType e : EscalationType.escalations) {
1728
			if (mine.isGreaterThanEqualTo(e) && !e.equals(mine)) below.add(e);
1729
		}
1730
		return below;
1731
	}
1732
 
1733
	private List<EscalationType> visibleLevelsFor(HttpServletRequest request) throws ProfitMandiBusinessException {
1734
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
1735
		AuthUser me = (ld != null) ? authRepository.selectByEmailOrMobile(ld.getEmailId()) : null;
1736
		return me == null ? java.util.Collections.emptyList() : getVisibleEscalationLevels(me);
1737
	}
1738
 
1739
	// Shared permission check for base-location admin actions: Sales L3+ OR super-admin.
1740
	private boolean isBaseLocationManager(AuthUser me) {
1741
		if (isSuperAdmin(me)) return true;
36681 ranu 1742
		return csService.getAuthUserIds(
1743
						com.spice.profitmandi.common.model.ProfitMandiConstants.TICKET_CATEGORY_SALES,
1744
						Arrays.asList(EscalationType.L3, EscalationType.L4))
1745
				.stream().anyMatch(u -> u.getId() == me.getId());
1746
	}
1747
 
36618 ranu 1748
	@PostMapping(value = "/beatPlan/saveBaseLocation")
1749
	public ResponseEntity<?> saveBaseLocation(
1750
			@RequestParam int authUserId,
1751
			@RequestParam String locationName,
1752
			@RequestParam String latitude,
1753
			@RequestParam String longitude,
1754
			@RequestParam(required = false) String address) {
1755
		AuthUserLocation loc = new AuthUserLocation();
1756
		loc.setAuthUserId(authUserId);
1757
		loc.setLocationType("BASE");
1758
		loc.setLocationName(locationName);
1759
		loc.setLatitude(latitude);
1760
		loc.setLongitude(longitude);
1761
		loc.setAddress(address);
1762
		loc.setCreatedTimestamp(LocalDateTime.now());
36681 ranu 1763
 
1764
		// First BASE for this user → auto-default so every user always has one.
1765
		List<AuthUserLocation> existing = authUserLocationRepository.selectAllByAuthUserIdAndType(authUserId, "BASE");
1766
		boolean noExistingDefault = existing.stream().noneMatch(AuthUserLocation::isDefault);
1767
		loc.setDefault(existing.isEmpty() || noExistingDefault);
36618 ranu 1768
		authUserLocationRepository.persist(loc);
1769
 
1770
		Map<String, Object> result = new HashMap<>();
1771
		result.put("status", true);
1772
		result.put("id", loc.getId());
36681 ranu 1773
		result.put("isDefault", loc.isDefault());
36618 ranu 1774
		return responseSender.ok(result);
1775
	}
1776
 
1777
	@GetMapping(value = "/beatPlan/getPartners")
1778
	public ResponseEntity<?> getPartners(
1779
			@RequestParam int authUserId,
1780
			@RequestParam int categoryId,
1781
			@RequestParam(required = false) String startLat,
1782
			@RequestParam(required = false) String startLng) throws ProfitMandiBusinessException {
1783
 
36802 ranu 1784
		// Beat planning needs every partner ever assigned — inactive ones included —
1785
		// so the planner can keep building beats around a partner that was paused
1786
		// after the assignment was made. The closed-store skip happens below per row.
1787
		Map<Integer, List<Integer>> pp = csService.getAuthUserIdAllPartnerIdMapping();
36618 ranu 1788
		List<Integer> fofoIds = pp.get(authUserId);
1789
 
36644 ranu 1790
		if (fofoIds == null || fofoIds.isEmpty()) {
36618 ranu 1791
			Map<String, Object> empty = new HashMap<>();
1792
			empty.put("partners", new ArrayList<>());
1793
			return responseSender.ok(empty);
1794
		}
1795
 
1796
		List<FofoStore> fofoStores = fofoStoreRepository.selectByRetailerIds(fofoIds);
1797
		Map<Integer, CustomRetailer> retailerMap = retailerService.getFofoRetailers(fofoIds);
1798
 
1799
		List<Map<String, Object>> partners = new ArrayList<>();
1800
 
1801
		for (FofoStore store : fofoStores) {
36802 ranu 1802
			// Closed partners are gone for good — skip. Inactive ones are kept
1803
			// so the planner can still drop a beat onto them (the assignment
1804
			// pre-dates the deactivation); the UI tags them visually.
1805
			if (store.isClosed()) continue;
36618 ranu 1806
			CustomRetailer retailer = retailerMap.get(store.getId());
1807
 
1808
			Map<String, Object> partnerData = new HashMap<>();
1809
			partnerData.put("fofoId", store.getId());
1810
			partnerData.put("code", store.getCode());
1811
			partnerData.put("outletName", store.getOutletName());
36802 ranu 1812
			partnerData.put("active", store.isActive());
36618 ranu 1813
			partnerData.put("type", "partner");
1814
 
36655 ranu 1815
			// Use FofoStore lat/lng directly (migrated from address geocode)
1816
			if (store.getLatitude() != null && !store.getLatitude().isEmpty()
1817
					&& store.getLongitude() != null && !store.getLongitude().isEmpty()) {
1818
				partnerData.put("latitude", store.getLatitude());
1819
				partnerData.put("longitude", store.getLongitude());
1820
			}
1821
 
36618 ranu 1822
			if (retailer != null) {
1823
				partnerData.put("businessName", retailer.getBusinessName());
1824
				if (retailer.getAddress() != null) {
36644 ranu 1825
					partnerData.put("address", retailer.getAddress().getAddressString());
36618 ranu 1826
				}
1827
			}
1828
			partners.add(partnerData);
1829
		}
1830
 
1831
		if (startLat != null && startLng != null && !startLat.isEmpty() && !startLng.isEmpty()) {
1832
			partners = sortByNearestNeighborFromStart(partners, Double.parseDouble(startLat), Double.parseDouble(startLng));
1833
		} else {
1834
			partners = sortByNearestNeighbor(partners);
1835
		}
1836
 
1837
		Map<String, Object> response = new HashMap<>();
1838
		response.put("partners", partners);
1839
		return responseSender.ok(response);
1840
	}
1841
 
1842
	@PostMapping(value = "/beatPlan/submitPlan")
1843
	public ResponseEntity<?> submitPlan(
1844
			HttpServletRequest request,
1845
			@RequestParam int authUserId,
1846
			@RequestParam String planData) throws Exception {
1847
 
1848
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
1849
		AuthUser currentUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
1850
 
1851
		Gson gson = new Gson();
1852
		Type type = new TypeToken<Map<String, Object>>() {
1853
		}.getType();
1854
		Map<String, Object> plan = gson.fromJson(planData, type);
1855
 
1856
		List<Map<String, Object>> days = (List<Map<String, Object>>) plan.get("days");
1857
		List<String> dates = (List<String>) plan.get("dates");
1858
 
36644 ranu 1859
		String beatName = (plan.get("beatName") != null ? (String) plan.get("beatName") : "Beat").trim();
36618 ranu 1860
 
36698 ranu 1861
		// Duplicate check — same name + same authUserId among ACTIVE beats only.
1862
		// Soft-deleted beats keep the name in the table; we don't want them to
1863
		// block the user from reusing a name they "deleted".
1864
		List<Beat> existingBeats = beatRepository.selectActiveByAuthUserId(authUserId);
36644 ranu 1865
		for (Beat existing : existingBeats) {
1866
			if (existing.getName() != null && beatName.equalsIgnoreCase(existing.getName().trim())) {
1867
				LOGGER.info("Duplicate beat blocked: name='{}' authUserId={} existingId={}", beatName, authUserId, existing.getId());
36618 ranu 1868
				Map<String, Object> response = new HashMap<>();
1869
				response.put("status", true);
36644 ranu 1870
				response.put("planGroupId", String.valueOf(existing.getId()));
36618 ranu 1871
				response.put("duplicate", true);
36644 ranu 1872
				response.put("message", "Beat '" + beatName + "' already exists");
36618 ranu 1873
				return responseSender.ok(response);
1874
			}
1875
		}
1876
 
36644 ranu 1877
		String beatColor = BEAT_COLORS[Math.abs(beatName.hashCode()) % BEAT_COLORS.length];
1878
		int totalDays = days.size();
36618 ranu 1879
 
36785 ranu 1880
		// One-beat-per-day guard: reject if any of the requested dates already
1881
		// has a beat scheduled for this user.
1882
		if (dates != null) {
1883
			List<LocalDate> candidateDates = new ArrayList<>();
1884
			for (String dStr : dates) {
1885
				if (dStr != null && !dStr.isEmpty()) {
1886
					try {
1887
						candidateDates.add(LocalDate.parse(dStr, DateTimeFormatter.ISO_DATE));
1888
					} catch (Exception ignored) {
1889
					}
1890
				}
1891
			}
1892
			Map<String, Object> conflict = findScheduleConflict(authUserId, candidateDates, 0);
1893
			if (conflict != null) return responseSender.badRequest(scheduleConflictMessage(conflict));
1894
		}
1895
 
36644 ranu 1896
		// Create Beat master
1897
		Beat beat = new Beat();
1898
		beat.setName(beatName);
1899
		beat.setAuthUserId(authUserId);
1900
		beat.setBeatColor(beatColor);
1901
		beat.setTotalDays(totalDays);
1902
		beat.setActive(true);
1903
		beat.setCreatedBy(currentUser.getId());
1904
		beat.setCreatedTimestamp(LocalDateTime.now());
1905
 
1906
		// Set start location from first day
1907
		if (!days.isEmpty()) {
1908
			Map<String, Object> firstDay = days.get(0);
1909
			beat.setStartLocationName((String) firstDay.get("startLocationName"));
1910
			beat.setStartLatitude((String) firstDay.get("startLatitude"));
1911
			beat.setStartLongitude((String) firstDay.get("startLongitude"));
1912
		}
1913
		beatRepository.persist(beat);
1914
 
1915
		// End date of the whole beat = last scheduled day's date
1916
		LocalDate beatEndDate = null;
1917
		if (dates != null) {
1918
			for (int d = dates.size() - 1; d >= 0; d--) {
1919
				if (dates.get(d) != null) {
1920
					beatEndDate = LocalDate.parse(dates.get(d), DateTimeFormatter.ISO_DATE);
1921
					break;
1922
				}
1923
			}
1924
		}
1925
 
1926
		// Create routes and schedules for each day
36618 ranu 1927
		for (int d = 0; d < days.size(); d++) {
1928
			Map<String, Object> day = days.get(d);
1929
			int dayNumber = d + 1;
1930
			LocalDate planDate = (dates != null && d < dates.size() && dates.get(d) != null)
36644 ranu 1931
					? LocalDate.parse(dates.get(d), DateTimeFormatter.ISO_DATE) : null;
36618 ranu 1932
 
36644 ranu 1933
			// Auto-determine end action: last day = HOME, others = DAYBREAK
1934
			String endAction = (String) day.get("endAction");
1935
			if (endAction == null || endAction.isEmpty()) {
1936
				endAction = (dayNumber == totalDays) ? "HOME" : "DAYBREAK";
36618 ranu 1937
			}
1938
 
36644 ranu 1939
			// Always create schedule (even if planDate is null — unscheduled beat)
1940
			BeatSchedule schedule = new BeatSchedule();
1941
			schedule.setBeatId(beat.getId());
1942
			schedule.setStartDate(planDate != null ? planDate : LocalDate.of(9999, 12, 31)); // placeholder for unscheduled
1943
			schedule.setEndDate(beatEndDate);
1944
			schedule.setDayNumber(dayNumber);
1945
			schedule.setEndAction(endAction);
1946
			schedule.setStayLocationName((String) day.get("stayLocationName"));
1947
			schedule.setStayLatitude((String) day.get("stayLatitude"));
1948
			schedule.setStayLongitude((String) day.get("stayLongitude"));
1949
			if (day.get("totalDistanceKm") != null)
1950
				schedule.setTotalDistanceKm(((Number) day.get("totalDistanceKm")).doubleValue());
1951
			if (day.get("totalTimeMins") != null)
1952
				schedule.setTotalTimeMins(((Number) day.get("totalTimeMins")).intValue());
1953
			schedule.setCreatedTimestamp(LocalDateTime.now());
1954
			beatScheduleRepository.persist(schedule);
1955
 
36711 ranu 1956
            // Routes (stops) — also persist per-leg distance/time supplied by the
1957
            // client so reports/dashboards don't have to recompute from lat/lng.
36618 ranu 1958
			List<Map<String, Object>> visits = (List<Map<String, Object>>) day.get("visits");
1959
			if (visits != null) {
1960
				for (int i = 0; i < visits.size(); i++) {
1961
					Map<String, Object> visit = visits.get(i);
36644 ranu 1962
					BeatRoute route = new BeatRoute();
1963
					route.setBeatId(beat.getId());
1964
					route.setFofoId(((Number) visit.get("id")).intValue());
36811 ranu 1965
					route.setVisitType("office".equals(visit.get("type"))
1966
							? com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.OFFICE
1967
							: com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.PARTNER);
36644 ranu 1968
					route.setSequenceOrder(i);
1969
					route.setDayNumber(dayNumber);
1970
					route.setActive(true);
36711 ranu 1971
                    if (visit.get("distanceFromPrevKm") != null)
1972
                        route.setDistanceFromPrevKm(((Number) visit.get("distanceFromPrevKm")).doubleValue());
1973
                    if (visit.get("timeFromPrevMins") != null)
1974
                        route.setTimeFromPrevMins(((Number) visit.get("timeFromPrevMins")).intValue());
36644 ranu 1975
					beatRouteRepository.persist(route);
36618 ranu 1976
				}
1977
			}
1978
		}
1979
 
1980
		Map<String, Object> response = new HashMap<>();
1981
		response.put("status", true);
36644 ranu 1982
		response.put("planGroupId", String.valueOf(beat.getId()));
36618 ranu 1983
		response.put("message", "Beat plan submitted successfully");
1984
		return responseSender.ok(response);
1985
	}
1986
 
36632 ranu 1987
	// ============ BULK UPLOAD ============
1988
 
1989
	@GetMapping(value = "/beatPlan/bulkUpload")
1990
	public String bulkUploadPage(HttpServletRequest request, Model model) {
1991
		return "beat-plan-bulk";
1992
	}
1993
 
36681 ranu 1994
	// Adds a new base location for the user. Caller can request this new row
1995
	// becomes the default. If the user has NO base locations yet, the new row
1996
	// is auto-defaulted (so every user always has exactly one default).
36668 ranu 1997
	@PostMapping(value = "/beatPlan/updateBaseLocation")
1998
	public ResponseEntity<?> updateBaseLocation(
1999
			HttpServletRequest request,
2000
			@RequestParam int authUserId,
2001
			@RequestParam String locationName,
2002
			@RequestParam String latitude,
2003
			@RequestParam String longitude,
36681 ranu 2004
			@RequestParam(required = false) String address,
2005
			@RequestParam(required = false, defaultValue = "false") boolean isDefault) throws Exception {
36668 ranu 2006
 
2007
		LoginDetails ld = cookiesProcessor.getCookiesObject(request);
2008
		AuthUser me = authRepository.selectByEmailOrMobile(ld.getEmailId());
2009
		if (me == null) return responseSender.unauthorized("Not logged in");
36681 ranu 2010
		if (!isBaseLocationManager(me)) {
2011
			return responseSender.forbidden("You are not authorized for this action. Only Sales L3 and above can update base location.");
2012
		}
36668 ranu 2013
 
36681 ranu 2014
		List<AuthUserLocation> existing = authUserLocationRepository.selectAllByAuthUserIdAndType(authUserId, "BASE");
2015
		boolean noExistingDefault = existing.stream().noneMatch(AuthUserLocation::isDefault);
2016
		boolean makeDefault = isDefault || existing.isEmpty() || noExistingDefault;
36668 ranu 2017
 
36681 ranu 2018
		// If this new row becomes the default, clear any existing default.
2019
		if (makeDefault) {
2020
			for (AuthUserLocation e : existing) {
2021
				if (e.isDefault()) {
2022
					e.setDefault(false);
2023
					authUserLocationRepository.persist(e);
2024
				}
2025
			}
36668 ranu 2026
		}
2027
 
2028
		AuthUserLocation loc = new AuthUserLocation();
2029
		loc.setAuthUserId(authUserId);
2030
		loc.setLocationType("BASE");
2031
		loc.setLocationName(locationName);
2032
		loc.setLatitude(latitude);
2033
		loc.setLongitude(longitude);
2034
		loc.setAddress(address);
36681 ranu 2035
		loc.setDefault(makeDefault);
36668 ranu 2036
		loc.setCreatedTimestamp(LocalDateTime.now());
2037
		authUserLocationRepository.persist(loc);
2038
 
2039
		Map<String, Object> result = new HashMap<>();
2040
		result.put("status", true);
2041
		result.put("id", loc.getId());
36681 ranu 2042
		result.put("isDefault", loc.isDefault());
2043
		result.put("message", makeDefault ? "Base location added and set as default" : "Base location added");
36668 ranu 2044
		return responseSender.ok(result);
2045
	}
2046
 
36814 ranu 2047
	// Read-only list of company offices — gives BMs a quick lookup of the codes
2048
	// they'll need to drop into the bulk-upload sheet for OFFICE stops.
2049
	// Two URL mappings: /companyOffice/list (canonical) + /company-office-list
2050
	// (matches the menu's action_class so the sidebar link works without an extra
2051
	// auth.menu update).
2052
	@GetMapping(value = {"/companyOffice/list", "/company-office-list"})
2053
	public String companyOfficeList(Model model) {
2054
		List<com.spice.profitmandi.dao.entity.logistics.CompanyOffice> offices = companyOfficeRepository.selectAll();
2055
		// Active first, then sort by code so the bulk-upload reference is stable across page loads.
2056
		offices.sort((a, b) -> {
2057
			int aa = a.isActive() ? 0 : 1;
2058
			int bb = b.isActive() ? 0 : 1;
2059
			if (aa != bb) return Integer.compare(aa, bb);
2060
			String ac = a.getCode() != null ? a.getCode() : "";
2061
			String bc = b.getCode() != null ? b.getCode() : "";
2062
			return ac.compareToIgnoreCase(bc);
2063
		});
2064
		model.addAttribute("offices", offices);
2065
		return "company-office-list";
2066
	}
2067
 
36632 ranu 2068
	@GetMapping(value = "/beatPlan/downloadTemplate")
36668 ranu 2069
	public ResponseEntity<?> downloadTemplate() throws java.io.IOException {
2070
		org.apache.poi.xssf.usermodel.XSSFWorkbook wb = new org.apache.poi.xssf.usermodel.XSSFWorkbook();
2071
		org.apache.poi.xssf.usermodel.XSSFSheet sheet = wb.createSheet("beat-plan");
36632 ranu 2072
 
36668 ranu 2073
		String[] cols = {"beat_name", "auth_user_id", "start_date", "day_number", "sequence_order", "partner_code"};
2074
 
2075
		// Header style
2076
		org.apache.poi.xssf.usermodel.XSSFCellStyle headerStyle = wb.createCellStyle();
2077
		org.apache.poi.xssf.usermodel.XSSFFont headerFont = wb.createFont();
2078
		headerFont.setBold(true);
2079
		headerStyle.setFont(headerFont);
2080
		headerStyle.setFillForegroundColor(new org.apache.poi.xssf.usermodel.XSSFColor(new java.awt.Color(230, 230, 230)));
2081
		headerStyle.setFillPattern(org.apache.poi.ss.usermodel.FillPatternType.SOLID_FOREGROUND);
2082
 
2083
		org.apache.poi.xssf.usermodel.XSSFRow header = sheet.createRow(0);
2084
		for (int i = 0; i < cols.length; i++) {
2085
			org.apache.poi.xssf.usermodel.XSSFCell c = header.createCell(i);
2086
			c.setCellValue(cols[i]);
2087
			c.setCellStyle(headerStyle);
2088
		}
2089
 
2090
		// Example rows — one partner per row. Inheritable columns blank after first row of a beat.
2091
		Object[][] sample = {
2092
				{"Jaipur East Route", "280", "2026-06-02", "1", "1", "RJKAI1478"},
2093
				{"", "", "", "1", "2", "RJBUN1449"},
2094
				{"", "", "", "1", "3", "RJDEG1443"},
2095
				{"", "", "", "2", "1", "RJALR1362"},
2096
				{"", "", "", "2", "2", "RJBTR1388"},
2097
				{"", "", "", "3", "1", "RJRSD1518"},
2098
				{"", "", "", "3", "2", "RJSML356"},
2099
				{"Agra Circuit", "145", "2026-06-05", "1", "1", "UPAGR101"},
2100
				{"", "", "", "1", "2", "UPAGR102"},
2101
		};
2102
		for (int r = 0; r < sample.length; r++) {
2103
			org.apache.poi.xssf.usermodel.XSSFRow row = sheet.createRow(r + 1);
2104
			for (int c = 0; c < cols.length; c++) {
2105
				row.createCell(c).setCellValue(sample[r][c].toString());
2106
			}
2107
		}
2108
		for (int i = 0; i < cols.length; i++) sheet.autoSizeColumn(i);
2109
 
2110
		java.io.ByteArrayOutputStream out = new java.io.ByteArrayOutputStream();
2111
		wb.write(out);
2112
		wb.close();
2113
 
36632 ranu 2114
		org.springframework.http.HttpHeaders headers = new org.springframework.http.HttpHeaders();
36668 ranu 2115
		headers.add("Content-Disposition", "attachment; filename=beat_plan_template.xlsx");
2116
		headers.add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
2117
		return new ResponseEntity<>(out.toByteArray(), headers, org.springframework.http.HttpStatus.OK);
36632 ranu 2118
	}
2119
 
2120
	@PostMapping(value = "/beatPlan/bulkUploadProcess")
2121
	public ResponseEntity<?> bulkUploadProcess(
2122
			HttpServletRequest request,
2123
			@RequestParam("file") org.springframework.web.multipart.MultipartFile file,
2124
			@RequestParam(value = "includeSundays", defaultValue = "false") boolean includeSundays) throws Exception {
2125
 
2126
		LoginDetails loginDetails = cookiesProcessor.getCookiesObject(request);
2127
		AuthUser currentUser = authRepository.selectByEmailOrMobile(loginDetails.getEmailId());
2128
 
36668 ranu 2129
		// Read .xlsx — one partner per row. beat_name / auth_user_id / start_date
2130
		// appear ONLY on the first row of a beat; subsequent rows inherit them.
2131
		org.apache.poi.ss.usermodel.Workbook workbook =
2132
				new org.apache.poi.xssf.usermodel.XSSFWorkbook(file.getInputStream());
2133
		org.apache.poi.ss.usermodel.Sheet sheet = workbook.getSheetAt(0);
36632 ranu 2134
 
36668 ranu 2135
		// Header → column index map
2136
		org.apache.poi.ss.usermodel.Row headerRow = sheet.getRow(0);
2137
		if (headerRow == null) {
2138
			workbook.close();
2139
			return responseSender.badRequest("Empty file");
2140
		}
2141
		Map<String, Integer> colIdx = new HashMap<>();
2142
		for (int i = 0; i < headerRow.getLastCellNum(); i++) {
2143
			String h = readCell(headerRow.getCell(i));
2144
			if (h != null) colIdx.put(h.trim().toLowerCase(), i);
2145
		}
2146
		for (String required : new String[]{"beat_name", "auth_user_id", "day_number", "partner_code"}) {
2147
			if (!colIdx.containsKey(required)) {
2148
				workbook.close();
2149
				return responseSender.badRequest("Missing required column: " + required);
2150
			}
2151
		}
36632 ranu 2152
 
36668 ranu 2153
		// Walk rows, group partners by (beat_name + auth_user_id) → day_number → sequence_order
2154
		Map<String, BulkBeatGroup> beatGroups = new LinkedHashMap<>();
2155
		String currentKey = null;
2156
		String currentBeatName = null;
2157
		String currentAuthId = null;
2158
		String currentStartDate = null;
2159
 
2160
		for (int r = 1; r <= sheet.getLastRowNum(); r++) {
2161
			org.apache.poi.ss.usermodel.Row row = sheet.getRow(r);
2162
			if (row == null) continue;
2163
 
2164
			String beatName = readCell(row.getCell(colIdx.get("beat_name")));
2165
			String authId = readCell(row.getCell(colIdx.get("auth_user_id")));
2166
			String startDate = colIdx.containsKey("start_date") ? readCell(row.getCell(colIdx.get("start_date"))) : null;
2167
			String dayNumber = readCell(row.getCell(colIdx.get("day_number")));
2168
			String seqOrder = colIdx.containsKey("sequence_order") ? readCell(row.getCell(colIdx.get("sequence_order"))) : null;
2169
			String code = readCell(row.getCell(colIdx.get("partner_code")));
2170
 
2171
			if (beatName != null && !beatName.trim().isEmpty()) {
2172
				// Start of a new beat — capture inheritable fields
2173
				currentBeatName = beatName.trim().replaceAll("\\s+", " ");
2174
				currentAuthId = authId != null ? authId.trim() : null;
2175
				currentStartDate = (startDate != null && !startDate.trim().isEmpty()) ? startDate.trim() : null;
2176
				currentKey = currentBeatName + "|" + currentAuthId;
2177
			}
2178
			if (currentKey == null) continue; // partner row before any beat header — skip
2179
			if (code == null || code.trim().isEmpty()) continue;
2180
 
2181
			final String beatNameF = currentBeatName;
2182
			final String authIdF = currentAuthId;
2183
			final String startDateF = currentStartDate;
2184
			BulkBeatGroup g = beatGroups.computeIfAbsent(currentKey, k -> new BulkBeatGroup(beatNameF, authIdF, startDateF));
2185
 
2186
			int day;
2187
			try {
2188
				day = Integer.parseInt(dayNumber.trim());
2189
			} catch (Exception e) {
2190
				continue;
2191
			} // bad day → skip row
2192
 
2193
			int seq = -1;
2194
			if (seqOrder != null && !seqOrder.trim().isEmpty()) {
2195
				try {
2196
					seq = Integer.parseInt(seqOrder.trim());
2197
				} catch (Exception ignore) {
2198
				}
2199
			}
2200
			g.addPartner(day, seq, code.trim(), r + 1);
36632 ranu 2201
		}
36668 ranu 2202
		workbook.close();
36632 ranu 2203
 
36811 ranu 2204
		// Partner-code lookup (legacy).
36632 ranu 2205
		List<FofoStore> allStores = fofoStoreRepository.selectAll();
2206
		Map<String, Integer> codeToId = new HashMap<>();
36644 ranu 2207
		for (FofoStore store : allStores) codeToId.put(store.getCode(), store.getId());
36632 ranu 2208
 
36811 ranu 2209
		// Office-code lookup — office stops share the same `partner_code` column in the bulk
2210
		// sheet; resolution dispatches by which catalogue the code belongs to. A code present
2211
		// in BOTH catalogues is treated as an error so the planner fixes the collision.
2212
		Map<String, Integer> officeCodeToId = new HashMap<>();
2213
		for (com.spice.profitmandi.dao.entity.logistics.CompanyOffice o : companyOfficeRepository.selectAll()) {
2214
			if (o.getCode() != null && !o.getCode().isEmpty()) officeCodeToId.put(o.getCode(), o.getId());
2215
		}
2216
 
36632 ranu 2217
		LocalDate holidayStart = LocalDate.now();
36644 ranu 2218
		List<PublicHolidays> holidays = publicHolidaysRepository.selectAllBetweenDates(holidayStart, holidayStart.plusMonths(6));
36632 ranu 2219
		Set<LocalDate> holidayDates = holidays.stream().map(PublicHolidays::getDate).collect(Collectors.toSet());
2220
 
36785 ranu 2221
		// =====================================================================
2222
		// All-or-nothing import: validate every group first; only persist if
2223
		// the entire file passes. A single bad row blocks the whole upload
2224
		// so the user can fix and re-upload without partial creations.
2225
		// =====================================================================
2226
 
36632 ranu 2227
		List<String> errorMessages = new ArrayList<>();
36785 ranu 2228
		List<ValidatedBulkBeat> ready = new ArrayList<>();
36632 ranu 2229
 
36785 ranu 2230
		// ----- Phase 1: validate every group, collect ALL errors -----
36668 ranu 2231
		for (BulkBeatGroup g : beatGroups.values()) {
36785 ranu 2232
			String beatName = g.beatName;
2233
 
2234
			int authUserId;
36632 ranu 2235
			try {
36785 ranu 2236
				authUserId = Integer.parseInt(g.authUserId);
2237
			} catch (Exception e) {
2238
				errorMessages.add("Beat '" + beatName + "': invalid auth_user_id '" + g.authUserId + "'.");
2239
				continue;
2240
			}
36632 ranu 2241
 
36785 ranu 2242
			LocalDate startDate;
2243
			try {
2244
				startDate = (g.startDate == null || g.startDate.isEmpty())
2245
						? null : LocalDate.parse(g.startDate, DateTimeFormatter.ISO_DATE);
2246
			} catch (Exception e) {
2247
				errorMessages.add("Beat '" + beatName + "': invalid start_date '" + g.startDate + "'.");
2248
				continue;
2249
			}
2250
			if (startDate != null && startDate.isBefore(LocalDate.now())) {
2251
				errorMessages.add("Beat '" + beatName + "': start_date in past.");
2252
				continue;
2253
			}
36632 ranu 2254
 
36785 ranu 2255
			List<Integer> sortedDays = new ArrayList<>(g.dayToPartners.keySet());
2256
			Collections.sort(sortedDays);
36668 ranu 2257
 
36785 ranu 2258
			List<LocalDate> scheduleDates = new ArrayList<>();
2259
			if (startDate != null) {
2260
				LocalDate d = startDate;
2261
				while (scheduleDates.size() < sortedDays.size()) {
2262
					if (holidayDates.contains(d) || (d.getDayOfWeek() == DayOfWeek.SUNDAY && !includeSundays)) {
36632 ranu 2263
						d = d.plusDays(1);
36785 ranu 2264
						continue;
36632 ranu 2265
					}
36785 ranu 2266
					scheduleDates.add(d);
2267
					d = d.plusDays(1);
36632 ranu 2268
				}
36785 ranu 2269
			}
36632 ranu 2270
 
36785 ranu 2271
			// Duplicate beat-name check (ACTIVE only; soft-deleted names are reusable).
2272
			boolean isDuplicate = beatRepository.selectActiveByAuthUserId(authUserId).stream()
2273
					.anyMatch(b -> b.getName() != null && beatName.equalsIgnoreCase(b.getName().trim()));
2274
			if (isDuplicate) {
2275
				errorMessages.add("Beat '" + beatName + "': already exists for user " + authUserId + ".");
2276
				continue;
2277
			}
2278
 
2279
			// One-beat-per-day guard (against existing beats).
2280
			Map<String, Object> bulkConflict = findScheduleConflict(authUserId, scheduleDates, 0);
2281
			if (bulkConflict != null) {
2282
				errorMessages.add("Beat '" + beatName + "': " + scheduleConflictMessage(bulkConflict));
2283
				continue;
2284
			}
2285
 
36811 ranu 2286
			// Validate codes upfront so we don't half-persist. A code may belong to
2287
			// fofo_store (PARTNER) or company_office (OFFICE) but not both.
36785 ranu 2288
			List<String> badCodes = new ArrayList<>();
36811 ranu 2289
			List<String> ambiguousCodes = new ArrayList<>();
36785 ranu 2290
			for (List<BulkPartner> ps : g.dayToPartners.values()) {
2291
				for (BulkPartner p : ps) {
36811 ranu 2292
					boolean inPartner = codeToId.containsKey(p.code);
2293
					boolean inOffice = officeCodeToId.containsKey(p.code);
2294
					if (inPartner && inOffice) {
2295
						ambiguousCodes.add(p.code + " (row " + p.rowNum + ")");
2296
					} else if (!inPartner && !inOffice) {
36785 ranu 2297
						badCodes.add(p.code + " (row " + p.rowNum + ")");
2298
					}
36644 ranu 2299
				}
36785 ranu 2300
			}
2301
			if (!badCodes.isEmpty()) {
36811 ranu 2302
				errorMessages.add("Beat '" + beatName + "': unknown code(s) — " + String.join(", ", badCodes) + ".");
36785 ranu 2303
				continue;
2304
			}
36811 ranu 2305
			if (!ambiguousCodes.isEmpty()) {
2306
				errorMessages.add("Beat '" + beatName + "': code(s) exist in both partner and office catalogues — " + String.join(", ", ambiguousCodes) + ".");
2307
				continue;
2308
			}
36632 ranu 2309
 
36785 ranu 2310
			ready.add(new ValidatedBulkBeat(g, authUserId, sortedDays, scheduleDates));
2311
		}
36632 ranu 2312
 
36785 ranu 2313
		// Intra-file conflict: two beats in the same upload requesting the same
2314
		// user + date. Caught here so the user fixes the file before re-uploading.
2315
		Set<String> seenUserDates = new HashSet<>();
2316
		for (ValidatedBulkBeat v : ready) {
2317
			for (LocalDate sd : v.scheduleDates) {
2318
				String key = v.authUserId + "|" + sd;
2319
				if (!seenUserDates.add(key)) {
2320
					errorMessages.add("Beat '" + v.g.beatName + "': date " + sd + " is also claimed by another beat in this file for the same user.");
2321
					break;
2322
				}
2323
			}
2324
		}
36632 ranu 2325
 
36785 ranu 2326
		// All-or-nothing: any error → return without persisting anything.
2327
		if (!errorMessages.isEmpty()) {
2328
			Map<String, Object> response = new HashMap<>();
2329
			response.put("status", false);
2330
			response.put("beatsCreated", 0);
2331
			response.put("errors", errorMessages.size());
2332
			response.put("errorMessages", errorMessages);
2333
			response.put("message", "No beats created. Fix the issues below and re-upload the file.");
2334
			return responseSender.ok(response);
2335
		}
36632 ranu 2336
 
36785 ranu 2337
		// ----- Phase 2: persist (only reached when every row was clean) -----
2338
		int beatsCreated = 0;
2339
		for (ValidatedBulkBeat v : ready) {
2340
			BulkBeatGroup g = v.g;
2341
			String beatName = g.beatName;
2342
			int authUserId = v.authUserId;
2343
			List<Integer> sortedDays = v.sortedDays;
2344
			List<LocalDate> scheduleDates = v.scheduleDates;
36668 ranu 2345
 
36785 ranu 2346
			String beatColor = BEAT_COLORS[Math.abs(beatName.hashCode()) % BEAT_COLORS.length];
2347
			AuthUserLocation homeLoc = authUserLocationRepository.selectDefaultByAuthUserIdAndType(authUserId, "BASE");
36632 ranu 2348
 
36785 ranu 2349
			Beat beat = new Beat();
2350
			beat.setName(beatName);
2351
			beat.setAuthUserId(authUserId);
2352
			beat.setBeatColor(beatColor);
2353
			beat.setTotalDays(sortedDays.size());
2354
			beat.setStartLocationName(homeLoc != null ? homeLoc.getLocationName() : "Home");
2355
			beat.setStartLatitude(homeLoc != null ? homeLoc.getLatitude() : null);
2356
			beat.setStartLongitude(homeLoc != null ? homeLoc.getLongitude() : null);
2357
			beat.setActive(true);
2358
			beat.setCreatedBy(currentUser.getId());
2359
			beat.setCreatedTimestamp(LocalDateTime.now());
2360
			beatRepository.persist(beat);
36668 ranu 2361
 
36785 ranu 2362
			LocalDate bulkEndDate = scheduleDates.isEmpty() ? null : scheduleDates.get(scheduleDates.size() - 1);
2363
 
2364
			for (int dayIdx = 0; dayIdx < sortedDays.size(); dayIdx++) {
2365
				int dayNumber = sortedDays.get(dayIdx);
2366
				LocalDate planDate = (dayIdx < scheduleDates.size()) ? scheduleDates.get(dayIdx) : null;
2367
 
2368
				BeatSchedule schedule = new BeatSchedule();
2369
				schedule.setBeatId(beat.getId());
2370
				schedule.setStartDate(planDate != null ? planDate : LocalDate.of(9999, 12, 31));
2371
				schedule.setEndDate(bulkEndDate);
2372
				schedule.setDayNumber(dayNumber);
2373
				schedule.setEndAction(dayIdx == sortedDays.size() - 1 ? "HOME" : "DAYBREAK");
2374
				schedule.setCreatedTimestamp(LocalDateTime.now());
2375
				beatScheduleRepository.persist(schedule);
2376
 
2377
				List<BulkPartner> partners = g.dayToPartners.get(dayNumber);
2378
				partners.sort((a, b) -> {
2379
					if (a.seq >= 0 && b.seq >= 0) return Integer.compare(a.seq, b.seq);
2380
					if (a.seq >= 0) return -1;
2381
					if (b.seq >= 0) return 1;
2382
					return Integer.compare(a.rowNum, b.rowNum);
2383
				});
2384
 
2385
				int autoSeq = 0;
2386
				for (BulkPartner p : partners) {
36811 ranu 2387
					Integer partnerId = codeToId.get(p.code);
2388
					Integer officeId = officeCodeToId.get(p.code);
36785 ranu 2389
					// Codes were validated in Phase 1, this is just a safety net.
36811 ranu 2390
					if (partnerId == null && officeId == null) continue;
36785 ranu 2391
					BeatRoute route = new BeatRoute();
2392
					route.setBeatId(beat.getId());
36811 ranu 2393
					if (partnerId != null) {
2394
						route.setFofoId(partnerId);
2395
						route.setVisitType(com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.PARTNER);
2396
					} else {
2397
						route.setFofoId(officeId);
2398
						route.setVisitType(com.spice.profitmandi.dao.enumuration.dtr.BeatVisitType.OFFICE);
2399
					}
36785 ranu 2400
					route.setSequenceOrder(p.seq >= 0 ? p.seq : autoSeq);
2401
					route.setDayNumber(dayNumber);
2402
					route.setActive(true);
2403
					beatRouteRepository.persist(route);
2404
					autoSeq++;
36632 ranu 2405
				}
2406
			}
36785 ranu 2407
			beatsCreated++;
36632 ranu 2408
		}
2409
 
2410
		Map<String, Object> response = new HashMap<>();
2411
		response.put("status", true);
2412
		response.put("beatsCreated", beatsCreated);
36785 ranu 2413
		response.put("errors", 0);
36632 ranu 2414
		response.put("errorMessages", errorMessages);
36785 ranu 2415
		response.put("message", beatsCreated + " beat(s) created.");
36632 ranu 2416
		return responseSender.ok(response);
2417
	}
2418
 
36785 ranu 2419
	// Move a beat from one date to another — used by calendar drag-and-drop.
2420
	// Behaviour: if the target date already has ANOTHER beat scheduled (for the
2421
	// same sales user), the two schedules swap — the other beat slides onto
2422
	// the source date. If the target date is empty, the source date becomes empty.
2423
	@PostMapping(value = "/beatPlan/moveScheduleDate")
2424
	public ResponseEntity<?> moveScheduleDate(
2425
			@RequestParam String planGroupId,
2426
			@RequestParam String fromDate,
2427
			@RequestParam String toDate) {
2428
		int beatId = Integer.parseInt(planGroupId);
2429
		LocalDate from = LocalDate.parse(fromDate);
2430
		LocalDate to = LocalDate.parse(toDate);
2431
 
2432
		if (from.equals(to)) {
2433
			Map<String, Object> ok = new HashMap<>();
2434
			ok.put("status", true);
2435
			ok.put("message", "Same date — no change");
2436
			return responseSender.ok(ok);
2437
		}
2438
 
2439
		// Today is the live/running slot — a beat already running today can't be
2440
		// bumped, and a future beat can't be moved onto today.
2441
		LocalDate today = LocalDate.now();
2442
		if (to.equals(today) || from.equals(today) || from.isBefore(today) || to.isBefore(today)) {
2443
			return responseSender.badRequest("Cannot move to or from today / a past date — today's beat is live. Use future dates only.");
2444
		}
2445
 
2446
		Beat beat = beatRepository.selectById(beatId);
2447
		if (beat == null) return responseSender.badRequest("Beat not found");
2448
 
2449
		List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(beatId);
2450
 
2451
		// Reject if THIS beat already has a different schedule row on the target date
2452
		// (it would create two schedule rows of the same beat on one day).
2453
		boolean selfConflict = schedules.stream()
2454
				.anyMatch(s -> s.getStartDate() != null && s.getStartDate().equals(to));
2455
		if (selfConflict) return responseSender.badRequest("Beat is already scheduled on " + toDate);
2456
 
2457
		BeatSchedule match = schedules.stream()
2458
				.filter(s -> s.getStartDate() != null && s.getStartDate().equals(from))
2459
				.findFirst().orElse(null);
2460
		if (match == null) return responseSender.badRequest("No schedule found for " + fromDate);
2461
 
2462
		// Look for ANY OTHER beat (same sales user) whose schedule sits on the target
2463
		// date — if found we'll swap it onto the source date.
2464
		BeatSchedule otherSchedule = null;
2465
		List<BeatSchedule> otherSchedules = null;
2466
		Beat otherBeat = null;
2467
		for (Beat ub : beatRepository.selectActiveByAuthUserId(beat.getAuthUserId())) {
2468
			if (ub.getId() == beatId) continue;
2469
			List<BeatSchedule> ubSchedules = beatScheduleRepository.selectByBeatId(ub.getId());
2470
			BeatSchedule hit = ubSchedules.stream()
2471
					.filter(s -> s.getStartDate() != null && s.getStartDate().equals(to))
2472
					.findFirst().orElse(null);
2473
			if (hit != null) {
2474
				otherSchedule = hit;
2475
				otherSchedules = ubSchedules;
2476
				otherBeat = ub;
2477
				break;
2478
			}
2479
		}
2480
 
36840 ranu 2481
		// Multi-day beats can't be drag-swapped — moving one of their day_numbers
2482
		// in isolation would leave the other days behind and break the
2483
		// Day 1 → Day 2 → … chronological contiguity of the plan. Block both
2484
		// sides of the swap, point the BM at the Reschedule flow which moves
2485
		// every day together.
2486
		if (beat.getTotalDays() > 1) {
2487
			return responseSender.badRequest(
2488
					"\"" + (beat.getName() != null ? beat.getName() : "Beat #" + beat.getId())
2489
							+ "\" is a multi-day beat (" + beat.getTotalDays()
2490
							+ " days). Drag-swap is only allowed for single-day beats. Use Reschedule to move the whole plan together.");
2491
		}
2492
		if (otherBeat != null && otherBeat.getTotalDays() > 1) {
2493
			return responseSender.badRequest(
2494
					"Target date already has \"" + (otherBeat.getName() != null ? otherBeat.getName() : "Beat #" + otherBeat.getId())
2495
							+ "\", a multi-day beat (" + otherBeat.getTotalDays()
2496
							+ " days). Drag-swap is only allowed when both beats are single-day. Use Reschedule on one of them first.");
2497
		}
2498
 
2499
		// Move the dragged beat onto the target date. Per-row invariant:
2500
		// each schedule row's end_date == its own start_date (single-day shape).
2501
		// We touch ONLY the two rows being swapped — other schedule rows for the
2502
		// same beat (e.g. older dates) are left exactly as they were.
36785 ranu 2503
		match.setStartDate(to);
36840 ranu 2504
		match.setEndDate(to);
36785 ranu 2505
		if (otherSchedule != null) {
2506
			otherSchedule.setStartDate(from);
36840 ranu 2507
			otherSchedule.setEndDate(from);
36785 ranu 2508
		}
2509
 
2510
		Map<String, Object> response = new HashMap<>();
2511
		response.put("status", true);
2512
		response.put("message", otherBeat != null
2513
				? "Swapped with \"" + (otherBeat.getName() != null ? otherBeat.getName() : "beat") + "\" on " + toDate
2514
				: "Moved from " + fromDate + " to " + toDate);
2515
		response.put("swapped", otherBeat != null);
2516
		return responseSender.ok(response);
2517
	}
2518
 
36668 ranu 2519
	private static class BulkBeatGroup {
2520
		final String beatName;
2521
		final String authUserId;
2522
		final String startDate;
2523
		final Map<Integer, List<BulkPartner>> dayToPartners = new LinkedHashMap<>();
2524
 
2525
		BulkBeatGroup(String beatName, String authUserId, String startDate) {
2526
			this.beatName = beatName;
2527
			this.authUserId = authUserId;
2528
			this.startDate = startDate;
2529
		}
2530
 
2531
		void addPartner(int day, int seq, String code, int rowNum) {
2532
			dayToPartners.computeIfAbsent(day, k -> new ArrayList<>()).add(new BulkPartner(seq, code, rowNum));
2533
		}
2534
	}
2535
 
2536
	private static class BulkPartner {
2537
		final int seq;
2538
		final String code;
2539
		final int rowNum;
2540
 
2541
		BulkPartner(int seq, String code, int rowNum) {
2542
			this.seq = seq;
2543
			this.code = code;
2544
			this.rowNum = rowNum;
2545
		}
2546
	}
2547
 
36644 ranu 2548
	// ============ CALENDAR ============
36618 ranu 2549
 
2550
	@PostMapping(value = "/beatPlan/delete")
2551
	public ResponseEntity<?> deleteBeat(@RequestParam String planGroupId) {
36644 ranu 2552
		int beatId = Integer.parseInt(planGroupId);
36698 ranu 2553
		// Hard delete — wipe all child rows first, then the beat itself.
2554
		// The name slot is freed naturally because the row is gone.
36644 ranu 2555
		beatRouteRepository.deleteByBeatId(beatId);
2556
		beatScheduleRepository.deleteByBeatId(beatId);
36698 ranu 2557
		leadRouteRepository.deleteByBeatId(beatId);
36644 ranu 2558
		Beat beat = beatRepository.selectById(beatId);
2559
		if (beat != null) {
36698 ranu 2560
			beatRepository.delete(beat);
36644 ranu 2561
		}
36618 ranu 2562
 
2563
		Map<String, Object> response = new HashMap<>();
2564
		response.put("status", true);
2565
		response.put("message", "Beat deleted");
2566
		return responseSender.ok(response);
2567
	}
2568
 
36670 ranu 2569
	// Unschedule the beat from ONE specific date — does NOT delete the beat.
2570
	// The beat (and its route template) stays; only the matching beat_schedule
2571
	// row is removed. If no real-date schedules remain, a placeholder
2572
	// (9999-12-31) row is added so the beat still shows up as "unscheduled".
2573
	@PostMapping(value = "/beatPlan/unscheduleDate")
2574
	public ResponseEntity<?> unscheduleDate(
2575
			@RequestParam String planGroupId,
2576
			@RequestParam String date) {
2577
		int beatId = Integer.parseInt(planGroupId);
2578
		LocalDate target = LocalDate.parse(date);
2579
 
2580
		Beat beat = beatRepository.selectById(beatId);
2581
		if (beat == null) return responseSender.badRequest("Beat not found");
2582
 
2583
		List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(beatId);
2584
		int removed = 0;
2585
		for (BeatSchedule s : schedules) {
2586
			if (s.getStartDate() != null && s.getStartDate().equals(target)) {
2587
				beatScheduleRepository.delete(s);
2588
				removed++;
2589
			}
2590
		}
2591
		if (removed == 0) return responseSender.badRequest("No schedule found for that date");
2592
 
2593
		// If no real-date schedules left, drop in a placeholder so the beat
2594
		// remains visible in the unscheduled bucket.
2595
		boolean hasReal = schedules.stream()
2596
				.filter(s -> !s.getStartDate().equals(target))
2597
				.anyMatch(s -> s.getStartDate() != null && s.getStartDate().getYear() != 9999);
2598
		if (!hasReal) {
2599
			boolean hasPlaceholder = schedules.stream()
2600
					.filter(s -> !s.getStartDate().equals(target))
2601
					.anyMatch(s -> s.getStartDate() != null && s.getStartDate().getYear() == 9999);
2602
			if (!hasPlaceholder) {
2603
				BeatSchedule ph = new BeatSchedule();
2604
				ph.setBeatId(beatId);
2605
				ph.setStartDate(LocalDate.of(9999, 12, 31));
2606
				ph.setDayNumber(1);
2607
				ph.setEndAction("HOME");
2608
				ph.setCreatedTimestamp(LocalDateTime.now());
2609
				beatScheduleRepository.persist(ph);
2610
			}
2611
		}
2612
 
2613
		Map<String, Object> response = new HashMap<>();
2614
		response.put("status", true);
2615
		response.put("message", "Unscheduled from " + date);
2616
		return responseSender.ok(response);
2617
	}
2618
 
36785 ranu 2619
	/**
2620
	 * Per-user one-beat-per-day guard. Walks every active beat the sales user
2621
	 * already has and looks for a schedule row on any of the candidate dates.
2622
	 * Returns null if the candidate dates are clear, otherwise a {date,beatName}
2623
	 * map describing the first collision so the caller can surface a clean error.
2624
	 * Pass `excludeBeatId` so callers that are re-scheduling an existing beat
2625
	 * don't trip on their own pre-existing schedule rows; pass 0 for new beats.
2626
	 */
2627
	private Map<String, Object> findScheduleConflict(int authUserId, java.util.Collection<LocalDate> candidates, int excludeBeatId) {
2628
		if (candidates == null || candidates.isEmpty()) return null;
2629
		Set<LocalDate> ds = new HashSet<>();
2630
		for (LocalDate d : candidates) {
2631
			if (d != null && d.getYear() != 9999) ds.add(d);
2632
		}
2633
		if (ds.isEmpty()) return null;
2634
		for (Beat ub : beatRepository.selectActiveByAuthUserId(authUserId)) {
2635
			if (ub.getId() == excludeBeatId) continue;
2636
			for (BeatSchedule s : beatScheduleRepository.selectByBeatId(ub.getId())) {
2637
				if (s.getStartDate() != null && ds.contains(s.getStartDate())) {
2638
					Map<String, Object> conflict = new HashMap<>();
2639
					conflict.put("date", s.getStartDate().toString());
2640
					conflict.put("beatName", ub.getName() != null ? ub.getName() : "Beat #" + ub.getId());
2641
					return conflict;
2642
				}
2643
			}
2644
		}
2645
		return null;
2646
	}
2647
 
2648
	private String scheduleConflictMessage(Map<String, Object> conflict) {
2649
		return "Cannot schedule on " + conflict.get("date")
2650
				+ " — \"" + conflict.get("beatName") + "\" is already scheduled for this user on that day.";
2651
	}
2652
 
2653
	@PostMapping(value = "/beatPlan/scheduleOnCalendar")
2654
	public ResponseEntity<?> scheduleOnCalendar(
2655
			HttpServletRequest request,
36670 ranu 2656
			@RequestParam String planGroupId,
36785 ranu 2657
			@RequestParam String dates,
2658
			@RequestParam(required = false) String beatName,
2659
			@RequestParam(required = false) String beatColor) throws Exception {
2660
 
36670 ranu 2661
		int beatId = Integer.parseInt(planGroupId);
36785 ranu 2662
		Gson gson = new Gson();
2663
		List<String> dateList = gson.fromJson(dates, new TypeToken<List<String>>() {
2664
		}.getType());
36670 ranu 2665
 
2666
		Beat beat = beatRepository.selectById(beatId);
2667
		if (beat == null) return responseSender.badRequest("Beat not found");
2668
 
36785 ranu 2669
		if (beatName != null) beat.setName(beatName);
2670
		if (beatColor != null && !beatColor.isEmpty()) beat.setBeatColor(beatColor);
36670 ranu 2671
 
36785 ranu 2672
		// One-beat-per-day guard: reject if any of the requested dates already
2673
		// has another beat scheduled for this user (excluding this beat itself).
2674
		List<LocalDate> requested = new ArrayList<>();
2675
		for (String s : dateList) {
2676
			try {
2677
				requested.add(LocalDate.parse(s));
2678
			} catch (Exception ignored) {
36670 ranu 2679
			}
2680
		}
36785 ranu 2681
		Map<String, Object> conflict = findScheduleConflict(beat.getAuthUserId(), requested, beatId);
2682
		if (conflict != null) return responseSender.badRequest(scheduleConflictMessage(conflict));
36670 ranu 2683
 
36785 ranu 2684
		// Delete old schedules and create new
2685
		beatScheduleRepository.deleteByBeatId(beatId);
2686
		LocalDate schEndDate = dateList.isEmpty() ? null : LocalDate.parse(dateList.get(dateList.size() - 1));
2687
		for (int i = 0; i < dateList.size() && i < beat.getTotalDays(); i++) {
2688
            int dayNumber = i + 1;
2689
            String endAction = (i == dateList.size() - 1) ? "HOME" : "DAYBREAK";
2690
			BeatSchedule schedule = new BeatSchedule();
2691
			schedule.setBeatId(beatId);
2692
			schedule.setStartDate(LocalDate.parse(dateList.get(i)));
2693
			schedule.setEndDate(schEndDate);
2694
            schedule.setDayNumber(dayNumber);
2695
            schedule.setEndAction(endAction);
2696
            // Fill total_distance_km / total_time_mins from beat_route so the new
2697
            // schedule row isn't NULL (this was the bug — these were left unset).
2698
            double[] totals = computeDayTotals(beatId, dayNumber, endAction);
2699
            schedule.setTotalDistanceKm(totals[0]);
2700
            schedule.setTotalTimeMins((int) totals[1]);
2701
			schedule.setCreatedTimestamp(LocalDateTime.now());
2702
			beatScheduleRepository.persist(schedule);
2703
		}
2704
 
36670 ranu 2705
		Map<String, Object> response = new HashMap<>();
2706
		response.put("status", true);
36785 ranu 2707
		response.put("message", "Beat scheduled successfully");
36670 ranu 2708
		return responseSender.ok(response);
2709
	}
2710
 
36618 ranu 2711
	@GetMapping(value = "/beatPlan/calendar")
2712
	public ResponseEntity<?> getCalendar(
2713
			@RequestParam int authUserId,
2714
			@RequestParam String month) {
2715
 
2716
		YearMonth ym = YearMonth.parse(month);
2717
		LocalDate startDate = ym.atDay(1);
2718
		LocalDate endDate = ym.atEndOfMonth();
2719
 
2720
		List<PublicHolidays> holidays = publicHolidaysRepository.selectAllBetweenDates(startDate, endDate);
2721
		List<Map<String, String>> holidayList = holidays.stream().map(h -> {
2722
			Map<String, String> m = new HashMap<>();
2723
			m.put("date", h.getDate().toString());
2724
			m.put("occasion", h.getOccasion());
2725
			return m;
2726
		}).collect(Collectors.toList());
2727
 
36644 ranu 2728
		List<Beat> allBeats = beatRepository.selectActiveByAuthUserId(authUserId);
36618 ranu 2729
		LocalDate today = LocalDate.now();
2730
		List<Map<String, Object>> scheduledBeats = new ArrayList<>();
2731
 
36644 ranu 2732
		for (Beat beat : allBeats) {
2733
			List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(beat.getId());
2734
			List<BeatRoute> routes = beatRouteRepository.selectByBeatId(beat.getId());
36618 ranu 2735
 
36644 ranu 2736
			boolean allNullDates = schedules.isEmpty() || schedules.stream().allMatch(s -> s.getStartDate().getYear() == 9999);
2737
			boolean hasToday = !allNullDates && schedules.stream().anyMatch(s -> s.getStartDate().equals(today));
2738
			boolean allPast = !allNullDates && schedules.stream().filter(s -> s.getStartDate().getYear() != 9999).allMatch(s -> s.getStartDate().isBefore(today));
2739
			boolean allFuture = !allNullDates && schedules.stream().filter(s -> s.getStartDate().getYear() != 9999).allMatch(s -> s.getStartDate().isAfter(today));
36618 ranu 2740
 
2741
			String status;
2742
			if (allNullDates) status = "unscheduled";
2743
			else if (hasToday) status = "running";
2744
			else if (allPast) status = "completed";
36644 ranu 2745
			else status = "scheduled";
36618 ranu 2746
 
36644 ranu 2747
			Map<String, Object> beatInfo = new HashMap<>();
2748
			beatInfo.put("planGroupId", String.valueOf(beat.getId()));
2749
			beatInfo.put("beatName", beat.getName() != null ? beat.getName() : "Beat");
2750
			beatInfo.put("beatColor", beat.getBeatColor() != null ? beat.getBeatColor() : "#3498DB");
2751
			beatInfo.put("status", status);
36728 vikas 2752
			beatInfo.put("totalDays", beat.getTotalDays());
36618 ranu 2753
 
2754
			List<Map<String, Object>> dayInfoList = new ArrayList<>();
36644 ranu 2755
			for (BeatSchedule s : schedules) {
36618 ranu 2756
				Map<String, Object> dayInfo = new HashMap<>();
36644 ranu 2757
				dayInfo.put("dayNumber", s.getDayNumber());
2758
				boolean isUnscheduled = s.getStartDate().getYear() == 9999;
2759
				dayInfo.put("planDate", isUnscheduled ? null : s.getStartDate().toString());
2760
				dayInfo.put("totalKm", s.getTotalDistanceKm());
2761
				dayInfo.put("totalMins", s.getTotalTimeMins());
36711 ranu 2762
                // endAction tells the planner whether to draw the return-to-home line
2763
                // for this day (HOME) or end at the last stop (DAYBREAK).
2764
                dayInfo.put("endAction", s.getEndAction());
36644 ranu 2765
				long visitCount = routes.stream().filter(r -> r.getDayNumber() == s.getDayNumber()).count();
2766
				dayInfo.put("visitCount", (int) visitCount);
36618 ranu 2767
				dayInfoList.add(dayInfo);
2768
			}
36644 ranu 2769
			if (schedules.isEmpty()) {
2770
				// No schedule at all — show from routes
2771
				Map<Integer, Long> dayCounts = routes.stream()
2772
						.collect(Collectors.groupingBy(BeatRoute::getDayNumber, Collectors.counting()));
2773
				for (int d = 1; d <= beat.getTotalDays(); d++) {
2774
					Map<String, Object> dayInfo = new HashMap<>();
2775
					dayInfo.put("dayNumber", d);
2776
					dayInfo.put("planDate", null);
2777
					dayInfo.put("totalKm", null);
2778
					dayInfo.put("totalMins", null);
2779
					dayInfo.put("visitCount", dayCounts.getOrDefault(d, 0L).intValue());
2780
					dayInfoList.add(dayInfo);
2781
				}
2782
			}
2783
			beatInfo.put("days", dayInfoList);
2784
			scheduledBeats.add(beatInfo);
36618 ranu 2785
		}
2786
 
2787
		Set<String> blockedDates = new HashSet<>();
2788
		for (LocalDate d = startDate; !d.isAfter(endDate); d = d.plusDays(1)) {
36644 ranu 2789
			if (d.getDayOfWeek() == DayOfWeek.SUNDAY) blockedDates.add(d.toString());
36618 ranu 2790
		}
36644 ranu 2791
		for (PublicHolidays h : holidays) blockedDates.add(h.getDate().toString());
36618 ranu 2792
 
2793
		Map<String, Object> response = new HashMap<>();
2794
		response.put("holidays", holidayList);
2795
		response.put("scheduledBeats", scheduledBeats);
2796
		response.put("blockedDates", blockedDates);
2797
		return responseSender.ok(response);
2798
	}
2799
 
36644 ranu 2800
	// Drag-drop scheduling — adds schedule dates to the EXISTING beat (no new beat created)
36618 ranu 2801
	@PostMapping(value = "/beatPlan/repeatBeat")
2802
	public ResponseEntity<?> repeatBeat(
2803
			HttpServletRequest request,
2804
			@RequestParam String sourcePlanGroupId,
2805
			@RequestParam int authUserId,
2806
			@RequestParam String dates) throws Exception {
2807
 
36644 ranu 2808
		int beatId = Integer.parseInt(sourcePlanGroupId);
36618 ranu 2809
		Gson gson = new Gson();
2810
		List<String> dateList = gson.fromJson(dates, new TypeToken<List<String>>() {
2811
		}.getType());
2812
 
36644 ranu 2813
		Beat beat = beatRepository.selectById(beatId);
2814
		if (beat == null) return responseSender.badRequest("Beat not found");
36618 ranu 2815
 
36785 ranu 2816
		// One-beat-per-day guard: reject if any of the new dates already has
2817
		// another beat scheduled for this user (excluding this beat itself).
2818
		List<LocalDate> repeatDates = new ArrayList<>();
2819
		for (String s : dateList) {
2820
			try {
2821
				repeatDates.add(LocalDate.parse(s));
2822
			} catch (Exception ignored) {
2823
			}
2824
		}
2825
		Map<String, Object> repeatConflict = findScheduleConflict(beat.getAuthUserId(), repeatDates, beatId);
2826
		if (repeatConflict != null) return responseSender.badRequest(scheduleConflictMessage(repeatConflict));
2827
 
36644 ranu 2828
		// Remove placeholder (unscheduled) schedule rows
2829
		List<BeatSchedule> existing = beatScheduleRepository.selectByBeatId(beatId);
2830
		for (BeatSchedule s : existing) {
2831
			if (s.getStartDate() != null && s.getStartDate().getYear() == 9999) {
2832
				beatScheduleRepository.delete(s);
2833
			}
36618 ranu 2834
		}
2835
 
36711 ranu 2836
        // Add new real-date schedule rows for the existing beat — fill totals
2837
        // from beat_route so total_distance_km / total_time_mins aren't NULL.
36644 ranu 2838
		LocalDate repeatEndDate = dateList.isEmpty() ? null : LocalDate.parse(dateList.get(dateList.size() - 1));
2839
		for (int i = 0; i < dateList.size(); i++) {
36711 ranu 2840
            int dayNumber = i + 1;
2841
            String endAction = (i == dateList.size() - 1) ? "HOME" : "DAYBREAK";
36644 ranu 2842
			BeatSchedule schedule = new BeatSchedule();
2843
			schedule.setBeatId(beatId);
2844
			schedule.setStartDate(LocalDate.parse(dateList.get(i)));
2845
			schedule.setEndDate(repeatEndDate);
36711 ranu 2846
            schedule.setDayNumber(dayNumber);
2847
            schedule.setEndAction(endAction);
2848
            double[] totals = computeDayTotals(beatId, dayNumber, endAction);
2849
            schedule.setTotalDistanceKm(totals[0]);
2850
            schedule.setTotalTimeMins((int) totals[1]);
36644 ranu 2851
			schedule.setCreatedTimestamp(LocalDateTime.now());
2852
			beatScheduleRepository.persist(schedule);
36618 ranu 2853
		}
2854
 
2855
		Map<String, Object> response = new HashMap<>();
2856
		response.put("status", true);
36644 ranu 2857
		response.put("planGroupId", String.valueOf(beatId));
2858
		response.put("message", "Beat scheduled successfully");
36618 ranu 2859
		return responseSender.ok(response);
2860
	}
2861
 
36785 ranu 2862
	private static class ValidatedBulkBeat {
2863
		final BulkBeatGroup g;
2864
		final int authUserId;
2865
		final List<Integer> sortedDays;
2866
		final List<LocalDate> scheduleDates;
2867
 
2868
		ValidatedBulkBeat(BulkBeatGroup g, int authUserId, List<Integer> sortedDays, List<LocalDate> scheduleDates) {
2869
			this.g = g;
2870
			this.authUserId = authUserId;
2871
			this.sortedDays = sortedDays;
2872
			this.scheduleDates = scheduleDates;
2873
		}
2874
	}
2875
 
36618 ranu 2876
	@GetMapping(value = "/beatPlan/availableSlots")
2877
	public ResponseEntity<?> getAvailableSlots(
2878
			@RequestParam int authUserId,
2879
			@RequestParam String month,
2880
			@RequestParam int daysNeeded) {
2881
 
2882
		YearMonth ym = YearMonth.parse(month);
2883
		LocalDate startDate = ym.atDay(1);
2884
		LocalDate endDate = ym.atEndOfMonth();
2885
		LocalDate today = LocalDate.now();
2886
 
2887
		Set<LocalDate> blocked = new HashSet<>();
2888
		for (LocalDate d = startDate; !d.isAfter(endDate); d = d.plusDays(1)) {
2889
			if (d.getDayOfWeek() == DayOfWeek.SUNDAY) blocked.add(d);
36644 ranu 2890
			if (!d.isAfter(today)) blocked.add(d);
36618 ranu 2891
		}
2892
 
2893
		List<PublicHolidays> holidays = publicHolidaysRepository.selectAllBetweenDates(startDate, endDate);
2894
		for (PublicHolidays h : holidays) blocked.add(h.getDate());
2895
 
36644 ranu 2896
		// Get all scheduled dates for this user
2897
		List<Beat> userBeats = beatRepository.selectActiveByAuthUserId(authUserId);
2898
		for (Beat b : userBeats) {
2899
			List<BeatSchedule> schedules = beatScheduleRepository.selectByBeatId(b.getId());
2900
			for (BeatSchedule s : schedules) blocked.add(s.getStartDate());
36618 ranu 2901
		}
2902
 
2903
		List<String> available = new ArrayList<>();
2904
		for (LocalDate d = startDate.isAfter(today) ? startDate : today.plusDays(1);
2905
			 !d.isAfter(endDate) && available.size() < daysNeeded;
2906
			 d = d.plusDays(1)) {
36644 ranu 2907
			if (!blocked.contains(d)) available.add(d.toString());
36618 ranu 2908
		}
2909
 
2910
		Map<String, Object> response = new HashMap<>();
2911
		response.put("suggestedDates", available);
2912
		response.put("totalAvailable", available.size());
2913
		return responseSender.ok(response);
2914
	}
2915
 
2916
	// --- Sorting helpers ---
2917
 
2918
	private List<Map<String, Object>> sortByNearestNeighborFromStart(
2919
			List<Map<String, Object>> partners, double startLat, double startLng) {
2920
		List<Map<String, Object>> withCoords = new ArrayList<>();
2921
		List<Map<String, Object>> withoutCoords = new ArrayList<>();
2922
		for (Map<String, Object> p : partners) {
36644 ranu 2923
			if (hasValidCoords(p)) withCoords.add(p);
2924
			else withoutCoords.add(p);
36618 ranu 2925
		}
2926
		List<Map<String, Object>> sorted = new ArrayList<>();
36644 ranu 2927
		double currentLat = startLat, currentLng = startLng;
36618 ranu 2928
		while (!withCoords.isEmpty()) {
2929
			int nearestIdx = 0;
2930
			double nearestDist = Double.MAX_VALUE;
2931
			for (int i = 0; i < withCoords.size(); i++) {
36644 ranu 2932
				double dist = haversine(currentLat, currentLng,
2933
						Double.parseDouble(withCoords.get(i).get("latitude").toString()),
2934
						Double.parseDouble(withCoords.get(i).get("longitude").toString()));
36618 ranu 2935
				if (dist < nearestDist) {
2936
					nearestDist = dist;
2937
					nearestIdx = i;
2938
				}
2939
			}
2940
			Map<String, Object> nearest = withCoords.remove(nearestIdx);
2941
			sorted.add(nearest);
2942
			currentLat = Double.parseDouble(nearest.get("latitude").toString());
2943
			currentLng = Double.parseDouble(nearest.get("longitude").toString());
2944
		}
2945
		sorted.addAll(withoutCoords);
2946
		return sorted;
2947
	}
2948
 
2949
	private List<Map<String, Object>> sortByNearestNeighbor(List<Map<String, Object>> partners) {
2950
		List<Map<String, Object>> withCoords = new ArrayList<>();
2951
		List<Map<String, Object>> withoutCoords = new ArrayList<>();
2952
		for (Map<String, Object> p : partners) {
36644 ranu 2953
			if (hasValidCoords(p)) withCoords.add(p);
2954
			else withoutCoords.add(p);
36618 ranu 2955
		}
2956
		List<Map<String, Object>> sorted = new ArrayList<>();
2957
		if (!withCoords.isEmpty()) {
2958
			sorted.add(withCoords.remove(0));
2959
			while (!withCoords.isEmpty()) {
2960
				Map<String, Object> last = sorted.get(sorted.size() - 1);
2961
				double lastLat = Double.parseDouble(last.get("latitude").toString());
2962
				double lastLng = Double.parseDouble(last.get("longitude").toString());
2963
				int nearestIdx = 0;
2964
				double nearestDist = Double.MAX_VALUE;
2965
				for (int i = 0; i < withCoords.size(); i++) {
36644 ranu 2966
					double dist = haversine(lastLat, lastLng,
2967
							Double.parseDouble(withCoords.get(i).get("latitude").toString()),
2968
							Double.parseDouble(withCoords.get(i).get("longitude").toString()));
36618 ranu 2969
					if (dist < nearestDist) {
2970
						nearestDist = dist;
2971
						nearestIdx = i;
2972
					}
2973
				}
2974
				sorted.add(withCoords.remove(nearestIdx));
2975
			}
2976
		}
2977
		sorted.addAll(withoutCoords);
2978
		return sorted;
2979
	}
2980
 
2981
	private boolean hasValidCoords(Map<String, Object> p) {
2982
		Object lat = p.get("latitude");
2983
		Object lng = p.get("longitude");
36644 ranu 2984
		return lat != null && lng != null && !lat.toString().isEmpty() && !lng.toString().isEmpty();
36618 ranu 2985
	}
2986
 
2987
	private double haversine(double lat1, double lng1, double lat2, double lng2) {
2988
		double R = 6371;
2989
		double dLat = Math.toRadians(lat2 - lat1);
2990
		double dLng = Math.toRadians(lng2 - lng1);
2991
		double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
2992
				+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2))
2993
				* Math.sin(dLng / 2) * Math.sin(dLng / 2);
2994
		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
2995
		return R * c;
2996
	}
2997
}