# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import _, api, fields, models from odoo.exceptions import UserError class ProductLabelLayout(models.TransientModel): _name = 'product.label.layout' _description = 'Choose the sheet layout to print the labels' print_format = fields.Selection([ ('dymo', 'Dymo'), ('2x7xprice', '2 x 7 with price'), ('4x7xprice', '4 x 7 with price'), ('4x12', '4 x 12'), ('4x12xprice', '4 x 12 with price')], string="Format", default='2x7xprice', required=True) custom_quantity = fields.Integer('Quantity', default=1, required=True) product_ids = fields.Many2many('product.product') product_tmpl_ids = fields.Many2many('product.template') extra_html = fields.Html('Extra Content', default='') rows = fields.Integer(compute='_compute_dimensions') columns = fields.Integer(compute='_compute_dimensions') pricelist_id = fields.Many2one('product.pricelist', string="Pricelist") @api.depends('print_format') def _compute_dimensions(self): for wizard in self: if 'x' in wizard.print_format: columns, rows = wizard.print_format.split('x')[:2] wizard.columns = columns.isdigit() and int(columns) or 1 wizard.rows = rows.isdigit() and int(rows) or 1 else: wizard.columns, wizard.rows = 1, 1 def _prepare_report_data(self): if self.custom_quantity <= 0: raise UserError(_('You need to set a positive quantity.')) # Get layout grid if self.print_format == 'dymo': xml_id = 'product.report_product_template_label_dymo' elif 'x' in self.print_format: xml_id = 'product.report_product_template_label_%sx%s' % (self.columns, self.rows) if 'xprice' not in self.print_format: xml_id += '_noprice' else: xml_id = '' active_model = '' if self.product_tmpl_ids: products = self.product_tmpl_ids.ids active_model = 'product.template' elif self.product_ids: products = self.product_ids.ids active_model = 'product.product' else: raise UserError(_("No product to print, if the product is archived please unarchive it before printing its label.")) # Build data to pass to the report data = { 'active_model': active_model, 'quantity_by_product': {p: self.custom_quantity for p in products}, 'layout_wizard': self.id, 'price_included': 'xprice' in self.print_format, } return xml_id, data def process(self): self.ensure_one() xml_id, data = self._prepare_report_data() if not xml_id: raise UserError(_('Unable to find report template for %s format', self.print_format)) report_action = self.env.ref(xml_id).report_action(None, data=data, config=False) report_action.update({'close_on_report_download': True}) return report_action