Subversion Repositories SmartDukaan

Rev

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

Rev 36064 Rev 37881
Line 66... Line 66...
66
            var payload = orderDetailsPayload();
66
            var payload = orderDetailsPayload();
67
            if (!validateOrderDetails()) {
67
            if (!validateOrderDetails()) {
68
                alert("Please fix highlighted errors");
68
                alert("Please fix highlighted errors");
69
                return false;
69
                return false;
70
            }
70
            }
-
 
71
            checkImeiEligibilityThenProceed(function () {
71
            doPostAjaxRequestWithJsonHandler(context + "/create-order", payload, function (response) {
72
                doPostAjaxRequestWithJsonHandler(context + "/create-order", payload, function (response) {
72
                emptyBag();
73
                    emptyBag();
73
                $('#main-content').html(response);
74
                    $('#main-content').html(response);
-
 
75
                });
74
            });
76
            });
75
            return false; // required to block normal submit since you used
77
            return false; // required to block normal submit since you used
76
            // ajax
78
            // ajax
77
        }
79
        }
78
 
80
 
Line 610... Line 612...
610
        return;
612
        return;
611
    }
613
    }
612
 
614
 
613
 
615
 
614
}
616
}
-
 
617
 
-
 
618
// =============================================================================
-
 
619
// IMEI activation-age check for Navratri scratch coupon eligibility.
-
 
620
//
-
 
621
// Two touch-points:
-
 
622
// 1. On blur/change of an IMEI input → inline ✓/⚠ note under the field so the
-
 
623
//    partner knows upfront whether that IMEI will earn a coupon.
-
 
624
// 2. On Create Order submit → checkImeiEligibilityThenProceed() runs a batch
-
 
625
//    check and, if any IMEI is stale, shows a confirm() dialog. The partner can
-
 
626
//    cancel (bill not created) or OK (bill created without a scratch coupon
-
 
627
//    for the stale IMEIs). Fresh IMEIs still earn coupons as normal.
-
 
628
//
-
 
629
// Never silently blocks the bill — the eligibility endpoint being down means
-
 
630
// we skip the check (fail-open) rather than freeze billing.
-
 
631
// =============================================================================
-
 
632
 
-
 
633
/**
-
 
634
 * Collect every IMEI entered on the form, hit /scratch-eligibility for each,
-
 
635
 * and only invoke onProceed() once we're sure the partner wants to continue.
-
 
636
 * - All eligible → onProceed() fires immediately after the checks finish.
-
 
637
 * - Any ineligible → confirm() dialog listing the stale IMEIs; onProceed only
-
 
638
 *   fires if the partner clicks OK.
-
 
639
 * - Endpoint error / no IMEIs → onProceed() fires without the dialog.
-
 
640
 */
-
 
641
function checkImeiEligibilityThenProceed(onProceed) {
-
 
642
    var imeis = [];
-
 
643
    $("form#cd input.serialNumber").each(function () {
-
 
644
        var raw = ($(this).val() || '').trim();
-
 
645
        if (!raw) return;
-
 
646
        raw.split(',').forEach(function (s) {
-
 
647
            var sn = s.trim();
-
 
648
            if (sn) imeis.push(sn);
-
 
649
        });
-
 
650
    });
-
 
651
    if (imeis.length === 0) {
-
 
652
        onProceed();
-
 
653
        return;
-
 
654
    }
-
 
655
 
-
 
656
    var stale = [];
-
 
657
    var pending = imeis.length;
-
 
658
    imeis.forEach(function (imei) {
-
 
659
        $.ajax({
-
 
660
            url: (typeof context !== 'undefined' ? context : '') + '/scratch-eligibility?imei=' + encodeURIComponent(imei),
-
 
661
            method: 'GET',
-
 
662
            dataType: 'json',
-
 
663
            success: function (data) {
-
 
664
                if (data && data.eligible === false) {
-
 
665
                    stale.push({imei: imei, message: data.message});
-
 
666
                }
-
 
667
            },
-
 
668
            error: function () {
-
 
669
                // Endpoint down or network blip — don't hold up the bill.
-
 
670
            },
-
 
671
            complete: function () {
-
 
672
                pending--;
-
 
673
                if (pending === 0) finalize();
-
 
674
            }
-
 
675
        });
-
 
676
    });
-
 
677
 
-
 
678
    function finalize() {
-
 
679
        if (stale.length === 0) {
-
 
680
            onProceed();
-
 
681
            return;
-
 
682
        }
-
 
683
        var msg = "⚠ The following IMEI(s) are NOT eligible for the Navratri scratch coupon:\n\n";
-
 
684
        stale.forEach(function (r) {
-
 
685
            msg += "• " + r.imei + "\n  " + (r.message || 'Activated more than 2 days ago') + "\n\n";
-
 
686
        });
-
 
687
        msg += "The bill will be created, but NO scratch coupon will be awarded for these IMEIs.\n\nDo you want to continue?";
-
 
688
        if (confirm(msg)) {
-
 
689
            onProceed();
-
 
690
        }
-
 
691
    }
-
 
692
}
-
 
