-
-
Notifications
You must be signed in to change notification settings - Fork 729
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[ADD] allows to create zero quants for products based on the inventor…
…y configuration to avoid having no smartbutton on the adjustment form
- Loading branch information
Showing
7 changed files
with
331 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,71 +1,89 @@ | ||
from odoo import _, api, fields, models | ||
from odoo import fields, models | ||
|
||
|
||
class StockQuant(models.Model): | ||
_inherit = "stock.quant" | ||
|
||
to_do = fields.Boolean(default=False) | ||
virtual_in_progress_inventory_id = fields.Many2one( | ||
comodel_name="stock.inventory", | ||
compute="_compute_virtual_in_progress_inventory_id", | ||
) | ||
stock_inventory_ids = fields.Many2many( | ||
comodel_name="stock.inventory", | ||
string="Inventory Adjustment", | ||
readonly=True, | ||
) | ||
|
||
def _compute_virtual_in_progress_inventory_id(self): | ||
Inventory = self.env["stock.inventory"] | ||
for rec in self: | ||
rec.virtual_in_progress_inventory_id = Inventory.search( | ||
[ | ||
("state", "=", "in_progress"), | ||
"|", | ||
"&", | ||
("exclude_sublocation", "=", False), | ||
("location_ids", "parent_of", rec.location_id.ids), | ||
"&", | ||
("exclude_sublocation", "=", True), | ||
("location_ids", "in", rec.location_id.ids), | ||
], | ||
limit=1, | ||
) | ||
|
||
def _apply_inventory(self): | ||
res = super()._apply_inventory() | ||
record_moves = self.env["stock.move.line"] | ||
adjustment = self.env["stock.inventory"].browse() | ||
for rec in self: | ||
adjustment = ( | ||
self.env["stock.inventory"] | ||
.search([("state", "=", "in_progress")]) | ||
.filtered( | ||
lambda x: rec.location_id in x.location_ids | ||
or ( | ||
rec.location_id in x.location_ids.child_internal_location_ids | ||
and not x.exclude_sublocation | ||
) | ||
) | ||
) | ||
moves = record_moves.search( | ||
adjustment = rec.virtual_in_progress_inventory_id | ||
move = record_moves.search( | ||
[ | ||
("id", "in", adjustment.stock_move_ids.ids), | ||
("product_id", "=", rec.product_id.id), | ||
("lot_id", "=", rec.lot_id.id), | ||
"|", | ||
("location_id", "=", rec.location_id.id), | ||
("location_dest_id", "=", rec.location_id.id), | ||
], | ||
order="create_date asc", | ||
limit=1, | ||
).filtered( | ||
lambda x: not x.company_id.id | ||
or not rec.company_id.id | ||
or rec.company_id.id == x.company_id.id | ||
) | ||
if len(moves) == 0: | ||
raise ValueError(_("No move lines have been created")) | ||
move = moves[len(moves) - 1] | ||
adjustment.stock_move_ids |= move | ||
if not move: | ||
continue | ||
reference = move.reference | ||
if adjustment.name and move.reference: | ||
reference = adjustment.name + ": " + move.reference | ||
elif adjustment.name: | ||
reference = adjustment.name | ||
move.write( | ||
{ | ||
"inventory_adjustment_id": adjustment.id, | ||
"reference": reference, | ||
} | ||
) | ||
rec.to_do = False | ||
if adjustment and self.env.company.stock_inventory_auto_complete: | ||
if adjustment and adjustment.company_id.stock_inventory_auto_complete: | ||
adjustment.action_auto_state_to_done() | ||
return res | ||
|
||
def _get_inventory_move_values(self, qty, location_id, location_dest_id, out=False): | ||
res = super()._get_inventory_move_values( | ||
qty, location_id, location_dest_id, out=out | ||
) | ||
inventory = self.virtual_in_progress_inventory_id | ||
if self.virtual_in_progress_inventory_id: | ||
for move_line_item in res.get("move_line_ids", []): | ||
move_line_values = move_line_item[-1] | ||
move_line_values.update( | ||
{ | ||
"inventory_adjustment_id": inventory.id, | ||
} | ||
) | ||
return res | ||
|
||
def _get_inventory_fields_write(self): | ||
return super()._get_inventory_fields_write() + ["to_do"] | ||
|
||
@api.model_create_multi | ||
def create(self, vals_list): | ||
res = super().create(vals_list) | ||
if self.env.context.get( | ||
"active_model", False | ||
) == "stock.inventory" and self.env.context.get("active_id", False): | ||
self.env["stock.inventory"].browse( | ||
self.env.context.get("active_id") | ||
).refresh_stock_quant_ids() | ||
return res |
Oops, something went wrong.