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

« back to all changes in this revision

Viewing changes to funding_pool/invoice.py

  • Committer: jf
  • Date: 2011-11-14 15:34:27 UTC
  • mfrom: (374.3.1 uf-585)
  • Revision ID: jf@tempo4-20111114153427-bcc5os034csl9vu2
UF-585 [MERGE]  Product list should be limitated by the parent list

Show diffs side-by-side

added added

removed removed

Lines of Context:
34
34
        res = super(account_invoice, self).line_get_convert(cr, uid, x, part, date, context=context)
35
35
        res['analytic_distribution_id'] = x.get('analytic_distribution_id', False)
36
36
        return res
37
 
 
38
 
    def button_analytic_distribution(self, cr, uid, ids, context=None):
39
 
        """
40
 
        Launch analytic distribution wizard on an invoice
41
 
        """
42
 
        # Some verifications
43
 
        if not context:
44
 
            context = {}
45
 
        if isinstance(ids, (int, long)):
46
 
            ids = [ids]
47
 
        # Prepare some values
48
 
        invoice = self.browse(cr, uid, ids[0], context=context)
49
 
        amount = 0.0
50
 
        # Search elements for currency
 
37
    
 
38
    def button_analytic_distribution(self, cr, uid, ids, context={}):
 
39
        # we get the analytical distribution object linked to this line
 
40
        distrib_id = False
 
41
        negative_inv = False
 
42
        invoice_obj = self.browse(cr, uid, ids[0], context=context)
51
43
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
52
 
        currency = invoice.currency_id and invoice.currency_id.id or company_currency
53
 
        for line in invoice.invoice_line:
54
 
            amount += line.price_subtotal
55
 
        # Get analytic_distribution_id
56
 
        distrib_id = invoice.analytic_distribution_id and invoice.analytic_distribution_id.id
57
 
        # Prepare values for wizard
58
 
        vals = {
59
 
            'total_amount': amount,
60
 
            'invoice_id': invoice.id,
61
 
            'currency_id': currency or False,
62
 
            'state': 'dispatch',
63
 
        }
64
 
        if distrib_id:
65
 
            vals.update({'distribution_id': distrib_id,})
66
 
        # Create the wizard
67
 
        wiz_obj = self.pool.get('analytic.distribution.wizard')
68
 
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
69
 
        # Update some context values
70
 
        context.update({
71
 
            'active_id': ids[0],
72
 
            'active_ids': ids,
73
 
        })
74
 
        # Open it!
 
44
        currency = invoice_obj.currency_id and invoice_obj.currency_id.id or company_currency
 
45
        amount_tot = 0.0
 
46
        if invoice_obj.type in ['out_invoice', 'in_refund']:
 
47
            negative_inv = True
 
48
        if invoice_obj.analytic_distribution_id:
 
49
            distrib_id = invoice_obj.analytic_distribution_id.id
 
50
        else:
 
51
            distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {}, context=context)
 
52
            newvals = {'analytic_distribution_id': distrib_id}
 
53
            super(account_invoice, self).write(cr, uid, ids, newvals, context=context)
 
54
        child_distributions = []
 
55
        for invoice_line in invoice_obj.invoice_line:
 
56
            il_amount = invoice_line.price_subtotal
 
57
            if negative_inv:
 
58
                il_amount = -1 * il_amount
 
59
            amount_tot += il_amount
 
60
            if invoice_line.analytic_distribution_id:
 
61
                if invoice_line.analytic_distribution_id.global_distribution \
 
62
                or ('reset_all' in context and context['reset_all']):
 
63
                    child_distributions.append((invoice_line.analytic_distribution_id.id, il_amount))
 
64
            else:
 
65
                child_distrib_id = self.pool.get('analytic.distribution').create(cr, uid, {'global_distribution': True}, context=context)
 
66
                child_vals = {'analytic_distribution_id': child_distrib_id}
 
67
                self.pool.get('account.invoice.line').write(cr, uid, [invoice_line.id], child_vals, context=context)
 
68
                child_distributions.append((child_distrib_id, il_amount))
 
69
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
 
70
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount_tot, 'distribution_id': distrib_id, 'currency_id': currency}, context=context)
 
71
        # we open a wizard
