InvoicingRule.py 6.12 KB
Newer Older
Sebastien Robin's avatar
Sebastien Robin committed
1 2
##############################################################################
#
3
# Copyright (c) 2002-2005 Nexedi SARL and Contributors. All Rights Reserved.
Sebastien Robin's avatar
Sebastien Robin committed
4
#                    Sebastien Robin <seb@nexedi.com>
5
#                    Romain Courteaud <romain@nexedi.com>
Sebastien Robin's avatar
Sebastien Robin committed
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 36 37 38 39 40
#
# 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

from zLOG import LOG

class InvoicingRule(Rule):
    """
41
      Invoicing Rule expand simulation created by a order rule.
Sebastien Robin's avatar
Sebastien Robin committed
42 43 44
    """

    # CMF Type Definition
45 46
    meta_type = 'ERP5 Invoicing Rule'
    portal_type = 'Invoicing Rule'
Sebastien Robin's avatar
Sebastien Robin committed
47 48 49 50 51 52
    add_permission = Permissions.AddPortalContent
    isPortalContent = 1
    isRADContent = 1

    # Declarative security
    security = ClassSecurityInfo()
53
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Sebastien Robin's avatar
Sebastien Robin committed
54

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

Sebastien Robin's avatar
Sebastien Robin committed
58 59 60 61 62 63 64
    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      )

65 66 67 68 69 70 71 72 73 74
    security.declareProtected(Permissions.AccessContentsInformation,
                              'isAccountable')
    def isAccountable(self, movement):
      """Tells wether generated movement needs to be accounted or not.

      Invoice movement are never accountable, so simulation movement for
      invoice movements should not be accountable either.
      """
      return 0

Sebastien Robin's avatar
Sebastien Robin committed
75 76 77 78 79
    security.declareProtected(Permissions.AccessContentsInformation, 'test')
    def test(self, movement):
      """
        Tests if the rule (still) applies
      """
80
      parent = movement.getParentValue()
81 82
      result = 0
      if (parent.getPortalType() == 'Applied Rule') and \
83 84
         (parent.getSpecialiseId() in ('default_order_rule',
                                       'default_delivery_rule' )):
85 86
        result = 1
      return result
Sebastien Robin's avatar
Sebastien Robin committed
87 88 89

    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
    def expand(self, applied_rule, **kw):
90
      """ Expands the current movement downward.
Sebastien Robin's avatar
Sebastien Robin committed
91 92 93
      """
      delivery_line_type = 'Simulation Movement'
      # Source that movement from the next node / stock
94
      my_context_movement = applied_rule.getParentValue()
95

96 97
      # Do not invoice within the same entity or whenever entities are
      # not all defined
98 99 100 101 102 103 104 105 106
      # It is OK to invoice within different entities of the same company
      # if we wish to get some internal analytical accounting
      # but that requires some processing to produce a balance sheet
      source_section = my_context_movement.getSourceSection()
      destination_section = my_context_movement.getDestinationSection()
      if source_section == destination_section or source_section is None \
         or destination_section is None:
        return Rule.expand(self, applied_rule, **kw)
      
Sebastien Robin's avatar
Sebastien Robin committed
107
      if my_context_movement.getSource() is not None:
108 109 110 111 112
        # XXX Please explain why ? Let us consider for
        # example a consumption movement of garbage which we
        # want to be invoiced (the cleanup company is working
        # within our premises)
        #
Sebastien Robin's avatar
Sebastien Robin committed
113 114
        # We should only expand movements if they have a source
        # otherwise, it creates infinite recursion
115 116 117
        # This happens for example whenever the source of a movement is 
        # acquired from an order which is deleted afterwards
        new_id = 'inv_mvt'
Sebastien Robin's avatar
Sebastien Robin committed
118
        if new_id in applied_rule.objectIds():
119
          invoice_line = applied_rule[new_id]
Sebastien Robin's avatar
Sebastien Robin committed
120
        else:
121
          invoice_line = applied_rule.newContent(
122 123 124 125
            type_name = delivery_line_type,
            id = new_id
          )
        # Edit movement
126
        invoice_line._edit(
127 128 129 130
          price = my_context_movement.getPrice(),
          quantity = my_context_movement.getQuantity(),
          quantity_unit = my_context_movement.getQuantityUnit(),
          efficiency = my_context_movement.getEfficiency(),
Romain Courteaud's avatar
Romain Courteaud committed
131
          resource = my_context_movement.getResource(),
132 133
          variation_category_list = my_context_movement.\
                                            getVariationCategoryList(),
134 135
          variation_property_dict = my_context_movement.\
              getVariationPropertyDict(),
136
          start_date = my_context_movement.getStartDate(),
137
          stop_date = my_context_movement.getStopDate(),
138
          source = my_context_movement.getSource(),
139
          source_section = source_section,
140
          destination = my_context_movement.getDestination(),
141
          destination_section = destination_section,
142
          # We do need to collect invoice lines to build invoices
143
          deliverable = 1,
144
        )
145
      
Sebastien Robin's avatar
Sebastien Robin committed
146 147 148 149
      # Create one submovement which sources the transformation
      Rule.expand(self, applied_rule, **kw)

    def isDeliverable(self, m):
150 151
      return m.getResource() is not None