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

When I confirm an order (sale.order) then it creates a stock.picking related to this order (when I confirm the sale order). I have a new field "insurance" at stock.picking which is filled when I select a carrier and then sums values of all lines excluding shipping and puts it in this field. I launch this with an @api_onchange method related to the carrier_id of the stock.picking


I want to directly set this value when stock.picking is created (when confirm an order clicking in Confirm button). How can I do this? I didn't find where the stock.picking is being created.


I'm using OCA delivery carrier branch and odoo v13

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

I finally did it work. I inherit sale.order and here I overwrite action_confirm method. Then I search inside self.picking_ids and here I can sum all lines excluding shipping method.

I'm not sure why picking doesn't exists if I try the solution proposed by Hamrul, but this works fine.

    def action_confirm(self):
        res = super(SaleOrder, self).action_confirm()
        self.ensure_one()
        for rec in self.picking_ids:
            # only if carrier is ups and have insurance
            if rec.carrier_id.delivery_type == "ups" and rec.carrier_id.ups_insurance:
                ups_insurance = rec.carrier_id.ups_insurance
                ups_insurance_value = sum(
                    sl.price_subtotal # value untaxed to sum
                    for ml in rec.move_lines
                    for sl in ml.sale_line_id
                    if "UPS" not in sl.name # if line name doesn't contain "UPS" in capital letters
                )
                rec.ups_insurance = ups_insurance,
                rec.ups_insurance_value = ups_insurance_value

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

You need to inherit confirm button action and write to your new field "insurance" on stock.picking model via carrier_id.

def action_confirm(self):
for rec in self:
rec.carrier_id.write({'insurance': self.insurance})
return super([Class_name], self).action_confirm()


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


Manuel González
hace 2 segundos Autor

It doesn't work. It never goes inside the for loop. I guess in this point stock.picking is still void or something like that.
If I print self I get stock.picking() but it doesn't print anything inside the for loop.

def action_confirm(self):
import logging
_logger = logging.getLogger(__name__)
res = super(StockPicking, self).action_confirm()
_logger.critical(self) #it prints 'stock.picking()'
for rec in self:
_logger.critical('hello') #print nothing
_logger.critical(rec) #print nothing
[...]

when you click confirm button as excepted result 'stock.picking(11,)' not 'stock.picking()'', please make sure function inherit right way.

Tác giả

The inherited function is done correctly, as far as I can see, it has declared the inheritance of stock.picking.

class StockPicking(models.Model):
_inherit = "stock.picking"

and I get the result I showed you before inside the action_confirm function.

I guess to make work this i should make inheritance with sale.order and, in the action_confirm function, iterate the self.picking_ids to find the pickings and work with them?

Bài viết liên quan Trả lời Lượt xem Hoạt động
0
thg 7 16
3361
1
thg 6 22
5118
0
thg 6 24
886
1
thg 3 24
2379
1
thg 6 22
2280