Exemple #1
0
def test_create_payment_for_booking_with_common_information(app):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    booking = create_booking(user=user, quantity=1, stock=stock)
    booking.stock.offer = Offer()
    booking.stock.offer.venue = VenueSQLEntity()
    offerer = create_offerer()
    create_bank_information(bic="QSDFGH8Z555",
                            iban="CF13QSDFGH456789",
                            offerer=offerer)
    booking.stock.offer.venue.managingOfferer = offerer
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert payment.booking == booking
    assert payment.amount == Decimal(10)
    assert payment.reimbursementRule == ReimbursementRules.PHYSICAL_OFFERS.value.description
    assert payment.reimbursementRate == ReimbursementRules.PHYSICAL_OFFERS.value.rate
    assert payment.comment is None
    assert payment.author == "batch"
    assert payment.transactionLabel == "pass Culture Pro - remboursement 2nde quinzaine 10-2018"
Exemple #2
0
def test_create_payment_for_booking_when_iban_is_on_venue_should_take_payment_info_from_venue(
        app):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    offerer = create_offerer(name="Test Offerer")
    venue = create_venue(
        offerer,
        name="Test Venue",
    )
    booking = create_booking(user=user, quantity=1, stock=stock)

    create_bank_information(bic="Lajr93",
                            iban="B135TGGEG532TG",
                            offerer=offerer)
    create_bank_information(bic="LokiJU76",
                            iban="KD98765RFGHZ788",
                            venue=venue)

    booking.stock.offer = Offer()
    booking.stock.offer.venue = venue
    booking.stock.offer.venue.managingOfferer = offerer
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert payment.iban == "KD98765RFGHZ788"
    assert payment.bic == "LOKIJU76"
Exemple #3
0
def test_create_payment_for_booking_when_no_iban_on_venue_should_take_payment_info_from_offerer(
        app):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    offerer = create_offerer(name="Test Offerer")
    venue = create_venue(offerer, name="Test Venue")

    create_bank_information(bic="QsdFGH8Z555",
                            iban="cf13QSDFGH456789",
                            offerer=offerer)
    create_bank_information(bic=None, iban=None, venue=venue)

    booking = create_booking(user=user, quantity=1, stock=stock)
    booking.stock.offer = Offer()
    booking.stock.offer.venue = venue
    booking.stock.offer.venue.managingOfferer = offerer
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert payment.iban == "CF13QSDFGH456789"
    assert payment.bic == "QSDFGH8Z555"
Exemple #4
0
def test_create_payment_for_booking_takes_recipient_name_and_siren_from_offerer(
        app):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    booking = create_booking(user=user, quantity=1, stock=stock)
    booking.stock.offer = Offer()
    offerer = create_offerer(name="Test Offerer", siren="123456789")
    venue = create_venue(offerer, name="Test Venue")

    create_bank_information(bic="QSDFGH8Z555",
                            iban="CF13QSDFGH456789",
                            offerer=offerer)
    create_bank_information(bic=None, iban=None, venue=venue)

    booking.stock.offer.venue = venue
    booking.stock.offer.venue.managingOfferer = offerer
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert payment.recipientName == "Test Offerer"
    assert payment.recipientSiren == "123456789"
Exemple #5
0
    def test_should_not_update_booking_if_a_payment_has_been_made(self, app):
        # Given
        offerer = create_offerer()
        user = create_user()
        venue = create_venue(offerer)
        offer = create_offer_with_event_product(venue=venue)
        stock = create_stock(beginning_datetime=datetime(
            2019, 3, 12, 00, 00, 00),
                             offer=offer,
                             price=0)
        date_used = datetime(2019, 3, 12, 00, 00, 00)
        booking = create_booking(user=user,
                                 stock=stock,
                                 token="QSDFG",
                                 is_used=True,
                                 date_used=date_used)
        booking_reimbursement = BookingReimbursement(
            booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))
        payment = create_payment_for_booking(booking_reimbursement)
        repository.save(booking, payment)

        # When
        cancel_booking_status_for_events_happening_during_quarantine()

        # Then
        bookings = Booking.query.all()

        assert bookings[0].isUsed is True
        assert bookings[0].dateUsed == date_used
Exemple #6
0
    def test_basics(self):
        offerer = offers_factories.OffererFactory(name="offerer",
                                                  siren="123456")
        booking = bookings_factories.IndividualBookingFactory(
            stock__offer__venue__managingOfferer=offerer)
        reimbursement = BookingReimbursement(booking,
                                             PhysicalOffersReimbursement(),
                                             Decimal(10))
        batch_date = datetime.utcnow()

        payment = create_payment_for_booking(reimbursement, batch_date)

        assert payment.bookingId == booking.id
        assert payment.amount == 10
        assert payment.reimbursementRule == PhysicalOffersReimbursement(
        ).description
        assert payment.reimbursementRate == PhysicalOffersReimbursement().rate
        assert payment.comment is None
        assert payment.author == "batch"
        assert payment.transactionLabel == "pass Culture Pro - remboursement 1ère quinzaine 01-2021"
        assert payment.batchDate == batch_date
        assert payment.iban is None
        assert payment.bic is None
        assert payment.recipientName == "offerer"
        assert payment.recipientSiren == "123456"
