repair.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  2. from odoo import api, models
  3. class Repair(models.Model):
  4. _inherit = 'repair.order'
  5. @api.model_create_multi
  6. def create(self, vals_list):
  7. orders = super().create(vals_list)
  8. orders.action_explode()
  9. return orders
  10. def write(self, vals):
  11. res = super().write(vals)
  12. self.action_explode()
  13. return res
  14. def action_explode(self):
  15. lines_to_unlink_ids = set()
  16. line_vals_list = []
  17. for op in self.operations:
  18. bom = self.env['mrp.bom'].sudo()._bom_find(op.product_id, company_id=op.company_id.id, bom_type='phantom')[op.product_id]
  19. if not bom:
  20. continue
  21. factor = op.product_uom._compute_quantity(op.product_uom_qty, bom.product_uom_id) / bom.product_qty
  22. _boms, lines = bom.sudo().explode(op.product_id, factor, picking_type=bom.picking_type_id)
  23. for bom_line, line_data in lines:
  24. if bom_line.product_id.type != 'service':
  25. line_vals_list.append(op._prepare_phantom_line_vals(bom_line, line_data['qty']))
  26. lines_to_unlink_ids.add(op.id)
  27. self.env['repair.line'].browse(lines_to_unlink_ids).sudo().unlink()
  28. if line_vals_list:
  29. self.env['repair.line'].create(line_vals_list)
  30. class RepairLine(models.Model):
  31. _inherit = 'repair.line'
  32. def _prepare_phantom_line_vals(self, bom_line, qty):
  33. self.ensure_one()
  34. product = bom_line.product_id
  35. uom = bom_line.product_uom_id
  36. partner = self.repair_id.partner_id
  37. price = self.repair_id.pricelist_id._get_product_price(product, qty, uom=uom)
  38. tax = self.env['account.tax']
  39. if partner:
  40. partner_invoice = self.repair_id.partner_invoice_id or partner
  41. fpos = self.env['account.fiscal.position']._get_fiscal_position(partner_invoice, delivery=self.repair_id.address_id)
  42. taxes = self.product_id.taxes_id.filtered(lambda x: x.company_id == self.repair_id.company_id)
  43. tax = fpos.map_tax(taxes)
  44. return {
  45. 'name': self.name,
  46. 'repair_id': self.repair_id.id,
  47. 'type': self.type,
  48. 'product_id': product.id,
  49. 'price_unit': price,
  50. 'tax_id': [(4, t.id) for t in tax],
  51. 'product_uom_qty': qty,
  52. 'product_uom': uom.id,
  53. 'location_id': self.location_id.id,
  54. 'location_dest_id': self.location_dest_id.id,
  55. 'state': 'draft',
  56. }