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

« back to all changes in this revision

Viewing changes to funding_pool/wizard/wizard_distribution.py

  • Committer: Quentin THEURET
  • Date: 2011-12-12 08:02:59 UTC
  • mto: This revision was merged to the branch mainline in revision 724.
  • Revision ID: qt@tempo-consulting.fr-20111212080259-oul1f0g37hcpubyc
UF-641 [ADD] Added the empty purchase_followup module

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) 2011 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
 
 
24
class wizard_distribution(osv.osv_memory):
 
25
    _name="wizard.distribution"
 
26
    
 
27
    _columns = {
 
28
        "total_amount": fields.float("Total amount to be allocated"),
 
29
        "distribution_id": fields.many2one("analytic.distribution", string='Analytic Distribution'),
 
30
        "currency_id": fields.many2one('res.currency', string="Currency"),
 
31
        "invoice_line": fields.many2one("account.invoice.line", "Invoice Line"),
 
32
    }
 
33
 
 
34
    def dummy(self, cr, uid, ids, context={}, *args, **kwargs):
 
35
        mode = self.read(cr, uid, ids, ['entry_mode'])[0]['entry_mode']
 
36
        self.write(cr, uid, [ids[0]], {'entry_mode': mode=='percent' and 'amount' or 'percent'})
 
37
        return {
 
38
                'type': 'ir.actions.act_window',
 
39
                'res_model': self._name,
 
40
                'view_type': 'form',
 
41
                'view_mode': 'form',
 
42
                'target': 'new',
 
43
                'res_id': ids[0],
 
44
                'context': context,
 
45
        }
 
46
 
 
47
    def store_distribution(self, cr, uid, wizard_id, date=False, source_date=False, context=None):
 
48
        wizard_obj = self.browse(cr, uid, wizard_id, context=context)
 
49
        distrib_obj = self.pool.get('analytic.distribution')
 
50
        distrib_id = wizard_obj.distribution_id.id
 
51
        distrib_obj.write(cr, uid, [distrib_id], vals={'global_distribution': False}, context=context)
 
52
        distrib = distrib_obj.browse(cr, uid, distrib_id, context=context)
 
53
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
 
54
        if 'wizard_ids' in context:
 
55
            if 'cost_center' in context['wizard_ids']:
 
56
                cc_wizard_obj = self.pool.get('wizard.costcenter.distribution').browse(cr, uid, context['wizard_ids']['cost_center'], context=context)
 
57
                cc_distrib_line_obj = self.pool.get('cost.center.distribution.line')
 
58
                # remove old lines
 
59
                for cc_distrib_line in distrib.cost_center_lines:
 
60
                    cc_distrib_line_obj.unlink(cr, uid, cc_distrib_line.id)
 
61
                # and save the new lines in the distribution
 
62
                for cc_wizard_line in cc_wizard_obj.wizard_distribution_lines:
 
63
                    distrib_line_vals = {
 
64
                        'name': cc_wizard_line.name,
 
65
                        'analytic_id': cc_wizard_line.analytic_id.id,
 
66
                        'amount': cc_wizard_line.amount,
 
67
                        'percentage': cc_wizard_line.percentage,
 
68
                        'distribution_id': distrib_id,
 
69
                        'currency_id': wizard_obj.currency_id.id,
 
70
                    }
 
71
                    if source_date:
 
72
                        distrib_line_vals.update({'source_date': source_date})
 
73
                    if date:
 
74
                        distrib_line_vals.update({'date': date})
 
75
                    cc_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
 
76
            if 'funding_pool' in context['wizard_ids']:
 
77
                fp_wizard_obj = self.pool.get('wizard.fundingpool.distribution').browse(cr, uid, context['wizard_ids']['funding_pool'], context=context)
 
78
                fp_distrib_line_obj = self.pool.get('funding.pool.distribution.line')
 
79
                # remove old lines
 
80
                for fp_distrib_line in distrib.funding_pool_lines:
 
81
                    fp_distrib_line_obj.unlink(cr, uid, fp_distrib_line.id)
 
82
                # and save the new lines in the distribution
 
83
                for fp_wizard_line in fp_wizard_obj.wizard_distribution_lines:
 
84
                    distrib_line_vals = {
 
85
                        'name': fp_wizard_line.name,
 
86
                        'analytic_id': fp_wizard_line.analytic_id.id,
 
87
                        'cost_center_id': fp_wizard_line.cost_center_id.id,
 
88
                        'amount': fp_wizard_line.amount,
 
89
                        'percentage': fp_wizard_line.percentage,
 
90
                        'distribution_id': distrib_id,
 
91
                        'currency_id': wizard_obj.currency_id.id,
 
92
                    }
 
93
                    if source_date:
 
