summaryrefslogtreecommitdiff
path: root/sql/modules/Drafts.sql
blob: 4031238854b93df8f58b8cb3e315a1e67e3731ba (plain)
  1. CREATE TYPE draft_search_result AS (
  2. id int,
  3. transdate date,
  4. description text,
  5. amount numeric
  6. );
  7. CREATE OR REPLACE FUNCTION draft__search(in_type text, in_with_accno text,
  8. in_from_date date, in_to_date date, in_amount_le numeric, in_amount_ge numeric)
  9. returns setof draft_search_result AS
  10. $$
  11. DECLARE out_row RECORD;
  12. BEGIN
  13. FOR out_row IN
  14. SELECT trans.id, trans.transdate, trans.description,
  15. sum(case when in_type = 'ap' AND chart.link = 'AP'
  16. THEN line.amount
  17. WHEN in_type = 'ar' AND chart.link = 'AR'
  18. THEN line.amount * -1
  19. WHEN in_type = 'gl' AND line.amount > 0
  20. THEN line.amount
  21. ELSE 0
  22. END) as amount
  23. FROM (
  24. SELECT id, transdate, description, approved from ap
  25. WHERE in_type = 'ap'
  26. UNION
  27. SELECT id, transdate, description, approved from ar
  28. WHERE in_type = 'ar'
  29. UNION
  30. SELECT id, transdate, description, approved from gl
  31. WHERE in_type = 'gl'
  32. ) trans
  33. JOIN acc_trans line ON (trans.id = line.trans_id)
  34. JOIN chart ON (line.chart_id = chart.id)
  35. WHERE (in_from_date IS NULL or trans.transdate >= in_from_date)
  36. AND (in_to_date IS NULL
  37. or trans.transdate <= in_to_date)
  38. AND trans.approved IS FALSE
  39. AND trans.id NOT IN (select trans_id from voucher)
  40. GROUP BY trans.id, trans.transdate, trans.description
  41. HAVING (in_with_accno IS NULL or in_with_accno =
  42. ANY(as_array(chart.accno)))
  43. LOOP
  44. RETURN NEXT out_row;
  45. END LOOP;
  46. END;
  47. $$ language plpgsql;
  48. CREATE OR REPLACE FUNCTION draft_approve(in_id int) returns bool as
  49. $$
  50. declare
  51. t_table text;
  52. begin
  53. SELECT table_name into t_table FROM transactions where id = in_id;
  54. IF (t_table = 'ar') THEN
  55. UPDATE ar set approved = true where id = in_id;
  56. ELSIF (t_table = 'ap') THEN
  57. UPDATE ap set approved = true where id = in_id;
  58. ELSIF (t_table = 'gl') THEN
  59. UPDATE gl set approved = true where id = in_id;
  60. ELSE
  61. raise exception 'Invalid table % in draft_approve for transaction %', t_table, in_id;
  62. END IF;
  63. RETURN FOUND;
  64. END;
  65. $$ LANGUAGE PLPGSQL SECURITY DEFINER;
  66. CREATE OR REPLACE FUNCTION draft_delete(in_id int) returns bool as
  67. $$
  68. declare
  69. t_table text;
  70. begin
  71. SELECT table_name into t_table FROM transactions where id = in_id;
  72. IF t_table = 'ar' THEN
  73. DELETE FROM ar WHERE id = in_id AND approved IS FALSE;
  74. ELSIF t_table = 'ap' THEN
  75. DELETE FROM ap WHERE id = in_id AND approved IS FALSE;
  76. ELSIF t_table = 'gl' THEN
  77. DELETE FROM gl WHERE id = in_id AND approved IS FALSE;
  78. ELSE
  79. raise exception 'Invalid table % in draft_delete for transaction %', t_table, in_id;
  80. END IF;
  81. RETURN FOUND;
  82. END;
  83. $$ LANGUAGE PLPGSQL SECURITY DEFINER;