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