[7121][ADD] stock_svl_removal_date: store the removal date of the lots on stock valuation layers - #260
[7121][ADD] stock_svl_removal_date: store the removal date of the lots on stock valuation layers#260nobuQuartile wants to merge 2 commits into
Conversation
…tock valuation layers The valuation layers cannot be filtered or grouped by removal date, because the standard Lot/Serial Number field of a layer is only filled for products with lot valuation enabled. Store the removal date on the layer instead, taken from the lots of the stock move so that it works regardless of that setting, and keep the earliest one when the move holds several lots. Existing layers are filled in SQL by a pre-install hook rather than by the ORM, so that installing the module stays fast on databases holding a large number of valuation layers. task-7121
|
Ready to review |
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: b83eb702be
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
| self.env["stock.move.line"] | ||
| .search([("lot_id", "in", self.ids), ("quantity", "!=", 0)]) | ||
| .move_id |
There was a problem hiding this comment.
Sudo the move-line lookup for shared lots
When a company-neutral lot is used by multiple companies and a user updates it with only one company enabled, this search remains subject to stock.move.line company record rules because it does not inherit the sudo() applied to the separate layers recordset. Consequently, moves from the other companies are omitted and their valuation layers without a direct lot_id retain the old removal date, despite this method explicitly intending to realign cross-company layers. Perform this lookup with elevated access as well.
Useful? React with 👍 / 👎.
|
Add ja.po Translation notesTranslations are referenced from existing Odoo translations where available (odoo/product_expiry, odoo/stock_account). The following terms have no reference and are translated independently:
Terminology follows |
| removal_date = fields.Datetime( | ||
| readonly=True, | ||
| index=True, | ||
| help="Removal date of the lot/serial of the stock move. When the move " | ||
| "holds several lots, the earliest removal date is kept.", | ||
| ) |
There was a problem hiding this comment.
@nobuQuartile I think current implementation is bloated. I believe below code can cover the functionality of current PR.
| removal_date = fields.Datetime( | |
| readonly=True, | |
| index=True, | |
| help="Removal date of the lot/serial of the stock move. When the move " | |
| "holds several lots, the earliest removal date is kept.", | |
| ) | |
| removal_date = fields.Datetime( | |
| compute="_compute_removal_date", store=True, index=True, compute_sudo=True, | |
| help="Removal date of the lot/serial of the stock move. When the move " | |
| "holds several lots, the earliest removal date is kept.", | |
| ) | |
| @api.depends("lot_id.removal_date", "stock_move_id.move_line_ids.lot_id.removal_date") | |
| def _compute_removal_date(self): | |
| for layer in self: | |
| # Layers of a lot valuated product carry the lot themselves; the | |
| # others take the earliest removal date of their move's lots. | |
| lots = layer.lot_id or layer.stock_move_id.lot_ids | |
| layer.removal_date = min(filter(None, lots.mapped("removal_date")), default=False) |
There was a problem hiding this comment.
It's good!
Maybe we should create this as an OCA Module, shouldn't we?
If you can, could you create the OCA PR?
|
@nobuQuartile Please close this PR. I created in hls-oca. |
QT7121
Adds a Removal Date field to stock valuation layers, taken from the lots/serials of the related stock move.
The standard Lot/Serial Number field of a valuation layer is only filled for products with lot valuation enabled, so the lots are read from the stock move (
stock_move_id.lot_ids) instead, which works regardless of that setting. When the move holds several lots, the earliest removal date is kept.The field is stored and indexed, so that the layers can be filtered, sorted and grouped by removal date from Inventory > Reporting > Valuation. Existing layers are filled in SQL by a pre-install hook rather than by the ORM, which keeps the installation fast even on databases holding a large number of valuation layers.
Notes for review:
stock.lot.write()comparesremoval_datebefore and aftersuper()rather than looking for the key invals.removal_dateis a stored computed field ofproduct_expiry, so it also changes as a side effect of writingexpiration_dateorproduct_id; such a recomputation is flushed through_write_multi()and never reaches thewrite()override.test_expiration_date_change_realigns_layersis the regression guard for this.sudo()in_update_layer_removal_date()is required becausestock.valuation.layeris only writable by stock managers while lot dates are maintained by any stock user.