product.py 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. from odoo.addons.base.models.res_partner import WARNING_MESSAGE, WARNING_HELP
  5. from odoo.tools.float_utils import float_round
  6. from dateutil.relativedelta import relativedelta
  7. class ProductTemplate(models.Model):
  8. _name = 'product.template'
  9. _inherit = 'product.template'
  10. purchased_product_qty = fields.Float(compute='_compute_purchased_product_qty', string='Purchased', digits='Product Unit of Measure')
  11. purchase_method = fields.Selection([
  12. ('purchase', 'On ordered quantities'),
  13. ('receive', 'On received quantities'),
  14. ], string="Control Policy", help="On ordered quantities: Control bills based on ordered quantities.\n"
  15. "On received quantities: Control bills based on received quantities.", default="receive")
  16. purchase_line_warn = fields.Selection(WARNING_MESSAGE, 'Purchase Order Line Warning', help=WARNING_HELP, required=True, default="no-message")
  17. purchase_line_warn_msg = fields.Text('Message for Purchase Order Line')
  18. def _compute_purchased_product_qty(self):
  19. for template in self:
  20. template.purchased_product_qty = float_round(sum([p.purchased_product_qty for p in template.product_variant_ids]), precision_rounding=template.uom_id.rounding)
  21. @api.model
  22. def get_import_templates(self):
  23. res = super(ProductTemplate, self).get_import_templates()
  24. if self.env.context.get('purchase_product_template'):
  25. return [{
  26. 'label': _('Import Template for Products'),
  27. 'template': '/purchase/static/xls/product_purchase.xls'
  28. }]
  29. return res
  30. def action_view_po(self):
  31. action = self.env["ir.actions.actions"]._for_xml_id("purchase.action_purchase_history")
  32. action['domain'] = ['&', ('state', 'in', ['purchase', 'done']), ('product_id', 'in', self.product_variant_ids.ids)]
  33. action['display_name'] = _("Purchase History for %s", self.display_name)
  34. return action
  35. class ProductProduct(models.Model):
  36. _name = 'product.product'
  37. _inherit = 'product.product'
  38. purchased_product_qty = fields.Float(compute='_compute_purchased_product_qty', string='Purchased',
  39. digits='Product Unit of Measure')
  40. def _compute_purchased_product_qty(self):
  41. date_from = fields.Datetime.to_string(fields.Date.context_today(self) - relativedelta(years=1))
  42. domain = [
  43. ('order_id.state', 'in', ['purchase', 'done']),
  44. ('product_id', 'in', self.ids),
  45. ('order_id.date_approve', '>=', date_from)
  46. ]
  47. order_lines = self.env['purchase.order.line']._read_group(domain, ['product_id', 'product_uom_qty'], ['product_id'])
  48. purchased_data = dict([(data['product_id'][0], data['product_uom_qty']) for data in order_lines])
  49. for product in self:
  50. if not product.id:
  51. product.purchased_product_qty = 0.0
  52. continue
  53. product.purchased_product_qty = float_round(purchased_data.get(product.id, 0), precision_rounding=product.uom_id.rounding)
  54. def action_view_po(self):
  55. action = self.env["ir.actions.actions"]._for_xml_id("purchase.action_purchase_history")
  56. action['domain'] = ['&', ('state', 'in', ['purchase', 'done']), ('product_id', 'in', self.ids)]
  57. action['display_name'] = _("Purchase History for %s", self.display_name)
  58. return action
  59. class ProductSupplierinfo(models.Model):
  60. _inherit = "product.supplierinfo"
  61. @api.onchange('partner_id')
  62. def _onchange_partner_id(self):
  63. self.currency_id = self.partner_id.property_purchase_currency_id.id or self.env.company.currency_id.id
  64. class ProductPackaging(models.Model):
  65. _inherit = 'product.packaging'
  66. purchase = fields.Boolean("Purchase", default=True, help="If true, the packaging can be used for purchase orders")