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