summaryrefslogtreecommitdiff
path: root/sql/modules/Customer.sql
blob: c0a5d6402fc220d1f6d7cbe825cc5040babbbf57 (plain)
  1. BEGIN;
  2. CREATE TYPE customer_search_return AS (
  3. legal_name text,
  4. id int,
  5. entity_id int,
  6. entity_class int,
  7. discount numeric,
  8. taxincluded bool,
  9. creditlimit numeric,
  10. terms int2,
  11. customernumber int,
  12. cc text,
  13. bcc text,
  14. business_id int,
  15. language_code text,
  16. pricegroup_id int,
  17. curr char,
  18. startdate date,
  19. enddate date,
  20. bic varchar,
  21. iban varchar,
  22. note text
  23. );
  24. CREATE OR REPLACE FUNCTION customer_save (
  25. in_id int,
  26. in_discount numeric, in_taxincluded bool, in_creditlimit numeric,
  27. in_discount_terms int,
  28. in_terms int, in_meta_number varchar(32), in_cc text, in_bcc text,
  29. in_business_id int, in_language varchar(6), in_pricegroup_id int,
  30. in_curr char, in_startdate date, in_enddate date,
  31. in_bic text, in_iban text,
  32. in_notes text,
  33. in_name text, in_tax_id TEXT,
  34. in_threshold
  35. ) returns INT as $$
  36. -- does not require entity_class, as entity_class is a known given to be 1
  37. DECLARE
  38. t_entity_class int;
  39. new_entity_id int;
  40. v_row company;
  41. l_id int;
  42. BEGIN
  43. t_entity_class := 1;
  44. SELECT INTO v_row * FROM company WHERE id = in_id;
  45. IF NOT FOUND THEN
  46. -- do some inserts
  47. new_entity_id := nextval('entity_id_seq');
  48. insert into entity (id, name, entity_class)
  49. VALUES (new_entity_id, in_name, t_entity_class);
  50. INSERT INTO company ( id, entity_id, legal_name, tax_id )
  51. VALUES ( in_id, new_entity_id, in_name, in_tax_id );
  52. INSERT INTO entity_credit_account (
  53. entity_id,
  54. entity_class,
  55. discount,
  56. taxincluded,
  57. creditlimit,
  58. terms,
  59. meta_number,
  60. cc,
  61. bcc,
  62. business_id,
  63. language_code,
  64. pricegroup_id,
  65. curr,
  66. startdate,
  67. enddate,
  68. discountterms,
  69. threshold
  70. )
  71. VALUES (
  72. new_entity_id,
  73. t_entity_class,
  74. in_discount,
  75. in_taxincluded,
  76. in_creditlimit,
  77. in_terms,
  78. in_meta_number,
  79. in_cc,
  80. in_bcc,
  81. in_business_id,
  82. in_language,
  83. in_pricegroup_id,
  84. in_curr,
  85. in_startdate,
  86. in_enddate,
  87. in_discount_terms,
  88. in_threashold
  89. );
  90. INSERT INTO entity_bank_account (
  91. entity_id,
  92. bic,
  93. iban
  94. )
  95. VALUES (
  96. new_entity_id,
  97. in_bic,
  98. in_iban
  99. );
  100. -- entity note class
  101. insert into entity_note (note_class, note, ref_key, vector) VALUES (
  102. 1, in_notes, new_entity_id, '');
  103. ELSIF FOUND THEN
  104. update company set tax_id = in_tax_id where id = in_id;
  105. update entity_credit_account SET
  106. discount = in_discount,
  107. taxincluded = in_taxincluded,
  108. creditlimit = in_creditlimit,
  109. terms = in_terms,
  110. meta_number = in_meta_number,
  111. cc = in_cc,
  112. bcc = in_bcc,
  113. business_id = in_business_id,
  114. language_code = in_language,
  115. pricegroup_id = in_pricegroup_id,
  116. curr = in_curr,
  117. startdate = in_startdate,
  118. enddate = in_enddate,
  119. threshold = in_threshold,
  120. discount_terms = in_discount_terms
  121. where entity_id = v_row.entity_id;
  122. UPDATE entity_bank_account SET
  123. bic = in_bic,
  124. iban = in_iban
  125. WHERE entity_id = v_row.entity_id;
  126. UPDATE entity_note SET
  127. note = in_note
  128. WHERE ref_key = v_row.entity_id;
  129. END IF;
  130. return in_id;
  131. END;
  132. $$ language 'plpgsql';
  133. CREATE OR REPLACE FUNCTION customer_location_save (
  134. in_company_id int,
  135. in_location_class int, in_line_one text, in_line_two text,
  136. in_city_province TEXT, in_mail_code text, in_country_code int,
  137. in_created date
  138. ) returns int AS $$
  139. BEGIN
  140. return _entity_location_save(
  141. in_company_id,
  142. in_location_class, in_line_one, in_line_two,
  143. in_city_province , in_mail_code, in_country_code,
  144. in_created);
  145. END;
  146. $$ language 'plpgsql';
  147. CREATE OR REPLACE FUNCTION customer_search(in_pattern TEXT) returns setof customer_search_return as $$
  148. -- searches customer name, account number, street address, city, state,
  149. -- other location-based stuff
  150. declare
  151. v_row customer_search_return;
  152. query text;
  153. begin
  154. for v_row in select c.legal_name, v.* from customer v
  155. join company c on c.entity_id = v.entity_id
  156. join entity e on e.id = v.entity_id
  157. join company_to_location ctl on c.id = ctl.company_id
  158. join location l on l.id = ctl.location_id
  159. where l.line_one % in_pattern
  160. OR l.line_two % in_pattern
  161. OR l.line_three % in_pattern
  162. OR l.city_province % in_pattern
  163. OR c.legal_name % in_pattern
  164. OR e.name % in_pattern
  165. LOOP
  166. RETURN NEXT v_row;
  167. END LOOP;
  168. RETURN;
  169. end;
  170. $$ language 'plpgsql';
  171. CREATE OR REPLACE FUNCTION customer_retrieve(INT) returns setof customer as $$
  172. select v.* from customer v
  173. join company c on c.entity_id = v.entity_id
  174. where v.id = $1;
  175. $$ language 'sql';
  176. COMMIT;
  177. CREATE OR REPLACE FUNCTION customer_next_customer_id() returns int as $$
  178. select nextval('company_id_seq');
  179. $$ language 'sql';ƒ