Changeset 12683 for main


Ignore:
Timestamp:
7 Mar 2015, 04:18:42 (10 years ago)
Author:
uli
Message:

Add adapter to turn lists of product opitons into payment items.

Location:
main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/contracts.py

    r12681 r12683  
    3232    IContractSelectProduct, ICustomersUtils, ISampleContract,
    3333    ISampleContractProcess, ISampleContractEdit, ISampleContractOfficialUse)
     34from waeup.ikoba.payments.interfaces import IPaymentItem
     35from waeup.ikoba.payments.payment import PaymentItem
    3436from waeup.ikoba.utils.helpers import attrs_to_fields
    3537
     
    5658
    5759ContractsContainer = attrs_to_fields(ContractsContainer)
     60
     61
     62def payment_items_from_contract(contract):
     63    """Turn contract product options into payment items.
     64    """
     65    result = []
     66    for num, option in enumerate(contract.product_options):
     67        item = PaymentItem()
     68        item.item_id = u'%s' % num
     69        item.title = option.title
     70        item.amount = option.fee
     71        result.append(item)
     72    return result
    5873
    5974
  • main/waeup.ikoba/branches/uli-payments/src/waeup/ikoba/customers/tests/test_contract.py

    r12682 r12683  
    1919Tests for contracts.
    2020"""
     21import decimal
    2122from zope.interface.verify import verifyClass, verifyObject
    2223from zope.component import createObject
     
    2627    IContractsContainer, IContract)
    2728from waeup.ikoba.customers.contracts import (
    28     ContractsContainer, SampleContract)
     29    ContractsContainer, SampleContract, payment_items_from_contract,
     30    )
     31from waeup.ikoba.payments.interfaces import IPaymentItem
     32from waeup.ikoba.products.productoptions import ProductOption
    2933from waeup.ikoba.testing import (FunctionalLayer, FunctionalTestCase)
    3034
     
    8993        self.assertTrue('Contract created by system' in messages)
    9094        return
     95
     96
     97class TestContractHelpers(FunctionalTestCase):
     98
     99    layer = FunctionalLayer
     100
     101    def test_payment_items_from_contract(self):
     102        # we can turn contracts into lists of payment items
     103        contract = SampleContract()
     104        option1 = ProductOption(u"Fee 1", decimal.Decimal("31.10"), "USD")
     105        option2 = ProductOption(u"Fee 2", decimal.Decimal("12.12"), "USD")
     106        contract.product_options = [option1, option2]
     107        payment_items = payment_items_from_contract(contract)
     108        assert len(payment_items) == 2
     109        item1, item2 = payment_items
     110        assert IPaymentItem.providedBy(item1)
     111        assert IPaymentItem.providedBy(item2)
     112        assert item1.item_id == "0"
     113        assert item2.item_id == "1"
     114        assert item1.title == u'Fee 1'
     115        assert item2.title == u'Fee 2'
     116        assert item1.amount == decimal.Decimal("31.10")
     117        assert item2.amount == decimal.Decimal("12.12")
     118
     119    def test_payment_item_from_contract_no_options(self):
     120        # we cope with empty contracts
     121        contract = SampleContract()
     122        payment_items = payment_items_from_contract(contract)
     123        assert payment_items == []
Note: See TracChangeset for help on using the changeset viewer.