123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170 |
- # -*- encoding: utf-8 -*-
- ##############################################################################
- #
- # Copyright (C) 2015 ICTSTUDIO (<http://www.ictstudio.eu>).
- #
- # This program is free software: you can redistribute it and/or modify
- # it under the terms of the GNU Affero General Public License as
- # published by the Free Software Foundation, either version 3 of the
- # License, or (at your option) any later version.
- #
- # This program is distributed in the hope that it will be useful,
- # but WITHOUT ANY WARRANTY; without even the implied warranty of
- # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- # GNU Affero General Public License for more details.
- #
- # You should have received a copy of the GNU Affero General Public License
- # along with this program. If not, see <http://www.gnu.org/licenses/>.
- #
- ##############################################################################
- from openerp import models, fields, api, _
- from openerp.exceptions import Warning
- import logging
- _logger = logging.getLogger(__name__)
- class SisStockTransfer(models.Model):
- _name = 'sis.stock.transfer'
- _inherit = ['mail.thread', 'ir.needaction_mixin']
- @api.model
- def _get_default_name(self):
- return self.env['ir.sequence'].get('sis.stock.transfer')
- @api.model
- def _get_default_date(self):
- return fields.Date.context_today(self)
- @api.model
- def _get_default_state(self):
- return 'draft'
- @api.multi
- @api.depends('pickings.state')
- def _calc_transfer_state(self):
- for rec in self:
- if rec.pickings:
- picking_states = [p.state for p in rec.pickings]
- if 'done' in picking_states:
- rec.state = 'done'
- else:
- rec.state = 'draft'
- @api.model
- def _get_default_source_warehouse(self):
- warehouse = self.env['stock.warehouse'].search([('user_id','=',self.env.user.id)])
- if warehouse:
- return warehouse[0].id
- @api.model
- def _get_default_dest_warehouse(self):
- if (self._context.get('type') == 'appointment'):
- warehouse = self.env['stock.warehouse'].search([('name','ilike','Cliente')])
- if warehouse:
- return warehouse[0].id
- else:
- raise Warning(_("No existe un almacén asignado para clientes"))
- name = fields.Char(string='Referencia')
- date = fields.Date(string='Fecha',default=_get_default_date)
- partner_id = fields.Many2one('res.partner', string="Paciente")
- doctor_id = fields.Many2one('res.users', string="Doctor", default=lambda self: self.env.user.id)
- appointment_reason = fields.Char('Motivo')
- source_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Origen', default=_get_default_source_warehouse)
- dest_warehouse = fields.Many2one(comodel_name='stock.warehouse',string='Déposito de Destino', default=_get_default_dest_warehouse)
- state = fields.Selection(
- selection=[
- ('draft', 'Borrador'),
- ('done', 'Hecho')],
- string='Estado',
- default=_get_default_state,
- store=True,
- compute=_calc_transfer_state)
- lines = fields.One2many(comodel_name='sis.stock.transfer.line',inverse_name='transfer',string='Lineas de Transferencia')
- pickings = fields.One2many(comodel_name='stock.picking',inverse_name='transfer',string='Transferencia Relacionada')
- 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'))
- note = fields.Char('Observación')
- type = fields.Selection([
- ('transfer', 'Transferencia'),
- ('appointment', 'Consulta')],
- string='Tipo', store=True)
- @api.model
- def create(self, vals):
- vals['type'] = self._context.get('type')
- if (self._context.get('type') == 'transfer'):
- vals['name'] = self.env['ir.sequence'].get('sis.stock.transfer') or '/'
- else:
- vals['name'] = self.env['ir.sequence'].get('sis.stock.appointment') or '/'
- return super(SisStockTransfer, self).create(vals)
- @api.one
- @api.onchange('doctor_id')
- def onchange_doctor_id(self):
- if self.doctor_id:
- warehouse = self.env['stock.warehouse'].search([('user_id','=',self.doctor_id.id)])
- if warehouse:
- self.source_warehouse = warehouse.id
- else:
- raise Warning(_("El Doctor seleccionado no posee almacén asignado"))
- def get_transfer_picking_type(self):
- self.ensure_one()
- picking_types = self.env['stock.picking.type'].search([('default_location_src_id', '=', self.source_warehouse.lot_stock_id.id),('code', '=', 'outgoing')])
- if not picking_types:
- _logger.error("No picking tye found")
- #TODO: Exception Raise
- return picking_types and picking_types[0]
- @api.multi
- def get_picking_vals(self):
- self.ensure_one()
- picking_type = self.get_transfer_picking_type()
- picking_vals = {
- 'picking_type_id' : picking_type.id,
- 'transfer' : self.id,
- 'origin': self.name
- }
- return picking_vals
- @api.multi
- def action_create_picking(self):
- for rec in self:
- picking_vals = rec.get_picking_vals()
- _logger.debug("Picking Vals: %s", picking_vals)
- picking = rec.pickings.create(picking_vals)
- if not picking:
- _logger.error("Error Creating Picking")
- #TODO: Add Exception
- pc_group = rec._get_procurement_group()
- for line in rec.lines:
- move_vals = line.get_move_vals(picking, pc_group)
- if move_vals:
- _logger.debug("Move Vals: %s", move_vals)
- move_id = self.env['stock.move'].create(move_vals)
- move_id.action_done()
- @api.model
- def _prepare_procurement_group(self):
- return {'name': self.name}
- @api.model
- def _get_procurement_group(self):
- pc_groups = self.env['procurement.group'].search(
- [
- ('name', '=', self.name)
- ]
- )
- if pc_groups:
- pc_group = pc_groups[0]
- else:
- pc_vals = self._prepare_procurement_group()
- pc_group = self.env['procurement.group'].create(pc_vals)
- return pc_group or False
|