From e9a1500790f0a41e9fd27a7e2a556e7d78646d9e Mon Sep 17 00:00:00 2001
From: tetragon <tetragon@4979c152-3d1c-0410-bac9-87ea11338e46>
Date: Wed, 22 Nov 2006 14:36:36 +0000
Subject: Adjust del_exchangerate to work with current defaults table layout

git-svn-id: https://ledger-smb.svn.sourceforge.net/svnroot/ledger-smb/branches/1.2@682 4979c152-3d1c-0410-bac9-87ea11338e46
---
 sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql | 56 +++++++++++++++++++++++++++++++++
 1 file changed, 56 insertions(+)

(limited to 'sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql')

diff --git a/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql b/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
index 44d015a8..a7f4a3f1 100644
--- a/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
+++ b/sql/legacy/Pg-upgrade-2.6.18-2.6.19.sql
@@ -287,6 +287,62 @@ SELECT 'appname', 'LedgerSMB'::text;
 
 DROP TABLE old_defaults;
 
+CREATE OR REPLACE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
+
+declare
+  t_transdate date;
+  t_curr char(3);
+  t_id int;
+  d_curr text;
+
+begin
+
+  select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
+  
+  if TG_RELNAME = ''ar'' then
+    select into t_curr, t_transdate curr, transdate from ar where id = old.id;
+  end if;
+  if TG_RELNAME = ''ap'' then
+    select into t_curr, t_transdate curr, transdate from ap where id = old.id;
+  end if;
+  if TG_RELNAME = ''oe'' then
+    select into t_curr, t_transdate curr, transdate from oe where id = old.id;
+  end if;
+
+  if d_curr != t_curr then
+
+    select into t_id a.id from acc_trans ac
+    join ar a on (a.id = ac.trans_id)
+    where a.curr = t_curr
+    and ac.transdate = t_transdate
+
+    except select a.id from ar a where a.id = old.id
+    
+    union
+    
+    select a.id from acc_trans ac
+    join ap a on (a.id = ac.trans_id)
+    where a.curr = t_curr
+    and ac.transdate = t_transdate
+    
+    except select a.id from ap a where a.id = old.id
+    
+    union
+    
+    select o.id from oe o
+    where o.curr = t_curr
+    and o.transdate = t_transdate
+    
+    except select o.id from oe o where o.id = old.id;
+
+    if not found then
+      delete from exchangerate where curr = t_curr and transdate = t_transdate;
+    end if;
+  end if;
+return old;
+
+end;
+' language 'plpgsql';
 
 CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
 RETURNS BOOL AS
-- 
cgit v1.2.3

