InvoiceRule.py 6.57 KB
Newer Older
1 2
##############################################################################
#
3
# Copyright (c) 2002-2005 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
5
#                    Romain Courteaud <romain@nexedi.com>
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
#
# 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 Products.ERP5Type import Permissions, PropertySheet, Constraint, Interface
from Products.ERP5.Document.Rule import Rule
33
from Products.ERP5.Document.DeliveryRule import DeliveryRule
34 35 36

from zLOG import LOG

37
class InvoiceRule(DeliveryRule):
38
    """
39
      Invoice Rule object make sure an Invoice in the simulation
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
      is consistent with the real invoice
      WARNING: what to do with movement split ?
    """

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

    # Declarative security
    security = ClassSecurityInfo()
    security.declareObjectProtected(Permissions.View)

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      )

    # Simulation workflow
    security.declareProtected(Permissions.ModifyPortalContent, 'expand')
    def expand(self, applied_rule, force=0, **kw):
      """
        Expands the current movement downward.

        -> new status -> expanded

        An applied rule can be expanded only if its parent movement
        is expanded.
      """
      invoice_line_type = 'Simulation Movement'
      # Get the invoice where we come from
      my_invoice = applied_rule.getDefaultCausalityValue()
76 77
      # Only expand if my_invoice is not None and 
      # state is not 'confirmed'
78
      if my_invoice is not None:
79 80 81
        # Only expand invoice rule if invoice not yet confirmed 
        # (This is consistent with the fact that once simulation is 
        # launched, we stick to it)
82
        if force or \
83 84 85 86
           (applied_rule.getLastExpandSimulationState() not in \
                     self.getPortalReservedInventoryStateList() and \
           applied_rule.getLastExpandSimulationState() not in \
                      self.getPortalCurrentInventoryStateList()):
87 88 89 90
          # First, check each contained movement and make
          # a list of invoice_line ids which do not need to be copied
          # eventually delete movement which do not exist anylonger
          existing_uid_list = []
91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
          movement_type_list = applied_rule.getPortalMovementTypeList()
          # non generic
          invoice_movement_type_list = \
                                 applied_rule.getPortalInvoiceMovementTypeList()
          for movement in applied_rule.contentValues(
                   filter={'portal_type':movement_type_list}):
            invoice_element = movement.getDeliveryValue(
                   portal_type=invoice_movement_type_list)

            if (invoice_element is None) or\
               (invoice_element.hasCellContent()) or\
               (len(invoice_element.getDeliveryRelatedValueList()) > 1):
              # Our invoice_element is already related 
              # to another simulation movement
              # Delete ourselve
  #             movement.flushActivity(invoke=0)
              # XXX Make sure this is not deleted if already in delivery
              applied_rule._delObject(movement.getId())  
109
            else:
110
              existing_uid_list_append(invoice_element.getUid())
111
          # Copy each movement (line or cell) from the invoice
112 113 114
          # non generic
          for invoice_line_object in my_delivery.getMovementList(
                   portal_type=self.getPortalInvoiceMovementTypeList()):
115
            try:
116 117 118 119 120 121
              # Only create if orphaned movement
              if invoice_line_object.getUid() not in existing_uid_list:
                # Generate a nicer ID
                if invoice_line_object.getParentUid() ==\
                                      invoice_line_object.getExplanationUid():
                  # We are on a line
122
                  new_id = invoice_line_object.getId()
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
                else:
                  # On a cell
                  new_id = "%s_%s" % (invoice_line_object.getParentId(),
                                      invoice_line_object.getId())
                # Generate the simulation movement
                new_sim_mvt = applied_rule.newContent(
                                portal_type=invoice_line_type,
                                id=new_id,
                                order_value=invoice_line_object,
                                delivery_value=invoice_line_object,
                                # XXX Do we need to copy the quantity
                                # Why not the resource, the variation,...
                                quantity=invoice_line_object.getQuantity(),
                                delivery_ratio=1,
                                deliverable=1)
138
            except AttributeError:
139 140 141 142 143 144 145
              LOG('ERP5: WARNING', 0, 
                  'AttributeError during expand on invoice line %s' \
                  % invoice_line_object.absolute_url())
          # Now we can set the last expand simulation state to the 
          # current state
          applied_rule.setLastExpandSimulationState(
              my_invoice.getSimulationState())
146 147
      # Pass to base class
      Rule.expand(self, applied_rule, force=force, **kw)