Skip to content

Commit

Permalink
REFactor base_deliv_carr_label: always use package
Browse files Browse the repository at this point in the history
  • Loading branch information
bealdav committed Aug 20, 2019
1 parent a614331 commit ffaab97
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 72 deletions.
36 changes: 28 additions & 8 deletions base_delivery_carrier_label/README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,17 @@ Base module for carrier labels
.. |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/12.0/base_delivery_carrier_label
:alt: OCA/delivery_carrier
.. |badge3| image:: https://img.shields.io/badge/github-OCA%2Fdelivery--carrier-lightgray.png?logo=github
:target: https://github.com/OCA/delivery-carrier/tree/12.0/base_delivery_carrier_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-12-0/delivery_carrier-12-0-base_delivery_carrier_label
:target: https://translation.odoo-community.org/projects/delivery-carrier-12-0/delivery-carrier-12-0-base_delivery_carrier_label
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runbot-Try%20me-875A7B.png
:target: https://runbot.odoo-community.org/runbot/99/12.0
:alt: Try me on Runbot

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

This module adds a button on delivery orders to generate a label as an
attachement. This module doesn't do anything by itself, it serves as a
Expand All @@ -31,13 +34,29 @@ base module for other carrier-specific modules.
.. contents::
:local:

Usage
=====

** How does it works ? **


In picking UI a button "Shipping label" trigger label generation
calling `action_generate_carrier_label()` in models/stock.picking.py


** How to implement my own carrier ? **


Override `generate_shipping_labels()` which is called by previous method
in the same file.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/delivery_carrier/issues>`_.
Bugs are tracked on `GitHub Issues <https://github.com/OCA/delivery-carrier/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/delivery_carrier/issues/new?body=module:%20base_delivery_carrier_label%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**>`_.
`feedback <https://github.com/OCA/delivery-carrier/issues/new?body=module:%20base_delivery_carrier_label%0Aversion:%2012.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.

Expand All @@ -60,6 +79,7 @@ Contributors
* Angel Moya <[email protected]>
* Ismael Calvo <[email protected]>
* Dave Lasley <[email protected]>
* Timothée Ringeard <[email protected]>

Maintainers
~~~~~~~~~~~
Expand All @@ -74,6 +94,6 @@ 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/delivery_carrier <https://github.com/OCA/delivery_carrier/tree/12.0/base_delivery_carrier_label>`_ project on GitHub.
This module is part of the `OCA/delivery-carrier <https://github.com/OCA/delivery-carrier/tree/12.0/base_delivery_carrier_label>`_ project on GitHub.

You are welcome to contribute. To learn how please visit https://odoo-community.org/page/Contribute.
5 changes: 4 additions & 1 deletion base_delivery_carrier_label/models/shipping_label.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ class ShippingLabel(models.Model):
@api.model
def _selection_file_type(self):
""" To inherit to add file type """
return [('pdf', 'PDF')]
return [
('pdf', 'PDF'),
('zpl2', 'ZPL2'),
]

file_type = fields.Selection(
selection='_selection_file_type',
Expand Down
87 changes: 41 additions & 46 deletions base_delivery_carrier_label/models/stock_picking.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl).

import base64
import logging
from odoo import _, api, fields, models
from odoo.exceptions import UserError

_logger = logging.getLogger(__name__)


class StockPicking(models.Model):
_inherit = 'stock.picking'
Expand All @@ -27,53 +30,39 @@ class StockPicking(models.Model):
option_ids = fields.Many2many(comodel_name='delivery.carrier.option',
string='Options')

@api.multi
def generate_default_label(self, package_ids=None):
def generate_default_label(self):
""" Abstract method
:param package_ids: optional list of ``stock.quant.package`` ids
only packs in this list will have their label
printed (all are generated when None)
:return: (file_binary, file_type)
"""
raise NotImplementedError(_('No label is configured for the '
'selected delivery method.'))

