summaryrefslogtreecommitdiff
path: root/sql/modules/Company.sql
blob: 3973ff2b577a2246e82126d2168ffab5a79d1e8a (plain)
  1. BEGIN;
  2. CREATE OR REPLACE FUNCTION entity_list_contact_class()
  3. RETURNS SETOF contact_class AS
  4. $$
  5. DECLARE out_row RECORD;
  6. BEGIN
  7. FOR out_row IN
  8. SELECT * FROM contact_class ORDER BY id
  9. LOOP
  10. RETURN NEXT out_row;
  11. END LOOP;
  12. END;
  13. $$ language plpgsql;
  14. CREATE TYPE entity_credit_search_return AS (
  15. legal_name text,
  16. id int,
  17. entity_id int,
  18. entity_class int,
  19. discount numeric,
  20. taxincluded bool,
  21. creditlimit numeric,
  22. terms int2,
  23. customernumber text,
  24. business_id int,
  25. language_code text,
  26. pricegroup_id int,
  27. curr char(3),
  28. startdate date,
  29. enddate date
  30. );
  31. COMMENT ON TYPE entity_credit_search_return IS
  32. $$ This may change in 1.4 and should not be relied upon too much $$;
  33. CREATE OR REPLACE FUNCTION entity__retrieve_credit
  34. (in_entity_id int, in_entity_cass int)
  35. RETURNS entity_credit_search_return AS
  36. $$
  37. DECLARE out_row customer_search_return;
  38. BEGIN
  39. SELECT c.legal_name, c.id, e.id, ec.entity_class, ec.discount,
  40. ec.taxincluded, ec.creditlimit, ec.terms, ec.meta_number,
  41. ec.business_id, ec.language_code, ec.pricegroup_id,
  42. ec.curr::char(3), ec.startdate, ec.enddate
  43. INTO out_row
  44. FROM company c
  45. JOIN entity e ON (c.entity_id = e.id)
  46. JOIN entity_credit_account ec ON (c.entity_id = ec.entity_id)
  47. WHERE e.id = in_entity_id
  48. AND ec.entity_class = in_entity_class;
  49. RETURN out_row;
  50. END;
  51. $$ LANGUAGE PLPGSQL;
  52. CREATE OR REPLACE FUNCTION entity_credit_save (
  53. in_id int, in_entity_class int,
  54. in_discount numeric, in_taxincluded bool, in_creditlimit numeric,
  55. in_discount_terms int,
  56. in_terms int, in_meta_number varchar(32), in_business_id int,
  57. in_language varchar(6), in_pricegroup_id int,
  58. in_curr char, in_startdate date, in_enddate date,
  59. in_notes text,
  60. in_name text, in_tax_id TEXT,
  61. in_threshold NUMERIC
  62. ) returns INT as $$
  63. DECLARE
  64. t_entity_class int;
  65. new_entity_id int;
  66. v_row company;
  67. l_id int;
  68. BEGIN
  69. -- TODO: Move every table to an upsert mode independantly.
  70. SELECT INTO v_row * FROM company WHERE id = in_id;
  71. IF NOT FOUND THEN
  72. -- do some inserts
  73. select nextval('entity_id_seq') into new_entity_id;
  74. insert into entity (id, name, entity_class)
  75. VALUES (new_entity_id, in_name, in_entity_class);
  76. INSERT INTO company ( entity_id, legal_name, tax_id )
  77. VALUES ( new_entity_id, in_name, in_tax_id );
  78. INSERT INTO entity_credit_account (
  79. entity_id,
  80. entity_class,
  81. discount,
  82. taxincluded,
  83. creditlimit,
  84. terms,
  85. meta_number,
  86. business_id,
  87. language_code,
  88. pricegroup_id,
  89. curr,
  90. startdate,
  91. enddate,
  92. discount_terms,
  93. threshold
  94. )
  95. VALUES (
  96. new_entity_id,
  97. in_entity_class,
  98. in_discount / 100,
  99. in_taxincluded,
  100. in_creditlimit,
  101. in_terms,
  102. in_meta_number,
  103. in_business_id,
  104. in_language,
  105. in_pricegroup_id,
  106. in_curr,
  107. in_startdate,
  108. in_enddate,
  109. in_discount_terms,
  110. in_threshold
  111. );
  112. -- entity note class
  113. insert into entity_note (note_class, note, ref_key, vector) VALUES (
  114. 1, in_notes, new_entity_id, '');
  115. return new_entity_id;
  116. ELSIF FOUND THEN
  117. update company set tax_id = in_tax_id where id = in_id;
  118. update entity_credit_account SET
  119. discount = in_discount,
  120. taxincluded = in_taxincluded,
  121. creditlimit = in_creditlimit,
  122. terms = in_terms,
  123. meta_number = in_meta_number,
  124. business_id = in_business_id,
  125. language_code = in_language,
  126. pricegroup_id = in_pricegroup_id,
  127. curr = in_curr,
  128. startdate = in_startdate,
  129. enddate = in_enddate,
  130. threshold = in_threshold,
  131. discount_terms = in_discount_terms
  132. where entity_id = v_row.entity_id;
  133. UPDATE entity_note SET
  134. note = in_note
  135. WHERE ref_key = v_row.entity_id;
  136. return in_id;
  137. END IF;
  138. END;
  139. $$ language 'plpgsql';
  140. CREATE OR REPLACE FUNCTION company__list_locations(in_entity_id int)
  141. RETURNS SETOF location_result AS
  142. $$
  143. DECLARE out_row RECORD;
  144. BEGIN
  145. FOR out_row IN
  146. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  147. l.state, c.name, lc.class
  148. FROM location l
  149. JOIN company_to_location ctl ON (ctl.location_id = l.id)
  150. JOIN company cp ON (ctl.company_id = cp.id)
  151. JOIN location_class lc ON (ctl.location_class = lc.id)
  152. JOIN country c ON (c.id = l.country_id)
  153. WHERE cp.entity_id = in_entity_id
  154. ORDER BY lc.id, l.id, c.name
  155. LOOP
  156. RETURN NEXT out_row;
  157. END LOOP;
  158. END;
  159. $$ LANGUAGE PLPGSQL;
  160. CREATE TYPE contact_list AS (
  161. class text,
  162. contact text
  163. );
  164. CREATE OR REPLACE FUNCTION company__list_contacts(in_entity_id int)
  165. RETURNS SETOF contact_list AS
  166. $$
  167. DECLARE out_row RECORD;
  168. BEGIN
  169. FOR out_row IN
  170. SELECT cc.class, c.contact
  171. FROM company_to_contact c
  172. JOIN contact_class cc ON (c.contact_class_id = cc.id)
  173. JOIN company cp ON (c.company_id = cp.id)
  174. WHERE cp.entity_id = in_entity_id
  175. LOOP
  176. RETURN NEXT out_row;
  177. END LOOP;
  178. END;
  179. $$ LANGUAGE plpgsql;
  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_list AS
  222. $$
  223. DECLARE out_row record;
  224. BEGIN
  225. FOR out_row IN
  226. SELECT id, note
  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;