Quantcast
Channel: SCN: Message List
Viewing all articles
Browse latest Browse all 10512

Help with editing a commisson query required - SAP B1

$
0
0

Hello SAP Experts,

 

I am quite new to SAP query writing and need help editing the query below which is used for calculating commissions. Right now we have a column 'Total Service Charges' which is currently populated with [ItmsGrpCode]='132'. We need 'Total Service Charges' to include [ItmsGrpCode] 132, 133, 134, 135, 137, 138, 139, 142 and 144.

 

I've tried editing this to get the desired result for a couple hours but no luck. Thank you in advance for your expertise and advice.

 

declare @FromDate as datetime

declare @ToDate as Datetime

declare @FrSlpName as varchar(100)

declare @ToSlpName as varchar(100)

 

Set @FromDate = (Select top 1 T0.DocDate

from OINV T0 where T0.DocDate >= '[%0]')

--set @FromDate = '2008-01-01'

Set @ToDate = (Select top 1 T0.DocDate

from OINV T0 where T0.DocDate >= '[%1]')

--set @ToDate = '2013-01-01'

 

 

Select

T2.[SlpName] AS 'Salesperson', T0.[DocDate]AS 'Posting Date',

'AR Invoice' as 'Document Type',

T0.[DocNum]As 'Document Number',

T0.[CardCode] AS 'Customer Code', T0.[CardName]AS 'Customer Name',T0.[VatSum]AS 'Total Tax',

T0.[TotalExpns] AS 'Total Freight',

isnull((SELECT SUM(isnull(T11.LineTotal,0))

FROM OINV T10 

INNER JOIN INV1 T11 ON T10.DocEntry = T11.DocEntry

INNER JOIN OITM T12 ON T11.ItemCode = T12.ItemCode

WHERE T10.[DocNum] = T0.DocNum and  T12.[ItmsGrpCod] ='132'),0) AS 'Total Service Charges',

T0.[DpmAmnt]AS 'Total Down Payment',T0.[PaidSum] AS 'Total Applied Amount',T0.[DocTotal] AS 'Total Document Amount', T2.[Commission] AS 'Commission Percentage',(

T0.DocTotal + T0.[DpmAmnt]- T0.VatSum - T0.TotalExpns) AS 'Commisionable Amt Work',

T2.[Commission], T0.[U_CRS_COMPAID]

--(T2.[Commission]*(T0.DocTotal - T0.VatSum - T0.TotalExpns)/100) AS 'Commission Earned'

into #temp1

FROM OINV T0 

INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode

WHERE T0.DocDate >= @FromDate and T0.DocDate <= @ToDate and

T0.DocType = 'I' and T0.[DocStatus] ='C' and  T0.[PaidSum] = T0.[DocTotal] and ISNULL(T0.[U_CRS_COMPAID],'') = 'N' and

isnull((SELECT count(T11.[ItmsGrpCod]) FROM INV1 T10  INNER JOIN OITM T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[DocEntry] = T0.[DocEntry] and T11.[ItmsGrpCod] = 132),0) <>

isnull((SELECT count(T11.[ItmsGrpCod]) FROM INV1 T10  INNER JOIN OITM T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[DocEntry] = T0.[DocEntry]),0)

 

--and T0.[DocNum] = '103086'

GROUP BY T2.[SlpName], T0.[DocDate], T0.[DocNum], T0.[CardCode], T0.[CardName],

T0.[VatSum],T0.[TotalExpns], T0.[PaidSum],T0.[DocTotal], (T0.DocTotal + T0.[DpmAmnt] - T0.VatSum - T0.TotalExpns), T2.[Commission],T0.[U_CRS_COMPAID], T0.[DpmAmnt]

 

union all

 

Select

T2.[SlpName] AS 'Salesperson', T0.[DocDate]AS 'Posting Date',

'AR Credit Memo' as 'Document Type',

T0.[DocNum]As 'Document Number',

T0.[CardCode] AS 'Customer Code', T0.[CardName]AS 'Customer Name',T0.[VatSum]*-1 AS 'Total Tax',

T0.[TotalExpns]*-1 AS 'Total Freight',

isnull((SELECT SUM(isnull(T11.LineTotal,0))

FROM ORIN T10 

INNER JOIN RIN1 T11 ON T10.DocEntry = T11.DocEntry

INNER JOIN OITM T12 ON T11.ItemCode = T12.ItemCode

WHERE T10.[DocNum] = T0.DocNum and  T12.[ItmsGrpCod] ='132'),0)*-1 AS 'Total Service Charges',

T0.[DpmAmnt] AS 'Total Down Payment', T0.[PaidSum]*-1 AS 'Total Applied Amount',T0.[DocTotal]*-1 AS 'Total Document Amount',

T2.[Commission] AS 'Commission Percentage',

(T0.DocTotal + T0.[DpmAmnt] - T0.VatSum - T0.TotalExpns)*-1 AS 'Commisionable Amt Work',

T2.[Commission] as 'Commission', T0.[U_CRS_COMPAID]

--(T2.[Commission]*(T0.DocTotal - T0.VatSum - T0.TotalExpns)/100) AS 'Commission Earned'

FROM ORIN T0 

INNER JOIN OSLP T2 ON T0.SlpCode = T2.SlpCode

WHERE T0.DocDate >= @FromDate and T0.DocDate <= @ToDate and

T0.DocType = 'I' and T0.[DocStatus] ='C' and  T0.[PaidSum] = T0.[DocTotal] and ISNULL(T0.[U_CRS_COMPAID],'') = 'N' and

isnull((SELECT count(T11.[ItmsGrpCod]) FROM RIN1 T10  INNER JOIN OITM T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[DocEntry] = T0.[DocEntry] and T11.[ItmsGrpCod] = 132),0) <>

isnull((SELECT count(T11.[ItmsGrpCod]) FROM RIN1 T10  INNER JOIN OITM T11 ON T10.ItemCode = T11.ItemCode

WHERE T10.[DocEntry] = T0.[DocEntry]),0)

 

--and T0.[DocNum] = '103086'

GROUP BY T2.[SlpName], T0.[DocDate], T0.[DocNum], T0.[CardCode], T0.[CardName],

T0.[VatSum],T0.[TotalExpns], T0.[PaidSum],T0.[DocTotal], (T0.DocTotal + T0.[DpmAmnt] - T0.VatSum - T0.TotalExpns), T2.[Commission], T0.[U_CRS_COMPAID],T0.[DpmAmnt]

 

Select T0.*, T0.[Commisionable Amt Work] - T0.[Total Service Charges] as 'Commissionable Amt',

((T0.[Commisionable Amt Work] - T0.[Total Service Charges])*T0.[Commission])/100 as 'Commision Earned'

into #temp2

from #temp1 T0

 

--Select T0.* from #temp2 t0

 

Select T0.[Salesperson], T0.[Posting Date], T0.[Document Type], T0.[Document Number], T0.[Customer Code],

T0.[Customer Name], T0.[Total Tax], T0.[Total Freight], T0.[Total Service Charges], T0.[Total Down Payment], T0.[Total Applied Amount],

T0.[Total Document Amount], T0.[Commissionable Amt], T0.Commission, T0.[Commision Earned], T0.[U_CRS_COMPAID]

from #temp2 T0

 

drop table #temp1

drop table #temp2


Viewing all articles
Browse latest Browse all 10512


<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>