Invoice.py 12.4 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 30
#
# 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
31
from Products.CMFCore.utils import getToolByName
32
from Products.ERP5.Document.AccountingTransaction import AccountingTransaction
33
from zLOG import LOG
Jean-Paul Smets's avatar
Jean-Paul Smets committed
34 35 36 37 38

class Invoice(AccountingTransaction):
    # CMF Type Definition
    meta_type = 'ERP5 Invoice'
    portal_type = 'Invoice'
39
    add_permission = Permissions.AddPortalContent
Jean-Paul Smets's avatar
Jean-Paul Smets committed
40 41 42
    isPortalContent = 1
    isRADContent = 1

43 44 45
    # Global variables
    _transaction_line_portal_type = 'Sale Invoice Transaction Line'
    
Jean-Paul Smets's avatar
Jean-Paul Smets committed
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    # Declarative security
    security = ClassSecurityInfo()
    security.declareObjectProtected(Permissions.View)

    # Default Properties
    property_sheets = ( PropertySheet.Base
                      , PropertySheet.XMLObject
                      , PropertySheet.CategoryCore
                      , PropertySheet.DublinCore
                      , PropertySheet.Delivery
                      , PropertySheet.Task
                      , PropertySheet.Arrow
                      , PropertySheet.Movement
                      , PropertySheet.Amount
                      , PropertySheet.Reference
                      , PropertySheet.PaymentCondition
62 63 64
                      , PropertySheet.ValueAddedTax
                      , PropertySheet.EcoTax
                      , PropertySheet.CopyrightTax
65
                      , PropertySheet.Folder
Jean-Paul Smets's avatar
Jean-Paul Smets committed
66 67
                      )

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
    security.declareProtected(Permissions.AccessContentsInformation, 'getTotalPrice')
    def getTotalPrice(self):
      """
        Returns the total price for this invoice
      """
      aggregate = self.Invoice_zGetTotal()[0]
      return aggregate.total_price

    security.declareProtected(Permissions.AccessContentsInformation, 'getTotalQuantity')
    def getTotalQuantity(self):
      """
        Returns the total quantity for this invoice
      """
      aggregate = self.Invoice_zGetTotal()[0]
      return aggregate.total_quantity
83

84 85 86 87 88 89
    security.declareProtected(Permissions.AccessContentsInformation, 'getTotalNetPrice')
    def getTotalNetPrice(self):
      """
        Returns the total net price for this invoice
      """
      return self.Invoice_zGetTotalNetPrice()
90

91
    security.declareProtected(Permissions.ModifyPortalContent, 'buildInvoiceTransactionList')
92 93 94 95 96 97 98
    def buildInvoiceTransactionList(self):
      """
        Retrieve all invoices transaction lines into the simulation
      """
      reindexable_movement_list = []

      parent_simulation_line_list = []
99 100
      # Browse invoice lines
      for o in self.getMovementList(portal_type = self.getPortalInvoiceMovementTypeList()) :
101 102 103 104 105 106 107 108
        parent_simulation_line_list += [x for x in o.getDeliveryRelatedValueList() \
                                        if x.getPortalType()=='Simulation Movement']
      invoice_transaction_rule_list = []
      simulation_line_list = []
      for o in parent_simulation_line_list:
        for rule in o.objectValues():
          invoice_transaction_rule_list.append(rule)
          simulation_line_list += rule.objectValues()
109
      #LOG('buildInvoiceTransactionList simulation_line_list',0,simulation_line_list)
110 111 112 113 114
      from Products.ERP5.MovementGroup import CategoryMovementGroup
      class_list = [CategoryMovementGroup, ]
      root_group = self.portal_simulation.collectMovement(simulation_line_list,class_list=class_list)

      if root_group is not None:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
115 116 117 118 119 120
        #LOG('buildInvoiceTransactionList root_group.group_list',0,root_group.group_list)
        # First delete existing accounting lines
        self.deleteContent(self.contentIds(
            filter={'portal_type':self.getPortalDeliveryMovementTypeList()}))
        # we don't want to overwrite the Invoice Lines
        existing_invoice_line_id_list = self.contentIds()
121
        for category_group in root_group.group_list:
Jean-Paul Smets's avatar
Jean-Paul Smets committed
122 123
          #LOG('buildInvoiceTransactionList category_group.group_list',0,category_group.group_list)
          #LOG('buildInvoiceTransactionList category_group.movement_list',0,category_group.movement_list)
124
          # sum quantities and add lines to invoice
Jean-Paul Smets's avatar
Jean-Paul Smets committed
125 126 127
          quantity = 0.0
          orig_group_id = None
          reference_movement = None
128 129
          for movement in category_group.movement_list :
            quantity += movement.getQuantity()
Jean-Paul Smets's avatar
Jean-Paul Smets committed
130 131 132 133 134 135
            # Guess an unused name for the new movement
            if orig_group_id is None:
              orig_group_id = movement.getId()
              reference_movement = movement
          #LOG('buildInvoiceTransactionList orig_group_id',0,orig_group_id)
          #LOG('buildInvoiceTransactionList existing_invoice_line_id_list',0,existing_invoice_line_id_list)
136 137 138 139
          if orig_group_id in existing_invoice_line_id_list :
            n = 1
            while '%s_%s' % (orig_group_id, n) in existing_invoice_line_id_list :
              n += 1
Jean-Paul Smets's avatar
Jean-Paul Smets committed
140
            group_id = '%s_%s' % (orig_group_id, n)            
141
          else :
Jean-Paul Smets's avatar
Jean-Paul Smets committed
142
            group_id = orig_group_id          
143
          existing_invoice_line_id_list.append(group_id)
Jean-Paul Smets's avatar
Jean-Paul Smets committed
144
            
145
          # add sum of movements to invoice
Jean-Paul Smets's avatar
Jean-Paul Smets committed
146
          #LOG('buildInvoiceTransactionList group_id',0,group_id)          
147 148 149 150
          #LOG('buildInvoiceTransactionList reference_movement',0,str(reference_movement.getRelativeUrl()))
          #LOG('buildInvoiceTransactionList reference_movement',0,str(reference_movement.showDict()))
          #LOG('buildInvoiceTransactionList reference_movement',0,str(reference_movement.getSource()))
          #LOG('buildInvoiceTransactionList reference_movement',0,str(reference_movement.getDestination()))
Jean-Paul Smets's avatar
Jean-Paul Smets committed
151
          sale_invoice_transaction_line_item = getattr(self, group_id, None)          
152
          if sale_invoice_transaction_line_item is None :
153 154
            sale_invoice_transaction_line_item = self.newContent(
                portal_type = self._transaction_line_portal_type
155
              , id = group_id
Jean-Paul Smets's avatar
Jean-Paul Smets committed
156 157
              , source = reference_movement.getSource()
              , destination = reference_movement.getDestination()
158 159
              , quantity = quantity
            )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
160 161 162 163
            if self.getDestinationSection() != reference_movement.getDestinationSection():
              sale_invoice_transaction_line_item._setDestinationSection(reference_movement.getDestinationSection())
            if self.getSourceSection() != reference_movement.getSourceSection():
              sale_invoice_transaction_line_item._setSourceSection(reference_movement.getSourceSection())
164
            #LOG('buildInvoiceTransactionList sale_invoice_transaction_line',0,str(sale_invoice_transaction_line_item.showDict())) 
165
          else :
Jean-Paul Smets's avatar
Jean-Paul Smets committed
166 167 168
            sale_invoice_transaction_line_item.edit(
                source = reference_movement.getSource()
              , destination = reference_movement.getDestination()
169
              , quantity = quantity
170
              , force_update = 1
171
            )
