Skip ke Konten
Menu
Pertanyaan ini telah diberikan tanda
2 Replies
2897 Tampilan

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

Avatar
Buang
Penulis Jawaban Terbai

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
Avatar
Buang
Jawaban Terbai

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()


Avatar
Buang
Penulis


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.

Penulis

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?

Post Terkait Replies Tampilan Aktivitas
0
Jul 16
3317
1
Jun 22
5058
0
Jun 24
864
1
Mar 24
2329
1
Jun 22
2212