Subversion Repositories SmartDukaan

Rev

Rev 36843 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 36843 Rev 36845
Line 12... Line 12...
12
    const m = String(d.getMonth() + 1).padStart(2, '0');
12
    const m = String(d.getMonth() + 1).padStart(2, '0');
13
    const day = String(d.getDate()).padStart(2, '0');
13
    const day = String(d.getDate()).padStart(2, '0');
14
    return `${y}-${m}-${day}`;
14
    return `${y}-${m}-${day}`;
15
}
15
}
16
 
16
 
-
 
17
// Parse a "YYYY-MM-DD" string into a LOCAL-midnight Date. `new Date("YYYY-MM-DD")`
-
 
18
// parses as UTC midnight, which in timezones ahead of UTC (e.g. IST +5:30) compares
-
 
19
// as later than local midnight "today" — making today's date wrongly fail the range check.
-
 
20
function parseLocalYMD(s) {
-
 
21
    const [y, m, day] = s.split('-').map(Number);
-
 
22
    return new Date(y, m - 1, day);
-
 
23
}
-
 
24
 
17
// Restrict the Acquired Date picker to the last 7 days (today and the 7 days before it).
25
// Restrict the Acquired Date picker to the last 7 days (today and the 7 days before it).
18
// The form HTML is injected via AJAX, so set min/max lazily when the field is focused.
26
// The form HTML is injected via AJAX, so set min/max lazily when the field is focused.
19
// NOTE: native min/max only reliably greys out dates in Chrome's calendar; Firefox/Safari
27
// NOTE: native min/max only reliably greys out dates in Chrome's calendar; Firefox/Safari
20
// still let the user pick an out-of-range date, so we also enforce the range on 'change'.
28
// still let the user pick an out-of-range date, so we also enforce the range on 'change'.
21
$(document).on('focus', 'input[name="acquiredDate"]', function () {
29
$(document).on('focus', 'input[name="acquiredDate"]', function () {
Line 29... Line 37...
29
// Browser-independent guard: if the selected Acquired Date is outside the last 7 days,
37
// Browser-independent guard: if the selected Acquired Date is outside the last 7 days,
30
// reject it immediately (clear the field) instead of waiting until form submit.
38
// reject it immediately (clear the field) instead of waiting until form submit.
31
$(document).on('change', 'input[name="acquiredDate"]', function () {
39
$(document).on('change', 'input[name="acquiredDate"]', function () {
32
    const picked = $(this).val();
40
    const picked = $(this).val();
33
    if (!picked) return;
41
    if (!picked) return;
34
    const acq = new Date(picked);
42
    const acq = parseLocalYMD(picked);
35
    const today = new Date();
43
    const today = new Date();
36
    today.setHours(0, 0, 0, 0);
44
    today.setHours(0, 0, 0, 0);
37
    const minAllowed = new Date(today);
45
    const minAllowed = new Date(today);
38
    minAllowed.setDate(today.getDate() - 7);
46
    minAllowed.setDate(today.getDate() - 7);
39
    if (acq > today || acq < minAllowed) {
47
    if (acq > today || acq < minAllowed) {
Line 187... Line 195...
187
    if (!stateHead) missingFields.push('State head');
195
    if (!stateHead) missingFields.push('State head');
188
    if (!acquiredDate) {
196
    if (!acquiredDate) {
189
        missingFields.push('Acquired date');
197
        missingFields.push('Acquired date');
190
    } else {
198
    } else {
191
        // Acquired date must fall within the last 7 days (not in the future, not older than 7 days back)
199
        // Acquired date must fall within the last 7 days (not in the future, not older than 7 days back)
192
        const acq = new Date(acquiredDate);
200
        const acq = parseLocalYMD(acquiredDate);
193
        const today = new Date();
201
        const today = new Date();
194
        today.setHours(0, 0, 0, 0);
202
        today.setHours(0, 0, 0, 0);
195
        const minAllowed = new Date(today);
203
        const minAllowed = new Date(today);
196
        minAllowed.setDate(today.getDate() - 7);
204
        minAllowed.setDate(today.getDate() - 7);
197
        if (acq > today || acq < minAllowed) {
205
        if (acq > today || acq < minAllowed) {