## $Id: container.py 12818 2015-03-24 08:49:53Z uli $
##
## Copyright (C) 2011 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
##
"""
Containers which contain payment objects.
"""
import grok
from waeup.ikoba.interfaces import IIkobaPluggable
from waeup.ikoba.payments.interfaces import IPaymentsContainer
from waeup.ikoba.utils.helpers import attrs_to_fields
from waeup.ikoba.utils.logger import Logger


class PaymentsContainer(grok.Container, Logger):
    """This is a container for all kind of payments.
    """
    grok.implements(IPaymentsContainer)
    grok.provides(IPaymentsContainer)

    logger_name = 'waeup.ikoba.${sitename}.payments'
    logger_filename = 'payments.log'

    def __init__(self):
        super(PaymentsContainer, self).__init__()
        return

    def archive(self, id=None):
        raise NotImplementedError()

    def clear(self, id=None, archive=True):
        raise NotImplementedError()


PaymentsContainer = attrs_to_fields(PaymentsContainer)


class PaymentsPlugin(grok.GlobalUtility):
    """A plugin that creates container for payments inside a company
    and updates payment objects.
    """
    grok.implements(IIkobaPluggable)
    grok.name('payments')

    def setup(self, site, name, logger):
        if 'payments' in site.keys():
            logger.warn('Could not create container for payments.')
            return
        site['payments'] = PaymentsContainer()
        logger.info('Container for payments created')
        return

    def update(self, site, name, logger):
        if not 'payments' in site.keys():
            self.setup(site, name, logger)
        for payment in site['payments'].values():
            # Add payment_items
            if not hasattr(payment, 'payment_items'):
                payment.payment_items = ()
                logger.info(
                    'PaymentsPlugin: %s attribute %s added.' % (
                        payment.payment_id, 'payment_items')
                    )
        return
