Advertisement
Guest User

Untitled

a guest
Jun 16th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.60 KB | None | 0 0
  1. create or replace function "dbLegal".trcasetranEvent_DefaultSysEvent()
  2. returns trigger as $$
  3. declare find record;
  4. -- caltask record;
  5. rec record; ok boolean; input_step int=new.lgstepproitemid;
  6. begin
  7.  
  8.  
  9. -- raise '677:trcasetranEvent_DefaultSysEvent()';
  10. -- raise '0873:new.lgstepproitemid %, new.lgstepproitemeventid %', new.lgstepproitemid, new.lgstepproitemeventid;
  11.  
  12. --* Only system event
  13. if new.lgstepproitemeventid is not null then
  14. --* key in directly.
  15. if new.sysevent is null then
  16.  
  17. /*select sysevent from "dbLegal".tblgstepproitemevent where id = new.lgstepproitemeventid
  18. into new.sysevent; */
  19.  
  20. execute format('select sysevent from "dbLegal".tblgstepproitemevent where id = $1.lgstepproitemeventid') using new
  21. into new.sysevent;
  22.  
  23. end if;
  24. else
  25. if new.sysevent is not null then
  26. --if new.lgstepproitemeventid is null then
  27. if true then
  28. execute format('
  29. select b.id
  30. from "dbLegal".trcasetran a
  31. left join "dbLegal".tblgstepproitemevent b on b.lgstepproid = a.lgstepproid and b.sysevent = $1.sysevent
  32. where a.id = $1.pid '
  33.  
  34.  
  35. ) using new
  36. into new.lgstepproitemeventid ;
  37. -- raise notice '701: new.title %, new.lgstepproitemeventid %', new.title, new.lgstepproitemeventid ;
  38. end if;
  39. elseif new.title is not null then
  40.  
  41. /*
  42. execute format('
  43. select b.id
  44. from "dbLegal".trcasetran a
  45. left join "dbLegal".tblgstepproitemevent b on b.lgstepproid = a.lgstepproid and b.title = $1.title and b.sysevent is null
  46. where a.id = $1.pid '
  47. ) using new
  48. into new.lgstepproitemeventid;
  49. */
  50. execute $x$
  51. select b.id
  52. , b.pid
  53. ,b.sysevent
  54. from "dbLegal".trcasetran a
  55. left join "dbLegal".tblgstepproitemevent b on b.lgstepproid = a.lgstepproid
  56. where a.id = $1.pid and b.title = $1.title
  57. $x$ using new
  58. into new.lgstepproitemeventid
  59. , new.lgstepproitemid -- override active step , if any
  60. , new.sysevent
  61. ;
  62.  
  63.  
  64. -- raise '1002: new.lgstepproitemeventid %, new.lgstepproitemid; % ', new.lgstepproitemeventid, new.lgstepproitemid;
  65.  
  66. --* not keep title, if found
  67. if new.lgstepproitemeventid is not null then
  68. new.title := null;
  69. else
  70. --* Use input step if not found
  71. --if new.lgstepproitemeventid is null then
  72. new.lgstepproitemid = input_step;
  73. -- end if;
  74. end if;
  75.  
  76. end if;
  77. end if;
  78.  
  79. --* Get from step if still null
  80. if new.lgstepproitemid is null then
  81. if new.lgstepproitemeventid is not null then
  82. execute $x$
  83. select pid, eventmulti from "dbLegal".tblgstepproitemevent where id = $1 .lgstepproitemeventid
  84. $x$ using new
  85. into new.lgstepproitemid, new.eventmulti ;
  86. --raise notice '1286 : new.lgstepproitemid "%"', new.lgstepproitemid;
  87. end if;
  88. end if;
  89.  
  90. -- raise notice '1210:new.lgstepproitemeventid"%", new.sysevent"%", new.eventmulti"%",new.lgstepproitemid"%"' ,new.lgstepproitemeventid, new.sysevent, new.eventmulti, new.lgstepproitemid;
  91.  
  92. --* Refresh profile attributs
  93. /*
  94. ok:=case
  95. when new.eventmulti is null then true
  96. when new.EventAmtFlag is null then true
  97. --else false
  98. when new.eventmulti or new.EventAmtFlag then true
  99. when new.debtadj is null then true
  100. else false
  101. end ;
  102. */
  103. ok=true ;
  104.  
  105. -- raise '705:ok %, new.lgstepproitemeventid %', ok, new.lgstepproitemeventid ;
  106. if ok then
  107. if new.lgstepproitemeventid is not null then
  108. /*
  109. execute format( '
  110. select
  111. case
  112. when a.eventmulti is null then false
  113. else a.eventmulti
  114. end
  115. ,a.eventamt EventAmtFlag
  116. ,a.debtadj
  117. ,a.mustremark
  118. ,a.eachdefd
  119. from "dbLegal".tblgstepproitemevent a where a.id = $1.lgstepproitemeventid '
  120. ) using new
  121. into rec ;
  122. -- raise '715:new.eventmulti %', new.eventmulti;
  123. new.eventmulti:= rec.eventmulti;
  124. new.EventAmtFlag:= rec.EventAmtFlag;
  125. new.debtadj := rec.debtadj;
  126. new.mustremark := rec.mustremark;
  127. new.eachdefd := rec.eachdefd;
  128. */
  129.  
  130. execute $x$
  131. select
  132. a.eventmulti
  133. ,a.eventamt EventAmtFlag
  134. ,a.debtadj
  135. ,a.mustremark
  136. ,a.eachdefd
  137. from "dbLegal".tblgstepproitemevent a where a.id = $1.lgstepproitemeventid
  138. $x$ using new
  139. into
  140. new.eventmulti
  141. ,new.EventAmtFlag
  142. ,new.debtadj
  143. ,new.mustremark
  144. ,new.eachdefd
  145. ;
  146.  
  147. end if;
  148. end if;
  149. -- raise '766:new.sysevent %, lgstepproitemid %', new.sysevent, new.lgstepproitemid;
  150. return new;
  151. end ; $$ language plpgsql;
  152.  
  153.  
  154. create or replace FUNCTION "dbLegal".trcasetranVW_UserData ( in UserID int , in AccID int
  155. ,in _id int = null
  156. ,in _assignment boolean = false -- assignment
  157. ,in _casekind smallint = null
  158. ,in _wkfrec_name text = null
  159.  
  160. )
  161. returns table (
  162. id int
  163.  
  164. ,ReceiveNo text -- varchar -- b.Tvalue เลขที่รับเรื่อง -- ReceiveNo
  165. ,Title text -- ,a.Title เรื่อง
  166.  
  167. ,ReceiveDate timestamp_wtz -- วันที่รับเรื่อง -- ReceiveDate
  168. ,CustID int
  169. ,CustName text -- concat_ws(' ',i1.Prefix, i1.FirstName ,i1.LastName, i1.Suffix, '-', i1.ID )ลูกค้า -- Customer
  170. ,Defendant boolean -- ฝ่าย -- Defendant vs Complaintor
  171. ,CourtRole text
  172.  
  173. ,ReceiverID int
  174. ,ReceiverName text -- concat_ws(' ', da.Firstname,da.Lastname,'-', d.TValue ) ผู้รับเรื่อง -- ReceiveBy
  175. ,OrgID int -- 10
  176. ,OrgName text -- concat_ws(' ',f1.Prefix, f1.FirstName ) หน่วยงาน -- Org
  177. ,LawOfficerID int
  178. ,LawOfficerName text -- concat_ws(' ',h1.Prefix, h1.FirstName ,h1.LastName, h1.Suffix, '-', h1.ID ) นิติกร -- LawOfficer
  179. ,lawofficer_contactid int
  180. ,LawOfficeID int
  181. ,LawOfficeName text -- concat_ws(' ',o1.Prefix, o1.FirstName ,o1.LastName, o1.Suffix, '-', o1.ID ) สำนักงานทนาย-- LawOffice
  182. ,LawyerID int
  183. ,LawyerName text -- concat_ws(' ',p1.Prefix, p1.FirstName ,p1.LastName, p1.Suffix, '-', p1.ID ) ทนาย
  184. ,LGTypeID int
  185. ,LGTypeName text -- e.Title ประเภทคดี --LGType
  186. ,ProfileID int -- 20
  187. ,ProfileName text -- concat_ws(' ',e1.Title,'-',e1.ID) โปรไฟล์คดี --- LegalProfile
  188. ,ActiveStepID int
  189. ,ActiveStepName text -- concat_ws(' ',l1.Title,'-',l1.id) ขั้นตอนคดีที่แอคทีพ -- ที่กำลังดำเนินการ Active step
  190. ,LGNo text -- varchar -- b1.TValue LGNo
  191. ,TranDate timestamp_wtz -- วันที่บันทึกข้อมูล
  192. ,TranDate_Dis timestamp_wtz -- วันที่ตั้งคดี = วันที่จ่ายคดีให้ สนง. ทนาย
  193. ,BlackNo text -- varchar -- j.TValue คดีดำ --BlackNo
  194. ,RedNo text -- varchar -- k.TValue คดีแดง -- RedNo
  195. -- ,CalenNameID int
  196. ,UID int
  197. ,ACC int -- 30
  198. ,ACNo text
  199.  
  200. ,Status text -- wk status
  201. ,wkfstatid int
  202. ,CaseComplete "dbSys".Percent
  203. ,CaseComplete_date timestamp_wtz
  204. ,CalTaskID int
  205. ,amt_total num -- จำนวนเงิน ทุุนทรัพย์
  206. ,CourtID int -- updatable
  207. ,CourtName text
  208. ,CourtAreaName text
  209. ,sue_date timestamp_wtz
  210. ,BlackNoBD text
  211. ,RedNoBD text -- 40
  212. ,Groups text
  213. ,remark text
  214. ,c_defd smallint -- Count of defendant
  215. ,docid int
  216.  
  217. ,CaseStatusID int
  218. ,CaseStatusName text
  219. ,LawCase text
  220. ,defendant1_id int
  221. ,Defendant1_Name text
  222. ,docno_client text -- เลชที่แฟ้มคดี ของลูกค้า
  223.  
  224. ,keyword text
  225. -- ,casetranid int
  226. ,auto_copy boolean
  227. ,casekind smallint
  228. ,assign_lawoff boolean
  229. ,taskid int
  230. --,casetranid int
  231. ,upddt timestamp_wtz
  232. ,updateamount boolean
  233. ,eleadevent_enable boolean
  234. ,assettypeid int
  235. ,assettype text
  236. ,refid int
  237. ,reflgno text
  238. ,reftrandate timestamp_wtz
  239. ,bankid int -- bank contact id
  240. ,BankName text
  241. ,BankBranchID int --branch contact id
  242. ,BankBranchName text
  243. ,tgCalculate boolean
  244. ,tgPrintForm boolean
  245. ,caseasset_gen boolean
  246. ) AS $$
  247. declare
  248. parm "dbSys".tbviewregis_userdata_type ;
  249. v_wkfrec_name text = format('"dbSysWKF".trcasetrnvw%s_wkfrec',accid) ;
  250.  
  251. begin
  252.  
  253. select userid, accid, _id, null::Int _pid, null::int _idgrp into parm;
  254.  
  255. return query execute $x$
  256.  
  257. select
  258. a.id
  259. ,b.Tvalue -- เลขที่รับเรื่อง -- ReceiveNo
  260. --,a.Title -- เรื่อง
  261. ,case when a.title is null then a3.title else a.title end Title
  262. ,n2.StartDate::timestamp_wtz -- วันที่รับเรื่อง -- ReceiveDate
  263. ,a.CustID
  264. --,concat_ws(' ',i1.Prefix, i1.FirstName ,i1.LastName, i1.Suffix ) customer -- ลูกค้า -- Customer
  265. ,i1._name customer -- ลูกค้า -- Customer
  266.  
  267. , a.Defendant ฝ่าย -- Defendant vs Complaintor
  268. ,a.CourtRole
  269. ,d.tvalue ReceiverID
  270. --,concat_ws(' ', da.Firstname,da.Lastname ) ReceiveBy --- ผู้รับเรื่อง --
  271. ,da._name ReceiveBy --- ผู้รับเรื่อง --
  272.  
  273. ,a.OrgID
  274. --,concat_ws(' ',f1.Prefix, f1.FirstName ) หน่วยงาน -- Org
  275. ,f1._name org -- หน่วยงาน -- Org
  276.  
  277. ,h2.id LawOfficerID -- h.tvalue LawOfficerID -- contact id
  278. --,concat_ws(' ',h1.Prefix, h1.FirstName ,h1.LastName, h1.Suffix ) lawofficer -- นิติกร -- LawOfficer
  279. ,h1._name lawofficer -- นิติกร -- LawOfficer
  280.  
  281. ,h1.id lawofficer_contactid
  282.  
  283. ,a.lawofficeid --- o2.id LawOfficeID
  284. --,concat_ws(' ',o1.Prefix, o1.FirstName ,o1.LastName, o1.Suffix ) สำนักงานทนาย-- LawOffice
  285. ,o1._name lawoffice -- สำนักงานทนาย-- LawOffice
  286.  
  287.  
  288. --,p2.id LawyerID --p.tvalue LawyerID
  289. ,a.lawyerid -- ,p2.id LawyerID --p.tvalue LawyerID
  290. -- ,concat_ws(' ',p1.Prefix, p1.FirstName ,p1.LastName, p1.Suffix ) LawyerName -- ทนาย
  291. ,p1._name LawyerName -- ทนาย
  292.  
  293. ,a.LGTypeID
  294. ,e.Title ประเภทคดี --LGType
  295. --,e.LGStepProID -- Profile id
  296. ,a.LGStepProID -- Profile id
  297. ,concat_ws(' ',e1.Title )
  298. ,l.LGStepProItemID -- LGStepProItem id
  299. ,"dbLegal".trcasetranVW_grpTitle_active(
  300. l1.title -- d.title
  301. ,l.idgrp ) LGStepProItemTitle
  302. ,a.LGNo
  303. ,a.trandate -- วันที่บันทึกข้อมูล
  304. ,a.TranDate_Dis
  305. ,j.TValue คดีดำ --BlackNo
  306. ,k.TValue คดีแดง -- RedNo
  307. -- ,a.CalenNameID
  308. ,a.UID
  309. ,a.ACC
  310. ,a.tranno -- ACNo , a/c no
  311.  
  312. --,"dbSysWKF".wkfrecord_status( q11.orderno, q11.title, q11.statusname , app:=q1.app , StatusAlias :=true ) Status -- wk status
  313. -- ,"dbSysWKF".wkfrecord_status( q1.orderno, q1.title, q1.statusname , app:=q1.app , StatusAlias :=true ) Status -- wk status
  314. -- ,"dbSysWKF".fnworkflow_act_status_wkfrec( a.id, case when _wkfrec_name is not null then _wkfrec_name else '"dbSysWKF".trcasetran_wkfrec' end ) status
  315. ,q1.status -- wkf status
  316. ,q1.wkfstatid -- wkf status id
  317. ,a.CaseComplete
  318. ,a.CaseComplete_date
  319. ,a.caltaskid
  320. ,a.total amt_total -- ทุนทรัพย์
  321. ,a.courtid
  322. --,concat_ws( ' ',r11.prefix, r11.FirstName, r11.LastName,r11.LastName ) CourtName
  323. ,r11._name CourtName
  324. ,r1.SiteName ::text CourtAreaName
  325. ,(b111.startdate ) ::timestamp_wtz -- sue_date
  326. ,to_date ( r2.tvalue,yt:=1, fm:='yyyy' ) BlackNoBD -- BD Year - Black no
  327. ,to_date ( r3.tvalue,yt:=1, fm:='yyyy' ) RedNoBD -- BD Year - Red No
  328. --,"dbLegal".trcasetranGrp_getgroups( a.id ) CaseGrps
  329. ,a.CaseGrps
  330. ,a.remark
  331. --,s3.fqty c_defd
  332. ,s1.fqty c_defd
  333. ,a.docid
  334. ,a.casestatusid
  335. ,a2.title casestatusname
  336. ,a.lawcase
  337. ,s1.contactid defendant1_id
  338. ,s1.contactname Defendant1_Name
  339. ,a.docno_client
  340. ,null::text -- "dbLegal".tbcasestran_fulltext_search(a.id) -- keyword
  341. ,a.auto_copy
  342. ,a.casekind
  343. ,a.assign_lawoff
  344. ,a.taskid
  345. ,a.upddt
  346. ,a.updateamount
  347. ,a.eleadevent_enable
  348. ,a.assettypeid
  349. ,a11.title assettype
  350. ,a.refcasetranid refid
  351. ,a12.lgno reflgno
  352. ,a12.trandate reftrandate
  353. ,a13.id -- bank id
  354. ,a13.fullname -- bank name
  355. ,a14.id -- bankbranch id
  356. ,a14.fullname -- bankbranch name
  357. ,false -- tgCalculate boolean
  358. ,false -- tgPrintForm boolean
  359. ,a.caseasset_gen
  360. from
  361. "dbLegal".trcasetran a
  362. left join "dbLegal".tbassettype a11 on a11.id = a.assettypeid
  363. left join "dbLegal".trcasetran a12 on a12.id = a.refcasetranid
  364. left join "dbLegal".trcasetrancont_contact(a.id, 'Bank' ) a13 on a13.pid = a.id
  365. left join "dbLegal".trcasetrancont_contact(a.id, 'BankBr' ) a14 on a13.pid = a.id
  366. left join "dbLegal".tbcasestatus a2 on a2.id = a.casestatusid
  367. left join "dbCalen".tbcaltask a3 on a3.id = a.caltaskid
  368. left outer join "dbLegal".trcasetranNo b on (b.pid = a.id and b.Title = 'Receive' and b.tcustom is null )
  369. left outer join "dbLegal".trcasetranDate c on (c.pid = a.id and c.Title = 'Receive'and c.tcustom is null )
  370. left outer join "dbLegal".trcasetranCont d on (d.pid = a.id and d.Title = 'Receive'and c.tcustom is null)
  371. -- left outer join "dbContact".tbContact da on (da.id = d.TValue )
  372. left outer join "dbContact".tbContact_fullname( d.tvalue) da on (da._id = d.TValue )
  373.  
  374. left outer join "dbLegal".tbLGType e on (e.id = a.LGTypeID )
  375. left outer join "dbLegal".tbLGStepPro e1 on (e1.ID = e.LGStepProID )
  376. left outer join "dbHR".tbOrg f on (f.id = a.OrgID )
  377. left outer join "dbContact".tbContact_fullname(f.contactid) f1 on (f1._ID = f.ContactID )
  378.  
  379. left outer join "dbLegal".trcasetranCont h on ( h.PID = a.id and h.Title = 'Law Officer' )
  380. -- left outer join "dbContact".tbContact h1 on (h1.ID = h.TValue )
  381. left outer join "dbContact".tbContact_fullname( h.tvalue) h1 on (h1._ID = h.TValue )
  382.  
  383. left join "dbHR".tbemp h2 on (h2.contactid = h.tvalue )
  384.  
  385. left outer join "dbContExt".tbCust i on (i.ID = a.CustID )
  386. -- left outer join "dbContact".tbContact i1 on (i1.ID = i.ContactID )
  387. left outer join "dbContact".tbContact_fullname(i.contactid ) i1 on (i1._ID = i.ContactID )
  388.  
  389. left outer join "dbLegal".trcasetranNo j on (j.PID = a.ID and j.Title = 'Black' and j.tcustom is null )
  390. left outer join "dbLegal".trcasetranNo k on (k.PID = a.ID and k.Title = 'Red' and k.tcustom is null )
  391.  
  392. left join "dbLegal".trcasetranStep l on (l.PID = a.ID and l.Active )
  393. left join "dbLegal".tbLGStepProItem l1 on (l1.ID = l.LGStepProItemID )
  394. left join "dbLegal".trcasetranevent_getdata1(a.id ,'Receive', strict:=false ) n2 on n2.casetranid = a.id
  395. --left join "dbLegal".trcasetranevent n2 on n2.pid = a.id and n2.sysevent = 'Receive'
  396.  
  397.  
  398. left join "dbLegal".tblawoffice o2 on (o2.id = a.lawofficeid )
  399. --left outer join "dbContact".tbContact o1 on (o1.id = o2.contactid )
  400. left outer join "dbContact".tbContact_fullname(o2.contactid ) o1 on (o1._id = o2.contactid )
  401.  
  402. left join "dbLegal".tblawyer p on (p.id = a.lawyerid )
  403. --left join "dbContact".tbContact p1 on (p1.id = p.contactid )
  404. left join "dbContact".tbContact_fullname(p.contactid) p1 on (p1._id = p.contactid )
  405.  
  406. --* wkf status
  407. -- left join "dbSysWKF".trcasetran_wkfrec q1 on ( q1.pid = a.id and q1.active ) -- ** Active status
  408. -- left join "dbSysWKF".tbwkfstat q11 on ( q11.id = q1.wkfstatid )
  409.  
  410. --left join "dbSysWKF".fnworkflow_status_wkfrec ( a.id, $3, _active:=true ) q1 on q1.pid = a.id
  411. left join "dbSysWKF".fnworkflow_status_wkfrec ( $1.accid, $3 , _active:=true ) q1 on q1.pid = a.id
  412.  
  413. left join "dbLegal".tbcourt r1 on (r1.id = a.courtid )
  414. -- left join "dbContact".tbcontact r11 on r11.id = r1.contactid
  415. left join "dbContact".tbcontact_fullname(r1.contactid) r11 on r11._id = r1.contactid
  416.  
  417. left join "dbLegal".tbcasetrandc_contact1
  418. ( a.id
  419. ,title := "dbLegal".trcasetrandc_contra_title_by_courtrole( a.courtrole )
  420. ) s1 on s1.id = a.id
  421. left outer join "dbContact".tbContAddr s2 on ( s2.pid = s1.id and s2.TCustom is null and s2.Title = 'Home') -- ทะเบียนบ้าน
  422. left outer join "dbContact".tbAddr s21 on ( s21.id = s2.TValue )
  423. left join "dbLegal".trcasetranevent_getdata(a.id,s1.contactid,'Notice' ) s22 on s22.casetranid = a.id
  424. left join "dbLegal".trcasetranevent_getdata(a.id,s1.contactid,'NoticeRec' ) s23 on s23.casetranid = a.id
  425. left join "dbLegal".trcasetranevent_getdata1(a.id ,'Sue' ) b111 on b111.casetranid = a.id
  426. -- BD Year
  427. left join "dbLegal".trcasetrandate r2 on ( r2.pid = a.id and r2.title = 'BlackNo' )
  428. left join "dbLegal".trcasetrandate r3 on ( r3.pid = a.id and r3.title = 'RedNo' )
  429.  
  430.  
  431. where
  432. case
  433. when $1._id is null then a.acc= $1.accid
  434. else a.id = $1._id
  435. end
  436. and case
  437. when $2 is null then a.casekind is null
  438. else a.casekind = $2
  439. end
  440.  
  441. order by
  442. n2.StartDate -- วันที่รับเรื่อง
  443. ,a.lgno -- LG No
  444. $x$ using parm, _casekind
  445. ,case
  446. when _wkfrec_name is not null then _wkfrec_name
  447. else case
  448. when "dbSys".havetable( v_wkfrec_name ) then v_wkfrec_name
  449. else '"dbSysWKF".trcasetran_wkfrec'
  450. end
  451. end
  452. ;
  453.  
  454. end; $$ LANGUAGE plpgsql stable PARALLEL SAFE;
  455.  
  456.  
  457. create or replace function "dbLegal".trcasetranevent_getdata1 (
  458. inout casetranid int -- case id
  459. ,in _sysevent text
  460. ,in strict boolean = true
  461. ,out StartDate date
  462. ,out caltaskid int
  463. ,out debtcalcid int
  464. -- ,out casetraneventid int
  465. -- ,out eventno smallint
  466. ) returns record as $$
  467. begin
  468. /*
  469. select
  470. s23.startdate
  471. ,s23.caltaskid
  472. ,s23.debtcalcid
  473.  
  474. from "dbLegal".trcasetranevent s23
  475.  
  476. left join "dbLegal".tblgstepproitemevent b on b.id = s23.lgstepproitemeventid
  477. where s23.pid = casetranid
  478. and case
  479. when strict then b.sysevent = _sysevent
  480. else s23.sysevent = _sysevent
  481. end
  482. order by EventNo desc
  483. limit 1
  484. into
  485. StartDate
  486. ,caltaskid
  487. ,debtcalcid
  488. ;
  489.  
  490. if startdate is null then
  491. startdate := (
  492. select b.startdate::date
  493. from "dbLegal".trcasetranevent a
  494. left join "dbCalen".tbcaltask b on b.id = a.caltaskid
  495. where a.pid = casetranid and a.title = vartext('legal_วันที่รับเรื่อง')
  496. ) ;
  497. end if;
  498. */
  499. execute $x$
  500. select
  501. s23.startdate
  502. ,s23.caltaskid
  503. ,s23.debtcalcid
  504. -- ,s23.id
  505. -- ,s23.eventno
  506. from "dbLegal".trcasetranevent s23
  507.  
  508. -- left join "dbLegal".tblgstepproitemevent b on b.id = s23.lgstepproitemeventid
  509. where s23.pid = $2
  510. and s23.sysevent = $1
  511. -- and case
  512. -- when strict then s23.sysevent = $1 else s23.sysevent = $1
  513. -- end
  514. order by EventNo, orderno
  515. limit 1
  516. $x$ using _sysevent, casetranid
  517. into
  518. StartDate
  519. ,caltaskid
  520. ,debtcalcid
  521. -- ,casetraneventid
  522. -- ,eventno
  523. ;
  524.  
  525. end $$ language plpgsql stable ;
  526.  
  527.  
  528. create or replace function "dbLegal".trcasetran_UpdateAmount()
  529. returns trigger as $$
  530. declare
  531. ok boolean; amt_reset boolean; rec record; diff record; _total num;
  532. err text;
  533. begin
  534. -- raise notice '1555: new.updating_amount %', new.updating_amount;
  535. if diff( old.updateamount,new.updateamount ) then
  536. if new.updateamount then
  537. --* Not allow change if have its own case amt setting
  538. if numval( new.count_calc)=0 then
  539. -- raise '3999';
  540. if new.count_asset >0 then
  541. execute $x$
  542. select
  543. sum(a.caseamt ) caseassetamt
  544. ,sum(b.tvalue) casetranamt
  545. ,diff( sum(a.caseamt )::numeric , sum(b.tvalue)::numeric ) ok
  546. from "dbLegal".trcasetranasset a
  547. left join "dbLegal".trcasetranamt b on b.pid = a.pid
  548. where a.pid = $1.id
  549. group by a.pid
  550. $x$ using new
  551. into diff ;
  552.  
  553. else
  554. select true ::boolean ok into diff;
  555. end if;
  556.  
  557. -- raise '4007: ok % ', diff.ok ;
  558. if diff.ok then
  559.  
  560. --* select * from "dbLegal".trcasetranasset
  561. amt_reset:=false;
  562. if not amt_reset then
  563. execute $x$
  564. delete from "dbLegal".trcasetranamt where pid = $1.id
  565. $x$ using new;
  566. amt_reset =true;
  567. end if;
  568.  
  569. for rec in execute $x$
  570. select a.id, a.caseamtid , a.caseamt, a.caseamtdate
  571.  
  572. ,numval( b1.tvalue) amt_debtcalcsum
  573. from "dbLegal".trcasetranasset a
  574. left join "dbLegal".tbdebtcalc b on b.id = a.caseamtid
  575. left join only "dbLegal".tbdebtcalcsum b1 on b1.pid = a.caseamtid and b1.is_total
  576.  
  577. where a.pid = $1.id
  578. $x$ using new
  579. loop
  580. -- raise '4019:rec.caseamtdate %, rec.caseamt %, amt_reset %, rec.amt_debtcalcsum %', rec.caseamtdate, rec.caseamt, amt_reset, rec.amt_debtcalcsum ;
  581.  
  582. --* Refresh caseamt if out of sync to its caseamtid
  583. if diff( rec.caseamt, rec.amt_debtcalcsum) then
  584.  
  585. -- update "dbLegal".trcasetranasset set caseamt = rec.amt_debtcalcsum where id = rec.id ;
  586. update "dbLegal".tbdebtcalcitem set tvalue = tvalue - (rec.amt_debtcalcsum - rec.caseamt ) where pid = rec.caseamtid and
  587. id in (
  588. select id from "dbLegal".tbdebtcalcitem where pid = rec.caseamtid limit 1
  589. );
  590. end if;
  591.  
  592. --* add new
  593. --* Sum amt by caseasset amt
  594. select total from "dbLegal".tbdebtcalc_sum (
  595. new.id -- tar - casetran
  596. , rec.caseamtid -- source
  597. ,target_item_ds := '"dbLegal".trcasetranamt'
  598.  
  599. ) into _total;
  600. -- raise notice '4045:total %', _total;
  601. --* clear old value once it 's used to update casetranamt
  602. --perform "dbLegal".tbdebtcalc_clear_oldvalue( rec.caseamtid );
  603. execute $x$
  604. update "dbLegal".tbdebtcalc set old_total=null, old_c_item = null where id = $1
  605. $x$ using rec.caseamtid;
  606.  
  607.  
  608. end loop;
  609. end if; -- diff.ok
  610. else
  611. /*
  612. -- raise exception '0021:%', format('Can not change asset, it have asset calculate already');
  613. err := 'ไม่สามารถแก้ไขทุนทรัพย์ได้ เนื่องจากมีการบันทึกยอดทุนทรัพย์โดยตรงแล้ว';
  614. select msg from "dbSys".sysCallErrMsg(
  615. 113
  616. ,format('%I.%s',tg_table_schema, tg_table_name) -- talbename = description
  617. ,new.id
  618. ,NEW.uID
  619. ,errmsg := err
  620. )
  621. into err;
  622. raise exception '%', format(err ) ;
  623. */
  624. end if;
  625. --*
  626. select a.tvalue from only "dbLegal".trcasetranamt a where a.pid = new.id and a.is_total into _total;
  627. --* toggle to false
  628. execute $x$ update "dbLegal".trcasetran set updateamount=false,total=$2 where id = $1.id $x$ using old, numval( _total);
  629. end if;
  630. end if;
  631. return new;
  632. end ; $$ language plpgsql;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement