~unifield-team/unifield-wm/us-671-homere

« back to all changes in this revision

Viewing changes to analytic_distribution_purchase/purchase.py

UF-338 [MERGE] Accounting Corrections

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 analaytic_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
            amount = pl.price_subtotal or 0.0
 
59
            if pl.analaytic_distribution_id:
 
60
                if pl.analaytic_distribution_id.global_distribution or context.get('reset_all', False):
 
61
                    child_distributions.append((pl.analaytic_distribution_id.id, 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, 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.analaytic_distribution_id and pl.analaytic_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}, 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
purchase_order_line()
 
139
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: