summaryrefslogtreecommitdiff
path: root/sql/Pg-database.sql
blob: 76b2207947ad6569b93136e02af6ca681e633e31 (plain)
  1. begin;
  2. CREATE SEQUENCE id;
  3. -- Central DB structure
  4. -- This is the central database stuff which is used across all datasets
  5. -- in the ledger-smb.conf it is called 'ledgersmb' by default, but obviously
  6. -- can be named anything.
  7. -- BEGIN new entity management
  8. CREATE TABLE entity (
  9. id serial UNIQUE,
  10. name text check (name ~ '[[:alnum:]_]'),
  11. entity_class integer not null,
  12. created date not null default current_date,
  13. PRIMARY KEY(name,entity_class));
  14. COMMENT ON TABLE entity IS $$ The primary entity table to map to all contacts $$;
  15. COMMENT ON COLUMN entity.name IS $$ This is the common name of an entity. If it was a person it may be Joshua Drake, a company Acme Corp. You may also choose to use a domain such as commandprompt.com $$;
  16. CREATE TABLE entity_class (
  17. id serial primary key,
  18. class text check (class ~ '[[:alnum:]_]') NOT NULL,
  19. active boolean not null default TRUE);
  20. COMMENT ON TABLE entity_class IS $$ Defines the class type such as vendor, customer, contact, employee $$;
  21. COMMENT ON COLUMN entity_class.id IS $$ The first 7 values are reserved and permanent $$;
  22. CREATE index entity_class_idx ON entity_class(lower(class));
  23. ALTER TABLE entity ADD FOREIGN KEY (entity_class) REFERENCES entity_class(id);
  24. INSERT INTO entity_class (id,class) VALUES (1,'Vendor');
  25. INSERT INTO entity_class (id,class) VALUES (2,'Customer');
  26. INSERT INTO entity_class (id,class) VALUES (3,'Employee');
  27. INSERT INTO entity_class (id,class) VALUES (4,'Contact');
  28. INSERT INTO entity_class (id,class) VALUES (5,'Lead');
  29. INSERT INTO entity_class (id,class) VALUES (6,'Referral');
  30. SELECT setval('entity_class_id_seq',7);
  31. CREATE TABLE entity_class_to_entity (
  32. entity_class_id integer not null references entity_class(id) ON DELETE CASCADE,
  33. entity_id integer not null references entity(id) ON DELETE CASCADE,
  34. PRIMARY KEY(entity_class_id,entity_id)
  35. );
  36. COMMENT ON TABLE entity_class_to_entity IS $$ Relation builder for classes to entity $$;
  37. -- USERS stuff --
  38. CREATE TABLE users (
  39. id serial UNIQUE,
  40. username varchar(30) primary key,
  41. entity_id int not null references entity(id) on delete cascade
  42. );
  43. COMMENT ON TABLE users IS $$username is the actual primary key here because we do not want duplicate users$$;
  44. create table user_connection (
  45. user_id int not null references users(id) on delete cascade,
  46. dbname text not null,
  47. host text not null default 'localhost',
  48. port int not null default '5432'
  49. );
  50. /*
  51. CREATE VIEW users_conf as
  52. select
  53. users.id,
  54. loc.address1 || '\n'|| loc.address2 ||'\n' || loc.address3,
  55. em.employeenumber,
  56. company,
  57. loc.country,
  58. currency,
  59. dateformat,
  60. 'Pg',
  61. u_cx.host,
  62. u_cx.dbname,
  63. u_cx.dbport,
  64. users.username,
  65. p.email,
  66. p.fax,
  67. 50,
  68. p.first_name || ' ' || p.last_name,
  69. p.number_format,
  70. '', -- password
  71. ;
  72. */
  73. CREATE TABLE users_conf(id integer primary key references users(id) deferrable initially deferred,
  74. acs text,
  75. address text,
  76. businessnumber text,
  77. company text,
  78. countrycode text,
  79. currency text,
  80. dateformat text,
  81. dbdriver text default 'Pg',
  82. dbhost text default 'localhost',
  83. dbname text,
  84. dboptions text,
  85. dbpasswd text,
  86. dbport text,
  87. dbuser text,
  88. email text,
  89. fax text,
  90. menuwidth text,
  91. name text,
  92. numberformat text,
  93. password varchar(32) check(length(password) = 32),
  94. crypted_password text,
  95. print text,
  96. printer text,
  97. role text,
  98. sid text,
  99. signature text,
  100. stylesheet text,
  101. tel text,
  102. templates text,
  103. timeout numeric,
  104. vclimit numeric);
  105. COMMENT ON TABLE users_conf IS 'This is a completely dumb table that is a place holder to get usersconf into the database. Next major release will have a much more sane implementation';
  106. COMMENT ON COLUMN users_conf.id IS 'Yes primary key with a FOREIGN KEY to users(id) is correct';
  107. COMMENT ON COLUMN users_conf.password IS 'This means we have to get rid of the current password stuff and move to presumably md5()';
  108. -- The two below statements must be run from a single session
  109. /* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  110. --INSERT INTO users(username) VALUES ('admin'); -- entity_id is declared NOT NULL !!
  111. INSERT INTO users_conf(id,password) VALUES (currval('users_id_seq'),NULL);
  112. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/
  113. -- Session tracking table
  114. CREATE TABLE session(
  115. session_id serial PRIMARY KEY,
  116. token VARCHAR(32) CHECK(length(token) = 32),
  117. last_used TIMESTAMP default now(),
  118. ttl int default 3600 not null,
  119. users_id INTEGER NOT NULL references users(id),
  120. transaction_id INTEGER NOT NULL
  121. );
  122. --
  123. CREATE TABLE transactions (
  124. id int PRIMARY KEY,
  125. table_name text,
  126. locked_by int references "session" (session_id) ON DELETE SET NULL
  127. );
  128. COMMENT on TABLE transactions IS
  129. $$ This table tracks basic transactions across AR, AP, and GL related tables.
  130. It provies a referential integrity enforcement mechanism for the financial data
  131. and also some common features such as discretionary (and pessimistic) locking
  132. for long batch workflows. $$;
  133. CREATE FUNCTION lock_record (int, int) returns bool as
  134. $$
  135. declare
  136. locked int;
  137. begin
  138. SELECT locked_by into locked from transactions where id = $1;
  139. IF NOT FOUND THEN
  140. RETURN FALSE;
  141. ELSEIF locked_by is not null AND locked_by <> $2 THEN
  142. RETURN FALSE;
  143. END IF;
  144. UPDATE transactions set locked_by = $2 where id = $1;
  145. RETURN TRUE;
  146. end;
  147. $$ language plpgsql;
  148. COMMENT ON column transactions.locked_by IS
  149. $$ This should only be used in pessimistic locking measures as required by large
  150. batch work flows. $$;
  151. -- LOCATION AND COUNTRY
  152. CREATE TABLE country (
  153. id serial PRIMARY KEY,
  154. name text check (name ~ '[[:alnum:]_]') NOT NULL,
  155. short_name text check (short_name ~ '[[:alnum:]_]') NOT NULL,
  156. itu text);
  157. COMMENT ON COLUMN country.itu IS $$ The ITU Telecommunication Standardization Sector code for calling internationally. For example, the US is 1, Great Britain is 44 $$;
  158. CREATE UNIQUE INDEX country_name_idx on country(lower(name));
  159. CREATE TABLE location_class (
  160. id serial UNIQUE,
  161. class text check (class ~ '[[:alnum:]_]') not null,
  162. authoritative boolean not null,
  163. PRIMARY KEY (class,authoritative));
  164. CREATE UNIQUE INDEX lower_class_unique ON location_class(lower(class));
  165. INSERT INTO location_class(id,class,authoritative) VALUES ('1','Billing',TRUE);
  166. INSERT INTO location_class(id,class,authoritative) VALUES ('2','Sales',TRUE);
  167. INSERT INTO location_class(id,class,authoritative) VALUES ('3','Shipping',TRUE);
  168. SELECT SETVAL('location_class_id_seq',4);
  169. CREATE TABLE location (
  170. id serial PRIMARY KEY,
  171. location_class integer not null references location_class(id),
  172. line_one text check (line_one ~ '[[:alnum:]_]') NOT NULL,
  173. line_two text,
  174. line_three text,
  175. city_province text check (city_province ~ '[[:alnum:]_]') NOT NULL,
  176. country_id integer not null REFERENCES country(id),
  177. mail_code text not null check (mail_code ~ '[[:alnum:]_]'),
  178. created date not null,
  179. inactive_date timestamp default null,
  180. active boolean not null default TRUE
  181. );
  182. CREATE INDEX location_unique_class_idx ON location (id,location_class);
  183. CREATE TABLE company (
  184. id serial UNIQUE,
  185. entity_id integer not null references entity(id),
  186. legal_name text check (legal_name ~ '[[:alnum:]_]'),
  187. tax_id text,
  188. created date default current_date not null,
  189. PRIMARY KEY (entity_id,legal_name));
  190. COMMENT ON COLUMN company.tax_id IS $$ In the US this would be a EIN. $$;
  191. CREATE TABLE company_to_location (
  192. location_id integer references location(id) not null,
  193. company_id integer not null references company(id) ON DELETE CASCADE,
  194. PRIMARY KEY(location_id,company_id));
  195. CREATE TABLE salutation (
  196. id serial unique,
  197. salutation text primary key);
  198. INSERT INTO salutation (id,salutation) VALUES ('1','Dr.');
  199. INSERT INTO salutation (id,salutation) VALUES ('2','Miss.');
  200. INSERT INTO salutation (id,salutation) VALUES ('3','Mr.');
  201. INSERT INTO salutation (id,salutation) VALUES ('4','Mrs.');
  202. INSERT INTO salutation (id,salutation) VALUES ('5','Ms.');
  203. INSERT INTO salutation (id,salutation) VALUES ('6','Sir.');
  204. SELECT SETVAL('salutation_id_seq',7);
  205. CREATE TABLE person (
  206. id serial PRIMARY KEY,
  207. entity_id integer references entity(id) not null,
  208. salutation_id integer references salutation(id),
  209. first_name text check (first_name ~ '[[:alnum:]_]') NOT NULL,
  210. middle_name text,
  211. last_name text check (last_name ~ '[[:alnum:]_]') NOT NULL,
  212. created date not null default current_date
  213. );
  214. COMMENT ON TABLE person IS $$ Every person, must have an entity to derive a common or display name. The correct way to get class information on a person would be person.entity_id->entity_class_to_entity.entity_id. $$;
  215. CREATE TABLE person_to_location (
  216. location_id integer not null references location(id),
  217. person_id integer not null references person(id) ON DELETE CASCADE,
  218. PRIMARY KEY (location_id,person_id));
  219. CREATE TABLE person_to_company (
  220. location_id integer references location(id) not null,
  221. person_id integer not null references person(id) ON DELETE CASCADE,
  222. company_id integer not null references company(id) ON DELETE CASCADE,
  223. PRIMARY KEY (location_id,person_id));
  224. CREATE TABLE entity_other_name (
  225. entity_id integer not null references entity(id) ON DELETE CASCADE,
  226. other_name text check (other_name ~ '[[:alnum:]_]'),
  227. PRIMARY KEY (other_name, entity_id));
  228. COMMENT ON TABLE entity_other_name IS $$ Similar to company_other_name, a person may be jd, Joshua Drake, linuxpoet... all are the same person. $$;
  229. CREATE TABLE person_to_entity (
  230. person_id integer not null references person(id) ON DELETE CASCADE,
  231. entity_id integer not null check (entity_id != person_id) references entity(id) ON DELETE CASCADE,
  232. related_how text,
  233. created date not null default current_date,
  234. PRIMARY KEY (person_id,entity_id));
  235. CREATE TABLE company_to_entity (
  236. company_id integer not null references company(id) ON DELETE CASCADE,
  237. entity_id integer check (company_id != entity_id) not null references entity(id) ON DELETE CASCADE,
  238. related_how text,
  239. created date not null default current_date,
  240. PRIMARY KEY (company_id,entity_id));
  241. CREATE TABLE contact_class (
  242. id serial UNIQUE,
  243. class text check (class ~ '[[:alnum:]_]') NOT NULL,
  244. PRIMARY KEY (class));
  245. CREATE UNIQUE INDEX contact_class_class_idx ON contact_class(lower(class));
  246. INSERT INTO contact_class (id,class) values (1,'Primary Phone');
  247. INSERT INTO contact_class (id,class) values (2,'Secondary Phone');
  248. INSERT INTO contact_class (id,class) values (3,'Cell Phone');
  249. INSERT INTO contact_class (id,class) values (4,'AIM');
  250. INSERT INTO contact_class (id,class) values (5,'Yahoo');
  251. INSERT INTO contact_class (id,class) values (6,'Gtalk');
  252. INSERT INTO contact_class (id,class) values (7,'MSN');
  253. INSERT INTO contact_class (id,class) values (8,'IRC');
  254. INSERT INTO contact_class (id,class) values (9,'Fax');
  255. INSERT INTO contact_class (id,class) values (10,'Generic Jabber');
  256. INSERT INTO contact_class (id,class) values (11,'Home Phone');
  257. INSERT INTO contact_class (id,class) values (12,'Email');
  258. SELECT SETVAL('contact_class_id_seq',12);
  259. CREATE TABLE person_to_contact (
  260. person_id integer not null references person(id) ON DELETE CASCADE,
  261. contact_class_id integer references contact_class(id) not null,
  262. contact text check(contact ~ '[[:alnum:]_]') not null,
  263. PRIMARY KEY (person_id,contact_class_id,contact));
  264. COMMENT ON TABLE person_to_contact IS $$ To keep track of the relationship between multiple contact methods and a single individual $$;
  265. CREATE TABLE company_to_contact (
  266. company_id integer not null references company(id) ON DELETE CASCADE,
  267. contact_class_id integer references contact_class(id) not null,
  268. contact text check(contact ~ '[[:alnum:]_]') not null,
  269. PRIMARY KEY (company_id,contact_class_id,contact));
  270. COMMENT ON TABLE company_to_contact IS $$ To keep track of the relationship between multiple contact methods and a single company $$;
  271. -- Begin rocking notes interface
  272. CREATE TABLE note_class(id serial primary key, class text not null check (class ~ '[[:alnum:]_]'));
  273. INSERT INTO note_class(id,class) VALUES (1,'Entity');
  274. INSERT INTO note_class(id,class) VALUES (2,'Invoice');
  275. CREATE UNIQUE INDEX note_class_idx ON note_class(lower(class));
  276. CREATE TABLE note (id serial primary key, note_class integer not null references note_class(id),
  277. note text not null, vector tsvector not null,
  278. created timestamp not null default now(),
  279. ref_key integer not null);
  280. CREATE TABLE entity_note() INHERITS (note);
  281. ALTER TABLE entity_note ADD CHECK (note_class = 1);
  282. ALTER TABLE entity_note ADD FOREIGN KEY (ref_key) REFERENCES entity(id) ON DELETE CASCADE;
  283. CREATE INDEX entity_note_id_idx ON entity_note(id);
  284. CREATE UNIQUE INDEX entity_note_class_idx ON note_class(lower(class));
  285. CREATE INDEX entity_note_vectors_idx ON entity_note USING gist(vector);
  286. CREATE TABLE invoice_note() INHERITS (note);
  287. CREATE INDEX invoice_note_id_idx ON invoice_note(id);
  288. CREATE UNIQUE INDEX invoice_note_class_idx ON note_class(lower(class));
  289. CREATE INDEX invoice_note_vectors_idx ON invoice_note USING gist(vector);
  290. -- END entity
  291. --
  292. CREATE TABLE makemodel (
  293. parts_id int PRIMARY KEY,
  294. make text,
  295. model text
  296. );
  297. --
  298. CREATE TABLE gl (
  299. id int DEFAULT nextval ( 'id' ) PRIMARY KEY REFERENCES transactions(id),
  300. reference text,
  301. description text,
  302. transdate date DEFAULT current_date,
  303. person_id integer references person(id),
  304. notes text,
  305. approved bool default true,
  306. department_id int default 0
  307. );
  308. --
  309. CREATE TABLE chart (
  310. id serial PRIMARY KEY,
  311. accno text NOT NULL,
  312. description text,
  313. charttype char(1) DEFAULT 'A',
  314. category char(1),
  315. link text,
  316. gifi_accno text,
  317. contra bool DEFAULT 'f'
  318. );
  319. --
  320. CREATE TABLE gifi (
  321. accno text PRIMARY KEY,
  322. description text
  323. );
  324. --
  325. CREATE TABLE defaults (
  326. setting_key text primary key,
  327. value text
  328. );
  329. /*
  330. inventory_accno_id int,
  331. income_accno_id int,
  332. expense_accno_id int,
  333. fxgain_accno_id int,
  334. fxloss_accno_id int,
  335. */
  336. \COPY defaults FROM stdin WITH DELIMITER |
  337. sinumber|1
  338. sonumber|1
  339. yearend|1
  340. businessnumber|1
  341. version|1.2.0
  342. closedto|\N
  343. revtrans|1
  344. ponumber|1
  345. sqnumber|1
  346. rfqnumber|1
  347. audittrail|0
  348. vinumber|1
  349. employeenumber|1
  350. partnumber|1
  351. customernumber|1
  352. vendornumber|1
  353. glnumber|1
  354. projectnumber|1
  355. \.
  356. -- */
  357. CREATE TABLE acc_trans (
  358. trans_id int NOT NULL REFERENCES transactions(id),
  359. chart_id int NOT NULL REFERENCES chart (id),
  360. amount NUMERIC,
  361. transdate date DEFAULT current_date,
  362. source text,
  363. cleared bool DEFAULT 'f',
  364. fx_transaction bool DEFAULT 'f',
  365. project_id int,
  366. memo text,
  367. invoice_id int,
  368. approved bool default true,
  369. entry_id SERIAL PRIMARY KEY
  370. );
  371. --
  372. CREATE TABLE invoice (
  373. id serial PRIMARY KEY,
  374. trans_id int,
  375. parts_id int,
  376. description text,
  377. qty integer,
  378. allocated integer,
  379. sellprice NUMERIC,
  380. fxsellprice NUMERIC,
  381. discount float4, -- jd: check into this
  382. assemblyitem bool DEFAULT 'f',
  383. unit varchar(5),
  384. project_id int,
  385. deliverydate date,
  386. serialnumber text,
  387. notes text
  388. );
  389. -- Added for Entity but can't be added due to order
  390. ALTER TABLE invoice_note ADD FOREIGN KEY (ref_key) REFERENCES invoice(id);
  391. --
  392. --
  393. -- pricegroup added here due to references
  394. CREATE TABLE pricegroup (
  395. id serial PRIMARY KEY,
  396. pricegroup text
  397. );
  398. CREATE TABLE entity_credit_account (
  399. id serial not null unique,
  400. entity_id int not null references entity(id) ON DELETE CASCADE,
  401. entity_class int not null references entity_class(id) check ( entity_class in (1,2) ),
  402. discount numeric,
  403. discount_terms int default 0,
  404. taxincluded bool default 'f',
  405. creditlimit NUMERIC default 0,
  406. terms int2 default 0,
  407. meta_number varchar(32),
  408. cc text,
  409. bcc text,
  410. business_id int,
  411. language_code varchar(6),
  412. pricegroup_id int references pricegroup(id),
  413. curr char(3),
  414. startdate date DEFAULT CURRENT_DATE,
  415. enddate date,
  416. threshold numeric default 0,
  417. PRIMARY KEY(entity_id, meta_number)
  418. );
  419. CREATE TABLE entity_bank_account (
  420. id serial not null,
  421. entity_id int not null references entity(id) ON DELETE CASCADE,
  422. bic varchar,
  423. iban varchar,
  424. UNIQUE (id),
  425. PRIMARY KEY (entity_id, bic, iban)
  426. );
  427. CREATE VIEW customer AS
  428. SELECT
  429. c.id,
  430. emd.entity_id,
  431. emd.entity_class,
  432. emd.discount,
  433. emd.taxincluded,
  434. emd.creditlimit,
  435. emd.terms,
  436. emd.meta_number as customernumber,
  437. emd.cc,
  438. emd.bcc,
  439. emd.business_id,
  440. emd.language_code,
  441. emd.pricegroup_id,
  442. emd.curr,
  443. emd.startdate,
  444. emd.enddate,
  445. eba.bic,
  446. eba.iban,
  447. ein.note as invoice_notes
  448. FROM entity_credit_account emd
  449. join entity_bank_account eba on emd.entity_id = eba.entity_id
  450. Left join entity_note ein on ein.ref_key = emd.entity_id
  451. join company c on c.entity_id = emd.entity_id
  452. where emd.entity_class = 2;
  453. CREATE VIEW vendor AS
  454. SELECT
  455. c.id,
  456. emd.entity_id,
  457. emd.entity_class,
  458. emd.discount,
  459. emd.taxincluded,
  460. emd.creditlimit,
  461. emd.terms,
  462. emd.meta_number as vendornumber,
  463. emd.cc,
  464. emd.bcc,
  465. emd.business_id,
  466. emd.language_code,
  467. emd.pricegroup_id,
  468. emd.curr,
  469. emd.startdate,
  470. emd.enddate,
  471. eba.bic,
  472. eba.iban,
  473. ein.note as
  474. invoice_notes
  475. FROM entity_credit_account emd
  476. join entity_bank_account eba on emd.entity_id = eba.entity_id
  477. left join entity_note ein on ein.ref_key = emd.entity_id
  478. join company c on c.entity_id = emd.entity_id
  479. where emd.entity_class = 1;
  480. COMMENT ON TABLE entity_credit_account IS $$ This is a metadata table for ALL entities in LSMB; it deprecates the use of customer and vendor specific tables (which were nearly identical and largely redundant), and replaces it with a single point of metadata. $$;
  481. COMMENT ON COLUMN entity_credit_account.entity_id IS $$ This is the relationship between entities and their metadata. $$;
  482. COMMENT ON COLUMN entity_credit_account.entity_class IS $$ A reference to entity_class, requiring that entity_credit_account only apply to vendors and customers, using the entity_class table as the Point Of Truth. $$;
  483. ALTER TABLE company ADD COLUMN sic_code varchar;
  484. --
  485. --
  486. create table employee (
  487. entity_id integer not null references entity(id) on delete cascade primary key,
  488. entity_class_id integer references entity_class(id) not null check (entity_class_id = 3),
  489. login text,
  490. startdate date default current_date,
  491. enddate date,
  492. notes text,
  493. role varchar(20),
  494. sales bool default 'f',
  495. ssn varchar(20),
  496. iban varchar(34),
  497. bic varchar(11),
  498. managerid int,
  499. employeenumber varchar(32),
  500. dob date
  501. );
  502. COMMENT ON TABLE employee IS $$ Is a metadata table specific to employees $$;
  503. CREATE TABLE parts (
  504. id serial PRIMARY KEY,
  505. partnumber text,
  506. description text,
  507. unit varchar(5),
  508. listprice NUMERIC,
  509. sellprice NUMERIC,
  510. lastcost NUMERIC,
  511. priceupdate date DEFAULT current_date,
  512. weight numeric,
  513. onhand numeric DEFAULT 0,
  514. notes text,
  515. makemodel bool DEFAULT 'f',
  516. assembly bool DEFAULT 'f',
  517. alternate bool DEFAULT 'f',
  518. rop float4, -- jd: what is this
  519. inventory_accno_id int,
  520. income_accno_id int,
  521. expense_accno_id int,
  522. bin text,
  523. obsolete bool DEFAULT 'f',
  524. bom bool DEFAULT 'f',
  525. image text,
  526. drawing text,
  527. microfiche text,
  528. partsgroup_id int,
  529. project_id int,
  530. avgcost NUMERIC
  531. );
  532. CREATE UNIQUE INDEX parts_partnumber_index_u ON parts (partnumber)
  533. WHERE obsolete is false;
  534. --
  535. CREATE TABLE assembly (
  536. id int,
  537. parts_id int,
  538. qty numeric,
  539. bom bool,
  540. adj bool,
  541. PRIMARY KEY (id, parts_id)
  542. );
  543. --
  544. CREATE TABLE ar (
  545. id int DEFAULT nextval ( 'id' ) PRIMARY KEY REFERENCES transactions(id),
  546. invnumber text,
  547. transdate date DEFAULT current_date,
  548. entity_id int REFERENCES entity(id),
  549. taxincluded bool,
  550. amount NUMERIC,
  551. netamount NUMERIC,
  552. paid NUMERIC,
  553. datepaid date,
  554. duedate date,
  555. invoice bool DEFAULT 'f',
  556. shippingpoint text,
  557. terms int2 DEFAULT 0,
  558. notes text,
  559. curr char(3),
  560. ordnumber text,
  561. person_id integer references employee(entity_id),
  562. till varchar(20),
  563. quonumber text,
  564. intnotes text,
  565. department_id int default 0,
  566. shipvia text,
  567. language_code varchar(6),
  568. ponumber text,
  569. on_hold bool default false,
  570. reverse bool default false,
  571. approved bool default true
  572. );
  573. COMMENT ON COLUMN ar.entity_id IS $$ Used to be customer_id, but customer is now metadata. You need to push to entity $$;
  574. --
  575. CREATE TABLE ap (
  576. id int DEFAULT nextval ( 'id' ) PRIMARY KEY REFERENCES transactions(id),
  577. invnumber text,
  578. transdate date DEFAULT current_date,
  579. entity_id int REFERENCES entity(id),
  580. taxincluded bool DEFAULT 'f',
  581. amount NUMERIC,
  582. netamount NUMERIC,
  583. paid NUMERIC,
  584. datepaid date,
  585. duedate date,
  586. invoice bool DEFAULT 'f',
  587. ordnumber text,
  588. curr char(3),
  589. notes text,
  590. person_id integer references employee(entity_id),
  591. till varchar(20),
  592. quonumber text,
  593. intnotes text,
  594. department_id int DEFAULT 0,
  595. shipvia text,
  596. language_code varchar(6),
  597. ponumber text,
  598. shippingpoint text,
  599. on_hold bool default false,
  600. approved bool default true,
  601. reverse bool default false,
  602. terms int2 DEFAULT 0
  603. );
  604. COMMENT ON COLUMN ap.entity_id IS $$ Used to be customer_id, but customer is now metadata. You need to push to entity $$;
  605. --
  606. CREATE TABLE taxmodule (
  607. taxmodule_id serial PRIMARY KEY,
  608. taxmodulename text NOT NULL
  609. );
  610. --
  611. CREATE TABLE taxcategory (
  612. taxcategory_id serial PRIMARY KEY,
  613. taxcategoryname text NOT NULL,
  614. taxmodule_id int NOT NULL,
  615. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  616. );
  617. --
  618. CREATE TABLE partstax (
  619. parts_id int,
  620. chart_id int,
  621. taxcategory_id int,
  622. PRIMARY KEY (parts_id, chart_id),
  623. FOREIGN KEY (parts_id) REFERENCES parts (id) on delete cascade,
  624. FOREIGN KEY (chart_id) REFERENCES chart (id),
  625. FOREIGN KEY (taxcategory_id) REFERENCES taxcategory (taxcategory_id)
  626. );
  627. --
  628. CREATE TABLE tax (
  629. chart_id int PRIMARY KEY,
  630. rate numeric,
  631. taxnumber text,
  632. validto date,
  633. pass integer DEFAULT 0 NOT NULL,
  634. taxmodule_id int DEFAULT 1 NOT NULL,
  635. FOREIGN KEY (chart_id) REFERENCES chart (id),
  636. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  637. );
  638. --
  639. CREATE TABLE customertax (
  640. customer_id int references entity_credit_account(id) on delete cascade,
  641. chart_id int,
  642. PRIMARY KEY (customer_id, chart_id)
  643. );
  644. --
  645. CREATE TABLE vendortax (
  646. vendor_id int references entity_credit_account(id) on delete cascade,
  647. chart_id int,
  648. PRIMARY KEY (vendor_id, chart_id)
  649. );
  650. --
  651. CREATE TABLE oe_class (
  652. id smallint unique check(id IN (1,2,3,4)),
  653. oe_class text primary key);
  654. INSERT INTO oe_class(id,oe_class) values (1,'Sales Order');
  655. INSERT INTO oe_class(id,oe_class) values (2,'Purchase Order');
  656. INSERT INTO oe_class(id,oe_class) values (3,'Quotation');
  657. INSERT INTO oe_class(id,oe_class) values (4,'RFQ');
  658. COMMENT ON TABLE oe_class IS $$ This could probably be done better. But I need to remove the customer_id/vendor_id relationship and instead rely on a classification $$;
  659. CREATE TABLE oe (
  660. id serial PRIMARY KEY,
  661. ordnumber text,
  662. transdate date default current_date,
  663. entity_id integer references entity(id),
  664. amount NUMERIC,
  665. netamount NUMERIC,
  666. reqdate date,
  667. taxincluded bool,
  668. shippingpoint text,
  669. notes text,
  670. curr char(3),
  671. person_id integer references person(id),
  672. closed bool default 'f',
  673. quotation bool default 'f',
  674. quonumber text,
  675. intnotes text,
  676. department_id int default 0,
  677. shipvia text,
  678. language_code varchar(6),
  679. ponumber text,
  680. terms int2 DEFAULT 0,
  681. oe_class_id int references oe_class(id) NOT NULL
  682. );
  683. --
  684. CREATE TABLE orderitems (
  685. id serial PRIMARY KEY,
  686. trans_id int,
  687. parts_id int,
  688. description text,
  689. qty numeric,
  690. sellprice NUMERIC,
  691. discount numeric,
  692. unit varchar(5),
  693. project_id int,
  694. reqdate date,
  695. ship numeric,
  696. serialnumber text,
  697. notes text
  698. );
  699. --
  700. CREATE TABLE exchangerate (
  701. curr char(3),
  702. transdate date,
  703. buy numeric,
  704. sell numeric,
  705. PRIMARY KEY (curr, transdate)
  706. );
  707. --
  708. -- batch stuff
  709. CREATE TABLE batch_class (
  710. id serial unique,
  711. class varchar primary key
  712. );
  713. insert into batch_class (id,class) values (1,'ap');
  714. insert into batch_class (id,class) values (2,'ar');
  715. insert into batch_class (id,class) values (3,'payment');
  716. insert into batch_class (id,class) values (4,'payment_reversal');
  717. insert into batch_class (id,class) values (5,'gl');
  718. SELECT SETVAL('batch_class_id_seq',6);
  719. CREATE TABLE batch (
  720. id serial primary key,
  721. batch_class_id integer references batch_class(id) not null,
  722. description text,
  723. approved_on date default null,
  724. approved_by int references employee(entity_id),
  725. created_by int references employee(entity_id),
  726. locked_by int references session(session_id),
  727. created_on date default now()
  728. );
  729. CREATE TABLE voucher (
  730. trans_id int,
  731. batch_id int references batch(id) not null,
  732. id serial primary key
  733. );
  734. --
  735. create table shipto (
  736. trans_id int,
  737. shiptoname varchar(64),
  738. shiptoaddress1 varchar(32),
  739. shiptoaddress2 varchar(32),
  740. shiptocity varchar(32),
  741. shiptostate varchar(32),
  742. shiptozipcode varchar(10),
  743. shiptocountry varchar(32),
  744. shiptocontact varchar(64),
  745. shiptophone varchar(20),
  746. shiptofax varchar(20),
  747. shiptoemail text,
  748. entry_id SERIAL PRIMARY KEY
  749. );
  750. -- SHIPTO really needs to be pushed into entities too
  751. --
  752. --
  753. CREATE TABLE project (
  754. id serial PRIMARY KEY,
  755. projectnumber text,
  756. description text,
  757. startdate date,
  758. enddate date,
  759. parts_id int,
  760. production numeric default 0,
  761. completed numeric default 0,
  762. customer_id int
  763. );
  764. --
  765. CREATE TABLE partsgroup (
  766. id serial PRIMARY KEY,
  767. partsgroup text
  768. );
  769. --
  770. CREATE TABLE status (
  771. trans_id int PRIMARY KEY,
  772. formname text,
  773. printed bool default 'f',
  774. emailed bool default 'f',
  775. spoolfile text
  776. );
  777. --
  778. CREATE TABLE department (
  779. id serial PRIMARY KEY,
  780. description text,
  781. role char(1) default 'P'
  782. );
  783. --
  784. -- department transaction table
  785. CREATE TABLE dpt_trans (
  786. trans_id int PRIMARY KEY,
  787. department_id int
  788. );
  789. --
  790. -- business table
  791. CREATE TABLE business (
  792. id serial PRIMARY KEY,
  793. description text,
  794. discount numeric
  795. );
  796. --
  797. -- SIC
  798. CREATE TABLE sic (
  799. code varchar(6) PRIMARY KEY,
  800. sictype char(1),
  801. description text
  802. );
  803. --
  804. CREATE TABLE warehouse (
  805. id serial PRIMARY KEY,
  806. description text
  807. );
  808. --
  809. CREATE TABLE inventory (
  810. entity_id integer references employee(entity_id),
  811. warehouse_id int,
  812. parts_id int,
  813. trans_id int,
  814. orderitems_id int,
  815. qty numeric,
  816. shippingdate date,
  817. entry_id SERIAL PRIMARY KEY
  818. );
  819. --
  820. CREATE TABLE yearend (
  821. trans_id int PRIMARY KEY,
  822. transdate date
  823. );
  824. --
  825. CREATE TABLE partsvendor (
  826. entity_id int not null references entity_credit_account(id) on delete cascade,
  827. parts_id int,
  828. partnumber text,
  829. leadtime int2,
  830. lastcost NUMERIC,
  831. curr char(3),
  832. entry_id SERIAL PRIMARY KEY
  833. );
  834. --
  835. CREATE TABLE partscustomer (
  836. parts_id int,
  837. customer_id int not null references entity_credit_account(id) on delete cascade,
  838. pricegroup_id int,
  839. pricebreak numeric,
  840. sellprice NUMERIC,
  841. validfrom date,
  842. validto date,
  843. curr char(3),
  844. entry_id SERIAL PRIMARY KEY
  845. );
  846. -- How does partscustomer.customer_id relate here?
  847. --
  848. CREATE TABLE language (
  849. code varchar(6) PRIMARY KEY,
  850. description text
  851. );
  852. --
  853. CREATE TABLE audittrail (
  854. trans_id int,
  855. tablename text,
  856. reference text,
  857. formname text,
  858. action text,
  859. transdate timestamp default current_timestamp,
  860. person_id integer references person(id) not null,
  861. entry_id BIGSERIAL PRIMARY KEY
  862. );
  863. --
  864. CREATE TABLE translation (
  865. trans_id int,
  866. language_code varchar(6),
  867. description text,
  868. PRIMARY KEY (trans_id, language_code)
  869. );
  870. --
  871. CREATE TABLE recurring (
  872. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  873. reference text,
  874. startdate date,
  875. nextdate date,
  876. enddate date,
  877. repeat int2,
  878. unit varchar(6),
  879. howmany int,
  880. payment bool default 'f'
  881. );
  882. --
  883. CREATE TABLE recurringemail (
  884. id int PRIMARY KEY,
  885. formname text,
  886. format text,
  887. message text
  888. );
  889. --
  890. CREATE TABLE recurringprint (
  891. id int PRIMARY KEY,
  892. formname text,
  893. format text,
  894. printer text
  895. );
  896. --
  897. CREATE TABLE jcitems (
  898. id serial PRIMARY KEY,
  899. project_id int,
  900. parts_id int,
  901. description text,
  902. qty numeric,
  903. allocated numeric,
  904. sellprice NUMERIC,
  905. fxsellprice NUMERIC,
  906. serialnumber text,
  907. checkedin timestamp with time zone,
  908. checkedout timestamp with time zone,
  909. person_id integer references person(id) not null,
  910. notes text
  911. );
  912. insert into transactions (id, table_name) SELECT id, 'ap' FROM ap;
  913. insert into transactions (id, table_name) SELECT id, 'ar' FROM ap;
  914. INSERT INTO transactions (id, table_name) SELECT id, 'gl' FROM gl;
  915. CREATE OR REPLACE FUNCTION track_global_sequence() RETURNS TRIGGER AS
  916. $$
  917. BEGIN
  918. IF tg_op = 'INSERT' THEN
  919. INSERT INTO transactions (id, table_name)
  920. VALUES (new.id, TG_RELNAME);
  921. ELSEIF tg_op = 'UPDATE' THEN
  922. IF new.id = old.id THEN
  923. return new;
  924. ELSE
  925. UPDATE transactions SET id = new.id WHERE id = old.id;
  926. END IF;
  927. ELSE
  928. DELETE FROM transactions WHERE id = old_id;
  929. END IF;
  930. RETURN new;
  931. END;
  932. $$ LANGUAGE PLPGSQL;
  933. CREATE TRIGGER ap_track_global_sequence before insert or update or delete on ap
  934. for each row execute procedure track_global_sequence();
  935. CREATE TRIGGER ar_track_global_sequence before insert or update or delete on ar
  936. for each row execute procedure track_global_sequence();
  937. CREATE TRIGGER gl_track_global_sequence before insert or update or delete on gl
  938. for each row execute procedure track_global_sequence();
  939. CREATE TABLE custom_table_catalog (
  940. table_id SERIAL PRIMARY KEY,
  941. extends TEXT,
  942. table_name TEXT
  943. );
  944. CREATE TABLE custom_field_catalog (
  945. field_id SERIAL PRIMARY KEY,
  946. table_id INT REFERENCES custom_table_catalog,
  947. field_name TEXT
  948. );
  949. INSERT INTO taxmodule (
  950. taxmodule_id, taxmodulename
  951. ) VALUES (
  952. 1, 'Simple'
  953. );
  954. create index acc_trans_trans_id_key on acc_trans (trans_id);
  955. create index acc_trans_chart_id_key on acc_trans (chart_id);
  956. create index acc_trans_transdate_key on acc_trans (transdate);
  957. create index acc_trans_source_key on acc_trans (lower(source));
  958. --
  959. create index ap_id_key on ap (id);
  960. create index ap_transdate_key on ap (transdate);
  961. create index ap_invnumber_key on ap (invnumber);
  962. create index ap_ordnumber_key on ap (ordnumber);
  963. create index ap_quonumber_key on ap (quonumber);
  964. --
  965. create index ar_id_key on ar (id);
  966. create index ar_transdate_key on ar (transdate);
  967. create index ar_invnumber_key on ar (invnumber);
  968. create index ar_ordnumber_key on ar (ordnumber);
  969. create index ar_quonumber_key on ar (quonumber);
  970. --
  971. create index assembly_id_key on assembly (id);
  972. --
  973. create index chart_id_key on chart (id);
  974. create unique index chart_accno_key on chart (accno);
  975. create index chart_category_key on chart (category);
  976. create index chart_link_key on chart (link);
  977. create index chart_gifi_accno_key on chart (gifi_accno);
  978. --
  979. create index customer_customer_id_key on customertax (customer_id);
  980. --
  981. create unique index employee_login_key on employee (login);
  982. --
  983. create index exchangerate_ct_key on exchangerate (curr, transdate);
  984. --
  985. create unique index gifi_accno_key on gifi (accno);
  986. --
  987. create index gl_id_key on gl (id);
  988. create index gl_transdate_key on gl (transdate);
  989. create index gl_reference_key on gl (reference);
  990. create index gl_description_key on gl (lower(description));
  991. --
  992. create index invoice_id_key on invoice (id);
  993. create index invoice_trans_id_key on invoice (trans_id);
  994. --
  995. create index makemodel_parts_id_key on makemodel (parts_id);
  996. create index makemodel_make_key on makemodel (lower(make));
  997. create index makemodel_model_key on makemodel (lower(model));
  998. --
  999. create index oe_id_key on oe (id);
  1000. create index oe_transdate_key on oe (transdate);
  1001. create index oe_ordnumber_key on oe (ordnumber);
  1002. create index orderitems_trans_id_key on orderitems (trans_id);
  1003. create index orderitems_id_key on orderitems (id);
  1004. --
  1005. create index parts_id_key on parts (id);
  1006. create index parts_partnumber_key on parts (lower(partnumber));
  1007. create index parts_description_key on parts (lower(description));
  1008. create index partstax_parts_id_key on partstax (parts_id);
  1009. --
  1010. --
  1011. create index shipto_trans_id_key on shipto (trans_id);
  1012. --
  1013. create index project_id_key on project (id);
  1014. create unique index projectnumber_key on project (projectnumber);
  1015. --
  1016. create index partsgroup_id_key on partsgroup (id);
  1017. create unique index partsgroup_key on partsgroup (partsgroup);
  1018. --
  1019. create index status_trans_id_key on status (trans_id);
  1020. --
  1021. create index department_id_key on department (id);
  1022. --
  1023. create index partsvendor_parts_id_key on partsvendor (parts_id);
  1024. --
  1025. create index pricegroup_pricegroup_key on pricegroup (pricegroup);
  1026. create index pricegroup_id_key on pricegroup (id);
  1027. --
  1028. create index audittrail_trans_id_key on audittrail (trans_id);
  1029. --
  1030. create index translation_trans_id_key on translation (trans_id);
  1031. --
  1032. create unique index language_code_key on language (code);
  1033. --
  1034. create index jcitems_id_key on jcitems (id);
  1035. -- Popular some entity data
  1036. INSERT INTO country(short_name,name) VALUES ('AC','Ascension Island');
  1037. INSERT INTO country(short_name,name) VALUES ('AD','Andorra');
  1038. INSERT INTO country(short_name,name) VALUES ('AE','United Arab Emirates');
  1039. INSERT INTO country(short_name,name) VALUES ('AF','Afghanistan');
  1040. INSERT INTO country(short_name,name) VALUES ('AG','Antigua and Barbuda');
  1041. INSERT INTO country(short_name,name) VALUES ('AI','Anguilla');
  1042. INSERT INTO country(short_name,name) VALUES ('AL','Albania');
  1043. INSERT INTO country(short_name,name) VALUES ('AM','Armenia');
  1044. INSERT INTO country(short_name,name) VALUES ('AN','Netherlands Antilles');
  1045. INSERT INTO country(short_name,name) VALUES ('AO','Angola');
  1046. INSERT INTO country(short_name,name) VALUES ('AQ','Antarctica');
  1047. INSERT INTO country(short_name,name) VALUES ('AR','Argentina');
  1048. INSERT INTO country(short_name,name) VALUES ('AS','American Samoa');
  1049. INSERT INTO country(short_name,name) VALUES ('AT','Austria');
  1050. INSERT INTO country(short_name,name) VALUES ('AU','Australia');
  1051. INSERT INTO country(short_name,name) VALUES ('AW','Aruba');
  1052. INSERT INTO country(short_name,name) VALUES ('AX','Aland Islands');
  1053. INSERT INTO country(short_name,name) VALUES ('AZ','Azerbaijan');
  1054. INSERT INTO country(short_name,name) VALUES ('BA','Bosnia and Herzegovina');
  1055. INSERT INTO country(short_name,name) VALUES ('BB','Barbados');
  1056. INSERT INTO country(short_name,name) VALUES ('BD','Bangladesh');
  1057. INSERT INTO country(short_name,name) VALUES ('BE','Belgium');
  1058. INSERT INTO country(short_name,name) VALUES ('BF','Burkina Faso');
  1059. INSERT INTO country(short_name,name) VALUES ('BG','Bulgaria');
  1060. INSERT INTO country(short_name,name) VALUES ('BH','Bahrain');
  1061. INSERT INTO country(short_name,name) VALUES ('BI','Burundi');
  1062. INSERT INTO country(short_name,name) VALUES ('BJ','Benin');
  1063. INSERT INTO country(short_name,name) VALUES ('BM','Bermuda');
  1064. INSERT INTO country(short_name,name) VALUES ('BN','Brunei Darussalam');
  1065. INSERT INTO country(short_name,name) VALUES ('BO','Bolivia');
  1066. INSERT INTO country(short_name,name) VALUES ('BR','Brazil');
  1067. INSERT INTO country(short_name,name) VALUES ('BS','Bahamas');
  1068. INSERT INTO country(short_name,name) VALUES ('BT','Bhutan');
  1069. INSERT INTO country(short_name,name) VALUES ('BV','Bouvet Island');
  1070. INSERT INTO country(short_name,name) VALUES ('BW','Botswana');
  1071. INSERT INTO country(short_name,name) VALUES ('BY','Belarus');
  1072. INSERT INTO country(short_name,name) VALUES ('BZ','Belize');
  1073. INSERT INTO country(short_name,name) VALUES ('CA','Canada');
  1074. INSERT INTO country(short_name,name) VALUES ('CC','Cocos (Keeling) Islands');
  1075. INSERT INTO country(short_name,name) VALUES ('CD','Congo, Democratic Republic');
  1076. INSERT INTO country(short_name,name) VALUES ('CF','Central African Republic');
  1077. INSERT INTO country(short_name,name) VALUES ('CG','Congo');
  1078. INSERT INTO country(short_name,name) VALUES ('CH','Switzerland');
  1079. INSERT INTO country(short_name,name) VALUES ('CI','Cote D\'Ivoire (Ivory Coast)');
  1080. INSERT INTO country(short_name,name) VALUES ('CK','Cook Islands');
  1081. INSERT INTO country(short_name,name) VALUES ('CL','Chile');
  1082. INSERT INTO country(short_name,name) VALUES ('CM','Cameroon');
  1083. INSERT INTO country(short_name,name) VALUES ('CN','China');
  1084. INSERT INTO country(short_name,name) VALUES ('CO','Colombia');
  1085. INSERT INTO country(short_name,name) VALUES ('CR','Costa Rica');
  1086. INSERT INTO country(short_name,name) VALUES ('CS','Czechoslovakia (former)');
  1087. INSERT INTO country(short_name,name) VALUES ('CU','Cuba');
  1088. INSERT INTO country(short_name,name) VALUES ('CV','Cape Verde');
  1089. INSERT INTO country(short_name,name) VALUES ('CX','Christmas Island');
  1090. INSERT INTO country(short_name,name) VALUES ('CY','Cyprus');
  1091. INSERT INTO country(short_name,name) VALUES ('CZ','Czech Republic');
  1092. INSERT INTO country(short_name,name) VALUES ('DE','Germany');
  1093. INSERT INTO country(short_name,name) VALUES ('DJ','Djibouti');
  1094. INSERT INTO country(short_name,name) VALUES ('DK','Denmark');
  1095. INSERT INTO country(short_name,name) VALUES ('DM','Dominica');
  1096. INSERT INTO country(short_name,name) VALUES ('DO','Dominican Republic');
  1097. INSERT INTO country(short_name,name) VALUES ('DZ','Algeria');
  1098. INSERT INTO country(short_name,name) VALUES ('EC','Ecuador');
  1099. INSERT INTO country(short_name,name) VALUES ('EE','Estonia');
  1100. INSERT INTO country(short_name,name) VALUES ('EG','Egypt');
  1101. INSERT INTO country(short_name,name) VALUES ('EH','Western Sahara');
  1102. INSERT INTO country(short_name,name) VALUES ('ER','Eritrea');
  1103. INSERT INTO country(short_name,name) VALUES ('ES','Spain');
  1104. INSERT INTO country(short_name,name) VALUES ('ET','Ethiopia');
  1105. INSERT INTO country(short_name,name) VALUES ('FI','Finland');
  1106. INSERT INTO country(short_name,name) VALUES ('FJ','Fiji');
  1107. INSERT INTO country(short_name,name) VALUES ('FK','Falkland Islands (Malvinas)');
  1108. INSERT INTO country(short_name,name) VALUES ('FM','Micronesia');
  1109. INSERT INTO country(short_name,name) VALUES ('FO','Faroe Islands');
  1110. INSERT INTO country(short_name,name) VALUES ('FR','France');
  1111. INSERT INTO country(short_name,name) VALUES ('FX','France, Metropolitan');
  1112. INSERT INTO country(short_name,name) VALUES ('GA','Gabon');
  1113. INSERT INTO country(short_name,name) VALUES ('GB','Great Britain (UK)');
  1114. INSERT INTO country(short_name,name) VALUES ('GD','Grenada');
  1115. INSERT INTO country(short_name,name) VALUES ('GE','Georgia');
  1116. INSERT INTO country(short_name,name) VALUES ('GF','French Guiana');
  1117. INSERT INTO country(short_name,name) VALUES ('GH','Ghana');
  1118. INSERT INTO country(short_name,name) VALUES ('GI','Gibraltar');
  1119. INSERT INTO country(short_name,name) VALUES ('GL','Greenland');
  1120. INSERT INTO country(short_name,name) VALUES ('GM','Gambia');
  1121. INSERT INTO country(short_name,name) VALUES ('GN','Guinea');
  1122. INSERT INTO country(short_name,name) VALUES ('GP','Guadeloupe');
  1123. INSERT INTO country(short_name,name) VALUES ('GQ','Equatorial Guinea');
  1124. INSERT INTO country(short_name,name) VALUES ('GR','Greece');
  1125. INSERT INTO country(short_name,name) VALUES ('GS','S. Georgia and S. Sandwich Isls.');
  1126. INSERT INTO country(short_name,name) VALUES ('GT','Guatemala');
  1127. INSERT INTO country(short_name,name) VALUES ('GU','Guam');
  1128. INSERT INTO country(short_name,name) VALUES ('GW','Guinea-Bissau');
  1129. INSERT INTO country(short_name,name) VALUES ('GY','Guyana');
  1130. INSERT INTO country(short_name,name) VALUES ('HK','Hong Kong');
  1131. INSERT INTO country(short_name,name) VALUES ('HM','Heard and McDonald Islands');
  1132. INSERT INTO country(short_name,name) VALUES ('HN','Honduras');
  1133. INSERT INTO country(short_name,name) VALUES ('HR','Croatia (Hrvatska)');
  1134. INSERT INTO country(short_name,name) VALUES ('HT','Haiti');
  1135. INSERT INTO country(short_name,name) VALUES ('HU','Hungary');
  1136. INSERT INTO country(short_name,name) VALUES ('ID','Indonesia');
  1137. INSERT INTO country(short_name,name) VALUES ('IE','Ireland');
  1138. INSERT INTO country(short_name,name) VALUES ('IL','Israel');
  1139. INSERT INTO country(short_name,name) VALUES ('IM','Isle of Man');
  1140. INSERT INTO country(short_name,name) VALUES ('IN','India');
  1141. INSERT INTO country(short_name,name) VALUES ('IO','British Indian Ocean Territory');
  1142. INSERT INTO country(short_name,name) VALUES ('IQ','Iraq');
  1143. INSERT INTO country(short_name,name) VALUES ('IR','Iran');
  1144. INSERT INTO country(short_name,name) VALUES ('IS','Iceland');
  1145. INSERT INTO country(short_name,name) VALUES ('IT','Italy');
  1146. INSERT INTO country(short_name,name) VALUES ('JE','Jersey');
  1147. INSERT INTO country(short_name,name) VALUES ('JM','Jamaica');
  1148. INSERT INTO country(short_name,name) VALUES ('JO','Jordan');
  1149. INSERT INTO country(short_name,name) VALUES ('JP','Japan');
  1150. INSERT INTO country(short_name,name) VALUES ('KE','Kenya');
  1151. INSERT INTO country(short_name,name) VALUES ('KG','Kyrgyzstan');
  1152. INSERT INTO country(short_name,name) VALUES ('KH','Cambodia');
  1153. INSERT INTO country(short_name,name) VALUES ('KI','Kiribati');
  1154. INSERT INTO country(short_name,name) VALUES ('KM','Comoros');
  1155. INSERT INTO country(short_name,name) VALUES ('KN','Saint Kitts and Nevis');
  1156. INSERT INTO country(short_name,name) VALUES ('KP','Korea (North)');
  1157. INSERT INTO country(short_name,name) VALUES ('KR','Korea (South)');
  1158. INSERT INTO country(short_name,name) VALUES ('KW','Kuwait');
  1159. INSERT INTO country(short_name,name) VALUES ('KY','Cayman Islands');
  1160. INSERT INTO country(short_name,name) VALUES ('KZ','Kazakhstan');
  1161. INSERT INTO country(short_name,name) VALUES ('LA','Laos');
  1162. INSERT INTO country(short_name,name) VALUES ('LB','Lebanon');
  1163. INSERT INTO country(short_name,name) VALUES ('LC','Saint Lucia');
  1164. INSERT INTO country(short_name,name) VALUES ('LI','Liechtenstein');
  1165. INSERT INTO country(short_name,name) VALUES ('LK','Sri Lanka');
  1166. INSERT INTO country(short_name,name) VALUES ('LR','Liberia');
  1167. INSERT INTO country(short_name,name) VALUES ('LS','Lesotho');
  1168. INSERT INTO country(short_name,name) VALUES ('LT','Lithuania');
  1169. INSERT INTO country(short_name,name) VALUES ('LU','Luxembourg');
  1170. INSERT INTO country(short_name,name) VALUES ('LV','Latvia');
  1171. INSERT INTO country(short_name,name) VALUES ('LY','Libya');
  1172. INSERT INTO country(short_name,name) VALUES ('MA','Morocco');
  1173. INSERT INTO country(short_name,name) VALUES ('MC','Monaco');
  1174. INSERT INTO country(short_name,name) VALUES ('MD','Moldova');
  1175. INSERT INTO country(short_name,name) VALUES ('MG','Madagascar');
  1176. INSERT INTO country(short_name,name) VALUES ('MH','Marshall Islands');
  1177. INSERT INTO country(short_name,name) VALUES ('MK','F.Y.R.O.M. (Macedonia)');
  1178. INSERT INTO country(short_name,name) VALUES ('ML','Mali');
  1179. INSERT INTO country(short_name,name) VALUES ('MM','Myanmar');
  1180. INSERT INTO country(short_name,name) VALUES ('MN','Mongolia');
  1181. INSERT INTO country(short_name,name) VALUES ('MO','Macau');
  1182. INSERT INTO country(short_name,name) VALUES ('MP','Northern Mariana Islands');
  1183. INSERT INTO country(short_name,name) VALUES ('MQ','Martinique');
  1184. INSERT INTO country(short_name,name) VALUES ('MR','Mauritania');
  1185. INSERT INTO country(short_name,name) VALUES ('MS','Montserrat');
  1186. INSERT INTO country(short_name,name) VALUES ('MT','Malta');
  1187. INSERT INTO country(short_name,name) VALUES ('MU','Mauritius');
  1188. INSERT INTO country(short_name,name) VALUES ('MV','Maldives');
  1189. INSERT INTO country(short_name,name) VALUES ('MW','Malawi');
  1190. INSERT INTO country(short_name,name) VALUES ('MX','Mexico');
  1191. INSERT INTO country(short_name,name) VALUES ('MY','Malaysia');
  1192. INSERT INTO country(short_name,name) VALUES ('MZ','Mozambique');
  1193. INSERT INTO country(short_name,name) VALUES ('NA','Namibia');
  1194. INSERT INTO country(short_name,name) VALUES ('NC','New Caledonia');
  1195. INSERT INTO country(short_name,name) VALUES ('NE','Niger');
  1196. INSERT INTO country(short_name,name) VALUES ('NF','Norfolk Island');
  1197. INSERT INTO country(short_name,name) VALUES ('NG','Nigeria');
  1198. INSERT INTO country(short_name,name) VALUES ('NI','Nicaragua');
  1199. INSERT INTO country(short_name,name) VALUES ('NL','Netherlands');
  1200. INSERT INTO country(short_name,name) VALUES ('NO','Norway');
  1201. INSERT INTO country(short_name,name) VALUES ('NP','Nepal');
  1202. INSERT INTO country(short_name,name) VALUES ('NR','Nauru');
  1203. INSERT INTO country(short_name,name) VALUES ('NT','Neutral Zone');
  1204. INSERT INTO country(short_name,name) VALUES ('NU','Niue');
  1205. INSERT INTO country(short_name,name) VALUES ('NZ','New Zealand (Aotearoa)');
  1206. INSERT INTO country(short_name,name) VALUES ('OM','Oman');
  1207. INSERT INTO country(short_name,name) VALUES ('PA','Panama');
  1208. INSERT INTO country(short_name,name) VALUES ('PE','Peru');
  1209. INSERT INTO country(short_name,name) VALUES ('PF','French Polynesia');
  1210. INSERT INTO country(short_name,name) VALUES ('PG','Papua New Guinea');
  1211. INSERT INTO country(short_name,name) VALUES ('PH','Philippines');
  1212. INSERT INTO country(short_name,name) VALUES ('PK','Pakistan');
  1213. INSERT INTO country(short_name,name) VALUES ('PL','Poland');
  1214. INSERT INTO country(short_name,name) VALUES ('PM','St. Pierre and Miquelon');
  1215. INSERT INTO country(short_name,name) VALUES ('PN','Pitcairn');
  1216. INSERT INTO country(short_name,name) VALUES ('PR','Puerto Rico');
  1217. INSERT INTO country(short_name,name) VALUES ('PS','Palestinian Territory, Occupied');
  1218. INSERT INTO country(short_name,name) VALUES ('PT','Portugal');
  1219. INSERT INTO country(short_name,name) VALUES ('PW','Palau');
  1220. INSERT INTO country(short_name,name) VALUES ('PY','Paraguay');
  1221. INSERT INTO country(short_name,name) VALUES ('QA','Qatar');
  1222. INSERT INTO country(short_name,name) VALUES ('RE','Reunion');
  1223. INSERT INTO country(short_name,name) VALUES ('RO','Romania');
  1224. INSERT INTO country(short_name,name) VALUES ('RS','Serbia');
  1225. INSERT INTO country(short_name,name) VALUES ('RU','Russian Federation');
  1226. INSERT INTO country(short_name,name) VALUES ('RW','Rwanda');
  1227. INSERT INTO country(short_name,name) VALUES ('SA','Saudi Arabia');
  1228. INSERT INTO country(short_name,name) VALUES ('SB','Solomon Islands');
  1229. INSERT INTO country(short_name,name) VALUES ('SC','Seychelles');
  1230. INSERT INTO country(short_name,name) VALUES ('SD','Sudan');
  1231. INSERT INTO country(short_name,name) VALUES ('SE','Sweden');
  1232. INSERT INTO country(short_name,name) VALUES ('SG','Singapore');
  1233. INSERT INTO country(short_name,name) VALUES ('SH','St. Helena');
  1234. INSERT INTO country(short_name,name) VALUES ('SI','Slovenia');
  1235. INSERT INTO country(short_name,name) VALUES ('SJ','Svalbard & Jan Mayen Islands');
  1236. INSERT INTO country(short_name,name) VALUES ('SK','Slovak Republic');
  1237. INSERT INTO country(short_name,name) VALUES ('SL','Sierra Leone');
  1238. INSERT INTO country(short_name,name) VALUES ('SM','San Marino');
  1239. INSERT INTO country(short_name,name) VALUES ('SN','Senegal');
  1240. INSERT INTO country(short_name,name) VALUES ('SO','Somalia');
  1241. INSERT INTO country(short_name,name) VALUES ('SR','Suriname');
  1242. INSERT INTO country(short_name,name) VALUES ('ST','Sao Tome and Principe');
  1243. INSERT INTO country(short_name,name) VALUES ('SU','USSR (former)');
  1244. INSERT INTO country(short_name,name) VALUES ('SV','El Salvador');
  1245. INSERT INTO country(short_name,name) VALUES ('SY','Syria');
  1246. INSERT INTO country(short_name,name) VALUES ('SZ','Swaziland');
  1247. INSERT INTO country(short_name,name) VALUES ('TC','Turks and Caicos Islands');
  1248. INSERT INTO country(short_name,name) VALUES ('TD','Chad');
  1249. INSERT INTO country(short_name,name) VALUES ('TF','French Southern Territories');
  1250. INSERT INTO country(short_name,name) VALUES ('TG','Togo');
  1251. INSERT INTO country(short_name,name) VALUES ('TH','Thailand');
  1252. INSERT INTO country(short_name,name) VALUES ('TJ','Tajikistan');
  1253. INSERT INTO country(short_name,name) VALUES ('TK','Tokelau');
  1254. INSERT INTO country(short_name,name) VALUES ('TM','Turkmenistan');
  1255. INSERT INTO country(short_name,name) VALUES ('TN','Tunisia');
  1256. INSERT INTO country(short_name,name) VALUES ('TO','Tonga');
  1257. INSERT INTO country(short_name,name) VALUES ('TP','East Timor');
  1258. INSERT INTO country(short_name,name) VALUES ('TR','Turkey');
  1259. INSERT INTO country(short_name,name) VALUES ('TT','Trinidad and Tobago');
  1260. INSERT INTO country(short_name,name) VALUES ('TV','Tuvalu');
  1261. INSERT INTO country(short_name,name) VALUES ('TW','Taiwan');
  1262. INSERT INTO country(short_name,name) VALUES ('TZ','Tanzania');
  1263. INSERT INTO country(short_name,name) VALUES ('UA','Ukraine');
  1264. INSERT INTO country(short_name,name) VALUES ('UG','Uganda');
  1265. INSERT INTO country(short_name,name) VALUES ('UK','United Kingdom');
  1266. INSERT INTO country(short_name,name) VALUES ('UM','US Minor Outlying Islands');
  1267. INSERT INTO country(short_name,name) VALUES ('US','United States');
  1268. INSERT INTO country(short_name,name) VALUES ('UY','Uruguay');
  1269. INSERT INTO country(short_name,name) VALUES ('UZ','Uzbekistan');
  1270. INSERT INTO country(short_name,name) VALUES ('VA','Vatican City State (Holy See)');
  1271. INSERT INTO country(short_name,name) VALUES ('VC','Saint Vincent & the Grenadines');
  1272. INSERT INTO country(short_name,name) VALUES ('VE','Venezuela');
  1273. INSERT INTO country(short_name,name) VALUES ('VG','British Virgin Islands');
  1274. INSERT INTO country(short_name,name) VALUES ('VI','Virgin Islands (U.S.)');
  1275. INSERT INTO country(short_name,name) VALUES ('VN','Viet Nam');
  1276. INSERT INTO country(short_name,name) VALUES ('VU','Vanuatu');
  1277. INSERT INTO country(short_name,name) VALUES ('WF','Wallis and Futuna Islands');
  1278. INSERT INTO country(short_name,name) VALUES ('WS','Samoa');
  1279. INSERT INTO country(short_name,name) VALUES ('YE','Yemen');
  1280. INSERT INTO country(short_name,name) VALUES ('YT','Mayotte');
  1281. INSERT INTO country(short_name,name) VALUES ('YU','Yugoslavia (former)');
  1282. INSERT INTO country(short_name,name) VALUES ('ZA','South Africa');
  1283. INSERT INTO country(short_name,name) VALUES ('ZM','Zambia');
  1284. INSERT INTO country(short_name,name) VALUES ('ZR','Zaire');
  1285. INSERT INTO country(short_name,name) VALUES ('ZW','Zimbabwe');
  1286. --
  1287. CREATE FUNCTION del_yearend() RETURNS TRIGGER AS '
  1288. begin
  1289. delete from yearend where trans_id = old.id;
  1290. return NULL;
  1291. end;
  1292. ' language 'plpgsql';
  1293. -- end function
  1294. --
  1295. CREATE TRIGGER del_yearend AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_yearend();
  1296. -- end trigger
  1297. --
  1298. CREATE FUNCTION del_department() RETURNS TRIGGER AS '
  1299. begin
  1300. delete from dpt_trans where trans_id = old.id;
  1301. return NULL;
  1302. end;
  1303. ' language 'plpgsql';
  1304. -- end function
  1305. --
  1306. CREATE TRIGGER del_department AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_department();
  1307. -- end trigger
  1308. CREATE TRIGGER del_department AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_department();
  1309. -- end trigger
  1310. CREATE TRIGGER del_department AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_department();
  1311. -- end trigger
  1312. CREATE TRIGGER del_department AFTER DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_department();
  1313. -- end trigger
  1314. --
  1315. CREATE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
  1316. declare
  1317. t_transdate date;
  1318. t_curr char(3);
  1319. t_id int;
  1320. d_curr text;
  1321. begin
  1322. select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
  1323. if TG_RELNAME = ''ar'' then
  1324. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  1325. end if;
  1326. if TG_RELNAME = ''ap'' then
  1327. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  1328. end if;
  1329. if TG_RELNAME = ''oe'' then
  1330. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  1331. end if;
  1332. if d_curr != t_curr then
  1333. select into t_id a.id from acc_trans ac
  1334. join ar a on (a.id = ac.trans_id)
  1335. where a.curr = t_curr
  1336. and ac.transdate = t_transdate
  1337. except select a.id from ar a where a.id = old.id
  1338. union
  1339. select a.id from acc_trans ac
  1340. join ap a on (a.id = ac.trans_id)
  1341. where a.curr = t_curr
  1342. and ac.transdate = t_transdate
  1343. except select a.id from ap a where a.id = old.id
  1344. union
  1345. select o.id from oe o
  1346. where o.curr = t_curr
  1347. and o.transdate = t_transdate
  1348. except select o.id from oe o where o.id = old.id;
  1349. if not found then
  1350. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  1351. end if;
  1352. end if;
  1353. return old;
  1354. end;
  1355. ' language 'plpgsql';
  1356. -- end function
  1357. --
  1358. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1359. -- end trigger
  1360. --
  1361. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1362. -- end trigger
  1363. --
  1364. CREATE TRIGGER del_exchangerate BEFORE DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1365. -- end trigger
  1366. --
  1367. CREATE FUNCTION check_inventory() RETURNS TRIGGER AS '
  1368. declare
  1369. itemid int;
  1370. row_data inventory%rowtype;
  1371. begin
  1372. if not old.quotation then
  1373. for row_data in select * from inventory where trans_id = old.id loop
  1374. select into itemid id from orderitems where trans_id = old.id and id = row_data.orderitems_id;
  1375. if itemid is null then
  1376. delete from inventory where trans_id = old.id and orderitems_id = row_data.orderitems_id;
  1377. end if;
  1378. end loop;
  1379. end if;
  1380. return old;
  1381. end;
  1382. ' language 'plpgsql';
  1383. -- end function
  1384. --
  1385. CREATE TRIGGER check_inventory AFTER UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_inventory();
  1386. -- end trigger
  1387. --
  1388. --
  1389. CREATE FUNCTION check_department() RETURNS TRIGGER AS '
  1390. declare
  1391. dpt_id int;
  1392. begin
  1393. if new.department_id = 0 then
  1394. delete from dpt_trans where trans_id = new.id;
  1395. return NULL;
  1396. end if;
  1397. select into dpt_id trans_id from dpt_trans where trans_id = new.id;
  1398. if dpt_id > 0 then
  1399. update dpt_trans set department_id = new.department_id where trans_id = dpt_id;
  1400. else
  1401. insert into dpt_trans (trans_id, department_id) values (new.id, new.department_id);
  1402. end if;
  1403. return NULL;
  1404. end;
  1405. ' language 'plpgsql';
  1406. -- end function
  1407. --
  1408. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ar FOR EACH ROW EXECUTE PROCEDURE check_department();
  1409. -- end trigger
  1410. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ap FOR EACH ROW EXECUTE PROCEDURE check_department();
  1411. -- end trigger
  1412. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON gl FOR EACH ROW EXECUTE PROCEDURE check_department();
  1413. -- end trigger
  1414. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_department();
  1415. -- end trigger
  1416. --
  1417. CREATE FUNCTION del_recurring() RETURNS TRIGGER AS '
  1418. BEGIN
  1419. DELETE FROM recurring WHERE id = old.id;
  1420. DELETE FROM recurringemail WHERE id = old.id;
  1421. DELETE FROM recurringprint WHERE id = old.id;
  1422. RETURN NULL;
  1423. END;
  1424. ' language 'plpgsql';
  1425. --end function
  1426. CREATE TRIGGER del_recurring AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1427. -- end trigger
  1428. CREATE TRIGGER del_recurring AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1429. -- end trigger
  1430. CREATE TRIGGER del_recurring AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1431. -- end trigger
  1432. --
  1433. CREATE FUNCTION avgcost(int) RETURNS FLOAT AS '
  1434. DECLARE
  1435. v_cost float;
  1436. v_qty float;
  1437. v_parts_id alias for $1;
  1438. BEGIN
  1439. SELECT INTO v_cost, v_qty SUM(i.sellprice * i.qty), SUM(i.qty)
  1440. FROM invoice i
  1441. JOIN ap a ON (a.id = i.trans_id)
  1442. WHERE i.parts_id = v_parts_id;
  1443. IF v_cost IS NULL THEN
  1444. v_cost := 0;
  1445. END IF;
  1446. IF NOT v_qty IS NULL THEN
  1447. IF v_qty = 0 THEN
  1448. v_cost := 0;
  1449. ELSE
  1450. v_cost := v_cost/v_qty;
  1451. END IF;
  1452. END IF;
  1453. RETURN v_cost;
  1454. END;
  1455. ' language 'plpgsql';
  1456. -- end function
  1457. --
  1458. CREATE FUNCTION lastcost(int) RETURNS FLOAT AS '
  1459. DECLARE
  1460. v_cost float;
  1461. v_parts_id alias for $1;
  1462. BEGIN
  1463. SELECT INTO v_cost sellprice FROM invoice i
  1464. JOIN ap a ON (a.id = i.trans_id)
  1465. WHERE i.parts_id = v_parts_id
  1466. ORDER BY a.transdate desc, a.id desc
  1467. LIMIT 1;
  1468. IF v_cost IS NULL THEN
  1469. v_cost := 0;
  1470. END IF;
  1471. RETURN v_cost;
  1472. END;
  1473. ' language plpgsql;
  1474. -- end function
  1475. --
  1476. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  1477. AS
  1478. '
  1479. BEGIN
  1480. IF NEW.onhand >= NEW.rop THEN
  1481. NOTIFY parts_short;
  1482. END IF;
  1483. RETURN NEW;
  1484. END;
  1485. ' LANGUAGE PLPGSQL;
  1486. -- end function
  1487. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  1488. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  1489. -- end function
  1490. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  1491. RETURNS BOOL AS
  1492. '
  1493. DECLARE
  1494. table_name ALIAS FOR $1;
  1495. new_field_name ALIAS FOR $2;
  1496. field_datatype ALIAS FOR $3;
  1497. BEGIN
  1498. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  1499. WHERE extends = '''''' || table_name || '''''' '';
  1500. IF NOT FOUND THEN
  1501. BEGIN
  1502. INSERT INTO custom_table_catalog (extends)
  1503. VALUES (table_name);
  1504. EXECUTE ''CREATE TABLE custom_''||table_name ||
  1505. '' (row_id INT PRIMARY KEY)'';
  1506. EXCEPTION WHEN duplicate_table THEN
  1507. -- do nothing
  1508. END;
  1509. END IF;
  1510. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  1511. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  1512. WHERE extends = ''''''|| table_name || ''''''))'';
  1513. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  1514. || new_field_name || '' '' || field_datatype;
  1515. RETURN TRUE;
  1516. END;
  1517. ' LANGUAGE PLPGSQL;
  1518. -- end function
  1519. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  1520. RETURNS BOOL AS
  1521. '
  1522. DECLARE
  1523. table_name ALIAS FOR $1;
  1524. custom_field_name ALIAS FOR $2;
  1525. BEGIN
  1526. DELETE FROM custom_field_catalog
  1527. WHERE field_name = custom_field_name AND
  1528. table_id = (SELECT table_id FROM custom_table_catalog
  1529. WHERE extends = table_name);
  1530. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  1531. '' DROP COLUMN '' || custom_field_name;
  1532. RETURN TRUE;
  1533. END;
  1534. ' LANGUAGE PLPGSQL;
  1535. -- end function
  1536. CREATE TABLE menu_node (
  1537. id serial NOT NULL,
  1538. label character varying NOT NULL,
  1539. parent integer,
  1540. "position" integer NOT NULL
  1541. );
  1542. ALTER TABLE public.menu_node OWNER TO ledgersmb;
  1543. --
  1544. -- Name: menu_node_id_seq; Type: SEQUENCE SET; Schema: public; Owner: ledgersmb
  1545. --
  1546. SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('menu_node', 'id'), 193, true);
  1547. --
  1548. -- Data for Name: menu_node; Type: TABLE DATA; Schema: public; Owner: ledgersmb
  1549. --
  1550. COPY menu_node (id, label, parent, "position") FROM stdin;
  1551. 0 Top-level \N 0
  1552. 1 AR 0 1
  1553. 2 Add Transaction 1 1
  1554. 3 Sales Invoice 1 2
  1555. 5 Transactions 4 1
  1556. 6 Outstanding 4 2
  1557. 7 AR Aging 4 3
  1558. 9 Taxable Sales 4 4
  1559. 10 Non-Taxable 4 5
  1560. 12 Add Customer 11 1
  1561. 13 Reports 11 2
  1562. 14 Search 13 1
  1563. 15 History 13 2
  1564. 16 Point of Sale 0 2
  1565. 17 Sale 16 1
  1566. 18 Open 16 2
  1567. 19 Receipts 16 3
  1568. 20 Close Till 16 4
  1569. 21 AP 0 3
  1570. 22 Add Transaction 21 1
  1571. 23 Vendor Invoice 21 2
  1572. 25 Transactions 24 1
  1573. 26 Outstanding 24 2
  1574. 27 AP Aging 24 3
  1575. 28 Taxable 24 4
  1576. 29 Non-taxable 24 5
  1577. 31 Add Vendor 30 1
  1578. 32 Reports 30 2
  1579. 33 Search 32 1
  1580. 34 History 32 2
  1581. 35 Cash 0 4
  1582. 36 Receipt 35 1
  1583. 38 Payment 35 3
  1584. 37 Receipts 35 2
  1585. 39 Payments 35 4
  1586. 40 Transfer 35 5
  1587. 42 Receipts 41 1
  1588. 43 Payments 41 2
  1589. 44 Reconciliation 41 3
  1590. 41 Reports 35 7
  1591. 45 Reconciliation 35 6
  1592. 46 HR 0 5
  1593. 47 Employees 46 1
  1594. 48 Add Employee 47 1
  1595. 49 Search 47 2
  1596. 50 Order Entry 0 6
  1597. 51 Sales Order 50 1
  1598. 52 Purchase Order 50 2
  1599. 53 Reports 50 3
  1600. 54 Sales Orders 53 1
  1601. 55 Purchase Orders 53 2
  1602. 57 Sales Orders 56 1
  1603. 58 Purchase Orders 56 2
  1604. 56 Generate 50 4
  1605. 60 Consolidate 50 5
  1606. 61 Sales Orders 60 1
  1607. 62 Purchase Orders 60 2
  1608. 63 Shipping 0 7
  1609. 64 Ship 63 1
  1610. 65 Receive 63 2
  1611. 66 Transfer 63 3
  1612. 67 Quotations 0 8
  1613. 68 Quotation 67 1
  1614. 69 RFQ 67 2
  1615. 70 Reports 67 3
  1616. 71 Quotations 70 1
  1617. 72 RFQs 70 2
  1618. 73 General Journal 0 9
  1619. 74 Journal Entry 73 1
  1620. 75 Adjust Till 73 2
  1621. 76 Reports 73 3
  1622. 77 Goods and Services 0 10
  1623. 78 Add Part 77 1
  1624. 79 Add Service 77 2
  1625. 80 Add Assembly 77 3
  1626. 81 Add Overhead 77 4
  1627. 82 Add Group 77 5
  1628. 83 Add Pricegroup 77 6
  1629. 84 Stock Assembly 77 7
  1630. 85 Reports 77 8
  1631. 86 All Items 85 1
  1632. 87 Parts 85 2
  1633. 88 Requirements 85 3
  1634. 89 Services 85 4
  1635. 90 Labor 85 5
  1636. 91 Groups 85 6
  1637. 92 Pricegroups 85 7
  1638. 93 Assembly 85 8
  1639. 94 Components 85 9
  1640. 95 Translations 77 9
  1641. 96 Description 95 1
  1642. 97 Partsgroup 95 2
  1643. 98 Projects 0 11
  1644. 99 Add Project 98 1
  1645. 100 Add Timecard 98 2
  1646. 101 Generate 98 3
  1647. 102 Sales Orders 101 1
  1648. 103 Reports 98 4
  1649. 104 Search 103 1
  1650. 105 Transactions 103 2
  1651. 106 Time Cards 103 3
  1652. 107 Translations 98 5
  1653. 108 Description 107 1
  1654. 109 Reports 0 12
  1655. 110 Chart of Accounts 109 1
  1656. 111 Trial Balance 109 2
  1657. 112 Income Statement 109 3
  1658. 113 Balance Sheet 109 4
  1659. 114 Inventory Activity 109 5
  1660. 115 Recurring Transactions 0 13
  1661. 116 Batch Printing 0 14
  1662. 117 Sales Invoices 116 1
  1663. 118 Sales Orders 116 2
  1664. 119 Checks 116 3
  1665. 120 Work Orders 116 4
  1666. 121 Quotations 116 5
  1667. 122 Packing Lists 116 6
  1668. 123 Pick Lists 116 7
  1669. 124 Purchase Orders 116 8
  1670. 125 Bin Lists 116 9
  1671. 126 RFQs 116 10
  1672. 127 Time Cards 116 11
  1673. 128 System 0 15
  1674. 129 Audit Control 128 1
  1675. 130 Taxes 128 2
  1676. 131 Defaults 128 3
  1677. 132 Yearend 128 4
  1678. 133 Backup 128 5
  1679. 134 Send to File 133 1
  1680. 135 Send to Email 133 2
  1681. 136 Chart of Accounts 128 6
  1682. 137 Add Accounts 136 1
  1683. 138 List Accounts 136 2
  1684. 139 Add GIFI 136 3
  1685. 140 List GIFI 136 4
  1686. 141 Warehouses 128 7
  1687. 142 Add Warehouse 141 1
  1688. 143 List Warehouse 141 2
  1689. 144 Departments 128 8
  1690. 145 Add Department 144 1
  1691. 146 List Departments 144 2
  1692. 147 Type of Business 128 9
  1693. 148 Add Business 147 1
  1694. 149 List Businesses 147 2
  1695. 150 Language 128 10
  1696. 151 Add Language 150 1
  1697. 152 List Languages 150 2
  1698. 153 SIC 128 11
  1699. 154 Add SIC 153 1
  1700. 155 List SIC 153 2
  1701. 156 HTML Templates 128 12
  1702. 157 Income Statement 156 1
  1703. 158 Balance Sheet 156 2
  1704. 159 Invoice 156 3
  1705. 160 AR Transaction 156 4
  1706. 161 AP Transaction 156 5
  1707. 162 Packing List 156 6
  1708. 163 Pick List 156 7
  1709. 164 Sales Order 156 8
  1710. 165 Work Order 156 9
  1711. 166 Purchase Order 156 10
  1712. 167 Bin List 156 11
  1713. 168 Statement 156 12
  1714. 169 Quotation 156 13
  1715. 170 RFQ 156 14
  1716. 171 Timecard 156 15
  1717. 172 LaTeX Templates 128 13
  1718. 173 Invoice 172 1
  1719. 174 AR Transaction 172 2
  1720. 175 AP Transaction 172 3
  1721. 176 Packing List 172 4
  1722. 177 Pick List 172 5
  1723. 178 Sales Order 172 6
  1724. 179 Work Order 172 7
  1725. 180 Purchase Order 172 8
  1726. 181 Bin List 172 9
  1727. 182 Statement 172 10
  1728. 183 Check 172 11
  1729. 184 Receipt 172 12
  1730. 185 Quotation 172 13
  1731. 186 RFQ 172 14
  1732. 187 Timecard 172 15
  1733. 188 Text Templates 128 14
  1734. 189 POS Invoice 188 1
  1735. 190 Stylesheet 0 16
  1736. 191 Preferences 0 17
  1737. 192 New Window 0 18
  1738. 193 Logout 0 19
  1739. 11 Customers 1 6
  1740. 4 Reports 1 5
  1741. 194 Credit Note 1 3
  1742. 195 Credit Invoice 1 4
  1743. 24 Reports 21 5
  1744. 30 Vendors 21 6
  1745. 196 Debit Note 21 3
  1746. 197 Debit Invoice 21 4
  1747. \.
  1748. --
  1749. -- Name: menu_node_parent_key; Type: CONSTRAINT; Schema: public; Owner: ledgersmb; Tablespace:
  1750. --
  1751. ALTER TABLE ONLY menu_node
  1752. ADD CONSTRAINT menu_node_parent_key UNIQUE (parent, "position");
  1753. --
  1754. -- Name: menu_node_pkey; Type: CONSTRAINT; Schema: public; Owner: ledgersmb; Tablespace:
  1755. --
  1756. ALTER TABLE ONLY menu_node
  1757. ADD CONSTRAINT menu_node_pkey PRIMARY KEY (id);
  1758. --
  1759. -- Name: menu_node_parent_fkey; Type: FK CONSTRAINT; Schema: public; Owner: ledgersmb
  1760. --
  1761. ALTER TABLE ONLY menu_node
  1762. ADD CONSTRAINT menu_node_parent_fkey FOREIGN KEY (parent) REFERENCES menu_node(id);
  1763. CREATE TABLE menu_attribute (
  1764. node_id integer NOT NULL,
  1765. attribute character varying NOT NULL,
  1766. value character varying NOT NULL,
  1767. id serial NOT NULL
  1768. );
  1769. --
  1770. -- Name: menu_attribute_id_seq; Type: SEQUENCE SET; Schema: public; Owner: ledgersmb
  1771. --
  1772. SELECT pg_catalog.setval(pg_catalog.pg_get_serial_sequence('menu_attribute', 'id'), 536, true);
  1773. --
  1774. -- Data for Name: menu_attribute; Type: TABLE DATA; Schema: public; Owner: ledgersmb
  1775. --
  1776. COPY menu_attribute (node_id, attribute, value, id) FROM stdin;
  1777. 1 menu 1 1
  1778. 2 module ar.pl 2
  1779. 2 action add 3
  1780. 3 action add 4
  1781. 3 module is.pl 5
  1782. 3 type invoice 6
  1783. 4 menu 1 7
  1784. 5 module ar.pl 8
  1785. 5 action search 9
  1786. 5 nextsub transactions 10
  1787. 6 module ar.pl 12
  1788. 6 action search 13
  1789. 6 nextsub transactions 14
  1790. 7 module rp.pl 15
  1791. 7 action report 16
  1792. 7 report ar_aging 17
  1793. 9 module rp.pl 21
  1794. 9 action report 22
  1795. 9 report tax_collected 23
  1796. 10 module rp.pl 24
  1797. 10 action report 25
  1798. 10 report nontaxable_sales 26
  1799. 11 menu 1 27
  1800. 12 module ct.pl 28
  1801. 12 action add 29
  1802. 12 db customer 30
  1803. 13 menu 1 31
  1804. 14 module ct.pl 32
  1805. 14 db customer 34
  1806. 15 module ct.pl 35
  1807. 15 action add 36
  1808. 15 db customer 37
  1809. 14 action history 33
  1810. 16 menu 1 38
  1811. 17 module ps.pl 39
  1812. 17 action add 40
  1813. 17 nextsub openinvoices 41
  1814. 18 action openinvoices 42
  1815. 18 module ps.pl 43
  1816. 19 module ps.pl 44
  1817. 19 action receipts 46
  1818. 20 module rc.pl 47
  1819. 20 action till_closing 48
  1820. 20 pos true 49
  1821. 21 menu 1 50
  1822. 22 action add 52
  1823. 22 module ap.pl 51
  1824. 23 action add 53
  1825. 23 type invoice 55
  1826. 23 module ir.pl 54
  1827. 24 menu 1 56
  1828. 25 action search 58
  1829. 25 nextsub transactions 59
  1830. 25 module ap.pl 57
  1831. 26 action search 61
  1832. 26 nextsub transactions 62
  1833. 26 module ap.pl 60
  1834. 27 module rp.pl 63
  1835. 27 action report 64
  1836. 28 module rp.pl 66
  1837. 28 action report 67
  1838. 28 report tax_collected 68
  1839. 27 report tax_paid 65
  1840. 29 module rp.pl 69
  1841. 29 action report 70
  1842. 29 report report 71
  1843. 30 menu 1 72
  1844. 31 module ct.pl 73
  1845. 31 action add 74
  1846. 31 db vendor 75
  1847. 32 menu 1 76
  1848. 33 module ct.pl 77
  1849. 33 action history 79
  1850. 33 db vendor 78
  1851. 34 module ct.pl 80
  1852. 34 action add 81
  1853. 34 db vendor 82
  1854. 35 menu 1 83
  1855. 36 module cp.pl 84
  1856. 36 action payment 85
  1857. 36 type receipt 86
  1858. 37 module cp.pl 87
  1859. 38 module cp.pl 90
  1860. 38 action payment 91
  1861. 37 type receipt 89
  1862. 37 action payments 88
  1863. 38 type check 92
  1864. 39 module cp.pl 93
  1865. 39 type check 95
  1866. 39 action payments 94
  1867. 40 module gl.pl 96
  1868. 40 action add 97
  1869. 40 transfer 1 98
  1870. 41 menu 1 99
  1871. 42 module rp.pl 100
  1872. 42 action report 101
  1873. 42 report receipts 102
  1874. 43 module rp.pl 103
  1875. 43 action report 104
  1876. 43 report payments 105
  1877. 45 module rc.pl 106
  1878. 45 action reconciliation 107
  1879. 44 module rc.pl 108
  1880. 44 action reconciliation 109
  1881. 44 report 1 110
  1882. 46 menu 1 111
  1883. 47 menu 1 112
  1884. 48 module hr.pl 113
  1885. 48 action add 114
  1886. 48 db employee 115
  1887. 49 module hr.pl 116
  1888. 49 db employee 118
  1889. 49 action search 117
  1890. 50 menu 1 119
  1891. 51 module oe.pl 120
  1892. 51 action add 121
  1893. 51 type sales_order 122
  1894. 52 module oe.pl 123
  1895. 52 action add 124
  1896. 52 type purchase_order 125
  1897. 53 menu 1 126
  1898. 54 module oe.pl 127
  1899. 54 type sales_order 129
  1900. 54 action search 128
  1901. 55 module oe.pl 130
  1902. 55 type purchase_order 132
  1903. 55 action search 131
  1904. 56 menu 1 133
  1905. 57 module oe.pl 134
  1906. 57 action search 136
  1907. 58 module oe.pl 137
  1908. 58 action search 139
  1909. 57 type generate_sales_order 135
  1910. 58 type generate_purchase_order 138
  1911. 61 module oe.pl 140
  1912. 61 action search 141
  1913. 62 module oe.pl 143
  1914. 62 action search 144
  1915. 62 type consolidate_purchase_order 145
  1916. 61 type consolidate_sales_order 142
  1917. 63 menu 1 146
  1918. 64 module oe.pl 147
  1919. 64 action search 148
  1920. 65 module oe.pl 150
  1921. 65 action search 151
  1922. 65 type consolidate_sales_order 152
  1923. 64 type receive_order 149
  1924. 66 module oe.pl 153
  1925. 66 action search_transfer 154
  1926. 67 menu 1 155
  1927. 68 module oe.pl 156
  1928. 68 action add 157
  1929. 69 module oe.pl 159
  1930. 69 action add 160
  1931. 68 type sales_quotation 158
  1932. 69 type request_quotation 161
  1933. 70 menu 1 162
  1934. 71 module oe.pl 163
  1935. 71 type sales_quotation 165
  1936. 71 action search 164
  1937. 72 module oe.pl 166
  1938. 72 action search 168
  1939. 72 type request_quotation 167
  1940. 73 menu 1 169
  1941. 74 module gl.pl 170
  1942. 74 action add 171
  1943. 75 module gl.pl 172
  1944. 75 action add_pos_adjust 174
  1945. 75 rowcount 3 175
  1946. 75 pos_adjust 1 176
  1947. 75 reference Adjusting Till: (Till) Source: (Source) 177
  1948. 75 descripton Adjusting till due to data entry error 178
  1949. 76 module gl.pl 180
  1950. 76 action search 181
  1951. 77 menu 1 182
  1952. 78 module ic.pl 183
  1953. 78 action add 184
  1954. 78 item part 185
  1955. 79 module ic.pl 186
  1956. 79 action add 187
  1957. 79 item service 188
  1958. 80 module ic.pl 189
  1959. 80 action add 190
  1960. 81 module ic.pl 192
  1961. 81 action add 193
  1962. 81 item part 194
  1963. 80 item labor 191
  1964. 82 action add 195
  1965. 82 module pe.pl 196
  1966. 83 action add 198
  1967. 83 module pe.pl 199
  1968. 83 type partsgroup 200
  1969. 82 type pricegroup 197
  1970. 84 module ic.pl 202
  1971. 84 action stock_assembly 203
  1972. 85 menu 1 204
  1973. 86 module ic.pl 205
  1974. 87 action search 206
  1975. 86 action search 207
  1976. 87 module ic.pl 208
  1977. 86 searchitems all 209
  1978. 88 module ic.pl 211
  1979. 88 action requirements 212
  1980. 89 action search 213
  1981. 89 module ic.pl 214
  1982. 89 searchitems service 215
  1983. 87 searchitems part 210
  1984. 90 action search 216
  1985. 90 module ic.pl 217
  1986. 90 searchitems labor 218
  1987. 91 module pe.pl 221
  1988. 91 type pricegroup 222
  1989. 91 action search 220
  1990. 92 module pe.pl 224
  1991. 92 type partsgroup 225
  1992. 92 action search 223
  1993. 93 action search 226
  1994. 93 module ic.pl 227
  1995. 93 searchitems assembly 228
  1996. 94 action search 229
  1997. 94 module ic.pl 230
  1998. 94 searchitems component 231
  1999. 95 menu 1 232
  2000. 96 module pe.pl 233
  2001. 96 action translation 234
  2002. 96 translation description 235
  2003. 97 module pe.pl 236
  2004. 97 action translation 237
  2005. 97 translation partsgroup 238
  2006. 98 menu 1 239
  2007. 99 module pe.pl 240
  2008. 99 action add 241
  2009. 99 type project 242
  2010. 100 module jc.pl 243
  2011. 100 action add 244
  2012. 99 project project 245
  2013. 100 project project 246
  2014. 100 type timecard 247
  2015. 101 menu 1 248
  2016. 102 module pe.pl 249
  2017. 102 action project_sales_order 250
  2018. 102 menu 1 255
  2019. 104 module pe.pl 256
  2020. 104 type project 258
  2021. 104 action search 257
  2022. 105 action report 260
  2023. 105 report projects 261
  2024. 105 module rp.pl 262
  2025. 106 module jc.pl 263
  2026. 106 action search 264
  2027. 106 type timecard 265
  2028. 106 project project 266
  2029. 107 menu 1 268
  2030. 108 module pe.pl 269
  2031. 108 action translation 270
  2032. 108 translation project 271
  2033. 109 menu 1 272
  2034. 110 module ca.pl 273
  2035. 110 action chart_of_accounts 274
  2036. 111 action report 275
  2037. 111 module rp.pl 276
  2038. 111 report trial_balance 277
  2039. 112 action report 278
  2040. 112 module rp.pl 279
  2041. 112 report income_statement 280
  2042. 113 action report 281
  2043. 113 module rp.pl 282
  2044. 113 report balance_sheet 283
  2045. 114 action report 284
  2046. 114 module rp.pl 285
  2047. 114 report inv_activity 286
  2048. 115 action recurring_transactions 287
  2049. 115 module am.pl 288
  2050. 116 menu 1 289
  2051. 119 module bp.pl 290
  2052. 119 action search 291
  2053. 119 type check 292
  2054. 119 vc vendor 293
  2055. 117 module bp.pl 294
  2056. 117 action search 295
  2057. 117 vc customer 297
  2058. 118 module bp.pl 298
  2059. 118 action search 299
  2060. 118 vc customer 300
  2061. 118 type invoice 301
  2062. 117 type sales_order 296
  2063. 120 module bp.pl 302
  2064. 120 action search 303
  2065. 120 vc customer 304
  2066. 121 module bp.pl 306
  2067. 121 action search 307
  2068. 121 vc customer 308
  2069. 122 module bp.pl 310
  2070. 122 action search 311
  2071. 122 vc customer 312
  2072. 120 type work_order 305
  2073. 121 type sales_quotation 309
  2074. 122 type packing_list 313
  2075. 123 module bp.pl 314
  2076. 123 action search 315
  2077. 123 vc customer 316
  2078. 123 type pick_list 317
  2079. 124 module bp.pl 318
  2080. 124 action search 319
  2081. 124 vc vendor 321
  2082. 124 type purchase_order 320
  2083. 125 module bp.pl 322
  2084. 125 action search 323
  2085. 125 vc vendor 325
  2086. 126 module bp.pl 326
  2087. 126 action search 327
  2088. 126 vc vendor 329
  2089. 127 module bp.pl 330
  2090. 127 action search 331
  2091. 127 type timecard 332
  2092. 125 type bin_list 324
  2093. 126 type request_quotation 328
  2094. 127 vc employee 333
  2095. 128 menu 1 334
  2096. 129 module am.pl 337
  2097. 130 module am.pl 338
  2098. 131 module am.pl 339
  2099. 129 action audit_control 340
  2100. 130 taxes audit_control 341
  2101. 131 action defaults 342
  2102. 130 action taxes 343
  2103. 132 module am.pl 346
  2104. 132 action yearend 347
  2105. 133 menu 1 348
  2106. 134 module am.pl 349
  2107. 135 module am.pl 350
  2108. 134 action backup 351
  2109. 135 action backup 352
  2110. 134 media file 353
  2111. 135 media email 354
  2112. 137 module am.pl 355
  2113. 138 module am.pl 356
  2114. 139 module am.pl 357
  2115. 140 module am.pl 358
  2116. 137 action add_account 359
  2117. 138 action list_account 360
  2118. 139 action add_gifi 361
  2119. 140 action list_gifi 362
  2120. 141 menu 1 363
  2121. 142 module am.pl 364
  2122. 143 module am.pl 365
  2123. 142 action add_warehouse 366
  2124. 143 action list_warehouse 367
  2125. 145 module am.pl 368
  2126. 146 module am.pl 369
  2127. 145 action add_department 370
  2128. 146 action list_department 371
  2129. 147 menu 1 372
  2130. 148 module am.pl 373
  2131. 149 module am.pl 374
  2132. 148 action add_business 375
  2133. 149 action list_business 376
  2134. 150 menu 1 377
  2135. 151 module am.pl 378
  2136. 152 module am.pl 379
  2137. 151 action add_language 380
  2138. 152 action list_language 381
  2139. 153 menu 1 382
  2140. 154 module am.pl 383
  2141. 155 module am.pl 384
  2142. 154 action add_sic 385
  2143. 155 action list_sic 386
  2144. 156 menu 1 387
  2145. 157 module am.pl 388
  2146. 158 module am.pl 389
  2147. 159 module am.pl 390
  2148. 160 module am.pl 391
  2149. 161 module am.pl 392
  2150. 162 module am.pl 393
  2151. 163 module am.pl 394
  2152. 164 module am.pl 395
  2153. 165 module am.pl 396
  2154. 166 module am.pl 397
  2155. 167 module am.pl 398
  2156. 168 module am.pl 399
  2157. 169 module am.pl 400
  2158. 170 module am.pl 401
  2159. 171 module am.pl 402
  2160. 157 action list_templates 403
  2161. 158 action list_templates 404
  2162. 159 action list_templates 405
  2163. 160 action list_templates 406
  2164. 161 action list_templates 407
  2165. 162 action list_templates 408
  2166. 163 action list_templates 409
  2167. 164 action list_templates 410
  2168. 165 action list_templates 411
  2169. 166 action list_templates 412
  2170. 167 action list_templates 413
  2171. 168 action list_templates 414
  2172. 169 action list_templates 415
  2173. 170 action list_templates 416
  2174. 171 action list_templates 417
  2175. 157 template income_statement 418
  2176. 158 template balance_sheet 419
  2177. 159 template invoice 420
  2178. 160 template ar_transaction 421
  2179. 161 template ap_transaction 422
  2180. 162 template packing_list 423
  2181. 163 template pick_list 424
  2182. 164 template sales_order 425
  2183. 165 template work_order 426
  2184. 166 template purchase_order 427
  2185. 167 template bin_list 428
  2186. 168 template statement 429
  2187. 169 template quotation 430
  2188. 170 template rfq 431
  2189. 171 template timecard 432
  2190. 157 format HTML 433
  2191. 158 format HTML 434
  2192. 159 format HTML 435
  2193. 160 format HTML 436
  2194. 161 format HTML 437
  2195. 162 format HTML 438
  2196. 163 format HTML 439
  2197. 164 format HTML 440
  2198. 165 format HTML 441
  2199. 166 format HTML 442
  2200. 167 format HTML 443
  2201. 168 format HTML 444
  2202. 169 format HTML 445
  2203. 170 format HTML 446
  2204. 171 format HTML 447
  2205. 172 menu 1 448
  2206. 173 action list_templates 449
  2207. 174 action list_templates 450
  2208. 175 action list_templates 451
  2209. 176 action list_templates 452
  2210. 177 action list_templates 453
  2211. 178 action list_templates 454
  2212. 179 action list_templates 455
  2213. 180 action list_templates 456
  2214. 181 action list_templates 457
  2215. 182 action list_templates 458
  2216. 183 action list_templates 459
  2217. 184 action list_templates 460
  2218. 185 action list_templates 461
  2219. 186 action list_templates 462
  2220. 187 action list_templates 463
  2221. 173 module am.pl 464
  2222. 174 module am.pl 465
  2223. 175 module am.pl 466
  2224. 176 module am.pl 467
  2225. 177 module am.pl 468
  2226. 178 module am.pl 469
  2227. 179 module am.pl 470
  2228. 180 module am.pl 471
  2229. 181 module am.pl 472
  2230. 182 module am.pl 473
  2231. 183 module am.pl 474
  2232. 184 module am.pl 475
  2233. 185 module am.pl 476
  2234. 186 module am.pl 477
  2235. 187 module am.pl 478
  2236. 173 format LATEX 479
  2237. 174 format LATEX 480
  2238. 175 format LATEX 481
  2239. 176 format LATEX 482
  2240. 177 format LATEX 483
  2241. 178 format LATEX 484
  2242. 179 format LATEX 485
  2243. 180 format LATEX 486
  2244. 181 format LATEX 487
  2245. 182 format LATEX 488
  2246. 183 format LATEX 489
  2247. 184 format LATEX 490
  2248. 185 format LATEX 491
  2249. 186 format LATEX 492
  2250. 187 format LATEX 493
  2251. 173 template invoice 506
  2252. 174 template ar_transaction 507
  2253. 175 template ap_transaction 508
  2254. 176 template packing_list 509
  2255. 177 template pick_list 510
  2256. 178 template sales_order 511
  2257. 179 template work_order 512
  2258. 180 template purchase_order 513
  2259. 181 template bin_list 514
  2260. 182 template statement 515
  2261. 185 template quotation 518
  2262. 186 template rfq 519
  2263. 187 template timecard 520
  2264. 183 template check 516
  2265. 184 template receipt 517
  2266. 188 menu 1 521
  2267. 189 module am.pl 522
  2268. 189 action list_templates 523
  2269. 189 template pos_invoice 524
  2270. 189 format TEXT 525
  2271. 190 action display_stylesheet 526
  2272. 190 module am.pl 527
  2273. 191 module am.pl 528
  2274. 191 action config 529
  2275. 193 module login.pl 532
  2276. 193 action logout 533
  2277. 193 target _top 534
  2278. 192 menu 1 530
  2279. 192 new 1 531
  2280. 0 menu 1 535
  2281. 136 menu 1 536
  2282. 144 menu 1 537
  2283. 194 module ar.pl 538
  2284. 194 action add 539
  2285. 195 action add 540
  2286. 195 module is.pl 541
  2287. 196 action add 543
  2288. 196 module ap.pl 544
  2289. 197 action add 545
  2290. 197 module ir.pl 547
  2291. 196 type debit_note 549
  2292. 194 type credit_note 548
  2293. 195 type credit_invoice 542
  2294. 197 type debit_invoice 546
  2295. \.
  2296. --
  2297. -- Name: menu_attribute_id_key; Type: CONSTRAINT; Schema: public; Owner: ledgersmb; Tablespace:
  2298. --
  2299. ALTER TABLE ONLY menu_attribute
  2300. ADD CONSTRAINT menu_attribute_id_key UNIQUE (id);
  2301. --
  2302. -- Name: menu_attribute_pkey; Type: CONSTRAINT; Schema: public; Owner: ledgersmb; Tablespace:
  2303. --
  2304. ALTER TABLE ONLY menu_attribute
  2305. ADD CONSTRAINT menu_attribute_pkey PRIMARY KEY (node_id, attribute);
  2306. --
  2307. -- Name: menu_attribute_node_id_fkey; Type: FK CONSTRAINT; Schema: public; Owner: ledgersmb
  2308. --
  2309. ALTER TABLE ONLY menu_attribute
  2310. ADD CONSTRAINT menu_attribute_node_id_fkey FOREIGN KEY (node_id) REFERENCES menu_node(id);
  2311. --
  2312. -- PostgreSQL database dump complete
  2313. --
  2314. --
  2315. CREATE TABLE menu_acl (
  2316. id serial NOT NULL,
  2317. role_name character varying,
  2318. acl_type character varying,
  2319. node_id integer,
  2320. CONSTRAINT menu_acl_acl_type_check CHECK ((((acl_type)::text = 'allow'::text) OR ((acl_type)::text = 'deny'::text)))
  2321. );
  2322. ALTER TABLE ONLY menu_acl
  2323. ADD CONSTRAINT menu_acl_pkey PRIMARY KEY (id);
  2324. ALTER TABLE ONLY menu_acl
  2325. ADD CONSTRAINT menu_acl_node_id_fkey FOREIGN KEY (node_id) REFERENCES menu_node(id);
  2326. --
  2327. -- PostgreSQL database dump complete
  2328. --
  2329. CREATE TYPE menu_item AS (
  2330. position int,
  2331. id int,
  2332. level int,
  2333. label varchar,
  2334. path varchar,
  2335. args varchar[]
  2336. );
  2337. CREATE OR REPLACE FUNCTION menu_generate() RETURNS SETOF menu_item AS
  2338. $$
  2339. DECLARE
  2340. item menu_item;
  2341. arg menu_attribute%ROWTYPE;
  2342. BEGIN
  2343. FOR item IN
  2344. SELECT n.position, n.id, c.level, n.label, c.path, '{}'
  2345. FROM connectby('menu_node', 'id', 'parent', 'position', '0',
  2346. 0, ',')
  2347. c(id integer, parent integer, "level" integer,
  2348. path text, list_order integer)
  2349. JOIN menu_node n USING(id)
  2350. LOOP
  2351. FOR arg IN
  2352. SELECT *
  2353. FROM menu_attribute
  2354. WHERE node_id = item.id
  2355. LOOP
  2356. item.args := item.args ||
  2357. (arg.attribute || '=' || arg.value)::varchar;
  2358. END LOOP;
  2359. RETURN NEXT item;
  2360. END LOOP;
  2361. END;
  2362. $$ language plpgsql;
  2363. CREATE OR REPLACE FUNCTION menu_children(in_parent_id int) RETURNS SETOF menu_item
  2364. AS $$
  2365. declare
  2366. item menu_item;
  2367. arg menu_attribute%ROWTYPE;
  2368. begin
  2369. FOR item IN
  2370. SELECT n.position, n.id, c.level, n.label, c.path, '{}'
  2371. FROM connectby('menu_node', 'id', 'parent', 'position',
  2372. in_parent_id, 1, ',')
  2373. c(id integer, parent integer, "level" integer,
  2374. path text, list_order integer)
  2375. JOIN menu_node n USING(id)
  2376. LOOP
  2377. FOR arg IN
  2378. SELECT *
  2379. FROM menu_attribute
  2380. WHERE node_id = item.id
  2381. LOOP
  2382. item.args := item.args ||
  2383. (arg.attribute || '=' || arg.value)::varchar;
  2384. END LOOP;
  2385. return next item;
  2386. end loop;
  2387. end;
  2388. $$ language plpgsql;
  2389. COMMENT ON FUNCTION menu_children(int) IS $$ This function returns all menu items which are children of in_parent_id (the only input parameter. $$;
  2390. CREATE OR REPLACE FUNCTION
  2391. menu_insert(in_parent_id int, in_position int, in_label text)
  2392. returns int
  2393. AS $$
  2394. DECLARE
  2395. new_id int;
  2396. BEGIN
  2397. UPDATE menu_node
  2398. SET position = position * -1
  2399. WHERE parent = in_parent_id
  2400. AND position >= in_position;
  2401. INSERT INTO menu_node (parent, position, label)
  2402. VALUES (in_parent_id, in_position, in_label);
  2403. SELECT INTO new_id currval('menu_node_id_seq');
  2404. UPDATE menu_node
  2405. SET position = (position * -1) + 1
  2406. WHERE parent = in_parent_id
  2407. AND position < 0;
  2408. RETURN new_id;
  2409. END;
  2410. $$ language plpgsql;
  2411. comment on function menu_insert(int, int, text) is $$
  2412. This function inserts menu items at arbitrary positions. The arguments are, in
  2413. order: parent, position, label. The return value is the id number of the menu
  2414. item created. $$;
  2415. CREATE VIEW menu_friendly AS
  2416. SELECT t."level", t.path, t.list_order, (repeat(' '::text, (2 * t."level")) || (n.label)::text) AS label, n.id, n."position" FROM (connectby('menu_node'::text, 'id'::text, 'parent'::text, 'position'::text, '0'::text, 0, ','::text) t(id integer, parent integer, "level" integer, path text, list_order integer) JOIN menu_node n USING (id));
  2417. ALTER TABLE public.menu_friendly OWNER TO ledgersmb;
  2418. --
  2419. -- PostgreSQL database dump complete
  2420. --
  2421. CREATE AGGREGATE as_array (
  2422. BASETYPE = ANYELEMENT,
  2423. STYPE = ANYARRAY,
  2424. SFUNC = ARRAY_APPEND,
  2425. INITCOND = '{}'
  2426. );
  2427. CREATE AGGREGATE compound_array (
  2428. BASETYPE = ANYARRAY,
  2429. STYPE = ANYARRAY,
  2430. SFUNC = ARRAY_CAT,
  2431. INITCOND = '{}'
  2432. );
  2433. commit;