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

« back to all changes in this revision

Viewing changes to res_currency_tables/res_currency.py

  • Committer: chloups208
  • Date: 2012-11-21 11:15:15 UTC
  • mto: This revision was merged to the branch mainline in revision 1340.
  • Revision ID: chloups208@chloups208-laptop-20121121111515-myqv282h6xmgh053
utp-171 modification of fields po, po line, product, so, so line

Show diffs side-by-side

added added

removed removed

Lines of Context:
35
35
    _sql_constraints = [
36
36
        ('name_uniq', 'unique (name, currency_table_id)', 'The currency name exists already in the system!')
37
37
    ]
38
 
 
 
38
    
39
39
    def _get_table_currency(self, cr, uid, currency_id, table_id, context=None):
40
40
        source_currency = self.browse(cr, uid, currency_id, context=context)
41
41
        if not source_currency.reference_currency_id or not source_currency.currency_table_id :
55
55
        else:
56
56
            # already ok
57
57
            return currency_id
58
 
 
 
58
    
59
59
    def search(self, cr, uid, args=None, offset=0, limit=None, order=None, context=None, count=False):
60
60
        # add argument to discard table currencies by default
61
61
        table_in_args = False
66
66
                table_in_args = True
67
67
        if not table_in_args:
68
68
            args.insert(0, ('currency_table_id', '=', False))
69
 
        return super(res_currency, self).search(cr, uid, args, offset, limit,
70
 
                order, context, count=count)
71
 
 
 
69
        return super(res_currency, self).search(cr, uid, args, offset, limit, order, context, count=count)
 
70
    
72
71
    def compute(self, cr, uid, from_currency_id, to_currency_id, from_amount, round=True, context=None):
73
72
        if context is None:
74
73
            context={}
75
74
        if context.get('currency_table_id', False):
76
75
            # A currency table is set, retrieve the correct currency ids
77
 
            # UTP-894: use currency table rate or default rate if currency not in currency table
78
76
            new_from_currency_id = self._get_table_currency(cr, uid, from_currency_id, context['currency_table_id'], context=context)
79
 
            if new_from_currency_id:
80
 
                from_currency_id = new_from_currency_id
81
77
            new_to_currency_id = self._get_table_currency(cr, uid, to_currency_id, context['currency_table_id'], context=context)
82
 
            if new_to_currency_id:
83
 
                to_currency_id = new_to_currency_id
 
78
            # only use new currencies if both are defined in the table
 
79
            if new_from_currency_id and new_to_currency_id:
 
80
                return super(res_currency, self).compute(cr, uid, new_from_currency_id, new_to_currency_id, from_amount, round, context=context)
 
81
        # Fallback case if no currency table or one currency not defined in the table
84
82
        return super(res_currency, self).compute(cr, uid, from_currency_id, to_currency_id, from_amount, round, context=context)
85
 
 
 
83
    
86
84
    def create_associated_pricelist(self, cr, uid, currency_id, context=None):
