Skip to content

Commit

Permalink
[12.0][MIG] delivery_carrier_label_batch
Browse files Browse the repository at this point in the history
  • Loading branch information
TDu authored and Hadrien Huvelle committed Apr 2, 2024
1 parent 57e0d1d commit 0a2ed95
Show file tree
Hide file tree
Showing 13 changed files with 57 additions and 66 deletions.
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Camptocamp SA
# Copyright 2013-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
{
'name': 'Carrier labels - Stock Batch Picking (link)',
'version': '9.0.1.0.0',
'version': '12.0.1.0.0',
'author': "Camptocamp,Odoo Community Association (OCA)",
'maintainer': 'Camptocamp',
'category': 'Carrier',
'complexity': 'normal',
'depends': ['base_delivery_carrier_label', 'stock_batch_picking'],
'website': 'http://www.camptocamp.com/',
'website': 'http://github.com/OCA/delivery-carrier',
'data': [
'views/stock_batch_picking.xml',
'wizard/generate_labels_view.xml',
'wizard/apply_carrier_view.xml',
],
'tests': [],
'installable': True,
'auto_install': True,
'license': 'AGPL-3',
Expand Down
5 changes: 2 additions & 3 deletions delivery_carrier_label_batch/models/stock_batch_picking.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Camptocamp SA
# Copyright 2013-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from openerp import _, api, fields, models
from odoo import _, api, fields, models


class StockBatchPicking(models.Model):
Expand Down
7 changes: 3 additions & 4 deletions delivery_carrier_label_batch/pdf_utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Camptocamp SA
# Copyright 2013-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from StringIO import StringIO
from io import BytesIO, StringIO
from PyPDF2 import PdfFileReader, PdfFileWriter


Expand All @@ -28,6 +27,6 @@ def assemble_pdf(pdf_list):

for page in range(reader.getNumPages()):
output.addPage(reader.getPage(page))
s = StringIO()
s = BytesIO()
output.write(s)
return s.getvalue()
1 change: 1 addition & 0 deletions delivery_carrier_label_batch/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* Yannick Vaucher <[email protected]>
7 changes: 7 additions & 0 deletions delivery_carrier_label_batch/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
This module adds a wizard on picking batch to generate the labels
of the packs. The labels are merged in one PDF file.

If you want multiple labels for one picking, all the moves should have been
put in a pack before the labels can be printed.

If you don't define your pack it will be considered a picking is a single pack.
3 changes: 3 additions & 0 deletions delivery_carrier_label_batch/readme/USAGE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
To use this module, you need to:

1. Go to...
1 change: 0 additions & 1 deletion delivery_carrier_label_batch/tests/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# Author: Yannick Vaucher
Expand Down
25 changes: 13 additions & 12 deletions delivery_carrier_label_batch/tests/test_generate_labels.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Camptocamp SA
# Copyright 2013-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
import openerp.tests.common as common
from openerp.modules import get_module_resource
import base64

import odoo.tests.common as common
from odoo.modules import get_module_resource


class TestGenerateLabels(common.TransactionCase):
Expand Down Expand Up @@ -41,8 +42,8 @@ def setUp(self):
Move.create(
{'name': '/',
'picking_id': picking_out_1.id,
'product_id': self.ref('product.product_product_33'),
'product_uom': self.ref('product.product_uom_unit'),
'product_id': self.ref('product.product_delivery_01'),
'product_uom': self.ref('uom.product_uom_unit'),
'product_uom_qty': 2,
'location_id': self.ref('stock.stock_location_14'),
'location_dest_id': self.ref('stock.stock_location_7'),
Expand All @@ -51,8 +52,8 @@ def setUp(self):
Move.create(
{'name': '/',
'picking_id': picking_out_2.id,
'product_id': self.ref('product.product_product_33'),
'product_uom': self.ref('product.product_uom_unit'),
'product_id': self.ref('product.product_delivery_01'),
'product_uom': self.ref('uom.product_uom_unit'),
'product_uom_qty': 1,
'location_id': self.ref('stock.stock_location_14'),
'location_dest_id': self.ref('stock.stock_location_7'),
Expand All @@ -61,22 +62,22 @@ def setUp(self):
label = ''
dummy_pdf_path = get_module_resource('delivery_carrier_label_batch',
'tests', 'dummy.pdf')
with file(dummy_pdf_path) as dummy_pdf:
with open(dummy_pdf_path, 'rb') as dummy_pdf:
label = dummy_pdf.read()

ShippingLabel.create(
{'name': 'picking_out_1',
'res_id': picking_out_1.id,
'res_model': 'stock.picking',
'datas': label.encode('base64'),
'datas': base64.b64encode(label),
'file_type': 'pdf',
})

ShippingLabel.create(
{'name': 'picking_out_2',
'res_id': picking_out_2.id,
'res_model': 'stock.picking',
'datas': label.encode('base64'),
'datas': base64.b64encode(label),
'file_type': 'pdf',
})

Expand All @@ -97,7 +98,7 @@ def test_00_action_generate_labels(self):
('res_id', '=', self.batch.id)]
)

self.assertEquals(len(attachment), 1)
self.assertEqual(len(attachment), 1)
self.assertTrue(attachment.datas)
self.assertTrue(attachment.name, 'demo_prep001.pdf')
self.assertTrue(attachment.mimetype, 'application/pdf')
2 changes: 1 addition & 1 deletion delivery_carrier_label_batch/views/stock_batch_picking.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
</group>
<separator string="Options"/>
<field name="option_ids" nolabel="1"/>
<label string="Warning, setting options will erase the existing ones in delivery orders"/>
<label for="" string="Warning, setting options will erase the existing ones in delivery orders"/>
<button name="action_set_options" string="Set Options"
class="oe_highlight" type="object"/>
</page>
Expand Down
5 changes: 2 additions & 3 deletions delivery_carrier_label_batch/wizard/apply_carrier.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
# -*- coding: utf-8 -*-
# Copyright 2016 Camptocamp SA
# Copyright 2016-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
from openerp import _, api, exceptions, fields, models
from odoo import _, api, exceptions, fields, models


class PickingBatchApplyCarrier(models.TransientModel):
Expand Down
12 changes: 2 additions & 10 deletions delivery_carrier_label_batch/wizard/apply_carrier_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<field name="model">picking.batch.apply.carrier</field>
<field name="arch" type="xml">
<form string="Apply a carrier and its options">
<label string="Warning, setting options will erase the existing ones in delivery orders"/>
<label for="" string="Warning, setting options will erase the existing ones in delivery orders"/>
<group>
<field name="carrier_id"/>
</group>
Expand All @@ -24,17 +24,9 @@
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="view_id" ref="view_picking_batch_apply_carrier"/>
<field name="binding_model_id" ref="stock_batch_picking.model_stock_batch_picking"/>
<field name="target">new</field>
</record>

<record id="action_picking_batch_apply_carrier_values" model="ir.values">
<field name="model_id" ref="stock_batch_picking.model_stock_batch_picking" />
<field name="name">Apply a carrier and its options</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_picking_batch_apply_carrier'))"/>
<field name="key">action</field>
<field name="model">stock.batch.picking</field>
</record>

</odoo>

36 changes: 18 additions & 18 deletions delivery_carrier_label_batch/wizard/generate_labels.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# -*- coding: utf-8 -*-
# Copyright 2013-2016 Camptocamp SA
# Copyright 2013-2019 Camptocamp SA
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl)
import Queue
import base64
import queue
import logging
import openerp
import odoo
import threading
from contextlib import closing, contextmanager
from contextlib import contextmanager
from itertools import groupby
from openerp import _, api, exceptions, fields, models
from odoo import _, api, exceptions, fields, models

from ..pdf_utils import assemble_pdf

Expand Down Expand Up @@ -39,7 +39,7 @@ def _get_batch_ids(self):

@api.model
def _get_packs(self, batch):
operations = batch.pack_operation_ids
operations = batch.move_line_ids # pack_operation_ids
operations = sorted(
operations,
key=lambda r: r.result_package_id.name or r.package_id.name
Expand Down Expand Up @@ -68,10 +68,10 @@ def _find_pack_label(self, pack):
@contextmanager
@api.model
def _do_in_new_env(self):
with openerp.api.Environment.manage():
with openerp.registry(self.env.cr.dbname).cursor() as new_cr:
yield openerp.api.Environment(new_cr, self.env.uid,
self.env.context)
with odoo.api.Environment.manage():
with odoo.registry(self.env.cr.dbname).cursor() as new_cr:
yield odoo.api.Environment(new_cr, self.env.uid,
self.env.context)

def _do_generate_labels(self, group):
""" Generate a label in a thread safe context
Expand Down Expand Up @@ -107,7 +107,7 @@ def _worker(self, data_queue, error_queue):
while not data_queue.empty():
try:
group = data_queue.get()
except Queue.Empty:
except queue.Empty:
return
try:
self._do_generate_labels(group)
Expand All @@ -131,8 +131,8 @@ def _get_num_workers(self):
def _get_all_pdf(self, batch):
self.ensure_one()

data_queue = Queue.Queue()
error_queue = Queue.Queue()
data_queue = queue.Queue()
error_queue = queue.Queue()

# If we have more than one pack in a picking, we must ensure
# they are not executed concurrently or we will have concurrent
Expand All @@ -147,7 +147,7 @@ def _get_all_pdf(self, batch):
(pack, picking, label)
)

for group in groups.itervalues():
for group in groups.values():
data_queue.put(group)

# create few workers to parallelize label generation
Expand Down Expand Up @@ -176,13 +176,13 @@ def _get_all_pdf(self, batch):
error_count[e.name] = 1
else:
error_count[e.name] += 1
messages.append(unicode(e) or '')
messages.append(str(e) or '')
else:
# raise other exceptions like PoolError if
# too many cursor where created by workers
raise e
titles = []
for key, v in error_count.iteritems():
for key, v in error_count.items():
titles.append('%sx %s' % (v, key))

message = _('Some labels couldn\'t be generated. Please correct '
Expand Down Expand Up @@ -235,7 +235,7 @@ def action_generate_labels(self):
'name': filename,
'res_id': batch.id,
'res_model': 'stock.batch.picking',
'datas': assemble_pdf(labels).encode('base64'),
'datas': base64.b64encode(assemble_pdf(labels)),
'datas_fname': filename,
}
self.env['ir.attachment'].create(data)
Expand Down
11 changes: 2 additions & 9 deletions delivery_carrier_label_batch/wizard/generate_labels_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<field name="arch" type="xml">
<form string="Generate Carriers Labels">
<group>
<label string="This wizard creates an attachement on each selected batch containing picking labels"/>
<label for="" string="This wizard creates an attachement on each selected batch containing picking labels"/>
</group>
<group>
<field name="batch_ids"/>
Expand All @@ -29,15 +29,8 @@
<field name="view_mode">form</field>
<field name="view_id" ref="view_delivery_carrier_label_generate"/>
<field name="groups_id" eval="[(6, 0, [ref('stock.group_stock_manager')])]"/>
<field name="binding_model_id" ref="stock_batch_picking.model_stock_batch_picking"/>
<field name="target">new</field>
</record>

<record model="ir.values" id="delivery_carrier_file_generate_ir_values">
<field name="name">Generate Carrier Labels</field>
<field name="key2">client_action_multi</field>
<field name="value" eval="'ir.actions.act_window,' + str(ref('action_delivery_carrier_label_generate'))" />
<field name="key">action</field>
<field name="model">stock.batch.picking</field>
</record>

</odoo>

0 comments on commit 0a2ed95

Please sign in to comment.