Skip to content

Commit

Permalink
[ADD] stock_picking_auto_print
Browse files Browse the repository at this point in the history
  • Loading branch information
jbaudoux committed Dec 12, 2022
1 parent 4072f81 commit 0202c43
Show file tree
Hide file tree
Showing 18 changed files with 962 additions and 0 deletions.
6 changes: 6 additions & 0 deletions setup/stock_picking_auto_print/setup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import setuptools

setuptools.setup(
setup_requires=['setuptools-odoo'],
odoo_addon=True,
)
80 changes: 80 additions & 0 deletions stock_picking_auto_print/README.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
========================
stock_picking_auto_print
========================

.. !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!! This file is generated by oca-gen-addon-readme !!
!! changes will be overwritten. !!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
.. |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%2Freport--print--send-lightgray.png?logo=github
:target: https://github.com/OCA/report-print-send/tree/14.0/stock_picking_auto_print
:alt: OCA/report-print-send
.. |badge4| image:: https://img.shields.io/badge/weblate-Translate%20me-F47D42.png
:target: https://translation.odoo-community.org/projects/report-print-send-14-0/report-print-send-14-0-stock_picking_auto_print
:alt: Translate me on Weblate
.. |badge5| image:: https://img.shields.io/badge/runboat-Try%20me-875A7B.png
:target: https://runboat.odoo-community.org/webui/builds.html?repo=OCA/report-print-send&target_branch=14.0
:alt: Try me on Runboat

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

When a picking is done, automatically trigger the printing of some documents.
This can be used to print a delivery slip (report) or labels received from the carrier (attachment).

**Table of contents**

.. contents::
:local:

Configuration
=============

Go to the Operation Type and configure which report or attachment to print.

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

Bugs are tracked on `GitHub Issues <https://github.com/OCA/report-print-send/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/report-print-send/issues/new?body=module:%20stock_picking_auto_print%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
~~~~~~~

* BCIM

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

* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* Christopher Hansen <christopher.hansen@raumschmiede.de>

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/report-print-send <https://github.com/OCA/report-print-send/tree/14.0/stock_picking_auto_print>`_ 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_auto_print/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
from . import models
23 changes: 23 additions & 0 deletions stock_picking_auto_print/__manifest__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Copyright 2022 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

{
"application": False,
"author": "BCIM, Odoo Community Association (OCA)",
"category": "Warehouse Management",
"data": [
"security/ir.model.access.csv",
"views/stock_auto_printing_views.xml",
"views/stock_picking_type_views.xml",
],
"demo_xml": [],
"depends": [
"stock",
"base_report_to_printer",
],
"installable": True,
"license": "AGPL-3",
"name": "stock_picking_auto_print",
"version": "14.0.1.0.0",
"website": "https://github.com/OCA/report-print-send",
}
3 changes: 3 additions & 0 deletions stock_picking_auto_print/models/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from . import stock_auto_printing
from . import stock_picking
from . import stock_picking_type
135 changes: 135 additions & 0 deletions stock_picking_auto_print/models/stock_auto_printing.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Copyright 2022 Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

import base64

from odoo import _, api, fields, models
from odoo.exceptions import UserError, ValidationError, Warning
from odoo.osv import expression
from odoo.tools.safe_eval import safe_eval


class StockAutoPrinting(models.Model):
_name = "stock.auto.printing"
_description = "Stock Auto Printing"

name = fields.Char(string="Name", required=True)
file_type = fields.Selection(
[
("report", "Report"),
("attachment", "Attachment"),
],
string="Type",
default="report",
required=True,
help=(
"Choose to print the result of an odoo report or a pre-existing "
"attachment (useful for labels received from carriers that are "
"recorded on the picking as an attachment)"
),
)
field_object = fields.Char(
"Object", help="Select on which document the report must be executed"
)

report_id = fields.Many2one("ir.actions.report")

attachment = fields.Char("Attachment", default="[]")
condition = fields.Char(
"Condition",
default="[]",
help="Give a domain that must be valid for printing this",
)

printer_id = fields.Many2one("printing.printer", string="Printer")
printer_tray_id = fields.Many2one("printing.tray")
nbr_of_copies = fields.Integer("Number of Copies", default=1)

picking_type_id = fields.Many2one("stock.picking.type", required=True)
label = fields.Boolean(string="Is Label")

