stock_inventory.py 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # -*- coding: utf-8 -*-
  2. # (c) 2015 Oihane Crucelaegui - AvanzOSC
  3. # License AGPL-3 - See http://www.gnu.org/licenses/agpl-3.0.html
  4. from openerp import models, fields, api, exceptions, _
  5. class StockInventory(models.Model):
  6. _inherit = "stock.inventory"
  7. @api.model
  8. def _get_available_filters(self):
  9. res = super(StockInventory, self)._get_available_filters()
  10. res.append(('file', _('By File')))
  11. return res
  12. @api.multi
  13. @api.depends('import_lines')
  14. def _file_lines_processed(self):
  15. for record in self:
  16. processed = True
  17. if record.import_lines:
  18. processed = any((not line.fail or
  19. (line.fail and
  20. line.fail_reason != _('No processed')))
  21. for line in record.import_lines)
  22. record.processed = processed
  23. imported = fields.Boolean('Imported')
  24. import_lines = fields.One2many('stock.inventory.import.line',
  25. 'inventory_id', string='Imported Lines')
  26. filter = fields.Selection(_get_available_filters,
  27. string='Selection Filter',
  28. required=True)
  29. processed = fields.Boolean(string='Has been processed at least once?',
  30. compute='_file_lines_processed')
  31. @api.multi
  32. def process_import_lines(self):
  33. """Process Inventory Load lines."""
  34. import_lines = self.mapped('import_lines')
  35. if not import_lines:
  36. raise exceptions.Warning(_("There must be one line at least to "
  37. "process"))
  38. inventory_line_obj = self.env['stock.inventory.line']
  39. stk_lot_obj = self.env['stock.production.lot']
  40. product_obj = self.env['product.product']
  41. for line in import_lines:
  42. if line.fail:
  43. if not line.product:
  44. prod_lst = product_obj.search([('default_code', '=',
  45. line.code)])
  46. if prod_lst:
  47. product = prod_lst[0]
  48. else:
  49. line.fail_reason = _('No product code found')
  50. continue
  51. else:
  52. product = line.product
  53. lot_id = None
  54. if line.lot:
  55. lot_lst = stk_lot_obj.search([('name', '=', line.lot)])
  56. if lot_lst:
  57. lot_id = lot_lst[0].id
  58. else:
  59. lot = stk_lot_obj.create({'name': line.lot,
  60. 'product_id': product.id})
  61. lot_id = lot.id
  62. inventory_line_obj.create({
  63. 'product_id': product.id,
  64. 'product_uom_id': product.uom_id.id,
  65. 'product_qty': line.quantity,
  66. 'inventory_id': line.inventory_id.id,
  67. 'location_id': line.location_id.id,
  68. 'prod_lot_id': lot_id})
  69. line.write({'fail': False, 'fail_reason': _('Processed')})
  70. return True
  71. @api.multi
  72. def action_done(self):
  73. for inventory in self:
  74. if not inventory.processed:
  75. raise exceptions.Warning(
  76. _("Loaded lines must be processed at least one time for "
  77. "inventory : %s") % (inventory.name))
  78. return super(StockInventory, self).action_done()