proyect_car.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. # -*- coding: utf-8 -*-
  2. from openerp import models, fields, api
  3. from openerp.exceptions import except_orm, Warning, RedirectWarning
  4. class CarCarModify(models.Model):
  5. _inherit = 'car.car'
  6. name = fields.Many2one('car.service', string='Nombre del vehiculo', required=True)
  7. agent_id = fields.Many2one('res.partner', string='Agente de Seguro')
  8. policy = fields.Boolean()
  9. @api.one
  10. @api.onchange('name')
  11. def onchange_name(self):
  12. self.partner_id = self.name.partner_id.id
  13. self.agent_id = self.name.agent_id.id
  14. self.policy = self.name.policy
  15. date_in = fields.Date('Fecha de entrada', default=fields.Date.today())
  16. date_out = fields.Date('Fecha de Salida')
  17. image_medium = fields.Binary('Binary File', related="name.logo")
  18. @api.multi
  19. def unlink(self):
  20. for project in self:
  21. if project.state in ('close'):
  22. raise Warning('No puedes borrar un proyecto en estado cerrado')
  23. if len(project.task_ids) > 0:
  24. raise Warning('No puedes borrar un proyecto que tiene tareas')
  25. return super(CarCarModify, self).unlink()
  26. def on_change_vehicle(self):
  27. if not self.name:
  28. return {}
  29. model = self.pool.get('car.service').browse(self.name)
  30. return {
  31. 'value': {
  32. 'image_medium': model.logo,
  33. }
  34. }
  35. class CarWorkshopModify(models.Model):
  36. _inherit = 'car.workshop'
  37. invoice_ids = fields.One2many('account.invoice', 'work_invoice_id')
  38. invoice_count = fields.Integer(
  39. string='Facturas',
  40. compute='_get_invoice_count',
  41. )
  42. @api.one
  43. @api.depends('invoice_ids','state')
  44. def _get_invoice_count(self):
  45. self.invoice_count = len(self.invoice_ids)
  46. @api.multi
  47. def button_draft(self):
  48. if self.invoice_count > 0:
  49. raise Warning('Esta tarea tiene una factura asociada')
  50. if self.invoice_count == 0:
  51. for work in self:
  52. work.write({'state': 'waiting'})
  53. return True
  54. @api.multi
  55. def workshop_create_invoices(self):
  56. self.state = 'workshop_create_invoices'
  57. inv_obj = self.env['account.invoice']
  58. inv_line_obj = self.env['account.invoice.line']
  59. customer = self.partner_id
  60. if not customer.name:
  61. raise osv.except_osv(_('UserError!'), _('Please select a Customer.'))
  62. company_id = self.env['res.users'].browse(1).company_id
  63. currency_value = company_id.currency_id.id
  64. self.ensure_one()
  65. ir_values = self.env['ir.values']
  66. journal_id = ir_values.get_default('workshop.config.setting', 'invoice_journal_type')
  67. if not journal_id:
  68. journal_id = 1
  69. inv_data = {
  70. 'name': customer.name,
  71. 'reference': customer.name,
  72. 'account_id': customer.property_account_receivable.id,
  73. 'partner_id': customer.id,
  74. 'currency_id': currency_value,
  75. 'journal_id': journal_id,
  76. 'origin': self.name,
  77. 'company_id': company_id.id,
  78. 'work_invoice_id' : self.id,
  79. }
  80. inv_id = inv_obj.create(inv_data)
  81. for records in self.planned_works:
  82. if records.planned_work.id:
  83. income_account = records.planned_work.property_account_income.id
  84. if not income_account:
  85. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  86. 'for this product: "%s".') % (records.planned_work.name,))
  87. inv_line_data = {
  88. 'name': records.planned_work.name,
  89. 'account_id': income_account,
  90. 'price_unit': records.work_cost,
  91. 'quantity': 1,
  92. 'product_id': records.planned_work.id,
  93. 'invoice_id': inv_id.id,
  94. }
  95. inv_line_obj.create(inv_line_data)
  96. for records in self.materials_used:
  97. if records.material.id:
  98. income_account = records.material.property_account_income.id
  99. if not income_account:
  100. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  101. 'for this product: "%s".') % (records.material.name,))
  102. inv_line_data = {
  103. 'name': records.material.name,
  104. 'account_id': records.material.property_account_income.id,
  105. 'price_unit': records.price,
  106. 'quantity': records.amount,
  107. 'product_id': records.material.id,
  108. 'invoice_id': inv_id.id,
  109. }
  110. inv_line_obj.create(inv_line_data)
  111. imd = self.env['ir.model.data']
  112. action = imd.xmlid_to_object('account.action_invoice_tree1')
  113. list_view_id = imd.xmlid_to_res_id('account.invoice_tree')
  114. form_view_id = imd.xmlid_to_res_id('account.invoice_form')
  115. result = {
  116. 'name': action.name,
  117. 'help': action.help,
  118. 'type': 'ir.actions.act_window',
  119. 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'],
  120. [False, 'calendar'], [False, 'pivot']],
  121. 'target': action.target,
  122. 'context': action.context,
  123. 'res_model': 'account.invoice',
  124. }
  125. if len(inv_id) > 1:
  126. result['domain'] = "[('id','in',%s)]" % inv_id.ids
  127. elif len(inv_id) == 1:
  128. result['views'] = [(form_view_id, 'form')]
  129. result['res_id'] = inv_id.ids[0]
  130. else:
  131. result = {'type': 'ir.actions.act_window_close'}
  132. invoiced_records = self.env['car.workshop']
  133. total = 0
  134. for rows in invoiced_records:
  135. invoiced_date = rows.date
  136. invoiced_date = invoiced_date[0:10]
  137. if invoiced_date == str(date.today()):
  138. total = total + rows.price_subtotal
  139. return result
  140. @api.multi
  141. def unlink(self):
  142. for task in self:
  143. if task.state in ('workshop_create_invoices'):
  144. raise Warning(('No puedes borrar una tarea ya facturada'))
  145. if len(task.planned_works)>0:
  146. raise Warning(('No puedes borrar una tarea que tiene actividades'))
  147. return super(CarWorkshopModify, self).unlink()
  148. class AccountInvoiceModify(models.Model):
  149. _inherit = 'account.invoice'
  150. work_invoice_id = fields.Many2one('car.workshop')