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