account_invoice.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_account_invoice_sale_type_inmobiliaria(): #historico de venta
  4. company_currency_rate = r.env.user.company_id.currency_id.rate
  5. validate_columns = '''
  6. SELECT EXISTS (SELECT 1 FROM information_schema.columns
  7. WHERE table_name='account_invoice' AND column_name in ('contado','credito'))'''
  8. query1 = '''
  9. SELECT
  10. invoice.id,
  11. rate.currency_id,
  12. invoice.date_invoice,
  13. invoice.type,
  14. invoice.origin,
  15. invoice.partner_id,
  16. invoice.user_id,
  17. invoice.amount_total * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  18. invoice.number,
  19. partner.name,
  20. customer.name,
  21. invoice.amount_tax * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  22. invoice.state,
  23. journal.store_id,
  24. invoice.journal_id,
  25. invoice.state,
  26. invoice.company_id,
  27. customer.ruc,
  28. invoice.supplier_invoice_number,
  29. customer.phone,
  30. customer.mobile,
  31. customer.email,
  32. invoice.cuotas
  33. FROM account_invoice AS invoice
  34. LEFT JOIN res_store_journal_rel AS journal
  35. ON journal.journal_id = invoice.journal_id
  36. LEFT JOIN res_currency_rate AS rate
  37. ON rate.currency_id = invoice.currency_id
  38. LEFT JOIN res_company AS company
  39. ON company.id = invoice.company_id
  40. LEFT JOIN res_users AS users
  41. ON users.id = invoice.user_id
  42. LEFT JOIN res_partner AS partner
  43. ON partner.id = users.partner_id
  44. LEFT JOIN res_partner AS customer
  45. ON customer.id = invoice.partner_id
  46. WHERE invoice.state NOT IN ('draft', 'cancel')
  47. AND invoice.type IN ('out_invoice')
  48. GROUP BY
  49. invoice.id,
  50. rate.currency_id,
  51. invoice.date_invoice,
  52. invoice.type,
  53. invoice.origin,
  54. invoice.amount_total,
  55. invoice.partner_id,
  56. invoice.user_id,
  57. invoice.amount_total,
  58. partner.name,
  59. customer.name,
  60. invoice.amount_tax,
  61. invoice.state,
  62. journal.store_id,
  63. invoice.journal_id,
  64. invoice.state,
  65. invoice.company_id,
  66. customer.ruc,
  67. invoice.supplier_invoice_number,
  68. customer.phone,
  69. customer.mobile,
  70. customer.email
  71. '''
  72. query2 = '''
  73. SELECT
  74. invoice.id,
  75. rate.currency_id,
  76. invoice.date_invoice,
  77. invoice.type,
  78. invoice.origin,
  79. invoice.partner_id,
  80. invoice.user_id,
  81. invoice.amount_total * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  82. invoice.number,
  83. partner.name,
  84. customer.name,
  85. invoice.amount_tax * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  86. invoice.state,
  87. journal.store_id,
  88. invoice.journal_id,
  89. invoice.state,
  90. invoice.company_id,
  91. customer.ruc,
  92. invoice.supplier_invoice_number,
  93. invoice.contado,
  94. invoice.credito,
  95. customer.phone,
  96. customer.mobile,
  97. customer.email,
  98. invoice.cuotas
  99. FROM account_invoice AS invoice
  100. LEFT JOIN res_store_journal_rel AS journal
  101. ON journal.journal_id = invoice.journal_id
  102. LEFT JOIN res_currency_rate AS rate
  103. ON rate.currency_id = invoice.currency_id
  104. LEFT JOIN res_company AS company
  105. ON company.id = invoice.company_id
  106. LEFT JOIN res_users AS users
  107. ON users.id = invoice.user_id
  108. LEFT JOIN res_partner AS partner
  109. ON partner.id = users.partner_id
  110. LEFT JOIN res_partner AS customer
  111. ON customer.id = invoice.partner_id
  112. WHERE invoice.state NOT IN ('draft', 'cancel')
  113. AND invoice.type IN ('out_invoice')
  114. GROUP BY
  115. invoice.id,
  116. rate.currency_id,
  117. invoice.date_invoice,
  118. invoice.type,
  119. invoice.origin,
  120. invoice.amount_total,
  121. invoice.partner_id,
  122. invoice.user_id,
  123. invoice.amount_total,
  124. partner.name,
  125. customer.name,
  126. invoice.amount_tax,
  127. invoice.state,
  128. journal.store_id,
  129. invoice.journal_id,
  130. invoice.state,
  131. invoice.company_id,
  132. customer.ruc,
  133. invoice.supplier_invoice_number,
  134. customer.phone,
  135. customer.mobile,
  136. customer.email
  137. '''
  138. r.cr.execute(validate_columns)
  139. for j in r.cr.fetchall():
  140. column = j[0]
  141. if column == True:
  142. r.cr.execute(query2,(tuple([company_currency_rate,company_currency_rate])))
  143. return [{
  144. 'invoice_id': j[0],
  145. 'currency_id': j[1],
  146. 'date': j[2],
  147. 'type': j[3],
  148. 'origin': j[4],
  149. 'customer_id':j[5],
  150. 'user_id':j[6],
  151. 'amount':j[7],
  152. 'number':j[8],
  153. 'user_name':j[9],
  154. 'customer_name':j[10],
  155. 'amount_tax':j[11],
  156. 'state':j[12],
  157. 'store_id':j[13],
  158. 'journal_id':j[14],
  159. 'state':j[15],
  160. 'company_id':j[16],
  161. 'customer_ruc':j[17],
  162. 'supplier_invoice_number':j[18],
  163. 'contado':j[19],
  164. 'credito':j[20],
  165. 'phone':j[21],
  166. 'mobile':j[22],
  167. 'email':j[23],
  168. 'cuotas':j[24],
  169. } for j in r.cr.fetchall()]
  170. else:
  171. r.cr.execute(query1,(tuple([company_currency_rate,company_currency_rate])))
  172. return [{
  173. 'invoice_id': j[0],
  174. 'currency_id': j[1],
  175. 'date': j[2],
  176. 'type': j[3],
  177. 'origin': j[4],
  178. 'customer_id':j[5],
  179. 'user_id':j[6],
  180. 'amount':j[7],
  181. 'number':j[8],
  182. 'user_name':j[9],
  183. 'customer_name':j[10],
  184. 'amount_tax':j[11],
  185. 'state':j[12],
  186. 'store_id':j[13],
  187. 'journal_id':j[14],
  188. 'state':j[15],
  189. 'company_id':j[16],
  190. 'customer_ruc':j[17],
  191. 'supplier_invoice_number':j[18],
  192. 'phone':j[19],
  193. 'mobile':j[20],
  194. 'email':j[21],
  195. 'cuotas':j[22],
  196. } for j in r.cr.fetchall()]