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