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] stock_picking_group_by_partner_by_carrier #641

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
104 changes: 104 additions & 0 deletions stock_picking_group_by_partner_by_carrier/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
===========================================
Stock Picking: group by partner and carrier
===========================================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

.. |badge1| image:: https://img.shields.io/badge/maturity-Alpha-red.png
:target: https://odoo-community.org/page/development-status
:alt: Alpha
.. |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%2Fstock--logistics--workflow-lightgray.png?logo=github
:target: https://github.com/OCA/stock-logistics-workflow/tree/13.0/stock_picking_group_by_partner_by_carrier
:alt: OCA/stock-logistics-workflow
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/stock-logistics-workflow-13-0/stock-logistics-workflow-13-0-stock_picking_group_by_partner_by_carrier
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/154/13.0
:alt: Try me on Runbot

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

This module can be used if your customers expect that several different orders
they passed will be shipped in a single delivery order.

With this module installed, when a sale order is confirmed, the stock moves for
the lines of the sale order can be placed in an existing delivery order that
shares the same delivery address and carrier (or lack thereof).

Sale orders with a Shipping Policy set to 'When all products are ready' always
get their own shipping.

.. IMPORTANT::
This is an alpha version, the data model and design can change at any time without warning.
Only for development or testing purpose, do not use in production.
`More details on development status <https://odoo-community.org/page/development-status>`_

**Table of contents**

.. contents::
:local:

Usage
=====

To use this module you need to enable grouping on the picking types for which you want grouping.

If you want to enable this for the shippings of a warehouse:

* be sure that in the settings of the Inventory app, you checked "Manage Push
and Pull inventory flows"
* enable "debug mode"
* go to the warehouse for which you want grouping and check the setting "Group
Shippings"


You can also enable this for individual picking types by checking the setting
"Group Pickings" on the picking type view.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/stock-logistics-workflow/issues>`_.
In case of trouble, please check there if your issue has already been reported.
If you spotted it first, help us smashing it by providing a detailed and welcomed
`feedback <https://github.com/OCA/stock-logistics-workflow/issues/new?body=module:%20stock_picking_group_by_partner_by_carrier%0Aversion:%2013.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
~~~~~~~

* Camptocamp

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

* Camptocamp:
* Alexandre Fayolle <[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/stock-logistics-workflow <https://github.com/OCA/stock-logistics-workflow/tree/13.0/stock_picking_group_by_partner_by_carrier>`_ 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 stock_picking_group_by_partner_by_carrier/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
20 changes: 20 additions & 0 deletions stock_picking_group_by_partner_by_carrier/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Copyright 2020 Camptocamp
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl.html).

{
"name": "Stock Picking: group by partner and carrier",
"Summary": "Group sales deliveries moves in 1 picking per partner and carrier",
"version": "13.0.1.0.0",
"development_status": "alpha",
"author": "Camptocamp, Odoo Community Association (OCA)",
"website": "https://github.com/OCA/stock-logistics-workflow",
"category": "Warehouse Management",
"depends": ["sale_stock", "delivery"],
"data": [
"views/stock_picking_type.xml",
"views/stock_warehouse.xml",
"report/report_delivery_slip.xml",
],
"installable": True,
"license": "AGPL-3",
}
6 changes: 6 additions & 0 deletions stock_picking_group_by_partner_by_carrier/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from . import procurement_group
from . import sale_order
from . import stock_move
from . import stock_picking
from . import stock_picking_type
from . import stock_warehouse
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from odoo import fields, models


class ProcurementGroup(models.Model):
_inherit = "procurement.group"

carrier_id = fields.Many2one("delivery.carrier", string="Delivery Method")
16 changes: 16 additions & 0 deletions stock_picking_group_by_partner_by_carrier/models/sale_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from odoo import fields, models


class SaleOrder(models.Model):
_inherit = "sale.order"

picking_ids = fields.Many2many("stock.picking", string="Transfers", copy=False)


class SaleOrderLine(models.Model):
_inherit = "sale.order.line"

def _prepare_procurement_group_vals(self):
vals = super()._prepare_procurement_group_vals()
vals["carrier_id"] = self.order_id.carrier_id.id
return vals
83 changes: 83 additions & 0 deletions stock_picking_group_by_partner_by_carrier/models/stock_move.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import re
from collections import namedtuple

from odoo import models


class StockMove(models.Model):
_inherit = "stock.move"

def _assign_picking(self):
return super(
StockMove, self.with_context(picking_no_overwrite_partner_origin=1)
)._assign_picking()

def _assign_picking_post_process(self, new=False):
res = super()._assign_picking_post_process(new=new)
if not new:
picking = self.mapped("picking_id")
picking.ensure_one()
sales = self.mapped("sale_line_id.order_id")
for sale in sales:
pattern = r"\b%s\b" % sale.name
if not re.search(pattern, picking.origin):
picking.origin += " " + sale.name
picking.message_post_with_view(
"mail.message_origin_link",
values={"self": picking, "origin": sale},
subtype_id=self.env.ref("mail.mt_note").id,
)
return res

def _search_picking_for_assignation(self):
# totally reimplement this one to add a hook to change the domain
self.ensure_one()
picking = self.env["stock.picking"].search(
self._domain_search_picking_for_assignation(), limit=1
)
return picking

