account_invoice.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  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. invoice.move_id
  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.cuotas,
  100. invoice.move_id
  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. 'cuotas':j[24],
  171. 'move_id':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. 'cuotas':j[22],
  199. 'move_id':j[23],
  200. } for j in r.cr.fetchall()]
  201. def get_account_invoice_sale_type_inmobiliaria2(): #incluye facturas en borrador
  202. company_currency_rate = r.env.user.company_id.currency_id.rate
  203. validate_columns = '''
  204. SELECT EXISTS (SELECT 1 FROM information_schema.columns
  205. WHERE table_name='account_invoice' AND column_name in ('contado','credito'))'''
  206. query1 = '''
  207. SELECT
  208. invoice.id,
  209. rate.currency_id,
  210. invoice.date_invoice,
  211. invoice.type,
  212. invoice.origin,
  213. invoice.partner_id,
  214. invoice.user_id,
  215. invoice.amount_total * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  216. invoice.number,
  217. partner.name,
  218. customer.name,
  219. invoice.amount_tax * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  220. invoice.state,
  221. journal.store_id,
  222. invoice.journal_id,
  223. invoice.state,
  224. invoice.company_id,
  225. customer.ruc,
  226. invoice.supplier_invoice_number,
  227. customer.phone,
  228. customer.mobile,
  229. customer.email,
  230. invoice.cuotas,
  231. invoice.move_id
  232. FROM account_invoice AS invoice
  233. LEFT JOIN res_store_journal_rel AS journal
  234. ON journal.journal_id = invoice.journal_id
  235. LEFT JOIN res_currency_rate AS rate
  236. ON rate.currency_id = invoice.currency_id
  237. LEFT JOIN res_company AS company
  238. ON company.id = invoice.company_id
  239. LEFT JOIN res_users AS users
  240. ON users.id = invoice.user_id
  241. LEFT JOIN res_partner AS partner
  242. ON partner.id = users.partner_id
  243. LEFT JOIN res_partner AS customer
  244. ON customer.id = invoice.partner_id
  245. WHERE invoice.state NOT IN ('cancel')
  246. AND invoice.type IN ('out_invoice')
  247. GROUP BY
  248. invoice.id,
  249. rate.currency_id,
  250. invoice.date_invoice,
  251. invoice.type,
  252. invoice.origin,
  253. invoice.amount_total,
  254. invoice.partner_id,
  255. invoice.user_id,
  256. invoice.amount_total,
  257. partner.name,
  258. customer.name,
  259. invoice.amount_tax,
  260. invoice.state,
  261. journal.store_id,
  262. invoice.journal_id,
  263. invoice.state,
  264. invoice.company_id,
  265. customer.ruc,
  266. invoice.supplier_invoice_number,
  267. customer.phone,
  268. customer.mobile,
  269. customer.email
  270. '''
  271. query2 = '''
  272. SELECT
  273. invoice.id,
  274. rate.currency_id,
  275. invoice.date_invoice,
  276. invoice.type,
  277. invoice.origin,
  278. invoice.partner_id,
  279. invoice.user_id,
  280. invoice.amount_total * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  281. invoice.number,
  282. partner.name,
  283. customer.name,
  284. invoice.amount_tax * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]),
  285. invoice.state,
  286. journal.store_id,
  287. invoice.journal_id,
  288. invoice.state,
  289. invoice.company_id,
  290. customer.ruc,
  291. invoice.supplier_invoice_number,
  292. invoice.contado,
  293. invoice.credito,
  294. customer.phone,
  295. customer.mobile,
  296. customer.email,
  297. invoice.cuotas,
  298. invoice.move_id
  299. FROM account_invoice AS invoice
  300. LEFT JOIN res_store_journal_rel AS journal
  301. ON journal.journal_id = invoice.journal_id
  302. LEFT JOIN res_currency_rate AS rate
  303. ON rate.currency_id = invoice.currency_id
  304. LEFT JOIN res_company AS company
  305. ON company.id = invoice.company_id
  306. LEFT JOIN res_users AS users
  307. ON users.id = invoice.user_id
  308. LEFT JOIN res_partner AS partner
  309. ON partner.id = users.partner_id
  310. LEFT JOIN res_partner AS customer
  311. ON customer.id = invoice.partner_id
  312. WHERE invoice.state NOT IN ('cancel')
  313. AND invoice.type IN ('out_invoice')
  314. GROUP BY
  315. invoice.id,
  316. rate.currency_id,
  317. invoice.date_invoice,
  318. invoice.type,
  319. invoice.origin,
  320. invoice.amount_total,
  321. invoice.partner_id,
  322. invoice.user_id,
  323. invoice.amount_total,
  324. partner.name,
  325. customer.name,
  326. invoice.amount_tax,
  327. invoice.state,
  328. journal.store_id,
  329. invoice.journal_id,
  330. invoice.state,
  331. invoice.company_id,
  332. customer.ruc,
  333. invoice.supplier_invoice_number,
  334. customer.phone,
  335. customer.mobile,
  336. customer.email
  337. '''
  338. r.cr.execute(validate_columns)
  339. for j in r.cr.fetchall():
  340. column = j[0]
  341. if column == True:
  342. r.cr.execute(query2,(tuple([company_currency_rate,company_currency_rate])))
  343. return [{
  344. 'invoice_id': j[0],
  345. 'currency_id': j[1],
  346. 'date': j[2],
  347. 'type': j[3],
  348. 'origin': j[4],
  349. 'customer_id':j[5],
  350. 'user_id':j[6],
  351. 'amount':j[7],
  352. 'number':j[8],
  353. 'user_name':j[9],
  354. 'customer_name':j[10],
  355. 'amount_tax':j[11],
  356. 'state':j[12],
  357. 'store_id':j[13],
  358. 'journal_id':j[14],
  359. 'state':j[15],
  360. 'company_id':j[16],
  361. 'customer_ruc':j[17],
  362. 'supplier_invoice_number':j[18],
  363. 'contado':j[19],
  364. 'credito':j[20],
  365. 'phone':j[21],
  366. 'mobile':j[22],
  367. 'email':j[23],
  368. 'cuotas':j[24],
  369. 'move_id':j[25],
  370. } for j in r.cr.fetchall()]
  371. else:
  372. r.cr.execute(query1,(tuple([company_currency_rate,company_currency_rate])))
  373. return [{
  374. 'invoice_id': j[0],
  375. 'currency_id': j[1],
  376. 'date': j[2],
  377. 'type': j[3],
  378. 'origin': j[4],
  379. 'customer_id':j[5],
  380. 'user_id':j[6],
  381. 'amount':j[7],
  382. 'number':j[8],
  383. 'user_name':j[9],
  384. 'customer_name':j[10],
  385. 'amount_tax':j[11],
  386. 'state':j[12],
  387. 'store_id':j[13],
  388. 'journal_id':j[14],
  389. 'state':j[15],
  390. 'company_id':j[16],
  391. 'customer_ruc':j[17],
  392. 'supplier_invoice_number':j[18],
  393. 'phone':j[19],
  394. 'mobile':j[20],
  395. 'email':j[21],
  396. 'cuotas':j[22],
  397. 'move_id':j[23],
  398. } for j in r.cr.fetchall()]