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