Skip to Content
Menu
This question has been flagged
2 Replies
7989 Views

Hi,

how to auto validate stock.picking from API python / python code? i call the function "button_validate" in stock.picking model, but it doesn't work. Below my python code:


sales_order_object = request.env['sale.order'].search(
[('x_studio_agsi_order_id', '=', new_delivery_order['order_id'])])

for picking in sales_order_object.picking_ids:
picking.action_assign()
picking.action_confirm()
for move in picking.move_lines.filtered(
lambda m: m.state not in ["done", "cancel"]
):
rounding = move.product_id.uom_id.rounding
if (
float_compare(
move.quantity_done,
move.product_qty,
precision_rounding=rounding,
)
== -1
):
for move_line in move.move_line_ids:
move_line.qty_done = move_line.product_uom_qty

picking.button_validate()


Thanks

Avatar
Discard
Best Answer

Hello,

You may try the below example:
immediate_transfer_line_ids = []
for picking in sales_order_object.picking_ids:
    immediate_transfer_line_ids.append([0, False, {
        'picking_id': picking.id,
        'to_immediate': True
    }])

res = self.env['stock.immediate.transfer'].create({
    'pick_ids': [(4, p.id) for p in sales_order_object.picking_ids],
    'show_transfers': False,
    'immediate_transfer_line_ids': immediate_transfer_line_ids
})

return res.with_context(button_validate_picking_ids=res.pick_ids.ids).process()

Avatar
Discard

Hi Matiar,
For Odoo version 13, I use below code and it runs.(I am not sure if button_validate_picking_ids in context work for anything)

res = self.env['stock.immediate.transfer'].create({
'pick_ids': [(4, p.id) for p in sales_order_object.picking_ids],
})
return res.with_context(button_validate_picking_ids=res.pick_ids.ids).process()

But there is the problem the code ends at return so nothing is done after return line if everything is ok.

Hi again,
I am working on despatch document that contains stock moves. And I want to validate the stock moves when I validate despatch document.(change despatch document state to "sent")
Below is the code I used at Odoo version 13:
class TheDespatch(models.Model):
_name = "the_despatch_document"

def button_confirm(self):
immediate_transfer_line_ids = []
for line in self.move_lines:
if line.state != 'done':
immediate_transfer_line_ids.append([0, False, {
'picking_id': line.picking_id.id,
'to_immediate': True
}])
res = self.env['stock.immediate.transfer'].create({
'pick_ids': [(4, p.picking_id.id) for p in self.move_lines],

})
return res.with_context(button_validate_picking_ids=res.pick_ids.ids,despatch_document=self.id ).process()

class StockBackorderConfirmation(models.TransientModel):
_inherit = 'stock.backorder.confirmation'

def _process(self, cancel_backorder=False):
res = super(StockBackorderConfirmation,self)._process(cancel_backorder=False)
despatch_document = self.env['the_despatch_document'].search([('id','=',self.env.context.get('despatch_document'))])
despatch_document.write({'state': 'sent'})
return res

Hope it helps

Can this code be used in Odoo studio automated action?

Best Answer

Hello ,

Is there any error ? how do you call your for loop? can you be more specific? In order to validate its enough to call picking.action_confirm() and picking.button_validate(). First please debug(print and see) to check whether you get any id's in 'sales_order_object' and in 'sales_order_object.picking_ids'

Avatar
Discard
Author

Hello Karthikeyan, thanks for your response.

I've tried debugging it by print id's sales_order_object and sales_order_object.picking_ids, I got the id's. As follows:

2021-02-18 07:50:32,931 29466 WARNING DB_gsi_2021-01-26 py.warnings: /opt/odoo14-e/odoo/addons/x_gsi_web_api/lib/jwt/api_jws.py:145: DeprecationWarning: It is strongly recommended that you pass in a value for the "algorithms" argument when calling decode(). This argument will be mandatory in a future version.

DeprecationWarning

sale.order(576,)

stock.picking(664,)

2021-02-18 07:50:33,073 29466 INFO DB_gsi_2021-01-26 odoo.modules.registry: Registry changed, signaling through the database

2021-02-18 07:50:33,076 29466 INFO DB_gsi_2021-01-26 werkzeug: 127.0.0.1 - - [18/Feb/2021 07:50:33] "POST /api/v1/post/delivery-order HTTP/1.1" 200 - 108 0.077 7.147

Then I've also tried just calling "picking.action_confirm()" and "picking.button_validate()", but that didn't return anything. I used the for loop above to change the qty_done in stock.move.line to be the same as the reserved qty (product_uom_qty).

Author

I'm creating an rest API for Odoo, with the following scenario:

- First, I create a rest API for: quotation -> sales order -> invoice down payment

- Second, after the first sales order is created until the invoice down payment, the next step is to make a delivery (delivery order). To make deliveries, I create a separate rest API by looking for which sales orders to send. It is a sales order whose x_studio_agsi_order_id is the same as that entered by the user. After the sales order is found, I want to validate the delivery order. But it didn't work.

Author

But it's different if the scenario is as follows:

Quotation - sales order - delivery - invoice.

If this scenario, only one rest API is used. So, from the rest API, it immediately generates sales orders to invoices. At the delivery order stage, I validated it with the same function as above. It worked.

Hmm, I've tried many times in the first scenario, which is a scenario that uses two separate rest API. But still it didn't work.