PaymentRule.py 7.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
#                    Sebastien Robin <seb@nexedi.com>
#
# WARNING: This program as such is intended to be used by professional
# programmers who take the whole responsability of assessing all potential
# consequences resulting from its eventual inadequacies and bugs
# End users who are looking for a ready-to-use solution with commercial
# garantees and support are strongly adviced to contract a Free Software
# Service Company
#
# 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.
#
##############################################################################

from AccessControl import ClassSecurityInfo
from Acquisition import aq_base, aq_parent, aq_inner, aq_acquire
from Products.CMFCore.utils import getToolByName

from Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5.Document.Rule import Rule

36
from zLOG import LOG, INFO
37 38

class PaymentRule(Rule):
39 40
    """Payment Rule generates payment simulation movement from invoice
    transaction simulation movements.
41 42 43 44 45 46 47 48 49 50 51
    """

    # CMF Type Definition
    meta_type = 'ERP5 Payment Rule'
    portal_type = 'Payment Rule'
    add_permission = Permissions.AddPortalContent
    isPortalContent = 1
    isRADContent = 1

    # Declarative security
    security = ClassSecurityInfo()
52
    security.declareObjectProtected(Permissions.AccessContentsInformation)
53

Jérome Perrin's avatar
Jérome Perrin committed
54 55 56
    __implements__ = ( Interface.Predicate,
                       Interface.Rule )

57 58 59 60 61
    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
62
                      , PropertySheet.Task
63 64
                      )

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    receivable_account_type_list = ('asset/receivable', )
    payable_account_type_list = ('liability/payable', )


    def _getPaymentConditionList(self, movement):
      """Returns payment conditions for this movement.
      """
      while 1:
        delivery_movement = movement.getDeliveryValue()
        if delivery_movement is not None:
          explanation = delivery_movement.getExplanationValue()
          payment_condition_list = explanation.contentValues(
                 filter=dict(portal_type='Payment Condition'))
          if payment_condition_list:
            return payment_condition_list

        order_movement = movement.getOrderValue()
        if order_movement is not None:
          explanation = order_movement.getExplanationValue()
          payment_condition_list = explanation.contentValues(
                 filter=dict(portal_type='Payment Condition'))
          if payment_condition_list:
            return payment_condition_list

        movement = movement.getParentValue().getParentValue()
        if movement.getPortalType() != 'Simulation Movement':
          LOG('ERP5', INFO, "PaymentRule couldn't find payment condition")
          return []
     
    def _createMovementsForPaymentCondition(self,
          applied_rule, payment_condition):
      """Create simulation movements for this payment condition.
97
      """
98 99 100 101 102 103 104 105
      simulation_movement = applied_rule.getParentValue()
      date = payment_condition.TradeCondition_getDueDate()
      
      if payment_condition.getQuantity():
        quantity = payment_condition.getQuantity()
      else:
        ratio = payment_condition.getEfficiency(1)
        quantity = simulation_movement.getQuantity() * ratio
106

107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
      edit_dict = dict(
            causality_value=payment_condition,
            payment_mode=payment_condition.getPaymentMode(),
            source=simulation_movement.getSource(),
            source_section=simulation_movement.getSourceSection(),
            source_payment=payment_condition.getSourcePayment() or
                              simulation_movement.getSourcePayment(),
            destination=simulation_movement.getDestination(),
            destination_section=simulation_movement.getDestinationSection(),
            destination_payment=payment_condition.getDestinationPayment() or
                              simulation_movement.getDestinationPayment(),
            resource=simulation_movement.getResource(),
            start_date=date,
            price=1,
            quantity= - quantity,)
      
      applied_rule.newContent( **edit_dict )
124

125 126 127 128 129 130 131 132 133
      edit_dict['source'] = self.getSourcePayment()
      edit_dict['destination'] = self.getDestinationPayment()
      edit_dict['quantity'] = - edit_dict['quantity']
      applied_rule.newContent( **edit_dict )
      
      
    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
    def expand(self, applied_rule, **kw):
      """Expands the current movement downward.
134 135 136
      """
      payment_line_type = 'Simulation Movement'

137
      my_parent_movement = applied_rule.getParentValue()
138 139 140 141
      # generate for source
      bank_account = self.getDestinationPaymentValue(
                             portal_type='Account')
      assert bank_account is not None
142

143 144 145 146 147 148 149 150 151 152 153
      for payment_condition in self._getPaymentConditionList(
                                            my_parent_movement):
        payment_condition_url = payment_condition.getRelativeUrl()
        # look for a movement for this payment condition:
        corresponding_movement_list = []
        for simulation_movement in applied_rule.contentValues():
          if simulation_movement.getCausality() == payment_condition_url:
            corresponding_movement_list.append(simulation_movement)
        if not corresponding_movement_list:
          self._createMovementsForPaymentCondition(applied_rule,
                                                   payment_condition)
154
        else:
155 156 157 158 159 160 161 162
          # TODO: update corresponding_movement_list
          pass
      
      #Rule.expand(self, applied_rule, **kw)

    def test(self, context, tested_base_category_list=None):
      """Test if this rule apply.
      """
163 164 165 166

      # XXX for now disable this rule
      return False

167 168 169 170 171 172 173 174 175 176 177 178 179 180
      if context.getParentValue()\
          .getSpecialiseValue().getPortalType() == 'Payment Rule':
        return False

      for account in ( context.getSourceValue(portal_type='Account'),
          context.getDestinationValue(portal_type='Account')):
        if account is not None:
          account_type = account.getAccountType()
          if account_type in self.receivable_account_type_list or \
              account_type in self.payable_account_type_list:
            return True

      return False