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

[ADD] account_move_stock_create: new addon #1827

Draft
wants to merge 1 commit into
base: 14.0
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 89 additions & 0 deletions account_move_stock_create/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
=========================
Account Move Stock Create
=========================

..
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! source digest: sha256:d6d4854666e5badaa6d03f574dd80462df7efbed75f3b700eddbba2277927b10
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Beta-yellow.png
:target: https://odoo-community.org/page/development-status
:alt: Beta
.. |badge2| image:: https://img.shields.io/badge/licence-AGPL--3-blue.png
:target: http://www.gnu.org/licenses/agpl-3.0-standalone.html
:alt: License: AGPL-3
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Faccount--invoicing-lightgray.png?logo=github
:target: https://github.com/OCA/account-invoicing/tree/14.0/account_move_stock_create
:alt: OCA/account-invoicing
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/account-invoicing-14-0/account-invoicing-14-0-account_move_stock_create
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/builds?repo=OCA/account-invoicing&target_branch=14.0
:alt: Try me on Runboat

|badge1| |badge2| |badge3| |badge4| |badge5|

This module enables the creation of stock transfers directly from invoices, streamlining the inventory update process and ensuring alignment between sales and stock levels.

**Table of contents**

.. contents::
:local:

Installation
============

This module depends on stock_picking_invoice_link. You can find it at [OCA/stock-logistics-workflow](https://github.com/OCA/stock-logistics-workflow)

Usage
=====

#. Go to *Invoices* then either *Customer > Invoices* or *Vendor > Bills*.
#. Select one invoice or bill..
#. Click on *Action > Create Stock Picking*.
#. Pickings are linked to invoices using *stock_picking_invoice_link* from [OCA/stock-logistics-workflow](https://github.com/OCA/stock-logistics-workflow)

Bug Tracker
===========

Bugs are tracked on `GitHub Issues <https://github.com/OCA/account-invoicing/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us to smash it by providing a detailed and welcomed
`feedback <https://github.com/OCA/account-invoicing/issues/new?body=module:%20account_move_stock_create%0Aversion:%2014.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.

Do not contact contributors directly about support or help with technical issues.

Credits
=======

Authors
~~~~~~~

* KMEE INFORMATICA LTDA

Contributors
~~~~~~~~~~~~

* Diego Paradeda <[email protected]>

Maintainers
~~~~~~~~~~~

This module is maintained by the OCA.

.. image:: https://odoo-community.org/logo.png
:alt: Odoo Community Association
:target: https://odoo-community.org

OCA, or the Odoo Community Association, is a nonprofit organization whose
mission is to support the collaborative development of Odoo features and
promote its widespread use.

This module is part of the `OCA/account-invoicing <https://github.com/OCA/account-invoicing/tree/14.0/account_move_stock_create>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
1 change: 1 addition & 0 deletions account_move_stock_create/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
21 changes: 21 additions & 0 deletions account_move_stock_create/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright (C) 2024-Today - KMEE (<http://www.kmee.com.br>).
# @author Diego Paradeda <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"name": "Account Move Stock Create",
"summary": """This addon creates stock transfers from an account move""",
"version": "14.0.1.0.0",
"license": "AGPL-3",
"author": "KMEE INFORMATICA LTDA,Odoo Community Association (OCA)",
Copy link
Member

Choose a reason for hiding this comment

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

KMEE only plz.

"website": "https://github.com/OCA/account-invoicing",
"depends": [
"stock",
"account",
"stock_picking_invoice_link",
],
"data": [
"views/account_move_views.xml",
],
"demo": [],
}
1 change: 1 addition & 0 deletions account_move_stock_create/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import account_move
100 changes: 100 additions & 0 deletions account_move_stock_create/models/account_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
# Copyright (C) 2024-Today - KMEE (<http://www.kmee.com.br>).
# Author: Diego Paradeda <[email protected]>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import _, models
from odoo.exceptions import UserError

INVOICE_TYPE_MAP = {
# Account Move Type | Picking Type Code | Local Origin Usage | Local Dest Usage
"in_invoice": ("incoming", "supplier", "internal"),
"in_refund": ("outgoing", "internal", "supplier"),
"out_invoice": ("outgoing", "internal", "customer"),
"out_refund": ("incoming", "customer", "internal"),
}


class AccountInvoice(models.Model):
_inherit = "account.move"

def action_generate_pickings_from_invoices(self):
"""Generate pickings from invoices."""
for record in self:
if record.picking_ids and len(self) == 1:
raise UserError(

Check warning on line 24 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L24

Added line #L24 was not covered by tests
_("There's already a picking created for this account move.")
)
elif record.picking_ids:
continue
record.generate_picking_from_invoice()

Check warning on line 29 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L28-L29

Added lines #L28 - L29 were not covered by tests
Copy link
Member

Choose a reason for hiding this comment

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

It is common to have more than one picking grouped in the same invoice...

But also more generally, I think it would be cool to have a wizard or maybe an o2m field were one could select existing pickings. If so, the module could try to match the existing stock moves with some kind of key (product_id, possibly quantity...) and simply link matched stock moves, possibly with some match flag that would visually show what was matched or not. If the line could not be matched, then it might be generated as you did...

It is indeed common that PO will generate incoming pickings that one will mostly want to match when electronic invoices are imported. With your current proposals you would need to delete manually these incoming pickings generated from the PO's and your PO will miss their delivery and invoicing status...

Bonus, it should be flexible so that it will keep working with the new PO/Bill matching system from v18.


def generate_picking_from_invoice(self):
"""Generate a picking from the invoice."""
self.ensure_one()

Check warning on line 33 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L33

Added line #L33 was not covered by tests
if self.move_type not in INVOICE_TYPE_MAP:
raise UserError(f"Unsupported move type: {self.move_type}")

Check warning on line 35 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L35

Added line #L35 was not covered by tests

picking_type_code, location_src_usage, location_dest_usage = INVOICE_TYPE_MAP[

Check warning on line 37 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L37

Added line #L37 was not covered by tests
self.move_type
]

# Picking type
picking_type_id = self.env["stock.picking.type"].search(

Check warning on line 42 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L42

Added line #L42 was not covered by tests
[("code", "=", picking_type_code)],
limit=1,
)

# Locations
location_src_id = self.env["stock.location"].search(

Check warning on line 48 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L48

Added line #L48 was not covered by tests
[("usage", "=", location_src_usage)],
limit=1,
)
location_dest_id = self.env["stock.location"].search(

Check warning on line 52 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L52

Added line #L52 was not covered by tests
[("usage", "=", location_dest_usage)],
limit=1,
)

# Picking
picking_values = {

Check warning on line 58 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L58

Added line #L58 was not covered by tests
"partner_id": self.partner_id.id,
"picking_type_id": picking_type_id.id,
"location_id": location_src_id.id,
"location_dest_id": location_dest_id.id,
"invoice_ids": self.ids,
"origin": self.name,
}

# Moves
move_values = [
self._prepare_stock_move_values(line, picking_values)
for line in self.invoice_line_ids
]
picking_values["move_lines"] = [(0, 0, move) for move in move_values]

self._create_picking(picking_values=picking_values)

Check warning on line 74 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L74

Added line #L74 was not covered by tests

def _prepare_stock_move_values(self, invoice_line, picking_values):
"""Prepare stock move values from invoice line."""
self.ensure_one()
return {

Check warning on line 79 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L78-L79

Added lines #L78 - L79 were not covered by tests
"name": invoice_line.product_id.name,
"product_id": invoice_line.product_id.id,
"location_id": picking_values.get("location_id"),
"location_dest_id": picking_values.get("location_dest_id"),
"state": "draft",
"company_id": self.company_id.id,
"product_uom_qty": invoice_line.quantity,
"product_uom": invoice_line.product_uom_id.id,
"invoice_line_ids": invoice_line.ids,
}

def _create_picking(self, picking_values):
"""Create a picking with the given values.

Override this method if you need to change any values of the
picking and the lines before the picking creation.

:param picking_values: dict with the picking and its lines
:return: picking
"""
return self.env["stock.picking"].create(picking_values)

Check warning on line 100 in account_move_stock_create/models/account_move.py

View check run for this annotation

Codecov / codecov/patch

account_move_stock_create/models/account_move.py#L100

Added line #L100 was not covered by tests
1 change: 1 addition & 0 deletions account_move_stock_create/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Diego Paradeda <[email protected]>
1 change: 1 addition & 0 deletions account_move_stock_create/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module enables the creation of stock transfers directly from invoices, streamlining the inventory update process and ensuring alignment between sales and stock levels.
1 change: 1 addition & 0 deletions account_move_stock_create/readme/INSTALL.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
This module depends on stock_picking_invoice_link. You can find it at [OCA/stock-logistics-workflow](https://github.com/OCA/stock-logistics-workflow)
4 changes: 4 additions & 0 deletions account_move_stock_create/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#. Go to *Invoices* then either *Customer > Invoices* or *Vendor > Bills*.
#. Select one invoice or bill..
#. Click on *Action > Create Stock Picking*.
#. Pickings are linked to invoices using *stock_picking_invoice_link* from [OCA/stock-logistics-workflow](https://github.com/OCA/stock-logistics-workflow)
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading