summaryrefslogtreecommitdiff
path: root/sql/modules/Payment.sql
blob: dc6ac74ad8cfc6724827f99a0900212748a56ab8 (plain)
  1. -- payment_get_open_accounts and the option to get all accounts need to be
  2. -- refactored and redesigned. -- CT
  3. CREATE OR REPLACE FUNCTION payment_get_open_accounts(in_account_class int)
  4. returns SETOF entity AS
  5. $$
  6. DECLARE out_entity entity%ROWTYPE;
  7. BEGIN
  8. FOR out_entity IN
  9. SELECT ec.id, cp.legal_name as name, e.entity_class, e.created
  10. FROM entity e
  11. JOIN entity_credit_account ec ON (ec.entity_id = e.id)
  12. JOIN company cp ON (cp.entity_id = e.id)
  13. WHERE ec.entity_class = in_account_class
  14. AND CASE WHEN in_account_class = 1 THEN
  15. e.id IN (SELECT entity_id FROM ap
  16. WHERE amount <> paid
  17. GROUP BY entity_id)
  18. WHEN in_account_class = 2 THEN
  19. e.id IN (SELECT entity_id FROM ar
  20. WHERE amount <> paid
  21. GROUP BY entity_id)
  22. END
  23. LOOP
  24. RETURN NEXT out_entity;
  25. END LOOP;
  26. END;
  27. $$ LANGUAGE PLPGSQL;
  28. COMMENT ON FUNCTION payment_get_open_accounts(int) IS
  29. $$ This function takes a single argument (1 for vendor, 2 for customer as
  30. always) and returns all entities with open accounts of the appropriate type. $$;
  31. CREATE OR REPLACE FUNCTION payment_get_all_accounts(in_account_class int)
  32. RETURNS SETOF entity AS
  33. $$
  34. DECLARE out_entity entity%ROWTYPE;
  35. BEGIN
  36. FOR out_entity IN
  37. SELECT ec.id,
  38. e.name, e.entity_class, e.created
  39. FROM entity e
  40. JOIN entity_credit_account ec ON (ec.entity_id = e.id)
  41. WHERE e.entity_class = in_account_class
  42. LOOP
  43. RETURN NEXT out_entity;
  44. END LOOP;
  45. END;
  46. $$ LANGUAGE PLPGSQL;
  47. COMMENT ON FUNCTION payment_get_open_accounts(int) IS
  48. $$ This function takes a single argument (1 for vendor, 2 for customer as
  49. always) and returns all entities with accounts of the appropriate type. $$;
  50. CREATE TYPE payment_invoice AS (
  51. invoice_id int,
  52. invnumber text,
  53. invoice_date date,
  54. amount numeric,
  55. discount numeric,
  56. due numeric
  57. );
  58. CREATE OR REPLACE FUNCTION payment_get_open_invoices
  59. (in_account_class int,
  60. in_entity_credit_id int,
  61. in_curr char(3),
  62. in_datefrom date,
  63. in_dateto date,
  64. in_amountfrom numeric,
  65. in_amountto numeric,
  66. in_department_id int)
  67. RETURNS SETOF payment_invoice AS
  68. $$
  69. DECLARE payment_inv payment_invoice;
  70. BEGIN
  71. FOR payment_inv IN
  72. SELECT a.id AS invoice_id, a.invnumber,
  73. a.transdate AS invoice_date, a.amount,
  74. CASE WHEN discount_terms
  75. > extract('days' FROM age(a.transdate))
  76. THEN 0
  77. ELSE (a.amount - a.paid) * c.discount / 100
  78. END AS discount,
  79. a.amount - a.paid -
  80. CASE WHEN discount_terms
  81. > extract('days' FROM age(a.transdate))
  82. THEN 0
  83. ELSE (a.amount - a.paid) * c.discount / 100
  84. END
  85. AS due
  86. FROM (SELECT id, invnumber, transdate, amount, entity_id,
  87. 1 as invoice_class, paid, curr,
  88. entity_credit_account, department_id
  89. FROM ap
  90. UNION
  91. SELECT id, invnumber, transdate, amount, entity_id,
  92. 2 AS invoice_class, paid, curr,
  93. entity_credit_account, department_id
  94. FROM ar
  95. ) a
  96. JOIN entity_credit_account c ON (c.id = a.entity_credit_account
  97. OR (a.entity_credit_account IS NULL and
  98. a.entity_id = c.entity_id))
  99. WHERE a.invoice_class = in_account_class
  100. AND c.entity_class = in_account_class
  101. AND a.amount - a.paid <> 0
  102. AND a.curr = in_curr
  103. AND (a.transdate >= in_datefrom
  104. OR in_datefrom IS NULL)
  105. AND (a.transdate <= in_dateto
  106. OR in_dateto IS NULL)
  107. AND (a.amount >= in_amountfrom
  108. OR in_amountfrom IS NULL)
  109. AND (a.amount <= in_amountto
  110. OR in_amountto IS NULL)
  111. AND (a.department_id = in_department_id
  112. OR in_department_id IS NULL)
  113. LOOP
  114. RETURN NEXT payment_inv;
  115. END LOOP;
  116. END;
  117. $$ LANGUAGE PLPGSQL;
  118. COMMENT ON FUNCTION payment_get_open_invoices(int, int, char(3), date, date, numeric, numeric, int) IS
  119. $$ This function takes three arguments:
  120. Type: 1 for vendor, 2 for customer
  121. Entity_id: The entity_id of the customer or vendor
  122. Currency: 3 characters for currency ('USD' for example).
  123. Returns all open invoices for the entity in question. $$;
  124. CREATE TYPE payment_contact_invoice AS (
  125. contact_id int,
  126. contact_name text,
  127. account_number text,
  128. total_due numeric,
  129. invoices text[],
  130. has_vouchers int
  131. );
  132. CREATE OR REPLACE FUNCTION payment_get_all_contact_invoices
  133. (in_account_class int, in_business_id int, in_currency char(3),
  134. in_date_from date, in_date_to date, in_batch_id int,
  135. in_ar_ap_accno text)
  136. RETURNS SETOF payment_contact_invoice AS
  137. $$
  138. DECLARE payment_item payment_contact_invoice;
  139. BEGIN
  140. FOR payment_item IN
  141. SELECT c.id AS contact_id, e.name AS contact_name,
  142. c.meta_number AS account_number,
  143. sum(p.due) AS total_due,
  144. compound_array(ARRAY[[
  145. a.id::text, a.invnumber, a.transdate::text,
  146. a.amount::text, (a.amount - p.due)::text,
  147. (CASE WHEN c.discount_terms
  148. > extract('days' FROM age(a.transdate))
  149. THEN 0
  150. ELSE (a.amount - coalesce((a.amount - p.due), 0)) * coalesce(c.discount, 0) / 100
  151. END)::text,
  152. (coalesce(p.due, 0) -
  153. (CASE WHEN c.discount_terms
  154. > extract('days' FROM age(a.transdate))
  155. THEN 0
  156. ELSE (coalesce(p.due, 0)) * coalesce(c.discount, 0) / 100
  157. END))::text]]),
  158. sum(case when v.batch_id = in_batch_id then 1
  159. else 0 END),
  160. bool_and(lock_record(a.id, (select max(session_id) FROM "session" where users_id = (
  161. select id from users WHERE username =
  162. SESSION_USER))))
  163. FROM entity e
  164. JOIN entity_credit_account c ON (e.id = c.entity_id)
  165. JOIN (SELECT id, invnumber, transdate, amount, entity_id,
  166. paid, curr, 1 as invoice_class,
  167. entity_credit_account, on_hold
  168. FROM ap
  169. WHERE in_account_class = 1
  170. UNION
  171. SELECT id, invnumber, transdate, amount, entity_id,
  172. paid, curr, 2 as invoice_class,
  173. entity_credit_account, on_hold
  174. FROM ar
  175. WHERE in_account_class = 2
  176. ORDER BY transdate
  177. ) a ON (a.entity_credit_account = c.id)
  178. JOIN transactions t ON (a.id = t.id)
  179. JOIN (SELECT trans_id,
  180. sum(CASE WHEN in_account_class = 1 THEN amount
  181. WHEN in_account_class = 2
  182. THEN amount * -1
  183. END) AS due
  184. FROM acc_trans
  185. JOIN chart ON (chart.id = acc_trans.chart_id)
  186. WHERE ((chart.link = 'AP' AND in_account_class = 1)
  187. OR (chart.link = 'AR' AND in_account_class = 2))
  188. GROUP BY trans_id) p ON (a.id = p.trans_id)
  189. LEFT JOIN voucher v ON (v.trans_id = a.id)
  190. WHERE v.batch_id = in_batch_id
  191. OR (a.invoice_class = in_account_class
  192. AND c.business_id =
  193. coalesce(in_business_id, c.business_id)
  194. AND ((a.transdate >= COALESCE(in_date_from, a.transdate)
  195. AND a.transdate <= COALESCE(in_date_to, a.transdate)))
  196. AND c.entity_class = in_account_class
  197. AND a.curr = in_currency
  198. AND a.entity_credit_account = c.id
  199. AND p.due <> 0
  200. AND a.amount <> a.paid
  201. AND NOT a.on_hold
  202. AND NOT (t.locked_by IS NOT NULL AND t.locked_by IN
  203. (select "session_id" FROM "session"
  204. WHERE users_id IN
  205. (select id from users
  206. where username <> SESSION_USER)))
  207. AND EXISTS (select trans_id FROM acc_trans
  208. WHERE trans_id = a.id AND
  209. chart_id = (SELECT id frOM chart
  210. WHERE accno
  211. = in_ar_ap_accno)
  212. ))
  213. GROUP BY c.id, e.name, c.meta_number, c.threshold
  214. HAVING sum(p.due) > c.threshold
  215. OR sum(case when v.batch_id = in_batch_id then 1
  216. else 0 END) > 0
  217. LOOP
  218. RETURN NEXT payment_item;
  219. END LOOP;
  220. END;
  221. $$ LANGUAGE plpgsql;
  222. COMMENT ON FUNCTION payment_get_all_contact_invoices
  223. (in_account_class int, in_business_type int, in_currency char(3),
  224. in_date_from date, in_date_to date, in_batch_id int,
  225. in_ar_ap_accno text) IS
  226. $$
  227. This function takes the following arguments (all prefaced with in_ in the db):
  228. account_class: 1 for vendor, 2 for customer
  229. business_type: integer of business.id.
  230. currency: char(3) of currency (for example 'USD')
  231. date_from, date_to: These dates are inclusive.
  232. 1;3B
  233. batch_id: For payment batches, where fees are concerned.
  234. ar_ap_accno: The AR/AP account number.
  235. This then returns a set of contact information with a 2 dimensional array
  236. cnsisting of outstanding invoices.
  237. $$;
  238. CREATE OR REPLACE FUNCTION payment_bulk_queue
  239. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  240. in_ar_ap_accno text, in_cash_accno text,
  241. in_payment_date date, in_account_class int)
  242. returns int as
  243. $$
  244. BEGIN
  245. INSERT INTO payments_queue
  246. (transactions, batch_id, source, total, ar_ap_accno, cash_accno,
  247. payment_date, account_class)
  248. VALUES
  249. (in_transactions, in_batch_id, in_source, in_total, in_ar_ap_accno,
  250. in_cash_accno, in_payment_date, in_account_class);
  251. RETURN array_upper(in_transactions, 1) -
  252. array_lower(in_transactions, 1);
  253. END;
  254. $$ LANGUAGE PLPGSQL;
  255. CREATE OR REPLACE FUNCTION job__process_payment(in_job_id int)
  256. RETURNS bool AS $$
  257. DECLARE
  258. queue_record RECORD;
  259. t_auth_name text;
  260. t_counter int;
  261. BEGIN
  262. -- TODO: Move the set session authorization into a utility function
  263. SELECT entered_by INTO t_auth_name FROM pending_job
  264. WHERE id = in_job_id;
  265. EXECUTE 'SET SESSION AUTHORIZATION ' || quote_ident(t_auth_name);
  266. t_counter := 0;
  267. FOR queue_record IN
  268. SELECT *
  269. FROM payments_queue WHERE job_id = in_job_id
  270. LOOP
  271. PERFORM payment_bulk_post
  272. (queue_record.transactions, queue_record.batch_id,
  273. queue_record.source, queue_record.total,
  274. queue_record.ar_ap_accno,
  275. queue_record.cash_accno,
  276. queue_record.payment_date,
  277. queue_record.account_class);
  278. t_counter := t_counter + 1;
  279. RAISE NOTICE 'Processed record %, starting transaction %',
  280. t_counter, queue_record.transactions[1][1];
  281. END LOOP;
  282. DELETE FROM payments_queue WHERE job_id = in_job_id;
  283. UPDATE pending_job
  284. SET completed_at = timeofday()::timestamp,
  285. success = true
  286. WHERE id = in_job_id;
  287. RETURN TRUE;
  288. END;
  289. $$ language plpgsql;
  290. CREATE OR REPLACE FUNCTION job__create(in_batch_class int, in_batch_id int)
  291. RETURNS int AS
  292. $$
  293. BEGIN
  294. INSERT INTO pending_job (batch_class, batch_id)
  295. VALUES (coalesce(in_batch_class, 3), in_batch_id);
  296. RETURN currval('pending_job_id_seq');
  297. END;
  298. $$ LANGUAGE PLPGSQL;
  299. CREATE TYPE job__status AS (
  300. completed int, -- 1 for completed, 0 for no
  301. success int, -- 1 for success, 0 for no
  302. completed_at timestamp,
  303. error_condition text -- error if not successful
  304. );
  305. CREATE OR REPLACE FUNCTION job__status(in_job_id int) RETURNS job__status AS
  306. $$
  307. DECLARE out_row job__status;
  308. BEGIN
  309. SELECT (completed_at IS NULL)::INT, success::int, completed_at,
  310. error_condition
  311. INTO out_row
  312. FROM pending_job
  313. WHERE id = in_job_id;
  314. RETURN out_row;
  315. END;
  316. $$ language plpgsql;
  317. CREATE OR REPLACE FUNCTION payment_bulk_post
  318. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  319. in_ar_ap_accno text, in_cash_accno text,
  320. in_payment_date date, in_account_class int)
  321. RETURNS int AS
  322. $$
  323. DECLARE
  324. out_count int;
  325. t_voucher_id int;
  326. t_trans_id int;
  327. t_amount numeric;
  328. t_ar_ap_id int;
  329. t_cash_id int;
  330. BEGIN
  331. IF in_batch_id IS NULL THEN
  332. -- t_voucher_id := NULL;
  333. RAISE EXCEPTION 'Bulk Post Must be from Batch!';
  334. ELSE
  335. INSERT INTO voucher (batch_id, batch_class, trans_id)
  336. values (in_batch_id, 3, in_transactions[1][1]);
  337. t_voucher_id := currval('voucher_id_seq');
  338. END IF;
  339. select id into t_ar_ap_id from chart where accno = in_ar_ap_accno;
  340. select id into t_cash_id from chart where accno = in_cash_accno;
  341. FOR out_count IN
  342. array_lower(in_transactions, 1) ..
  343. array_upper(in_transactions, 1)
  344. LOOP
  345. INSERT INTO acc_trans
  346. (trans_id, chart_id, amount, approved, voucher_id,
  347. transdate, source)
  348. VALUES
  349. (in_transactions[out_count][1],
  350. case when in_account_class = 1 THEN t_cash_id
  351. WHEN in_account_class = 2 THEN t_ar_ap_id
  352. ELSE -1 END,
  353. in_transactions[out_count][2],
  354. CASE WHEN t_voucher_id IS NULL THEN true
  355. ELSE false END,
  356. t_voucher_id, in_payment_date, in_source);
  357. INSERT INTO acc_trans
  358. (trans_id, chart_id, amount, approved, voucher_id,
  359. transdate, source)
  360. VALUES
  361. (in_transactions[out_count][1],
  362. case when in_account_class = 1 THEN t_ar_ap_id
  363. WHEN in_account_class = 2 THEN t_cash_id
  364. ELSE -1 END,
  365. in_transactions[out_count][2]* -1,
  366. CASE WHEN t_voucher_id IS NULL THEN true
  367. ELSE false END,
  368. t_voucher_id, in_payment_date, in_source);
  369. UPDATE ap
  370. set paid = paid +in_transactions[out_count][2]
  371. where id =in_transactions[out_count][1];
  372. END LOOP;
  373. return out_count;
  374. END;
  375. $$ language plpgsql;
  376. COMMENT ON FUNCTION payment_bulk_post
  377. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  378. in_ar_ap_accno text, in_cash_accno text,
  379. in_payment_date date, in_account_class int)
  380. IS
  381. $$ Note that in_transactions is a two-dimensional numeric array. Of each
  382. sub-array, the first element is the (integer) transaction id, and the second
  383. is the amount for that transaction. If the total of the amounts do not add up
  384. to in_total, then an error is generated. $$;
  385. CREATE OR REPLACE FUNCTION payment_post
  386. (in_payment_date date,
  387. in_account_class int,
  388. in_person_id int,
  389. in_currency char(3),
  390. in_notes text,
  391. in_department int,
  392. in_gl_description text,
  393. in_cash_accno int[],
  394. in_cash_amount int[],
  395. in_cash_approved bool[],
  396. in_cash_source text[],
  397. in_accno int[],
  398. in_amount int[],
  399. in_approved bool[],
  400. in_source text[],
  401. in_transaction_id int[],
  402. in_type int[],
  403. in_approved bool)
  404. RETURNS INT AS
  405. $$
  406. DECLARE var_payment_id int;
  407. DECLARE var_gl_id int;
  408. DECLARE var_entry_id int[];
  409. DECLARE out_count int;
  410. BEGIN
  411. -- FIRST WE HAVE TO INSERT THE PAYMENT
  412. -- THE ID IS GENERATED BY payment_id_seq
  413. --
  414. INSERT INTO payment (reference, payment_class, payment_date,
  415. person_id, currency, notes, department_id)
  416. VALUES ((CASE WHEN in_account_class = 1 THEN
  417. setting_increment('rcptnumber') -- I FOUND THIS ON sql/modules/Settings.sql
  418. ELSE -- and it is very usefull
  419. setting_increment('paynumber')
  420. END),
  421. in_account_class, in_payment_date, in_person_id,
  422. in_currency, in_notes, in_department);
  423. SELECT currval('payment_id_seq') INTO var_payment_id; -- WE'LL NEED THIS VALUE TO USE payment_link table
  424. -- SECOND WE HAVE TO MAKE THE GL TO HOLD THE TRANSACTIONS
  425. -- THE ID IS GENERATED BY gl_id_seq
  426. --
  427. INSERT INTO gl (reference, description, transdate,
  428. person_id, notes, approved, department_id)
  429. VALUES (setting_increment('glnumber'),
  430. in_gl_description, in_payment_date, in_person_id,
  431. in_notes, in_department, coalesce(in_approved, true));
  432. SELECT currval('id') INTO var_gl_id; -- WE'LL NEED THIS VALUE TO JOIN WITH PAYMENT
  433. -- NOW COMES THE HEAVY PART, STORING ALL THE POSSIBLE TRANSACTIONS...
  434. --
  435. -- FIRST WE SHOULD INSERT THE CASH ACCOUNTS
  436. --
  437. -- WE SHOULD HAVE THE DATA STORED AS (ACCNO, AMOUNT), SO
  438. FOR out_count IN
  439. array_lower(in_cash_accno, 1) ..
  440. array_upper(in_cash_accno, 1)
  441. LOOP
  442. INSERT INTO acc_trans (chart_id, amount,
  443. trans_id, transdate, approved, source)
  444. VALUES ((SELECT id FROM chart WHERE accno = in_cash_accno[out_count]),
  445. CASE WHEN in_account_class = 2 THEN in_cash_amount[out_count] * -1
  446. ELSE in_cash_amount[out_count]
  447. END,
  448. var_gl_id, in_payment_date, coalesce(in_cash_approved[1], true),
  449. in_cash_source[out_count]);
  450. --SELECT currval('acc_trans_entry_id_seq') INTO var_entry_id[out_count];--WE'LL NEED THIS FOR THE PAYMENT_LINK
  451. END LOOP;
  452. --
  453. -- NOW LETS HANDLE THE AR/AP/OVERPAYMENT ACCOUNT
  454. --
  455. FOR var_count IN
  456. array_lower(in_accno, 1) ..
  457. array_upper(in_accno, 1)
  458. LOOP
  459. INSERT INTO acc_trans (chart_id, amount,
  460. trans_id, transdate, approved, source)
  461. VALUES ((SELECT id FROM chart WHERE accno = in_accno[out_count]),
  462. CASE WHEN in_account_class = 2 THEN in_amount[out_count] * -1
  463. ELSE in_amount[out_count]
  464. END,
  465. var_gl_id, in_payment_date, coalesce(in_approved[1], true),
  466. in_source[out_count]);
  467. --
  468. -- WE WILL INSERT THE LINK INTO PAYMENT_LINKS NOW
  469. --
  470. INSERT INTO payment_links
  471. VALUES (var_payment_id, currval(acc_trans_entry_id_seq),
  472. in_transaction_id[out_count], in_type[var_count]);
  473. END LOOP;
  474. return 0;
  475. END;
  476. $$ LANGUAGE PLPGSQL;
  477. -- I HAVE TO MAKE A COMMENT ON THIS FUNCTION
  478. -- Move this to the projects module when we start on that. CT
  479. CREATE OR REPLACE FUNCTION project_list_open(in_date date)
  480. RETURNS SETOF project AS
  481. $$
  482. DECLARE out_project project%ROWTYPE;
  483. BEGIN
  484. FOR out_project IN
  485. SELECT * from project
  486. WHERE startdate <= in_date AND enddate >= in_date
  487. AND completed = 0
  488. LOOP
  489. return next out_project;
  490. END LOOP;
  491. END;
  492. $$ language plpgsql;
  493. comment on function project_list_open(in_date date) is
  494. $$ This function returns all projects that were open as on the date provided as
  495. the argument.$$;
  496. -- Move this to the projects module when we start on that. CT
  497. CREATE OR REPLACE FUNCTION department_list(in_role char)
  498. RETURNS SETOF department AS
  499. $$
  500. DECLARE out_department department%ROWTYPE;
  501. BEGIN
  502. FOR out_department IN
  503. SELECT * from department
  504. WHERE role = coalesce(in_role, role)
  505. LOOP
  506. return next out_department;
  507. END LOOP;
  508. END;
  509. $$ language plpgsql;
  510. -- Move this into another module.
  511. comment on function department_list(in_role char) is
  512. $$ This function returns all department that match the role provided as
  513. the argument.$$;
  514. CREATE OR REPLACE FUNCTION payments_get_open_currencies(in_account_class int)
  515. RETURNS SETOF char(3) AS
  516. $$
  517. DECLARE resultrow record;
  518. BEGIN
  519. FOR resultrow IN
  520. SELECT curr AS curr FROM ar
  521. WHERE amount <> paid
  522. OR paid IS NULL
  523. AND in_account_class=2
  524. UNION
  525. SELECT curr FROM ap
  526. WHERE amount <> paid
  527. OR paid IS NULL
  528. AND in_account_class=1
  529. ORDER BY curr
  530. LOOP
  531. return next resultrow.curr;
  532. END LOOP;
  533. END;
  534. $$ language plpgsql;
  535. CREATE OR REPLACE FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int)
  536. RETURNS NUMERIC AS
  537. $$
  538. DECLARE
  539. out_exrate exchangerate.buy%TYPE;
  540. BEGIN
  541. IF in_account_class = 1 THEN
  542. SELECT INTO out_exrate buy
  543. FROM exchangerate
  544. WHERE transdate = in_date AND curr = in_currency;
  545. ELSE
  546. SELECT INTO out_exrate sell
  547. FROM exchangerate
  548. WHERE transdate = in_date AND curr = in_currency;
  549. END IF;
  550. RETURN out_exrate;
  551. END;
  552. $$ language plpgsql;
  553. COMMENT ON FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int) IS
  554. $$ This function return the exchange rate of a given currency, date and exchange rate class (buy or sell). $$;
  555. --
  556. -- payment_location_result has the same arch as location_result, except for one field
  557. -- This should be unified on the API when we get things working - David Mora
  558. --
  559. CREATE TYPE payment_location_result AS (
  560. id int,
  561. line_one text,
  562. line_two text,
  563. line_three text,
  564. city text,
  565. state text,
  566. mail_code text
  567. country text,
  568. class text
  569. );
  570. --
  571. -- payment_get_vc_info has the same arch as company__list_locations, except for the filtering capabilities
  572. -- This should be unified on the API when we get things working - David Mora
  573. --
  574. CREATE OR REPLACE FUNCTION payment_get_vc_info(in_entity_id int, in_location_class_id int)
  575. RETURNS SETOF payment_location_result AS
  576. $$
  577. DECLARE out_row RECORD;
  578. BEGIN
  579. FOR out_row IN
  580. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  581. l.state, l.mail_code, c.name, lc.class
  582. FROM location l
  583. JOIN company_to_location ctl ON (ctl.location_id = l.id)
  584. JOIN company cp ON (ctl.company_id = cp.id)
  585. JOIN location_class lc ON (ctl.location_class = lc.id)
  586. JOIN country c ON (c.id = l.country_id)
  587. WHERE cp.entity_id = in_entity_id AND
  588. lc.id = in_location_class_id
  589. ORDER BY lc.id, l.id, c.name
  590. LOOP
  591. RETURN NEXT out_row;
  592. END LOOP;
  593. END;
  594. $$ LANGUAGE PLPGSQL;
  595. COMMENT ON FUNCTION payment_get_vc_info(in_entity_id int, in_location_class_id int) IS
  596. $$ This function returns vendor or customer info $$;
  597. CREATE TYPE payment_record AS (
  598. amount numeric,
  599. meta_number text,
  600. credit_id int,
  601. company_paid text,
  602. accounts text[],
  603. source text,
  604. date_paid date
  605. );
  606. CREATE OR REPLACE FUNCTION payment__search
  607. (in_source text, in_date_from date, in_date_to date, in_credit_id int,
  608. in_cash_accno text, in_account_class int)
  609. RETURNS SETOF payment_record AS
  610. $$
  611. DECLARE
  612. out_row payment_record;
  613. BEGIN
  614. FOR out_row IN
  615. select sum(CASE WHEN c.entity_class = 1 then a.amount
  616. ELSE a.amount * -1 END), c.meta_number,
  617. c.id, co.legal_name,
  618. compound_array(ARRAY[ARRAY[ch.id::text, ch.accno,
  619. ch.description]]), a.source, a.transdate
  620. FROM entity_credit_account c
  621. JOIN ( select entity_credit_account, id
  622. FROM ar WHERE in_account_class = 2
  623. UNION
  624. SELECT entity_credit_account, id
  625. FROM ap WHERE in_account_class = 1
  626. ) arap ON (arap.entity_credit_account = c.id)
  627. JOIN acc_trans a ON (arap.id = a.trans_id)
  628. JOIN chart ch ON (ch.id = a.chart_id)
  629. JOIN company co ON (c.entity_id = co.entity_id)
  630. WHERE (ch.accno = in_cash_accno)
  631. AND (c.id = in_credit_id OR in_credit_id IS NULL)
  632. AND (a.transdate >= in_date_from
  633. OR in_date_from IS NULL)
  634. AND (a.transdate <= in_date_to OR in_date_to IS NULL)
  635. AND (source = in_source OR in_source IS NULL)
  636. GROUP BY c.meta_number, c.id, co.legal_name, a.transdate,
  637. a.source
  638. ORDER BY a.transdate, c.meta_number, a.source
  639. LOOP
  640. RETURN NEXT out_row;
  641. END LOOP;
  642. END;
  643. $$ language plpgsql;
  644. CREATE OR REPLACE FUNCTION payment__reverse
  645. (in_source text, in_date_paid date, in_credit_id int, in_cash_accno text,
  646. in_date_reversed date, in_account_class int, in_batch_id int)
  647. RETURNS INT
  648. AS $$
  649. DECLARE
  650. pay_row record;
  651. t_voucher_id int;
  652. t_voucher_inserted bool;
  653. BEGIN
  654. IF in_batch_id IS NOT NULL THEN
  655. t_voucher_id := nextval('voucher_id_seq');
  656. t_voucher_inserted := FALSE;
  657. END IF;
  658. FOR pay_row IN
  659. SELECT a.*, c.ar_ap_account_id
  660. FROM acc_trans a
  661. JOIN (select id, entity_credit_account
  662. FROM ar WHERE in_account_class = 2
  663. UNION
  664. SELECT id, entity_credit_account
  665. FROM ap WHERE in_account_class = 1
  666. ) arap ON (a.trans_id = arap.id)
  667. JOIN entity_credit_account c
  668. ON (arap.entity_credit_account = c.id)
  669. JOIN chart ch ON (a.chart_id = ch.id)
  670. WHERE coalesce(source, '') = coalesce(in_source, '')
  671. AND transdate = in_date_paid
  672. AND in_credit_id = c.id
  673. AND in_cash_accno = ch.accno
  674. LOOP
  675. IF in_batch_id IS NOT NULL
  676. AND t_voucher_inserted IS NOT TRUE
  677. THEN
  678. INSERT INTO voucher
  679. (id, trans_id, batch_id, batch_class)
  680. VALUES
  681. (t_voucher_id, pay_row.trans_id, in_batch_id,
  682. CASE WHEN in_account_class = 1 THEN 4
  683. WHEN in_account_class = 2 THEN 7
  684. END);
  685. t_voucher_inserted := TRUE;
  686. END IF;
  687. INSERT INTO acc_trans
  688. (trans_id, chart_id, amount, transdate, source, memo, approved,
  689. voucher_id)
  690. VALUES
  691. (pay_row.trans_id, pay_row.chart_id, pay_row.amount * -1,
  692. in_date_reversed, in_source, 'Reversing ' ||
  693. COALESCE(in_source, ''),
  694. case when in_batch_id is not null then false
  695. else true end, t_voucher_id);
  696. INSERT INTO acc_trans
  697. (trans_id, chart_id, amount, transdate, source, memo, approved,
  698. voucher_id)
  699. VALUES
  700. (pay_row.trans_id, pay_row.ar_ap_account_id, pay_row.amount,
  701. in_date_reversed, in_source, 'Reversing ' ||
  702. COALESCE(in_source, ''),
  703. case when in_batch_id is not null then false
  704. else true end, t_voucher_id);
  705. IF in_account_class = 1 THEN
  706. UPDATE ap SET paid = amount -
  707. (SELECT sum(a.amount)
  708. FROM acc_trans a
  709. JOIN chart c ON (a.chart_id = c.id)
  710. WHERE c.link = 'AP'
  711. AND trans_id = pay_row.trans_id
  712. )
  713. WHERE id = pay_row.trans_id;
  714. ELSIF in_account_class = 2 THEN
  715. update ar SET paid = amount -
  716. (SELECT sum(a.amount)
  717. FROM acc_trans a
  718. JOIN chart c ON (a.chart_id = c.id)
  719. WHERE c.link = 'AR'
  720. AND trans_id = pay_row.trans_id
  721. ) * -1
  722. WHERE id = pay_row.trans_id;
  723. ELSE
  724. RAISE EXCEPTION 'Unknown account class for payments %',
  725. in_account_class;
  726. END IF;
  727. END LOOP;
  728. RETURN 1;
  729. END;
  730. $$ LANGUAGE PLPGSQL;