0% found this document useful (0 votes)
8 views4 pages

Cust Conta NCR Report

Uploaded by

nidhigauravshah3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views4 pages

Cust Conta NCR Report

Uploaded by

nidhigauravshah3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

ALTER procedure [dbo].

[USP_CRM_CustomerContact_Report]

--Declare
@BranchID int,
@CompanyID int,
@YearId int,
@FromDate datetime,
@ToDate datetime

As

--set @BranchID = 4
--set @FromDate = '01-Jan-2024'
--set @ToDate = getdate()

BEGIN
with vView as (
--- Lead ----
SELECT distinct row_number() over (Order by d.dtDocDate) as
RowNumber,d.lId,d.sDocNo as [Lead No],d.dtDocDate,a1.sName as [Customer Name],
emp.sRemarks as [Sales Person],
dt.sName as [Transaction Type],d.sDocNo as [Transaction No]
,ST.sName AS [Transaction Status]
,cl.sName as [Contact Person],cl.sMobile as [Contact No.],
case when cl.cFlag='N' then 'Note' else isnull(ct.sName,'') end as Activity,
case when em.lId>0 then 'Email' end As Email,case when cl.cFlag='N' then cl.sNarr
else cl.sSubject end as Subject,cl.lStatusHdr,
convert(varchar,convert(date,convert(varchar(20),cl.dtFromDate)),103)as [Schedule
Date],
case when cl.cFlag='N' then '' else cl.sNarr end as Remarks,
case when (cl.lStatus != 40 and cl.lStatus != 50 and cl.dtFromDate <
DateAdd(Minute,330,GETUTCDATE()) and cl.cFlag in ('C')) then 'Overdue'
when cl.lStatus=10 then 'Done' when cl.lStatus=20 then 'Process' when cl.lStatus=30
then 'Hold' when cl.lStatus=30 then 'Done' when cl.lStatus=50 then 'Closed' else ''
end as [Status of Activity],
Convert(decimal(18,2),isnull(cf.cfValue1,0)) as Amount,cnt.sEmailId as [Email
ID],cm.sName as Currency

from TXNTYP as dt inner join TXNHDR as d on dt.lTypId=d.lTypId


left join BUSMST as a1 on d.lAccId1=a1.lId
left join USRMST as emp on d.lEmpId=emp.lId
--left join BUSCNT as bc On bc.lId = a1.lId
left join TXNSTAT as st on st.lId=d.lId
left join CURMST cm on cm.lid=d.lCurrId
left join CALLLOG as cl on cl.lId=d.lId and cl.lStatusHdr=st.lStatusId
left join CALLTYP ct on ct.lId=cl.lActTyp
left join TXNCOM em on em.lId=d.lId and lBusId!=0 and cl.lStatusHdr=em.lStatusHdr
left join (select cf.lId,Max(Case when cf.lFieldNo=29 then cf.sValue Else 0 End)
as cfValue1
from TXNCF as cf where cf.lLine=0 Group by cf.lId) as cf on d.lId=cf.lId
left join txncnt cnt on cnt.sName=cl.sName and cnt.lId=cl.lId

where d.lId>0 and d.bDel=0 and


d.lCompId=@BranchID
and (CONVERT(date, CONVERT(VARCHAR(10), CONVERT(varchar(50), d.dtDocDate, 105),
23)) between convert(date,@fromdate,101) and convert(date,@todate,101))
and dt.lTypId = 421
and (isnull(st.lUsrId,0) > 0 and st.lUsrDel is null)
-- --- Opportunity ----
union
SELECT distinct row_number() over (Order by d.dtDocDate) as
RowNumber,hdro.lId,d.sDocNo as [Lead No],d.dtDocDate,a1.sName as [Customer
Name],emp.sRemarks as [Sales Person],
dto.sName as [Transaction Type],hdro.sDocNo as [Transaction No],ST.sName AS
[Transaction Status],cl.sName as [Contact Person],cl.sMobile as [Contact No.],
case when cl.cFlag='N' then 'Note' else ct.sName end as Activity,
case when em.lId>0 then 'Email' end As Email,case when cl.cFlag='N' then cl.sNarr
else cl.sSubject end as Subject,cl.lStatusHdr,
convert(varchar,convert(date,convert(varchar(20),cl.dtFromDate)),103)as [Schedule
Date],
case when cl.cFlag='N' then '' else cl.sNarr end as Remarks,
case when cl.lStatus=10 then 'Done' when cl.lStatus=20 then 'Process' when
cl.lStatus=30 then 'Hold' when cl.lStatus=30 then 'Done' when cl.lStatus=50 then
'Closed' else '' end as [Status of Activity]
,Convert(decimal(18,2),isnull(cf.cfValue1,0)) as Amount,cnt.sEmailId as [Email
ID],cm.sName as Currency

from TXNTYP as dt inner join TXNHDR as d on dt.lTypId=d.lTypId


left join BUSMST as a1 on d.lAccId1=a1.lId
--left join BUSCNT as bc On bc.lId = a1.lId
left join txncf deto on deto.lValue=d.lId and deto.lFieldNo=21 and deto.lLine=0
left join txnhdr hdro on hdro.lid=deto.lId
left join TXNTYP as dto on dto.lTypId=hdro.lTypId
left join USRMST as emp on hdro.lEmpId=emp.lId
left join TXNSTAT as st on st.lId=hdro.lId
left join CALLLOG as cl on cl.lId=hdro.lId and cl.lStatusHdr=st.lStatusId
left join CALLTYP ct on ct.lId=cl.lActTyp
left join CURMST cm on cm.lid=hdro.lCurrId
left join TXNCOM em on em.lId=hdro.lId and lBusId!=0 and
cl.lStatusHdr=em.lStatusHdr
left join (select cf.lId,Max(Case when cf.lFieldNo=29 then cf.sValue Else 0 End)
as cfValue1
from TXNCF as cf where cf.lLine=0 Group by cf.lId) as cf on d.lId=cf.lId
left join txncnt cnt on cnt.sName=cl.sName and cnt.lId=cl.lId
where hdro.lId>0 and hdro.bDel=0
and hdro.lCompId=@BranchID
and (CONVERT(date, CONVERT(VARCHAR(10), CONVERT(varchar(50), hdro.dtDocDate,
105), 23)) between convert(date,@fromdate,101) and convert(date,@todate,101))
and dt.lTypId = 421 and hdro.lTypId=513 and (isnull(st.lUsrId,0) > 0 and
st.lUsrDel is null)

-- --- Quots ----


union
SELECT distinct row_number() over (Order by d.dtDocDate) as
RowNumber,hdrq.lId,d.sDocNo as [Lead No],d.dtDocDate,a1.sName as [Customer
Name],emp.sRemarks as [Sales Person],
dto.sName as [Transaction Type],hdrq.sDocNo as [Transaction No],ST.sName AS
[Transaction Status],cl.sName as [Contact Person],cl.sMobile as [Contact No.],
case when cl.cFlag='N' then 'Note' else isnull(ct.sName,'') end as Activity,
case when em.lId>0 then 'Email' end As Email,case when cl.cFlag='N' then cl.sNarr
else cl.sSubject end as Subject,cl.lStatusHdr,
convert(varchar,convert(date,convert(varchar(20),cl.dtFromDate)),103)as [Schedule
Date],
case when cl.cFlag='N' then '' else cl.sNarr end as Remarks,
case when cl.lStatus=10 then 'Done' when cl.lStatus=20 then 'Process' when
cl.lStatus=30 then 'Hold' when cl.lStatus=30 then 'Done' when cl.lStatus=50 then
'Closed' else '' end as [Status of Activity]
,Convert(decimal(18,2),isnull(hdrq.dTotal,0)) as Amount,cnt.sEmailId as [Email
ID],cm.sName as Currency

from TXNTYP as dt inner join TXNHDR as d on dt.lTypId=d.lTypId


left join BUSMST as a1 on d.lAccId1=a1.lId
--left join BUSCNT as bc On bc.lId = a1.lId
left join txncf deto on deto.lValue=d.lId and deto.lFieldNo=21 and deto.lLine=0
left join txnhdr hdro on hdro.lid=deto.lId
left join txncf detq on detq.lValue=hdro.lId and deto.lFieldNo=21 and deto.lLine=0
left join txnhdr hdrq on hdrq.lid=detq.lId
left join CURMST cm on cm.lid=hdrq.lCurrId

left join TXNTYP as dto on dto.lTypId=hdrq.lTypId


left join USRMST as emp on hdrq.lEmpId=emp.lId
left join TXNSTAT as st on st.lId=hdrq.lId
left join CALLLOG as cl on cl.lId=hdrq.lId and cl.lStatusHdr=st.lStatusId
left join CALLTYP ct on ct.lId=cl.lActTyp
left join TXNCOM em on em.lId=hdrq.lId and lBusId!=0 and
cl.lStatusHdr=em.lStatusHdr
left join txncnt cnt on cnt.sName=cl.sName and cnt.lId=cl.lId

where hdrq.lId>0 and hdrq.bDel=0


and hdrq.lCompId=@BranchID
and (CONVERT(date, CONVERT(VARCHAR(10), CONVERT(varchar(50), hdrq.dtDocDate,
105), 23)) between convert(date,@fromdate,101) and convert(date,@todate,101))
and dt.lTypId = 421 and hdro.lTypId=513 and hdrq.lTypId=418 and
(isnull(st.lUsrId,0) > 0 and st.lUsrDel is null)

-- --- Order ----


union
SELECT distinct row_number() over (Order by d.dtDocDate) as
RowNumber,hdror.lId,d.sDocNo as [Lead No],d.dtDocDate,a1.sName as [Customer
Name],emp.sRemarks as [Sales Person],
dto.sName as [Transaction Type],hdror.sDocNo as [Transaction No],ST.sName AS
[Transaction Status],cl.sName as [Contact Person],cl.sMobile as [Contact No.],
case when cl.cFlag='N' then 'Note' else ct.sName end as Activity,
case when em.lId>0 then 'Email' end As Email,case when cl.cFlag='N' then cl.sNarr
else cl.sSubject end as Subject,cl.lStatusHdr,
convert(varchar,convert(date,convert(varchar(20),cl.dtFromDate)),103)as [Schedule
Date],
case when cl.cFlag='N' then '' else cl.sNarr end as Remarks,
case when cl.lStatus=10 then 'Done' when cl.lStatus=20 then 'Process' when
cl.lStatus=30 then 'Hold' when cl.lStatus=30 then 'Done' when cl.lStatus=50 then
'Closed' else '' end as [Status of Activity]
,Convert(decimal(18,2),isnull(hdror.dTotal,0)) as Amount,cnt.sEmailId as [Email
ID],cm.sName as Currency

from TXNTYP as dt inner join TXNHDR as d on dt.lTypId=d.lTypId


left join BUSMST as a1 on d.lAccId1=a1.lId
--left join BUSCNT as bc On bc.lId = a1.lId
left join txncf deto on deto.lValue=d.lId and deto.lFieldNo=21 and deto.lLine=0
left join txnhdr hdro on hdro.lid=deto.lId
left join txncf detq on detq.lValue=hdro.lId and deto.lFieldNo=21 and deto.lLine=0
left join txnhdr hdrq on hdrq.lid=detq.lId
left join txncf detor on detor.lValue=hdrq.lId and detor.lFieldNo=1 and
detor.lLine=0
left join txnhdr hdror on hdror.lid=detor.lId
left join TXNTYP as dto on dto.lTypId=hdror.lTypId
left join USRMST as emp on hdror.lEmpId=emp.lId
left join TXNSTAT as st on st.lId=hdror.lId
left join CALLLOG as cl on cl.lId=hdror.lId and cl.lStatusHdr=st.lStatusId
left join CALLTYP ct on ct.lId=cl.lActTyp
left join TXNCOM em on em.lId=hdror.lId and lBusId!=0 and
cl.lStatusHdr=em.lStatusHdr
left join txncnt cnt on cnt.sName=cl.sName and cnt.lId=cl.lId
left join CURMST cm on cm.lid=hdror.lCurrId

where hdror.lId>0 and hdror.bDel=0


and hdrq.lCompId=@BranchID
and (CONVERT(date, CONVERT(VARCHAR(10), CONVERT(varchar(50), hdror.dtDocDate,
105), 23)) between convert(date,@fromdate,101) and convert(date,@todate,101))
and dt.lTypId = 421 and hdro.lTypId=513 and hdrq.lTypId=418 and hdror.lTypId=399
)

Select D.[Lead No] as [Lead No],[Customer Name], [Sales Person],[Transaction


Type],[Transaction No],
--dbo.F_AddThousandSeparators(convert(varchar, convert(decimal(18, 2), Amount),
1)) As
Amount,Currency,[Transaction Status],[Contact Person],
[Contact No.],[Email ID],
Activity,Subject,[Schedule Date],
Remarks,[Status of Activity]
from vView as d

--where [Lead No]='LD/096/24'


order by [Lead No],lId,isnull(lStatusHdr,1000)

ENd

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy