forked from OCA/stock-logistics-workflow
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stock_picking.py
111 lines (100 loc) · 4.53 KB
/
stock_picking.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# Copyright 2013-2015 Camptocamp SA - Nicolas Bessi
# Copyright 2018 Camptocamp SA - Julien Coux
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
from odoo import _, models
from odoo.exceptions import UserError
class StockPicking(models.Model):
"""Adds picking split without done state."""
_inherit = "stock.picking"
def split_process(self, keep_lines=False):
"""Use to trigger the wizard from button with correct context"""
for picking in self:
# Check the picking state and condition before split
if picking.state == "draft":
raise UserError(_("Mark as todo this picking please."))
if all([x.qty_done == 0.0 for x in picking.move_line_ids]):
raise UserError(
_(
"You must enter done quantity in order to split your "
"picking in several ones."
)
)
# Split moves considering the qty_done on moves
new_moves = self.env["stock.move"]
new_move_lines = self.env["stock.move.line"]
for move in picking.move_lines:
qty_done = move.quantity_done
if qty_done:
qty_uom_split = move.product_uom._compute_quantity(
qty_done, move.product_id.uom_id, rounding_method="HALF-UP"
)
new_move_id = move._split(qty_uom_split)
for move_line in move.move_line_ids:
if move_line.product_qty and move_line.qty_done:
if keep_lines:
new_move_line_id = move_line._split(
move_line.qty_done, new_move_id
)
new_move_lines |= self.env["stock.move.line"].browse(
new_move_line_id
)
else:
# To avoid an error
# when picking is partially available
try:
move_line.write(
{"product_uom_qty": move_line.qty_done}
)
except UserError:
pass
new_moves |= self.env["stock.move"].browse(new_move_id)
# If we have new moves to move, create the backorder picking
if new_moves:
backorder_picking = picking._create_split_backorder()
new_moves.write({"picking_id": backorder_picking.id})
if new_move_lines:
new_move_lines.write({"picking_id": backorder_picking.id})
new_moves._recompute_state()
else:
new_moves._action_assign()
return backorder_picking
def _create_split_backorder(self, default=None):
"""Copy current picking with defaults passed, post message about
backorder"""
self.ensure_one()
backorder_picking = self.copy(
dict(
{
"name": "/",
"move_lines": [],
"move_line_ids": [],
"backorder_id": self.id,
},
**(default or {})
)
)
self.message_post(
body=_(
'The backorder <a href="#" '
'data-oe-model="stock.picking" '
'data-oe-id="%d">%s</a> has been created.'
)
% (backorder_picking.id, backorder_picking.name)
)
return backorder_picking
def _split_off_moves(self, moves):
"""Remove moves from pickings in self and put them into a new one"""
new_picking = self.env["stock.picking"]
for this in self:
if this.state in ("done", "cancel"):
raise UserError(
_("Cannot split picking %s in state %s") % (this.name, this.state,)
)
new_picking = new_picking or this._create_split_backorder()
if not this.move_lines - moves:
raise UserError(
_("Cannot split off all moves from picking %s") % this.name
)
moves.write({"picking_id": new_picking.id})
moves.mapped("move_line_ids").write({"picking_id": new_picking.id})
return new_picking