summaryrefslogtreecommitdiff
path: root/sql/modules/Reconciliaton.sql
blob: 1dfc7d7ebd04c4ee1a46c96b09104a9e3a034bfb (plain)
  1. CREATE TABLE pending_reports (
  2. id bigserial primary key not null,
  3. report_id int,
  4. scn int,
  5. their_balance INT,
  6. our_balance INT,
  7. errorcode INT,
  8. user TEXT,
  9. corrections INT NOT NULL DEFAULT 0
  10. clear_time TIMESTAMP NOT NULL,
  11. insert_time TIMESTAMPTZ NOT NULL DEFAULT now(),
  12. ledger_id int REFERENCES acc_trans(entry_id),
  13. overlook boolean not null default 'f'
  14. );
  15. CREATE TABLE report_corrections (
  16. id serial primary key not null,
  17. correction_id int not null default 1,
  18. entry references pending_reports(id) not null,
  19. user text not null,
  20. reason text not null,
  21. insert_time timestamptz not null default now()
  22. );
  23. -- to correct OUR wrong amount.
  24. CREATE OR REPLACE FUNCTION reconciliation_correct_ledger (in_report_id INT, in_id int, in_new_amount NUMERIC, reason TEXT) returns INT AS $$
  25. DECLARE
  26. new_code INT;
  27. current_row RECORD;
  28. l_row acc_trans;
  29. in_user TEXT;
  30. full_reason TEXT;
  31. BEGIN
  32. select into in_user from current_user;
  33. select into current_row from pending_reports where pending_reports.id = in_report_id and pending_reports.id = in_id;
  34. select into l_row from acc_trans where entry_id = current_row.lid;
  35. IF NOT FOUND THEN
  36. RAISE EXCEPTION 'No such id % in this report.', in_scn;
  37. END IF;
  38. IF user <> current_row.user THEN
  39. IF current_row.our_balance <> in_new_amount AND in_new_amount = current_row.their_balance THEN
  40. update pending_reports pr
  41. set pr.corrections = pending_reports.corrections + 1,
  42. pr.new_balance = in_new_amount,
  43. error_code = 0
  44. where id = in_report_id and scn = in_scn;
  45. return 0;
  46. -- After that, it is required to update the general ledger.
  47. full_reason := "User % is filing a reconciliation correction on the general ledger, changing amount % to amount %.
  48. Their reason given is: %", in_user, current_row.our_balance, in_new_amount, reason;
  49. select update_ledger(current_row.lid, in_new_amount, full_reason)
  50. ELSE IF current_row.our_balance = in_new_amount THEN
  51. -- This should be something, does it equal the original
  52. -- balance? If so, there's no change.
  53. return current_row.error_code;
  54. END IF;
  55. END IF;
  56. return current_row.error_code;
  57. END;
  58. $$ language 'plpgsql';
  59. -- to correct an incorrect bank statement value.
  60. CREATE OR REPLACE FUNCTION reconciliation_correct_bank_statement (in_report_id INT, in_id int, in_new_amount NUMERIC) returns INT AS $$
  61. DECLARE
  62. new_code INT;
  63. current_row RECORD;
  64. in_user TEXT;
  65. BEGIN
  66. select into in_user from current_user;
  67. select into current_row from pending_reports where pending_reports.id = in_report_id and pending_reports.scn = in_scn;
  68. IF NOT FOUND THEN
  69. RAISE EXCEPTION 'No such SCN % in this report.', in_scn;
  70. END IF;
  71. IF user <> current_row.user THEN
  72. IF current_row.their_balance <> in_new_amount AND in_new_amount = current_row.our_balance THEN
  73. update pending_reports pr
  74. set pr.corrections = pending_reports.corrections + 1,
  75. pr.new_balance = in_new_amount,
  76. error_code = 0
  77. where id = in_report_id and scn = in_scn;
  78. return 0;
  79. ELSE IF current_row.their_balance = in_new_amount THEN
  80. -- This should be something, does it equal the original
  81. -- balance? If so, there's no change.
  82. return current_row.error_code;
  83. END IF;
  84. END IF;
  85. return current_row.error_code;
  86. END;
  87. $$ language 'plpgsql';
  88. CREATE OR REPLACE reconciliation_correct_passthrough ( in_report_id int, in_id int ) returns INT AS $$
  89. DECLARE
  90. in_user TEXT;
  91. pending_entry pending_reports;
  92. BEGIN
  93. select into in_user from current_user;
  94. select into pending_entry from pending_reports where report_id = in_report_id and id = in_id;
  95. IF NOT FOUND THEN
  96. -- Raise an exception.
  97. RAISE EXCEPTION "Cannot find entry.";
  98. ELSE IF pending_entry.errorcode <> 4 THEN
  99. -- Only error codes of 4 may be "passed through" safely.
  100. RAISE EXCEPTION "Selected entry not permitted to be passed through.";
  101. ELSE
  102. -- Then we mark it passthroughable, and "approve" will overlook it.
  103. update pending_reports set overlook = 't', errorcode = 0 where report_id = in_report_id and id = in_id;
  104. return 0;
  105. END IF;
  106. END;
  107. $$ language 'plpgsql';
  108. CREATE OR REPLACE FUNCTION reconciliation_correct_bank_charge (in_report_id int, in_id int) returns INT AS $$
  109. DECLARE
  110. in_user TEXT;
  111. pending_entry pending_reports;
  112. BEGIN
  113. IF NOT FOUND THEN
  114. -- Raise an exception.
  115. RAISE EXCEPTION "Cannot find entry with ID % in report %.", in_id, in_report_id;
  116. ELSE IF pending_entry.errorcode <> 2 THEN
  117. -- Only error codes of 4 may be "passed through" safely.
  118. RAISE EXCEPTION "Attempt to retroactively add a non-bank-charge entry to the ledger.";
  119. ELSE
  120. -- Then we mark it passthroughable, and "approve" will overlook it.
  121. select create_entry (pending_entry.their_balance, 'payable', pending_entry.clear_date, 'Bank charge');
  122. update pending_reports set errorcode = 0 where report_id = in_report_id and id = in_id;
  123. return 0;
  124. END IF;
  125. END;
  126. $$ LANGUAGE 'plpgsql';
  127. CREATE OR REPLACE FUNCTION reconciliation_correct_unaccounted_charge (in_report_id int, in_id int, reason TEXT) RETURNS INT AS $$
  128. DECLARE
  129. in_user TEXT;
  130. pending_entry pending_reports;
  131. note TEXT;
  132. BEGIN
  133. IF NOT FOUND THEN
  134. -- Raise an exception.
  135. RAISE EXCEPTION "Cannot find entry with ID % in report %.", in_id, in_report_id;
  136. ELSE IF pending_entry.errorcode <> 3 THEN
  137. -- Only error codes of 4 may be "passed through" safely.
  138. RAISE EXCEPTION "Not an unaccounted charge; cannot be retroactively added to the ledger.";
  139. ELSE
  140. -- Then we mark it passthroughable, and "approve" will overlook it.
  141. note := 'Retroactive addition of an unaccounted entry, of value %.
  142. Being added by user % with the following explanation: %', pending_entry.their_balance, in_user, in_reason;
  143. select create_entry (pending_entry.their_balance, 'payable', pending_entry.clear_date,note);
  144. update pending_reports set errorcode = 0 where report_id = in_report_id and id = in_id;
  145. return 0;
  146. END IF;
  147. END;
  148. $$ language 'plpgsql';
  149. CREATE OR REPLACE FUNCTION reconciliation_report_approve (in_report_id INT) returns INT as $$
  150. -- Does some basic checks before allowing the approval to go through;
  151. -- moves the approval to "reports", I guess, or some other "final" table.
  152. --
  153. -- Pending may just be a single flag in the database to mark that it is
  154. -- not finalized. Will need to discuss with Chris.
  155. DECLARE
  156. current_row RECORD;
  157. completed pending_reports;
  158. total_errors INT;
  159. in_user TEXT;
  160. BEGIN
  161. select into in_user current_user;
  162. select into current_row distinct on user * from pending_reports where report_id = in_report_id;
  163. IF NOT FOUND THEN
  164. RAISE EXCEPTION "Fatal Error: Pending report % not found", in_report_id;
  165. END IF;
  166. IF current_row.user = in_user THEN
  167. RAISE EXCEPTION "Fatal Error: User % cannot self-approve report!", in_user;
  168. END IF;
  169. SELECT INTO total_errors count(*) from pending_reports where report_id = in_report_id and error_code <> 0;
  170. IF total_errors <> 0 THEN
  171. RAISE EXCEPTION "Fatal Error: Cannot approve while % uncorrected errors remain.", total_errors;
  172. END IF;
  173. -- so far, so good. Different user, and no errors remain. Therefore, we can move it to completed reports.
  174. --
  175. -- User may not be necessary - I would think it better to use the
  176. -- in_user, to note who approved the report, than the user who
  177. -- filed it. This may require clunkier syntax..
  178. insert into reports (report_id, scn, their_balance, our_balance, code, user, correction )
  179. SELECT report_id, scn, their_balance, our_balance, code, user, corrections
  180. FROM pending_reports
  181. WHERE report_id = in_report_id;
  182. return 1;
  183. END;
  184. $$ language 'plpgsql';
  185. CREATE OR REPLACE FUNCTION reconciliation_new_report_id () returns INT as $$
  186. SELECT nextval('pending_report_report_id_seq');
  187. $$ language 'sql';
  188. create or replace function reconciliation_add_entry(in_report_id INT, in_scn INT, in_amount INT, in_account INT, in_user TEXT, in_date TIMESTAMP) RETURNS INT AS $$
  189. DELCARE
  190. la RECORD;
  191. errorcode INT;
  192. our_value NUMERIC;
  193. lid INT;
  194. BEGIN
  195. SELECT INTO la FROM acc_trans gl
  196. WHERE gl.source = in_scn
  197. and gl.account = in_account
  198. and gl.amount = in_amount;
  199. lid := NULL;
  200. IF NOT FOUND THEN
  201. -- they have it, we don't. This is Bad, and implies either a bank
  202. -- charge or an unaccounted cheque.
  203. if in_scn <> '' and in_scn IS NOT NULL THEN
  204. -- It's a bank charge. Approval action will probably be
  205. -- adding it as an entry to the general ledger.
  206. errorcode := 2;
  207. our_value := 0;
  208. ELSE
  209. -- Okay, now this is bad.
  210. -- They have a cheque/sourced charge that we don't.
  211. -- REsolution action is going to be
  212. errorcode := 3;
  213. our_value := 0;
  214. END IF;
  215. ELSE if la.amount <> in_amount THEN
  216. errorcode := 1;
  217. our_value := la.amount;
  218. lid := la.entry_id;
  219. ELSE
  220. -- it reconciles. No problem.
  221. errorcode := 0;
  222. our_value := la.amount;
  223. lid := la.entry_id;
  224. END IF;
  225. INSERT INTO pending_reports (
  226. report_id,
  227. scn,
  228. their_balance,
  229. our_balance,
  230. error_code,
  231. user,
  232. clear_time,
  233. ledger_id
  234. )
  235. VALUES (
  236. in_report_id,
  237. in_scn,
  238. in_amount,
  239. gl.balance,
  240. errorcode,
  241. in_user,
  242. in_date,
  243. lid
  244. );
  245. -- success, basically. This could very likely be collapsed to
  246. -- do the compare check here, instead of in the Perl app. Save us a DB
  247. -- call.
  248. return 1;
  249. END;
  250. $$ language 'plpgsql';
  251. create or replace function reconciliation_pending_transactions (in_report_id INT, in_month TIMESTAMP, in_user INT) RETURNS setof int as $$
  252. DECLARE
  253. gl_row acc_trans;
  254. BEGIN
  255. FOR gl_row IN
  256. select gl.* from acc_trans gl, pending_reports pr
  257. where gl.cleared = 'f'
  258. and date_trunc('month',gl.transdate) <= date_trunc('month', in_month)
  259. and gl.entry_id <> pr.lid
  260. LOOP
  261. INSERT INTO pending_reports (
  262. report_id,
  263. scn,
  264. their_balance,
  265. our_balance,
  266. error_code,
  267. user,
  268. clear_time,
  269. ledger_id
  270. )
  271. VALUES (
  272. in_report_id, -- add it to the report ID
  273. gl_row.source, -- the source control number
  274. 0, -- The banks' amount for the transaction
  275. gl_row.amount, -- our recorded amount
  276. 4, -- The error code, meaning it's uncleared.
  277. in_user, -- the report-generating user
  278. in_month, -- basically, right now.
  279. gl_row.entry_id -- the foreign key to the ledger
  280. );
  281. END LOOP;
  282. END;
  283. $$ language plpgsql;
  284. CREATE OR REPLACE FUNCTION reconciliation_report (in_report_id INT) RETURNS setof pending_reports as $$
  285. DECLARE
  286. row pending_reports;
  287. BEGIN
  288. FOR row IN select * from pending_reports where report_id = in_report_id LOOP
  289. RETURN NEXT row;
  290. END LOOP;
  291. END;
  292. $$ language 'plpgsql';
  293. CREATE OR REPLACE FUNCTION reconciliation_get_total (in_report_id INT) returns setof pending_reports AS $$
  294. DECLARE
  295. row pending_reports;
  296. BEGIN
  297. SELECT INTO row FROM pending_reports
  298. WHERE ledger_id IS NULL
  299. and report_id = in_report_id
  300. AND scn = -1;
  301. IF NOT FOUND THEN -- I think this is a fairly major error condition
  302. RAISE EXCEPTION "No Bank Total found.";
  303. ELSE
  304. return row;
  305. END IF;
  306. END;
  307. $$ language 'plpgsql';
  308. CREATE OR REPLACE FUNCTION reconciliation_corrections (in_report_id INT, in_id INT) returns setof report_corrections AS $$
  309. DECLARE
  310. corr report_corrections;
  311. BEGIN
  312. SELECT INTO corr FROM report_corrections WHERE report_id = in_report_id AND id = in_id LIMIT 1;
  313. IF NOT FOUND THEN
  314. RAISE EXCEPTION "No corrections for selected entry.";
  315. ELSE
  316. FOR corr IN select * from report_corrections WHERE report_id = in_report_id AND id = in_id LOOP
  317. RETURN NEXT corr;
  318. END LOOP;
  319. END IF;
  320. END;
  321. $$ language 'plplsql';
  322. CREATE OR REPLACE FUNCTION reconciliation_single_entry (in_report_id INT, in_id INT) returns setof pending_reports AS
  323. DECLARE
  324. row pending_reports;
  325. BEGIN
  326. SELECT INTO row FROM pending_reports WHERE report_id = in_report_id and id = in_id LIMIT 1;
  327. -- if there's more than one, that's a Bad Thing
  328. IF NOT FOUND THEN
  329. RAISE EXCEPTION "Could not find selected report entry";
  330. ELSE
  331. RETURN row;
  332. END IF;
  333. END;
  334. $$ language 'plpgsql';