summaryrefslogtreecommitdiff
path: root/sql/modules/Payment.sql
blob: b3b9d1c297597a9bfcdff63f94a749401a832389 (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. (in_transactions[out_count][1],
  358. case when in_account_class = 1 THEN t_ar_ap_id
  359. WHEN in_account_class = 2 THEN t_cash_id
  360. ELSE -1 END,
  361. in_transactions[out_count][2]* -1,
  362. CASE WHEN t_voucher_id IS NULL THEN true
  363. ELSE false END,
  364. t_voucher_id, in_payment_date, in_source);
  365. UPDATE ap
  366. set paid = paid +in_transactions[out_count][2]
  367. where id =in_transactions[out_count][1];
  368. END LOOP;
  369. return out_count;
  370. END;
  371. $$ language plpgsql;
  372. COMMENT ON FUNCTION payment_bulk_post
  373. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  374. in_ar_ap_accno text, in_cash_accno text,
  375. in_payment_date date, in_account_class int)
  376. IS
  377. $$ Note that in_transactions is a two-dimensional numeric array. Of each
  378. sub-array, the first element is the (integer) transaction id, and the second
  379. is the amount for that transaction. If the total of the amounts do not add up
  380. to in_total, then an error is generated. $$;
  381. CREATE OR REPLACE FUNCTION payment_post
  382. (in_payment_date date,
  383. in_account_class int,
  384. in_person_id int,
  385. in_currency char(3),
  386. in_notes text,
  387. in_department int,
  388. in_gl_description text,
  389. in_cash_accno int[],
  390. in_cash_amount int[],
  391. in_cash_approved bool[],
  392. in_cash_source text[],
  393. in_accno int[],
  394. in_amount int[],
  395. in_approved bool[],
  396. in_source text[],
  397. in_transaction_id int[],
  398. in_type int[],
  399. in_approved bool)
  400. RETURNS INT AS
  401. $$
  402. DECLARE var_payment_id int;
  403. DECLARE var_gl_id int;
  404. DECLARE var_entry_id int[];
  405. DECLARE out_count int;
  406. BEGIN
  407. -- FIRST WE HAVE TO INSERT THE PAYMENT
  408. -- THE ID IS GENERATED BY payment_id_seq
  409. --
  410. INSERT INTO payment (reference, payment_class, payment_date,
  411. person_id, currency, notes, department_id)
  412. VALUES ((CASE WHEN in_account_class = 1 THEN
  413. setting_increment('rcptnumber') -- I FOUND THIS ON sql/modules/Settings.sql
  414. ELSE -- and it is very usefull
  415. setting_increment('paynumber')
  416. END),
  417. in_account_class, in_payment_date, in_person_id,
  418. in_currency, in_notes, in_department);
  419. SELECT currval('payment_id_seq') INTO var_payment_id; -- WE'LL NEED THIS VALUE TO USE payment_link table
  420. -- SECOND WE HAVE TO MAKE THE GL TO HOLD THE TRANSACTIONS
  421. -- THE ID IS GENERATED BY gl_id_seq
  422. --
  423. INSERT INTO gl (reference, description, transdate,
  424. person_id, notes, approved, department_id)
  425. VALUES (setting_increment('glnumber'),
  426. in_gl_description, in_payment_date, in_person_id,
  427. in_notes, in_department, coalesce(in_approved, true));
  428. SELECT currval('id') INTO var_gl_id; -- WE'LL NEED THIS VALUE TO JOIN WITH PAYMENT
  429. -- NOW COMES THE HEAVY PART, STORING ALL THE POSSIBLE TRANSACTIONS...
  430. --
  431. -- FIRST WE SHOULD INSERT THE CASH ACCOUNTS
  432. --
  433. -- WE SHOULD HAVE THE DATA STORED AS (ACCNO, AMOUNT), SO
  434. FOR out_count IN
  435. array_lower(in_cash_accno, 1) ..
  436. array_upper(in_cash_accno, 1)
  437. LOOP
  438. INSERT INTO acc_trans (chart_id, amount,
  439. trans_id, transdate, approved, source)
  440. VALUES ((SELECT id FROM chart WHERE accno = in_cash_accno[out_count]),
  441. CASE WHEN in_account_class = 2 THEN in_cash_amount[out_count] * -1
  442. ELSE in_cash_amount[out_count]
  443. END,
  444. var_gl_id, in_payment_date, coalesce(in_cash_approved[1], true),
  445. in_cash_source[out_count]);
  446. --SELECT currval('acc_trans_entry_id_seq') INTO var_entry_id[out_count];--WE'LL NEED THIS FOR THE PAYMENT_LINK
  447. END LOOP;
  448. --
  449. -- NOW LETS HANDLE THE AR/AP/OVERPAYMENT ACCOUNT
  450. --
  451. FOR var_count IN
  452. array_lower(in_accno, 1) ..
  453. array_upper(in_accno, 1)
  454. LOOP
  455. INSERT INTO acc_trans (chart_id, amount,
  456. trans_id, transdate, approved, source)
  457. VALUES ((SELECT id FROM chart WHERE accno = in_accno[out_count]),
  458. CASE WHEN in_account_class = 2 THEN in_amount[out_count] * -1
  459. ELSE in_amount[out_count]
  460. END,
  461. var_gl_id, in_payment_date, coalesce(in_approved[1], true),
  462. in_source[out_count]);
  463. --
  464. -- WE WILL INSERT THE LINK INTO PAYMENT_LINKS NOW
  465. --
  466. INSERT INTO payment_links
  467. VALUES (var_payment_id, currval(acc_trans_entry_id_seq),
  468. in_transaction_id[out_count], in_type[var_count]);
  469. END LOOP;
  470. return 0;
  471. END;
  472. $$ LANGUAGE PLPGSQL;
  473. -- I HAVE TO MAKE A COMMENT ON THIS FUNCTION
  474. -- Move this to the projects module when we start on that. CT
  475. CREATE OR REPLACE FUNCTION project_list_open(in_date date)
  476. RETURNS SETOF project AS
  477. $$
  478. DECLARE out_project project%ROWTYPE;
  479. BEGIN
  480. FOR out_project IN
  481. SELECT * from project
  482. WHERE startdate <= in_date AND enddate >= in_date
  483. AND completed = 0
  484. LOOP
  485. return next out_project;
  486. END LOOP;
  487. END;
  488. $$ language plpgsql;
  489. comment on function project_list_open(in_date date) is
  490. $$ This function returns all projects that were open as on the date provided as
  491. the argument.$$;
  492. -- Move this to the projects module when we start on that. CT
  493. CREATE OR REPLACE FUNCTION department_list(in_role char)
  494. RETURNS SETOF department AS
  495. $$
  496. DECLARE out_department department%ROWTYPE;
  497. BEGIN
  498. FOR out_department IN
  499. SELECT * from department
  500. WHERE role = coalesce(in_role, role)
  501. LOOP
  502. return next out_department;
  503. END LOOP;
  504. END;
  505. $$ language plpgsql;
  506. -- Move this into another module.
  507. comment on function department_list(in_role char) is
  508. $$ This function returns all department that match the role provided as
  509. the argument.$$;
  510. CREATE OR REPLACE FUNCTION payments_get_open_currencies(in_account_class int)
  511. RETURNS SETOF char(3) AS
  512. $$
  513. DECLARE resultrow record;
  514. BEGIN
  515. FOR resultrow IN
  516. SELECT curr AS curr FROM ar
  517. WHERE amount <> paid
  518. OR paid IS NULL
  519. AND in_account_class=2
  520. UNION
  521. SELECT curr FROM ap
  522. WHERE amount <> paid
  523. OR paid IS NULL
  524. AND in_account_class=1
  525. ORDER BY curr
  526. LOOP
  527. return next resultrow.curr;
  528. END LOOP;
  529. END;
  530. $$ language plpgsql;
  531. CREATE OR REPLACE FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int)
  532. RETURNS NUMERIC AS
  533. $$
  534. DECLARE
  535. out_exrate exchangerate.buy%TYPE;
  536. BEGIN
  537. IF in_account_class = 1 THEN
  538. SELECT INTO out_exrate buy
  539. FROM exchangerate
  540. WHERE transdate = in_date AND curr = in_currency;
  541. ELSE
  542. SELECT INTO out_exrate sell
  543. FROM exchangerate
  544. WHERE transdate = in_date AND curr = in_currency;
  545. END IF;
  546. RETURN out_exrate;
  547. END;
  548. $$ language plpgsql;
  549. COMMENT ON FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int) IS
  550. $$ This function return the exchange rate of a given currency, date and exchange rate class (buy or sell). $$;
  551. --
  552. -- payment_location_result has the same arch as location_result, except for one field
  553. -- This should be unified on the API when we get things working - David Mora
  554. --
  555. CREATE TYPE payment_location_result AS (
  556. id int,
  557. line_one text,
  558. line_two text,
  559. line_three text,
  560. city text,
  561. state text,
  562. mail_code text
  563. country text,
  564. class text
  565. );
  566. --
  567. -- payment_get_vc_info has the same arch as company__list_locations, except for the filtering capabilities
  568. -- This should be unified on the API when we get things working - David Mora
  569. --
  570. CREATE OR REPLACE FUNCTION payment_get_vc_info(in_entity_id int, in_location_class_id int)
  571. RETURNS SETOF payment_location_result AS
  572. $$
  573. DECLARE out_row RECORD;
  574. BEGIN
  575. FOR out_row IN
  576. SELECT l.id, l.line_one, l.line_two, l.line_three, l.city,
  577. l.state, l.mail_code, c.name, lc.class
  578. FROM location l
  579. JOIN company_to_location ctl ON (ctl.location_id = l.id)
  580. JOIN company cp ON (ctl.company_id = cp.id)
  581. JOIN location_class lc ON (ctl.location_class = lc.id)
  582. JOIN country c ON (c.id = l.country_id)
  583. WHERE cp.entity_id = in_entity_id AND
  584. lc.id = in_location_class_id
  585. ORDER BY lc.id, l.id, c.name
  586. LOOP
  587. RETURN NEXT out_row;
  588. END LOOP;
  589. END;
  590. $$ LANGUAGE PLPGSQL;
  591. COMMENT ON FUNCTION payment_get_vc_info(in_entity_id int, in_location_class_id int) IS
  592. $$ This function returns vendor or customer info $$;
  593. CREATE TYPE payment_record AS (
  594. amount numeric,
  595. meta_number text,
  596. credit_id int,
  597. company_paid text,
  598. accounts text[],
  599. source text,
  600. date_paid date
  601. );
  602. CREATE OR REPLACE FUNCTION payment__search
  603. (in_source text, in_date_from date, in_date_to date, in_credit_id int,
  604. in_cash_accno text, in_account_class int)
  605. RETURNS SETOF payment_record AS
  606. $$
  607. DECLARE
  608. out_row payment_record;
  609. BEGIN
  610. FOR out_row IN
  611. select sum(CASE WHEN c.entity_class = 1 then a.amount
  612. ELSE a.amount * -1 END), c.meta_number,
  613. c.id, co.legal_name,
  614. compound_array(ARRAY[ARRAY[ch.id::text, ch.accno,
  615. ch.description]]), a.source, a.transdate
  616. FROM entity_credit_account c
  617. JOIN ( select entity_credit_account, id
  618. FROM ar WHERE in_account_class = 2
  619. UNION
  620. SELECT entity_credit_account, id
  621. FROM ap WHERE in_account_class = 1
  622. ) arap ON (arap.entity_credit_account = c.id)
  623. JOIN acc_trans a ON (arap.id = a.trans_id)
  624. JOIN chart ch ON (ch.id = a.chart_id)
  625. JOIN company co ON (c.entity_id = co.entity_id)
  626. WHERE (ch.accno = in_cash_accno)
  627. AND (c.id = in_credit_id OR in_credit_id IS NULL)
  628. AND (a.transdate >= in_date_from
  629. OR in_date_from IS NULL)
  630. AND (a.transdate <= in_date_to OR in_date_to IS NULL)
  631. AND (source = in_source OR in_source IS NULL)
  632. GROUP BY c.meta_number, c.id, co.legal_name, a.transdate,
  633. a.source
  634. ORDER BY a.transdate, c.meta_number, a.source
  635. LOOP
  636. RETURN NEXT out_row;
  637. END LOOP;
  638. END;
  639. $$ language plpgsql;
  640. CREATE OR REPLACE FUNCTION payment__reverse
  641. (in_source text, in_date_paid date, in_credit_id int, in_cash_accno text,
  642. in_date_reversed date, in_account_class int, in_batch_id int)
  643. RETURNS INT
  644. AS $$
  645. DECLARE
  646. pay_row record;
  647. t_voucher_id int;
  648. t_voucher_inserted bool;
  649. BEGIN
  650. IF in_batch_id IS NOT NULL THEN
  651. t_voucher_id := nextval('voucher_id_seq');
  652. t_voucher_inserted := FALSE;
  653. END IF;
  654. FOR pay_row IN
  655. SELECT a.*, c.ar_ap_account_id
  656. FROM acc_trans a
  657. JOIN (select id, entity_credit_account
  658. FROM ar WHERE in_account_class = 2
  659. UNION
  660. SELECT id, entity_credit_account
  661. FROM ap WHERE in_account_class = 1
  662. ) arap ON (a.trans_id = arap.id)
  663. JOIN entity_credit_account c
  664. ON (arap.entity_credit_account = c.id)
  665. JOIN chart ch ON (a.chart_id = ch.id)
  666. WHERE coalesce(source, '') = coalesce(in_source, '')
  667. AND transdate = in_date_paid
  668. AND in_credit_id = c.id
  669. AND in_cash_accno = ch.accno
  670. LOOP
  671. IF in_batch_id IS NOT NULL
  672. AND t_voucher_inserted IS NOT TRUE
  673. THEN
  674. INSERT INTO voucher
  675. (id, trans_id, batch_id, batch_class)
  676. VALUES
  677. (t_voucher_id, pay_row.trans_id, in_batch_id,
  678. CASE WHEN in_account_class = 1 THEN 4
  679. WHEN in_account_class = 2 THEN 7
  680. END);
  681. t_voucher_inserted := TRUE;
  682. END IF;
  683. INSERT INTO acc_trans
  684. (trans_id, chart_id, amount, transdate, source, memo, approved,
  685. voucher_id)
  686. VALUES
  687. (pay_row.trans_id, pay_row.chart_id, pay_row.amount * -1,
  688. in_date_reversed, in_source, 'Reversing ' ||
  689. COALESCE(in_source, ''),
  690. case when in_batch_id is not null then false
  691. else true end, t_voucher_id),
  692. (pay_row.trans_id, pay_row.ar_ap_account_id, pay_row.amount,
  693. in_date_reversed, in_source, 'Reversing ' ||
  694. COALESCE(in_source, ''),
  695. case when in_batch_id is not null then false
  696. else true end, t_voucher_id);
  697. IF in_account_class = 1 THEN
  698. UPDATE ap SET paid = amount -
  699. (SELECT sum(a.amount)
  700. FROM acc_trans a
  701. JOIN chart c ON (a.chart_id = c.id)
  702. WHERE c.link = 'AP'
  703. AND trans_id = pay_row.trans_id
  704. )
  705. WHERE id = pay_row.trans_id;
  706. ELSIF in_account_class = 2 THEN
  707. update ar SET paid = amount -
  708. (SELECT sum(a.amount)
  709. FROM acc_trans a
  710. JOIN chart c ON (a.chart_id = c.id)
  711. WHERE c.link = 'AR'
  712. AND trans_id = pay_row.trans_id
  713. ) * -1
  714. WHERE id = pay_row.trans_id;
  715. ELSE
  716. RAISE EXCEPTION 'Unknown account class for payments %',
  717. in_account_class;
  718. END IF;
  719. END LOOP;
  720. RETURN 1;
  721. END;
  722. $$ LANGUAGE PLPGSQL;