693
 
-
 
694
 
-
 
695
// Also fire the IMEI check whenever the Unit Price is entered/edited, since
-
 
696
// the IMEI input itself is often readonly (populated when the item is added)
-
 
697
// and never emits a blur event of its own. Price entry is a real user action
-
 
698
// that happens on every bill, so it's a reliable moment to re-check the row.
-
 
699
$(document).on('blur change', 'input.unitPrice', function () {
-
 
700
    var $row = $(this).closest('tr');
-
 
701
    $row.find('input.serialNumber').each(function () {
-
 
702
        $(this).trigger('blur');
-
 
703
    });
-
 
704
});
-
 
705
 
-
 
706
$(document).on('blur change', 'input.serialNumber', function () {
-
 
707
    var $input = $(this);
-
 
708
    var raw = ($input.val() || '').trim();
-
 
709
    var $cell = $input.closest('td');
-
 
710
    var $note = $cell.find('.scratch-eligibility-note');
-
 
711
    if ($note.length === 0) {
-
 
712
        $note = $('<div class="scratch-eligibility-note" style="margin-top:4px;line-height:1.3;"></div>');
-
 
713
        // Place below the entire input-group (so it spans the cell, not just the input width).
-
 
714
        $input.closest('.input-group').after($note);
-
 
715
    }
-
 
716
    $note.empty();
-
 
717
    if (raw === '') {
-
 
718
        return;
-
 
719
    }
-
 
720
    var imeis = raw.split(',')
-
 
721
        .map(function (s) {
-
 
722
            return s.trim();
-
 
723
        })
-
 
724
        .filter(function (s) {
-
 
725
            return s !== '';
-
 
726
        });
-
 
727
    imeis.forEach(function (imei) {
-
 
728
        $.ajax({
-
 
729
            url: (typeof context !== 'undefined' ? context : '') + '/scratch-eligibility?imei=' + encodeURIComponent(imei),
-
 
730
            method: 'GET',
-
 
731
            dataType: 'json',
-
 
732
            success: function (data) {
-
 
733
                if (!data) return;
-
 
734
                var eligible = data.eligible === true;
-
 
735
                // Shortened message — the IMEI is already visible in the input right above.
-
 
736
                var short;
-
 
737
                if (data.daysSinceActivation == null) {
-
 
738
                    short = 'Fresh IMEI — coupon will be awarded';
-
 
739
                } else if (eligible) {
-
 
740
                    short = 'Activated ' + data.daysSinceActivation + 'd ago — coupon will be awarded';
-
 
741
                } else {
-
 
742
                    short = 'Activated ' + data.daysSinceActivation + 'd ago — NOT eligible for coupon';
-
 
743
                }
-
 
744
                var bg = eligible ? '#e9f6ec' : '#fdecee';
-
 
745
                var color = eligible ? '#1e6b2a' : '#a7101f';
-
 
746
                var border = eligible ? '#b7e0bf' : '#f1b4bb';
-
 
747
                var icon = eligible ? '✓' : '⚠';
-
 
748
                $('<div></div>')
-
 
749
                    .css({
-
 
750
                        display: 'inline-block',
-
 
751
                        maxWidth: '100%',
-
 
752
                        padding: '3px 8px',
-
 
753
                        marginTop: imeis.length > 1 ? '3px' : '0',
-
 
754
                        background: bg,
-
 
755
                        color: color,
-
 
756
                        border: '1px solid ' + border,
-
 
757
                        borderRadius: '4px',
-
 
758
                        fontSize: '11px',
-
 
759
                        fontWeight: 600,
-
 
760
                        whiteSpace: 'nowrap',
-
 
761
                        overflow: 'hidden',
-
 
762
                        textOverflow: 'ellipsis'
-
 
763
                    })
-
 
764
                    .attr('title', imei + ' — ' + (data.message || ''))
-
 
765
                    .text(icon + ' ' + short)
-
 
766
                    .appendTo($note);
-
 
767
            },
-
 
768
            error: function () {
-
 
769
                // Silently ignore — eligibility is a hint, not a blocker.
-
 
770
            }
-
 
771
        });
-
 
772
    });
-
 
773
});
-
 
774