summaryrefslogtreecommitdiff
path: root/sql/modules/Voucher.sql
blob: 84df47f0e85aac1c7194b00099359c750df54c2b (plain)
  1. CREATE OR REPLACE FUNCTION voucher_get_batch (in_batch_id integer)
  2. RETURNS batches AS
  3. $$
  4. DECLARE
  5. batch_out batches%ROWTYPE;
  6. BEGIN
  7. SELECT * INTO batch_out FROM batches b WHERE b.id = in_batch_id;
  8. RETURN batch_out;
  9. END;
  10. $$ language plpgsql;
  11. CREATE TYPE batch_list AS (
  12. id integer,
  13. batch_number text,
  14. description text,
  15. entered date,
  16. approved date,
  17. amount numeric,
  18. employee text,
  19. manager text);
  20. CREATE FUNCTION batch_search
  21. (in_batch text, in_description text, in_batch_number text, in_date_from date,
  22. in_date_to date, in_date_include date, in_approved boolean)
  23. RETURNS SETOF batch_list
  24. AS $$
  25. DECLARE
  26. batch_out batch_list;
  27. BEGIN
  28. FOR batch_out IN
  29. SELECT b.id, b.batch, b.batch_number, b.description,
  30. b.entered, b.approved, b.amount,
  31. e.name AS employee, m.name AS manager
  32. FROM batches b
  33. LEFT JOIN employees e ON (b.employee_id = e.id)
  34. LEFT JOIN employees m ON (b.managerid = m.id)
  35. WHERE supplied_and_equal(in_batch, b.batch)
  36. AND supplied_and_like(in_description, description)
  37. AND supplied_and_like(in_batch_number, batch_number)
  38. AND supplied_and_later(in_date_from, entered)
  39. AND supplied_and_earlier(in_date_to, entered)
  40. AND (coalesce(in_approved, 'f') = (approved IS NULL))
  41. LOOP
  42. RETURN NEXT batch_out;
  43. END LOOP;
  44. END;
  45. $$ language PLPGSQL;
  46. CREATE FUNCTION batch_post (in_batch text, in_login varchar, in_entered date,
  47. in_batch_number text, in_description text, in_id integer)
  48. RETURNS integer AS
  49. $$
  50. BEGIN
  51. UPDATE batches
  52. SET batch_number = in_batch_number,
  53. description = in_description,
  54. entered = in_entered
  55. WHERE id = in_id;
  56. IF FOUND THEN
  57. RETURN in_id;
  58. END IF;
  59. INSERT INTO batches (batch, employee_id, batch_number, description,
  60. entered)
  61. VALUES (in_batch, (SELECT id FROM employees WHERE login = in_login),
  62. in_batch_number, description);
  63. RETURN currval('id');
  64. END;
  65. $$ LANGUAGE PLPGSQL;
  66. CREATE TYPE voucher_list AS (
  67. id int,
  68. reference text,
  69. description text,
  70. batch_id int,
  71. transaction_id integer,
  72. amount numeric,
  73. transaction_date date,
  74. batch_class text
  75. );
  76. CREATE OR REPLACE FUNCTION voucher_list (in_batch_id integer)
  77. RETURNS SETOF voucher_list AS
  78. $$
  79. declare voucher_item record;
  80. BEGIN
  81. FOR voucher_item IN
  82. SELECT v.id, a.invnumber, e.name, v.batch_id, v.trans_id,
  83. a.amount - a.paid, a.transdate, 'Payable'
  84. FROM voucher v
  85. JOIN ap a ON (v.trans_id = a.id)
  86. JOIN entity e ON (a.entity_id = e.id)
  87. WHERE v.batch_id = in_batch_id
  88. AND v.batch_class = (select id from batch_class
  89. WHERE class = 'payable')
  90. UNION
  91. SELECT v.id, a.invnumber, e.name, v.batch_id, v.trans_id,
  92. a.amount - a.paid, a.transdate, 'Receivable'
  93. FROM voucher v
  94. JOIN ar a ON (v.trans_id = a.id)
  95. JOIN entity e ON (a.entity_id = e.id)
  96. WHERE v.batch_id = in_batch_id
  97. AND v.batch_class = (select id from batch_class
  98. WHERE class = 'receivable')
  99. UNION
  100. SELECT v.id, a.source, a.memo, v.batch_id, v.trans_id,
  101. a.amount, a.transdate, bc.class
  102. FROM voucher v
  103. JOIN acc_trans a ON (v.trans_id = a.trans_id)
  104. JOIN batch_class bc ON (bc.id = v.batch_class)
  105. WHERE v.batch_id = in_batch_id
  106. AND a.voucher_id = v.id
  107. AND bc.class like 'payment%'
  108. OR bc.class like 'receipt%'
  109. UNION
  110. SELECT v.id, g.reference, g.description, v.batch_id, v.trans_id,
  111. sum(a.amount), g.transdate, 'gl'
  112. FROM voucher v
  113. JOIN gl g ON (g.id = v.trans_id)
  114. JOIN acc_trans a ON (v.trans_id = a.trans_id)
  115. WHERE a.amount > 0
  116. AND v.batch_id = in_batch_id
  117. AND v.batch_class IN (select id from batch_class
  118. where class = 'gl')
  119. GROUP BY v.id, g.reference, g.description, v.batch_id,
  120. v.trans_id, g.transdate
  121. ORDER BY 7, 1
  122. LOOP
  123. RETURN NEXT voucher_item;
  124. END LOOP;
  125. END;
  126. $$ language plpgsql;
  127. CREATE TYPE batch_list_item AS (
  128. id integer,
  129. batch_class text,
  130. description text,
  131. created_by text,
  132. created_on date,
  133. total numeric
  134. );
  135. CREATE FUNCTION batch_list RETURNS SETOF batch_list_item AS
  136. $$
  137. $$ LANGUAGE PLPGSQL;
  138. CREATE OR REPLACE FUNCTION batch_post in_batch_id INTEGER)
  139. returns int AS
  140. $$;
  141. $$ LANGUAGE PLPGSQL;
  142. CREATE OR REPLACE FUNCTION batch_create(
  143. in_batch_number text, in_description text, in_batch_class text) RETURNS int AS
  144. $$
  145. BEGIN
  146. INSERT INTO
  147. batch (batch_class_id, description, control_code, created_by)
  148. VALUES ((SELECT id FROM batch_class WHERE class = in_batch_class),
  149. in_description, in_batch_number,
  150. (select entity_id FROM users WHERE username = session_user));
  151. return currval('batch_id_seq');
  152. END;
  153. $$ LANGUAGE PLPGSQL;