summaryrefslogtreecommitdiff
path: root/sql/modules/Customer.sql
blob: 7f2a8da07e1ad80f79e2f2ba67b3ffea79ec0e05 (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 customer_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 int,
  24. cc text,
  25. bcc text,
  26. business_id int,
  27. language_code text,
  28. pricegroup_id int,
  29. curr char,
  30. startdate date,
  31. enddate date,
  32. bic varchar,
  33. iban varchar,
  34. note text
  35. );
  36. -- COMMENT ON TYPE customer_search_result IS
  37. -- $$ This structure will change greatly in 1.4.
  38. -- If you want to reply on it heavily, be prepared for breakage later. $$;
  39. CREATE OR REPLACE FUNCTION entity_credit_save (
  40. in_id int, in_entity_class int,
  41. in_discount numeric, in_taxincluded bool, in_creditlimit numeric,
  42. in_discount_terms int,
  43. in_terms int, in_meta_number varchar(32), in_business_id int,
  44. in_language varchar(6), in_pricegroup_id int,
  45. in_curr char, in_startdate date, in_enddate date,
  46. in_notes text,
  47. in_name text, in_tax_id TEXT,
  48. in_threshold NUMERIC
  49. ) returns INT as $$
  50. DECLARE
  51. t_entity_class int;
  52. new_entity_id int;
  53. v_row company;
  54. l_id int;
  55. BEGIN
  56. SELECT INTO v_row * FROM company WHERE id = in_id;
  57. IF NOT FOUND THEN
  58. -- do some inserts
  59. select nextval('entity_id_seq') into new_entity_id;
  60. insert into entity (id, name, entity_class)
  61. VALUES (new_entity_id, in_name, in_entity_class);
  62. INSERT INTO company ( entity_id, legal_name, tax_id )
  63. VALUES ( new_entity_id, in_name, in_tax_id );
  64. INSERT INTO entity_credit_account (
  65. entity_id,
  66. entity_class,
  67. discount,
  68. taxincluded,
  69. creditlimit,
  70. terms,
  71. meta_number,
  72. business_id,
  73. language_code,
  74. pricegroup_id,
  75. curr,
  76. startdate,
  77. enddate,
  78. discount_terms,
  79. threshold
  80. )
  81. VALUES (
  82. new_entity_id,
  83. in_entity_class,
  84. in_discount / 100,
  85. in_taxincluded,
  86. in_creditlimit,
  87. in_terms,
  88. in_meta_number,
  89. in_business_id,
  90. in_language,
  91. in_pricegroup_id,
  92. in_curr,
  93. in_startdate,
  94. in_enddate,
  95. in_discount_terms,
  96. in_threshold
  97. );
  98. -- entity note class
  99. insert into entity_note (note_class, note, ref_key, vector) VALUES (
  100. 1, in_notes, new_entity_id, '');
  101. return new_entity_id;
  102. ELSIF FOUND THEN
  103. update company set tax_id = in_tax_id where id = in_id;
  104. update entity_credit_account SET
  105. discount = in_discount,
  106. taxincluded = in_taxincluded,
  107. creditlimit = in_creditlimit,
  108. terms = in_terms,
  109. meta_number = in_meta_number,
  110. business_id = in_business_id,
  111. language_code = in_language,
  112. pricegroup_id = in_pricegroup_id,
  113. curr = in_curr,
  114. startdate = in_startdate,
  115. enddate = in_enddate,
  116. threshold = in_threshold,
  117. discount_terms = in_discount_terms
  118. where entity_id = v_row.entity_id;
  119. UPDATE entity_note SET
  120. note = in_note
  121. WHERE ref_key = v_row.entity_id;
  122. return in_id;
  123. END IF;
  124. END;
  125. $$ language 'plpgsql';
  126. CREATE OR REPLACE FUNCTION customer_location_save (
  127. in_company_id int,
  128. in_location_class int, in_line_one text, in_line_two text,
  129. in_line_three text,
  130. in_city TEXT, in_state text, in_mail_code text, in_country_code int
  131. ) returns int AS $$
  132. BEGIN
  133. return _entity_location_save(
  134. in_company_id,
  135. in_location_class, in_line_one, in_line_two, in_line_three,
  136. in_city, in_state, in_mail_code, in_country_code);
  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. CREATE OR REPLACE FUNCTION customer_next_customer_id() returns bigint as $$
  169. select nextval('company_id_seq');
  170. $$ language 'sql';
  171. COMMIT;