summaryrefslogtreecommitdiff
path: root/sql/Pg-tables.sql
blob: 32dffb9ab40b18e34115b1af6005aec5e5fa4978 (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 taxmodule (
  246. taxmodule_id serial PRIMARY KEY,
  247. taxmodulename text NOT NULL
  248. );
  249. --
  250. CREATE TABLE tax (
  251. chart_id int PRIMARY KEY,
  252. rate numeric,
  253. taxnumber text,
  254. validto date,
  255. pass integer DEFAULT 0 NOT NULL,
  256. taxmodule_id int DEFAULT 1 NOT NULL,
  257. FOREIGN KEY (chart_id) REFERENCES chart (id),
  258. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  259. );
  260. --
  261. CREATE TABLE customertax (
  262. customer_id int,
  263. chart_id int,
  264. PRIMARY KEY (customer_id, chart_id)
  265. );
  266. --
  267. CREATE TABLE vendortax (
  268. vendor_id int,
  269. chart_id int,
  270. PRIMARY KEY (vendor_id, chart_id)
  271. );
  272. --
  273. CREATE TABLE oe (
  274. id int default nextval('id') PRIMARY KEY,
  275. ordnumber text,
  276. transdate date default current_date,
  277. vendor_id int,
  278. customer_id int,
  279. amount NUMERIC,
  280. netamount NUMERIC,
  281. reqdate date,
  282. taxincluded bool,
  283. shippingpoint text,
  284. notes text,
  285. curr char(3),
  286. employee_id int,
  287. closed bool default 'f',
  288. quotation bool default 'f',
  289. quonumber text,
  290. intnotes text,
  291. department_id int default 0,
  292. shipvia text,
  293. language_code varchar(6),
  294. ponumber text,
  295. terms int2 DEFAULT 0
  296. );
  297. --
  298. CREATE TABLE orderitems (
  299. id int default nextval('orderitemsid') PRIMARY KEY,
  300. trans_id int,
  301. parts_id int,
  302. description text,
  303. qty numeric,
  304. sellprice NUMERIC,
  305. discount numeric,
  306. unit varchar(5),
  307. project_id int,
  308. reqdate date,
  309. ship numeric,
  310. serialnumber text,
  311. notes text
  312. );
  313. --
  314. CREATE TABLE exchangerate (
  315. curr char(3),
  316. transdate date,
  317. buy numeric,
  318. sell numeric,
  319. PRIMARY KEY (curr, transdate)
  320. );
  321. --
  322. create table employee (
  323. id int default nextval('id') PRIMARY KEY,
  324. login text,
  325. name varchar(64),
  326. address1 varchar(32),
  327. address2 varchar(32),
  328. city varchar(32),
  329. state varchar(32),
  330. zipcode varchar(10),
  331. country varchar(32),
  332. workphone varchar(20),
  333. homephone varchar(20),
  334. startdate date default current_date,
  335. enddate date,
  336. notes text,
  337. role varchar(20),
  338. sales bool default 'f',
  339. email text,
  340. ssn varchar(20),
  341. iban varchar(34),
  342. bic varchar(11),
  343. managerid int,
  344. employeenumber varchar(32),
  345. dob date
  346. );
  347. --
  348. create table shipto (
  349. trans_id int,
  350. shiptoname varchar(64),
  351. shiptoaddress1 varchar(32),
  352. shiptoaddress2 varchar(32),
  353. shiptocity varchar(32),
  354. shiptostate varchar(32),
  355. shiptozipcode varchar(10),
  356. shiptocountry varchar(32),
  357. shiptocontact varchar(64),
  358. shiptophone varchar(20),
  359. shiptofax varchar(20),
  360. shiptoemail text,
  361. entry_id SERIAL PRIMARY KEY
  362. );
  363. --
  364. CREATE TABLE vendor (
  365. id int default nextval('id') PRIMARY KEY,
  366. name varchar(64),
  367. address1 varchar(32),
  368. address2 varchar(32),
  369. city varchar(32),
  370. state varchar(32),
  371. zipcode varchar(10),
  372. country varchar(32),
  373. contact varchar(64),
  374. phone varchar(20),
  375. fax varchar(20),
  376. email text,
  377. notes text,
  378. terms int2 default 0,
  379. taxincluded bool default 'f',
  380. vendornumber varchar(32),
  381. cc text,
  382. bcc text,
  383. gifi_accno varchar(30),
  384. business_id int,
  385. taxnumber varchar(32),
  386. sic_code varchar(6),
  387. discount numeric,
  388. creditlimit numeric default 0,
  389. iban varchar(34),
  390. bic varchar(11),
  391. employee_id int,
  392. language_code varchar(6),
  393. pricegroup_id int,
  394. curr char(3),
  395. startdate date,
  396. enddate date
  397. );
  398. --
  399. CREATE TABLE project (
  400. id int default nextval('id') PRIMARY KEY,
  401. projectnumber text,
  402. description text,
  403. startdate date,
  404. enddate date,
  405. parts_id int,
  406. production numeric default 0,
  407. completed numeric default 0,
  408. customer_id int
  409. );
  410. --
  411. CREATE TABLE partsgroup (
  412. id int default nextval('id') PRIMARY KEY,
  413. partsgroup text
  414. );
  415. --
  416. CREATE TABLE status (
  417. trans_id int PRIMARY KEY,
  418. formname text,
  419. printed bool default 'f',
  420. emailed bool default 'f',
  421. spoolfile text
  422. );
  423. --
  424. CREATE TABLE department (
  425. id int default nextval('id') PRIMARY KEY,
  426. description text,
  427. role char(1) default 'P'
  428. );
  429. --
  430. -- department transaction table
  431. CREATE TABLE dpt_trans (
  432. trans_id int PRIMARY KEY,
  433. department_id int
  434. );
  435. --
  436. -- business table
  437. CREATE TABLE business (
  438. id int default nextval('id') PRIMARY KEY,
  439. description text,
  440. discount numeric
  441. );
  442. --
  443. -- SIC
  444. CREATE TABLE sic (
  445. code varchar(6) PRIMARY KEY,
  446. sictype char(1),
  447. description text
  448. );
  449. --
  450. CREATE TABLE warehouse (
  451. id int default nextval('id') PRIMARY KEY,
  452. description text
  453. );
  454. --
  455. CREATE TABLE inventory (
  456. warehouse_id int,
  457. parts_id int,
  458. trans_id int,
  459. orderitems_id int,
  460. qty numeric,
  461. shippingdate date,
  462. employee_id int,
  463. entry_id SERIAL PRIMARY KEY
  464. );
  465. --
  466. CREATE TABLE yearend (
  467. trans_id int PRIMARY KEY,
  468. transdate date
  469. );
  470. --
  471. CREATE TABLE partsvendor (
  472. vendor_id int,
  473. parts_id int,
  474. partnumber text,
  475. leadtime int2,
  476. lastcost NUMERIC,
  477. curr char(3),
  478. entry_id SERIAL PRIMARY KEY
  479. );
  480. --
  481. CREATE TABLE pricegroup (
  482. id int default nextval('id') PRIMARY KEY,
  483. pricegroup text
  484. );
  485. --
  486. CREATE TABLE partscustomer (
  487. parts_id int,
  488. customer_id int,
  489. pricegroup_id int,
  490. pricebreak numeric,
  491. sellprice NUMERIC,
  492. validfrom date,
  493. validto date,
  494. curr char(3),
  495. entry_id SERIAL PRIMARY KEY
  496. );
  497. --
  498. CREATE TABLE language (
  499. code varchar(6) PRIMARY KEY,
  500. description text
  501. );
  502. --
  503. CREATE TABLE audittrail (
  504. trans_id int,
  505. tablename text,
  506. reference text,
  507. formname text,
  508. action text,
  509. transdate timestamp default current_timestamp,
  510. employee_id int,
  511. entry_id BIGSERIAL PRIMARY KEY
  512. );
  513. --
  514. CREATE TABLE translation (
  515. trans_id int,
  516. language_code varchar(6),
  517. description text,
  518. PRIMARY KEY (trans_id, language_code)
  519. );
  520. --
  521. CREATE TABLE recurring (
  522. id int PRIMARY KEY,
  523. reference text,
  524. startdate date,
  525. nextdate date,
  526. enddate date,
  527. repeat int2,
  528. unit varchar(6),
  529. howmany int,
  530. payment bool default 'f'
  531. );
  532. --
  533. CREATE TABLE recurringemail (
  534. id int PRIMARY KEY,
  535. formname text,
  536. format text,
  537. message text
  538. );
  539. --
  540. CREATE TABLE recurringprint (
  541. id int PRIMARY KEY,
  542. formname text,
  543. format text,
  544. printer text
  545. );
  546. --
  547. CREATE TABLE jcitems (
  548. id int default nextval('jcitemsid') PRIMARY KEY,
  549. project_id int,
  550. parts_id int,
  551. description text,
  552. qty numeric,
  553. allocated numeric,
  554. sellprice NUMERIC,
  555. fxsellprice NUMERIC,
  556. serialnumber text,
  557. checkedin timestamp with time zone,
  558. checkedout timestamp with time zone,
  559. employee_id int,
  560. notes text
  561. );
  562. -- Session tracking table
  563. CREATE SEQUENCE session_session_id_seq;
  564. CREATE TABLE session(
  565. session_id INTEGER PRIMARY KEY DEFAULT nextval('session_session_id_seq'),
  566. sl_login VARCHAR(50),
  567. token CHAR(32),
  568. last_used TIMESTAMP default now()
  569. );
  570. insert into transactions (id, table_name) SELECT id, 'ap' FROM ap;
  571. CREATE RULE ap_id_track_i AS ON insert TO ap
  572. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ap');
  573. CREATE RULE ap_id_track_u AS ON update TO ap
  574. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  575. insert into transactions (id, table_name) SELECT id, 'ar' FROM ap;
  576. CREATE RULE ar_id_track_i AS ON insert TO ar
  577. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ar');
  578. CREATE RULE ar_id_track_u AS ON update TO ar
  579. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  580. INSERT INTO transactions (id, table_name) SELECT id, 'business' FROM business;
  581. CREATE RULE business_id_track_i AS ON insert TO business
  582. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'business');
  583. CREATE RULE business_id_track_u AS ON update TO business
  584. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  585. INSERT INTO transactions (id, table_name) SELECT id, 'chart' FROM chart;
  586. CREATE RULE chart_id_track_i AS ON insert TO chart
  587. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'chart');
  588. CREATE RULE chart_id_track_u AS ON update TO chart
  589. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  590. INSERT INTO transactions (id, table_name) SELECT id, 'customer' FROM customer;
  591. CREATE RULE customer_id_track_i AS ON insert TO customer
  592. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'customer');
  593. CREATE RULE customer_id_track_u AS ON update TO customer
  594. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  595. INSERT INTO transactions (id, table_name) SELECT id, 'department' FROM department;
  596. CREATE RULE department_id_track_i AS ON insert TO department
  597. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'department');
  598. CREATE RULE department_id_track_u AS ON update TO department
  599. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  600. INSERT INTO transactions (id, table_name) SELECT id, 'employee' FROM employee;
  601. CREATE RULE employee_id_track_i AS ON insert TO employee
  602. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  603. CREATE RULE employee_id_track_u AS ON update TO employee
  604. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  605. INSERT INTO transactions (id, table_name) SELECT id, 'gl' FROM gl;
  606. CREATE RULE gl_id_track_i AS ON insert TO gl
  607. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'gl');
  608. CREATE RULE gl_id_track_u AS ON update TO gl
  609. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  610. INSERT INTO transactions (id, table_name) SELECT id, 'oe' FROM oe;
  611. CREATE RULE oe_id_track_i AS ON insert TO oe
  612. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'oe');
  613. CREATE RULE oe_id_track_u AS ON update TO oe
  614. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  615. INSERT INTO transactions (id, table_name) SELECT id, 'parts' FROM parts;
  616. CREATE RULE parts_id_track_i AS ON insert TO parts
  617. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'parts');
  618. CREATE RULE parts_id_track_u AS ON update TO parts
  619. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  620. INSERT INTO transactions (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  621. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  622. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'partsgroup');
  623. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  624. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  625. INSERT INTO transactions (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  626. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  627. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'pricegroup');
  628. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  629. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  630. INSERT INTO transactions (id, table_name) SELECT id, 'project' FROM project;
  631. CREATE RULE project_id_track_i AS ON insert TO project
  632. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'project');
  633. CREATE RULE project_id_track_u AS ON update TO project
  634. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  635. INSERT INTO transactions (id, table_name) SELECT id, 'vendor' FROM vendor;
  636. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  637. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'vendor');
  638. CREATE RULE employee_id_track_u AS ON update TO vendor
  639. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  640. INSERT INTO transactions (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  641. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  642. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  643. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  644. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  645. CREATE TABLE custom_table_catalog (
  646. table_id SERIAL PRIMARY KEY,
  647. extends TEXT,
  648. table_name TEXT
  649. );
  650. CREATE TABLE custom_field_catalog (
  651. field_id SERIAL PRIMARY KEY,
  652. table_id INT REFERENCES custom_table_catalog,
  653. field_name TEXT
  654. );
  655. INSERT INTO defaults (version) VALUES ('2.6.18');
  656. INSERT INTO taxmodule (
  657. taxmodule_id, taxmodulename
  658. ) VALUES (
  659. 1, 'Simple'
  660. );