summaryrefslogtreecommitdiff
path: root/sql/modules/Company.sql
blob: e630a6a28f2319fc6aff9b72fb893d9537dc0d30 (plain)
  1. -- BEGIN;
  2. CREATE TYPE company_search_result AS (
  3. entity_id int,
  4. entity_control_code text,
  5. company_id int,
  6. entity_credit_id int,
  7. meta_number text,
  8. credit_description text,
  9. entity_class int,
  10. legal_name text,
  11. sic_code text,
  12. business_type text,
  13. curr text
  14. );
  15. CREATE OR REPLACE FUNCTION company__search
  16. (in_account_class int, in_contact text, in_contact_info text[],
  17. in_meta_number text, in_address text, in_city text, in_state text,
  18. in_mail_code text, in_country text, in_date_from date, in_date_to date,
  19. in_business_id int, in_legal_name text, in_control_code text)
  20. RETURNS SETOF company_search_result AS $$
  21. DECLARE
  22. out_row company_search_result;
  23. loop_count int;
  24. t_contact_info text[];
  25. BEGIN
  26. t_contact_info = in_contact_info;
  27. FOR out_row IN
  28. SELECT e.id, e.control_code, c.id, ec.id, ec.meta_number,
  29. ec.description, ec.entity_class,
  30. c.legal_name, c.sic_code, b.description , ec.curr::text
  31. FROM entity e
  32. JOIN company c ON (e.id = c.entity_id)
  33. JOIN entity_credit_account ec ON (ec.entity_id = e.id)
  34. LEFT JOIN business b ON (ec.business_id = b.id)
  35. WHERE ec.entity_class = in_account_class
  36. AND (e.control_code = in_control_code
  37. or in_control_code IS NULL)
  38. AND (c.id IN (select company_id FROM company_to_contact
  39. WHERE contact LIKE ALL(t_contact_info))
  40. OR '' LIKE ALL(t_contact_info))
  41. AND (ec.meta_number = in_meta_number
  42. OR in_meta_number IS NULL)
  43. AND (c.legal_name like '%' || in_legal_name || '%'
  44. OR in_legal_name IS NULL)
  45. AND ((in_address IS NULL AND in_city IS NULL
  46. AND in_state IS NULL
  47. AND in_country IS NULL)
  48. OR (c.id IN
  49. (select company_id FROM company_to_location
  50. WHERE location_id IN
  51. (SELECT id FROM location
  52. WHERE line_one
  53. ilike '%' ||
  54. coalesce(in_address, '')
  55. || '%'
  56. AND city ILIKE
  57. '%' ||
  58. coalesce(in_city, '')
  59. || '%'
  60. AND state ILIKE
  61. '%' ||
  62. coalesce(in_state, '')
  63. || '%'
  64. AND mail_code ILIKE
  65. '%' ||
  66. coalesce(in_mail_code,
  67. '')
  68. || '%'
  69. AND country_id IN
  70. (SELECT id FROM country
  71. WHERE name LIKE '%' ||
  72. in_country ||'%'
  73. OR short_name
  74. ilike
  75. in_country)))))
  76. AND (ec.business_id =
  77. coalesce(in_business_id, ec.business_id)
  78. OR (ec.business_id IS NULL
  79. AND in_business_id IS NULL))
  80. AND (ec.startdate <= coalesce(in_date_to,
  81. ec.startdate)
  82. OR (ec.startdate IS NULL))
  83. AND (ec.enddate >= coalesce(in_date_from, ec.enddate)
  84. OR (ec.enddate IS NULL))
  85. LOOP
  86. RETURN NEXT out_row;
  87. END LOOP;
  88. END;
  89. $$ language plpgsql;
  90. CREATE OR REPLACE FUNCTION entity__save_notes(in_entity_id int, in_note text)
  91. RETURNS INT AS
  92. $$
  93. DECLARE out_id int;
  94. BEGIN
  95. -- TODO, change this to create vector too
  96. INSERT INTO entity_note (ref_key, note_class, entity_id, note, vector)
  97. VALUES (in_entity_id, 1, in_entity_id, in_note, '');
  98. SELECT currval('note_id_seq') INTO out_id;
  99. RETURN out_id;
  100. END;
  101. $$ LANGUAGE PLPGSQL;
  102. CREATE OR REPLACE FUNCTION eca__save_notes(in_credit_id int, in_note text)
  103. RETURNS INT AS
  104. $$
  105. DECLARE out_id int;
  106. BEGIN
  107. -- TODO, change this to create vector too
  108. INSERT INTO eca_note (ref_key, note_class, note, vector)
  109. VALUES (in_credit_id, 3, in_note, '');
  110. SELECT currval('note_id_seq') INTO out_id;
  111. RETURN out_id;
  112. END;
  113. $$ LANGUAGE PLPGSQL;
  114. CREATE OR REPLACE FUNCTION entity_credit_get_id_by_meta_number
  115. (in_meta_number text, in_account_class int)
  116. returns int AS
  117. $$
  118. DECLARE out_credit_id int;
  119. BEGIN
  120. SELECT id INTO out_credit_id
  121. FROM entity_credit_account
  122. WHERE meta_number = in_meta_number
  123. AND entity_class = in_account_class;
  124. RETURN out_credit_id;
  125. END;
  126. $$ LANGUAGE plpgsql;
  127. CREATE OR REPLACE FUNCTION entity_list_contact_class()
  128. RETURNS SETOF contact_class AS
  129. $$
  130. DECLARE out_row RECORD;
  131. BEGIN
  132. FOR out_row IN
  133. SELECT * FROM contact_class ORDER BY id
  134. LOOP
  135. RETURN NEXT out_row;
  136. END LOOP;
  137. END;
  138. $$ language plpgsql;
  139. CREATE TYPE entity_credit_search_return AS (
  140. legal_name text,
  141. id int,
  142. entity_id int,
  143. entity_control_code text,
  144. entity_class int,
  145. discount numeric,
  146. taxincluded bool,
  147. creditlimit numeric,
  148. terms int2,
  149. meta_number text,
  150. credit_description text,
  151. business_id int,
  152. language_code text,
  153. pricegroup_id int,
  154. curr char(3),
  155. startdate date,
  156. enddate date,
  157. ar_ap_account_id int,
  158. cash_account_id int,
  159. tax_id text,
  160. threshold numeric
  161. );
  162. CREATE TYPE entity_credit_retrieve AS (
  163. id int,
  164. entity_id int,
  165. entity_class int,
  166. discount numeric,
  167. taxincluded bool,
  168. creditlimit numeric,
  169. terms int2,
  170. meta_number text,
  171. description text,
  172. business_id int,
  173. language_code text,
  174. pricegroup_id int,
  175. curr text,
  176. startdate date,
  177. enddate date,
  178. ar_ap_account_id int,
  179. cash_account_id int,
  180. threshold numeric,
  181. control_code text,
  182. credit_id int
  183. );
  184. COMMENT ON TYPE entity_credit_search_return IS
  185. $$ This may change in 1.4 and should not be relied upon too much $$;
  186. CREATE OR REPLACE FUNCTION entity_credit_get_id
  187. (in_entity_id int, in_entity_class int, in_meta_number text)
  188. RETURNS int AS $$
  189. DECLARE out_var int;
  190. BEGIN
  191. SELECT id INTO out_var FROM entity_credit_account
  192. WHERE entity_id = in_entity_id
  193. AND in_entity_class = entity_class
  194. AND in_meta_number = meta_number;
  195. RETURN out_var;
  196. END;
  197. $$ language plpgsql;
  198. CREATE OR REPLACE FUNCTION entity__list_credit
  199. (in_entity_id int, in_entity_class int)
  200. RETURNS SETOF entity_credit_retrieve AS
  201. $$
  202. DECLARE out_row entity_credit_retrieve;
  203. BEGIN
  204. FOR out_row IN
  205. SELECT c.id, e.id, ec.entity_class, ec.discount,
  206. ec.taxincluded, ec.creditlimit, ec.terms,
  207. ec.meta_number, ec.description, ec.business_id,
  208. ec.language_code,
  209. ec.pricegroup_id, ec.curr, ec.startdate,
  210. ec.enddate, ec.ar_ap_account_id, ec.cash_account_id,
  211. ec.threshold, e.control_code, ec.id
  212. FROM company c
  213. JOIN entity e ON (c.entity_id = e.id)
  214. JOIN entity_credit_account ec ON (c.entity_id = ec.entity_id)
  215. WHERE e.id = in_entity_id
  216. AND ec.entity_class =
  217. CASE WHEN in_entity_class = 3 THEN 2
  218. WHEN in_entity_class IS NULL
  219. THEN ec.entity_class
  220. ELSE in_entity_class END
  221. LOOP
  222. RETURN NEXT out_row;
  223. END LOOP;
  224. END;
  225. $$ LANGUAGE PLPGSQL;
  226. CREATE OR REPLACE FUNCTION company_retrieve (in_entity_id int) RETURNS company AS
  227. $$
  228. DECLARE t_company company;
  229. BEGIN
  230. SELECT * INTO t_company FROM company WHERE entity_id = in_entity_id;
  231. RETURN t_company;
  232. END;
  233. $$ language plpgsql;
  234. CREATE TYPE company_billing_info AS (
  235. legal_name text,
  236. meta_number text,
  237. control_code text,
  238. tax_id text,
  239. street1 text,
  240. street2 text,
  241. street3 text,
  242. city text,
  243. state text,
  244. mail_code text,
  245. country text
  246. );
  247. CREATE OR REPLACE FUNCTION company_get_billing_info (in_id int)
  248. returns company_billing_info as
  249. $$
  250. DECLARE out_var company_billing_info;
  251. t_id INT;
  252. BEGIN
  253. select c.legal_name, eca.meta_number, e.control_code, c.tax_id, a.line_one, a.line_two, a.line_three,
  254. a.city, a.state, a.mail_code, cc.name
  255. into out_var
  256. FROM company c
  257. JOIN entity e ON (c.entity_id = e.id)
  258. JOIN entity_credit_account eca ON (eca.entity_id = e.id)
  259. LEFT JOIN eca_to_location cl ON (eca.id = cl.credit_id)
  260. LEFT JOIN location a ON (a.id = cl.location_id)
  261. LEFT JOIN country cc ON (cc.id = a.country_id)
  262. WHERE eca.id = in_id AND (location_class = 1 or location_class is null);
  263. RETURN out_var;
  264. END;
  265. $$ language plpgsql;
  266. CREATE OR REPLACE FUNCTION company_save (
  267. in_id int, in_control_code text, in_entity_class int,
  268. in_name text, in_tax_id TEXT,
  269. in_entity_id int, in_sic_code text
  270. ) RETURNS INT AS $$
  271. DECLARE t_entity_id INT;
  272. t_company_id INT;
  273. t_control_code TEXT;
  274. BEGIN
  275. t_company_id := in_id;
  276. IF in_control_code IS NULL THEN
  277. t_control_code := setting_increment('company_control');
  278. ELSE
  279. t_control_code := in_control_code;
  280. END IF;
  281. IF in_entity_id IS NULL THEN
  282. IF in_id IS NULL THEN
  283. RAISE NOTICE 'in_id is null';
  284. SELECT id INTO t_company_id FROM company
  285. WHERE entity_id = (SELECT id FROM entity WHERE
  286. control_code = t_control_code);
  287. END IF;
  288. IF t_company_id IS NOT NULL THEN
  289. SELECT entity_id INTO t_entity_id FROM company
  290. WHERE id = t_company_id;
  291. END IF;
  292. ELSE
  293. t_entity_id := in_entity_id;
  294. END IF;
  295. IF t_entity_id IS NULL THEN
  296. INSERT INTO entity (name, entity_class, control_code)
  297. VALUES (in_name, in_entity_class, t_control_code);
  298. t_entity_id := currval('entity_id_seq');
  299. END IF;
  300. UPDATE company
  301. SET legal_name = in_name,
  302. tax_id = in_tax_id,
  303. sic_code = in_sic_code
  304. WHERE id = t_company_id;
  305. IF NOT FOUND THEN
  306. INSERT INTO company(entity_id, legal_name, tax_id, sic_code)
  307. VALUES (t_entity_id, in_name, in_tax_id, in_sic_code);
  308. END IF;
  309. RETURN t_entity_id;
  310. END;
  311. $$ LANGUAGE PLPGSQL;
  312. CREATE OR REPLACE FUNCTION entity_credit_save (
  313. in_credit_id int, in_entity_class int,
  314. in_entity_id int, in_description text,
  315. in_discount numeric, in_taxincluded bool, in_creditlimit numeric,
  316. in_discount_terms int,
  317. in_terms int, in_meta_number varchar(32), in_business_id int,
  318. in_language varchar(6), in_pricegroup_id int,
  319. in_curr char, in_startdate date, in_enddate date,
  320. in_threshold NUMERIC,
  321. in_ar_ap_account_id int,
  322. in_cash_account_id int
  323. ) returns INT as $$
  324. DECLARE
  325. t_entity_class int;
  326. l_id int;
  327. t_meta_number text;
  328. t_mn_default_key text;
  329. BEGIN
  330. -- TODO: Move to mapping table.
  331. IF in_entity_class = 1 THEN
  332. t_mn_default_key := 'vendornumber';
  333. ELSIF in_entity_class = 2 THEN
  334. t_mn_default_key := 'customernumber';
  335. END IF;
  336. IF in_meta_number IS NULL THEN
  337. t_meta_number := setting_increment(t_mn_default_key);
  338. ELSE
  339. t_meta_number := in_meta_number;
  340. END IF;
  341. update entity_credit_account SET
  342. discount = in_discount,
  343. taxincluded = in_taxincluded,
  344. creditlimit = in_creditlimit,
  345. description = in_description,
  346. terms = in_terms,
  347. ar_ap_account_id = in_ar_ap_account_id,
  348. cash_account_id = in_cash_account_id,
  349. meta_number = t_meta_number,
  350. business_id = in_business_id,
  351. language_code = in_language,
  352. pricegroup_id = in_pricegroup_id,
  353. curr = in_curr,
  354. startdate = in_startdate,
  355. enddate = in_enddate,
  356. threshold = in_threshold,
  357. discount_terms = in_discount_terms
  358. where id = in_credit_id;
  359. IF FOUND THEN
  360. RETURN in_credit_id;
  361. ELSE
  362. INSERT INTO entity_credit_account (
  363. entity_id,
  364. entity_class,
  365. discount,
  366. description,
  367. taxincluded,
  368. creditlimit,
  369. terms,
  370. meta_number,
  371. business_id,
  372. language_code,
  373. pricegroup_id,
  374. curr,
  375. startdate,
  376. enddate,
  377. discount_terms,
  378. threshold,
  379. ar_ap_account_id,
  380. cash_account_id
  381. )
  382. VALUES (
  383. in_entity_id,
  384. in_entity_class,
  385. in_discount,
  386. in_description,
  387. in_taxincluded,
  388. in_creditlimit,
  389. in_terms,
  390. t_meta_number,
  391. in_business_id,
  392. in_language,
  393. in_pricegroup_id,
  394. in_curr,
  395. in_startdate,
  396. in_enddate,
  397. in_discount_terms,
  398. in_threshold,
  399. in_ar_ap_account_id,
  400. in_cash_account_id
  401. );
  402. RETURN currval('entity_credit_account_id_seq');
  403. END IF;
  404. END;
  405. $$ language 'plpgsql';
  406. CREATE OR REPLACE FUNCTION company__list_locations(in_entity_id int)
  407. RETURNS SETOF location_result AS
  408. $$
  409. DECLARE out_row RECORD;
  410. BEGIN
  411. FOR out_row IN
  412. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  413. l.state, l.mail_code, c.name, lc.class
  414. FROM location l
  415. JOIN company_to_location ctl ON (ctl.location_id = l.id)
  416. JOIN location_class lc ON (ctl.location_class = lc.id)
  417. JOIN country c ON (c.id = l.country_id)
  418. WHERE ctl.company_id = (select id from company where entity_id = in_entity_id)
  419. ORDER BY lc.id, l.id, c.name
  420. LOOP
  421. RETURN NEXT out_row;
  422. END LOOP;
  423. END;
  424. $$ LANGUAGE PLPGSQL;
  425. CREATE TYPE contact_list AS (
  426. class text,
  427. class_id int,
  428. description text,
  429. contact text
  430. );
  431. CREATE OR REPLACE FUNCTION company__list_contacts(in_entity_id int)
  432. RETURNS SETOF contact_list AS $$
  433. DECLARE out_row contact_list;
  434. BEGIN
  435. FOR out_row IN
  436. SELECT cl.class, cl.id, c.description, c.contact
  437. FROM company_to_contact c
  438. JOIN contact_class cl ON (c.contact_class_id = cl.id)
  439. WHERE company_id =
  440. (select id FROM company
  441. WHERE entity_id = in_entity_id)
  442. LOOP
  443. return next out_row;
  444. END LOOP;
  445. END;
  446. $$ language plpgsql;
  447. CREATE OR REPLACE FUNCTION company__list_bank_account(in_entity_id int)
  448. RETURNS SETOF entity_bank_account AS
  449. $$
  450. DECLARE out_row entity_bank_account%ROWTYPE;
  451. BEGIN
  452. FOR out_row IN
  453. SELECT * from entity_bank_account where entity_id = in_entity_id
  454. LOOP
  455. RETURN NEXT out_row;
  456. END LOOP;
  457. END;
  458. $$ LANGUAGE PLPGSQL;
  459. CREATE OR REPLACE FUNCTION entity__save_bank_account
  460. (in_entity_id int, in_credit_id int, in_bic text, in_iban text)
  461. RETURNS int AS
  462. $$
  463. DECLARE out_id int;
  464. BEGIN
  465. INSERT INTO entity_bank_account(entity_id, bic, iban)
  466. VALUES(in_entity_id, in_bic, in_iban);
  467. SELECT CURRVAL('entity_bank_account_id_seq') INTO out_id ;
  468. IF in_credit_id IS NOT NULL THEN
  469. UPDATE entity_credit_account SET bank_account = out_id
  470. WHERE id = in_credit_id;
  471. END IF;
  472. RETURN out_id;
  473. END;
  474. $$ LANGUAGE PLPGSQL;
  475. CREATE OR REPLACE FUNCTION entity__save_bank_account
  476. (in_entity_id int, in_bic text, in_iban text)
  477. RETURNS int AS
  478. $$
  479. DECLARE out_id int;
  480. BEGIN
  481. INSERT INTO entity_bank_account(entity_id, bic, iban)
  482. VALUES(in_entity_id, in_bic, in_iban);
  483. SELECT CURRVAL('entity_bank_account_id_seq') INTO out_id ;
  484. RETURN out_id;
  485. END;
  486. $$ LANGUAGE PLPGSQL;
  487. CREATE OR REPLACE FUNCTION company__save_contact
  488. (in_entity_id int, in_contact_class int, in_description text, in_contact text)
  489. RETURNS INT AS
  490. $$
  491. DECLARE out_id int;
  492. BEGIN
  493. INSERT INTO company_to_contact(company_id, contact_class_id,
  494. description, contact)
  495. SELECT id, in_contact_class, in_description, in_contact FROM company
  496. WHERE entity_id = in_entity_id;
  497. RETURN 1;
  498. END;
  499. $$ LANGUAGE PLPGSQL;
  500. CREATE TYPE entity_note_list AS (
  501. id int,
  502. note_class int,
  503. note text
  504. );
  505. CREATE OR REPLACE FUNCTION company__list_notes(in_entity_id int)
  506. RETURNS SETOF entity_note AS
  507. $$
  508. DECLARE out_row record;
  509. BEGIN
  510. FOR out_row IN
  511. SELECT *
  512. FROM entity_note
  513. WHERE ref_key = in_entity_id
  514. ORDER BY created
  515. LOOP
  516. RETURN NEXT out_row;
  517. END LOOP;
  518. END;
  519. $$ LANGUAGE PLPGSQL;
  520. CREATE OR REPLACE FUNCTION eca__list_notes(in_credit_id int)
  521. RETURNS SETOF note AS
  522. $$
  523. DECLARE out_row record;
  524. t_entity_id int;
  525. BEGIN
  526. SELECT entity_id INTO t_entity_id
  527. FROM entity_credit_account
  528. WHERE id = in_credit_id;
  529. FOR out_row IN
  530. SELECT *
  531. FROM note
  532. WHERE (note_class = 3 and ref_key = in_credit_id) or
  533. (note_class = 1 and ref_key = t_entity_id)
  534. ORDER BY created
  535. LOOP
  536. RETURN NEXT out_row;
  537. END LOOP;
  538. END;
  539. $$ LANGUAGE PLPGSQL SECURITY DEFINER;
  540. REVOKE EXECUTE ON FUNCTION eca__list_notes(INT) FROM public;
  541. CREATE OR REPLACE FUNCTION company__next_id() returns bigint as $$
  542. select nextval('company_id_seq');
  543. $$ language 'sql';
  544. CREATE OR REPLACE FUNCTION company__location_save (
  545. in_entity_id int, in_location_id int,
  546. in_location_class int, in_line_one text, in_line_two text,
  547. in_city TEXT, in_state TEXT, in_mail_code text, in_country_code int,
  548. in_created date
  549. ) returns int AS $$
  550. BEGIN
  551. return _entity_location_save(
  552. in_entity_id, in_location_id,
  553. in_location_class, in_line_one, in_line_two,
  554. '', in_city , in_state, in_mail_code, in_country_code);
  555. END;
  556. $$ language 'plpgsql';
  557. create or replace function _entity_location_save(
  558. in_entity_id int, in_location_id int,
  559. in_location_class int, in_line_one text, in_line_two text,
  560. in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text,
  561. in_country_code int
  562. ) returns int AS $$
  563. DECLARE
  564. l_row location;
  565. l_id INT;
  566. t_company_id int;
  567. BEGIN
  568. SELECT id INTO t_company_id
  569. FROM company WHERE entity_id = in_entity_id;
  570. DELETE FROM company_to_location
  571. WHERE company_id = t_company_id
  572. AND location_class = in_location_class
  573. AND location_id = in_location_id;
  574. SELECT location_save(NULL, in_line_one, in_line_two, in_line_three, in_city,
  575. in_state, in_mail_code, in_country_code)
  576. INTO l_id;
  577. INSERT INTO company_to_location
  578. (company_id, location_class, location_id)
  579. VALUES (t_company_id, in_location_class, l_id);
  580. RETURN l_id;
  581. END;
  582. $$ language 'plpgsql';
  583. create or replace function eca__location_save(
  584. in_credit_id int, in_location_id int,
  585. in_location_class int, in_line_one text, in_line_two text,
  586. in_line_three text, in_city TEXT, in_state TEXT, in_mail_code text,
  587. in_country_code int
  588. ) returns int AS $$
  589. DECLARE
  590. l_row location;
  591. l_id INT;
  592. BEGIN
  593. DELETE FROM eca_to_location
  594. WHERE credit_id = in_credit_id
  595. AND location_class = in_location_class
  596. AND location_id = in_location_id;
  597. -- don't pass the in_location_id through because that is not safe.
  598. SELECT location_save(NULL, in_line_one, in_line_two, in_line_three,
  599. in_city,
  600. in_state, in_mail_code, in_country_code)
  601. INTO l_id;
  602. INSERT INTO eca_to_location
  603. (credit_id, location_class, location_id)
  604. VALUES (in_credit_id, in_location_class, l_id);
  605. RETURN l_id;
  606. END;
  607. $$ language 'plpgsql';
  608. CREATE OR REPLACE FUNCTION eca__list_locations(in_credit_id int)
  609. RETURNS SETOF location_result AS
  610. $$
  611. DECLARE out_row RECORD;
  612. BEGIN
  613. FOR out_row IN
  614. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  615. l.state, l.mail_code, c.name, lc.class
  616. FROM location l
  617. JOIN eca_to_location ctl ON (ctl.location_id = l.id)
  618. JOIN location_class lc ON (ctl.location_class = lc.id)
  619. JOIN country c ON (c.id = l.country_id)
  620. WHERE ctl.credit_id = in_credit_id
  621. ORDER BY lc.id, l.id, c.name
  622. LOOP
  623. RETURN NEXT out_row;
  624. END LOOP;
  625. END;
  626. $$ LANGUAGE PLPGSQL;
  627. CREATE OR REPLACE FUNCTION eca__list_contacts(in_credit_id int)
  628. RETURNS SETOF contact_list AS $$
  629. DECLARE out_row contact_list;
  630. BEGIN
  631. FOR out_row IN
  632. SELECT cl.class, cl.id, c.description, c.contact
  633. FROM eca_to_contact c
  634. JOIN contact_class cl ON (c.contact_class_id = cl.id)
  635. WHERE credit_id = in_credit_id
  636. LOOP
  637. return next out_row;
  638. END LOOP;
  639. END;
  640. $$ language plpgsql;
  641. CREATE OR REPLACE FUNCTION eca__save_contact
  642. (in_credit_id int, in_contact_class int, in_description text, in_contact text,
  643. in_old_contact text, in_old_contact_class int)
  644. RETURNS INT AS
  645. $$
  646. DECLARE out_id int;
  647. BEGIN
  648. DELETE FROM eca_to_contact
  649. WHERE credit_id = in_credit_id
  650. AND contact = in_old_contact
  651. AND contact_class_id = in_old_contact_class;
  652. INSERT INTO eca_to_contact(credit_id, contact_class_id,
  653. description, contact)
  654. VALUES (in_credit_id, in_contact_class, in_description, in_contact);
  655. RETURN 1;
  656. END;
  657. $$ LANGUAGE PLPGSQL;
  658. -- COMMIT;