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

« back to all changes in this revision

Viewing changes to funding_pool/funding_pool.py

UF-558 [ADD] Add account_id on analytic distribution wizard to add a constraint on Funding Pool

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# -*- coding: utf-8 -*-
2
 
##############################################################################
3
 
#
4
 
#    OpenERP, Open Source Management Solution
5
 
#    Copyright (C) MSF, TeMPO Consulting.
6
 
#
7
 
#    This program is free software: you can redistribute it and/or modify
8
 
#    it under the terms of the GNU Affero General Public License as
9
 
#    published by the Free Software Foundation, either version 3 of the
10
 
#    License, or (at your option) any later version.
11
 
#
12
 
#    This program is distributed in the hope that it will be useful,
13
 
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 
#    GNU Affero General Public License for more details.
16
 
#
17
 
#    You should have received a copy of the GNU Affero General Public License
18
 
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19
 
#
20
 
##############################################################################
21
 
 
22
 
from osv import fields, osv
23
 
import decimal_precision as dp
24
 
from tools.misc import flatten
25
 
 
26
 
class analytic_distribution(osv.osv):
27
 
    _name = "analytic.distribution"
28
 
 
29
 
    _columns = {
30
 
        'name': fields.char('Name', size=12, required=True),
31
 
        'global_distribution': fields.boolean('Is this distribution copied from the global distribution'),
32
 
        'analytic_lines': fields.one2many('account.analytic.line', 'distribution_id', 'Analytic Lines'),
33
 
        'invoice_ids': fields.one2many('account.invoice', 'analytic_distribution_id', string="Invoices"),
34
 
        'invoice_line_ids': fields.one2many('account.invoice.line', 'analytic_distribution_id', string="Invoice Lines"),
35
 
        'register_line_ids': fields.one2many('account.bank.statement.line', 'analytic_distribution_id', string="Register Lines"),
36
 
        'move_line_ids': fields.one2many('account.move.line', 'analytic_distribution_id', string="Move Lines"),
37
 
    }
38
 
 
39
 
    _defaults ={
40
 
        'name': lambda *a: 'Distribution',
41
 
        'global_distribution': lambda *a: False,
42
 
    }
43
 
 
44
 
analytic_distribution()
45
 
 
46
 
class distribution_line(osv.osv):
47
 
    _name = "distribution.line"
48
 
 
49
 
    _columns = {
50
 
        'name': fields.char('Name', size=64, required=True),
51
 
        "distribution_id": fields.many2one('analytic.distribution', 'Associated Analytic Distribution', ondelete='cascade'),
52
 
        "analytic_id": fields.many2one('account.analytic.account', 'Analytical Account'),
53
 
        "amount": fields.float('Amount', required=True),
54
 
        "percentage": fields.float('Percentage'),
55
 
        "currency_id": fields.many2one('res.currency', 'Currency', required=True),
56
 
        "date": fields.date(string="Date"),
57
 
        "source_date": fields.date(string="Source Date", help="This date is for source_date for analytic lines"),
58
 
    }
59
 
 
60
 
    _defaults ={
61
 
        'name': 'Distribution Line',
62
 
    }
63
 
 
64
 
distribution_line()
65
 
 
66
 
class cost_center_distribution_line(osv.osv):
67
 
    _name = "cost.center.distribution.line"
68
 
    _inherit = "distribution.line"
69
 
    
70
 
cost_center_distribution_line()
71
 
 
72
 
class funding_pool_distribution_line(osv.osv):
73
 
    _name = "funding.pool.distribution.line"
74
 
    _inherit = "distribution.line"
75
 
    _columns = {
76
 
        "cost_center_id": fields.many2one('account.analytic.account', 'Cost Center Account'),
77
 
    }
78
 
    
79
 
funding_pool_distribution_line()
80
 
 
81
 
class free_1_distribution_line(osv.osv):
82
 
    _name = "free.1.distribution.line"
83
 
    _inherit = "distribution.line"
84
 
 
85
 
free_1_distribution_line()
86
 
 
87
 
class free_2_distribution_line(osv.osv):
88
 
    _name = "free.2.distribution.line"
89
 
    _inherit = "distribution.line"
90
 
 
91
 
free_2_distribution_line()
92
 
 
93
 
class analytic_distribution(osv.osv):
94
 
    _inherit = "analytic.distribution"
95
 
 
96
 
    _columns = {
97
 
        'cost_center_lines': fields.one2many('cost.center.distribution.line', 'distribution_id', 'Cost Center Distribution'),
98
 
        'funding_pool_lines': fields.one2many('funding.pool.distribution.line', 'distribution_id', 'Funding Pool Distribution'),
99
 
        'free_1_lines': fields.one2many('free.1.distribution.line', 'distribution_id', 'Free 1 Distribution'),
100
 
        'free_2_lines': fields.one2many('free.2.distribution.line', 'distribution_id', 'Free 2 Distribution'),
101
 
    }
102
 
    
103
 
    def copy_from_global_distribution(self, cr, uid, source_id, destination_id, destination_amount, destination_currency, context={}):
104
 
        cc_distrib_line_obj = self.pool.get('cost.center.distribution.line')
105
 
        fp_distrib_line_obj = self.pool.get('funding.pool.distribution.line')
106
 
        f1_distrib_line_obj = self.pool.get('free.1.distribution.line')
107
 
        f2_distrib_line_obj = self.pool.get('free.2.distribution.line')
108
 
        source_obj = self.browse(cr, uid, source_id, context=context)
109
 
        destination_obj = self.browse(cr, uid, destination_id, context=context)
