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