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