From a6c77e35298641b9a808792876de4da658def0da Mon Sep 17 00:00:00 2001 From: Florian da Costa Date: Thu, 23 Jan 2025 11:25:21 +0100 Subject: [PATCH] [ADD] delivery_carrier_shipping_label : extracted from base_delivery_carrier_label (issue 896) --- delivery_carrier_shipping_label/README.rst | 111 +++++ delivery_carrier_shipping_label/__init__.py | 1 + .../__manifest__.py | 17 + .../models/__init__.py | 3 + .../models/delivery_carrier.py | 19 + .../models/shipping_label.py | 22 + .../models/stock_picking.py | 50 ++ .../pyproject.toml | 3 + .../readme/CONTRIBUTORS.md | 10 + .../readme/DESCRIPTION.md | 3 + .../readme/USAGE.md | 12 + .../security/ir.model.access.csv | 3 + .../static/description/index.html | 452 ++++++++++++++++++ .../tests/__init__.py | 1 + .../tests/test_shipping_label.py | 30 ++ .../views/delivery_carrier.xml | 15 + .../views/delivery_carrier_option.xml | 38 ++ .../delivery_carrier_template_option.xml | 35 ++ .../views/stock_picking.xml | 20 + 19 files changed, 845 insertions(+) create mode 100644 delivery_carrier_shipping_label/README.rst create mode 100644 delivery_carrier_shipping_label/__init__.py create mode 100644 delivery_carrier_shipping_label/__manifest__.py create mode 100644 delivery_carrier_shipping_label/models/__init__.py create mode 100644 delivery_carrier_shipping_label/models/delivery_carrier.py create mode 100644 delivery_carrier_shipping_label/models/shipping_label.py create mode 100644 delivery_carrier_shipping_label/models/stock_picking.py create mode 100644 delivery_carrier_shipping_label/pyproject.toml create mode 100644 delivery_carrier_shipping_label/readme/CONTRIBUTORS.md create mode 100644 delivery_carrier_shipping_label/readme/DESCRIPTION.md create mode 100644 delivery_carrier_shipping_label/readme/USAGE.md create mode 100644 delivery_carrier_shipping_label/security/ir.model.access.csv create mode 100644 delivery_carrier_shipping_label/static/description/index.html create mode 100644 delivery_carrier_shipping_label/tests/__init__.py create mode 100644 delivery_carrier_shipping_label/tests/test_shipping_label.py create mode 100644 delivery_carrier_shipping_label/views/delivery_carrier.xml create mode 100644 delivery_carrier_shipping_label/views/delivery_carrier_option.xml create mode 100644 delivery_carrier_shipping_label/views/delivery_carrier_template_option.xml create mode 100644 delivery_carrier_shipping_label/views/stock_picking.xml diff --git a/delivery_carrier_shipping_label/README.rst b/delivery_carrier_shipping_label/README.rst new file mode 100644 index 0000000000..c0ec49c049 --- /dev/null +++ b/delivery_carrier_shipping_label/README.rst @@ -0,0 +1,111 @@ +=============================== +Delivery Carrier Shipping Label +=============================== + +.. + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! This file is generated by oca-gen-addon-readme !! + !! changes will be overwritten. !! + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + !! source digest: sha256:4fe4b408850191ac08d98293294acc226617419145b2c065873039d185b43f67 + !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! + +.. |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%2Fdelivery--carrier-lightgray.png?logo=github + :target: https://github.com/OCA/delivery-carrier/tree/18.0/delivery_carrier_shipping_label + :alt: OCA/delivery-carrier +.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png + :target: https://translation.odoo-community.org/projects/delivery-carrier-18-0/delivery-carrier-18-0-delivery_carrier_shipping_label + :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/delivery-carrier&target_branch=18.0 + :alt: Try me on Runboat + +|badge1| |badge2| |badge3| |badge4| |badge5| + +This module is kind of a technical module as it adds a new model +inheriting the ir.attachment which purpose is to isolate the shipping +label and eventually add some specific fields for this kind of document. +The final main goal beeing to identifiy these shipping labels from the +other documents attached to a picking in order to send it to the right +printer. + +**Table of contents** + +.. contents:: + :local: + +Usage +===== + +This module is meant to be used in carrier specific implementation. In +the carrier specific label generation method +(delivery.carrier.mycarrier_send_shipping), include the label +information in the result with a dedicated key labels. These labels will +then be attached to the picking and isolated in the dedicated attachment +table. { "labels": [{ "name": "mylabel", "file": base64_label, +"file_type": "zpl", # optional "package_id": package_id, # optional } ] +} + +Bug Tracker +=========== + +Bugs are tracked on `GitHub 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 `_. + +Do not contact contributors directly about support or help with technical issues. + +Credits +======= + +Authors +------- + +* Camptocamp +* Akretion + +Contributors +------------ + +- David BEAL +- Sébastien BEAU +- Yannick Vaucher +- Alexis de Lattre +- Angel Moya +- Ismael Calvo +- Dave Lasley +- Timothée Ringeard +- Pimolnat Suntian +- Raphaël Reverdy + +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. + +.. |maintainer-florian-dacosta| image:: https://github.com/florian-dacosta.png?size=40px + :target: https://github.com/florian-dacosta + :alt: florian-dacosta + +Current `maintainer `__: + +|maintainer-florian-dacosta| + +This module is part of the `OCA/delivery-carrier `_ project on GitHub. + +You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute. diff --git a/delivery_carrier_shipping_label/__init__.py b/delivery_carrier_shipping_label/__init__.py new file mode 100644 index 0000000000..0650744f6b --- /dev/null +++ b/delivery_carrier_shipping_label/__init__.py @@ -0,0 +1 @@ +from . import models diff --git a/delivery_carrier_shipping_label/__manifest__.py b/delivery_carrier_shipping_label/__manifest__.py new file mode 100644 index 0000000000..c8733011b8 --- /dev/null +++ b/delivery_carrier_shipping_label/__manifest__.py @@ -0,0 +1,17 @@ +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl.html). +{ + "name": "Delivery Carrier Shipping Label", + "version": "18.0.1.0.0", + "author": "Camptocamp,Akretion,Odoo Community Association (OCA)", + "category": "Delivery", + "maintainers": ["florian-dacosta"], + "depends": [ + "stock_delivery", + ], + "website": "https://github.com/OCA/delivery-carrier", + "data": [ + "security/ir.model.access.csv", + ], + "installable": True, + "license": "AGPL-3", +} diff --git a/delivery_carrier_shipping_label/models/__init__.py b/delivery_carrier_shipping_label/models/__init__.py new file mode 100644 index 0000000000..dd318c010b --- /dev/null +++ b/delivery_carrier_shipping_label/models/__init__.py @@ -0,0 +1,3 @@ +from . import delivery_carrier +from . import stock_picking +from . import shipping_label diff --git a/delivery_carrier_shipping_label/models/delivery_carrier.py b/delivery_carrier_shipping_label/models/delivery_carrier.py new file mode 100644 index 0000000000..177be446ed --- /dev/null +++ b/delivery_carrier_shipping_label/models/delivery_carrier.py @@ -0,0 +1,19 @@ +# Copyright 2012 Akretion . +# Copyright 2013-2016 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import models + + +class DeliveryCarrier(models.Model): + _inherit = "delivery.carrier" + + def send_shipping(self, pickings): + """Handle labels and if we have them. Expected format is {'labels': [{}, ...]} + The dicts are input for stock.picking#attach_label""" + result = super().send_shipping(pickings) + if result: + for result_dict, picking in zip(result, pickings, strict=False): + for label in result_dict.get("labels", []): + picking.attach_shipping_label(label) + return result diff --git a/delivery_carrier_shipping_label/models/shipping_label.py b/delivery_carrier_shipping_label/models/shipping_label.py new file mode 100644 index 0000000000..e95f7ac15f --- /dev/null +++ b/delivery_carrier_shipping_label/models/shipping_label.py @@ -0,0 +1,22 @@ +# Copyright 2013-2016 Camptocamp SA +# Copyright 2014 Akretion +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import fields, models + + +class ShippingLabel(models.Model): + """Child class of ir attachment to identify which are labels""" + + _name = "shipping.label" + _inherits = {"ir.attachment": "attachment_id"} + _description = "Shipping Label" + + file_type = fields.Char(string="File type", default="pdf") + package_id = fields.Many2one(comodel_name="stock.quant.package", string="Pack") + attachment_id = fields.Many2one( + comodel_name="ir.attachment", + string="Attachement", + required=True, + ondelete="cascade", + ) diff --git a/delivery_carrier_shipping_label/models/stock_picking.py b/delivery_carrier_shipping_label/models/stock_picking.py new file mode 100644 index 0000000000..b0bbb94b1d --- /dev/null +++ b/delivery_carrier_shipping_label/models/stock_picking.py @@ -0,0 +1,50 @@ +# Copyright 2012-2015 Akretion . +# Copyright 2013-2016 Camptocamp SA +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). + +from odoo import _, models +from odoo.exceptions import UserError + + +class StockPicking(models.Model): + _inherit = "stock.picking" + + def get_shipping_label_values(self, label): + self.ensure_one() + return { + "name": label["name"], + "res_id": self.id, + "res_model": "stock.picking", + "datas": label["file"], + "file_type": label["file_type"], + "package_id": label.get("package_id", False), + } + + def attach_shipping_label(self, label): + """Attach a label returned by send_to_shipper to a picking""" + self.ensure_one() + data = self.get_shipping_label_values(label) + context_attachment = self.env.context.copy() + # remove default_type setted for stock_picking + # as it would try to define default value of attachement + if "default_type" in context_attachment: + del context_attachment["default_type"] + return ( + self.env["shipping.label"].with_context(**context_attachment).create(data) + ) + + def _check_existing_shipping_label(self): + """Check that labels don't already exist for this picking""" + self.ensure_one() + labels = self.env["shipping.label"].search( + [("res_id", "=", self.id), ("res_model", "=", "stock.picking")] + ) + if labels: + raise UserError( + _( + "Some labels already exist for the picking %s.\n" + "Please delete the existing labels in the " + "attachments of this picking and try again" + ) + % self.name + ) diff --git a/delivery_carrier_shipping_label/pyproject.toml b/delivery_carrier_shipping_label/pyproject.toml new file mode 100644 index 0000000000..4231d0cccb --- /dev/null +++ b/delivery_carrier_shipping_label/pyproject.toml @@ -0,0 +1,3 @@ +[build-system] +requires = ["whool"] +build-backend = "whool.buildapi" diff --git a/delivery_carrier_shipping_label/readme/CONTRIBUTORS.md b/delivery_carrier_shipping_label/readme/CONTRIBUTORS.md new file mode 100644 index 0000000000..90852f9446 --- /dev/null +++ b/delivery_carrier_shipping_label/readme/CONTRIBUTORS.md @@ -0,0 +1,10 @@ +- David BEAL \<\> +- Sébastien BEAU \<\> +- Yannick Vaucher \<\> +- Alexis de Lattre \<\> +- Angel Moya \<\> +- Ismael Calvo \<\> +- Dave Lasley \<\> +- Timothée Ringeard \<\> +- Pimolnat Suntian \<\> +- Raphaël Reverdy \<\> diff --git a/delivery_carrier_shipping_label/readme/DESCRIPTION.md b/delivery_carrier_shipping_label/readme/DESCRIPTION.md new file mode 100644 index 0000000000..6be4438a1e --- /dev/null +++ b/delivery_carrier_shipping_label/readme/DESCRIPTION.md @@ -0,0 +1,3 @@ +This module is kind of a technical module as it adds a new model inheriting the ir.attachment +which purpose is to isolate the shipping label and eventually add some specific fields for this kind of document. +The final main goal beeing to identifiy these shipping labels from the other documents attached to a picking in order to send it to the right printer. diff --git a/delivery_carrier_shipping_label/readme/USAGE.md b/delivery_carrier_shipping_label/readme/USAGE.md new file mode 100644 index 0000000000..5697eeca2e --- /dev/null +++ b/delivery_carrier_shipping_label/readme/USAGE.md @@ -0,0 +1,12 @@ +This module is meant to be used in carrier specific implementation. +In the carrier specific label generation method (delivery.carrier.mycarrier_send_shipping), include the label information in the result with +a dedicated key labels. These labels will then be attached to the picking and isolated in the dedicated attachment table. +{ + "labels": [{ + "name": "mylabel", + "file": base64_label, + "file_type": "zpl", # optional + "package_id": package_id, # optional + } + ] +} diff --git a/delivery_carrier_shipping_label/security/ir.model.access.csv b/delivery_carrier_shipping_label/security/ir.model.access.csv new file mode 100644 index 0000000000..6a59a79556 --- /dev/null +++ b/delivery_carrier_shipping_label/security/ir.model.access.csv @@ -0,0 +1,3 @@ +id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink +access_shipping_label_user,shipping.label user,model_shipping_label,stock.group_stock_user,1,1,1,0 +access_shipping_label_manager,shipping.label manager,model_shipping_label,stock.group_stock_manager,1,1,1,1 diff --git a/delivery_carrier_shipping_label/static/description/index.html b/delivery_carrier_shipping_label/static/description/index.html new file mode 100644 index 0000000000..4775ae39c5 --- /dev/null +++ b/delivery_carrier_shipping_label/static/description/index.html @@ -0,0 +1,452 @@ + + + + + +Delivery Carrier Shipping Label + + + +
+

