1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
|
# -*- coding: utf-8 -*-
from odoo import api, fields, models
from odoo.modules import get_resource_path
try:
import sass as libsass
except ImportError:
libsass = None
class MultipleInvoiceLayout(models.TransientModel):
"""
Customise the invoice copy document layout and display a live preview
"""
_name = 'multiple.invoice.layout'
_description = 'Multiple Invoice Document Layout'
def _get_default_journal(self):
return self.env['account.journal'].search(
[('id', '=', self.env.context.get('active_id'))]).id
company_id = fields.Many2one(
'res.company', default=lambda self: self.env.company, required=True)
layout = fields.Char(related="company_id.external_report_layout_id.key")
journal_id = fields.Many2one('account.journal', string='Journal',
required=True, default=_get_default_journal)
multiple_invoice_type = fields.Selection(
related='journal_id.multiple_invoice_type', readonly=False,
required=True)
text_position = fields.Selection(related='journal_id.text_position',
readonly=False, required=True,
default='header')
body_text_position = fields.Selection(
related='journal_id.body_text_position',
readonly=False)
text_align = fields.Selection(
related='journal_id.text_align',
readonly=False)
preview = fields.Html(compute='_compute_preview',
sanitize=False,
sanitize_tags=False,
sanitize_attributes=False,
sanitize_style=False,
sanitize_form=False,
strip_style=False,
strip_classes=False)
@api.depends('multiple_invoice_type', 'text_position', 'body_text_position',
'text_align')
def _compute_preview(self):
""" compute a qweb based preview to display on the wizard """
styles = self._get_asset_style()
for wizard in self:
if wizard.company_id:
preview_css = self._get_css_for_preview(styles, wizard.id)
layout = self._get_layout_for_preview()
ir_ui_view = wizard.env['ir.ui.view']
wizard.preview = ir_ui_view._render_template(
'base_accounting_kit.multiple_invoice_wizard_preview',
{'company': wizard.company_id, 'preview_css': preview_css,
'layout': layout,
'mi_type': self.multiple_invoice_type,
'txt_position': self.text_position,
'body_txt_position': self.body_text_position,
'txt_align': self.text_align,
'mi': self.env.ref(
'base_accounting_kit.multiple_invoice_sample_name')
})
else:
wizard.preview = False
def _get_asset_style(self):
template_style = self.env.ref('web.styles_company_report',
raise_if_not_found=False)
if not template_style:
return b''
company_styles = template_style._render({
'company_ids': self.company_id,
})
return company_styles
@api.model
def _get_css_for_preview(self, scss, new_id):
"""
Compile the scss into css.
"""
css_code = self._compile_scss(scss)
return css_code
@api.model
def _compile_scss(self, scss_source):
"""
This code will compile valid scss into css.
Parameters are the same from odoo/addons/base/models/assetsbundle.py
Simply copied and adapted slightly
"""
# No scss ? still valid, returns empty css
if not scss_source.strip():
return ""
precision = 8
output_style = 'expanded'
bootstrap_path = get_resource_path('web', 'static', 'lib', 'bootstrap',
'scss')
try:
return libsass.compile(
string=scss_source,
include_paths=[
bootstrap_path,
],
output_style=output_style,
precision=precision,
)
except libsass.CompileError as e:
raise libsass.CompileError(e.args[0])
def _get_layout_for_preview(self):
if self.layout == 'web.external_layout_boxed':
new_layout = 'base_accounting_kit.boxed'
elif self.layout == 'web.external_layout_clean':
new_layout = 'base_accounting_kit.clean'
elif self.layout == 'web.external_layout_background':
new_layout = 'base_accounting_kit.background'
else:
new_layout = 'base_accounting_kit.standard'
return new_layout
def document_layout_save(self):
# meant to be overridden
return self.env.context.get('report_action') or {
'type': 'ir.actions.act_window_close'}
|