CREATE TABLE IF NOT EXISTS `cash_voucher` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `company` mediumint(8) unsigned NOT NULL,
  `origin` varchar(10) NOT NULL,
  `date` date NOT NULL COMMENT '凭证日期',
  `type` char(3) NOT NULL COMMENT '凭证字',
  `number` smallint(5) unsigned NOT NULL COMMENT '凭证号',
  `currency` varchar(5) NOT NULL DEFAULT 'RMB' COMMENT '币别代码',
  `debit` decimal(12,2) NOT NULL COMMENT '借方',
  `credit` decimal(12,2) NOT NULL COMMENT '贷方',
  `preparer` varchar(30) NOT NULL COMMENT '制单',
  `checker` varchar(30) NOT NULL COMMENT '审核',
  `approve` varchar(30) NOT NULL COMMENT '核准',
  `cashier` varchar(30) NOT NULL COMMENT '出纳',
  `handler` varchar(30) NOT NULL COMMENT '经办',
  `settleType` varchar(10) NOT NULL COMMENT '结算方式',
  `settleNo` varchar(10) NOT NULL COMMENT '结算号',
  `explanation` text NOT NULL COMMENT '凭证摘要',
  `reference` varchar(10) NOT NULL COMMENT '参考信息',
  `transDate` date NOT NULL COMMENT '业务日期',
  `transNo` varchar(10) NOT NULL COMMENT '往来业务编号',
  `attachments` tinyint(3) NOT NULL COMMENT '附件数',
  `serialNum` smallint(5) NOT NULL COMMENT '序号',
  `objectName` varchar(10) NOT NULL COMMENT '系统模块',
  `parameter` varchar(10) NOT NULL COMMENT '业务描述',
  `exchangeRate` decimal(12,4) NOT NULL COMMENT '汇率',
  `posted` tinyint(3) NOT NULL COMMENT '过账',
  `internalInd` varchar(10) NOT NULL COMMENT '机制凭证',
  `cashFlow` varchar(10) NOT NULL COMMENT '现金流量',
  `status` varchar(10) NOT NULL DEFAULT 'wait',
  `createdBy` char(30) NOT NULL,
  `createdDate` datetime NOT NULL,
  `editedBy` char(30) NOT NULL,
  `editedDate` datetime NOT NULL,
  `confirmedBy` char(30) NOT NULL,
  `confirmedDate` datetime NOT NULL,
  PRIMARY KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `cash_voucherdetail` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `voucher` mediumint(8) unsigned NOT NULL,
  `trade` varchar(255) NOT NULL,
  `invoice` varchar(255) NOT NULL,
  `account` varchar(255) NOT NULL COMMENT '科目代码',
  `debit` decimal(12,2) NOT NULL COMMENT '借方',
  `credit` decimal(12,2) NOT NULL COMMENT '贷方',
  `quantity` decimal(12,2) NOT NULL COMMENT '数量',
  `measureUnitID` varchar(10) NOT NULL COMMENT '数量单位',
  `unitPrice` decimal(12,2) NOT NULL COMMENT '单价',
  `exchangeRate` decimal(12,4) NOT NULL COMMENT '汇率',
  `entryID`  tinyint(3) NOT NULL COMMENT '分录序号',
  `explanation` text NOT NULL COMMENT '凭证摘要',
  `item` varchar(255) NOT NULL COMMENT '核算项目',
  `createdBy` char(30) NOT NULL,
  `createdDate` datetime NOT NULL,
  `editedBy` char(30) NOT NULL,
  `editedDate` datetime NOT NULL,
  PRIMARY KEY `id` (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `sys_workflowlinkdata` (
  `objectType` varchar(30) NOT NULL,
  `objectID` mediumint(8) unsigned NOT NULL,
  `linkedType` varchar(30) NOT NULL,
  `linkedID` mediumint(8) unsigned NOT NULL,
  `createdBy` varchar(30) NOT NULL,
  `createdDate` datetime NOT NULL,
  UNIQUE KEY `unique` (`objectType`, `objectID`, `linkedType`, `linkedID`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `sys_workflowrelation` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `prev` varchar(30) NOT NULL,
  `next` varchar(30) NOT NULL, 
  `field` varchar(50) NOT NULL,
  `actions` varchar(20) NOT NULL,
  `createdBy` char(30) NOT NULL,
  `createdDate` datetime NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

CREATE TABLE IF NOT EXISTS `sys_workflowrelationlayout` (
  `id` mediumint(8) unsigned NOT NULL AUTO_INCREMENT,
  `prev` varchar(30) NOT NULL,
  `next` varchar(30) NOT NULL,
  `action` varchar(50) NOT NULL,
  `field` varchar(50) NOT NULL,
  `order` smallint(5) unsigned NOT NULL,
  PRIMARY KEY `id` (`id`),
  KEY `prev` (`prev`),
  KEY `next` (`next`),
  KEY `action` (`action`),
  KEY `order` (`order`),
  UNIQUE KEY `unique` (`prev`, `next`, `action`, `field`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

ALTER TABLE `ameba_budget`    CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `ameba_category`  CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `ameba_fee`       CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `ameba_rule`      CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `ameba_sharefee`  CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `ameba_statement` CHANGE `desc` `desc` text NOT NULL;
ALTER TABLE `crm_orderfield`  CHANGE `desc` `desc` text NOT NULL;

ALTER TABLE `cash_invoicedetail`   ADD `itemType` varchar(10) NOT NULL AFTER `item`;
ALTER TABLE `cash_invoicedetail`   ADD `itemID` mediumint(8) unsigned NOT NULL AFTER `itemType`;
ALTER TABLE `cash_mappingrelation` ADD `saleType` varchar(30) NOT NULL AFTER `invoice`;
ALTER TABLE `cash_mappingrelation` ADD UNIQUE KEY (`company`, `type`, `depositor`, `category`, `product`, `invoice`, `saleType`);
ALTER TABLE `cash_trade`           ADD `noInvoice` enum('0', '1') NOT NULL DEFAULT '0' AFTER `currency`;
ALTER TABLE `cash_tradeinvoice`    ADD `createdBy` varchar(30) NOT NULL;
ALTER TABLE `cash_tradeinvoice`    ADD `createdDate` datetime NOT NULL;
ALTER TABLE `sys_user`             ADD `noSalary` enum('0', '1') NOT NULL DEFAULT '0' AFTER `unofficial`;

ALTER TABLE `sys_workflowaction` ADD `type` enum('single', 'batch') NOT NULL DEFAULT 'single' AFTER `name`;
ALTER TABLE `sys_workflowaction` ADD `batchMode` enum('same', 'different') NOT NULL DEFAULT 'different' AFTER `type`;
ALTER TABLE `sys_workflowaction` ADD `linkages` text NOT NULL AFTER `hooks`;
ALTER TABLE `sys_workflowfield`  ADD `readonly` enum('0', '1') NOT NULL DEFAULT '0' AFTER `desc`;
ALTER TABLE `sys_workflowlayout` ADD `readonly` enum('0', '1') NOT NULL DEFAULT '0' AFTER `position`;
ALTER TABLE `sys_workflowfield`  CHANGE `control` `control` varchar(20) NOT NULL;

REPLACE INTO `sys_workflowrelation` (`prev`, `next`, `field`, `actions`) SELECT t1.child, t1.module, t2.field, 'one2one,many2many' FROM `sys_workflow` AS t1 LEFT JOIN `sys_workflowfield` AS t2 ON (t1.module = t2.module) WHERE t1.type = 'flow' AND t1.child != '' AND t2.isForeignKey = '1';
ALTER TABLE `sys_workflowfield` DROP `isForeignKey`;

UPDATE `cash_invoice` SET `type` = 'companyDigitalOrdinary' WHERE `invoiceType` = 'digital';
ALTER TABLE `cash_invoice` DROP `invoiceType`;

UPDATE `cash_mappingrelation` SET `invoice` = 'companyDigitalOrdinary' WHERE `invoiceType` = 'digital';
ALTER TABLE `cash_mappingrelation` DROP `invoiceType`;

UPDATE `sys_workflow` SET `parent` = '', `child` = '' WHERE `type` = 'flow';
UPDATE `sys_workflowaction` SET `open` = 'normal' WHERE `action` = 'view' AND `open` = 'none';
UPDATE `sys_workflowaction` SET `open` = 'none' WHERE `action` IN ('export', 'exporttemplate', 'import');
UPDATE `sys_workflowfield` SET `readonly` = '1' WHERE `field` in ('id', 'parent', 'createdBy', 'createdDate', 'editedBy', 'editedDate', 'deleted');
UPDATE `sys_workflowfield` SET `options` = 'prevModule' WHERE `options` = 'submodule';

UPDATE `sys_config` SET `value` = '{"amount":""}' WHERE `owner` = 'system' AND `app` = 'hr' AND `module` = 'salary' AND `section` = 'exemption' AND `key` = '报销';

REPLACE INTO `sys_workflowlayout` (`module`, `action`, `field`, `order`, `defaultValue`, `layoutRules`) SELECT `module`, 'batchcreate', `field`, `order`, `defaultValue`, `layoutRules` FROM `sys_workflowlayout` WHERE `action` = 'create';
REPLACE INTO `sys_workflowlayout` (`module`, `action`, `field`, `order`, `defaultValue`, `layoutRules`) SELECT `module`, 'batchedit', `field`, `order`, `defaultValue`, `layoutRules` FROM `sys_workflowlayout` WHERE `action` = 'edit';

REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowrelation',  'admin'  FROM `sys_grouppriv` WHERE `module` = 'workflow'       AND `method` = 'setSubModule'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowcondition', 'browse' FROM `sys_grouppriv` WHERE `module` = 'workflowaction' AND `method` = 'setCondition'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowcondition', 'create' FROM `sys_grouppriv` WHERE `module` = 'workflowaction' AND `method` = 'setCondition'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowcondition', 'edit'   FROM `sys_grouppriv` WHERE `module` = 'workflowaction' AND `method` = 'setCondition'; 
REPLACE INTO `sys_grouppriv` (`group`, `module`, `method`) SELECT `group`, 'workflowcondition', 'delete' FROM `sys_grouppriv` WHERE `module` = 'workflowaction' AND `method` = 'setCondition'; 

DELETE FROM `sys_grouppriv` WHERE `module` = 'workflow' AND `method` = 'setSubModule'; 
DELETE FROM `sys_grouppriv` WHERE `module` = 'workflowaction' AND `method` = 'setCondition'; 
