stock_rules_report.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import api, fields, models
  4. class StockRulesReport(models.TransientModel):
  5. _name = 'stock.rules.report'
  6. _description = 'Stock Rules report'
  7. product_id = fields.Many2one('product.product', string='Product', required=True)
  8. product_tmpl_id = fields.Many2one('product.template', string='Product Template', required=True)
  9. warehouse_ids = fields.Many2many('stock.warehouse', string='Warehouses', required=True,
  10. help="Show the routes that apply on selected warehouses.")
  11. product_has_variants = fields.Boolean('Has variants', default=False, required=True)
  12. @api.model
  13. def default_get(self, fields):
  14. res = super(StockRulesReport, self).default_get(fields)
  15. product_tmpl_id = self.env['product.template']
  16. if 'product_id' in fields:
  17. if self.env.context.get('default_product_id'):
  18. product_id = self.env['product.product'].browse(self.env.context['default_product_id'])
  19. product_tmpl_id = product_id.product_tmpl_id
  20. res['product_tmpl_id'] = product_id.product_tmpl_id.id
  21. res['product_id'] = product_id.id
  22. elif self.env.context.get('default_product_tmpl_id'):
  23. product_tmpl_id = self.env['product.template'].browse(self.env.context['default_product_tmpl_id'])
  24. res['product_tmpl_id'] = product_tmpl_id.id
  25. res['product_id'] = product_tmpl_id.product_variant_id.id
  26. if len(product_tmpl_id.product_variant_ids) > 1:
  27. res['product_has_variants'] = True
  28. if 'warehouse_ids' in fields:
  29. company = product_tmpl_id.company_id or self.env.company
  30. warehouse_id = self.env['stock.warehouse'].search([('company_id', '=', company.id)], limit=1).id
  31. res['warehouse_ids'] = [(6, 0, [warehouse_id])]
  32. return res
  33. def _prepare_report_data(self):
  34. data = {
  35. 'product_id': self.product_id.id,
  36. 'warehouse_ids': self.warehouse_ids.ids,
  37. }
  38. return data
  39. def print_report(self):
  40. self.ensure_one()
  41. data = self._prepare_report_data()
  42. return self.env.ref('stock.action_report_stock_rule').report_action(None, data=data)