Inventory.py 6.47 KB
Newer Older
Jean-Paul Smets's avatar
Jean-Paul Smets committed
1 2 3
##############################################################################
#
# Copyright (c) 2002 Nexedi SARL and Contributors. All Rights Reserved.
4
#                    Jean-Paul Smets-Solanes <jp@nexedi.com>
Jean-Paul Smets's avatar
Jean-Paul Smets committed
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
#
# 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
30 31
from Products.ERP5Type import Permissions, PropertySheet
from Products.ERP5.Document.Delivery import Delivery
32 33
from Acquisition import aq_base
from zLOG import LOG
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34

35
class Inventory(Delivery):
36
    """
37
      Inventory
38
    """
Jean-Paul Smets's avatar
Jean-Paul Smets committed
39 40 41 42 43
    # CMF Type Definition
    meta_type = 'ERP5 Inventory'
    portal_type = 'Inventory'
    isPortalContent = 1
    isRADContent = 1
44
    isDelivery = 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62

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

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Delivery
                      , PropertySheet.Path
                      , PropertySheet.FlowCapacity
                      )

Yoshinori Okuji's avatar
Yoshinori Okuji committed
63
    security.declareProtected(Permissions.AccessContentsInformation, 'getSimulationState')
Jean-Paul Smets's avatar
Jean-Paul Smets committed
64 65 66 67 68
    def getSimulationState(self, id_only=1):
      """
        Returns the current state in simulation
      """
      return 'delivered' # For now, consider that Inventory has no workflow XXX
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152


    def immediateReindexObject(self,**kw):
      """
      Rewrite reindexObject so that we can insert lines in stock table
      to make sure all stock values for resources in this inventory
      is equal to null before the date of this inventory
      """
      resource_and_variation_list = []
      stock_object_list = []
      from Products.ERP5Type.Document import newTempDeliveryLine
      start_date = self.getStartDate()
      node = self.getDestination()
      for movement in self.getMovementList():
        resource =  movement.getResourceValue()
        variation_text = movement.getVariationText()
        if (resource,variation_text) not in resource_and_variation_list:
          resource_and_variation_list.append((resource,variation_text))
          current_inventory_list = resource.getInventoryList( \
                                  to_date          = start_date
                                , variation_text   = variation_text
                                , node             = node
                                , simulation_state = self.getPortalCurrentInventoryStateList()
                                , group_by_sub_variation = 1
                                , group_by_variation = 1
                                )
          kwd = {'uid':self.getUid()}
          kwd['start_date'] = start_date
          variation_list = variation_text.split('/n')
          for inventory in current_inventory_list:
            sub_variation_list = []
            if inventory.sub_variation_text is not None:
              sub_variation_list = inventory.sub_variation_text.split('\n')
            category_list = self.getCategoryList()
            if inventory.total_quantity != 0:
              temp_delivery_line = newTempDeliveryLine(self, 
                                                       self.getId())
              kwd['quantity'] = - inventory.total_quantity
              category_list.append('resource/%s' % inventory.resource_relative_url)
              category_list.extend(variation_list)
              category_list.extend(sub_variation_list)
              kwd['category_list'] = category_list
              temp_delivery_line.edit(**kwd)
              stock_object_list.append(temp_delivery_line)
      object_list = [self]
      self.portal_catalog.catalogObjectList(object_list)
      self.portal_catalog.catalogObjectList(stock_object_list,
           method_id_list=('z_catalog_stock_list',),
           disable_cache=1)

    security.declarePublic( 'recursiveReindexObject' )
    def recursiveReindexObject(self, *args, **kw):
      """
      Do not use group_method_id for the inventory, but it can
      be used for inventory lines and cells
      """
      root_indexable = int(getattr(self.getPortalObject(),'isIndexable',1))
      self._reindexObject()
      if self.isIndexable and root_indexable:
        self.activate(group_method_id='portal_catalog/catalogObjectList', 
            expand_method_id='getIndexableChildValueList', 
            **kw).recursiveImmediateReindexObject(*args, **kw)

    security.declareProtected( Permissions.AccessContentsInformation, 'getIndexableChildValueList' )
    def getIndexableChildValueList(self):
      """
        Get indexable childen recursively.
      """
      value_list = []
      if self.isIndexable:
        #value_list.append(self) # do not include self
        for c in self.objectValues():
          if hasattr(aq_base(c), 'getIndexableChildValueList'):
            value_list.extend(c.getIndexableChildValueList())
      return value_list

    def _reindexObject(self, *args, **kw):
      """
      Defined here because we want to 
      Make sure to call without the group_method_id for inventories
      """
      root_indexable = int(getattr(self.getPortalObject(),'isIndexable',1))
      if self.isIndexable and root_indexable:
        self.activate(**kw).immediateReindexObject(*args, **kw)