Source

trytond-product_price_list_formula / price_list.py

#This file is part product_price_list_formula module for Tryton.
#The COPYRIGHT file at the top level of this repository contains 
#the full copyright notices and license terms.

from trytond.model import ModelView, ModelSQL, fields
from trytond.tools import safe_eval, datetime_strftime
from trytond.transaction import Transaction
from trytond.pool import Pool, PoolMeta

from decimal import Decimal

__all__ = ['PriceList', 'PriceListLine']
__metaclass__ = PoolMeta

class PriceList:
    __name__ = 'product.price_list'

    @classmethod
    def __setup__(cls):
        super(PriceList, cls).__setup__()
        cls._error_messages.update({
            'not_found_price_list': 'Not found price list: %s!',
        })

    def _get_context_price_list_line(self, party, product, unit_price,
            quantity, uom):
        '''
        Add party and product available in formula

        :param party: the BrowseRecord of the party.party
        :param product: the BrowseRecord of the product.product
        :param unit_price: a Decimal for the default unit price in the
            company's currency and default uom of the product
        :param quantity: the quantity of product
        :param uom: the BrowseRecord of the product.uom
        :return: a dictionary
        '''
        res = super(PriceList, self)._get_context_price_list_line(
            party, product, unit_price, quantity, uom)
        res['product'] = product
        res['party'] = party
        res['quantity'] = quantity
        res['price_list'] = Pool().get('product.price_list')
        return res

    @classmethod
    def compute_price_list(self, price_list):
        '''
        Compute price based another price list

        :param price_list: the price list id or the BrowseRecord of the
            product.price_list
        :return: the computed unit price
        '''
        if isinstance(price_list, (int, long)):
            price_list = self(price_list)

        try:
            price_list.name
        except:
            self.raise_user_error('not_found_price_list', price_list)

        quantity=0
        product = Transaction().context['product']
        return self.compute(
                        price_list,
                        Transaction().context['customer'],
                        product, 
                        Transaction().context['unit_price'],
                        quantity,
                        Transaction().context.get('uom', product.default_uom))


class PriceListLine:
    'Price List Line'
    __name__ = 'product.price_list.line'

    def check_formula(self):
        '''
        Check formula
        Add new params test in context: product, customer and price list object
        '''
        pool = Pool()
        PriceList = pool.get('product.price_list')
        context = PriceList()._get_context_price_list_line(None, None,
                Decimal('0.0'), 0, None)

        product = pool.get('product.product').search([
            ], 0, 1, None)[0]
        context['product'] =  pool.get('product.product')(product)
        customer = pool.get('party.party').search([], 0, 1, None)[0]
        context['customer'] = pool.get('party.party')(customer)
        context['price_list'] = pool.get('product.price_list')

        with Transaction().set_context(**context):
            try:
                if not isinstance(self.get_unit_price(), Decimal):
                    return False
            except Exception:
                return False
        return True
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.