summaryrefslogtreecommitdiff
path: root/sql/Pg-database.sql
blob: a3100a2cd95690f884d0ae757b8394bd6ebe0069 (plain)
  1. begin;
  2. --
  3. CREATE TABLE transactions (
  4. id int PRIMARY KEY,
  5. table_name text
  6. );
  7. -- BEGIN new entity management
  8. CREATE TABLE entity (
  9. id serial PRIMARY KEY,
  10. name text check (name ~ '[[:alnum:]_]'),
  11. entity_class integer not null);
  12. COMMENT ON TABLE entity IS $$ The primary entity table to map to all contacts $$;
  13. 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 $$;
  14. CREATE TABLE entity_class (
  15. id serial primary key,
  16. class text check (class ~ '[[:alnum:]_]') NOT NULL,
  17. active boolean not null default TRUE);
  18. COMMENT ON TABLE entity_class IS $$ Defines the class type such as vendor, customer, contact, employee $$;
  19. COMMENT ON COLUMN entity_class.id IS $$ The first 7 values are reserved and permanent $$;
  20. CREATE UNIQUE index entity_class_unique_idx ON entity_class(lower(class));
  21. COMMENT ON INDEX entity_class_unique_idx IS $$ Helps truly define unique. Which we could do that with Primary Keys $$;
  22. ALTER TABLE entity ADD FOREIGN KEY (entity_class) REFERENCES entity_class(id);
  23. INSERT INTO entity_class (id,class) VALUES (1,'Vendor');
  24. INSERT INTO entity_class (id,class) VALUES (2,'Customer');
  25. INSERT INTO entity_class (id,class) VALUES (3,'Employee');
  26. INSERT INTO entity_class (id,class) VALUES (4,'Contact');
  27. INSERT INTO entity_class (id,class) VALUES (5,'Lead');
  28. INSERT INTO entity_class (id,class) VALUES (6,'Referral');
  29. SELECT setval('entity_class_id_seq',7);
  30. CREATE TABLE entity_class_to_entity (
  31. entity_class_id integer not null references entity_class(id),
  32. entity_id integer not null references entity(id),
  33. PRIMARY KEY(entity_class_id,entity_id)
  34. );
  35. COMMENT ON TABLE entity_class_to_entity IS $$ Relation builder for classes to entity $$;
  36. CREATE TABLE country (
  37. id serial PRIMARY KEY,
  38. name text check (name ~ '[[:alnum:]_]') NOT NULL,
  39. short_name text check (short_name ~ '[[:alnum:]_]') NOT NULL,
  40. itu text);
  41. 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 $$;
  42. CREATE UNIQUE INDEX country_name_idx on country(lower(name));
  43. CREATE TABLE location (
  44. id serial PRIMARY KEY,
  45. line_one text check (line_one ~ '[[:alnum:]_]') NOT NULL,
  46. line_two text,
  47. line_three text,
  48. city_province text check (city_province ~ '[[:alnum:]_]') NOT NULL,
  49. country_id integer not null REFERENCES country(id),
  50. mail_code text not null check (mail_code ~ '[[:alnum:]_]'));
  51. CREATE TABLE company (
  52. id serial UNIQUE,
  53. entity_id integer not null references entity(id),
  54. legal_name text check (legal_name ~ '[[:alnum:]_]'),
  55. entity_class_id integer not null references entity_class(id),
  56. primary_location_id integer references location(id),
  57. tax_id text,
  58. PRIMARY KEY (legal_name,primary_location_id));
  59. COMMENT ON COLUMN company.primary_location_id IS $$ This is the location that should show up by default for any forms $$;
  60. COMMENT ON COLUMN company.tax_id IS $$ In the US this would be a EIN. $$;
  61. CREATE TABLE company_to_location (
  62. location_id integer references location(id) not null,
  63. company_id integer references company(id) not null,
  64. PRIMARY KEY(location_id,company_id));
  65. CREATE TABLE salutation (
  66. id serial unique,
  67. salutation text primary key);
  68. INSERT INTO salutation (id,salutation) VALUES ('1','Dr.');
  69. INSERT INTO salutation (id,salutation) VALUES ('2','Miss.');
  70. INSERT INTO salutation (id,salutation) VALUES ('3','Mr.');
  71. INSERT INTO salutation (id,salutation) VALUES ('4','Mrs.');
  72. INSERT INTO salutation (id,salutation) VALUES ('5','Ms.');
  73. INSERT INTO salutation (id,salutation) VALUES ('6','Sir.');
  74. SELECT SETVAL('salutation_id_seq',7);
  75. CREATE TABLE person (
  76. id serial PRIMARY KEY,
  77. entity_id integer references entity(id) not null,
  78. salutation_id integer references salutation(id),
  79. first_name text check (first_name ~ '[[:alnum:]_]') NOT NULL,
  80. middle_name text,
  81. last_name text check (last_name ~ '[[:alnum:]_]') NOT NULL,
  82. primary_location_id integer references location(id));
  83. 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. $$;
  84. CREATE TABLE person_to_company (
  85. location_id integer references location(id) not null,
  86. person_id integer references person(id) not null,
  87. PRIMARY KEY (location_id,person_id));
  88. CREATE TABLE entity_other_name (
  89. entity_id integer not null references entity(id),
  90. other_name text check (other_name ~ '[[:alnum:]_]'),
  91. PRIMARY KEY (other_name, entity_id));
  92. 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. $$;
  93. CREATE TABLE person_to_entity (
  94. person_id integer not null references person(id),
  95. entity_id integer not null references entity(id) check (entity_id != person_id),
  96. related_how text,
  97. created date not null default current_date,
  98. PRIMARY KEY (person_id,entity_id));
  99. CREATE TABLE company_to_entity (
  100. company_id integer not null references company(id),
  101. entity_id integer not null references entity(id) check (entity_id != company_id),
  102. related_how text,
  103. created date not null default current_date,
  104. PRIMARY KEY (company_id,entity_id));
  105. CREATE TABLE contact_class (
  106. id serial UNIQUE,
  107. class text check (class ~ '[[:alnum:]_]') NOT NULL,
  108. PRIMARY KEY (class));
  109. CREATE UNIQUE INDEX contact_class_class_idx ON contact_class(lower(class));
  110. INSERT INTO contact_class (id,class) values (1,'Primary Phone');
  111. INSERT INTO contact_class (id,class) values (2,'Secondary Phone');
  112. INSERT INTO contact_class (id,class) values (3,'Cell Phone');
  113. INSERT INTO contact_class (id,class) values (4,'AIM');
  114. INSERT INTO contact_class (id,class) values (5,'Yahoo');
  115. INSERT INTO contact_class (id,class) values (6,'Gtalk');
  116. INSERT INTO contact_class (id,class) values (7,'MSN');
  117. INSERT INTO contact_class (id,class) values (8,'IRC');
  118. INSERT INTO contact_class (id,class) values (9,'Fax');
  119. INSERT INTO contact_class (id,class) values (10,'Generic Jabber');
  120. INSERT INTO contact_class (id,class) values (11,'Home Phone');
  121. SELECT SETVAL('contact_class_id_seq',12);
  122. CREATE TABLE person_to_contact (
  123. person_id integer references person(id) not null,
  124. contact_class_id integer references contact_class(id) not null,
  125. contact text check(contact ~ '[[:alnum:]_]') not null,
  126. PRIMARY KEY (person_id,contact_class_id,contact));
  127. COMMENT ON TABLE person_to_contact IS $$ To keep track of the relationship between multiple contact methods and a single individual $$;
  128. CREATE TABLE company_to_contact (
  129. company_id integer references company(id) not null,
  130. contact_class_id integer references contact_class(id) not null,
  131. contact text check(contact ~ '[[:alnum:]_]') not null,
  132. PRIMARY KEY (company_id,contact_class_id,contact));
  133. COMMENT ON TABLE company_to_contact IS $$ To keep track of the relationship between multiple contact methods and a single company $$;
  134. -- END entity
  135. --
  136. CREATE TABLE makemodel (
  137. parts_id int PRIMARY KEY,
  138. make text,
  139. model text
  140. );
  141. --
  142. CREATE TABLE gl (
  143. id serial PRIMARY KEY,
  144. reference text,
  145. description text,
  146. transdate date DEFAULT current_date,
  147. person_id integer references person(id),
  148. notes text,
  149. department_id int default 0
  150. );
  151. --
  152. CREATE TABLE chart (
  153. id serial PRIMARY KEY,
  154. accno text NOT NULL,
  155. description text,
  156. charttype char(1) DEFAULT 'A',
  157. category char(1),
  158. link text,
  159. gifi_accno text,
  160. contra bool DEFAULT 'f'
  161. );
  162. --
  163. CREATE TABLE gifi (
  164. accno text PRIMARY KEY,
  165. description text
  166. );
  167. --
  168. CREATE TABLE defaults (
  169. setting_key text primary key,
  170. value text
  171. );
  172. /*
  173. inventory_accno_id int,
  174. income_accno_id int,
  175. expense_accno_id int,
  176. fxgain_accno_id int,
  177. fxloss_accno_id int,
  178. */
  179. \COPY defaults FROM stdin WITH DELIMITER |
  180. sinumber|1
  181. sonumber|1
  182. yearend|1
  183. businessnumber|1
  184. version|1.2.0
  185. closedto|\N
  186. revtrans|1
  187. ponumber|1
  188. sqnumber|1
  189. rfqnumber|1
  190. audittrail|0
  191. vinumber|1
  192. employeenumber|1
  193. partnumber|1
  194. customernumber|1
  195. vendornumber|1
  196. glnumber|1
  197. projectnumber|1
  198. \.
  199. -- */
  200. CREATE TABLE acc_trans (
  201. trans_id int,
  202. chart_id int NOT NULL REFERENCES chart (id),
  203. amount NUMERIC,
  204. transdate date DEFAULT current_date,
  205. source text,
  206. cleared bool DEFAULT 'f',
  207. fx_transaction bool DEFAULT 'f',
  208. project_id int,
  209. memo text,
  210. invoice_id int,
  211. entry_id SERIAL PRIMARY KEY
  212. );
  213. --
  214. CREATE TABLE invoice (
  215. id serial PRIMARY KEY,
  216. trans_id int,
  217. parts_id int,
  218. description text,
  219. qty integer,
  220. allocated integer,
  221. sellprice NUMERIC,
  222. fxsellprice NUMERIC,
  223. discount float4, -- jd: check into this
  224. assemblyitem bool DEFAULT 'f',
  225. unit varchar(5),
  226. project_id int,
  227. deliverydate date,
  228. serialnumber text,
  229. notes text
  230. );
  231. --
  232. CREATE TABLE customer (
  233. id serial PRIMARY KEY,
  234. entity_id int references entity(id),
  235. discount numeric,
  236. taxincluded bool default 'f',
  237. creditlimit NUMERIC default 0,
  238. terms int2 default 0,
  239. customernumber varchar(32),
  240. cc text,
  241. bcc text,
  242. business_id int,
  243. taxnumber varchar(32),
  244. sic_code varchar(6),
  245. iban varchar(34),
  246. bic varchar(11),
  247. language_code varchar(6),
  248. pricegroup_id int,
  249. curr char(3),
  250. startdate date DEFAULT CURRENT_DATE,
  251. enddate date
  252. );
  253. COMMENT ON TABLE customer IS $$ This is now a metadata table that holds information specific to customers. Source info is not part of the entity management $$;
  254. COMMENT ON COLUMN customer.entity_id IS $$ This is the relationship between entities and customers $$;
  255. --
  256. --
  257. CREATE TABLE parts (
  258. id serial PRIMARY KEY,
  259. partnumber text,
  260. description text,
  261. unit varchar(5),
  262. listprice NUMERIC,
  263. sellprice NUMERIC,
  264. lastcost NUMERIC,
  265. priceupdate date DEFAULT current_date,
  266. weight numeric,
  267. onhand numeric DEFAULT 0,
  268. notes text,
  269. makemodel bool DEFAULT 'f',
  270. assembly bool DEFAULT 'f',
  271. alternate bool DEFAULT 'f',
  272. rop float4, -- jd: what is this
  273. inventory_accno_id int,
  274. income_accno_id int,
  275. expense_accno_id int,
  276. bin text,
  277. obsolete bool DEFAULT 'f',
  278. bom bool DEFAULT 'f',
  279. image text,
  280. drawing text,
  281. microfiche text,
  282. partsgroup_id int,
  283. project_id int,
  284. avgcost NUMERIC
  285. );
  286. --
  287. CREATE TABLE assembly (
  288. id int,
  289. parts_id int,
  290. qty numeric,
  291. bom bool,
  292. adj bool,
  293. PRIMARY KEY (id, parts_id)
  294. );
  295. --
  296. CREATE TABLE ar (
  297. id serial PRIMARY KEY,
  298. invnumber text,
  299. transdate date DEFAULT current_date,
  300. entity_id int REFERENCES entity(id),
  301. taxincluded bool,
  302. amount NUMERIC,
  303. netamount NUMERIC,
  304. paid NUMERIC,
  305. datepaid date,
  306. duedate date,
  307. invoice bool DEFAULT 'f',
  308. shippingpoint text,
  309. terms int2 DEFAULT 0,
  310. notes text,
  311. curr char(3),
  312. ordnumber text,
  313. person_id integer references person(id) not null,
  314. till varchar(20),
  315. quonumber text,
  316. intnotes text,
  317. department_id int default 0,
  318. shipvia text,
  319. language_code varchar(6),
  320. ponumber text
  321. );
  322. COMMENT ON COLUMN ar.entity_id IS $$ Used to be customer_id, but customer is now metadata. You need to push to entity $$;
  323. --
  324. CREATE TABLE ap (
  325. id serial PRIMARY KEY,
  326. invnumber text,
  327. transdate date DEFAULT current_date,
  328. entity_id int REFERENCES entity(id),
  329. taxincluded bool DEFAULT 'f',
  330. amount NUMERIC,
  331. netamount NUMERIC,
  332. paid NUMERIC,
  333. datepaid date,
  334. duedate date,
  335. invoice bool DEFAULT 'f',
  336. ordnumber text,
  337. curr char(3),
  338. notes text,
  339. person_id integer references person(id) not null,
  340. till varchar(20),
  341. quonumber text,
  342. intnotes text,
  343. department_id int DEFAULT 0,
  344. shipvia text,
  345. language_code varchar(6),
  346. ponumber text,
  347. shippingpoint text,
  348. terms int2 DEFAULT 0
  349. );
  350. COMMENT ON COLUMN ap.entity_id IS $$ Used to be customer_id, but customer is now metadata. You need to push to entity $$;
  351. --
  352. CREATE TABLE taxmodule (
  353. taxmodule_id serial PRIMARY KEY,
  354. taxmodulename text NOT NULL
  355. );
  356. --
  357. CREATE TABLE taxcategory (
  358. taxcategory_id serial PRIMARY KEY,
  359. taxcategoryname text NOT NULL,
  360. taxmodule_id int NOT NULL,
  361. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  362. );
  363. --
  364. CREATE TABLE partstax (
  365. parts_id int,
  366. chart_id int,
  367. taxcategory_id int,
  368. PRIMARY KEY (parts_id, chart_id),
  369. FOREIGN KEY (parts_id) REFERENCES parts (id),
  370. FOREIGN KEY (chart_id) REFERENCES chart (id),
  371. FOREIGN KEY (taxcategory_id) REFERENCES taxcategory (taxcategory_id)
  372. );
  373. --
  374. CREATE TABLE tax (
  375. chart_id int PRIMARY KEY,
  376. rate numeric,
  377. taxnumber text,
  378. validto date,
  379. pass integer DEFAULT 0 NOT NULL,
  380. taxmodule_id int DEFAULT 1 NOT NULL,
  381. FOREIGN KEY (chart_id) REFERENCES chart (id),
  382. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  383. );
  384. --
  385. CREATE TABLE customertax (
  386. customer_id int,
  387. chart_id int,
  388. PRIMARY KEY (customer_id, chart_id)
  389. );
  390. --
  391. CREATE TABLE vendortax (
  392. vendor_id int,
  393. chart_id int,
  394. PRIMARY KEY (vendor_id, chart_id)
  395. );
  396. --
  397. CREATE TABLE oe_class (
  398. id smallint unique check(id IN (1,2)),
  399. oe_class text primary key);
  400. INSERT INTO oe_class(id,oe_class) values (1,'Sales Order');
  401. INSERT INTO oe_class(id,oe_class) values (2,'Purchase Order');
  402. 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 $$;
  403. CREATE TABLE oe (
  404. id serial PRIMARY KEY,
  405. ordnumber text,
  406. transdate date default current_date,
  407. entity_id integer references entity(id) NOT NULL,
  408. amount NUMERIC,
  409. netamount NUMERIC,
  410. reqdate date,
  411. taxincluded bool,
  412. shippingpoint text,
  413. notes text,
  414. curr char(3),
  415. person_id integer references person(id) not null,
  416. closed bool default 'f',
  417. quotation bool default 'f',
  418. quonumber text,
  419. intnotes text,
  420. department_id int default 0,
  421. shipvia text,
  422. language_code varchar(6),
  423. ponumber text,
  424. terms int2 DEFAULT 0,
  425. oe_class_id int references oe_class(id) NOT NULL
  426. );
  427. --
  428. CREATE TABLE orderitems (
  429. id serial PRIMARY KEY,
  430. trans_id int,
  431. parts_id int,
  432. description text,
  433. qty numeric,
  434. sellprice NUMERIC,
  435. discount numeric,
  436. unit varchar(5),
  437. project_id int,
  438. reqdate date,
  439. ship numeric,
  440. serialnumber text,
  441. notes text
  442. );
  443. --
  444. CREATE TABLE exchangerate (
  445. curr char(3),
  446. transdate date,
  447. buy numeric,
  448. sell numeric,
  449. PRIMARY KEY (curr, transdate)
  450. );
  451. --
  452. create table employee (
  453. id serial PRIMARY KEY,
  454. entity_id integer references entity(id) not null,
  455. login text,
  456. startdate date default current_date,
  457. enddate date,
  458. notes text,
  459. role varchar(20),
  460. sales bool default 'f',
  461. ssn varchar(20),
  462. iban varchar(34),
  463. bic varchar(11),
  464. managerid int,
  465. employeenumber varchar(32),
  466. dob date
  467. );
  468. COMMENT ON TABLE employee IS $$ Is a metadata table specific to employees $$;
  469. --
  470. create table shipto (
  471. trans_id int,
  472. shiptoname varchar(64),
  473. shiptoaddress1 varchar(32),
  474. shiptoaddress2 varchar(32),
  475. shiptocity varchar(32),
  476. shiptostate varchar(32),
  477. shiptozipcode varchar(10),
  478. shiptocountry varchar(32),
  479. shiptocontact varchar(64),
  480. shiptophone varchar(20),
  481. shiptofax varchar(20),
  482. shiptoemail text,
  483. entry_id SERIAL PRIMARY KEY
  484. );
  485. -- SHIPTO really needs to be pushed into entities too
  486. --
  487. CREATE TABLE vendor (
  488. id serial PRIMARY KEY,
  489. entity_id int references entity(id) not null,
  490. terms int2 default 0,
  491. taxincluded bool default 'f',
  492. vendornumber varchar(32),
  493. cc text,
  494. bcc text,
  495. gifi_accno varchar(30),
  496. business_id int,
  497. taxnumber varchar(32),
  498. sic_code varchar(6),
  499. discount numeric,
  500. creditlimit numeric default 0,
  501. iban varchar(34),
  502. bic varchar(11),
  503. language_code varchar(6),
  504. pricegroup_id int,
  505. curr char(3),
  506. startdate date,
  507. enddate date
  508. );
  509. COMMENT ON TABLE vendor IS $$ Now a meta data table $$;
  510. --
  511. CREATE TABLE project (
  512. id serial PRIMARY KEY,
  513. projectnumber text,
  514. description text,
  515. startdate date,
  516. enddate date,
  517. parts_id int,
  518. production numeric default 0,
  519. completed numeric default 0,
  520. customer_id int
  521. );
  522. --
  523. CREATE TABLE partsgroup (
  524. id serial PRIMARY KEY,
  525. partsgroup text
  526. );
  527. --
  528. CREATE TABLE status (
  529. trans_id int PRIMARY KEY,
  530. formname text,
  531. printed bool default 'f',
  532. emailed bool default 'f',
  533. spoolfile text
  534. );
  535. --
  536. CREATE TABLE department (
  537. id serial PRIMARY KEY,
  538. description text,
  539. role char(1) default 'P'
  540. );
  541. --
  542. -- department transaction table
  543. CREATE TABLE dpt_trans (
  544. trans_id int PRIMARY KEY,
  545. department_id int
  546. );
  547. --
  548. -- business table
  549. CREATE TABLE business (
  550. id serial PRIMARY KEY,
  551. description text,
  552. discount numeric
  553. );
  554. --
  555. -- SIC
  556. CREATE TABLE sic (
  557. code varchar(6) PRIMARY KEY,
  558. sictype char(1),
  559. description text
  560. );
  561. --
  562. CREATE TABLE warehouse (
  563. id serial PRIMARY KEY,
  564. description text
  565. );
  566. --
  567. CREATE TABLE inventory (
  568. warehouse_id int,
  569. parts_id int,
  570. trans_id int,
  571. orderitems_id int,
  572. qty numeric,
  573. shippingdate date,
  574. person_id integer references person(id) not null,
  575. entry_id SERIAL PRIMARY KEY
  576. );
  577. --
  578. CREATE TABLE yearend (
  579. trans_id int PRIMARY KEY,
  580. transdate date
  581. );
  582. --
  583. CREATE TABLE partsvendor (
  584. entity_id int references entity(id) not null,
  585. parts_id int,
  586. partnumber text,
  587. leadtime int2,
  588. lastcost NUMERIC,
  589. curr char(3),
  590. entry_id SERIAL PRIMARY KEY
  591. );
  592. --
  593. CREATE TABLE pricegroup (
  594. id serial PRIMARY KEY,
  595. pricegroup text
  596. );
  597. --
  598. CREATE TABLE partscustomer (
  599. parts_id int,
  600. customer_id int,
  601. pricegroup_id int,
  602. pricebreak numeric,
  603. sellprice NUMERIC,
  604. validfrom date,
  605. validto date,
  606. curr char(3),
  607. entry_id SERIAL PRIMARY KEY
  608. );
  609. -- How does partscustomer.customer_id relate here?
  610. --
  611. CREATE TABLE language (
  612. code varchar(6) PRIMARY KEY,
  613. description text
  614. );
  615. --
  616. CREATE TABLE audittrail (
  617. trans_id int,
  618. tablename text,
  619. reference text,
  620. formname text,
  621. action text,
  622. transdate timestamp default current_timestamp,
  623. person_id integer references person(id) not null,
  624. entry_id BIGSERIAL PRIMARY KEY
  625. );
  626. --
  627. CREATE TABLE translation (
  628. trans_id int,
  629. language_code varchar(6),
  630. description text,
  631. PRIMARY KEY (trans_id, language_code)
  632. );
  633. --
  634. CREATE TABLE recurring (
  635. id int PRIMARY KEY,
  636. reference text,
  637. startdate date,
  638. nextdate date,
  639. enddate date,
  640. repeat int2,
  641. unit varchar(6),
  642. howmany int,
  643. payment bool default 'f'
  644. );
  645. --
  646. CREATE TABLE recurringemail (
  647. id int PRIMARY KEY,
  648. formname text,
  649. format text,
  650. message text
  651. );
  652. --
  653. CREATE TABLE recurringprint (
  654. id int PRIMARY KEY,
  655. formname text,
  656. format text,
  657. printer text
  658. );
  659. --
  660. CREATE TABLE jcitems (
  661. id serial PRIMARY KEY,
  662. project_id int,
  663. parts_id int,
  664. description text,
  665. qty numeric,
  666. allocated numeric,
  667. sellprice NUMERIC,
  668. fxsellprice NUMERIC,
  669. serialnumber text,
  670. checkedin timestamp with time zone,
  671. checkedout timestamp with time zone,
  672. person_id integer references person(id) not null,
  673. notes text
  674. );
  675. insert into transactions (id, table_name) SELECT id, 'ap' FROM ap;
  676. CREATE RULE ap_id_track_i AS ON insert TO ap
  677. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ap');
  678. CREATE RULE ap_id_track_u AS ON update TO ap
  679. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  680. insert into transactions (id, table_name) SELECT id, 'ar' FROM ap;
  681. CREATE RULE ar_id_track_i AS ON insert TO ar
  682. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ar');
  683. CREATE RULE ar_id_track_u AS ON update TO ar
  684. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  685. INSERT INTO transactions (id, table_name) SELECT id, 'business' FROM business;
  686. CREATE RULE business_id_track_i AS ON insert TO business
  687. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'business');
  688. CREATE RULE business_id_track_u AS ON update TO business
  689. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  690. INSERT INTO transactions (id, table_name) SELECT id, 'chart' FROM chart;
  691. CREATE RULE chart_id_track_i AS ON insert TO chart
  692. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'chart');
  693. CREATE RULE chart_id_track_u AS ON update TO chart
  694. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  695. INSERT INTO transactions (id, table_name) SELECT id, 'customer' FROM customer;
  696. CREATE RULE customer_id_track_i AS ON insert TO customer
  697. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'customer');
  698. CREATE RULE customer_id_track_u AS ON update TO customer
  699. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  700. INSERT INTO transactions (id, table_name) SELECT id, 'department' FROM department;
  701. CREATE RULE department_id_track_i AS ON insert TO department
  702. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'department');
  703. CREATE RULE department_id_track_u AS ON update TO department
  704. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  705. INSERT INTO transactions (id, table_name) SELECT id, 'employee' FROM employee;
  706. INSERT INTO transactions (id, table_name) SELECT id, 'gl' FROM gl;
  707. CREATE RULE gl_id_track_i AS ON insert TO gl
  708. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'gl');
  709. CREATE RULE gl_id_track_u AS ON update TO gl
  710. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  711. INSERT INTO transactions (id, table_name) SELECT id, 'oe' FROM oe;
  712. CREATE RULE oe_id_track_i AS ON insert TO oe
  713. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'oe');
  714. CREATE RULE oe_id_track_u AS ON update TO oe
  715. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  716. INSERT INTO transactions (id, table_name) SELECT id, 'parts' FROM parts;
  717. CREATE RULE parts_id_track_i AS ON insert TO parts
  718. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'parts');
  719. CREATE RULE parts_id_track_u AS ON update TO parts
  720. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  721. INSERT INTO transactions (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  722. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  723. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'partsgroup');
  724. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  725. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  726. INSERT INTO transactions (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  727. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  728. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'pricegroup');
  729. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  730. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  731. INSERT INTO transactions (id, table_name) SELECT id, 'project' FROM project;
  732. CREATE RULE project_id_track_i AS ON insert TO project
  733. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'project');
  734. CREATE RULE project_id_track_u AS ON update TO project
  735. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  736. INSERT INTO transactions (id, table_name) SELECT id, 'vendor' FROM vendor;
  737. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  738. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'vendor');
  739. INSERT INTO transactions (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  740. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  741. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  742. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  743. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  744. CREATE TABLE custom_table_catalog (
  745. table_id SERIAL PRIMARY KEY,
  746. extends TEXT,
  747. table_name TEXT
  748. );
  749. CREATE TABLE custom_field_catalog (
  750. field_id SERIAL PRIMARY KEY,
  751. table_id INT REFERENCES custom_table_catalog,
  752. field_name TEXT
  753. );
  754. INSERT INTO taxmodule (
  755. taxmodule_id, taxmodulename
  756. ) VALUES (
  757. 1, 'Simple'
  758. );
  759. create index acc_trans_trans_id_key on acc_trans (trans_id);
  760. create index acc_trans_chart_id_key on acc_trans (chart_id);
  761. create index acc_trans_transdate_key on acc_trans (transdate);
  762. create index acc_trans_source_key on acc_trans (lower(source));
  763. --
  764. create index ap_id_key on ap (id);
  765. create index ap_transdate_key on ap (transdate);
  766. create index ap_invnumber_key on ap (invnumber);
  767. create index ap_ordnumber_key on ap (ordnumber);
  768. create index ap_quonumber_key on ap (quonumber);
  769. --
  770. create index ar_id_key on ar (id);
  771. create index ar_transdate_key on ar (transdate);
  772. create index ar_invnumber_key on ar (invnumber);
  773. create index ar_ordnumber_key on ar (ordnumber);
  774. create index ar_quonumber_key on ar (quonumber);
  775. --
  776. create index assembly_id_key on assembly (id);
  777. --
  778. create index chart_id_key on chart (id);
  779. create unique index chart_accno_key on chart (accno);
  780. create index chart_category_key on chart (category);
  781. create index chart_link_key on chart (link);
  782. create index chart_gifi_accno_key on chart (gifi_accno);
  783. --
  784. create index customer_id_key on customer (id);
  785. create index customer_customernumber_key on customer (customernumber);
  786. create index customer_customer_id_key on customertax (customer_id);
  787. --
  788. create unique index employee_login_key on employee (login);
  789. --
  790. create index exchangerate_ct_key on exchangerate (curr, transdate);
  791. --
  792. create unique index gifi_accno_key on gifi (accno);
  793. --
  794. create index gl_id_key on gl (id);
  795. create index gl_transdate_key on gl (transdate);
  796. create index gl_reference_key on gl (reference);
  797. create index gl_description_key on gl (lower(description));
  798. --
  799. create index invoice_id_key on invoice (id);
  800. create index invoice_trans_id_key on invoice (trans_id);
  801. --
  802. create index makemodel_parts_id_key on makemodel (parts_id);
  803. create index makemodel_make_key on makemodel (lower(make));
  804. create index makemodel_model_key on makemodel (lower(model));
  805. --
  806. create index oe_id_key on oe (id);
  807. create index oe_transdate_key on oe (transdate);
  808. create index oe_ordnumber_key on oe (ordnumber);
  809. create index orderitems_trans_id_key on orderitems (trans_id);
  810. create index orderitems_id_key on orderitems (id);
  811. --
  812. create index parts_id_key on parts (id);
  813. create index parts_partnumber_key on parts (lower(partnumber));
  814. create index parts_description_key on parts (lower(description));
  815. create index partstax_parts_id_key on partstax (parts_id);
  816. --
  817. create index vendor_id_key on vendor (id);
  818. create index vendor_vendornumber_key on vendor (vendornumber);
  819. --
  820. create index shipto_trans_id_key on shipto (trans_id);
  821. --
  822. create index project_id_key on project (id);
  823. create unique index projectnumber_key on project (projectnumber);
  824. --
  825. create index partsgroup_id_key on partsgroup (id);
  826. create unique index partsgroup_key on partsgroup (partsgroup);
  827. --
  828. create index status_trans_id_key on status (trans_id);
  829. --
  830. create index department_id_key on department (id);
  831. --
  832. create index partsvendor_parts_id_key on partsvendor (parts_id);
  833. --
  834. create index pricegroup_pricegroup_key on pricegroup (pricegroup);
  835. create index pricegroup_id_key on pricegroup (id);
  836. --
  837. create index audittrail_trans_id_key on audittrail (trans_id);
  838. --
  839. create index translation_trans_id_key on translation (trans_id);
  840. --
  841. create unique index language_code_key on language (code);
  842. --
  843. create index jcitems_id_key on jcitems (id);
  844. -- Popular some entity data
  845. INSERT INTO country(short_name,name) VALUES ('AC','Ascension Island');
  846. INSERT INTO country(short_name,name) VALUES ('AD','Andorra');
  847. INSERT INTO country(short_name,name) VALUES ('AE','United Arab Emirates');
  848. INSERT INTO country(short_name,name) VALUES ('AF','Afghanistan');
  849. INSERT INTO country(short_name,name) VALUES ('AG','Antigua and Barbuda');
  850. INSERT INTO country(short_name,name) VALUES ('AI','Anguilla');
  851. INSERT INTO country(short_name,name) VALUES ('AL','Albania');
  852. INSERT INTO country(short_name,name) VALUES ('AM','Armenia');
  853. INSERT INTO country(short_name,name) VALUES ('AN','Netherlands Antilles');
  854. INSERT INTO country(short_name,name) VALUES ('AO','Angola');
  855. INSERT INTO country(short_name,name) VALUES ('AQ','Antarctica');
  856. INSERT INTO country(short_name,name) VALUES ('AR','Argentina');
  857. INSERT INTO country(short_name,name) VALUES ('AS','American Samoa');
  858. INSERT INTO country(short_name,name) VALUES ('AT','Austria');
  859. INSERT INTO country(short_name,name) VALUES ('AU','Australia');
  860. INSERT INTO country(short_name,name) VALUES ('AW','Aruba');
  861. INSERT INTO country(short_name,name) VALUES ('AX','Aland Islands');
  862. INSERT INTO country(short_name,name) VALUES ('AZ','Azerbaijan');
  863. INSERT INTO country(short_name,name) VALUES ('BA','Bosnia and Herzegovina');
  864. INSERT INTO country(short_name,name) VALUES ('BB','Barbados');
  865. INSERT INTO country(short_name,name) VALUES ('BD','Bangladesh');
  866. INSERT INTO country(short_name,name) VALUES ('BE','Belgium');
  867. INSERT INTO country(short_name,name) VALUES ('BF','Burkina Faso');
  868. INSERT INTO country(short_name,name) VALUES ('BG','Bulgaria');
  869. INSERT INTO country(short_name,name) VALUES ('BH','Bahrain');
  870. INSERT INTO country(short_name,name) VALUES ('BI','Burundi');
  871. INSERT INTO country(short_name,name) VALUES ('BJ','Benin');
  872. INSERT INTO country(short_name,name) VALUES ('BM','Bermuda');
  873. INSERT INTO country(short_name,name) VALUES ('BN','Brunei Darussalam');
  874. INSERT INTO country(short_name,name) VALUES ('BO','Bolivia');
  875. INSERT INTO country(short_name,name) VALUES ('BR','Brazil');
  876. INSERT INTO country(short_name,name) VALUES ('BS','Bahamas');
  877. INSERT INTO country(short_name,name) VALUES ('BT','Bhutan');
  878. INSERT INTO country(short_name,name) VALUES ('BV','Bouvet Island');
  879. INSERT INTO country(short_name,name) VALUES ('BW','Botswana');
  880. INSERT INTO country(short_name,name) VALUES ('BY','Belarus');
  881. INSERT INTO country(short_name,name) VALUES ('BZ','Belize');
  882. INSERT INTO country(short_name,name) VALUES ('CA','Canada');
  883. INSERT INTO country(short_name,name) VALUES ('CC','Cocos (Keeling) Islands');
  884. INSERT INTO country(short_name,name) VALUES ('CD','Congo, Democratic Republic');
  885. INSERT INTO country(short_name,name) VALUES ('CF','Central African Republic');
  886. INSERT INTO country(short_name,name) VALUES ('CG','Congo');
  887. INSERT INTO country(short_name,name) VALUES ('CH','Switzerland');
  888. INSERT INTO country(short_name,name) VALUES ('CI','Cote D\'Ivoire (Ivory Coast)');
  889. INSERT INTO country(short_name,name) VALUES ('CK','Cook Islands');
  890. INSERT INTO country(short_name,name) VALUES ('CL','Chile');
  891. INSERT INTO country(short_name,name) VALUES ('CM','Cameroon');
  892. INSERT INTO country(short_name,name) VALUES ('CN','China');
  893. INSERT INTO country(short_name,name) VALUES ('CO','Colombia');
  894. INSERT INTO country(short_name,name) VALUES ('CR','Costa Rica');
  895. INSERT INTO country(short_name,name) VALUES ('CS','Czechoslovakia (former)');
  896. INSERT INTO country(short_name,name) VALUES ('CU','Cuba');
  897. INSERT INTO country(short_name,name) VALUES ('CV','Cape Verde');
  898. INSERT INTO country(short_name,name) VALUES ('CX','Christmas Island');
  899. INSERT INTO country(short_name,name) VALUES ('CY','Cyprus');
  900. INSERT INTO country(short_name,name) VALUES ('CZ','Czech Republic');
  901. INSERT INTO country(short_name,name) VALUES ('DE','Germany');
  902. INSERT INTO country(short_name,name) VALUES ('DJ','Djibouti');
  903. INSERT INTO country(short_name,name) VALUES ('DK','Denmark');
  904. INSERT INTO country(short_name,name) VALUES ('DM','Dominica');
  905. INSERT INTO country(short_name,name) VALUES ('DO','Dominican Republic');
  906. INSERT INTO country(short_name,name) VALUES ('DZ','Algeria');
  907. INSERT INTO country(short_name,name) VALUES ('EC','Ecuador');
  908. INSERT INTO country(short_name,name) VALUES ('EE','Estonia');
  909. INSERT INTO country(short_name,name) VALUES ('EG','Egypt');
  910. INSERT INTO country(short_name,name) VALUES ('EH','Western Sahara');
  911. INSERT INTO country(short_name,name) VALUES ('ER','Eritrea');
  912. INSERT INTO country(short_name,name) VALUES ('ES','Spain');
  913. INSERT INTO country(short_name,name) VALUES ('ET','Ethiopia');
  914. INSERT INTO country(short_name,name) VALUES ('FI','Finland');
  915. INSERT INTO country(short_name,name) VALUES ('FJ','Fiji');
  916. INSERT INTO country(short_name,name) VALUES ('FK','Falkland Islands (Malvinas)');
  917. INSERT INTO country(short_name,name) VALUES ('FM','Micronesia');
  918. INSERT INTO country(short_name,name) VALUES ('FO','Faroe Islands');
  919. INSERT INTO country(short_name,name) VALUES ('FR','France');
  920. INSERT INTO country(short_name,name) VALUES ('FX','France, Metropolitan');
  921. INSERT INTO country(short_name,name) VALUES ('GA','Gabon');
  922. INSERT INTO country(short_name,name) VALUES ('GB','Great Britain (UK)');
  923. INSERT INTO country(short_name,name) VALUES ('GD','Grenada');
  924. INSERT INTO country(short_name,name) VALUES ('GE','Georgia');
  925. INSERT INTO country(short_name,name) VALUES ('GF','French Guiana');
  926. INSERT INTO country(short_name,name) VALUES ('GH','Ghana');
  927. INSERT INTO country(short_name,name) VALUES ('GI','Gibraltar');
  928. INSERT INTO country(short_name,name) VALUES ('GL','Greenland');
  929. INSERT INTO country(short_name,name) VALUES ('GM','Gambia');
  930. INSERT INTO country(short_name,name) VALUES ('GN','Guinea');
  931. INSERT INTO country(short_name,name) VALUES ('GP','Guadeloupe');
  932. INSERT INTO country(short_name,name) VALUES ('GQ','Equatorial Guinea');
  933. INSERT INTO country(short_name,name) VALUES ('GR','Greece');
  934. INSERT INTO country(short_name,name) VALUES ('GS','S. Georgia and S. Sandwich Isls.');
  935. INSERT INTO country(short_name,name) VALUES ('GT','Guatemala');
  936. INSERT INTO country(short_name,name) VALUES ('GU','Guam');
  937. INSERT INTO country(short_name,name) VALUES ('GW','Guinea-Bissau');
  938. INSERT INTO country(short_name,name) VALUES ('GY','Guyana');
  939. INSERT INTO country(short_name,name) VALUES ('HK','Hong Kong');
  940. INSERT INTO country(short_name,name) VALUES ('HM','Heard and McDonald Islands');
  941. INSERT INTO country(short_name,name) VALUES ('HN','Honduras');
  942. INSERT INTO country(short_name,name) VALUES ('HR','Croatia (Hrvatska)');
  943. INSERT INTO country(short_name,name) VALUES ('HT','Haiti');
  944. INSERT INTO country(short_name,name) VALUES ('HU','Hungary');
  945. INSERT INTO country(short_name,name) VALUES ('ID','Indonesia');
  946. INSERT INTO country(short_name,name) VALUES ('IE','Ireland');
  947. INSERT INTO country(short_name,name) VALUES ('IL','Israel');
  948. INSERT INTO country(short_name,name) VALUES ('IM','Isle of Man');
  949. INSERT INTO country(short_name,name) VALUES ('IN','India');
  950. INSERT INTO country(short_name,name) VALUES ('IO','British Indian Ocean Territory');
  951. INSERT INTO country(short_name,name) VALUES ('IQ','Iraq');
  952. INSERT INTO country(short_name,name) VALUES ('IR','Iran');
  953. INSERT INTO country(short_name,name) VALUES ('IS','Iceland');
  954. INSERT INTO country(short_name,name) VALUES ('IT','Italy');
  955. INSERT INTO country(short_name,name) VALUES ('JE','Jersey');
  956. INSERT INTO country(short_name,name) VALUES ('JM','Jamaica');
  957. INSERT INTO country(short_name,name) VALUES ('JO','Jordan');
  958. INSERT INTO country(short_name,name) VALUES ('JP','Japan');
  959. INSERT INTO country(short_name,name) VALUES ('KE','Kenya');
  960. INSERT INTO country(short_name,name) VALUES ('KG','Kyrgyzstan');
  961. INSERT INTO country(short_name,name) VALUES ('KH','Cambodia');
  962. INSERT INTO country(short_name,name) VALUES ('KI','Kiribati');
  963. INSERT INTO country(short_name,name) VALUES ('KM','Comoros');
  964. INSERT INTO country(short_name,name) VALUES ('KN','Saint Kitts and Nevis');
  965. INSERT INTO country(short_name,name) VALUES ('KP','Korea (North)');
  966. INSERT INTO country(short_name,name) VALUES ('KR','Korea (South)');
  967. INSERT INTO country(short_name,name) VALUES ('KW','Kuwait');
  968. INSERT INTO country(short_name,name) VALUES ('KY','Cayman Islands');
  969. INSERT INTO country(short_name,name) VALUES ('KZ','Kazakhstan');
  970. INSERT INTO country(short_name,name) VALUES ('LA','Laos');
  971. INSERT INTO country(short_name,name) VALUES ('LB','Lebanon');
  972. INSERT INTO country(short_name,name) VALUES ('LC','Saint Lucia');
  973. INSERT INTO country(short_name,name) VALUES ('LI','Liechtenstein');
  974. INSERT INTO country(short_name,name) VALUES ('LK','Sri Lanka');
  975. INSERT INTO country(short_name,name) VALUES ('LR','Liberia');
  976. INSERT INTO country(short_name,name) VALUES ('LS','Lesotho');
  977. INSERT INTO country(short_name,name) VALUES ('LT','Lithuania');
  978. INSERT INTO country(short_name,name) VALUES ('LU','Luxembourg');
  979. INSERT INTO country(short_name,name) VALUES ('LV','Latvia');
  980. INSERT INTO country(short_name,name) VALUES ('LY','Libya');
  981. INSERT INTO country(short_name,name) VALUES ('MA','Morocco');
  982. INSERT INTO country(short_name,name) VALUES ('MC','Monaco');
  983. INSERT INTO country(short_name,name) VALUES ('MD','Moldova');
  984. INSERT INTO country(short_name,name) VALUES ('MG','Madagascar');
  985. INSERT INTO country(short_name,name) VALUES ('MH','Marshall Islands');
  986. INSERT INTO country(short_name,name) VALUES ('MK','F.Y.R.O.M. (Macedonia)');
  987. INSERT INTO country(short_name,name) VALUES ('ML','Mali');
  988. INSERT INTO country(short_name,name) VALUES ('MM','Myanmar');
  989. INSERT INTO country(short_name,name) VALUES ('MN','Mongolia');
  990. INSERT INTO country(short_name,name) VALUES ('MO','Macau');
  991. INSERT INTO country(short_name,name) VALUES ('MP','Northern Mariana Islands');
  992. INSERT INTO country(short_name,name) VALUES ('MQ','Martinique');
  993. INSERT INTO country(short_name,name) VALUES ('MR','Mauritania');
  994. INSERT INTO country(short_name,name) VALUES ('MS','Montserrat');
  995. INSERT INTO country(short_name,name) VALUES ('MT','Malta');
  996. INSERT INTO country(short_name,name) VALUES ('MU','Mauritius');
  997. INSERT INTO country(short_name,name) VALUES ('MV','Maldives');
  998. INSERT INTO country(short_name,name) VALUES ('MW','Malawi');
  999. INSERT INTO country(short_name,name) VALUES ('MX','Mexico');
  1000. INSERT INTO country(short_name,name) VALUES ('MY','Malaysia');
  1001. INSERT INTO country(short_name,name) VALUES ('MZ','Mozambique');
  1002. INSERT INTO country(short_name,name) VALUES ('NA','Namibia');
  1003. INSERT INTO country(short_name,name) VALUES ('NC','New Caledonia');
  1004. INSERT INTO country(short_name,name) VALUES ('NE','Niger');
  1005. INSERT INTO country(short_name,name) VALUES ('NF','Norfolk Island');
  1006. INSERT INTO country(short_name,name) VALUES ('NG','Nigeria');
  1007. INSERT INTO country(short_name,name) VALUES ('NI','Nicaragua');
  1008. INSERT INTO country(short_name,name) VALUES ('NL','Netherlands');
  1009. INSERT INTO country(short_name,name) VALUES ('NO','Norway');
  1010. INSERT INTO country(short_name,name) VALUES ('NP','Nepal');
  1011. INSERT INTO country(short_name,name) VALUES ('NR','Nauru');
  1012. INSERT INTO country(short_name,name) VALUES ('NT','Neutral Zone');
  1013. INSERT INTO country(short_name,name) VALUES ('NU','Niue');
  1014. INSERT INTO country(short_name,name) VALUES ('NZ','New Zealand (Aotearoa)');
  1015. INSERT INTO country(short_name,name) VALUES ('OM','Oman');
  1016. INSERT INTO country(short_name,name) VALUES ('PA','Panama');
  1017. INSERT INTO country(short_name,name) VALUES ('PE','Peru');
  1018. INSERT INTO country(short_name,name) VALUES ('PF','French Polynesia');
  1019. INSERT INTO country(short_name,name) VALUES ('PG','Papua New Guinea');
  1020. INSERT INTO country(short_name,name) VALUES ('PH','Philippines');
  1021. INSERT INTO country(short_name,name) VALUES ('PK','Pakistan');
  1022. INSERT INTO country(short_name,name) VALUES ('PL','Poland');
  1023. INSERT INTO country(short_name,name) VALUES ('PM','St. Pierre and Miquelon');
  1024. INSERT INTO country(short_name,name) VALUES ('PN','Pitcairn');
  1025. INSERT INTO country(short_name,name) VALUES ('PR','Puerto Rico');
  1026. INSERT INTO country(short_name,name) VALUES ('PS','Palestinian Territory, Occupied');
  1027. INSERT INTO country(short_name,name) VALUES ('PT','Portugal');
  1028. INSERT INTO country(short_name,name) VALUES ('PW','Palau');
  1029. INSERT INTO country(short_name,name) VALUES ('PY','Paraguay');
  1030. INSERT INTO country(short_name,name) VALUES ('QA','Qatar');
  1031. INSERT INTO country(short_name,name) VALUES ('RE','Reunion');
  1032. INSERT INTO country(short_name,name) VALUES ('RO','Romania');
  1033. INSERT INTO country(short_name,name) VALUES ('RS','Serbia');
  1034. INSERT INTO country(short_name,name) VALUES ('RU','Russian Federation');
  1035. INSERT INTO country(short_name,name) VALUES ('RW','Rwanda');
  1036. INSERT INTO country(short_name,name) VALUES ('SA','Saudi Arabia');
  1037. INSERT INTO country(short_name,name) VALUES ('SB','Solomon Islands');
  1038. INSERT INTO country(short_name,name) VALUES ('SC','Seychelles');
  1039. INSERT INTO country(short_name,name) VALUES ('SD','Sudan');
  1040. INSERT INTO country(short_name,name) VALUES ('SE','Sweden');
  1041. INSERT INTO country(short_name,name) VALUES ('SG','Singapore');
  1042. INSERT INTO country(short_name,name) VALUES ('SH','St. Helena');
  1043. INSERT INTO country(short_name,name) VALUES ('SI','Slovenia');
  1044. INSERT INTO country(short_name,name) VALUES ('SJ','Svalbard & Jan Mayen Islands');
  1045. INSERT INTO country(short_name,name) VALUES ('SK','Slovak Republic');
  1046. INSERT INTO country(short_name,name) VALUES ('SL','Sierra Leone');
  1047. INSERT INTO country(short_name,name) VALUES ('SM','San Marino');
  1048. INSERT INTO country(short_name,name) VALUES ('SN','Senegal');
  1049. INSERT INTO country(short_name,name) VALUES ('SO','Somalia');
  1050. INSERT INTO country(short_name,name) VALUES ('SR','Suriname');
  1051. INSERT INTO country(short_name,name) VALUES ('ST','Sao Tome and Principe');
  1052. INSERT INTO country(short_name,name) VALUES ('SU','USSR (former)');
  1053. INSERT INTO country(short_name,name) VALUES ('SV','El Salvador');
  1054. INSERT INTO country(short_name,name) VALUES ('SY','Syria');
  1055. INSERT INTO country(short_name,name) VALUES ('SZ','Swaziland');
  1056. INSERT INTO country(short_name,name) VALUES ('TC','Turks and Caicos Islands');
  1057. INSERT INTO country(short_name,name) VALUES ('TD','Chad');
  1058. INSERT INTO country(short_name,name) VALUES ('TF','French Southern Territories');
  1059. INSERT INTO country(short_name,name) VALUES ('TG','Togo');
  1060. INSERT INTO country(short_name,name) VALUES ('TH','Thailand');
  1061. INSERT INTO country(short_name,name) VALUES ('TJ','Tajikistan');
  1062. INSERT INTO country(short_name,name) VALUES ('TK','Tokelau');
  1063. INSERT INTO country(short_name,name) VALUES ('TM','Turkmenistan');
  1064. INSERT INTO country(short_name,name) VALUES ('TN','Tunisia');
  1065. INSERT INTO country(short_name,name) VALUES ('TO','Tonga');
  1066. INSERT INTO country(short_name,name) VALUES ('TP','East Timor');
  1067. INSERT INTO country(short_name,name) VALUES ('TR','Turkey');
  1068. INSERT INTO country(short_name,name) VALUES ('TT','Trinidad and Tobago');
  1069. INSERT INTO country(short_name,name) VALUES ('TV','Tuvalu');
  1070. INSERT INTO country(short_name,name) VALUES ('TW','Taiwan');
  1071. INSERT INTO country(short_name,name) VALUES ('TZ','Tanzania');
  1072. INSERT INTO country(short_name,name) VALUES ('UA','Ukraine');
  1073. INSERT INTO country(short_name,name) VALUES ('UG','Uganda');
  1074. INSERT INTO country(short_name,name) VALUES ('UK','United Kingdom');
  1075. INSERT INTO country(short_name,name) VALUES ('UM','US Minor Outlying Islands');
  1076. INSERT INTO country(short_name,name) VALUES ('US','United States');
  1077. INSERT INTO country(short_name,name) VALUES ('UY','Uruguay');
  1078. INSERT INTO country(short_name,name) VALUES ('UZ','Uzbekistan');
  1079. INSERT INTO country(short_name,name) VALUES ('VA','Vatican City State (Holy See)');
  1080. INSERT INTO country(short_name,name) VALUES ('VC','Saint Vincent & the Grenadines');
  1081. INSERT INTO country(short_name,name) VALUES ('VE','Venezuela');
  1082. INSERT INTO country(short_name,name) VALUES ('VG','British Virgin Islands');
  1083. INSERT INTO country(short_name,name) VALUES ('VI','Virgin Islands (U.S.)');
  1084. INSERT INTO country(short_name,name) VALUES ('VN','Viet Nam');
  1085. INSERT INTO country(short_name,name) VALUES ('VU','Vanuatu');
  1086. INSERT INTO country(short_name,name) VALUES ('WF','Wallis and Futuna Islands');
  1087. INSERT INTO country(short_name,name) VALUES ('WS','Samoa');
  1088. INSERT INTO country(short_name,name) VALUES ('YE','Yemen');
  1089. INSERT INTO country(short_name,name) VALUES ('YT','Mayotte');
  1090. INSERT INTO country(short_name,name) VALUES ('YU','Yugoslavia (former)');
  1091. INSERT INTO country(short_name,name) VALUES ('ZA','South Africa');
  1092. INSERT INTO country(short_name,name) VALUES ('ZM','Zambia');
  1093. INSERT INTO country(short_name,name) VALUES ('ZR','Zaire');
  1094. INSERT INTO country(short_name,name) VALUES ('ZW','Zimbabwe');
  1095. --
  1096. CREATE FUNCTION del_yearend() RETURNS TRIGGER AS '
  1097. begin
  1098. delete from yearend where trans_id = old.id;
  1099. return NULL;
  1100. end;
  1101. ' language 'plpgsql';
  1102. -- end function
  1103. --
  1104. CREATE TRIGGER del_yearend AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_yearend();
  1105. -- end trigger
  1106. --
  1107. CREATE FUNCTION del_department() RETURNS TRIGGER AS '
  1108. begin
  1109. delete from dpt_trans where trans_id = old.id;
  1110. return NULL;
  1111. end;
  1112. ' language 'plpgsql';
  1113. -- end function
  1114. --
  1115. CREATE TRIGGER del_department AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_department();
  1116. -- end trigger
  1117. CREATE TRIGGER del_department AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_department();
  1118. -- end trigger
  1119. CREATE TRIGGER del_department AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_department();
  1120. -- end trigger
  1121. CREATE TRIGGER del_department AFTER DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_department();
  1122. -- end trigger
  1123. --
  1124. CREATE FUNCTION del_customer() RETURNS TRIGGER AS '
  1125. begin
  1126. delete from shipto where trans_id = old.id;
  1127. delete from customertax where customer_id = old.id;
  1128. delete from partscustomer where customer_id = old.id;
  1129. return NULL;
  1130. end;
  1131. ' language 'plpgsql';
  1132. -- end function
  1133. --
  1134. CREATE TRIGGER del_customer AFTER DELETE ON customer FOR EACH ROW EXECUTE PROCEDURE del_customer();
  1135. -- end trigger
  1136. --
  1137. CREATE FUNCTION del_vendor() RETURNS TRIGGER AS '
  1138. begin
  1139. delete from shipto where trans_id = old.id;
  1140. delete from vendortax where vendor_id = old.id;
  1141. delete from partsvendor where vendor_id = old.id;
  1142. return NULL;
  1143. end;
  1144. ' language 'plpgsql';
  1145. -- end function
  1146. --
  1147. CREATE TRIGGER del_vendor AFTER DELETE ON vendor FOR EACH ROW EXECUTE PROCEDURE del_vendor();
  1148. -- end trigger
  1149. --
  1150. CREATE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
  1151. declare
  1152. t_transdate date;
  1153. t_curr char(3);
  1154. t_id int;
  1155. d_curr text;
  1156. begin
  1157. select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
  1158. if TG_RELNAME = ''ar'' then
  1159. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  1160. end if;
  1161. if TG_RELNAME = ''ap'' then
  1162. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  1163. end if;
  1164. if TG_RELNAME = ''oe'' then
  1165. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  1166. end if;
  1167. if d_curr != t_curr then
  1168. select into t_id a.id from acc_trans ac
  1169. join ar a on (a.id = ac.trans_id)
  1170. where a.curr = t_curr
  1171. and ac.transdate = t_transdate
  1172. except select a.id from ar a where a.id = old.id
  1173. union
  1174. select a.id from acc_trans ac
  1175. join ap a on (a.id = ac.trans_id)
  1176. where a.curr = t_curr
  1177. and ac.transdate = t_transdate
  1178. except select a.id from ap a where a.id = old.id
  1179. union
  1180. select o.id from oe o
  1181. where o.curr = t_curr
  1182. and o.transdate = t_transdate
  1183. except select o.id from oe o where o.id = old.id;
  1184. if not found then
  1185. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  1186. end if;
  1187. end if;
  1188. return old;
  1189. end;
  1190. ' language 'plpgsql';
  1191. -- end function
  1192. --
  1193. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1194. -- end trigger
  1195. --
  1196. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1197. -- end trigger
  1198. --
  1199. CREATE TRIGGER del_exchangerate BEFORE DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  1200. -- end trigger
  1201. --
  1202. CREATE FUNCTION check_inventory() RETURNS TRIGGER AS '
  1203. declare
  1204. itemid int;
  1205. row_data inventory%rowtype;
  1206. begin
  1207. if not old.quotation then
  1208. for row_data in select * from inventory where trans_id = old.id loop
  1209. select into itemid id from orderitems where trans_id = old.id and id = row_data.orderitems_id;
  1210. if itemid is null then
  1211. delete from inventory where trans_id = old.id and orderitems_id = row_data.orderitems_id;
  1212. end if;
  1213. end loop;
  1214. end if;
  1215. return old;
  1216. end;
  1217. ' language 'plpgsql';
  1218. -- end function
  1219. --
  1220. CREATE TRIGGER check_inventory AFTER UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_inventory();
  1221. -- end trigger
  1222. --
  1223. --
  1224. CREATE FUNCTION check_department() RETURNS TRIGGER AS '
  1225. declare
  1226. dpt_id int;
  1227. begin
  1228. if new.department_id = 0 then
  1229. delete from dpt_trans where trans_id = new.id;
  1230. return NULL;
  1231. end if;
  1232. select into dpt_id trans_id from dpt_trans where trans_id = new.id;
  1233. if dpt_id > 0 then
  1234. update dpt_trans set department_id = new.department_id where trans_id = dpt_id;
  1235. else
  1236. insert into dpt_trans (trans_id, department_id) values (new.id, new.department_id);
  1237. end if;
  1238. return NULL;
  1239. end;
  1240. ' language 'plpgsql';
  1241. -- end function
  1242. --
  1243. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ar FOR EACH ROW EXECUTE PROCEDURE check_department();
  1244. -- end trigger
  1245. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ap FOR EACH ROW EXECUTE PROCEDURE check_department();
  1246. -- end trigger
  1247. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON gl FOR EACH ROW EXECUTE PROCEDURE check_department();
  1248. -- end trigger
  1249. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_department();
  1250. -- end trigger
  1251. --
  1252. CREATE FUNCTION del_recurring() RETURNS TRIGGER AS '
  1253. BEGIN
  1254. DELETE FROM recurring WHERE id = old.id;
  1255. DELETE FROM recurringemail WHERE id = old.id;
  1256. DELETE FROM recurringprint WHERE id = old.id;
  1257. RETURN NULL;
  1258. END;
  1259. ' language 'plpgsql';
  1260. --end function
  1261. CREATE TRIGGER del_recurring AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1262. -- end trigger
  1263. CREATE TRIGGER del_recurring AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1264. -- end trigger
  1265. CREATE TRIGGER del_recurring AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  1266. -- end trigger
  1267. --
  1268. CREATE FUNCTION avgcost(int) RETURNS FLOAT AS '
  1269. DECLARE
  1270. v_cost float;
  1271. v_qty float;
  1272. v_parts_id alias for $1;
  1273. BEGIN
  1274. SELECT INTO v_cost, v_qty SUM(i.sellprice * i.qty), SUM(i.qty)
  1275. FROM invoice i
  1276. JOIN ap a ON (a.id = i.trans_id)
  1277. WHERE i.parts_id = v_parts_id;
  1278. IF v_cost IS NULL THEN
  1279. v_cost := 0;
  1280. END IF;
  1281. IF NOT v_qty IS NULL THEN
  1282. IF v_qty = 0 THEN
  1283. v_cost := 0;
  1284. ELSE
  1285. v_cost := v_cost/v_qty;
  1286. END IF;
  1287. END IF;
  1288. RETURN v_cost;
  1289. END;
  1290. ' language 'plpgsql';
  1291. -- end function
  1292. --
  1293. CREATE FUNCTION lastcost(int) RETURNS FLOAT AS '
  1294. DECLARE
  1295. v_cost float;
  1296. v_parts_id alias for $1;
  1297. BEGIN
  1298. SELECT INTO v_cost sellprice FROM invoice i
  1299. JOIN ap a ON (a.id = i.trans_id)
  1300. WHERE i.parts_id = v_parts_id
  1301. ORDER BY a.transdate desc, a.id desc
  1302. LIMIT 1;
  1303. IF v_cost IS NULL THEN
  1304. v_cost := 0;
  1305. END IF;
  1306. RETURN v_cost;
  1307. END;
  1308. ' language plpgsql;
  1309. -- end function
  1310. --
  1311. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  1312. AS
  1313. '
  1314. BEGIN
  1315. IF NEW.onhand >= NEW.rop THEN
  1316. NOTIFY parts_short;
  1317. END IF;
  1318. RETURN NEW;
  1319. END;
  1320. ' LANGUAGE PLPGSQL;
  1321. -- end function
  1322. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  1323. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  1324. -- end function
  1325. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  1326. RETURNS BOOL AS
  1327. '
  1328. DECLARE
  1329. table_name ALIAS FOR $1;
  1330. new_field_name ALIAS FOR $2;
  1331. field_datatype ALIAS FOR $3;
  1332. BEGIN
  1333. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  1334. WHERE extends = '''''' || table_name || '''''' '';
  1335. IF NOT FOUND THEN
  1336. BEGIN
  1337. INSERT INTO custom_table_catalog (extends)
  1338. VALUES (table_name);
  1339. EXECUTE ''CREATE TABLE custom_''||table_name ||
  1340. '' (row_id INT PRIMARY KEY)'';
  1341. EXCEPTION WHEN duplicate_table THEN
  1342. -- do nothing
  1343. END;
  1344. END IF;
  1345. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  1346. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  1347. WHERE extends = ''''''|| table_name || ''''''))'';
  1348. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  1349. || new_field_name || '' '' || field_datatype;
  1350. RETURN TRUE;
  1351. END;
  1352. ' LANGUAGE PLPGSQL;
  1353. -- end function
  1354. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  1355. RETURNS BOOL AS
  1356. '
  1357. DECLARE
  1358. table_name ALIAS FOR $1;
  1359. custom_field_name ALIAS FOR $2;
  1360. BEGIN
  1361. DELETE FROM custom_field_catalog
  1362. WHERE field_name = custom_field_name AND
  1363. table_id = (SELECT table_id FROM custom_table_catalog
  1364. WHERE extends = table_name);
  1365. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  1366. '' DROP COLUMN '' || custom_field_name;
  1367. RETURN TRUE;
  1368. END;
  1369. ' LANGUAGE PLPGSQL;
  1370. -- end function
  1371. commit;