Delivery Carrier Shipping Label

+ + +

Beta License: AGPL-3 OCA/delivery-carrier Translate me on Weblate Try me on Runboat

+

This module is kind of a technical module as it adds a new model +inheriting the ir.attachment which purpose is to isolate the shipping +label and eventually add some specific fields for this kind of document. +The final main goal beeing to identifiy these shipping labels from the +other documents attached to a picking in order to send it to the right +printer.

+

Table of contents

+ +
+

Usage

+

This module is meant to be used in carrier specific implementation. In +the carrier specific label generation method +(delivery.carrier.mycarrier_send_shipping), include the label +information in the result with a dedicated key labels. These labels will +then be attached to the picking and isolated in the dedicated attachment +table. { “labels”: [{ “name”: “mylabel”, “file”: base64_label, +“file_type”: “zpl”, # optional “package_id”: package_id, # optional } ] +}

+
+
+

Bug Tracker

+

Bugs are tracked on GitHub 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.

+

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

+
+
+

Credits

+
+

Authors

+
    +
  • Camptocamp
  • +
  • Akretion
  • +
+
+
+

Contributors

+ +
+
+

Maintainers

+

This module is maintained by the OCA.

+ +Odoo Community Association + +

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.

+

Current maintainer:

+

florian-dacosta

+

This module is part of the OCA/delivery-carrier project on GitHub.

+

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.

+
+
+
+ + diff --git a/delivery_carrier_shipping_label/tests/__init__.py b/delivery_carrier_shipping_label/tests/__init__.py new file mode 100644 index 0000000000..7c75100de8 --- /dev/null +++ b/delivery_carrier_shipping_label/tests/__init__.py @@ -0,0 +1 @@ +from . import test_shipping_label diff --git a/delivery_carrier_shipping_label/tests/test_shipping_label.py b/delivery_carrier_shipping_label/tests/test_shipping_label.py new file mode 100644 index 0000000000..93f735a684 --- /dev/null +++ b/delivery_carrier_shipping_label/tests/test_shipping_label.py @@ -0,0 +1,30 @@ +# Copyright 2020 Hunki Enterprises BV +# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). +import base64 + +from odoo.tests.common import TransactionCase + + +class TestShippingLabel(TransactionCase): + def test_attach_shipping_label(self): + """Test if attaching labels works correctly""" + picking = self.env["stock.picking"].new( + dict( + carrier_id=self.env.ref("delivery.delivery_carrier").id, + company_id=self.env.user.company_id.id, + ) + ) + label = picking.with_context( + # test if the function protect against an unwanted key in the context + default_type="binary", + ).attach_shipping_label( + dict( + name="hello_world.pdf", + file=base64.b64encode(bytes("hello world", "utf8")), + file_type="pdf", + package_id=self.env["stock.quant.package"] + .create(dict(name="package")) + .id, + ) + ) + self.assertEqual(label.name, "hello_world.pdf") diff --git a/delivery_carrier_shipping_label/views/delivery_carrier.xml b/delivery_carrier_shipping_label/views/delivery_carrier.xml new file mode 100644 index 0000000000..c2140e796c --- /dev/null +++ b/delivery_carrier_shipping_label/views/delivery_carrier.xml @@ -0,0 +1,15 @@ + + + + delivery_base.delivery.carrier.view_form + delivery.carrier + + + + + + + + + + diff --git a/delivery_carrier_shipping_label/views/delivery_carrier_option.xml b/delivery_carrier_shipping_label/views/delivery_carrier_option.xml new file mode 100644 index 0000000000..147eafd7ca --- /dev/null +++ b/delivery_carrier_shipping_label/views/delivery_carrier_option.xml @@ -0,0 +1,38 @@ + + + + delivery_base.delivery_carrier_option.view_form + delivery.carrier.option + +
+ + + + + + + + + + + + + +
+
+
+ + + delivery_base.delivery_carrier_option.view_list + delivery.carrier.option + list + + + + + + + + + +
diff --git a/delivery_carrier_shipping_label/views/delivery_carrier_template_option.xml b/delivery_carrier_shipping_label/views/delivery_carrier_template_option.xml new file mode 100644 index 0000000000..5efc67050b --- /dev/null +++ b/delivery_carrier_shipping_label/views/delivery_carrier_template_option.xml @@ -0,0 +1,35 @@ + + + + delivery_base.delivery_carrier_option.view_form + delivery.carrier.template.option + +
+ + + + + + + + + + +
+
+
+ + + delivery_base.delivery_carrier_template_option.view_list + delivery.carrier.template.option + + + + + + + + +
diff --git a/delivery_carrier_shipping_label/views/stock_picking.xml b/delivery_carrier_shipping_label/views/stock_picking.xml new file mode 100644 index 0000000000..8c4e950879 --- /dev/null +++ b/delivery_carrier_shipping_label/views/stock_picking.xml @@ -0,0 +1,20 @@ + + + + stock.picking + + + + + + + +