Subversion Repositories SmartDukaan

Rev

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

Rev 36860 Rev 36930
Line 63... Line 63...
63
import org.springframework.web.bind.annotation.*;
63
import org.springframework.web.bind.annotation.*;
64
import org.springframework.web.multipart.MultipartFile;
64
import org.springframework.web.multipart.MultipartFile;
65
 
65
 
66
import javax.servlet.http.HttpServletRequest;
66
import javax.servlet.http.HttpServletRequest;
67
import java.io.IOException;
67
import java.io.IOException;
68
import java.time.DayOfWeek;
-
 
69
import java.time.LocalDate;
68
import java.time.LocalDate;
70
import java.time.LocalDateTime;
69
import java.time.LocalDateTime;
71
import java.time.YearMonth;
70
import java.time.YearMonth;
72
import java.time.format.DateTimeFormatter;
71
import java.time.format.DateTimeFormatter;
73
import java.time.temporal.TemporalAdjusters;
72
import java.time.temporal.TemporalAdjusters;
Line 2492... Line 2491...
2492
        int fofoId = uc.getUserId();
2491
        int fofoId = uc.getUserId();
2493
 
2492
 
2494
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
2493
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
2495
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
2494
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
2496
 
2495
 
2497
        // Calculate current week boundaries (Monday to Sunday)
-
 
2498
        LocalDate today = LocalDate.now();
2496
        LocalDate today = LocalDate.now();
2499
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
2497
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2500
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
2498
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
2501
        LocalDateTime startOfWeekDateTime = startOfWeek.atStartOfDay();
2499
        LocalDateTime startOfMonthDateTime = startOfMonth.atStartOfDay();
2502
        LocalDateTime endOfWeekDateTime = endOfWeek.atTime(23, 59, 59);
2500
        LocalDateTime endOfMonthDateTime = endOfMonth.atTime(23, 59, 59);
2503
 
2501
 
2504
        // Check if RBM rating already exists for this week
-
 
2505
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdAndRbmIdForCurrentMonth(fofoId, rbmL1, startOfWeekDateTime, endOfWeekDateTime);
2502
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdAndRbmIdForCurrentMonth(fofoId, rbmL1, startOfMonthDateTime, endOfMonthDateTime);
2506
        if (!existingRbmRatings.isEmpty()) {
2503
        if (!existingRbmRatings.isEmpty()) {
2507
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this week already exists.");
2504
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
2508
        }
2505
        }
2509
 
2506
 
2510
        // Check if Sales Person rating already exists for this week
-
 
2511
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdAndSalesL1IdForCurrentMonth(fofoId, salesL1Id, startOfWeekDateTime, endOfWeekDateTime);
2507
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdAndSalesL1IdForCurrentMonth(fofoId, salesL1Id, startOfMonthDateTime, endOfMonthDateTime);
2512
        if (!existingSalesRatings.isEmpty()) {
2508
        if (!existingSalesRatings.isEmpty()) {
2513
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this week already exists.");
2509
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("Rating for this month already exists.");
2514
        }
2510
        }
2515
 
2511
 
2516
        // Save RBM rating
2512
        // Save RBM rating
2517
        RbmRating rbmRating = new RbmRating();
2513
        RbmRating rbmRating = new RbmRating();
2518
        rbmRating.setComment(rbmSalesRatingRequest.getRbmComment());
2514
        rbmRating.setComment(rbmSalesRatingRequest.getRbmComment());
Line 2541... Line 2537...
2541
        int fofoId = uc.getUserId();
2537
        int fofoId = uc.getUserId();
2542
 
2538
 
2543
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
2539
        int rbmL1 = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_RBM, EscalationType.L1, fofoId);
2544
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
2540
        int salesL1Id = csService.getAuthUserId(ProfitMandiConstants.TICKET_CATEGORY_SALES, EscalationType.L1, fofoId);
2545
 
2541
 
2546
        // Calculate current week boundaries (Monday to Sunday)
-
 
2547
        LocalDate today = LocalDate.now();
2542
        LocalDate today = LocalDate.now();
2548
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
2543
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2549
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
2544
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
2550
        LocalDateTime startOfWeekDateTime = startOfWeek.atStartOfDay();
2545
        LocalDateTime startOfMonthDateTime = startOfMonth.atStartOfDay();
2551
        LocalDateTime endOfWeekDateTime = endOfWeek.atTime(23, 59, 59);
2546
        LocalDateTime endOfMonthDateTime = endOfMonth.atTime(23, 59, 59);
2552
 
2547
 
2553
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfWeekDateTime, endOfWeekDateTime);
2548
        List<RbmRating> existingRbmRatings = rbmRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfMonthDateTime, endOfMonthDateTime);