94
                        distrib_line_vals.update({'source_date': source_date})
 
95
                    if date:
 
96
                        distrib_line_vals.update({'date': date})
 
97
                    fp_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
 
98
            if 'free_1' in context['wizard_ids']:
 
99
                f1_wizard_obj = self.pool.get('wizard.free1.distribution').browse(cr, uid, context['wizard_ids']['free_1'], context=context)
 
100
                f1_distrib_line_obj = self.pool.get('free.1.distribution.line')
 
101
                # remove old lines
 
102
                for f1_distrib_line in distrib.free_1_lines:
 
103
                    f1_distrib_line_obj.unlink(cr, uid, f1_distrib_line.id)
 
104
                # and save the new lines in the distribution
 
105
                for f1_wizard_line in f1_wizard_obj.wizard_distribution_lines:
 
106
                    distrib_line_vals = {
 
107
                        'name': f1_wizard_line.name,
 
108
                        'analytic_id': f1_wizard_line.analytic_id.id,
 
109
                        'amount': f1_wizard_line.amount,
 
110
                        'percentage': f1_wizard_line.percentage,
 
111
                        'distribution_id': distrib_id,
 
112
                        'currency_id': wizard_obj.currency_id.id,
 
113
                    }
 
114
                    if source_date:
 
115
                        distrib_line_vals.update({'source_date': source_date})
 
116
                    if date:
 
117
                        distrib_line_vals.update({'date': date})
 
118
                    f1_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
 
119
            if 'free_2' in context['wizard_ids']:
 
120
                f2_wizard_obj = self.pool.get('wizard.free2.distribution').browse(cr, uid, context['wizard_ids']['free_2'], context=context)
 
121
                f2_distrib_line_obj = self.pool.get('free.2.distribution.line')
 
122
                # remove old lines
 
123
                for f2_distrib_line in distrib.free_2_lines:
 
124
                    f2_distrib_line_obj.unlink(cr, uid, f2_distrib_line.id)
 
125
                # and save the new lines in the distribution
 
126
                for f2_wizard_line in f2_wizard_obj.wizard_distribution_lines:
 
127
                    distrib_line_vals = {
 
128
                        'name': f2_wizard_line.name,
 
129
                        'analytic_id': f2_wizard_line.analytic_id.id,
 
130
                        'amount': f2_wizard_line.amount,
 
131
                        'percentage': f2_wizard_line.percentage,
 
132
                        'distribution_id': distrib_id,
 
133
                        'currency_id': wizard_obj.currency_id.id,
 
134
                    }
 
135
                    if source_date:
 
136
                        distrib_line_vals.update({'source_date': source_date})
 
137
                    if date:
 
138
                        distrib_line_vals.update({'date': date})
 
139
                    f2_distrib_line_obj.create(cr, uid, distrib_line_vals, context=context)
 
140
            # if there are child distributions, we refresh them
 
141
            if 'child_distributions' in context and context['child_distributions']:
 
142
                for child in context['child_distributions']:
 
143
                    distrib_obj.copy_from_global_distribution(cr, uid, distrib_id, child[0], child[1], wizard_obj.currency_id.id, context=context)
 
144
        return
 
145
 
 
146
    def update_analytic_lines(self, cr, uid, ids, context={}):
 
147
        """
 
148
        Update analytic lines with an ugly method: delete old lines and create new ones
 
149
        """
 
150
        if not context:
 
151
            context = {}
 
152
        if isinstance(ids, (int, long)):
 
153
            ids = [ids]
 
154
        # Process all given wizards
 
155
        for wizard in self.browse(cr, uid, ids, context=context):
 
156
            # Prepare some values
 
157
            distrib = wizard.distribution_id or False
 
158
            move_lines = [x.id for x in distrib.move_line_ids]
 
159
            aal_obj = self.pool.get('account.analytic.line')
 
160
            ml_obj = self.pool.get('account.move.line')
 
161
            if not distrib:
 
162
                return False
 
163
            # Search account analytic lines attached to this move lines
 
164
            operator = 'in'
 
165
            if len(move_lines) == 1:
 
166
                operator = '='
 
167
            aal_ids = aal_obj.search(cr, uid, [('move_id', operator, move_lines)], context=context)
 
168
            if aal_ids:
 
169
                # delete old analytic lines
 
170
                aal_obj.unlink(cr, uid, aal_ids, context=context)
 
171
                # create new analytic lines
 
172
                ml_obj.create_analytic_lines(cr, uid, move_lines, context=context)
 
173
 
 
174
            if not move_lines and wizard.invoice_line:
 
175
                wizard.invoice_line.create_engagement_lines()
 
176
        return True
 
177
 
 
178
wizard_distribution()
 
179
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: