|
11 | 11 | from odoo.addons.job_worker.delay import group |
12 | 12 |
|
13 | 13 | from .. import constants |
| 14 | +from .pagination_utils import compute_id_ranges |
14 | 15 |
|
15 | 16 | _logger = logging.getLogger(__name__) |
16 | 17 |
|
@@ -515,21 +516,32 @@ def _check_eligibility_async(self, cycle, beneficiaries_count): |
515 | 516 | cycle.message_post(body=_("Eligibility check of %s beneficiaries started.", beneficiaries_count)) |
516 | 517 | cycle.write({"is_locked": True, "locked_reason": "Eligibility check of beneficiaries"}) |
517 | 518 |
|
| 519 | + states = ("draft", "enrolled", "not_eligible") |
| 520 | + id_ranges = compute_id_ranges( |
| 521 | + self.env.cr, |
| 522 | + "spp_cycle_membership", |
| 523 | + "cycle_id = %s AND state IN %s", |
| 524 | + (cycle.id, states), |
| 525 | + self.MAX_ROW_JOB_QUEUE, |
| 526 | + ) |
| 527 | + |
518 | 528 | jobs = [] |
519 | | - for i in range(0, beneficiaries_count, self.MAX_ROW_JOB_QUEUE): |
520 | | - jobs.append( |
521 | | - self.delayable(channel="cycle")._check_eligibility(cycle, offset=i, limit=self.MAX_ROW_JOB_QUEUE) |
522 | | - ) |
| 529 | + for min_id, max_id in id_ranges: |
| 530 | + jobs.append(self.delayable(channel="cycle")._check_eligibility(cycle, min_id=min_id, max_id=max_id)) |
523 | 531 | main_job = group(*jobs) |
524 | 532 | main_job.on_done(self.delayable(channel="cycle").mark_check_eligibility_as_done(cycle)) |
525 | 533 | main_job.delay() |
526 | 534 |
|
527 | | - def _check_eligibility(self, cycle, beneficiaries=None, offset=0, limit=None, do_count=False): |
| 535 | + def _check_eligibility( |
| 536 | + self, cycle, beneficiaries=None, offset=0, limit=None, min_id=None, max_id=None, do_count=False |
| 537 | + ): |
528 | 538 | if beneficiaries is None: |
529 | 539 | beneficiaries = cycle.get_beneficiaries( |
530 | 540 | ["draft", "enrolled", "not_eligible"], |
531 | 541 | offset=offset, |
532 | 542 | limit=limit, |
| 543 | + min_id=min_id, |
| 544 | + max_id=max_id, |
533 | 545 | order="id", |
534 | 546 | ) |
535 | 547 |
|
@@ -585,26 +597,38 @@ def _prepare_entitlements_async(self, cycle, beneficiaries_count): |
585 | 597 | } |
586 | 598 | ) |
587 | 599 |
|
| 600 | + id_ranges = compute_id_ranges( |
| 601 | + self.env.cr, |
| 602 | + "spp_cycle_membership", |
| 603 | + "cycle_id = %s AND state IN %s", |
| 604 | + (cycle.id, ("enrolled",)), |
| 605 | + self.MAX_ROW_JOB_QUEUE, |
| 606 | + ) |
| 607 | + |
588 | 608 | jobs = [] |
589 | | - for i in range(0, beneficiaries_count, self.MAX_ROW_JOB_QUEUE): |
590 | | - jobs.append(self.delayable(channel="cycle")._prepare_entitlements(cycle, i, self.MAX_ROW_JOB_QUEUE)) |
| 609 | + for min_id, max_id in id_ranges: |
| 610 | + jobs.append(self.delayable(channel="cycle")._prepare_entitlements(cycle, min_id=min_id, max_id=max_id)) |
591 | 611 | main_job = group(*jobs) |
592 | 612 | main_job.on_done( |
593 | 613 | self.delayable(channel="cycle").mark_prepare_entitlement_as_done(cycle, _("Entitlement Ready.")) |
594 | 614 | ) |
595 | 615 | main_job.delay() |
596 | 616 |
|
597 | | - def _prepare_entitlements(self, cycle, offset=0, limit=None, do_count=False): |
| 617 | + def _prepare_entitlements(self, cycle, offset=0, limit=None, min_id=None, max_id=None, do_count=False): |
598 | 618 | """Prepare Entitlements |
599 | 619 | Get the beneficiaries and generate their entitlements. |
600 | 620 |
|
601 | 621 | :param cycle: The cycle |
602 | | - :param offset: Optional integer value for the ORM search offset |
603 | | - :param limit: Optional integer value for the ORM search limit |
| 622 | + :param offset: Optional integer value for the ORM search offset (deprecated, use min_id/max_id) |
| 623 | + :param limit: Optional integer value for the ORM search limit (deprecated, use min_id/max_id) |
| 624 | + :param min_id: Minimum record ID for ID-range pagination (inclusive) |
| 625 | + :param max_id: Maximum record ID for ID-range pagination (inclusive) |
604 | 626 | :param do_count: Boolean - set to False to not run compute function |
605 | 627 | :return: |
606 | 628 | """ |
607 | | - beneficiaries = cycle.get_beneficiaries(["enrolled"], offset=offset, limit=limit, order="id") |
| 629 | + beneficiaries = cycle.get_beneficiaries( |
| 630 | + ["enrolled"], offset=offset, limit=limit, min_id=min_id, max_id=max_id, order="id" |
| 631 | + ) |
608 | 632 | ent_manager = self.program_id.get_manager(constants.MANAGER_ENTITLEMENT) |
609 | 633 | if not ent_manager: |
610 | 634 | raise UserError(_("No Entitlement Manager defined.")) |
|
0 commit comments