-
Notifications
You must be signed in to change notification settings - Fork 1
/
delivery-windows.liquid.html
208 lines (188 loc) · 5.95 KB
/
delivery-windows.liquid.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
<script type="text/javascript">
// Defines the cutoff windows
// @cutoff_dow: the day-of-week beyond which this window is not available (Sun=0;Sat=6)
// @cutoff_hour: the last hour of cutoff_dow when users can qualify for this window
// @dow: the day-of-week for the delivery (0=sunday, 6=saturday)
// @hours: the hours of the delivery window on `dow`
// @price: the price for delivery during that window
// NB: list *must* be in ascending order by cutoff day/hour, and may not work if
// cutoff and delivery span across the 6-0 boundary (i.e. the modular arithmetic)
var DeliveryWindows = {
"SF": [{
'cutoff_dow': 0,
'cutoff_hour': 9,
'dow': 1,
'hours': '8:00 AM - 12:00 PM'
},
{
'cutoff_dow': 0,
'cutoff_hour': 9,
'dow': 1,
'hours': '5:00 PM - 9:00 PM'
},{
'cutoff_dow': 1,
'cutoff_hour': 9,
'dow': 2,
'hours': '5:00 PM - 9:00 PM'
},
{
'cutoff_dow': 2,
'cutoff_hour': 9,
'dow': 3,
'hours': '8:00 AM - 12:00 PM'
},
{
'cutoff_dow': 2,
'cutoff_hour': 9,
'dow': 3,
'hours': '5:00 PM - 9:00 PM'
},
{
'cutoff_dow': 3,
'cutoff_hour': 9,
'dow': 4,
'hours': '5:00 PM - 9:00 PM'
},
{
'cutoff_dow': 4,
'cutoff_hour': 9,
'dow': 5,
'hours': '8:00 AM - 12:00 PM'
}],
"PE": [{
'cutoff_dow': 0,
'cutoff_hour': 9,
'dow': 1,
'hours': '8:00 AM - 12:00 PM'
}],
"EB": [{
'cutoff_dow': 0,
'cutoff_hour': 9,
'dow': 1,
'hours': '8:00 AM - 12:00 PM'
}],
"pickup": [{
'cutoff_dow': 0,
'cutoff_hour': 21,
'dow': 8,
'hours': '4-6 PM',
'product': 327399525,
'variant': 909212649,
'title': 'Presidio Knolls School (for PKS community only)'
}, {
'cutoff_dow': 5,
'cutoff_hour': 9,
'dow': 6,
'hours': '8 AM-Noon',
'product': 327399525,
'variant': 787566381,
'title': 'Downtown Palo Alto Farmers Market'
}],
"shipping": [{
'cutoff_dow': 0,
'cutoff_hour': 21,
'dow': 4,
'hours': '',
'product': 441096728,
'variant': 1213987804,
'title': 'California (outside Bay Area)'
},
{
'cutoff_dow': 0,
'cutoff_hour': 21,
'dow': 4,
'hours': '',
'product': 441096728,
'variant': 1213990852,
'title': 'Nationwide'
}]
};
// getWindows(referenceTime) -- returns a list of dates and delivery windows
// whose cutoff times have not yet passed
//
// @region: One of {SF, PE, EB} as defined in the global DeliveryWindows variable;
// @referenceDate: (optional) an XDate representing when to begin the search
//
// @returns: an array of {date, delivery_time} objects
function getWindows(region, referenceDate) {
referenceDate = (typeof referenceDate === "undefined") ? new XDate() : referenceDate;
var returnList = new Array();
for (var i = 0; i < DeliveryWindows[region].length; i++) {
var deliveryDate = referenceDate.clone();
if (referenceDate.getDay() == DeliveryWindows[region][i].cutoff_dow) {
if (referenceDate.getHours() < DeliveryWindows[region][i].cutoff_hour) {
daysUntilDOW = (DeliveryWindows[region][i].dow - deliveryDate.getDay() + 7) % 7;
deliveryDate.addDays(daysUntilDOW);
} else {
daysUntilDOW = (DeliveryWindows[region][i].dow - deliveryDate.getDay() + 7) % 7;
deliveryDate.addDays(daysUntilDOW);
deliveryDate.addWeeks(1); // plus a week, because we're past cutoff
}
} else {
deliveryDate.addDays(1); // just need to get to "tomorrow"
daysUntilDOW = (DeliveryWindows[region][i].dow - deliveryDate.getDay() + 7) % 7;
if (daysUntilDOW == 0) daysUntilDOW = 7; // make sure it's always a week
deliveryDate.addDays(daysUntilDOW);
}
returnList.push({
"date": deliveryDate.toString("dddd, MMMM dS, "),
"date_native": deliveryDate.getTime(),
"hours": DeliveryWindows[region][i].hours,
"price": DeliveryWindows[region][i].price,
"product": DeliveryWindows[region][i].product,
"variant": DeliveryWindows[region][i].variant,
"title": DeliveryWindows[region][i].title
});
}
returnList.sort(function(a, b) {
return a.date_native - b.date_native;
});
return (returnList);
}
function dateBetween(testDate, testHour, cutoffDate, cutoffHour, deliveryDate, deliveryHour) {
if (testDate > cutoffDate || (testDate == cutoffDate && testHour > cutoffHour)) if (testDate < deliveryDate || (testDate == deliveryDate && testHour < deliveryHour)) return true;
return false;
}
function makeOptions(windowList, selectObject) {
$.each(windowList, function(index, deliveryWindow) {
windowString = deliveryWindow.date + deliveryWindow.hours;
$("<option value='" + windowString + "'" + (deliveryWindow.variant === undefined ? ">" : "id='" + deliveryWindow.variant + "'>" + deliveryWindow.title + ": ") + windowString + "</option>").appendTo(selectObject);
});
if (windowList.length == 1) {
windowString = windowList[0].date + windowList[0].hours;
if (windowList[0].title)
windowString = windowList[0].title + " - " + windowString;
selectObject.after("<span>" + windowString + "</span>");
selectObject.val(windowString);
selectObject.hide();
}
}
function randMax(max) {
return Math.floor((Math.random() * max));
}
function testBetween() {
for (var i = 0; i < 100; i++) {
var testDate = randMax(7);
var cutoffDate = randMax(7);
var deliveryDate = randMax(7);
var testHour = randMax(24);
var cutoffHour = randMax(24);
var deliveryHour = randMax(24);
//////// console.log(testDate + ":" + testHour + "," + cutoffDate + ":" + cutoffHour + "," + deliveryDate + ":" + deliveryHour + " - " + dateBetween(testDate, testHour, cutoffDate, cutoffHour, deliveryDate, deliveryHour));
}
}
// run a unit test of all hours across seven days
function testWindows() {
for (var date = 1; date < 30; date++) {
for (var hour = 0; hour <= 23; hour++) {
var referenceDate = new XDate(2014, 2, date, hour); // test month of March (2)
var windowList = getWindows("SF", referenceDate);
var windowListStr = " - ";
for (var i = 0; i < windowList.length; i++) {
windowListStr += windowList[i].date.toString("M/dd,");
}
//////// console.log(referenceDate.toString("ddd MMM dS@HH") + "h - " + windowListStr);
}
}
}
</script>