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