Launchpad automatic translations update.
[odoo/odoo.git] / addons / base_contact / base_contact_installer.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2009 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23
24 class base_contact_installer(osv.osv_memory):
25     _name = 'base.contact.installer'
26     _inherit = 'res.config.installer'
27
28     _columns = {
29         'name': fields.char('Name', size=64),
30         'migrate': fields.boolean('Migrate', help="If you select this, all addresses will be migrated."),
31     }
32
33     def execute(self, cr, uid, ids, context=None):
34         """
35         This function is used to create contact and address from existing partner address
36         """
37         obj = self.pool.get("base.contact.installer").browse(cr, uid, uid, context=context)
38         if obj.migrate:
39             # Enable PL/pgSQL if not enabled yet in the database
40             cr.execute("SELECT 1 FROM pg_language WHERE lanname = 'plpgsql'")
41             if not cr.fetchone():
42                 cr.execute("CREATE LANGUAGE plpgsql;")
43
44             cr.execute("""DROP TRIGGER IF EXISTS contactjob on res_partner_contact;
45                           CREATE OR REPLACE FUNCTION add_to_job() RETURNS TRIGGER AS $contactjob$
46                             DECLARE
47                             new_name varchar;
48                             new_phonenum varchar;
49                             BEGIN
50                                IF(TG_OP='INSERT') THEN
51                                INSERT INTO res_partner_job(contact_id, address_id, function, state) VALUES(NEW.id, NEW.website::integer,NEW.first_name, 'current');
52                                UPDATE res_partner_contact set first_name=Null, website=Null, active=True where id=NEW.id;
53                             END IF;
54                             RETURN NEW;
55                             END;
56                           $contactjob$ LANGUAGE plpgsql;
57                           CREATE TRIGGER contactjob AFTER INSERT ON res_partner_contact FOR EACH ROW EXECUTE PROCEDURE add_to_job();""")
58
59             cr.execute("INSERT into res_partner_contact (name, title, email, first_name, website)  (SELECT coalesce(name, 'Noname'), title, email, function , to_char(id, '99999999') from res_partner_address)")
60
61             cr.execute("DROP TRIGGER  IF EXISTS contactjob  on res_partner_contact")
62
63             cr.execute("DROP FUNCTION IF EXISTS  add_to_job()")
64
65 base_contact_installer()
66
67 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: