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