@api.multi
def generate_shipping_labels(self, package_ids=None):
def generate_shipping_labels(self):
"""Generate a shipping label by default
This method can be inherited to create specific shipping labels
a list of label must be return as we can have multiple
stock.quant.package for a single picking representing packs
:param package_ids: optional list of ``stock.quant.package`` ids
only packs in this list will have their label
printed (all are generated when None)
:return: list of dict containing
name: name to give to the attachement
file: file as string
file_type: string of file type like 'PDF'
(optional)
tracking_id: tracking_id if picking lines have tracking_id and
if label generator creates shipping label per
pack
tracking_number: tracking id defined by your carrier
"""
default_label = self.generate_default_label(package_ids=package_ids)
if not package_ids:
return [default_label]
self.ensure_one()
default_label = self.generate_default_label()
labels = []
for package_id in package_ids:
for package in self._get_packages_from_picking():
pack_label = default_label.copy()
pack_label['tracking_id'] = package_id
pack_label['tracking_number'] = package.id
labels.append(pack_label)
return labels

@api.multi
def get_shipping_label_values(self, label):
self.ensure_one()
return {
Expand All @@ -85,23 +74,36 @@ def get_shipping_label_values(self, label):
'file_type': label['file_type'],
}

@api.multi
def generate_labels(self, package_ids=None):
""" Generate the labels.
def generate_labels(self):
""" Legacy method. Remove me after 12.0
"""
_logger.warning(
"Your delivery module depending on your carrier must call "
"action_generate_carrier_label() method "
"instead of generate_labels()")
return self.action_generate_carrier_label()

def _set_a_default_package(self):
""" Pickings using this module must have a package
If not this method put it one silently
"""
for picking in self:
move_lines = picking.move_line_ids.filtered(
lambda s: not (s.package_id or s.result_package_id))
if move_lines:
package = self.env['stock.quant.package'].create({})
move_lines.write({'result_package_id': package.id})

A list of package ids can be given, in that case it will generate
the labels only of these packages.
def action_generate_carrier_label(self):
""" Method for the 'Generate Label' button.
"""
label_obj = self.env['shipping.label']
It will generate the labels for all the packages of the picking.
Packages are mandatory in this case
"""
for pick in self:
if package_ids:
shipping_labels = pick.generate_shipping_labels(
package_ids=package_ids
)
else:
shipping_labels = pick.generate_shipping_labels()
pick._set_a_default_package()
shipping_labels = pick.generate_shipping_labels()
for label in shipping_labels:
data = pick.get_shipping_label_values(label)
if label.get('package_id'):
Expand All @@ -111,18 +113,13 @@ def generate_labels(self, package_ids=None):
# as it would try to define default value of attachement
if 'default_type' in context_attachment:
del context_attachment['default_type']
label_obj.with_context(context_attachment).create(data)
self.env['shipping.label'].with_context(
context_attachment).create(data)
if len(shipping_labels) == 1:
pick.write(
{'carrier_tracking_ref': label.get('tracking_number')})
return True

@api.multi
def action_generate_carrier_label(self):
""" Method for the 'Generate Label' button.
It will generate the labels for all the packages of the picking.
"""
return self.generate_labels()

@api.onchange('carrier_id')
def onchange_carrier_id(self):
""" Inherit this method in your module """
Expand Down Expand Up @@ -219,7 +216,6 @@ def create(self, vals):
vals = self._values_with_carrier_options(vals)
return super(StockPicking, self).create(vals)

@api.multi
def _get_label_sender_address(self):
""" On each carrier label module you need to define
which is the sender of the parcel.
Expand All @@ -243,7 +239,6 @@ def _get_label_sender_address(self):
address_id = partner.address_get(adr_pref=['delivery'])['delivery']
return self.env['res.partner'].browse(address_id)

@api.multi
def _check_existing_shipping_label(self):
""" Check that labels don't already exist for this picking """
self.ensure_one()
Expand Down
12 changes: 12 additions & 0 deletions base_delivery_carrier_label/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
** How does it works ? **


In picking UI a button "Shipping label" trigger label generation
calling `action_generate_carrier_label()` in models/stock.picking.py


** How to implement my own carrier ? **


Override `generate_shipping_labels()` which is called by previous method
in the same file.
41 changes: 26 additions & 15 deletions base_delivery_carrier_label/static/description/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="Docutils 0.14: http://docutils.sourceforge.net/" />
<meta name="generator" content="Docutils 0.15.1: http://docutils.sourceforge.net/" />
<title>Base module for carrier labels</title>
<style type="text/css">

