work_order.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. # -*- coding: utf-8 -*-
  2. # License, author and contributors information in:
  3. # __openerp__.py file at the root folder of this module.
  4. from openerp import api, models, fields
  5. from openerp.exceptions import ValidationError, except_orm, Warning, RedirectWarning
  6. import logging
  7. _log = logging.getLogger(__name__)
  8. class WorkOrder(models.Model):
  9. _name = 'repair.workorderimproved'
  10. _description = 'Work order improved'
  11. _inherit = ['mail.thread', 'ir.needaction_mixin']
  12. def _get_user(self):
  13. return self.env.uid
  14. def _get_number(self):
  15. return self.env['ir.sequence'].get('repair.workorderimproved') or '*'
  16. name = fields.Char(
  17. string=u'Code',
  18. default=_get_number
  19. )
  20. user_id = fields.Many2one(
  21. comodel_name='res.users',
  22. string='Usuario',
  23. default=_get_user
  24. )
  25. responsable = fields.Char(
  26. string='Técnico Responsable'
  27. )
  28. movil = fields.Char(
  29. string='Móvil'
  30. )
  31. partner_id = fields.Many2one(
  32. comodel_name='res.partner',
  33. string='Partner'
  34. )
  35. line_ids = fields.One2many(
  36. comodel_name='repair.workorderimproved.line',
  37. inverse_name='workorder_id',
  38. string='Pedidos inicial del cliente'
  39. )
  40. order_date = fields.Datetime(
  41. string='Order date',
  42. default=fields.Datetime.now
  43. )
  44. planned_start_date = fields.Datetime(
  45. string='Planned start date'
  46. )
  47. planned_end_date = fields.Datetime(
  48. string='Planned end date'
  49. )
  50. date_service = fields.Datetime(
  51. string='Mantenimiento próximo'
  52. )
  53. name_obra = fields.Char(
  54. string='Obra'
  55. )
  56. name_local = fields.Char(
  57. string='Local'
  58. )
  59. contacto_obra = fields.Char(
  60. string='Contacto de la Obra'
  61. )
  62. nro_factura = fields.Char(
  63. string='N° de factura'
  64. )
  65. emergente = fields.Text(
  66. string='Pedidos adicionales y emergentes'
  67. )
  68. problemasline_ids = fields.One2many(
  69. comodel_name='repair.problemasorderimproved.line',
  70. inverse_name='workorder_id',
  71. string='Problemas o sintomas encontrados'
  72. )
  73. pedidoline_ids = fields.One2many(
  74. comodel_name='repair.pedidosorderimproved.line',
  75. inverse_name='workorder_id',
  76. string='Pedido adicional para servicio extra'
  77. )
  78. consumed_ids = fields.One2many(
  79. comodel_name='repair.workorderimproved.consumed',
  80. inverse_name='workorder_id',
  81. string='Lista de Materiales proveídos por Biolectric'
  82. )
  83. calidadline_ids = fields.One2many(
  84. comodel_name='repair.calidadorderimproved.line',
  85. inverse_name='workorder_id',
  86. string='Ensayos de control de calidad realizados a los trabajos o equipos entregados al cliente'
  87. )
  88. resumenline_ids = fields.One2many(
  89. comodel_name='repair.resumenorderimproved.line',
  90. inverse_name='workorder_id',
  91. string='Estado final de equipos y sistemas entregados al cliente luego de terminar los trabajos'
  92. )
  93. sugerencialine_ids = fields.One2many(
  94. comodel_name='repair.sugerenciaorderimproved.line',
  95. inverse_name='workorder_id',
  96. string='Sugerencias y agendamientos'
  97. )
  98. diagnostic = fields.Text(
  99. string='Diagnostic'
  100. )
  101. causes = fields.Text(
  102. string='Causes'
  103. )
  104. actions = fields.Text(
  105. string='Acciones'
  106. )
  107. recommendations = fields.Text(
  108. string="recommendations"
  109. )
  110. state = fields.Selection([
  111. ('draft', 'Pending'),
  112. ('in_progress', 'In progress'),
  113. ('done', 'Done'),
  114. ('canceled', 'Canceled'),
  115. ('invoiced', 'Invoiced')],
  116. string='State',
  117. default='draft'
  118. )
  119. invoice_ids = fields.One2many('account.invoice', 'work_invoice_id')
  120. invoice_count = fields.Integer(
  121. string='Facturas',
  122. compute='_get_invoice_count',
  123. )
  124. @api.multi
  125. def button_draft(self):
  126. if self.invoice_count > 0:
  127. raise Warning('Este trabajo tiene una factura asociada')
  128. if self.invoice_count == 0:
  129. for work in self:
  130. work.write({'state': 'draft'})
  131. return True
  132. @api.one
  133. @api.depends('invoice_ids')
  134. def _get_invoice_count(self):
  135. self.invoice_count = len(self.invoice_ids)
  136. @api.one
  137. def onchange_partner_id(self, partner_id):
  138. _log.info('-'*100)
  139. _log.info(partner_id)
  140. @api.one
  141. def button_in_progress(self):
  142. self.state = 'in_progress'
  143. @api.one
  144. def button_in_progress_back(self):
  145. self.state = 'draft'
  146. @api.one
  147. def button_done(self):
  148. # product = self.line_ids
  149. # works = self.consumed_ids
  150. # if not product or not works:
  151. # raise Warning('El trabajo debe tener productos y trabajos asociados')
  152. # else:
  153. self.state = 'done'
  154. @api.one
  155. def button_done_back(self):
  156. self.state = 'in_progress'
  157. @api.one
  158. def button_cancel(self):
  159. self.state = 'canceled'
  160. @api.multi
  161. def Facturado(self):
  162. inv_obj = self.env['account.invoice']
  163. inv_line_obj = self.env['account.invoice.line']
  164. customer = self.partner_id
  165. if not customer.name:
  166. raise osv.except_osv(_('UserError!'), _('Please select a Customer.'))
  167. company_id = self.env['res.users'].browse(1).company_id
  168. self.ensure_one()
  169. ir_values = self.env['ir.values']
  170. inv_data = {
  171. 'name': customer.name,
  172. 'reference': customer.name,
  173. 'account_id': customer.property_account_receivable.id,
  174. 'partner_id': customer.id,
  175. 'origin': self.name,
  176. 'work_invoice_id': self.id
  177. }
  178. inv_id = inv_obj.create(inv_data)
  179. for records in self.consumed_ids:
  180. if records.product_id.id:
  181. income_account = records.product_id.categ_id.property_account_income_categ.id
  182. if not income_account:
  183. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  184. 'for this product: "%s".') % (records.product_id.name,))
  185. inv_line_data = {
  186. 'name': records.product_id.name,
  187. 'account_id': income_account,
  188. 'price_unit': records.price_unit,
  189. 'quantity': records.quantity,
  190. 'product_id': records.product_id.id,
  191. 'invoice_id': inv_id.id,
  192. 'invoice_line_tax_id': [(6, 0, [x.id for x in records.product_id.taxes_id])],
  193. }
  194. inv_line_obj.create(inv_line_data)
  195. self.state = 'invoiced'
  196. imd = self.env['ir.model.data']
  197. action = imd.xmlid_to_object('account.action_invoice_tree1')
  198. list_view_id = imd.xmlid_to_res_id('account.invoice_tree')
  199. form_view_id = imd.xmlid_to_res_id('account.invoice_form')
  200. result = {
  201. 'name': action.name,
  202. 'help': action.help,
  203. 'type': 'ir.actions.act_window',
  204. 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'],
  205. [False, 'calendar'], [False, 'pivot']],
  206. 'target': action.target,
  207. 'context': action.context,
  208. 'res_model': 'account.invoice',
  209. }
  210. if len(inv_id) > 1:
  211. result['domain'] = "[('id','in',%s)]" % inv_id.ids
  212. elif len(inv_id) == 1:
  213. result['views'] = [(form_view_id, 'form')]
  214. result['res_id'] = inv_id.ids[0]
  215. else:
  216. result = {'type': 'ir.actions.act_window_close'}
  217. invoiced_records = self.env['repair.workorderimproved']
  218. total = 0
  219. self.stage_id = 3
  220. for rows in invoiced_records:
  221. invoiced_date = rows.date
  222. invoiced_date = invoiced_date[0:10]
  223. if invoiced_date == str(date.today()):
  224. total = total + rows.price_subtotal
  225. inv_id.signal_workflow('invoice_open')
  226. return result
  227. class WorkOrderLine(models.Model):
  228. _name = 'repair.workorderimproved.line'
  229. _description = 'Pedidos inicial del cliente'
  230. _inherit = ['mail.thread', 'ir.needaction_mixin']
  231. workorder_id = fields.Many2one(
  232. comodel_name='repair.workorderimproved',
  233. string='Work order improved')
  234. description = fields.Char(string='Description')
  235. quantity = fields.Float(string='Quantity')
  236. brand = fields.Char(string='Marca')
  237. number = fields.Char(string="Numero de serie")
  238. class PedidosOrderLine(models.Model):
  239. _name = 'repair.pedidosorderimproved.line'
  240. _description = 'Pedido adicional para servicio extra'
  241. _inherit = ['mail.thread', 'ir.needaction_mixin']
  242. workorder_id = fields.Many2one(
  243. comodel_name='repair.workorderimproved',
  244. string='Work order improved')
  245. description = fields.Char(string='Description')
  246. quantity = fields.Float(string='Quantity')
  247. brand = fields.Char(string='Marca')
  248. number = fields.Char(string="Numero de serie")
  249. class ResumenOrderLine(models.Model):
  250. _name = 'repair.resumenorderimproved.line'
  251. _description = 'Estado final de equipos y sistemas entregados al cliente luego de terminar los trabajos'
  252. _inherit = ['mail.thread', 'ir.needaction_mixin']
  253. workorder_id = fields.Many2one(
  254. comodel_name='repair.workorderimproved',
  255. string='Work order improved')
  256. description = fields.Char(string='Description')
  257. class CalidadOrderLine(models.Model):
  258. _name = 'repair.calidadorderimproved.line'
  259. _description = 'Ensayos de control de calidad realizados a los trabajos o equipos entregados al cliente'
  260. _inherit = ['mail.thread', 'ir.needaction_mixin']
  261. workorder_id = fields.Many2one(
  262. comodel_name='repair.workorderimproved',
  263. string='Work order improved')
  264. description = fields.Char(string='Description')
  265. brand = fields.Char(string='Marca')
  266. number = fields.Char(string="Numero de serie")
  267. class SugerenciasOrderLine(models.Model):
  268. _name = 'repair.sugerenciaorderimproved.line'
  269. _description = 'Sugerencias y agendamientos'
  270. _inherit = ['mail.thread', 'ir.needaction_mixin']
  271. workorder_id = fields.Many2one(
  272. comodel_name='repair.workorderimproved',
  273. string='Work order improved')
  274. description = fields.Char(string='Description')
  275. number = fields.Char(string="Numero de serie")
  276. class ProblemasOrderLine(models.Model):
  277. _name = 'repair.problemasorderimproved.line'
  278. _description = 'Problemas o síntomas encontrados'
  279. _inherit = ['mail.thread', 'ir.needaction_mixin']
  280. workorder_id = fields.Many2one(
  281. comodel_name='repair.workorderimproved',
  282. string='Work order improved')
  283. description = fields.Char(string='Description')
  284. class WorkOrderConsumed(models.Model):
  285. _name = 'repair.workorderimproved.consumed'
  286. _description = 'Lista de Materiales proveídos por Biolectric'
  287. _inherit = ['mail.thread', 'ir.needaction_mixin']
  288. workorder_id = fields.Many2one(
  289. comodel_name='repair.workorderimproved',
  290. string='Work order'
  291. )
  292. product_id = fields.Many2one(
  293. comodel_name='product.product',
  294. string='Product'
  295. )
  296. type = fields.Selection([
  297. ('service', 'Service'),
  298. ('product', 'Product')],
  299. string='Type',
  300. required=True,
  301. default='service'
  302. )
  303. description = fields.Char(
  304. string='Description',
  305. required=True
  306. )
  307. quantity = fields.Float(
  308. string='Quantity',
  309. default=1
  310. )
  311. price_unit = fields.Float(
  312. string='Price unit'
  313. )
  314. subtotal = fields.Float(
  315. string='Subtotal',
  316. compute='compute_subtotal'
  317. )
  318. @api.one
  319. @api.depends('quantity', 'price_unit')
  320. def compute_subtotal(self):
  321. self.subtotal = self.quantity * self.price_unit
  322. @api.onchange('product_id')
  323. def onchange_product_id(self):
  324. if self.product_id:
  325. self.description = self.product_id.name
  326. self.type = 'service' if self.product_id.type == 'service' \
  327. else 'product'
  328. # @ TODO impuestos??
  329. # Obtener el precio del producto a partir de la tarifa del cliente
  330. self.price_unit = self.product_id.list_price
  331. class AccountInvoice(models.Model):
  332. _inherit = 'account.invoice'
  333. work_invoice_id = fields.Many2one('repair.workorderimproved')