sis_stock_transfer.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  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. import logging
  22. _logger = logging.getLogger(__name__)
  23. class SisStockTransfer(models.Model):
  24. _name = 'sis.stock.transfer'
  25. _inherit = ['mail.thread', 'ir.needaction_mixin']
  26. @api.model
  27. def _get_default_name(self):
  28. return self.env['ir.sequence'].get('sis.stock.transfer')
  29. @api.model
  30. def _get_default_date(self):
  31. return fields.Date.context_today(self)
  32. @api.model
  33. def _get_default_state(self):
  34. return 'draft'
  35. @api.multi
  36. @api.depends('pickings.state')
  37. def _calc_transfer_state(self):
  38. for rec in self:
  39. if rec.pickings:
  40. picking_states = [p.state for p in rec.pickings]
  41. if 'done' in picking_states:
  42. rec.state = 'done'
  43. else:
  44. rec.state = 'draft'
  45. name = fields.Char(string='Referencia')
  46. date = fields.Date(string='Fecha',default=_get_default_date)
  47. partner_id = fields.Many2one('res.partner', string="Paciente")
  48. doctor_id = fields.Many2one('res.users', string="Doctor")
  49. appointment_reason = fields.Char('Motivo')
  50. source_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Origen')
  51. dest_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Destino')
  52. state = fields.Selection(
  53. selection=[
  54. ('draft', 'Borrador'),
  55. ('done', 'Hecho')],
  56. string='Status',
  57. default=_get_default_state,
  58. store=True,
  59. compute=_calc_transfer_state)
  60. lines = fields.One2many(comodel_name='sis.stock.transfer.line',inverse_name='transfer',string='Lineas de Transferencia')
  61. pickings = fields.One2many(comodel_name='stock.picking',inverse_name='transfer',string='Transferencia Relacionada')
  62. 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'))
  63. note = fields.Char('Observación')
  64. type = fields.Selection([
  65. ('transfer', 'Transferencia'),
  66. ('appointment', 'Consulta')],
  67. string='Tipo')
  68. @api.model
  69. def create(self, vals):
  70. vals['type'] = self._context.get('type')
  71. if (self._context.get('type') == 'transfer'):
  72. vals['name'] = self.env['ir.sequence'].get('sis.stock.transfer') or '/'
  73. else:
  74. vals['name'] = self.env['ir.sequence'].get('sis.stock.appointment') or '/'
  75. return super(SisStockTransfer, self).create(vals)
  76. def get_transfer_picking_type(self):
  77. self.ensure_one()
  78. picking_types = self.env['stock.picking.type'].search(
  79. [
  80. ('default_location_src_id', '=', self.source_warehouse.lot_stock_id.id),
  81. ('code', '=', 'outgoing')
  82. ]
  83. )
  84. if not picking_types:
  85. _logger.error("No picking tye found")
  86. #TODO: Exception Raise
  87. return picking_types and picking_types[0]
  88. @api.multi
  89. def get_picking_vals(self):
  90. self.ensure_one()
  91. picking_type = self.get_transfer_picking_type()
  92. picking_vals = {
  93. 'picking_type_id' : picking_type.id,
  94. 'transfer' : self.id,
  95. 'origin': self.name
  96. }
  97. return picking_vals
  98. @api.multi
  99. def action_create_picking(self):
  100. for rec in self:
  101. picking_vals = rec.get_picking_vals()
  102. _logger.debug("Picking Vals: %s", picking_vals)
  103. picking = rec.pickings.create(picking_vals)
  104. if not picking:
  105. _logger.error("Error Creating Picking")
  106. #TODO: Add Exception
  107. pc_group = rec._get_procurement_group()
  108. for line in rec.lines:
  109. move_vals = line.get_move_vals(picking, pc_group)
  110. if move_vals:
  111. _logger.debug("Move Vals: %s", move_vals)
  112. self.env['stock.move'].create(move_vals)
  113. picking.action_confirm()
  114. picking.action_assign()
  115. @api.model
  116. def _prepare_procurement_group(self):
  117. return {'name': self.name}
  118. @api.model
  119. def _get_procurement_group(self):
  120. pc_groups = self.env['procurement.group'].search(
  121. [
  122. ('name', '=', self.name)
  123. ]
  124. )
  125. if pc_groups:
  126. pc_group = pc_groups[0]
  127. else:
  128. pc_vals = self._prepare_procurement_group()
  129. pc_group = self.env['procurement.group'].create(pc_vals)
  130. return pc_group or False