def _domain_search_picking_for_assignation(self):
states = ("draft", "confirmed", "waiting", "partially_available", "assigned")
if (
not self.picking_type_id.group_pickings
or self.sale_line_id.order_id.picking_policy == "one"
):
# use the normal domain from the stock module
domain = [
("group_id", "=", self.group_id.id),
]
else:
domain = [
# same partner
("partner_id", "=", self.group_id.partner_id.id),
# same carrier
("carrier_id", "=", self.group_id.carrier_id.id),
# avoid mixing picking policies
("move_type", "=", self.group_id.move_type),
# don't search on the procurement.group
]
domain += [
("location_id", "=", self.location_id.id),
("location_dest_id", "=", self.location_dest_id.id),
("picking_type_id", "=", self.picking_type_id.id),
("printed", "=", False),
("immediate_transfer", "=", False),
("state", "in", states),
]
if self.env.context.get("picking_no_copy_if_can_group"):
# we are in the context of the creation of a backorder:
# don't consider the current move's picking
domain.append(("id", "!=", self.picking_id.id))
return domain

def _key_assign_picking(self):
return (
self.sale_line_id.order_id.partner_shipping_id,
PickingPolicy(id=self.sale_line_id.order_id.picking_policy),
) + super()._key_assign_picking()


# we define a named tuple because the code in module stock expects the values in
# the tuple returned by _key_assign_picking to be records with an id attribute
PickingPolicy = namedtuple("PickingPolicy", ["id"])
99 changes: 99 additions & 0 deletions stock_picking_group_by_partner_by_carrier/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
from collections import namedtuple
from itertools import groupby

from odoo import api, fields, models


class StockPicking(models.Model):
_inherit = "stock.picking"

sale_ids = fields.Many2many("sale.order", compute="_compute_sale_ids", store=True)
# don't copy the printed state of a picking otherwise the backorder of a
# printed picking becomes printed
printed = fields.Boolean(copy=False)

@api.depends("move_lines.sale_line_id.order_id")
def _compute_sale_ids(self):
for rec in self:
rec.sale_ids = rec.mapped("move_lines.sale_line_id.order_id")

def write(self, values):
if self.env.context.get("picking_no_overwrite_partner_origin"):
written_fields = set(values.keys())
if written_fields == {"partner_id", "origin"}:
values = {}
Copy link
Contributor

Choose a reason for hiding this comment

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

any chance to land here w/ other values you'd better preserve?

return super().write(values)

def _create_backorder(self):
return super(
StockPicking, self.with_context(picking_no_copy_if_can_group=1)
)._create_backorder()

def copy(self, defaults=None):
if self.env.context.get("picking_no_copy_if_can_group") and self.move_lines:
# we are in the process of the creation of a backorder. If we can
# find a suitable picking, then use it instead of copying the one
# we are creating a backorder from
picking = self.move_lines[0]._search_picking_for_assignation()
if picking:
return picking
return super(
StockPicking, self.with_context(picking_no_copy_if_can_group=0)
).copy(defaults)

def do_something(self):
return "bla bla"
Copy link
Contributor

Choose a reason for hiding this comment

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

I hope it does 😄


def get_delivery_report_lines(self):
self.ensure_one()
if self.state != "done":
moves = self.move_lines.filtered("product_uom_qty").sorted(
lambda m: m.sale_line_id.order_id
)
if len(moves.mapped("sale_line_id.order_id")) > 1:
sales_and_moves = []
for sale, sale_moves in groupby(
moves, lambda m: m.sale_line_id.order_id
):
sales_and_moves.append(
MockedMove(
product_id=False,
description_picking=sale.name,
product_uom_qty=0,
product_uom=False,
lot_name="",
)
)
for move in sale_moves:
sales_and_moves.append(move)
return sales_and_moves
else:
return moves
else:
moves = self.move_lines.sorted(lambda m: m.sale_line_id.order_id)
if len(moves.mapped("sale_line_id.order_id")) > 1:
sales_and_moves = []
for sale, sale_moves in groupby(
Copy link
Contributor

Choose a reason for hiding this comment

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

can we split this part as it's the same as at line 55?

moves, lambda m: m.sale_line_id.order_id
):
sales_and_moves.append(
MockedMove(
product_id=False,
description_picking=sale.name,
product_uom_qty=0,
product_uom=False,
lot_name="",
)
)
for move in sale_moves:
for move_line in move.move_line_ids:
sales_and_moves.append(move_line)
return sales_and_moves
else:
return self.move_line_ids


MockedMove = namedtuple(
"MockedMove",
["product_id", "description_picking", "product_uom_qty", "product_uom", "lot_name"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from odoo import fields, models


class StockPickingType(models.Model):
_inherit = "stock.picking.type"

group_pickings = fields.Boolean(
"Group pickings",
help="Group pickings for the same partner and carrier. "
"Pickings with shipping policy set to "
"'When all products are ready' are never grouped.",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
from odoo import fields, models


class StockWarehouse(models.Model):
_inherit = "stock.warehouse"

group_shippings = fields.Boolean(
"Group shippings",
related="out_type_id.group_pickings",
readonly=False,
help="Group shippings for the same partner and carrier. "
"Shippings with shipping policy set to "
"'When all products are ready' are never grouped.",
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Camptocamp:
* Alexandre Fayolle <[email protected]>
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
This module can be used if your customers expect that several different orders
they passed will be shipped in a single delivery order.

With this module installed, when a sale order is confirmed, the stock moves for
the lines of the sale order can be placed in an existing delivery order that
shares the same delivery address and carrier (or lack thereof).

Sale orders with a Shipping Policy set to 'When all products are ready' always
get their own shipping.
13 changes: 13 additions & 0 deletions stock_picking_group_by_partner_by_carrier/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
To use this module you need to enable grouping on the picking types for which you want grouping.

If you want to enable this for the shippings of a warehouse:

* be sure that in the settings of the Inventory app, you checked "Manage Push
and Pull inventory flows"
* enable "debug mode"
* go to the warehouse for which you want grouping and check the setting "Group
Shippings"


You can also enable this for individual picking types by checking the setting
"Group Pickings" on the picking type view.
Loading