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

« back to all changes in this revision

Viewing changes to analytic_distribution/invoice.py

UF-368 [MERGE] Specific Stock Locations

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 osv, fields
23
 
from tools.translate import _
24
 
 
25
 
class account_invoice(osv.osv):
26
 
    _name = 'account.invoice'
27
 
    _inherit = 'account.invoice'
28
 
 
29
 
    _columns = {
30
 
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
31
 
    }
32
 
 
33
 
    def line_get_convert(self, cr, uid, x, part, date, context=None):
34
 
        res = super(account_invoice, self).line_get_convert(cr, uid, x, part, date, context=context)
35
 
        res['analytic_distribution_id'] = x.get('analytic_distribution_id', False)
36
 
        return res
37
 
 
38
 
    def button_analytic_distribution_from_direct_inv(self, cr, uid, ids, context=None):
39
 
        if not context:
40
 
            context = {}
41
 
        st_line_id = self.read(cr, uid, context['active_id'], ['register_line_ids'])
42
 
        context['from_direct_inv'] = st_line_id.get('register_line_ids') and st_line_id['register_line_ids'][0] 
43
 
        return self.button_analytic_distribution(cr, uid, ids, context)
44
 
    
45
 
 
46
 
    def button_analytic_distribution(self, cr, uid, ids, context=None):
47
 
        """
48
 
        Launch analytic distribution wizard on an invoice
49
 
        """
50
 
        # Some verifications
51
 
        if not context:
52
 
            context = {}
53
 
        if isinstance(ids, (int, long)):
54
 
            ids = [ids]
55
 
        # Prepare some values
56
 
        invoice = self.browse(cr, uid, ids[0], context=context)
57
 
        amount = 0.0
58
 
        # Search elements for currency
59
 
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
60
 
        currency = invoice.currency_id and invoice.currency_id.id or company_currency
61
 
        for line in invoice.invoice_line:
62
 
            amount += line.price_subtotal
63
 
        # Get analytic_distribution_id
64
 
        distrib_id = invoice.analytic_distribution_id and invoice.analytic_distribution_id.id
65
 
        # Prepare values for wizard
66
 
        vals = {
67
 
            'total_amount': amount,
68
 
            'invoice_id': invoice.id,
69
 
            'currency_id': currency or False,
70
 
            'state': 'dispatch',
71
 
            'posting_date': invoice.date_invoice,
72
 
            'document_date': invoice.document_date,
73
 
        }
74
 
        if context.get('from_direct_inv'):
75
 
            vals['from_direct_inv'] = context['from_direct_inv']
76
 
        if distrib_id:
77
 
            vals.update({'distribution_id': distrib_id,})
78
 
        # Create the wizard
79
 
        wiz_obj = self.pool.get('analytic.distribution.wizard')
80
 
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
81
 
        # Update some context values
82
 
        context.update({
83
 
            'active_id': ids[0],
84
 
            'active_ids': ids,
85
 
        })
86
 
        # Open it!
87
 
        return {
88
 
                'name': _('Global analytic distribution'),
89
 
                'type': 'ir.actions.act_window',
90
 
                'res_model': 'analytic.distribution.wizard',
91
 
                'view_type': 'form',
92
 
                'view_mode': 'form',
93
 
                'target': 'new',
94
 
                'res_id': [wiz_id],
95
 
                'context': context,
96
 
        }
97
 
 
98
 
account_invoice()
99
 
 
100
 
class account_invoice_line(osv.osv):
101
 
    _name = 'account.invoice.line'
102
 
    _inherit = 'account.invoice.line'
103
 
 
104
 
    _columns = {
105
 
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
106
 
    }
107
 
    
108
 
    def button_analytic_distribution_from_direct_inv_line(self, cr, uid, ids, context=None):
109
 
        if not context:
110
 
            context = {}
111
 
        acc_inv_line = self.browse(cr, uid, context['active_id'])
112
 
        context['from_direct_inv'] = acc_inv_line.invoice_id and acc_inv_line.invoice_id.register_line_ids and acc_inv_line.invoice_id.register_line_ids[0].id
