Inventory.py 6.91 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
45
    isInventory = 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47 48

    # Declarative security
    security = ClassSecurityInfo()
49
    security.declareObjectProtected(Permissions.AccessContentsInformation)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
50 51 52 53 54 55 56 57 58 59 60 61

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Delivery
                      , PropertySheet.Path
                      , PropertySheet.FlowCapacity
62
                      , PropertySheet.Inventory
Jean-Paul Smets's avatar
Jean-Paul Smets committed
63 64
                      )

65 66
    security.declarePublic('alternateReindexObject')
    def alternateReindexObject(self, **kw):
67 68
      """This method is called when an inventory object is included in a
      group of catalogged objects.
69 70
      """
      return self.immediateReindexObject(**kw)
71

72
    def immediateReindexObject(self,temp_constructor=None,**kw):
73 74 75 76
      """
      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
77 78 79 80

      temp_constructor is used in some particular cases where we want
      to have our own temp object constructor, this is usefull if we
      want to use some classes with some particular methods
81
      """
82
      resource_and_variation_dict = {}
83
      stock_object_list = []
84 85 86
      if temp_constructor is None:
        from Products.ERP5Type.Document import newTempDeliveryLine
        temp_constructor = newTempDeliveryLine
87 88
      start_date = self.getStartDate()
      node = self.getDestination()
89 90
      portal_simulation = self.getPortalObject().portal_simulation
      current_inventory_list = portal_simulation.getInventoryList( \
91 92
                                    to_date          = start_date
                                  , node             = node
93
                                  , simulation_state = self.getPortalCurrentInventoryStateList()
94 95
                                  , group_by_sub_variation = 1
                                  , group_by_variation = 1
96
                                  , group_by_resource = 1
97
                                  )
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
      current_inventory_dict = {}
      current_inventory_key_id_list = ('resource_relative_url', 'variation_text')
      for line in current_inventory_list:
        current_inventory_key = tuple([line[x] for x in current_inventory_key_id_list])
        try:
          current_inventory_by_sub_variation = current_inventory_dict[current_inventory_key]
        except KeyError:
          current_inventory_by_sub_variation = current_inventory_dict[current_inventory_key] = {}
        current_inventory_by_sub_variation[line['sub_variation_text']] = line['total_quantity']
      def getCurrentInventoryBySubVariation(**criterion_dict):
        current_inventory_key = tuple([criterion_dict[x] for x in current_inventory_key_id_list])
        return current_inventory_dict.get(current_inventory_key, {})
      for movement in self.getMovementList():
        if movement.getResourceValue() is not None and movement.getQuantity() not in (None,''):
          resource_path =  movement.getResource()
          variation_text = movement.getVariationText()
          resource_and_variation_key = (resource_path, variation_text)
          if resource_and_variation_key not in resource_and_variation_dict:
            resource_and_variation_dict[resource_and_variation_key] = None
117 118
            kwd = {'uid':self.getUid(),
                   'start_date': start_date}
Yoshinori Okuji's avatar
Yoshinori Okuji committed
119
            variation_list = variation_text.split('\n')
120 121 122 123
            inventory_by_subvariation_dict = getCurrentInventoryBySubVariation(
              resource_relative_url=resource_path,
              variation_text=variation_text)
            for sub_variation_text, total_quantity in inventory_by_subvariation_dict.iteritems():
124
              sub_variation_list = []
125 126
              if sub_variation_text is not None:
                sub_variation_list = sub_variation_text.split('\n')
127
              category_list = self.getCategoryList()
128
              if total_quantity != 0:
129 130
                temp_delivery_line = temp_constructor(self,
                                                      self.getId())
131 132
                kwd['quantity'] = - total_quantity
                category_list.append('resource/%s' % resource_path)
133 134 135 136 137
                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)
138 139
      object_list = [self]
      self.portal_catalog.catalogObjectList(object_list)
140
      if len(stock_object_list)==0:
Aurel's avatar
Aurel committed
141
        # Make sure to remove all lines
142
        from Products.ERP5Type.Document import newTempBase
143
        stock_object_list.append(temp_constructor(self,self.getId(),
144
                                 uid=self.getUid()))
145 146
      self.portal_catalog.catalogObjectList(stock_object_list,
           method_id_list=('z_catalog_stock_list',),
147
           disable_cache=1,check_uid=0)