def do_automated_printing(self, pickings):
for rec in self:
for pick in pickings:
rec._do_automated_printing(pick)

def _do_automated_printing(self, picking):
self.ensure_one()
picking.ensure_one()
if not self.check_condition(picking):
return False

printer, behaviour = self.get_printer_and_behaviour()

record = self._get_record(picking)
for content in self.get_content(record):
for _n in range(self.nbr_of_copies):
self._send_file_to_printer(content, behaviour, printer)

def _send_file_to_printer(self, content, behaviour, printer):
return printer.print_document(
report=None, content=content, doc_formart="qweb-pdf", **behaviour
)

def _get_record(self, record):
if self.field_object:
try:
return safe_eval(f"obj.{self.field_object}", {"obj": record})
except Exception as e:
raise ValidationError(
_("The Object field could not be applied because: %s") % str(e)
) from e
return record

def check_condition(self, picking):
domain = safe_eval(self.condition, {"env": self.env})
return picking.filtered_domain(domain)

def get_content(self, record):
if self.file_type == "report":
return [self.generate_data_from_report(record)]
attachments = self.get_attachment(record)
return [base64.b64decode(a.datas) for a in attachments]

def get_behaviour(self):
if self.printer_id:
tray = self.printer_tray_id and self.printer_tray_id.system_name
return {"printer": self.printer_id, "tray": tray}
elif self.file_type == "report":
return self.report_id.behaviour()
elif self.label:
return self.env.user.default_label_printer_id
else:
return self.env["ir.actions.report"]._get_user_default_print_behaviour()

def get_printer_and_behaviour(self):
behaviour = self.get_behaviour()
printer = behaviour.pop("printer", None)
if not printer:
raise Warning(_("No printer configured to print this file."))
return printer, behaviour

def get_attachment(self, record):
user_domain = safe_eval(self.attachment)
attachment_domain = [
("res_id", "=", record.id),
("res_model", "=", record._name),
]
domain = expression.AND([attachment_domain, user_domain])
attachments = self.env["ir.attachment"].search(domain)
if not attachments:
raise ValidationError(_("No attachment was found."))
return attachments

def generate_data_from_report(self, record):
self.ensure_one()
data, _ = self.report_id.with_context(
must_skip_send_to_printer=True
)._render_qweb_pdf(record.id)
return data

@api.constrains("report_id", "file_type")
def check_report(self):
for rec in self:
if rec.file_type == "report" and not rec.report_id:
raise UserError(_("Report was not set"))
16 changes: 16 additions & 0 deletions stock_picking_auto_print/models/stock_picking.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import models


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

def _action_done(self):
result = super()._action_done()
self.send_files_to_printer()
return result

def send_files_to_printer(self):
for rec in self:
rec.picking_type_id.do_automated_printing(rec)
17 changes: 17 additions & 0 deletions stock_picking_auto_print/models/stock_picking_type.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).

from odoo import fields, models


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

auto_printing_ids = fields.One2many(
"stock.auto.printing", "picking_type_id", "Auto Printing Configuration"
)

def do_automated_printing(self, pickings):
for rec in self:
if not rec.auto_printing_ids:
continue
rec.auto_printing_ids.do_automated_printing(pickings)
1 change: 1 addition & 0 deletions stock_picking_auto_print/readme/CONFIGURE.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Go to the Operation Type and configure which report or attachment to print.
2 changes: 2 additions & 0 deletions stock_picking_auto_print/readme/CONTRIBUTORS.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
* Jacques-Etienne Baudoux (BCIM) <je@bcim.be>
* Christopher Hansen <christopher.hansen@raumschmiede.de>
2 changes: 2 additions & 0 deletions stock_picking_auto_print/readme/DESCRIPTION.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
When a picking is done, automatically trigger the printing of some documents.
This can be used to print a delivery slip (report) or labels received from the carrier (attachment).
3 changes: 3 additions & 0 deletions stock_picking_auto_print/security/ir.model.access.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
access_manager,stock_picking_auto_print - manager,model_stock_auto_printing,stock.group_stock_manager,1,1,1,1
access_user,stock_picking_auto_print - user,model_stock_auto_printing,stock.group_stock_user,1,0,0,0
Loading

0 comments on commit 0202c43

Please sign in to comment.