~opencrea/+junk/aprobio

« back to all changes in this revision

Viewing changes to printer_zpl2/wizard/print_record_label.py

  • Committer: joannes
  • Date: 2017-05-17 09:40:42 UTC
  • Revision ID: joannes@debian-20170517094042-47q3j6on72w2h1il
community module

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*-
 
2
# Copyright (C) 2016 SYLEAM (<http://www.syleam.fr>)
 
3
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
 
4
 
 
5
from odoo import models, api, fields
 
6
 
 
7
 
 
8
class PrintRecordLabel(models.TransientModel):
 
9
    _name = 'wizard.print.record.label'
 
10
    _description = 'Print Record Label'
 
11
 
 
12
    printer_id = fields.Many2one(
 
13
        comodel_name='printing.printer', string='Printer', required=True,
 
14
        help='Printer used to print the labels.')
 
15
    label_id = fields.Many2one(
 
16
        comodel_name='printing.label.zpl2', string='Label', required=True,
 
17
        domain=lambda self: [
 
18
            ('model_id.model', '=', self.env.context.get('active_model'))],
 
19
        help='Label to print.')
 
20
 
 
21
    @api.model
 
22
    def default_get(self, fields_list):
 
23
        values = super(PrintRecordLabel, self).default_get(fields_list)
 
24
 
 
25
        # Automatically select the printer and label, if only one is available
 
26
        printers = self.env['printing.printer'].search([])
 
27
        if len(printers) == 1:
 
28
            values['printer_id'] = printers.id
 
29
 
 
30
        labels = self.env['printing.label.zpl2'].search([
 
31
            ('model_id.model', '=', self.env.context.get('active_model')),
 
32
        ])
 
33
        if len(labels) == 1:
 
34
            values['label_id'] = labels.id
 
35
 
 
36
        return values
 
37
 
 
38
    @api.multi
 
39
    def print_label(self):
 
40
        """ Prints a label per selected record """
 
41
        record_model = self.env.context['active_model']
 
42
        for record_id in self.env.context['active_ids']:
 
43
            record = self.env[record_model].browse(record_id)
 
44
            self.label_id.print_label(self.printer_id, record)