87
85
        '''
88
86
        Create purchase and sale pricelists according to the currency
90
88
        pricelist_obj = self.pool.get('product.pricelist')
91
89
        version_obj = self.pool.get('product.pricelist.version')
92
90
        item_obj = self.pool.get('product.pricelist.item')
93
 
 
 
91
        
94
92
        company_id = self.pool.get('res.users').browse(cr, uid, uid, context=context).company_id.id
95
93
        currency = self.browse(cr, uid, currency_id, context=context)
96
 
 
 
94
        
97
95
        # Create the sale pricelist
98
 
        sale_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
99
 
                                                       'name': currency.name,
 
96
        sale_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id, 
 
97
                                                       'name': currency.name, 
100
98
                                                       'active': currency.active,
101
99
                                                       'type': 'sale',
102
100
                                                       'company_id': company_id}, context=context)
103
 
 
 
101
        
104
102
        # Create the sale pricelist version
105
103
        sale_version_id = version_obj.create(cr, uid, {'pricelist_id': sale_price_id,
106
104
                                                       'name': 'Default Sale %s Version' % currency.name,
107
105
                                                       'active': currency.active}, context=context)
108
 
 
 
106
        
109
107
        # Create the sale pricelist item
110
108
        item_obj.create(cr, uid, {'price_version_id': sale_version_id,
111
109
                                  'name': 'Default Sale %s Line' % currency.name,
112
110
                                  'base': 1,
113
111
                                  'min_quantity': 0.00}, context=context)
114
 
 
 
112
        
115
113
        # Create the purchase pricelist
116
 
        purchase_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id,
117
 
                                                           'name': currency.name,
 
114
        purchase_price_id = pricelist_obj.create(cr, uid, {'currency_id': currency.id, 
 
115
                                                           'name': currency.name, 
118
116
                                                           'active': currency.active,
119
117
                                                           'type': 'purchase',
120
118
                                                           'company_id': company_id}, context=context)
121
 
 
 
119
        
122
120
        # Create the sale pricelist version
123
121
        purchase_version_id = version_obj.create(cr, uid, {'pricelist_id': purchase_price_id,
124
122
                                                           'name': 'Default Purchase %s Version' % currency.name,
125
123
                                                           'active': currency.active}, context=context)
126
 
 
 
124
        
127
125
        # Create the sale pricelist item
128
126
        item_obj.create(cr, uid, {'price_version_id': purchase_version_id,
129
127
                                  'name': 'Default Purchase %s Line' % currency.name,
130
128
                                  'base': -2,
131
129
                                  'min_quantity': 0.00}, context=context)
132
 
 
 
130
        
133
131
        return [sale_price_id, purchase_price_id]
134
 
 
 
132
    
135
133
    def create(self, cr, uid, values, context=None):
136
134
        '''
137
135
        Create automatically a purchase and a sales pricelist on
138
136
        currency creation
139
 
        '''
 
137
        '''    
140
138
        res = super(res_currency, self).create(cr, uid, values, context=context)
141
 
 
142
 
        # Create the corresponding pricelists (only for non currency that have a currency_table)
143
 
        if not values.get('currency_table_id', False):
144
 
            self.create_associated_pricelist(cr, uid, res, context=context)
145
 
 
146
 
            # Check if currencies has no associated pricelists
147
 
            cr.execute('SELECT id FROM res_currency WHERE id NOT IN (SELECT currency_id FROM product_pricelist) AND currency_table_id IS NULL')
148
 
            curr_ids = cr.fetchall()
149
 
            for cur_id in curr_ids:
150
 
                self.create_associated_pricelist(cr, uid, cur_id[0], context=context)
151
 
 
 
139
        
 
140
        # Create the corresponding pricelists
 
141
        self.create_associated_pricelist(cr, uid, res, context=context)
 
142
        
 
143
        # Check if currencies has no associated pricelists
 
144
        cr.execute('SELECT id FROM res_currency WHERE id NOT IN (SELECT currency_id FROM product_pricelist)')
 
145
        curr_ids = cr.fetchall()
 
146
        for cur_id in curr_ids:
 
147
            self.create_associated_pricelist(cr, uid, cur_id[0], context=context)
 
148
        
152
149
        return res
153
 
 
 
150
    
154
151
    def write(self, cr, uid, ids, values, context=None):
155
152
        '''
156
153
        Active/De-active pricelists according to activation/de-activation of the currency
157
154
        '''
158
155
        pricelist_obj = self.pool.get('product.pricelist')
159
156
        version_obj = self.pool.get('product.pricelist.version')
160
 
 
 
157
        
161
158
        if isinstance(ids, (int, long)):
162
159
            ids = [ids]
163
 
 
164
 
        if 'active' in values and not values.get('currency_table_id', False):
 
160
        
 
161
        if 'active' in values:
165
162
            if values['active'] == False:
166
163
                self.check_in_use(cr, uid, ids, 'de-activate', context=context)
167
164
            # Get all pricelists and versions for the given currency
168
165
            pricelist_ids = pricelist_obj.search(cr, uid, [('currency_id', 'in', ids), ('active', 'in', ['t', 'f'])], context=context)
169
166
            if not pricelist_ids:
170
 
                to_create = self.search(cr, uid, [('currency_table_id', '=', False), ('id', 'in', ids)], context=context)
171
 
                for cur_id in to_create:
 
167
                for cur_id in ids:
