summaryrefslogtreecommitdiff
path: root/sql/Pg-upgrade-2.3.0-2.3.1.sql
blob: 866774ad9072f6c732a1ffb335d21d420542a16a (plain)
  1. -- function check_department
  2. create function check_department() returns opaque as '
  3. declare
  4. dpt_id int;
  5. begin
  6. if new.department_id = 0 then
  7. delete from dpt_trans where trans_id = new.id;
  8. return NULL;
  9. end if;
  10. select into dpt_id trans_id from dpt_trans where trans_id = new.id;
  11. if dpt_id > 0 then
  12. update dpt_trans set department_id = new.department_id where trans_id = dpt_id;
  13. else
  14. insert into dpt_trans (trans_id, department_id) values (new.id, new.department_id);
  15. end if;
  16. return NULL;
  17. end;
  18. ' language 'plpgsql';
  19. -- end function
  20. -- department transaction table
  21. create table dpt_trans (trans_id int, department_id int);
  22. -- function del_department
  23. create function del_department() returns opaque as '
  24. begin
  25. delete from dpt_trans where trans_id = old.id;
  26. return NULL;
  27. end;
  28. ' language 'plpgsql';
  29. -- end function
  30. -- triggers
  31. --
  32. create trigger check_department after insert or update on ar for each row execute procedure check_department();
  33. -- end trigger
  34. create trigger check_department after insert or update on ap for each row execute procedure check_department();
  35. -- end trigger
  36. create trigger check_department after insert or update on gl for each row execute procedure check_department();
  37. -- end trigger
  38. create trigger check_department after insert or update on oe for each row execute procedure check_department();
  39. -- end trigger
  40. --
  41. --
  42. create trigger del_department after delete on ar for each row execute procedure del_department();
  43. -- end trigger
  44. create trigger del_department after delete on ap for each row execute procedure del_department();
  45. -- end trigger
  46. create trigger del_department after delete on gl for each row execute procedure del_department();
  47. -- end trigger
  48. create trigger del_department after delete on oe for each row execute procedure del_department();
  49. -- end trigger
  50. --
  51. -- business table
  52. create table business (id int default nextval('id'), description text, discount float4);
  53. --
  54. -- SIC
  55. create table sic (code text, sictype char(1), description text);
  56. --
  57. alter table vendor add column gifi_accno text;
  58. alter table vendor add column business_id int;
  59. alter table vendor add column taxnumber text;
  60. alter table vendor add column sic_code text;
  61. --
  62. alter table customer add column business_id int;
  63. alter table customer add column taxnumber text;
  64. alter table customer add column sic_code text;
  65. --
  66. create function del_customer() returns opaque as '
  67. begin
  68. delete from shipto where trans_id = old.id;
  69. delete from customertax where customer_id = old.id;
  70. return NULL;
  71. end;
  72. ' language 'plpgsql';
  73. -- end function
  74. --
  75. create function del_vendor() returns opaque as '
  76. begin
  77. delete from shipto where trans_id = old.id;
  78. delete from vendortax where vendor_id = old.id;
  79. return NULL;
  80. end;
  81. ' language 'plpgsql';
  82. -- end function
  83. --
  84. create trigger del_customer after delete on customer for each row execute procedure del_customer();
  85. -- end trigger
  86. create trigger del_vendor after delete on vendor for each row execute procedure del_vendor();
  87. -- end trigger
  88. --
  89. alter table acc_trans add column memo text;
  90. --
  91. alter table employee add column sales bool;
  92. alter table employee alter column sales set default 't';
  93. --
  94. alter table vendor add discount float4;
  95. alter table vendor add creditlimit float;
  96. --
  97. -- function del_exchangerate
  98. create function del_exchangerate() returns opaque as '
  99. declare
  100. t_transdate date;
  101. t_curr char(3);
  102. t_id int;
  103. d_curr text;
  104. begin
  105. select into d_curr substr(curr,1,3) from defaults;
  106. if TG_RELNAME = ''ar'' then
  107. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  108. end if;
  109. if TG_RELNAME = ''ap'' then
  110. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  111. end if;
  112. if TG_RELNAME = ''oe'' then
  113. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  114. end if;
  115. if d_curr != t_curr then
  116. select into t_id a.id from acc_trans ac
  117. join ar a on (a.id = ac.trans_id)
  118. where a.curr = t_curr
  119. and ac.transdate = t_transdate
  120. except select a.id from ar a where a.id = old.id
  121. union
  122. select a.id from acc_trans ac
  123. join ap a on (a.id = ac.trans_id)
  124. where a.curr = t_curr
  125. and ac.transdate = t_transdate
  126. except select a.id from ap a where a.id = old.id
  127. union
  128. select o.id from oe o
  129. where o.curr = t_curr
  130. and o.transdate = t_transdate
  131. except select o.id from oe o where o.id = old.id;
  132. if not found then
  133. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  134. end if;
  135. end if;
  136. return old;
  137. end;
  138. ' language 'plpgsql';
  139. -- end function
  140. --
  141. -- triggers
  142. --
  143. create trigger del_exchangerate before delete on ar for each row execute procedure del_exchangerate();
  144. -- end trigger
  145. --
  146. create trigger del_exchangerate before delete on ap for each row execute procedure del_exchangerate();
  147. -- end trigger
  148. --
  149. create trigger del_exchangerate before delete on oe for each row execute procedure del_exchangerate();
  150. -- end trigger
  151. --
  152. --
  153. alter table orderitems add ship float4;
  154. alter table orderitems add serialnumber text;
  155. --
  156. --
  157. create sequence orderitemsid maxvalue 100000 cycle;
  158. alter table orderitems add id int;
  159. alter table orderitems alter id set default nextval('orderitemsid');
  160. --
  161. create table warehouse (id int default nextval('id'), description text);
  162. --
  163. create table inventory (warehouse_id int, parts_id int, oe_id int, orderitems_id int, qty float4, shippingdate date);
  164. --
  165. -- update orderitems, fill in id
  166. create table temp (id int default nextval('orderitemsid'), tempid oid);
  167. insert into temp (tempid) select oid from orderitems;
  168. update orderitems set id = temp.id from temp where orderitems.oid = temp.tempid;
  169. drop table temp;
  170. --
  171. create index orderitems_id_key on orderitems (id);
  172. --
  173. alter table ar add shipvia text;
  174. alter table ap add shipvia text;
  175. alter table oe add shipvia text;
  176. --
  177. --
  178. alter table inventory add employee_id int;
  179. --
  180. --
  181. create function check_inventory() returns opaque as '
  182. declare
  183. itemid int;
  184. row_data inventory%rowtype;
  185. begin
  186. if not old.quotation then
  187. for row_data in select * from inventory where oe_id = old.id loop
  188. select into itemid id from orderitems where trans_id = old.id and id = row_data.orderitems_id;
  189. if itemid is null then
  190. delete from inventory where oe_id = old.id and orderitems_id = row_data.orderitems_id;
  191. end if;
  192. end loop;
  193. end if;
  194. return old;
  195. end;
  196. ' language 'plpgsql';
  197. -- end function
  198. --
  199. create trigger check_inventory after update on oe for each row execute procedure check_inventory();
  200. -- end trigger
  201. --
  202. --
  203. create table yearend (
  204. trans_id int,
  205. transdate date
  206. );
  207. --
  208. -- function del_yearend
  209. create function del_yearend() returns opaque as '
  210. begin
  211. delete from yearend where trans_id = old.id;
  212. return NULL;
  213. end;
  214. ' language 'plpgsql';
  215. -- end function
  216. -- triggers
  217. --
  218. create trigger del_yearend after delete on gl for each row execute procedure del_yearend();
  219. -- end trigger
  220. --
  221. --
  222. create table temp (
  223. id int default nextval('id'),
  224. name varchar(64),
  225. addr1 varchar(64),
  226. addr2 varchar(64),
  227. addr3 varchar(64),
  228. addr4 varchar(64),
  229. contact varchar(64),
  230. phone varchar(20),
  231. fax varchar(20),
  232. email text,
  233. notes text,
  234. discount float4,
  235. taxincluded bool,
  236. creditlimit float default 0,
  237. terms int2 default 0,
  238. customernumber varchar(64),
  239. cc text,
  240. bcc text,
  241. business_id int,
  242. taxnumber varchar(64),
  243. sic_code varchar(6),
  244. iban varchar(34),
  245. bic varchar(11)
  246. );
  247. insert into temp (id, name, addr1, addr2, addr3, addr4, contact, phone, fax, email, notes, discount, taxincluded, creditlimit, terms, customernumber, cc, bcc, business_id, taxnumber, sic_code) select id, name, addr1, addr2, addr3, addr4, contact, phone, fax, email, notes, discount, taxincluded, creditlimit, terms, customernumber, cc, bcc, business_id, taxnumber, sic_code from customer;
  248. --
  249. drop table customer;
  250. --
  251. alter table temp rename to customer;
  252. --
  253. create index customer_id_key on customer (id);
  254. create index customer_customernumber_key on customer (customernumber);
  255. create index customer_name_key on customer (name);
  256. create index customer_contact_key on customer (contact);
  257. --
  258. create trigger del_customer after delete on customer for each row execute procedure del_customer();
  259. -- end trigger
  260. --
  261. create table temp (
  262. id int default nextval('id'),
  263. name varchar(64),
  264. addr1 varchar(64),
  265. addr2 varchar(64),
  266. addr3 varchar(64),
  267. addr4 varchar(64),
  268. contact varchar(64),
  269. phone varchar(20),
  270. fax varchar(20),
  271. email text,
  272. notes text,
  273. terms int2 default 0,
  274. taxincluded bool,
  275. vendornumber varchar(64),
  276. cc text,
  277. bcc text,
  278. gifi_accno varchar(30),
  279. business_id int,
  280. taxnumber varchar(64),
  281. sic_code varchar(6),
  282. discount float4,
  283. creditlimit float default 0,
  284. iban varchar(34),
  285. bic varchar(11)
  286. );
  287. insert into temp (id, name, addr1, addr2, addr3, addr4, contact, phone, fax, email, notes, discount, taxincluded, creditlimit, terms, vendornumber, cc, bcc, business_id, taxnumber, sic_code) select id, name, addr1, addr2, addr3, addr4, contact, phone, fax, email, notes, discount, taxincluded, creditlimit, terms, vendornumber, cc, bcc, business_id, taxnumber, sic_code from vendor;
  288. --
  289. drop table vendor;
  290. --
  291. alter table temp rename to vendor;
  292. --
  293. create index vendor_id_key on vendor (id);
  294. create index vendor_name_key on vendor (name);
  295. create index vendor_vendornumber_key on vendor (vendornumber);
  296. create index vendor_contact_key on vendor (contact);
  297. --
  298. create trigger del_vendor after delete on vendor for each row execute procedure del_vendor();
  299. -- end trigger
  300. --
  301. create table temp (
  302. code varchar(6),
  303. sictype char(1),
  304. description text
  305. );
  306. insert into temp (code, sictype, description) select code, sictype, description from sic;
  307. drop table sic;
  308. alter table temp rename to sic;
  309. --
  310. create table temp (
  311. trans_id int,
  312. shiptoname varchar(64),
  313. shiptoaddr1 varchar(64),
  314. shiptoaddr2 varchar(64),
  315. shiptoaddr3 varchar(64),
  316. shiptoaddr4 varchar(64),
  317. shiptocontact varchar(64),
  318. shiptophone varchar(20),
  319. shiptofax varchar(20),
  320. shiptoemail text
  321. );
  322. insert into temp (trans_id, shiptoname, shiptoaddr1, shiptoaddr2, shiptoaddr3, shiptoaddr4, shiptocontact, shiptophone, shiptofax, shiptoemail) select trans_id, shiptoname, shiptoaddr1, shiptoaddr2, shiptoaddr3, shiptoaddr4, shiptocontact, shiptophone, shiptofax, shiptoemail from shipto;
  323. drop table shipto;
  324. alter table temp rename to shipto;
  325. create index shipto_trans_id_key on shipto (trans_id);
  326. --
  327. --
  328. create table temp (
  329. id int default nextval('id'),
  330. login text,
  331. name varchar(64),
  332. addr1 varchar(64),
  333. addr2 varchar(64),
  334. addr3 varchar(64),
  335. addr4 varchar(64),
  336. workphone varchar(20),
  337. homephone varchar(20),
  338. startdate date default current_date,
  339. enddate date,
  340. notes text,
  341. role varchar(20),
  342. sales bool,
  343. email text,
  344. sin varchar(20),
  345. iban varchar(34),
  346. bic varchar(11)
  347. );
  348. insert into temp (id,login,name,addr1,addr2,addr3,addr4,workphone,homephone,startdate,enddate,notes,role,sales) select id,login,name,addr1,addr2,addr3,addr4,workphone,homephone,startdate,enddate,notes,role,sales from employee;
  349. --
  350. drop table employee;
  351. alter table temp rename to employee;
  352. --
  353. create index employee_id_key on employee (id);
  354. create unique index employee_login_key on employee (login);
  355. create index employee_name_key on employee (name);
  356. --
  357. update defaults set version = '2.3.1';