Exemple #7
0
    def test_with_custom_reimbursement_rule(self):
        booking = bookings_factories.IndividualBookingFactory()
        rule = payments_factories.CustomReimbursementRuleFactory(
            offer=booking.stock.offer, amount=2)
        reimbursement = BookingReimbursement(booking, rule, Decimal(2))
        batch_date = datetime.utcnow()

        payment = create_payment_for_booking(reimbursement, batch_date)
        assert payment.amount == 2
        assert payment.customReimbursementRuleId == rule.id
        assert payment.reimbursementRate is None
        assert payment.reimbursementRule is None
Exemple #8
0
    def test_use_iban_and_bic_from_offerer(self):
        booking = bookings_factories.IndividualBookingFactory()
        offers_factories.BankInformationFactory(offerer=booking.offerer,
                                                iban="iban",
                                                bic="bic")
        reimbursement = BookingReimbursement(booking,
                                             PhysicalOffersReimbursement(),
                                             Decimal(10))
        batch_date = datetime.utcnow()

        payment = create_payment_for_booking(reimbursement, batch_date)

        assert payment.iban == "IBAN"
        assert payment.bic == "BIC"
Exemple #9
0
def test_create_payment_for_booking_with_not_processable_status_when_no_bank_information_linked_to_venue_or_offerer(
):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    booking = create_booking(user=user, quantity=1, stock=stock)
    booking.stock.offer = Offer()
    booking.stock.offer.venue = VenueSQLEntity()
    booking.stock.offer.venue.managingOfferer = create_offerer(
        name="Test Offerer")
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert len(payment.statuses) == 1
    assert payment.statuses[0].status == TransactionStatus.NOT_PROCESSABLE
    assert payment.statuses[0].detail == "IBAN et BIC manquants sur l'offreur"
Exemple #10
0
def test_create_payment_for_booking_with_pending_status(app):
    # given
    user = create_user()
    stock = create_stock(price=10, quantity=5)
    booking = create_booking(user=user, quantity=1, stock=stock)
    booking.stock.offer = Offer()
    booking.stock.offer.venue = VenueSQLEntity()
    offerer = create_offerer()
    booking.stock.offer.venue.managingOfferer = offerer
    create_bank_information(bic="QSDFGH8Z555",
                            iban="CF13QSDFGH456789",
                            offerer=offerer)
    booking_reimbursement = BookingReimbursement(
        booking, ReimbursementRules.PHYSICAL_OFFERS, Decimal(10))

    # when
    payment = create_payment_for_booking(booking_reimbursement)

    # then
    assert len(payment.statuses) == 1
    assert payment.statuses[0].status == TransactionStatus.PENDING
    assert payment.statuses[0].detail is None
    assert payment.statuses[0].date == datetime(2018, 10, 15, 9, 21, 34)
def generate_new_payments(cutoff_date: datetime, batch_date: datetime) -> None:
    n_payments = 0
    logger.info("Fetching venues to reimburse")
    venues_to_reimburse = get_venues_to_reimburse(cutoff_date)
    logger.info("Found %d venues to reimburse", len(venues_to_reimburse))
    custom_rule_finder = CustomRuleFinder()
    n_payments = 0
    for venue_id, venue_name in venues_to_reimburse:
        logger.info("[BATCH][PAYMENTS] Fetching bookings for venue: %s",
                    venue_name,
                    extra={"venue": venue_id})
        bookings = booking_repository.find_bookings_eligible_for_payment_for_venue(
            venue_id, cutoff_date)
        logger.info(
            "[BATCH][PAYMENTS] Calculating reimbursements for venue: %s",
            venue_name,
            extra={"venue": venue_id})
        reimbursements = find_all_booking_reimbursements(
            bookings, custom_rule_finder)
        to_pay = filter_out_already_paid_for_bookings(
            filter_out_bookings_without_cost(reimbursements))
        if not to_pay:
            logger.info(
                "[BATCH][PAYMENTS] No payments generated for venue: %s",
                venue_name,
                extra={"venue": venue_id})
            continue
        n_payments += len(to_pay)

        logger.info(
            "[BATCH][PAYMENTS] Creating Payment objects for venue: %s",
            venue_name,
            extra={
                "venue": venue_id,
                "payments": len(to_pay)
            },
        )
        payments = [
            create_payment_for_booking(reimbursement, batch_date)
            for reimbursement in to_pay
        ]
        logger.info(
            "[BATCH][PAYMENTS] Inserting Payment objects for venue: %s",
            venue_name,
            extra={
                "venue": venue_id,
                "payments": len(to_pay)
            },
        )
        db.session.bulk_save_objects(payments)
        db.session.commit()

        logger.info(
            "[BATCH][PAYMENTS] Saved %i payments for venue: %s",
            len(payments),
            venue_name,
            extra={
                "venue": venue_id,
                "payments": len(payments)
            },
        )

    # Create all payment statuses. We get payments created above by
    # looking at their batch date.
    if n_payments:
        base_payment_query = Payment.query.filter_by(batchDate=batch_date)
        payments_api.bulk_create_payment_statuses(
            base_payment_query.filter(Payment.iban.isnot(None)),
            status=TransactionStatus.PENDING,
        )
        payments_api.bulk_create_payment_statuses(
            base_payment_query.filter(Payment.iban.is_(None)),
            status=TransactionStatus.NOT_PROCESSABLE,
            detail="IBAN et BIC manquants sur l'offreur",
        )
        db.session.commit()

    logger.info(
        "[BATCH][PAYMENTS] Generated %i payments for %i venues",
        n_payments,
        len(venues_to_reimburse),
    )