1# -*- coding: utf-8 -*-
2# Part of Odoo. See LICENSE file for full copyright and licensing details.
3
4from odoo import api, fields, models, _
5from odoo.exceptions import ValidationError
6
7# see http://doc.qt.io/archives/qt-4.8/qprinter.html#PaperSize-enum
8PAPER_SIZES = [
9    {
10        'description': 'A0  5   841 x 1189 mm',
11        'key': 'A0',
12        'height': 1189.0,
13        'width': 841.0,
14    }, {
15        'key': 'A1',
16        'description': 'A1  6   594 x 841 mm',
17        'height': 841.0,
18        'width': 594.0,
19    }, {
20        'key': 'A2',
21        'description': 'A2  7   420 x 594 mm',
22        'height': 594.0,
23        'width': 420.0,
24    }, {
25        'key': 'A3',
26        'description': 'A3  8   297 x 420 mm',
27        'height': 420.0,
28        'width': 297.0,
29    }, {
30        'key': 'A4',
31        'description': 'A4  0   210 x 297 mm, 8.26 x 11.69 inches',
32        'height': 297.0,
33        'width': 210.0,
34    }, {
35        'key': 'A5',
36        'description': 'A5  9   148 x 210 mm',
37        'height': 210.0,
38        'width': 148.0,
39    }, {
40        'key': 'A6',
41        'description': 'A6  10  105 x 148 mm',
42        'height': 148.0,
43        'width': 105.0,
44    }, {
45        'key': 'A7',
46        'description': 'A7  11  74 x 105 mm',
47        'height': 105.0,
48        'width': 74.0,
49    }, {
50        'key': 'A8',
51        'description': 'A8  12  52 x 74 mm',
52        'height': 74.0,
53        'width': 52.0,
54    }, {
55        'key': 'A9',
56        'description': 'A9  13  37 x 52 mm',
57        'height': 52.0,
58        'width': 37.0,
59    }, {
60        'key': 'B0',
61        'description': 'B0  14  1000 x 1414 mm',
62        'height': 1414.0,
63        'width': 1000.0,
64    }, {
65        'key': 'B1',
66        'description': 'B1  15  707 x 1000 mm',
67        'height': 1000.0,
68        'width': 707.0,
69    }, {
70        'key': 'B2',
71        'description': 'B2  17  500 x 707 mm',
72        'height': 707.0,
73        'width': 500.0,
74    }, {
75        'key': 'B3',
76        'description': 'B3  18  353 x 500 mm',
77        'height': 500.0,
78        'width': 353.0,
79    }, {
80        'key': 'B4',
81        'description': 'B4  19  250 x 353 mm',
82        'height': 353.0,
83        'width': 250.0,
84    }, {
85        'key': 'B5',
86        'description': 'B5  1   176 x 250 mm, 6.93 x 9.84 inches',
87        'height': 250.0,
88        'width': 176.0,
89    }, {
90        'key': 'B6',
91        'description': 'B6  20  125 x 176 mm',
92        'height': 176.0,
93        'width': 125.0,
94    }, {
95        'key': 'B7',
96        'description': 'B7  21  88 x 125 mm',
97        'height': 125.0,
98        'width': 88.0,
99    }, {
100        'key': 'B8',
101        'description': 'B8  22  62 x 88 mm',
102        'height': 88.0,
103        'width': 62.0,
104    }, {
105        'key': 'B9',
106        'description': 'B9  23  33 x 62 mm',
107        'height': 62.0,
108        'width': 33.0,
109    }, {
110        'key': 'B10',
111        'description': 'B10    16  31 x 44 mm',
112        'height': 44.0,
113        'width': 31.0,
114    }, {
115        'key': 'C5E',
116        'description': 'C5E 24  163 x 229 mm',
117        'height': 229.0,
118        'width': 163.0,
119    }, {
120        'key': 'Comm10E',
121        'description': 'Comm10E 25  105 x 241 mm, U.S. Common 10 Envelope',
122        'height': 241.0,
123        'width': 105.0,
124    }, {
125        'key': 'DLE',
126        'description': 'DLE 26 110 x 220 mm',
127        'height': 220.0,
128        'width': 110.0,
129    }, {
130        'key': 'Executive',
131        'description': 'Executive 4   7.5 x 10 inches, 190.5 x 254 mm',
132        'height': 254.0,
133        'width': 190.5,
134    }, {
135        'key': 'Folio',
136        'description': 'Folio 27  210 x 330 mm',
137        'height': 330.0,
138        'width': 210.0,
139    }, {
140        'key': 'Ledger',
141        'description': 'Ledger  28  431.8 x 279.4 mm',
142        'height': 279.4,
143        'width': 431.8,
144    }, {
145        'key': 'Legal',
146        'description': 'Legal    3   8.5 x 14 inches, 215.9 x 355.6 mm',
147        'height': 355.6,
148        'width': 215.9,
149    }, {
150        'key': 'Letter',
151        'description': 'Letter 2 8.5 x 11 inches, 215.9 x 279.4 mm',
152        'height': 279.4,
153        'width': 215.9,
154    }, {
155        'key': 'Tabloid',
156        'description': 'Tabloid 29 279.4 x 431.8 mm',
157        'height': 431.8,
158        'width': 279.4,
159    }, {
160        'key': 'custom',
161        'description': 'Custom',
162    },
163]
164
165
166class report_paperformat(models.Model):
167    _name = "report.paperformat"
168    _description = "Paper Format Config"
169
170    name = fields.Char('Name', required=True)
171    default = fields.Boolean('Default paper format ?')
172    format = fields.Selection([(ps['key'], ps['description']) for ps in PAPER_SIZES], 'Paper size', default='A4', help="Select Proper Paper size")
173    margin_top = fields.Float('Top Margin (mm)', default=40)
174    margin_bottom = fields.Float('Bottom Margin (mm)', default=20)
175    margin_left = fields.Float('Left Margin (mm)', default=7)
176    margin_right = fields.Float('Right Margin (mm)', default=7)
177    page_height = fields.Integer('Page height (mm)', default=False)
178    page_width = fields.Integer('Page width (mm)', default=False)
179    orientation = fields.Selection([
180        ('Landscape', 'Landscape'),
181        ('Portrait', 'Portrait')
182        ], 'Orientation', default='Landscape')
183    header_line = fields.Boolean('Display a header line', default=False)
184    header_spacing = fields.Integer('Header spacing', default=35)
185    dpi = fields.Integer('Output DPI', required=True, default=90)
186    report_ids = fields.One2many('ir.actions.report', 'paperformat_id', 'Associated reports', help="Explicitly associated reports")
187    print_page_width = fields.Float('Print page width (mm)', compute='_compute_print_page_size')
188    print_page_height = fields.Float('Print page height (mm)', compute='_compute_print_page_size')
189
190    @api.constrains('format')
191    def _check_format_or_page(self):
192        if self.filtered(lambda x: x.format != 'custom' and (x.page_width or x.page_height)):
193            raise ValidationError(_('You can select either a format or a specific page width/height, but not both.'))
194
195    def _compute_print_page_size(self):
196        for record in self:
197            width = height = 0.0
198            if record.format:
199                if record.format == 'custom':
200                    width = record.page_width
201                    height = record.page_height
202                else:
203                    paper_size = next(ps for ps in PAPER_SIZES if ps['key'] == record.format)
204                    width = paper_size['width']
205                    height = paper_size['height']
206
207            if record.orientation == 'Landscape':
208                # swap sizes
209                width, height = height, width
210
211            record.print_page_width = width
212            record.print_page_height = height
213