work_order.py 11 KB

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