def validate_credits(credit_trades):
        errors = []
        temp_storage = []

        for credit_trade in credit_trades:
            from_starting_index, from_starting_balance = CreditTradeService. \
                get_temp_balance(temp_storage, credit_trade.credits_from.id)

            to_starting_index, to_starting_balance = CreditTradeService. \
                get_temp_balance(temp_storage, credit_trade.credits_to.id)

            from_credits_remaining = from_starting_balance - \
                credit_trade.number_of_credits

            to_credits_remaining = to_starting_balance + \
                credit_trade.number_of_credits

            CreditTradeService.update_temp_balance(
                temp_storage, from_starting_index, from_credits_remaining,
                credit_trade.credits_from.id)

            CreditTradeService.update_temp_balance(temp_storage,
                                                   to_starting_index,
                                                   to_credits_remaining,
                                                   credit_trade.credits_to.id)

            if from_credits_remaining < 0:
                errors.append("[ID: {}] "
                              "Can't complete transaction,"
                              "`{}` has insufficient credits.".format(
                                  credit_trade.id,
                                  credit_trade.credits_from.name))

        if len(errors) > 0:
            raise PositiveIntegerException(errors)
Exemple #2
0
    def transfer_credits(_from, _to, credit_trade_id, num_of_credits,
                         effective_date):
        """
        Make the appropriate addition and reduction to the credits for the
        organizations Involved
        """
        from_starting_bal, _ = OrganizationBalance.objects.get_or_create(
            organization_id=_from.id,
            expiration_date=None,
            defaults={'validated_credits': 0})

        to_starting_bal, _ = OrganizationBalance.objects.get_or_create(
            organization_id=_to.id,
            expiration_date=None,
            defaults={'validated_credits': 0})

        # Compute for end balance
        from_credits = from_starting_bal.validated_credits - num_of_credits
        to_credits = to_starting_bal.validated_credits + num_of_credits

        if from_credits < 0:
            raise PositiveIntegerException("Can't complete transaction,"
                                           "`{}` has insufficient credits"
                                           .format(_from.name))

        # Update old balance effective date
        from_starting_bal.expiration_date = effective_date
        to_starting_bal.expiration_date = effective_date

        # Create new fuel supplier balance
        from_new_bal = OrganizationBalance(
            organization=_from,
            validated_credits=from_credits,
            effective_date=effective_date,
            credit_trade_id=credit_trade_id
        )

        to_new_bal = OrganizationBalance(
            organization=_to,
            validated_credits=to_credits,
            effective_date=effective_date,
            credit_trade_id=credit_trade_id
        )

        # Save everything
        from_starting_bal.save()
        to_starting_bal.save()

        from_new_bal.save()
        to_new_bal.save()
Exemple #3
0
    def transfer_credits(_from, _to, credit_trade_id, num_of_credits,
                         effective_date):
        from_starting_bal = OrganizationBalance.objects.get(
            organization_id=_from.id, expiration_date=None)

        to_starting_bal = OrganizationBalance.objects.get(
            organization_id=_to.id, expiration_date=None)

        # Compute for end balance
        from_credits = from_starting_bal.validated_credits - num_of_credits
        to_credits = to_starting_bal.validated_credits + num_of_credits

        if 0 > from_credits:
            raise PositiveIntegerException("Can't complete transaction,"
                                           "insufficient credits")

        # Update old balance effective date
        from_starting_bal.expiration_date = effective_date
        to_starting_bal.expiration_date = effective_date

        # Create new fuel supplier balance
        from_new_bal = OrganizationBalance(organization=_from,
                                           validated_credits=from_credits,
                                           effective_date=effective_date,
                                           credit_trade_id=credit_trade_id)

        to_new_bal = OrganizationBalance(organization=_to,
                                         validated_credits=to_credits,
                                         effective_date=effective_date,
                                         credit_trade_id=credit_trade_id)

        # Save everything
        from_starting_bal.save()
        to_starting_bal.save()

        from_new_bal.save()
        to_new_bal.save()