account_invoice.py 6.7 KB

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