· 10 years ago · Aug 21, 2016, 03:06 PM
1drop function if exists "dbLegal".tbPInvVW_UserData ( in UserID int , in AccID int ) cascade;
2create or replace FUNCTION "dbLegal".tbPInvVW_UserData ( in UserID int , in AccID int )
3returns table (
4 id int -- ID of acestor
5 ,TranNo text -- varchar(20) null, -- comment 'เลขที่เà¸à¸à¸ªà¸²à¸£',
6 ,TranDate timestamp_wtz -- date -- null, -- comment 'วันที่ทำรายà¸à¸²à¸£à¸•ามเà¸à¸à¸ªà¸²à¸£' ,
7 ,Reference text -- null, -- comment 'à¸à¹‰à¸²à¸‡à¸à¸´à¸‡',
8 ,Description text -- null , -- เรื่à¸à¸‡
9
10 -- ,PReqID int
11
12 ,PayerType text
13 ,Payer int -- ลูà¸à¸„้า
14 , PayerName text -- ชื่à¸à¸¥à¸¹à¸à¸„้า
15 ,PayeeType text
16 ,Payee int -- null, -- comment 'ผู้รับ',
17 , PayeeName text
18 ,IncExp Boolean -- default 'Y' , --enum ('Y','N') comment 'รายรับ หรืภรายจ่าย' ,
19 ,TaskType int -- null , -- comment 'ประเภทงาน',
20 , TaskName text -- ชื่à¸à¸›à¸£à¸°à¹€à¸ ทงาน
21 ,SumItem smallint -- null , -- comment 'จำนวนรายà¸à¸²à¸£',
22 ,SumAmt Num -- null, -- float(19,4) null comment 'รวามจำนวนเงินทั้งหมด ' ,
23
24 ,SumPay num
25 ,SumBal num
26 ,v_tax num
27 ,v_amt num
28 ,whd_amt num
29
30 ,UID int -- not null, -- comment 'User DeName/ID',
31 ,ACC int -- not null,
32 ,TranType text
33 --* Workflow
34 ,Status text -- Status
35 ,StatusUpdate timestamp -- approve update
36
37 --* Extend
38 --,PInvID -- ID of decedant
39 ,CaseTranID int
40 ,LGNo text
41 ,CaseTitle text
42 ,LGStepProItemID int
43 ,LGStepProItemName text -- Active step
44 ,sum_collect num
45) AS $$
46
47 select
48 a.*
49 --* Extend
50 -- ,b.id
51 ,aa.CaseTranID
52 ,r.lgno -- r1.tvalue LGNo
53 ,r.title
54
55 ,aa.LGStepProItemID
56 ,s.title LGStepProItemName
57 ,aa.sum_collect
58 from
59 (
60
61 select
62 --a.id
63 a.id
64 ,a.TranNo -- varchar(20) null, -- comment 'เลขที่เà¸à¸à¸ªà¸²à¸£',
65 ,a.TranDate -- date null, -- comment 'วันที่ทำรายà¸à¸²à¸£à¸•ามเà¸à¸à¸ªà¸²à¸£' ,
66 ,a.Reference -- varchar(20) null, -- comment 'à¸à¹‰à¸²à¸‡à¸à¸´à¸‡',
67 ,a.Description -- varchar(100) null , -- comment 'รายละเà¸à¸µà¸¢à¸”',
68
69 -- ,aa.PReqID
70
71 ,a.PayerType
72 ,a.Payer -- int null, -- comment 'ผู้จ่าย',
73 ,concat_ws(' ',b.prefix,b.firstname, b.lastname, b.suffix ) PayerName
74 ,a.PayeeType
75 ,a.Payee -- int null, -- comment 'ผู้รับ',
76 ,concat_ws(' ',c.prefix,c.firstname, c.lastname, c.suffix ) PayeeName
77 ,a.IncExp -- Boolean default 'Y' , --enum ('Y','N') comment 'รายรับ หรืภรายจ่าย' ,
78 ,a.TaskType -- int null , -- comment 'ประเภทงาน',
79 ,f.tasktype TaskName -- ชื่à¸à¸›à¸£à¸°à¹€à¸ ทงาน
80 --,f.customstatus TaskStatus
81 --,f.workflowstatus TaskWorkFlow
82 ,a.SumItem -- smallint null , -- comment 'จำนวนรายà¸à¸²à¸£',
83 ,a.SumAmt -- Num null, -- float(19,4) null comment 'รวามจำนวนเงินทั้งหมด ' ,
84
85 ,a.SumPay
86 ,a.SumBal
87 ,a.v_tax
88 ,a.v_amt
89 ,a.whd_amt
90
91
92 ,a.UID -- int not null, -- comment 'User DeName/ID',
93 ,a.ACC -- int not null,
94 ,a.trantype
95 --* workflow
96 ,"dbSysWKF".wkfrecord_status(
97 f11.orderno
98 , f11.title
99 , f11.statusname
100 , app := f1.app
101 , StatusAlias :=true
102 ) Status
103
104 ,"dbSysWKF".wkfrecord_upd(f1.app) -- status update
105
106 from
107 "dbLegal".tbPInv a
108 -- join "dbFinance".tbPReq a on (a.id = aa.PReqID )
109 left join "dbContact".tbcontact b on ( b.id = a.payer )
110 left join "dbContact".tbcontact c on ( c.id = a.payee )
111 left join "dbBase".tbTaskType f on (f.id = a.tasktype )
112 --* wkf status
113 left join "dbSysWKF".tbpinv_wkfrec_dbLegal f1 on ( f1.pid = a.id and f1.active ) -- ** Active status
114 left join "dbSysWKF".tbwkfstat f11 on ( f11.id = f1.wkfstatid )
115
116 where
117 a.UID in (select a1.UID from "dbUserAcc".tbaccuser a1 where a1.PID = AccID )
118 order by a.tranno
119 ) a
120
121 left join "dbLegal".tbPinv aa on ( aa.id = a.id )
122
123 left join "dbLegal".tbCaseTran r on ( r.id = aa.CaseTranID )
124 -- left join "dbLegal".tbCaseTranNo r1 on ( r1.pid = aa.CaseTranID and r1.Title = 'LG' and r1.tcustom is null )
125 left join "dbLegal".tbLGStepProItem s on ( s.id = aa.LGStepProItemID )
126
127 ;
128
129$$ LANGUAGE sql;