summaryrefslogtreecommitdiff
path: root/sql/Pg-tables.sql
blob: 42471a1525e85d1a80df3cce425b47507aa768dd (plain)
  1. --
  2. CREATE SEQUENCE id start 10000;
  3. SELECT nextval ('id');
  4. --
  5. CREATE SEQUENCE invoiceid;
  6. SELECT nextval ('invoiceid');
  7. --
  8. CREATE SEQUENCE orderitemsid;
  9. SELECT nextval ('orderitemsid');
  10. --
  11. CREATE SEQUENCE jcitemsid;
  12. SELECT nextval ('jcitemsid');
  13. --
  14. create table transactions (
  15. id int PRIMARY KEY,
  16. table_name text
  17. );
  18. --
  19. CREATE TABLE makemodel (
  20. parts_id int PRIMARY KEY,
  21. make text,
  22. model text
  23. );
  24. --
  25. CREATE TABLE gl (
  26. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  27. reference text,
  28. description text,
  29. transdate date DEFAULT current_date,
  30. employee_id int,
  31. notes text,
  32. department_id int default 0
  33. );
  34. --
  35. CREATE TABLE chart (
  36. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  37. accno text NOT NULL,
  38. description text,
  39. charttype char(1) DEFAULT 'A',
  40. category char(1),
  41. link text,
  42. gifi_accno text,
  43. contra bool DEFAULT 'f'
  44. );
  45. --
  46. CREATE TABLE gifi (
  47. accno text PRIMARY KEY,
  48. description text
  49. );
  50. --
  51. CREATE TABLE defaults (
  52. inventory_accno_id int,
  53. income_accno_id int,
  54. expense_accno_id int,
  55. fxgain_accno_id int,
  56. fxloss_accno_id int,
  57. sinumber text,
  58. sonumber text,
  59. yearend varchar(5),
  60. weightunit varchar(5),
  61. businessnumber text,
  62. version varchar(8) PRIMARY KEY,
  63. curr text,
  64. closedto date,
  65. revtrans bool DEFAULT 't',
  66. ponumber text,
  67. sqnumber text,
  68. rfqnumber text,
  69. audittrail bool default 'f',
  70. vinumber text,
  71. employeenumber text,
  72. partnumber text,
  73. customernumber text,
  74. vendornumber text,
  75. glnumber text,
  76. projectnumber text
  77. );
  78. --
  79. CREATE TABLE acc_trans (
  80. trans_id int REFERENCES transactions(id),
  81. chart_id int NOT NULL REFERENCES chart (id),
  82. amount NUMERIC,
  83. transdate date DEFAULT current_date,
  84. source text,
  85. cleared bool DEFAULT 'f',
  86. fx_transaction bool DEFAULT 'f',
  87. project_id int,
  88. memo text,
  89. invoice_id int,
  90. entry_id SERIAL PRIMARY KEY
  91. );
  92. --
  93. CREATE TABLE invoice (
  94. id int DEFAULT nextval ( 'invoiceid' ) PRIMARY KEY,
  95. trans_id int,
  96. parts_id int,
  97. description text,
  98. qty integer,
  99. allocated integer,
  100. sellprice NUMERIC,
  101. fxsellprice NUMERIC,
  102. discount float4, -- jd: check into this
  103. assemblyitem bool DEFAULT 'f',
  104. unit varchar(5),
  105. project_id int,
  106. deliverydate date,
  107. serialnumber text,
  108. notes text
  109. );
  110. --
  111. CREATE TABLE customer (
  112. id int default nextval('id') PRIMARY KEY,
  113. name varchar(64),
  114. address1 varchar(32),
  115. address2 varchar(32),
  116. city varchar(32),
  117. state varchar(32),
  118. zipcode varchar(10),
  119. country varchar(32),
  120. contact varchar(64),
  121. phone varchar(20),
  122. fax varchar(20),
  123. email text,
  124. notes text,
  125. discount numeric,
  126. taxincluded bool default 'f',
  127. creditlimit NUMERIC default 0,
  128. terms int2 default 0,
  129. customernumber varchar(32),
  130. cc text,
  131. bcc text,
  132. business_id int,
  133. taxnumber varchar(32),
  134. sic_code varchar(6),
  135. iban varchar(34),
  136. bic varchar(11),
  137. employee_id int,
  138. language_code varchar(6),
  139. pricegroup_id int,
  140. curr char(3),
  141. startdate date,
  142. enddate date
  143. );
  144. --
  145. --
  146. CREATE TABLE parts (
  147. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  148. partnumber text,
  149. description text,
  150. unit varchar(5),
  151. listprice NUMERIC,
  152. sellprice NUMERIC,
  153. lastcost NUMERIC,
  154. priceupdate date DEFAULT current_date,
  155. weight numeric,
  156. onhand numeric DEFAULT 0,
  157. notes text,
  158. makemodel bool DEFAULT 'f',
  159. assembly bool DEFAULT 'f',
  160. alternate bool DEFAULT 'f',
  161. rop float4, -- jd: what is this
  162. inventory_accno_id int,
  163. income_accno_id int,
  164. expense_accno_id int,
  165. bin text,
  166. obsolete bool DEFAULT 'f',
  167. bom bool DEFAULT 'f',
  168. image text,
  169. drawing text,
  170. microfiche text,
  171. partsgroup_id int,
  172. project_id int,
  173. avgcost NUMERIC
  174. );
  175. --
  176. CREATE TABLE assembly (
  177. id int,
  178. parts_id int,
  179. qty numeric,
  180. bom bool,
  181. adj bool,
  182. PRIMARY KEY (id, parts_id)
  183. );
  184. --
  185. CREATE TABLE ar (
  186. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  187. invnumber text,
  188. transdate date DEFAULT current_date,
  189. customer_id int,
  190. taxincluded bool,
  191. amount NUMERIC,
  192. netamount NUMERIC,
  193. paid NUMERIC,
  194. datepaid date,
  195. duedate date,
  196. invoice bool DEFAULT 'f',
  197. shippingpoint text,
  198. terms int2 DEFAULT 0,
  199. notes text,
  200. curr char(3),
  201. ordnumber text,
  202. employee_id int,
  203. till varchar(20),
  204. quonumber text,
  205. intnotes text,
  206. department_id int default 0,
  207. shipvia text,
  208. language_code varchar(6),
  209. ponumber text
  210. );
  211. --
  212. CREATE TABLE ap (
  213. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  214. invnumber text,
  215. transdate date DEFAULT current_date,
  216. vendor_id int,
  217. taxincluded bool DEFAULT 'f',
  218. amount NUMERIC,
  219. netamount NUMERIC,
  220. paid NUMERIC,
  221. datepaid date,
  222. duedate date,
  223. invoice bool DEFAULT 'f',
  224. ordnumber text,
  225. curr char(3),
  226. notes text,
  227. employee_id int,
  228. till varchar(20),
  229. quonumber text,
  230. intnotes text,
  231. department_id int DEFAULT 0,
  232. shipvia text,
  233. language_code varchar(6),
  234. ponumber text,
  235. shippingpoint text,
  236. terms int2 DEFAULT 0
  237. );
  238. --
  239. CREATE TABLE partstax (
  240. parts_id int,
  241. chart_id int,
  242. PRIMARY KEY (parts_id, chart_id)
  243. );
  244. --
  245. CREATE TABLE tax (
  246. chart_id int PRIMARY KEY,
  247. rate numeric,
  248. taxnumber text,
  249. validto date,
  250. FOREIGN KEY (chart_id) REFERENCES chart (id)
  251. );
  252. --
  253. CREATE TABLE customertax (
  254. customer_id int,
  255. chart_id int,
  256. PRIMARY KEY (customer_id, chart_id)
  257. );
  258. --
  259. CREATE TABLE vendortax (
  260. vendor_id int,
  261. chart_id int,
  262. PRIMARY KEY (vendor_id, chart_id)
  263. );
  264. --
  265. CREATE TABLE oe (
  266. id int default nextval('id') PRIMARY KEY,
  267. ordnumber text,
  268. transdate date default current_date,
  269. vendor_id int,
  270. customer_id int,
  271. amount NUMERIC,
  272. netamount NUMERIC,
  273. reqdate date,
  274. taxincluded bool,
  275. shippingpoint text,
  276. notes text,
  277. curr char(3),
  278. employee_id int,
  279. closed bool default 'f',
  280. quotation bool default 'f',
  281. quonumber text,
  282. intnotes text,
  283. department_id int default 0,
  284. shipvia text,
  285. language_code varchar(6),
  286. ponumber text,
  287. terms int2 DEFAULT 0
  288. );
  289. --
  290. CREATE TABLE orderitems (
  291. id int default nextval('orderitemsid') PRIMARY KEY,
  292. trans_id int,
  293. parts_id int,
  294. description text,
  295. qty numeric,
  296. sellprice NUMERIC,
  297. discount numeric,
  298. unit varchar(5),
  299. project_id int,
  300. reqdate date,
  301. ship numeric,
  302. serialnumber text,
  303. notes text
  304. );
  305. --
  306. CREATE TABLE exchangerate (
  307. curr char(3),
  308. transdate date,
  309. buy numeric,
  310. sell numeric,
  311. PRIMARY KEY (curr, transdate)
  312. );
  313. --
  314. create table employee (
  315. id int default nextval('id') PRIMARY KEY,
  316. login text,
  317. name varchar(64),
  318. address1 varchar(32),
  319. address2 varchar(32),
  320. city varchar(32),
  321. state varchar(32),
  322. zipcode varchar(10),
  323. country varchar(32),
  324. workphone varchar(20),
  325. homephone varchar(20),
  326. startdate date default current_date,
  327. enddate date,
  328. notes text,
  329. role varchar(20),
  330. sales bool default 'f',
  331. email text,
  332. ssn varchar(20),
  333. iban varchar(34),
  334. bic varchar(11),
  335. managerid int,
  336. employeenumber varchar(32),
  337. dob date
  338. );
  339. --
  340. create table shipto (
  341. trans_id int,
  342. shiptoname varchar(64),
  343. shiptoaddress1 varchar(32),
  344. shiptoaddress2 varchar(32),
  345. shiptocity varchar(32),
  346. shiptostate varchar(32),
  347. shiptozipcode varchar(10),
  348. shiptocountry varchar(32),
  349. shiptocontact varchar(64),
  350. shiptophone varchar(20),
  351. shiptofax varchar(20),
  352. shiptoemail text,
  353. entry_id SERIAL PRIMARY KEY
  354. );
  355. --
  356. CREATE TABLE vendor (
  357. id int default nextval('id') PRIMARY KEY,
  358. name varchar(64),
  359. address1 varchar(32),
  360. address2 varchar(32),
  361. city varchar(32),
  362. state varchar(32),
  363. zipcode varchar(10),
  364. country varchar(32),
  365. contact varchar(64),
  366. phone varchar(20),
  367. fax varchar(20),
  368. email text,
  369. notes text,
  370. terms int2 default 0,
  371. taxincluded bool default 'f',
  372. vendornumber varchar(32),
  373. cc text,
  374. bcc text,
  375. gifi_accno varchar(30),
  376. business_id int,
  377. taxnumber varchar(32),
  378. sic_code varchar(6),
  379. discount numeric,
  380. creditlimit numeric default 0,
  381. iban varchar(34),
  382. bic varchar(11),
  383. employee_id int,
  384. language_code varchar(6),
  385. pricegroup_id int,
  386. curr char(3),
  387. startdate date,
  388. enddate date
  389. );
  390. --
  391. CREATE TABLE project (
  392. id int default nextval('id') PRIMARY KEY,
  393. projectnumber text,
  394. description text,
  395. startdate date,
  396. enddate date,
  397. parts_id int,
  398. production numeric default 0,
  399. completed numeric default 0,
  400. customer_id int
  401. );
  402. --
  403. CREATE TABLE partsgroup (
  404. id int default nextval('id') PRIMARY KEY,
  405. partsgroup text
  406. );
  407. --
  408. CREATE TABLE status (
  409. trans_id int PRIMARY KEY,
  410. formname text,
  411. printed bool default 'f',
  412. emailed bool default 'f',
  413. spoolfile text
  414. );
  415. --
  416. CREATE TABLE department (
  417. id int default nextval('id') PRIMARY KEY,
  418. description text,
  419. role char(1) default 'P'
  420. );
  421. --
  422. -- department transaction table
  423. CREATE TABLE dpt_trans (
  424. trans_id int PRIMARY KEY,
  425. department_id int
  426. );
  427. --
  428. -- business table
  429. CREATE TABLE business (
  430. id int default nextval('id') PRIMARY KEY,
  431. description text,
  432. discount numeric
  433. );
  434. --
  435. -- SIC
  436. CREATE TABLE sic (
  437. code varchar(6) PRIMARY KEY,
  438. sictype char(1),
  439. description text
  440. );
  441. --
  442. CREATE TABLE warehouse (
  443. id int default nextval('id') PRIMARY KEY,
  444. description text
  445. );
  446. --
  447. CREATE TABLE inventory (
  448. warehouse_id int,
  449. parts_id int,
  450. trans_id int,
  451. orderitems_id int,
  452. qty numeric,
  453. shippingdate date,
  454. employee_id int,
  455. entry_id SERIAL PRIMARY KEY
  456. );
  457. --
  458. CREATE TABLE yearend (
  459. trans_id int PRIMARY KEY,
  460. transdate date
  461. );
  462. --
  463. CREATE TABLE partsvendor (
  464. vendor_id int,
  465. parts_id int,
  466. partnumber text,
  467. leadtime int2,
  468. lastcost NUMERIC,
  469. curr char(3),
  470. entry_id SERIAL PRIMARY KEY
  471. );
  472. --
  473. CREATE TABLE pricegroup (
  474. id int default nextval('id') PRIMARY KEY,
  475. pricegroup text
  476. );
  477. --
  478. CREATE TABLE partscustomer (
  479. parts_id int,
  480. customer_id int,
  481. pricegroup_id int,
  482. pricebreak numeric,
  483. sellprice NUMERIC,
  484. validfrom date,
  485. validto date,
  486. curr char(3),
  487. entry_id SERIAL PRIMARY KEY
  488. );
  489. --
  490. CREATE TABLE language (
  491. code varchar(6) PRIMARY KEY,
  492. description text
  493. );
  494. --
  495. CREATE TABLE audittrail (
  496. trans_id int,
  497. tablename text,
  498. reference text,
  499. formname text,
  500. action text,
  501. transdate timestamp default current_timestamp,
  502. employee_id int,
  503. entry_id BIGSERIAL PRIMARY KEY
  504. );
  505. --
  506. CREATE TABLE translation (
  507. trans_id int,
  508. language_code varchar(6),
  509. description text,
  510. PRIMARY KEY (trans_id, language_code)
  511. );
  512. --
  513. CREATE TABLE recurring (
  514. id int PRIMARY KEY,
  515. reference text,
  516. startdate date,
  517. nextdate date,
  518. enddate date,
  519. repeat int2,
  520. unit varchar(6),
  521. howmany int,
  522. payment bool default 'f'
  523. );
  524. --
  525. CREATE TABLE recurringemail (
  526. id int PRIMARY KEY,
  527. formname text,
  528. format text,
  529. message text
  530. );
  531. --
  532. CREATE TABLE recurringprint (
  533. id int PRIMARY KEY,
  534. formname text,
  535. format text,
  536. printer text
  537. );
  538. --
  539. CREATE TABLE jcitems (
  540. id int default nextval('jcitemsid') PRIMARY KEY,
  541. project_id int,
  542. parts_id int,
  543. description text,
  544. qty numeric,
  545. allocated numeric,
  546. sellprice NUMERIC,
  547. fxsellprice NUMERIC,
  548. serialnumber text,
  549. checkedin timestamp with time zone,
  550. checkedout timestamp with time zone,
  551. employee_id int,
  552. notes text
  553. );
  554. -- Session tracking table
  555. CREATE SEQUENCE session_session_id_seq;
  556. CREATE TABLE session(
  557. session_id INTEGER PRIMARY KEY DEFAULT nextval('session_session_id_seq'),
  558. sl_login VARCHAR(50),
  559. token CHAR(32),
  560. last_used TIMESTAMP default now()
  561. );
  562. insert into transactions (id, table_name) SELECT id, 'ap' FROM ap;
  563. CREATE RULE ap_id_track_i AS ON insert TO ap
  564. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ap');
  565. CREATE RULE ap_id_track_u AS ON update TO ap
  566. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  567. insert into transactions (id, table_name) SELECT id, 'ar' FROM ap;
  568. CREATE RULE ar_id_track_i AS ON insert TO ar
  569. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ar');
  570. CREATE RULE ar_id_track_u AS ON update TO ar
  571. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  572. INSERT INTO transactions (id, table_name) SELECT id, 'business' FROM business;
  573. CREATE RULE business_id_track_i AS ON insert TO business
  574. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'business');
  575. CREATE RULE business_id_track_u AS ON update TO business
  576. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  577. INSERT INTO transactions (id, table_name) SELECT id, 'chart' FROM chart;
  578. CREATE RULE chart_id_track_i AS ON insert TO chart
  579. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'chart');
  580. CREATE RULE chart_id_track_u AS ON update TO chart
  581. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  582. INSERT INTO transactions (id, table_name) SELECT id, 'customer' FROM customer;
  583. CREATE RULE customer_id_track_i AS ON insert TO customer
  584. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'customer');
  585. CREATE RULE customer_id_track_u AS ON update TO customer
  586. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  587. INSERT INTO transactions (id, table_name) SELECT id, 'department' FROM department;
  588. CREATE RULE department_id_track_i AS ON insert TO department
  589. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'department');
  590. CREATE RULE department_id_track_u AS ON update TO department
  591. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  592. INSERT INTO transactions (id, table_name) SELECT id, 'employee' FROM employee;
  593. CREATE RULE employee_id_track_i AS ON insert TO employee
  594. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  595. CREATE RULE employee_id_track_u AS ON update TO employee
  596. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  597. INSERT INTO transactions (id, table_name) SELECT id, 'gl' FROM gl;
  598. CREATE RULE gl_id_track_i AS ON insert TO gl
  599. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'gl');
  600. CREATE RULE gl_id_track_u AS ON update TO gl
  601. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  602. INSERT INTO transactions (id, table_name) SELECT id, 'oe' FROM oe;
  603. CREATE RULE oe_id_track_i AS ON insert TO oe
  604. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'oe');
  605. CREATE RULE oe_id_track_u AS ON update TO oe
  606. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  607. INSERT INTO transactions (id, table_name) SELECT id, 'parts' FROM parts;
  608. CREATE RULE parts_id_track_i AS ON insert TO parts
  609. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'parts');
  610. CREATE RULE parts_id_track_u AS ON update TO parts
  611. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  612. INSERT INTO transactions (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  613. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  614. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'partsgroup');
  615. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  616. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  617. INSERT INTO transactions (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  618. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  619. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'pricegroup');
  620. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  621. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  622. INSERT INTO transactions (id, table_name) SELECT id, 'project' FROM project;
  623. CREATE RULE project_id_track_i AS ON insert TO project
  624. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'project');
  625. CREATE RULE project_id_track_u AS ON update TO project
  626. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  627. INSERT INTO transactions (id, table_name) SELECT id, 'vendor' FROM vendor;
  628. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  629. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'vendor');
  630. CREATE RULE employee_id_track_u AS ON update TO vendor
  631. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  632. INSERT INTO transactions (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  633. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  634. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  635. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  636. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  637. CREATE TABLE custom_table_catalog (
  638. table_id SERIAL PRIMARY KEY,
  639. extends TEXT,
  640. table_name TEXT
  641. );
  642. CREATE TABLE custom_field_catalog (
  643. field_id SERIAL PRIMARY KEY,
  644. table_id INT REFERENCES custom_table_catalog,
  645. field_name TEXT
  646. );
  647. INSERT INTO defaults (version) VALUES ('2.6.18');