Advertisement
Guest User

Untitled

a guest
Jul 18th, 2019
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.72 KB | None | 0 0
  1. SELECT
  2. inr.CompanyID
  3. , DistributorID = inr.BranchID
  4. , intr.TranDate
  5. , ints.InventoryID
  6. , IsFree = CASE
  7. WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND (porl.LineAmt = 0 OR porl.ManualDisc = 1))
  8. OR (ints.TranType = 'INV' AND sol.IsFree = 1)
  9. THEN 1
  10. ELSE 0
  11. END
  12. , LotSerialNbr = ISNULL(ints.LotSerialNbr, '')
  13. , ExpiredDate = ints.ExpireDate
  14. , ints.SiteID
  15. , ints.LocationID
  16. , InQty = CASE
  17. WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt > 0) ---- Nhập mua
  18. OR (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt = 0) ---- Nhập khuyến mãi
  19. OR (ints.TranType = 'RCP' AND inr.OrigModule = 'IN') ---- Nhập khác
  20. OR (ints.TranType = 'TRX' AND ints.InvtMult = 1) ---- Chuyển kho - nhập
  21. OR (ints.TranType = 'ADJ' AND ints.InvtMult = 1) ---- Điều chỉnh tăng kho
  22. OR (ints.TranType = 'RET' ) ---- Nhập trả
  23. OR (ints.TranType = 'CRM' AND sol.IsFree = 0) ---- Nhập khách hàng trả
  24. OR (ints.TranType = 'CRM' AND sol.IsFree = 1) ---- Nhập khách hàng trả KM
  25. THEN ints.BaseQty
  26. WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NOT NULL) ---- Xuất trả ncc
  27. THEN ints.BaseQty * -1
  28. ELSE 0
  29. END
  30. , OutQty = CASE
  31. WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NULL) ---- Xuất khác
  32. OR (ints.TranType = 'TRX' AND ints.InvtMult = -1) ---- Chuyển kho - xuất
  33. OR (ints.TranType = 'ADJ' AND ints.InvtMult = -1) ---- Điều chỉnh giảm kho
  34. OR (ints.TranType = 'DRM' ) ---- Xuất debit
  35. OR (ints.TranType = 'INV' AND sol.IsFree = 0) ---- Xuất bán
  36. OR (ints.TranType = 'INV' AND sol.IsFree = 1) ---- Xuất khuyến mãi
  37. OR (ints.TranType = 'INV' AND sol.IsFree = 1 AND sol.UsrIncentiveID IS NOT NULL) ---- Xuất trả thưởng
  38. THEN ints.BaseQty
  39. ELSE 0
  40. END
  41. , ReasonCodeDescr = CASE
  42. ---- Nhập
  43. WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt > 0) ---- Nhập mua
  44. OR (ints.TranType = 'III' AND por.ReceiptNbr IS NOT NULL) ---- Xuất trả ncc
  45. OR (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND ( porl.LineAmt = 0 OR porl.ManualDisc = 1)) ---- Nhập khuyến mãi
  46. THEN 'PurchaseReceipt' ---- Gom chung là nhập mua
  47.  
  48. WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'IN') ---- Nhập khác
  49. OR (ints.TranType = 'TRX' AND ints.InvtMult = 1) ---- Chuyển kho - nhập
  50. OR (ints.TranType = 'ADJ' AND ints.InvtMult = 1) ---- Điều chỉnh tăng kho
  51. OR (ints.TranType = 'RET' ) ---- Nhập trả
  52. OR (ints.TranType = 'CRM' AND soo.UsrIsDispose = 1) --- Nhập khách hàng trả nhưng tào lao
  53. THEN 'OtherReceipt' ---- Gom chung là nhập khác
  54.  
  55. WHEN ints.TranType = 'CRM' AND sol.IsFree = 0
  56. THEN 'CustomerReturn' ---- Nhập khách hàng trả
  57.  
  58. WHEN ints.TranType = 'CRM' AND sol.IsFree = 1
  59. THEN 'CustomerPromotionReturn' ---- Nhập khách hàng trả KM
  60.  
  61. ---- Xuất
  62. WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NULL) ---- Xuất khác
  63. OR (ints.TranType = 'TRX' AND ints.InvtMult = -1) ---- Chuyển kho - xuất
  64. OR (ints.TranType = 'ADJ' AND ints.InvtMult = -1) ---- Điều chỉnh giảm kho
  65. OR (ints.TranType = 'DRM' ) ---- Xuất debit
  66. THEN 'OtherIssue' ---- Gom chung là xuất khác
  67.  
  68. WHEN ints.TranType = 'INV' AND sol.IsFree = 0
  69. THEN 'SalesIssue' ---- Xuất bán
  70.  
  71. WHEN ints.TranType = 'INV' AND sol.IsFree = 1
  72. THEN 'PromotionIssue' ---- Xuất khuyến mãi
  73.  
  74. WHEN ints.TranType = 'INV' AND sol.IsFree = 1 AND sol.UsrIncentiveID IS NOT NULL
  75. THEN 'IncentiveIssue' ---- Xuất trả thưởng
  76. END
  77. , intr.SOOrderNbr
  78. , intr.SOOrderType
  79. FROM
  80. dbo.INRegister inr WITH (NOLOCK)
  81. JOIN dbo.INTran intr WITH (NOLOCK) ON intr.CompanyID = inr.CompanyID
  82. AND intr.RefNbr = inr.RefNbr
  83. AND intr.DocType = inr.DocType
  84. JOIN dbo.INTranSplit ints WITH (NOLOCK) ON ints.CompanyID = intr.CompanyID
  85. AND ints.RefNbr = intr.RefNbr
  86. AND ints.DocType = intr.DocType
  87. AND ints.LineNbr = intr.LineNbr
  88. LEFT JOIN dbo.SOOrder soo WITH (NOLOCK) ON soo.CompanyID = intr.CompanyID
  89. AND soo.OrderNbr = intr.SOOrderNbr
  90. AND soo.OrderType = intr.SOOrderType
  91. LEFT JOIN dbo.SOLine sol WITH (NOLOCK) ON sol.CompanyID = intr.CompanyID
  92. AND sol.OrderNbr = intr.SOOrderNbr
  93. AND sol.OrderType = intr.SOOrderType
  94. AND sol.LineNbr = intr.SOOrderLineNbr
  95. LEFT JOIN dbo.POReceipt por WITH (NOLOCK) ON por.CompanyID = intr.CompanyID
  96. AND por.ReceiptNbr = intr.POReceiptNbr
  97. LEFT JOIN dbo.POReceiptLine porl WITH (NOLOCK) ON porl.CompanyID = intr.CompanyID
  98. AND porl.ReceiptNbr = intr.POReceiptNbr
  99. AND porl.LineNbr = intr.POReceiptLineNbr
  100. WHERE
  101. inr.CompanyID = 3
  102. AND inr.Released = 1
  103. AND (intr.SOOrderNbr IS NULL
  104. OR (soo.OrderType != 'CM' AND ISNULL(soo.UsrIsDispose, 0) = 0)
  105. OR (soo.OrderType = 'CM' AND ISNULL(soo.UsrIsRevenueDeduction, 0) = 1))
  106. AND inr.TranDate BETWEEN '2019-07-01' AND '2019-07-18'
  107. AND soo.OrderNbr = 'DH360-068186'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement