Subversion Repositories SmartDukaan

Rev

Rev 36962 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36962 Rev 37060
Line 509... Line 509...
509
											lat: p.latitude ? parseFloat(p.latitude) : null,
509
											lat: p.latitude ? parseFloat(p.latitude) : null,
510
											lng: p.longitude ? parseFloat(p.longitude) : null
510
											lng: p.longitude ? parseFloat(p.longitude) : null
511
										});
511
										});
512
									}
512
									}
513
								});
513
								});
-
 
514
								// Day 1 starts at the beat's own saved start location when set;
-
 
515
								// otherwise fall back to the user's default base. Later days
-
 
516
								// start where the prior day ended (handled by the Day Break flow).
-
 
517
								var dayStartLat = state.homeLat, dayStartLng = state.homeLng, dayStartName = state.homeName;
-
 
518
								if (d.dayNumber === 1) {
-
 
519
									var bLat = beat.startLatitude != null ? parseFloat(beat.startLatitude) : NaN;
-
 
520
									var bLng = beat.startLongitude != null ? parseFloat(beat.startLongitude) : NaN;
-
 
521
									if (!isNaN(bLat) && !isNaN(bLng)) {
-
 
522
										dayStartLat = bLat;
-
 
523
										dayStartLng = bLng;
-
 
524
										dayStartName = beat.startLocationName || state.homeName;
-
 
525
									}
-
 
526
								}
514
								state.days.push({
527
								state.days.push({
515
									dayNumber: d.dayNumber,
528
									dayNumber: d.dayNumber,
516
									startLat: state.homeLat, startLng: state.homeLng,
529
									startLat: dayStartLat, startLng: dayStartLng,
517
									startName: state.homeName,
530
									startName: dayStartName,
518
									visits: builtVisits,
531
									visits: builtVisits,
519
									endAction: d.endAction || (d.dayNumber < beat.days.length ? 'DAYBREAK' : 'HOME'),
532
									endAction: d.endAction || (d.dayNumber < beat.days.length ? 'DAYBREAK' : 'HOME'),
520
									totalKm: 0, totalMins: 0
533
									totalKm: 0, totalMins: 0
521
								});
534
								});
522
							});
535
							});
Line 1087... Line 1100...
1087
		lng: p.longitude ? parseFloat(p.longitude) : null
1100
		lng: p.longitude ? parseFloat(p.longitude) : null
1088
	};
1101
	};
1089
 
1102
 
1090
	day.visits.push(visit);
1103
	day.visits.push(visit);
1091
 
1104
 
1092
	// Update marker to green
1105
	// Auto-order the day into a nearest-neighbor route. The just-added stop may
1093
	if (markers[fofoId]) {
-
 
1094
		markers[fofoId].setIcon({
-
 
1095
			path: google.maps.SymbolPath.CIRCLE, scale: 14,
1106
	// no longer be last, so recolour + renumber every current-day marker by its
1096
			fillColor: '#22c55e', fillOpacity: 0.9, strokeColor: '#fff', strokeWeight: 2
1107
	// new index (resetMarkerColors handles both) rather than labelling this one.
1097
		});
-
 
1098
		markers[fofoId].setLabel({text: '' + day.visits.length, color: '#fff', fontSize: '11px', fontWeight: '700'});
1108
	sortDayByNearestNeighbor(day);
1099
	}
1109
	resetMarkerColors();
1100
 
1110
 
1101
	recalcDay();
1111
	recalcDay();
1102
	drawRoute();
1112
	drawRoute();
1103
	renderRoutePanel();
1113
	renderRoutePanel();
1104
 
1114
 
Line 1276... Line 1286...
1276
	day.totalKm = km;
1286
	day.totalKm = km;
1277
	day.totalMins = (km / AVG_SPEED) * 60 + day.visits.length * VISIT_MINS;
1287
	day.totalMins = (km / AVG_SPEED) * 60 + day.visits.length * VISIT_MINS;
1278
	updateBottomBar();
1288
	updateBottomBar();
1279
}
1289
}
1280
 
1290
 
-
 
1291
// Auto-order a day's stops into a nearest-neighbor route from the day's start
-
 
1292
// point (beat start / base for day 1, prior day's end for later days). Reorders
-
 
1293
// day.visits in place; sequence = array index downstream. Stops without valid
-
 
1294
// coords (e.g. leads with no captured geo) are parked at the end in their
-
 
1295
// existing relative order rather than dropped.
-
 
1296
function sortDayByNearestNeighbor(day) {
-
 
1297
	if (!day || !day.visits || day.visits.length < 2) return;
-
 
1298
 
-
 
1299
	var routable = [], parked = [];
-
 
1300
	day.visits.forEach(function (v) {
-
 
1301
		if (typeof v.lat === 'number' && typeof v.lng === 'number' && !isNaN(v.lat) && !isNaN(v.lng)) routable.push(v);
-
 
1302
		else parked.push(v);
-
 
1303
	});
-
 
1304
 
-
 
1305
	var haveStart = (typeof day.startLat === 'number' && typeof day.startLng === 'number' && !isNaN(day.startLat) && !isNaN(day.startLng));
-
 
1306
	var ordered = [], curLat, curLng;
-
 
1307
	if (haveStart) {
-
 
1308
		curLat = day.startLat;
-
 
1309
		curLng = day.startLng;
-
 
1310
	} else if (routable.length > 0) {
-
 
1311
		var first = routable.shift();
-
 
1312
		ordered.push(first);
-
 
1313
		curLat = first.lat;
-
 
1314
		curLng = first.lng;
-
 
1315
	}
-
 
1316
 
-
 
1317
	while (routable.length > 0) {
-
 
1318
		var bestIdx = 0, bestD = Infinity;
-
 
1319
		for (var i = 0; i < routable.length; i++) {
-
 
1320
			var d = haversine(curLat, curLng, routable[i].lat, routable[i].lng);
-
 
1321
			if (d < bestD) { bestD = d; bestIdx = i; }
-
 
1322
		}
-
 
1323
		var next = routable.splice(bestIdx, 1)[0];
-
 
1324
		ordered.push(next);
-
 
1325
		curLat = next.lat;
-
 
1326
		curLng = next.lng;
-
 
1327
	}
-
 
1328
 
-
 
1329
	day.visits.length = 0;
-
 
1330
	ordered.forEach(function (v) { day.visits.push(v); });
-
 
1331
	parked.forEach(function (v) { day.visits.push(v); });
-
 
1332
}
-
 
1333
 
1281
function updateBottomBar() {
1334
function updateBottomBar() {
1282
	var day = curDay();
1335
	var day = curDay();
1283
	$('#bb-day-label').text('Day ' + day.dayNumber);
1336
	$('#bb-day-label').text('Day ' + day.dayNumber);
1284
	$('#bb-stops').text(day.visits.length + ' stops');
1337
	$('#bb-stops').text(day.visits.length + ' stops');
1285
	$('#bb-distance').text(day.totalKm.toFixed(1) + ' km');
1338
	$('#bb-distance').text(day.totalKm.toFixed(1) + ' km');
Line 1437... Line 1490...
1437
	// Remove visit from day
1490
	// Remove visit from day
1438
	day.visits = day.visits.filter(function (v) {
1491
	day.visits = day.visits.filter(function (v) {
1439
		return v.id !== fofoId;
1492
		return v.id !== fofoId;
1440
	});
1493
	});
1441
 
1494
 
-
 
1495
	// Re-route the remaining stops (nearest-neighbor) before re-numbering below.
-
 
1496
	sortDayByNearestNeighbor(day);
-
 
1497
 
1442
	// Reset marker back to red (unvisited)
1498
	// Reset marker back to red (unvisited)
1443
	if (markers[fofoId]) {
1499
	if (markers[fofoId]) {
1444
		var p = state.partners.find(function (x) {
1500
		var p = state.partners.find(function (x) {
1445
			return x.fofoId === fofoId;
1501
			return x.fofoId === fofoId;
1446
		});
1502
		});