2554
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfWeekDateTime, endOfWeekDateTime);
2549
        List<SalesRating> existingSalesRatings = salesRatingRepository.findByFofoIdForCurrentWeek(fofoId, startOfMonthDateTime, endOfMonthDateTime);
2555
 
2550
 
2556
        boolean pending = existingRbmRatings.isEmpty() || existingSalesRatings.isEmpty();
2551
        boolean pending = existingRbmRatings.isEmpty() || existingSalesRatings.isEmpty();
2557
 
2552
 
2558
        Map<String, Object> response = new HashMap<>();
2553
        Map<String, Object> response = new HashMap<>();
2559
        response.put("pending", pending);
2554
        response.put("pending", pending);
2560
        response.put("weekStart", startOfWeek.toString());
2555
        response.put("weekStart", startOfMonth.toString());
2561
        response.put("weekEnd", endOfWeek.toString());
2556
        response.put("weekEnd", endOfMonth.toString());
2562
 
2557
 
2563
        if (rbmL1 > 0) {
2558
        if (rbmL1 > 0) {
2564
            AuthUser rbmUser = authRepository.selectById(rbmL1);
2559
            AuthUser rbmUser = authRepository.selectById(rbmL1);
2565
            if (rbmUser != null) {
2560
            if (rbmUser != null) {
2566
                response.put("rbmName", rbmUser.getFullName());
2561
                response.put("rbmName", rbmUser.getFullName());
Line 2584... Line 2579...
2584
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
2579
        int userId = (int) request.getAttribute(ProfitMandiConstants.USER_ID);
2585
        UserCart uc = userAccountRepository.getUserCart(userId);
2580
        UserCart uc = userAccountRepository.getUserCart(userId);
2586
        int fofoId = uc.getUserId();
2581
        int fofoId = uc.getUserId();
2587
 
2582
 
2588
        LocalDate today = LocalDate.now();
2583
        LocalDate today = LocalDate.now();
2589
        LocalDate startOfWeek = today.with(TemporalAdjusters.previousOrSame(DayOfWeek.MONDAY));
2584
        LocalDate startOfMonth = today.with(TemporalAdjusters.firstDayOfMonth());
2590
        LocalDate endOfWeek = today.with(TemporalAdjusters.nextOrSame(DayOfWeek.SUNDAY));
2585
        LocalDate endOfMonth = today.with(TemporalAdjusters.lastDayOfMonth());
2591
 
2586
 
2592
        RatingReminder reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfWeek);
2587
        RatingReminder reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfMonth);
2593
 
2588
 
2594
        int attemptsRemaining;
2589
        int attemptsRemaining;
2595
        if (reminder == null) {
2590
        if (reminder == null) {
2596
            // First hit this week: create entry initialised to MAX, no decrement
-
 
2597
            reminder = new RatingReminder();
2591
            reminder = new RatingReminder();
2598
            reminder.setFofoId(fofoId);
2592
            reminder.setFofoId(fofoId);
2599
            reminder.setWeekStart(startOfWeek);
2593
            reminder.setWeekStart(startOfMonth);
2600
            reminder.setWeekEnd(endOfWeek);
2594
            reminder.setWeekEnd(endOfMonth);
2601
            reminder.setAttemptsRemaining(RatingReminder.MAX_ATTEMPTS_PER_WEEK);
2595
            reminder.setAttemptsRemaining(RatingReminder.MAX_ATTEMPTS_PER_WEEK);
2602
            reminder.setCreateTimestamp(LocalDateTime.now());
2596
            reminder.setCreateTimestamp(LocalDateTime.now());
2603
            ratingReminderRepository.persist(reminder);
2597
            ratingReminderRepository.persist(reminder);
2604
            attemptsRemaining = RatingReminder.MAX_ATTEMPTS_PER_WEEK;
2598
            attemptsRemaining = RatingReminder.MAX_ATTEMPTS_PER_WEEK;
2605
        } else {
2599
        } else {
2606
            // Subsequent hit: atomically decrement (floor 0) and re-read
-
 
2607
            ratingReminderRepository.decrementRemaining(reminder.getId());
2600
            ratingReminderRepository.decrementRemaining(reminder.getId());
2608
            reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfWeek);
2601
            reminder = ratingReminderRepository.findByFofoIdAndWeek(fofoId, startOfMonth);
2609
            attemptsRemaining = reminder.getAttemptsRemaining();
2602
            attemptsRemaining = reminder.getAttemptsRemaining();
2610
        }
2603
        }
2611
 
2604
 
2612
        Map<String, Object> response = new HashMap<>();
2605
        Map<String, Object> response = new HashMap<>();
2613
        response.put("partnerId", fofoId);
2606
        response.put("partnerId", fofoId);