Ejemplo n.º 1
0
 def test_cancel_card_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(wallet=wallet_obj,
                             amount=Money(1000, 'SEK'),
                             trx_status=TrxStatus.FINALIZED)
     product_obj1 = ProductFactory.create(code='1337733113370',
                                          name='Billys Original',
                                          price=Money(13, 'SEK'))
     product_obj2 = ProductFactory.create(code='7331733113370',
                                          name='Kebaba',
                                          price=Money(30, 'SEK'))
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     purchase_obj = api.purchase(account_obj.id, products)
     api.cancel_purchase(purchase_obj.id)
     purchase_obj, _ = api.get_purchase(purchase_obj.id)
     self.assertEqual(purchase_obj.status, enums.PurchaseStatus.CANCELED)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, 0)
     self.assertEqual(product_obj2.qty, 0)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(1000, 'SEK'))
     _, balance = wallet_api.get_balance(settings.FOOBAR_MAIN_WALLET)
     self.assertEqual(balance, Money(0, 'SEK'))
Ejemplo n.º 2
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         code='7331733113370',
         name='Kebaba',
         price=Money(30, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     purchase_obj = api.purchase(account_obj.id, products)
     self.assertEqual(purchase_obj.amount, Money(69, 'SEK'))
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, -3)
     self.assertEqual(product_obj2.qty, -1)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(931, 'SEK'))
     _, balance = wallet_api.get_balance(settings.FOOBAR_MAIN_WALLET)
     self.assertEqual(balance, Money(69, 'SEK'))
Ejemplo n.º 3
0
 def test_calculation_correctíon(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     user_obj = User.objects.create_superuser(
         'the_baconator', '*****@*****.**', '123'
     )
     # Test positive balance change
     correction_obj = api.calculate_correction(
         new_balance=Money(1200, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 200)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1200)
     # Test negative balance change
     correction_obj = api.calculate_correction(
         new_balance=Money(1000, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1200)
     self.assertEqual(correction_obj.amount.amount, -200)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1000)
     # Test when balance is the same = no change
     correction_obj = api.calculate_correction(
         new_balance=Money(1000, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type.value, 0)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 0)
     _, balance_correction = wallet_api.get_balance(
         correction_obj.wallet.owner_id
     )
     self.assertEqual(balance_correction.amount, 1000)
Ejemplo n.º 4
0
def each_context(request):
    cw_obj, cash_balance = wallet_api.get_balance(settings.FOOBAR_CASH_WALLET)
    mw_obj, main_balance = wallet_api.get_balance(settings.FOOBAR_MAIN_WALLET)
    total_balance = wallet_api.total_balance(
        exclude_ids=[settings.FOOBAR_CASH_WALLET, settings.FOOBAR_MAIN_WALLET])

    ctx = _each_context(request)
    ctx.update({
        'cash_wallet': cw_obj,
        'cash_balance': cash_balance,
        'main_wallet': mw_obj,
        'main_balance': main_balance,
        'credit_account_balance': total_balance,
        'swish_number': settings.SWISH_NUMBER
    })
    return ctx
Ejemplo n.º 5
0
 def test_purchase(self):
     account_obj = AccountFactory.create()
     wallet_obj = WalletFactory.create(owner_id=account_obj.id)
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_status=TrxStatus.FINALIZED
     )
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         code='7331733113370',
         name='Kebaba',
         price=Money(30, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     api.purchase(account_obj.id, products)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, -3)
     self.assertEqual(product_obj2.qty, -1)
     _, balance = wallet_api.get_balance(account_obj.id)
     self.assertEqual(balance, Money(931, 'SEK'))
Ejemplo n.º 6
0
 def test_cancel_cash_purchase(self):
     product_obj1 = ProductFactory.create(
         code='1337733113370',
         name='Billys Original',
         price=Money(13, 'SEK')
     )
     product_obj2 = ProductFactory.create(
         code='7331733113370',
         name='Kebaba',
         price=Money(30, 'SEK')
     )
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     purchase_obj = api.purchase(None, products)
     api.cancel_purchase(purchase_obj.id)
     purchase_obj, _ = api.get_purchase(purchase_obj.id)
     self.assertEqual(purchase_obj.status, enums.PurchaseStatus.CANCELED)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, 0)
     self.assertEqual(product_obj2.qty, 0)
     _, balance = wallet_api.get_balance(settings.FOOBAR_CASH_WALLET)
     self.assertEqual(balance, Money(0, 'SEK'))
Ejemplo n.º 7
0
def make_deposit_or_withdrawal(amount, owner_id, user, reference=None):
    wallet, old_balance = wallet_api.get_balance(owner_id=owner_id, )

    correction_obj = WalletLogEntry.objects.create(
        user=user,
        trx_type=enums.TrxType.CORRECTION,
        wallet=wallet,
        comment=reference,
        amount=amount,
        pre_balance=old_balance)

    if amount.amount < 0:
        wallet_api.withdraw(owner_id=owner_id,
                            amount=-amount,
                            reference=correction_obj.id)
        correction_obj.trx_type = enums.TrxType.WITHDRAWAL
        correction_obj.save()
    elif amount.amount > 0:
        wallet_api.deposit(owner_id=owner_id,
                           amount=amount,
                           reference=correction_obj.id)
        correction_obj.trx_type = enums.TrxType.DEPOSIT
        correction_obj.save()

    return correction_obj
Ejemplo n.º 8
0
 def test_make_deposit_or_withdrawal(self):
     wallet_obj = WalletFactory.create()
     WalletTrxFactory.create(
         wallet=wallet_obj,
         amount=Money(1000, 'SEK'),
         trx_type=TrxType.FINALIZED
     )
     user_obj = User.objects.create_superuser(
         'the_baconator', '*****@*****.**', '123'
     )
     # Test a deposit
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(100, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
      )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.DEPOSIT)
     self.assertEqual(correction_obj.pre_balance.amount, 1000)
     self.assertEqual(correction_obj.amount.amount, 100)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1100)
     # Test a withdraw
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(-50, 'SEK'),
         user=user_obj, owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.WITHDRAWAL)
     self.assertEqual(correction_obj.pre_balance.amount, 1100)
     self.assertEqual(correction_obj.amount.amount, -50)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1050)
     # Test when user tries to deposit or withdraw 0
     correction_obj = api.make_deposit_or_withdrawal(
         amount=Money(0, 'SEK'),
         user=user_obj,
         owner_id=wallet_obj.owner_id
     )
     self.assertEqual(correction_obj.wallet.owner_id, wallet_obj.owner_id)
     self.assertEqual(correction_obj.trx_type, enums.TrxType.CORRECTION)
     self.assertEqual(correction_obj.pre_balance.amount, 1050)
     self.assertEqual(correction_obj.amount.amount, 0)
     _, balance = wallet_api.get_balance(wallet_obj.owner_id)
     self.assertEqual(balance.amount, 1050)
Ejemplo n.º 9
0
 def test_cash_purchase(self):
     product_obj1 = ProductFactory.create(code='1337733113370',
                                          name='Billys Original',
                                          price=Money(13, 'SEK'))
     product_obj2 = ProductFactory.create(code='7331733113370',
                                          name='Kebaba',
                                          price=Money(30, 'SEK'))
     products = [
         (product_obj1.id, 3),
         (product_obj2.id, 1),
     ]
     api.purchase(None, products)
     product_obj1.refresh_from_db()
     product_obj2.refresh_from_db()
     self.assertEqual(product_obj1.qty, -3)
     self.assertEqual(product_obj2.qty, -1)
     _, balance = wallet_api.get_balance(settings.FOOBAR_CASH_WALLET)
     self.assertEqual(balance, Money(69, 'SEK'))
Ejemplo n.º 10
0
def calculate_correction(new_balance, owner_id, user, reference=None):
    """ Calculate the correct balance in the cash wallet """
    wallet, old_balance = wallet_api.get_balance(owner_id=owner_id, )
    correction_obj = WalletLogEntry.objects.create(
        user=user,
        trx_type=enums.TrxType.CORRECTION,
        wallet=wallet,
        comment=reference,
        amount=0,
        pre_balance=old_balance)

    correction_difference, difference = wallet_api.set_balance(
        owner_id=owner_id,
        new_balance=new_balance,
        reference=correction_obj.id)

    correction_obj.amount = difference
    correction_obj.save()
    return correction_obj
Ejemplo n.º 11
0
 def balance(self, obj):
     if obj.id is not None:
         _, balance = wallet_api.get_balance(obj.id)
         return balance
Ejemplo n.º 12
0
 def get_balance(self, instance):
     _, balance = wallet_api.get_balance(instance.owner_id)
     return MoneyField().to_representation(balance)
Ejemplo n.º 13
0
 def clean_deposit_or_withdrawal(self):
     data = self.cleaned_data['deposit_or_withdrawal']
     wallet, balance = api.get_balance(self.owner_id)
     if data.amount < 0 and -data > balance:
         raise forms.ValidationError(_('Not enough funds'))
     return data
Ejemplo n.º 14
0
 def balance(self, obj):
     _, balance = wallet_api.get_balance(obj.id)
     return balance