~savoirfairelinux-openerp/purchase-wkfl/pallet-delivery-optimized

1
2
3
4
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
31
32
33
34
35
36
37
38
39
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
# -*- encoding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2013 Savoir-faire Linux
#    (<http://www.savoirfairelinux.com>).
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU Affero General Public License as
#    published by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
#
#    You should have received a copy of the GNU Affero General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################

from openerp.osv import orm, fields


class account_analytic_account(orm.Model):
    _inherit = 'account.analytic.account'

    def _estimated_tcu(self, cr, uid, ids, name, arg, context=None):
        # Pools
        stock_move_pool = self.pool.get('stock.move')
        po_line_pool = self.pool.get('purchase.order.line')

        def transformed_tcu():

            # Get the stock move id of the transformed product
            stock_move_ids = stock_move_pool.search(cr, uid, [('prodlot_id.name', '=', line.code)], context=context)
            if not stock_move_ids:
                return 0
            stock_move = stock_move_pool.browse(cr, uid, stock_move_ids[0], context=context)

            # Get the stock move id of the initial product
            # We can safely assume there is only one
            if not stock_move.production_id:
                return 0
            initial_stock_move = stock_move.production_id.move_lines2[0]
            initial_analytic_id = initial_stock_move.prodlot_id.account_analytic_id.id

            """Get parent_po_line and calculate price based on transformation"""
            parent_po_line_ids = po_line_pool.search(cr, uid, [('account_analytic_id', '=', initial_analytic_id)], context=context)
            if not parent_po_line_ids:
                return 0
            parent_po_line = po_line_pool.browse(cr, uid, parent_po_line_ids[0], context=context)

            # Calculate transformed price
            return parent_po_line.landed_costs / (parent_po_line.product_qty * stock_move.production_id.bom_id.product_qty)

        # Call super
        res = super(account_analytic_account, self)._estimated_tcu(cr, uid, ids, name, arg, context)
        for line in self.browse(cr, uid, ids):
            if line.code.startswith('LOT') and not po_line_pool.search(cr, uid, [('account_analytic_id', '=', line.id)]):
                res[line.id] = transformed_tcu()

        return res

    # Overwrite estimated_tcu
    _columns = {
        'estimated_tcu': fields.function(_estimated_tcu, string='Estimated Total Cost per Unit', type='float'),
    }

# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: