Ejemplo n.º 1
0
    def _process_lock(self, session, curs, data, operation, balance_updater):
        lock_f = BalanceLockFilter(session, {
            'id': data['lock_id'],
            'balance_id': data['balance_id']
        }, {}, None)
        lock = lock_f.filter_one_obj(curs)

        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        info = data.get('info', {})

        trans_data = {
            'environment_id': session.environment_id,
            'user_id': balance.user_id,
            'balance_id': balance.id,
            'currency_code': currency.code,
            'type': operation,
            'real_amount': cents_to_decimal(currency, lock.real_amount),
            'virtual_amount': cents_to_decimal(currency, lock.virtual_amount),
            'order_id': lock.order_id,
            'info': info
        }

        balance_updater(balance, lock)
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.delete(curs, lock)
        mapping.insert(curs, trans)
        return response_ok(transaction_id=trans.id)
Ejemplo n.º 2
0
    def _process_lock(self, session, curs, data, operation, balance_updater):
        lock_f = BalanceLockFilter(session, {'id': data['lock_id'],
            'balance_id': data['balance_id']}, {}, None)
        lock = lock_f.filter_one_obj(curs)

        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        info = data.get('info', {})

        trans_data = {'environment_id': session.environment_id, 'user_id': balance.user_id,
            'balance_id': balance.id, 'currency_code': currency.code, 'type': operation,
            'real_amount': cents_to_decimal(currency, lock.real_amount),
            'virtual_amount': cents_to_decimal(currency, lock.virtual_amount),
            'order_id': lock.order_id, 'info': info}

        balance_updater(balance, lock)
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.delete(curs, lock)
        mapping.insert(curs, trans)
        return response_ok(transaction_id=trans.id)
Ejemplo n.º 3
0
    def _make_income_transaction(self, curs, data, session, transaction_type):
        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        amount_dec = Decimal(data['amount'])
        amount = decimal_to_cents(currency, amount_dec)
        enrolled_amount_dec = cents_to_decimal(currency, amount)

        if amount < 0:
            amount *= -1

        trans_data = {'environment_id': session.environment_id,
            'user_id': balance.user_id, 'balance_id': balance.id,
            'currency_code': currency.code, 'real_amount': 0, 'virtual_amount': 0,
            'type': transaction_type, 'info': data.get('info', {})}

        if transaction_type == 'receipt':
            balance.real_amount += amount
            trans_data['real_amount'] = enrolled_amount_dec
        elif transaction_type == 'bonus':
            balance.virtual_amount += amount
            trans_data['virtual_amount'] = enrolled_amount_dec
        else:
            raise HelixbillingError('Unhandled income transaction type: %s' %
                transaction_type)
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.insert(curs, trans)
        return trans.id
Ejemplo n.º 4
0
 def test_update(self, curs=None):
     obj = mapping.get(curs, self.T, cond=Eq('id', 1))
     obj.name = 'mamba'
     obj.client_ids = [44, 23]
     mapping.update(curs, obj)
     new_obj = mapping.get(curs, self.T, cond=Eq('id', 1))
     self.assertEqual(obj.name, new_obj.name)
     self.assertEqual(obj.client_ids, new_obj.client_ids)
Ejemplo n.º 5
0
    def lock(self, data, session, curs=None):
        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        lock_amount_dec = Decimal(data['amount'])
        lock_amount = decimal_to_cents(currency, lock_amount_dec)

        if lock_amount < 0:
            lock_amount *= -1

        locking_order = data['locking_order']
        amounts_to_lock = compute_locks(balance, lock_amount, locking_order)
        lock_real = amounts_to_lock.get('real_amount', 0)
        lock_virtual = amounts_to_lock.get('virtual_amount', 0)
        order_id = data['order_id']
        info = data.get('info', {})
        info['locking_order'] = locking_order

        trans_data = {
            'environment_id': session.environment_id,
            'user_id': balance.user_id,
            'balance_id': balance.id,
            'currency_code': currency.code,
            'type': 'lock',
            'real_amount': cents_to_decimal(currency, lock_real),
            'virtual_amount': cents_to_decimal(currency, lock_virtual),
            'order_id': order_id,
            'info': info
        }
        lock_data = {
            'environment_id': session.environment_id,
            'user_id': balance.user_id,
            'balance_id': balance.id,
            'real_amount': lock_real,
            'locking_order': locking_order,
            'order_id': order_id,
            'currency_id': currency.id,
            'virtual_amount': lock_virtual,
            'info': info
        }

        lock = BalanceLock(**lock_data)
        balance.real_amount -= lock_real
        balance.virtual_amount -= lock_virtual
        balance.locked_amount += lock_real + lock_virtual
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.insert(curs, lock)
        mapping.insert(curs, trans)
        return response_ok(transaction_id=trans.id, lock_id=lock.id)
Ejemplo n.º 6
0
    def test_get(self, curs=None):
        obj = mapping.get(curs, self.T, cond=Eq('id', 1))
        self.assertEqual(1, obj.id)
        self.assertEqual('0', obj.name)
        self.assertEqual([0, 1, 2], obj.client_ids)

        obj.client_ids = [1, 1, 1]
        mapping.update(curs, obj)

        objs = mapping.get_list(curs, self.T, cond=Any(1, 'client_ids'), order_by='id')
        self.assertEqual(2, len(objs))
        self.assertEqual(1, objs[0].id)
        self.assertEqual(2, objs[1].id)
Ejemplo n.º 7
0
    def apply_draft_rules(self, data, session, curs=None):
        t_f = TariffFilter(session, {'id': data['tariff_id']}, {}, None)
        t_f.filter_one_obj(curs)

        f = RuleFilter(session, {'tariff_id': data['tariff_id']}, {}, ['id'])
        rs = f.filter_objs(curs, for_update=True)
        checker = RuleChecker()
        for r in rs:
            if r.draft_rule:
                checker.check(r.draft_rule)
                r.rule = r.draft_rule
                r.draft_rule = None
                mapping.update(curs, r)
        return response_ok()
Ejemplo n.º 8
0
    def apply_draft_rules(self, data, session, curs=None):
        t_f = TariffFilter(session, {'id': data['tariff_id']}, {}, None)
        t_f.filter_one_obj(curs)

        f = RuleFilter(session, {'tariff_id': data['tariff_id']}, {}, ['id'])
        rs = f.filter_objs(curs, for_update=True)
        checker = RuleChecker()
        for r in rs:
            if r.draft_rule:
                checker.check(r.draft_rule)
                r.rule = r.draft_rule
                r.draft_rule = None
                mapping.update(curs, r)
        return response_ok()
Ejemplo n.º 9
0
 def update_objs(self, curs, data, load_obj_func):
     to_update = self.get_fields_for_update(data)
     updated_objs = []
     objs = load_obj_func()
     if not isinstance(objs, (list, tuple)):
         objs = [objs]
     if len(to_update):
         for obj in objs:
             for f, new_f in to_update.items():
                 setattr(obj, f, data[new_f])
             mapping.update(curs, obj)
             updated_objs.append(obj)
     else:
         updated_objs = objs
     return updated_objs
Ejemplo n.º 10
0
    def lock(self, data, session, curs=None):
        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        lock_amount_dec = Decimal(data['amount'])
        lock_amount = decimal_to_cents(currency, lock_amount_dec)

        if lock_amount < 0:
            lock_amount *= -1

        locking_order = data['locking_order']
        amounts_to_lock = compute_locks(balance, lock_amount, locking_order)
        lock_real = amounts_to_lock.get('real_amount', 0)
        lock_virtual = amounts_to_lock.get('virtual_amount', 0)
        order_id = data['order_id']
        info = data.get('info', {})
        info['locking_order'] = locking_order

        trans_data = {'environment_id': session.environment_id, 'user_id': balance.user_id,
            'balance_id': balance.id, 'currency_code': currency.code,
            'type': 'lock', 'real_amount': cents_to_decimal(currency, lock_real),
            'virtual_amount': cents_to_decimal(currency, lock_virtual),
            'order_id': order_id, 'info': info}
        lock_data = {'environment_id': session.environment_id, 'user_id': balance.user_id,
            'balance_id': balance.id, 'real_amount': lock_real, 'locking_order': locking_order,
            'order_id': order_id, 'currency_id': currency.id, 'virtual_amount': lock_virtual,
            'info': info}

        lock = BalanceLock(**lock_data)
        balance.real_amount -= lock_real
        balance.virtual_amount -= lock_virtual
        balance.locked_amount += lock_real + lock_virtual
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.insert(curs, lock)
        mapping.insert(curs, trans)
        return response_ok(transaction_id=trans.id, lock_id=lock.id)
Ejemplo n.º 11
0
    def _make_income_transaction(self, curs, data, session, transaction_type):
        balance = self._get_active_balance(session, curs, data)

        currs_id_idx = self._get_currs_idx(curs, 'id')
        currency = currs_id_idx[balance.currency_id]

        amount_dec = Decimal(data['amount'])
        amount = decimal_to_cents(currency, amount_dec)
        enrolled_amount_dec = cents_to_decimal(currency, amount)

        if amount < 0:
            amount *= -1

        trans_data = {
            'environment_id': session.environment_id,
            'user_id': balance.user_id,
            'balance_id': balance.id,
            'currency_code': currency.code,
            'real_amount': 0,
            'virtual_amount': 0,
            'type': transaction_type,
            'info': data.get('info', {})
        }

        if transaction_type == 'receipt':
            balance.real_amount += amount
            trans_data['real_amount'] = enrolled_amount_dec
        elif transaction_type == 'bonus':
            balance.virtual_amount += amount
            trans_data['virtual_amount'] = enrolled_amount_dec
        else:
            raise HelixbillingError('Unhandled income transaction type: %s' %
                                    transaction_type)
        mapping.update(curs, balance)

        trans = Transaction(**trans_data)
        mapping.insert(curs, trans)
        return trans.id