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