work_order.py 8.3 KB

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