Expand Down Expand Up @@ -367,41 +367,51 @@ <h1 class="title">Base module for carrier labels</h1>
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/delivery_carrier/tree/12.0/base_delivery_carrier_label"><img alt="OCA/delivery_carrier" src="https://img.shields.io/badge/github-OCA%2Fdelivery_carrier-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/delivery_carrier-12-0/delivery_carrier-12-0-base_delivery_carrier_label"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a></p>
<p><a class="reference external" href="https://odoo-community.org/page/development-status"><img alt="Beta" src="https://img.shields.io/badge/maturity-Beta-yellow.png" /></a> <a class="reference external" href="http://www.gnu.org/licenses/agpl-3.0-standalone.html"><img alt="License: AGPL-3" src="https://img.shields.io/badge/licence-AGPL--3-blue.png" /></a> <a class="reference external" href="https://github.com/OCA/delivery-carrier/tree/12.0/base_delivery_carrier_label"><img alt="OCA/delivery-carrier" src="https://img.shields.io/badge/github-OCA%2Fdelivery--carrier-lightgray.png?logo=github" /></a> <a class="reference external" href="https://translation.odoo-community.org/projects/delivery-carrier-12-0/delivery-carrier-12-0-base_delivery_carrier_label"><img alt="Translate me on Weblate" src="https://img.shields.io/badge/weblate-Translate%20me-F47D42.png" /></a> <a class="reference external" href="https://runbot.odoo-community.org/runbot/99/12.0"><img alt="Try me on Runbot" src="https://img.shields.io/badge/runbot-Try%20me-875A7B.png" /></a></p>
<p>This module adds a button on delivery orders to generate a label as an
attachement. This module doesn’t do anything by itself, it serves as a
base module for other carrier-specific modules.</p>
<p><strong>Table of contents</strong></p>
<div class="contents local topic" id="contents">
<ul class="simple">
<li><a class="reference internal" href="#bug-tracker" id="id1">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id2">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id3">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id4">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id5">Maintainers</a></li>
<li><a class="reference internal" href="#usage" id="id1">Usage</a></li>
<li><a class="reference internal" href="#bug-tracker" id="id2">Bug Tracker</a></li>
<li><a class="reference internal" href="#credits" id="id3">Credits</a><ul>
<li><a class="reference internal" href="#authors" id="id4">Authors</a></li>
<li><a class="reference internal" href="#contributors" id="id5">Contributors</a></li>
<li><a class="reference internal" href="#maintainers" id="id6">Maintainers</a></li>
</ul>
</li>
</ul>
</div>
<div class="section" id="usage">
<h1><a class="toc-backref" href="#id1">Usage</a></h1>
<p>** How does it works ? **</p>
<p>In picking UI a button “Shipping label” trigger label generation
calling <cite>action_generate_carrier_label()</cite> in models/stock.picking.py</p>
<p>** How to implement my own carrier ? **</p>
<p>Override <cite>generate_shipping_labels()</cite> which is called by previous method
in the same file.</p>
</div>
<div class="section" id="bug-tracker">
<h1><a class="toc-backref" href="#id1">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/delivery_carrier/issues">GitHub Issues</a>.
<h1><a class="toc-backref" href="#id2">Bug Tracker</a></h1>
<p>Bugs are tracked on <a class="reference external" href="https://github.com/OCA/delivery-carrier/issues">GitHub Issues</a>.
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
<a class="reference external" href="https://github.com/OCA/delivery_carrier/issues/new?body=module:%20base_delivery_carrier_label%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<a class="reference external" href="https://github.com/OCA/delivery-carrier/issues/new?body=module:%20base_delivery_carrier_label%0Aversion:%2012.0%0A%0A**Steps%20to%20reproduce**%0A-%20...%0A%0A**Current%20behavior**%0A%0A**Expected%20behavior**">feedback</a>.</p>
<p>Do not contact contributors directly about support or help with technical issues.</p>
</div>
<div class="section" id="credits">
<h1><a class="toc-backref" href="#id2">Credits</a></h1>
<h1><a class="toc-backref" href="#id3">Credits</a></h1>
<div class="section" id="authors">
<h2><a class="toc-backref" href="#id3">Authors</a></h2>
<h2><a class="toc-backref" href="#id4">Authors</a></h2>
<ul class="simple">
<li>Camptocamp</li>
<li>Akretion</li>
</ul>
</div>
<div class="section" id="contributors">
<h2><a class="toc-backref" href="#id4">Contributors</a></h2>
<h2><a class="toc-backref" href="#id5">Contributors</a></h2>
<ul class="simple">
<li>David BEAL &lt;<a class="reference external" href="mailto:david.beal&#64;akretion.com">david.beal&#64;akretion.com</a>&gt;</li>
<li>Sébastien BEAU &lt;<a class="reference external" href="mailto:sebastien.beau&#64;akretion.com">sebastien.beau&#64;akretion.com</a>&gt;</li>
Expand All @@ -410,16 +420,17 @@ <h2><a class="toc-backref" href="#id4">Contributors</a></h2>
<li>Angel Moya &lt;<a class="reference external" href="mailto:angel.moya&#64;pesol.es">angel.moya&#64;pesol.es</a>&gt;</li>
<li>Ismael Calvo &lt;<a class="reference external" href="mailto:ismael.calvo&#64;factorlibre.com">ismael.calvo&#64;factorlibre.com</a>&gt;</li>
<li>Dave Lasley &lt;<a class="reference external" href="mailto:dave&#64;laslabs.com">dave&#64;laslabs.com</a>&gt;</li>
<li>Timothée Ringeard &lt;<a class="reference external" href="mailto:timothee.ringeard&#64;camptocamp.com">timothee.ringeard&#64;camptocamp.com</a>&gt;</li>
</ul>
</div>
<div class="section" id="maintainers">
<h2><a class="toc-backref" href="#id5">Maintainers</a></h2>
<h2><a class="toc-backref" href="#id6">Maintainers</a></h2>
<p>This module is maintained by the OCA.</p>
<a class="reference external image-reference" href="https://odoo-community.org"><img alt="Odoo Community Association" src="https://odoo-community.org/logo.png" /></a>
<p>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.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/delivery_carrier/tree/12.0/base_delivery_carrier_label">OCA/delivery_carrier</a> project on GitHub.</p>
<p>This module is part of the <a class="reference external" href="https://github.com/OCA/delivery-carrier/tree/12.0/base_delivery_carrier_label">OCA/delivery-carrier</a> project on GitHub.</p>
<p>You are welcome to contribute. To learn how please visit <a class="reference external" href="https://odoo-community.org/page/Contribute">https://odoo-community.org/page/Contribute</a>.</p>
</div>
</div>
Expand Down
14 changes: 12 additions & 2 deletions base_delivery_carrier_label/views/stock.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,10 @@
<field name="inherit_id" ref="delivery.view_picking_withcarrier_out_form"/>
<field name="arch" type="xml">
<field name="state" position="before">
<button name="action_generate_carrier_label" states="done"
string="Create Shipping Label" type="object"/>
<button name="action_generate_carrier_label"
help="Create Shipping Label 🚚"
attrs="{'invisible': ['|', ('carrier_code', '=', False), ('state', '!=', 'done')]}"
string="Shipping Label 🚚" type="object"/>
</field>
<xpath expr="//page//field[@name='carrier_id']" position="after">
<field name="carrier_code"/>
Expand Down Expand Up @@ -40,6 +42,11 @@
<field name="parcel_tracking_uri"/>
<field name="weight"/>
</field>
<div name="button_box" position="inside">
<button type="object" name="open_website_url" class="oe_stat_button"
icon='fa-truck' string="Tracking"
attrs="{'invisible': [('parcel_tracking','=',False)]}"/>
</div>
</field>
</record>

Expand All @@ -48,6 +55,9 @@
<field name="inherit_id" ref="stock.view_quant_package_tree"/>
<field name="arch" type="xml">
<field name="display_name" position="after">
<button type="object" name="open_website_url"
icon='fa-truck' string=""
attrs="{'invisible': [('parcel_tracking','=',False)]}"/>
<field name="parcel_tracking"/>
<field name="weight"/>
</field>
Expand Down

0 comments on commit ffaab97

Please sign in to comment.