Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
1 Trả lời
7184 Lượt xem
0

I have this function that calculates qty_incoming, but there is an outgoing_qty field that I want to calculate with the same function and not to create separate function for its calculation. how can I do this?

 _columns = {
            '
            'incoming_qty': fields.function(_product_inc_out_qty, type='float',
                digits_compute=dp.get_precision('Product Unit of Measure'),
                string='Incoming'
            ),
            'outgoing_qty': fields.function(_product_inc_out_qty, type='float',
                digits_compute=dp.get_precision('Product Unit of Measure'),
                string='Outgoing'
            ),
        }

function:

def _product_inc_out_qty(self, cr, uid, ids, field_names=None, arg=False, context=None):
        if context is None:
            context = {}

        res = {}
        for move_id in ids:
            move = self.browse(cr, uid, move_id, context=context)

            res[move.id] = move.product_id.incoming_qty or 0.0
        return res



if I do something like this, then I get error TypeError: float() argument must be a string or a number



def _product_inc_out_qty(self, cr, uid, ids, field_names=None, arg=False, context=None):
        if context is None:
            context = {}

        res = {}
        vals = {
            'outgoing_qty': 0.0,
            'incoming_qty': 0.0,


        }
        for move_id in ids:
            move = self.browse(cr, uid, move_id, context=context)
            vals['outgoing_qty'] = move.product_id.qty_available or 0.0
            vals['incoming_qty'] = move.product_id.incoming_qty or 0.0
            res[move.id] = vals

        return res

Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

Hi,

In the sales module you can see similar sample in which for multiple field values are computed using the same function, have a look at those function and you can update your case.


See the sample:

amount_tax = fields.Monetary(string='Taxes', store=True, readonly=True, compute='_amount_all')
amount_total = fields.Monetary(string='Total', store=True, readonly=True, compute='_amount_all', track_visibility='always', track_sequence=6)

Compute function:

@api.depends('order_line.price_total')
def _amount_all(self):
"""
Compute the total amounts of the SO.
"""
for order in self:
amount_untaxed = amount_tax = 0.0
for line in order.order_line:
amount_untaxed += line.price_subtotal
amount_tax += line.price_tax
order.update({
'amount_untaxed': amount_untaxed,
'amount_tax': amount_tax,
'amount_total': amount_untaxed + amount_tax,
})


You can see same code in v8 here in this file: https://github.com/odoo/odoo/blob/8.0/addons/sale/sale.py

Thanks

Ảnh đại diện
Huỷ bỏ
Tác giả

I updated my question, I tried something similar but getting error

Tác giả

TypeError: float() argument must be a string or a number

Bài viết liên quan Trả lời Lượt xem Hoạt động
Expected singleton Đã xử lý
9
thg 3 20
42039
2
thg 10 18
3954
1
thg 10 17
4210
1
thg 3 15
4653
2
thg 3 15
6719