work_order.py 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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='Engineer',
  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='Products delivered'
  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. emergente = fields.Text(
  57. string='Pedidos adicionales y emergentes'
  58. )
  59. diagnostic = fields.Text(
  60. string='Diagnostic'
  61. )
  62. causes = fields.Text(
  63. string='Causes'
  64. )
  65. actions = fields.Text(
  66. string='Acciones'
  67. )
  68. recommendations = fields.Text(
  69. string="recommendations"
  70. )
  71. state = fields.Selection([
  72. ('draft', 'Pending'),
  73. ('in_progress', 'In progress'),
  74. ('done', 'Done'),
  75. ('canceled', 'Canceled'),
  76. ('invoiced', 'Invoiced')],
  77. string='State',
  78. default='draft'
  79. )
  80. invoice_ids = fields.One2many('account.invoice', 'work_invoice_id')
  81. invoice_count = fields.Integer(
  82. string='Facturas',
  83. compute='_get_invoice_count',
  84. )
  85. @api.multi
  86. def button_draft(self):
  87. if self.invoice_count > 0:
  88. raise Warning('Este trabajo tiene una factura asociada')
  89. if self.invoice_count == 0:
  90. for work in self:
  91. work.write({'state': 'draft'})
  92. return True
  93. @api.one
  94. @api.depends('invoice_ids')
  95. def _get_invoice_count(self):
  96. self.invoice_count = len(self.invoice_ids)
  97. @api.one
  98. def onchange_partner_id(self, partner_id):
  99. _log.info('-'*100)
  100. _log.info(partner_id)
  101. @api.one
  102. def button_in_progress(self):
  103. self.state = 'in_progress'
  104. @api.one
  105. def button_in_progress_back(self):
  106. self.state = 'draft'
  107. @api.one
  108. def button_done(self):
  109. product = self.line_ids
  110. works = self.consumed_ids
  111. if not product or not works:
  112. raise Warning('El trabajo debe tener productos y trabajos asociados')
  113. else:
  114. self.state = 'done'
  115. @api.one
  116. def button_done_back(self):
  117. self.state = 'in_progress'
  118. @api.one
  119. def button_cancel(self):
  120. self.state = 'canceled'
  121. @api.multi
  122. def Facturado(self):
  123. inv_obj = self.env['account.invoice']
  124. inv_line_obj = self.env['account.invoice.line']
  125. customer = self.partner_id
  126. if not customer.name:
  127. raise osv.except_osv(_('UserError!'), _('Please select a Customer.'))
  128. company_id = self.env['res.users'].browse(1).company_id
  129. self.ensure_one()
  130. ir_values = self.env['ir.values']
  131. inv_data = {
  132. 'name': customer.name,
  133. 'reference': customer.name,
  134. 'account_id': customer.property_account_receivable.id,
  135. 'partner_id': customer.id,
  136. 'origin': self.name,
  137. 'work_invoice_id': self.id
  138. }
  139. inv_id = inv_obj.create(inv_data)
  140. for records in self.consumed_ids:
  141. if records.product_id.id:
  142. income_account = records.product_id.categ_id.property_account_income_categ.id
  143. if not income_account:
  144. raise osv.except_osv(_('UserError!'), _('There is no income account defined '
  145. 'for this product: "%s".') % (records.product_id.name,))
  146. inv_line_data = {
  147. 'name': records.product_id.name,
  148. 'account_id': income_account,
  149. 'price_unit': records.price_unit,
  150. 'quantity': records.quantity,
  151. 'product_id': records.product_id.id,
  152. 'invoice_id': inv_id.id,
  153. 'invoice_line_tax_id': [(6, 0, [x.id for x in records.product_id.taxes_id])],
  154. }
  155. inv_line_obj.create(inv_line_data)
  156. self.state = 'invoiced'
  157. imd = self.env['ir.model.data']
  158. action = imd.xmlid_to_object('account.action_invoice_tree1')
  159. list_view_id = imd.xmlid_to_res_id('account.invoice_tree')
  160. form_view_id = imd.xmlid_to_res_id('account.invoice_form')
  161. result = {
  162. 'name': action.name,
  163. 'help': action.help,
  164. 'type': 'ir.actions.act_window',
  165. 'views': [[list_view_id, 'tree'], [form_view_id, 'form'], [False, 'graph'], [False, 'kanban'],
  166. [False, 'calendar'], [False, 'pivot']],
  167. 'target': action.target,
  168. 'context': action.context,
  169. 'res_model': 'account.invoice',
  170. }
  171. if len(inv_id) > 1:
  172. result['domain'] = "[('id','in',%s)]" % inv_id.ids
  173. elif len(inv_id) == 1:
  174. result['views'] = [(form_view_id, 'form')]
  175. result['res_id'] = inv_id.ids[0]
  176. else:
  177. result = {'type': 'ir.actions.act_window_close'}
  178. invoiced_records = self.env['repair.workorderimproved']
  179. total = 0
  180. self.stage_id = 3
  181. for rows in invoiced_records:
  182. invoiced_date = rows.date
  183. invoiced_date = invoiced_date[0:10]
  184. if invoiced_date == str(date.today()):
  185. total = total + rows.price_subtotal
  186. inv_id.signal_workflow('invoice_open')
  187. return result
  188. class WorkOrderLine(models.Model):
  189. _name = 'repair.workorderimproved.line'
  190. _description = 'Product to repair'
  191. _inherit = ['mail.thread', 'ir.needaction_mixin']
  192. workorder_id = fields.Many2one(
  193. comodel_name='repair.workorderimproved',
  194. string='Work order improved')
  195. description = fields.Char(string='Description')
  196. quantity = fields.Float(string='Quantity', default=1.0)
  197. brand = fields.Char(string='Marca')
  198. number = fields.Char(string="Numero de serie")
  199. class WorkOrderConsumed(models.Model):
  200. _name = 'repair.workorderimproved.consumed'
  201. _description = 'Services for repair'
  202. _inherit = ['mail.thread', 'ir.needaction_mixin']
  203. workorder_id = fields.Many2one(
  204. comodel_name='repair.workorderimproved',
  205. string='Work order'
  206. )
  207. product_id = fields.Many2one(
  208. comodel_name='product.product',
  209. string='Product'
  210. )
  211. type = fields.Selection([
  212. ('service', 'Service'),
  213. ('product', 'Product')],
  214. string='Type',
  215. required=True,
  216. default='service'
  217. )
  218. description = fields.Char(
  219. string='Description',
  220. required=True
  221. )
  222. quantity = fields.Float(
  223. string='Quantity',
  224. default=1
  225. )
  226. price_unit = fields.Float(
  227. string='Price unit'
  228. )
  229. subtotal = fields.Float(
  230. string='Subtotal',
  231. compute='compute_subtotal'
  232. )
  233. @api.one
  234. @api.depends('quantity', 'price_unit')
  235. def compute_subtotal(self):
  236. self.subtotal = self.quantity * self.price_unit
  237. @api.onchange('product_id')
  238. def onchange_product_id(self):
  239. if self.product_id:
  240. self.description = self.product_id.name
  241. self.type = 'service' if self.product_id.type == 'service' \
  242. else 'product'
  243. # @ TODO impuestos??
  244. # Obtener el precio del producto a partir de la tarifa del cliente
  245. self.price_unit = self.product_id.list_price
  246. class AccountInvoice(models.Model):
  247. _inherit = 'account.invoice'
  248. work_invoice_id = fields.Many2one('repair.workorderimproved')