75
72
        return {
76
 
                'name': 'Global analytic distribution',
77
73
                'type': 'ir.actions.act_window',
78
 
                'res_model': 'analytic.distribution.wizard',
 
74
                'res_model': 'wizard.costcenter.distribution',
79
75
                'view_type': 'form',
80
76
                'view_mode': 'form',
81
77
                'target': 'new',
82
78
                'res_id': [wiz_id],
83
 
                'context': context,
 
79
                'context': {
 
80
                    'active_id': ids[0],
 
81
                    'active_ids': ids,
 
82
                    'wizard_ids': {'cost_center': wiz_id},
 
83
                    'child_distributions': child_distributions
 
84
               }
84
85
        }
85
86
 
86
87
account_invoice()
93
94
        'analytic_distribution_id': fields.many2one('analytic.distribution', 'Analytic Distribution'),
94
95
    }
95
96
 
96
 
    def button_analytic_distribution(self, cr, uid, ids, context=None):
97
 
        """
98
 
        Launch analytic distribution wizard on an invoice line
99
 
        """
100
 
        # Some verifications
101
 
        if not context:
102
 
            context = {}
103
 
        if isinstance(ids, (int, long)):
104
 
            ids = [ids]
105
 
        if not ids:
106
 
            raise osv.except_osv(_('Error'), _('No invoice line given. Please save your invoice line before.'))
107
 
        # Prepare some values
108
 
        invoice_line = self.browse(cr, uid, ids[0], context=context)
 
97
    def button_analytic_distribution(self, cr, uid, ids, context={}):
 
98
        # we get the analytical distribution object linked to this line
109
99
        distrib_id = False
110
100
        negative_inv = False
111
 
        amount = invoice_line.price_subtotal or 0.0
 
101
        invoice_line_obj = self.browse(cr, uid, ids[0], context=context)
 
102
        amount = invoice_line_obj.price_subtotal or 0.0
112
103
        # Search elements for currency
113
104
        company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
114
 
        currency = invoice_line.invoice_id.currency_id and invoice_line.invoice_id.currency_id.id or company_currency
115
 
        # Change amount sign if necessary
116
 
        if invoice_line.invoice_id.type in ['out_invoice', 'in_refund']:
 
105
        currency = invoice_line_obj.invoice_id.currency_id and invoice_line_obj.invoice_id.currency_id.id or company_currency
 
106
        if invoice_line_obj.invoice_id.type in ['out_invoice', 'in_refund']:
117
107
            negative_inv = True
118
108
        if negative_inv:
119
109
            amount = -1 * amount
120
 
        # Get analytic distribution id from this line
121
 
        distrib_id = invoice_line and invoice_line.analytic_distribution_id and invoice_line.analytic_distribution_id.id or False
122
 
        # Prepare values for wizard
123
 
        vals = {
124
 
            'total_amount': amount,
125
 
            'invoice_line_id': invoice_line.id,
126
 
            'currency_id': currency or False,
127
 
            'state': 'dispatch',
128
 
            'account_id': invoice_line.account_id and invoice_line.account_id.id or False,
129
 
        }
130
 
        if distrib_id:
131
 
            vals.update({'distribution_id': distrib_id,})
132
 
        # Create the wizard
133
 
        wiz_obj = self.pool.get('analytic.distribution.wizard')
134
 
        wiz_id = wiz_obj.create(cr, uid, vals, context=context)
135
 
        # Update some context values
 
110
        if invoice_line_obj.analytic_distribution_id:
 
111
            distrib_id = invoice_line_obj.analytic_distribution_id.id
 
112
        else:
 
113
            raise osv.except_osv(_('No Analytic Distribution !'),_("You have to define an analytic distribution for the whole invoice first!"))
 
114
        wiz_obj = self.pool.get('wizard.costcenter.distribution')
 
115
        wiz_id = wiz_obj.create(cr, uid, {'total_amount': amount, 'distribution_id': distrib_id, 'currency_id': currency, 'invoice_line': ids[0]}, context=context)
 
116
        # we open a wizard
136
117
        context.update({
137
 
            'active_id': ids[0],
138
 
            'active_ids': ids,
 
118
          'active_id': ids[0],
 
119
          'active_ids': ids,
 
120
          'wizard_ids': {'cost_center': wiz_id}
139
121
        })
140
 
        # Open it!
141
122
        return {
142
 
                'name': 'Analytic distribution',
143
123
                'type': 'ir.actions.act_window',
144
 
                'res_model': 'analytic.distribution.wizard',
 
124
                'res_model': 'wizard.costcenter.distribution',
145
125
                'view_type': 'form',
146
126
                'view_mode': 'form',
147
127
                'target': 'new',
149
129
                'context': context,
150
130
        }
151
131
 
 
132
    def create(self, cr, uid, vals, context=None):
 
