work_order.py 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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='Lista de pedidos'
  39. )
  40. consumed_ids = fields.One2many(
  41. comodel_name='repair.workorderimproved.consumed',
  42. inverse_name='workorder_id',
  43. string='Product & Services consumed'
  44. )
  45. order_date = fields.Datetime(
  46. string='Order date',
  47. default=fields.Datetime.now
  48. )
  49. planned_start_date = fields.Datetime(
  50. string='Planned start date'
  51. )
  52. planned_end_date = fields.Datetime(
  53. string='Planned end date'
  54. )
  55. name_obra = fields.Char(
  56. string='Obra'
  57. )
  58. name_local = fields.Char(
  59. string='Local'
  60. )
  61. contacto_obra = fields.Char(
  62. string='Contacto de la Obra'
  63. )
  64. nro_factura = fields.Char(
  65. string='N° de factura'
  66. )
  67. emergente = fields.Text(
  68. string='Pedidos adicionales y emergentes'
  69. )
  70. pedidoline_ids = fields.One2many(
  71. comodel_name='repair.pedidosorderimproved.line',
  72. inverse_name='workorder_id',
  73. string='Pedidos adicionales y emergentes'
  74. )
  75. resumenline_ids = fields.One2many(
  76. comodel_name='repair.resumenorderimproved.line',
  77. inverse_name='workorder_id',
  78. string='Estado y resumen de trabajos'
  79. )
  80. calidadline_ids = fields.One2many(
  81. comodel_name='repair.calidadorderimproved.line',
  82. inverse_name='workorder_id',
  83. string='Control de calidad, fichas y lacres'
  84. )
  85. sugerencialine_ids = fields.One2many(
  86. comodel_name='repair.sugerenciaorderimproved.line',
  87. inverse_name='workorder_id',
  88. string='Sugerencias y pendientes'
  89. )
  90. diagnostic = fields.Text(
  91. string='Diagnostic'
  92. )
  93. causes = fields.Text(
  94. string='Causes'
  95. )
  96. actions = fields.Text(
  97. string='Acciones'
  98. )
  99. recommendations = fields.Text(
  100. string="recommendations"
  101. )
  102. state = fields.Selection([
  103. ('draft', 'Pending'),
  104. ('in_progress', 'In progress'),
  105. ('done', 'Done'),
  106. ('canceled', 'Canceled'),
  107. ('invoiced', 'Invoiced')],
  108. string='State',
  109. default='draft'
  110. )
  111. invoice_ids = fields.One2many('account.invoice', 'work_invoice_id')
  112. invoice_count = fields.Integer(
  113. string='Facturas',
  114. compute='_get_invoice_count',
  115. )
  116. @api.multi
  117. def button_draft(self):
  118. if self.invoice_count > 0:
  119. raise Warning('Este trabajo tiene una factura asociada')
  120. if self.invoice_count == 0:
  121. for work in self:
  122. work.write({'state': 'draft'})
  123. return True
  124. @api.one
  125. @api.depends('invoice_ids')
  126. def _get_invoice_count(self):
  127. self.invoice_count = len(self.invoice_ids)
  128. @api.one
  129. def onchange_partner_id(self, partner_id):
  130. _log.info('-'*100)
  131. _log.info(partner_id)
  132. @api.one
  133. def button_in_progress(self):
  134. self.state = 'in_progress'
  135. @api.one
  136. def button_in_progress_back(self):
  137. self.state = 'draft'
  138. @api.one
  139. def button_done(self):
  140. # product = self.line_ids
  141. # works = self.consumed_ids
  142. # if not product or not works:
  143. # raise Warning('El trabajo debe tener productos y trabajos asociados')
  144. # else:
  145. self.state = 'done'
  146. @api.one
  147. def button_done_back(self):
  148. self.state = 'in_progress'
  149. @api.one
  150. def button_cancel(self):
  151. self.state = 'canceled'
  152. @api.multi
  153. def Facturado(self):
  154. inv_obj = self.env['account.invoice']
  155. inv_line_obj = self.env['account.invoice.line']
  156. customer = self.partner_id
  157. if not customer.name:
  158. raise osv.except_osv(_('UserError!'), _('Please select a Customer.'))
  159. company_id = self.env['res.users'].browse(1).company_id
  160. self.ensure_one()
  161. ir_values = self.env['ir.values']
  162. inv_data = {
  163. 'name': customer.name,
  164. 'reference': customer.name,
  165. 'account_id': customer.property_account_receivable.id,
  166. 'partner_id': customer.id,
  167. 'origin': self.name,
  168. 'work_invoice_id': self.id
  169. }
  170. inv_id = inv_obj.create(inv_data)
  171. for records in self.consumed_ids:
  172. if records.product_id.id:
  173. income_account = records.product_id.categ_id.property_account_income_categ.id
  174. if not income_account:
  175. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  176. 'for this product: "%s".') % (records.product_id.name,))
  177. inv_line_data = {
  178. 'name': records.product_id.name,
  179. 'account_id': income_account,
  180. 'price_unit': records.price_unit,
  181. 'quantity': records.quantity,
  182. 'product_id': records.product_id.id,
  183. 'invoice_id': inv_id.id,
  184. 'invoice_line_tax_id': [(6, 0, [x.id for x in records.product_id.taxes_id])],
  185. }
  186. inv_line_obj.create(inv_line_data)
  187. self.state = 'invoiced'
  188. imd = self.env['ir.model.data']
  189. action = imd.xmlid_to_object('account.action_invoice_tree1')
  190. list_view_id = imd.xmlid_to_res_id('account.invoice_tree')
  191. form_view_id = imd.xmlid_to_res_id('account.invoice_form')
  192. result = {
  193. 'name': action.name,
  194. 'help': action.help,
  195. 'type': 'ir.actions.act_window',
  196. 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'],
  197. [False, 'calendar'], [False, 'pivot']],
  198. 'target': action.target,
  199. 'context': action.context,
  200. 'res_model': 'account.invoice',
  201. }
  202. if len(inv_id) > 1:
  203. result['domain'] = "[('id','in',%s)]" % inv_id.ids
  204. elif len(inv_id) == 1:
  205. result['views'] = [(form_view_id, 'form')]
  206. result['res_id'] = inv_id.ids[0]
  207. else:
  208. result = {'type': 'ir.actions.act_window_close'}
  209. invoiced_records = self.env['repair.workorderimproved']
  210. total = 0
  211. self.stage_id = 3
  212. for rows in invoiced_records:
  213. invoiced_date = rows.date
  214. invoiced_date = invoiced_date[0:10]
  215. if invoiced_date == str(date.today()):
  216. total = total + rows.price_subtotal
  217. inv_id.signal_workflow('invoice_open')
  218. return result
  219. class WorkOrderLine(models.Model):
  220. _name = 'repair.workorderimproved.line'
  221. _description = 'Product to repair'
  222. _inherit = ['mail.thread', 'ir.needaction_mixin']
  223. workorder_id = fields.Many2one(
  224. comodel_name='repair.workorderimproved',
  225. string='Work order improved')
  226. description = fields.Char(string='Description')
  227. quantity = fields.Float(string='Quantity', default=1.0)
  228. brand = fields.Char(string='Marca')
  229. number = fields.Char(string="Numero de serie")
  230. class PedidosOrderLine(models.Model):
  231. _name = 'repair.pedidosorderimproved.line'
  232. _description = 'Pedidos adicionales y emergentes'
  233. _inherit = ['mail.thread', 'ir.needaction_mixin']
  234. workorder_id = fields.Many2one(
  235. comodel_name='repair.workorderimproved',
  236. string='Work order improved')
  237. description = fields.Char(string='Description')
  238. quantity = fields.Float(string='Quantity', default=1.0)
  239. brand = fields.Char(string='Marca')
  240. number = fields.Char(string="Numero de serie")
  241. class ResumenOrderLine(models.Model):
  242. _name = 'repair.resumenorderimproved.line'
  243. _description = 'Estado y resumen de trabajos'
  244. _inherit = ['mail.thread', 'ir.needaction_mixin']
  245. workorder_id = fields.Many2one(
  246. comodel_name='repair.workorderimproved',
  247. string='Work order improved')
  248. description = fields.Char(string='Description')
  249. class CalidadOrderLine(models.Model):
  250. _name = 'repair.calidadorderimproved.line'
  251. _description = 'Control de calidad, fichas y lacres'
  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. brand = fields.Char(string='Marca')
  258. number = fields.Char(string="Numero de serie")
  259. class SugerenciasOrderLine(models.Model):
  260. _name = 'repair.sugerenciaorderimproved.line'
  261. _description = 'Sugerencias y pendientes'
  262. _inherit = ['mail.thread', 'ir.needaction_mixin']
  263. workorder_id = fields.Many2one(
  264. comodel_name='repair.workorderimproved',
  265. string='Work order improved')
  266. description = fields.Char(string='Description')
  267. number = fields.Char(string="Numero de serie")
  268. class WorkOrderConsumed(models.Model):
  269. _name = 'repair.workorderimproved.consumed'
  270. _description = 'Services for repair'
  271. _inherit = ['mail.thread', 'ir.needaction_mixin']
  272. workorder_id = fields.Many2one(
  273. comodel_name='repair.workorderimproved',
  274. string='Work order'
  275. )
  276. product_id = fields.Many2one(
  277. comodel_name='product.product',
  278. string='Product'
  279. )
  280. type = fields.Selection([
  281. ('service', 'Service'),
  282. ('product', 'Product')],
  283. string='Type',
  284. required=True,
  285. default='service'
  286. )
  287. description = fields.Char(
  288. string='Description',
  289. required=True
  290. )
  291. quantity = fields.Float(
  292. string='Quantity',
  293. default=1
  294. )
  295. price_unit = fields.Float(
  296. string='Price unit'
  297. )
  298. subtotal = fields.Float(
  299. string='Subtotal',
  300. compute='compute_subtotal'
  301. )
  302. @api.one
  303. @api.depends('quantity', 'price_unit')
  304. def compute_subtotal(self):
  305. self.subtotal = self.quantity * self.price_unit
  306. @api.onchange('product_id')
  307. def onchange_product_id(self):
  308. if self.product_id:
  309. self.description = self.product_id.name
  310. self.type = 'service' if self.product_id.type == 'service' \
  311. else 'product'
  312. # @ TODO impuestos??
  313. # Obtener el precio del producto a partir de la tarifa del cliente
  314. self.price_unit = self.product_id.list_price
  315. class AccountInvoice(models.Model):
  316. _inherit = 'account.invoice'
  317. work_invoice_id = fields.Many2one('repair.workorderimproved')