Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[IMP] Add option to cancel merged source inventories #113

Open
wants to merge 1 commit into
base: 12.0
Choose a base branch
from

Conversation

azoellner
Copy link

For module "stock_inventory_merge" in version 12.0, this branch adds the option to cancel the merged source inventories.

The "Merge Inventories" wizard will have a checkbox "Cancel source inventories?" When checked, the source inventories will get into state "cancel" during the merge.

Note that the Odoo core method action_cancel_draft() sets the stock.inventory to state "draft" and deletes the inventory lines:
https://github.com/odoo/odoo/blob/6b24df0e037d64be0d4e48045e47215059bffddb/addons/stock/models/stock_inventory.py#L209-L214
But instead, we want to have state "draft" and keep the lines.

@codecov
Copy link

codecov bot commented Jun 23, 2021

Codecov Report

Merging #113 (fd2cfdd) into 12.0 (1674aac) will increase coverage by 0.07%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##             12.0     #113      +/-   ##
==========================================
+ Coverage   72.31%   72.39%   +0.07%     
==========================================
  Files          66       66              
  Lines        1510     1514       +4     
==========================================
+ Hits         1092     1096       +4     
  Misses        418      418              
Impacted Files Coverage Δ
...ntory_merge/models/wizard_stock_inventory_merge.py 89.65% <100.00%> (+1.65%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 1674aac...fd2cfdd. Read the comment docs.

@legalsylvain
Copy link
Member

Hi @azoellner ! Thanks a lot for your 2 contributions !
makes sense. I'm quite busy just right now, but I'll make review and merge during the summer.

kind regards.

Copy link
Member

@legalsylvain legalsylvain left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A design remark. Otherwise, LGTM, thanks !

  1. you can add you in the author / credit of the module if you want.

  2. As this module seems interesting for other people (out of GRAP), we can imagine share this module in the OCA repo : github.com/oca/stock-logistics-warehouse What do you think ?

@@ -51,6 +55,12 @@ def action_merge(self):
for line in inventories.mapped("line_ids"):
line.copy(default={"inventory_id": inventory.id})

if self.cancel_src:
# Note: `inventories.action_cancel_draft()` would remove the lines
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi. regarding that point, what do you think about a simple OCA module stock_inventory_draft_no_unlink that overwrite the function action_cancel_draft() like

    def action_cancel_draft(self):
        self.mapped('move_ids')._action_cancel()
        self.write({
            'state': 'draft'
        })

And then, here, only call action_cancel_draft(). I see that change quite generic and not linked to that specific module (stock_inventory_merge)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. But be aware that in my PR, we would like to have state cancel, not draft. But this could be easily solved.

I don't know the rationale of the Odoo core team for removing the lines when resetting the inventory to draft.
I mean, if the user wants to reset the inventory, they could always remove the lines manually afterwards.
IMHO this method violates the single-responsibility principle.

So yes, such a module "stock_inventory_draft_no_unlink" might be in order. Possibly providing also a separate method action_cancel() for model stock.inventory.

def action_cancel(self):
    self.mapped('move_ids')._action_cancel()
    self.write({
        'state': 'cancel'
    })

def action_cancel_draft(self):
    self.action_cancel()
    self.write({
        'state': 'draft'
    })

Also, I agree that this module "stock_inventory_merge" itself could also make a nice addition to OCA's stock-logistics-warehouse.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for your answer, and sorry again for the delay.

IMHO this method violates the single-responsibility principle.

Indeed !

well, I think that there is a little problem on Odoo / Core :

  1. State 'draft' seems to be a "dead" state. I mean, it exist in the selection, but I don't see any place where an inventory can reach to that state (in Odoo Core). maybe it has been kept for legacy reason. What do you think ?
  2. inventories.mapped('move_ids')._action_cancel() seems to be useless (in Odoo core I mean, in action_cancel_draft) because at this step, the inventory is not confirmed, so there are no move_ids. What do you think ?

Due to 1. if we set the inventory to the "cancel" state, it is not possible to reset to draft. (except if we implement an other function "action_reset_to_draft") but we can also assume that the inventory is definitively canceled.

so i'm quite perplex !

Except of that, 👍 for that PR. Thanks !

Copy link
Author

@azoellner azoellner Nov 7, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thinking about it again, the state 'draft' normally occurs only when initially creating a new "stock.inventory". Such an inventory does not have any lines yet. Only when starting it, which means progressing into state 'confirm' = "In Progess", the user can add lines.
So it makes indeed sense to remove the lines when resetting into state draft.
However, when merging inventories, the original ones should (IMHO) keep their lines. Consequently, this would then mean to not reset them back to state 'draft', but rather to keep them in 'confirm'. Or set them to 'cancel', as per the suggested option.
Would you agree?

This would then mean

def action_cancel(self):
    # cancel all moves
    self.mapped('move_ids')._action_cancel()
    self.write({
        'state': 'cancel',
    })

def action_cancel_draft(self):
    # first cancel
    self.action_cancel()
    # then set into 'draft', which implies inventory has no lines
    self.write({
        'line_ids': [(5,)],
        'state': 'draft'
    })

Regarding your second point: Yes, for confirmed, but not yet validated inventories, the self.mapped('move_ids')._action_cancel() is useless, since the inventory does not yet have any stock moves.
However, the method may also be called on validated inventories, and in this case cancelling the stock moves is necessary.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants