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