summaryrefslogtreecommitdiff
path: root/sql/modules/Payment.sql
blob: d302642093d6533e8bcb04b1c7245724b14e9b24 (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, e.name, e.entity_class, e.created
  10. FROM entity e
  11. JOIN entity_credit_account ec ON (ec.entity_id = e.id)
  12. WHERE ec.entity_class = in_account_class
  13. AND CASE WHEN in_account_class = 1 THEN
  14. id IN (SELECT entity_id FROM ap
  15. WHERE amount <> paid
  16. GROUP BY entity_id)
  17. WHEN in_account_class = 2 THEN
  18. id IN (SELECT entity_id FROM ar
  19. WHERE amount <> paid
  20. GROUP BY entity_id)
  21. END
  22. LOOP
  23. RETURN NEXT out_entity;
  24. END LOOP;
  25. END;
  26. $$ LANGUAGE PLPGSQL;
  27. COMMENT ON FUNCTION payment_get_open_accounts(int) IS
  28. $$ This function takes a single argument (1 for vendor, 2 for customer as
  29. always) and returns all entities with open accounts of the appropriate type. $$;
  30. CREATE OR REPLACE FUNCTION payment_get_all_accounts(in_account_class int)
  31. RETURNS SETOF entity AS
  32. $$
  33. DECLARE out_entity entity%ROWTYPE;
  34. BEGIN
  35. FOR out_entity IN
  36. SELECT ec.id,
  37. e.name, e.entity_class, e.created
  38. FROM entity e
  39. JOIN entity_credit_account ec ON (ec.entity_id = e.id)
  40. WHERE e.entity_class = in_account_class
  41. LOOP
  42. RETURN NEXT out_entity;
  43. END LOOP;
  44. END;
  45. $$ LANGUAGE PLPGSQL;
  46. COMMENT ON FUNCTION payment_get_open_accounts(int) IS
  47. $$ This function takes a single argument (1 for vendor, 2 for customer as
  48. always) and returns all entities with accounts of the appropriate type. $$;
  49. CREATE TYPE payment_invoice AS (
  50. invoice_id int,
  51. invnumber text,
  52. invoice_date date,
  53. amount numeric,
  54. discount numeric,
  55. due numeric
  56. );
  57. CREATE OR REPLACE FUNCTION payment_get_open_invoices
  58. (in_account_class int, in_entity_credit_id int, in_curr char(3))
  59. RETURNS SETOF payment_invoice AS
  60. $$
  61. DECLARE payment_inv payment_invoice;
  62. BEGIN
  63. FOR payment_inv IN
  64. SELECT a.id AS invoice_id, a.invnumber,
  65. a.transdate AS invoice_date, a.amount,
  66. CASE WHEN discount_terms
  67. > extract('days' FROM age(a.transdate))
  68. THEN 0
  69. ELSE (a.amount - a.paid) * c.discount / 100
  70. END AS discount,
  71. a.amount - a.paid -
  72. CASE WHEN discount_terms
  73. > extract('days' FROM age(a.transdate))
  74. THEN 0
  75. ELSE (a.amount - a.paid) * c.discount / 100
  76. END AS due
  77. FROM (SELECT id, invnumber, transdate, amount, entity_id,
  78. 1 as invoice_class, paid, curr
  79. FROM ap
  80. UNION
  81. SELECT id, invnumber, transdate, amount, entity_id,
  82. 2 AS invoice_class, paid, curr
  83. FROM ar
  84. ) a
  85. JOIN entity_credit_account c USING (entity_id)
  86. WHERE a.invoice_class = in_account_class
  87. AND c.entity_class = in_account_class
  88. AND a.amount - a.paid <> 0
  89. AND a.curr = in_curr
  90. AND a.credit_account = coalesce(in_entity_credit_id,
  91. a.credit_account)
  92. LOOP
  93. RETURN NEXT payment_inv;
  94. END LOOP;
  95. END;
  96. $$ LANGUAGE PLPGSQL;
  97. COMMENT ON FUNCTION payment_get_open_invoices(int, int, char(3)) IS
  98. $$ This function takes three arguments:
  99. Type: 1 for vendor, 2 for customer
  100. Entity_id: The entity_id of the customer or vendor
  101. Currency: 3 characters for currency ('USD' for example).
  102. Returns all open invoices for the entity in question. $$;
  103. CREATE TYPE payment_contact_invoice AS (
  104. contact_id int,
  105. contact_name text,
  106. account_number text,
  107. total_due numeric,
  108. invoices text[]
  109. );
  110. CREATE OR REPLACE FUNCTION payment_get_all_contact_invoices
  111. (in_account_class int, in_business_type int, in_currency char(3),
  112. in_date_from date, in_date_to date, in_batch_id int,
  113. in_ar_ap_accno text)
  114. RETURNS SETOF payment_contact_invoice AS
  115. $$
  116. DECLARE payment_item payment_contact_invoice;
  117. BEGIN
  118. FOR payment_item IN
  119. SELECT c.id AS contact_id, e.name AS contact_name,
  120. c.meta_number AS account_number,
  121. sum(a.amount - a.paid) AS total_due,
  122. compound_array(ARRAY[[
  123. a.id::text, a.invnumber, a.transdate::text,
  124. a.amount::text, a.paid::text,
  125. (CASE WHEN c.discount_terms
  126. > extract('days' FROM age(a.transdate))
  127. THEN 0
  128. ELSE (a.amount - coalesce(a.paid, 0)) * coalesce(c.discount, 0) / 100
  129. END)::text,
  130. (a.amount - coalesce(a.paid, 0) -
  131. (CASE WHEN c.discount_terms
  132. > extract('days' FROM age(a.transdate))
  133. THEN 0
  134. ELSE (a.amount - coalesce(a.paid, 0)) * coalesce(c.discount, 0) / 100
  135. END))::text]]),
  136. bool_and(lock_record(a.id, (select max(session_id) FROM "session" where users_id = (
  137. select id from users WHERE username =
  138. SESSION_USER))))
  139. FROM entity e
  140. JOIN entity_credit_account c ON (e.id = c.entity_id)
  141. JOIN (SELECT id, invnumber, transdate, amount, entity_id,
  142. paid, curr, 1 as invoice_class,
  143. entity_credit_account
  144. FROM ap
  145. UNION
  146. SELECT id, invnumber, transdate, amount, entity_id,
  147. paid, curr, 2 as invoice_class,
  148. entity_credit_account
  149. FROM ar
  150. ORDER BY transdate
  151. ) a USING (entity_id)
  152. JOIN transactions t ON (a.id = t.id)
  153. WHERE a.invoice_class = in_account_class
  154. AND c.business_id =
  155. coalesce(in_business_type, c.business_id)
  156. AND ((a.transdate >= COALESCE(in_date_from, a.transdate)
  157. AND a.transdate <= COALESCE(in_date_to, a.transdate))
  158. OR a.id IN (select voucher.trans_id FROM voucher
  159. WHERE batch_id = in_batch_id))
  160. AND c.entity_class = in_account_class
  161. AND a.curr = in_currency
  162. AND a.entity_credit_account = c.id
  163. AND a.amount - a.paid <> 0
  164. AND NOT (t.locked_by IS NOT NULL AND t.locked_by IN
  165. (select "session_id" FROM "session"
  166. WHERE users_id IN
  167. (select id from users
  168. where username <> SESSION_USER)))
  169. AND EXISTS (select trans_id FROM acc_trans
  170. WHERE trans_id = a.id AND
  171. chart_id = (SELECT id frOM chart
  172. WHERE accno
  173. = in_ar_ap_accno)
  174. )
  175. GROUP BY c.id, e.name, c.meta_number, c.threshold
  176. HAVING sum(a.amount - a.paid) > c.threshold
  177. LOOP
  178. RETURN NEXT payment_item;
  179. END LOOP;
  180. END;
  181. $$ LANGUAGE plpgsql;
  182. COMMENT ON FUNCTION payment_get_all_contact_invoices
  183. (in_account_class int, in_business_type int, in_currency char(3),
  184. in_date_from date, in_date_to date, in_batch_id int,
  185. in_ar_ap_accno text) IS
  186. $$
  187. This function takes the following arguments (all prefaced with in_ in the db):
  188. account_class: 1 for vendor, 2 for customer
  189. business_type: integer of business.id.
  190. currency: char(3) of currency (for example 'USD')
  191. date_from, date_to: These dates are inclusive.
  192. batch_id: For payment batches, where fees are concerned.
  193. ar_ap_accno: The AR/AP account number.
  194. This then returns a set of contact information with a 2 dimensional array
  195. cnsisting of outstanding invoices.
  196. $$;
  197. CREATE OR REPLACE FUNCTION payment_bulk_post
  198. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  199. in_ar_ap_accno text, in_cash_accno text,
  200. in_payment_date date, in_account_class int)
  201. RETURNS int AS
  202. $$
  203. DECLARE
  204. payment_trans numeric[];
  205. out_count int;
  206. t_voucher_id int;
  207. t_trans_id int;
  208. t_amount numeric;
  209. BEGIN
  210. IF in_batch_id IS NULL THEN
  211. t_voucher_id := NULL;
  212. ELSE
  213. INSERT INTO voucher (batch_id, trans_id)
  214. values (in_batch_id, in_transactions[1][1]);
  215. t_voucher_id := currval('voucher_id_seq');
  216. END IF;
  217. FOR out_count IN
  218. array_lower(in_transactions, 1) ..
  219. array_upper(in_transactions, 1)
  220. LOOP
  221. INSERT INTO acc_trans
  222. (trans_id, chart_id, amount, approved, voucher_id,
  223. transdate)
  224. VALUES
  225. (in_transactions[out_count][1],
  226. case when in_account_class = 1 THEN
  227. (SELECT id FROM chart
  228. WHERE accno = in_cash_accno)
  229. WHEN in_account_class = 2 THEN
  230. (SELECT id FROM chart
  231. WHERE accno = in_ar_ap_accno)
  232. ELSE -1 END,
  233. in_transactions[out_count][2],
  234. CASE WHEN t_voucher_id IS NULL THEN true
  235. ELSE false END,
  236. t_voucher_id, in_payment_date),
  237. (in_transactions[out_count][1],
  238. case when in_account_class = 1 THEN
  239. (SELECT id FROM chart
  240. WHERE accno = in_ar_ap_accno)
  241. WHEN in_account_class = 2 THEN
  242. (SELECT id FROM chart
  243. WHERE accno = in_cash_accno)
  244. ELSE -1 END,
  245. in_transactions[out_count][2]* -1,
  246. CASE WHEN t_voucher_id IS NULL THEN true
  247. ELSE false END,
  248. t_voucher_id, in_payment_date);
  249. insert into test_pay(id, amount) values (in_transactions[out_count][1],in_transactions[out_count][2]);
  250. UPDATE ap
  251. set paid = paid +in_transactions[out_count][2]
  252. where id =in_transactions[out_count][1];
  253. END LOOP;
  254. return out_count;
  255. END;
  256. $$ language plpgsql;
  257. COMMENT ON FUNCTION payment_bulk_post
  258. (in_transactions numeric[], in_batch_id int, in_source text, in_total numeric,
  259. in_ar_ap_accno text, in_cash_accno text,
  260. in_payment_date date, in_account_class int)
  261. IS
  262. $$ Note that in_transactions is a two-dimensional numeric array. Of each
  263. sub-array, the first element is the (integer) transaction id, and the second
  264. is the amount for that transaction. If the total of the amounts do not add up
  265. to in_total, then an error is generated. $$;
  266. CREATE OR REPLACE FUNCTION payment_post
  267. (in_trans_id int, in_batch_id int, in_source text, in_amount numeric,
  268. in_ar_ap_accno text, in_cash_accno text, in_approved bool,
  269. in_payment_date date, in_account_class int)
  270. RETURNS INT AS
  271. $$
  272. DECLARE out_entry_id int;
  273. BEGIN
  274. INSERT INTO acc_trans (chart_id, amount,
  275. trans_id, transdate, approved, source)
  276. VALUES ((SELECT id FROM chart WHERE accno = in_ar_ap_accno),
  277. CASE WHEN in_account_class = 1 THEN in_amount * -1
  278. ELSE amount
  279. END,
  280. in_trans_id, in_payment_date, in_approved, in_source);
  281. INSERT INTO acc_trans (chart_id, amount,
  282. trans_id, transdate, approved, source)
  283. VALUES ((SELECT id FROM chart WHERE accno = in_cash_accno),
  284. CASE WHEN in_account_class = 2 THEN in_amount * -1
  285. ELSE amount
  286. END,
  287. in_trans_id, in_payment_date, coalesce(in_approved, true),
  288. in_source);
  289. SELECT currval('acc_trans_entry_id_seq') INTO out_entry_id;
  290. RETURN out_entry_id;
  291. END;
  292. $$ LANGUAGE PLPGSQL;
  293. COMMENT ON FUNCTION payment_post
  294. (in_trans_id int, in_source text, in_amount numeric, in_ar_ap_accno text,
  295. in_cash_accno text, in_approved bool, in_payment_date date,
  296. in_account_class int)
  297. IS $$
  298. This function takes the following arguments (prefaced with in_ in the db):
  299. trans_id: Id for ar/ap transaction.
  300. source: text for source documnet identifier (for example, check number)
  301. amount: numeric for the amount of the transaction
  302. ar_ap_accno: AR/AP account number
  303. cash_accno: Cash Account number, i.e. the account where the payment will be
  304. held
  305. approved: False, for a voucher.
  306. This function posts the payment or saves the payment voucher.
  307. $$;
  308. -- Move this to the projects module when we start on that. CT
  309. CREATE OR REPLACE FUNCTION project_list_open(in_date date)
  310. RETURNS SETOF project AS
  311. $$
  312. DECLARE out_project project%ROWTYPE;
  313. BEGIN
  314. FOR out_project IN
  315. SELECT * from project
  316. WHERE startdate <= in_date AND enddate >= in_date
  317. AND completed = 0
  318. LOOP
  319. return next out_project;
  320. END LOOP;
  321. END;
  322. $$ language plpgsql;
  323. comment on function project_list_open(in_date date) is
  324. $$ This function returns all projects that were open as on the date provided as
  325. the argument.$$;
  326. -- Move this to the projects module when we start on that. CT
  327. CREATE OR REPLACE FUNCTION department_list(in_role char)
  328. RETURNS SETOF department AS
  329. $$
  330. DECLARE out_department department%ROWTYPE;
  331. BEGIN
  332. FOR out_department IN
  333. SELECT * from department
  334. WHERE role = coalesce(in_role, role)
  335. LOOP
  336. return next out_department;
  337. END LOOP;
  338. END;
  339. $$ language plpgsql;
  340. -- Move this into another module.
  341. comment on function department_list(in_role char) is
  342. $$ This function returns all department that match the role provided as
  343. the argument.$$;
  344. CREATE OR REPLACE FUNCTION payments_get_open_currencies(in_account_class int)
  345. RETURNS SETOF char(3) AS
  346. $$
  347. DECLARE resultrow record;
  348. BEGIN
  349. FOR resultrow IN
  350. SELECT curr AS curr FROM ar
  351. WHERE amount <> paid
  352. OR paid IS NULL
  353. AND in_account_class=2
  354. UNION
  355. SELECT curr FROM ap
  356. WHERE amount <> paid
  357. OR paid IS NULL
  358. AND in_account_class=1
  359. ORDER BY curr
  360. LOOP
  361. return next resultrow.curr;
  362. END LOOP;
  363. END;
  364. $$ language plpgsql;
  365. CREATE OR REPLACE FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int)
  366. RETURNS NUMERIC AS
  367. $$
  368. DECLARE
  369. out_exrate exchangerate.buy%TYPE;
  370. BEGIN
  371. IF in_account_class = 1 THEN
  372. SELECT INTO out_exrate buy
  373. FROM exchangerate
  374. WHERE transdate = in_date AND curr = in_currency;
  375. ELSE
  376. SELECT INTO out_exrate sell
  377. FROM exchangerate
  378. WHERE transdate = in_date AND curr = in_currency;
  379. END IF;
  380. RETURN out_exrate;
  381. END;
  382. $$ language plpgsql;
  383. COMMENT ON FUNCTION currency_get_exchangerate(in_currency char(3), in_date date, in_account_class int) IS
  384. $$ This function return the exchange rate of a given currency, date and exchange rate class (buy or sell). $$;
  385. CREATE OR REPLACE FUNCTION payment_get_vc_info(in_entity_id int)
  386. RETURNS SETOF entity AS
  387. $$
  388. DECLARE
  389. out_info entity%ROWTYPE;
  390. BEGIN
  391. FOR out_info IN
  392. SELECT e.id, e.name FROM entity e
  393. JOIN company c ON (e.id = c.entity_id)
  394. WHERE e.id = in_entity_id
  395. --SELECT e.id, c.legal_name, l.line_one, l.city_province, cy.name FROM entity e
  396. --JOIN company c ON (e.id = c.entity_id)
  397. --JOIN company_to_location cl ON (c.id = cl.company_id)
  398. --JOIN location l ON (l.id = cl.location_id)
  399. --JOIN country cy ON (cy.id = l.country_id)
  400. LOOP
  401. return next out_info;
  402. END LOOP;
  403. IF NOT FOUND THEN
  404. RAISE EXCEPTION 'ID % not found!!!!!', in_entity_id;
  405. END IF;
  406. END;
  407. $$ language plpgsql;
  408. COMMENT ON FUNCTION payment_get_vc_info(in_entity_id int) IS
  409. $$ This function return vendor or customer info, its under construction $$;