56aa0199921e65a71d08d8b0ec2c8f0d3723a4d3
[fa-stable.git] / sql / alter2.4.sql
1 INSERT INTO `0_sys_prefs` VALUES('tax_algorithm','glsetup.customer', 'tinyint', 1, '1');
2 INSERT INTO `0_sys_prefs` VALUES('gl_closing_date','setup.closing_date', 'date', 8, '');
3 ALTER TABLE `0_audit_trail` CHANGE `fiscal_year` `fiscal_year` int(11) NOT NULL default 0;
4
5 # Fix eventual invalid date/year in audit records
6 UPDATE `0_audit_trail` audit 
7                 LEFT JOIN `0_gl_trans` gl ON  gl.`type`=audit.`type` AND gl.type_no=audit.trans_no
8                 LEFT JOIN `0_fiscal_year` year ON year.begin<=gl.tran_date AND year.end>=gl.tran_date
9                 SET audit.gl_date=gl.tran_date, audit.fiscal_year=year.id
10                 WHERE NOT ISNULL(gl.`type`);
11
12 DROP TABLE IF EXISTS `0_wo_costing`;
13
14 CREATE TABLE `0_wo_costing` (
15   `id` int(11) NOT NULL auto_increment,
16   `workorder_id` int(11) NOT NULL default '0',
17   `cost_type`   tinyint(1) NOT NULL default '0',
18   `trans_type` int(11) NOT NULL default '0',
19   `trans_no` int(11) NOT NULL default '0',
20   `factor` double NOT NULL default '1',
21   PRIMARY KEY  (`id`)
22 ) ENGINE=InnoDB;
23
24 UPDATE `0_gl_trans` gl
25                 LEFT JOIN `0_cust_branch` br ON br.receivables_account=gl.account AND br.debtor_no=gl.person_id AND gl.person_type_id=2
26                 LEFT JOIN `0_suppliers` sup ON sup.payable_account=gl.account AND sup.supplier_id=gl.person_id AND gl.person_type_id=3
27  SET `person_id` = IF(br.receivables_account, br.debtor_no, IF(sup.payable_account, sup.supplier_id, NULL)), 
28         `person_type_id` = IF(br.receivables_account, 2, IF(sup.payable_account, 3, NULL));
29
30 ALTER TABLE `0_tax_group_items` ADD COLUMN `tax_shipping` tinyint(1) NOT NULL default '0' AFTER `rate`;
31 UPDATE `0_tax_group_items` tgi
32         SET tgi.tax_shipping=1
33         WHERE tgi.rate=(SELECT 0_tax_types.rate FROM 0_tax_types, 0_tax_groups 
34                 WHERE tax_shipping=1 AND tgi.tax_group_id=0_tax_groups.id AND tgi.tax_type_id=0_tax_types.id);
35
36 ALTER TABLE `0_sales_order_details` ADD KEY `stkcode` (`stk_code`);
37 ALTER TABLE `0_purch_order_details` ADD KEY `itemcode` (`item_code`);
38 ALTER TABLE `0_sys_prefs` CHANGE `value` `value` TEXT NOT NULL DEFAULT '';
39 ALTER TABLE `0_cust_branch` ADD COLUMN `bank_account` varchar(60) DEFAULT NULL AFTER `notes`;
40
41 ALTER TABLE `0_debtor_trans` ADD COLUMN `tax_included` tinyint(1) unsigned NOT NULL default '0' AFTER `payment_terms`;
42 UPDATE `0_debtor_trans` tr, `0_trans_tax_details` td SET tr.tax_included=td.included_in_price
43         WHERE tr.`type`=td.trans_type AND tr.trans_no=td.trans_no AND td.included_in_price;
44
45 ALTER TABLE `0_bank_accounts` ADD COLUMN `bank_charge_act` varchar(15) NOT NULL DEFAULT '' AFTER `id`;
46 UPDATE `0_bank_accounts` SET `bank_charge_act`=(SELECT `value` FROM 0_sys_prefs WHERE name='bank_charge_act');
47
48 ALTER TABLE `0_users` ADD `transaction_days` INT( 6 ) NOT NULL default '30' COMMENT 'Transaction days' AFTER `startup_tab`;
49
50 ALTER TABLE `0_purch_orders` ADD COLUMN `prep_amount` double NOT NULL DEFAULT 0 AFTER `total`;
51 ALTER TABLE `0_purch_orders` ADD COLUMN `alloc` double NOT NULL DEFAULT 0 AFTER `prep_amount`;
52
53 ALTER TABLE `0_sales_orders` ADD COLUMN `prep_amount` double NOT NULL DEFAULT 0 AFTER `total`;
54 ALTER TABLE `0_sales_orders` ADD COLUMN `alloc` double NOT NULL DEFAULT 0 AFTER `prep_amount`;
55
56 ALTER TABLE `0_cust_allocations` ADD  UNIQUE KEY(`trans_type_from`,`trans_no_from`,`trans_type_to`,`trans_no_to`);
57 ALTER TABLE `0_supp_allocations` ADD  UNIQUE KEY(`trans_type_from`,`trans_no_from`,`trans_type_to`,`trans_no_to`);
58
59 ALTER TABLE `0_sales_order_details` ADD COLUMN `invoiced` double NOT NULL DEFAULT 0 AFTER `quantity`;
60
61 # update sales_order_details.invoiced with sum of invoiced quantities on all related SI
62 UPDATE `0_sales_order_details` so
63         LEFT JOIN `0_debtor_trans_details` delivery ON delivery.`debtor_trans_type`=13 AND src_id=so.id
64         LEFT JOIN (SELECT src_id, sum(quantity) as qty FROM `0_debtor_trans_details` WHERE `debtor_trans_type`=10 GROUP BY src_id) inv
65                 ON inv.src_id=delivery.id
66         SET `invoiced` = `invoiced`+inv.qty;
67
68 ALTER TABLE `0_debtor_trans` ADD COLUMN `prep_amount` double NOT NULL DEFAULT 0 AFTER `alloc`;
69
70 INSERT INTO `0_sys_prefs` VALUES ('deferred_income_act', 'glsetup.sales', 'varchar', '15', '');
71
72 # set others transactions edition for all roles for backward  compatibility
73 UPDATE `0_security_roles` SET `sections`=CONCAT_WS(';', `sections`, '768'), `areas`='775'
74         WHERE NOT `sections` REGEXP '[^0-9]?768[^0-9]?';
75
76 UPDATE `0_security_roles` SET `areas`=CONCAT_WS(';', `areas`, '775')
77         WHERE NOT `areas` REGEXP '[^0-9]?775[^0-9]?';
78
79 ALTER TABLE `0_stock_master` ADD COLUMN `no_purchase` tinyint(1) NOT NULL default '0' AFTER `no_sale`;
80 ALTER TABLE `0_stock_category` ADD COLUMN `dflt_no_purchase` tinyint(1) NOT NULL default '0' AFTER `dflt_no_sale`;
81
82 # added exchange rate field in grn_batch
83 ALTER TABLE `0_grn_batch` ADD COLUMN `rate` double NULL default '1' AFTER `loc_code`;
84 ALTER TABLE `0_users` CHANGE `query_size` `query_size` TINYINT(1) UNSIGNED NOT NULL DEFAULT 10; 
85
86 ALTER TABLE `0_users` ADD `save_report_selections` SMALLINT( 6 ) NOT NULL default '0' COMMENT 'Save Report Selection Days' AFTER `transaction_days`;
87 ALTER TABLE `0_users` ADD `use_date_picker` TINYINT(1) NOT NULL default '1' COMMENT 'Use Date Picker for all Date Values' AFTER `save_report_selections`;
88 ALTER TABLE `0_users` ADD `def_print_destination` TINYINT(1) NOT NULL default '0' COMMENT 'Default Report Destination' AFTER `use_date_picker`;
89 ALTER TABLE `0_users` ADD `def_print_orientation` TINYINT(1) NOT NULL default '0' COMMENT 'Default Report Orientation' AFTER `def_print_destination`;
90
91 INSERT INTO `0_sys_prefs` VALUES('no_zero_lines_amount', 'glsetup.sales', 'tinyint', 1, '1');
92 INSERT INTO `0_sys_prefs` VALUES('show_po_item_codes', 'glsetup.purchase', 'tinyint', 1, '0');
93 INSERT INTO `0_sys_prefs` VALUES('accounts_alpha', 'glsetup.general', 'tinyint', 1, '0');
94 INSERT INTO `0_sys_prefs` VALUES('loc_notification', 'glsetup.inventory', 'tinyint', 1, '0');
95 INSERT INTO `0_sys_prefs` VALUES('print_invoice_no', 'glsetup.sales', 'tinyint', 1, '0');
96 INSERT INTO `0_sys_prefs` VALUES('allow_negative_prices', 'glsetup.inventory', 'tinyint', 1, '1');
97 INSERT INTO `0_sys_prefs` VALUES('print_item_images_on_quote', 'glsetup.inventory', 'tinyint', 1, '0');
98 INSERT INTO `0_sys_prefs` VALUES('default_receival_required', 'glsetup.purchase', 'smallint', 6, '10');
99
100 # switching all MyISAM tables to InnoDB
101 ALTER TABLE `0_areas` ENGINE=InnoDB;
102 ALTER TABLE `0_attachments` ENGINE=InnoDB;
103 ALTER TABLE `0_bank_accounts` ENGINE=InnoDB;
104 ALTER TABLE `0_bom` ENGINE=InnoDB;
105 ALTER TABLE `0_chart_class` ENGINE=InnoDB;
106 ALTER TABLE `0_chart_master` ENGINE=InnoDB;
107 ALTER TABLE `0_chart_types` ENGINE=InnoDB;
108 ALTER TABLE `0_credit_status` ENGINE=InnoDB;
109 ALTER TABLE `0_currencies` ENGINE=InnoDB;
110 ALTER TABLE `0_cust_branch` ENGINE=InnoDB;
111 ALTER TABLE `0_debtors_master` ENGINE=InnoDB;
112 ALTER TABLE `0_exchange_rates` ENGINE=InnoDB;
113 ALTER TABLE `0_groups` ENGINE=InnoDB;
114 ALTER TABLE `0_item_codes` ENGINE=InnoDB;
115 ALTER TABLE `0_item_units` ENGINE=InnoDB;
116 ALTER TABLE `0_locations` ENGINE=InnoDB;
117 ALTER TABLE `0_payment_terms` ENGINE=InnoDB;
118 ALTER TABLE `0_prices` ENGINE=InnoDB;
119 ALTER TABLE `0_printers` ENGINE=InnoDB;
120 ALTER TABLE `0_print_profiles` ENGINE=InnoDB;
121 ALTER TABLE `0_purch_data` ENGINE=InnoDB;
122 ALTER TABLE `0_quick_entries` ENGINE=InnoDB;
123 ALTER TABLE `0_quick_entry_lines` ENGINE=InnoDB;
124 ALTER TABLE `0_salesman` ENGINE=InnoDB;
125 ALTER TABLE `0_sales_pos` ENGINE=InnoDB;
126 ALTER TABLE `0_sales_types` ENGINE=InnoDB;
127 ALTER TABLE `0_security_roles` ENGINE=InnoDB;
128 ALTER TABLE `0_shippers` ENGINE=InnoDB;
129 ALTER TABLE `0_sql_trail` ENGINE=InnoDB;
130 ALTER TABLE `0_stock_category` ENGINE=InnoDB;
131 ALTER TABLE `0_suppliers` ENGINE=InnoDB;
132 ALTER TABLE `0_sys_prefs` ENGINE=InnoDB;
133 ALTER TABLE `0_tags` ENGINE=InnoDB;
134 ALTER TABLE `0_tag_associations` ENGINE=InnoDB;
135 ALTER TABLE `0_useronline` ENGINE=InnoDB;
136 ALTER TABLE `0_users` ENGINE=InnoDB;
137 ALTER TABLE `0_workcentres` ENGINE=InnoDB;
138
139 ALTER TABLE `0_gl_trans` CHANGE `type_no` `type_no` int(11) NOT NULL default '0';
140 ALTER TABLE `0_loc_stock` CHANGE `reorder_level` `reorder_level` double NOT NULL default '0';
141
142 # added dimensions in supplier documents
143 ALTER TABLE `0_supp_invoice_items` ADD COLUMN `dimension_id` int(11) NOT NULL DEFAULT '0' AFTER `memo_`;
144 ALTER TABLE `0_supp_invoice_items` ADD COLUMN `dimension2_id` int(11) NOT NULL DEFAULT '0' AFTER `dimension_id`;
145
146 UPDATE `0_supp_invoice_items` si
147         LEFT JOIN `0_gl_trans` gl ON si.supp_trans_type=gl.`type` AND si.supp_trans_no=gl.type_no AND si.gl_code=gl.account
148         SET si.dimension_id=gl.dimension_id, si.dimension2_id=gl.dimension2_id
149 WHERE si.grn_item_id=-1 AND (gl.dimension_id OR gl.dimension2_id);
150
151 ALTER TABLE `0_quick_entries` ADD COLUMN `usage` varchar(120) NULL AFTER `description`;
152 ALTER TABLE `0_quick_entry_lines` ADD COLUMN `memo` tinytext NOT NULL AFTER `amount`;
153
154 # multiply allocations to single jiurnal transaction
155 ALTER TABLE `0_cust_allocations` ADD COLUMN `person_id` int(11) DEFAULT NULL AFTER `id`;
156 UPDATE `0_cust_allocations` alloc LEFT JOIN `0_debtor_trans` trans ON alloc.trans_no_to=trans.trans_no AND alloc.trans_type_to=trans.type
157         SET alloc.person_id = trans.debtor_no;
158
159 ALTER TABLE `0_supp_allocations` ADD COLUMN `person_id` int(11) DEFAULT NULL AFTER `id`;
160 UPDATE `0_supp_allocations` alloc LEFT JOIN `0_supp_trans` trans ON alloc.trans_no_to=trans.trans_no AND alloc.trans_type_to=trans.type
161         SET alloc.person_id = trans.supplier_id;
162
163 ALTER TABLE `0_cust_allocations` DROP KEY `trans_type_from`;
164 ALTER TABLE `0_cust_allocations` ADD  UNIQUE KEY(`person_id`,`trans_type_from`,`trans_no_from`,`trans_type_to`,`trans_no_to`);
165 ALTER TABLE `0_supp_allocations` DROP KEY `trans_type_from`;
166 ALTER TABLE `0_supp_allocations` ADD  UNIQUE KEY(`person_id`,`trans_type_from`,`trans_no_from`,`trans_type_to`,`trans_no_to`);
167
168 # full support for any journal transaction
169 DROP TABLE IF EXISTS `0_journal`;
170 CREATE TABLE `0_journal` (
171   `type` smallint(6) NOT NULL DEFAULT '0',
172   `trans_no` int(11) NOT NULL DEFAULT '0',
173   `tran_date` date DEFAULT '0000-00-00',
174   `reference` varchar(60) NOT NULL DEFAULT '',
175   `source_ref` varchar(60) NOT NULL DEFAULT '',
176   `event_date` date DEFAULT '0000-00-00',
177   `doc_date` date NOT NULL DEFAULT '0000-00-00',
178   `currency` char(3) NOT NULL DEFAULT '',
179   `amount` double NOT NULL DEFAULT '0',
180   `rate` double NOT NULL DEFAULT '1',
181   PRIMARY KEY `Type_and_Number` (`type`,`trans_no`),
182   KEY `tran_date` (`tran_date`)
183 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ;
184
185 INSERT INTO `0_journal` (`type`, `trans_no`, `tran_date`, `reference`, `event_date`,`doc_date`,`currency`,`amount`)
186  SELECT `gl`.`type`, `gl`.`type_no`, `gl`.`tran_date`, `ref`.`reference`, `gl`.`tran_date`,
187                 `gl`.`tran_date`, `sys_curr`.`value`, SUM(IF(`gl`.`amount`>0,`gl`.`amount`,0))
188  FROM `0_gl_trans` gl LEFT JOIN `0_refs` ref ON gl.type = ref.type AND gl.type_no=ref.id
189  LEFT JOIN `0_sys_prefs` sys_curr ON `sys_curr`.`name`='curr_default'
190  WHERE `gl`.`type` IN(0, 35)
191  GROUP BY `type`,`type_no`;
192
193 # allow multiply customers.suppliers in single journal transaction
194 ALTER TABLE `0_debtor_trans` DROP PRIMARY KEY;
195 ALTER TABLE `0_debtor_trans` ADD  PRIMARY KEY (`type`,`trans_no`, `debtor_no`);
196 ALTER TABLE `0_supp_trans` DROP PRIMARY KEY;
197 ALTER TABLE `0_supp_trans` ADD  PRIMARY KEY (`type`,`trans_no`, `supplier_id`);
198
199 ALTER TABLE  `0_trans_tax_details` ADD COLUMN `reg_type` tinyint(1) DEFAULT NULL AFTER `memo`;
200
201 UPDATE `0_trans_tax_details` reg
202         SET reg.reg_type=1
203         WHERE reg.trans_type IN(20, 21);
204
205 UPDATE `0_trans_tax_details` reg
206         SET reg.reg_type=0
207         WHERE reg.trans_type IN(10, 11);
208
209 INSERT IGNORE INTO `0_sys_prefs` VALUES
210         ('grn_clearing_act', 'glsetup.purchase', 'varchar', 15, 0),
211         ('default_receival_required', 'glsetup.purchase', 'smallint', 6, '10'),
212         ('default_quote_valid_days', 'glsetup.sales', 'smallint', 6, 30),
213         ('no_zero_lines_amount', 'glsetup.sales', 'tinyint', 1, '1'),
214         ('show_po_item_codes', 'glsetup.purchase', 'tinyint', 1, '0'),
215         ('accounts_alpha', 'glsetup.general', 'tinyint', 1, '0'),
216         ('loc_notification', 'glsetup.inventory', 'tinyint', 1, '0'),
217         ('print_invoice_no', 'glsetup.sales', 'tinyint', 1, '0'),
218         ('allow_negative_prices', 'glsetup.inventory', 'tinyint', 1, '1'),
219         ('print_item_images_on_quote', 'glsetup.inventory', 'tinyint', 1, '0'),
220         ('bcc_email', 'setup.company', 'varchar', 100, ''),
221         ('alternative_tax_include_on_docs', 'setup.company', 'tinyint', 1, '0'),
222         ('suppress_tax_rates', 'setup.company', 'tinyint', 1, '0');
223
224 # stock_moves.visible field is obsolete
225 # removing obsolete moves for writeoffs
226 DELETE moves
227         FROM `0_stock_moves` moves 
228         INNER JOIN (SELECT * FROM `0_stock_moves` WHERE `type`=11 AND `qty`<0) writeoffs ON writeoffs.`trans_no`=moves.`trans_no` AND writeoffs.`type`=11
229         WHERE moves.`type`=11;
230
231 # stock_moves.discount_percent field are obsolete
232
233 UPDATE `0_stock_moves` SET
234         price = price*(1-discount_percent);
235
236 DROP TABLE IF EXISTS `0_movement_types`;
237
238 # change salesman breakpoint meaning to turnover level
239 UPDATE `0_salesman`
240         SET `break_pt` = `break_pt`*100.0/`provision`
241 WHERE `provision` != 0;
242