172
168
                    pricelist_ids = self.create_associated_pricelist(cr, uid, cur_id, context=context)
173
169
            version_ids = version_obj.search(cr, uid, [('pricelist_id', 'in', pricelist_ids), ('active', 'in', ['t', 'f'])], context=context)
174
170
            # Update the pricelists and versions
175
171
            pricelist_obj.write(cr, uid, pricelist_ids, {'active': values['active']}, context=context)
176
172
            version_obj.write(cr, uid, version_ids, {'active': values['active']}, context=context)
177
 
 
 
173
        
178
174
        return super(res_currency, self).write(cr, uid, ids, values, context=context)
179
 
 
 
175
    
180
176
    def check_in_use(self, cr, uid, ids, keyword='delete', context=None):
181
177
        '''
182
178
        Check if the currency is currently in used in the system
185
181
        purchase_obj = self.pool.get('purchase.order')
186
182
        sale_obj = self.pool.get('sale.order')
187
183
        property_obj = self.pool.get('ir.property')
188
 
 
 
184
        
189
185
        if isinstance(ids, (int, long)):
190
186
            ids = [ids]
191
 
 
 
187
            
192
188
        pricelist_ids = pricelist_obj.search(cr, uid, [('currency_id', 'in', ids), ('active', 'in', ['t', 'f'])], context=context)
193
189
        if pricelist_ids:
194
190
            # Get all documents which disallow the deletion of the currency
200
196
            partner_ids = property_obj.search(cr, uid, ['|', ('name', '=', 'property_product_pricelist'),
201
197
                                                             ('name', '=', 'property_product_pricelist_purcahse'),
202
198
                                                             ('value_reference', 'in', value_reference)])
203
 
 
204
 
            # Raise an error if the currency is used on partner form
 
199
    
 
200
            # Raise an error if the currency is used on partner form        
205
201
            if partner_ids:
206
202
                raise osv.except_osv(_('Currency currently used !'), _('The currency you want to %s is currently used on at least one partner form.') % keyword)
207
 
 
 
203
            
208
204
            # Raise an error if the currency is used on sale or purchase order
209
205
            if purchase_ids or sale_ids:
210
206
                raise osv.except_osv(_('Currency currently used !'), _('The currency you want to %s is currently used on at least one sale order or purchase order.') % keyword)
211
 
 
 
207
            
212
208
        return pricelist_ids
213
 
 
 
209
            
214
210
    def unlink(self, cr, uid, ids, context=None):
215
211
        '''
216
 
        Unlink the pricelist associated to the currency
 
212
        Unlink the pricelist associated to the currency 
217
213
        '''
218
214
        if isinstance(ids, (int, long)):
219
215
            ids = [ids]
220
 
 
221
 
        # Search for move lines with this currency. If those exists,
222
 
        # exception.
223
 
        move_line_ids = self.pool.get('account.move.line').search(cr, uid, [('|'),
224
 
                                                                            ('currency_id','in',ids),
225
 
                                                                            ('functional_currency_id', 'in', ids)], context=context)
226
 
        if len(move_line_ids) > 0:
227
 
            raise osv.except_osv(_('Currency currently used !'), _('The currency cannot be deleted as one or more journal items are currently using it!'))
228
 
 
 
216
            
229
217
        pricelist_obj = self.pool.get('product.pricelist')
230
 
 
 
218
            
231
219
        # If no error, unlink pricelists
232
220
        for p_list in self.check_in_use(cr, uid, ids, 'delete', context=context):
233
221
            pricelist_obj.unlink(cr, uid, p_list, context=context)
234
 
        return super(res_currency, self).unlink(cr, uid, ids, context=context)
 
222
        for cur_id in ids:
 
223
            res = super(res_currency, self).unlink(cr, uid, cur_id, context=context)
 
224
            
 
225
        return res   
235
226
 
236
227
#        remove this block because it has been moved to the sync module
237
228
#    def get_unique_xml_name(self, cr, uid, uuid, table_name, res_id):
238
229
#        currency = self.browse(cr, uid, res_id)
239
230
#        table_name = currency.currency_table_id and currency.currency_table_id.name or ''
240
231
#        return currency.name + table_name
241
 
 
 
232
            
242
233
res_currency()
243
234
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: