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