ALTER TABLE `visit` ADD COLUMN `visitNo`  varchar(4) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' AFTER `serviceJobId`;
CREATE INDEX `visitNo` ON `visit`(`visitNo`) USING BTREE ;

DROP TABLE IF EXISTS `visititemasset`;
CREATE TABLE `visititemasset` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`visitId`  int(10) NOT NULL DEFAULT 0 ,
`partInstanceId`  int(10) NOT NULL DEFAULT 0 ,
`comments`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' ,
`active`  tinyint(1) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`createdById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`updated`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
`updatedById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`),
INDEX `visitId` (`visitId`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=Compact;

DROP TABLE IF EXISTS `visititemextra`;
CREATE TABLE `visititemextra` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`visitId`  int(10) NOT NULL DEFAULT 0 ,
`startTime`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`endTime`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`description`  varchar(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`rate`  double(10,4) NOT NULL DEFAULT 0.0000 ,
`manHour`  double(6,2) NOT NULL DEFAULT 0.00 ,
`totalPrice`  double(14,4) NOT NULL DEFAULT 0.0000 ,
`active`  tinyint(1) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`createdById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`updated`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
`updatedById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`),
INDEX `startTime` (`startTime`) USING BTREE ,
INDEX `endTime` (`endTime`) USING BTREE ,
INDEX `description` (`description`) USING BTREE ,
INDEX `rate` (`rate`) USING BTREE ,
INDEX `manHour` (`manHour`) USING BTREE ,
INDEX `totalPrice` (`totalPrice`) USING BTREE ,
INDEX `visitId` (`visitId`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=Compact;

DROP TABLE IF EXISTS `visititemparts`;
CREATE TABLE `visititemparts` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`visitId`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`productId`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`usedForPartInstanceId`  int(10) NULL DEFAULT 0 ,
`partInstanceIds`  varchar(50) CHARACTER SET utf8 COLLATE utf8_general_ci NULL DEFAULT '' ,
`qty`  double(14,4) UNSIGNED NOT NULL DEFAULT 0.0000 ,
`unitPrice`  double(10,4) UNSIGNED NOT NULL DEFAULT 0.0000 ,
`totalPrice`  double(14,4) UNSIGNED NOT NULL DEFAULT 0.0000 ,
`active`  tinyint(1) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`createdById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`updated`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
`updatedById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`),
INDEX `productId` (`productId`) USING BTREE ,
INDEX `unitPrice` (`unitPrice`) USING BTREE ,
INDEX `totalPrice` (`totalPrice`) USING BTREE ,
INDEX `qty` (`qty`) USING BTREE ,
INDEX `visitId` (`visitId`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=Compact;

DROP TABLE `visititem`;
DROP TABLE `usedpart`;

ALTER TABLE `partinstance` MODIFY COLUMN `warrantyDate`  date NULL DEFAULT NULL AFTER `model`;

DROP TABLE IF EXISTS `jobdocument`;
CREATE TABLE `jobdocument` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`serviceJobId`  int(10) UNSIGNED NULL DEFAULT NULL ,
`documentAssetId`  varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`active`  tinyint(1) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`createdById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`updated`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
`updatedById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`),
INDEX `active` (`active`) USING BTREE ,
INDEX `created` (`created`) USING BTREE ,
INDEX `createdById` (`createdById`) USING BTREE ,
INDEX `updated` (`updated`) USING BTREE ,
INDEX `updatedById` (`updatedById`) USING BTREE ,
INDEX `documentAssetId` (`documentAssetId`) USING BTREE ,
INDEX `serviceJobId` (`serviceJobId`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=Compact;

DROP TABLE IF EXISTS `visititemdocument`;
CREATE TABLE `visititemdocument` (
`id`  int(10) UNSIGNED NOT NULL AUTO_INCREMENT ,
`visitId`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`documentAssetId`  varchar(32) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL DEFAULT '' ,
`active`  tinyint(1) NOT NULL DEFAULT 1 ,
`created`  datetime NOT NULL DEFAULT '0001-01-01 00:00:00' ,
`createdById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
`updated`  timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP ,
`updatedById`  int(10) UNSIGNED NOT NULL DEFAULT 0 ,
PRIMARY KEY (`id`),
INDEX `active` (`active`) USING BTREE ,
INDEX `created` (`created`) USING BTREE ,
INDEX `createdById` (`createdById`) USING BTREE ,
INDEX `updated` (`updated`) USING BTREE ,
INDEX `updatedById` (`updatedById`) USING BTREE ,
INDEX `documentAssetId` (`documentAssetId`) USING BTREE ,
INDEX `visitId` (`visitId`) USING BTREE
)
ENGINE=InnoDB
DEFAULT CHARACTER SET=utf8 COLLATE=utf8_general_ci
ROW_FORMAT=Compact;

ALTER TABLE `visit` ADD COLUMN `orderId`  int(10) NULL DEFAULT 0 AFTER `vanId`;
ALTER TABLE `visit` ADD COLUMN `orderItemId`  int(10) NULL DEFAULT 0 AFTER `orderId`;

ALTER TABLE `visititemparts` ADD COLUMN `totalMargin`  double(14,4) NOT NULL DEFAULT 0.0000 AFTER `totalPrice`;
CREATE INDEX `totalMargin` ON `visititemparts`(`totalMargin`) USING BTREE ;
ALTER TABLE `visititemextra` ADD COLUMN `hours`  double(6,2) UNSIGNED NOT NULL DEFAULT 0.00 AFTER `description`;
CREATE INDEX `hours` ON `visititemextra`(`hours`) USING BTREE ;
ALTER TABLE `orderitem` ADD COLUMN `visitId` int(10) UNSIGNED NULL AFTER `productId`;
