product_product.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_product_product():
  4. user_store = r.env.user.store_id.id
  5. company_currency_rate = r.env.user.company_id.currency_id.rate
  6. validate_brand = '''
  7. SELECT EXISTS(
  8. SELECT table_name
  9. FROM information_schema.columns
  10. WHERE table_schema='public'
  11. AND table_name='product_brand')
  12. '''
  13. query_with_brand = '''
  14. SELECT
  15. product.id,
  16. template.categ_id,
  17. CASE
  18. WHEN product.default_code IS NOT NULL
  19. THEN ('[' || product.default_code || '] ' || product.name_template)
  20. ELSE product.name_template
  21. END AS display_name,
  22. (array_agg(attr_rel.att_id)) AS attr_rel,
  23. (array_agg(attr_value.name)) AS attr_value,
  24. (array_agg(attr.id)) AS attr,
  25. template.product_brand_id,
  26. brand.name,
  27. product.product_tmpl_id,
  28. template.list_price,
  29. product.default_code,
  30. product.active,
  31. template.sale_ok,
  32. template.company_id,
  33. template.store_id,
  34. template.type,
  35. product.ean13,
  36. (array_agg(history.cost ORDER BY history.id DESC))[1] AS standard_price
  37. FROM product_product AS product
  38. LEFT JOIN product_template AS template
  39. ON template.id = product.product_tmpl_id
  40. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  41. ON attr_rel.prod_id = product.id
  42. LEFT JOIN product_attribute_value AS attr_value
  43. ON attr_value.id = attr_rel.att_id
  44. LEFT JOIN product_attribute AS attr
  45. ON attr.id = attr_value.attribute_id
  46. LEFT JOIN product_brand AS brand
  47. ON brand.id = template.product_brand_id
  48. LEFT JOIN product_price_history AS history
  49. ON history.product_template_id = product.product_tmpl_id
  50. GROUP BY
  51. product.id,
  52. template.categ_id,
  53. product.default_code,
  54. template.product_brand_id,
  55. brand.name,
  56. template.list_price,
  57. template.sale_ok,
  58. template.company_id,
  59. template.store_id,
  60. template.type
  61. '''
  62. query_without_brand = '''
  63. SELECT
  64. product.id,
  65. template.categ_id,
  66. CASE
  67. WHEN product.default_code IS NOT NULL
  68. THEN ('[' || product.default_code || '] ' || product.name_template)
  69. ELSE product.name_template
  70. END AS display_name,
  71. (array_agg(attr_rel.att_id)) AS attr_rel,
  72. (array_agg(attr_value.name)) AS attr_value,
  73. (array_agg(attr.id)) AS attr,
  74. product.product_tmpl_id,
  75. template.list_price,
  76. product.default_code,
  77. product.active,
  78. template.sale_ok,
  79. template.company_id,
  80. template.store_id,
  81. template.type,
  82. product.ean13,
  83. (array_agg(history.cost ORDER BY history.id DESC))[1] AS standard_price
  84. FROM product_product AS product
  85. LEFT JOIN product_template AS template
  86. ON template.id = product.product_tmpl_id
  87. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  88. ON attr_rel.prod_id = product.id
  89. LEFT JOIN product_attribute_value AS attr_value
  90. ON attr_value.id = attr_rel.att_id
  91. LEFT JOIN product_attribute AS attr
  92. ON attr.id = attr_value.attribute_id
  93. LEFT JOIN product_price_history AS history
  94. ON history.product_template_id = product.product_tmpl_id
  95. GROUP BY
  96. product.id,
  97. template.categ_id,
  98. product.default_code,
  99. template.list_price,
  100. template.sale_ok,
  101. template.company_id,
  102. template.store_id,
  103. template.type
  104. '''
  105. r.cr.execute(validate_brand)
  106. for j in r.cr.fetchall():
  107. brand = j[0]
  108. if brand == True:
  109. r.cr.execute(query_with_brand,([company_currency_rate]))
  110. return [
  111. {
  112. 'product_id': j[0],
  113. 'categ_id': j[1],
  114. 'product_name': j[2],
  115. 'attribute_value_ids':j[3],
  116. 'attribute_values':j[4],
  117. 'attribute':j[5],
  118. 'product_brand_id':j[6],
  119. 'brand_name': j[7],
  120. 'product_tmpl_id': j[8],
  121. 'list_price': j[9],
  122. 'default_code': j[10],
  123. 'active': j[11],
  124. 'sale_ok': j[12],
  125. 'company_id': j[13],
  126. 'store_id': j[14],
  127. 'type': j[15],
  128. 'ean13': j[16],
  129. 'standard_price': j[17],
  130. } for j in r.cr.fetchall()
  131. ]
  132. else:
  133. r.cr.execute(query_without_brand,([company_currency_rate]))
  134. return [
  135. {
  136. 'product_id': j[0],
  137. 'categ_id': j[1],
  138. 'product_name': j[2],
  139. 'attribute_value_ids':j[3],
  140. 'attribute_values':j[4],
  141. 'attribute':j[5],
  142. 'product_tmpl_id': j[6],
  143. 'list_price': j[7],
  144. 'default_code': j[8],
  145. 'active': j[9],
  146. 'sale_ok': j[10],
  147. 'company_id': j[11],
  148. 'store_id': j[12],
  149. 'type': j[13],
  150. 'ean13': j[14],
  151. 'standard_price': j[15],
  152. } for j in r.cr.fetchall()
  153. ]