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