OCA reporting engine fork for dev and update.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

103 lines
3.1 KiB

  1. from odoo import api, models, fields
  2. class ReportLabelWizard(models.TransientModel):
  3. _name = "report.label.wizard"
  4. _description = "Report Label Wizard"
  5. @api.model
  6. def _default_line_ids(self):
  7. """ Compute line_ids based on context """
  8. active_model = self.env.context.get("active_model")
  9. active_ids = self.env.context.get("active_ids", [])
  10. if not active_model or not active_ids:
  11. return False
  12. return [
  13. (0, 0, {
  14. "res_id": res_id,
  15. "quantity": 1,
  16. })
  17. for res_id in active_ids
  18. ]
  19. model_id = fields.Many2one(
  20. "ir.model",
  21. "Model",
  22. required=True,
  23. default=lambda self: self.env.context.get("res_model_id"),
  24. )
  25. label_paperformat_id = fields.Many2one(
  26. "report.paperformat.label",
  27. "Label Paper Format",
  28. readonly=True,
  29. required=True,
  30. default=lambda self: self.env.context.get("label_paperformat_id"),
  31. )
  32. label_template = fields.Char(
  33. "Label QWeb Template",
  34. readonly=True,
  35. required=True,
  36. default=lambda self: self.env.context.get("label_template"),
  37. )
  38. offset = fields.Integer(
  39. help="Number of labels to skip when printing",
  40. )
  41. line_ids = fields.One2many(
  42. "report.label.wizard.line",
  43. "wizard_id",
  44. "Lines",
  45. default=_default_line_ids,
  46. required=True,
  47. )
  48. def _prepare_report_data(self):
  49. self.ensure_one()
  50. return {
  51. "label_format": self.label_paperformat_id.read()[0],
  52. "label_template": self.label_template,
  53. "offset": self.offset,
  54. "res_model": self.model_id.model,
  55. "lines": [
  56. {
  57. "res_id": line.res_id,
  58. "quantity": line.quantity,
  59. }
  60. for line in self.line_ids
  61. ],
  62. }
  63. def print_report(self):
  64. self.ensure_one()
  65. report = self.env.ref("report_label.report_label")
  66. action = report.report_action(self, data=self._prepare_report_data())
  67. action["context"] = {
  68. "paperformat_id": self.label_paperformat_id.paperformat_id.id,
  69. }
  70. return action
  71. class ReportLabelWizardLine(models.TransientModel):
  72. _name = "report.label.wizard.line"
  73. _description = "Report Label Wizard Line"
  74. _order = "sequence"
  75. wizard_id = fields.Many2one(
  76. "report.label.wizard",
  77. "Wizard",
  78. required=True,
  79. ondelete="cascade",
  80. )
  81. sequence = fields.Integer(default=10)
  82. res_id = fields.Integer("Resource ID", required=True)
  83. res_name = fields.Char(compute="_compute_res_name")
  84. quantity = fields.Integer(default=1, required=True)
  85. @api.depends("wizard_id.model_id", "res_id")
  86. def _compute_res_name(self):
  87. wizard = self.mapped("wizard_id")
  88. wizard.ensure_one()
  89. res_model = wizard.model_id.model
  90. res_ids = self.mapped("res_id")
  91. names_map = dict(self.env[res_model].browse(res_ids).name_get())
  92. for rec in self:
  93. rec.res_name = names_map.get(rec.res_id)