Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- SELECT
- inr.CompanyID
- , DistributorID = inr.BranchID
- , intr.TranDate
- , ints.InventoryID
- , IsFree = CASE
- WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND (porl.LineAmt = 0 OR porl.ManualDisc = 1))
- OR (ints.TranType = 'INV' AND sol.IsFree = 1)
- THEN 1
- ELSE 0
- END
- , LotSerialNbr = ISNULL(ints.LotSerialNbr, '')
- , ExpiredDate = ints.ExpireDate
- , ints.SiteID
- , ints.LocationID
- , InQty = CASE
- WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt > 0) ---- Nhập mua
- OR (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt = 0) ---- Nhập khuyến mãi
- OR (ints.TranType = 'RCP' AND inr.OrigModule = 'IN') ---- Nhập khác
- OR (ints.TranType = 'TRX' AND ints.InvtMult = 1) ---- Chuyển kho - nhập
- OR (ints.TranType = 'ADJ' AND ints.InvtMult = 1) ---- Điều chỉnh tăng kho
- OR (ints.TranType = 'RET' ) ---- Nhập trả
- OR (ints.TranType = 'CRM' AND sol.IsFree = 0) ---- Nhập khách hàng trả
- OR (ints.TranType = 'CRM' AND sol.IsFree = 1) ---- Nhập khách hàng trả KM
- THEN ints.BaseQty
- WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NOT NULL) ---- Xuất trả ncc
- THEN ints.BaseQty * -1
- ELSE 0
- END
- , OutQty = CASE
- WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NULL) ---- Xuất khác
- OR (ints.TranType = 'TRX' AND ints.InvtMult = -1) ---- Chuyển kho - xuất
- OR (ints.TranType = 'ADJ' AND ints.InvtMult = -1) ---- Điều chỉnh giảm kho
- OR (ints.TranType = 'DRM' ) ---- Xuất debit
- OR (ints.TranType = 'INV' AND sol.IsFree = 0) ---- Xuất bán
- OR (ints.TranType = 'INV' AND sol.IsFree = 1) ---- Xuất khuyến mãi
- OR (ints.TranType = 'INV' AND sol.IsFree = 1 AND sol.UsrIncentiveID IS NOT NULL) ---- Xuất trả thưởng
- THEN ints.BaseQty
- ELSE 0
- END
- , ReasonCodeDescr = CASE
- ---- Nhập
- WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND porl.LineAmt > 0) ---- Nhập mua
- OR (ints.TranType = 'III' AND por.ReceiptNbr IS NOT NULL) ---- Xuất trả ncc
- OR (ints.TranType = 'RCP' AND inr.OrigModule = 'PO' AND ( porl.LineAmt = 0 OR porl.ManualDisc = 1)) ---- Nhập khuyến mãi
- THEN 'PurchaseReceipt' ---- Gom chung là nhập mua
- WHEN (ints.TranType = 'RCP' AND inr.OrigModule = 'IN') ---- Nhập khác
- OR (ints.TranType = 'TRX' AND ints.InvtMult = 1) ---- Chuyển kho - nhập
- OR (ints.TranType = 'ADJ' AND ints.InvtMult = 1) ---- Điều chỉnh tăng kho
- OR (ints.TranType = 'RET' ) ---- Nhập trả
- OR (ints.TranType = 'CRM' AND soo.UsrIsDispose = 1) --- Nhập khách hàng trả nhưng tào lao
- THEN 'OtherReceipt' ---- Gom chung là nhập khác
- WHEN ints.TranType = 'CRM' AND sol.IsFree = 0
- THEN 'CustomerReturn' ---- Nhập khách hàng trả
- WHEN ints.TranType = 'CRM' AND sol.IsFree = 1
- THEN 'CustomerPromotionReturn' ---- Nhập khách hàng trả KM
- ---- Xuất
- WHEN (ints.TranType = 'III' AND por.ReceiptNbr IS NULL) ---- Xuất khác
- OR (ints.TranType = 'TRX' AND ints.InvtMult = -1) ---- Chuyển kho - xuất
- OR (ints.TranType = 'ADJ' AND ints.InvtMult = -1) ---- Điều chỉnh giảm kho
- OR (ints.TranType = 'DRM' ) ---- Xuất debit
- THEN 'OtherIssue' ---- Gom chung là xuất khác
- WHEN ints.TranType = 'INV' AND sol.IsFree = 0
- THEN 'SalesIssue' ---- Xuất bán
- WHEN ints.TranType = 'INV' AND sol.IsFree = 1
- THEN 'PromotionIssue' ---- Xuất khuyến mãi
- WHEN ints.TranType = 'INV' AND sol.IsFree = 1 AND sol.UsrIncentiveID IS NOT NULL
- THEN 'IncentiveIssue' ---- Xuất trả thưởng
- END
- , intr.SOOrderNbr
- , intr.SOOrderType
- FROM
- dbo.INRegister inr WITH (NOLOCK)
- JOIN dbo.INTran intr WITH (NOLOCK) ON intr.CompanyID = inr.CompanyID
- AND intr.RefNbr = inr.RefNbr
- AND intr.DocType = inr.DocType
- JOIN dbo.INTranSplit ints WITH (NOLOCK) ON ints.CompanyID = intr.CompanyID
- AND ints.RefNbr = intr.RefNbr
- AND ints.DocType = intr.DocType
- AND ints.LineNbr = intr.LineNbr
- LEFT JOIN dbo.SOOrder soo WITH (NOLOCK) ON soo.CompanyID = intr.CompanyID
- AND soo.OrderNbr = intr.SOOrderNbr
- AND soo.OrderType = intr.SOOrderType
- LEFT JOIN dbo.SOLine sol WITH (NOLOCK) ON sol.CompanyID = intr.CompanyID
- AND sol.OrderNbr = intr.SOOrderNbr
- AND sol.OrderType = intr.SOOrderType
- AND sol.LineNbr = intr.SOOrderLineNbr
- LEFT JOIN dbo.POReceipt por WITH (NOLOCK) ON por.CompanyID = intr.CompanyID
- AND por.ReceiptNbr = intr.POReceiptNbr
- LEFT JOIN dbo.POReceiptLine porl WITH (NOLOCK) ON porl.CompanyID = intr.CompanyID
- AND porl.ReceiptNbr = intr.POReceiptNbr
- AND porl.LineNbr = intr.POReceiptLineNbr
- WHERE
- inr.CompanyID = 3
- AND inr.Released = 1
- AND (intr.SOOrderNbr IS NULL
- OR (soo.OrderType != 'CM' AND ISNULL(soo.UsrIsDispose, 0) = 0)
- OR (soo.OrderType = 'CM' AND ISNULL(soo.UsrIsRevenueDeduction, 0) = 1))
- AND inr.TranDate BETWEEN '2019-07-01' AND '2019-07-18'
- AND soo.OrderNbr = 'DH360-068186'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement