def test_returns_full_reimbursement_for_all_bookings_above_20000_if_rule_is_not_valid_anymore(
                self):
            # given
            now = datetime.utcnow()
            booking1 = create_booking_for_event(amount=50,
                                                quantity=1,
                                                date_created=now)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=50,
                                                quantity=3,
                                                date_created=now)
            booking3 = create_booking_for_thing(amount=1995,
                                                quantity=10,
                                                date_created=now)
            bookings = [booking1, booking2, booking3]
            ReimbursementRules.MAX_REIMBURSEMENT.value.valid_from = now - timedelta(
                weeks=5)
            ReimbursementRules.MAX_REIMBURSEMENT.value.valid_until = now + timedelta(
                weeks=5)

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, CURRENT_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)
            assert_total_reimbursement(booking_reimbursements[2], booking3)

            # tear down
            ReimbursementRules.MAX_REIMBURSEMENT.value.valid_from = None
            ReimbursementRules.MAX_REIMBURSEMENT.value.valid_until = None
        def test_returns_85_reimbursement_rate_between_40000_and_100000_when_cumulative_value_is_100000(
                self):
            # given
            booking1 = create_booking_for_event(amount=19000, quantity=1)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=50,
                                                quantity=3)
            booking3 = create_booking_for_thing(amount=19000, quantity=4)
            booking4 = create_booking_for_thing(amount=5000, quantity=1)
            bookings = [booking1, booking2, booking3, booking4]
            cumulative_value_for_bookings_1_and_3_and_4 = booking1.amount * booking1.quantity + \
                                                          booking3.amount * booking3.quantity + \
                                                          booking4.amount * booking4.quantity

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, NEW_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)
            assert_degressive_reimbursement(
                booking_reimbursements[2], booking3,
                cumulative_value_for_bookings_1_and_3_and_4)
            assert_degressive_reimbursement(
                booking_reimbursements[3], booking4,
                cumulative_value_for_bookings_1_and_3_and_4)
Example #3
0
def generate_new_payments() -> Tuple[List[Payment], List[Payment]]:
    offerers = Offerer.query.all()
    all_payments = []

    for offerer in offerers:
        if is_active(FeatureToggle.DEGRESSIVE_REIMBURSEMENT_RATE):
            booking_reimbursements = []
            for venue in offerer.managedVenues:
                final_bookings = find_eligible_bookings_for_venue(venue.id)
                booking_reimbursements += find_all_booking_reimbursements(
                    final_bookings, NEW_RULES)
        else:
            final_bookings = find_eligible_bookings_for_offerer(offerer.id)
            booking_reimbursements = find_all_booking_reimbursements(
                final_bookings, CURRENT_RULES)

        booking_reimbursements_to_pay = filter_out_already_paid_for_bookings(
            filter_out_bookings_without_cost(booking_reimbursements))

        with db.session.no_autoflush:
            payments = list(
                map(create_payment_for_booking, booking_reimbursements_to_pay))

        if payments:
            PcObject.save(*payments)
            all_payments.extend(payments)
        logger.info('[BATCH][PAYMENTS] Saved %s payments for offerer : %s' %
                    (len(payments), offerer.name))

    logger.info(
        '[BATCH][PAYMENTS] Generated %s payments for %s offerers in total' %
        (len(all_payments), len(offerers)))

    pending_payments = keep_only_pending_payments(all_payments)
    not_processable_payments = keep_only_not_processable_payments(all_payments)
    logger.info('[BATCH][PAYMENTS] %s Payments in status PENDING to send' %
                len(pending_payments))
    return pending_payments, not_processable_payments
        def test_returns_full_reimbursement_for_all_bookings(self):
            # given
            booking1 = create_booking_for_event(amount=50, quantity=1)
            booking2 = create_booking_for_thing(amount=40, quantity=3)
            booking3 = create_booking_for_event(amount=100, quantity=2)
            bookings = [booking1, booking2, booking3]

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, NEW_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_total_reimbursement(booking_reimbursements[1], booking2)
            assert_total_reimbursement(booking_reimbursements[2], booking3)
        def test_returns_a_different_reimbursement_for_digital_booking(self):
            # given
            booking1 = create_booking_for_event(amount=50, quantity=1)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=40,
                                                quantity=3)
            booking3 = create_booking_for_event(amount=100, quantity=2)
            bookings = [booking1, booking2, booking3]

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, NEW_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[2], booking3)
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)
        def test_returns_65_reimbursement_rate_above_100000_euros_for_last_booking(
                self):
            # given
            booking1 = create_booking_for_event(amount=19000, quantity=1)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=50,
                                                quantity=3)
            booking3 = create_booking_for_thing(amount=2000, quantity=120)
            bookings = [booking1, booking2, booking3]

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, NEW_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_degressive_reimbursement(booking_reimbursements[2],
                                            booking3, 430000)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)
        def test_returns_no_reimbursement_above_20000_euros_for_last_booking(
                self):
            # given
            booking1 = create_booking_for_event(amount=60, quantity=1)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=50,
                                                quantity=3)
            booking3 = create_booking_for_thing(amount=1995, quantity=10)
            bookings = [booking1, booking2, booking3]

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, CURRENT_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)
            assert_no_reimbursement_beyond_max(booking_reimbursements[2],
                                               booking3)
        def test_returns_full_reimbursement_when_cumulative_value_is_20000(
                self):
            # given
            booking1 = create_booking_for_event(amount=19990, quantity=1)
            booking2 = create_booking_for_thing(url='http://truc',
                                                amount=50,
                                                quantity=3)
            booking3 = create_booking_for_thing(amount=10, quantity=1)
            bookings = [booking1, booking2, booking3]
            cumulative_value_for_bookings_1_and_3 = booking1.amount * booking1.quantity + \
                                                    booking3.amount * booking3.quantity

            # when
            booking_reimbursements = find_all_booking_reimbursements(
                bookings, NEW_RULES)

            # then
            assert_total_reimbursement(booking_reimbursements[0], booking1)
            assert_total_reimbursement(booking_reimbursements[2], booking3)
            assert_no_reimbursement_for_digital(booking_reimbursements[1],
                                                booking2)