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 16, 2022
1 parent 4072f81 commit 9f047d5
Show file tree
Hide file tree
Showing 18 changed files with 942 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) <[email protected]>
* Christopher Hansen <[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/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) <[email protected]>
# 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
131 changes: 131 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,131 @@
# Copyright 2022 Jacques-Etienne Baudoux (BCIM) <[email protected]>
# 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_domain = 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")

@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"))

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

def _do_automated_printing(self, picking):
if not self.nbr_of_copies:
return

if not self._check_condition(picking):
return False

behaviour = self.get_behaviour()
printer = behaviour["printer"]
if not printer:
raise Warning(_("No printer configured to print this file."))

record = self._get_record(picking)
for content in self.get_content(record):
for _n in range(self.nbr_of_copies):
printer.print_document(report=None, content=content, **behaviour)

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}
if self.file_type == "report":
return self.report_id.behaviour()
if self.label:
return {"printer": self.env.user.default_label_printer_id}
return self.env["ir.actions.report"]._get_user_default_print_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)]
if self.file_type == "attachment":
return self.generate_data_from_attachments(record)
return []

def _prepare_attachment_domain(self, record):
domain = safe_eval(self.attachment_domain)
record_domain = [
("res_id", "=", record.id),
("res_model", "=", record._name),
]
return expression.AND([domain, record_domain])

def generate_data_from_attachments(self, record):
domain = self._prepare_attachment_domain(record)
attachments = self.env["ir.attachment"].search(domain)
if not attachments:
raise ValidationError(_("No attachment was found."))
return [base64.b64decode(a.datas) for a in 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
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 StockPicking(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 StockPickingType(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) <[email protected]>
* Christopher Hansen <[email protected]>
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 9f047d5

Please sign in to comment.