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

« back to all changes in this revision

Viewing changes to analytic_distribution_purchase/purchase.py

  • Committer: chloups208
  • Date: 2011-05-03 13:42:32 UTC
  • mto: (140.1.1 unifield-wm)
  • mto: This revision was merged to the branch mainline in revision 142.
  • Revision ID: chloups208@chloups208-laptop-20110503134232-omqmqbkrr8f3detr
[UF-33] correction of asset which is now linked to stock.move in a wizard when processing the stock.picking

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
2
 
#-*- encoding: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
 
 
27
 
class purchase_order(osv.osv):
28
 
    _name = 'purchase.order'
29
 
    _inherit = 'purchase.order'
30
 
 
31
 
    _columns = {
32
 
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
33
 
    }
34
 
 
35
 
    def button_analytic_distribution(self, cr, uid, ids, context={}):
36
 
        """
37
 
        Launch analytic distribution wizard on a purchase order
38
 
        """
39
 
        # Some verifications
40
 
        if not context:
41
 
            context = {}
42
 
        if isinstance(ids, (int, long)):
43
 
            ids = [ids]
44
 
        # Prepare some values
45
 
        ana_obj = self.pool.get('analytic.distribution')
46
 
        purchase = self.browse(cr, uid, ids[0], context=context)
47
 
        amount = purchase.amount_total or 0.0
48
 
        child_distributions = []
49
 
        # Get analytic_distribution_id
50
 
        distrib_id = purchase.analytic_distribution_id and purchase.analytic_distribution_id.id
51
 
        # Create an analytic_distribution_id if no one exists
52
 
        if not distrib_id:
53
 
            res_id = ana_obj.create(cr, uid, {}, context=context)
54
 
            super(purchase_order, self).write(cr, uid, ids, {'analytic_distribution_id': res_id}, context=context)
55
 
            distrib_id = res_id
56
 
        # Search analytic distribution to renew if necessary
57
 
        for pl in purchase.order_line:
58
 
            pl_amount = pl.price_subtotal or 0.0
59
 
            if pl.analytic_distribution_id:
60
 
                if pl.analytic_distribution_id.global_distribution or context.get('reset_all', False):
61
 
                    child_distributions.append((pl.analytic_distribution_id.id, pl_amount))
62
 
            else:
63
 
                child_distrib_id = ana_obj.create(cr, uid, {'global_distribution': True}, context=context)
64
 
                self.pool.get('purchase.order.line').write(cr, uid, [pl.id], {'analytic_distribution_id': child_distrib_id}, context=context)
65
 
                child_distributions.append((child_distrib_id, pl_amount))
66
 
        # Create the wizard
67
 
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
68
 
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id}, context=context)
69
 
        # Update some context values
70
 
        context.update({
71
 
            'active_id': ids[0],
72
 
            'active_ids': ids,
73
 
            'wizard_ids': {'cost_center': wiz_id},
74
 
            'child_distributions': child_distributions
75
 
        })
76
 
        # Open it!
77
 
        return {
78
 
                'type': 'ir.actions.act_window',
79
 
                'res_model': 'wizard.costcenter.distribution',
80
 
                'view_type': 'form',
81
 
                'view_mode': 'form',
82
 
                'target': 'new',
83
 
                'res_id': [wiz_id],
84
 
                'context': context,
85
 
        }
86
 
 
87
 
purchase_order()
88
 
 
89
 
class purchase_order_line(osv.osv):
90
 
    _name = 'purchase.order.line'
91
 
    _inherit = 'purchase.order.line'
92
 
 
93
 
    _columns = {
94
 
        'analytic_distribution_id': fields.many2one('analytic.distribution', string="Analytic Distribution"),
95
 
    }
96
 
 
97
 
    def button_analytic_distribution(self, cr, uid, ids, context={}):
98
 
        """
99
 
        Launch the analytic distribution wizard on the first given id (from ids)
100
 
        """
101
 
        # Some verifications
102
 
        if not context:
103
 
            context = {}
104
 
        if isinstance(ids, (int, long)):
105
 
            ids = [ids]
106
 
        # Prepare some values
107
 
        pl = self.browse(cr, uid, ids[0], context=context) # purchase line
108
 
        amount = pl.price_subtotal or 0.0
109
 
        child_distributions = []
110
 
        # Search elements for currency
111
 
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
112
 
        currency = pl.order_id.currency_id and pl.order_id.currency_id.id or company_currency
113
 
        # Get analytic distribution
114
 
        distrib_id = pl.analytic_distribution_id and pl.analytic_distribution_id.id or False
115
 
        if not distrib_id:
116
 
            raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution for the whole purchase order first!"))
117
 
        # Create the wizard
118
 
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
119
 
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
120
 
        # Add some values in context
121
 
        context.update({
122
 
            'active_id': ids[0],
123
 
            'active_ids': ids,
124
 
            'wizard_ids': {'cost_center': wiz_id},
125
 
            'child_distributions': child_distributions
126
 
        })
127
 
        # Open it!
128
 
        return {
129
 
                'type': 'ir.actions.act_window',
130
 
                'res_model': 'wizard.costcenter.distribution',
131
 
                'view_type': 'form',
132
 
                'view_mode': 'form',
133
 
                'target': 'new',
134
 
                'res_id': [wiz_id],
135
 
                'context': context,
136
 
        }
137
 
 
138
 
    def create(self, cr, uid, vals, context={}):
139
 
        """
140
 
        Link analytic distribution on purchase order line after its creation
141
 
        """
142
 
        if not context:
143
 
            context = {}
144
 
        if not vals:
145
 
            vals = {}
146
 
        if vals.get('order_id', False):
147
 
            # Search global distribution (those from purchase order)
148
 
            po = self.pool.get('purchase.order').browse(cr, uid, vals.get('order_id'), context=context)
149
 
            if po.analytic_distribution_id:
150
 
                # Create a new global analytic distribution
151
 
                ana_obj = self.pool.get('analytic.distribution')
152
 
                child_distrib_id = ana_obj.create(cr, uid, {'global_distribution': True}, context=context)
153
 
                vals.update({'analytic_distribution_id': child_distrib_id,})
154
 
                res = super(purchase_order_line, self).create(cr, uid, vals, context=context)
155
 
                total_amount = self._amount_line(cr, uid, [res], None, None, context=context)[res]
156
 
                amount = total_amount or 0.0
157
 
                # Search currency (by default those of company)
158
 
                company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
159
 
                currency = po.currency_id and po.currency_id.id or company_currency
160
 
                ana_obj.copy_from_global_distribution(cr, uid, po.analytic_distribution_id.id, child_distrib_id, amount, currency, context=context)
161
 
                return res
162
 
        return super(purchase_order_line, self).create(cr, uid, vals, context=context)
163
 
 
164
 
    def write(self, cr, uid, ids, vals, context={}):
165
 
        """
166
 
        Update analytic lines if an analytic distribution exists
167
 
        """
168
 
        # Some verifications
169
 
        if not context:
170
 
            context = {}
171
 
        if isinstance(ids, (int, long)):
172
 
            ids = [ids]
173
 
        # Write first new purchase order line
174
 
        res = super(purchase_order_line, self).write(cr, uid, ids, vals, context=context)
175
 
        for pl in self.browse(cr, uid, ids, context=context):
176
 
            # do something only if analytic_distribution_id field is filled in
177
 
            if pl.order_id.analytic_distribution_id:
178
 
                # prepare some values
179
 
                po_distrib = pl.order_id.analytic_distribution_id
180
 
                company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
181
 
                currency = pl.order_id.currency_id and pl.order_id.currency_id.id or company_currency
182
 
                if 'price_unit' in vals or 'product_qty' in vals or context.get('reset_all', False) or pl.analytic_distribution_id.global_distribution:
183
 
                    amount = pl.price_subtotal or 0.0
184
 
                    self.pool.get('analytic.distribution').copy_from_global_distribution(cr, uid, po_distrib.id, pl.analytic_distribution_id.id, amount, currency, context=context)
185
 
        return res
186
 
 
187
 
purchase_order_line()
188
 
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: