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