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

« back to all changes in this revision

Viewing changes to analytic_distribution_invoice/invoice.py

  • Committer: jf
  • Date: 2011-12-30 09:08:24 UTC
  • mfrom: (395.8.8 UF-661)
  • Revision ID: jf@tempo4-20111230090824-4zbrgeyqqu4z4so3
UF-661 [MERGE] Financing contracts: Export CSV File

Show diffs side-by-side

added added

removed removed

Lines of Context:
19
19
#
20
20
##############################################################################
21
21
 
22
 
from osv import osv, fields
 
22
from osv import osv
 
23
from osv import fields
 
24
from tools.translate import _
23
25
 
24
26
class account_invoice(osv.osv):
25
27
    _name = 'account.invoice'
26
28
    _inherit = 'account.invoice'
27
29
 
28
 
    def _have_analytic_distribution(self, cr, uid, ids, name, arg, context={}):
 
30
    def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
29
31
        """
30
 
        If invoice have an analytic distribution, return True, else return False
 
32
        Return analytic distribution line count (given by analytic distribution)
31
33
        """
32
34
        # Some verifications
33
35
        if not context:
34
36
            context = {}
35
37
        if isinstance(ids, (int, long)):
36
38
            ids = [ids]
 
39
        # Prepare some values
37
40
        res = {}
 
41
        # Browse given invoices
38
42
        for inv in self.browse(cr, uid, ids, context=context):
39
 
            res[inv.id] = False
40
 
            if inv.analytic_distribution_id:
41
 
                res[inv.id] = True
 
43
            res[inv.id] = inv.analytic_distribution_id and inv.analytic_distribution_id.lines_count or 'None'
42
44
        return res
43
45
 
44
46
    _columns = {
45
 
        'have_analytic_distribution': fields.function(_have_analytic_distribution, method=True, type='boolean', string='Have an analytic distribution?'),
 
47
        'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
 
48
            string="Analytic distribution count", readonly=True, store=False),
46
49
    }
47
50
 
48
51
    def _hook_fields_for_refund(self, cr, uid, *args):
112
115
                default.update({'analytic_distribution_id': new_distrib_id})
113
116
        return super(account_invoice, self).copy(cr, uid, id, default, context)
114
117
 
 
118
    def action_open_invoice(self, cr, uid, ids, context={}, *args):
 
119
        """
 
120
        Add verification on all lines for analytic_distribution_id to be present and valid !
 
121
        """
 
122
        # Some verifications
 
123
        if not context:
 
124
            context = {}
 
125
        if isinstance(ids, (int, long)):
 
126
            ids = [ids]
 
127
        # Browse invoice and all invoice lines to detect a non-valid line
 
128
        for inv in self.browse(cr, uid, ids, context=context):
 
129
            for invl in inv.invoice_line:
 
130
                if inv.from_yml_test or invl.from_yml_test:
 
131
                    continue
 
132
                if invl.analytic_distribution_state != 'valid':
 
133
                    raise osv.except_osv(_('Error'), _('Analytic distribution is not valid for "%s"' % invl.name))
 
134
        # FIXME: copy invoice analytic distribution header if valid and no analytic_distribution_id
 
135
        # FIXME: what about analytic accountancy?
 
136
        return super(account_invoice, self).action_open_invoice(cr, uid, ids, context, args)
 
137
 
115
138
account_invoice()
 
139
 
 
140
class account_invoice_line(osv.osv):
 
141
    _name = 'account.invoice.line'
 
142
    _inherit = 'account.invoice.line'
 
143
 
 
144
    def _get_distribution_state(self, cr, uid, ids, name, args, context={}):
 
145
        """
 
146
        Get state of distribution:
 
147
         - if compatible with the invoice line, then "valid"
 
148
         - if no distribution, take a tour of invoice distribution, if compatible, then "valid"
 
149
         - if no distribution on invoice line and invoice, then "none"
 
150
         - all other case are "invalid"
 
151
        """
 
152
        # Some verifications
 
