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

« back to all changes in this revision

Viewing changes to analytic_distribution/wizard/import_commitment_wizard.py

  • Committer: jf
  • Date: 2015-02-09 12:53:11 UTC
  • mfrom: (2365.13.20 unifield-wm)
  • Revision ID: jfb@tempo-consulting.fr-20150209125311-dh2m9vx50qjt6dqk
BKLG-4 [IMP] Improvements to international commitments
lp:~unifield-team/unifield-wm/bklg-4

Show diffs side-by-side

added added

removed removed

Lines of Context:
58
58
        journal_ids = self.pool.get('account.analytic.journal').search(cr, uid, [('code', '=', 'ENGI')], context=context)
59
59
        to_be_deleted_ids = analytic_obj.search(cr, uid, [('imported_commitment', '=', True)], context=context)
60
60
        functional_currency_obj = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id
 
61
        default_founding_pool_id = self.pool.get('account.analytic.account').search(
 
62
            cr, uid,  [('category', '=', 'FUNDING'), ('code', '=', 'PF')], context=context)
 
63
        if not default_founding_pool_id:
 
64
            raise osv.except_osv(_('Error'), _('Default PF Funding Pool not found'))
 
65
        default_founding_pool_id = default_founding_pool_id[0]
61
66
 
62
67
        now = False
63
68
        if len(journal_ids) > 0:
131
136
                        else:
132
137
                            raise osv.except_osv(_('Error'), raise_msg_prefix + (_('Destination "%s" doesn\'t exist!') % (destination,)))
133
138
                    else:
134
 
                        raise osv.except_osv(_('Error'), raise_msg_prefix + _('No destination code found!'))
 
139
                        # try to get default account destination by default
 
140
                        account_br = self.pool.get('account.account').browse(cr,
 
141
                            uid, account_ids[0])
 
142
                        if account_br.default_destination_id:
 
143
                            vals['destination_id'] = account_br.default_destination_id.id
 
144
                        else:
 
145
                            msg = _("No destination code found and no default destination for account %s !") % account_code
 
146
                            raise osv.except_osv(_('Error'), raise_msg_prefix + msg)
135
147
                    # Cost Center
136
148
                    if cost_center:
137
149
                        cc_id = self.pool.get('account.analytic.account').search(cr, uid, ['|', ('code', '=', cost_center), ('name', '=', cost_center)])
149
161
                        else:
150
162
                            raise osv.except_osv(_('Error'), raise_msg_prefix +_(('Funding Pool "%s" doesn\'t exist!') % (funding_pool,)))
151
163
                    else:
152
 
                        raise osv.except_osv(_('Error'), raise_msg_prefix + _('No funding pool code found!'))
 
164
                        vals['account_id'] = default_founding_pool_id
153
165
                    # description
154
166
                    if description:
155
167
                        vals.update({'name': description})
198
210
                    else:
199
211
                        raise osv.except_osv(_('Error'), raise_msg_prefix + _('No booking amount found!'))
200
212
 
 
213
                    # Check AJI consistency
 
214
                    no_compat = analytic_obj.check_dest_cc_fp_compatibility(cr,
 
215
                        uid, False,
 
216
                        dest_id=dest_id[0], cc_id=cc_id[0], fp_id=fp_id[0],
 
217
                        from_import=True,
 
218
                        from_import_general_account_id=account_ids[0],
 
219
                        from_import_posting_date=line_date,
 
220
                        context=context)
 
221
                    if no_compat:
 
222
                        no_compat = no_compat[0]
 
223
                        # no compatible AD
 
224
                        msg = _("Dest / Cost Center / Funding Pool are not" \
 
225
                            " compatible for entry name:'%s', ref:'%s'" \
 
226
                            " reason: '%s'")
 
227
                        raise osv.except_osv(_('Error'), msg % (
 
228
                            vals.get('name', ''), vals.get('ref', ''),
 
229
                            no_compat[2] or '', )
 
230
                        )
 
231
 
201
232
                    analytic_obj.create(cr, uid, vals, context=context)
202
233
                    sequence_number += 1
203
234
 
209
240
        return {'type' : 'ir.actions.act_window_close'}
210
241
 
211
242
import_commitment_wizard()
 
243
 
 
244
 
 
245
class int_commitment_clear_wizard(osv.osv_memory):
 
246
    _name = 'int.commitment.clear.wizard'
 
247
    _description = 'Clear Intl Commitments Wizard'
 
248
 
 
249
    def _get_to_del_ids(self, cr, uid, context=None, count=False):
 
250
        domain = [
 
251
            ('type', '=', 'engagement'),
 
252
            ('code', '=', 'ENGI'),
 
253
        ]
 
254
        journal_ids = self.pool.get('account.analytic.journal').search(cr, uid,
 
255
            domain, context=context)
 
256
        if not journal_ids:
 
257
            return False
 
258
 
 
259
        domain = [
 
260
            ('imported_commitment', '=', True),
 
261
            ('journal_id', 'in', journal_ids),
 
262
        ]
 
263
        res_ids = self.pool.get('account.analytic.line').search(cr, uid, domain,
 
264
            context=context, count=count)
 
265
        return res_ids
 
266
 
 
267
    _columns = {
 
268
        'entries_count': fields.integer('Count Intl Commitments to delete'),
 
269
    }
 
270
 
 
271
    _defaults = {
 
272
        'entries_count': lambda s, cr, uid, context: s._get_to_del_ids(cr, uid, context=context, count=True),
 
273
    }
 
274
 
 
275
    def mass_delete(self, cr, uid, ids, context=None):
 
276
        to_del_ids = self._get_to_del_ids(cr, uid, context=context, count=False)
 
277
        if to_del_ids:
 
278
            self.pool.get('account.analytic.line').unlink(cr, uid, to_del_ids,
 
279
                context=context)
 
280
        return {'type': 'ir.actions.act_window_close'}
 
281
 
 
282
int_commitment_clear_wizard()
 
283
 
 
284
 
 
285
class int_commitment_export_wizard(osv.osv_memory):
 
286
    _name = 'int.commitment.export.wizard'
 
287
    _description = 'Export Intl Commitments'
 
288
 
 
289
    _csv_filename_pattern = 'Intl_Commitments_%s.csv'
 
290
    _csv_delimiter = ','
 
291
    _csv_header = ['Description', 'Ref', 'Document Date', 'Posting Date',
 
292
        'General Account', 'Destination', 'Cost Center' , 'Funding Pool',
 
293
        'Third Party', 'Booking Amount', 'Booking Currency', ]
 
294
 
 
295
    _columns = {
 
296
        'data': fields.binary('File', readonly=True),
 
297
        'name': fields.char('File Name', 128, readonly=True),
 
298
        'state': fields.selection((('choose','choose'), ('get','get'), ),
 
299
            readonly=True, invisible=True),
 
300
    }
 
301
 
 
302
    _defaults = {
 
303
        'state': lambda *a: 'choose',
 
304
    }
 
305
 
 
306
    def button_export(self, cr, uid, ids, context=None):
 
307
        aal_obj = self.pool.get('account.analytic.line')
 
308
 
 
309
        instance_name = self.pool.get('res.users').browse(cr, uid, [uid],
 
310
            context=context)[0].company_id.instance_id.name or ''
 
311
        file_name = self._csv_filename_pattern % (instance_name, )
 
312
 
 
313
        # csv prepare and header
 
314
        csv_buffer = StringIO.StringIO()
 
315
        csv_writer = csv.writer(csv_buffer, delimiter=self._csv_delimiter,
 
316
            quoting=csv.QUOTE_MINIMAL)
 
317
        csv_writer.writerow(self._csv_header)
 
318
 
 
319
        # data lines
 
320
        domain = [
 
321
            ('journal_id.type', '=', 'engagement'),
 
322
            ('journal_id.code', '=', 'ENGI'),
 
323
        ]
 
324
        export_ids = aal_obj.search(cr, uid, domain, context=context)
 
325
        for export_br in aal_obj.browse(cr, uid, export_ids, context=context):
 
326
            csv_writer.writerow(self._export_entry(export_br))
 
327
 
 
328
        # download csv
 
329
        vals = {
 
330
            'state': 'get',
 
331
            'data': base64.encodestring(csv_buffer.getvalue()),
 
332
            'name': file_name,
 
333
        }
 
334
        csv_buffer.close()
 
335
        return self.write(cr, uid, ids, vals, context=context)
 
336
 
 
337
    def _export_entry(self, item_br):
 
338
        def decode_m2o(m2o, want_code=False):
 
339
            if not m2o:
 
340
                return ''
 
341
            res = m2o.code if want_code else m2o.name
 
342
            return res or ''
 
343
 
 
344
        def decode_date(dt):
 
345
            # '%Y-%m-%d' orm -> '%d/%m/%Y' of import csv format
 
346
            return dt and time.strftime('%d/%m/%Y', time.strptime(dt, '%Y-%m-%d')) or ''
 
347
 
 
348
        return [
 
349
            # Description
 
350
            decode_m2o(item_br),
 
351
 
 
352
            # Ref
 
353
            item_br.ref or '',
 
354
 
 
355
            # Document Date
 
356
            decode_date(item_br.document_date),
 
357
 
 
358
            # Posting Date
 
359
            decode_date(item_br.date),
 
360
 
 
361
            # General Account
 
362
            decode_m2o(item_br.general_account_id, want_code=True),
 
363
 
 
364
            # Destination
 
365
            decode_m2o(item_br.destination_id, want_code=True),
 
366
 
 
367
            # Cost Center
 
368
            decode_m2o(item_br.cost_center_id, want_code=True),
 
369
 
 
370
            # Funding Pool
 
371
            decode_m2o(item_br.account_id, want_code=True),
 
372
 
 
373
            # Third Party
 
374
            item_br.partner_txt or '',
 
375
 
 
376
            # Booking Amount
 
377
            str(-1. * item_br.amount_currency),
 
378
 
 
379
            # Booking Currency
 
380
            decode_m2o(item_br.currency_id),
 
381
        ]
 
382
 
 
383
 
 
384
int_commitment_export_wizard()
212
385
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: