proyect_car.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  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 AccountInvoiceModify(models.Model):
  36. _inherit = 'account.invoice'
  37. work_invoice_id = fields.Many2one('car.workshop')
  38. class CarWorkshopModify(models.Model):
  39. _inherit = 'car.workshop'
  40. invoice_ids = fields.One2many('account.invoice', 'work_invoice_id')
  41. invoice_count = fields.Integer(
  42. string='Facturas',
  43. compute='_get_invoice_count',
  44. )
  45. @api.one
  46. @api.depends('invoice_ids','state')
  47. def _get_invoice_count(self):
  48. self.invoice_count = len(self.invoice_ids)
  49. @api.multi
  50. def button_draft(self):
  51. if self.invoice_count > 0:
  52. raise Warning('Esta tarea tiene una factura asociada')
  53. if self.invoice_count == 0:
  54. for work in self:
  55. work.write({'state': 'waiting'})
  56. return True
  57. @api.multi
  58. def unlink(self):
  59. for task in self:
  60. if task.state in ('workshop_create_invoices'):
  61. raise Warning(('No puedes borrar una tarea ya facturada'))
  62. if len(task.planned_works)>0:
  63. raise Warning(('No puedes borrar una tarea que tiene actividades'))
  64. return super(CarWorkshopModify, self).unlink()
  65. @api.multi
  66. def workshop_create_invoices(self):
  67. self.state = 'workshop_create_invoices'
  68. inv_obj = self.env['account.invoice']
  69. inv_line_obj = self.env['account.invoice.line']
  70. customer = self.partner_id
  71. if not customer.name:
  72. raise osv.except_osv(_('UserError!'), _('Please select a Customer.'))
  73. company_id = self.env['res.users'].browse(1).company_id
  74. currency_value = company_id.currency_id.id
  75. self.ensure_one()
  76. ir_values = self.env['ir.values']
  77. journal_id = ir_values.get_default('workshop.config.setting', 'invoice_journal_type')
  78. if not journal_id:
  79. journal_id = 1
  80. inv_data = {
  81. 'name': customer.name,
  82. 'reference': customer.name,
  83. 'account_id': customer.property_account_receivable.id,
  84. 'partner_id': customer.id,
  85. 'currency_id': currency_value,
  86. 'journal_id': journal_id,
  87. 'origin': self.name,
  88. 'company_id': company_id.id,
  89. 'work_invoice_id': self.id
  90. }
  91. inv_id = inv_obj.create(inv_data)
  92. for records in self.planned_works:
  93. if records.planned_work.id:
  94. income_account = records.planned_work.property_account_income.id
  95. if not income_account:
  96. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  97. 'for this product: "%s".') % (records.planned_work.name,))
  98. inv_line_data = {
  99. 'name': records.planned_work.name,
  100. 'account_id': income_account,
  101. 'price_unit': records.work_cost,
  102. 'quantity': 1,
  103. 'product_id': records.planned_work.id,
  104. 'invoice_id': inv_id.id,
  105. }
  106. inv_line_obj.create(inv_line_data)
  107. for records in self.materials_used:
  108. if records.material.id:
  109. income_account = records.material.property_account_income.id
  110. if not income_account:
  111. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  112. 'for this product: "%s".') % (records.material.name,))
  113. if self.include_materials==True:
  114. inv_line_data = {
  115. 'name': records.material.name,
  116. 'account_id': records.material.property_account_income.id,
  117. 'price_unit': records.price,
  118. 'quantity': records.amount,
  119. 'product_id': records.material.id,
  120. 'invoice_id': inv_id.id,
  121. }
  122. inv_line_obj.create(inv_line_data)
  123. imd = self.env['ir.model.data']
  124. action = imd.xmlid_to_object('account.action_invoice_tree1')
  125. list_view_id = imd.xmlid_to_res_id('account.invoice_tree')
  126. form_view_id = imd.xmlid_to_res_id('account.invoice_form')
  127. result = {
  128. 'name': action.name,
  129. 'help': action.help,
  130. 'type': 'ir.actions.act_window',
  131. 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'],
  132. [False, 'calendar'], [False, 'pivot']],
  133. 'target': action.target,
  134. 'context': action.context,
  135. 'res_model': 'account.invoice',
  136. }
  137. if len(inv_id) > 1:
  138. result['domain'] = "[('id','in',%s)]" % inv_id.ids
  139. elif len(inv_id) == 1:
  140. result['views'] = [(form_view_id, 'form')]
  141. result['res_id'] = inv_id.ids[0]
  142. else:
  143. result = {'type': 'ir.actions.act_window_close'}
  144. invoiced_records = self.env['car.workshop']
  145. total = 0
  146. for rows in invoiced_records:
  147. invoiced_date = rows.date
  148. invoiced_date = invoiced_date[0:10]
  149. if invoiced_date == str(date.today()):
  150. total = total + rows.price_subtotal
  151. return result