orden_servicio.py 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455
  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. from openerp.tools import DEFAULT_SERVER_TIME_FORMAT
  7. from openerp.tools import DEFAULT_SERVER_DATETIME_FORMAT
  8. from datetime import datetime
  9. import logging
  10. _log = logging.getLogger(__name__)
  11. class OrdenServicio(models.Model):
  12. _name = 'orden.servicio'
  13. _description = 'Orden de servicio'
  14. _inherit = ['mail.thread', 'ir.needaction_mixin']
  15. def _get_user(self):
  16. return self.env.uid
  17. def _get_number(self):
  18. return self.env['ir.sequence'].get('orden.servicio') or '*'
  19. name = fields.Char(
  20. string=u'Referencia',
  21. default=_get_number
  22. )
  23. user_id = fields.Many2one(
  24. comodel_name='res.users',
  25. string='Usuario',
  26. default=_get_user
  27. )
  28. partner_id = fields.Many2one(
  29. comodel_name='res.partner',
  30. string='Cliente'
  31. )
  32. celular_partner = fields.Char(related='partner_id.mobile', string='Móvil', store=True)
  33. telefono_partner = fields.Char(related='partner_id.phone', string='Teléfono', store=True)
  34. ubicacion_google_link = fields.Char(string='Ubicación Google Link')
  35. currency_id = fields.Many2one('res.currency', string='Moneda Base')
  36. company_id = fields.Many2one('res.company', string='Empresa')
  37. name_obra = fields.Char(string='Obra', required=True)
  38. name_local = fields.Char(string='Local', required=True)
  39. order_date = fields.Datetime(
  40. string='Fecha de pedido de servicio', required=True,
  41. default=fields.Datetime.now
  42. )
  43. planned_start_date = fields.Datetime(
  44. string='Fecha y hora inicio de obra', required=True
  45. )
  46. planned_end_date = fields.Datetime(
  47. string='Fecha y hora fin de obra', required=True
  48. )
  49. contacto_obra = fields.Char(
  50. string='Persona de contacto de la obra', required=True
  51. )
  52. responsable = fields.Char(
  53. string='Técnico Responsable', required=True
  54. )
  55. celular_obra = fields.Char(
  56. string='Celular/Tel. de la Obra'
  57. )
  58. nro_factura = fields.Char(
  59. string='N° de factura'
  60. )
  61. distancia_obra = fields.Float(
  62. string='Distancia en km', required=True
  63. )
  64. croquis = fields.Char(
  65. string='Croquis de la obra/Ubicación', required=True
  66. )
  67. hrs_total = fields.Char(
  68. string='Hora total del trabajo', required=True
  69. )
  70. zona_obra = fields.Text(string='Zona obra de trabajo', required=True)
  71. obs_obra = fields.Text(
  72. string='Obs.'
  73. )
  74. fotos_obras = fields.Many2many(comodel_name='ir.attachment', relation='obra_attachment_rel', column1='obra_id', column2='attachment_id', string='Fotos del trabajo')
  75. horarios_dia = fields.One2many('horario.dia', 'servicio_id', 'Horarios por día')
  76. sale_order_id = fields.Many2one('sale.order', string='Orden de venta')
  77. product_ids = fields.One2many(
  78. comodel_name='servicio.producto',
  79. inverse_name='servicio_id',
  80. string='Productos a utilizar'
  81. )
  82. insumos_ids = fields.One2many(
  83. comodel_name='servicio.insumo',
  84. inverse_name='servicio_id',
  85. string='Insumos a utilizar'
  86. )
  87. logistica_ids = fields.One2many(
  88. comodel_name='servicio.logistica',
  89. inverse_name='servicio_id',
  90. string='Gastos de lógistica'
  91. )
  92. devolucion_ids = fields.One2many(
  93. comodel_name='devolucion.insumo',
  94. inverse_name='servicio_id',
  95. string='Devolución de productos e insumos'
  96. )
  97. recepcion_ids = fields.One2many(
  98. comodel_name='recepcion.fabrica',
  99. inverse_name='servicio_id',
  100. string='Recepción en fábrica'
  101. )
  102. # invoice_ids = fields.One2many('account.invoice', 'servicio_invoice_id')
  103. # invoice_ids = fields.Many2many(
  104. # comodel_name='account.invoice',
  105. # relation='orden_servicio_invoice_rel',
  106. # column1='orden_servicio_id',
  107. # column2='invoice_id',
  108. # string='Facturas relacionadas'
  109. # )
  110. invoice_ids = fields.One2many(
  111. comodel_name='account.invoice',
  112. inverse_name='servicio_invoice_id',
  113. string='Facturas'
  114. )
  115. invoice_count = fields.Integer(
  116. string='Facturas',
  117. compute='_get_invoice_count'
  118. )
  119. state = fields.Selection([
  120. ('draft', 'Pendiente'),
  121. ('in_progress', 'En progreso'),
  122. ('done', 'Realizado'),
  123. ('canceled', 'Cancelado')],
  124. string='Estado',
  125. default='draft'
  126. )
  127. @api.multi
  128. @api.depends('product_ids.subtotal')
  129. def _compute_total_producto(self):
  130. for record in self:
  131. record.total_producto = sum(record.product_ids.mapped('subtotal'))
  132. @api.multi
  133. @api.depends('insumos_ids.subtotal')
  134. def _compute_total_insumo(self):
  135. for record in self:
  136. record.total_insumo = sum(record.insumos_ids.mapped('subtotal'))
  137. @api.multi
  138. @api.depends('product_ids.subtotal')
  139. def _compute_total_logistica(self):
  140. for record in self:
  141. record.total_logistica = sum(record.logistica_ids.mapped('subtotal'))
  142. @api.multi
  143. @api.depends('insumos_ids.subtotal')
  144. def _compute_total_devolucion(self):
  145. for record in self:
  146. record.total_devolucion = sum(record.devolucion_ids.mapped('subtotal'))
  147. @api.multi
  148. @api.depends('insumos_ids.subtotal')
  149. def _compute_total_fabrica(self):
  150. for record in self:
  151. record.total_fabrica = sum(record.recepcion_ids.mapped('subtotal'))
  152. @api.depends('total_producto', 'total_insumo', 'total_logistica')
  153. def _compute_total_obra(self):
  154. for record in self:
  155. record.total_obra = record.total_producto + record.total_insumo + record.total_logistica
  156. total_producto = fields.Float(compute='_compute_total_producto', string='Total Producto', store=True, digits=(10, 0))
  157. total_insumo = fields.Float(compute='_compute_total_insumo', string='Total Insumo', store=True, digits=(10, 0))
  158. total_logistica = fields.Float(compute='_compute_total_logistica', string='Total Logística', store=True, digits=(10, 0))
  159. total_devolucion = fields.Float(compute='_compute_total_devolucion', string='Total Devolución', store=True, digits=(10, 0))
  160. total_fabrica = fields.Float(compute='_compute_total_fabrica', string='Total Recepción', store=True, digits=(10, 0))
  161. total_obra = fields.Float(compute='_compute_total_obra', string='Total Obra', store=True, digits=(10, 0))
  162. @api.model
  163. def defaults(self):
  164. res = super(OrderServicio, self).defaults()
  165. company_id = self.env['res.company']._company_default_get('order.servicio')
  166. currency_id = company_id.currency_id.id if company_id.currency_id else False
  167. res.update({
  168. 'currency_id': currency_id,
  169. 'company_id': company_id.id,
  170. })
  171. return res
  172. @api.one
  173. @api.depends('invoice_ids')
  174. def _get_invoice_count(self):
  175. self.invoice_count = len(self.invoice_ids)
  176. @api.one
  177. def button_in_progress(self):
  178. self.state = 'in_progress'
  179. @api.one
  180. def button_in_progress_back(self):
  181. self.state = 'draft'
  182. @api.one
  183. def button_done_back(self):
  184. self.state = 'in_progress'
  185. @api.one
  186. def button_done(self):
  187. self.state = 'done'
  188. @api.one
  189. def button_cancel(self):
  190. self.state = 'canceled'
  191. @api.one
  192. def onchange_partner_id(self, partner_id):
  193. _log.info('-'*100)
  194. _log.info(partner_id)
  195. @api.onchange('ubicacion_google_link')
  196. def on_ubicacion_google_link_change(self):
  197. for record in self:
  198. if record.ubicacion_google_link:
  199. # Abrir la ubicación en otra pestaña
  200. # Esto puede requerir personalización adicional dependiendo de tu requisito exacto
  201. url = record.ubicacion_google_link
  202. class ProductoServicio(models.Model):
  203. _name = 'servicio.producto'
  204. servicio_id = fields.Many2one(
  205. comodel_name='orden.servicio',
  206. string='Orden de servicio'
  207. )
  208. product_id = fields.Many2one('product.product', 'Producto', domain=[('type', '=', 'product')])
  209. categ_id = fields.Char('Categoría de producto')
  210. quantity = fields.Float(
  211. string='Cantidad',
  212. default=1,
  213. digits=(5, 3)
  214. )
  215. price_unit = fields.Float('Precio de costo')
  216. subtotal = fields.Float('Subtotal', compute='compute_subtotal', store=True, digits=(10, 0))
  217. @api.one
  218. @api.depends('quantity', 'price_unit')
  219. def compute_subtotal(self):
  220. self.subtotal = self.quantity * self.price_unit
  221. @api.onchange('product_id')
  222. def onchange_product_id(self):
  223. if self.product_id:
  224. self.description = self.product_id.name
  225. self.categ_id = self.product_id.categ_id.name
  226. self.type = 'product' if self.product_id.type == 'product' \
  227. else 'service'
  228. # @ TODO impuestos??
  229. # Obtener el precio del producto a partir de la tarifa del cliente
  230. self.price_unit = self.product_id.standard_price
  231. class ServicioInsumo(models.Model):
  232. _name = 'servicio.insumo'
  233. _description = 'Lista de Materiales proveidos'
  234. _inherit = ['mail.thread', 'ir.needaction_mixin']
  235. servicio_id = fields.Many2one(
  236. comodel_name='orden.servicio',
  237. string='Orden de servicio'
  238. )
  239. product_id = fields.Many2one('product.product', 'Producto', domain=[('type', '=', 'product')])
  240. descripcion = fields.Char(
  241. string='Descripcion',
  242. required=True
  243. )
  244. quantity = fields.Float(
  245. string='Cantidad',
  246. default=1,
  247. digits=(5, 3)
  248. )
  249. price_unit = fields.Float(
  250. string='Precio Unit.'
  251. )
  252. subtotal = fields.Float(
  253. string='Subtotal',
  254. compute='compute_subtotal',
  255. digits=(10, 0)
  256. )
  257. @api.one
  258. @api.depends('quantity', 'price_unit')
  259. def compute_subtotal(self):
  260. self.subtotal = self.quantity * self.price_unit
  261. @api.onchange('product_id')
  262. def onchange_product_id(self):
  263. if self.product_id:
  264. self.descripcion = self.product_id.name
  265. self.price_unit = self.product_id.standard_price
  266. class ServicioLogistica(models.Model):
  267. _name = 'servicio.logistica'
  268. _description = 'Gastos de logística'
  269. _inherit = ['mail.thread', 'ir.needaction_mixin']
  270. servicio_id = fields.Many2one(
  271. comodel_name='orden.servicio',
  272. string='Orden de servicio'
  273. )
  274. fecha = fields.Date(
  275. string='Fecha',
  276. required=True
  277. )
  278. product_id = fields.Many2one('product.product', 'Servicio', domain=[('type', '=', 'service')])
  279. descripcion = fields.Char(
  280. string='Descripcion',
  281. required=True
  282. )
  283. quantity = fields.Float(
  284. string='Km recorrido',
  285. default=1
  286. )
  287. price_unit = fields.Float(
  288. string='Precio Unit.'
  289. )
  290. subtotal = fields.Float(
  291. string='Subtotal',
  292. compute='compute_subtotal',
  293. digits=(10, 0)
  294. )
  295. @api.one
  296. @api.depends('quantity', 'price_unit')
  297. def compute_subtotal(self):
  298. self.subtotal = self.quantity * self.price_unit
  299. @api.onchange('product_id')
  300. def onchange_product_id(self):
  301. if self.product_id:
  302. self.descripcion = self.product_id.name
  303. self.price_unit = self.product_id.standard_price
  304. class DevolucionInsumo(models.Model):
  305. _name = 'devolucion.insumo'
  306. _description = 'Devolución de productos e insumos'
  307. _inherit = ['mail.thread', 'ir.needaction_mixin']
  308. servicio_id = fields.Many2one(
  309. comodel_name='orden.servicio',
  310. string='Orden de servicio'
  311. )
  312. product_id = fields.Many2one('product.product', 'Producto', domain=[('type', '=', 'product')])
  313. descripcion = fields.Char(
  314. string='Descripcion',
  315. required=True
  316. )
  317. quantity = fields.Float(
  318. string='Cantidad',
  319. default=1,
  320. digits=(5, 3)
  321. )
  322. price_unit = fields.Float(
  323. string='Precio Unit.'
  324. )
  325. subtotal = fields.Float(
  326. string='Subtotal',
  327. compute='compute_subtotal',
  328. digits=(10, 0)
  329. )
  330. @api.one
  331. @api.depends('quantity', 'price_unit')
  332. def compute_subtotal(self):
  333. self.subtotal = self.quantity * self.price_unit
  334. @api.onchange('product_id')
  335. def onchange_product_id(self):
  336. if self.product_id:
  337. self.descripcion = self.product_id.name
  338. self.price_unit = self.product_id.standard_price
  339. class RecepcionFabrica(models.Model):
  340. _name = 'recepcion.fabrica'
  341. _description = 'Recepción en fábrica '
  342. _inherit = ['mail.thread', 'ir.needaction_mixin']
  343. servicio_id = fields.Many2one(
  344. comodel_name='orden.servicio',
  345. string='Orden de servicio'
  346. )
  347. fecha = fields.Date(
  348. string='Fecha',
  349. required=True
  350. )
  351. product_id = fields.Many2one(
  352. comodel_name='product.product',
  353. string='Productos'
  354. )
  355. descripcion = fields.Char(
  356. string='Descripcion',
  357. required=True
  358. )
  359. quantity = fields.Float(
  360. string='Cantidad',
  361. default=1
  362. )
  363. price_unit = fields.Float(
  364. string='Precio Unit.'
  365. )
  366. subtotal = fields.Float(
  367. string='Subtotal',
  368. compute='compute_subtotal',
  369. digits=(10, 0)
  370. )
  371. employee_id = fields.Many2one(
  372. 'hr.employee',
  373. string='Empleado'
  374. )
  375. @api.one
  376. @api.depends('quantity', 'price_unit')
  377. def compute_subtotal(self):
  378. self.subtotal = self.quantity * self.price_unit
  379. @api.onchange('product_id')
  380. def onchange_product_id(self):
  381. if self.product_id:
  382. self.descripcion = self.product_id.name
  383. self.price_unit = self.product_id.standard_price
  384. class AccountInvoice(models.Model):
  385. _inherit = 'account.invoice'
  386. servicio_invoice_id = fields.Many2one(
  387. comodel_name='orden.servicio',
  388. string='Orden de Servicio'
  389. )