113
 
        return self.button_analytic_distribution(cr, uid, ids, context)
114
 
 
115
 
    def button_analytic_distribution(self, cr, uid, ids, context=None):
116
 
        """
117
 
        Launch analytic distribution wizard on an invoice line
118
 
        """
119
 
        # Some verifications
120
 
        if not context:
121
 
            context = {}
122
 
        if isinstance(ids, (int, long)):
123
 
            ids = [ids]
124
 
        if not ids:
125
 
            raise osv.except_osv(_('Error'), _('No invoice line given. Please save your invoice line before.'))
126
 
        # Prepare some values
127
 
        invoice_line = self.browse(cr, uid, ids[0], context=context)
128
 
        distrib_id = False
129
 
        negative_inv = False
130
 
        amount = invoice_line.price_subtotal or 0.0
131
 
        # Search elements for currency
132
 
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
133
 
        currency = invoice_line.invoice_id.currency_id and invoice_line.invoice_id.currency_id.id or company_currency
134
 
        # Change amount sign if necessary
135
 
        if invoice_line.invoice_id.type in ['out_invoice', 'in_refund']:
136
 
            negative_inv = True
137
 
        if negative_inv:
138
 
            amount = -1 * amount
139
 
        # Get analytic distribution id from this line
140
 
        distrib_id = invoice_line and invoice_line.analytic_distribution_id and invoice_line.analytic_distribution_id.id or False
141
 
        # Prepare values for wizard
142
 
        vals = {
143
 
            'total_amount': amount,
144
 
            'invoice_line_id': invoice_line.id,
145
 
            'currency_id': currency or False,
146
 
            'state': 'dispatch',
147
 
            'account_id': invoice_line.account_id and invoice_line.account_id.id or False,
148
 
            'posting_date': invoice_line.invoice_id.date_invoice,
149
 
            'document_date': invoice_line.invoice_id.document_date,
150
 
        }
151
 
        if context.get('from_direct_inv'):
152
 
            vals['from_direct_inv'] = context['from_direct_inv']
153
 
        if distrib_id:
154
 
            vals.update({'distribution_id': distrib_id,})
155
 
        # Create the wizard
156
 
        wiz_obj = self.pool.get('analytic.distribution.wizard')
157
 
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
158
 
        # Update some context values
159
 
        context.update({
160
 
            'active_id': ids[0],
161
 
            'active_ids': ids,
162
 
        })
163
 
        # Open it!
164
 
        return {
165
 
                'name': _('Analytic distribution'),
166
 
                'type': 'ir.actions.act_window',
167
 
                'res_model': 'analytic.distribution.wizard',
168
 
                'view_type': 'form',
169
 
                'view_mode': 'form',
170
 
                'target': 'new',
171
 
                'res_id': [wiz_id],
172
 
                'context': context,
173
 
        }
174
 
 
175
 
    def move_line_get_item(self, cr, uid, line, context=None):
176
 
        """
177
 
        Give right analytic distribution when creating move lines
178
 
        """
179
 
        # Some verifications
180
 
        if not context:
181
 
            context = {}
182
 
        # Default result
183
 
        res = super(account_invoice_line, self).move_line_get_item(cr, uid, line, context=context)
184
 
        # Update result by copying analytic distribution from invoice line
185
 
        ana_obj = self.pool.get('analytic.distribution')
186
 
        if line.analytic_distribution_id:
187
 
            new_distrib_id = ana_obj.copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
188
 
            if new_distrib_id:
189
 
                res['analytic_distribution_id'] = new_distrib_id
190
 
        # If no distribution on invoice line, take those from invoice and copy it!
191
 
        elif line.invoice_id and line.invoice_id.analytic_distribution_id:
192
 
            new_distrib_id = ana_obj.copy(cr, uid, line.invoice_id.analytic_distribution_id.id, {}, context=context)
193
 
            if new_distrib_id:
194
 
                res['analytic_distribution_id'] = new_distrib_id
195
 
        return res
196
 
 
197
 
account_invoice_line()