sale_order.py 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # -*- coding: utf-8 -*-
  2. #################################################################################
  3. #
  4. # Odoo, Open Source Management Solution
  5. # Copyright (C) 2017 Ascetic Business Solution <www.asceticbs.com>
  6. #
  7. # This program is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU Affero General Public License as
  9. # published by the Free Software Foundation, either version 3 of the
  10. # License, or (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU Affero General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Affero General Public License
  18. # along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. #
  20. #################################################################################
  21. from openerp import api, fields, models, SUPERUSER_ID, _
  22. from datetime import datetime, timedelta
  23. import datetime
  24. from datetime import datetime
  25. from dateutil import relativedelta
  26. class sale_order_line(models.Model):
  27. _inherit = "sale.order.line"
  28. warehouse_quantity = fields.Char('Stock Quantity per Warehouse')
  29. #For update the onchange of product
  30. def product_id_change(self, cr, uid, ids, pricelist, product, qty=0,
  31. uom=False, qty_uos=0, uos=False, name='', partner_id=False,
  32. lang=False, update_tax=True, date_order=False, packaging=False, fiscal_position=False, flag=False, context=None):
  33. result = {}
  34. res = super(sale_order_line, self).product_id_change(cr, uid, ids, pricelist, product, qty=qty,
  35. uom=uom, qty_uos=qty_uos, uos=uos, name=name, partner_id=partner_id,
  36. lang=lang, update_tax=update_tax, date_order=date_order, packaging=packaging, fiscal_position=fiscal_position, flag=flag, context=context)
  37. if res.get('value',False):
  38. result = res['value']
  39. warehouse_quantity_text = ''
  40. quant_ids = self.pool.get('stock.quant').search(cr, SUPERUSER_ID, [('product_id','=',product),('reservation_id','=',None),('location_id.usage','=','internal')], context=context)
  41. t_warehouses = {}
  42. for quant in quant_ids:
  43. quant_obj = self.pool.get('stock.quant').browse(cr, SUPERUSER_ID, quant, context=context)
  44. if quant_obj.location_id:
  45. if quant_obj.location_id not in t_warehouses:
  46. t_warehouses.update({quant_obj.location_id:0})
  47. t_warehouses[quant_obj.location_id] += quant_obj.qty
  48. tt_warehouses = {}
  49. for location in t_warehouses:
  50. location_obj = self.pool.get('stock.location').browse(cr, SUPERUSER_ID, location.id, context=context)
  51. warehouse = False
  52. location1 = location_obj
  53. while (not warehouse and location1):
  54. warehouse_id = self.pool.get('stock.warehouse').search(cr, SUPERUSER_ID, [('lot_stock_id','=',location1.id)], context=context)
  55. warehouse_obj_id = self.pool.get('stock.warehouse').browse(cr, SUPERUSER_ID, warehouse_id, context=context)
  56. if len(warehouse_obj_id) > 0:
  57. warehouse = True
  58. else:
  59. warehouse = False
  60. location1 = location1.location_id
  61. if warehouse_obj_id:
  62. if warehouse_obj_id.name not in tt_warehouses:
  63. tt_warehouses.update({warehouse_obj_id.name:0})
  64. tt_warehouses[warehouse_obj_id.name] += t_warehouses[location]
  65. for item in tt_warehouses:
  66. if tt_warehouses[item] != 0:
  67. warehouse_quantity_text = warehouse_quantity_text + ' ** ' + item + ': ' + str(tt_warehouses[item])
  68. result['warehouse_quantity'] = warehouse_quantity_text
  69. return {'value': result}