summaryrefslogtreecommitdiff
path: root/sql/modules/Company.sql
blob: 8d78c9468b6da9b5717350b4c5f9b43e4bf66589 (plain)
  1. BEGIN;
  2. CREATE OR REPLACE FUNCTION entity__save_notes(in_entity_id int, in_note text)
  3. RETURNS INT AS
  4. $$
  5. DECLARE out_id int;
  6. BEGIN
  7. -- TODO, change this to create vector too
  8. INSERT INTO entity_note (ref_key, note_class, entity_id, note, vector)
  9. VALUES (in_entity_id, 1, in_entity_id, in_note, '');
  10. SELECT currval('note_id_seq') INTO out_id;
  11. RETURN out_id;
  12. END;
  13. $$ LANGUAGE PLPGSQL;
  14. CREATE OR REPLACE FUNCTION entity_list_contact_class()
  15. RETURNS SETOF contact_class AS
  16. $$
  17. DECLARE out_row RECORD;
  18. BEGIN
  19. FOR out_row IN
  20. SELECT * FROM contact_class ORDER BY id
  21. LOOP
  22. RETURN NEXT out_row;
  23. END LOOP;
  24. END;
  25. $$ language plpgsql;
  26. CREATE TYPE entity_credit_search_return AS (
  27. legal_name text,
  28. id int,
  29. entity_id int,
  30. entity_class int,
  31. discount numeric,
  32. taxincluded bool,
  33. creditlimit numeric,
  34. terms int2,
  35. customernumber text,
  36. business_id int,
  37. language_code text,
  38. pricegroup_id int,
  39. curr char(3),
  40. startdate date,
  41. enddate date
  42. );
  43. COMMENT ON TYPE entity_credit_search_return IS
  44. $$ This may change in 1.4 and should not be relied upon too much $$;
  45. CREATE OR REPLACE FUNCTION entity__retrieve_credit
  46. (in_entity_id int, in_entity_class int)
  47. RETURNS entity_credit_search_return AS
  48. $$
  49. DECLARE out_row entity_credit_search_return;
  50. BEGIN
  51. SELECT c.legal_name, c.id, e.id, ec.entity_class, ec.discount,
  52. ec.taxincluded, ec.creditlimit, ec.terms, ec.meta_number,
  53. ec.business_id, ec.language_code, ec.pricegroup_id,
  54. ec.curr::char(3), ec.startdate, ec.enddate
  55. INTO out_row
  56. FROM company c
  57. JOIN entity e ON (c.entity_id = e.id)
  58. JOIN entity_credit_account ec ON (c.entity_id = ec.entity_id)
  59. WHERE e.id = in_entity_id
  60. AND ec.entity_class = CASE WHEN in_entity_class = 3 THEN 2
  61. ELSE in_entity_class END;
  62. RETURN out_row;
  63. END;
  64. $$ LANGUAGE PLPGSQL;
  65. CREATE OR REPLACE FUNCTION entity_credit_save (
  66. in_id int, in_entity_class int,
  67. in_discount numeric, in_taxincluded bool, in_creditlimit numeric,
  68. in_discount_terms int,
  69. in_terms int, in_meta_number varchar(32), in_business_id int,
  70. in_language varchar(6), in_pricegroup_id int,
  71. in_curr char, in_startdate date, in_enddate date,
  72. in_notes text,
  73. in_name text, in_tax_id TEXT,
  74. in_threshold NUMERIC
  75. ) returns INT as $$
  76. DECLARE
  77. t_entity_class int;
  78. new_entity_id int;
  79. v_row company;
  80. l_id int;
  81. BEGIN
  82. -- TODO: Move every table to an upsert mode independantly.
  83. SELECT INTO v_row * FROM company WHERE id = in_id;
  84. IF NOT FOUND THEN
  85. -- do some inserts
  86. select nextval('entity_id_seq') into new_entity_id;
  87. insert into entity (id, name, entity_class)
  88. VALUES (new_entity_id, in_name, in_entity_class);
  89. INSERT INTO company ( entity_id, legal_name, tax_id )
  90. VALUES ( new_entity_id, in_name, in_tax_id );
  91. IF in_entity_class NOT IN (1, 2) THEN
  92. RETURN new_entity_id;
  93. END IF;
  94. INSERT INTO entity_credit_account (
  95. entity_id,
  96. entity_class,
  97. discount,
  98. taxincluded,
  99. creditlimit,
  100. terms,
  101. meta_number,
  102. business_id,
  103. language_code,
  104. pricegroup_id,
  105. curr,
  106. startdate,
  107. enddate,
  108. discount_terms,
  109. threshold
  110. )
  111. VALUES (
  112. new_entity_id,
  113. in_entity_class,
  114. in_discount / 100,
  115. in_taxincluded,
  116. in_creditlimit,
  117. in_terms,
  118. in_meta_number,
  119. in_business_id,
  120. in_language,
  121. in_pricegroup_id,
  122. in_curr,
  123. in_startdate,
  124. in_enddate,
  125. in_discount_terms,
  126. in_threshold
  127. );
  128. -- entity note class
  129. insert into entity_note (note_class, note, ref_key, vector) VALUES (
  130. 1, in_notes, new_entity_id, to_tsvector(in_notes));
  131. return new_entity_id;
  132. ELSIF FOUND THEN
  133. update company set tax_id = in_tax_id where id = in_id;
  134. update entity_credit_account SET
  135. discount = in_discount,
  136. taxincluded = in_taxincluded,
  137. creditlimit = in_creditlimit,
  138. terms = in_terms,
  139. meta_number = in_meta_number,
  140. business_id = in_business_id,
  141. language_code = in_language,
  142. pricegroup_id = in_pricegroup_id,
  143. curr = in_curr,
  144. startdate = in_startdate,
  145. enddate = in_enddate,
  146. threshold = in_threshold,
  147. discount_terms = in_discount_terms
  148. where entity_id = v_row.entity_id;
  149. UPDATE entity_note SET
  150. note = in_note
  151. WHERE ref_key = v_row.entity_id;
  152. return in_id;
  153. END IF;
  154. END;
  155. $$ language 'plpgsql';
  156. CREATE OR REPLACE FUNCTION company__list_locations(in_entity_id int)
  157. RETURNS SETOF location_result AS
  158. $$
  159. DECLARE out_row RECORD;
  160. BEGIN
  161. FOR out_row IN
  162. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  163. l.state, c.name, lc.class
  164. FROM location l
  165. JOIN company_to_location ctl ON (ctl.location_id = l.id)
  166. JOIN company cp ON (ctl.company_id = cp.id)
  167. JOIN location_class lc ON (ctl.location_class = lc.id)
  168. JOIN country c ON (c.id = l.country_id)
  169. WHERE cp.entity_id = in_entity_id
  170. ORDER BY lc.id, l.id, c.name
  171. LOOP
  172. RETURN NEXT out_row;
  173. END LOOP;
  174. END;
  175. $$ LANGUAGE PLPGSQL;
  176. CREATE TYPE contact_list AS (
  177. class text,
  178. contact text
  179. );
  180. CREATE OR REPLACE FUNCTION company__list_bank_account(in_entity_id int)
  181. RETURNS SETOF entity_bank_account AS
  182. $$
  183. DECLARE out_row entity_bank_account%ROWTYPE;
  184. BEGIN
  185. FOR out_row IN
  186. SELECT * from entity_bank_account where entity_id = in_entity_id
  187. LOOP
  188. RETURN NEXT out_row;
  189. END LOOP;
  190. END;
  191. $$ LANGUAGE PLPGSQL;
  192. CREATE OR REPLACE FUNCTION entity__save_bank_account
  193. (in_entity_id int, in_bic text, in_iban text)
  194. RETURNS int AS
  195. $$
  196. DECLARE out_id int;
  197. BEGIN
  198. INSERT INTO entity_bank_account(entity_id, bic, iban)
  199. VALUES(in_entity_id, in_bic, in_iban);
  200. SELECT CURRVAL('entity_bank_account_id_seq') INTO out_id ;
  201. RETURN out_id;
  202. END;
  203. $$ LANGUAGE PLPGSQL;
  204. CREATE OR REPLACE FUNCTION company__save_contact
  205. (in_entity_id int, in_contact_class int, in_contact text)
  206. RETURNS INT AS
  207. $$
  208. DECLARE out_id int;
  209. BEGIN
  210. INSERT INTO company_to_contact(company_id, contact_class_id, contact)
  211. SELECT id, in_contact_class, in_contact FROM company
  212. WHERE entity_id = in_entity_id;
  213. RETURN 1;
  214. END;
  215. $$ LANGUAGE PLPGSQL;
  216. CREATE TYPE entity_note_list AS (
  217. id int,
  218. note text
  219. );
  220. CREATE OR REPLACE FUNCTION company__list_notes(in_entity_id int)
  221. RETURNS SETOF entity_note AS
  222. $$
  223. DECLARE out_row record;
  224. BEGIN
  225. FOR out_row IN
  226. SELECT *
  227. FROM entity_note
  228. WHERE ref_key = in_entity_id
  229. LOOP
  230. RETURN NEXT out_row;
  231. END LOOP;
  232. END;
  233. $$ LANGUAGE PLPGSQL;
  234. CREATE OR REPLACE FUNCTION company__next_id() returns bigint as $$
  235. select nextval('company_id_seq');
  236. $$ language 'sql';
  237. CREATE OR REPLACE FUNCTION company__location_save (
  238. in_company_id int,
  239. in_location_class int, in_line_one text, in_line_two text,
  240. in_city_province TEXT, in_mail_code text, in_country_code int,
  241. in_created date
  242. ) returns int AS $$
  243. BEGIN
  244. return _entity_location_save(
  245. in_company_id,
  246. in_location_class, in_line_one, in_line_two,
  247. in_city_province , in_mail_code, in_country_code,
  248. in_created);
  249. END;
  250. $$ language 'plpgsql';
  251. create or replace function _entity_location_save(
  252. in_entity_id int, in_location_id int,
  253. in_location_class int, in_line_one text, in_line_two text,
  254. in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text,
  255. in_country_code int
  256. ) returns int AS $$
  257. DECLARE
  258. l_row location;
  259. l_id INT;
  260. t_company_id int;
  261. BEGIN
  262. SELECT id INTO t_company_id
  263. FROM company WHERE entity_id = in_entity_id;
  264. DELETE FROM company_to_location
  265. WHERE company_id = t_company_id
  266. AND location_class = in_location_class
  267. AND location_id = in_location_id;
  268. SELECT location_save(in_line_one, in_line_two, in_line_three, in_city,
  269. in_state, in_mail_code, in_country_code)
  270. INTO l_id;
  271. INSERT INTO company_to_location
  272. (company_id, location_class, location_id)
  273. VALUES (t_company_id, in_location_class, l_id);
  274. RETURN l_id;
  275. END;
  276. $$ language 'plpgsql';
  277. COMMIT;