account_move.py 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import models, fields, api, _
  4. class AccountMove(models.Model):
  5. _inherit = 'account.move'
  6. def _post(self, soft=True):
  7. vendor_bill_service = self.env.ref('account_fleet.data_fleet_service_type_vendor_bill', raise_if_not_found=False)
  8. if not vendor_bill_service:
  9. return super()._post(soft)
  10. val_list = []
  11. log_list = []
  12. not_posted_before = self.filtered(lambda r: not r.posted_before)
  13. posted = super()._post(soft) # We need the move name to be set, but we also need to know which move are posted for the first time.
  14. for line in (not_posted_before & posted).line_ids.filtered(lambda ml: ml.vehicle_id and ml.move_id.move_type == 'in_invoice'):
  15. val = line._prepare_fleet_log_service()
  16. log = _(
  17. 'Service Vendor Bill: %s',
  18. line.move_id._get_html_link(),
  19. )
  20. val_list.append(val)
  21. log_list.append(log)
  22. log_service_ids = self.env['fleet.vehicle.log.services'].create(val_list)
  23. for log_service_id, log in zip(log_service_ids, log_list):
  24. log_service_id.message_post(body=log)
  25. return posted
  26. class AccountMoveLine(models.Model):
  27. _inherit = 'account.move.line'
  28. vehicle_id = fields.Many2one('fleet.vehicle', string='Vehicle', index='btree_not_null')
  29. # used to decide whether the vehicle_id field is editable
  30. need_vehicle = fields.Boolean(compute='_compute_need_vehicle')
  31. def _compute_need_vehicle(self):
  32. self.need_vehicle = False
  33. def _prepare_fleet_log_service(self):
  34. vendor_bill_service = self.env.ref('account_fleet.data_fleet_service_type_vendor_bill', raise_if_not_found=False)
  35. return {
  36. 'service_type_id': vendor_bill_service.id,
  37. 'vehicle_id': self.vehicle_id.id,
  38. 'amount': self.price_subtotal,
  39. 'vendor_id': self.partner_id.id,
  40. 'description': self.name,
  41. }