This question has been flagged
1 Reply
3455 Views

I am working on creating a module that will allow a minimum price for a sales order line to be set, but am having some trouble getting it working. I started by defining a minimium lot charge value in product.product:

class ec_products(osv.Model):

    _inherit = 'product.product'

    _columns = {

        'x_min_lot_charge':fields.float('Min Lot Charge', size=64),

        }

Added it to the view without issue, so this code is working fine. I then inherited sale.order.line and and modified the _amount_line() method, which is where things aren't working. This is my code currently:

class sales_order_detail_field(osv.Model):

    _inherit = 'sale.order.line'

    def _amount_line(self, cr, uid, ids, field_name, arg, context=None):

        tax_obj = self.pool.get('account.tax')

        cur_obj = self.pool.get('res.currency')

        res = {}

        if context is None:

            context = {}

        for line in self.browse(cr, uid, ids, context=context):

            price = line.price_unit * (1 - (line.discount or 0.0) / 100.0)

            taxes = tax_obj.compute_all(cr, uid, line.tax_id, price, line.product_uom_qty, line.product_id, line.order_id.partner_id)

            ###  Min lot charge calc

            line_min = line.product_id.x_min_lot_charge

            if taxes['total'] < line_min:

                taxes['total']= line_min

            ###

            cur = line.order_id.pricelist_id.currency_id

            res[line.id] = cur_obj.round(cr, uid, cur, taxes['total'])

        return res

So basically I grab the minimum lot charge that is definied on the product page using line_min = line.product_id.x_min_lot_charge, then compare that to the calulated subtotal, taxes['total'], and set that value to the minimum charge if it is smaller. Seems pretty simple, but it isn't working. I tried removing the if statement to see if I could just set the subtotal to the value defined on the product, but that didn't work either. The subtotal field I am trying to modify is defined in sale.py as a functional field relating to the _amount_line method that I am editing above:

'price_subtotal': fields.function(_amount_line, string='Subtotal', digits_compute= dp.get_precision('Account'))

Any idea what I am doing wrong here? Any advice would be greatly appreciated!

Avatar
Discard

I wanted to implement a minimum product quantity using the same approach. I have created a field, 'min_quantity' under product.product model. Do you have an idea on how I can set it as default in sale.order.line and ensure that it is the minimum value that a user can set?

Best Answer

Probably method sales_order_detail_field() is not called. You need to define again

'price_subtotal': fields.function(_amount_line, string='Subtotal', digits_compute= dp.get_precision('Account'))

in your module.

Avatar
Discard
Author

That's exactly it! Also I had to define the function before the _columns dictionary for it to work correctly. Thanks for your help zbik!