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.

100 lines
3.5 KiB

  1. from odoo import api, fields, models
  2. class PurchaseOrder(models.Model):
  3. _inherit = "purchase.order"
  4. # create_uid must mirror the supervisor_id value.
  5. # create_uid is a magic field that belongs to the ORM that is not
  6. # editable via a form.
  7. create_uid = fields.Many2one(
  8. comodel_name="res.users", compute="_compute_create_uid"
  9. )
  10. supervisor_id = fields.Many2one(
  11. comodel_name="res.users",
  12. string="Responsible",
  13. required=True,
  14. default=lambda self: self.env.user,
  15. )
  16. @api.depends("supervisor_id")
  17. def _compute_create_uid(self):
  18. for rec in self:
  19. if rec.supervisor_id:
  20. rec.create_uid = rec.supervisor_id
  21. @api.multi
  22. def write(self, vals):
  23. if "supervisor_id" in vals:
  24. new_partner = (
  25. self.env["res.users"]
  26. .browse(vals["supervisor_id"])
  27. .partner_id.id
  28. )
  29. for rec in self:
  30. rec.message_unsubscribe(
  31. partner_ids=rec.supervisor_id.partner_id.ids
  32. )
  33. rec.message_subscribe(
  34. partner_ids=[new_partner], subtype_ids=[]
  35. )
  36. return super(PurchaseOrder, self).write(vals)
  37. @api.multi
  38. def action_toggle_adapt_purchase_price(self):
  39. for order in self:
  40. for line in order.order_line:
  41. line.adapt_purchase_price ^= True
  42. @api.multi
  43. def action_toggle_adapt_selling_price(self):
  44. for order in self:
  45. for line in order.order_line:
  46. line.adapt_selling_price ^= True
  47. @api.multi
  48. def button_confirm(self):
  49. res = super(PurchaseOrder, self).button_confirm()
  50. for order in self:
  51. for line in order.order_line:
  52. product_id = line.product_id
  53. product_tmpl_id = product_id.product_tmpl_id
  54. seller = product_id._select_seller(
  55. partner_id=line.order_id.partner_id,
  56. quantity=line.product_qty,
  57. date=order.date_order and order.date_order.date(),
  58. uom_id=line.product_uom,
  59. params={"order_id": line.order_id},
  60. )
  61. price = line.price_unit
  62. suggested_price = (
  63. price * product_tmpl_id.uom_po_id.factor
  64. ) * (1 + product_tmpl_id.categ_id.profit_margin / 100)
  65. if line.adapt_purchase_price and line.adapt_selling_price:
  66. seller.price = price
  67. # will asynchronously trigger _compute_cost()
  68. # on `product.template` in `beesdoo_product`
  69. product_tmpl_id.list_price = suggested_price
  70. elif line.adapt_purchase_price:
  71. seller.price = price # see above comment
  72. elif line.adapt_selling_price:
  73. product_tmpl_id.list_price = suggested_price
  74. return res
  75. class PurchaseOrderLine(models.Model):
  76. _inherit = "purchase.order.line"
  77. adapt_purchase_price = fields.Boolean(
  78. default=False,
  79. string="Adapt vendor purchase price",
  80. help="Check this box to adapt the purchase price "
  81. "on the product page when confirming Purchase Order",
  82. )
  83. adapt_selling_price = fields.Boolean(
  84. default=False,
  85. string="Adapt product seling price",
  86. help="Check this box to adapt the selling price "
  87. "on the product page when confirming Purchase Order",
  88. )