Advertisement
Guest User

Comptable requete

a guest
Feb 24th, 2020
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.86 KB | None | 0 0
  1. SELECT *
  2. from (select 'FACTURE' as Type_facture,
  3. MAX(to_char(l.created_at, 'DD/MM/YYYY')) as date_facture,
  4. MAX(li.reference) as Ref_facture,
  5. AVG(l.seller_id) as Num_seller,
  6. AVG(l.buyer_id) as Num_buyer,
  7. (SELECT SUM(lilHT.quantity * lilHT.unit_price)
  8. FROM lorder_invoice as liHT
  9. LEFT JOIN lorder_invoice_line as lilHT ON liHT.id = lilHT.lorder_invoice_id
  10. WHERE liHT.id = li.id
  11. GROUP BY liHT.reference)
  12. as tot_HT,
  13. (SELECT SUM(lilTVA20.quantity * lilTVA20.unit_price)
  14. FROM lorder_invoice as liTVA20
  15. LEFT JOIN lorder_invoice_line as lilTVA20 ON liTVA20.id = lilTVA20.lorder_invoice_id
  16. WHERE lilTVA20.vat_rate = '20.00'
  17. and liTVA20.id = li.id
  18. GROUP BY liTVA20.reference)
  19. as TVA20,
  20. (SELECT SUM(lilTVA210.quantity * lilTVA210.unit_price)
  21. FROM lorder_invoice as liTVA210
  22. LEFT JOIN lorder_invoice_line as lilTVA210 ON liTVA210.id = lilTVA210.lorder_invoice_id
  23. WHERE lilTVA210.vat_rate = '2.10'
  24. and liTVA210.id = li.id
  25. GROUP BY liTVA210.reference)
  26. as TVA210,
  27. (SELECT SUM(lilTVA55.quantity * lilTVA55.unit_price)
  28. FROM lorder_invoice as liTVA55
  29. LEFT JOIN lorder_invoice_line as lilTVA55 ON liTVA55.id = lilTVA55.lorder_invoice_id
  30. WHERE lilTVA55.vat_rate = '5.50'
  31. and liTVA55.id = li.id
  32. GROUP BY liTVA55.reference)
  33. as TVA55,
  34. (SELECT SUM(lilTVA10.quantity * lilTVA10.unit_price)
  35. FROM lorder_invoice as liTVA10
  36. LEFT JOIN lorder_invoice_line as lilTVA10 ON liTVA10.id = lilTVA10.lorder_invoice_id
  37. WHERE lilTVA10.vat_rate = '10.00'
  38. and liTVA10.id = li.id
  39. GROUP BY liTVA10.reference)
  40. as TVA10,
  41. AVG(li.total_ttc) as Tot_ttc,
  42. AVG(l.shipping_fees) as FDP,
  43. MAX(ROUND(commission_invoice_buyer.total_ttc / '1.2', '2')) as com_buyer,
  44. MAX(ROUND(cis.total_ttc / '1.2', '2')) as com_seller,
  45. MAX(commission_invoice_buyer.reference) as Num_facture_buyer,
  46. MAX(cis.reference) as Num_facture_seller,
  47. '-' as Reference_vente_remb,
  48. MAX(l.reference) as Reference_cmde,
  49. max(subPayIn.date_Payin) as Date_Debit,
  50. max(subTransfer.date_transfert) as Date_Transfert,
  51. max(subPayOUT.date_Payout) as Date_Credit,
  52. max(ls.machine_name) as Statut_commande
  53. from lorder l
  54. left join (select right(mt.tag, 15) as refcmd,
  55. to_timestamp(mt.creationdate) as date_Payin
  56. from mangopay_transaction mt
  57. left join lorder l on right(mt.tag, 15) = l.reference
  58. where mt.status in ('SUCCEEDED', 'CREATED')
  59. and mt.type = 'PAYIN'
  60. ) subPayIn on l.reference = subPayIn.refcmd
  61. left join (select right(mt.tag, 15) as refcmd,
  62. to_timestamp(mt.creationdate) as date_transfert
  63. from mangopay_transaction mt
  64. right join lorder l on l.reference = right(mt.tag, 15)
  65. where substr(mt.tag, 1, 5) = 'order'
  66. and mt.nature = 'REGULAR'
  67. and mt.status = 'SUCCEEDED'
  68. and mt.type = 'TRANSFER'
  69. ) subTransfer on l.reference = subTransfer.refcmd
  70. left join(select right(mt.tag, 15) as refcmd,
  71. to_timestamp(mt.creationdate) as date_Payout
  72. from mangopay_transaction mt
  73. right join lorder l on l.reference = right(mt.tag, 15)
  74. where mt.nature = 'REGULAR'
  75. and mt.status = 'SUCCEEDED'
  76. and mt.type = 'PAYOUT'
  77. and substr(mt.tag, 1, 5) = 'order'
  78. ) subPayOUT on l.reference = subPayOUT.refcmd
  79.  
  80. --left join client c on l.seller_id = c.id
  81. left join lorder_invoice li on l.id = li.order_id
  82. -- LEFT JOIN lorder_invoice_line liline ON li.id = liline.lorder_invoice_id
  83. -- LEFT JOIN shopping_cart_line scl ON l.id = scl.order_id
  84. LEFT JOIN commission_invoice_buyer ON l.id = commission_invoice_buyer.order_id
  85. LEFT JOIN commission_invoice_seller cis ON l.id = cis.order_id
  86. Left join lorder_status ls ON ls.id = l.status_id
  87. where (l.created_at between '2019-01-01' and '2019-12-31')
  88. group by li.id
  89. UNION
  90. SELECT 'AVOIR' as Type_facture,
  91. MAX(to_char(cnr.created_at, 'DD/MM/YYYY')) as date_facture,
  92. MAX(cnr.reference_bill) as Ref_facture,
  93. AVG(l.seller_id) as Num_seller,
  94. AVG(l.buyer_id) as Num_buyer,
  95. (SELECT SUM(lrcHT.quantity * lrcHT.discounted_price * (-1))
  96. FROM lorder_invoice as liCreditHT
  97. LEFT JOIN lorder as orderRefundHT ON liCreditHT.order_id = orderRefundHT.id
  98. LEFT JOIN lorder_refund as lrHT ON orderRefundHT.id = lrHT.order_id
  99. LEFT JOIN lorder_refund_content as lrcHT ON lrHT.id = lrcHT.lorder_refund_id
  100. WHERE liCreditHT.id = li.id
  101. GROUP BY liCreditHT.reference) as tot_HT,
  102. (SELECT SUM(lrc20CR.quantity * lrc20CR.discounted_price * (-1))
  103. FROM lorder_invoice as liTVA20CRED
  104. LEFT JOIN lorder as orderTVA20CRED ON liTVA20CRED.order_id = orderTVA20CRED.id
  105. LEFT JOIN lorder_refund as lr20CR ON orderTVA20CRED.id = lr20CR.order_id
  106. LEFT JOIN lorder_refund_content as lrc20CR ON lr20CR.id = lrc20CR.lorder_refund_id
  107. WHERE lrc20CR.vat_rate = '20.00'
  108. and liTVA20CRED.id = li.id
  109. GROUP BY liTVA20CRED.reference)
  110. as TVA20,
  111. (SELECT SUM(lrc210CR.quantity * lrc210CR.discounted_price * (-1))
  112. FROM lorder_invoice as liTVA210CRED
  113. LEFT JOIN lorder as orderTVA210CRED ON liTVA210CRED.order_id = orderTVA210CRED.id
  114. LEFT JOIN lorder_refund as lr210CR ON orderTVA210CRED.id = lr210CR.order_id
  115. LEFT JOIN lorder_refund_content as lrc210CR ON lr210CR.id = lrc210CR.lorder_refund_id
  116. WHERE lrc210CR.vat_rate = '2.10'
  117. and liTVA210CRED.id = li.id
  118. GROUP BY liTVA210CRED.reference)
  119. as TVA210,
  120. (SELECT SUM(lrc55CR.quantity * lrc55CR.discounted_price * (-1))
  121. FROM lorder_invoice as liTVA55CRED
  122. LEFT JOIN lorder as orderTVA55CRED ON liTVA55CRED.order_id = orderTVA55CRED.id
  123. LEFT JOIN lorder_refund as lr55CR ON orderTVA55CRED.id = lr55CR.order_id
  124. LEFT JOIN lorder_refund_content as lrc55CR ON lr55CR.id = lrc55CR.lorder_refund_id
  125. WHERE lrc55CR.vat_rate = '5.50'
  126. and liTVA55CRED.id = li.id
  127. GROUP BY liTVA55CRED.reference)
  128. as TVA55,
  129. (SELECT SUM(lrc10CR.quantity * lrc10CR.discounted_price * (-1))
  130. FROM lorder_invoice as liTVA10CRED
  131. LEFT JOIN lorder as orderTVA10CRED ON liTVA10CRED.order_id = orderTVA10CRED.id
  132. LEFT JOIN lorder_refund as lr10CR ON orderTVA10CRED.id = lr10CR.order_id
  133. LEFT JOIN lorder_refund_content as lrc10CR ON lr10CR.id = lrc10CR.lorder_refund_id
  134. WHERE lrc10CR.vat_rate = '10'
  135. and liTVA10CRED.id = li.id
  136. GROUP BY liTVA10CRED.reference)
  137. as TVA10,
  138. (SELECT ROUND(SUM(lrcTTC.quantity * lrcTTC.discounted_price * ('1' + (lrcTTC.vat_rate / '100'))), '2') *
  139. (-1)
  140. FROM lorder_invoice as liCredit
  141. LEFT JOIN lorder as orderRefund ON liCredit.order_id = orderRefund.id
  142. LEFT JOIN lorder_refund as lrTTC ON orderRefund.id = lrTTC.order_id
  143. LEFT JOIN lorder_refund_content as lrcTTC ON lrTTC.id = lrcTTC.lorder_refund_id
  144. WHERE liCredit.id = li.id
  145. GROUP BY liCredit.reference) as Tot_ttc,
  146. AVG(cn.shipping_fees) * (-1) as FDP,
  147. (SELECT ROUND(SUM(lrComBuy.refund_commissions_amount) / '1.2', '2') * (-1)
  148. FROM lorder_invoice as liComBuy
  149. LEFT JOIN lorder as orderComBuy ON liComBuy.order_id = orderComBuy.id
  150. LEFT JOIN lorder_refund as lrComBuy ON orderComBuy.id = lrComBuy.order_id
  151. WHERE liComBuy.id = li.id
  152. GROUP BY orderComBuy.id) as com_buyer,
  153. (SELECT ROUND(SUM(lrComSel.refund_commissions_amount) / '1.2', '2') * (-1)
  154. FROM lorder_invoice as liComSel
  155. LEFT JOIN lorder as orderComSel ON liComSel.order_id = orderComSel.id
  156. LEFT JOIN lorder_refund as lrComSel ON orderComSel.id = lrComSel.order_id
  157. WHERE liComSel.id = li.id
  158. GROUP BY orderComSel.id) as com_seller,
  159. MAX(commission_invoice_buyer.reference) as Num_facture_buyer,
  160. MAX(cis.reference) as Num_facture_seller,
  161. MAX(li.reference) as Reference_vente_remb,
  162. MAX(l.reference) as Reference_cmde,
  163. to_timestamp(0) as date_debit,
  164. to_timestamp(0) as date_transfer,
  165. max(subPayOUT.date_Payout) as date_Credit,
  166. max(ls.machine_name) as Statut_commande
  167. FROM lorder l
  168. left join (select right(mt.tag, 15) as refcmd,
  169. to_timestamp(mt.creationdate) as date_Payout
  170. from mangopay_transaction mt
  171. right join lorder l on l.reference = right(mt.tag, 15)
  172. where mt.nature = 'REFUND'
  173. and mt.status = 'SUCCEEDED'
  174. and mt.type = 'PAYOUT') subPayOUT on l.reference = subPayOUT.refcmd
  175. LEFT JOIN lorder_invoice as li ON l.id = li.order_id
  176. --LEFT JOIN lorder_invoice_line as liline ON li.id = liline.lorder_invoice_id
  177. --LEFT JOIN shopping_cart_line as scl ON lorder.id = scl.order_id
  178. LEFT JOIN commission_invoice_buyer ON l.id = commission_invoice_buyer.order_id
  179. LEFT JOIN commission_invoice_seller as cis ON l.id = cis.order_id
  180. LEFT JOIN credit_note cn ON l.id = cn.order_id
  181. LEFT JOIN credit_note_reference cnr ON cn.credit_note_reference_id = cnr.id
  182. --LEFT JOIN lorder_refund ON lorder.id = lorder_refund.order_id
  183. --LEFT JOIN lorder_refund_content ON lorder_refund.id = lorder_refund_content.lorder_refund_id
  184. Left join lorder_status as ls ON ls.id = l.status_id
  185. WHERE cn.credit_type = 'PARTIAL'
  186. and (l.created_at between '2019-01-01' and '2019-12-31')
  187. GROUP BY li.id
  188. UNION
  189. SELECT 'AVOIR' as Type_facture,
  190. MAX(to_char(cnr.created_at, 'DD/MM/YYYY')) as date_facture,
  191. MAX(cnr.reference_bill) as Ref_facture,
  192. AVG(l.seller_id) as Num_seller,
  193. AVG(l.buyer_id) as Num_buyer,
  194. (SELECT SUM(lilHT.quantity * lilHT.unit_price * (-1))
  195. FROM lorder_invoice as liHT
  196. LEFT JOIN lorder_invoice_line as lilHT ON liHT.id = lilHT.lorder_invoice_id
  197. WHERE liHT.id = li.id
  198. GROUP BY liHT.reference)
  199. as tot_HT,
  200. (SELECT SUM(lilTVA20.quantity * lilTVA20.unit_price * (-1))
  201. FROM lorder_invoice as liTVA20
  202. LEFT JOIN lorder_invoice_line as lilTVA20 ON liTVA20.id = lilTVA20.lorder_invoice_id
  203. WHERE lilTVA20.vat_rate = '20.00'
  204. and liTVA20.id = li.id
  205. GROUP BY liTVA20.reference)
  206. as TVA20,
  207. (SELECT SUM(lilTVA210.quantity * lilTVA210.unit_price * (-1))
  208. FROM lorder_invoice as liTVA210
  209. LEFT JOIN lorder_invoice_line as lilTVA210 ON liTVA210.id = lilTVA210.lorder_invoice_id
  210. WHERE lilTVA210.vat_rate = '2.10'
  211. and liTVA210.id = li.id
  212. GROUP BY liTVA210.reference)
  213. as TVA210,
  214. (SELECT SUM(lilTVA55.quantity * lilTVA55.unit_price * (-1))
  215. FROM lorder_invoice as liTVA55
  216. LEFT JOIN lorder_invoice_line as lilTVA55 ON liTVA55.id = lilTVA55.lorder_invoice_id
  217. WHERE lilTVA55.vat_rate = '5.50'
  218. and liTVA55.id = li.id
  219. GROUP BY liTVA55.reference)
  220. as TVA55,
  221. (SELECT SUM(lilTVA10.quantity * lilTVA10.unit_price * (-1))
  222. FROM lorder_invoice as liTVA10
  223. LEFT JOIN lorder_invoice_line as lilTVA10 ON liTVA10.id = lilTVA10.lorder_invoice_id
  224. WHERE lilTVA10.vat_rate = '10.00'
  225. and liTVA10.id = li.id
  226. GROUP BY liTVA10.reference)
  227. as TVA10,
  228. AVG(li.total_ttc) * (-1) as Tot_ttc,
  229. AVG(l.shipping_fees) * (-1) as FDP,
  230. AVG(ROUND(commission_invoice_buyer.total_ttc / '1.2', '2')) * (-1) as com_buyer,
  231. AVG(ROUND(cis.total_ttc / '1.2', '2')) * (-1) as com_seller,
  232. MAX(commission_invoice_buyer.reference) as Num_facture_buyer,
  233. MAX(cis.reference) as Num_facture_seller,
  234. '-' as Reference_vente_remb,
  235. MAX(l.reference) as Reference_cmde,
  236. to_timestamp(0) as date_debit,
  237. to_timestamp(0) as date_transfer,
  238. max(subPayOUT.date_Payout) as date_Credit,
  239.  
  240. max(ls.machine_name) as Statut_commande
  241. FROM lorder l
  242. left join (select right(mt.tag, 15) as refcmd,
  243. to_timestamp(mt.creationdate) as date_Payout
  244. from mangopay_transaction mt
  245. right join lorder l on l.reference = right(mt.tag, 15)
  246.  
  247. where mt.nature = 'REFUND'
  248. and mt.status = 'SUCCEEDED'
  249. and mt.type = 'PAYOUT') subPayOUT on l.reference = subPayOUT.refcmd
  250. LEFT JOIN lorder_invoice as li ON li.order_id = l.id
  251. --LEFT JOIN lorder_invoice_line as liline ON li.id = liline.lorder_invoice_id
  252. --LEFT JOIN shopping_cart_line as scl ON lorder.id = scl.order_id
  253. LEFT JOIN commission_invoice_buyer ON l.id = commission_invoice_buyer.order_id
  254. LEFT JOIN commission_invoice_seller as cis ON l.id = cis.order_id
  255. Left join lorder_status as ls ON ls.id = l.status_id
  256. LEFT JOIN credit_note cn ON l.id = cn.order_id
  257. LEFT JOIN credit_note_reference cnr ON cn.credit_note_reference_id = cnr.id
  258. WHERE cn.credit_type = 'TOTAL'
  259. and (l.created_at between '2019-01-01' and '2019-12-31')
  260. GROUP BY li.id) as sub
  261. WHERE Ref_facture NOTNULL
  262. ORDER BY sub.Reference_cmde ASC;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement