merge_in_invoice.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 AccountInvoiceAppend(models.Model):
  25. _inherit = "account.invoice"
  26. @api.model
  27. def create(self, vals):
  28. if "invoice_line" in vals.keys():
  29. product_list = []
  30. for obj in vals['invoice_line']:
  31. if obj[2]:
  32. if "product_id" in obj[2]:
  33. if obj[2]['product_id'] not in product_list:
  34. product_list.append(obj[2]['product_id'])
  35. list_new = vals['invoice_line']
  36. new_list = []
  37. for obj in product_list:
  38. count = 0
  39. qty = 0
  40. for ele in list_new:
  41. if obj == ele[2]['product_id']:
  42. count += 1
  43. qty += ele[2]['quantity']
  44. if count == 1:
  45. new_list.append(ele)
  46. for att in new_list:
  47. if obj == att[2]['product_id']:
  48. att[2]['quantity'] = qty
  49. vals['invoice_line'] = new_list
  50. res = super(AccountInvoiceAppend, self).create(vals)
  51. return res
  52. @api.one
  53. def write(self, vals):
  54. product_list_ext = []
  55. product_list_new = []
  56. if "invoice_line" in vals.keys():
  57. new_list = vals['invoice_line']
  58. for att in new_list:
  59. if att[0] == 4:
  60. s = self.invoice_line.browse(att[1])
  61. if s.product_id.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. pro_list = []
  67. for obj in product_list_new:
  68. pro_qty = 0
  69. if obj in product_list_ext:
  70. for att in new_list:
  71. if att[0] == 4:
  72. o = self.invoice_line.browse(att[1])
  73. if o.product_id.id == obj:
  74. pro_qty += o.quantity
  75. if att[1] == 0:
  76. if att[2]['product_id'] == obj:
  77. pro_qty += 1
  78. for att1 in new_list:
  79. if att1[0] == 4:
  80. o = self.invoice_line.browse(att1[1])
  81. if o.product_id.id == obj:
  82. o.quantity = pro_qty
  83. for obj1 in product_list_new:
  84. pro_qty = 0
  85. count = 0
  86. if obj 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]['quantity']
  91. for att2 in new_list:
  92. if att2[0] == 0:
  93. if att2[2]['product_id'] == obj:
  94. count += 1
  95. if count == 1:
  96. att2[2]['quantity'] = pro_qty
  97. pro_list.append(att2)
  98. for obj2 in product_list_ext:
  99. if obj2 not in product_list_new:
  100. for att2 in new_list:
  101. if att2[0] == 4:
  102. o = self.invoice_line.browse(att2[1])
  103. if o.product_id == obj2:
  104. pro_list.append(att2)
  105. for att3 in new_list:
  106. if att3[0] == 2:
  107. pro_list.append(att3)
  108. if att3[0] == 1:
  109. o = self.invoice_line.browse(att3[1])
  110. if "quantity" in att3[2]:
  111. o.quantity = att3[2]['quantity']
  112. vals['invoice_line'] = pro_list
  113. res = super(AccountInvoiceAppend, self).write(vals)
  114. return res