· 6 years ago · Aug 20, 2019, 05:06 AM
1/******************************************
2 View : Simple form III
3
4 Wut, 2019-06-22
5
6*******************************************/
7 -- * Data selected for underlying user
8drop function if exists "dbLegal".trCaseTrn040VW_UserData ( in UserID int , in AccID int
9 ,in _id int
10) cascade;
11drop FUNCTION if exists "dbLegal".trCaseTrn040VW_UserData ( in UserID int , in AccID int
12 ,in _id int
13 ,in _assignment boolean
14 ,in _casekind smallint
15
16
17) cascade;
18drop FUNCTION if exists "dbLegal".trCaseTrn040VW_UserData ( in UserID int , in AccID int
19 ,in _id int
20 ,in _assignment boolean
21 ,in _casekind smallint
22
23
24) cascade;
25drop FUNCTION if exists "dbLegal".trCaseTrn040VW_UserData ( in UserID int , in AccID int
26 ,in _id int
27 ,in _assignment boolean
28 ,in _casekind smallint
29
30) cascade;
31DROP FUNCTION if exists "dbLegal".trCaseTrn040vw_userdata(integer, integer, integer, boolean, smallint, text) cascade;
32
33create or replace FUNCTION "dbLegal".trCaseTrn040VW_UserData ( in UserID int , in AccID int
34 ,in _id int = null
35 ,in _assignment boolean = false -- assignment
36 ,in _casekind smallint = null ::smallint
37 ,in _wkfrec_name text = null
38
39)
40returns table (
41 id int
42
43 ,ReceiveNo text -- varchar -- b.Tvalue เลขที่รับเรื่อง -- ReceiveNo
44 ,Title text -- ,a.Title เรื่อง
45 ,ReceiveDate timestamp_wtz -- วันที่รับเรื่อง -- ReceiveDate
46 ,CustID int
47 ,CustName text -- concat_ws(' ',i1.Prefix, i1.FirstName ,i1.LastName, i1.Suffix, '-', i1.ID )ลูกค้า -- Customer
48 ,Defendant boolean -- ฝ่าย -- Defendant vs Complaintor
49 ,CourtRole text
50
51 ,ReceiverID int
52 ,ReceiverName text -- concat_ws(' ', da.Firstname,da.Lastname,'-', d.TValue ) ผู้รับเรื่อง -- ReceiveBy
53 ,OrgID int -- 10
54 ,OrgName text -- concat_ws(' ',f1.Prefix, f1.FirstName ) หน่วยงาน -- Org
55 ,LawOfficerID int
56 ,LawOfficerName text -- concat_ws(' ',h1.Prefix, h1.FirstName ,h1.LastName, h1.Suffix, '-', h1.ID ) นิติกร -- LawOfficer
57 ,lawofficer_contactid int
58 ,LawOfficeID int
59 ,LawOfficeName text -- concat_ws(' ',o1.Prefix, o1.FirstName ,o1.LastName, o1.Suffix, '-', o1.ID ) สำนักงานทนาย-- LawOffice
60 ,LawyerID int
61 ,LawyerName text -- concat_ws(' ',p1.Prefix, p1.FirstName ,p1.LastName, p1.Suffix, '-', p1.ID ) ทนาย
62 ,LGTypeID int
63 ,LGTypeName text -- e.Title ประเภทคดี --LGType
64 ,ProfileID int -- 20
65 ,ProfileName text -- concat_ws(' ',e1.Title,'-',e1.ID) โปรไฟล์คดี --- LegalProfile
66 ,ActiveStepID int
67 ,ActiveStepName text -- concat_ws(' ',l1.Title,'-',l1.id) ขั้นตอนคดีที่แอคทีพ -- ที่กำลังดำเนินการ Active step
68 ,LGNo text -- varchar -- b1.TValue LGNo
69 ,TranDate timestamp_wtz -- วันที่บันทึกข้อมูล
70 ,TranDate_Dis timestamp_wtz -- วันที่ตั้งคดี = วันที่จ่ายคดีให้ สนง. ทนาย
71 ,BlackNo text -- varchar -- j.TValue คดีดำ --BlackNo
72 ,RedNo text -- varchar -- k.TValue คดีแดง -- RedNo
73 -- ,CalenNameID int
74 ,UID int
75 ,ACC int -- 30
76 ,ACNo text
77
78 ,Status text
79 ,wkfstatid int
80 ,CaseComplete "dbSys".Percent
81 ,CaseComplete_date timestamp_wtz
82 ,CalTaskID int
83 ,amt_total num -- จำนวนเงิน ทุุนทรัพย์
84 ,CourtID int -- updatable
85 ,CourtName text
86 ,CourtAreaName text
87 ,sue_date timestamp_wtz
88 ,BlackNoBD text
89 ,RedNoBD text -- 40
90 ,Groups text
91 ,remark text
92 ,c_defd smallint -- Count of defendant
93 ,docid int
94
95 ,CaseStatusID int
96 ,CaseStatusName text
97 ,LawCase text
98 ,defendant1_id int
99 ,Defendant1_Name text
100 ,docno_client text -- เลชที่แฟ้มคดี ของลูกค้า
101
102 ,keyword text
103 -- ,casetranid int
104 ,auto_copy boolean
105 ,casekind smallint
106 ,assign_lawoff boolean
107 ,taskid int
108 --,casetranid int
109 ,upddt timestamp_wtz
110 ,updateamount boolean
111 ,eleadevent_enable boolean
112 ,assettypeid int
113 ,assettype text
114 ,refid int
115 ,reflgno text
116 ,reftrandate timestamp_wtz
117 ,bankid int
118 ,BankName text
119 ,BankBranchID int
120 ,BankBranchName text
121 ,tgCalculate boolean
122 ,tgPrintForm boolean
123 ,caseasset_gen boolean
124 ,db_reconcile_date timestamp_wtz -- นัดไกล่เกลี่ย
125 ,db_testify_date timestamp_wtz -- นัดสืบพยาน จำเลย
126 --* Simple III ---------------------------------------------------------------------
127 -- ,db_contractno text
128 ,OrgResponseID int
129 ,OrgResponse text
130 ,db_testify_comp_date timestamp_wtz -- นัสสืบพยานโจทก์ , sysevent = TestifyComp
131
132 ,db_case_outcome text -- ผลพิพากาษ ศาลขั้นต้น , Judge
133 -- , db_case_outcome_id text
134 ,db_case_outcome_date timestamp_wtz
135 ,db_case_outcome_abbr text
136 --* ศาลอุทธรณ์ --------
137 ,db_App2_date timestamp_wtz -- ครบกำหนด
138 ,db_app2_pp1_date timestamp_wtz -- ขอขยาย
139 ,db_app2_enroll_date timestamp_wtz -- ยื่นเรื่อง
140 ,db_app2_amend_date timestamp_wtz -- ยื่นแก้ไข
141 ,db_app2_outcome text -- ผลพิพากษา ประเภท
142 ,db_app2_outcome_date timestamp_wtz -- ผลพิพากษา วันที่
143 ,db_App2_outcome_abbr text -- คำพิพากษาย่อ
144
145 --* ศาลฎีกา---------------------
146 ,db_App3_date timestamp_wtz -- ครบกำหนด
147 ,db_app3_appv_date timestamp_wtz -- ขออนุมัติ
148 ,db_app3_pp1_date timestamp_wtz -- ขอขยาย
149 ,db_app3_enroll_date timestamp_wtz -- ยื่นเรื่อง
150 ,db_app3_amend_date timestamp_wtz -- ยื่นแก้ไข
151 ,db_app3_outcome text -- ผลพิพากษา ประเภท
152 ,db_app3_outcome_date timestamp_wtz -- ผลพิพากษา วันที่
153 ,db_App3_outcome_abbr text -- คำพิพากษาย่อ
154 --* Enforcement
155 ,db_VerdExpire_date timestamp_wtz
156
157) AS $$
158declare
159-- v_id int = _id;
160-- v_assignment boolean = _assignment;
161-- v_casekind smallint = _casekind;
162 --v_wkfrec_name text = _wkfrec_name;
163 v_wkfrec_name text = format('"dbSysWKF".trcasetrn040vw%s_wkfrec',accid) ;
164begin
165
166 return query execute $x$
167 select
168 a.*
169 --* Simple III --------------------------------------
170 ,a21.orgid OrgResponseID
171 --,a211._name OrgResponse
172 ,"dbSys".grpTitles(
173 null
174 , a211.idgrp --
175 , '"dbUserReg".tbuserreg'
176 ,_sql := ' select a.id,b._name title , a.idgrp from %s a left join "dbContact".tbcontact_fullname(a.contactid) b on b._id = a.contactid where a.id = $1'
177 ,CH_delimeter := '<'
178 ,check_exist:= true
179 ) OrgResponse
180 ,aa22.startdate db_testify_comp_date
181
182 -- ,a43.title db_case_outcome
183 --* 1st court
184 ,a4.sysstep db_case_outcome
185 ,a41.startdate db_case_outcome_date
186 ,a42.description db_case_outcome_abbr
187
188 --* ศาลอุทธรณ์ --------
189 ,aa23.startdate ::timestamp_wtz db_App2_date -- ครบกำหนด
190 ,aa24.startdate db_app2_pp1_date -- ขอขยาย
191 ,aa25.startdate db_app2_enroll_date -- ยื่นเรื่อง
192 ,aa26.startdate db_app2_amend_date -- ยื่นแก้ไข
193 ,aa27.sysstep db_app2_outcome -- ผลพิพากษา ประเภท
194 ,aa271.startdate db_app2_outcome_date -- ผลพิพากษา วันที่
195 ,aa272.description db_App2_outcome_abbr -- คำพิพากษาย่อ
196
197 --* ศาลฎีกา --------
198 ,cc23.startdate ::timestamp_wtz db_App3_date -- ครบกำหนด
199 ,null::timestamp_wtz db_app3_appv_date -- ขออนุมัติ
200 ,cc24.startdate db_App3_pp1_date -- ขอขยาย
201 ,cc25.startdate db_App3_enroll_date -- ยื่นเรื่อง
202 ,cc26.startdate db_App3_amend_date -- ยื่นแก้ไข
203 ,cc27.sysstep db_App3_outcome -- ผลพิพากษา ประเภท
204 ,cc271.startdate db_App3_outcome_date -- ผลพิพากษา วันที่
205 ,cc272.description db_App3_outcome_abbr -- คำพิพากษาย่อ
206
207 ,aa4.startdate db_VerdExpire_date
208
209 from "dbLegal".trcasetranVW_UserData (
210 $3 -- userid
211 ,$2 -- accid
212 ,_id := $1-- v_id
213 ,_assignment := $4 -- v_assignment
214 ,_casekind := $5 -- v_casekind -- 1::smallint
215 ,_wkfrec_name := -- v_wkfrec_name
216 case
217 when $6 is not null then $6 -- _wkfrec_name
218 else case
219 when "dbSys".havetable( $7 ) then $7 -- v_wkfrec_name
220 else null
221 end
222 end
223 ) a
224
225 --* Simple III --------------------------------------------------------------------------
226 -- select * from "dbLegal".trcasetrancont where acc=179; --
227
228 left join "dbLegal".trcasetranorg a21 on a21.pid = a.id and a21.orgrole = 'InCharge' and a21.customrole is null
229 left join "dbHR".tbOrg a212 on a212.id = a21.orgid
230 left join "dbUserReg".tbuserreg a211 on a211.contactid = a212.contactid and not a211.deleted
231
232 left join "dbLegal".tbcasetranevent_getIII( a.id ,'TestifyComp' ) aa22 on aa22._pid = a.id
233 left join "dbLegal".trcasetranstep a4 on a4.pid = a.id and a4.selected and (a4.sysstep in ('Judge', 'Compromise', 'Withdraw' ) )
234 left join "dbLegal".trcasetranevent a41 on a41.pid = a.id and a41.sysevent = a4.sysstep
235 left join "dbCalen".tbCalTask a42 on a42.ID = a4.caltaskid
236
237 --* 2nd court
238 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App2_Due' ) aa23 on aa23._pid = a.id
239 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App2_Postpone' ) aa24 on aa24._pid = a.id
240 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App2_Enroll' ) aa25 on aa25._pid = a.id
241 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App2_Amend' ) aa26 on aa25._pid = a.id
242 left join "dbLegal".trcasetranstep aa27 on aa27.pid = a.id and aa27.selected and (aa27.sysstep in ('Insist2', 'Dissolve2', 'Amend2' ) )
243 left join "dbLegal".trcasetranevent aa271 on aa271.pid = a.id and aa271.sysevent = aa27.sysstep
244 left join "dbCalen".tbCalTask aa272 on aa272.ID = aa27.caltaskid
245
246 --* 3rd court
247 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App3_Due' ) cc23 on cc23._pid = a.id
248 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App3_Postpone' ) cc24 on cc24._pid = a.id
249 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App3_Enroll' ) cc25 on cc25._pid = a.id
250 left join "dbLegal".tbcasetranevent_getIII( a.id ,'App3_Amend' ) cc26 on cc25._pid = a.id
251 left join "dbLegal".trcasetranstep cc27 on cc27.pid = a.id and cc27.selected and (cc27.sysstep in ('Insist3', 'Dissolve3', 'Amend3' ) )
252 left join "dbLegal".trcasetranevent cc271 on cc271.pid = a.id and cc271.sysevent = cc27.sysstep
253 left join "dbCalen".tbCalTask cc272 on cc272.ID = cc27.caltaskid
254
255 --* enforcement
256 left join "dbLegal".tbcasetranevent_getIII( a.id ,'VerdExpire' ) aa4 on aa4._pid = a.id
257
258
259 where
260
261 case
262 when $1 is null then a.acc=$2
263 else a.id = $1
264 end
265 $x$ using _id,accid ,userid -- 1,2,3
266 ,_assignment -- 4
267 ,_casekind --5
268 ,_wkfrec_name --6
269 ,v_wkfrec_name --7
270 ;
271
272
273end; $$ LANGUAGE plpgsql stable parallel safe;
274
275
276
277-- * How to update underlying tables whose data is projected to this view
278drop function if exists "dbLegal".trCaseTrn040VW_UpdData() cascade;
279create or replace function "dbLegal".trCaseTrn040VW_UpdData()
280returns trigger as $$
281-- declare assettype record;caseasset record;
282begin
283 if (TG_OP = 'INSERT') then
284
285
286 --* default 1st asset
287 perform "dbLegal".trcasetranasset_add_item( new.id, new.uid, new.acc );
288
289 --* Simple III--------------------------------------------------------------------------------
290 --*
291 if new.db_testify_comp_date is not null then
292 -- raise '0229: new.db_testify_comp_date %', new.db_testify_comp_date;
293 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'TestifyComp', new.db_testify_comp_date,new.profileid, new.uid, new.acc );
294 end if;
295
296-- raise '0216: new.id %', new.id;
297
298 --* Org in charge
299 if new.orgresponseid is not null then
300 execute $x$
301 insert into "dbLegal".trcasetranorg (pid,orgrole,orgid ,uid, acc) select $1.id,'InCharge',$1.orgresponseid,$1.uid, $1.acc
302 $x$ using new;
303 end if;
304
305 -- select * from "dbLegal".trcasetranorg;
306
307 --* 2nd court event
308 if diff( new.db_App2_date ,null::timestamp_wtz ) then
309 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Due', new.db_App2_date,new.profileid, new.uid, new.acc );
310 end if;
311 if diff( new.db_app2_pp1_date ,null::timestamp_wtz ) then
312 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Postpone', new.db_app2_pp1_date,new.profileid, new.uid, new.acc );
313 end if;
314 if diff( new.db_app2_enroll_date ,null::timestamp_wtz ) then
315 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Enroll', new.db_app2_enroll_date,new.profileid, new.uid, new.acc );
316 end if;
317 if diff( new.db_app2_amend_date ,null::timestamp_wtz ) then
318 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Amend', new.db_app2_amend_date,new.profileid, new.uid, new.acc );
319 end if;
320
321 --* 3rd court event
322 if diff( new.db_App3_date ,null::timestamp_wtz ) then
323 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Due', new.db_App3_date,new.profileid, new.uid, new.acc );
324 end if;
325 if diff( new.db_app3_appv_date ,null::timestamp_wtz ) then
326 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Appv', new.db_app3_appv_date,new.profileid, new.uid, new.acc );
327 end if;
328 if diff( new.db_App3_pp1_date ,null::timestamp_wtz ) then
329 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Postpone', new.db_App3_pp1_date,new.profileid, new.uid, new.acc );
330 end if;
331 if diff( new.db_App3_enroll_date ,null::timestamp_wtz ) then
332 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Enroll', new.db_App3_enroll_date,new.profileid, new.uid, new.acc );
333 end if;
334 if diff( new.db_App3_amend_date ,null::timestamp_wtz ) then
335 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Amend', new.db_App3_amend_date,new.profileid, new.uid, new.acc );
336 end if;
337 if new.db_VerdExpire_date is not null then
338 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'VerdExpire', new.db_VerdExpire_date,new.profileid, new.uid, new.acc );
339 end if;
340
341 return NEW;
342 elseif (TG_OP = 'UPDATE') then
343
344 --* Simple III--------------------------------------------------------------------------------
345
346 if diff(old.defendant1_id, new.defendant1_id) then
347
348 -- raise 'xx';
349 update "dbLegal".trcasetrandc set sync_part = true where id = (
350 select id from "dbLegal".trcasetrandc where pid = new.id and tvalue = new.defendant1_id
351 );
352 end if;
353
354 --* Org in charge
355 if diff( new.orgresponseid,old.orgresponseid ) then
356 --insert into "dbLegal".trcasetranorg (pid,orgrole,orgid ,uid, acc ) select new.id,'InCharge',new.orgresponseid,new.uid, new.acc );
357 execute $x$
358 update "dbLegal".trcasetranorg set orgid = $1.orgresponseid where pid= $1.id and orgrole = 'InCharge' and customrole is null
359 $x$ using new;
360
361 end if;
362 -- select * from "dbLegal".trcasetranorg;
363
364 --* contractno
365
366 /*
367 if diff( new.db_contractno ,old.db_contractno) then
368 perform "dbLegal".tbcaseassetattr_addupdIII( new.id,'เลขที่สัญญา', new.db_contractno,new.assettype.id, new.uid, new.acc );
369 end if; */
370
371
372 --* 1st court outcome
373 if diff( new.db_testify_comp_date ,old.db_testify_comp_date ) then
374 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'TestifyComp', new.db_testify_comp_date,new.profileid, new.uid, new.acc );
375 end if;
376 -- raise '0332: new.db_case_outcome %, old.db_case_outcome %', new.db_case_outcome, old.db_case_outcome ;
377 if diff( new.db_case_outcome, old.db_case_outcome ) or diff( new.db_case_outcome_abbr, old.db_case_outcome_abbr ) then
378 -- raise '0335';
379 perform "dbLegal".tbcasetranstep_addupdIII( new.id
380 , new.db_case_outcome
381 , new.db_case_outcome_abbr
382 ,new.profileid, new.uid, new.acc );
383 end if;
384 if diff ( new.db_case_outcome_date, old.db_case_outcome_date ) then
385 perform "dbLegal".tbcasetranevent_addupdIII( new.id,new.db_case_outcome, new.db_case_outcome_date,new.profileid, new.uid, new.acc );
386 end if;
387
388 --* 2nd court event
389 if diff( new.db_App2_date ,old.db_App2_date ) then
390 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Due', new.db_App2_date,new.profileid, new.uid, new.acc );
391 end if;
392 if diff( new.db_app2_pp1_date ,old.db_app2_pp1_date ) then
393 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Postpone', new.db_app2_pp1_date,new.profileid, new.uid, new.acc );
394 end if;
395 if diff( new.db_app2_enroll_date ,old.db_app2_enroll_date ) then
396 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Enroll', new.db_app2_enroll_date,new.profileid, new.uid, new.acc );
397 end if;
398 if diff( new.db_app2_amend_date ,old.db_app2_amend_date ) then
399 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App2_Amend', new.db_app2_amend_date,new.profileid, new.uid, new.acc );
400 end if;
401
402 -- raise '0355: new.db_app2_outcome %, old.db_app2_outcome % ', new.db_app2_outcome, old.db_app2_outcome ;
403 --* 2nd court outcome
404 if diff( new.db_app2_outcome, old.db_app2_outcome ) or diff( new.db_app2_outcome_abbr, old.db_app2_outcome_abbr ) then
405 perform "dbLegal".tbcasetranstep_addupdIII( new.id
406 , new.db_app2_outcome
407 , new.db_app2_outcome_abbr
408 ,new.profileid, new.uid, new.acc );
409 end if;
410 if diff ( new.db_app2_outcome_date, old.db_app2_outcome_date ) then
411 perform "dbLegal".tbcasetranevent_addupdIII( new.id, new.db_app2_outcome, new.db_app2_outcome_date,new.profileid, new.uid, new.acc );
412 end if;
413
414 --* 3rd court event
415 if diff( new.db_App3_date ,old.db_App3_date ) then
416 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Due', new.db_App3_date,new.profileid, new.uid, new.acc );
417 end if;
418 if diff( new.db_app3_appv_date , old.db_app3_appv_date ) then
419 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Appv', new.db_app3_appv_date,new.profileid, new.uid, new.acc );
420 end if;
421 if diff( new.db_App3_pp1_date ,old.db_App3_pp1_date ) then
422 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Postpone', new.db_App3_pp1_date,new.profileid, new.uid, new.acc );
423 end if;
424 if diff( new.db_App3_enroll_date ,old.db_App3_enroll_date ) then
425 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Enroll', new.db_App3_enroll_date,new.profileid, new.uid, new.acc );
426 end if;
427 if diff( new.db_App3_amend_date ,old.db_App3_amend_date ) then
428 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'App3_Amend', new.db_App3_amend_date,new.profileid, new.uid, new.acc );
429 end if;
430
431 --* 3rd court outcome
432 if diff( new.db_App3_outcome, old.db_App3_outcome ) or diff( new.db_App3_outcome_abbr, old.db_App3_outcome_abbr ) then
433 perform "dbLegal".tbcasetranstep_addupdIII( new.id
434 , new.db_App3_outcome
435 , new.db_App3_outcome_abbr
436 ,new.profileid, new.uid, new.acc );
437 end if;
438 if diff ( new.db_App3_outcome_date, old.db_App3_outcome_date ) then
439 perform "dbLegal".tbcasetranevent_addupdIII( new.id, new.db_App3_outcome, new.db_App3_outcome_date,new.profileid, new.uid, new.acc );
440 end if;
441 --* enforcement
442 if diff( new.db_VerdExpire_date ,old.db_VerdExpire_date )l then
443 perform "dbLegal".tbcasetranevent_addupdIII( new.id,'VerdExpire', new.db_VerdExpire_date,new.profileid, new.uid, new.acc );
444 end if;
445
446 return NEW;
447 elseif TG_OP = 'DELETE' then
448 return OLD;
449 end if;
450end ; $$ LANGUAGE plpgsql;
451
452
453/*****************************
454 View registration
455******************************/
456select "dbSys".viewRegistration( '"dbLegal".trCaseTrn040VW'
457 , pers := '"dbLegal".trcasetran'
458 --, _delete_system_data := true
459 -- ,_init_data := true, _init_data_uid := 1 ,_init_data_acc := 4 , _init_sysorg := true
460 --,_init_data := true, _init_data_uid := 1 ,_init_data_acc := null , _init_sysorg := true
461
462
463 ,_fts := true
464 ,_ftsfnvalexc := 'projectid,DebtCalcID,PeriodID,taskidgrp,count_event,count_asset,count_org,count_dc,count_doc,count_amt,count_gua,count_calc,post_inout,post_casestat,casekind,NoDefProf,create_require_event_by_dc,updateamount
465
466 CustName t ext
467 ,Defendant
468 ,CourtRole
469
470 ,ReceiverName text
471
472 ,OrgName text
473
474 ,LawOfficerName text
475 ,lawofficer_contactid
476
477 ,LawOfficeName
478
479 ,LawyerName
480
481 ,LGTypeName
482
483 ,ProfileName
484
485 ,ActiveStepName
486
487 ,UID int
488 ,ACC int
489 ,Status text
490
491 ,CaseComplete
492 ,CaseComplete_date
493
494 ,CourtName
495 ,CourtAreaName
496 ,sue_date
497
498 ,c_defd
499
500 ,CaseStatusName
501
502 ,Defendant1_Name
503
504
505 ,keyword
506
507 ,auto_copy
508 ,casekind
509 ,assign_lawoff
510
511 ,upddt
512 ,updateamount
513 ,eleadevent_enable
514
515 ,assettype
516 ,refid
517 ,reflgno
518 ,reftrandate
519
520 ,BankName
521
522 ,BankBranchName
523 ,tgCalculate
524 ,tgPrintForm
525 ,caseasset_gen
526'
527
528
529 ,_dup:=null -- disable
530
531);
532
533
534/************************************************************
535 Registration : inheritance
536
537 select * from "dbSys".tbinrregis where sch = 'dbLegal' and nam = 'trpinvvw'
538 select * from "dbSys".tbinrregis where sch = 'dbFinance' and nam = 'trpinvvw'
539 select * from "dbSys".tbinrregis where sch = 'dbFinance' and nam = 'trpreqvw'
540
541*************************************************************/
542-- select "dbSys".tblRegistration_Inherit( '"dbLegal".trCaseTrn040VW','"dbLegal".trcasetranVW', true, true, true );
543select "dbSys".tblRegistration_Inherit( '"dbLegal".trCaseTrn040VW','"dbLegal".trcasetranVW', false , false , true );
544
545
546--* create view
547select "dbSys".tvCreateView ( varint('userid'), varint('accid'), '"dbLegal".trCaseTrn040',dropit:=true );
548
549-- select * from "dbLegal".trCaseTrn040VW12;
550
551-- select * from "dbLegal".trCaseTrn040VW4;
552 -- select "dbSys".recreate_views( _accid:= null , _tbl :='"dbLegal".trCaseTrn040' , _createview:=true ,_dropview:=true ,_regis:=true,_fts:=null );
553
554
555-----------------------------------------
556
557alter table "dbLegalEDU".rpCaseAR add column lawyername text null;
558
559
560create or replace function "dbLegalEDU".rpCaseAR( in prm "dbLegalEDU".rpCaseAR
561 ,out sql text, out sql_createtemp text
562 ,out sql1 text
563) returns record as $$
564declare CH_DEL text = ',';
565 sql2 text; sql21 text; sql22 text;
566 s text; ss text;
567 c int ;
568 eventTitles record ;
569 eventTitlesA text[];
570 date_clause text;
571 date_title text ;
572 tbview99 text;
573
574begin
575 --* user view
576 select "dbSys".tvcreateview( prm.uid, prm.acc , '"dbLegal".trcasetran' ) into tbview99;
577
578 --* 1. Case ---------------------
579 sql := format( '
580select
581 a.id casetranid
582 ,null::int col_1 -- no
583 ,concat_ws(%L,b.prefix, b.firstname ) col_2 --A
584 ,b.lastname col_3
585 ,concat(%L, concat_ws(%L,a1.BlackNo,a1.blacknobd) , concat_ws(%L,a1.redno, a1.rednobd) ) col_4 -- B
586 , a2.startdate_wtz col_5 -- วันที่พิพากษา
587 ,a1.CourtName col_6 -- ศาล
588 , a3.startdate_wtz col_7 -- วันที่ทำประนีประนอมยอมความ
589 ,a4.tvalue col_8 -- ต้นเงิน
590 ,a11.balance col_9 --ต้นเงินคงเหลือยกมา
591 ,a12.balance col_10 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ต้นเงิน
592 ,a13.balance col_11 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ย
593 ,a14.balance col_12 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ยระหว่างดำเนินคดี
594 ,a15.balance col_13 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ค่าปรับ
595 ,a16.balance col_14 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ค่าทนาย
596 ,a17.balance col_15 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ยหลังคำพิพากษา
597 -- ,a18.balance col_16
598 ,numval( a19.out) - numval(a19.in) col_17 --ชำระต้นเงินระหว่างปี
599 --,numval( a20.out) - numval(a20.in) col_18 --หักชำระระหว่างปี
600 ,numval( a19.out) - numval(a19.in) col_18 --หักชำระระหว่างปี
601 ,numval(a23.in) col_21 -- ดอกเบี้ย-เพิ่มระหว่างปี
602 ,numval( a21.out) - numval(a21.in) col_22 --ดอกเบี้ย-ชำระดอกเบี้ยพึง
603 ,numval( a22.out) - numval(a22.in) col_23 --ดอกเบี้ย-ชำระดอกเบี้ยค้าง
604 ,a.remark col_30 --สรุปสาระสำคัญ(หมายเหตุ)
605 --* Extend-SRR ------------------------------------------
606 ,a1.defendant1_name col_24 -- ชื่อ-สกุลจำเลย
607 ,a1.ReceiveDate col_25 -- วันที่ส่งสำนักงาน/วันที่รับเรื่อง
608 ,a1.lawyername col_26 --ชื่อทนายความ
609 ,a30.phonehome col_27 -- เบอร์โทร (มือถือ)/เบอร์โทรติดต่อของทนายความ
610 ,a1.casestatusname col_28 -- ขั้นตอนปัจจุบัน/สถานะคดี
611 ,a31.startdate col_29 -- วันครบกำหนดทวงถาม
612 ,a32.startdate col_31 -- กำหนดวันฟ้อง
613 ,a1.lgtypename col_32 -- ประเภทคดี
614 ,concat_ws(%L, a1.blackno, a1.blacknobd ) col_33 -- เลขคดีดำ -- B-1
615 ,concat_ws(%L, a1.redno, a1.rednobd ) col_34 -- เลขคดีแดง -- B-2
616 ,a33.startdate col_35 -- วันฟ้อง
617 ,a1.amt_total col_36 -- ทุนทรัพย์ฟ้อง
618 ,a34.startdate_wtz col_37 -- วันที่นัดสืบพยาน
619 ,a34.startdate_wtz col_38 -- เวลานัดสืบพยาน
620 ,a35.startdate col_39 -- วันที่ออกคำบังคับ
621 ,a36.startdate col_40 -- วันที่ส่งคำบังคับ / วันที่ออกหมายบังคับคดี
622
623 ,a37.startdate col_41 -- วันที่ยื่นอุทธรณ์
624 ,a38.startdate col_42 -- วันที่แก้อุทธรณ์
625 ,a39.startdate col_43 -- วันที่ยื่นฎีกา
626 ,a40.startdate col_44 -- วันที่แก้ฎีกา
627
628 ,a41.startdate col_45 -- วันที่รับเงินค่าธรรมเนียมศาลสั่งคืน
629 ,a42.startdate col_46 -- วันที่รับต้นฉบับคืนจากศาล
630 ,null::timestamp_wtz col_47 -- วันที่เบิกเงิน
631
632from
633 "dbLegal".trcasetran a
634 --join "dbLegal".trcasetranVW_UserData( %s,%s ) a1 on a1.id = a.id -- B-3
635 -- right join %s a1 on a1.id = a.id -- B-4
636
637 right join ( select distinct a.id id from (
638 select id from "dbLegal".trcasetranVW%s where id is not null --B-4.1
639 union all
640 select id from "dbLegal".trcasetrn040VW%s where id is not null -- B-4.2
641 ) a
642 ) a0 on a0.id = a.id
643 left join "dbLegal".trcasetranvw_userdata( 1 , %s ) a1 on a1.id = a.id -- B-4.3
644 --*Debt -----------------------------------------------
645 left join "dbFinance".rpARCard_OpenBalanceII(
646 a1.defendant1_id
647 ,%s -- periodid -- B.1
648 ,a.acc
649 ,prm_stockid := %s --B.1.1
650 ) a11 on a11.storeid = a1.defendant1_id
651 left join "dbFinance".rpARCard_OpenBalanceII(
652 a1.defendant1_id
653 ,%s -- periodid -- B.2
654 ,a.acc
655 ,prm_stockid := %s --B.2.1
656 ) a12 on a12.storeid = a1.defendant1_id
657 left join "dbFinance".rpARCard_OpenBalanceII(
658 a1.defendant1_id
659 ,%s -- periodid -- B.3
660 ,a.acc
661 ,prm_stockid := %s --B.3.1
662 ) a13 on a13.storeid = a1.defendant1_id
663 left join "dbFinance".rpARCard_OpenBalanceII(
664 a1.defendant1_id
665 ,%s -- periodid -- B.4
666 ,a.acc
667 ,prm_stockid := %s --B.4.1
668 ) a14 on a14.storeid = a1.defendant1_id
669 left join "dbFinance".rpARCard_OpenBalanceII(
670 a1.defendant1_id
671 ,%s -- periodid -- B.5
672 ,a.acc
673 ,prm_stockid := %s --B.5.1
674 ) a15 on a15.storeid = a1.defendant1_id
675 left join "dbFinance".rpARCard_OpenBalanceII(
676 a1.defendant1_id
677 ,%s -- periodid -- B.6
678 ,a.acc
679 ,prm_stockid := %s --B.6.1
680 ) a16 on a16.storeid = a1.defendant1_id
681 left join "dbFinance".rpARCard_OpenBalanceII(
682 a1.defendant1_id
683 ,%s -- periodid -- B.7
684 ,a.acc
685 ,prm_stockid := %s --B.7.1
686 ) a17 on a17.storeid = a1.defendant1_id
687 left join "dbFinance".rpARCard_CloseBalanceIII(
688 a1.defendant1_id
689 ,%s -- periodid -- B.8
690 ,a.acc
691 ) a18 on a18.storeid = a1.defendant1_id
692 left join "dbFinance".rpARCard_TransactionII(
693 a1.defendant1_id
694 ,%s -- periodid -- B.9
695 ,a.acc
696 ,prm_stockid := %s --B.9.1
697 ,prm_PeriodMonth := %s --B.9.2
698 ) a19 on a19.storeid = a1.defendant1_id
699 left join "dbFinance".rpARCard_TransactionII(
700 a1.defendant1_id
701 ,%s -- periodid -- B.10
702 ,a.acc
703 ,prm_stockid_exclude := %s --B.10.1
704 ,prm_PeriodMonth := %s --B.10.2
705 ,sumit := true
706 ) a20 on a20.storeid = a1.defendant1_id
707
708 left join "dbFinance".rpARCard_TransactionII(
709 a1.defendant1_id
710 ,%s -- periodid -- B.11
711 ,a.acc
712 ,prm_stockid := %s --B.9.11.1
713 ,prm_PeriodMonth := %s --B.11.2
714 ) a21 on a21.storeid = a1.defendant1_id
715
716 left join "dbFinance".rpARCard_TransactionII(
717 a1.defendant1_id
718 ,%s -- periodid -- B.12
719 ,a.acc
720 ,prm_stockid := %s --B.9.12.1
721 ,prm_PeriodMonth := %s --B.12.2
722 ) a22 on a22.storeid = a1.defendant1_id
723
724 left join "dbFinance".rpARCard_TransactionII(
725 a1.defendant1_id
726 ,%s -- periodid -- B.13
727 ,a.acc
728 ,prm_stockid := %s --B.9.13.1
729 ,prm_PeriodMonth := %s --B.13.2
730 ) a23 on a23.storeid = a1.defendant1_id
731 --* End of Debt ------------------------------------
732 left join "dbLegal".tbLawyerVW_UserData( 1 ,%s ) a30 on a30.id = a1.lawyerid -- A-0
733 left join "dbLegal".trcasetranevent_getdata( a.id,etitle:= %L ) a31 on a31.id = a.id --A-1
734 left join "dbLegal".trcasetranevent_getdata( a.id,etitle:= %L ) a32 on a32.id = a.id --A-2
735 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a33 on a33.id = a.id --A-3
736 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a34 on a34.id = a.id --A-4
737 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a35 on a35.id = a.id --A-5
738 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a36 on a36.id = a.id --A-6
739
740 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a37 on a37.id = a.id --A-7
741 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a38 on a38.id = a.id --A-8
742 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a39 on a39.id = a.id --A-9
743 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a40 on a40.id = a.id --A-10
744
745 left join "dbLegal".trcasetranevent_getdata( a.id, eTitle := %L ) a41 on a41.id = a.id --A-11
746 left join "dbLegal".trcasetranevent_getdata( a.id, eTitle := %L ) a42 on a42.id = a.id --A-12
747
748 left join "dbLegal".trcasetranevent_getdata( a.id, %L ) a2 on a2.id = a.id --C
749 left join "dbLegal".trcasetranevent_getdata( a.id, %L ) a3 on a3.id = a.id --D
750 left join "dbLegal".trcasetranamt_getdata(a.id,%L) a4 on a.id = a.id -- E
751 left join "dbContact".tbcontact b on b.id = a1.defendant1_id
752 left outer join "dbLegal".trcasetranNo j on (j.PID = a.ID and j.Title = %L and j.tcustom is null ) --F
753 left join "dbLegal".trcasetraneventvw_titles( a.id ) g on g._pid = a.id
754where
755 a1.defendant1_id is not null and a1.acc = %s --AA
756
757 '
758 ,' ' --A
759 ,',','/','/' --B
760 ,'/' --B-1
761 ,'/' --B-2
762 , prm.uid, prm.acc --- B-3
763 , tbview99 -- B-4
764 , prm.acc --B-4.1
765 , prm.acc --B-4.2
766 , prm.acc --B-4.3
767
768
769 --, "dbBase".getperiodid( varint('accid'),( vardate('trandate') - interval '1 year' ):: timestamp_wtz ) -- prm.periodid --B.1
770 -- , "dbBase".getperiodid( varint('accid'),( prm.period_date_fr ):: timestamp_wtz ) -- prm.periodid --B.1
771 -- , "dbBase".getperiodid( prm.acc ,( prm.period_date_fr ):: timestamp_wtz ) -- prm.periodid --B.1
772 ,prm.PeriodID_start --8.1
773
774 ,fmval( (
775 select b.id
776 from "dbInventory".tbinven a
777 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
778 where a.acc = 4 and a.no = 'เงินต้น'
779 )::int ) -- B.1.1
780 , prm.periodid --B.2
781 ,fmval( (
782 select b.id
783 from "dbInventory".tbinven a
784 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
785 where a.acc = 4 and a.no = 'เงินต้น'
786 )::int ) -- B.2.1
787 , prm.periodid --B.3
788 ,fmval( (
789 select b.id
790 from "dbInventory".tbinven a
791 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
792 where a.acc = 4 and a.no = 'ดอกเบี้ย'
793 )::int ) -- B.3.1
794 , prm.periodid --B.4
795 ,fmval( (
796 select b.id
797 from "dbInventory".tbinven a
798 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
799 where a.acc = 4 and a.no = 'ดอกเบี้ยระหว่างดำเนินคดี'
800 )::int ) -- B.4.1
801 , prm.periodid --B.5
802 ,fmval( (
803 select b.id
804 from "dbInventory".tbinven a
805 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
806 where a.acc = 4 and a.no = 'ค่าปรับ'
807 )::int ) -- B.5.1
808 , prm.periodid --B.6
809 ,fmval( (
810 select b.id
811 from "dbInventory".tbinven a
812 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
813 where a.acc = 4 and a.no = 'ค่าทนาย'
814 )::int ) -- B.6.1
815 , prm.periodid --B.7
816 ,fmval( (
817 select b.id
818 from "dbInventory".tbinven a
819 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
820 where a.acc = 4 and a.no = 'ดอกเบี้ยหลังคำพิพากษา'
821 )::int ) -- B.7.1
822 , prm.periodid --B.8
823 , prm.periodid --B.9
824 ,fmval( (
825 select b.id
826 from "dbInventory".tbinven a
827 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
828 where a.acc = 4 and a.no = 'เงินต้น'
829 )::int ) -- B.9.1
830 ,prm.PeriodMonth --B.9.2
831
832 , prm.periodid --B.10
833 ,fmval(
834 concat_ws( ','
835 ,( select b.id
836 from "dbInventory".tbinven a
837 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
838 where a.acc = 4 and a.no = 'เงินต้น'
839 )::text
840
841 ,( select b.id
842 from "dbInventory".tbinven a
843 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
844 where a.acc = 4 and a.no = 'ดอกเบี้ย'
845 )::text
846 ,( select b.id
847 from "dbInventory".tbinven a
848 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
849 where a.acc = 4 and a.no = 'ดอกเบี้ยพีง'
850 )::text
851 ,( select b.id
852 from "dbInventory".tbinven a
853 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
854 where a.acc = 4 and a.no = 'ดอกเบี้ยค้าง'
855 )::text
856
857 )
858 ) -- B.10.1 -- exclude these service
859 ,prm.PeriodMonth --B.10.2
860
861 , prm.periodid --B.11
862 ,fmval( (
863 select b.id
864 from "dbInventory".tbinven a
865 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
866 where a.acc = 4 and a.no = 'ดอกเบี้ยพีง'
867 )::int ) -- B.11.1
868 ,prm.PeriodMonth --B.11.2
869
870 , prm.periodid --B.12
871 ,fmval( (
872 select b.id
873 from "dbInventory".tbinven a
874 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
875 where a.acc = 4 and a.no = 'ดอกเบี้ยค้าง'
876 )::int ) -- B.12.1
877 ,prm.PeriodMonth --B.12.2
878
879 , prm.periodid --B.13
880 ,fmval( (
881 select b.id
882 from "dbInventory".tbinven a
883 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
884 where a.acc = 4 and a.no = 'ดอกเบี้ยเพิ่มระหว่างปี'
885 )::int ) -- B.13.1
886 ,prm.PeriodMonth --B.13.2
887
888
889 -------------------------------------------------------------------
890 ,prm.acc --- A-0
891 ,'วันครบกำหนดทวงถาม' --A-1
892 ,'กำหนดวันฟ้อง' --A-2
893 ,'Sue' --A-3
894 ,'Testify' --A-4
895 ,'ForceOut' --A-5
896 ,'ForceSubmit' --A-6
897
898 ,'App2_Enroll' --A-7
899 ,'App2_Amend' --A-8
900 ,'App3_Enroll' --A-9
901 ,'App3_Amend' --A-10
902
903 ,'วันที่รับเงินค่าธรรมเนียมศาลสั่งคืน' --A-11
904 ,'วันที่รับต้นฉบับคืนจากศาล' --A-12
905 ,'Judge' -- วันที่พิพากษา --C select * from "dbLegal".trcasetranevent_getdata( varint('casetranid'), 'Compromise' );
906 ,'Compromise' -- 'วันที่ทำประนีประนอมยอมความ' --D
907 ,'เงินต้น' --E select * from "dbLegal".trcasetranamt_getdata( varint('casetranid') , _tlabel := 'เงินต้น' );
908
909 ,'Black' --F
910 ,prm.acc --AA
911 );
912
913 --* defendant
914 if prm.Defendant1_Name is not null then
915 -- sql := "dbSys".sqlAddwhere(sql, format( 'a.defendant1_name = %L',prm.defendant1_name ), multi:=true, orderby_groupby := true );
916 sql := "dbSys".sqlAddwhere(sql, format( 'a1.defendant1_name = %L',prm.defendant1_name ), multi:=true, orderby_groupby := true );
917
918 end if;
919
920 if prm.groups is not null then
921 sql := "dbSys".sqlAddwhere(sql, format( 'ExistStr( "dbLegal".trcasetranGrp_getgroups(a.id) , %L )>0 ', prm.groups ) ,multi:=false, orderby_groupby := true );
922 end if;
923
924 --* blackno
925 if prm.blackno is not null then
926 sql := "dbSys".sqlAddwhere(sql, format( 'j.tvalue = %L',prm.blackno ), multi:=true, orderby_groupby := true );
927 end if;
928
929 --* Case status
930 if prm.casestatuss is not null then
931 sql := "dbSys".sqlAddwhere(sql, format( 'a1.casestatusname = %L',prm.casestatuss ), multi:=true, orderby_groupby := true );
932
933 end if;
934 --* Court role
935 if prm.courtrole is not null then
936 sql := "dbSys".sqlAddwhere(sql, format( 'a1.courtrole = %L',prm.courtrole ), multi:=false , orderby_groupby := true );
937
938 end if;
939 -- select id,courtrole from "dbLegal".trcasetranvw_userdata(1,12);
940
941 --* Event options ----------------
942 if prm.evTitles is not null then
943 ss:= null;
944 -- select * from "dbLegal".trcasetraneventvw_titles ( prm.casetranid ) into eventTitles;
945 eventTitlesA = string_to_array(prm.evtitles, ',') ;
946
947 -- raise '0643: eventTitlesA %', eventTitlesA;
948 if not prm.evEnable then
949
950 c := 0;
951 ss:= null;
952 foreach s in array eventTitlesA loop
953 -- raise notice '0658: s %, eventTitlesA %, 1= %', s, eventTitlesA, eventTitlesA[1];
954
955 c = c + 1;
956 -- case when 'วันที่รับเรื่อง' = any( g._titles ) then g._startdates[1] is null else true end
957 -- ss:= format('case when %L = any( g._titles ) then g._startdates[%s] is null else true end ', s, i );
958 -- {วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่รับเรื่อง,วันที่ยื่นฟ้อง,วันนัดไกล่เกลี่ย,วันนัดสืบพยานโจทก์,วันที่ขอแก้ไขคำฟ้อง,วันที่รับชำระหนี้ก่อนฟ้อง,วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่ทำสัญญายอม,วันที่พิพากษา}
959
960 -- ss = concat_ws( case when c>1 then chr(13) else '' end || ' or ', ss, format('case when %L = any( g._titles ) then g._startdates[array_position(g._titles, %L ) ] is null else true end ', s, s ) );
961 ss = concat_ws( chr(13) || case when c>1 then ' or ' else ' ' end , ss, format(
962' case when any_exist( g._titles, %L ) >0 then ( --A
963 select startdate from "dbLegal".trcasetranevent where pid = a.id and id = any(
964 array_values_by_pos( g._ids, array_exists( g._titles, %L ) ) --B
965 ) and startdate is null limit 1
966 ) is null else true end '
967 , s --A
968 , s --B
969
970 ) );
971
972 end loop;
973 --*
974 prm.evreporttitle := format( '%s %s',prm.evDate_title_NoDate,prm.evTitles ); -- ที่ยังไม่ได้กำหนดวันที่
975
976 else --- evEnable
977 date_clause = null;
978 --* date clause
979 if prm.evFR is null and prm.evTO is null then
980 date_clause = format(' is not null ' );
981 date_title = prm.evDate_title_HaveDate ; -- ที่กำหนดวันที่แล้ว
982 elseif prm.evFR is not null and prm.evTO is null then
983 date_clause = format(' >= %L::date ', prm.evFR::text );
984 --date_title = format( prm.evDate_title_FromDate, prm.evFR::text ); -- ตั้งแต่วันที่ %s เป็นค้นไป
985 date_title = format( prm.evDate_title_FromDate, to_date( prm.evFR ) ); -- ตั้งแต่วันที่ %s เป็นค้นไป
986
987 elseif prm.evFR is null and prm.evTO is not null then
988 date_clause = format(' <= %L::date ', prm.evTO::text );
989 -- date_title = format( prm.evDate_title_ToDate, prm.evFR::text ); -- ตั้งแต่วันที่ %s เป็นค้นมา
990 date_title = format( prm.evDate_title_ToDate, to_date(prm.evTo) ); -- ตั้งแต่วันที่ %s เป็นค้นมา
991 else -- prm.evFR is not null and prm.evTO is not null
992 date_clause = format('between %L::date and %L::date ', prm.evFR::text , prm.evTO::text );
993 -- date_title = format( prm.evDate_title_Between, prm.evFR::text, prm.evTO::text ); -- ระหว่างวันที่ %s ถึง %s
994 date_title = format( prm.evDate_title_Between, to_date(prm.evFR), to_date(prm.evTO) ); -- ระหว่างวันที่ %s ถึง %s
995
996 end if;
997
998 --*
999 c := 0;
1000 ss:= null;
1001 foreach s in array eventTitlesA loop
1002 c = c + 1;
1003 ss = concat_ws( chr(13) || case when c>1 then ' or ' else ' ' end , ss, format(
1004' case when any_exist( g._titles, %L ) >0 then ( --A
1005 select startdate from "dbLegal".trcasetranevent where pid = a.id and id = any(
1006 array_values_by_pos( g._ids, array_exists( g._titles, %L ) ) --B
1007 ) and startdate is not null limit 1
1008 ) %s else false end ' --C
1009 , s --A
1010 , s --B
1011 , date_clause-- C
1012 ) );
1013
1014 end loop;
1015
1016 --* Reporttitle: สำหรับ การรับเรื่อง,การยื่นฟ้อง ระหว่างวันที่ 01/01/2559 - 31/03/2559
1017 prm.evreporttitle := format('สำหรับ %s %s',prm.evTitles, date_title );
1018
1019 -- raise '0703: sql=%',sql;
1020 end if;
1021
1022 --raise '0652: ss=%', ss;
1023 sql := "dbSys".sqlAddwhere(sql, format(
1024'
1025(%s
1026)' ,ss
1027
1028 ) );
1029 end if;
1030
1031
1032 --*
1033 sql1:=sql;
1034
1035
1036 --*.2.2 : Show all asset
1037 sql22 = format(
1038'
1039select
1040 a.id caseid
1041 ,c1311.tvalue col_101 --ทะเบียนลูกค้า
1042 ,c1.tranno col_102 -- เลขที่สัญญา
1043 ,c1.signdate col_103 -- วันที่ทำสัญญา
1044from "dbLegal".trcasetran a
1045 join %s a1 on a1.id = a.id -- A-0
1046 left join "dbLegal".trcasetranasset c on c.pid = a.id and c.kind is null and c.orderno = 1
1047 left join "dbLegal".trcaseassetVW_UserData(
1048 1 -- %s --A-1
1049 ,%s --A-2
1050 ) c1 on c1.id = c.caseassetid --
1051 --left join "dbLegal".trcaseassetno c11 on c11.pid = c1.id
1052 --left join "dbLegal".trcaseassetcont c12 on c12.pid = c1.id
1053 left join "dbLegal".trcaseassetpart c13 on c13.pid = c1.id and c13.tvalue = a1.defendant1_id
1054 left join "dbContExt".tbloanee c131 on c131.contactid = c13.tvalue
1055 left join "dbContact".tbcontnumber c1311 on c1311.pid = c131.contactid and c1311.title = %L and c1311.tcustom is null --A
1056
1057 where a.id = aa.casetranid -- lateral query
1058 order by c1311,c1.tranno
1059'
1060 ,tbview99 -- A-0
1061 , prm.uid --A-1
1062 , prm.acc --- A-2
1063 , 'CIF' --A
1064
1065 );
1066
1067 sql21:= format(
1068'
1069select
1070 a.caseid
1071 ,string_agg ( %s a.col_41,%L) col_41
1072 ,string_agg ( %s a.col_42,%L) col_42
1073 ,string_agg ( %s a.col_43,%L) col_43
1074from (%s) a
1075group by a.caseid
1076
1077'
1078 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1079 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1080 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1081 ,sql22
1082 );
1083
1084
1085 --* Finall SQL -------------------------------------
1086 sql := format(
1087 '
1088select
1089 aa.*
1090 , b.*
1091from (%s
1092) aa
1093 left join lateral (%s) b on b.caseid = aa.casetranid
1094 '
1095 ,sql1
1096 , case when prm.show_asset then sql21 else sql22 end
1097
1098 );
1099 -- raise '0679:sql=%',sql;
1100 --* save parameter
1101 execute format('update "dbLegalEDU".rpCaseAR
1102 set evreporttitle = %L --A
1103 where id = %s '
1104 ,prm.evreporttitle
1105 ,prm.id
1106 )
1107 ;
1108
1109
1110end $$ language plpgsql;
1111
1112
1113create or replace function "dbLegalEDU".rpCaseAR( in prm "dbLegalEDU".rpCaseAR
1114 ,out sql text, out sql_createtemp text
1115 ,out sql1 text
1116) returns record as $$
1117declare CH_DEL text = ',';
1118 sql2 text; sql21 text; sql22 text;
1119 s text; ss text;
1120 c int ;
1121 eventTitles record ;
1122 eventTitlesA text[];
1123 date_clause text;
1124 date_title text ;
1125 tbview99 text;
1126
1127begin
1128 --* user view
1129 select "dbSys".tvcreateview( prm.uid, prm.acc , '"dbLegal".trcasetran' ) into tbview99;
1130
1131 --* 1. Case ---------------------
1132 sql := format( '
1133select
1134 a.id casetranid
1135 ,null::int col_1 -- no
1136 ,concat_ws(%L,b.prefix, b.firstname ) col_2 --A
1137 ,b.lastname col_3
1138 ,concat(%L, concat_ws(%L,a1.BlackNo,a1.blacknobd) , concat_ws(%L,a1.redno, a1.rednobd) ) col_4 -- B
1139 , a2.startdate_wtz col_5 -- วันที่พิพากษา
1140 ,a1.CourtName col_6 -- ศาล
1141 , a3.startdate_wtz col_7 -- วันที่ทำประนีประนอมยอมความ
1142 ,a4.tvalue col_8 -- ต้นเงิน
1143 ,null::num col_9 -- a11.balance col_9 --ต้นเงินคงเหลือยกมา
1144 ,null::num col_10 --a12. col_10 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ต้นเงิน
1145 ,null::num col_11 --a13.balance col_11 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ย
1146 ,null::num col_12 --a14.balance col_12 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ยระหว่างดำเนินคดี
1147 ,null::num col_13 --a15.balance col_13 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ค่าปรับ
1148 ,null::num col_14 --a16.balance col_14 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ค่าทนาย
1149 ,null::num col_15 --a17.balance col_15 --รายละเอียดแยกต้นเงินคงเหลือยกมา-ดอกเบี้ยหลังคำพิพากษา
1150 -- ,a18.balance col_16
1151 ,numval( a19.out) - numval(a19.in) col_17 --ชำระต้นเงินระหว่างปี
1152 --,numval( a20.out) - numval(a20.in) col_18 --หักชำระระหว่างปี
1153 ,numval( a19.out) - numval(a19.in) col_18 --หักชำระระหว่างปี
1154 ,numval(a23.in) col_21 -- ดอกเบี้ย-เพิ่มระหว่างปี
1155 ,numval( a21.out) - numval(a21.in) col_22 --ดอกเบี้ย-ชำระดอกเบี้ยพึง
1156 ,numval( a22.out) - numval(a22.in) col_23 --ดอกเบี้ย-ชำระดอกเบี้ยค้าง
1157 ,a.remark col_30 --สรุปสาระสำคัญ(หมายเหตุ)
1158 --* Extend-SRR ------------------------------------------
1159 ,a1.defendant1_name col_24 -- ชื่อ-สกุลจำเลย
1160 ,a1.ReceiveDate col_25 -- วันที่ส่งสำนักงาน/วันที่รับเรื่อง
1161 ,a1.lawyername col_26 --ชื่อทนายความ
1162 ,a30.phonehome col_27 -- เบอร์โทร (มือถือ)/เบอร์โทรติดต่อของทนายความ
1163 ,a1.casestatusname col_28 -- ขั้นตอนปัจจุบัน/สถานะคดี
1164 ,a31.startdate col_29 -- วันครบกำหนดทวงถาม
1165 ,a32.startdate col_31 -- กำหนดวันฟ้อง
1166 ,a1.lgtypename col_32 -- ประเภทคดี
1167 ,concat_ws(%L, a1.blackno, a1.blacknobd ) col_33 -- เลขคดีดำ -- B-1
1168 ,concat_ws(%L, a1.redno, a1.rednobd ) col_34 -- เลขคดีแดง -- B-2
1169 ,a33.startdate col_35 -- วันฟ้อง
1170 ,a1.amt_total col_36 -- ทุนทรัพย์ฟ้อง
1171 ,a34.startdate_wtz col_37 -- วันที่นัดสืบพยาน
1172 ,a34.startdate_wtz col_38 -- เวลานัดสืบพยาน
1173 ,a35.startdate col_39 -- วันที่ออกคำบังคับ
1174 ,a36.startdate col_40 -- วันที่ส่งคำบังคับ / วันที่ออกหมายบังคับคดี
1175
1176 ,a37.startdate col_41 -- วันที่ยื่นอุทธรณ์
1177 ,a38.startdate col_42 -- วันที่แก้อุทธรณ์
1178 ,a39.startdate col_43 -- วันที่ยื่นฎีกา
1179 ,a40.startdate col_44 -- วันที่แก้ฎีกา
1180
1181 ,a41.startdate col_45 -- วันที่รับเงินค่าธรรมเนียมศาลสั่งคืน
1182 ,a42.startdate col_46 -- วันที่รับต้นฉบับคืนจากศาล
1183 ,null::timestamp_wtz col_47 -- วันที่เบิกเงิน
1184
1185from
1186 "dbLegal".trcasetran a
1187 --join "dbLegal".trcasetranVW_UserData( %s,%s ) a1 on a1.id = a.id -- B-3
1188 -- right join %s a1 on a1.id = a.id -- B-4
1189
1190 right join ( select distinct a.id id from (
1191 select id from "dbLegal".trcasetranVW%s where id is not null --B-4.1
1192 union all
1193 select id from "dbLegal".trcasetrn040VW%s where id is not null -- B-4.2
1194 ) a
1195 ) a0 on a0.id = a.id
1196 left join "dbLegal".trcasetranvw_userdata( 1 , %s ) a1 on a1.id = a.id -- B-4.3
1197 --*Debt -----------------------------------------------
1198 /*
1199 left join "dbFinance".rpARCard_OpenBalanceII(
1200 a1.defendant1_id
1201 ,%s -- periodid -- B.1
1202 ,a.acc
1203 ,prm_stockid := %s --B.1.1
1204 ) a11 on a11.storeid = a1.defendant1_id
1205 left join "dbFinance".rpARCard_OpenBalanceII(
1206 a1.defendant1_id
1207 ,%s -- periodid -- B.2
1208 ,a.acc
1209 ,prm_stockid := %s --B.2.1
1210 ) a12 on a12.storeid = a1.defendant1_id
1211 left join "dbFinance".rpARCard_OpenBalanceII(
1212 a1.defendant1_id
1213 ,%s -- periodid -- B.3
1214 ,a.acc
1215 ,prm_stockid := %s --B.3.1
1216 ) a13 on a13.storeid = a1.defendant1_id
1217 left join "dbFinance".rpARCard_OpenBalanceII(
1218 a1.defendant1_id
1219 ,%s -- periodid -- B.4
1220 ,a.acc
1221 ,prm_stockid := %s --B.4.1
1222 ) a14 on a14.storeid = a1.defendant1_id
1223 left join "dbFinance".rpARCard_OpenBalanceII(
1224 a1.defendant1_id
1225 ,%s -- periodid -- B.5
1226 ,a.acc
1227 ,prm_stockid := %s --B.5.1
1228 ) a15 on a15.storeid = a1.defendant1_id
1229 left join "dbFinance".rpARCard_OpenBalanceII(
1230 a1.defendant1_id
1231 ,%s -- periodid -- B.6
1232 ,a.acc
1233 ,prm_stockid := %s --B.6.1
1234 ) a16 on a16.storeid = a1.defendant1_id
1235 left join "dbFinance".rpARCard_OpenBalanceII(
1236 a1.defendant1_id
1237 ,%s -- periodid -- B.7
1238 ,a.acc
1239 ,prm_stockid := %s --B.7.1
1240 ) a17 on a17.storeid = a1.defendant1_id
1241*/
1242 left join "dbFinance".rpARCard_CloseBalanceIII(
1243 a1.defendant1_id
1244 ,%s -- periodid -- B.8
1245 ,%s --B.8.1
1246 ) a18 on a18.storeid = a1.defendant1_id
1247 left join "dbFinance".rpARCard_TransactionII(
1248 a1.defendant1_id
1249 ,%s -- periodid -- B.9
1250 ,%s --B.9.0
1251 ,prm_stockid := %s --B.9.1
1252 ,prm_PeriodMonth := %s --B.9.2
1253 ) a19 on a19.storeid = a1.defendant1_id
1254 left join "dbFinance".rpARCard_TransactionII(
1255 a1.defendant1_id
1256 ,%s -- periodid -- B.10
1257 ,%s -- a.acc --B.10.0
1258 ,prm_stockid_exclude := %s --B.10.1
1259 ,prm_PeriodMonth := %s --B.10.2
1260 ,sumit := true
1261 ) a20 on a20.storeid = a1.defendant1_id
1262
1263 left join "dbFinance".rpARCard_TransactionII(
1264 a1.defendant1_id
1265 ,%s -- periodid -- B.11
1266 ,%s -- a.acc --B.11.0
1267 ,prm_stockid := %s --B.9.11.1
1268 ,prm_PeriodMonth := %s --B.11.2
1269 ) a21 on a21.storeid = a1.defendant1_id
1270
1271 left join "dbFinance".rpARCard_TransactionII(
1272 a1.defendant1_id
1273 ,%s -- periodid -- B.12
1274 ,%s --B.12.0
1275 ,prm_stockid := %s --B.9.12.1
1276 ,prm_PeriodMonth := %s --B.12.2
1277 ) a22 on a22.storeid = a1.defendant1_id
1278
1279 left join "dbFinance".rpARCard_TransactionII(
1280 a1.defendant1_id
1281 ,%s -- periodid -- B.13
1282 ,%s -- a.acc --B.13.0
1283 ,prm_stockid := %s --B.9.13.1
1284 ,prm_PeriodMonth := %s --B.13.2
1285 ) a23 on a23.storeid = a1.defendant1_id
1286 --* End of Debt ------------------------------------
1287 left join "dbLegal".tbLawyerVW_UserData( 1 ,%s ) a30 on a30.id = a1.lawyerid -- A-0
1288 left join "dbLegal".trcasetranevent_getdata( a.id,etitle:= %L ) a31 on a31.id = a.id --A-1
1289 left join "dbLegal".trcasetranevent_getdata( a.id,etitle:= %L ) a32 on a32.id = a.id --A-2
1290 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a33 on a33.id = a.id --A-3
1291 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a34 on a34.id = a.id --A-4
1292 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a35 on a35.id = a.id --A-5
1293 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a36 on a36.id = a.id --A-6
1294
1295 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a37 on a37.id = a.id --A-7
1296 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a38 on a38.id = a.id --A-8
1297 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a39 on a39.id = a.id --A-9
1298 left join "dbLegal".trcasetranevent_getdata( a.id,event_title := %L ) a40 on a40.id = a.id --A-10
1299
1300 left join "dbLegal".trcasetranevent_getdata( a.id, eTitle := %L ) a41 on a41.id = a.id --A-11
1301 left join "dbLegal".trcasetranevent_getdata( a.id, eTitle := %L ) a42 on a42.id = a.id --A-12
1302
1303 left join "dbLegal".trcasetranevent_getdata( a.id, %L ) a2 on a2.id = a.id --C
1304 left join "dbLegal".trcasetranevent_getdata( a.id, %L ) a3 on a3.id = a.id --D
1305 left join "dbLegal".trcasetranamt_getdata(a.id,%L) a4 on a.id = a.id -- E
1306 left join "dbContact".tbcontact b on b.id = a1.defendant1_id
1307 left outer join "dbLegal".trcasetranNo j on (j.PID = a.ID and j.Title = %L and j.tcustom is null ) --F
1308 left join "dbLegal".trcasetraneventvw_titles( a.id ) g on g._pid = a.id
1309where
1310 a1.defendant1_id is not null and a1.acc = %s --AA
1311
1312 '
1313 ,' ' --A
1314 ,',','/','/' --B
1315 ,'/' --B-1
1316 ,'/' --B-2
1317 , prm.uid, prm.acc --- B-3
1318 , tbview99 -- B-4
1319 , prm.acc --B-4.1
1320 , prm.acc --B-4.2
1321 , prm.acc --B-4.3
1322
1323
1324 --, "dbBase".getperiodid( varint('accid'),( vardate('trandate') - interval '1 year' ):: timestamp_wtz ) -- prm.periodid --B.1
1325 -- , "dbBase".getperiodid( varint('accid'),( prm.period_date_fr ):: timestamp_wtz ) -- prm.periodid --B.1
1326 -- , "dbBase".getperiodid( prm.acc ,( prm.period_date_fr ):: timestamp_wtz ) -- prm.periodid --B.1
1327 ,prm.PeriodID_start --8.1
1328
1329 ,fmval( (
1330 select b.id
1331 from "dbInventory".tbinven a
1332 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1333 where a.acc = 4 and a.no = 'เงินต้น'
1334 )::int ) -- B.1.1
1335 , prm.periodid --B.2
1336 ,fmval( (
1337 select b.id
1338 from "dbInventory".tbinven a
1339 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1340 where a.acc = 4 and a.no = 'เงินต้น'
1341 )::int ) -- B.2.1
1342 , prm.periodid --B.3
1343 ,fmval( (
1344 select b.id
1345 from "dbInventory".tbinven a
1346 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1347 where a.acc = 4 and a.no = 'ดอกเบี้ย'
1348 )::int ) -- B.3.1
1349 , prm.periodid --B.4
1350 ,fmval( (
1351 select b.id
1352 from "dbInventory".tbinven a
1353 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1354 where a.acc = 4 and a.no = 'ดอกเบี้ยระหว่างดำเนินคดี'
1355 )::int ) -- B.4.1
1356 , prm.periodid --B.5
1357 ,fmval( (
1358 select b.id
1359 from "dbInventory".tbinven a
1360 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1361 where a.acc = 4 and a.no = 'ค่าปรับ'
1362 )::int ) -- B.5.1
1363 , prm.periodid --B.6
1364 ,fmval( (
1365 select b.id
1366 from "dbInventory".tbinven a
1367 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1368 where a.acc = 4 and a.no = 'ค่าทนาย'
1369 )::int ) -- B.6.1
1370 , prm.periodid --B.7
1371 ,fmval( (
1372 select b.id
1373 from "dbInventory".tbinven a
1374 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1375 where a.acc = 4 and a.no = 'ดอกเบี้ยหลังคำพิพากษา'
1376 )::int ) -- B.7.1
1377 , prm.periodid --B.8
1378 , prm.acc --B.8.1
1379 , prm.periodid --B.9
1380 , prm.acc --B.9.0
1381 ,fmval( (
1382 select b.id
1383 from "dbInventory".tbinven a
1384 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1385 where a.acc = 4 and a.no = 'เงินต้น'
1386 )::int ) -- B.9.1
1387 ,prm.PeriodMonth --B.9.2
1388
1389 , prm.periodid --B.10
1390 , prm.acc --B.10.0
1391 ,fmval(
1392 concat_ws( ','
1393 ,( select b.id
1394 from "dbInventory".tbinven a
1395 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1396 where a.acc = 4 and a.no = 'เงินต้น'
1397 )::text
1398
1399 ,( select b.id
1400 from "dbInventory".tbinven a
1401 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1402 where a.acc = 4 and a.no = 'ดอกเบี้ย'
1403 )::text
1404 ,( select b.id
1405 from "dbInventory".tbinven a
1406 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1407 where a.acc = 4 and a.no = 'ดอกเบี้ยพีง'
1408 )::text
1409 ,( select b.id
1410 from "dbInventory".tbinven a
1411 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1412 where a.acc = 4 and a.no = 'ดอกเบี้ยค้าง'
1413 )::text
1414
1415 )
1416 ) -- B.10.1 -- exclude these service
1417 ,prm.PeriodMonth --B.10.2
1418
1419 , prm.periodid --B.11
1420 , prm.acc -- B.11.0
1421 ,fmval( (
1422 select b.id
1423 from "dbInventory".tbinven a
1424 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1425 where a.acc = 4 and a.no = 'ดอกเบี้ยพีง'
1426 )::int ) -- B.11.1
1427 ,prm.PeriodMonth --B.11.2
1428
1429 , prm.periodid --B.12
1430 , prm.acc --B.12.0
1431 ,fmval( (
1432 select b.id
1433 from "dbInventory".tbinven a
1434 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1435 where a.acc = 4 and a.no = 'ดอกเบี้ยค้าง'
1436 )::int ) -- B.12.1
1437 ,prm.PeriodMonth --B.12.2
1438
1439 , prm.periodid --B.13
1440 , prm.acc --B.13.0
1441 ,fmval( (
1442 select b.id
1443 from "dbInventory".tbinven a
1444 left join "dbInventory".tbinven b on b.acc= prm.acc and case when b.acc=a.acc then b.orgid is null else b.orgid = a.id end
1445 where a.acc = 4 and a.no = 'ดอกเบี้ยเพิ่มระหว่างปี'
1446 )::int ) -- B.13.1
1447 ,prm.PeriodMonth --B.13.2
1448
1449
1450 -------------------------------------------------------------------
1451 ,prm.acc --- A-0
1452 ,'วันครบกำหนดทวงถาม' --A-1
1453 ,'กำหนดวันฟ้อง' --A-2
1454 ,'Sue' --A-3
1455 ,'Testify' --A-4
1456 ,'ForceOut' --A-5
1457 ,'ForceSubmit' --A-6
1458
1459 ,'App2_Enroll' --A-7
1460 ,'App2_Amend' --A-8
1461 ,'App3_Enroll' --A-9
1462 ,'App3_Amend' --A-10
1463
1464 ,'วันที่รับเงินค่าธรรมเนียมศาลสั่งคืน' --A-11
1465 ,'วันที่รับต้นฉบับคืนจากศาล' --A-12
1466 ,'Judge' -- วันที่พิพากษา --C select * from "dbLegal".trcasetranevent_getdata( varint('casetranid'), 'Compromise' );
1467 ,'Compromise' -- 'วันที่ทำประนีประนอมยอมความ' --D
1468 ,'เงินต้น' --E select * from "dbLegal".trcasetranamt_getdata( varint('casetranid') , _tlabel := 'เงินต้น' );
1469
1470 ,'Black' --F
1471 ,prm.acc --AA
1472 );
1473
1474 --* defendant
1475 if prm.Defendant1_Name is not null then
1476 -- sql := "dbSys".sqlAddwhere(sql, format( 'a.defendant1_name = %L',prm.defendant1_name ), multi:=true, orderby_groupby := true );
1477 sql := "dbSys".sqlAddwhere(sql, format( 'a1.defendant1_name = %L',prm.defendant1_name ), multi:=true, orderby_groupby := true );
1478
1479 end if;
1480
1481 if prm.groups is not null then
1482 sql := "dbSys".sqlAddwhere(sql, format( 'ExistStr( "dbLegal".trcasetranGrp_getgroups(a.id) , %L )>0 ', prm.groups ) ,multi:=false, orderby_groupby := true );
1483 end if;
1484
1485 --* blackno
1486 if prm.blackno is not null then
1487 sql := "dbSys".sqlAddwhere(sql, format( 'j.tvalue = %L',prm.blackno ), multi:=true, orderby_groupby := true );
1488 end if;
1489
1490 --* Case status
1491 if prm.casestatuss is not null then
1492 sql := "dbSys".sqlAddwhere(sql, format( 'a1.casestatusname = %L',prm.casestatuss ), multi:=true, orderby_groupby := true );
1493
1494 end if;
1495 --* Court role
1496 if prm.courtrole is not null then
1497 sql := "dbSys".sqlAddwhere(sql, format( 'a1.courtrole = %L',prm.courtrole ), multi:=false , orderby_groupby := true );
1498
1499 end if;
1500
1501 -- select id,courtrole from "dbLegal".trcasetranvw_userdata(1,12);
1502 --* lawyer --- a1.lawyername
1503 if prm.lawyername is not null then
1504 sql := "dbSys".sqlAddwhere(sql, format( 'a1.lawyername = %L',prm.lawyername ), multi:=false , orderby_groupby := true );
1505
1506 end if;
1507
1508 --* Event options ----------------
1509 if prm.evTitles is not null then
1510 ss:= null;
1511 -- select * from "dbLegal".trcasetraneventvw_titles ( prm.casetranid ) into eventTitles;
1512 eventTitlesA = string_to_array(prm.evtitles, ',') ;
1513
1514 -- raise '0643: eventTitlesA %', eventTitlesA;
1515 if not prm.evEnable then
1516
1517 c := 0;
1518 ss:= null;
1519 foreach s in array eventTitlesA loop
1520 -- raise notice '0658: s %, eventTitlesA %, 1= %', s, eventTitlesA, eventTitlesA[1];
1521
1522 c = c + 1;
1523 -- case when 'วันที่รับเรื่อง' = any( g._titles ) then g._startdates[1] is null else true end
1524 -- ss:= format('case when %L = any( g._titles ) then g._startdates[%s] is null else true end ', s, i );
1525 -- {วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่รับเรื่อง,วันที่ยื่นฟ้อง,วันนัดไกล่เกลี่ย,วันนัดสืบพยานโจทก์,วันที่ขอแก้ไขคำฟ้อง,วันที่รับชำระหนี้ก่อนฟ้อง,วันที่ส่งคำบังคับ,วันที่ส่งหมายเรียก,วันที่ทำสัญญายอม,วันที่พิพากษา}
1526
1527 -- ss = concat_ws( case when c>1 then chr(13) else '' end || ' or ', ss, format('case when %L = any( g._titles ) then g._startdates[array_position(g._titles, %L ) ] is null else true end ', s, s ) );
1528 ss = concat_ws( chr(13) || case when c>1 then ' or ' else ' ' end , ss, format(
1529' case when any_exist( g._titles, %L ) >0 then ( --A
1530 select startdate from "dbLegal".trcasetranevent where pid = a.id and id = any(
1531 array_values_by_pos( g._ids, array_exists( g._titles, %L ) ) --B
1532 ) and startdate is null limit 1
1533 ) is null else true end '
1534 , s --A
1535 , s --B
1536
1537 ) );
1538
1539 end loop;
1540 --*
1541 prm.evreporttitle := format( '%s %s',prm.evDate_title_NoDate,prm.evTitles ); -- ที่ยังไม่ได้กำหนดวันที่
1542
1543 else --- evEnable
1544 date_clause = null;
1545 --* date clause
1546 if prm.evFR is null and prm.evTO is null then
1547 date_clause = format(' is not null ' );
1548 date_title = prm.evDate_title_HaveDate ; -- ที่กำหนดวันที่แล้ว
1549 elseif prm.evFR is not null and prm.evTO is null then
1550 date_clause = format(' >= %L::date ', prm.evFR::text );
1551 --date_title = format( prm.evDate_title_FromDate, prm.evFR::text ); -- ตั้งแต่วันที่ %s เป็นค้นไป
1552 date_title = format( prm.evDate_title_FromDate, to_date( prm.evFR ) ); -- ตั้งแต่วันที่ %s เป็นค้นไป
1553
1554 elseif prm.evFR is null and prm.evTO is not null then
1555 date_clause = format(' <= %L::date ', prm.evTO::text );
1556 -- date_title = format( prm.evDate_title_ToDate, prm.evFR::text ); -- ตั้งแต่วันที่ %s เป็นค้นมา
1557 date_title = format( prm.evDate_title_ToDate, to_date(prm.evTo) ); -- ตั้งแต่วันที่ %s เป็นค้นมา
1558 else -- prm.evFR is not null and prm.evTO is not null
1559 date_clause = format('between %L::date and %L::date ', prm.evFR::text , prm.evTO::text );
1560 -- date_title = format( prm.evDate_title_Between, prm.evFR::text, prm.evTO::text ); -- ระหว่างวันที่ %s ถึง %s
1561 date_title = format( prm.evDate_title_Between, to_date(prm.evFR), to_date(prm.evTO) ); -- ระหว่างวันที่ %s ถึง %s
1562
1563 end if;
1564
1565 --*
1566 c := 0;
1567 ss:= null;
1568 foreach s in array eventTitlesA loop
1569 c = c + 1;
1570 ss = concat_ws( chr(13) || case when c>1 then ' or ' else ' ' end , ss, format(
1571' case when any_exist( g._titles, %L ) >0 then ( --A
1572 select startdate from "dbLegal".trcasetranevent where pid = a.id and id = any(
1573 array_values_by_pos( g._ids, array_exists( g._titles, %L ) ) --B
1574 ) and startdate is not null limit 1
1575 ) %s else false end ' --C
1576 , s --A
1577 , s --B
1578 , date_clause-- C
1579 ) );
1580
1581 end loop;
1582
1583 --* Reporttitle: สำหรับ การรับเรื่อง,การยื่นฟ้อง ระหว่างวันที่ 01/01/2559 - 31/03/2559
1584 prm.evreporttitle := format('สำหรับ %s %s',prm.evTitles, date_title );
1585
1586 -- raise '0703: sql=%',sql;
1587 end if;
1588
1589
1590 --raise '0652: ss=%', ss;
1591 sql := "dbSys".sqlAddwhere(sql, format(
1592'
1593(%s
1594)' ,ss
1595
1596 ) );
1597 end if;
1598
1599
1600 --*
1601 sql1:=sql;
1602
1603
1604 --*.2.2 : Show all asset
1605 sql22 = format(
1606'
1607select
1608 a.id caseid
1609 ,c1311.tvalue col_101 --ทะเบียนลูกค้า
1610 ,c1.tranno col_102 -- เลขที่สัญญา
1611 ,c1.signdate col_103 -- วันที่ทำสัญญา
1612from "dbLegal".trcasetran a
1613 join %s a1 on a1.id = a.id -- A-0
1614 left join "dbLegal".trcasetranasset c on c.pid = a.id and c.kind is null and c.orderno = 1
1615 left join "dbLegal".trcaseassetVW_UserData(
1616 1 -- %s --A-1
1617 ,%s --A-2
1618 ) c1 on c1.id = c.caseassetid --
1619 --left join "dbLegal".trcaseassetno c11 on c11.pid = c1.id
1620 --left join "dbLegal".trcaseassetcont c12 on c12.pid = c1.id
1621 left join "dbLegal".trcaseassetpart c13 on c13.pid = c1.id and c13.tvalue = a1.defendant1_id
1622 left join "dbContExt".tbloanee c131 on c131.contactid = c13.tvalue
1623 left join "dbContact".tbcontnumber c1311 on c1311.pid = c131.contactid and c1311.title = %L and c1311.tcustom is null --A
1624
1625 where a.id = aa.casetranid -- lateral query
1626 order by c1311,c1.tranno
1627'
1628 ,tbview99 -- A-0
1629 , prm.uid --A-1
1630 , prm.acc --- A-2
1631 , 'CIF' --A
1632
1633 );
1634
1635 sql21:= format(
1636'
1637select
1638 a.caseid
1639 ,string_agg ( %s a.col_41,%L) col_41
1640 ,string_agg ( %s a.col_42,%L) col_42
1641 ,string_agg ( %s a.col_43,%L) col_43
1642from (%s) a
1643group by a.caseid
1644
1645'
1646 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1647 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1648 ,case when prm.show_distinct then 'distinct' else '' end, prm.show_del
1649 ,sql22
1650 );
1651
1652
1653 --* Finall SQL -------------------------------------
1654 sql := format(
1655 '
1656select
1657 aa.*
1658 , b.*
1659from (%s
1660) aa
1661 left join lateral (%s) b on b.caseid = aa.casetranid
1662 '
1663 ,sql1
1664 , case when prm.show_asset then sql21 else sql22 end
1665
1666 );
1667 -- raise '0679:sql=%',sql;
1668 --* save parameter
1669 execute format('update "dbLegalEDU".rpCaseAR
1670 set evreporttitle = %L --A
1671 where id = %s '
1672 ,prm.evreporttitle
1673 ,prm.id
1674 )
1675 ;
1676
1677
1678end $$ language plpgsql;
1679
1680
1681
1682drop function if exists "dbSys".grpTitles(
1683 in _title ttext -- default
1684 ,in _idgrp int
1685 ,tbl ttext
1686);
1687
1688drop function if exists "dbSys".grpTitles(
1689 in _title ttext -- default
1690 ,in _idgrp int
1691 ,tbl ttext
1692 ,_sql text
1693);
1694
1695
1696drop function if exists "dbSys".grpTitles(
1697 in _title ttext -- default
1698 ,in _idgrp int
1699 ,tbl ttext
1700 ,_sql text
1701 ,_c int
1702 -- ,in title_name text = 'title'
1703);
1704
1705drop function if exists "dbSys".grpTitles(
1706 in _title ttext -- default
1707 ,in _idgrp int
1708 ,tbl ttext
1709 ,_sql text
1710 ,_c int
1711 -- ,in title_name text = 'title'
1712);
1713
1714drop function if exists "dbSys".grpTitles(
1715
1716 in _title ttext -- default
1717 ,in _idgrp int
1718 ,in tbl ttext
1719 ,in _sql text
1720 ,in _c int
1721 ,in check_exist boolean
1722
1723 -- ,in title_name text = 'title'
1724);
1725
1726create or replace function "dbSys".grpTitles(
1727
1728 in _title ttext -- default
1729 ,in _idgrp int
1730 ,in tbl ttext
1731 ,in _sql text = ' select a.id,a.title, a.idgrp from %s a where a.id = $1'
1732 ,in _c int =1
1733 ,in check_exist boolean = false
1734 ,in CH_delimeter text = ' '
1735 -- ,in title_name text = 'title'
1736) returns text as $$
1737declare grp record; result ttext = null; title ttext; ok boolean;
1738 _stop boolean=false ;
1739 _check_exist boolean = check_exist;
1740 _CH_delimeter text = CH_delimeter;
1741begin
1742 -- if _c>10 then raise '6771'; end if;
1743 result:=_title;
1744 if _idgrp is not null then
1745 -- execute format ( ' select a.id,a.title, a.idgrp from %s a where a.id = $1' , tbl )
1746 execute format ( _sql , tbl )
1747 using _idgrp
1748 into grp;
1749
1750 -- raise notice '6761: _idgrp %, grp.id %, gpr.title %, grp.idgrp %, result %' , _idgrp , grp.id, grp.title , grp.idgrp, result;
1751
1752 if grp.id is not null then
1753 -- raise '6767: position=%, grp.title %', position( grp.title in result ), grp.title;
1754 -- _stop := grp.title = 'Cloudora Justice';
1755 --if not _stop then
1756 if grp.title <> 'Cloudora Justice' then -- title_task_legal
1757 -- raise notice '0030: result %,grp.title %', result, grp.title;
1758 /*
1759 if result is null then
1760 result:= concat_ws (ch_delimeter, result, grp.title );
1761 -- result:= concat_ws (ch_delimeter, case when position(result in grp.title )=0 then result else null end , grp.title );
1762 else
1763 result:= concat_ws (ch_delimeter, result, grp.title );
1764
1765 end if; */
1766 result:= concat_ws (ch_delimeter, result, grp.title );
1767
1768 -- raise notice '0018: result %,grp.title %', result, grp.title;
1769
1770
1771 --*
1772 ok:= case
1773 when _c=1 then case
1774 when grp.title is not null then case
1775 when _title is not null then case
1776 when position( _title in grp.title) >0 then true
1777 else false
1778 end
1779 else false
1780 end
1781 else false
1782 end
1783 else false
1784 end ;
1785
1786 --- raise notice '6789:ok %, _c %, grp.title %, _idgrp %, grp.idgrp %, positiong() %',ok, _c, grp.title, _idgrp, grp.idgrp, position( _title in grp.title ) ;
1787 -- 6789:ok f, _c 1, grp.title >> วันที่ยื่นฟ้อง=07/04/2019 เลขดำ=blackno 001 เลขแดง=readno 002 <<, _idgrp 1123, grp.idgrp 412, positiong() <NULL>
1788
1789
1790
1791 --if _c = 1 and position( grp.title in result) >0 then
1792 if ok then
1793
1794 title = result;
1795 else
1796 if not check_exist then
1797 -- raise notice '0066: gr.title "%", result "%"', grp.title, result ;
1798
1799 title = concat_ws(CH_delimeter,result, trim( grp.title) );
1800
1801 else
1802 -- raise notice '7044: gr.title "%", result "%"', grp.title, result ;
1803 --*
1804
1805 if position (trim( grp.title) in result ) =0 then
1806
1807 title = concat_ws(ch_delimeter,result, trim( grp.title) );
1808 else
1809 -- raise notice '0073:title %',title;
1810 title = result;
1811 end if;
1812
1813 end if;
1814 end if;
1815
1816 -- raise notice '6779:title %', title;
1817 result := "dbSys".grpTitles( title , grp.idgrp, tbl, _sql, _c :=_c+1 ,check_exist:= _check_exist, CH_delimeter:=_CH_delimeter );
1818 -- raise notice '0082:result %,title %', result, title;
1819 end if; -- grp.title <> 'Cloudora Justice'
1820 end if;
1821 end if;
1822
1823 return result;
1824
1825end $$ language plpgsql stable;