Commit d587b50a authored by Frank Bergmann's avatar Frank Bergmann

Initial Import

parents
Pipeline #943 failed with stages
<?xml version="1.0"?>
<!-- Generated by the OpenACS Package Manager -->
<package key="intranet-navision" url="http://openacs.org/repository/apm/packages/intranet-navision" type="apm_application">
<package-name>]project-open[ Microsoft NAV (Navision) Integration</package-name>
<pretty-plural>]project-open[ Microsoft NAV (Navision) Integration</pretty-plural>
<initial-install-p>f</initial-install-p>
<singleton-p>t</singleton-p>
<implements-subsite-p>f</implements-subsite-p>
<inherit-templates-p>f</inherit-templates-p>
<auto-mount>intranet-navision</auto-mount>
<version name="4.0.4.0.0" url="http://openacs.org/repository/download/apm/intranet-navision-4.0.4.0.0.apm">
<owner url="mailto:frank.bergmann@project-open.com">Frank Bergmann</owner>
<summary>Imports a number of objects from NAV into ]project-open[</summary>
<vendor url="http://www.project-open.com/">]project-open[</vendor>
<description format="text/html">Provides a number of PL/SQL scripts that take as input a number of NAV tables that have been copied to the PostgreSQL database. The scripts insert/update the relevant ]project-open[ objects.</description>
<maturity>0</maturity>
<provides url="intranet-navision" version="4.0.4.0.0"/>
<callbacks>
</callbacks>
<parameters>
<!-- No version parameters -->
</parameters>
</version>
</package>
---------------------------------------------------------------------------------
-- Categories
---------------------------------------------------------------------------------
---------------------------------------------------------------------------------
-- Categories
---------------------------------------------------------------------------------
-- Reserved range of Categories for Navision integration
-- 50000-59999 Navision Integration (10000)
-- 50000-50099 free
-- 50100-50199 Navision Reuse Band
-- 50200-50499 Intranet Translation Task Type (Activity Group + Activities)
-- 50500-50599 Navision General Posting Group
-- 50600-50699 Navision VAT Posting Group
-- 50700-50799 Navision Customer Posting Group
-- 50800-50899 Navision Vendor Posting Group
-- 50900-50999 Navision Payment Method Code
-- 51000-51499 Task Types based on Activity Codes (?)
-- 60000-60099 ----
-- Delete the old "Navision Activity" Category type
delete from im_category_hierarchy where child_id in (select category_id from im_categories where category_type = 'Navision Activity');
delete from im_category_hierarchy where parent_id in (select category_id from im_categories where category_type = 'Navision Activity');
delete from im_categories where category_type = 'Navision Activity';
---------------------------------------------------------------------------------
-- Currencies
---------------------------------------------------------------------------------
insert into currency_codes VALUES ('ARS','Argentine Peso','t',NULL,100);
---------------------------------------------------------------------------------
-- Country Codes
---------------------------------------------------------------------------------
insert into country_codes VALUES ('am', 'Armenia');
insert into country_codes VALUES ('bd', 'Bangladesh');
insert into country_codes VALUES ('cm', 'Cameroon');
insert into country_codes VALUES ('cs', 'Serbia and Montenegro');
insert into country_codes VALUES ('gb', 'Great Britain');
insert into country_codes VALUES ('ke', 'Kenya');
insert into country_codes VALUES ('hk', 'Hong Kong');
insert into country_codes VALUES ('kz', 'Kazakhstan');
insert into country_codes VALUES ('kh', 'Cambodia');
insert into country_codes VALUES ('me', 'Montenegro');
insert into country_codes VALUES ('pa', 'Panama');
insert into country_codes VALUES ('rs', 'Serbia');
insert into country_codes VALUES ('sc', 'Seychelly');
insert into country_codes VALUES ('sp', 'Somalia');
insert into country_codes VALUES ('th', 'Thailand');
insert into country_codes VALUES ('uz', 'Uzbekistan');
insert into country_codes VALUES ('vn', 'Vietnam');
---------------------------------------------------------------------------------
-- Subject Areas
---------------------------------------------------------------------------------
update im_categories set category = 'AUT', category_description = 'Automotive' where category_id = 555;
update im_categories set category = 'MED', category_description = 'Medical' where category_id = 545;
update im_categories set category = 'TEL', category_description = 'Telecom' where category_id = 560;
update im_categories set category = 'LEG', category_description = 'Legal' where category_id = 525;
update im_categories set category = 'FIN', category_description = 'Finance' where category_id = 515;
update im_categories set category = 'PRS', category_description = 'Press Releases and Articles' where category_id = 540;
update im_categories set category = 'IT', category_description = 'IT' where category_id = 535;
update im_categories set category = 'MAR', category_description = 'Marcom' where category_id = 510;
update im_categories set category = 'MRC', category_description = 'Creative Marcom' where category_id = 520;
update im_categories set category = 'MRT', category_description = 'Technical Marcom' where category_id = 500;
update im_categories set category = 'MSD', category_description = 'MS.Com specivid' where category_id = 505;
update im_categories set category = 'M1R', category_description = 'M1R' where category_id = 550;
update im_categories set category = '13', category_description = '13' where category_id = 570;
update im_categories set enabled_p = 'f' where category_id in (530, 565);
update im_categories set enabled_p = 't' where category_id in (550, 570);
SELECT im_category_new(50100,'100%','Navision Reuse Band');
SELECT im_category_new(50110,'80%','Navision Reuse Band');
SELECT im_category_new(50115,'66%','Navision Reuse Band');
SELECT im_category_new(50120,'60%','Navision Reuse Band');
SELECT im_category_new(50125,'50%','Navision Reuse Band');
SELECT im_category_new(50130,'40%','Navision Reuse Band');
SELECT im_category_new(50135,'33%','Navision Reuse Band');
SELECT im_category_new(50140,'30%','Navision Reuse Band');
SELECT im_category_new(50150,'25%','Navision Reuse Band');
SELECT im_category_new(50155,'22%','Navision Reuse Band');
SELECT im_category_new(50160,'20%','Navision Reuse Band');
SELECT im_category_new(50170,'10%','Navision Reuse Band');
SELECT im_category_new(50180,'5%','Navision Reuse Band');
update im_categories set aux_string2=100 where category_id = 50100;
update im_categories set aux_string2=80 where category_id = 50110;
update im_categories set aux_string2=66 where category_id = 50115;
update im_categories set aux_string2=60 where category_id = 50120;
update im_categories set aux_string2=50 where category_id = 50125;
update im_categories set aux_string2=40 where category_id = 50130;
update im_categories set aux_string2=33 where category_id = 50135;
update im_categories set aux_string2=30 where category_id = 50140;
update im_categories set aux_string2=25 where category_id = 50150;
update im_categories set aux_string2=22 where category_id = 50155;
update im_categories set aux_string2=20 where category_id = 50160;
update im_categories set aux_string2=10 where category_id = 50170;
update im_categories set aux_string2=5 where category_id = 50180;
-- Conver project types into translation task types.
--
update im_categories
set category_type = 'Intranet Translation Task Type'
where
category_type = 'Intranet Project Type' and
category_id in (87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 2503);
-- Project Types "Archieved" and "Blocked"
SELECT im_category_new (nextval('im_categories_seq')::integer,'Archived','Intranet Project Status');
SELECT im_category_hierarchy_new (
(select category_id from im_categories where category = 'Archived' and category_type = 'Intranet Project Status'),
81
);
SELECT im_category_new (nextval('im_categories_seq')::integer,'Blocked','Intranet Project Status');
SELECT im_category_hierarchy_new (
(select category_id from im_categories where category = 'Blocked' and category_type = 'Intranet Project Status'),
81
);
---------------------------------------------------------------------------------
-- Navision UoM
---------------------------------------------------------------------------------
SELECT im_category_new (nextval('im_categories_seq')::integer,'Percentage','Intranet UoM');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Character','Intranet UoM');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Screen','Intranet UoM');
SELECT im_category_new (nextval('im_categories_seq')::integer,'New Word','Intranet UoM');
SELECT im_category_new (nextval('im_categories_seq')::integer,'License','Intranet UoM');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Other','Intranet UoM');
update im_categories set aux_string2 = 'HOUR' where category = 'Hour' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'DAY' where category = 'Day' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'PIECE' where category = 'Unit' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'PAGE' where category = 'Page' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'WORD' where category = 'S-Word' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = '%' where category = 'Percentage' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'CHARACTER' where category = 'Character' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'SCREEN' where category = 'Screen' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'OTHER' where category = 'Other' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'NEW WORD' where category = 'New Word' and category_type = 'Intranet UoM';
update im_categories set aux_string2 = 'LICENSE' where category = 'License' and category_type = 'Intranet UoM';
---------------------------------------------------------------------------------
-- Navision Translation Languages
---------------------------------------------------------------------------------
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AF'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AM'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('AZ'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BE'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BER'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BG'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BH'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('BS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('CA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('CS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('CY'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('DE_AT'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('EN_ID'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('EN_NZ'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('EN_ZA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ES_UN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ES_ME'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ET'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('FA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('FI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('FIL'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('FL'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('FR_CA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('GA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('GD'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('GLI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('GU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HE'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('HY'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ID'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('IG'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('IS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('IT_CH'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('IU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('JV'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KM'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KOK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('KY'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('LB'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('LO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('LT'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('LV'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ML'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MS_BN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MT'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('MY'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('NE'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('NN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('NO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('NS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('NSO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('OR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('PA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('PL'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('PS'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('QU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('RM'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('RO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('RW'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SL'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SQ'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ST'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SU'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SV'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('SW'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TE'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TG'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TH'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TI'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TL'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('TT'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('UK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('UR'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('UZ'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('VA'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('VN'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('WO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('XH'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('YO'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ZH'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ZH_HK'),'Intranet Translation Language');
SELECT im_category_new (nextval('im_categories_seq')::integer,lower('ZU'),'Intranet Translation Language');
-- Recreate the original NAV languages in aux_string2
update im_categories
set aux_string2 = upper(substring(category from '^..'))
where category ~ '^..$' and
category_type = 'Intranet Translation Language'
;
-- Recreate the original NAV languages in aux_string2
-- 4 digit languages - add the additional part for 4 digit languages
update im_categories
set aux_string2 = aux_string2 || ' ' || upper(substring(category from '...(..)'))
where category ~ '^.._..$' and
category_type = 'Intranet Translation Language'
;
-- Create the "Activty Group" main categories
SELECT im_category_new(50200,'DEV','Intranet Translation Task Type','IT_DEV - IT development');
SELECT im_category_new(50202,'DOC','Intranet Translation Task Type','DOC - Documentation localization');
SELECT im_category_new(50204,'DTP','Intranet Translation Task Type','DTP');
SELECT im_category_new(50206,'GRF','Intranet Translation Task Type','GRF - graphics');
SELECT im_category_new(50208,'ITOPS','Intranet Translation Task Type','IT Operations');
SELECT im_category_new(50210,'LEN','Intranet Translation Task Type','LOC_ENG - localization engineering');
SELECT im_category_new(50212,'LING','Intranet Translation Task Type','LING - linguistic tasks');
SELECT im_category_new(50214,'MM','Intranet Translation Task Type','Multimedia');
SELECT im_category_new(50216,'MTR','Intranet Translation Task Type','MTR - machine translation');
SELECT im_category_new(50218,'OTHER','Intranet Translation Task Type','Other');
SELECT im_category_new(50220,'PM','Intranet Translation Task Type','PM - project management');
SELECT im_category_new(50222,'TDE','Intranet Translation Task Type','MTE_TDE - Test development engineering');
SELECT im_category_new(50224,'TEN','Intranet Translation Task Type','MTE_ENG - MTE Sw engineering');
SELECT im_category_new(50226,'TUA','Intranet Translation Task Type','TST_UA - Help testing');
SELECT im_category_new(50228,'TUI','Intranet Translation Task Type','TST_UI - Sw testing');
SELECT im_category_new(50230,'UA','Intranet Translation Task Type','UA - Help localization');
SELECT im_category_new(50232,'UI','Intranet Translation Task Type','UI - Sw localization');
-- Create the "Sub-Activity" below "Activity Group"
SELECT im_category_new(50301,'DEV-ACT','Intranet Translation Task Type','Czech Telecom Address Book (Adresar Ceskeho Telecomu)');
SELECT im_category_new(50302,'DEV-CIP','Intranet Translation Task Type','Caller Id service for IP telephony (CLIP pro IP telefonii)');
SELECT im_category_new(50303,'DEV-DEV','Intranet Translation Task Type','Development');
SELECT im_category_new(50304,'DEV-MSIP','Intranet Translation Task Type','Messaging for IP Telephony(Messaging pro IP telefonii)');
SELECT im_category_new(50305,'DEV-PHBK','Intranet Translation Task Type','Company phone directory (Firemni tel. seznam)');
SELECT im_category_new(50306,'DEV-SA','Intranet Translation Task Type','Service fee (Servisni poplatek)');
SELECT im_category_new(50307,'DEV-SUP','Intranet Translation Task Type','Support and Maintenance');
SELECT im_category_new(50308,'DOC-FP','Intranet Translation Task Type','Fast proof');
SELECT im_category_new(50309,'DOC-LP','Intranet Translation Task Type','Large proof');
SELECT im_category_new(50310,'DOC-SPCH','Intranet Translation Task Type','Spotcheck');
SELECT im_category_new(50311,'DOC-TR','Intranet Translation Task Type','Translation');
SELECT im_category_new(50312,'DOC-TRCH','Intranet Translation Task Type','Translation (with native check)');
SELECT im_category_new(50313,'DOC-TRP','Intranet Translation Task Type','Translation (Piece)');
SELECT im_category_new(50314,'DTP-5BP','Intranet Translation Task Type','5th Black Plate');
SELECT im_category_new(50315,'DTP-DS','Intranet Translation Task Type','DocSys tool');
SELECT im_category_new(50316,'DTP-DTPPS','Intranet Translation Task Type','DTP (including PS)');
SELECT im_category_new(50317,'DTP-DY','Intranet Translation Task Type','Daily');
SELECT im_category_new(50318,'DTP-FDTP','Intranet Translation Task Type','Fast DTP');
SELECT im_category_new(50319,'DTP-FM','Intranet Translation Task Type','FrameMaker');
SELECT im_category_new(50320,'DTP-FMP','Intranet Translation Task Type','FrameMaker (including PS)');
SELECT im_category_new(50321,'DTP-HR','Intranet Translation Task Type','Hourly');
SELECT im_category_new(50322,'DTP-HRF','Intranet Translation Task Type','Hourly (Flash)');
SELECT im_category_new(50323,'DTP-HRTHS','Intranet Translation Task Type','Hourly (TSH special)');
SELECT im_category_new(50324,'DTP-ID','Intranet Translation Task Type','InDesign');
SELECT im_category_new(50325,'DTP-IDP','Intranet Translation Task Type','InDesign (including PS)');
SELECT im_category_new(50326,'DTP-ILL','Intranet Translation Task Type','Illustrator');
SELECT im_category_new(50327,'DTP-ILLP','Intranet Translation Task Type','Illustrator (including PS)');
SELECT im_category_new(50328,'DTP-MSW','Intranet Translation Task Type','MS Word');
SELECT im_category_new(50329,'DTP-MSWP','Intranet Translation Task Type','MS Word (including PS)');
SELECT im_category_new(50330,'DTP-PIECE','Intranet Translation Task Type','Per Piece');
SELECT im_category_new(50331,'DTP-PM','Intranet Translation Task Type','PageMaker');
SELECT im_category_new(50332,'DTP-PMP','Intranet Translation Task Type','PageMaker (including PS)');
SELECT im_category_new(50333,'DTP-PPT','Intranet Translation Task Type','Powepoint');
SELECT im_category_new(50334,'DTP-PPTP','Intranet Translation Task Type','Powepoint (including PS)');
SELECT im_category_new(50335,'DTP-PS','Intranet Translation Task Type','PDF / Postscripting');
SELECT im_category_new(50336,'DTP-QA2IL','Intranet Translation Task Type','QuarkXpress to AdobeIlustrator');
SELECT im_category_new(50337,'DTP-QS','Intranet Translation Task Type','Quicksilver (Interleaf)');
SELECT im_category_new(50338,'DTP-QSP','Intranet Translation Task Type','Quicksilver (Interleaf) (including PS)');
SELECT im_category_new(50339,'DTP-QX','Intranet Translation Task Type','Quark');
SELECT im_category_new(50340,'DTP-QXP','Intranet Translation Task Type','Quark (including PS)');
SELECT im_category_new(50341,'DTP-TUT','Intranet Translation Task Type','Tutor (Oracle tool)');
SELECT im_category_new(50342,'GRF-ACH','Intranet Translation Task Type','Art Capture (per hour)');
SELECT im_category_new(50343,'GRF-ACS','Intranet Translation Task Type','Art Capture (per screen)');
SELECT im_category_new(50344,'GRF-AMK','Intranet Translation Task Type','Art Make-up');
SELECT im_category_new(50345,'GRF-HR','Intranet Translation Task Type','Hourly');
SELECT im_category_new(50346,'GRF-SCR','Intranet Translation Task Type','Screenshoting');
SELECT im_category_new(50347,'LEN-OFB','Intranet Translation Task Type','LocEng (offsite)');
SELECT im_category_new(50348,'LEN-OFN','Intranet Translation Task Type','LocEng (offsite) Nanjing');
SELECT im_category_new(50349,'LEN-OFR','Intranet Translation Task Type','LocEng (offiste) Rosario');
SELECT im_category_new(50350,'LING-ADT','Intranet Translation Task Type','Adaptation');
SELECT im_category_new(50351,'LING-GLS','Intranet Translation Task Type','Glossary');
SELECT im_category_new(50352,'LING-GNRL','Intranet Translation Task Type','General');
SELECT im_category_new(50353,'LING-ICR','Intranet Translation Task Type','In-country review');
SELECT im_category_new(50354,'LING-MINF','Intranet Translation Task Type','Minimum fee');
SELECT im_category_new(50355,'LING-PSP','Intranet Translation Task Type','Process Specification');
SELECT im_category_new(50356,'LING-TMM','Intranet Translation Task Type','TM management');
SELECT im_category_new(50357,'MM-AVR','Intranet Translation Task Type','A/V Recording');
SELECT im_category_new(50358,'MM-VCR','Intranet Translation Task Type','Voice recording');
SELECT im_category_new(50359,'MTR-AU','Intranet Translation Task Type','Authentication');
SELECT im_category_new(50360,'MTR-CM','Intranet Translation Task Type','Comparision');
SELECT im_category_new(50361,'MTR-CO','Intranet Translation Task Type','Consultancy');
SELECT im_category_new(50362,'MTR-IN','Intranet Translation Task Type','Interpreting');
SELECT im_category_new(50363,'MTR-PE','Intranet Translation Task Type','Postediting');
SELECT im_category_new(50364,'MTR-PO','Intranet Translation Task Type','Postate');
SELECT im_category_new(50365,'MTR-PR','Intranet Translation Task Type','Proofreading');
SELECT im_category_new(50366,'MTR-RO','Intranet Translation Task Type','Rounding');
SELECT im_category_new(50367,'MTR-SR','Intranet Translation Task Type','Surcharge');
SELECT im_category_new(50368,'MTR-TN','Intranet Translation Task Type','Transcription');
SELECT im_category_new(50369,'MTR-TR','Intranet Translation Task Type','Translation');
SELECT im_category_new(50370,'PM-HR','Intranet Translation Task Type','Hourly');
SELECT im_category_new(50371,'PM-LRM','Intranet Translation Task Type','LingReview Mgmt');
SELECT im_category_new(50372,'PM-PRC','Intranet Translation Task Type','%');
SELECT im_category_new(50373,'TDE-TDOF','Intranet Translation Task Type','TestDevelopment (offsite)');
SELECT im_category_new(50374,'TDE-TDOFN','Intranet Translation Task Type','TestDevelopment (offsite) Nanjing');
SELECT im_category_new(50375,'TDE-TDON','Intranet Translation Task Type','TestDevelopment (onsite)');
SELECT im_category_new(50376,'TEN-TEOF','Intranet Translation Task Type','Engineering (offsite)');
SELECT im_category_new(50377,'TEN-TEOFN','Intranet Translation Task Type','Engineering (offsite) Nanjing');
SELECT im_category_new(50378,'TEN-TEOFW','Intranet Translation Task Type','Engineering (offsite) - weekend');
SELECT im_category_new(50379,'TEN-TEON','Intranet Translation Task Type','Engineering (onsite)');
SELECT im_category_new(50380,'TEN-TEONW','Intranet Translation Task Type','Engineering (onsite) - weekend');
SELECT im_category_new(50381,'TEN-TSAD','Intranet Translation Task Type','Test Admin');
SELECT im_category_new(50382,'TEN-TSCC','Intranet Translation Task Type','Test Cases creation');
SELECT im_category_new(50383,'TEN-TSPC','Intranet Translation Task Type','Test Plan creation');
SELECT im_category_new(50384,'TUA-TOF','Intranet Translation Task Type','Testing (offsite)');
SELECT im_category_new(50385,'TUA-TOFN','Intranet Translation Task Type','Testing (offsite) Nanjing');
SELECT im_category_new(50386,'TUA-TOFR','Intranet Translation Task Type','Testing (offsite) Rosario');
SELECT im_category_new(50387,'TUI-TOF','Intranet Translation Task Type','Testing (offsite)');
SELECT im_category_new(50388,'TUI-TOFN','Intranet Translation Task Type','Testing (offsite) Nanjing');
SELECT im_category_new(50389,'TUI-TOFR','Intranet Translation Task Type','Testing (offsite) Rosario');
SELECT im_category_new(50390,'TUI-TON','Intranet Translation Task Type','Testing (onsite)');
SELECT im_category_new(50391,'TUI-TONN','Intranet Translation Task Type','Testing (onsite) Nanjing');
SELECT im_category_new(50392,'UA-FP','Intranet Translation Task Type','Fast proof');
SELECT im_category_new(50393,'UA-LP','Intranet Translation Task Type','Large proof');
SELECT im_category_new(50394,'UA-MTP','Intranet Translation Task Type','MT postediting');
SELECT im_category_new(50395,'UA-PRF','Intranet Translation Task Type','Proofreading');
SELECT im_category_new(50396,'UA-PTR','Intranet Translation Task Type','PostTransReview');
SELECT im_category_new(50397,'UA-SPCH','Intranet Translation Task Type','Spotcheck');
SELECT im_category_new(50398,'UA-TR','Intranet Translation Task Type','Translation');
SELECT im_category_new(50399,'UA-TRP','Intranet Translation Task Type','Translation (Piece)');
SELECT im_category_new(50400,'UI-MTP','Intranet Translation Task Type','MT postediting');
SELECT im_category_new(50401,'UI-TR','Intranet Translation Task Type','Translation');
SELECT im_category_new(50402,'OTHER-TRAV','Intranet Translation Task Type','Travel');
SELECT im_category_new(50403,'OTHER-ADUA','Intranet Translation Task Type','ATD_UA_60_30_25');
SELECT im_category_new(50404,'OTHER-ADUI','Intranet Translation Task Type','ATD_SW_50_35');
SELECT im_category_new(50405,'OTHER-BAS','Intranet Translation Task Type','Basic rates');
SELECT im_category_new(50406,'OTHER-CSC','Intranet Translation Task Type','Cisco_80_60_40_25');
SELECT im_category_new(50407,'OTHER-GEN','Intranet Translation Task Type','Generic_60_30_5');
SELECT im_category_new(50408,'OTHER-M01','Intranet Translation Task Type','M_40');
SELECT im_category_new(50409,'OTHER-M02','Intranet Translation Task Type','M_50');
SELECT im_category_new(50410,'OTHER-M03','Intranet Translation Task Type','M_60_30');
SELECT im_category_new(50411,'OTHER-M04','Intranet Translation Task Type','M_60_30_40');
SELECT im_category_new(50412,'OTHER-M05','Intranet Translation Task Type','M_60_40_22');
SELECT im_category_new(50413,'OTHER-M06','Intranet Translation Task Type','M_66_33');
SELECT im_category_new(50414,'OTHER-M07','Intranet Translation Task Type','M_66_40_33');
SELECT im_category_new(50415,'OTHER-MSH','Intranet Translation Task Type','MS Helium_50_40_25');
SELECT im_category_new(50416,'OTHER-MST','Intranet Translation Task Type','MS Trados_66_33_30');
SELECT im_category_new(50417,'OTHER-MT01','Intranet Translation Task Type','MS_MT_70');
SELECT im_category_new(50418,'OTHER-MT02','Intranet Translation Task Type','EMC_MT_85');
SELECT im_category_new(50419,'OTHER-NVL','Intranet Translation Task Type','Novel (50,25,10)');
SELECT im_category_new(50420,'OTHER-OAPP','Intranet Translation Task Type','Oracle APP_60_30_10');
SELECT im_category_new(50421,'OTHER-ODT','Intranet Translation Task Type','Oracle ODT_60_30_10');
SELECT im_category_new(50422,'OTHER-OILT','Intranet Translation Task Type','Oracle ILT_60_30_10');
SELECT im_category_new(50423,'OTHER-OMST','Intranet Translation Task Type','Oracle MST_60_30_10H');
-- Setup the hierarchy of Sub-Activities below the Activity Groups
SELECT im_category_hierarchy_new('DEV-ACT','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-CIP','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-DEV','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-MSIP','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-PHBK','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-SA','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DEV-SUP','DEV','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-FP','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-LP','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-SPCH','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-TR','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-TRCH','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DOC-TRP','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-5BP','DOC','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-DS','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-DTPPS','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-DY','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-FDTP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-FM','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-FMP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-HR','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-HRF','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-HRTHS','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-ID','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-IDP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-ILL','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-ILLP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-MSW','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-MSWP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PIECE','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PM','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PMP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PPT','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PPTP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-PS','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-QA2IL','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-QS','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-QSP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-QX','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-QXP','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('DTP-TUT','DTP','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('GRF-ACH','GRF','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('GRF-ACS','GRF','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('GRF-AMK','GRF','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('GRF-HR','GRF','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('GRF-SCR','GRF','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LEN-OFB','LEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LEN-OFN','LEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LEN-OFR','LEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-ADT','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-GLS','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-GNRL','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-ICR','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-MINF','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-PSP','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('LING-TMM','LING','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MM-AVR','MM','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MM-VCR','MM','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-AU','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-CM','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-CO','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-IN','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-PE','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-PO','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-PR','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-RO','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-SR','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-TN','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('MTR-TR','MTR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('PM-HR','PM','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('PM-LRM','PM','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('PM-PRC','PM','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TDE-TDOF','TDE','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TDE-TDOFN','TDE','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TDE-TDON','TDE','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TEOF','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TEOFN','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TEOFW','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TEON','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TEONW','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TSAD','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TSCC','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TEN-TSPC','TEN','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUA-TOF','TUA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUA-TOFN','TUA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUA-TOFR','TUA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUI-TOF','TUI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUI-TOFN','TUI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUI-TOFR','TUI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUI-TON','TUI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('TUI-TONN','TUI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-FP','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-LP','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-MTP','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-PRF','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-PTR','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-SPCH','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-TR','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UA-TRP','UA','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UI-MTP','UI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('UI-TR','UI','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-TRAV','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-ADUA','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-ADUI','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-BAS','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-CSC','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-GEN','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M01','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M02','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M03','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M04','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M05','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M06','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-M07','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-MSH','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-MST','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-MT01','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-MT02','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-NVL','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-OAPP','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-ODT','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-OILT','OTHER','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('OTHER-OMST','OTHER','Intranet Translation Task Type');
-- More the old static workflow types below "DOC-TR"
SELECT im_category_hierarchy_new('Trans + Edit','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Edit Only','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Trans + Edit + Proof','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Linguistic Validation','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Localization','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Technology','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Trans Only','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Trans + Int. Spotcheck','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Proof Only','DOC-TR','Intranet Translation Task Type');
SELECT im_category_hierarchy_new('Glossary Compilation','DOC-TR','Intranet Translation Task Type');
-- Define that all of the translation tasks are associated with a "trans only" workflow
update im_categories
set aux_string1 = 'trans'
where aux_string1 is null and
category_type = 'Intranet Translation Task Type' and
category_id >= 50200 and
category_id <= 50499;
--------------------------------------------------------------------------------
-- Navision General Posting Group --
--------------------------------------------------------------------------------
SELECT im_category_new(50500,'CN','Navision General Posting Group');
SELECT im_category_new(50501,'CUSTOMER','Navision General Posting Group');
SELECT im_category_new(50502,'CZ','Navision General Posting Group');
SELECT im_category_new(50503,'FREELANCER','Navision General Posting Group');
SELECT im_category_new(50504,'HU','Navision General Posting Group');
SELECT im_category_new(50505,'MIT_HU','Navision General Posting Group');
SELECT im_category_new(50506,'MIT_US','Navision General Posting Group');
SELECT im_category_new(50507,'MIT_CN_WOF','Navision General Posting Group');
SELECT im_category_new(50508,'MIT_SK','Navision General Posting Group');
SELECT im_category_new(50509,'QAREVIEWER','Navision General Posting Group');
SELECT im_category_new(50510,'SK','Navision General Posting Group');
SELECT im_category_new(50511,'SUPP_OTHER','Navision General Posting Group');
SELECT im_category_new(50512,'US','Navision General Posting Group');
SELECT im_category_new(50513,'VENDOR','Navision General Posting Group');
update im_categories set aux_string2='CN' where category_id = 50500;
update im_categories set aux_string2='CUSTOMER' where category_id = 50501;
update im_categories set aux_string2='CZ' where category_id = 50502;
update im_categories set aux_string2='FREELANCER' where category_id = 50503;
update im_categories set aux_string2='HU' where category_id = 50504;
update im_categories set aux_string2='MIT_HU' where category_id = 50505;
update im_categories set aux_string2='MIT_US' where category_id = 50506;
update im_categories set aux_string2='MIT_CN_WOF' where category_id = 50507;
update im_categories set aux_string2='MIT_SK' where category_id = 50508;
update im_categories set aux_string2='QAREVIEWER' where category_id = 50509;
update im_categories set aux_string2='SK' where category_id = 50510;
update im_categories set aux_string2='SUPP_OTHER' where category_id = 50511;
update im_categories set aux_string2='US' where category_id = 50512;
update im_categories set aux_string2='VENDOR' where category_id = 50513;
update im_categories set category_description='Suppliers, customers - MIT CN' where category_id = 50500;
update im_categories set category_description='Customer' where category_id = 50501;
update im_categories set category_description='Suppliers, customers - CZ' where category_id = 50502;
update im_categories set category_description='Localization - freelancers' where category_id = 50503;
update im_categories set category_description='Suppliers, customers - MIT HU only' where category_id = 50504;
update im_categories set category_description='MIT HU only' where category_id = 50505;
update im_categories set category_description='MIT US only' where category_id = 50506;
update im_categories set category_description='MIT CN WOFE only' where category_id = 50507;
update im_categories set category_description='MIT SK only' where category_id = 50508;
update im_categories set category_description='Localization - reviewers' where category_id = 50509;
update im_categories set category_description='Suppliers, customers - MIT SK only' where category_id = 50510;
update im_categories set category_description='Suppliers foreign - other' where category_id = 50511;
update im_categories set category_description='Suppliers, customers - MIT US only' where category_id = 50512;
update im_categories set category_description='Localization - vendors' where category_id = 50513;
--------------------------------------------------------------------------------
-- Navision VAT Posting Group --
--------------------------------------------------------------------------------
SELECT im_category_new(50600,'CELNICE','Navision VAT Posting Group');
SELECT im_category_new(50601,'EU_NEREG','Navision VAT Posting Group');
SELECT im_category_new(50602,'EU_REG','Navision VAT Posting Group');
SELECT im_category_new(50603,'TUZEMSKO','Navision VAT Posting Group');
SELECT im_category_new(50604,'ZAHRANICI','Navision VAT Posting Group');
update im_categories set category_description='customs' where category_id = 50600;
update im_categories set category_description='subject non registered for VAT in EU-mostly EU sub' where category_id = 50601;
update im_categories set category_description='subj.registered for VAT in EU-need to fill VAT no.' where category_id = 50602;
update im_categories set category_description='subject with residence in CZ' where category_id = 50603;
update im_categories set category_description='subject with resindence out of CZ and out of EU' where category_id = 50604;
update im_categories set aux_string2='CELNICE' where category_id = 50600;
update im_categories set aux_string2='EU_NEREG' where category_id = 50601;
update im_categories set aux_string2='EU_REG' where category_id = 50602;
update im_categories set aux_string2='TUZEMSKO' where category_id = 50603;
update im_categories set aux_string2='ZAHRANICI' where category_id = 50604;
--------------------------------------------------------------------------------
-- Navision Customer Posting Group --
--------------------------------------------------------------------------------
SELECT im_category_new(50700,'EMPLOYEE','Navision Customer Posting Group');
SELECT im_category_new(50701,'FORWARD','Navision Customer Posting Group');
SELECT im_category_new(50702,'MIT_CN_JV','Navision Customer Posting Group');
SELECT im_category_new(50703,'MIT_CN_WOF','Navision Customer Posting Group');
SELECT im_category_new(50704,'MIT_CZ','Navision Customer Posting Group');
SELECT im_category_new(50705,'MIT_HU','Navision Customer Posting Group');
SELECT im_category_new(50706,'MIT_SK','Navision Customer Posting Group');
SELECT im_category_new(50707,'MIT_USA','Navision Customer Posting Group');
SELECT im_category_new(50708,'MT_CZ','Navision Customer Posting Group');
SELECT im_category_new(50709,'OTHER_CN','Navision Customer Posting Group');
SELECT im_category_new(50710,'OTHER_CZ','Navision Customer Posting Group');
SELECT im_category_new(50711,'OTHER_FOR','Navision Customer Posting Group');
SELECT im_category_new(50712,'OTHER_HU','Navision Customer Posting Group');
SELECT im_category_new(50713,'OTHER_SK','Navision Customer Posting Group');
SELECT im_category_new(50714,'OTHER_USA','Navision Customer Posting Group');
SELECT im_category_new(50715,'SMENKA CZK','Navision Customer Posting Group');
SELECT im_category_new(50716,'SMENKA EUR','Navision Customer Posting Group');
SELECT im_category_new(50717,'SMENKA USD','Navision Customer Posting Group');
update im_categories set aux_string2='EMPLOYEE' where category_id = 50700;
update im_categories set aux_string2='FORWARD' where category_id = 50701;
update im_categories set aux_string2='MIT_CN_JV' where category_id = 50702;
update im_categories set aux_string2='MIT_CN_WOF' where category_id = 50703;
update im_categories set aux_string2='MIT_CZ' where category_id = 50704;
update im_categories set aux_string2='MIT_HU' where category_id = 50705;
update im_categories set aux_string2='MIT_SK' where category_id = 50706;
update im_categories set aux_string2='MIT_USA' where category_id = 50707;
update im_categories set aux_string2='MT_CZ' where category_id = 50708;
update im_categories set aux_string2='OTHER_CN' where category_id = 50709;
update im_categories set aux_string2='OTHER_CZ' where category_id = 50710;
update im_categories set aux_string2='OTHER_FOR' where category_id = 50711;
update im_categories set aux_string2='OTHER_HU' where category_id = 50712;
update im_categories set aux_string2='OTHER_SK' where category_id = 50713;
update im_categories set aux_string2='OTHER_USA' where category_id = 50714;
update im_categories set aux_string2='SMENKA CZK' where category_id = 50715;
update im_categories set aux_string2='SMENKA EUR' where category_id = 50716;
update im_categories set aux_string2='SMENKA USD' where category_id = 50717;
update im_categories set category_description='Employees' where category_id = 50700;
update im_categories set category_description='Forwardy' where category_id = 50701;
update im_categories set category_description='CN JV' where category_id = 50702;
update im_categories set category_description='CN WOFE' where category_id = 50703;
update im_categories set category_description='IT' where category_id = 50704;
update im_categories set category_description='HU' where category_id = 50705;
update im_categories set category_description='SK' where category_id = 50706;
update im_categories set category_description='USA' where category_id = 50707;
update im_categories set category_description='Translations (CZ)' where category_id = 50708;
update im_categories set category_description='Other CN customers' where category_id = 50709;
update im_categories set category_description='Other CZ customers' where category_id = 50710;
update im_categories set category_description='Other customers' where category_id = 50711;
update im_categories set category_description='Other HU customers' where category_id = 50712;
update im_categories set category_description='Other SK customers' where category_id = 50713;
update im_categories set category_description='US customers of MIT US (valid for MIT US only)' where category_id = 50714;
update im_categories set category_description='Bill of Exchange CZK' where category_id = 50715;
update im_categories set category_description='Bill of Exchange EUR' where category_id = 50716;
update im_categories set category_description='Bill of Exchange USD' where category_id = 50717;
--------------------------------------------------------------------------------
-- Navision Vendor Posting Group --
--------------------------------------------------------------------------------
SELECT im_category_new(50800,'CN_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50801,'CN_OST','Navision Vendor Posting Group');
SELECT im_category_new(50802,'CZ_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50803,'CZ_OST','Navision Vendor Posting Group');
SELECT im_category_new(50804,'EMPLOYEE','Navision Vendor Posting Group');
SELECT im_category_new(50805,'HU_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50806,'HU_OST','Navision Vendor Posting Group');
SELECT im_category_new(50807,'MIT_CN_JV','Navision Vendor Posting Group');
SELECT im_category_new(50808,'MIT_CN_WOF','Navision Vendor Posting Group');
SELECT im_category_new(50809,'MIT_CZ','Navision Vendor Posting Group');
SELECT im_category_new(50810,'MIT_HU','Navision Vendor Posting Group');
SELECT im_category_new(50811,'MIT_SK','Navision Vendor Posting Group');
SELECT im_category_new(50812,'MIT_US','Navision Vendor Posting Group');
SELECT im_category_new(50813,'MT_CZ','Navision Vendor Posting Group');
SELECT im_category_new(50814,'SK_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50815,'SK_OST','Navision Vendor Posting Group');
SELECT im_category_new(50816,'US_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50817,'US_OST','Navision Vendor Posting Group');
SELECT im_category_new(50818,'ZAH_LOK','Navision Vendor Posting Group');
SELECT im_category_new(50819,'ZAH_OST','Navision Vendor Posting Group');
update im_categories set aux_string2='CN_LOK' where category_id = 50800;
update im_categories set aux_string2='CN_OST' where category_id = 50801;
update im_categories set aux_string2='CZ_LOK' where category_id = 50802;
update im_categories set aux_string2='CZ_OST' where category_id = 50803;
update im_categories set aux_string2='EMPLOYEE' where category_id = 50804;
update im_categories set aux_string2='HU_LOK' where category_id = 50805;
update im_categories set aux_string2='HU_OST' where category_id = 50806;
update im_categories set aux_string2='MIT_CN_JV' where category_id = 50807;
update im_categories set aux_string2='MIT_CN_WOF' where category_id = 50808;
update im_categories set aux_string2='MIT_CZ' where category_id = 50809;
update im_categories set aux_string2='MIT_HU' where category_id = 50810;
update im_categories set aux_string2='MIT_SK' where category_id = 50811;
update im_categories set aux_string2='MIT_US' where category_id = 50812;
update im_categories set aux_string2='MT_CZ' where category_id = 50813;
update im_categories set aux_string2='SK_LOK' where category_id = 50814;
update im_categories set aux_string2='SK_OST' where category_id = 50815;
update im_categories set aux_string2='US_LOK' where category_id = 50816;
update im_categories set aux_string2='US_OST' where category_id = 50817;
update im_categories set aux_string2='ZAH_LOK' where category_id = 50818;
update im_categories set aux_string2='ZAH_OST' where category_id = 50819;
update im_categories set category_description='Localization CN' where category_id = 50800;
update im_categories set category_description='Other CN' where category_id = 50801;
update im_categories set category_description='Localization CZ' where category_id = 50802;
update im_categories set category_description='Other CZ' where category_id = 50803;
update im_categories set category_description='Employees' where category_id = 50804;
update im_categories set category_description='Localization HU' where category_id = 50805;
update im_categories set category_description='Other HU' where category_id = 50806;
update im_categories set category_description='CN JV' where category_id = 50807;
update im_categories set category_description='CN WOFE' where category_id = 50808;
update im_categories set category_description='IT' where category_id = 50809;
update im_categories set category_description='HU' where category_id = 50810;
update im_categories set category_description='SK' where category_id = 50811;
update im_categories set category_description='US' where category_id = 50812;
update im_categories set category_description='Translations (CZ)' where category_id = 50813;
update im_categories set category_description='Localization SK' where category_id = 50814;
update im_categories set category_description='Other SK' where category_id = 50815;
update im_categories set category_description='Localization US' where category_id = 50816;
update im_categories set category_description='Other US' where category_id = 50817;
update im_categories set category_description='Localization' where category_id = 50818;
update im_categories set category_description='Non localization' where category_id = 50819;
--------------------------------------------------------------------------------
--50900-50929
--PAYMENT METHOD
--------------------------------------------------------------------------------
SELECT im_category_new(50900,'EMPTY','Navision Payment Method');
SELECT im_category_new(50901,'CHEQUE','Navision Payment Method');
SELECT im_category_new(50902,'TRANSFER','Navision Payment Method');
SELECT im_category_new(50903,'CASH','Navision Payment Method');
update im_categories set aux_string2='' where category_id = 50900;
update im_categories set aux_string2='CHEQUE' where category_id = 50901;
update im_categories set aux_string2='TRANSFER' where category_id = 50902;
update im_categories set aux_string2='CASH' where category_id = 50903;
--------------------------------------------------------------------------------
--50930-50999
--Payment Terms
--------------------------------------------------------------------------------
SELECT im_category_new(50930, '10_D', 'Navision Payment Terms');
SELECT im_category_new(50931, '14_D', 'Navision Payment Terms');
SELECT im_category_new(50932, '14_D_D', 'Navision Payment Terms');
SELECT im_category_new(50933, '20_D', 'Navision Payment Terms');
SELECT im_category_new(50934, '21_D', 'Navision Payment Terms');
SELECT im_category_new(50935, '30_D', 'Navision Payment Terms');
SELECT im_category_new(50936, '30_D_D', 'Navision Payment Terms');
SELECT im_category_new(50937, '45_D', 'Navision Payment Terms');
SELECT im_category_new(50938, '45_D_D', 'Navision Payment Terms');
SELECT im_category_new(50939, '60_D', 'Navision Payment Terms');
SELECT im_category_new(50940, '60_D_D', 'Navision Payment Terms');
SELECT im_category_new(50941, '75_D', 'Navision Payment Terms');
SELECT im_category_new(50942, '90_D', 'Navision Payment Terms');
SELECT im_category_new(50943, 'HALF_YEAR', 'Navision Payment Terms');
SELECT im_category_new(50944, '2_Y', 'Navision Payment Terms');
SELECT im_category_new(50945, 'IMM', 'Navision Payment Terms');
SELECT im_category_new(50946, 'UNDO', 'Navision Payment Terms');
SELECT im_category_new(50947, 'UNPAID', 'Navision Payment Terms');
update im_categories set aux_string2='10_D' where category_id = 50930;
update im_categories set aux_string2='14_D' where category_id = 50931;
update im_categories set aux_string2='14_D_D' where category_id = 50932;
update im_categories set aux_string2='20_D' where category_id = 50933;
update im_categories set aux_string2='21_D' where category_id = 50934;
update im_categories set aux_string2='30_D' where category_id = 50935;
update im_categories set aux_string2='30_D_D' where category_id = 50936;
update im_categories set aux_string2='45_D' where category_id = 50937;
update im_categories set aux_string2='45_D_D' where category_id = 50938;
update im_categories set aux_string2='60_D' where category_id = 50939;
update im_categories set aux_string2='60_D_D' where category_id = 50940;
update im_categories set aux_string2='75_D' where category_id = 50941;
update im_categories set aux_string2='90_D' where category_id = 50942;
update im_categories set aux_string2='HALF_YEAR' where category_id = 50943;
update im_categories set aux_string2='2_Y' where category_id = 50944;
update im_categories set aux_string2='IMM' where category_id = 50945;
update im_categories set aux_string2='UNDO' where category_id = 50946;
update im_categories set aux_string2='NEPLATIT' where category_id = 50947;
-- 51000-59999 - Reserved for Navision
--------------------------------------------------------------------------------
-- Create TaskTypes with Activity Codes
--------------------------------------------------------------------------------
create or replace function inline_0 ()
returns integer as $body$
declare
v_name varchar;
v_task_type_name varchar;
v_category_id integer;
v_reuse_band_id integer;
row RECORD;
begin
FOR row IN
select *
from nav."Item"
LOOP
RAISE NOTICE 'Import Activity Codes: % - %', row."No_", row."Activity Code";
v_task_type_name := row."Activity Group" || '-' || row."Sub-Activity";
v_name := row."Activity Group" || '-' || row."Sub-Activity" || ' ' || row."No_" || ' (' || row."Description" || ')';
-- Check if the category already exists
select category_id into v_category_id from im_categories
where category = v_name and category_type = 'Intranet Translation Task Type';
-- Only create the category if it was not there before.
IF v_category_id is null THEN
v_category_id := im_category_new (nextval('im_categories_seq')::integer, v_name, 'Intranet Translation Task Type');
PERFORM im_category_hierarchy_new(v_name, v_task_type_name, 'Intranet Translation Task Type');
PERFORM im_category_hierarchy_new(v_name, row."Activity Group", 'Intranet Translation Task Type');
update im_categories set
aux_string1 = 'trans',
aux_string2 = category_id
where category = v_name and
category_type = 'Intranet Translation Task Type';
-- Reuse Band
select category_id into v_reuse_band_id from im_categories
where aux_string2 = row."Price Type" and category_type = 'Navision Reuse Band';
insert into im_categories_trans_task_ext (
category_id,
activity_code, activity_group, sub_activity,
reuse_band_id, difficulty, description,
without_pm_fee_p, pm_fee_p
) values (
v_category_id,
row."No_"::integer, row."Activity Group", row."Sub-Activity",
v_reuse_band_id, row."Difficulty", row."Description",
row."Without PM fee", row."PM fee"
);
END IF;
END LOOP;
return 0;
end; $body$ language 'plpgsql';
select inline_0 ();
drop function inline_0 ();
---------------------------------------------------------------------------------
-- Navision Relationship between contacts and companies
---------------------------------------------------------------------------------
-- Adding the NAV "Contact Business Relationship" to acs_rels
drop function nav_import_contact_business_relationships();
create or replace function nav_import_contact_business_relationships ()
returns integer as $BODY$
DECLARE
row RECORD;
v_user_id integer;
v_company_id integer;
v_exists_p integer;
v_group_id integer;
BEGIN
FOR row IN
select *
from nav."Contact Business Relation"
order by "Contact No_", "No_"
LOOP
--------------------------------------------------------------------
-- Lookup user
select min(person_id) into v_user_id from persons
where nav_contact_nr ~ row."Contact No_";
IF v_user_id is null THEN
RAISE WARNING 'Did not find user=%', row."Contact No_";
continue;
END IF;
--------------------------------------------------------------------
-- Lookup company
select company_id into v_company_id from im_companies
where nav_vendor_nr = row."No_";
IF v_company_id is null THEN
select company_id into v_company_id from im_companies
where nav_customer_nr = row."No_";
END IF;
IF v_company_id is null THEN
RAISE WARNING 'Did not find company=%', row."No_";
continue;
END IF;
--------------------------------------------------------------------
-- Create the relationship between company and contact
RAISE NOTICE 'Making % member of %', im_name_from_user_id(v_user_id), v_company_id;
PERFORM im_biz_object_member__new (
null,
'im_biz_object_member',
v_company_id, -- the business object
v_user_id, -- the person related to biz_object
1300, -- role: Full Member
null, -- percentage
null, -- creation user
'0.0.0.0' -- creation IP
);
--------------------------------------------------------------------
-- Make the contact the "primary_contact_id" of the company
-- and set the nav_vendor_nr on the person
update im_companies
set primary_contact_id = v_user_id
where company_id = v_company_id;
update persons
set nav_primary_contact_for_company_id = v_company_id
where person_id = v_user_id;
--------------------------------------------------------------------
-- Make the user member of the groups Providers or Customers
IF 'Z' = substring(row."Business Relation Code" for 1) THEN
RAISE NOTICE 'User is a Customer: %', row."Contact No_";
select group_id into v_group_id from groups
where group_name = 'Customers';
ELSE
RAISE NOTICE 'User is a Provider: %', row."Contact No_";
select group_id into v_group_id from groups
where group_name = 'Freelancers';
END IF;
-- Check if the user is already a member of that group
select count(*)
into v_exists_p
from acs_rels r
where rel_type = 'membership_rel' and
object_id_one = v_group_id and
object_id_two = v_user_id;
-- Add the user to the group if he is not a member already.
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding User to Group: %', row."Contact No_";
PERFORM membership_rel__new(
null, -- rel_id, default null
'membership_rel', -- object_type, default membership_rel
v_group_id, -- object_id_one
v_user_id, -- object_id_two
'approved', -- new__member_state, default approved
null, -- creation_user, default null
'0.0.0.0' -- creation_ip, default null
);
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_contact_business_relationships ();
---------------------------------------------------------------------------------
-- Navision Services per Contacts
---------------------------------------------------------------------------------
-- Adding the services to the ]po[ Freelance Skill Database
drop function nav_import_contact_services();
create or replace function nav_import_contact_services ()
returns integer as $BODY$
DECLARE
row RECORD;
v_user_id integer;
v_service_id integer;
v_exists_p integer;
v_source_language varchar;
v_source_language_id integer;
v_target_language varchar;
v_target_language_id integer;
v_skill_type_id integer;
v_skill_type_category varchar;
BEGIN
FOR row IN
select *
from nav."Contact Service"
where "Service Name" = 'Translation'
order by "Contact No_"
LOOP
-- Lookup user
select person_id into v_user_id from persons
where nav_contact_nr ~ row."Contact No_";
IF v_user_id is null THEN
RAISE WARNING 'Did not find user=%', row."Contact No_";
continue;
END IF;
-- Transform source and target languages into ]po[ convention
v_source_language := lower(substring(row."Source Language" from '^(..)'));
v_source_language_id := import_cat(v_source_language, 'Intranet Translation Language');
IF v_source_language_id is null THEN
RAISE WARNING 'Did not find source language %', v_source_language;
continue;
END IF;
v_target_language := lower(substring(row."Target Language" from '^(..)'));
IF length(row."Target Language") > 2 THEN
v_target_language := v_target_language || '_' || lower(substring(row."Target Language" from '^...(..)'));
END IF;
v_target_language_id := import_cat(v_target_language, 'Intranet Translation Language');
IF v_target_language_id is null THEN
RAISE WARNING 'Did not find target language %', v_target_language;
continue;
END IF;
-- Add Source Language
v_skill_type_id := 2000;
select count(*) into v_exists_p from im_freelance_skills
where user_id = v_user_id and skill_id = v_source_language_id;
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding Source Language: User=%, Skill=%', row."Contact No_", v_source_language;
insert into im_freelance_skills (
user_id, skill_id, skill_type_id, claimed_experience_id, confirmed_experience_id
) values (
v_user_id, v_source_language_id, v_skill_type_id, 2203, 2203
);
END IF;
-- Add Target Language
v_skill_type_id := 2002;
select count(*) into v_exists_p from im_freelance_skills
where user_id = v_user_id and skill_id = v_target_language_id;
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding Target Language: User=%, Skill=%', row."Contact No_", v_target_language;
insert into im_freelance_skills (
user_id, skill_id, skill_type_id, claimed_experience_id, confirmed_experience_id
) values (
v_user_id, v_target_language_id, v_skill_type_id, 2203, 2203
);
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_contact_services ();
---------------------------------------------------------------------------------
-- Navision Tools per Contacts
---------------------------------------------------------------------------------
SELECT im_category_new (nextval('im_categories_seq')::integer,'Atril Deja Vu','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Trados ExtraTerm','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Trados MultiTerm','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Trados TagEditor','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Trados Workbench','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'WordFast','Intranet TM Tool');
SELECT im_category_new (nextval('im_categories_seq')::integer,'Star Transit','Intranet TM Tool');
drop function nav_import_contact_tool_categories();
create or replace function nav_import_contact_tool_categories ()
returns integer as $BODY$
DECLARE
row RECORD;
v_exists_p integer;
BEGIN
FOR row IN
select distinct "Tools Name"
from nav."Contact Tools"
LOOP
select count(*) into v_exists_p from im_categories
where category = row."Tools Name" and category_type in ('Intranet TM Tool', 'Intranet LOC Tool');
IF v_exists_p = 0 THEN
PERFORM im_category_new (nextval('im_categories_seq')::integer,row."Tools Name",'Intranet LOC Tool');
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_contact_tool_categories ();
-- Adding the tools the ]po[ Freelance Skill Database
drop function nav_import_contact_tools();
create or replace function nav_import_contact_tools ()
returns integer as $BODY$
DECLARE
row RECORD;
v_user_id integer;
v_tool_id integer;
v_exists_p integer;
v_skill_type_id integer;
v_skill_type_category varchar;
BEGIN
FOR row IN
select *
from nav."Contact Tools"
order by "Contact No_"
LOOP
-- Lookup user
select person_id into v_user_id from persons
where nav_contact_nr ~ row."Contact No_";
IF v_user_id is null THEN
RAISE WARNING 'Did not find user=%', row."Contact No_";
continue;
END IF;
-- Lookup Tool
select category_id into v_tool_id from im_categories
where category = row."Tools Name" and category_type in ('Intranet TM Tool', 'Intranet LOC Tool');
IF v_tool_id is null THEN
RAISE WARNING 'Did not find tool=%', row."Tools Name";
continue;
END IF;
-- Determine the skill type
select category_type into v_skill_type_category from im_categories where category_id = v_tool_id;
select category_id into v_skill_type_id from im_categories
where category_type = 'Intranet Skill Type' and
category_description = v_skill_type_category;
IF v_skill_type_id is null THEN
RAISE WARNING 'Did not find skill type=%, tool=%', v_skill_type_category, v_tool_id;
continue;
END IF;
-- Check if the entry was already there
select count(*) into v_exists_p from im_freelance_skills
where user_id = v_user_id and skill_id = v_tool_id;
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding Skill: User=%, Skill=%', row."Contact No_", row."Tools Name";
insert into im_freelance_skills (
user_id, skill_id, skill_type_id, claimed_experience_id, confirmed_experience_id
) values (
v_user_id, v_tool_id, v_skill_type_id, 2203, 2203
);
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_contact_tools ();
---------------------------------------------------------------------------------
-- Navision Contacts
---------------------------------------------------------------------------------
-- convert empty strings in NULL values to allow coalesce() to work
--
ALTER TABLE nav."Contact" ALTER COLUMN "First Name" DROP NOT NULL;
update nav."Contact" set "First Name" = null where "First Name" = '';
--
ALTER TABLE nav."Contact" ALTER COLUMN "Surname" DROP NOT NULL;
update nav."Contact" set "Surname" = null where "Surname" = '';
-- Actually create new users by going through the nav."Contact" table
-- line by line and inserting the user into the DB.
--
drop function nav_import_contact();
create or replace function nav_import_contact ()
returns integer as $BODY$
DECLARE
row RECORD;
v_user_id integer;
v_string varchar;
v_exists_p integer;
v_username varchar;
v_group_id integer;
v_email varchar;
v_email_domain varchar;
v_email_name varchar;
v_customer_group_id integer;
v_first_names varchar;
v_last_name varchar;
BEGIN
FOR row IN
select *,
coalesce("First Name", substring("Name" from '^([^ ]+)')) as first_names,
coalesce("Surname", substring("Name" from '^[^ ]+ (.*)$')) as last_name
from nav."Contact"
order by "No_"
LOOP
v_email := substring(lower(trim(row."E-Mail")) from '[a-zA-Z0-9_\_\.]+\@[a-zA-Z0-9_\_\.]+');
v_email_domain := substring(v_email from '@(.*)');
v_email_name := substring(v_email from '(.*)@');
v_email := v_email_name || '@' || v_email_domain;
-- Transform first and last name
v_first_names := row.first_names;
v_last_name := row.last_name;
-- Accept empty last names for company contacts. Replace by first name
IF row."Type" = 0 and (v_last_name is null OR v_last_name = '') THEN
v_last_name := v_first_names;
END IF;
-- Accept empty email for company contacts.
IF row."Type" = 0 and (v_email is null OR v_email = '') THEN
v_email = v_first_names ||'.'||v_last_name||'@nowhere.com';
END IF;
RAISE NOTICE 'Processing User: first=%, last=%, email=%, no=%, company_no=%',
v_first_names, v_last_name, v_email, row."No_", row."Company No_";
IF v_first_names is null OR v_first_names = '' THEN
RAISE NOTICE 'Skipping User (empty first_names): % % (%)', v_first_names, v_last_name, v_email;
continue;
END IF;
IF v_last_name is null OR v_last_name = '' THEN
RAISE NOTICE 'Skipping User (empty last_name): % % (%)', v_first_names, v_last_name, v_email;
continue;
END IF;
IF v_email is null OR v_email = '' THEN
RAISE NOTICE 'Skipping User (empty email): % % (%)', v_first_names, v_last_name, v_email;
continue;
END IF;
-- Check if the user already exists / has been imported already
-- during the last run of this script. PrimKey is first_names+last_name.
-- Check 1. ContactNo_, 2. Email and 3. Name
select min(person_id) into v_user_id from persons
where nav_contact_nr ~ row."No_";
IF v_user_id is null THEN
select min(party_id) into v_user_id from parties p
where trim(lower(p.email)) = trim(lower(v_email));
END IF;
IF v_user_id is null THEN
select min(person_id) into v_user_id from persons p
where (trim(lower(p.first_names)) = trim(lower(v_first_names)) and
trim(lower(p.last_name)) = trim(lower(v_last_name)));
END IF;
-- Create a new user if the user wasnt there
IF v_user_id is null THEN
RAISE NOTICE 'Insert User: % % (%)', v_first_names, v_last_name, v_email;
-- Create a default username
v_username := v_email;
v_user_id := acs__add_user(
null, 'user', now(), 0, '0.0.0.0',
null, v_username, v_email, null,
v_first_names, v_last_name,
'hashed_password', 'salt',
v_username, 't', 'approved'
);
INSERT INTO users_contact (user_id) VALUES (v_user_id);
INSERT INTO im_employees (employee_id) VALUES (v_user_id);
INSERT INTO im_freelancers (user_id) VALUES (v_user_id);
ELSE
SELECT username into v_username from users
where user_id = v_user_id;
END IF;
-----------------------------------------------------------------
-- This is the main part of the import process, this part is
-- executed for every line in the import_users table every time
-- this script is executed.
-- Update the users information, no matter whether its a new or
-- an already existing user.
RAISE NOTICE 'Update User: email=%, id=%', v_email, v_user_id;
update users_contact set
wa_line1 = row."Address",
wa_line2 = row."Address 2",
wa_city = row."City",
wa_postal_code = row."Post Code",
work_phone = row."Phone No_",
fax = row."Telex No_",
pager = row."Pager"
where user_id = v_user_id;
update users set
username = v_username
where user_id = v_user_id;
update persons set
first_names = v_first_names,
last_name = v_last_name,
nav_middle_name = row."Middle Name",
nav_skype = row."Skype"
where person_id = v_user_id;
update parties set
url = row."Home Page"
where party_id = v_user_id;
update im_employees set
job_title = row."Job Title"
where employee_id = v_user_id;
----------------------------------------------------------------
-- Update persons.nav_contact_nr. It's a space separated list of NAV contact_nrs
select coalesce(nav_contact_nr, '') into v_string
from persons where person_id = v_user_id;
IF v_string !~ row."No_" THEN
IF v_string is null or v_string = ''
THEN v_string := row."No_";
ELSE v_string := v_string || ' ' || row."No_";
END IF;
update persons
set nav_contact_nr = v_string
where person_id = v_user_id;
END IF;
----------------------------------------------------------------
-- Make the new user a member of group Customers or Providers
-- select out the ID of the Customers group
IF 'Z' = substring(row."No_" for 1) THEN
RAISE NOTICE 'User is a Customer: %', v_email;
select group_id into v_customer_group_id from groups
where group_name = 'Customers';
ELSE
RAISE NOTICE 'User is a Provider: %', v_email;
select group_id into v_customer_group_id from groups
where group_name = 'Freelancers';
END IF;
-- Check if the user is already a member of that group
select count(*)
into v_exists_p
from acs_rels r
where rel_type = 'membership_rel' and
object_id_one = v_customer_group_id and
object_id_two = v_user_id;
-- Add the user to the group if he is not a member already.
IF false AND v_exists_p = 0 THEN
RAISE NOTICE 'Adding User to Group: %', v_email;
PERFORM membership_rel__new(
null, -- rel_id, default null
'membership_rel', -- object_type, default membership_rel
v_customer_group_id, -- object_id_one
v_user_id, -- object_id_two
'approved', -- new__member_state, default approved
null, -- creation_user, default null
'0.0.0.0' -- creation_ip, default null
);
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
-- select nav_import_contact ();
-- Actually create new users by going through the nav."Contact" table
-- line by line and inserting the user into the DB.
--
drop function nav_import_contact_memberships();
create or replace function nav_import_contact_memberships ()
returns integer as $BODY$
DECLARE
row RECORD;
v_user_id integer;
v_company_id integer;
BEGIN
FOR row IN
select *,
coalesce("First Name", substring("Name" from '^([^ ]+)')) as first_names,
coalesce("Surname", substring("Name" from '^[^ ]+ (.*)$')) as last_name
from nav."Contact"
where " Company No_" = 'ZL00140'
order by "No_"
LOOP
RAISE NOTICE 'Contact Memberships: Processing User: name=%, email=%, no=%, company_no=%',
row."Name", row."E-Mail", row."No_", row."Company No_";
--------------------------------------------------------------------
-- Interpret the "Company No_".
-- This key references a "Contact" of type 0=Company, which in turn
-- is connected to the "Vendor" via "Contact Business Relation".
v_user_id := (select min(person_id) from persons where nav_contact_nr ~ row."No_");
IF v_user_id is null THEN
RAISE WARNING 'Contact Memberships: Did not find user no=%, name=%', row."No_", row."Name";
continue;
END IF;
-- Check for the type of contact:
-- 0=Company
-- 1=User
IF 0 = row."Type" THEN
RAISE NOTICE 'Contact Memberships: Found a "company": no=%, name=%', row."No_", row."Name";
-- Type=0: A company
select min(company_id) into v_company_id
from im_companies
where (nav_customer_nr in (select "No_" from nav."Contact Business Relation" where "Contact No_" = row."No_") OR
nav_vendor_nr in (select "No_" from nav."Contact Business Relation" where "Contact No_" = row."No_"))
;
ELSE
RAISE NOTICE 'Contact Memberships: Found a "person": no=%, name=%', row."No_", row."Name";
-- Type=1: A user that represents a company
v_company_id := (select min(company_id) from im_companies where nav_vendor_nr in (
select "No_" from nav."Vendor" where "No_" in (
select "No_" from nav."Contact Business Relation" where "Contact No_" in (
select "No_" from nav."Contact" where "No_" = row."Company No_"
)
)
));
END IF;
IF v_company_id is null THEN
RAISE WARNING 'Contact Memberships: Did not find company no=%', row."Company No_";
continue;
END IF;
RAISE NOTICE 'Contact Memberships: Adding User % to Company % for %', row."E-Mail", v_company_id, row."Company No_";
PERFORM im_biz_object_member__new (
null,
'im_biz_object_member',
v_company_id, -- the business object
v_user_id, -- the person related to biz_object
1300, -- role: Full Member
null, -- percentage
null, -- creation user
'0.0.0.0' -- creation IP
);
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_contact_memberships ();
---------------------------------------------------------------------------------
-- Navision Customers
---------------------------------------------------------------------------------
drop function nav_import_customer ();
create or replace function nav_import_customer ()
returns integer as '
DECLARE
row RECORD;
v_company_id integer;
v_office_id integer;
v_exists_p integer;
v_company_name varchar;
v_office_name varchar;
v_office_status_id integer;
v_office_type_id integer;
v_company_status_id integer;
v_company_type_id integer;
v_duplicate_p integer;
v_payment_days integer;
v_primary_contact_id integer;
v_note varchar;
v_parent_company_id integer;
v_nav_project_manager_id integer;
v_nav_account_manager_id integer;
v_country_code varchar;
v_counter integer;
BEGIN
v_counter := 0;
FOR row IN
select *
from nav."Customer"
order by "No_"
LOOP
v_counter := v_counter + 1;
------------------------ Name Fiddling ---------------------------------------------
-- Process the name in order to deal with duplicates.
-- ]po[ doesnt allow for duplicate company or office names.
-- By default just use the standard name
v_company_name := row."Name";
-- There are 28 Microsofts with one ]po[ customer...
-- However, "Search Name" contains a reasonable division name then.
IF v_company_name = ''Microsoft'' THEN
v_company_name := v_company_name || '' '' || row."Search Name";
END IF;
-- Add the "Division" if available.
select count(*) into v_duplicate_p from nav."Customer"
where "Name" = v_company_name;
IF v_duplicate_p > 1 THEN
IF row."Division" is not null AND row."Division" <> '''' THEN
v_company_name := v_company_name || '' '' || row."Division";
END IF;
END IF;
-- Add the "Name 2" if available.
select count(*) into v_duplicate_p from nav."Customer"
where "Name" = v_company_name;
IF v_duplicate_p > 1 THEN
IF row."Name 2" is not null AND row."Name 2" <> '''' THEN
v_company_name := v_company_name || '' '' || row."Name 2";
END IF;
END IF;
-- Add the customer no. if its still not unique within the NAV table
select count(*) into v_duplicate_p from nav."Customer"
where "Name" = v_company_name;
IF v_duplicate_p > 1 THEN
v_company_name := v_company_name || '' ('' || row."No_" || '')'';
END IF;
-- Add the customer no. also if the name is not unique in ]po[
select count(*) into v_duplicate_p from im_companies
where company_name = v_company_name;
IF v_duplicate_p > 0 THEN
v_company_name := v_company_name || '' ('' || row."No_" || '')'';
END IF;
-- The office name is the company name + "Main Office".
v_office_name := v_company_name || '' Main Office'';
------------------------ Default Values ---------------------------------------------
RAISE NOTICE ''Processing Customer %: %'', v_counter, v_company_name;
-- Default Status for company and office
v_office_status_id := import_cat(''Active'', ''Intranet Office Status'');
v_office_type_id := import_cat(''Main Office'', ''Intranet Office Type'');
v_company_status_id := import_cat(''Active'', ''Intranet Company Status'');
v_company_type_id := import_cat(''Customer'', ''Intranet Company Type'');
------------------------ New Office and Comany ------------------------------------
-- Check if the office is already there
select office_id into v_office_id from im_offices o
where o.nav_customer_nr = row."No_";
-- Create a new office first, because the im_company.main_office_id is not null
IF v_office_id is null THEN
RAISE NOTICE ''Insert Office: %'', v_office_name;
v_office_id := im_office__new (
NULL, ''im_office'', now()::date, 0, ''0.0.0.0'', null,
v_office_name, row."No_", v_office_type_id,
v_office_status_id, null
);
END IF;
-- Check if the company is already there
select company_id into v_company_id from im_companies c
where c.nav_customer_nr = row."No_";
IF v_company_id is null THEN
RAISE NOTICE ''Insert Company: %'', v_company_name;
v_company_id := im_company__new (
NULL, ''im_company'', now()::date, 0, ''0.0.0.0'', null,
v_company_name, row."No_",
v_office_id, v_company_type_id, v_company_status_id
);
END IF;
------------------------ Update Office and Comany----------------------------------
-- Conver Payment Terms Code
v_payment_days := NULL;
IF row."Payment Terms Code" = ''10_D'' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = ''14_D'' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = ''14_D_D'' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = ''20_D'' THEN v_payment_days := 20; END IF;
IF row."Payment Terms Code" = ''21_D'' THEN v_payment_days := 21; END IF;
IF row."Payment Terms Code" = ''30_D'' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = ''30_D_D'' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = ''45_D'' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = ''45_D_D'' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = ''60_D'' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = ''60_D_D'' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = ''75_D'' THEN v_payment_days := 75; END IF;
IF row."Payment Terms Code" = ''90_D'' THEN v_payment_days := 90; END IF;
IF row."Payment Terms Code" = ''HALF_YEAR'' THEN v_payment_days := 182; END IF;
IF row."Payment Terms Code" = ''2_Y'' THEN v_payment_days := 730; END IF;
IF row."Payment Terms Code" = ''IMM'' THEN v_payment_days := 0; END IF;
IF row."Payment Terms Code" = ''UNDO'' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = ''NEPLATIT'' THEN v_payment_days := 10000; END IF;
-- Add other fields to the "note" field
v_note := NULL;
IF row."Home Page" is not null AND row."Home Page" <> '''' THEN v_note := v_note || ''Home Page='' || row."Home Page" ||'', ''; END IF;
IF row."Registration No_" is not null AND row."Registration No_" <> '''' THEN v_note := v_note || ''Registration No_='' || row."Registration No_" ||'', ''; END IF;
IF row."Invoicing Conditions" is not null AND row."Invoicing Conditions" <> '''' THEN v_note := v_note || ''Invoicing Conditions='' || row."Invoicing Conditions" ||'', ''; END IF;
IF row."Contact Text" is not null AND row."Contact Text" <> '''' THEN v_note := v_note || ''Contact Text='' || row."Contact Text" ||'', ''; END IF;
IF row."Bank Account No_" is not null AND row."Bank Account No_" <> '''' THEN v_note := v_note || ''Bank Account No_='' || row."Bank Account No_" ||'', ''; END IF;
-- Primary contact
select person_id into v_primary_contact_id from persons
where nav_contact_nr = row."Primary Contact No_";
IF v_primary_contact_id is null THEN
IF row."E-Mail" is not null AND row."E-Mail" <> '''' THEN
v_primary_contact_id := lookup_user_email(row."E-Mail", ''nav_import_customer.im_company.primary_contact_id'');
END IF;
END IF;
-- Parent Company
select company_id into v_parent_company_id from im_companies
where nav_customer_nr = row."Mother Company";
-- Fix Country Codes
v_country_code := lower(row."Country_Region Code");
IF v_country_code = ''gb'' THEN v_country_code := ''uk''; END IF;
IF v_country_code = '''' THEN v_country_code := NULL; END IF;
RAISE NOTICE ''Update Office: %'', v_office_name;
update im_offices set
office_name = v_office_name,
office_status_id = v_office_status_id,
office_type_id = v_office_type_id,
phone = row."Phone No_",
fax = row."Fax No_",
address_line1 = row."Address",
address_line2 = row."Address 2",
address_city = row."City",
address_country_code = v_country_code,
address_postal_code = row."Post Code",
nav_customer_nr = row."No_"
where office_id = v_office_id;
RAISE NOTICE ''Update Company: %'', v_company_name;
update im_companies set
company_name = v_company_name,
main_office_id = v_office_id,
company_status_id = v_company_status_id,
company_type_id = v_company_type_id,
vat_number = row."VAT Registration No_",
default_payment_days = v_payment_days,
primary_contact_id = v_primary_contact_id,
accounting_contact_id = v_primary_contact_id,
parent_company_id = v_parent_company_id,
note = v_note,
nav_pm_percentage = row."PM %",
nav_customer_nr = row."No_",
nav_name2 = row."Name 2",
nav_gl_dimension_1 = row."Global Dimension 1 Code",
nav_customer_posting_group = lookup_category_id_from_aux_string2(row."Customer Posting Group", ''Navision Customer Posting Group''::varchar),
nav_blocked = row."Blocked",
nav_payment_method = lookup_category_id_from_aux_string2(row."Payment Method Code", ''Navision Payment Method''),
nav_payment_terms = lookup_category_id_from_aux_string2(row."Payment Terms Code",''Navision Payment Terms''),
nav_gen_posting_group = lookup_category_id_from_aux_string2(row."Gen_ Bus_ Posting Group", ''Navision General Posting Group''),
nav_vat_posting_group = lookup_category_id_from_aux_string2(row."VAT Bus_ Posting Group", ''Navision VAT Posting Group''),
nav_minimal_tariff_pm = row."Minimal Tariff for PM Rate",
nav_minimal_amount = row."Minimal Amount",
nav_division = row."Division",
nav_line_discount_percentage = row."Line Discount %",
nav_currency = row."Currency Code"
where company_id = v_company_id;
-- Project Manager: Establish a im_biz_object_member relationship
-- between the company and the project manager with as "Key Project".
select person_id into v_nav_project_manager_id from persons
where nav_employee_nr = row."Project Manager";
IF v_nav_project_manager_id is not null THEN
RAISE NOTICE ''Adding Project Manager for company: %'', v_company_name;
PERFORM im_biz_object_member__new (
null,
''im_biz_object_member'',
v_company_id, -- the business object
v_nav_project_manager_id, -- the person related to biz_object
1301, -- role: Key Account
null, -- percentage
null, -- creation user
''0.0.0.0'' -- creation IP
);
END IF;
-- Account Manager: Establish a im_biz_object_member relationship
-- between the company and the account manager with as "Key Account".
select person_id into v_nav_account_manager_id from persons
where nav_employee_nr = row."Account Manager";
IF v_nav_account_manager_id is not null THEN
RAISE NOTICE ''Adding Account Manager for company: %'', v_company_name;
PERFORM im_biz_object_member__new (
null,
''im_biz_object_member'',
v_company_id, -- the business object
v_nav_account_manager_id, -- the person related to biz_object
1302, -- role: Key Account
null, -- percentage
null, -- creation user
''0.0.0.0'' -- creation IP
);
END IF;
-- -------------------------- Important Fields to convert ------------------------------------
-- Invoice Disc_ Code - Skipped: Something to allow NAV users to import Excel sheets
-- Gen_ Bus_ Posting Group - Skipped: Related to VAT or account to which costs are accounted (during invoicing)
-- Customer Posting Group - same
-- Project Group - Not used in NAV
-- Administration Overhead % - Skpped
-- Allow Line Disc_ - Skipped at the moment
-- Bank Account No_ - Skipped. The Invoicing will be done in NAV
-- Global Dimension 1 Code: To which Customer Subsidary does the customer belong? Deferred at the moment.
-- Global Dimension 2 Code: To which Department does the customer belong? Deferred at the moment.
-- Amount
-- Application Method
-- Bill Customer
-- Bill-to Customer No_
-- Block Payment Tolerance
-- Blocked
-- Budgeted Amount
-- Chain Name
-- Collection Method
-- Combine Shipments
-- Contact
-- Copy Sell-to Addr_ to Qte From
-- Country_Region Code
-- County
-- Credit Limit (LCY)
-- Currency Code
-- Customer Disc_ Group
-- Customer MT
-- Customer Price Group
-- Default CP Activity
-- Fin_ Charge Terms Code
-- Fiscal Year from
-- Forward Customer
-- Generate PDF
-- IC Partner Code
-- Intercompany
-- Invoice Copies
-- Invoicing Conditions
-- Language Code
-- Last Date Modified
-- Last Statement No_
-- Line Discount %
-- Localization Segment
-- Location Code
-- Minimal Amount
-- Minimal Tariff for PM Rate
-- Mother Company
-- Name 2
-- No_ Series
-- Our Account No_
-- PM %
-- PM Fee Activity
-- Passage Contact Customer
-- Passage Contact Vendor
-- Payment Method Code
-- Phone No_
-- Picture
-- Place of Export
-- Post Code
-- Prepayment %
-- Price List Mandatory
-- Prices Including VAT
-- Primary Contact No_
-- Print Statements
-- Priority
-- Production Overhead %
-- Project No_ Mandatory
-- Registration No_
-- Reminder Terms Code
-- Reserve
-- Responsibility Center
-- Salesperson Code
-- Search Name
-- Service Zone Code
-- Shipment Method Code
-- Shipping Advice
-- Shipping Agent Code
-- Shipping Agent Service Code
-- Shipping Time
-- Statistics Group
-- Tax Area Code
-- Tax Liable
-- Telex Answer Back
-- Territory Code
-- VAT Bus_ Posting Group
-- VAT Registration No_
-- Web - Order Scenario
-- Web - Own Page Properties
-- Web - See Avaible Quantity
END LOOP;
RETURN 0;
END;' language 'plpgsql';
select nav_import_customer ();
---------------------------------------------------------------------------------
-- Navision Employees Table
---------------------------------------------------------------------------------
-- This script should be run AFTER importing users from Secure Access
-- Again, the Email ("MT Email") is used as the primary key to identify
-- a user.
drop function nav_import_employee();
create or replace function nav_import_employee ()
returns integer as $BODY$
DECLARE
row RECORD;
v_loginname varchar;
v_disabled_p integer;
v_count integer;
v_user_id integer;
v_user_login varchar;
v_email varchar;
v_email_domain varchar;
v_email_name varchar;
v_supervisor_id integer;
v_exists_p integer;
v_group_id integer;
BEGIN
FOR row IN
select *,
trim(lower(substring("MT Email" from '^([^@]+)'))) as windows_login
from nav."Employee"
order by "Personal ID"
LOOP
-- get and massage the email.
v_email := lower(row."MT Email");
v_email_domain := substring(v_email from '@(.*)');
v_email_name := substring(v_email from '(.*)@');
v_email := v_email_name || '@' || v_email_domain;
IF row."First Name" is null OR row."First Name" = '' THEN
RAISE NOTICE 'Skipping User (empty first_name): % % (%)', row."First Name", row."Surname", v_email;
continue;
END IF;
IF row."Surname" is null OR row."Surname" = '' THEN
RAISE NOTICE 'Skipping User (empty last_name): % % (%)', row."First Name", row."Surname", v_email;
continue;
END IF;
IF row."MT Email" is null OR row."MT Email" = '' or v_email is null or v_email = '' THEN
RAISE NOTICE 'Skipping User (empty email): % % (%)', row."First Name", row."Surname", v_email;
continue;
END IF;
-- Check if the user already exists.
v_user_id := null;
IF v_user_id is null THEN
select min(person_id) into v_user_id from persons
where nav_windows_login = row.windows_login;
END IF;
IF v_user_id is null THEN
select min(party_id) into v_user_id from parties
where email = v_email;
END IF;
IF v_user_id is null THEN
RAISE WARNING 'Did not find user email: % % (%)', row."First Name", row."Surname", v_email;
continue;
END IF;
RAISE NOTICE 'Processing User: First=%, Last=%, Email=%, OID=% => %', row."First Name", row."Surname", v_email, row."Personal ID", v_user_id;
-- Get the supervisor and make sure the user is not his own supervisor
select lower(person_id) into v_supervisor_id from persons
where nav_windows_login = trim(lower(row."Superior"));
IF v_supervisor_id = v_user_id THEN v_supervisor_id := NULL; END IF;
IF row."Superior" is not null and row."Superior" != '' and v_supervisor_id is null THEN
RAISE WARNING 'Did not find superior % for user %', row."Superior", row."MT Email";
END IF;
update persons set
nav_employee_nr = row."Personal ID"
where person_id = v_user_id;
update im_employees set
supervisor_id = v_supervisor_id
where employee_id = v_user_id;
-- Make the new user an Employee
select group_id into v_group_id from groups
where group_name = 'Employees';
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding: user=%, group=%', v_user_id, v_group_id;
PERFORM membership_rel__new(
null, -- rel_id, default null
'membership_rel', -- object_type, default membership_rel
v_group_id, -- object_id_one
v_user_id, -- object_id_two
'approved', -- new__member_state, default approved
null, -- creation_user, default null
'0.0.0.0' -- creation_ip, default null
);
ELSE
RAISE NOTICE 'User % already member of group Employees', v_user_id;
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_employee ();
---------------------------------------------------------------------------------
-- Navision Price List
---------------------------------------------------------------------------------
drop function nav_import_pricelist ();
create or replace function nav_import_pricelist ()
returns integer as $BODY$
DECLARE
row RECORD;
v_counter integer;
v_price_id integer;
v_valid_p integer;
v_debug integer;
v_company_id integer;
v_project_id integer;
v_task_type_id integer;
v_subject_area_id integer;
v_source_language_id integer;
v_target_language_id integer;
v_uom_id integer;
v_file_type_id integer;
v_difficulty integer;
v_final_customer_id integer;
v_reuse_band_id integer;
v_activity_nr integer;
v_valid_from timestamptz;
v_valid_to timestamptz;
v_currency varchar;
BEGIN
v_counter := 0;
v_debug := 0;
FOR row IN
select *
from nav."Activities Price List" l,
nav."Item" i
where l."Activity No_" = i."No_"
LOOP
v_counter := v_counter + 1;
-- Get the company for the entry. At the moment only customers...
select company_id into v_company_id from im_companies
where nav_customer_nr = row."Partner No_";
IF v_company_id is null THEN
select company_id into v_company_id from im_companies
where nav_vendor_nr = row."Partner No_";
END IF;
-- Get the project for the entries. Project can be NULL
select project_id into v_project_id from im_projects
where nav_project_nr = row."Project No_";
-- Get the final company for the entry.
select company_id into v_final_customer_id from im_companies
where nav_customer_nr = row."Customer No_";
-- Get the Trans Task Type from ActivityGroup/SubActivity
select category_id into v_task_type_id from im_categories
where category = row."Activity Group" || '-' || row."Sub-Activity" and category_type = 'Intranet Translation Task Type';
-- Get the "Segment" (=Subject Area)
select category_id into v_subject_area_id from im_categories
where category = row."Segment" and category_type = 'Intranet Translation Subject Area';
-- Get the "Source Language"
select category_id into v_source_language_id from im_categories
where lower(category) = replace(lower(row."Source Language"),' ','_') and category_type = 'Intranet Translation Language';
-- Get the "Target Language"
select category_id into v_target_language_id from im_categories
where lower(category) = replace(lower(row."Target Language"),' ','_') and category_type = 'Intranet Translation Language';
-- Get the "UoM"
select category_id into v_uom_id from im_categories
where aux_string2 = row."Unit of Measure" and category_type = 'Intranet UoM';
-- Reuse Band
select category_id into v_reuse_band_id from im_categories
where aux_string2 = row."Price Type" and category_type = 'Navision Reuse Band';
v_valid_p := 1;
IF row."Partner No_" is not null AND v_company_id is null THEN
RAISE WARNING 'pricelist: company_id is null';
v_valid_p := 0;
END IF;
IF v_uom_id is null THEN
RAISE WARNING 'pricelist: uom_id is null';
v_valid_p := 0;
END IF;
IF v_task_type_id is null THEN
RAISE WARNING 'pricelist: task_type_id is null';
v_valid_p := 0;
END IF;
IF row."Segment" is not null AND row."Segment" != '' AND v_subject_area_id is null THEN
RAISE WARNING 'pricelist: subject_area_id is null';
v_valid_p := 0;
END IF;
IF row."Source Language" is not null AND v_source_language_id is null THEN
RAISE WARNING 'pricelist: source_language_id is null';
v_valid_p := 0;
END IF;
IF row."Target Language" is not null AND row."Target Language" != 'N/A' AND v_target_language_id is null THEN
RAISE WARNING 'pricelist: target_language is null';
v_valid_p := 0;
END IF;
IF row."Price Type" is not null AND v_reuse_band_id is null THEN
RAISE WARNING 'pricelist: reuse_band is null';
v_valid_p := 0;
END IF;
v_valid_from = row."Valid From";
v_valid_to = row."Valid To";
IF v_valid_to < '2000-01-01'::date THEN v_valid_to := '2010_12_31'::date; END IF;
v_currency = coalesce(row."Currency", 'ALL');
v_difficulty = row."Difficulty";
v_activity_nr = row."Activity No_";
IF trim(v_currency) = '' THEN v_currency := 'ALL'; END IF;
select price_id into v_price_id from im_trans_prices
where company_id = v_company_id and
coalesce(uom_id,0) = coalesce(v_uom_id,0) and
coalesce(project_id,0) = coalesce(v_project_id,0) and
coalesce(task_type_id,0) = coalesce(v_task_type_id,0) and
coalesce(target_language_id,0) = coalesce(v_target_language_id,0) and
coalesce(source_language_id,0) = coalesce(v_source_language_id,0) and
coalesce(subject_area_id,0) = coalesce(v_subject_area_id,0) and
coalesce(file_type_id,0) = coalesce(v_file_type_id,0) and
coalesce(currency,'AFA') = coalesce(v_currency,'AFA') and
coalesce(reuse_band_id,0) = coalesce(v_reuse_band_id,0) and
coalesce(difficulty,-1) = coalesce(v_difficulty,-1) and
coalesce(activity_nr,-1) = coalesce(v_activity_nr,-1) and
coalesce(final_customer_id,0) = coalesce(v_final_customer_id,0) and
coalesce(valid_from,'2000-01-01') = coalesce(v_valid_from,'2000-01-01') and
coalesce(valid_through,'2000-01-01') = coalesce(v_valid_to,'2000-01-01');
IF v_debug > 0 OR v_valid_p = 0 THEN
RAISE NOTICE 'pricelist #%: %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%, %=%',
v_counter,
row."Partner No_", v_company_id,
row."Project No_", v_project_id,
row."Activity Group" || '-' || row."Sub-Activity", v_task_type_id,
row."Unit of Measure", v_uom_id,
row."Currency", v_currency,
row."Activity No_", v_activity_nr,
row."Difficulty", v_difficulty,
row."Price Type", v_reuse_band_id,
row."Segment", v_subject_area_id,
row."Source Language", v_source_language_id,
row."Target Language", v_target_language_id,
row."Customer No_", v_final_customer_id,
row."Valid From"::date, v_valid_from::date,
row."Valid To"::date, v_valid_to::date
;
END IF;
-- Insert the price line
IF v_valid_p > 0 THEN
IF v_price_id is null THEN
INSERT INTO im_trans_prices (
price_id, uom_id,
company_id, task_type_id,
activity_nr,
target_language_id, source_language_id,
subject_area_id, file_type_id,
project_id, reuse_band_id,
difficulty, final_customer_id,
currency, price,
min_price, note,
valid_from, valid_through
) VALUES (
nextval('im_trans_prices_seq'), v_uom_id,
v_company_id, v_task_type_id,
v_activity_nr,
v_target_language_id, v_source_language_id,
v_subject_area_id, v_file_type_id,
v_project_id, v_reuse_band_id,
v_difficulty, v_final_customer_id,
v_currency, row."Price",
NULL, NULL,
v_valid_from, v_valid_to
);
ELSE
UPDATE im_trans_prices SET
uom_id = v_uom_id,
company_id = v_company_id,
task_type_id = v_task_type_id,
activity_nr = v_activity_nr,
target_language_id = v_target_language_id,
source_language_id = v_source_language_id,
subject_area_id = v_subject_area_id,
file_type_id = v_file_type_id,
project_id = v_project_id,
reuse_band_id = v_reuse_band_id,
difficulty = v_difficulty,
final_customer_id = v_final_customer_id,
currency = v_currency,
price = row."Price",
min_price = NULL,
note = NULL,
valid_from = v_valid_from,
valid_through = v_valid_to
WHERE price_id = v_price_id;
END IF;
END IF;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_import_pricelist();
---------------------------------------------------------------------------------
-- Navision Projects
---------------------------------------------------------------------------------
alter table nav."Job" alter column "Starting Date" drop not null;
alter table nav."Job" alter column "Ending Date" drop not null;
alter table nav."Job" alter column "Creation Date" drop not null;
alter table nav."Job" alter column "Delivery Date" drop not null;
alter table nav."Job" alter column "Closing Date" drop not null;
alter table nav."Job" alter column "Last Date" drop not null;
-- Replace garbage dates with NULL values in nav."Job":
update nav."Job" set "Starting Date" = null where "Starting Date" = '1753-01-01'::date;
update nav."Job" set "Ending Date" = null where "Ending Date" = '1753-01-01'::date;
update nav."Job" set "Creation Date" = null where "Creation Date" = '1753-01-01'::date;
update nav."Job" set "Delivery Date" = null where "Delivery Date" = '1753-01-01'::date;
update nav."Job" set "Closing Date" = null where "Closing Date" = '1753-01-01'::date;
update nav."Job" set "Last Date" = null where "Last Date" = '1753-01-01'::date;
drop function nav_import_project ();
create or replace function nav_import_project ()
returns integer as $body$
DECLARE
row RECORD;
v_counter integer;
v_exists_p integer;
v_duplicate_p integer;
v_project_id integer;
v_project_name varchar;
v_project_nr varchar;
v_project_status_id integer;
v_project_type_id integer;
v_customer_id integer;
v_note varchar;
v_bill_to_contact_id integer;
v_nav_project_manager_id integer;
v_nav_account_manager_id integer;
BEGIN
v_counter := 0;
FOR row IN
select *
from nav."Job"
order by "No_"
LOOP
v_counter := v_counter + 1;
------------------------ Name Fiddling ---------------------------------------------
-- Process the name in order to deal with duplicates.
-- ]po[ doesnt allow for duplicate project names.
-- By default just use the standard name
v_project_name := trim(row."Bill-to Name") || ' ' || trim(row."Description") || ' (' || row."No_" || ')';
v_project_nr := trim(lower(row."No_"));
------------------------ Status and Type ---------------------------------------------
RAISE NOTICE 'Processing #%: %', v_counter, v_project_name;
-- Default Type for project
v_project_type_id := import_cat('Translation Project', 'Intranet Project Type');
-- Status is set depending on the NAV status
v_project_status_id := import_cat('Open', 'Intranet Project Status');
IF 0 = row."Status" THEN v_project_status_id := import_cat('Inquiring', 'Intranet Project Status'); END IF;
IF 1 = row."Status" THEN v_project_status_id := import_cat('Quoting', 'Intranet Project Status'); END IF;
IF 2 = row."Status" THEN v_project_status_id := import_cat('Open', 'Intranet Project Status'); END IF;
IF 3 = row."Status" THEN v_project_status_id := import_cat('Closed', 'Intranet Project Status'); END IF;
-- set project to closed that are from 2008 or older
-- IF row."No_" < '2007' THEN
-- v_project_status_id := import_cat('Closed', 'Intranet Project Status');
-- END IF;
-- Start-date in 2008 or earlier
-- IF coalesce(row."Starting Date", row."Creation Date") < '2009-01-01'::date THEN
-- v_project_status_id := import_cat('Closed', 'Intranet Project Status');
-- END IF;
-- End-date in 2008 or earlier
-- IF coalesce(row."Ending Date", row."Delivery Date") < '2009-01-01'::date THEN
-- v_project_status_id := import_cat('Closed', 'Intranet Project Status');
-- END IF;
-- Blocked projects
IF row."Blocked" = 2 THEN
v_project_status_id := import_cat('Blocked', 'Intranet Project Status');
END IF;
-- Archived projects
IF row."Archived" > 0 THEN
v_project_status_id := import_cat('Archived', 'Intranet Project Status');
END IF;
------------------------ Customer ---------------------------------------------
select company_id into v_customer_id from im_companies
where nav_customer_nr = row."Bill-to Customer No_";
IF v_customer_id is null THEN
-- Check for the "internal" customer
IF row."Internal Project" = 1 THEN
select company_id into v_customer_id from im_companies
where company_path = 'internal';
END IF;
END IF;
IF v_customer_id is null THEN
RAISE WARNING 'Did not find customer #% for project #%', row."Bill-to Customer No_", row."No_";
continue;
END IF;
------------------------ New Project ------------------------------------
-- Check if the project is already there
select project_id into v_project_id from im_projects p
where p.nav_project_nr = row."No_";
-- Create a new project first, because the im_project.main_project_id is not null
IF v_project_id is null THEN
RAISE NOTICE 'Insert Project: %', v_project_name;
v_project_id := im_project__new (
NULL, -- project_id, NULL indicates to create new ID
'im_project', -- type of object to create, must be im_project
now(), -- date of creation - now
0, -- creation user - 0 is guest
'0.0.0.0', -- creation IP - any default
null, -- context_id - parent for permission inheritance - null
v_project_name, -- name of project, unique
v_project_nr, -- nr of project, unique
v_project_nr, -- path of project, unique
null, -- parent project - null means this is a main-project
v_customer_id, -- customer paying for the project
v_project_type_id, -- Type - sub-type for DynField field control
v_project_status_id -- Status - lifecylce control
);
END IF;
------------------------ Transform Fields ----------------------------------
-- Billing contact
select person_id into v_bill_to_contact_id from persons
where nav_contact_nr = row."Bill-to Contact No_";
IF v_bill_to_contact_id is null and coalesce(row."Bill-to Contact No_",'') != '' THEN
RAISE WARNING 'Did not find bill-to contact %', row."Bill-to Contact No_";
END IF;
-- Project Manager
select person_id into v_nav_project_manager_id from persons
where nav_employee_nr = row."Project Manager";
IF v_nav_project_manager_id is null and coalesce(row."Project Manager",'') != '' THEN
RAISE WARNING 'Did not find project manager %', row."Project Manager";
END IF;
-- Account Manager
select person_id into v_nav_account_manager_id from persons
where nav_employee_nr = row."Account Manager";
IF v_nav_account_manager_id is null and coalesce(row."Account Manager",'') != '' THEN
RAISE WARNING 'Did not find account manager %', row."Account Manager";
END IF;
-- Project Manager: Establish a im_biz_object_member relationship
-- between the project and the project manager as "Project Manager".
IF v_nav_project_manager_id is not null THEN
RAISE NOTICE 'Adding Project Manager for project: %', v_project_name;
PERFORM im_biz_object_member__new (
null,
'im_biz_object_member',
v_project_id, -- the business object
v_nav_project_manager_id, -- the person related to biz_object
1301, -- role: Project Manager
null, -- percentage
null, -- creation user
'0.0.0.0' -- creation IP
);
END IF;
IF v_nav_account_manager_id is not null THEN
RAISE NOTICE 'Adding Account Manager for project: %', v_project_name;
PERFORM im_biz_object_member__new (
null,
'im_biz_object_member',
v_project_id, -- the business object
v_nav_account_manager_id, -- the person related to biz_object
1301, -- role: Project Manager
null, -- percentage
null, -- creation user
'0.0.0.0' -- creation IP
);
END IF;
------------------------ Update Project ----------------------------------
RAISE NOTICE 'Update Project: %', v_project_name;
update im_projects set
project_name = v_project_name,
project_nr = v_project_nr,
project_path = v_project_nr,
parent_id = null,
company_id = v_customer_id,
project_status_id = v_project_status_id,
project_type_id = v_project_type_id,
project_lead_id = v_nav_project_manager_id,
supervisor_id = v_nav_account_manager_id,
company_contact_id = v_bill_to_contact_id,
start_date = coalesce(row."Starting Date", row."Creation Date"),
end_date = coalesce(row."Ending Date", row."Delivery Date"),
nav_delivery_date = row."Delivery Date",
nav_closing_date = row."Closing Date",
nav_last_date = row."Last Date",
note = v_note,
nav_project_nr = row."No_"
where project_id = v_project_id;
END LOOP;
RETURN 0;
END;$body$ language 'plpgsql';
select nav_import_project ();
--------------------------------------------------------------------------------
-- Check for users that are not included in Secure Access anymore
---------------------------------------------------------------------------------
-- Find out which users in po are not in Secure Access
--
drop function nav_prune_users();
create or replace function nav_prune_users ()
returns integer as $BODY$
DECLARE
row_user RECORD;
row_group RECORD;
v_count integer;
v_user_id integer;
BEGIN
v_count := 0;
FOR row_user IN
select *
from (select u.*,
auth.short_name as authority,
auth.short_name || '\\' || u.username as windows_login
from cc_users u,
auth_authorities auth
where u.authority_id = auth.authority_id and
u.member_state != 'banned'
) u
where
windows_login not in (
select lower(u."Loginname")
from sa."AR_User" u
)
and authority != 'local'
order by u.user_id
LOOP
v_count := v_count + 1;
RAISE NOTICE 'Disabling User #%: First=%, Last=%, Uname=%, Login=% => %',
v_count, row_user.first_names, row_user.last_name, row_user.username, row_user.windows_login, row_user.user_id;
update membership_rels
set member_state = 'banned'
where rel_id in (
select rel_id
from acs_rels
where object_id_one = -2 and
object_id_two = row_user.user_id
);
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
select nav_prune_users ();
--------------------------------------------------------------------------------
-- Prune user grous
---------------------------------------------------------------------------------
-- Find out which users in po are not in Secure Access
--
drop function nav_prune_user_groups();
create or replace function nav_prune_user_groups ()
returns integer as $BODY$
DECLARE
row_user RECORD;
row_group RECORD;
v_count integer;
v_user_id integer;
BEGIN
v_count := 0;
FOR row_user IN
select *
from
(select u.*,
auth.short_name || '\\' || u.username as windows_login
from cc_users u,
auth_authorities auth
where u.authority_id = auth.authority_id
) u
where
windows_login not in (
select lower(u."Loginname")
from sa."AR_User" u
)
order by u.user_id
LOOP
v_count := v_count + 1;
RAISE NOTICE 'Processing User #%: First=%, Last=%, Login=%, Email=%', v_count, row_user.first_names, row_user.last_name, row_user.windows_login, row_user.email;
-- Delete the user from groups he is not a member of anymore
FOR row_group IN
select g.group_name,
r.rel_id
from acs_rels r, groups g, membership_rels mr
where r.rel_id = mr.rel_id and
r.object_id_one = g.group_id and
mr.member_state = 'approved' and
r.object_id_two = v_user_id and
g.group_name not in (
select sgm.po_group_name
from sa.view_ar_relationshipall_parent_valid var,
sa_group_mapping sgm
where var."ObjectName" = sgm.sa_group_name and
var."ChildObjectID" in (
-- build the union of all group memberships of all accounts
select "ObjectID"
from sa."AR_User"
where lower(trim("EmailAddress")) = v_email
)
) and
g.group_name not in ('Registered Users', 'The Public')
LOOP
RAISE NOTICE 'Deleting user % from group %', v_loginname, row_group.group_name;
-- Get the PO group_id
select group_id into v_group_id from groups where group_name = row_group.group_name;
RAISE NOTICE 'Deleting: user=%, group=%', v_user_id, v_group_id;
PERFORM membership_rel__delete(row_group.rel_id);
END LOOP;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
-- select nav_prune_user_groups ();
---------------------------------------------------------------------------------
-- Navision Receipts
---------------------------------------------------------------------------------
drop function nav_import_receipt ();
create or replace function nav_import_receipt ()
returns integer as $body$
DECLARE
row RECORD;
v_counter integer;
v_exists_p integer;
v_cost_id integer;
v_customer_id integer;
v_vendor_id integer;
v_project_id integer;
v_cost_type_id integer;
v_cost_status_id integer;
v_payment_days integer;
v_creation_user_id integer;
v_company_contact_id integer;
v_cost_center_id integer;
BEGIN
v_counter := 0;
FOR row IN
select *
from nav."IT$Purch_ Rcpt_ Header"
order by "No_"
LOOP
v_counter := v_counter + 1;
------------------------ Custome and Provider ---------------------------------------------
select company_id into v_vendor_id from im_companies
where nav_vendor_nr = row."Pay-to Vendor No_";
IF v_vendor_id is null THEN
RAISE WARNING 'Vendor % not found', row."Pay-to Vendor No_";
continue;
END IF;
select company_id into v_customer_id from im_companies
where company_path = 'internal';
------------------------ Default Values ---------------------------------------------
RAISE NOTICE 'Processing Receipt %: %', v_counter, row."No_";
v_cost_status_id := import_cat('Created', 'Intranet Cost Status');
v_cost_type_id := import_cat('Provider Receipt', 'Intranet Cost Type');
-- Creation user
select person_id into v_creation_user_id from persons
where nav_windows_login = lower(row."User ID");
-- Contact provider
select person_id into v_company_contact_id from persons
where lower(nav_contact_nr) ~ lower(row."Pay-to Contact No_");
-- Project
select project_id into v_project_id from im_projects
where nav_project_nr = row."Project No_";
-- Payment Conditions
v_payment_days := NULL;
IF row."Payment Terms Code" = '10_D' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = '14_D' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = '14_D_D' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = '20_D' THEN v_payment_days := 20; END IF;
IF row."Payment Terms Code" = '21_D' THEN v_payment_days := 21; END IF;
IF row."Payment Terms Code" = '30_D' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = '30_D_D' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = '45_D' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = '45_D_D' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = '60_D' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = '60_D_D' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = '75_D' THEN v_payment_days := 75; END IF;
IF row."Payment Terms Code" = '90_D' THEN v_payment_days := 90; END IF;
IF row."Payment Terms Code" = 'HALF_YEAR' THEN v_payment_days := 182; END IF;
IF row."Payment Terms Code" = '2_Y' THEN v_payment_days := 730; END IF;
IF row."Payment Terms Code" = 'IMM' THEN v_payment_days := 0; END IF;
IF row."Payment Terms Code" = 'UNDO' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = 'NEPLATIT' THEN v_payment_days := 10000; END IF;
-- make sure the currency code exists
select count(*) into v_exists_p from currency_codes
where iso = row."Currency Code";
IF v_exists_p = 0 THEN
RAISE WARNING 'Currency "%" does not exist for FinDoc: %', row."Currency Code", row."No_";
continue;
END IF;
------------------------ New Cost and Invoice ------------------------------------
-- Check if the cost item is already there
select cost_id into v_cost_id from im_costs c
where c.cost_name = row."No_";
IF v_cost_id is null THEN
RAISE NOTICE 'Insert Invoice: %', row."No_";
v_cost_id := im_invoice__new (
null, 'im_invoice', row."Document Date"::timestamptz, v_creation_user_id, '0.0.0.0', null,
row."No_", -- cost_nr
v_customer_id, -- customer
v_vendor_id, -- provider
null, -- customer contact
row."Order Date", -- effective_date
row."Currency Code", -- currency
null, -- template_id
v_cost_status_id, -- status_id
v_cost_type_id, -- type_id
null, -- payment_method_id
v_payment_days, -- payment_days
0.0, -- amount
null, -- vat
null, -- tax
null -- note
);
END IF;
update im_costs set
cost_name = row."No_",
cost_nr = v_cost_id::varchar,
customer_id = v_customer_id,
provider_id = v_vendor_id,
project_id = v_project_id,
effective_date = row."Order Date",
currency = row."Currency Code",
cost_status_id = v_cost_status_id,
cost_type_id = v_cost_type_id,
payment_days = v_payment_days,
amount = 0.0,
cost_center_id = v_cost_center_id,
tax = null,
vat = null,
nav_purchase_receipt_nr = row."No_"
where cost_id = v_cost_id;
update im_invoices set
company_contact_id = v_company_contact_id,
invoice_nr = row."No_",
payment_method_id = null
where invoice_id = v_cost_id;
END LOOP;
RETURN 0;
END;$body$ language 'plpgsql';
select nav_import_receipt ();
drop function nav_import_receipt_lines ();
create or replace function nav_import_receipt_lines ()
returns integer as $body$
DECLARE
row RECORD;
v_counter integer;
v_cost_id integer;
v_exists_p integer;
v_name varchar;
v_uom_id integer;
v_line_id integer;
v_currency_code varchar;
v_material_id integer;
v_amount numeric;
BEGIN
v_counter := 0;
FOR row IN
select *
from nav."IT$Purch_ Rcpt_ Line"
where "Document No_" in (select cost_name from im_costs)
order by "Document No_", "Line No_"
LOOP
v_counter := v_counter + 1;
------------------------ Financial Document ---------------------------------------------
select cost_id into v_cost_id from im_costs
where cost_name = row."Document No_";
IF v_cost_id is null THEN
RAISE WARNING 'FinDoc % not found', row."Document No_";
continue;
END IF;
select currency into v_currency_code from im_costs
where cost_id = v_cost_id;
------------------------ Transform Fields ------------------------------------
-- Unit of Measure
v_uom_id := import_cat ('Unit', 'Intranet UoM');
IF row."Unit of Measure" = 'hodina' THEN v_uom_id = import_cat ('Hour', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'slovo' THEN v_uom_id = import_cat ('S-Word', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'ks' THEN v_uom_id = import_cat ('Unit', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'den' THEN v_uom_id = import_cat ('Day', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'NS' THEN v_uom_id = import_cat ('Page', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'strana' THEN v_uom_id = import_cat ('Page', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'iNEW WORD' THEN v_uom_id = import_cat ('S-Word', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'license' THEN v_uom_id = import_cat ('Unit', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'znak' THEN v_uom_id = import_cat ('Unit', 'Intranet UoM'); END IF;
IF row."Unit of Measure" = 'kus' THEN v_uom_id = import_cat ('Unit', 'Intranet UoM'); END IF;
-- Name Generation
-- Names need to be unique in ]po[
v_name := coalesce(row."Description 2", row."Description");
v_name := v_name || ' (' || row."Source Language" || ' -> ' || row."Target Language" || ')';
-- Material
select material_id into v_material_id from im_materials
where material_nr = 'default';
------------------------ New Line ------------------------------------
-- Check for duplicated entries (i.e. names)
select count(*) into v_exists_p
from im_invoice_items
where item_name = v_name and
invoice_id = v_cost_id and
sort_order = row."Line No_" and
item_uom_id = v_uom_id;
IF v_exists_p > 1 THEN
RAISE WARNING 'Found more then one invoice line matching criteria for FinDoc: %', row."Document No_";
continue;
END IF;
select item_id into v_line_id
from im_invoice_items
where item_name = v_name and
invoice_id = v_cost_id and
sort_order = row."Line No_" and
item_uom_id = v_uom_id;
IF v_line_id is null THEN
RAISE NOTICE 'Insert Invoice Line for: %', row."Document No_";
v_line_id := nextval('im_invoice_items_seq');
insert into im_invoice_items (
item_id,
item_name,
invoice_id,
item_units,
item_uom_id,
price_per_unit,
currency,
sort_order,
item_type_id,
item_status_id,
description,
item_material_id
) values (
v_line_id,
v_name,
v_cost_id,
row."Quantity",
v_uom_id,
row."Direct Unit Cost",
v_currency_code,
row."Line No_",
null,
null,
null,
v_material_id
);
ELSE
RAISE NOTICE 'Update Invoice Line for: %', row."Document No_";
END IF;
update im_invoice_items set
item_name = v_name,
invoice_id = v_cost_id,
item_units = row."Quantity",
item_uom_id = v_uom_id,
price_per_unit = row."Direct Unit Cost",
currency = v_currency_code,
sort_order = row."Line No_",
item_type_id = null,
item_status_id = null,
description = null,
item_material_id = v_material_id
where item_id = v_line_id;
-- Update the amount of the invoice
select amount into v_amount from im_costs
where cost_id = v_cost_id;
v_amount := v_amount + row."Direct Unit Cost" * row."Quantity";
update im_costs set amount = v_amount
where cost_id = v_cost_id;
END LOOP;
RETURN 0;
END;$body$ language 'plpgsql';
select nav_import_receipt_lines ();
---------------------------------------------------------------------------------
-- Navision Vendors
---------------------------------------------------------------------------------
drop function nav_import_vendor ();
create or replace function nav_import_vendor ()
returns integer as $body$
DECLARE
row RECORD;
v_company_id integer;
v_office_id integer;
v_exists_p integer;
v_company_name varchar;
v_office_name varchar;
v_office_status_id integer;
v_office_type_id integer;
v_company_status_id integer;
v_company_type_id integer;
v_duplicate_p integer;
v_payment_days integer;
v_primary_contact_id integer;
v_note varchar;
v_parent_company_id integer;
v_nav_pm_id integer;
v_nav_account_manager_id integer;
v_country_code varchar;
v_counter integer;
BEGIN
v_counter := 0;
FOR row IN
select *
from nav."Vendor"
order by "No_"
LOOP
v_counter := v_counter + 1;
------------------------ Name Fiddling ---------------------------------------------
-- Process the name in order to deal with duplicates.
-- ]po[ doesnt allow for duplicate company or office names.
-- By default just use the standard name
v_company_name := row."Name";
-- Add the vendor no_ if the name is not unique within the NAV table
select count(*) into v_duplicate_p from nav."Vendor"
where lower("Name") = lower(v_company_name);
IF v_duplicate_p > 1 THEN
v_company_name := v_company_name || ' (' || row."No_" || ')';
END IF;
-- The office name is the company name + "Main Office".
v_office_name := v_company_name || ' Main Office';
------------------------ Default Values ---------------------------------------------
RAISE NOTICE 'Processing Vendor %: %', v_counter, v_company_name;
-- Default Status for company and office
v_office_status_id := import_cat('Active', 'Intranet Office Status');
v_office_type_id := import_cat('Main Office', 'Intranet Office Type');
v_company_status_id := import_cat('Active', 'Intranet Company Status');
v_company_type_id := import_cat('Provider', 'Intranet Company Type');
------------------------ New Office and Comany ------------------------------------
-- Check if the office is already there
select office_id into v_office_id from im_offices o
where lower(o.office_name) = lower(v_office_name);
IF v_office_id is null THEN
select office_id into v_office_id from im_offices o
where o.nav_vendor_nr = row."No_";
END IF;
-------------------------------------------------------------------------------------
-- Fix Country Codes
v_country_code := lower(row."Country_Region Code");
IF v_country_code = 'gb' THEN v_country_code := 'uk'; END IF;
IF v_country_code = '' THEN v_country_code := NULL; END IF;
select count(*) into v_exists_p
from country_codes where iso = v_country_code;
IF v_country_code is not null and v_exists_p < 1 THEN
RAISE WARNING 'Skipping (unknown country code %): %', v_country_code, v_office_name;
continue;
END IF;
-------------------------------------------------------------------------------------
-- Create a new office first, because the im_company.main_office_id is not null
IF v_office_id is null THEN
RAISE NOTICE 'Insert Office: %', v_office_name;
select count(*) into v_exists_p from im_offices
where lower(office_name) = lower(v_office_name);
IF v_exists_p > 0 THEN
v_office_name := v_office_name || ' (' || row."No_" || ')';
END IF;
v_office_id := im_office__new (
NULL, 'im_office', now()::date, 0, '0.0.0.0', null,
v_office_name, row."No_", v_office_type_id,
v_office_status_id, null
);
update im_offices set nav_vendor_nr = row."No_" where office_id = v_office_id;
END IF;
-- Check if the company is already there
select company_id into v_company_id from im_companies c
where c.nav_vendor_nr = row."No_";
IF v_company_id is null THEN
RAISE NOTICE 'Insert Company: %', v_company_name;
select count(*) into v_exists_p from im_companies
where lower(company_name) = lower(v_company_name);
IF v_exists_p > 0 THEN
v_company_name := v_company_name || ' (' || row."No_" || ')';
END IF;
v_company_id := im_company__new (
NULL, 'im_company', now()::date, 0, '0.0.0.0', null,
v_company_name, row."No_",
v_office_id, v_company_type_id, v_company_status_id
);
update im_companies set nav_vendor_nr = row."No_" where company_id = v_company_id;
END IF;
------------------------ Update Office and Comany----------------------------------
-- Conver Payment Terms Code
v_payment_days := NULL;
IF row."Payment Terms Code" = '10_D' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = '14_D' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = '14_D_D' THEN v_payment_days := 14; END IF;
IF row."Payment Terms Code" = '20_D' THEN v_payment_days := 20; END IF;
IF row."Payment Terms Code" = '21_D' THEN v_payment_days := 21; END IF;
IF row."Payment Terms Code" = '30_D' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = '30_D_D' THEN v_payment_days := 30; END IF;
IF row."Payment Terms Code" = '45_D' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = '45_D_D' THEN v_payment_days := 45; END IF;
IF row."Payment Terms Code" = '60_D' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = '60_D_D' THEN v_payment_days := 60; END IF;
IF row."Payment Terms Code" = '75_D' THEN v_payment_days := 75; END IF;
IF row."Payment Terms Code" = '90_D' THEN v_payment_days := 90; END IF;
IF row."Payment Terms Code" = 'HALF_YEAR' THEN v_payment_days := 182; END IF;
IF row."Payment Terms Code" = '2_Y' THEN v_payment_days := 730; END IF;
IF row."Payment Terms Code" = 'IMM' THEN v_payment_days := 0; END IF;
IF row."Payment Terms Code" = 'UNDO' THEN v_payment_days := 10; END IF;
IF row."Payment Terms Code" = 'NEPLATIT' THEN v_payment_days := 10000; END IF;
-- Add other fields to the "note" field
v_note := NULL;
IF row."Home Page" is not null AND row."Home Page" <> '' THEN v_note := v_note || 'Home Page=' || row."Home Page" ||', '; END IF;
IF row."VAT Bus_ Posting Group" is not null AND row."VAT Bus_ Posting Group" <> '' THEN v_note := v_note || 'VAT Bus Posting Group=' || row."VAT Bus_ Posting Group" || ', '; END IF;
IF row."Registration No_" is not null AND row."Registration No_" <> '' THEN v_note := v_note || 'Registration No_=' || row."Registration No_" ||', '; END IF;
IF row."Contact Text" is not null AND row."Contact Text" <> '' THEN v_note := v_note || 'Contact Text=' || row."Contact Text" ||', '; END IF;
IF row."Bank Account No_" is not null AND row."Bank Account No_" <> '' THEN v_note := v_note || 'Bank Account No_=' || row."Bank Account No_" ||', '; END IF;
-- Primary contact
select person_id into v_primary_contact_id from persons
where nav_contact_nr ~ row."Primary Contact No_";
IF v_primary_contact_id is null THEN
IF row."E-Mail" is not null AND row."E-Mail" <> '' THEN
v_primary_contact_id := lookup_user_email(row."E-Mail", 'nav_import_vendor.im_company.primary_contact_id');
END IF;
END IF;
-- Default Project Manager
select person_id into v_nav_pm_id from persons
where nav_employee_nr = row."Project Manager";
-- Parent Company
select company_id into v_parent_company_id from im_companies
where nav_vendor_nr = row."Mother Company";
RAISE NOTICE 'Update Office: %', v_office_name;
update im_offices set
office_name = v_office_name,
office_status_id = v_office_status_id,
office_type_id = v_office_type_id,
phone = row."Phone No_",
fax = row."Fax No_",
address_line1 = row."Address",
address_line2 = row."Address 2",
address_city = row."City",
address_country_code = v_country_code,
address_postal_code = row."Post Code",
nav_vendor_nr = row."No_"
where office_id = v_office_id;
RAISE NOTICE 'Update Company: %', v_company_name;
update im_companies set
company_name = v_company_name,
main_office_id = v_office_id,
company_status_id = v_company_status_id,
company_type_id = v_company_type_id,
vat_number = row."VAT Registration No_",
default_payment_days = v_payment_days,
primary_contact_id = v_primary_contact_id,
accounting_contact_id = v_primary_contact_id,
parent_company_id = v_parent_company_id,
note = v_note,
nav_vendor_posting_group = row."Vendor Posting Group",
nav_pm_percentage = row."PM %",
nav_vendor_nr = row."No_"
where company_id = v_company_id;
-- Account Manager: Establish a im_biz_object_member relationship
-- between the company and the account manager with as "Key Account".
select person_id into v_nav_account_manager_id from persons
where nav_employee_nr = row."Account Manager";
IF v_nav_account_manager_id is not null THEN
RAISE NOTICE 'Adding Account Manager for company: %', v_company_name;
PERFORM im_biz_object_member__new (
null,
'im_biz_object_member',
v_company_id, -- the business object
v_nav_account_manager_id, -- the person related to biz_object
1302, -- role: Key Account
null, -- percentage
null, -- creation user
'0.0.0.0' -- creation IP
);
END IF;
END LOOP;
RETURN 0;
END;$body$ language 'plpgsql';
select nav_import_vendor ();
# ----------------------------------------------------------
# Run all import scripts
# ----------------------------------------------------------
# Cleanup old log files
rm -f ~/log/import.*.log
# First import table extensions and categories
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import.sql > ~/log/import.nav-import.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-categories.sql > ~/log/import.nav-import-categories.log 2>&1
# Add users from Secure Access and Navision, and then delete the ones not in NAV or SA anymore
# Then import additional info about users including services and tools.
psql -f ~/packages/intranet-navision/sql/postgresql/sa-import-user.sql > ~/log/import.sa-import-user.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-employees.sql > ~/log/import.nav-import-employees.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-contacts.sql > ~/log/import.nav-import-contacts.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-contacts-tools.sql > ~/log/import.nav-import-contacts-tools.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-contacts-service.sql > ~/log/import.nav-import-contacts-service.log 2>&1
# Disabled while there is an issue with Secure Access import data containing wrong UTF-8 characters
# psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-prune-users.sql > ~/log/import.nav-import-prune-users.log 2>&1
# Now come customer and provider companies
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-customer.sql > ~/log/import.nav-import-customer.log 2>&1
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-vendor.sql > ~/log/import.nav-import-vendor.log 2>&1
# Relationship between users and companies
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-contacts-business-relation.sql > ~/log/import.nav-import-contacts-business-relation.log 2>&1
# Projects require companies and users.
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-projects.sql > ~/log/import.nav-import-projects.log 2>&1
# Financial Document require projects and users
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-receipts.sql > ~/log/import.nav-import-receipts.log 2>&1
# The pricelist requires everything to be there and comes last.
psql -f ~/packages/intranet-navision/sql/postgresql/nav-import-pricelist.sql > ~/log/import.nav-import-pricelist.log 2>&1
---------------------------------------------------------------------------------
-- Update the data model
---------------------------------------------------------------------------------
drop trigger im_projects_calendar_update_tr on im_projects;
drop trigger im_forum_topics_calendar_update_tr on im_forum_topics;
-- Projects
alter table im_projects rename column nav_project_no to nav_project_no;
alter table im_projects add nav_project_nr text;
alter table im_projects add bill_to_contact_id integer references persons;
alter table im_projects add nav_delivery_date timestamptz;
alter table im_projects add nav_closing_date timestamptz;
alter table im_projects add nav_last_date timestamptz;
-- Create a check constraint on the project_path file to make
-- sure it is located on the /mnt/... share OR it is a relative path.
-- project_type_id=100 identified timesheet tasks, which are not
-- bound by these restrictions because they do not have a filestorage.
alter table im_projects add constraint im_project_path_ck check (
project_type_id = 100 OR
project_path ~ '^/mnt/.*$' OR
project_path ~ '^[a-zA-Z0-9_]*$'
);
-- Companies
alter table im_companies add parent_company_id integer;
alter table im_companies rename column nav_customer_no to nav_customer_nr;
alter table im_companies add nav_customer_nr text;
alter table im_companies rename column nav_vendor_no to nav_vendor_nr;
alter table im_companies add nav_vendor_nr text;
alter table im_companies add nav_customer_posting_group text;
alter table im_companies add nav_vendor_posting_group text;
alter table im_companies add nav_pm_percentage numeric(12,1);
alter table im_companies add nav_pm integer references persons;
alter table im_companies add nav_payment_terms integer;
alter table im_companies add nav_currency varchar;
alter table im_companies ADD COLUMN nav_name2 varchar(50);
alter table im_companies ADD COLUMN nav_gl_dimension_1 varchar(20);
alter table im_companies ADD COLUMN nav_blocked smallint;
alter table im_companies ADD COLUMN nav_payment_method integer;
alter table im_companies ADD COLUMN nav_gen_posting_group integer;
alter table im_companies ADD COLUMN nav_vat_posting_group integer;
alter table im_companies ADD COLUMN nav_minimal_tariff_pm numeric(38,20);
alter table im_companies ADD COLUMN nav_minimal_amount numeric(38,20);
alter table im_companies ADD COLUMN nav_division varchar(150);
alter table im_companies ADD COLUMN nav_line_discount_percentage numeric(12,1);
alter table im_companies ALTER COLUMN nav_customer_posting_group TYPE integer USING
lookup_category_id_from_aux_string2(nav_customer_posting_group::varchar, 'Navision General Posting Group'::varchar);
-- Offices
alter table im_offices rename column nav_customer_no to nav_customer_nr;
alter table im_offices add nav_customer_nr text;
alter table im_offices rename column nav_vendor_no to nav_vendor_nr;
alter table im_offices add nav_vendor_nr text;
-- Persons
alter table persons rename column nav_contact_no to nav_contact_nr;
alter table persons add nav_contact_nr varchar(20);
alter table persons rename column nav_employee_no to nav_employee_nr;
alter table persons add nav_employee_nr varchar(20);
alter table persons add nav_skype varchar;
alter table persons add nav_middle_name varchar;
alter table persons add nav_windows_login varchar;
alter table persons add nav_primary_contact_for_company_id integer;
-- Unique key for Secure Access
alter table persons add sa_object_id integer;
create unique index persons_sa_object_idx on persons (sa_object_id);
-- Fix old DynField definition
update acs_attributes set attribute_name = 'nav_project_nr' where attribute_name = 'navision_project_nr';
-- Costs
alter table im_costs add nav_sales_invoice_nr text;
alter table im_costs add nav_sales_quote_nr text;
alter table im_costs add nav_sales_delnote_nr text;
alter table im_costs add nav_purchase_invoice_nr text;
alter table im_costs add nav_purchase_po_nr text;
alter table im_costs add nav_purchase_receipt_nr text;
-- Categories extension table for Translation Task Types
create table im_categories_trans_task_ext (
category_id integer
constraint im_categories_trans_task_ext_pk
primary key
constraint im_categories_trans_task_ext_category_fk
references im_categories,
activity_code integer,
activity_group text,
sub_activity text,
reuse_band_id integer
constraint im_categories_trans_task_ext_reuse_band_fk
references im_categories,
difficulty integer,
description text,
without_pm_fee_p integer,
pm_fee_p integer
);
---------------------------------------------------------------------------------
-- Auxilary Functions and Helpers
---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION nav.ms_getdate()
RETURNS timestamp without time zone AS 'SELECT LOCALTIMESTAMP'
LANGUAGE 'sql' VOLATILE;
-- Lookup a category in po and return the category_id
create or replace function import_cat (varchar, varchar)
returns integer as '
DECLARE
p_category alias for $1;
p_category_type alias for $2;
v_category_id integer;
BEGIN
SELECT c.category_id INTO v_category_id FROM im_categories c
WHERE lower(c.category) = lower(p_category)
AND lower(c.category_type) = lower(p_category_type);
IF v_category_id is null AND p_category is not null THEN
RAISE WARNING ''import_cat(%,%): Did not find category'', p_category, p_category_type;
END IF;
RETURN v_category_id;
END;' language 'plpgsql';
-- select import_cat('open','intranet project status');
-- Lookup the "primary key" (email or username) of a user into a user_id
create or replace function lookup_user_email (varchar, varchar)
returns integer as '
DECLARE
p_email alias for $1;
p_purpose alias for $2;
v_user_id integer;
BEGIN
IF p_email is null THEN return null; END IF;
IF p_email = '''' THEN return null; END IF;
SELECT p.party_id INTO v_user_id
FROM parties p
WHERE lower(p.email) = lower(p_email);
IF v_user_id is null THEN
SELECT u.user_id INTO v_user_id
FROM users u
WHERE lower(u.username) = lower(p_email);
END IF;
IF v_user_id is null THEN
RAISE WARNING ''lookup_user_email(%) for %: Did not find user'', p_email, p_purpose;
END IF;
RETURN v_user_id;
END;' language 'plpgsql';
-- Lookup the "primary key" (first_names + last_name) of a user into a user_id
create or replace function lookup_user_name (varchar, varchar, varchar)
returns integer as '
DECLARE
p_first_names alias for $1;
p_last_name alias for $2;
p_purpose alias for $3;
v_user_id integer;
BEGIN
IF p_first_names is null OR p_last_name is null THEN return null; END IF;
IF p_first_names = '''' OR p_last_name = '''' THEN return null; END IF;
SELECT p.person_id INTO v_user_id
FROM persons p
WHERE lower(p.first_names) = lower(p_first_names) and
lower(p.last_name) = lower(p_last_name);
IF v_user_id is null THEN
RAISE WARNING ''lookup_user_name(%) for % %: Did not find user'', p_first_names, p_last_name, p_purpose;
END IF;
RETURN v_user_id;
END;' language 'plpgsql';
---------------------------------------------------------------------------------
--Helper function im_category_id_from_aux_string2
---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION lookup_category_id_from_aux_string2(varchar, varchar)
RETURNS integer AS $BODY$
DECLARE
p_aux2 alias for $1;
p_type alias for $2;
v_category_row RECORD;
BEGIN
SELECT category_id INTO v_category_row
FROM im_categories WHERE aux_string2 = p_aux2 AND category_type = p_type
LIMIT 1;
IF NOT FOUND THEN
RAISE WARNING 'lookup_category_id_from_aux_string2(%,%): Did not find category with specified aux_string2', p_aux2, p_type;
IF p_aux2 IS NULL OR p_aux2 = '' THEN
RETURN NULL;
ELSE
RETURN -1;
END IF;
ELSE
RETURN v_category_row.category_id;
END IF;
END; $BODY$ LANGUAGE 'plpgsql';
-- Function: timesheet_user__get_id(character varying, character varying)
-- DROP FUNCTION timesheet_user__get_id(character varying, character varying);
CREATE OR REPLACE FUNCTION timesheet_user__get_id(character varying, character varying)
RETURNS integer AS
$BODY$
DECLARE
p_user_name alias for $1;
p_email alias for $2;
v_user_id integer;
BEGIN
IF coalesce(p_user_name, '') = '' AND coalesce(p_email, '') = '' THEN
return NULL;
END IF;
SELECT o.object_id INTO v_user_id
FROM acs_objects o, parties pa, persons pe, users u
WHERE
o.object_id = pa.party_id AND
pa.party_id = pe.person_id AND
pe.person_id = u.user_id AND
(
(coalesce(p_user_name, '') <> '' AND u.username ~~* p_user_name)
OR (coalesce(p_email, '') <> '' AND pa.email ~~* p_email)
)
ORDER BY
pe.sa_object_id;
IF v_user_id IS NULL THEN
RAISE WARNING 'timesheet_user__get_id(''%'', ''%'') did not find user.', p_user_name, p_email;
END IF;
RETURN v_user_id;
END;$BODY$
LANGUAGE 'plpgsql' VOLATILE;
ALTER FUNCTION timesheet_user__get_id(character varying, character varying) OWNER TO postgres;
--------------------------------------------------------------------------------
-- Views
---------------------------------------------------------------------------------
CREATE OR REPLACE FUNCTION sa.ms_getdate()
RETURNS timestamp without time zone AS 'SELECT LOCALTIMESTAMP'
LANGUAGE 'sql' VOLATILE;
CREATE or replace VIEW sa.view_ar_relationshipall_parent_valid AS
SELECT
PARENT."ObjectID",
PARENT."ObjectName",
PARENT."ObjectAlias",
PARENT."ObjectDescription",
PARENT."ObjectTypeID",
PARENT."ObjectCustomField1",
PARENT."ObjectCustomField2",
PARENT."ObjectValidFrom",
PARENT."ObjectValidTo",
PARENT_OBJTP."ObjectTypeNamespace",
PARENT_OBJTP."ObjectTypeAlias",
deriv_parent."ParentObjectID",
deriv_parent."ChildObjectID",
deriv_parent."RelationshipValidFrom",
deriv_parent."RelationshipValidTo",
deriv_parent."TransitivePath",
PARENT."OwnerID"
FROM
(SELECT "ParentObjectID",
"ChildObjectID",
MIN(DERIV_TBL."RelationshipValidTo") As "RelationshipValidTo",
MAX(DERIV_TBL."RelationshipValidFrom") As "RelationshipValidFrom",
"TransitivePath"
FROM
(SELECT sa."AR_Relationship"."ParentObjectID",
sa."AR_Relationship"."ChildObjectID",
sa."AR_Relationship"."RelationshipValidFrom",
sa."AR_Relationship"."RelationshipValidTo",
CAST(sa."AR_Relationship"."ParentObjectID" AS varchar) || ',' ||
CAST(sa."AR_Relationship"."ChildObjectID" AS varchar) || ',' AS "TransitivePath"
FROM sa."AR_Relationship"
WHERE "RelationshipValidFrom" <=sa.ms_getdate() AND
(("RelationshipValidTo" >= sa.ms_getdate()) OR ("RelationshipValidTo" is NULL))
UNION
SELECT sa."AR_RelationshipAll"."ParentObjectID",
sa."AR_RelationshipAll"."ChildObjectID",
sa."AR_RelationshipAll"."RelationshipValidFrom",
sa."AR_RelationshipAll"."RelationshipValidTo",
sa."AR_RelationshipAll"."TransitivePath"
FROM sa."AR_RelationshipAll"
WHERE (sa."AR_RelationshipAll"."RelationshipValidFrom" <= sa.ms_getdate()) AND
((sa."AR_RelationshipAll"."RelationshipValidTo" >= sa.ms_getdate())
OR (sa."AR_RelationshipAll"."RelationshipValidTo" is NULL))
UNION
SELECT sa."AR_RelationshipAll"."ParentObjectID",
sa."AR_Relationship"."ChildObjectID",
sa."AR_RelationshipAll"."RelationshipValidFrom",
sa."AR_RelationshipAll"."RelationshipValidTo",
sa."AR_RelationshipAll"."TransitivePath" ||
CAST(sa."AR_Relationship"."ChildObjectID" AS varchar) || ', ' AS "TransitivePath"
FROM sa."AR_Relationship"
INNER JOIN sa."AR_RelationshipAll"
ON sa."AR_Relationship"."ParentObjectID" = sa."AR_RelationshipAll"."ChildObjectID"
WHERE sa."AR_Relationship"."RelationshipValidFrom" <= sa.ms_getdate() AND
((sa."AR_Relationship"."RelationshipValidTo" >= sa.ms_getdate()) OR
(sa."AR_Relationship"."RelationshipValidTo" is NULL)) AND
sa."AR_RelationshipAll"."RelationshipValidFrom" <= sa.ms_getdate() AND
((sa."AR_RelationshipAll"."RelationshipValidTo" >= sa.ms_getdate())
OR (sa."AR_RelationshipAll"."RelationshipValidTo" is NULL))
) AS DERIV_TBL
GROUP BY
"ParentObjectID",
"ChildObjectID",
"TransitivePath"
) AS deriv_parent
LEFT JOIN sa."AR_Object" PARENT on PARENT."ObjectID" = deriv_parent."ParentObjectID"
LEFT OUTER JOIN sa."AR_ObjectType" PARENT_OBJTP ON PARENT_OBJTP."ObjectTypeID" = PARENT."ObjectTypeID";
CREATE OR REPLACE VIEW sa.view_ar_relationshipall_child_valid AS
select
CHILD."ObjectID",CHILD."ObjectName",CHILD."ObjectAlias",CHILD."ObjectDescription",CHILD."ObjectTypeID",
CHILD."ObjectCustomField1",CHILD."ObjectCustomField2",CHILD."ObjectValidFrom",CHILD."ObjectValidTo",
CHILD_OBJTP."ObjectTypeNamespace",CHILD_OBJTP."ObjectTypeAlias", deriv_child."ParentObjectID",
deriv_child."ChildObjectID",deriv_child."RelationshipValidFrom", deriv_child."RelationshipValidTo",
deriv_child."TransitivePath", CHILD."OwnerID"
FROM
(SELECT "ParentObjectID", "ChildObjectID",
MIN(DERIV_TBL."RelationshipValidTo") As "RelationshipValidTo",
MAX(DERIV_TBL."RelationshipValidFrom") As "RelationshipValidFrom",
"TransitivePath"
FROM
(SELECT sa."AR_Relationship"."ParentObjectID", sa."AR_Relationship"."ChildObjectID",
sa."AR_Relationship"."RelationshipValidFrom", sa."AR_Relationship"."RelationshipValidTo",
CAST(sa."AR_Relationship"."ParentObjectID" As varchar) || ', ' ||
CAST(sa."AR_Relationship"."ChildObjectID" As varchar) || ', ' As "TransitivePath"
FROM sa."AR_Relationship"
WHERE "RelationshipValidFrom" <=sa.ms_getdate() AND
(("RelationshipValidTo" >= sa.ms_getdate()) OR ("RelationshipValidTo" is NULL))
UNION
SELECT sa."AR_RelationshipAll"."ParentObjectID", sa."AR_RelationshipAll"."ChildObjectID",
sa."AR_RelationshipAll"."RelationshipValidFrom", sa."AR_RelationshipAll"."RelationshipValidTo",
sa."AR_RelationshipAll"."TransitivePath"
FROM sa."AR_RelationshipAll"
WHERE sa."AR_RelationshipAll"."RelationshipValidFrom" <=sa.ms_getdate() AND
((sa."AR_RelationshipAll"."RelationshipValidTo" >= sa.ms_getdate()) OR
(sa."AR_RelationshipAll"."RelationshipValidTo" is NULL))
UNION
SELECT sa."AR_RelationshipAll"."ParentObjectID", sa."AR_Relationship"."ChildObjectID",
sa."AR_RelationshipAll"."RelationshipValidFrom", sa."AR_RelationshipAll"."RelationshipValidTo",
sa."AR_RelationshipAll"."TransitivePath" || CAST(sa."AR_Relationship"."ChildObjectID" As varchar) ||
', ' As "TransitivePath"
FROM sa."AR_Relationship"
INNER JOIN sa."AR_RelationshipAll"
ON sa."AR_Relationship"."ParentObjectID" = sa."AR_RelationshipAll"."ChildObjectID"
WHERE sa."AR_Relationship"."RelationshipValidFrom" <=sa.ms_getdate() AND
((sa."AR_Relationship"."RelationshipValidTo" >= sa.ms_getdate()) OR
(sa."AR_Relationship"."RelationshipValidTo" is NULL)) AND
sa."AR_RelationshipAll"."RelationshipValidFrom" <=sa.ms_getdate() AND
((sa."AR_RelationshipAll"."RelationshipValidTo" >= sa.ms_getdate()) OR
(sa."AR_RelationshipAll"."RelationshipValidTo" is NULL))
) AS DERIV_TBL
GROUP BY "ParentObjectID", "ChildObjectID", "TransitivePath"
) AS deriv_child
LEFT JOIN sa."AR_Object" CHILD on CHILD."ObjectID" = deriv_child."ChildObjectID"
LEFT OUTER JOIN sa."AR_ObjectType" CHILD_OBJTP ON CHILD_OBJTP."ObjectTypeID" = CHILD."ObjectTypeID";
---------------------------------------------------------------------------------
-- Secure Access Mapping from AR_Object to groups
---------------------------------------------------------------------------------
drop table sa_group_mapping;
create table sa_group_mapping (
sa_group_name text,
po_group_name text
);
-- Senior Managers
insert into sa_group_mapping VALUES ('MBU_LGMs','Senior Managers');
insert into sa_group_mapping VALUES ('Acn_Loc_LGMs','Senior Managers');
insert into sa_group_mapping VALUES ('Acn_Loc_LGM','Senior Managers');
insert into sa_group_mapping VALUES ('LGM','Senior Managers');
insert into sa_group_mapping VALUES ('MBU_LGMs','Senior Managers');
insert into sa_group_mapping VALUES ('Loc_LGMs','Senior Managers');
-- Accounting
insert into sa_group_mapping VALUES ('Accounting','Accounting');
-- Project Managers
insert into sa_group_mapping VALUES ('Acn_PM_PMs','Project Managers');
insert into sa_group_mapping VALUES ('Acn_PM_Pms','Project Managers');
insert into sa_group_mapping VALUES ('Acn_Ling_PMs','Project Managers');
insert into sa_group_mapping VALUES ('Acn_MBU_PMs','Project Managers');
insert into sa_group_mapping VALUES ('PMs_LU','Project Managers');
insert into sa_group_mapping VALUES ('PMs','Project Managers');
-- Project Coordinators
insert into sa_group_mapping VALUES ('Acn_DTP_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('Acn_Ling_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('Acn_LocEng_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('Acn_DTP_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('MBU_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('DTP_Leads','Project Coordinators');
insert into sa_group_mapping VALUES ('Loc_Leads','Project Coordinators');
-- HR Managers
insert into sa_group_mapping VALUES ('Acn_HR_Members','HR Managers');
-- Sales
insert into sa_group_mapping VALUES ('Acn_GBS_Mgmt','Sales');
-- Resource/Freelance Managers
insert into sa_group_mapping VALUES ('BPG_FRL','Freelance Managers');
-- Customers
insert into sa_group_mapping VALUES ('Acn_All_Customers','Customers');
-- Freelancers + Vendors
insert into sa_group_mapping VALUES ('Acn_All_Vendors','Freelancers');
insert into sa_group_mapping VALUES ('Acn_Freelancers_CZ','Freelancers');
insert into sa_group_mapping VALUES ('Acn_Freelancers_SK','Freelancers');
insert into sa_group_mapping VALUES ('Acn_BusinessPartnerPortal_Vendor','Freelancers');
-- Employees
insert into sa_group_mapping VALUES ('Developers','Employees');
insert into sa_group_mapping VALUES ('All.Hilleho','Employees');
insert into sa_group_mapping VALUES ('PMs','Employees');
---------------------------------------------------------------------------------
-- Secure Access Contacts
---------------------------------------------------------------------------------
-- Create new users by going through the ar_users table
-- line by line and inserting the user into the DB.
drop function sa_import_user();
drop function sa_import_user(integer, integer);
create or replace function sa_import_user (integer, integer)
returns integer as $BODY$
DECLARE
p_offset alias for $1;
p_limit alias for $2;
row_user RECORD;
row_group RECORD;
v_loginname varchar;
v_uname varchar;
v_aname varchar;
v_authority_id integer;
v_old_authority_id integer;
v_old_authority_short_name varchar;
v_update_authority_p integer;
v_count integer;
v_user_id integer;
v_duplicate_user_id integer;
v_exists_p integer;
v_username varchar;
v_group_id integer;
v_email varchar;
v_email_name varchar;
v_email_domain varchar;
v_sa_group_object_id integer;
BEGIN
v_count := 0;
FOR row_user IN
select
*
from
sa."AR_User" u
LEFT JOIN sa."AR_Object" o ON (u."ObjectID" = o."ObjectID")
where
1=1
order by
u."ObjectID" DESC
LIMIT p_limit+2
OFFSET p_offset
LOOP
v_count := v_count + 1;
-- get and massage the email.
v_email := lower(row_user."EmailAddress");
v_email_domain := substring(v_email from '@(.*)');
v_email_name := substring(v_email from '(.*)@');
v_email := v_email_name || '@' || v_email_domain;
IF row_user."ObjectValidFrom" > now() OR row_user."ObjectValidTo" < now() THEN
RAISE NOTICE 'Skipping User (invalid from/to): % % (%)', row_user."FirstName", row_user."LastName", v_email;
continue;
END IF;
IF v_email is null or trim(v_email) = '' THEN
RAISE NOTICE 'Skipping User (empty email): % % (%)', row_user."FirstName", row_user."LastName", v_email;
continue;
END IF;
IF row_user."FirstName" is null OR row_user."FirstName" = '' THEN
RAISE NOTICE 'Skipping User (empty first name): % % (%)', row_user."FirstName", row_user."LastName", v_email;
continue;
END IF;
IF row_user."LastName" is null OR row_user."LastName" = '' THEN
RAISE NOTICE 'Skipping User (empty last name): % % (%)', row_user."FirstName", row_user."LastName", v_email;
continue;
END IF;
IF v_email is null OR v_email = '' THEN
v_email := row_user."FirstName" || '.' || row_user."LastName" || '@nowhere.com';
END IF;
-- Split loginname into uname and aname (authority name)
v_loginname := lower(row_user."Loginname");
v_uname := substring(v_loginname from '\\\\(.*)');
v_aname := substring(v_loginname from '(.*)\\\\');
IF v_loginname like 'ex_' THEN
RAISE NOTICE 'Skipping User (ex_*): % % (%) %', row_user."FirstName", row_user."LastName", v_email, v_loginname;
continue;
END IF;
-- Determine the authority
select authority_id into v_authority_id from auth_authorities
where short_name = v_aname;
IF v_authority_id is null THEN
RAISE WARNING 'Authority not found: % % (%) (%)', row_user."FirstName", row_user."LastName", v_email, v_loginname;
continue;
END IF;
-- Check if the user already exists / has been imported already during
-- the last run of this script. PrimKey is the Secure Access ObjectID.
v_user_id := null;
select min(party_id) into v_user_id from parties
where trim(lower(email)) = trim(lower(v_email));
-- Email not found. Let's check for changed emails etc.
IF v_user_id is null THEN
select person_id into v_user_id from persons
where sa_object_id = row_user."ObjectID";
END IF;
-- Skip the import if we would overwrite a non-mnet account
select a.authority_id, a.short_name into v_old_authority_id, v_old_authority_short_name
from auth_authorities a, users u
where u.authority_id = a.authority_id and u.user_id = v_user_id;
IF v_old_authority_short_name is not null and v_old_authority_short_name != 'mnet' and v_old_authority_short_name != 'local' and v_aname = 'mnet' THEN
RAISE NOTICE 'Skipping User (do not overwrite a non-mnet account): % % (%) % vs. %', row_user."FirstName", row_user."LastName", v_email, v_loginname, v_old_authority_short_name;
continue;
END IF;
RAISE NOTICE 'Processing User #%: First=%, Last=%, Login=%, Email=%, OID=% => %', v_count, row_user."FirstName", row_user."LastName", v_loginname, v_email, row_user."ObjectID", v_user_id;
IF v_user_id is null THEN
-- Create a new user if the user wasnt there:
RAISE NOTICE 'Insert User: % % (%)', row_user."FirstName", row_user."LastName", v_email;
v_user_id := acs__add_user(
null, 'user', now(), 0, '0.0.0.0',
null, v_uname, v_email, null,
row_user."FirstName", row_user."LastName",
'hashed_password', 'salt',
v_uname, 't', 'approved'
);
INSERT INTO users_contact (user_id) VALUES (v_user_id);
INSERT INTO im_employees (employee_id) VALUES (v_user_id);
INSERT INTO im_freelancers (user_id) VALUES (v_user_id);
ELSE
-- Check that the user does not have a duplicate auth_id/username
-- (auth_id/username are not checked as "primary key", but they
-- are unique).
select user_id into v_duplicate_user_id
from users u
where u.user_id != v_user_id and
u.username = v_uname and
u.authority_id = v_authority_id;
IF v_duplicate_user_id is not null THEN
RAISE WARNING 'Skipping User because of duplicate auth/username: % % (%). auth_id=%, username=%', row_user."FirstName", row_user."LastName", v_email, v_authority_id, v_uname;
continue;
END IF;
END IF;
-- users_contact contains the address of a user.
update users_contact set
wa_line1 = row_user."Address1",
wa_line2 = row_user."Address2" || row_user."Address2",
wa_city = row_user."City",
wa_postal_code = row_user."Zip",
work_phone = row_user."PhoneOffice",
fax = row_user."Fax"
where user_id = v_user_id;
update users set
username = v_uname,
authority_id = v_authority_id
where user_id = v_user_id;
update persons set
sa_object_id = row_user."ObjectID",
first_names = row_user."FirstName",
last_name = row_user."LastName",
nav_middle_name = row_user."MiddleName",
nav_windows_login = substring(v_email from '^([^@]+)')
where person_id = v_user_id;
update im_employees set
job_title = row_user."Title"
where employee_id = v_user_id;
-- Add the user to groups he is not yet a member of:
-- Unfortunately, there may be multiple accounts for a
-- single email, so we have to build the union of the
-- group memberships of all the users accounts.
-- Get the final email address of the guy:
SELECT lower(email) into v_email from parties where party_id = v_user_id;
FOR row_group IN
select distinct
sgm.po_group_name as group_name
from sa.view_ar_relationshipall_parent_valid var,
sa_group_mapping sgm
where var."ObjectName" = sgm.sa_group_name and
var."ChildObjectID" in (
-- build the union of all group memberships of all accounts
select "ObjectID"
from sa."AR_User"
where lower(trim("EmailAddress")) = v_email
) and
sgm.po_group_name not in (
select g.group_name
from acs_rels r, groups g, membership_rels mr
where r.rel_id = mr.rel_id and
r.object_id_one = g.group_id and
mr.member_state = 'approved' and
r.object_id_two = v_user_id
)
LOOP
RAISE NOTICE 'Adding user % to group %', v_loginname, row_group.group_name;
-- Get the PO group_id
select group_id into v_group_id from groups where group_name = row_group.group_name;
-- Check of the user is alrady a member of that group...
select count(*) into v_exists_p
from acs_rels r
where object_id_one = v_group_id and
object_id_two = v_user_id;
-- Make the new user a member of the group
IF v_exists_p = 0 THEN
RAISE NOTICE 'Adding: user=%, group=%', v_user_id, v_group_id;
PERFORM membership_rel__new(
null, -- rel_id, default null
'membership_rel', -- object_type, default membership_rel
v_group_id, -- object_id_one
v_user_id, -- object_id_two
'approved', -- new__member_state, default approved
null, -- creation_user, default null
'0.0.0.0' -- creation_ip, default null
);
ELSE
RAISE NOTICE 'User % already member of group %', v_loginname, row_group.group_name;
END IF;
END LOOP;
END LOOP;
RETURN 0;
END;$BODY$ language 'plpgsql';
-- Group the import into smaller chunks.
-- Othewise the DB will block the application...
select sa_import_user ( 0,200);
select sa_import_user ( 200,200);
select sa_import_user ( 400,200);
select sa_import_user ( 600,200);
select sa_import_user ( 800,200);
select sa_import_user (1000,200);
select sa_import_user (1200,200);
select sa_import_user (1400,200);
select sa_import_user (1600,200);
select sa_import_user (1800,200);
select sa_import_user (2000,200);
select sa_import_user (2200,200);
select sa_import_user (2400,200);
select sa_import_user (2600,200);
select sa_import_user (2800,200);
select sa_import_user (3000,200);
select sa_import_user (3200,200);
select sa_import_user (3400,200);
select sa_import_user (3600,200);
select sa_import_user (3800,200);
select sa_import_user (4000,200);
select sa_import_user (4200,200);
select sa_import_user (4400,200);
select sa_import_user (4600,200);
select sa_import_user (4800,200);
select sa_import_user (5000,200);
select sa_import_user (5200,200);
select sa_import_user (5400,200);
select sa_import_user (5600,200);
select sa_import_user (5800,200);
select sa_import_user (6000,200);
select sa_import_user (6200,200);
select sa_import_user (6400,200);
select sa_import_user (6600,200);
select sa_import_user (6800,200);
-- View: "timesheet_import"
-- DROP VIEW timesheet_import;
CREATE OR REPLACE VIEW timesheet_import AS
SELECT
d."Date"::timestamp with time zone AS "day",
fr."WorkTime"::numeric(7,4) AS hours,
rtrim(e."LoginName") AS username,
rtrim(e."MT_Email") AS email,
CASE
WHEN length(fr."ProjectID"::text) >= 8 THEN fr."ProjectID"::text
ELSE 'I'::text || lpad(fr."ProjectID"::text, 4, '0'::text)
END AS navision_project_nr,
p."InternalProject" AS project_internal
FROM
(SELECT raw."EmployeeID", raw."DateID", raw."ProjectID", sum(raw."WorkTime") AS "WorkTime"
FROM timesheet."TS_FactRow" raw
GROUP BY
raw."EmployeeID", raw."DateID", raw."ProjectID"
HAVING
sum(raw."WorkTime") > 0::numeric
) fr
JOIN timesheet."TS_Date" d ON fr."DateID" = d."DateID"
JOIN timesheet."TS_Employee" e ON fr."EmployeeID" = e."EmployeeID"
JOIN timesheet."TS_Project" p ON fr."ProjectID" = p."ProjectID"
;
-- Function: timesheet_import_routine()
-- DROP FUNCTION timesheet_import_routine();
CREATE OR REPLACE FUNCTION timesheet_import_routine()
RETURNS integer AS
$BODY$
DECLARE
row RECORD;
v_user_id integer;
v_project_id integer;
v_min_date timestamp with time zone;
v_count integer;
v_hours numeric;
BEGIN
-- Determine starting date for deleting entries
SELECT min(day) INTO v_min_date FROM timesheet_import;
-------------------------------------------------------------------
-- Deal with removed timesheet entries.
-- Search for those TS entries that are present in im_hours, but
-- not present (anymore...) in timesheet_import.
RAISE NOTICE 'timesheet-import-routine.sql: Starting to delete entries';
FOR row IN
select
h.day, h.project_id, h.user_id, h.cost_id
from
im_hours h,
im_projects p,
users u
where
h.day >= v_min_date and
h.project_id = p.project_id and
h.user_id = u.user_id and
not exists (
select *
from timesheet_import t
where p.nav_project_nr = t.navision_project_nr and
lower(u.username) = lower(t.username) and
h.day = t.day
)
LOOP
RAISE NOTICE 'timesheet-import-routine.sql: Deleting im_hours entry: user_id=%, project_id=%, day=%', row.user_id, row.project_id, row.day;
-- Deal with im_costs representing the timesheet cost.
-- Needs to be deleted before we can remove the im_hours entry.
UPDATE im_hours SET cost_id = NULL
WHERE cost_id = row.cost_id;
PERFORM im_cost__delete(row.cost_id);
DELETE from im_hours
WHERE day = row.day and
project_id = row.project_id and
user_id = row.user_id;
END LOOP;
-------------------------------------------------------------------
-- Import actual timesheet records
RAISE NOTICE 'timesheet-import-routine.sql: Starting to insert entries';
FOR row IN
SELECT * FROM timesheet_import
LOOP
v_user_id = timesheet_user__get_id(row.username, row.email);
IF v_user_id IS NULL THEN
RAISE WARNING 'timesheet-import-routine.sql: Did not find username % %', row.username, row.email;
CONTINUE;
END IF;
SELECT project_id INTO v_project_id FROM im_projects
WHERE nav_project_nr = row.navision_project_nr;
IF v_project_id IS NULL THEN
RAISE WARNING 'timesheet-import-routine.sql: Did not find project %', row.navision_project_nr;
CONTINUE;
END IF;
-- RAISE NOTICE 'timesheet-import-routine.sql: % %', row.username, row.email;
SELECT count(*) into v_count from im_hours
WHERE user_id = v_user_id and
project_id = v_project_id and
"day" = row."day";
IF v_count = 0 THEN
INSERT INTO im_hours(
user_id, project_id, "day", hours, note, internal_note, material_id
) VALUES (
v_user_id, v_project_id, row."day", row."hours", NULL, NULL, NULL
);
ELSE
-- Try to avoid updates, because updates trigger the
-- cost cache, which is a quite slow procedure.
SELECT hours into v_hours FROM im_hours
WHERE user_id = v_user_id and
project_id = v_project_id and
"day" = row."day";
IF v_hours != row.hours THEN
RAISE NOTICE 'timesheet-import-routine.sql: % %: Updating existing record', row.username, row.email;
UPDATE im_hours SET
hours = row.hours
WHERE user_id = v_user_id and
project_id = v_project_id and
"day" = row."day";
END IF;
END IF;
END LOOP;
RETURN 0;
END;$BODY$ LANGUAGE 'plpgsql' VOLATILE;
SELECT timesheet_import_routine();
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment