Advertisement
Guest User

Untitled

a guest
Jan 24th, 2020
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. SELECT p.id AS 'Product ID',
  2. p.barcode AS 'Barcode',
  3. p.name AS 'Product Name',
  4. b.name AS 'Brand Name',
  5. m.name AS 'Merchant Name',
  6. m.description AS 'Merchant Name 2',
  7. c.cat_1 AS 'Primary Category',
  8. c.cat_2 AS 'Sub Category',
  9. so.transaction_id AS 'Transaction ID',
  10. so.order_date AS 'Confirm Date',
  11. so.claim_date AS 'Claim Date',
  12. so.status as 'Current Status',
  13. so.seven_connect_ref_num AS 'Seven Connect Ref Num',
  14. CAST(so.delivery_location_id AS SIGNED) AS 'Store ID',
  15. rn.store_name AS 'Store Name',
  16. so.total_price AS 'Peso Paid',
  17. IFNULL(pp.amount, pp2.amount) AS 'Product Price',
  18. IFNULL(pc.amount, pc2.amount) AS 'Product Cost',
  19. IF(so.is_point_cash = 1, MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END), 0) as 'Points Used',
  20. IFNULL(cast(so.is_wallet AS SIGNED),0) AS 'Is_Wallet',
  21. soi.quantity AS 'Volume',
  22. CAST(so.is_cod AS SIGNED) AS 'Is_COD',
  23. IFNULL(so.coupon_code,'N/A') AS 'Coupon Code',
  24. CAST(cu.mobile_number AS SIGNED) AS 'Mobile Number',
  25. IFNULL(CAST(is_point_cash AS SIGNED),0) AS 'Is_Pts+peso',
  26. so.requesting_device AS 'Requesting Device',
  27. CASE
  28. WHEN so.is_wallet = 1 AND so.is_point_cash = 1 THEN MAX(CASE WHEN sop.wallet_type = 'CLIQQWALLET' THEN sop.amount END) + MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END)
  29. WHEN so.is_wallet = 1 AND so.is_point_cash IS NULL THEN MAX(CASE WHEN sop.wallet_type = 'CLIQQWALLET' THEN sop.amount END)
  30. WHEN (so.is_wallet = 0 OR so.is_wallet IS NULL) AND so.is_point_cash = 1 THEN so.total_price + MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END)
  31. WHEN (so.is_wallet = 0 OR so.is_wallet IS NULL) AND so.is_point_cash IS NULL THEN so.total_price
  32. END as 'Total Payment',
  33. CASE
  34. WHEN so.is_wallet = 1 AND so.is_point_cash = 1 THEN MAX(CASE WHEN sop.wallet_type = 'CLIQQWALLET' THEN sop.amount END) + MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END)
  35. WHEN so.is_wallet = 1 AND so.is_point_cash IS NULL THEN MAX(CASE WHEN sop.wallet_type = 'CLIQQWALLET' THEN sop.amount END)
  36. WHEN (so.is_wallet = 0 OR so.is_wallet IS NULL) AND so.is_point_cash = 1 THEN so.total_price + MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END)
  37. WHEN (so.is_wallet = 0 OR so.is_wallet IS NULL) AND so.is_point_cash IS NULL THEN so.total_price
  38. END - so.total_price - IF(so.is_point_cash = 1, MAX(CASE WHEN sop.currency = 'LPTS' THEN sop.amount END), 0) as 'Wallet Credits Used'
  39.  
  40. FROM ecms.sales_order so
  41. LEFT JOIN ecms.sales_order_item soi ON soi.sales_order_id = so.id
  42. LEFT JOIN ecms.sales_order_payment sop ON sop.sales_order_id = so.id
  43. LEFT JOIN ecms.customer cu ON cu.id = so.customer_id
  44. LEFT JOIN ecms.history h ON h.transaction_id = so.transaction_id
  45. LEFT JOIN ecms.product p ON p.id = soi.product_id
  46. LEFT JOIN ecms.product_price pp ON pp.product_id = p.id AND pp.date_created <= so.date_created AND so.date_created BETWEEN COALESCE(pp.from_date, so.date_created) AND COALESCE(pp.thru_date, so.date_created)
  47. LEFT JOIN ecms.product_price pp2 ON pp2.product_id = p.id
  48. LEFT JOIN ecms.product_cost pc ON pc.product_id = p.id AND pc.date_created <= so.date_created AND so.date_created BETWEEN COALESCE(pc.from_date, so.date_created) AND COALESCE(pc.thru_date, so.date_created)
  49. LEFT JOIN ecms.product_cost pc2 ON pc2.product_id = p.id
  50. LEFT JOIN ecms.merchant m ON m.id = p.merchant_id
  51. LEFT JOIN ecms.brand b ON b.id = p.brand_id
  52. LEFT JOIN ecms.route_number rn ON rn.facility_id = so.delivery_location_id
  53. LEFT JOIN (
  54. SELECT
  55.  
  56. c0.id,
  57. IFNULL(c1.name, c0.name) as cat_1,
  58. c0.name as cat_2
  59.  
  60. FROM ecms.category c0
  61. LEFT JOIN ecms.category_rollup cr ON category_id = c0.id
  62. LEFT JOIN ecms.category c1 ON c1.id = cr.parent_category_id
  63. )c ON c.id = p.category_id
  64.  
  65. WHERE so.order_type = 'ITEM FULFILLMENT'
  66. AND so.status IN ('FOR CONFIRMATION','PL GEN COMPLETED','CONFIRMED','FOR SORTING','FOR DELIVERY','IN-TRANSIT','UNSERVED','LOST','DELIVERED TO STORE','CLAIMED','FOR PULL OUT','FOR RETURN TO MERCHANT','RETURNED TO MERCHANT','FOR REFUND','REFUNDED','DELIVERED TO WAREHOUSE_REGIONAL','IN-TRANSIT_REGIONAL','ARRIVED AT STORE','ARRIVED AT WAREHOUSE','ON VEHICLE TO STORE','ON VEHICLE TO WAREHOUSE')
  67. AND p.name NOT IN ('CLiQQ Live Test Product','Live Test Product','711 TOUR Race Kit 48KM 500','711 TOUR Race Kit 106KM 900')
  68.  
  69. AND date(so.order_date) BETWEEN '2020-01-01 00:00:00' AND '2020-01-23 23:59:59'
  70. AND so.requesting_device IN ('PWA','KIOSK','CLIQQ')
  71.  
  72. GROUP BY 9,1,2,3,4,5,6,7,8,10,11,12,13,14,15,16,17,18,20,21,22,23,24,25,26
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement