sale_order.py 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, api, fields
  3. class NewSaleOrderLine(models.Model):
  4. _inherit = 'sale.order.line'
  5. medic_id = fields.Many2one('res.partner', "Médico", domain="[('is_medic', '=', True)]")
  6. def _prepare_order_line_invoice_line(self, cr, uid, line, account_id=False, context=None):
  7. """Prepare the dict of values to create the new invoice line for a
  8. sales order line. This method may be overridden to implement custom
  9. invoice generation (making sure to call super() to establish
  10. a clean extension chain).
  11. :param browse_record line: sale.order.line record to invoice
  12. :param int account_id: optional ID of a G/L account to force
  13. (this is used for returning products including service)
  14. :return: dict of values to create() the invoice line
  15. """
  16. res = {}
  17. if not line.invoiced:
  18. if not account_id:
  19. if line.product_id:
  20. account_id = line.product_id.property_account_income.id
  21. if not account_id:
  22. account_id = line.product_id.categ_id.property_account_income_categ.id
  23. if not account_id:
  24. raise osv.except_osv(_('Error!'),
  25. _('Please define income account for this product: "%s" (id:%d).') % \
  26. (line.product_id.name, line.product_id.id,))
  27. else:
  28. prop = self.pool.get('ir.property').get(cr, uid,
  29. 'property_account_income_categ', 'product.category',
  30. context=context)
  31. account_id = prop and prop.id or False
  32. uosqty = self._get_line_qty(cr, uid, line, context=context)
  33. uos_id = self._get_line_uom(cr, uid, line, context=context)
  34. pu = 0.0
  35. if uosqty:
  36. pu = round(line.price_unit * line.product_uom_qty / uosqty,
  37. self.pool.get('decimal.precision').precision_get(cr, uid, 'Product Price'))
  38. fpos = line.order_id.fiscal_position or False
  39. account_id = self.pool.get('account.fiscal.position').map_account(cr, uid, fpos, account_id)
  40. if not account_id:
  41. raise osv.except_osv(_('Error!'),
  42. _('There is no Fiscal Position defined or Income category account defined for default properties of Product categories.'))
  43. res = {
  44. 'medic_id': line.medic_id.id or False,
  45. 'name': line.name,
  46. 'sequence': line.sequence,
  47. 'origin': line.order_id.name,
  48. 'account_id': account_id,
  49. 'price_unit': pu,
  50. 'quantity': uosqty,
  51. 'discount': line.discount,
  52. 'uos_id': uos_id,
  53. 'product_id': line.product_id.id or False,
  54. 'invoice_line_tax_id': [(6, 0, [x.id for x in line.tax_id])],
  55. 'account_analytic_id': line.order_id.project_id and line.order_id.project_id.id or False,
  56. }
  57. return res