account_invoice_line.py 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005
  1. # -*- coding: utf-8 -*-
  2. from openerp.http import request as r
  3. def get_account_invoice_line_all_type():
  4. user_store = r.env.user.store_id.id
  5. company_currency_rate = r.env.user.company_id.currency_id.rate
  6. query = '''
  7. SELECT
  8. invoice.id AS invoice_id,
  9. line.id AS invoice_line_id,
  10. invoice.number,
  11. invoice.origin,
  12. invoice.date_invoice,
  13. invoice.type,
  14. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  15. line.quantity AS cant,
  16. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  17. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  18. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  19. --line.name,
  20. journal.store_id,
  21. template.categ_id
  22. FROM account_invoice AS invoice
  23. LEFT JOIN account_invoice_line AS line
  24. ON line.invoice_id = invoice.id
  25. LEFT JOIN product_product AS product
  26. ON line.product_id = product.id
  27. LEFT JOIN product_template AS template
  28. ON template.id = product.product_tmpl_id
  29. LEFT JOIN res_store_journal_rel AS journal
  30. ON journal.journal_id = invoice.journal_id
  31. LEFT JOIN product_price_history AS history
  32. ON history.product_template_id = product.product_tmpl_id
  33. LEFT JOIN res_currency_rate AS rate
  34. ON rate.currency_id = invoice.currency_id
  35. WHERE invoice.state NOT IN ('draft', 'cancel')
  36. GROUP BY
  37. invoice.id,
  38. line.id,
  39. invoice.number,
  40. invoice.origin,
  41. invoice.date_invoice,
  42. product.name_template,
  43. line.price_unit,
  44. line.quantity,
  45. line.price_subtotal,
  46. journal.store_id,
  47. template.categ_id
  48. '''
  49. r.cr.execute(query,(tuple([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])))
  50. return [
  51. {
  52. 'invoice_id': j[0],
  53. 'invoice_line_id': j[1],
  54. 'number': j[2],
  55. 'origin': j[3],
  56. 'date': j[4],
  57. 'type': j[5],
  58. 'product_name':j[6],
  59. 'price_unit':j[7],
  60. 'quantity':j[8],
  61. 'subtotal':j[9],
  62. 'tax': j[10],
  63. 'cost': j[11],
  64. 'store_id': j[12],
  65. 'categ_id': j[13],
  66. } for j in r.cr.fetchall()
  67. ]
  68. def get_account_invoice_line_out_invoice(): #analisis de venta
  69. user_store = r.env.user.store_id.id
  70. company_currency_rate = r.env.user.company_id.currency_id.rate
  71. validate_brand = '''
  72. SELECT EXISTS(
  73. SELECT table_name
  74. FROM information_schema.columns
  75. WHERE table_schema='public'
  76. AND table_name='product_brand')
  77. '''
  78. query_with_brand = '''
  79. SELECT
  80. invoice.id AS invoice_id,
  81. line.id AS invoice_line_id,
  82. invoice.number,
  83. invoice.origin,
  84. invoice.date_invoice,
  85. invoice.type,
  86. CASE
  87. WHEN product.default_code IS NOT NULL
  88. THEN ('[' || product.default_code || '] ' || product.name_template)
  89. ELSE product.name_template
  90. END AS display_name,
  91. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  92. line.quantity AS cant,
  93. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  94. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (((line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) * line.discount)/100))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  95. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  96. journal.store_id,
  97. template.categ_id,
  98. partner.ruc,
  99. partner.name,
  100. product.id,
  101. invoice.company_id,
  102. invoice.journal_id,
  103. (array_agg(attr_rel.att_id)) AS attr_rel,
  104. (array_agg(attr_value.name)) AS attr_value,
  105. (array_agg(attr.id)) AS attr,
  106. template.product_brand_id,
  107. brand.name,
  108. line.discount,
  109. line.name
  110. FROM account_invoice AS invoice
  111. LEFT JOIN account_invoice_line AS line
  112. ON line.invoice_id = invoice.id
  113. --Product
  114. LEFT JOIN product_product AS product
  115. ON line.product_id = product.id
  116. LEFT JOIN product_template AS template
  117. ON template.id = product.product_tmpl_id
  118. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  119. ON attr_rel.prod_id = product.id
  120. LEFT JOIN product_attribute_value AS attr_value
  121. ON attr_value.id = attr_rel.att_id
  122. LEFT JOIN product_attribute AS attr
  123. ON attr.id = attr_value.attribute_id
  124. LEFT JOIN res_store_journal_rel AS journal
  125. ON journal.journal_id = invoice.journal_id
  126. LEFT JOIN product_price_history AS history
  127. ON history.product_template_id = product.product_tmpl_id
  128. LEFT JOIN res_currency_rate AS rate
  129. ON rate.currency_id = invoice.currency_id
  130. LEFT JOIN res_partner AS partner
  131. ON partner.id = invoice.partner_id
  132. LEFT JOIN product_brand AS brand
  133. ON brand.id = template.product_brand_id
  134. WHERE invoice.state NOT IN ('draft', 'cancel')
  135. AND invoice.type = 'out_invoice'
  136. GROUP BY
  137. invoice.id,
  138. line.id,
  139. invoice.number,
  140. invoice.origin,
  141. invoice.date_invoice,
  142. product.name_template,
  143. line.price_unit,
  144. line.quantity,
  145. line.price_subtotal,
  146. journal.store_id,
  147. template.categ_id,
  148. product.default_code,
  149. partner.ruc,
  150. partner.name,
  151. product.id,
  152. invoice.company_id,
  153. invoice.journal_id,
  154. template.product_brand_id,
  155. brand.name,
  156. line.name
  157. '''
  158. query_without_brand = '''
  159. SELECT
  160. invoice.id AS invoice_id,
  161. line.id AS invoice_line_id,
  162. invoice.number,
  163. invoice.origin,
  164. invoice.date_invoice,
  165. invoice.type,
  166. CASE
  167. WHEN product.default_code IS NOT NULL
  168. THEN ('[' || product.default_code || '] ' || product.name_template)
  169. ELSE product.name_template
  170. END AS display_name,
  171. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  172. line.quantity AS cant,
  173. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  174. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (((line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) * line.discount)/100))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  175. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  176. journal.store_id,
  177. template.categ_id,
  178. partner.ruc,
  179. partner.name,
  180. product.id,
  181. invoice.company_id,
  182. invoice.journal_id,
  183. (array_agg(attr_rel.att_id)) AS attr_rel,
  184. (array_agg(attr_value.name)) AS attr_value,
  185. (array_agg(attr.id)) AS attr,
  186. line.discount,
  187. line.name
  188. FROM account_invoice AS invoice
  189. LEFT JOIN account_invoice_line AS line
  190. ON line.invoice_id = invoice.id
  191. --Product
  192. LEFT JOIN product_product AS product
  193. ON line.product_id = product.id
  194. LEFT JOIN product_template AS template
  195. ON template.id = product.product_tmpl_id
  196. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  197. ON attr_rel.prod_id = product.id
  198. LEFT JOIN product_attribute_value AS attr_value
  199. ON attr_value.id = attr_rel.att_id
  200. LEFT JOIN product_attribute AS attr
  201. ON attr.id = attr_value.attribute_id
  202. LEFT JOIN res_store_journal_rel AS journal
  203. ON journal.journal_id = invoice.journal_id
  204. LEFT JOIN product_price_history AS history
  205. ON history.product_template_id = product.product_tmpl_id
  206. LEFT JOIN res_currency_rate AS rate
  207. ON rate.currency_id = invoice.currency_id
  208. LEFT JOIN res_partner AS partner
  209. ON partner.id = invoice.partner_id
  210. WHERE invoice.state NOT IN ('draft', 'cancel')
  211. AND invoice.type = 'out_invoice'
  212. GROUP BY
  213. invoice.id,
  214. line.id,
  215. invoice.number,
  216. invoice.origin,
  217. invoice.date_invoice,
  218. product.name_template,
  219. line.price_unit,
  220. line.quantity,
  221. line.price_subtotal,
  222. journal.store_id,
  223. template.categ_id,
  224. product.default_code,
  225. partner.ruc,
  226. partner.name,
  227. product.id,
  228. invoice.company_id,
  229. invoice.journal_id,
  230. line.name
  231. '''
  232. r.cr.execute(validate_brand)
  233. for j in r.cr.fetchall():
  234. brand = j[0]
  235. if brand == True:
  236. r.cr.execute(query_with_brand,[company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])
  237. return [
  238. {
  239. 'invoice_id': j[0],
  240. 'invoice_line_id': j[1],
  241. 'number': j[2],
  242. 'origin': j[3],
  243. 'date': j[4],
  244. 'type': j[5],
  245. 'product_name':j[6],
  246. 'price_unit':j[7],
  247. 'quantity':j[8],
  248. 'subtotal':j[9],
  249. 'tax': j[10],
  250. 'cost': j[11],
  251. 'store_id': j[12],
  252. 'categ_id': j[13],
  253. 'partner_name': j[14],
  254. 'partner_ruc': j[15],
  255. 'product_id': j[16],
  256. 'company_id': j[17],
  257. 'journal_id': j[18],
  258. 'attribute_value_ids': j[19],
  259. 'attribute_values': j[20],
  260. 'attribute_ids': j[21],
  261. 'product_brand_id': j[22],
  262. 'brand_name': j[23],
  263. 'discount': j[24],
  264. 'name': j[25],
  265. } for j in r.cr.fetchall()
  266. ]
  267. else:
  268. r.cr.execute(query_without_brand,[company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])
  269. return [
  270. {
  271. 'invoice_id': j[0],
  272. 'invoice_line_id': j[1],
  273. 'number': j[2],
  274. 'origin': j[3],
  275. 'date': j[4],
  276. 'type': j[5],
  277. 'product_name':j[6],
  278. 'price_unit':j[7],
  279. 'quantity':j[8],
  280. 'subtotal':j[9],
  281. 'tax': j[10],
  282. 'cost': j[11],
  283. 'store_id': j[12],
  284. 'categ_id': j[13],
  285. 'partner_ruc': j[14],
  286. 'partner_name': j[15],
  287. 'product_id': j[16],
  288. 'company_id': j[17],
  289. 'journal_id': j[18],
  290. 'attribute_value_ids': j[19],
  291. 'attribute_values': j[20],
  292. 'attribute_ids': j[21],
  293. 'discount': j[22],
  294. 'name': j[23],
  295. } for j in r.cr.fetchall()
  296. ]
  297. def get_account_invoice_line_out_invoice_and_out_refund():
  298. user_store = r.env.user.store_id.id
  299. company_currency_rate = r.env.user.company_id.currency_id.rate
  300. validate_brand = '''
  301. SELECT EXISTS(
  302. SELECT table_name
  303. FROM information_schema.columns
  304. WHERE table_schema='public'
  305. AND table_name='product_brand')
  306. '''
  307. query_with_brand = '''
  308. SELECT
  309. invoice.id AS invoice_id,
  310. line.id AS invoice_line_id,
  311. invoice.number,
  312. invoice.origin,
  313. invoice.date_invoice,
  314. invoice.type,
  315. CASE
  316. WHEN product.default_code IS NOT NULL
  317. THEN ('[' || product.default_code || '] ' || product.name_template)
  318. ELSE product.name_template
  319. END AS display_name,
  320. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  321. line.quantity AS cant,
  322. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  323. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  324. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  325. journal.store_id,
  326. template.categ_id,
  327. partner.name,
  328. product.id,
  329. invoice.company_id,
  330. invoice.journal_id,
  331. (array_agg(attr_rel.att_id)) AS attr_rel,
  332. (array_agg(attr_value.name)) AS attr_value,
  333. (array_agg(attr.id)) AS attr,
  334. template.product_brand_id,
  335. brand.name,
  336. partner.id
  337. FROM account_invoice AS invoice
  338. LEFT JOIN account_invoice_line AS line
  339. ON line.invoice_id = invoice.id
  340. --Product
  341. LEFT JOIN product_product AS product
  342. ON line.product_id = product.id
  343. LEFT JOIN product_template AS template
  344. ON template.id = product.product_tmpl_id
  345. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  346. ON attr_rel.prod_id = product.id
  347. LEFT JOIN product_attribute_value AS attr_value
  348. ON attr_value.id = attr_rel.att_id
  349. LEFT JOIN product_attribute AS attr
  350. ON attr.id = attr_value.attribute_id
  351. LEFT JOIN res_store_journal_rel AS journal
  352. ON journal.journal_id = invoice.journal_id
  353. LEFT JOIN product_price_history AS history
  354. ON history.product_template_id = product.product_tmpl_id
  355. LEFT JOIN res_currency_rate AS rate
  356. ON rate.currency_id = invoice.currency_id
  357. LEFT JOIN res_partner AS partner
  358. ON partner.id = invoice.partner_id
  359. LEFT JOIN product_brand AS brand
  360. ON brand.id = template.product_brand_id
  361. WHERE invoice.state NOT IN ('draft', 'cancel')
  362. AND invoice.type in ('out_invoice','out_refund')
  363. GROUP BY
  364. invoice.id,
  365. line.id,
  366. invoice.number,
  367. invoice.origin,
  368. invoice.date_invoice,
  369. product.name_template,
  370. line.price_unit,
  371. line.quantity,
  372. line.price_subtotal,
  373. journal.store_id,
  374. template.categ_id,
  375. product.default_code,
  376. partner.name,
  377. product.id,
  378. invoice.company_id,
  379. invoice.journal_id,
  380. template.product_brand_id,
  381. brand.name,
  382. partner.id
  383. '''
  384. query_without_brand = '''
  385. SELECT
  386. invoice.id AS invoice_id,
  387. line.id AS invoice_line_id,
  388. invoice.number,
  389. invoice.origin,
  390. invoice.date_invoice,
  391. invoice.type,
  392. CASE
  393. WHEN product.default_code IS NOT NULL
  394. THEN ('[' || product.default_code || '] ' || product.name_template)
  395. ELSE product.name_template
  396. END AS display_name,
  397. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  398. line.quantity AS cant,
  399. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  400. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  401. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  402. --line.name
  403. journal.store_id,
  404. template.categ_id,
  405. partner.name,
  406. product.id,
  407. invoice.company_id,
  408. invoice.journal_id,
  409. (array_agg(attr_rel.att_id)) AS attr_rel,
  410. (array_agg(attr_value.name)) AS attr_value,
  411. (array_agg(attr.id)) AS attr,
  412. partner.id
  413. FROM account_invoice AS invoice
  414. LEFT JOIN account_invoice_line AS line
  415. ON line.invoice_id = invoice.id
  416. --Product
  417. LEFT JOIN product_product AS product
  418. ON line.product_id = product.id
  419. LEFT JOIN product_template AS template
  420. ON template.id = product.product_tmpl_id
  421. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  422. ON attr_rel.prod_id = product.id
  423. LEFT JOIN product_attribute_value AS attr_value
  424. ON attr_value.id = attr_rel.att_id
  425. LEFT JOIN product_attribute AS attr
  426. ON attr.id = attr_value.attribute_id
  427. LEFT JOIN res_store_journal_rel AS journal
  428. ON journal.journal_id = invoice.journal_id
  429. LEFT JOIN product_price_history AS history
  430. ON history.product_template_id = product.product_tmpl_id
  431. LEFT JOIN res_currency_rate AS rate
  432. ON rate.currency_id = invoice.currency_id
  433. LEFT JOIN res_partner AS partner
  434. ON partner.id = invoice.partner_id
  435. WHERE invoice.state NOT IN ('draft', 'cancel')
  436. AND invoice.type = 'out_invoice'
  437. GROUP BY
  438. invoice.id,
  439. line.id,
  440. invoice.number,
  441. invoice.origin,
  442. invoice.date_invoice,
  443. product.name_template,
  444. line.price_unit,
  445. line.quantity,
  446. line.price_subtotal,
  447. journal.store_id,
  448. template.categ_id,
  449. product.default_code,
  450. partner.name,
  451. product.id,
  452. invoice.company_id,
  453. invoice.journal_id,
  454. partner.id
  455. '''
  456. r.cr.execute(validate_brand)
  457. for j in r.cr.fetchall():
  458. brand = j[0]
  459. if brand == True:
  460. r.cr.execute(query_with_brand,(tuple([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])))
  461. return [
  462. {
  463. 'invoice_id': j[0],
  464. 'invoice_line_id': j[1],
  465. 'number': j[2],
  466. 'origin': j[3],
  467. 'date': j[4],
  468. 'type': j[5],
  469. 'product_name':j[6],
  470. 'price_unit':j[7],
  471. 'quantity':j[8],
  472. 'subtotal':j[9],
  473. 'tax': j[10],
  474. 'cost': j[11],
  475. 'store_id': j[12],
  476. 'categ_id': j[13],
  477. 'partner_name': j[14],
  478. 'product_id': j[15],
  479. 'company_id': j[16],
  480. 'journal_id': j[17],
  481. 'attribute_value_ids': j[18],
  482. 'attribute_values': j[19],
  483. 'attribute_ids': j[20],
  484. 'product_brand_id': j[21],
  485. 'brand_name': j[22],
  486. 'partner_id': j[23],
  487. } for j in r.cr.fetchall()
  488. ]
  489. else:
  490. r.cr.execute(query_without_brand,(tuple([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate])))
  491. return [
  492. {
  493. 'invoice_id': j[0],
  494. 'invoice_line_id': j[1],
  495. 'number': j[2],
  496. 'origin': j[3],
  497. 'date': j[4],
  498. 'type': j[5],
  499. 'product_name':j[6],
  500. 'price_unit':j[7],
  501. 'quantity':j[8],
  502. 'subtotal':j[9],
  503. 'tax': j[10],
  504. 'cost': j[11],
  505. 'store_id': j[12],
  506. 'categ_id': j[13],
  507. 'partner_name': j[14],
  508. 'product_id': j[15],
  509. 'company_id': j[16],
  510. 'journal_id': j[17],
  511. 'attribute_value_ids': j[18],
  512. 'attribute_values': j[19],
  513. 'attribute_ids': j[20],
  514. 'partner_id': j[21],
  515. } for j in r.cr.fetchall()
  516. ]
  517. def get_account_invoice_line_in_invoice_purchase():
  518. user_store = r.env.user.store_id.id
  519. company_currency_rate = r.env.user.company_id.currency_id.rate
  520. origin = '%PO%'
  521. validate_brand = '''
  522. SELECT EXISTS(
  523. SELECT table_name
  524. FROM information_schema.columns
  525. WHERE table_schema='public'
  526. AND table_name='product_brand')
  527. '''
  528. query_with_brand = '''
  529. SELECT
  530. invoice.id AS invoice_id,
  531. line.id AS invoice_line_id,
  532. invoice.number,
  533. invoice.origin,
  534. invoice.supplier_invoice_number,
  535. invoice.date_invoice,
  536. invoice.type,
  537. CASE
  538. WHEN product.default_code IS NOT NULL
  539. THEN ('[' || product.default_code || '] ' || product.name_template)
  540. ELSE product.name_template
  541. END AS display_name,
  542. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  543. line.quantity AS cant,
  544. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  545. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  546. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  547. journal.store_id,
  548. template.categ_id,
  549. partner.ruc,
  550. partner.name,
  551. product.id,
  552. invoice.company_id,
  553. invoice.journal_id,
  554. (array_agg(attr_rel.att_id)) AS attr_rel,
  555. (array_agg(attr_value.name)) AS attr_value,
  556. (array_agg(attr.id)) AS attr,
  557. template.product_brand_id,
  558. brand.name,
  559. line.name
  560. FROM account_invoice AS invoice
  561. LEFT JOIN account_invoice_line AS line
  562. ON line.invoice_id = invoice.id
  563. --Product
  564. LEFT JOIN product_product AS product
  565. ON line.product_id = product.id
  566. LEFT JOIN product_template AS template
  567. ON template.id = product.product_tmpl_id
  568. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  569. ON attr_rel.prod_id = product.id
  570. LEFT JOIN product_attribute_value AS attr_value
  571. ON attr_value.id = attr_rel.att_id
  572. LEFT JOIN product_attribute AS attr
  573. ON attr.id = attr_value.attribute_id
  574. LEFT JOIN res_store_journal_rel AS journal
  575. ON journal.journal_id = invoice.journal_id
  576. LEFT JOIN product_price_history AS history
  577. ON history.product_template_id = product.product_tmpl_id
  578. LEFT JOIN res_currency_rate AS rate
  579. ON rate.currency_id = invoice.currency_id
  580. LEFT JOIN res_partner AS partner
  581. ON partner.id = invoice.partner_id
  582. LEFT JOIN product_brand AS brand
  583. ON brand.id = template.product_brand_id
  584. WHERE invoice.state NOT IN ('draft', 'cancel')
  585. AND invoice.type = 'in_invoice'
  586. AND invoice.origin LIKE %s
  587. GROUP BY
  588. invoice.id,
  589. line.id,
  590. invoice.number,
  591. invoice.origin,
  592. invoice.supplier_invoice_number,
  593. invoice.date_invoice,
  594. product.name_template,
  595. line.price_unit,
  596. line.quantity,
  597. line.price_subtotal,
  598. journal.store_id,
  599. template.categ_id,
  600. product.default_code,
  601. partner.ruc,
  602. partner.name,
  603. product.id,
  604. invoice.company_id,
  605. invoice.journal_id,
  606. template.product_brand_id,
  607. brand.name,
  608. line.name
  609. '''
  610. query_without_brand = '''
  611. SELECT
  612. invoice.id AS invoice_id,
  613. line.id AS invoice_line_id,
  614. invoice.number,
  615. invoice.origin,
  616. invoice.supplier_invoice_number,
  617. invoice.date_invoice,
  618. invoice.type,
  619. CASE
  620. WHEN product.default_code IS NOT NULL
  621. THEN ('[' || product.default_code || '] ' || product.name_template)
  622. ELSE product.name_template
  623. END AS display_name,
  624. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  625. line.quantity AS cant,
  626. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  627. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  628. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  629. journal.store_id,
  630. template.categ_id,
  631. partner.ruc,
  632. partner.name,
  633. product.id,
  634. invoice.company_id,
  635. invoice.journal_id,
  636. (array_agg(attr_rel.att_id)) AS attr_rel,
  637. (array_agg(attr_value.name)) AS attr_value,
  638. (array_agg(attr.id)) AS attr,
  639. line.name
  640. FROM account_invoice AS invoice
  641. LEFT JOIN account_invoice_line AS line
  642. ON line.invoice_id = invoice.id
  643. --Product
  644. LEFT JOIN product_product AS product
  645. ON line.product_id = product.id
  646. LEFT JOIN product_template AS template
  647. ON template.id = product.product_tmpl_id
  648. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  649. ON attr_rel.prod_id = product.id
  650. LEFT JOIN product_attribute_value AS attr_value
  651. ON attr_value.id = attr_rel.att_id
  652. LEFT JOIN product_attribute AS attr
  653. ON attr.id = attr_value.attribute_id
  654. LEFT JOIN res_store_journal_rel AS journal
  655. ON journal.journal_id = invoice.journal_id
  656. LEFT JOIN product_price_history AS history
  657. ON history.product_template_id = product.product_tmpl_id
  658. LEFT JOIN res_currency_rate AS rate
  659. ON rate.currency_id = invoice.currency_id
  660. LEFT JOIN res_partner AS partner
  661. ON partner.id = invoice.partner_id
  662. WHERE invoice.state NOT IN ('draft', 'cancel')
  663. AND invoice.type = 'in_invoice'
  664. AND invoice.origin LIKE %s
  665. GROUP BY
  666. invoice.id,
  667. line.id,
  668. invoice.number,
  669. invoice.origin,
  670. invoice.supplier_invoice_number,
  671. invoice.date_invoice,
  672. product.name_template,
  673. line.price_unit,
  674. line.quantity,
  675. line.price_subtotal,
  676. journal.store_id,
  677. template.categ_id,
  678. product.default_code,
  679. partner.ruc,
  680. partner.name,
  681. product.id,
  682. invoice.company_id,
  683. invoice.journal_id,
  684. line.name
  685. '''
  686. r.cr.execute(validate_brand)
  687. for j in r.cr.fetchall():
  688. brand = j[0]
  689. if brand == True:
  690. r.cr.execute(query_with_brand,([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,origin]))
  691. return [
  692. {
  693. 'invoice_id': j[0],
  694. 'invoice_line_id': j[1],
  695. 'number': j[2],
  696. 'origin': j[3],
  697. 'supplier_invoice_number': j[4],
  698. 'date': j[5],
  699. 'type': j[6],
  700. 'product_name':j[7],
  701. 'price_unit':j[8],
  702. 'quantity':j[9],
  703. 'subtotal':j[10],
  704. 'tax': j[11],
  705. 'cost': j[12],
  706. 'store_id': j[13],
  707. 'categ_id': j[14],
  708. 'partner_ruc': j[15],
  709. 'partner_name': j[16],
  710. 'product_id': j[17],
  711. 'company_id': j[18],
  712. 'journal_id': j[19],
  713. 'attribute_value_ids': j[20],
  714. 'attribute_values': j[21],
  715. 'attribute_ids': j[22],
  716. 'product_brand_id': j[23],
  717. 'brand_name': j[24],
  718. 'name': j[25],
  719. } for j in r.cr.fetchall()
  720. ]
  721. else:
  722. r.cr.execute(query_without_brand,([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,origin]))
  723. return [
  724. {
  725. 'invoice_id': j[0],
  726. 'invoice_line_id': j[1],
  727. 'number': j[2],
  728. 'origin': j[3],
  729. 'supplier_invoice_number': j[4],
  730. 'date': j[5],
  731. 'type': j[6],
  732. 'product_name':j[7],
  733. 'price_unit':j[8],
  734. 'quantity':j[9],
  735. 'subtotal':j[10],
  736. 'tax': j[11],
  737. 'cost': j[12],
  738. 'store_id': j[13],
  739. 'categ_id': j[14],
  740. 'partner_ruc': j[15],
  741. 'partner_name': j[16],
  742. 'product_id': j[17],
  743. 'company_id': j[18],
  744. 'journal_id': j[19],
  745. 'attribute_value_ids': j[20],
  746. 'attribute_values': j[21],
  747. 'attribute_ids': j[22],
  748. 'name': j[23],
  749. } for j in r.cr.fetchall()
  750. ]
  751. def get_account_invoice_line_in_invoice_expense():
  752. user_store = r.env.user.store_id.id
  753. company_currency_rate = r.env.user.company_id.currency_id.rate
  754. origin = '%PO%'
  755. validate_brand = '''
  756. SELECT EXISTS(
  757. SELECT table_name
  758. FROM information_schema.columns
  759. WHERE table_schema='public'
  760. AND table_name='product_brand')
  761. '''
  762. query_with_brand = '''
  763. SELECT
  764. invoice.id AS invoice_id,
  765. line.id AS invoice_line_id,
  766. invoice.number,
  767. invoice.origin,
  768. invoice.date_invoice,
  769. invoice.type,
  770. CASE
  771. WHEN product.default_code IS NOT NULL
  772. THEN ('[' || product.default_code || '] ' || product.name_template)
  773. ELSE product.name_template
  774. END AS display_name,
  775. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  776. line.quantity AS cant,
  777. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  778. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  779. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  780. journal.store_id,
  781. template.categ_id,
  782. partner.ruc,
  783. partner.name,
  784. product.id,
  785. invoice.company_id,
  786. invoice.journal_id,
  787. (array_agg(attr_rel.att_id)) AS attr_rel,
  788. (array_agg(attr_value.name)) AS attr_value,
  789. (array_agg(attr.id)) AS attr,
  790. template.product_brand_id,
  791. brand.name,
  792. line.name
  793. FROM account_invoice AS invoice
  794. LEFT JOIN account_invoice_line AS line
  795. ON line.invoice_id = invoice.id
  796. --Product
  797. LEFT JOIN product_product AS product
  798. ON line.product_id = product.id
  799. LEFT JOIN product_template AS template
  800. ON template.id = product.product_tmpl_id
  801. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  802. ON attr_rel.prod_id = product.id
  803. LEFT JOIN product_attribute_value AS attr_value
  804. ON attr_value.id = attr_rel.att_id
  805. LEFT JOIN product_attribute AS attr
  806. ON attr.id = attr_value.attribute_id
  807. LEFT JOIN res_store_journal_rel AS journal
  808. ON journal.journal_id = invoice.journal_id
  809. LEFT JOIN product_price_history AS history
  810. ON history.product_template_id = product.product_tmpl_id
  811. LEFT JOIN res_currency_rate AS rate
  812. ON rate.currency_id = invoice.currency_id
  813. LEFT JOIN res_partner AS partner
  814. ON partner.id = invoice.partner_id
  815. LEFT JOIN product_brand AS brand
  816. ON brand.id = template.product_brand_id
  817. WHERE invoice.state NOT IN ('draft', 'cancel')
  818. AND invoice.type = 'in_invoice'
  819. AND (invoice.origin NOT LIKE %s OR invoice.origin IS NULL)
  820. GROUP BY
  821. invoice.id,
  822. line.id,
  823. invoice.number,
  824. invoice.origin,
  825. invoice.date_invoice,
  826. product.name_template,
  827. line.price_unit,
  828. line.quantity,
  829. line.price_subtotal,
  830. journal.store_id,
  831. template.categ_id,
  832. product.default_code,
  833. partner.ruc,
  834. partner.name,
  835. product.id,
  836. invoice.company_id,
  837. invoice.journal_id,
  838. template.product_brand_id,
  839. brand.name,
  840. line.name
  841. '''
  842. query_without_brand = '''
  843. SELECT
  844. invoice.id AS invoice_id,
  845. line.id AS invoice_line_id,
  846. invoice.number,
  847. invoice.origin,
  848. invoice.date_invoice,
  849. invoice.type,
  850. CASE
  851. WHEN product.default_code IS NOT NULL
  852. THEN ('[' || product.default_code || '] ' || product.name_template)
  853. ELSE product.name_template
  854. END AS display_name,
  855. line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) as price_unit,
  856. line.quantity AS cant,
  857. line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]) AS subtotal,
  858. (line.quantity * (line.price_unit * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1]))) - (line.price_subtotal * (%s / (array_agg(rate.rate ORDER BY rate.id DESC))[1])) AS impuestos,
  859. (array_agg(history.cost ORDER BY history.id DESC))[1] AS cost,
  860. journal.store_id,
  861. template.categ_id,
  862. partner.ruc,
  863. partner.name,
  864. product.id,
  865. invoice.company_id,
  866. invoice.journal_id,
  867. (array_agg(attr_rel.att_id)) AS attr_rel,
  868. (array_agg(attr_value.name)) AS attr_value,
  869. (array_agg(attr.id)) AS attr,
  870. line.name
  871. FROM account_invoice AS invoice
  872. LEFT JOIN account_invoice_line AS line
  873. ON line.invoice_id = invoice.id
  874. --Product
  875. LEFT JOIN product_product AS product
  876. ON line.product_id = product.id
  877. LEFT JOIN product_template AS template
  878. ON template.id = product.product_tmpl_id
  879. LEFT JOIN product_attribute_value_product_product_rel AS attr_rel
  880. ON attr_rel.prod_id = product.id
  881. LEFT JOIN product_attribute_value AS attr_value
  882. ON attr_value.id = attr_rel.att_id
  883. LEFT JOIN product_attribute AS attr
  884. ON attr.id = attr_value.attribute_id
  885. LEFT JOIN res_store_journal_rel AS journal
  886. ON journal.journal_id = invoice.journal_id
  887. LEFT JOIN product_price_history AS history
  888. ON history.product_template_id = product.product_tmpl_id
  889. LEFT JOIN res_currency_rate AS rate
  890. ON rate.currency_id = invoice.currency_id
  891. LEFT JOIN res_partner AS partner
  892. ON partner.id = invoice.partner_id
  893. WHERE invoice.state NOT IN ('draft', 'cancel')
  894. AND invoice.type = 'in_invoice'
  895. AND (invoice.origin NOT LIKE %s OR invoice.origin IS NULL)
  896. GROUP BY
  897. invoice.id,
  898. line.id,
  899. invoice.number,
  900. invoice.origin,
  901. invoice.date_invoice,
  902. product.name_template,
  903. line.price_unit,
  904. line.quantity,
  905. line.price_subtotal,
  906. journal.store_id,
  907. template.categ_id,
  908. product.default_code,
  909. partner.ruc,
  910. partner.name,
  911. product.id,
  912. invoice.company_id,
  913. invoice.journal_id,
  914. line.name
  915. '''
  916. r.cr.execute(validate_brand)
  917. for j in r.cr.fetchall():
  918. brand = j[0]
  919. if brand == True:
  920. r.cr.execute(query_with_brand,([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,origin]))
  921. return [
  922. {
  923. 'invoice_id': j[0],
  924. 'invoice_line_id': j[1],
  925. 'number': j[2],
  926. 'origin': j[3],
  927. 'date': j[4],
  928. 'type': j[5],
  929. 'product_name':j[6],
  930. 'price_unit':j[7],
  931. 'quantity':j[8],
  932. 'subtotal':j[9],
  933. 'tax': j[10],
  934. 'cost': j[11],
  935. 'store_id': j[12],
  936. 'categ_id': j[13],
  937. 'partner_ruc': j[14],
  938. 'partner_name': j[15],
  939. 'product_id': j[16],
  940. 'company_id': j[17],
  941. 'journal_id': j[18],
  942. 'attribute_value_ids': j[19],
  943. 'attribute_values': j[20],
  944. 'attribute_ids': j[21],
  945. 'product_brand_id': j[22],
  946. 'brand_name': j[23],
  947. 'name': j[24],
  948. } for j in r.cr.fetchall()
  949. ]
  950. else:
  951. r.cr.execute(query_without_brand,([company_currency_rate,company_currency_rate,company_currency_rate,company_currency_rate,origin]))
  952. return [
  953. {
  954. 'invoice_id': j[0],
  955. 'invoice_line_id': j[1],
  956. 'number': j[2],
  957. 'origin': j[3],
  958. 'date': j[4],
  959. 'type': j[5],
  960. 'product_name':j[6],
  961. 'price_unit':j[7],
  962. 'quantity':j[8],
  963. 'subtotal':j[9],
  964. 'tax': j[10],
  965. 'cost': j[11],
  966. 'store_id': j[12],
  967. 'categ_id': j[13],
  968. 'partner_ruc': j[14],
  969. 'partner_name': j[15],
  970. 'product_id': j[16],
  971. 'company_id': j[17],
  972. 'journal_id': j[18],
  973. 'attribute_value_ids': j[19],
  974. 'attribute_values': j[20],
  975. 'attribute_ids': j[21],
  976. 'name': j[22],
  977. } for j in r.cr.fetchall()
  978. ]