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