133
        res_id = False
 
134
        analytic_obj = self.pool.get('analytic.distribution')
 
135
        if 'invoice_id' in vals and vals['invoice_id']:
 
136
            #new line, we add the global distribution
 
137
            if self._name == 'wizard.account.invoice.line':
 
138
                obj_name = 'wizard.account.invoice'
 
139
            else:
 
140
                obj_name = 'account.invoice'
 
141
            invoice_obj = self.pool.get(obj_name).browse(cr, uid, vals['invoice_id'], context=context)
 
142
            if invoice_obj.analytic_distribution_id and not vals.get('analytic_distribution_id'):
 
143
                child_distrib_id = analytic_obj.create(cr, uid, {'global_distribution': True}, context=context)
 
144
                vals['analytic_distribution_id'] = child_distrib_id
 
145
                res_id =  super(account_invoice_line, self).create(cr, uid, vals, context=context)
 
146
                amount = self._amount_line(cr, uid, [res_id], None, None, {})[res_id] or 0.0
 
147
                if invoice_obj.type in ['out_invoice', 'in_refund']:
 
148
                    amount = -1 * amount
 
149
                company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
 
150
                currency = invoice_obj.currency_id and invoice_obj.currency_id.id or company_currency
 
151
                analytic_obj.copy_from_global_distribution(cr,
 
152
                                                           uid,
 
153
                                                           invoice_obj.analytic_distribution_id.id,
 
154
                                                           child_distrib_id,
 
155
                                                           amount,
 
156
                                                           currency,
 
157
                                                           context=context)
 
158
        if res_id:
 
159
            return res_id
 
160
        else:
 
161
            return super(account_invoice_line, self).create(cr, uid, vals, context=context)
 
162
        
 
163
    def write(self, cr, uid, ids, vals, context=None):
 
164
        # Update values from invoice line
 
165
        res = super(account_invoice_line, self).write(cr, uid, ids, vals, context=context)
 
166
        # Browse invoice lines
 
167
        for line in self.browse(cr, uid, ids, context=context):
 
168
            # Do some update if this line have an analytic distribution
 
169
            if line.analytic_distribution_id:
 
170
                if 'price_unit' in vals or 'quantity' in vals or 'discount' in vals or context.get('reset_all', False):
 
171
                    amount = line.price_subtotal or 0.0
 
172
                    if line.invoice_id.type in ['out_invoice', 'in_refund']:
 
173
                        amount = -1 * amount
 
174
                    company_currency = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.currency_id.id
 
175
                    currency = line.invoice_id.currency_id and line.invoice_id.currency_id.id or company_currency
 
176
                    distrib_obj = self.pool.get('analytic.distribution')
 
177
                    if line.analytic_distribution_id.global_distribution:
 
178
                        source = line.invoice_id.analytic_distribution_id.id
 
179
                        dest = line.analytic_distribution_id.id
 
180
                        distrib_obj.copy_from_global_distribution(cr, uid, source, dest, amount, currency, context=context)
 
181
                    else:
 
182
                        distrib_obj.update_distribution_line_amount(cr, uid, [line.analytic_distribution_id.id], amount, context=context)
 
183
        return res
 
184
 
152
185
    def move_line_get_item(self, cr, uid, line, context=None):
153
 
        """
154
 
        Give right analytic distribution when creating move lines
155
 
        """
156
 
        # Some verifications
157
 
        if not context:
158
 
            context = {}
159
 
        # Default result
160
186
        res = super(account_invoice_line, self).move_line_get_item(cr, uid, line, context=context)
161
 
        # Update result by copying analytic distribution from invoice line
162
 
        ana_obj = self.pool.get('analytic.distribution')
163
 
        if line.analytic_distribution_id:
164
 
            new_distrib_id = ana_obj.copy(cr, uid, line.analytic_distribution_id.id, {}, context=context)
165
 
            if new_distrib_id:
166
 
                res['analytic_distribution_id'] = new_distrib_id
167
 
        # If no distribution on invoice line, take those from invoice and copy it!
168
 
        elif line.invoice_id and line.invoice_id.analytic_distribution_id:
169
 
            new_distrib_id = ana_obj.copy(cr, uid, line.invoice_id.analytic_distribution_id.id, {}, context=context)
170
 
            if new_distrib_id:
171
 
                res['analytic_distribution_id'] = new_distrib_id
 
187
        res['analytic_distribution_id'] = line.analytic_distribution_id.id
172
188
        return res
173
189
 
174
190
account_invoice_line()