153
        if not context:
 
154
            context = {}
 
155
        if isinstance(ids, (int, long)):
 
156
            ids = [ids]
 
157
        # Prepare some values
 
158
        res = {}
 
159
        # Browse all given lines
 
160
        for line in self.browse(cr, uid, ids, context=context):
 
161
            if line.from_yml_test:
 
162
                res[line.id] = 'valid'
 
163
            else:
 
164
                res[line.id] = self.pool.get('analytic.distribution')._get_distribution_state(cr, uid, line.analytic_distribution_id.id, line.invoice_id.analytic_distribution_id.id, line.account_id.id)
 
165
        return res
 
166
 
 
167
    def _get_distribution_line_count(self, cr, uid, ids, name, args, context={}):
 
168
        """
 
169
        Return analytic distribution line count (given by analytic distribution)
 
170
        """
 
171
        # Some verifications
 
172
        if not context:
 
173
            context = {}
 
174
        if isinstance(ids, (int, long)):
 
175
            ids = [ids]
 
176
        # Prepare some values
 
177
        res = {}
 
178
        # Browse given invoices
 
179
        for invl in self.browse(cr, uid, ids, context=context):
 
180
            res[invl.id] = invl.analytic_distribution_id and invl.analytic_distribution_id.lines_count or ''
 
181
        return res
 
182
 
 
183
    def _have_analytic_distribution_from_header(self, cr, uid, ids, name, arg, context={}):
 
184
        """
 
185
        If invoice have an analytic distribution, return False, else return True
 
186
        """
 
187
        # Some verifications
 
188
        if not context:
 
189
            context = {}
 
190
        if isinstance(ids, (int, long)):
 
191
            ids = [ids]
 
192
        res = {}
 
193
        for inv in self.browse(cr, uid, ids, context=context):
 
194
            res[inv.id] = True
 
195
            if inv.analytic_distribution_id:
 
196
                res[inv.id] = False
 
197
        return res
 
198
 
 
199
    _columns = {
 
200
        'analytic_distribution_line_count': fields.function(_get_distribution_line_count, method=True, type='char', size=256,
 
201
            string="Analytic distribution count", readonly=True, store=False),
 
202
        'analytic_distribution_state': fields.function(_get_distribution_state, method=True, type='selection', 
 
203
            selection=[('none', 'None'), ('valid', 'Valid'), ('invalid', 'Invalid')], 
 
204
            string="Distribution state", help="Informs from distribution state among 'none', 'valid', 'invalid."),
 
205
        'have_analytic_distribution_from_header': fields.function(_have_analytic_distribution_from_header, method=True, type='boolean', 
 
206
            string='Header Distrib.?'),
 
207
        'newline': fields.boolean('New line'),
 
208
    }
 
209
    
 
210
    _defaults = {
 
211
        'newline': lambda *a: True,
 
212
        'have_analytic_distribution_from_header': lambda *a: True,
 
213
    }
 
214
 
 
215
    def create(self, cr, uid, vals, context={}):
 
216
        vals['newline'] = False
 
217
        return super(account_invoice_line, self).create(cr, uid, vals, context)
 
218
 
 
219
    def copy_data(self, cr, uid, id, default={}, context={}):
 
220
        """
 
221
        Copy global distribution and give it to new invoice line
 
222
        """
 
223
        # Some verifications
 
224
        if not context:
 
225
            context = {}
 
226
        # Copy analytic distribution
 
227
        invl = self.browse(cr, uid, [id], context=context)[0]
 
228
        if invl.analytic_distribution_id:
 
229
            new_distrib_id = self.pool.get('analytic.distribution').copy(cr, uid, invl.analytic_distribution_id.id, {}, context=context)
 
230
            if new_distrib_id:
 
231
                default.update({'analytic_distribution_id': new_distrib_id})
 
232
        return super(account_invoice_line, self).copy_data(cr, uid, id, default, context)
 
233
 
 
234
account_invoice_line()
116
235
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: