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