## $Id: contracts.py 12698 2015-03-09 02:04:06Z uli $ ## ## Copyright (C) 2014 Uli Fouquet & Henrik Bettermann ## This program is free software; you can redistribute it and/or modify ## it under the terms of the GNU General Public License as published by ## the Free Software Foundation; either version 2 of the License, or ## (at your option) any later version. ## ## This program is distributed in the hope that it will be useful, ## but WITHOUT ANY WARRANTY; without even the implied warranty of ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the ## GNU General Public License for more details. ## ## You should have received a copy of the GNU General Public License ## along with this program; if not, write to the Free Software ## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA ## """ Customer contract components. """ import grok from zope.component import getUtility from zope.component.interfaces import IFactory from zope.interface import implementedBy from zope.schema import getFields from hurry.workflow.interfaces import IWorkflowInfo, IWorkflowState from waeup.ikoba.interfaces import MessageFactory as _ from waeup.ikoba.interfaces import ( IObjectHistory, VERIFIED, APPROVED, PROVISIONALLY, IIDSource) from waeup.ikoba.customers.interfaces import ( IContractsContainer, ICustomerNavigation, IContract, IContractSelectProduct, ICustomersUtils, ISampleContract, ISampleContractProcess, ISampleContractEdit, ISampleContractOfficialUse) from waeup.ikoba.payments.interfaces import IPayer from waeup.ikoba.payments.payment import PaymentItem from waeup.ikoba.utils.helpers import attrs_to_fields class ContractsContainer(grok.Container): """This is a container for customer contracts. """ grok.implements(IContractsContainer, ICustomerNavigation) grok.provides(IContractsContainer) def addContract(self, contract): if not IContract.providedBy(contract): raise TypeError( 'ContractsContainers contain only IContract instances') self[contract.contract_id] = contract return @property def customer(self): return self.__parent__ def writeLogMessage(self, view, message): return self.__parent__.writeLogMessage(view, message) ContractsContainer = attrs_to_fields(ContractsContainer) def payment_items_from_contract(contract): """Turn contract product options into payment items. """ result = [] for num, option in enumerate(contract.product_options): item = PaymentItem() item.item_id = u'%s' % num item.title = option.title item.amount = option.fee result.append(item) return result class ContractPayer(grok.Adapter): """Adapter to turn contracts into IPayers. """ grok.implements(IPayer) grok.context(IContract) def __init__(self, context): self.context = context @property def _customer(self): return self.context.customer @property def first_name(self): return getattr(self._customer, 'firstname', None) @property def last_name(self): return getattr(self._customer, 'lastname', None) @property def payer_id(self): return getattr(self._customer, 'customer_id', None) class ContractBase(grok.Container): """This is a customer contract baseclass. """ grok.implements(IContractSelectProduct) # Neccessary for the # selectproduct page (why?) grok.baseclass() def __init__(self): super(ContractBase, self).__init__() # The site doesn't exist in unit tests source = getUtility(IIDSource) self.contract_id = unicode(source.get_hex_uuid()) self.last_product_id = None self.tc_dict = {} self.title = None self.valid_to = None self.valid_from = None return @property def history(self): history = IObjectHistory(self) return history @property def state(self): return IWorkflowState(self).getState() @property def translated_state(self): try: TRANSLATED_STATES = getUtility( ICustomersUtils).TRANSLATED_CONTRACT_STATES return TRANSLATED_STATES[self.state] except KeyError: return @property def class_name(self): return self.__class__.__name__ @property def formatted_transition_date(self): try: return self.history.messages[-1].split(' - ')[0] except IndexError: return @property def customer(self): try: return self.__parent__.__parent__ except AttributeError: return None @property def user_id(self): if self.customer is not None: return self.customer.customer_id return def writeLogMessage(self, view, message): return self.__parent__.__parent__.writeLogMessage(view, message) @property def is_editable(self): try: # Customer must have requested cond1 = self.customer.state in getUtility( ICustomersUtils).CONMANAGE_CUSTOMER_STATES # Contract must be in state created cond2 = self.state in getUtility( ICustomersUtils).CONMANAGE_CONTRACT_STATES if not (cond1 and cond2): return False except AttributeError: pass return True @property def is_approvable(self): if self.customer and not self.customer.state in ( APPROVED, PROVISIONALLY): return False, _("Customer has not yet been approved.") for key, field in getFields(self.check_docs_interface).items(): if key.endswith('_object'): obj = getattr(self, key, None) state = getattr(obj, 'state', None) if state and state != VERIFIED: return False, _( "Attached documents must be verified first.") return True, None @property def translated_class_name(self): try: CONTYPES_DICT = getUtility(ICustomersUtils).CONTYPES_DICT return CONTYPES_DICT[self.class_name] except KeyError: return @property def fee_based(self): if self.product_options: amount = 0 for option in self.product_options: amount += option.fee if amount: return True return False class SampleContract(ContractBase): """This is a sample contract. """ grok.implements( ISampleContractProcess, # must come before ISampleContract ISampleContract, ISampleContractEdit, ICustomerNavigation) contract_category = 'sample' form_fields_interface = ISampleContract edit_form_fields_interface = ISampleContractEdit ou_form_fields_interface = ISampleContractOfficialUse check_docs_interface = ISampleContract SampleContract = attrs_to_fields(SampleContract) # Contracts must be importable. So we might need a factory. class SampleContractFactory(grok.GlobalUtility): """A factory for contracts. """ grok.implements(IFactory) grok.name(u'waeup.SampleContract') title = u"Create a new contract.", description = u"This factory instantiates new sample contract instances." def __call__(self, *args, **kw): return SampleContract(*args, **kw) def getInterfaces(self): return implementedBy(SampleContract) @grok.subscribe(IContract, grok.IObjectAddedEvent) def handle_contract_added(contract, event): """If an contract is added the transition create is fired. The latter produces a logging message. """ if IWorkflowState(contract).getState() is None: IWorkflowInfo(contract).fireTransition('create') return