merge_in_sales.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. # -*- coding: utf-8 -*-
  2. ##############################################################################
  3. #
  4. # Cybrosys Technologies Pvt. Ltd.
  5. # Copyright (C) 2009-TODAY Cybrosys Technologies(<http://www.cybrosys.com>).
  6. # Author: Niyas Raphy(<http://www.cybrosys.com>)
  7. # you can modify it under the terms of the GNU LESSER
  8. # GENERAL PUBLIC LICENSE (LGPL v3), Version 3.
  9. #
  10. # It is forbidden to publish, distribute, sublicense, or sell copies
  11. # of the Software or modified copies of the Software.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. # GNU LESSER GENERAL PUBLIC LICENSE (LGPL v3) for more details.
  17. #
  18. # You should have received a copy of the GNU LESSER GENERAL PUBLIC LICENSE
  19. # GENERAL PUBLIC LICENSE (LGPL v3) along with this program.
  20. # If not, see <http://www.gnu.org/licenses/>.
  21. #
  22. ##############################################################################
  23. from openerp import api, models
  24. class SaleOrderAppend(models.Model):
  25. _inherit = "sale.order"
  26. @api.model
  27. def create(self, vals):
  28. if "order_line" in vals.keys():
  29. product_list = []
  30. for obj in vals['order_line']:
  31. if obj[2]['product_id'] not in product_list:
  32. product_list.append(obj[2]['product_id'])
  33. list_new = vals['order_line']
  34. new_list = []
  35. for obj in product_list:
  36. count = 0
  37. qty = 0
  38. for element in list_new:
  39. if obj == element[2]['product_id']:
  40. qty += element[2]['product_uom_qty']
  41. for ele in list_new:
  42. if obj == ele[2]['product_id']:
  43. count += 1
  44. if count == 1:
  45. ele[2]['product_uom_qty'] = qty
  46. ele[2]['product_uos_qty'] = qty
  47. new_list.append(ele)
  48. vals['order_line'] = new_list
  49. res = super(SaleOrderAppend, self).create(vals)
  50. return res
  51. @api.one
  52. def write(self, vals):
  53. product_list_ext = []
  54. product_list_new = []
  55. if "order_line" in vals.keys():
  56. new_list = vals['order_line']
  57. pro_list = []
  58. for att in new_list:
  59. if att[0] == 4:
  60. s = self.order_line.browse(att[1])
  61. if s.product_id not in product_list_ext:
  62. product_list_ext.append(s.product_id.id)
  63. if att[0] == 0:
  64. if att[2]['product_id'] not in product_list_new:
  65. product_list_new.append(att[2]['product_id'])
  66. for obj in product_list_new:
  67. pro_qty = 0
  68. if obj in product_list_ext:
  69. for att in new_list:
  70. if att[0] == 4:
  71. o = self.order_line.browse(att[1])
  72. if o.product_id.id == obj:
  73. pro_qty += o.product_uom_qty
  74. if att[1] == 0:
  75. if att[2]['product_id'] == obj:
  76. pro_qty += att[2]['product_uom_qty']
  77. for att1 in new_list:
  78. if att1[0] == 4:
  79. o = self.order_line.browse(att1[1])
  80. if o.product_id.id == obj:
  81. o.product_uom_qty = pro_qty
  82. o.product_uos_qty = pro_qty
  83. for obj1 in product_list_new:
  84. pro_qty = 0
  85. count = 0
  86. if obj1 not in product_list_ext:
  87. for att1 in new_list:
  88. if att1[0] == 0:
  89. if att1[2]['product_id'] == obj1:
  90. pro_qty += att1[2]['product_uom_qty']
  91. for att2 in new_list:
  92. if att2[0] == 0:
  93. if att2[2]['product_id'] == obj1:
  94. count += 1
  95. if count == 1:
  96. att2[2]['product_uom_qty'] = pro_qty
  97. att2[2]['product_uos_qty'] = pro_qty
  98. pro_list.append(att2)
  99. for obj2 in product_list_ext:
  100. if obj2 not in product_list_new:
  101. for att2 in new_list:
  102. if att2[0] == 4:
  103. o = self.order_line.browse(att2[1])
  104. if o.product_id.id == obj2:
  105. pro_list.append(att2)
  106. for att3 in new_list:
  107. if att3[0] == 2:
  108. pro_list.append(att3)
  109. if att3[0] == 1:
  110. o = self.order_line.browse(att3[1])
  111. o.product_uom_qty = att3[2]['product_uom_qty']
  112. vals['order_line'] = pro_list
  113. res = super(SaleOrderAppend, self).write(vals)
  114. return res