stock_transfer_order.py 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. # -*- encoding: utf-8 -*-
  2. from openerp import models, fields, api, _
  3. from openerp.exceptions import Warning
  4. import openerp.addons.decimal_precision as dp
  5. from datetime import datetime
  6. from openerp.tools import float_compare
  7. class stock_transfer_details(models.TransientModel):
  8. _name = 'stock.transfer.order.details'
  9. sale_id = fields.Many2one('sale.order', 'Pedido')
  10. item_ids = fields.One2many('stock.transfer.order.details.items', 'transfer_id', 'Items', domain=[('product_id', '!=', False)])
  11. @api.model
  12. def _get_picking_type(self):
  13. picking_type = self.env['stock.picking.type'].search([('active','=',True),('warehouse_id','=',False),('code','=','internal'),('default_location_src_id','!=','default_location_dest_id')])
  14. if picking_type:
  15. return picking_type[0].id
  16. else:
  17. raise Warning(_('No existe un tipo de transferencia adecuado para esta operación'))
  18. picking_type_id = fields.Many2one('stock.picking.type', 'Tipo de transferencia', default=_get_picking_type, required=True)
  19. picking_source_warehouse_id = fields.Many2one('stock.warehouse', string="Head source location", compute="getWarehouse", store=True)
  20. picking_destination_warehouse_id = fields.Many2one('stock.warehouse', string="Head destination location", compute="getWarehouse", store=True)
  21. @api.one
  22. @api.depends('picking_type_id')
  23. def getWarehouse(self):
  24. for rec in self:
  25. warehouse_origen = self.env['stock.warehouse'].search([('lot_stock_id','=',self.picking_type_id.default_location_src_id.id)])
  26. warehouse_dest = self.env['stock.warehouse'].search([('lot_stock_id','=',self.picking_type_id.default_location_dest_id.id)])
  27. self.picking_source_warehouse_id = warehouse_origen.id,
  28. self.picking_destination_warehouse_id = warehouse_dest.id
  29. @api.multi
  30. def wizard_view(self, transfer_details):
  31. view = self.env.ref('stock_transfer_order.wizard_transfer_details')
  32. return {
  33. 'name': _('Enter transfer details'),
  34. 'type': 'ir.actions.act_window',
  35. 'view_type': 'form',
  36. 'view_mode': 'form',
  37. 'res_model': 'stock.transfer.order.details',
  38. 'views': [(view.id, 'form')],
  39. 'view_id': view.id,
  40. 'target': 'new',
  41. 'res_id': transfer_details.id,
  42. }
  43. @api.multi
  44. def do_detailed_transfer2(self):
  45. now = datetime.now()
  46. for item in self.item_ids:
  47. if item.product_id.type == 'product':
  48. uom_record = item.product_id.uom_id
  49. qty_available = self.get_location_qty(item.product_id.id, item.sourceloc_id.id)
  50. compare_qty = float_compare(qty_available, item.quantity, precision_rounding=uom_record.rounding)
  51. if compare_qty == -1:
  52. warn_msg = _('Estas intentando transferir %.2f %s de %s pero el almacén de origen posee %.2f %s disponible!') % \
  53. (item.quantity, uom_record.name,
  54. item.product_id.name,
  55. max(0,qty_available), uom_record.name)
  56. raise Warning(_("No hay stock suficiente: "),_(warn_msg))
  57. picking_item = {
  58. 'origin': self.sale_id.name,
  59. 'move_type':'one',
  60. 'invoice_state':'none',
  61. 'date_done': now.strftime("%Y-%m-%d %H:%M:%S"),
  62. 'priority':'1',
  63. 'picking_type_id' : self.picking_type_id.id
  64. }
  65. picking_id = self.env['stock.picking'].create(picking_item)
  66. for item in self.item_ids:
  67. move = {
  68. 'name': item.name,
  69. 'picking_id': picking_id.id,
  70. 'product_id': item.product_id.id,
  71. 'product_uom_qty': item.quantity,
  72. 'product_uos_qty': item.quantity,
  73. 'price_unit': item.price_unit,
  74. 'product_uom': item.product_uom_id.id,
  75. 'picking_type_id': self.picking_type_id.id,
  76. 'location_id':item.sourceloc_id.id,
  77. 'location_dest_id':item.destinationloc_id.id,
  78. 'origin': self.sale_id.name,
  79. 'company_id': self.env.user.company_id.id,
  80. 'date': now.strftime("%Y-%m-%d %H:%M:%S"),
  81. 'date_expected': now.strftime("%Y-%m-%d %H:%M:%S"),
  82. 'invoice_state':'none',
  83. }
  84. move_id = self.env['stock.move'].create(move)
  85. move_id.action_done()
  86. sale = self.env['sale.order'].search([('id','=',self.sale_id.id)])
  87. sale.write({'state': 'manual','transfered':True})
  88. return True
  89. @api.multi
  90. def get_location_qty(self, product_id, location_id):
  91. suma = 0
  92. quant_ids = self.env['stock.quant'].search([('product_id','=', product_id),('location_id','=',location_id)])
  93. if quant_ids:
  94. for quant_id in quant_ids:
  95. suma = suma + quant_id.qty
  96. return suma
  97. class stock_transfer_details_items(models.TransientModel):
  98. _name = 'stock.transfer.order.details.items'
  99. select = fields.Boolean('Seleccionar')
  100. transfer_id = fields.Many2one('stock.transfer.order.details', 'Transferencia')
  101. product_id = fields.Many2one('product.product', 'Producto', required=True)
  102. name = fields.Char('Descripción')
  103. product_uom_id = fields.Many2one('product.uom', 'Unidad de medida')
  104. quantity = fields.Float('Cantidad', digits=dp.get_precision('Product Unit of Measure'), default = 1.0)
  105. price_unit = fields.Float('Precio Unitario')
  106. source_warehouse = fields.Many2one('stock.warehouse', 'Déposito Origen')
  107. dest_warehouse = fields.Many2one('stock.warehouse', 'Déposito Destino')
  108. sourceloc_id = fields.Many2one('stock.location', 'Ubicación Origen', related="source_warehouse.lot_stock_id", store=True)
  109. destinationloc_id = fields.Many2one('stock.location', 'Ubicación Destino',related="dest_warehouse.lot_stock_id", store=True)
  110. date = fields.Datetime('Fecha')
  111. origin = fields.Char('Pedido')
  112. owner_id = fields.Many2one('res.partner', 'Creado por:', help="Owner of the quants")
  113. @api.multi
  114. def product_id_change(self, product, uom=False):
  115. result = {}
  116. if product:
  117. prod = self.env['product.product'].browse(product)
  118. result['product_uom_id'] = prod.uom_id and prod.uom_id.id
  119. return {'value': result}
  120. class sale_order(models.Model):
  121. _inherit = 'sale.order'
  122. @api.multi
  123. def stock_transfer_action(self):
  124. for item in self:
  125. created_id = self.env['stock.transfer.order.details'].create({'sale_id': item.id or False})
  126. for each in item.order_line:
  127. line = self.env['sale.order.line'].browse(each.id)
  128. items = {
  129. 'transfer_id' : created_id.id,
  130. 'product_id' : line.product_id.id,
  131. 'name' : line.name,
  132. 'product_uom_id' : line.product_uom.id,
  133. 'quantity' : line.product_uom_qty,
  134. 'source_warehouse' : created_id.picking_source_warehouse_id.id,
  135. 'dest_warehouse' : created_id.picking_destination_warehouse_id.id,
  136. 'origin' : item.name,
  137. 'price_unit' : line.price_unit,
  138. }
  139. self.env['stock.transfer.order.details.items'].create(items)
  140. return self.env['stock.transfer.order.details'].wizard_view(created_id)
  141. class stock_warehouse(models.Model):
  142. _inherit = "stock.warehouse"
  143. @api.multi
  144. def name_get(self):
  145. if self._context is None:
  146. self._context = {}
  147. res = []
  148. if self._context.get('nombre_para_stock_transfer', False):
  149. product = self._context.get('transfer_product_id')
  150. for location in self:
  151. suma = 0
  152. quant_ids = self.env['stock.quant'].search([('product_id','=', product),('location_id','=',location.lot_stock_id.id)])
  153. if quant_ids:
  154. for quant_id in quant_ids:
  155. suma = suma + quant_id.qty
  156. res.append((location.id, ("%(location_name)s %(location_qty)s") % {
  157. 'location_name': location.name,
  158. 'location_qty': suma
  159. }))
  160. else:
  161. for record in self:
  162. res.append((record.id, ("%(location_name)s") % {
  163. 'location_name': record.name
  164. }))
  165. return res
  166. class stock_picking(models.Model):
  167. _inherit = 'stock.picking'
  168. @api.multi
  169. def entregar_producto(self):
  170. for item in self:
  171. for move in item.move_lines:
  172. if move.product_id.type == 'product' and move.location_id.usage == 'internal':
  173. uom_record = move.product_id.uom_id
  174. qty_available = self.get_location_qty(move.product_id.id, move.location_id.id)
  175. compare_qty = float_compare(qty_available, move.product_uom_qty, precision_rounding=uom_record.rounding)
  176. if compare_qty == -1:
  177. warn_msg = _('Estas intentando transferir %.2f %s de %s pero el almacén de origen posee %.2f %s disponible!') % \
  178. (move.product_uom_qty, uom_record.name,
  179. move.product_id.name,
  180. max(0,qty_available), uom_record.name)
  181. raise Warning(_("No hay stock suficiente: "),_(warn_msg))
  182. move.action_done()
  183. return True
  184. @api.multi
  185. def get_location_qty(self, product_id, location_id):
  186. suma = 0
  187. quant_ids = self.env['stock.quant'].search([('product_id','=', product_id),('location_id','=',location_id)])
  188. if quant_ids:
  189. for quant_id in quant_ids:
  190. suma = suma + quant_id.qty
  191. return suma