Feature 5388: Print Invoices (documents) list gets too long. Fixed by default 180...
[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_quote_valid_days', 'glsetup.sales', 'smallint', 6, 30),
212         ('no_zero_lines_amount', 'glsetup.sales', 'tinyint', 1, '1'),
213         ('accounts_alpha', 'glsetup.general', 'tinyint', 1, '0'),
214         ('bcc_email', 'setup.company', 'varchar', 100, ''),
215         ('alternative_tax_include_on_docs', 'setup.company', 'tinyint', 1, '0'),
216         ('suppress_tax_rates', 'setup.company', 'tinyint', 1, '0');
217
218 # stock_moves.visible field is obsolete
219 # removing obsolete moves for writeoffs
220 DELETE moves
221         FROM `0_stock_moves` moves 
222         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
223         WHERE moves.`type`=11;
224
225 # stock_moves.discount_percent field are obsolete
226
227 UPDATE `0_stock_moves` SET
228         price = price*(1-discount_percent);
229
230 DROP TABLE IF EXISTS `0_movement_types`;
231
232 # change salesman breakpoint meaning to turnover level
233 UPDATE `0_salesman`
234         SET `break_pt` = `break_pt`*100.0/`provision`
235 WHERE `provision` != 0;
236
237 # reference lines
238 DROP TABLE IF EXISTS `0_reflines`;
239 CREATE TABLE `0_reflines` (
240   `id` int(11) NOT NULL AUTO_INCREMENT,
241   `trans_type` int(11) NOT NULL,
242   `prefix` char(5) NOT NULL DEFAULT '',
243   `pattern` varchar(35) NOT NULL DEFAULT '1',
244   `description` varchar(60) NOT NULL DEFAULT '',
245   `default` tinyint(1) NOT NULL DEFAULT '0',
246   `inactive` tinyint(1) NOT NULL DEFAULT '0',
247   PRIMARY KEY (`id`),
248   UNIQUE KEY `prefix` (`trans_type`, `prefix`)
249 ) ENGINE=InnoDB;
250
251 INSERT INTO `0_reflines` (`trans_type`, `pattern`, `default`) SELECT `type_id`, `next_reference`, 1 FROM `0_sys_types`;
252
253 DROP TABLE `0_sys_types`;
254
255 ALTER TABLE `0_cust_branch` DROP KEY `branch_code`;
256 ALTER TABLE `0_supp_trans` DROP KEY `SupplierID_2`;
257 ALTER TABLE `0_supp_trans` DROP KEY `type`;
258
259 # new fixed assets module
260 ALTER TABLE `0_locations` ADD COLUMN `fixed_asset` tinyint(1) NOT NULL DEFAULT '0' after `contact`;
261
262 DROP TABLE IF EXISTS `0_stock_fa_class`;
263 CREATE TABLE `0_stock_fa_class` (
264   `fa_class_id` varchar(20) NOT NULL DEFAULT '',
265   `parent_id` varchar(20) NOT NULL DEFAULT '',
266   `description` varchar(200) NOT NULL DEFAULT '',
267   `long_description` tinytext NOT NULL,
268   `depreciation_rate` double NOT NULL DEFAULT '0',
269   `inactive` tinyint(1) NOT NULL DEFAULT '0',
270   PRIMARY KEY (`fa_class_id`)
271 ) ENGINE=InnoDB;
272
273 ALTER TABLE `0_stock_master` ADD COLUMN `depreciation_method` char(1) NOT NULL DEFAULT 'S' AFTER `editable`;
274 ALTER TABLE `0_stock_master` ADD COLUMN `depreciation_rate` double NOT NULL DEFAULT '0' AFTER `depreciation_method`;
275 ALTER TABLE `0_stock_master` ADD COLUMN `depreciation_factor` double NOT NULL DEFAULT '0' AFTER `depreciation_rate`;
276 ALTER TABLE `0_stock_master` ADD COLUMN `depreciation_start` date NOT NULL DEFAULT '0000-00-00' AFTER `depreciation_factor`;
277 ALTER TABLE `0_stock_master` ADD COLUMN `depreciation_date` date NOT NULL DEFAULT '0000-00-00' AFTER `depreciation_start`;
278 ALTER TABLE `0_stock_master` ADD COLUMN `fa_class_id` varchar(20) NOT NULL DEFAULT '' AFTER `depreciation_date`;
279 ALTER TABLE `0_stock_master` CHANGE `actual_cost` `purchase_cost` double NOT NULL default 0;
280
281 INSERT IGNORE INTO `0_sys_prefs` VALUES
282         ('default_loss_on_asset_disposal_act', 'glsetup.items', 'varchar', '15', '5660'),
283         ('depreciation_period', 'glsetup.company', 'tinyint', '1', '1'),
284         ('use_manufacturing','setup.company', 'tinyint', 1, '1'),
285         ('use_fixed_assets','setup.company', 'tinyint', 1, '1');
286
287 # manufacturing rewrite
288 ALTER TABLE `0_wo_issue_items` ADD COLUMN  `unit_cost` double NOT NULL default '0' AFTER `qty_issued`;
289 ALTER TABLE `0_wo_requirements` CHANGE COLUMN `std_cost` `unit_cost` double NOT NULL default '0';
290
291 ALTER TABLE `0_stock_master` DROP COLUMN `last_cost`;
292 UPDATE `0_stock_master` SET `material_cost`=`material_cost`+`labour_cost`+`overhead_cost`;
293
294 ALTER TABLE `0_stock_master` CHANGE COLUMN `assembly_account` `wip_account` VARCHAR(15) NOT NULL default '';
295 ALTER TABLE `0_stock_category` CHANGE COLUMN `dflt_assembly_act` `dflt_wip_act` VARCHAR(15) NOT NULL default '';
296 UPDATE `0_sys_prefs` SET `name`='default_wip_act' WHERE `name`='default_assembly_act';
297
298 UPDATE `0_wo_issue_items` i, `0_stock_moves` m
299         SET i.unit_cost=m.standard_cost
300         WHERE i.unit_cost=0 AND i.stock_id=m.stock_id AND m.trans_no=i.issue_id AND m.`type`=28 AND m.qty=-i.qty_issued;
301
302 UPDATE `0_wo_requirements` r, `0_stock_moves` m
303         SET r.unit_cost=m.standard_cost
304         WHERE r.unit_cost=0 AND r.stock_id=m.stock_id AND m.trans_no=r.workorder_id AND m.`type`=26 AND m.qty=-r.units_issued;
305
306 UPDATE `0_bank_trans` SET person_id=trans_no WHERE person_type_id=26;
307
308 ALTER TABLE `0_budget_trans` CHANGE `counter` `id` int(11) NOT NULL AUTO_INCREMENT;
309 ALTER TABLE `0_sys_prefs` CHANGE `value` `value` text NOT NULL default '';
310
311 ALTER TABLE `0_debtor_trans`
312         CHANGE `debtor_no` `debtor_no` int(11) unsigned NOT NULL,
313         DROP PRIMARY KEY,
314         ADD PRIMARY KEY (`type`,`trans_no`,`debtor_no`);
315         
316 ALTER TABLE `0_supp_trans`
317         CHANGE `supplier_id` `supplier_id` int(11) unsigned NOT NULL,
318         DROP PRIMARY KEY,
319         ADD PRIMARY KEY (`type`,`trans_no`,`supplier_id`);