work_order.py 8.7 KB

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