~tempo-openerp/+junk/axima_ax_64

« back to all changes in this revision

Viewing changes to sale_order_global_discount.py

  • Committer: vg at tempo-consulting
  • Date: 2014-10-01 13:09:48 UTC
  • Revision ID: vg@tempo-consulting.fr-20141001130948-404eilrv4lwbdije
[IMP] sale order: remise globale saisie en pourcentage qui calcule le montant globale de la remise

Show diffs side-by-side

added added

removed removed

Lines of Context:
68
68
 
69
69
        return res
70
70
 
 
71
    def _get_lines_total_amount(self, cr, uid, ids, context=None):
 
72
        """
 
73
        :return : { 'id', lines_total, ... }
 
74
        """
 
75
        res = {}
 
76
        if not ids:
 
77
            return res
 
78
        if isinstance(ids, (int, long)):
 
79
            ids = [ids]
 
80
        for so in self.browse(cr, uid, ids, context=context):
 
81
            amount = 0.
 
82
            for sl in so.order_line:
 
83
                if sl.price_subtotal:
 
84
                    amount += sl.price_subtotal
 
85
            res[so.id] = amount
 
86
        return res
 
87
 
71
88
    _columns = {
72
89
        'global_discount': fields.float(
73
90
            string='Remise globale',
215
232
        return super(sale_order, self).button_dummy(cr, uid, ids, context=context)
216
233
 
217
234
    def on_change_global_discount_rate(self, cr, uid, ids, global_discount_rate,
218
 
        amount_untaxed, context = None):
 
235
        context=None):
219
236
        """
220
237
        from a user input of global discount rate, compute the global rate
221
238
        amount truely used by the global amount system
222
239
        """
223
 
        res = {}
224
 
        discount_amount = 0.
225
 
        if amount_untaxed and amount_untaxed > 0.:
226
 
            discount_amount = amount_untaxed * global_discount_rate
227
 
        res['value'] = { 'global_discount': discount_amount, }
 
240
        if not ids:
 
241
            return {}
 
242
        if isinstance(ids, (int, long)):
 
243
            ids = [ids]
 
244
        lines_total = self._get_lines_total_amount(cr, uid, ids,
 
245
            context=context)[ids[0]]  # get so lines total to compute from
 
246
 
 
247
        discount_amount = 0
 
248
        if global_discount_rate and \
 
249
            isinstance(global_discount_rate, (str, unicode)):
 
250
            # % widget conversion 2 float
 
251
            global_discount_rate = float(global_discount_rate.replace(',', '.'))
 
252
        if not global_discount_rate:
 
253
            global_discount_rate = 0.
 
254
        if lines_total and lines_total > 0.:
 
255
            discount_amount = (lines_total * global_discount_rate) / 100
 
256
        err = False
 
257
        if discount_amount > lines_total:
 
258
            err = 'La remise globale ne peut dépasser le montant'
 
259
        elif discount_amount < 0:
 
260
            err = 'La remise globale ne peut être négative'
 
261
        if err:
 
262
            res = {
 
263
                'value': {'global_discount_rate': 0., 'global_discount': 0.,},
 
264
                'warning': {
 
265
                    'title': 'Remise globale',
 
266
                    'message': err,
 
267
                },
 
268
            }
 
269
        else:
 
270
            res = {
 
271
                'value': {'global_discount': discount_amount, },
 
272
            }
228
273
        return res
229
274
 
230
275