account_invoice.py 6.5 KB

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