Advertisement
Guest User

Untitled

a guest
Feb 21st, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.39 KB | None | 0 0
  1. /****************************************************************
  2. tbTaskVW.sql
  3.  
  4. A collection of procedures related to View. Please follow this cnonvestional here.
  5.  
  6. 2 functions as mandatory for view :-
  7. - Prepare data for VIEW. Function name must be formated "Schema".tablenameVWM_UserData( ... )
  8. - Update data fro VIEW. Function name must
  9.  
  10. Wutirkai Pornchai
  11. 2015-10-21
  12. *****************************************************************/
  13. /******************************************
  14. View Procedures
  15. *******************************************/
  16. -- * Data selected for underlying user
  17. drop function if exists "dbUserAccReg".tbTaskVW_UserData ( in UserID int , in AccID int ) cascade;
  18. create or replace FUNCTION "dbUserAccReg".tbTaskVW_UserData ( in UserID int , in AccID int )
  19. returns table (
  20. id int
  21. --* Project mandatory
  22. ,title text
  23. --,Duration int
  24. ,Duration interval
  25.  
  26. ,StartDate date
  27. ,FinishDate date
  28. ,Complete numeric -- "dbSys".Percent -- num
  29.  
  30. ,ActualStart date
  31. ,ActualFinish date
  32. ,ActualComplete numeric -- "dbSys".Percent --num
  33. ,PredecessorID int
  34. ,Resources text
  35. ,Tags text
  36. ,ManDay text
  37. ,Remark text
  38. --*
  39. ,DBSchema text
  40. ,DBTable text
  41. ,DBFunction text
  42. --*
  43. ,Project text
  44. ,UID int
  45. ,ACC int
  46. ,PID int
  47. ,IDGrp int
  48. ,gpr text
  49. --,planCalTaskID int
  50. ,ActualCalTaskID int
  51. ,dis smallint
  52. ,orderno smallint
  53. ,fqty smallint
  54. ,path int[]
  55. --* ancestor
  56. ,CalenNameID int
  57. ,tasktypeid int
  58. ,venue text
  59. ,description text
  60. ,WholeDay boolean
  61. ,part_ids int[] -- array of contact id
  62. ,part_names text
  63.  
  64.  
  65. , SenderID int
  66. , receiverID int
  67. , ResponsibleID int
  68. --* Extend ------
  69. ,privilege text
  70. -- ,ProductTitle text
  71. --,Lookup text
  72. --,FileName text
  73. --,orderno smallint
  74. ,ViewID int
  75.  
  76. ) AS $$
  77. declare sql text; foundACC int;
  78. begin
  79.  
  80. return query
  81.  
  82. select
  83. a.*
  84. --* Extend ------------
  85. ,a01.tvalue privilege
  86. -- ,a02.tvalue FileName
  87. -- ,"dbSys".grpGetValues( c.invenid,'"dbInventory".tbinven','title' )
  88. --,aa.orderno
  89. ,a02.tvalue::int viewid
  90. from
  91. "dbPM".tbTaskVW_UserData ( UserID , AccID ) a
  92. -- left join "dbUserAccReg".tbtskpriv b on (b.pid = a.id )
  93. left join "dbPM".tbTkField a01 on (a01.pid = a.id and a01.Title = 'Custom' and a01.TCustom = 'privilege' )
  94. left join "dbPM".tbTkField a02 on (a02.pid = a.id and a02.Title = 'Custom' and a02.TCustom = 'ViewID' )
  95.  
  96. --left join "dbPM".tbproject c on (c.id= a.pid )
  97. -- left join "dbPM".tbTkField a02 on (a02.pid = a.id and a02.Title = 'Custom' and a02.TCustom = 'FileName' )
  98. --left join "dbPM".tbtask aa on (aa.id=a.id )
  99. ;
  100.  
  101. end;
  102. $$ LANGUAGE plpgsql ;
  103.  
  104.  
  105. /***************************
  106. Function : Get hierarch task data
  107. Wut, 2016-08-23
  108. *********************************/
  109. drop function if exists "dbUserAccReg".tbTaskVW_getdata (
  110. in newprojectid int
  111. ,in prodgroupid int
  112. ,in taskid int
  113.  
  114. );
  115.  
  116. create or replace function "dbUserAccReg".tbTaskVW_getdata (
  117. in newprojectid int
  118. ,in prodgroupid int
  119. ,in newuid int
  120. ,in newacc int
  121. ,in taskid int = null
  122.  
  123. )
  124. returns table(
  125. id int
  126. ,title text
  127. ,privilege text -- tags
  128. ,edition text
  129. ,viewRegistrationID int
  130. ,viewRegistrationID_Top int
  131.  
  132. ,pid int -- prodgroupid, task for group, not product id , from 2nd input parameter
  133. ,dis smallint
  134. ,path int[]
  135. ,no text
  136. ,taskid_grp int
  137. ,remark text
  138. ,consint text
  139. ,idgrp int
  140. ) as $$
  141. begin
  142. return query
  143. select
  144. a.id
  145. --,e.title
  146. ,a.title
  147. ,a01.tvalue privilege -- tags
  148. ,b.tags edition
  149. ,d.id viewRegistrationID
  150. ,d11.id viewRegistrationID_Top
  151.  
  152. ,prodgroupid pid -- task for group, not product id
  153. ,a.dis
  154. ,z.path
  155. ,c.no
  156. ,a.id taskid_grp
  157. ,a1.tvalue remark
  158. ,a2.consint
  159. ,a.idgrp
  160.  
  161. from "dbPM".tbtask a
  162. left join "dbPM".tbtkfield a1 on a1.pid =a.id and a1.title = 'Remark'
  163. --* constraint
  164. left join (
  165. select
  166. aa.id
  167. ,string_agg( aa1.tvalue, ',' ) consint
  168. from "dbPM".tbtask aa
  169. left join "dbSys".grpDelete_GetChild( aa.id ,'"dbPM".tbtask' ) bb on bb.idgrp = aa.id
  170. left join "dbPM".tbtkfield aa1 on aa1.pid =bb.id and aa1.title = 'Remark'
  171. left join "dbPM".tbTask_resources(bb.id) aa2 on aa2.id = bb.id
  172. where aa.pid = newprojectid
  173. and aa2.val is not null
  174. --and position('F' in aa2.val ) > 0
  175. and ExistStr('F', aa2.val::text ) > 0
  176. --and case when aa2.val is null then false else ExistStr('F', aa2.val ) > 0 end
  177.  
  178. --and ( position ( 'F' in "dbPM".tbTask_TagNames( bb.id, '"dbPM".tbTKTag', '"dbPM".tbpjtag' ) )>0 )
  179. -- and case when aa2.val is not null then position('F' in aa2.val ) > 0 else false end
  180.  
  181. group by aa.id
  182. ) a2 on a2.id =a.id
  183. left join "dbPM".tbProject a3 on ( a3.id= a.pid )
  184. left join "dbPM".tbTask_TagNamesII( a.id, '"dbPM".tbTKTag', '"dbPM".tbpjtag' ) b on (b.id = a.id )
  185. left join "dbInventory".tbinven c on (c.id = a3.invenid) -- new.invenid )
  186. right join "dbSys".grpGetData( newuid, newacc, '"dbPM".tbtask' ,_pid:=newprojectid , path_only:=true ) z on ( z.id=a.id )
  187. left join "dbPM".tbTkField a01 on (a01.pid = a.id and a01.Title = 'Custom' and a01.TCustom = 'privilege' )
  188. left join "dbPM".tbTkField a0301 on (a0301.pid = a.id and a0301.Title = 'DB Schema' )
  189. left join "dbPM".tbTkField a0302 on (a0302.pid = a.id and a0302.Title = 'DB Table' )
  190. left join "dbSys".tbviewregistration d on (d.sch = a0301.tvalue and d.nam = a0302.tvalue )
  191. left join "dbSys".tbtblregis d1 on ( d1.id = d.id )
  192. left join "dbSys".tbviewregistration d11 on (d11.id = d1.tblregisid_top )
  193. -- left join "dbCalen".tbcaltask e on e.id = a.plancaltaskid
  194.  
  195. where a.pid = newprojectid
  196. -- and case when b.tags is null then true else position ('F' in b.tags )=0 end
  197. and case when taskid is null then true else a.id = taskid end
  198. order by z.path ;
  199.  
  200.  
  201. end $$ language plpgsql;
  202.  
  203. -- * How to update underlying tables whose data is projected to this view
  204. drop function if exists "dbUserAccReg".tbTaskVW_UpdData() cascade;
  205. create or replace function "dbUserAccReg".tbTaskVW_UpdData()
  206. returns trigger as $$
  207. declare viewreg record; oldd record;
  208. sql text; sql1 text;
  209. rec record;
  210. const_ar text[];
  211. const text;
  212. const_key text;
  213.  
  214. const_dup_c int;
  215. prodtask record;
  216.  
  217. err text;
  218. begin
  219. -- raise notice '92 %, %', TG_NAME, TG_OP;
  220. if (TG_OP = 'INSERT') then
  221. if new.tags is not null then
  222. perform "dbUserAcc".tbprod_validateEditions( new.tags, warning := true );
  223. end if;
  224.  
  225. -- raise notice '224 new.id %, new.idgrp %, new.pid %', new.id,new.idgrp, new.pid;
  226.  
  227. if (new.dbSchema is not null ) and (new.dbtable is not null) then
  228. select a.id,a.sch,a.nam from "dbSys".tbviewregistration a
  229. where a.sch = trim(new.dbschema) and a.nam = lower(trim(new.dbtable))
  230. into viewreg;
  231. if viewreg.id is null then
  232. raise exception '%',format('No datasource "%s".%s',new.dbschema, new.dbtable);
  233. else
  234. select viewreg.sch, viewreg.nam, viewreg.id into new.dbschema, new.dbtable, new.viewid;
  235. select null::text dbschema , null::text dbtable, null::text viewid into oldd;
  236.  
  237. perform "dbContact".updContactChild( '"dbPM".tbTkField', NEW.id ,'DB Schema', null,OLDd.DBSchema,NEW.DBSchema,'TValue' );
  238. perform "dbContact".updContactChild( '"dbPM".tbTkField', NEW.id ,'DB Table', null,OLDd.DBTable,NEW.DBTable,'TValue' );
  239. perform "dbContact".updContactChild( '"dbPM".tbTkField', NEW.id ,'Custom', 'ViewID',oldd.viewid::text,new.viewid::text ,'TValue' );
  240.  
  241. end if;
  242. end if;
  243.  
  244. if new.privilege is null then
  245. if new.idgrp is not null then
  246. select tvalue from "dbPM".tbtkfield where pid=new.idgrp and title='Custom' and tcustom='privilege'
  247. into new.privilege;
  248. end if;
  249. end if;
  250.  
  251. if new.privilege is not null then
  252. insert into "dbPM".tbtkfield (title,tcustom, tvalue,pid) values ( 'Custom','privilege',new.privilege, new.id );
  253. end if;
  254.  
  255. --* Updating by insert to system product directly -------------
  256.  
  257. --raise notice '287 start' ;
  258. if new.idgrp is null then
  259. select
  260. b.*
  261. ,null::text consint_grp
  262. ,c2.id produgroupid
  263. from
  264. "dbPM".tbtask a
  265. left join "dbPM".tbProject c on ( c.id= a.pid )
  266. left join "dbUserAcc".tbprod c2 on (c2.title = "dbSys".grpGetValues( c.invenid, '"dbInventory".tbinven', 'title' ) ) -- 4
  267. left join "dbUserAccReg".tbTaskVW_getdata (
  268. new.pid -- projectid
  269. ,c2.id -- prodgroupid
  270.  
  271. ,new.uid
  272. ,new.acc
  273. ,taskid := new.id
  274. ) b on b.id = a.id
  275.  
  276. where a.id = new.id
  277. into rec;
  278. -- raise notice '305 new.id %,new.pid %, rec.produgroupid %, rec.pid %', new.id ,new.pid, rec.produgroupid, rec.pid ;
  279. else
  280.  
  281. --if new.idgrp is not null then
  282. sql := format(
  283. 'select
  284. b.*
  285. ,d.consint consint_grp
  286. ,c2.id produgroupid
  287. from
  288. "dbPM".tbtask a
  289. left join "dbPM".tbProject c on ( c.id= a.pid )
  290. left join "dbUserAcc".tbprod c2 on (c2.title = "dbSys".grpGetValues( c.invenid, %L, %L ) ) -- A
  291. left join "dbUserAccReg".tbTaskVW_getdata (
  292. %s -- projectid B
  293. ,c2.id -- prodgroupid B.1
  294. ,%s -- C
  295. ,%s -- D
  296. ,taskid := a.id
  297. ) b on b.id = a.id
  298. left join "dbUserAccReg".tbTaskVW_getdata (
  299. %s -- new.projectid E
  300. ,c2.id -- prodgroupid E.1
  301.  
  302. ,%s -- F
  303. , %s -- G
  304. %s -- taskid = 55 -- H
  305. )d on d.id = a.idgrp
  306.  
  307. where a.id = %s --I
  308.  
  309. '
  310. , '"dbInventory".tbinven' , 'title' -- A
  311. , new.pid -- B
  312.  
  313. , new.uid -- C
  314. , new.acc -- D
  315. , new.pid -- E
  316.  
  317. , new.uid -- F
  318. , new.acc -- G
  319. , case when new.idgrp is null then '' else ',taskid := '|| new.idgrp end --quote_nullable( new.idgrp ) -- H
  320. , new.id -- I
  321.  
  322. );
  323.  
  324. --raise notice '351 sql=%', sql;
  325.  
  326. execute sql into rec;
  327. --raise notice '311 new.pid %, new.tags %, rec.pid %, rec.title %, rec.remark %, rec.consint_grp %, rec.produgroupid %',new.pid, new.tags, rec.pid, rec.title , rec.remark, rec.consint_grp, rec.produgroupid;
  328. end if;
  329.  
  330. --if true then
  331. if (rec.pid is not null) then
  332. --if (rec.pid is not null) and (rec.consint_grp is not null ) then
  333.  
  334. --* Remove
  335. --* check duplication , if it's feature with remark -----------------
  336. if --(position('F' in new.tags )>0)
  337. ( ExistStr('F', new.tags ) > 0) and (new.remark is not null ) then
  338. const_dup_c := 0;
  339. const_ar := string_to_array(rec.consint_grp,',' );
  340. if const_ar is not null then
  341.  
  342. foreach const in array const_ar loop
  343. const_key := trim(split_part(const , '=', 1 ));
  344.  
  345. --raise notice '334 rec.consint_grp %, const_key %, new.remark %, position=%',rec.consint_grp, const_key, new.remark ,position ( const_key in new.remark ) ;
  346.  
  347. if position ( const_key in new.remark ) > 0 then
  348. const_dup_c := const_dup_c + 1;
  349. if const_dup_c > 1 then
  350.  
  351. -- raise exception '%', format('Constraint "%s" is already exist,"%s"',new.remark, rec.consint_grp); -- 89
  352.  
  353. err := 'Constraint "%s" is already exist,"%s"';
  354. select msg from "dbSys".sysCallErrMsg(
  355. 89
  356. ,null -- talbename = description
  357. ,null -- new.id
  358. ,null -- NEW.uID
  359. ,errmsg := err
  360. )
  361. into err;
  362. raise exception '%', format(err,new.remark, rec.consint_grp );
  363.  
  364. end if;
  365. end if;
  366. end loop;
  367. end if;
  368. end if;
  369.  
  370. --* insert to system product
  371. insert into "dbUserAcc".tbprodtask ( pid, title,privilege,edition, viewRegistrationID, dis, path
  372. ,viewRegistrationID_top
  373. ,remark
  374. ,taskid
  375. ,idgrp
  376. )
  377. select rec.pid, rec.title, rec.privilege, rec.edition, rec.viewRegistrationID, rec.dis, rec.path
  378. ,rec.viewRegistrationID_Top
  379. ,new.remark
  380. ,new.id
  381. ,new.idgrp
  382. returning id, title into prodtask;
  383.  
  384. -- raise notice '363 rec.pid %, new.idgrp %, rec.consint_grp %,prodtask.id %, prodtask.title %', rec.pid, new.idgrp, rec.consint_grp, prodtask.id, prodtask.title;
  385.  
  386. --* Update to group in produc task
  387. update "dbUserAcc".tbprodtask set consint = rec.consint_grp where pid = rec.pid and taskid = new.idgrp;
  388.  
  389. --*
  390. /*
  391. raise notice '197, rec.taskid_grp %,new.title = %, position(F in new.title )=%', rec.taskid_grp,new.title, position('F' in new.title );
  392. if (position('F' in new.tags )>0 ) and (new.remark is not null) and (rec.taskid_grp is not null) then
  393. raise notice 'ok';
  394. update "dbUserAcc".tbprodtask set remark = concat_ws(',' ,rec.remark_grp, new.remark )
  395. where taskid = rec.taskid_grp and position(new.remark in rec.remark_grp) =0;
  396. end if; */
  397.  
  398. --* insert to system user product -----
  399. sql := format(
  400. 'select
  401. %s taskid -- 1
  402. ,a.id pid
  403. ,a.no
  404. from
  405. "dbUserAccReg".tbuserprod a
  406. left join "dbPM".tbproject b on b.id = %s -- 1.1
  407. where
  408. a.inven_idgrp = b.invenid -- 2
  409. and a.prodeditionid is not null
  410. '
  411. ,new.id -- 1
  412. ,new.pid -- 1.1
  413.  
  414.  
  415. );
  416. -- raise notice '435, sql=%', sql;
  417. for rec in execute sql
  418. loop
  419. -- raise notice '438 rec.no %, rec.taskid %, rec.pid %', rec.no, rec.taskid, rec.pid;
  420. insert into "dbUserAccReg".tbuserprdmenu ( taskid, pid ) values (rec.taskid, rec.pid);
  421. end loop;
  422. end if;
  423. -----------------------
  424. --raise notice '436 dbUserAccReg.tbtask new.id %', new.id;
  425.  
  426. return NEW;
  427. elseif (TG_OP = 'UPDATE') then
  428.  
  429. if diff(OLD.orderno, NEW.orderno) or diff(OLD.idgrp, NEW.idgrp) then
  430. --* changes - order no
  431. if diff(OLD.orderno, NEW.orderno) then
  432. execute format('select "dbSys".grpChangeOrderNo(%s,%s::smallint,%L)',new.id,new.orderno,'"dbPM".tbtask');
  433. end if;
  434. --* update path /dis
  435. select path, dis from "dbSys".grpGetData( new.uid,new.acc, '"dbPM".tbtask', _pid:= new.pid )
  436. where id = new.id
  437. into new.path, new.dis;
  438.  
  439. end if;
  440.  
  441. if new.tags is not null then
  442. --if diff(old.tags, new.tags) then
  443. perform "dbUserAcc".tbprod_validateEditions( new.tags, warning := true );
  444. end if;
  445.  
  446. perform "dbContact".updContactChild( '"dbPM".tbTkField', NEW.id ,'Custom', 'privilege',OLD.privilege,NEW.privilege,'TValue' );
  447. --perform "dbContact".updContactChild( '"dbPM".tbTkField', NEW.id ,'Custom', 'FileName',OLD.FileName,NEW.FileName,'TValue' );
  448.  
  449. --* Changes to system product
  450. sql := null;
  451. sql := "dbSys".sqlSetValue( OLD.title, NEW.title, sql,'title' );
  452. sql := "dbSys".sqlSetValue( OLD.privilege, NEW.privilege, sql,'privilege' );
  453. sql := "dbSys".sqlSetValue( OLD.tags, NEW.tags, sql,'edition' );
  454. sql := "dbSys".sqlSetValue( OLD.path, NEW.path, sql,'path' );
  455. sql := "dbSys".sqlSetValue( OLD.dis, NEW.dis, sql,'dis' );
  456.  
  457. sql := "dbSys".sqlSetValue( OLD.remark, NEW.remark, sql,'remark' );
  458. sql := "dbSys".sqlSetValue( OLD.idgrp, NEW.idgrp, sql,'idgrp' );
  459.  
  460. --raise notice '404,OLD.tags %, NEW.tags %, new.idgrp %, sql=%', OLD.tags, NEW.tags,new.idgrp , sql;
  461. if (sql is not null) then
  462.  
  463.  
  464. sql1 := format ( '
  465. select
  466. b.*
  467. ,d.consint consint_grp
  468. ,c2.id prodgroupid
  469. from
  470. "dbPM".tbtask a
  471. left join "dbPM".tbProject c on ( c.id= a.pid )
  472. left join "dbInventory".tbinven c1 on (c1.id = c.invenid )
  473. left join "dbUserAcc".tbprod c2 on (c2.title = "dbSys".grpGetValues( c.invenid, %L,%L ) ) -- 4
  474.  
  475. left join "dbUserAccReg".tbTaskVW_getdata (
  476. %s -- 5 new.projectid
  477. ,c2.id -- prodgroupid
  478.  
  479. ,%s -- 6
  480. ,%s -- 7
  481. ,taskid := %s -- 8 new.id
  482. ) b on b.id = a.id
  483. left join "dbUserAccReg".tbTaskVW_getdata (
  484. %s -- 9 new.pid -- new.projectid
  485. ,c2.id -- prodgroupid
  486.  
  487. ,%s -- new.uid 10
  488. ,%s -- new.acc 11
  489. %s -- taskid := -- 12 new.idgrp
  490. )d on d.id = a.idgrp
  491.  
  492. where a.id = %s -- new.id -- 13
  493. '
  494. ,'"dbInventory".tbinven', 'title' -- 4
  495. , new.pid -- 5
  496. , new.uid -- 6
  497. , new.acc -- 7
  498. , new.id -- 8
  499. , new.pid -- 9
  500. , new.uid -- 10
  501. , new.acc -- 11
  502. , case when new.idgrp is null then '' else ',taskid := '|| new.idgrp end -- new.idgrp -- 12
  503. , new.id -- 13
  504.  
  505. );
  506. -- raise notice '557 sql1=%',sql1;
  507. execute sql1
  508. into rec;
  509.  
  510. --raise notice '481 new.id %, prodgroupid %, rec.pid %, rec.title %, rec.remark %, rec.consint_grp %',new.id,rec.prodgroupid, rec.pid, rec.title , rec.remark, rec.consint_grp;
  511. --* fatal error
  512. /*
  513. if rec.prodgroupid is null then
  514. raise exception 'Can not get product group, sql1=%', sql1;
  515. end if; */
  516. begin
  517. execute format('
  518. update "dbUserAcc".tbprodtask %s where pid = %s and taskid = %s
  519. ',sql
  520. ,rec.prodgroupid
  521. ,new.id
  522. );
  523. exception when others then
  524.  
  525. raise exception 'Error,% sql1=%'
  526. ,case
  527. when rec.prodgroupid is null then 'Product Group is null'
  528. when new.idgrp is null then format( 'IDGRP is null, id "%s"', new.id )
  529. else ''
  530. end
  531. ,sql1;
  532.  
  533. end ;
  534. --*
  535. /*
  536. execute format(
  537. 'update "dbUserAcc".tbprodtask %s -- 1
  538. where title= %L and pid in ( -- 1.1
  539. select prodid_idgrp from "dbUserAccReg".tbuserprod where inven_idgrp = (
  540. select invenid from "dbPM".tbproject where id = %s -- 2
  541. )
  542. )', sql -- 1
  543. ,old.title -- 1.1
  544. ,new.pid -- 2
  545. ); */
  546.  
  547.  
  548.  
  549.  
  550. ---* Update to system product task
  551. /*
  552. --** Get total remark belongs to parent of this child
  553. sql := format( '
  554. select
  555. bb.idgrp id_grp
  556.  
  557. ,string_agg( aa1.tvalue, %L ) consint -- 1
  558.  
  559. from "dbPM".tbtask aa
  560. left join "dbSys".grpDelete_GetChild( aa.id , %L ) bb on bb.idgrp = aa.id -- a
  561. left join "dbPM".tbtkfield aa1 on aa1.pid =bb.id and aa1.title = %L -- b
  562.  
  563. where aa.pid = %s -- c
  564. and bb.idgrp = (select idgrp from "dbPM".tbtask where id = %s ) -- d
  565. group by bb.idgrp
  566.  
  567. '
  568. , ',' -- 1
  569. ,'"dbPM".tbtask' --- a
  570. ,'Remark' -- b
  571. , new.pid -- c
  572. , new.id -- d
  573. );
  574. raise notice '258 , sql=%s', sql;
  575. execute sql into rec;
  576. --** then update to product task of paraent of this child, if any
  577. if rec.id_grp is not null then
  578. sql := format ( 'update "dbUserAcc".tbprodtask set consint = %L where taskid = %s'
  579. ,rec.consint , rec.id_grp
  580. );
  581.  
  582. execute sql;
  583. end if; */
  584.  
  585. --* Update constratin to group in produc task
  586. --if (position('F' in new.tags )>0) and (new.remark is not null ) then
  587. if (rec.pid is not null ) and (new.idgrp is not null) then
  588. update "dbUserAcc".tbprodtask set consint = rec.consint_grp
  589. where
  590. pid = rec.prodgroupid
  591. and taskid = new.idgrp;
  592. end if;
  593.  
  594. end if;
  595.  
  596. --raise notice '258 , sql=%s', sql;
  597. --* Refresh view, if edition / privilege change
  598. if diff(OLD.privilege, NEW.privilege) or diff(OLD.tags, NEW.tags) then
  599. -- raise notice '245';
  600. update "dbUserAccReg".tbuserprdmenu set sql_CreateView = null where taskid = new.id and (uid= userid);
  601. end if;
  602.  
  603. return NEW;
  604.  
  605. elseif TG_OP = 'DELETE' then
  606. --* clear constrain by making it null befoer really delete it
  607. update "dbPM".tbtkfield set tvalue = null where pid = old.id and title = 'Remark';
  608.  
  609. --* get data
  610. select
  611. b.*
  612. ,d.consint consint_grp
  613. ,c2.id prodgroupid
  614. ,d.id id_grp
  615. ,d.pid prodgroupid_grp
  616. from
  617. "dbPM".tbtask a
  618. left join "dbPM".tbProject c on ( c.id= a.pid )
  619. left join "dbInventory".tbinven c1 on (c1.id = c.invenid )
  620. left join "dbUserAcc".tbprod c2 on (c2.title = "dbSys".grpGetValues( c.invenid, '"dbInventory".tbinven', 'title' ) ) -- 4
  621.  
  622. left join "dbUserAccReg".tbTaskVW_getdata (
  623. old.pid -- new.projectid
  624. ,c2.id -- prodgroupid
  625.  
  626. ,old.uid
  627. ,old.acc
  628. ,taskid := old.id
  629. ) b on b.id = a.id
  630. left join "dbUserAccReg".tbTaskVW_getdata (
  631. old.pid -- new.projectid
  632. ,c2.id -- prodgroupid
  633.  
  634. ,old.uid
  635. ,old.acc
  636. ,taskid := old.idgrp
  637. )d on d.id = a.idgrp
  638.  
  639. where a.id = old.id
  640. into rec;
  641.  
  642. -- raise notice '543 prodgroupid %, rec.pid %, rec.title %, rec.remark %, rec.consint_grp %, rec.prodgroupid_grp % rec.id_grp %',rec.prodgroupid, rec.pid, rec.title , rec.remark, rec.consint_grp, rec.prodgroupid_grp, rec.id_grp;
  643.  
  644. --* update product task
  645. update "dbUserAcc".tbprodtask set consint = rec.consint_grp where pid = rec.prodgroupid_grp and taskid = rec.id_grp;
  646.  
  647.  
  648. --* delete from system user product
  649. sql := format(
  650. 'delete from "dbUserAccReg".tbuserprdmenu where taskid = %s'
  651. ,old.id
  652. );
  653. -- raise notice '247, SQL=%', sql;
  654. execute sql;
  655. --*
  656.  
  657. -- * delete from system product menu
  658. execute format(
  659. 'delete from "dbUserAcc".tbprodtask
  660. where title= %L and pid in ( -- 1.1
  661. select prodid_idgrp from "dbUserAccReg".tbuserprod where inven_idgrp = (
  662. select invenid from "dbPM".tbproject where id = %s -- 2
  663. )
  664. )'
  665. ,old.title -- 1.1
  666. ,old.pid -- 2
  667. );
  668.  
  669.  
  670.  
  671. return OLD;
  672. end if;
  673. end ; $$ LANGUAGE plpgsql;
  674.  
  675. select "dbSys".viewRegistration( '"dbUserAccReg".tbTaskvw'
  676. , pers := '"dbPM".tbTask'
  677. --, _delete_system_data := false
  678. --,_init_data := true, _init_data_uid := varint('userid') ,_init_data_acc := null
  679. --,_init_data := true, _init_data_uid := varint('userid') ,_init_data_acc := varint('accid')
  680.  
  681. -- , _init_sysorg := true
  682. -- ,_init_count := 'c_task'
  683. -- ,_fnCheckPost := 'sumamt' -- Field to check cannot be changed when refered by posting data source
  684. );
  685.  
  686. /************************************************************
  687. Registration : inherite the standard project task
  688. *************************************************************/
  689. select "dbSys".tblRegistration_Inherit( '"dbUserAccReg".tbTaskVW', '"dbPM".tbTaskVW', false,false,true );
  690.  
  691. /*****************************
  692. View registration
  693. ******************************/
  694. /*
  695. select "dbSys".viewRegistration( '"dbUserAccReg".tbTaskvw'
  696. , pers := '"dbPM".tbTask'
  697. -- , _delete_system_data := true
  698. -- ,_init_data := true, _init_data_uid := 1 ,_init_data_acc := null
  699. -- ,_fnCheckPost := <fn> -- Field to check cannot be changed when refered by posting data source
  700. );
  701. */
  702.  
  703.  
  704.  
  705. select * from "dbSys".tvCreateView ( varint('userid_admin_cloudora'), varint('accid_cloudora'), '"dbUserAccReg".tbTask'
  706.  
  707. ,CheckUserAcc:=true
  708. ,dropit:=true
  709.  
  710. );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement