sis_stock_transfer.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. # -*- encoding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Copyright (C) 2015 ICTSTUDIO (<http://www.ictstudio.eu>).
  5. #
  6. # This program is free software: you can redistribute it and/or modify
  7. # it under the terms of the GNU Affero General Public License as
  8. # published by the Free Software Foundation, either version 3 of the
  9. # License, or (at your option) any later version.
  10. #
  11. # This program is distributed in the hope that it will be useful,
  12. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. # GNU Affero General Public License for more details.
  15. #
  16. # You should have received a copy of the GNU Affero General Public License
  17. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. #
  19. ##############################################################################
  20. from openerp import models, fields, api, _
  21. from openerp.exceptions import Warning
  22. import logging
  23. _logger = logging.getLogger(__name__)
  24. class SisStockTransfer(models.Model):
  25. _name = 'sis.stock.transfer'
  26. _inherit = ['mail.thread', 'ir.needaction_mixin']
  27. @api.model
  28. def _get_default_name(self):
  29. return self.env['ir.sequence'].get('sis.stock.transfer')
  30. @api.model
  31. def _get_default_date(self):
  32. return fields.Date.context_today(self)
  33. @api.model
  34. def _get_default_state(self):
  35. return 'draft'
  36. @api.multi
  37. @api.depends('pickings.state')
  38. def _calc_transfer_state(self):
  39. for rec in self:
  40. if rec.pickings:
  41. picking_states = [p.state for p in rec.pickings]
  42. if 'done' in picking_states:
  43. rec.state = 'done'
  44. else:
  45. rec.state = 'draft'
  46. @api.model
  47. def _get_default_source_warehouse(self):
  48. warehouse = self.env['stock.warehouse'].search([('user_id','=',self.env.user.id)])
  49. if warehouse:
  50. return warehouse[0].id
  51. @api.model
  52. def _get_default_dest_warehouse(self):
  53. if (self._context.get('type') == 'appointment'):
  54. warehouse = self.env['stock.warehouse'].search([('name','ilike','Cliente')])
  55. if warehouse:
  56. return warehouse[0].id
  57. else:
  58. raise Warning(_("No existe un almacén asignado para clientes"))
  59. name = fields.Char(string='Referencia')
  60. date = fields.Date(string='Fecha',default=_get_default_date)
  61. partner_id = fields.Many2one('res.partner', string="Paciente")
  62. doctor_id = fields.Many2one('res.users', string="Doctor", default=lambda self: self.env.user.id)
  63. appointment_reason = fields.Char('Motivo')
  64. source_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Origen', default=_get_default_source_warehouse)
  65. dest_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Destino', default=_get_default_dest_warehouse)
  66. state = fields.Selection(
  67. selection=[
  68. ('draft', 'Borrador'),
  69. ('done', 'Hecho')],
  70. string='Estado',
  71. default=_get_default_state,
  72. store=True,
  73. compute=_calc_transfer_state)
  74. lines = fields.One2many(comodel_name='sis.stock.transfer.line',inverse_name='transfer',string='Lineas de Transferencia')
  75. pickings = fields.One2many(comodel_name='stock.picking',inverse_name='transfer',string='Transferencia Relacionada')
  76. company_id = fields.Many2one(comodel_name='res.company', string='Compania', required=True,default=lambda self: self.env['res.company']._company_default_get('sis.stock.transfer'))
  77. note = fields.Char('Observación')
  78. type = fields.Selection([
  79. ('transfer', 'Transferencia'),
  80. ('appointment', 'Consulta')],
  81. string='Tipo', store=True)
  82. @api.model
  83. def create(self, vals):
  84. vals['type'] = self._context.get('type')
  85. if (self._context.get('type') == 'transfer'):
  86. vals['name'] = self.env['ir.sequence'].get('sis.stock.transfer') or '/'
  87. else:
  88. vals['name'] = self.env['ir.sequence'].get('sis.stock.appointment') or '/'
  89. return super(SisStockTransfer, self).create(vals)
  90. @api.one
  91. @api.onchange('doctor_id')
  92. def onchange_doctor_id(self):
  93. if self.doctor_id:
  94. warehouse = self.env['stock.warehouse'].search([('user_id','=',self.doctor_id.id)])
  95. if warehouse:
  96. self.source_warehouse = warehouse.id
  97. else:
  98. raise Warning(_("El Doctor seleccionado no posee almacén asignado"))
  99. def get_transfer_picking_type(self):
  100. self.ensure_one()
  101. picking_types = self.env['stock.picking.type'].search([('default_location_src_id', '=', self.source_warehouse.lot_stock_id.id),('code', '=', 'outgoing')])
  102. if not picking_types:
  103. _logger.error("No picking tye found")
  104. #TODO: Exception Raise
  105. return picking_types and picking_types[0]
  106. @api.multi
  107. def get_picking_vals(self):
  108. self.ensure_one()
  109. picking_type = self.get_transfer_picking_type()
  110. picking_vals = {
  111. 'picking_type_id' : picking_type.id,
  112. 'transfer' : self.id,
  113. 'origin': self.name
  114. }
  115. return picking_vals
  116. @api.multi
  117. def action_create_picking(self):
  118. for rec in self:
  119. picking_vals = rec.get_picking_vals()
  120. _logger.debug("Picking Vals: %s", picking_vals)
  121. picking = rec.pickings.create(picking_vals)
  122. if not picking:
  123. _logger.error("Error Creating Picking")
  124. #TODO: Add Exception
  125. pc_group = rec._get_procurement_group()
  126. for line in rec.lines:
  127. move_vals = line.get_move_vals(picking, pc_group)
  128. if move_vals:
  129. _logger.debug("Move Vals: %s", move_vals)
  130. move_id = self.env['stock.move'].create(move_vals)
  131. move_id.action_done()
  132. @api.model
  133. def _prepare_procurement_group(self):
  134. return {'name': self.name}
  135. @api.model
  136. def _get_procurement_group(self):
  137. pc_groups = self.env['procurement.group'].search(
  138. [
  139. ('name', '=', self.name)
  140. ]
  141. )
  142. if pc_groups:
  143. pc_group = pc_groups[0]
  144. else:
  145. pc_vals = self._prepare_procurement_group()
  146. pc_group = self.env['procurement.group'].create(pc_vals)
  147. return pc_group or False