Subversion Repositories SmartDukaan

Rev

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

Rev 37497 Rev 37517
Line 2422... Line 2422...
2422
      if (p) {
2422
      if (p) {
2423
        startDate = p.startDate.format("YYYY-MM-DD");
2423
        startDate = p.startDate.format("YYYY-MM-DD");
2424
        endDate = p.endDate.format("YYYY-MM-DD");
2424
        endDate = p.endDate.format("YYYY-MM-DD");
2425
      }
2425
      }
2426
      load();
2426
      load();
2427
      // If a Sales filter is currently active, re-fetch its data for the new range.
2427
      // If a Sales role is currently active, re-fetch sales-data for the new range.
2428
      if (CURRENT_SALES_AUTH_ID) {
2428
      if (CURRENT_SALES_ROLE) {
2429
        $.ajax({
2429
        $.ajax({
2430
          url: "/rbm-drr/sales-data",
2430
          url: "/rbm-drr/sales-data",
2431
          type: "GET",
2431
          type: "GET",
2432
          data: {startDate: startDate, endDate: endDate},
2432
          data: {startDate: startDate, endDate: endDate},
2433
          dataType: "json",
2433
          dataType: "json",
Line 2606... Line 2606...
2606
    }
2606
    }
2607
 
2607
 
2608
    // Cached latest payload so the "Target Status by RBM" modal can render
2608
    // Cached latest payload so the "Target Status by RBM" modal can render
2609
    // without a re-fetch when the KPI card is clicked.
2609
    // without a re-fetch when the KPI card is clicked.
2610
    var LAST_PAYLOAD = null;
2610
    var LAST_PAYLOAD = null;
2611
    var SALES_PAYLOAD = null;              // fetched on-demand from /rbm-drr/sales-data
2611
    var SALES_PAYLOAD = null;              // fetched from /rbm-drr/sales-data when a Sales role is selected
-
 
2612
    var CURRENT_SALES_ROLE = null;         // "ASM" / "CM" / … — when set with SALES_PAYLOAD, we render Sales view
2612
    var CURRENT_SALES_AUTH_ID = null;      // when set, renderFiltered() uses SALES_PAYLOAD
2613
    var CURRENT_SALES_AUTH_ID = null;      // when set, further narrows Sales view to this person
2613
    // Set of RBM authIds allowed by the current filter. null = no filter (show all).
2614
    // Set of RBM authIds allowed by the current filter. null = no filter (show all).
2614
    var CURRENT_FILTER_AUTH_IDS = null;
2615
    var CURRENT_FILTER_AUTH_IDS = null;
2615
 
2616
 
2616
    function render(data) {
2617
    function render(data) {
2617
      LAST_PAYLOAD = data;
2618
      LAST_PAYLOAD = data;
2618
      renderFiltered();
2619
      renderFiltered();
2619
    }
2620
    }
2620
 
2621
 
-
 
2622
    // Maps a sales-data row into the same shape the RBM grid renderer expects.
-
 
2623
    function normaliseSalesRow(r) {
-
 
2624
      return {
-
 
2625
        authId: r.salesAuthId, warehouseId: r.warehouseId,
-
 
2626
        rbmName: r.salesName + (r.role ? " (" + r.role + ")" : ""),
-
 
2627
        warehouseName: r.warehouseName,
-
 
2628
        totalAch: r.totalAch, totalTgt: r.totalTgt,
-
 
2629
        hidAch: r.hidAch, hidTgt: r.hidTgt,
-
 
2630
        fastAch: r.fastAch, fastTgt: r.fastTgt,
-
 
2631
        runAch: r.runAch, runTgt: r.runTgt,
-
 
2632
        slowAch: r.slowAch, slowTgt: r.slowTgt,
-
 
2633
        otherAch: r.otherAch, otherTgt: r.otherTgt
-
 
2634
      };
-
 
2635
    }
-
 
2636
 
2621
    // Renders KPIs + grid from the appropriate payload:
2637
    // Renders KPIs + grid from the appropriate payload:
2622
    //  - Sales filter active → SALES_PAYLOAD filtered to selected sales person (rows are sales × warehouse)
2638
    //  - Sales role active + payload loaded → filter SALES_PAYLOAD to that role (and optionally one person)
2623
    //  - RBM filter or no filter → LAST_PAYLOAD filtered by CURRENT_FILTER_AUTH_IDS (rows are RBM × warehouse)
2639
    //  - RBM filter or no filter → LAST_PAYLOAD filtered by CURRENT_FILTER_AUTH_IDS
2624
    function renderFiltered() {
2640
    function renderFiltered() {
2625
      if (CURRENT_SALES_AUTH_ID && SALES_PAYLOAD) {
2641
      if (CURRENT_SALES_ROLE && SALES_PAYLOAD) {
2626
        var salesRows = (SALES_PAYLOAD.rows || []).filter(function (r) {
2642
        var salesRows = (SALES_PAYLOAD.rows || []).filter(function (r) {
2627
          return String(r.salesAuthId) === String(CURRENT_SALES_AUTH_ID);
2643
          if (r.role !== CURRENT_SALES_ROLE) return false;
2628
        }).map(function (r) {
-
 
2629
          // Normalise sales row into the same shape the RBM grid renderer expects.
2644
          if (CURRENT_SALES_AUTH_ID && String(r.salesAuthId) !== String(CURRENT_SALES_AUTH_ID)) return false;
2630
          return {
2645
          return true;
2631
            authId: r.salesAuthId, warehouseId: r.warehouseId,
-
 
2632
            rbmName: r.salesName + (r.role ? " (" + r.role + ")" : ""),
-
 
2633
            warehouseName: r.warehouseName,
2646
        }).map(normaliseSalesRow);
2634
            totalAch: r.totalAch, totalTgt: r.totalTgt,
-
 
2635
            hidAch: r.hidAch, hidTgt: r.hidTgt,
-
 
2636
            fastAch: r.fastAch, fastTgt: r.fastTgt,
-
 
2637
            runAch: r.runAch, runTgt: r.runTgt,
-
 
2638
            slowAch: r.slowAch, slowTgt: r.slowTgt,
-
 
2639
            otherAch: r.otherAch, otherTgt: r.otherTgt
-
 
2640
          };
-
 
2641
        });
-
 
2642
        renderRowsAsGrid(salesRows, true);
2647
        renderRowsAsGrid(salesRows, true);
2643
        return;
2648
        return;
2644
      }
2649
      }
2645
      if (!LAST_PAYLOAD) return;
2650
      if (!LAST_PAYLOAD) return;
2646
      var allRows = LAST_PAYLOAD.rows || [];
2651
      var allRows = LAST_PAYLOAD.rows || [];
Line 3301... Line 3306...
3301
        salesHtml += '<option value="' + s.authId + '">' + escapeHtml(s.name || "Unknown") + '</option>';
3306
        salesHtml += '<option value="' + s.authId + '">' + escapeHtml(s.name || "Unknown") + '</option>';
3302
      });
3307
      });
3303
      personSel.html(salesHtml).prop("disabled", false);
3308
      personSel.html(salesHtml).prop("disabled", false);
3304
    }
3309
    }
