summaryrefslogtreecommitdiff
path: root/sql/Pg-functions.sql
blob: b42e2083e46cf11446cf33b9f42ef0bbdce8bb32 (plain)
  1. CREATE LANGUAGE plpgsql;
  2. --
  3. CREATE FUNCTION del_yearend() RETURNS OPAQUE AS '
  4. begin
  5. delete from yearend where trans_id = old.id;
  6. return NULL;
  7. end;
  8. ' language 'plpgsql';
  9. -- end function
  10. --
  11. CREATE TRIGGER del_yearend AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_yearend();
  12. -- end trigger
  13. --
  14. CREATE FUNCTION del_department() RETURNS OPAQUE AS '
  15. begin
  16. delete from dpt_trans where trans_id = old.id;
  17. return NULL;
  18. end;
  19. ' language 'plpgsql';
  20. -- end function
  21. --
  22. CREATE TRIGGER del_department AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_department();
  23. -- end trigger
  24. CREATE TRIGGER del_department AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_department();
  25. -- end trigger
  26. CREATE TRIGGER del_department AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_department();
  27. -- end trigger
  28. CREATE TRIGGER del_department AFTER DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_department();
  29. -- end trigger
  30. --
  31. CREATE FUNCTION del_customer() RETURNS OPAQUE AS '
  32. begin
  33. delete from shipto where trans_id = old.id;
  34. delete from customertax where customer_id = old.id;
  35. delete from partscustomer where customer_id = old.id;
  36. return NULL;
  37. end;
  38. ' language 'plpgsql';
  39. -- end function
  40. --
  41. CREATE TRIGGER del_customer AFTER DELETE ON customer FOR EACH ROW EXECUTE PROCEDURE del_customer();
  42. -- end trigger
  43. --
  44. CREATE FUNCTION del_vendor() RETURNS OPAQUE AS '
  45. begin
  46. delete from shipto where trans_id = old.id;
  47. delete from vendortax where vendor_id = old.id;
  48. delete from partsvendor where vendor_id = old.id;
  49. return NULL;
  50. end;
  51. ' language 'plpgsql';
  52. -- end function
  53. --
  54. CREATE TRIGGER del_vendor AFTER DELETE ON vendor FOR EACH ROW EXECUTE PROCEDURE del_vendor();
  55. -- end trigger
  56. --
  57. CREATE FUNCTION del_exchangerate() RETURNS OPAQUE AS '
  58. declare
  59. t_transdate date;
  60. t_curr char(3);
  61. t_id int;
  62. d_curr text;
  63. begin
  64. select into d_curr substr(curr,1,3) from defaults;
  65. if TG_RELNAME = ''ar'' then
  66. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  67. end if;
  68. if TG_RELNAME = ''ap'' then
  69. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  70. end if;
  71. if TG_RELNAME = ''oe'' then
  72. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  73. end if;
  74. if d_curr != t_curr then
  75. select into t_id a.id from acc_trans ac
  76. join ar a on (a.id = ac.trans_id)
  77. where a.curr = t_curr
  78. and ac.transdate = t_transdate
  79. except select a.id from ar a where a.id = old.id
  80. union
  81. select a.id from acc_trans ac
  82. join ap a on (a.id = ac.trans_id)
  83. where a.curr = t_curr
  84. and ac.transdate = t_transdate
  85. except select a.id from ap a where a.id = old.id
  86. union
  87. select o.id from oe o
  88. where o.curr = t_curr
  89. and o.transdate = t_transdate
  90. except select o.id from oe o where o.id = old.id;
  91. if not found then
  92. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  93. end if;
  94. end if;
  95. return old;
  96. end;
  97. ' language 'plpgsql';
  98. -- end function
  99. --
  100. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  101. -- end trigger
  102. --
  103. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  104. -- end trigger
  105. --
  106. CREATE TRIGGER del_exchangerate BEFORE DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  107. -- end trigger
  108. --
  109. CREATE FUNCTION check_inventory() RETURNS OPAQUE AS '
  110. declare
  111. itemid int;
  112. row_data inventory%rowtype;
  113. begin
  114. if not old.quotation then
  115. for row_data in select * from inventory where trans_id = old.id loop
  116. select into itemid id from orderitems where trans_id = old.id and id = row_data.orderitems_id;
  117. if itemid is null then
  118. delete from inventory where trans_id = old.id and orderitems_id = row_data.orderitems_id;
  119. end if;
  120. end loop;
  121. end if;
  122. return old;
  123. end;
  124. ' language 'plpgsql';
  125. -- end function
  126. --
  127. CREATE TRIGGER check_inventory AFTER UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_inventory();
  128. -- end trigger
  129. --
  130. --
  131. CREATE FUNCTION check_department() RETURNS OPAQUE AS '
  132. declare
  133. dpt_id int;
  134. begin
  135. if new.department_id = 0 then
  136. delete from dpt_trans where trans_id = new.id;
  137. return NULL;
  138. end if;
  139. select into dpt_id trans_id from dpt_trans where trans_id = new.id;
  140. if dpt_id > 0 then
  141. update dpt_trans set department_id = new.department_id where trans_id = dpt_id;
  142. else
  143. insert into dpt_trans (trans_id, department_id) values (new.id, new.department_id);
  144. end if;
  145. return NULL;
  146. end;
  147. ' language 'plpgsql';
  148. -- end function
  149. --
  150. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ar FOR EACH ROW EXECUTE PROCEDURE check_department();
  151. -- end trigger
  152. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ap FOR EACH ROW EXECUTE PROCEDURE check_department();
  153. -- end trigger
  154. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON gl FOR EACH ROW EXECUTE PROCEDURE check_department();
  155. -- end trigger
  156. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_department();
  157. -- end trigger
  158. --
  159. CREATE FUNCTION del_recurring() returns opaque as '
  160. BEGIN
  161. DELETE FROM recurring WHERE id = old.id;
  162. DELETE FROM recurringemail WHERE id = old.id;
  163. DELETE FROM recurringprint WHERE id = old.id;
  164. RETURN NULL;
  165. END;
  166. ' language 'plpgsql';
  167. --end function
  168. CREATE TRIGGER del_recurring AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  169. -- end trigger
  170. CREATE TRIGGER del_recurring AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  171. -- end trigger
  172. CREATE TRIGGER del_recurring AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  173. -- end trigger
  174. --
  175. CREATE FUNCTION avgcost(int) RETURNS FLOAT AS '
  176. DECLARE
  177. v_cost float;
  178. v_qty float;
  179. v_parts_id alias for $1;
  180. BEGIN
  181. SELECT INTO v_cost, v_qty SUM(i.sellprice * i.qty), SUM(i.qty)
  182. FROM invoice i
  183. JOIN ap a ON (a.id = i.trans_id)
  184. WHERE i.parts_id = v_parts_id;
  185. IF v_cost IS NULL THEN
  186. v_cost := 0;
  187. END IF;
  188. IF NOT v_qty IS NULL THEN
  189. IF v_qty = 0 THEN
  190. v_cost := 0;
  191. ELSE
  192. v_cost := v_cost/v_qty;
  193. END IF;
  194. END IF;
  195. RETURN v_cost;
  196. END;
  197. ' language 'plpgsql';
  198. -- end function
  199. --
  200. CREATE FUNCTION lastcost(int) RETURNS FLOAT AS '
  201. DECLARE
  202. v_cost float;
  203. v_parts_id alias for $1;
  204. BEGIN
  205. SELECT INTO v_cost sellprice FROM invoice i
  206. JOIN ap a ON (a.id = i.trans_id)
  207. WHERE i.parts_id = v_parts_id
  208. ORDER BY a.transdate desc, a.id desc
  209. LIMIT 1;
  210. IF v_cost IS NULL THEN
  211. v_cost := 0;
  212. END IF;
  213. RETURN v_cost;
  214. END;
  215. ' language plpgsql;
  216. -- end function
  217. --
  218. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  219. AS
  220. '
  221. BEGIN
  222. IF NEW.onhand >= NEW.rop THEN
  223. NOTIFY parts_short;
  224. END IF;
  225. RETURN NEW;
  226. END;
  227. ' LANGUAGE PLPGSQL;
  228. -- end function
  229. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  230. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  231. -- end function
  232. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  233. RETURNS BOOL AS
  234. '
  235. DECLARE
  236. table_name ALIAS FOR $1;
  237. new_field_name ALIAS FOR $2;
  238. field_datatype ALIAS FOR $3;
  239. BEGIN
  240. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  241. WHERE extends = '''''' || table_name || '''''' '';
  242. IF NOT FOUND THEN
  243. BEGIN
  244. INSERT INTO custom_table_catalog (extends)
  245. VALUES (table_name);
  246. EXECUTE ''CREATE TABLE custom_''||table_name ||
  247. '' (row_id INT)'';
  248. EXCEPTION WHEN duplicate_table THEN
  249. -- do nothing
  250. END;
  251. END IF;
  252. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  253. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  254. WHERE extends = ''''''|| table_name || ''''''))'';
  255. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  256. || new_field_name || '' '' || field_datatype;
  257. RETURN TRUE;
  258. END;
  259. ' LANGUAGE PLPGSQL;
  260. -- end function
  261. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  262. RETURNS BOOL AS
  263. '
  264. DECLARE
  265. table_name ALIAS FOR $1;
  266. custom_field_name ALIAS FOR $2;
  267. BEGIN
  268. DELETE FROM custom_field_catalog
  269. WHERE field_name = custom_field_name AND
  270. table_id = (SELECT table_id FROM custom_table_catalog
  271. WHERE extends = table_name);
  272. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  273. '' DROP COLUMN '' || custom_field_name;
  274. RETURN TRUE;
  275. END;
  276. ' LANGUAGE PLPGSQL;
  277. -- end function