~unifield-team/unifield-wm/us-826

« back to all changes in this revision

Viewing changes to analytic_distribution_supply/account_commitment.py

UF-358 [ADD] Initial creation : backup of this day

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
# -*- coding: utf-8 -*-
3
 
##############################################################################
4
 
#
5
 
#    OpenERP, Open Source Management Solution
6
 
#    Copyright (C) 2011 TeMPO Consulting, MSF. All Rights Reserved
7
 
#    Developer: Olivier DOSSMANN
8
 
#
9
 
#    This program is free software: you can redistribute it and/or modify
10
 
#    it under the terms of the GNU Affero General Public License as
11
 
#    published by the Free Software Foundation, either version 3 of the
12
 
#    License, or (at your option) any later version.
13
 
#
14
 
#    This program is distributed in the hope that it will be useful,
15
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
16
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17
 
#    GNU Affero General Public License for more details.
18
 
#
19
 
#    You should have received a copy of the GNU Affero General Public License
20
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
21
 
#
22
 
##############################################################################
23
 
 
24
 
from osv import osv
25
 
from osv import fields
26
 
from collections import defaultdict
27
 
 
28
 
class account_commitment(osv.osv):
29
 
    _name = 'account.commitment'
30
 
    _inherit = 'account.commitment'
31
 
 
32
 
    _columns = {
33
 
        'purchase_id': fields.many2one('purchase.order', string="Source document", readonly=True),
34
 
    }
35
 
 
36
 
account_commitment()
37
 
 
38
 
class account_commitment_line(osv.osv):
39
 
    _name = 'account.commitment.line'
40
 
    _inherit = 'account.commitment.line'
41
 
 
42
 
    _columns = {
43
 
        'purchase_order_line_ids': fields.many2many('purchase.order.line', 'purchase_line_commitment_rel', 'commitment_id', 'purchase_id', 
44
 
            string="Purchase Order Lines", readonly=True),
45
 
    }
46
 
 
47
 
    def create_distribution_from_order_line(self, cr, uid, ids, context=None):
48
 
        """
49
 
        Create an analytic distribution regarding those from attached PO lines (if exists).
50
 
        """
51
 
        # Some verifications
52
 
        if not context:
53
 
            context = {}
54
 
        if isinstance(ids, (int, long)):
55
 
            ids = [ids]
56
 
        # Browse commitment lines
57
 
        for line in self.browse(cr, uid, ids, context=context):
58
 
            cc_lines = defaultdict(list) # a dict that permits to show all amount for a specific cost_center (regarding analytic_id from cost_center_line)
59
 
            # browse purchase order lines attached to this commitment line
60
 
            for pol in line.purchase_order_line_ids:
61
 
                # browse cost_center line if an analytic distribution exists for this purchase order line
62
 
                if pol.analytic_distribution_id:
63
 
                    origin_cc_lines = pol.analytic_distribution_id.cost_center_lines
64
 
                # else retrieve CC lines from PO
65
 
                else:
66
 
                    origin_cc_lines = pol.order_id.analytic_distribution_id.cost_center_lines
67
 
                # Compute CC lines amounts
68
 
                for aline in origin_cc_lines:
69
 
                    if aline.analytic_id:
70
 
                        # Compute amount regarding pol subtotal and cost_center_line percentage
71
 
                        amount = (pol.price_subtotal * aline.percentage) / 100
72
 
                        cc_lines[(aline.analytic_id.id, aline.destination_id and aline.destination_id.id or False)].append(amount)
73
 
            # Browse result and create corresponding analytic lines
74
 
            if cc_lines:
75
 
                # create distribution an link it to commitment line
76
 
                distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
77
 
                self.write(cr, uid, [line.id], {'analytic_distribution_id': distrib_id}, context=context)
78
 
                for el in cc_lines:
79
 
                    analytic_id = el[0] or False
80
 
                    vals = {
81
 
                            'distribution_id': distrib_id,
82
 
                            'analytic_id': analytic_id,
83
 
                            'currency_id': line.commit_id.currency_id.id,
84
 
                            'destination_id': el[1] or False,
85
 
                    }
86
 
                    total_amount = 0.0
87
 
                    # fetch total amount
88
 
                    for amount in cc_lines[el]:
89
 
                        total_amount += amount
90
 
                    if not total_amount:
91
 
                        continue
92
 
                    # compute percentage
93
 
                    percentage = (total_amount / line.amount) * 100 or 0.0
94
 
                    vals.update({'percentage': percentage})
95
 
                    # create cost_center_line
96
 
                    distrib_line_id = self.pool.get('cost.center.distribution.line').create(cr, uid, vals, context=context)
97
 
                # Create funding pool lines if needed
98
 
                self.pool.get('analytic.distribution').create_funding_pool_lines(cr, uid, [distrib_id], line.account_id.id, context=context)
99
 
        return True
100
 
 
101
 
account_commitment_line()
102
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: