merge_in_purchase.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 PurchaseOrderAppend(models.Model):
  25. _inherit = "purchase.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 ele in list_new:
  39. if obj == ele[2]['product_id']:
  40. count += 1
  41. qty += ele[2]['product_qty']
  42. if count == 1:
  43. new_list.append(ele)
  44. for att in new_list:
  45. if obj == att[2]['product_id']:
  46. att[2]['product_qty'] = qty
  47. vals['order_line'] = new_list
  48. res = super(PurchaseOrderAppend, self).create(vals)
  49. return res
  50. @api.one
  51. def write(self, vals):
  52. product_list_ext = []
  53. product_list_new = []
  54. if "order_line" in vals.keys():
  55. new_list = vals['order_line']
  56. pro_list = []
  57. for att in new_list:
  58. if att[0] == 4:
  59. s = self.order_line.browse(att[1])
  60. if s.product_id.id not in product_list_ext:
  61. product_list_ext.append(s.product_id.id)
  62. if att[0] == 0:
  63. if att[2]['product_id'] not in product_list_new:
  64. product_list_new.append(att[2]['product_id'])
  65. for obj in product_list_new:
  66. pro_qty = 0
  67. if obj in product_list_ext:
  68. for att in new_list:
  69. if att[0] == 4:
  70. o = self.order_line.browse(att[1])
  71. if o.product_id.id == obj:
  72. pro_qty += o.product_qty
  73. if att[1] == 0:
  74. if att[2]['product_id'] == obj:
  75. pro_qty += att[2]['product_qty']
  76. for att1 in new_list:
  77. if att1[0] == 4:
  78. o = self.order_line.browse(att1[1])
  79. if o.product_id.id == obj:
  80. o.product_qty = pro_qty
  81. for obj1 in product_list_new:
  82. pro_qty = 0
  83. count = 0
  84. if obj not in product_list_ext:
  85. for att1 in new_list:
  86. if att1[0] == 0:
  87. if att1[2]['product_id'] == obj1:
  88. pro_qty += 1
  89. for att2 in new_list:
  90. if att2[0] == 0:
  91. if att2[2]['product_id'] == obj:
  92. count += 1
  93. if count == 1:
  94. att2[2]['product_qty'] = pro_qty
  95. pro_list.append(att2)
  96. for obj2 in product_list_ext:
  97. if obj2 not in product_list_new:
  98. for att2 in new_list:
  99. if att2[0] == 4:
  100. o = self.order_line.browse(att2[1])
  101. if o.product_id == obj2:
  102. pro_list.append(att2)
  103. for att3 in new_list:
  104. if att3[0] == 2:
  105. pro_list.append(att3)
  106. if att3[0] == 1:
  107. o = self.order_line.browse(att3[1])
  108. o.product_qty = att3[2]['product_qty']
  109. vals['order_line'] = pro_list
  110. res = super(PurchaseOrderAppend, self).write(vals)
  111. return res