3305
 
3310
 
-
 
3311
    // Fetches (or refetches) /rbm-drr/sales-data for the current date range,
-
 
3312
    // then renders. Called on Level change and on Apply while Sales view active.
-
 
3313
    function fetchAndRenderSales() {
-
 
3314
      $("#rdrrTableWrap").html('<div class="loading"><div class="spinner"></div>Loading&hellip;</div>');
-
 
3315
      $.ajax({
-
 
3316
        url: "/rbm-drr/sales-data", type: "GET",
-
 
3317
        data: {startDate: startDate, endDate: endDate}, dataType: "json",
-
 
3318
        success: function (payload) {
-
 
3319
          SALES_PAYLOAD = payload;
-
 
3320
          renderFiltered();
-
 
3321
        },
-
 
3322
        error: function () {
-
 
3323
          SALES_PAYLOAD = {rows: []};
-
 
3324
          renderFiltered();
-
 
3325
        }
-
 
3326
      });
-
 
3327
    }
-
 
3328
 
3306
    // Type toggles: show/hide Level; reset filter state; refresh Person options.
3329
    // Type toggles: show/hide Level; reset filter state; refresh Person options.
3307
    $("#rdrrTypeSelect").on("change", function () {
3330
    $("#rdrrTypeSelect").on("change", function () {
3308
      var type = $(this).val();
3331
      var type = $(this).val();
3309
      if (type === "SALES") {
3332
      if (type === "SALES") {
3310
        $("#rdrrLevelSelect").css("display", "").val("");
3333
        $("#rdrrLevelSelect").css("display", "").val("");
3311
      } else {
3334
      } else {
3312
        $("#rdrrLevelSelect").css("display", "none").val("");
3335
        $("#rdrrLevelSelect").css("display", "none").val("");
3313
      }
3336
      }
3314
      // Clear all filter state whenever the type changes.
3337
      // Clear all filter state whenever the type changes.
3315
      CURRENT_FILTER_AUTH_IDS = null;
3338
      CURRENT_FILTER_AUTH_IDS = null;
-
 
3339
      CURRENT_SALES_ROLE = null;
3316
      CURRENT_SALES_AUTH_ID = null;
3340
      CURRENT_SALES_AUTH_ID = null;
3317
      SALES_PAYLOAD = null;
3341
      SALES_PAYLOAD = null;
3318
      repopulatePersonForCurrentType();
3342
      repopulatePersonForCurrentType();
3319
      renderFiltered();
3343
      renderFiltered();
3320
    });
3344
    });
