import_inventory.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. from openerp import fields, models, exceptions, api, _
  2. import base64
  3. import csv
  4. import cStringIO
  5. class ImportInventory(models.TransientModel):
  6. _name = 'import.inventory'
  7. _description = 'Import inventory'
  8. def _get_default_location(self):
  9. ctx = self._context
  10. if 'active_id' in ctx:
  11. inventory_obj = self.env['stock.inventory']
  12. inventory = inventory_obj.browse(ctx['active_id'])
  13. return inventory.location_id
  14. data = fields.Binary('File', required=True)
  15. name = fields.Char('Filename')
  16. delimeter = fields.Char('Delimeter', default=',',
  17. help='Default delimeter is ","')
  18. location = fields.Many2one('stock.location', 'Default Location',
  19. default=_get_default_location, required=True)
  20. @api.one
  21. def action_import(self):
  22. """Load Inventory data from the CSV file."""
  23. ctx = self._context
  24. stloc_obj = self.env['stock.location']
  25. inventory_obj = self.env['stock.inventory']
  26. inv_imporline_obj = self.env['stock.inventory.import.line']
  27. product_obj = self.env['product.product']
  28. if 'active_id' in ctx:
  29. inventory = inventory_obj.browse(ctx['active_id'])
  30. if not self.data:
  31. raise exceptions.Warning(_("You need to select a file!"))
  32. # Decode the file data
  33. data = base64.b64decode(self.data)
  34. file_input = cStringIO.StringIO(data)
  35. file_input.seek(0)
  36. location = self.location
  37. reader_info = []
  38. if self.delimeter:
  39. delimeter = str(self.delimeter)
  40. else:
  41. delimeter = ','
  42. reader = csv.reader(file_input, delimiter=delimeter,
  43. lineterminator='\r\n')
  44. try:
  45. reader_info.extend(reader)
  46. except Exception:
  47. raise exceptions.Warning(_("Not a valid file!"))
  48. keys = reader_info[0]
  49. # check if keys exist
  50. if not isinstance(keys, list) or ('code' not in keys or
  51. 'quantity' not in keys):
  52. raise exceptions.Warning(
  53. _("Not 'code' or 'quantity' keys found"))
  54. del reader_info[0]
  55. values = {}
  56. actual_date = fields.Date.today()
  57. inv_name = self.name + ' - ' + actual_date
  58. inventory.write({'name': inv_name,
  59. 'date': fields.Datetime.now(),
  60. 'imported': True, 'state': 'confirm'})
  61. for i in range(len(reader_info)):
  62. val = {}
  63. field = reader_info[i]
  64. values = dict(zip(keys, field))
  65. prod_location = location.id
  66. if 'location' in values and values['location']:
  67. locations = stloc_obj.search([('name', '=',
  68. values['location'])])
  69. prod_location = locations[:1].id
  70. prod_lst = product_obj.search([('default_code', '=',
  71. values['code'])])
  72. if prod_lst:
  73. val['product'] = prod_lst[0].id
  74. if 'lot' in values and values['lot']:
  75. val['lot'] = values['lot']
  76. val['code'] = values['code']
  77. val['quantity'] = values['quantity']
  78. val['location_id'] = prod_location
  79. val['inventory_id'] = inventory.id
  80. val['fail'] = True
  81. val['fail_reason'] = _('No processed')
  82. inv_imporline_obj.create(val)
  83. class StockInventoryImportLine(models.Model):
  84. _name = "stock.inventory.import.line"
  85. _description = "Stock Inventory Import Line"
  86. code = fields.Char('Product Code')
  87. product = fields.Many2one('product.product', 'Found Product')
  88. quantity = fields.Float('Quantity')
  89. inventory_id = fields.Many2one('stock.inventory', 'Inventory',
  90. readonly=True)
  91. location_id = fields.Many2one('stock.location', 'Location')
  92. lot = fields.Char('Product Lot')
  93. fail = fields.Boolean('Fail')
  94. fail_reason = fields.Char('Fail Reason')