-- DROP TABLE IF EXISTS `cash_kingdeeaccount`;
CREATE TABLE IF NOT EXISTS `cash_kingdeeaccount` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `company` mediumint(8) unsigned NOT NULL,
  `number` varchar(128) NOT NULL,
  `name` varchar(160) NOT NULL,
  `helperCode` varchar(40) NOT NULL,
  `groupID` varchar(255) NOT NULL,
  `dc` varchar(10) NOT NULL,
  `currencyID` varchar(255) NOT NULL,
  `adjustRate` varchar(10) NOT NULL,
  `contact` varchar(10) NOT NULL,
  `quantities` varchar(10) NOT NULL,
  `unitGroupID` varchar(255) NOT NULL,
  `measureUnitID` varchar(255) NOT NULL,
  `isCash` varchar(10) NOT NULL,
  `isBank` varchar(10) NOT NULL,
  `journal` varchar(10) NOT NULL,
  `detailID` varchar(10) NOT NULL,
  `isBalance` varchar(10) NOT NULL,
  `mainItemID` varchar(255) NOT NULL,
  `subItemID` varchar(255) NOT NULL,
  `itemClassNumber` varchar(255) NOT NULL,
  `itemClassName` varchar(255) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `unique` (`company`, `number`, `name`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

-- DROP TABLE IF EXISTS `cash_mappingrelation`;
CREATE TABLE IF NOT EXISTS `cash_mappingrelation` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `company` mediumint(8) unsigned NOT NULL,
  `type` varchar(10) NOT NULL,
  `depositor` mediumint(8) unsigned NOT NULL,
  `category` mediumint(8) unsigned NOT NULL,
  `product` mediumint(8) unsigned NOT NULL,
  `invoice` varchar(50) NOT NULl,
  `invoiceType` varchar(50) NOT NULl,
  `account` varchar(255) NOT NULL,
  `createdBy` varchar(30) NOT NULL,
  `createdDate` datetime NOT NULL,
  `editedBy` varchar(30) NOT NULL,
  `editedDate` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

ALTER TABLE `cash_invoice` ADD `saleType` varchar(30) NOT NULL AFTER `type`;

RENAME TABLE `sys_workflowmenu` TO `sys_workflowlabel`;

ALTER TABLE `sys_workflow` ADD `table` varchar(50) NOT NULL AFTER `module`;
ALTER TABLE `sys_workflow` ADD `buildin` tinyint(1) unsigned NOT NULL AFTER `order`;
ALTER TABLE `sys_workflowaction` ADD `extensionType` varchar(10) NOT NULL DEFAULT 'override' comment 'none | extend | override' AFTER `name`;
ALTER TABLE `sys_workflowaction` ADD `buildin` tinyint(1) unsigned NOT NULL AFTER `order`;
ALTER TABLE `sys_workflowaction` ADD `layout` char(20) NOT NULL AFTER `position`;
ALTER TABLE `sys_workflowfield` ADD `buildin` tinyint(1) unsigned NOT NULL AFTER `order`;
ALTER TABLE `sys_workflowlabel` ADD `code` varchar(30) NOT NULL AFTER `module`;
ALTER TABLE `sys_workflowlabel` ADD `buildin` tinyint(1) unsigned NOT NULL AFTER `order`;

ALTER TABLE `sys_workflowaction` CHANGE `results` `hooks` text NOT NULL;
ALTER TABLE `sys_workflowrule` CHANGE `type` `type` enum('system', 'regex', 'func') NOT NULL DEFAULT 'regex';
ALTER TABLE `sys_workflowversion` CHANGE `menus` `labels` text NOT NULL;

ALTER TABLE `sys_workflow` DROP `deleted`;
ALTER TABLE `sys_workflowaction` DROP `deleted`;
ALTER TABLE `sys_workflowdatasource` DROP `deleted`;
ALTER TABLE `sys_workflowlabel` DROP `deleted`;
ALTER TABLE `sys_workflowrule` DROP `deleted`;

UPDATE `sys_workflowaction` SET `open` = 'none' WHERE `action` = 'delete';

UPDATE `sys_workflowfield` SET `type` = 'varchar', `length` = 30 WHERE `options` = 'user';

UPDATE `sys_workflowlabel` SET `code` = CONCAT('label', `id`) WHERE `code` = '';
UPDATE `sys_workflowlabel` SET `params` = REPLACE(`params`, '\"key\":', '\"field\":');

REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflow', 'browseFlow' FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'browse'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflow', 'browseDB' FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'browse'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowaction', 'view' FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'adminAction'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowfield', 'setExport' FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'adminField'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowlabel', 'sort' FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'adminModuleMenu'; 

UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminAction';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createAction';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editAction';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteAction';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'setCondition'    WHERE `module` = 'workflow' AND `method` = 'adminCondition';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'setVerification' WHERE `module` = 'workflow' AND `method` = 'verification';
UPDATE `sys_grouppriv` SET `module` = 'workflowaction',     `method` = 'setNotice'       WHERE `module` = 'workflow' AND `method` = 'setNotice';

UPDATE `sys_grouppriv` SET `module` = 'workflowdatasource', `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminDatasource';
UPDATE `sys_grouppriv` SET `module` = 'workflowdatasource', `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createDatasource';
UPDATE `sys_grouppriv` SET `module` = 'workflowdatasource', `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteDatasource';
UPDATE `sys_grouppriv` SET `module` = 'workflowdatasource', `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editDatasource';

UPDATE `sys_grouppriv` SET `module` = 'workflowfield',      `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminField';
UPDATE `sys_grouppriv` SET `module` = 'workflowfield',      `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createField';
UPDATE `sys_grouppriv` SET `module` = 'workflowfield',      `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteField';
UPDATE `sys_grouppriv` SET `module` = 'workflowfield',      `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editField';

UPDATE `sys_grouppriv` SET `module` = 'workflowhook',       `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminResult';
UPDATE `sys_grouppriv` SET `module` = 'workflowhook',       `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createResult';
UPDATE `sys_grouppriv` SET `module` = 'workflowhook',       `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteResult';
UPDATE `sys_grouppriv` SET `module` = 'workflowhook',       `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editResult';

UPDATE `sys_grouppriv` SET `module` = 'workflowlabel',      `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminModuleMenu';
UPDATE `sys_grouppriv` SET `module` = 'workflowlabel',      `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createModuleMenu';
UPDATE `sys_grouppriv` SET `module` = 'workflowlabel',      `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteModuleMenu';
UPDATE `sys_grouppriv` SET `module` = 'workflowlabel',      `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editModuleMenu';

UPDATE `sys_grouppriv` SET `module` = 'workflowlayout',     `method` = 'admin'           WHERE `module` = 'workflow' AND `method` = 'adminLayout';

UPDATE `sys_grouppriv` SET `module` = 'workflowrule',       `method` = 'browse'          WHERE `module` = 'workflow' AND `method` = 'adminRule';
UPDATE `sys_grouppriv` SET `module` = 'workflowrule',       `method` = 'create'          WHERE `module` = 'workflow' AND `method` = 'createRule';
UPDATE `sys_grouppriv` SET `module` = 'workflowrule',       `method` = 'delete'          WHERE `module` = 'workflow' AND `method` = 'deleteRule';
UPDATE `sys_grouppriv` SET `module` = 'workflowrule',       `method` = 'edit'            WHERE `module` = 'workflow' AND `method` = 'editRule';
UPDATE `sys_grouppriv` SET `module` = 'workflowrule',       `method` = 'view'            WHERE `module` = 'workflow' AND `method` = 'viewRule';

DELETE FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'browse';
DELETE FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'browseTable';