3321
 
3345
 
3322
    $("#rdrrLevelSelect").on("change", function () {
3346
    $("#rdrrLevelSelect").on("change", function () {
-
 
3347
      var level = $(this).val();
3323
      // New level → clear Sales filter state and repopulate Person.
3348
      // Reset the person-specific filter; role determines the view.
3324
      CURRENT_FILTER_AUTH_IDS = null;
3349
      CURRENT_FILTER_AUTH_IDS = null;
3325
      CURRENT_SALES_AUTH_ID = null;
3350
      CURRENT_SALES_AUTH_ID = null;
3326
      SALES_PAYLOAD = null;
3351
      CURRENT_SALES_ROLE = level || null;
3327
      repopulatePersonForCurrentType();
3352
      repopulatePersonForCurrentType();
-
 
3353
      if (level) {
-
 
3354
        // Fetch Sales-data once, then all subsequent role/person filtering is client-side.
-
 
3355
        fetchAndRenderSales();
-
 
3356
      } else {
-
 
3357
        SALES_PAYLOAD = null;
3328
      renderFiltered();
3358
        renderFiltered();
-
 
3359
      }
3329
    });
3360
    });
3330
 
3361
 
3331
    $("#rdrrPersonSelect").on("change", function () {
3362
    $("#rdrrPersonSelect").on("change", function () {
3332
      var authId = $(this).val();
3363
      var authId = $(this).val();
3333
      var type = $("#rdrrTypeSelect").val();
3364
      var type = $("#rdrrTypeSelect").val();
3334
 
3365
 
3335
      if (type === "RBM") {
3366
      if (type === "RBM") {
3336
        // RBM filter (single authId) — no server round trip needed.
3367
        // RBM filter (single authId) — no server round trip needed.
-
 
3368
        CURRENT_SALES_ROLE = null;
3337
        CURRENT_SALES_AUTH_ID = null;
3369
        CURRENT_SALES_AUTH_ID = null;
3338
        SALES_PAYLOAD = null;
3370
        SALES_PAYLOAD = null;
3339
        if (!authId) {
3371
        if (!authId) {
3340
          CURRENT_FILTER_AUTH_IDS = null;
3372
          CURRENT_FILTER_AUTH_IDS = null;
3341
        } else {
3373
        } else {
Line 3345... Line 3377...
3345
        }
3377
        }
3346
        renderFiltered();
3378
        renderFiltered();
3347
        return;
3379
        return;
3348
      }
3380
      }
3349
 
3381
 
-
 
3382
      // SALES type — role already active from Level dropdown; SALES_PAYLOAD already fetched.
3350
      // SALES type
3383
      // Person selection just further filters client-side (no re-fetch).
3351
      CURRENT_FILTER_AUTH_IDS = null;
3384
      CURRENT_FILTER_AUTH_IDS = null;
3352
      if (!authId) {
-
 
3353
        // "All <role>s" — no filter (show everyone in the DRR grid, RBM view).
-
 
3354
        CURRENT_SALES_AUTH_ID = null;
3385
      CURRENT_SALES_AUTH_ID = authId || null;
3355
        SALES_PAYLOAD = null;
3386
      if (SALES_PAYLOAD) {
3356
        renderFiltered();
3387
        renderFiltered();
-
 
3388
      } else if (CURRENT_SALES_ROLE) {
-
 
3389
        // Edge case: person changed before Level's fetch completed — trigger the fetch.
3357
        return;
3390
        fetchAndRenderSales();
3358
      }
3391
      }
3359
      // Pick a specific Sales person → fetch that person's aggregated data.
-
 
3360
      CURRENT_SALES_AUTH_ID = authId;
-
 
3361
      $("#rdrrTableWrap").html('<div class="loading"><div class="spinner"></div>Loading&hellip;</div>');
-
 
3362
      $.ajax({
-
 
3363
        url: "/rbm-drr/sales-data", type: "GET",
-
 
3364
        data: {startDate: startDate, endDate: endDate}, dataType: "json",
-
 
3365
        success: function (payload) {
-
 
3366
          SALES_PAYLOAD = payload;
-
 
3367
          renderFiltered();
-
 
3368
        },
-
 
3369
        error: function () {
-
 
3370
          SALES_PAYLOAD = {rows: []};
-
 
3371
          renderFiltered();
-
 
3372
        }
-
 
3373
      });
-
 
3374
    });
3392
    });
3375
 
3393
 
3376
    // initial load
3394
    // initial load
3377
    loadFilterOptions();
3395
    loadFilterOptions();
3378
    load();
3396
    load();