Jean-Paul Smets's avatar
Jean-Paul Smets committed
172 173 174 175
            if self.getDestinationSection() != reference_movement.getDestinationSection():
              sale_invoice_transaction_line_item._setDestinationSection(reference_movement.getDestinationSection())
            if self.getSourceSection() != reference_movement.getSourceSection():
              sale_invoice_transaction_line_item._setSourceSection(reference_movement.getSourceSection())
176 177 178 179 180 181 182 183 184 185

          # What do we really need to update in the simulation movement ?
          for movement in category_group.movement_list :
            if movement.getPortalType() == 'Simulation Movement' :
              movement._setDeliveryValue(sale_invoice_transaction_line_item)
              reindexable_movement_list.append(movement)

      # we now reindex the movements we modified
      for movement in reindexable_movement_list :
        movement.immediateReindexObject()
186
      return [self]
187

188
    security.declareProtected(Permissions.ModifyPortalContent, 'buildPaymentTransactionList')
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
    def buildPaymentTransactionList(self):
      """
        Retrieve all payments transaction lines into the simulation

        For this rule, we don't want to group anything : legally, we need to have every payment matching the quantity of a sale invoice.

        Warning : this code is not good, it is too simple, but is here to fulfill a very specific need at the moment.
      """
      reindexable_movement_list = []
      payment_transaction_list = []

      parent_simulation_line_list = []
      for o in self.contentValues(filter={'portal_type':'Sale Invoice Transaction Line'}) :
        parent_simulation_line_list += [x for x in o.getDeliveryRelatedValueList() \
                                        if x.getPortalType()=='Simulation Movement']
      payment_transaction_rule_list = []
      simulation_line_list = []
      for o in parent_simulation_line_list:
        for rule in o.objectValues():
          payment_transaction_rule_list.append(rule)
          simulation_line_list += rule.objectValues()
210
      #LOG('buildPaymentTransactionList simulation_line_list',0,simulation_line_list)
211 212 213 214 215 216 217

      # create payment transaction
      accounting_module = self.accounting
      payment_type = 'Payment Transaction'
      payment_id = str(accounting_module.generateNewId())
      payment_transaction = accounting_module.newContent(portal_type = payment_type
          , id = payment_id
218
          , reference = self.getReference()
219
          , resource = self.getResource()
220
          , start_date = self.getStartDate()
221 222 223 224 225
          , source_payment = self.getSourcePayment()
          , source_section = self.getSourceSection()
          , destination_payment = self.getDestinationPayment()
          , destination_section = self.getDestinationSection()
          )
226
      #LOG('buildPaymentTransactionList payment_transaction', 0, repr(( payment_transaction.showDict() )))
227 228 229 230 231 232 233 234 235

      # fill quantity in lines
      for movement in simulation_line_list :
      
          quantity = movement.getQuantity()
          movement_id = movement.getId()

          payment_transaction_line = getattr(payment_transaction, movement_id, None)
          if payment_transaction_line is None :
236 237
            payment_transaction.newContent(
                portal_type = 'Accounting Transaction Line'
238 239
              , id = movement_id
            )
240
            previous_quantity = 0.0
241
          else :
242
            previous_quantity = payment_transaction_line.getQuantity()
243 244 245 246 247 248 249 250
          if previous_quantity is not None:
            quantity = quantity + previous_quantity
          payment_transaction_line.edit(
              quantity = quantity
            , source = movement.getSource()
            , destination = movement.getDestination()
            , force_update = 1
            )
251

252 253 254
          #LOG('buildPaymentTransactionList movement', 0, repr(( movement.showDict() )))              
          #LOG('buildPaymentTransactionList payment_transaction_line', 0, repr(( payment_transaction_line.showDict() )))              
              
255 256 257 258 259 260 261 262
          # What do we really need to update in the simulation movement ?
          if movement.getPortalType() == 'Simulation Movement' :
            movement._setDeliveryValue(payment_transaction_line)
            reindexable_movement_list.append(movement)

      # we now reindex the movements we modified
      for movement in reindexable_movement_list :
        movement.immediateReindexObject()
263
      return [self]