summaryrefslogtreecommitdiff
path: root/sql/Pg-database.sql
blob: 525dc405bf547ead2e73a30edbf783247c75cb4e (plain)
  1. begin;
  2. --
  3. CREATE SEQUENCE id start 10000;
  4. SELECT nextval ('id');
  5. --
  6. CREATE SEQUENCE invoiceid;
  7. SELECT nextval ('invoiceid');
  8. --
  9. CREATE SEQUENCE orderitemsid;
  10. SELECT nextval ('orderitemsid');
  11. --
  12. CREATE SEQUENCE jcitemsid;
  13. SELECT nextval ('jcitemsid');
  14. --
  15. CREATE TABLE transactions (
  16. id int PRIMARY KEY,
  17. table_name text
  18. );
  19. --
  20. CREATE TABLE makemodel (
  21. parts_id int PRIMARY KEY,
  22. make text,
  23. model text
  24. );
  25. --
  26. CREATE TABLE gl (
  27. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  28. reference text,
  29. description text,
  30. transdate date DEFAULT current_date,
  31. employee_id int,
  32. notes text,
  33. department_id int default 0
  34. );
  35. --
  36. CREATE TABLE chart (
  37. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  38. accno text NOT NULL,
  39. description text,
  40. charttype char(1) DEFAULT 'A',
  41. category char(1),
  42. link text,
  43. gifi_accno text,
  44. contra bool DEFAULT 'f'
  45. );
  46. --
  47. CREATE TABLE gifi (
  48. accno text PRIMARY KEY,
  49. description text
  50. );
  51. --
  52. CREATE TABLE defaults (
  53. setting_key text primary key,
  54. value text
  55. );
  56. /*
  57. inventory_accno_id int,
  58. income_accno_id int,
  59. expense_accno_id int,
  60. fxgain_accno_id int,
  61. fxloss_accno_id int,
  62. */
  63. \COPY defaults FROM stdin WITH DELIMITER |
  64. sinumber|1
  65. sonumber|1
  66. yearend|1
  67. businessnumber|1
  68. version|1.2.0
  69. closedto|\N
  70. revtrans|1
  71. ponumber|1
  72. sqnumber|1
  73. rfqnumber|1
  74. audittrail|0
  75. vinumber|1
  76. employeenumber|1
  77. partnumber|1
  78. customernumber|1
  79. vendornumber|1
  80. glnumber|1
  81. projectnumber|1
  82. \.
  83. -- */
  84. CREATE TABLE acc_trans (
  85. trans_id int,
  86. chart_id int NOT NULL REFERENCES chart (id),
  87. amount NUMERIC,
  88. transdate date DEFAULT current_date,
  89. source text,
  90. cleared bool DEFAULT 'f',
  91. fx_transaction bool DEFAULT 'f',
  92. project_id int,
  93. memo text,
  94. invoice_id int,
  95. entry_id SERIAL PRIMARY KEY
  96. );
  97. --
  98. CREATE TABLE invoice (
  99. id int DEFAULT nextval ( 'invoiceid' ) PRIMARY KEY,
  100. trans_id int,
  101. parts_id int,
  102. description text,
  103. qty NUMERIC,
  104. allocated NUMERIC,
  105. sellprice NUMERIC,
  106. fxsellprice NUMERIC,
  107. discount float4, -- jd: check into this
  108. assemblyitem bool DEFAULT 'f',
  109. unit varchar(5),
  110. project_id int,
  111. deliverydate date,
  112. serialnumber text,
  113. notes text
  114. );
  115. --
  116. CREATE TABLE customer (
  117. id int default nextval('id') PRIMARY KEY,
  118. name varchar(64),
  119. address1 varchar(32),
  120. address2 varchar(32),
  121. city varchar(32),
  122. state varchar(32),
  123. zipcode varchar(10),
  124. country varchar(32),
  125. contact varchar(64),
  126. phone varchar(20),
  127. fax varchar(20),
  128. email text,
  129. notes text,
  130. discount numeric,
  131. taxincluded bool default 'f',
  132. creditlimit NUMERIC default 0,
  133. terms int2 default 0,
  134. customernumber varchar(32),
  135. cc text,
  136. bcc text,
  137. business_id int,
  138. taxnumber varchar(32),
  139. sic_code varchar(6),
  140. iban varchar(34),
  141. bic varchar(11),
  142. employee_id int,
  143. language_code varchar(6),
  144. pricegroup_id int,
  145. curr char(3),
  146. startdate date,
  147. enddate date
  148. );
  149. --
  150. --
  151. CREATE TABLE parts (
  152. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  153. partnumber text,
  154. description text,
  155. unit varchar(5),
  156. listprice NUMERIC,
  157. sellprice NUMERIC,
  158. lastcost NUMERIC,
  159. priceupdate date DEFAULT current_date,
  160. weight numeric,
  161. onhand numeric DEFAULT 0,
  162. notes text,
  163. makemodel bool DEFAULT 'f',
  164. assembly bool DEFAULT 'f',
  165. alternate bool DEFAULT 'f',
  166. rop float4, -- jd: what is this
  167. inventory_accno_id int,
  168. income_accno_id int,
  169. expense_accno_id int,
  170. bin text,
  171. obsolete bool DEFAULT 'f',
  172. bom bool DEFAULT 'f',
  173. image text,
  174. drawing text,
  175. microfiche text,
  176. partsgroup_id int,
  177. project_id int,
  178. avgcost NUMERIC
  179. );
  180. --
  181. CREATE TABLE assembly (
  182. id int,
  183. parts_id int,
  184. qty numeric,
  185. bom bool,
  186. adj bool,
  187. PRIMARY KEY (id, parts_id)
  188. );
  189. --
  190. CREATE TABLE ar (
  191. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  192. invnumber text,
  193. transdate date DEFAULT current_date,
  194. customer_id int,
  195. taxincluded bool,
  196. amount NUMERIC,
  197. netamount NUMERIC,
  198. paid NUMERIC,
  199. datepaid date,
  200. duedate date,
  201. invoice bool DEFAULT 'f',
  202. shippingpoint text,
  203. terms int2 DEFAULT 0,
  204. notes text,
  205. curr char(3),
  206. ordnumber text,
  207. employee_id int,
  208. till varchar(20),
  209. quonumber text,
  210. intnotes text,
  211. department_id int default 0,
  212. shipvia text,
  213. language_code varchar(6),
  214. ponumber text
  215. );
  216. --
  217. CREATE TABLE ap (
  218. id int DEFAULT nextval ( 'id' ) PRIMARY KEY,
  219. invnumber text,
  220. transdate date DEFAULT current_date,
  221. vendor_id int,
  222. taxincluded bool DEFAULT 'f',
  223. amount NUMERIC,
  224. netamount NUMERIC,
  225. paid NUMERIC,
  226. datepaid date,
  227. duedate date,
  228. invoice bool DEFAULT 'f',
  229. ordnumber text,
  230. curr char(3),
  231. notes text,
  232. employee_id int,
  233. till varchar(20),
  234. quonumber text,
  235. intnotes text,
  236. department_id int DEFAULT 0,
  237. shipvia text,
  238. language_code varchar(6),
  239. ponumber text,
  240. shippingpoint text,
  241. terms int2 DEFAULT 0
  242. );
  243. --
  244. CREATE TABLE taxmodule (
  245. taxmodule_id serial PRIMARY KEY,
  246. taxmodulename text NOT NULL
  247. );
  248. --
  249. CREATE TABLE taxcategory (
  250. taxcategory_id serial PRIMARY KEY,
  251. taxcategoryname text NOT NULL,
  252. taxmodule_id int NOT NULL,
  253. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  254. );
  255. --
  256. CREATE TABLE partstax (
  257. parts_id int,
  258. chart_id int,
  259. taxcategory_id int,
  260. PRIMARY KEY (parts_id, chart_id),
  261. FOREIGN KEY (parts_id) REFERENCES parts (id),
  262. FOREIGN KEY (chart_id) REFERENCES chart (id),
  263. FOREIGN KEY (taxcategory_id) REFERENCES taxcategory (taxcategory_id)
  264. );
  265. --
  266. CREATE TABLE tax (
  267. chart_id int PRIMARY KEY,
  268. rate numeric,
  269. taxnumber text,
  270. validto date,
  271. pass integer DEFAULT 0 NOT NULL,
  272. taxmodule_id int DEFAULT 1 NOT NULL,
  273. FOREIGN KEY (chart_id) REFERENCES chart (id),
  274. FOREIGN KEY (taxmodule_id) REFERENCES taxmodule (taxmodule_id)
  275. );
  276. --
  277. CREATE TABLE customertax (
  278. customer_id int,
  279. chart_id int,
  280. PRIMARY KEY (customer_id, chart_id)
  281. );
  282. --
  283. CREATE TABLE vendortax (
  284. vendor_id int,
  285. chart_id int,
  286. PRIMARY KEY (vendor_id, chart_id)
  287. );
  288. --
  289. CREATE TABLE oe (
  290. id int default nextval('id') PRIMARY KEY,
  291. ordnumber text,
  292. transdate date default current_date,
  293. vendor_id int,
  294. customer_id int,
  295. amount NUMERIC,
  296. netamount NUMERIC,
  297. reqdate date,
  298. taxincluded bool,
  299. shippingpoint text,
  300. notes text,
  301. curr char(3),
  302. employee_id int,
  303. closed bool default 'f',
  304. quotation bool default 'f',
  305. quonumber text,
  306. intnotes text,
  307. department_id int default 0,
  308. shipvia text,
  309. language_code varchar(6),
  310. ponumber text,
  311. terms int2 DEFAULT 0
  312. );
  313. --
  314. CREATE TABLE orderitems (
  315. id int default nextval('orderitemsid') PRIMARY KEY,
  316. trans_id int,
  317. parts_id int,
  318. description text,
  319. qty numeric,
  320. sellprice NUMERIC,
  321. discount numeric,
  322. unit varchar(5),
  323. project_id int,
  324. reqdate date,
  325. ship numeric,
  326. serialnumber text,
  327. notes text
  328. );
  329. --
  330. CREATE TABLE exchangerate (
  331. curr char(3),
  332. transdate date,
  333. buy numeric,
  334. sell numeric,
  335. PRIMARY KEY (curr, transdate)
  336. );
  337. --
  338. create table employee (
  339. id int default nextval('id') PRIMARY KEY,
  340. login text,
  341. name varchar(64),
  342. address1 varchar(32),
  343. address2 varchar(32),
  344. city varchar(32),
  345. state varchar(32),
  346. zipcode varchar(10),
  347. country varchar(32),
  348. workphone varchar(20),
  349. homephone varchar(20),
  350. startdate date default current_date,
  351. enddate date,
  352. notes text,
  353. role varchar(20),
  354. sales bool default 'f',
  355. email text,
  356. ssn varchar(20),
  357. iban varchar(34),
  358. bic varchar(11),
  359. managerid int,
  360. employeenumber varchar(32),
  361. dob date
  362. );
  363. --
  364. create table shipto (
  365. trans_id int,
  366. shiptoname varchar(64),
  367. shiptoaddress1 varchar(32),
  368. shiptoaddress2 varchar(32),
  369. shiptocity varchar(32),
  370. shiptostate varchar(32),
  371. shiptozipcode varchar(10),
  372. shiptocountry varchar(32),
  373. shiptocontact varchar(64),
  374. shiptophone varchar(20),
  375. shiptofax varchar(20),
  376. shiptoemail text,
  377. entry_id SERIAL PRIMARY KEY
  378. );
  379. --
  380. CREATE TABLE vendor (
  381. id int default nextval('id') PRIMARY KEY,
  382. name varchar(64),
  383. address1 varchar(32),
  384. address2 varchar(32),
  385. city varchar(32),
  386. state varchar(32),
  387. zipcode varchar(10),
  388. country varchar(32),
  389. contact varchar(64),
  390. phone varchar(20),
  391. fax varchar(20),
  392. email text,
  393. notes text,
  394. terms int2 default 0,
  395. taxincluded bool default 'f',
  396. vendornumber varchar(32),
  397. cc text,
  398. bcc text,
  399. gifi_accno varchar(30),
  400. business_id int,
  401. taxnumber varchar(32),
  402. sic_code varchar(6),
  403. discount numeric,
  404. creditlimit numeric default 0,
  405. iban varchar(34),
  406. bic varchar(11),
  407. employee_id int,
  408. language_code varchar(6),
  409. pricegroup_id int,
  410. curr char(3),
  411. startdate date,
  412. enddate date
  413. );
  414. --
  415. CREATE TABLE project (
  416. id int default nextval('id') PRIMARY KEY,
  417. projectnumber text,
  418. description text,
  419. startdate date,
  420. enddate date,
  421. parts_id int,
  422. production numeric default 0,
  423. completed numeric default 0,
  424. customer_id int
  425. );
  426. --
  427. CREATE TABLE partsgroup (
  428. id int default nextval('id') PRIMARY KEY,
  429. partsgroup text
  430. );
  431. --
  432. CREATE TABLE status (
  433. trans_id int PRIMARY KEY,
  434. formname text,
  435. printed bool default 'f',
  436. emailed bool default 'f',
  437. spoolfile text
  438. );
  439. --
  440. CREATE TABLE department (
  441. id int default nextval('id') PRIMARY KEY,
  442. description text,
  443. role char(1) default 'P'
  444. );
  445. --
  446. -- department transaction table
  447. CREATE TABLE dpt_trans (
  448. trans_id int PRIMARY KEY,
  449. department_id int
  450. );
  451. --
  452. -- business table
  453. CREATE TABLE business (
  454. id int default nextval('id') PRIMARY KEY,
  455. description text,
  456. discount numeric
  457. );
  458. --
  459. -- SIC
  460. CREATE TABLE sic (
  461. code varchar(6) PRIMARY KEY,
  462. sictype char(1),
  463. description text
  464. );
  465. --
  466. CREATE TABLE warehouse (
  467. id int default nextval('id') PRIMARY KEY,
  468. description text
  469. );
  470. --
  471. CREATE TABLE inventory (
  472. warehouse_id int,
  473. parts_id int,
  474. trans_id int,
  475. orderitems_id int,
  476. qty numeric,
  477. shippingdate date,
  478. employee_id int,
  479. entry_id SERIAL PRIMARY KEY
  480. );
  481. --
  482. CREATE TABLE yearend (
  483. trans_id int PRIMARY KEY,
  484. transdate date
  485. );
  486. --
  487. CREATE TABLE partsvendor (
  488. vendor_id int,
  489. parts_id int,
  490. partnumber text,
  491. leadtime int2,
  492. lastcost NUMERIC,
  493. curr char(3),
  494. entry_id SERIAL PRIMARY KEY
  495. );
  496. --
  497. CREATE TABLE pricegroup (
  498. id int default nextval('id') PRIMARY KEY,
  499. pricegroup text
  500. );
  501. --
  502. CREATE TABLE partscustomer (
  503. parts_id int,
  504. customer_id int,
  505. pricegroup_id int,
  506. pricebreak numeric,
  507. sellprice NUMERIC,
  508. validfrom date,
  509. validto date,
  510. curr char(3),
  511. entry_id SERIAL PRIMARY KEY
  512. );
  513. --
  514. CREATE TABLE language (
  515. code varchar(6) PRIMARY KEY,
  516. description text
  517. );
  518. --
  519. CREATE TABLE audittrail (
  520. trans_id int,
  521. tablename text,
  522. reference text,
  523. formname text,
  524. action text,
  525. transdate timestamp default current_timestamp,
  526. employee_id int,
  527. entry_id BIGSERIAL PRIMARY KEY
  528. );
  529. --
  530. CREATE TABLE translation (
  531. trans_id int,
  532. language_code varchar(6),
  533. description text,
  534. PRIMARY KEY (trans_id, language_code)
  535. );
  536. --
  537. CREATE TABLE recurring (
  538. id int PRIMARY KEY,
  539. reference text,
  540. startdate date,
  541. nextdate date,
  542. enddate date,
  543. repeat int2,
  544. unit varchar(6),
  545. howmany int,
  546. payment bool default 'f'
  547. );
  548. --
  549. CREATE TABLE recurringemail (
  550. id int PRIMARY KEY,
  551. formname text,
  552. format text,
  553. message text
  554. );
  555. --
  556. CREATE TABLE recurringprint (
  557. id int PRIMARY KEY,
  558. formname text,
  559. format text,
  560. printer text
  561. );
  562. --
  563. CREATE TABLE jcitems (
  564. id int default nextval('jcitemsid') PRIMARY KEY,
  565. project_id int,
  566. parts_id int,
  567. description text,
  568. qty numeric,
  569. allocated numeric,
  570. sellprice NUMERIC,
  571. fxsellprice NUMERIC,
  572. serialnumber text,
  573. checkedin timestamp with time zone,
  574. checkedout timestamp with time zone,
  575. employee_id int,
  576. notes text
  577. );
  578. insert into transactions (id, table_name) SELECT id, 'ap' FROM ap;
  579. CREATE RULE ap_id_track_i AS ON insert TO ap
  580. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ap');
  581. CREATE RULE ap_id_track_u AS ON update TO ap
  582. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  583. insert into transactions (id, table_name) SELECT id, 'ar' FROM ap;
  584. CREATE RULE ar_id_track_i AS ON insert TO ar
  585. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'ar');
  586. CREATE RULE ar_id_track_u AS ON update TO ar
  587. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  588. INSERT INTO transactions (id, table_name) SELECT id, 'business' FROM business;
  589. CREATE RULE business_id_track_i AS ON insert TO business
  590. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'business');
  591. CREATE RULE business_id_track_u AS ON update TO business
  592. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  593. INSERT INTO transactions (id, table_name) SELECT id, 'chart' FROM chart;
  594. CREATE RULE chart_id_track_i AS ON insert TO chart
  595. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'chart');
  596. CREATE RULE chart_id_track_u AS ON update TO chart
  597. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  598. INSERT INTO transactions (id, table_name) SELECT id, 'customer' FROM customer;
  599. CREATE RULE customer_id_track_i AS ON insert TO customer
  600. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'customer');
  601. CREATE RULE customer_id_track_u AS ON update TO customer
  602. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  603. INSERT INTO transactions (id, table_name) SELECT id, 'department' FROM department;
  604. CREATE RULE department_id_track_i AS ON insert TO department
  605. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'department');
  606. CREATE RULE department_id_track_u AS ON update TO department
  607. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  608. INSERT INTO transactions (id, table_name) SELECT id, 'employee' FROM employee;
  609. CREATE RULE employee_id_track_i AS ON insert TO employee
  610. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  611. CREATE RULE employee_id_track_u AS ON update TO employee
  612. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  613. INSERT INTO transactions (id, table_name) SELECT id, 'gl' FROM gl;
  614. CREATE RULE gl_id_track_i AS ON insert TO gl
  615. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'gl');
  616. CREATE RULE gl_id_track_u AS ON update TO gl
  617. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  618. INSERT INTO transactions (id, table_name) SELECT id, 'oe' FROM oe;
  619. CREATE RULE oe_id_track_i AS ON insert TO oe
  620. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'oe');
  621. CREATE RULE oe_id_track_u AS ON update TO oe
  622. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  623. INSERT INTO transactions (id, table_name) SELECT id, 'parts' FROM parts;
  624. CREATE RULE parts_id_track_i AS ON insert TO parts
  625. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'parts');
  626. CREATE RULE parts_id_track_u AS ON update TO parts
  627. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  628. INSERT INTO transactions (id, table_name) SELECT id, 'partsgroup' FROM partsgroup;
  629. CREATE RULE partsgroup_id_track_i AS ON insert TO partsgroup
  630. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'partsgroup');
  631. CREATE RULE partsgroup_id_track_u AS ON update TO partsgroup
  632. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  633. INSERT INTO transactions (id, table_name) SELECT id, 'pricegroup' FROM pricegroup;
  634. CREATE RULE pricegroup_id_track_i AS ON insert TO pricegroup
  635. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'pricegroup');
  636. CREATE RULE pricegroup_id_track_u AS ON update TO pricegroup
  637. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  638. INSERT INTO transactions (id, table_name) SELECT id, 'project' FROM project;
  639. CREATE RULE project_id_track_i AS ON insert TO project
  640. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'project');
  641. CREATE RULE project_id_track_u AS ON update TO project
  642. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  643. INSERT INTO transactions (id, table_name) SELECT id, 'vendor' FROM vendor;
  644. CREATE RULE vendor_id_track_i AS ON insert TO vendor
  645. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'vendor');
  646. CREATE RULE employee_id_track_u AS ON update TO vendor
  647. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  648. INSERT INTO transactions (id, table_name) SELECT id, 'warehouse' FROM warehouse;
  649. CREATE RULE warehouse_id_track_i AS ON insert TO warehouse
  650. DO INSERT INTO transactions (id, table_name) VALUES (new.id, 'employee');
  651. CREATE RULE warehouse_id_track_u AS ON update TO warehouse
  652. DO UPDATE transactions SET id = new.id WHERE id = old.id;
  653. CREATE TABLE custom_table_catalog (
  654. table_id SERIAL PRIMARY KEY,
  655. extends TEXT,
  656. table_name TEXT
  657. );
  658. CREATE TABLE custom_field_catalog (
  659. field_id SERIAL PRIMARY KEY,
  660. table_id INT REFERENCES custom_table_catalog,
  661. field_name TEXT
  662. );
  663. INSERT INTO taxmodule (
  664. taxmodule_id, taxmodulename
  665. ) VALUES (
  666. 1, 'Simple'
  667. );
  668. create index acc_trans_trans_id_key on acc_trans (trans_id);
  669. create index acc_trans_chart_id_key on acc_trans (chart_id);
  670. create index acc_trans_transdate_key on acc_trans (transdate);
  671. create index acc_trans_source_key on acc_trans (lower(source));
  672. --
  673. create index ap_id_key on ap (id);
  674. create index ap_transdate_key on ap (transdate);
  675. create index ap_invnumber_key on ap (invnumber);
  676. create index ap_ordnumber_key on ap (ordnumber);
  677. create index ap_vendor_id_key on ap (vendor_id);
  678. create index ap_employee_id_key on ap (employee_id);
  679. create index ap_quonumber_key on ap (quonumber);
  680. --
  681. create index ar_id_key on ar (id);
  682. create index ar_transdate_key on ar (transdate);
  683. create index ar_invnumber_key on ar (invnumber);
  684. create index ar_ordnumber_key on ar (ordnumber);
  685. create index ar_customer_id_key on ar (customer_id);
  686. create index ar_employee_id_key on ar (employee_id);
  687. create index ar_quonumber_key on ar (quonumber);
  688. --
  689. create index assembly_id_key on assembly (id);
  690. --
  691. create index chart_id_key on chart (id);
  692. create unique index chart_accno_key on chart (accno);
  693. create index chart_category_key on chart (category);
  694. create index chart_link_key on chart (link);
  695. create index chart_gifi_accno_key on chart (gifi_accno);
  696. --
  697. create index customer_id_key on customer (id);
  698. create index customer_customernumber_key on customer (customernumber);
  699. create index customer_name_key on customer (lower(name));
  700. create index customer_contact_key on customer (lower(contact));
  701. create index customer_customer_id_key on customertax (customer_id);
  702. --
  703. create index employee_id_key on employee (id);
  704. create unique index employee_login_key on employee (login);
  705. create index employee_name_key on employee (lower(name));
  706. --
  707. create index exchangerate_ct_key on exchangerate (curr, transdate);
  708. --
  709. create unique index gifi_accno_key on gifi (accno);
  710. --
  711. create index gl_id_key on gl (id);
  712. create index gl_transdate_key on gl (transdate);
  713. create index gl_reference_key on gl (reference);
  714. create index gl_description_key on gl (lower(description));
  715. create index gl_employee_id_key on gl (employee_id);
  716. --
  717. create index invoice_id_key on invoice (id);
  718. create index invoice_trans_id_key on invoice (trans_id);
  719. --
  720. create index makemodel_parts_id_key on makemodel (parts_id);
  721. create index makemodel_make_key on makemodel (lower(make));
  722. create index makemodel_model_key on makemodel (lower(model));
  723. --
  724. create index oe_id_key on oe (id);
  725. create index oe_transdate_key on oe (transdate);
  726. create index oe_ordnumber_key on oe (ordnumber);
  727. create index oe_employee_id_key on oe (employee_id);
  728. create index orderitems_trans_id_key on orderitems (trans_id);
  729. create index orderitems_id_key on orderitems (id);
  730. --
  731. create index parts_id_key on parts (id);
  732. create index parts_partnumber_key on parts (lower(partnumber));
  733. create index parts_description_key on parts (lower(description));
  734. create index partstax_parts_id_key on partstax (parts_id);
  735. --
  736. create index vendor_id_key on vendor (id);
  737. create index vendor_name_key on vendor (lower(name));
  738. create index vendor_vendornumber_key on vendor (vendornumber);
  739. create index vendor_contact_key on vendor (lower(contact));
  740. create index vendortax_vendor_id_key on vendortax (vendor_id);
  741. --
  742. create index shipto_trans_id_key on shipto (trans_id);
  743. --
  744. create index project_id_key on project (id);
  745. create unique index projectnumber_key on project (projectnumber);
  746. --
  747. create index partsgroup_id_key on partsgroup (id);
  748. create unique index partsgroup_key on partsgroup (partsgroup);
  749. --
  750. create index status_trans_id_key on status (trans_id);
  751. --
  752. create index department_id_key on department (id);
  753. --
  754. create index partsvendor_vendor_id_key on partsvendor (vendor_id);
  755. create index partsvendor_parts_id_key on partsvendor (parts_id);
  756. --
  757. create index pricegroup_pricegroup_key on pricegroup (pricegroup);
  758. create index pricegroup_id_key on pricegroup (id);
  759. --
  760. create index audittrail_trans_id_key on audittrail (trans_id);
  761. --
  762. create index translation_trans_id_key on translation (trans_id);
  763. --
  764. create unique index language_code_key on language (code);
  765. --
  766. create index jcitems_id_key on jcitems (id);
  767. --
  768. CREATE FUNCTION del_yearend() RETURNS TRIGGER AS '
  769. begin
  770. delete from yearend where trans_id = old.id;
  771. return NULL;
  772. end;
  773. ' language 'plpgsql';
  774. -- end function
  775. --
  776. CREATE TRIGGER del_yearend AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_yearend();
  777. -- end trigger
  778. --
  779. CREATE FUNCTION del_department() RETURNS TRIGGER AS '
  780. begin
  781. delete from dpt_trans where trans_id = old.id;
  782. return NULL;
  783. end;
  784. ' language 'plpgsql';
  785. -- end function
  786. --
  787. CREATE TRIGGER del_department AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_department();
  788. -- end trigger
  789. CREATE TRIGGER del_department AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_department();
  790. -- end trigger
  791. CREATE TRIGGER del_department AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_department();
  792. -- end trigger
  793. CREATE TRIGGER del_department AFTER DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_department();
  794. -- end trigger
  795. --
  796. CREATE FUNCTION del_customer() RETURNS TRIGGER AS '
  797. begin
  798. delete from shipto where trans_id = old.id;
  799. delete from customertax where customer_id = old.id;
  800. delete from partscustomer where customer_id = old.id;
  801. return NULL;
  802. end;
  803. ' language 'plpgsql';
  804. -- end function
  805. --
  806. CREATE TRIGGER del_customer AFTER DELETE ON customer FOR EACH ROW EXECUTE PROCEDURE del_customer();
  807. -- end trigger
  808. --
  809. CREATE FUNCTION del_vendor() RETURNS TRIGGER AS '
  810. begin
  811. delete from shipto where trans_id = old.id;
  812. delete from vendortax where vendor_id = old.id;
  813. delete from partsvendor where vendor_id = old.id;
  814. return NULL;
  815. end;
  816. ' language 'plpgsql';
  817. -- end function
  818. --
  819. CREATE TRIGGER del_vendor AFTER DELETE ON vendor FOR EACH ROW EXECUTE PROCEDURE del_vendor();
  820. -- end trigger
  821. --
  822. CREATE FUNCTION del_exchangerate() RETURNS TRIGGER AS '
  823. declare
  824. t_transdate date;
  825. t_curr char(3);
  826. t_id int;
  827. d_curr text;
  828. begin
  829. select into d_curr substr(value,1,3) from defaults where setting_key = ''curr'';
  830. if TG_RELNAME = ''ar'' then
  831. select into t_curr, t_transdate curr, transdate from ar where id = old.id;
  832. end if;
  833. if TG_RELNAME = ''ap'' then
  834. select into t_curr, t_transdate curr, transdate from ap where id = old.id;
  835. end if;
  836. if TG_RELNAME = ''oe'' then
  837. select into t_curr, t_transdate curr, transdate from oe where id = old.id;
  838. end if;
  839. if d_curr != t_curr then
  840. select into t_id a.id from acc_trans ac
  841. join ar a on (a.id = ac.trans_id)
  842. where a.curr = t_curr
  843. and ac.transdate = t_transdate
  844. except select a.id from ar a where a.id = old.id
  845. union
  846. select a.id from acc_trans ac
  847. join ap a on (a.id = ac.trans_id)
  848. where a.curr = t_curr
  849. and ac.transdate = t_transdate
  850. except select a.id from ap a where a.id = old.id
  851. union
  852. select o.id from oe o
  853. where o.curr = t_curr
  854. and o.transdate = t_transdate
  855. except select o.id from oe o where o.id = old.id;
  856. if not found then
  857. delete from exchangerate where curr = t_curr and transdate = t_transdate;
  858. end if;
  859. end if;
  860. return old;
  861. end;
  862. ' language 'plpgsql';
  863. -- end function
  864. --
  865. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  866. -- end trigger
  867. --
  868. CREATE TRIGGER del_exchangerate BEFORE DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  869. -- end trigger
  870. --
  871. CREATE TRIGGER del_exchangerate BEFORE DELETE ON oe FOR EACH ROW EXECUTE PROCEDURE del_exchangerate();
  872. -- end trigger
  873. --
  874. CREATE FUNCTION check_inventory() RETURNS TRIGGER AS '
  875. declare
  876. itemid int;
  877. row_data inventory%rowtype;
  878. begin
  879. if not old.quotation then
  880. for row_data in select * from inventory where trans_id = old.id loop
  881. select into itemid id from orderitems where trans_id = old.id and id = row_data.orderitems_id;
  882. if itemid is null then
  883. delete from inventory where trans_id = old.id and orderitems_id = row_data.orderitems_id;
  884. end if;
  885. end loop;
  886. end if;
  887. return old;
  888. end;
  889. ' language 'plpgsql';
  890. -- end function
  891. --
  892. CREATE TRIGGER check_inventory AFTER UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_inventory();
  893. -- end trigger
  894. --
  895. --
  896. CREATE FUNCTION check_department() RETURNS TRIGGER AS '
  897. declare
  898. dpt_id int;
  899. begin
  900. if new.department_id = 0 then
  901. delete from dpt_trans where trans_id = new.id;
  902. return NULL;
  903. end if;
  904. select into dpt_id trans_id from dpt_trans where trans_id = new.id;
  905. if dpt_id > 0 then
  906. update dpt_trans set department_id = new.department_id where trans_id = dpt_id;
  907. else
  908. insert into dpt_trans (trans_id, department_id) values (new.id, new.department_id);
  909. end if;
  910. return NULL;
  911. end;
  912. ' language 'plpgsql';
  913. -- end function
  914. --
  915. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ar FOR EACH ROW EXECUTE PROCEDURE check_department();
  916. -- end trigger
  917. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON ap FOR EACH ROW EXECUTE PROCEDURE check_department();
  918. -- end trigger
  919. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON gl FOR EACH ROW EXECUTE PROCEDURE check_department();
  920. -- end trigger
  921. CREATE TRIGGER check_department AFTER INSERT OR UPDATE ON oe FOR EACH ROW EXECUTE PROCEDURE check_department();
  922. -- end trigger
  923. --
  924. CREATE FUNCTION del_recurring() RETURNS TRIGGER AS '
  925. BEGIN
  926. DELETE FROM recurring WHERE id = old.id;
  927. DELETE FROM recurringemail WHERE id = old.id;
  928. DELETE FROM recurringprint WHERE id = old.id;
  929. RETURN NULL;
  930. END;
  931. ' language 'plpgsql';
  932. --end function
  933. CREATE TRIGGER del_recurring AFTER DELETE ON ar FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  934. -- end trigger
  935. CREATE TRIGGER del_recurring AFTER DELETE ON ap FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  936. -- end trigger
  937. CREATE TRIGGER del_recurring AFTER DELETE ON gl FOR EACH ROW EXECUTE PROCEDURE del_recurring();
  938. -- end trigger
  939. --
  940. CREATE FUNCTION avgcost(int) RETURNS FLOAT AS '
  941. DECLARE
  942. v_cost float;
  943. v_qty float;
  944. v_parts_id alias for $1;
  945. BEGIN
  946. SELECT INTO v_cost, v_qty SUM(i.sellprice * i.qty), SUM(i.qty)
  947. FROM invoice i
  948. JOIN ap a ON (a.id = i.trans_id)
  949. WHERE i.parts_id = v_parts_id;
  950. IF v_cost IS NULL THEN
  951. v_cost := 0;
  952. END IF;
  953. IF NOT v_qty IS NULL THEN
  954. IF v_qty = 0 THEN
  955. v_cost := 0;
  956. ELSE
  957. v_cost := v_cost/v_qty;
  958. END IF;
  959. END IF;
  960. RETURN v_cost;
  961. END;
  962. ' language 'plpgsql';
  963. -- end function
  964. --
  965. CREATE FUNCTION lastcost(int) RETURNS FLOAT AS '
  966. DECLARE
  967. v_cost float;
  968. v_parts_id alias for $1;
  969. BEGIN
  970. SELECT INTO v_cost sellprice FROM invoice i
  971. JOIN ap a ON (a.id = i.trans_id)
  972. WHERE i.parts_id = v_parts_id
  973. ORDER BY a.transdate desc, a.id desc
  974. LIMIT 1;
  975. IF v_cost IS NULL THEN
  976. v_cost := 0;
  977. END IF;
  978. RETURN v_cost;
  979. END;
  980. ' language plpgsql;
  981. -- end function
  982. --
  983. CREATE OR REPLACE FUNCTION trigger_parts_short() RETURNS TRIGGER
  984. AS
  985. '
  986. BEGIN
  987. IF NEW.onhand >= NEW.rop THEN
  988. NOTIFY parts_short;
  989. END IF;
  990. RETURN NEW;
  991. END;
  992. ' LANGUAGE PLPGSQL;
  993. -- end function
  994. CREATE TRIGGER parts_short AFTER UPDATE ON parts
  995. FOR EACH ROW EXECUTE PROCEDURE trigger_parts_short();
  996. -- end function
  997. CREATE OR REPLACE FUNCTION add_custom_field (VARCHAR, VARCHAR, VARCHAR)
  998. RETURNS BOOL AS
  999. '
  1000. DECLARE
  1001. table_name ALIAS FOR $1;
  1002. new_field_name ALIAS FOR $2;
  1003. field_datatype ALIAS FOR $3;
  1004. BEGIN
  1005. EXECUTE ''SELECT TABLE_ID FROM custom_table_catalog
  1006. WHERE extends = '''''' || table_name || '''''' '';
  1007. IF NOT FOUND THEN
  1008. BEGIN
  1009. INSERT INTO custom_table_catalog (extends)
  1010. VALUES (table_name);
  1011. EXECUTE ''CREATE TABLE custom_''||table_name ||
  1012. '' (row_id INT PRIMARY KEY)'';
  1013. EXCEPTION WHEN duplicate_table THEN
  1014. -- do nothing
  1015. END;
  1016. END IF;
  1017. EXECUTE ''INSERT INTO custom_field_catalog (field_name, table_id)
  1018. VALUES ( '''''' || new_field_name ||'''''', (SELECT table_id FROM custom_table_catalog
  1019. WHERE extends = ''''''|| table_name || ''''''))'';
  1020. EXECUTE ''ALTER TABLE custom_''||table_name || '' ADD COLUMN ''
  1021. || new_field_name || '' '' || field_datatype;
  1022. RETURN TRUE;
  1023. END;
  1024. ' LANGUAGE PLPGSQL;
  1025. -- end function
  1026. CREATE OR REPLACE FUNCTION drop_custom_field (VARCHAR, VARCHAR)
  1027. RETURNS BOOL AS
  1028. '
  1029. DECLARE
  1030. table_name ALIAS FOR $1;
  1031. custom_field_name ALIAS FOR $2;
  1032. BEGIN
  1033. DELETE FROM custom_field_catalog
  1034. WHERE field_name = custom_field_name AND
  1035. table_id = (SELECT table_id FROM custom_table_catalog
  1036. WHERE extends = table_name);
  1037. EXECUTE ''ALTER TABLE custom_'' || table_name ||
  1038. '' DROP COLUMN '' || custom_field_name;
  1039. RETURN TRUE;
  1040. END;
  1041. ' LANGUAGE PLPGSQL;
  1042. -- end function
  1043. commit;