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

« back to all changes in this revision

Viewing changes to analytic_distribution/wizard/analytic_distribution_wizard.py

[MERGE] commitment lp:~unifield-team/unifield-wm/UF_675_commitments

Show diffs side-by-side

added added

removed removed

Lines of Context:
364
364
            # verify invoice line state
365
365
            if el.invoice_line_id and el.invoice_line_id.invoice_id and el.invoice_line_id.invoice_id.state in ['open', 'paid']:
366
366
                res[el.id] = False
 
367
            # verify commitment state
 
368
            if el.commitment_id and el.commitment_id.state in ['done']:
 
369
                res[el.id] = False
 
370
            # verify commitment line state
 
371
            if el.commitment_line_id and el.commitment_line_id.commit_id and el.commitment_line_id.commit_id.state in ['done']:
 
372
                res[el.id] = False
367
373
        return res
368
374
 
369
375
    def _have_header(self, cr, uid, ids, name, args, context={}):
405
411
        'invoice_line_id': fields.many2one('account.invoice.line', string="Invoice Line"),
406
412
        'register_line_id': fields.many2one('account.bank.statement.line', string="Register Line"),
407
413
        'move_line_id': fields.many2one('account.move.line', string="Journal Item"),
 
414
        'commitment_id': fields.many2one('account.commitment', string="Commitment Voucher"),
 
415
        'commitment_line_id': fields.many2one('account.commitment.line', string="Commitment Voucher Line"),
408
416
        'distribution_id': fields.many2one('analytic.distribution', string="Analytic Distribution"),
409
417
        'is_writable': fields.function(_is_writable, method=True, string='Is this wizard writable?', type='boolean', readonly=True, 
410
418
            help="This informs wizard if it could be saved or not regarding invoice state or purchase order state", store=False),
538
546
            # Verify that invoice from invoice line is in good state if necessary
539
547
            if wiz.invoice_line_id and wiz.invoice_line_id.invoice_id and wiz.invoice_line_id.invoice_id.state in ['open', 'paid']:
540
548
                raise osv.except_osv(_('Error'), _('You cannot change the distribution.'))
 
549
            # Verify that commitment is in good state if necessary
 
550
            if wiz.commitment_id and wiz.commitment_id.state in ['done']:
 
551
                raise osv.except_osv(_('Error'), _('You cannot change the distribution.'))
 
552
            if wiz.commitment_line_id and wiz.commitment_line_id.commit_id and wiz.commitment_line_id.commit_id.state in ['done']:
 
553
                raise osv.except_osv(_('Error'), _('You cannot change the distribution.'))
541
554
            # Verify that Cost Center are done if we come from a purchase order
542
 
            if not wiz.line_ids and wiz.purchase_id:
 
555
            if not wiz.line_ids and (wiz.purchase_id or wiz.purchase_line_id):
543
556
                raise osv.except_osv(_('Warning'), _('No Cost Center Allocation done!'))
544
 
            if wiz.invoice_id and not wiz.fp_line_ids:
 
557
            # Verify that Funding Pool Lines are done if we come from an invoice, invoice line, direct invoice, direct invoice line, register line, 
 
558
            #+ move line, commitment or commitment line
 
559
            if not wiz.fp_line_ids and (wiz.invoice_id or wiz.invoice_line_id) :
 
560
                raise osv.except_osv(_('Warning'), _('No Funding Pool Allocation done!'))
 
561
            if not wiz.fp_line_ids and (wiz.direct_invoice_id or wiz.direct_invoice_line_id):
 
562
                raise osv.except_osv(_('Warning'), _('No Funding Pool Allocation done!'))
 
563
            if not wiz.fp_line_ids and wiz.register_line_id:
 
564
                raise osv.except_osv(_('Warning'), _('No Funding Pool Allocation done!'))
 
565
            if not wiz.fp_line_ids and wiz.move_line_id:
 
566
                raise osv.except_osv(_('Warning'), _('No Funding Pool Allocation done!'))
 
567
            if not wiz.fp_line_ids and (wiz.commitment_id or wiz.commitment_line_id):
545
568
                raise osv.except_osv(_('Warning'), _('No Funding Pool Allocation done!'))
546
569
            # Verify that allocation is 100% on each type of distribution, but only if there some lines
547
570
            for lines in [wiz.line_ids, wiz.fp_line_ids, wiz.f1_line_ids, wiz.f2_line_ids]:
707
730
                # link it to the element we come from (purchase order, invoice, purchase order line, invoice line, etc.)
708
731
                for el in [('invoice_id', 'account.invoice'), ('invoice_line_id', 'account.invoice.line'), ('purchase_id', 'purchase.order'), 
709
732
                    ('purchase_line_id', 'purchase.order.line'), ('register_line_id', 'account.bank.statement.line'), 
710
 
                    ('move_line_id', 'account.move.line'), ('direct_invoice_id', 'wizard.account.invoice'), ('direct_invoice_line_id', 'wizard.account.invoice.line')]:
 
733
                    ('move_line_id', 'account.move.line'), ('direct_invoice_id', 'wizard.account.invoice'), 
 
734
                    ('direct_invoice_line_id', 'wizard.account.invoice.line'), ('commitment_id', 'account.commitment'), 
 
735
                    ('commitment_line_id', 'account.commitment.line')]:
711
736
                    if getattr(wiz, el[0], False):
712
737
                        id = getattr(wiz, el[0], False).id
713
738
                        self.pool.get(el[1]).write(cr, uid, [id], {'analytic_distribution_id': distrib_id}, context=context)
877
902
        for wizard in self.browse(cr, uid, ids, context=context):
878
903
            # Prepare some values
879
904
            distrib = wizard.distribution_id or False
880
 
            move_lines = [x.id for x in distrib.move_line_ids]
881
905
            aal_obj = self.pool.get('account.analytic.line')
882
906
            ml_obj = self.pool.get('account.move.line')
883
907
            if not distrib:
884
908
                return False
885
 
            # Search account analytic lines attached to this move lines
886
 
            operator = 'in'
887
 
            if len(move_lines) == 1:
888
 
                operator = '='
889
 
            aal_ids = aal_obj.search(cr, uid, [('move_id', operator, move_lines)], context=context)
890
 
            if aal_ids:
891
 
                # delete old analytic lines
892
 
                aal_obj.unlink(cr, uid, aal_ids, context=context)
893
 
                # create new analytic lines
894
 
                ml_obj.create_analytic_lines(cr, uid, move_lines, context=context)
895
 
 
896
 
            if not move_lines and wizard.invoice_line_id:
897
 
                self.pool.get('account.invoice.line').create_engagement_lines(cr, uid, [wizard.invoice_line_id.id], context=context)
 
909
            if wizard.move_line_id:
 
910
                move_lines = [x.id for x in distrib.move_line_ids]
 
911
                # Search account analytic lines attached to this move lines
 
912
                operator = 'in'
 
913
                if len(move_lines) == 1:
 
914
                    operator = '='
 
915
                aal_ids = aal_obj.search(cr, uid, [('move_id', operator, move_lines)], context=context)
 
916
                if aal_ids:
 
917
                    # delete old analytic lines
 
918
                    aal_obj.unlink(cr, uid, aal_ids, context=context)
 
919
                    # create new analytic lines
 
920
                    ml_obj.create_analytic_lines(cr, uid, move_lines, context=context)
 
921
            elif wizard.commitment_line_id:
 
922
                # Do process only if commitment is on 'open' state
 
923
                if wizard.commitment_line_id.commit_id and wizard.commitment_line_id.commit_id.state == 'open':
 
924
                    self.pool.get('account.commitment.line').update_analytic_lines(cr, uid, [wizard.commitment_line_id.id], wizard.total_amount, context=context)
 
925
            elif wizard.commitment_id:
 
926
                if wizard.commitment_id.state == 'open':
 
927
                    # Search commitment lines that doesn't have any distribution and that are linked to this commitment
 
928
                    cl_ids = self.pool.get('account.commitment.line').search(cr, uid, [('commit_id', '=', wizard.commitment_id.id), 
 
929
                        ('analytic_distribution_id', '=', False)], context=context)
 
930
                    if cl_ids:
 
931
                        operator = 'in'
 
932
                        if len(cl_ids) == 1:
 
933
                            operator = '='
 
934
                        # Search all analytic lines linked to this commitment lines
 
935
                        aal_ids = aal_obj.search(cr, uid, [('commitment_line_id', operator, cl_ids)], context=context)
 
936
                        if aal_ids:
 
937
                            # delete old analytic lines
 
938
                            aal_obj.unlink(cr, uid, aal_ids, context=context)
 
939
                            # create new analytic lines
 
940
                            self.pool.get('account.commitment').create_analytic_lines(cr, uid, [wizard.commitment_id.id], context=context)
898
941
        return True
899
942
 
900
943
analytic_distribution_wizard()