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