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