110
 
        # clean up
111
 
        for cost_center_line in destination_obj.cost_center_lines:
112
 
            cc_distrib_line_obj.unlink(cr, uid, cost_center_line.id)
113
 
        for funding_pool_line in destination_obj.funding_pool_lines:
114
 
            fp_distrib_line_obj.unlink(cr, uid, funding_pool_line.id)
115
 
        for free_1_line in destination_obj.free_1_lines:
116
 
            f1_distrib_line_obj.unlink(cr, uid, free_1_line.id)
117
 
        for free_2_line in destination_obj.free_2_lines:
118
 
            f2_distrib_line_obj.unlink(cr, uid, free_2_line.id)
119
 
        # add values
120
 
        vals = {}
121
 
        vals['name'] = source_obj.name
122
 
        vals['global_distribution'] = True
123
 
        for source_cost_center_line in source_obj.cost_center_lines:
124
 
            distrib_line_vals = {
125
 
                'name': source_cost_center_line.name,
126
 
                'analytic_id': source_cost_center_line.analytic_id.id,
127
 
                'percentage': source_cost_center_line.percentage,
128
 
                'amount': round(source_cost_center_line.percentage * destination_amount) / 100.0,
129
 
                'distribution_id': destination_id,
130
 
                'currency_id': destination_currency,
131
 
                'date': source_cost_center_line.date or False,
132
 
                'source_date': source_cost_center_line.source_date or False,
133
 
            }
134
 
            cc_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
135
 
        for source_funding_pool_line in source_obj.funding_pool_lines:
136
 
            distrib_line_vals = {
137
 
                'name': source_funding_pool_line.name,
138
 
                'analytic_id': source_funding_pool_line.analytic_id.id,
139
 
                'cost_center_id': source_funding_pool_line.cost_center_id.id,
140
 
                'percentage': source_funding_pool_line.percentage,
141
 
                'amount': round(source_funding_pool_line.percentage * destination_amount) / 100.0,
142
 
                'distribution_id': destination_id,
143
 
                'currency_id': destination_currency,
144
 
                'date': source_funding_pool_line.date or False,
145
 
                'source_date': source_funding_pool_line.source_date or False,
146
 
            }
147
 
            fp_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
148
 
        for source_free_1_line in source_obj.free_1_lines:
149
 
            distrib_line_vals = {
150
 
                'name': source_free_1_line.name,
151
 
                'analytic_id': source_free_1_line.analytic_id.id,
152
 
                'percentage': source_free_1_line.percentage,
153
 
                'amount': round(source_free_1_line.percentage * destination_amount) / 100.0,
154
 
                'distribution_id': destination_id,
155
 
                'currency_id': destination_currency,
156
 
                'date': source_free_1_line.date or False,
157
 
                'source_date': source_free_1_line.source_date or False,
158
 
            }
159
 
            f1_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
160
 
        for source_free_2_line in source_obj.free_2_lines:
161
 
            distrib_line_vals = {
162
 
                'name': source_free_2_line.name,
163
 
                'analytic_id': source_free_2_line.analytic_id.id,
164
 
                'percentage': source_free_2_line.percentage,
165
 
                'amount': round(source_free_2_line.percentage * destination_amount) / 100.0,
166
 
                'distribution_id': destination_id,
167
 
                'currency_id': destination_currency,
168
 
                'date': source_free_2_line.date or False,
169
 
                'source_date': source_free_2_line.source_date or False,
170
 
            }
171
 
            f2_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
172
 
        if destination_obj.invoice_line_ids:
173
 
            self.pool.get('account.invoice.line').create_engagement_lines(cr, uid, [x.id for x in destination_obj.invoice_line_ids])
174
 
        return super(analytic_distribution, self).write(cr, uid, [destination_id], vals, context=context)
175
 
 
176
 
    def update_distribution_line_amount(self, cr, uid, ids, amount=False, context={}):
177
 
        """
178
 
        Update amount on distribution lines for given distribution (ids)
179
 
        """
180
 
        # Some verifications
181
 
        if not context:
182
 
            context = {}
183
 
        if isinstance(ids, (int, long)):
184
 
            ids = [ids]
185
 
        if not amount:
186
 
            return False
187
 
        # Process distributions
188
 
        for distrib_id in ids:
189
 
            for dl_name in ['cost.center.distribution.line', 'funding.pool.distribution.line', 'free.1.distribution.line', 'free.2.distribution.line']:
190
 
                dl_obj = self.pool.get(dl_name)
191
 
                dl_ids = dl_obj.search(cr, uid, [('distribution_id', '=', distrib_id)], context=context)
192
 
                for dl in dl_obj.browse(cr, uid, dl_ids, context=context):
193
 
                    dl_vals = {
194
 
                        'amount': round(dl.percentage * amount) / 100.0,
195
 
                    }
196
 
                    dl_obj.write(cr, uid, [dl.id], dl_vals, context=context)
197
 
        return True
198
 
 
199
 
    def copy(self, cr, uid, id, defaults={}, context={}):
200
 
        """
201
 
        Copy an analytic distribution without the one2many links
202
 
        """
203
 
        if not context:
204
 
            context = {}
205
 
        defaults.update({
206
 
            'analytic_lines': False,
207
 
            'invoice_ids': False,
208
 
            'invoice_line_ids': False,
209
 
            'register_line_ids': False,
210
 
            'move_line_ids': False,
211
 
        })
212
 
        return super(osv.osv, self).copy(cr, uid, id, defaults, context=context)
213
 
 
214
 
analytic_distribution()
215
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: