|
| 1 | +# Part of Odoo. See LICENSE file for full copyright and licensing details. |
| 2 | + |
| 3 | +from odoo import api, fields, models |
| 4 | + |
| 5 | + |
| 6 | +class ReceiptsLayout(models.TransientModel): |
| 7 | + _name = "receipts.layout" |
| 8 | + _description = "Custom Reciept Template" |
| 9 | + |
| 10 | + pos_config_id = fields.Many2one( |
| 11 | + "pos.config", |
| 12 | + string="Point of Sale", |
| 13 | + default=lambda self: self.env["pos.config"].browse( |
| 14 | + self.env.context.get("active_pos_config_id") |
| 15 | + ), |
| 16 | + required=True, |
| 17 | + ) |
| 18 | + receipt_layout = fields.Selection(related="pos_config_id.receipt_layout", required=True) |
| 19 | + receipt_logo = fields.Binary(related="pos_config_id.receipt_logo") |
| 20 | + receipt_header = fields.Html(related="pos_config_id.receipt_header_html") |
| 21 | + receipt_footer = fields.Html(related="pos_config_id.receipt_footer_html") |
| 22 | + preview = fields.Html("_compute_receipt_preview") |
| 23 | + |
| 24 | + def receipt_layout_save(self): |
| 25 | + return {"type": "ir.actions.act_window_close"} |
| 26 | + |
| 27 | + @api.depends("receipt_layout", "receipt_header", "receipt_footer", "receipt_logo") |
| 28 | + def _compute_receipt_preview(self): |
| 29 | + for wizard in self: |
| 30 | + if wizard.pos_config_id: |
| 31 | + wizard.preview = wizard.env["ir.ui.view"]._render_template( |
| 32 | + wizard._get_template(), |
| 33 | + { |
| 34 | + "receipt_header": wizard.receipt_header, |
| 35 | + "receipt_footer": wizard.receipt_footer, |
| 36 | + "receipt_logo": wizard.receipt_logo, |
| 37 | + }, |
| 38 | + ) |
| 39 | + else: |
| 40 | + wizard.preview = False |
| 41 | + |
| 42 | + def _get_template(self): |
| 43 | + is_restaurant = self.pos_config_id.module_pos_restaurant |
| 44 | + if is_restaurant: |
| 45 | + base_module = "pos_receipt.report_restaurant_preview" |
| 46 | + else: |
| 47 | + base_module = "pos_receipt.report_receipts_wizard_preview" |
| 48 | + |
| 49 | + layout_templates = { |
| 50 | + "light": f"{base_module}_light", |
| 51 | + "boxes": f"{base_module}_boxes", |
| 52 | + "lined": f"{base_module}_lined", |
| 53 | + } |
| 54 | + |
| 55 | + return layout_templates.get(self.receipt_layout, layout_templates["light"]) |
0 commit comments