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

« back to all changes in this revision

Viewing changes to msf_tools/msf_tools.py

  • Committer: jf
  • Date: 2014-05-28 13:16:31 UTC
  • mto: This revision was merged to the branch mainline in revision 2187.
  • Revision ID: jfb@tempo-consulting.fr-20140528131631-13qcl8f5h390rmtu
UFTP-244 [FIX] In sync context, do not auto create the link between account.account and account.destination.link for default destination
this link is created by a dedicated sync rule

Show diffs side-by-side

added added

removed removed

Lines of Context:
28
28
from tools.translate import _
29
29
from dateutil.relativedelta import relativedelta
30
30
from datetime import datetime
 
31
from decimal import Decimal, ROUND_UP
31
32
 
32
33
import netsvc
33
34
 
351
352
                    # numbering value
352
353
                    start_num = start_num+1
353
354
                    if item_data[i][seq_field] != start_num:
354
 
                        dest_obj.write(cr, uid, [item_data[i]['id']], {seq_field: start_num}, context=context)
 
355
                        cr.execute("update "+dest_obj._table+" set "+seq_field+"=%s where id=%s", (start_num, item_data[i]['id']))
 
356
                        #dest_obj.write(cr, uid, [item_data[i]['id']], {seq_field: start_num}, context=context)
355
357
            
356
358
            # reset sequence to start_num + 1 all time, checking if needed would take much time
357
359
            # get the sequence id
501
503
 
502
504
 
503
505
ir_translation()
 
506
 
 
507
 
 
508
class uom_tools(osv.osv_memory):
 
509
    """
 
510
    This osv_memory class helps to check certain consistency related to the UOM.
 
511
    """
 
512
    _name = 'uom.tools'
 
513
 
 
514
    def check_uom(self, cr, uid, product_id, uom_id, context=None):
 
515
        """
 
516
        Check the consistency between the category of the UOM of a product and the category of a UOM.
 
517
        Return a boolean value (if false, it will raise an error).
 
518
        :param cr: database cursor
 
519
        :param product_id: takes the id of a product
 
520
        :param product_id: takes the id of a uom
 
521
        Note that this method is not consistent with the onchange method that returns a dictionary.
 
522
        """
 
523
        if context is None:
 
524
            context = {}
 
525
        if product_id and uom_id:
 
526
            if isinstance(product_id, (int, long)):
 
527
                product_id = [product_id]
 
528
            if isinstance(uom_id, (int, long)):
 
529
                uom_id = [uom_id]
 
530
            cr.execute(
 
531
                """
 
532
                SELECT COUNT(*)
 
533
                FROM product_uom AS uom,
 
534
                    product_template AS pt,
 
535
                    product_product AS pp,
 
536
                    product_uom AS uom2
 
537
                WHERE uom.id = pt.uom_id
 
538
                AND pt.id = pp.product_tmpl_id
 
539
                AND pp.id = %s
 
540
                AND uom2.category_id = uom.category_id
 
541
                AND uom2.id = %s""",
 
542
                (product_id[0], uom_id[0]))
 
543
            count = cr.fetchall()[0][0]
 
544
            return count > 0
 
545
        return True
 
546
 
 
547
uom_tools()
 
548
 
 
549
 
 
550
class product_uom(osv.osv):
 
551
    _inherit = 'product.uom'
 
552
 
 
553
    def _compute_round_up_qty(self, cr, uid, uom_id, qty, context=None):
 
554
        '''
 
555
        Round up the qty according to the UoM
 
556
        '''
 
557
        uom = self.browse(cr, uid, uom_id, context=context)
 
558
        rounding_value = Decimal(str(uom.rounding).rstrip('0'))
 
559
 
 
560
        return float(Decimal(str(qty)).quantize(rounding_value, rounding=ROUND_UP))
 
561
 
 
562
    def _change_round_up_qty(self, cr, uid, uom_id, qty, fields=[], result=None, context=None):
 
563
        '''
 
564
        Returns the error message and the rounded value
 
565
        '''
 
566
        if not result:
 
567
            result = {'value': {}, 'warning': {}}
 
568
 
 
569
        if isinstance(fields, str):
 
570
            fields = [fields]
 
571
 
 
572
        message = {'title': _('Bad rounding'),
 
573
                   'message': _('The quantity entered is not valid according to the rounding value of the UoM. The product quantity has been rounded to the highest good value.')}
 
574
 
 
575
        if uom_id and qty:
 
576
            new_qty = self._compute_round_up_qty(cr, uid, uom_id, qty, context=context)
 
577
            if qty != new_qty:
 
578
                for f in fields:
 
579
                    result.setdefault('value', {}).update({f: new_qty})
 
580
                result.setdefault('warning', {}).update(message)
 
581
 
 
582
        return result
 
583
 
 
584
product_uom()