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

« back to all changes in this revision

Viewing changes to analytic_distribution_supply/account_commitment.py

  • Committer: Quentin THEURET
  • Date: 2016-03-04 12:15:00 UTC
  • Revision ID: qt@tempo-consulting.fr-20160304121500-u2ay8zrf83ih9fu3
US-826 [IMP] Change the way to check if products is not consistent on add multiple line wizard

Show diffs side-by-side

added added

removed removed

Lines of Context:
40
40
    _inherit = 'account.commitment.line'
41
41
 
42
42
    _columns = {
43
 
        'purchase_order_line_ids': fields.many2many('purchase.order.line', 'purchase_line_commitment_rel', 'commitment_id', 'purchase_id', 
 
43
        'purchase_order_line_ids': fields.many2many('purchase.order.line', 'purchase_line_commitment_rel', 'commitment_id', 'purchase_id',
44
44
            string="Purchase Order Lines", readonly=True),
45
45
    }
46
46
 
60
60
            for pol in line.purchase_order_line_ids:
61
61
                # browse cost_center line if an analytic distribution exists for this purchase order line
62
62
                if pol.analytic_distribution_id:
63
 
                    for aline in pol.analytic_distribution_id.cost_center_lines:
64
 
                        if aline.analytic_id:
65
 
                            # Compute amount regarding pol subtotal and cost_center_line percentage
66
 
                            amount = (pol.price_subtotal * aline.percentage) / 100
67
 
                            cc_lines[aline.analytic_id.id].append(amount)
 
63
                    origin_cc_lines = pol.analytic_distribution_id.cost_center_lines
 
64
                # else retrieve CC lines from PO
 
65
                else:
 
66
                    origin_cc_lines = pol.order_id.analytic_distribution_id.cost_center_lines
 
67
                # Compute CC lines amounts
 
68
                for aline in origin_cc_lines:
 
69
                    if aline.analytic_id:
 
70
                        # Compute amount regarding pol subtotal and cost_center_line percentage
 
71
                        amount = (pol.price_subtotal * aline.percentage) / 100
 
72
                        cc_lines[(aline.analytic_id.id, aline.destination_id and aline.destination_id.id or False)].append(amount)
68
73
            # Browse result and create corresponding analytic lines
69
74
            if cc_lines:
70
75
                # create distribution an link it to commitment line
71
76
                distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
72
77
                self.write(cr, uid, [line.id], {'analytic_distribution_id': distrib_id}, context=context)
73
 
                for analytic_id in cc_lines:
 
78
                for el in cc_lines:
 
79
                    analytic_id = el[0] or False
74
80
                    vals = {
75
81
                            'distribution_id': distrib_id,
76
82
                            'analytic_id': analytic_id,
77
83
                            'currency_id': line.commit_id.currency_id.id,
 
84
                            'destination_id': el[1] or False,
78
85
                    }
79
86
                    total_amount = 0.0
80
87
                    # fetch total amount
81
 
                    for amount in cc_lines[analytic_id]:
 
88
                    for amount in cc_lines[el]:
82
89
                        total_amount += amount
83
90
                    if not total_amount:
84
91
                        continue
86
93
                    percentage = (total_amount / line.amount) * 100 or 0.0
87
94
                    vals.update({'percentage': percentage})
88
95
                    # create cost_center_line
89
 
                    distrib_line_id = self.pool.get('cost.center.distribution.line').create(cr, uid, vals, context=context)
 
96
                    self.pool.get('cost.center.distribution.line').create(cr, uid, vals, context=context)
90
97
                # Create funding pool lines if needed
91
 
                self.pool.get('analytic.distribution').create_funding_pool_lines(cr, uid, [distrib_id], context=context)
 
98
                self.pool.get('analytic.distribution').create_funding_pool_lines(cr, uid, [distrib_id], line.account_id.id, context=context)
92
99
        return True
93
100
 
94
101
account_commitment_line()