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