
CREATE TABLE `tbl_user_statusreport` (
  `RecNo` int(11) NOT NULL AUTO_INCREMENT,
  `UserID` varchar(100) DEFAULT 'NULL',
  `UserName` varchar(300) DEFAULT 'NULL',
  `Role` varchar(100) DEFAULT 'NULL',
  `Dept` varchar(100) DEFAULT 'NULL',
  `Status` varchar(200) DEFAULT 'NULL',
  `HMIID` varchar(20) DEFAULT '0',
  PRIMARY KEY (`RecNo`)
) ENGINE=InnoDB AUTO_INCREMENT=22 DEFAULT CHARSET=utf8;

/*Data for the table `tbl_user_statusreport` */

insert  into `tbl_user_statusreport`(`RecNo`,`UserID`,`UserName`,`Role`,`Dept`,`Status`,`HMIID`) values 
(1,'Tantra','Tantrasoft','SuperAdmin','IT','Active','192.168.1.157'),
(2,'a','Abhishek','Admin','QC','Active','192.168.1.157'),
(3,'11','Mangesh','Admin','QC','Active','192.168.1.157'),
(4,'12','Sail','Admin','QC','Active','192.168.1.157'),
(5,'13','Devendra','Admin','QC','Active','192.168.1.157'),
(6,'14','Prakash','Admin','QC','Active','192.168.1.157'),
(7,'D','Dharmesh sakpal','Admin','QC','Active','192.168.1.157'),
(8,'1','Prachiti Shah','Supervisor','QC','Active','192.168.1.157'),
(9,'45','shruti','Admin','QC','Active','192.168.1.157'),
(10,'21','shruti','Supervisor','QC','Active','192.168.1.157'),
(11,'Tantra','Tantrasoft','SuperAdmin','IT','Active','192.168.1.129'),
(12,'a','Abhishek','Admin','QC','Active','192.168.1.129'),
(13,'11','Mangesh','Admin','QC','Active','192.168.1.129'),
(14,'12','Sail','Admin','QC','Active','192.168.1.129'),
(15,'13','Devendra','Admin','QC','Active','192.168.1.129'),
(16,'14','Prakash','Admin','QC','Active','192.168.1.129'),
(17,'D','Dharmesh sakpal','Admin','QC','Active','192.168.1.129'),
(18,'1','Prachiti Shah','Supervisor','QC','Active','192.168.1.129'),
(19,'45','shruti','Admin','QC','Active','192.168.1.129'),
(20,'21','shruti','Supervisor','QC','Active','192.168.1.129'),
(21,'021','Tejal','Supervisor','QC1','Active','192.168.1.129');

/*Table structure for table `tbl_users` */


CREATE TABLE `tbl_users` (
  `Id` int(11) NOT NULL AUTO_INCREMENT,
  `UserID` varchar(12) NOT NULL DEFAULT 'NULL',
  `UserName` varchar(100) NOT NULL DEFAULT 'NULL',
  `Pwd` varchar(50) DEFAULT 'NULL',
  `userType` int(11) NOT NULL DEFAULT '0',
  `PwdChgDate` date NOT NULL,
  `PwdExpStauts` bit(1) NOT NULL DEFAULT b'0' COMMENT '0=Active',
  `Status` int(11) NOT NULL DEFAULT '0' COMMENT '0 : Enabled || 1: Temp Disabled || 2:Permanent Disable || 4: Auto Disable || 6: User Locked',
  `Reason` varchar(50) DEFAULT 'NULL',
  `PwdChg` bit(1) NOT NULL DEFAULT b'0' COMMENT 'By defaullt value is 1,1 means this password is set by admin.0 means set by user',
  `isadmin` tinyint(1) NOT NULL DEFAULT '0',
  `LastLoginDt` date NOT NULL,
  `UserInitials` varchar(30) NOT NULL DEFAULT 'NULL',
  `loginatmpt` int(11) NOT NULL DEFAULT '0',
  `suspensionPeriod` timestamp NULL DEFAULT NULL,
  `autoEnbl` int(11) NOT NULL DEFAULT '0',
  `active` tinyint(1) NOT NULL DEFAULT '0' COMMENT 'To check logged in already or not',
  `realPassword` varchar(100) DEFAULT 'NULL',
  `lstActvtyTime` datetime DEFAULT NULL,
  `Department` varchar(250) NOT NULL DEFAULT 'NULL',
  `Role` varchar(250) NOT NULL DEFAULT 'NULL',
  `HostName` varchar(100) DEFAULT 'NULL',
  `locked` bit(1) NOT NULL DEFAULT b'0',
  `editCounter` int(11) NOT NULL DEFAULT '0',
  `loginCounter` int(11) DEFAULT '0',
  `source` varchar(15) NOT NULL DEFAULT 'NULL',
  `PREV_STATUS` int(10) DEFAULT '0' COMMENT 'This will store the previous status when user is autodisable and he made unsuccessfull attempts',
  `Domain` varchar(30) DEFAULT 'NULL',
  `failedAtmpTime` datetime DEFAULT NULL,
  PRIMARY KEY (`Id`),
  UNIQUE KEY `userid` (`UserID`)
) ENGINE=InnoDB AUTO_INCREMENT=213 DEFAULT CHARSET=latin1;

/*Data for the table `tbl_users` */

insert  into `tbl_users`(`Id`,`UserID`,`UserName`,`Pwd`,`userType`,`PwdChgDate`,`PwdExpStauts`,`Status`,`Reason`,`PwdChg`,`isadmin`,`LastLoginDt`,`UserInitials`,`loginatmpt`,`suspensionPeriod`,`autoEnbl`,`active`,`realPassword`,`lstActvtyTime`,`Department`,`Role`,`HostName`,`locked`,`editCounter`,`loginCounter`,`source`,`PREV_STATUS`,`Domain`,`failedAtmpTime`) values 
(200,'Tantra','Tantrasoft',NULL,1,'2023-09-05','\0',0,'0','',0,'2025-10-06','Tantra',0,NULL,0,1,'1','2025-10-04 14:17:51','IT','SuperAdmin','192.168.1.157','\0',0,0,'SOFTWARE',0,'0','2025-09-15 11:10:09'),
(202,'a','Abhishek','6dcd4ce23d88e2ee9568ba546c007c63d9131c1b',2,'2025-09-10','\0',0,'0','\0',0,'2025-10-03','Abhishek',0,NULL,0,0,'a','2025-10-03 11:14:32','QC','Admin','0','\0',0,0,'0',0,'0','2025-09-10 12:27:49'),
(203,'11','Mangesh','17ba0791499db908433b80f37c5fbc89b870084b',2,'2025-09-10','\0',0,'0','\0',0,'2025-09-27','Mangesh',1,NULL,0,0,'11','2025-09-27 09:57:06','QC','Admin','0','\0',0,0,'0',0,'0','2025-10-03 15:39:07'),
(204,'12','Sail','7b52009b64fd0a2a49e6d8a939753077792b0554',2,'2025-09-10','\0',0,'0','\0',0,'2025-10-06','Sail',0,NULL,0,1,'12','2025-10-04 16:56:18','QC','Admin','192.168.1.105','\0',0,0,'SOFTWARE',0,'0','2025-09-22 14:33:39'),
(205,'13','Devendra','da4b9237bacccdf19c0760cab7aec4a8359010b0',2,'2025-09-10','\0',0,'0','\0',0,'2025-10-06','Dev@123',0,NULL,0,0,'2','2025-10-06 10:04:37','QC','Admin','192.168.1.108','\0',0,0,'0',0,'0','2025-10-06 10:04:14'),
(206,'14','Prakash','77de68daecd823babbb58edb1c8e14d7106e83bb',2,'2025-09-10','\0',0,'0','\0',0,'2025-10-06','P@112',0,NULL,0,0,'3','2025-10-06 11:46:39','QC','Admin','192.168.1.108','\0',0,0,'0',0,'0','2025-09-13 09:15:08'),
(207,'D','Dharmesh sakpal','356a192b7913b04c54574d18c28d46e6395428ab',2,'2025-09-10','\0',0,'0','\0',0,'2025-10-06','D@132',0,NULL,0,0,'1','2025-10-06 11:48:33','QC','Admin','','\0',0,782,'',0,'0','1970-01-01 05:30:00'),
(208,'1','Prachiti Shah','356a192b7913b04c54574d18c28d46e6395428ab',2,'2025-09-15','\0',0,'0','\0',0,'2025-10-06','Prachiti Shah',0,NULL,0,1,'1','2025-10-06 11:37:33','QC','Supervisor','192.168.1.166','\0',0,0,'SOFTWARE',0,'0','2025-09-23 08:54:58'),
(209,'45','shruti','80e28a51cbc26fa4bd34938c5e593b36146f5e0c',2,'2025-09-15','\0',0,'0','\0',0,'2025-10-06','s',0,NULL,0,1,'54','2025-10-06 11:36:48','QC','Admin','192.168.1.157','\0',0,0,'SOFTWARE',0,'0','2025-09-16 14:30:12'),
(210,'21','shruti','12c6fc06c99a462375eeb3f43dfd832b08ca9e17',2,'2025-09-17','\0',0,'0','\0',0,'2025-10-06','s',0,NULL,0,0,'22','2025-10-06 11:50:07','QC','Supervisor','','\0',4,0,'',0,'0','1970-01-01 05:30:00'),
(211,'021','Tejal','632667547e7cd3e0466547863e1207a8c0c0c549',2,'2025-09-26','\0',0,'0','\0',0,'2025-10-03','TW',0,NULL,0,0,'31','2025-10-03 18:12:31','QC1','Supervisor','0','\0',0,0,'0',0,'0','2025-10-03 15:42:04'),
(212,'123','123','9b5775a8efab7bfc65fc0121c67cb85cd4ec4b41',2,'2025-10-03','\0',0,'0','',0,'2025-10-03','123',2,NULL,0,0,'TANTRA@123','1970-01-01 05:30:00','QC','role11','0','\0',0,0,'0',0,'0','2025-10-03 10:52:10');

/*Table structure for table `tbl_vernier` */


CREATE TABLE `tbl_vernier` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `VernierNo` varchar(50) DEFAULT 'NULL',
  `VernierID` varchar(50) DEFAULT 'NULL',
  `Model` varchar(50) DEFAULT 'NULL',
  `leastCount` decimal(10,5) DEFAULT '0.00000',
  `RangeUnit` varchar(10) DEFAULT 'NULL',
  `RangeMinVal` decimal(10,5) DEFAULT '0.00000',
  `RangeMaxVal` decimal(10,5) DEFAULT '0.00000',
  `CalDueDT` date DEFAULT NULL,
  `CalDuration` int(11) DEFAULT '0',
  `Caldates` varchar(50) DEFAULT 'NULL',
  `CalibStoreType` int(11) NOT NULL DEFAULT '0',
  `CalReminder` int(11) NOT NULL DEFAULT '0',
  `Ver_Dept` varchar(50) NOT NULL DEFAULT 'NULL',
  `locked` int(1) NOT NULL DEFAULT '0',
  `editCounter` int(11) NOT NULL DEFAULT '0',
  `Ver_IsActivate` int(1) DEFAULT '0',
  `ver_DP` int(2) DEFAULT '0',
  `userID` varchar(200) DEFAULT 'NULL',
  `Ver_IsApproved` int(1) DEFAULT '0',
  `Ver_ApprovedBy` varchar(200) DEFAULT 'NULL',
  `Ver_IsCalib` tinyint(1) DEFAULT '0',
  `Ver_IsNew` int(1) DEFAULT '0' COMMENT '0:No,1:Yes',
  `Make` varchar(100) DEFAULT 'NULL',
  PRIMARY KEY (`id`),
  UNIQUE KEY `VernierNo` (`VernierNo`)
) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=latin1 COMMENT='Not Using this table-Will Delete';

/*Data for the table `tbl_vernier` */

insert  into `tbl_vernier`(`id`,`VernierNo`,`VernierID`,`Model`,`leastCount`,`RangeUnit`,`RangeMinVal`,`RangeMaxVal`,`CalDueDT`,`CalDuration`,`Caldates`,`CalibStoreType`,`CalReminder`,`Ver_Dept`,`locked`,`editCounter`,`Ver_IsActivate`,`ver_DP`,`userID`,`Ver_IsApproved`,`Ver_ApprovedBy`,`Ver_IsCalib`,`Ver_IsNew`,`Make`) values 
(1,'12422','Ver_23','3321',0.02000,'mm',0.02000,320.00000,'2025-10-01',0,'1',0,5,'NA',0,0,1,2,'11',1,'12',1,0,'Baker'),
(2,'VR1009','VR01','VER_BK',0.01000,'mm',0.01000,99.99000,'2025-09-30',20,'',1,5,'NA',0,0,1,2,'14',1,'12',1,0,'Baker'),
(3,'10095WER','VER02','IP 67',0.01000,'mm',0.01000,99.99000,'2025-10-26',25,'',1,5,'NA',0,0,1,2,'14',1,'13',1,0,'Baker'),
(4,'003','V001','ABCSD',0.01000,'mm',0.01000,99.90000,'2025-10-12',8,'',1,7,'NA',0,0,1,2,'021',1,'Tantra',1,0,'Baker'),
(5,'12345','vernier05','erty',0.01000,'mm',0.01000,200.00000,'2025-09-27',8,'',1,4,'NA',0,0,1,2,'45',1,'12',1,1,'Baker');

/*Table structure for table `tbl_vernier_blocks` */


CREATE TABLE `tbl_vernier_blocks` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `ver_ID` varchar(50) NOT NULL DEFAULT 'NULL',
  `Ver_StdBlock` decimal(10,5) NOT NULL DEFAULT '0.00000',
  `Ver_NegTol` decimal(10,5) NOT NULL DEFAULT '0.00000',
  `Ver_PosTol` decimal(10,5) NOT NULL DEFAULT '0.00000',
  `Ver_blnPeriodic` int(1) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=14 DEFAULT CHARSET=latin1;

/*Data for the table `tbl_vernier_blocks` */

insert  into `tbl_vernier_blocks`(`id`,`ver_ID`,`Ver_StdBlock`,`Ver_NegTol`,`Ver_PosTol`,`Ver_blnPeriodic`) values 
(1,'Ver_23',0.03000,0.02000,0.04000,1),
(2,'Ver_23',100.00000,99.94000,100.07000,1),
(3,'Ver_23',150.00000,149.95000,150.06000,1),
(4,'VR01',2.00000,1.50000,2.50000,1),
(5,'VR01',10.00000,9.00000,11.00000,1),
(6,'VER02',5.00000,4.00000,6.00000,1),
(7,'VER02',10.00000,9.00000,11.00000,1),
(8,'V001',0.30000,0.10000,0.40000,1),
(9,'V001',65.00000,60.00000,68.00000,1),
(10,'V001',15.00000,14.00000,16.00000,1),
(11,'vernier05',0.03000,0.01000,0.05000,1),
(12,'vernier05',50.00000,25.00000,90.00000,1),
(13,'vernier05',150.00000,50.00000,200.00000,1);

/*Table structure for table `tbl_vernier_print` */


CREATE TABLE `tbl_vernier_print` (
  `RecNo` int(11) NOT NULL AUTO_INCREMENT,
  `RepSrNo` int(11) NOT NULL,
  `PrintNo` int(11) NOT NULL,
  `Reason` varchar(300) NOT NULL DEFAULT 'NULL',
  `Title` varchar(20) NOT NULL DEFAULT 'NULL',
  `UserID` varchar(50) NOT NULL DEFAULT 'NULL',
  `UserName` varchar(100) NOT NULL DEFAULT 'NULL',
  `Print_Dt` date NOT NULL,
  `Print_Tm` time NOT NULL,
  PRIMARY KEY (`RecNo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `tbl_vernier_print` */

/*Table structure for table `tbl_versionhistory` */


CREATE TABLE `tbl_versionhistory` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `A` tinyint(4) DEFAULT NULL COMMENT 'Major',
  `B` tinyint(4) DEFAULT NULL COMMENT 'Minor',
  `C` tinyint(4) DEFAULT NULL COMMENT 'Build',
  `D` smallint(6) DEFAULT NULL COMMENT 'All Bugs from client',
  `E` smallint(6) DEFAULT NULL COMMENT 'All Bugs from tester',
  `F` varchar(2000) DEFAULT 'NULL' COMMENT 'Description',
  `DeveloperName` varchar(200) DEFAULT 'NULL',
  `DateTime` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `tbl_versionhistory` */

/*Table structure for table `test` */


CREATE TABLE `test` (
  `id` int(10) NOT NULL AUTO_INCREMENT,
  `str` varchar(5000) DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*Data for the table `test` */

/*!50106 set global event_scheduler = 1*/;

/* Event structure for event `ArchiveCapsuleContent1` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleContent1`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleContent1` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master9_archived','tbl_cap_detail9_archived','tbl_cap_master9','tbl_cap_detail9');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleContent2` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleContent2`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleContent2` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master10_archived','tbl_cap_detail10_archived','tbl_cap_master10','tbl_cap_detail10');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleDiameter` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleDiameter`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleDiameter` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master4_archived','tbl_cap_detail4_archived','tbl_cap_master4','tbl_cap_detail4');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleDifferntial` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleDifferntial`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleDifferntial` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master3_archived','tbl_cap_detail3_archived','tbl_cap_master3','tbl_cap_detail3');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleDT` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleDT`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleDT` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_cap_master6_archived','tbl_cap_detail6_archived','tbl_cap_master6','tbl_cap_detail6');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleGroup` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleGroup`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleGroup` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master2_archived','tbl_cap_detail2_archived','tbl_cap_master2','tbl_cap_detail2');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleIndividual` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleIndividual`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleIndividual` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master1_archived','tbl_cap_detail1_archived','tbl_cap_master1','tbl_cap_detail1');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleLength` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleLength`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleLength` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master5_archived','tbl_cap_detail5_archived','tbl_cap_master5','tbl_cap_detail5');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveCapsuleWidth` */

/*!50106 DROP EVENT IF EXISTS `ArchiveCapsuleWidth`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveCapsuleWidth` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_cap_master4_archived','tbl_cap_detail4_archived','tbl_cap_master4','tbl_cap_detail4');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletDiameter` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletDiameter`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletDiameter` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master6_archived','tbl_tab_detail6_archived','tbl_tab_master6','tbl_tab_detail6');		
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletDT` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletDT`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletDT` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_tab_master13_archived','tbl_tab_detail13_archived','tbl_tab_master13','tbl_tab_detail13');	
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletFriability` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletFriability`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletFriability` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_tab_friability_master_archived','tbl_tab_friability_detail_archived','tbl_tab_friability_master','tbl_tab_friability_detail');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletGroup` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletGroup`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletGroup` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master2_archived','tbl_tab_detail2_archived','tbl_tab_master2','tbl_tab_detail2');		
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletHardness` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletHardness`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletHardness` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master7_archived','tbl_tab_detail7_archived','tbl_tab_master7','tbl_tab_detail7');		
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletIndividual` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletIndividual`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletIndividual` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_tab_master1_archived','tbl_tab_detail1_archived','tbl_tab_master1','tbl_tab_detail1');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletIndLayer1` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletIndLayer1`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletIndLayer1` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_tab_master9_archived','tbl_tab_detail9_archived','tbl_tab_master9','tbl_tab_detail9');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletLength` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletLength`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletLength` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master5_archived','tbl_tab_detail5_archived','tbl_tab_master5','tbl_tab_detail5');		
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletLod` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletLod`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletLod` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
 CALL ArchivedTables('tbl_lodmaster_archived','NA','tbl_lodmaster','NA');
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletThickness` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletThickness`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletThickness` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master3_archived','tbl_tab_detail3_archived','tbl_tab_master3','tbl_tab_detail3');		
END */$$
DELIMITER ;

/* Event structure for event `ArchiveTabletWidth` */

/*!50106 DROP EVENT IF EXISTS `ArchiveTabletWidth`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ArchiveTabletWidth` ON SCHEDULE EVERY 30 MINUTE STARTS '2019-10-21 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL ArchivedTables('tbl_tab_master4_archived','tbl_tab_detail4_archived','tbl_tab_master4','tbl_tab_detail4');		
END */$$
DELIMITER ;

/* Event structure for event `AutoDisableUser` */

/*!50106 DROP EVENT IF EXISTS `AutoDisableUser`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `AutoDisableUser` ON SCHEDULE EVERY 1 SECOND STARTS '2017-04-22 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
	    UPDATE `tbl_users` SET `Status`= 0, `Reason`='0' WHERE
	    DATEDIFF(CURDATE(),`LastLoginDt`) > 
		(
			SELECT `tbl_config_AutoDisablePeriod` FROM `tbl_setallparameter`
		)
	    AND `Status` <> 4 AND `Status` = 0  AND `Role` <> 'Admin';
	END */$$
DELIMITER ;

/* Event structure for event `Manage_Edit_Status` */

/*!50106 DROP EVENT IF EXISTS `Manage_Edit_Status`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `Manage_Edit_Status` ON SCHEDULE EVERY 5 SECOND STARTS '2017-10-23 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL edit_status_counter('tbl_role');
CALL edit_status_counter('tbl_users');
CALL edit_status_counter('tbl_balance');
CALL edit_status_counter('tbl_vernier');
CALL edit_status_counter('tbl_calibrationbox');
CALL edit_status_counter('tbl_product_master');
CALL edit_status_counter('tbl_cubical');
CALL edit_status_counter('tbl_otherequipment');
CALL edit_status_counter('tbl_machine');
END */$$
DELIMITER ;

/* Event structure for event `ReleaseSuspension` */

/*!50106 DROP EVENT IF EXISTS `ReleaseSuspension`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ReleaseSuspension` ON SCHEDULE EVERY 1 SECOND STARTS '2017-04-22 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
CALL user_suspensionPeriod_reset();
END */$$
DELIMITER ;

/* Event structure for event `ReleaseUser` */

/*!50106 DROP EVENT IF EXISTS `ReleaseUser`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ReleaseUser` ON SCHEDULE EVERY 5 SECOND STARTS '2017-10-23 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO /* uncomment the example below you want to use */
	-- scheduleexample 1: run once
	   --  AT 'YYYY-MM-DD HH:MM.SS'/CURRENT_TIMESTAMP { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] }
	-- scheduleexample 2: run at intervals forever after creation
	   -- EVERY 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...]
	-- scheduleexample 3: specified start time, end time and interval for execution
	   /*EVERY 1  [HOUR|MONTH|WEEK|DAY|MINUTE|...]
	   STARTS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1[HOUR|MONTH|WEEK|DAY|MINUTE|...] }
	   ENDS CURRENT_TIMESTAMP/'YYYY-MM-DD HH:MM.SS' { + INTERVAL 1 [HOUR|MONTH|WEEK|DAY|MINUTE|...] } */
/*[ON COMPLETION [NOT] PRESERVE]
[ENABLE | DISABLE]
[COMMENT 'comment']*/
	    CALL user_counter_reset() */$$
DELIMITER ;

/* Event structure for event `ResetLoginAttempt` */

/*!50106 DROP EVENT IF EXISTS `ResetLoginAttempt`*/;

DELIMITER $$

/*!50106 CREATE DEFINER=`TANTRASOFT`@`%` EVENT `ResetLoginAttempt` ON SCHEDULE EVERY 5 SECOND STARTS '2018-05-23 00:00:00' ON COMPLETION NOT PRESERVE ENABLE DO BEGIN
	/*UPDATE tbl_users SET loginatmpt=0 WHERE loginatmpt>0 AND failedAtmpTime<DATE_ADD((SELECT failedAtmpTime FROM (SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME='tbl_users' LIMIT 1) AS tbl_users WHERE loginatmpt>0 LIMIT 1), INTERVAL 1 HOUR);*/
	UPDATE tbl_users SET loginatmpt=0 WHERE loginatmpt>0 AND failedAtmpTime <> '0000-00-00 00:00:00' AND DATE_ADD((SELECT failedAtmpTime FROM (SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME='tbl_users' LIMIT 1) AS tbl_users WHERE loginatmpt>0 LIMIT 1), INTERVAL 30 MINUTE)<NOW();
	/*########## NOTES ####################################
	Description: This Event is used to reset 'loginatmpt' column in tbl_users.
	Reson: To AUTOMATICALLY reset 'loginatmpt' after some period.
	Calulation: If(`loginatmpt`>0) THEN get `failedAtmpTime` add INTERVAL in X HOUR check if the current `failedAtmpTime` is less than X
	PLEASE NOTE: SUBQUERY(SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME='tbl_users' LIMIT 1) is deliberately used here because of MySQL Error 1093 - Can't specify target table for update in FROM clause
	#########################################################*/
	/* TEST QUERY:
	update test set data=DATE_ADD((SELECT failedAtmpTime FROM (SELECT TABLE_NAME FROM information_schema.tables WHERE TABLE_NAME='tbl_users' LIMIT 1) AS tbl_users WHERE loginatmpt>0 LIMIT 1), INTERVAL 3 MINUTE) where id=1;*/	
	END */$$
DELIMITER ;

/* Function  structure for function  `errorLog` */

/*!50003 DROP FUNCTION IF EXISTS `errorLog` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `errorLog`(strError VARCHAR(100),strWarning VARCHAR(100)) RETURNS tinyint(1)
BEGIN
	INSERT INTO `tbl_sp_error_log` (`error`,`warning`) VALUES (strError,strWarning);
	RETURN 1;
    END */$$
DELIMITER ;

/* Function  structure for function  `getAverageOfDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getAverageOfDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getAverageOfDataValue`(repSerNoFromMaster INT, weightmentModeNo INT,strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(54) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT AVG(`DataValue1`) INTO @Average FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT AVG(`NetWeight`) INTO @Average FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getAverageOfDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;		
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT AVG(`DataValue1`) INTO @Average FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT AVG(`NetWeight`) INTO @Average FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getAverageOfDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;	
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2_archived` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2_archived` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail2_archived` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT AVG(`DataValue1`) INTO @Average FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT AVG(`NetWeight`) INTO @Average FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT AVG(`DataValue`) INTO @Average FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getAverageOfDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;	
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getAverageOfDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
IF (weightmentModeNo = 1 
OR weightmentModeNo = 2 
OR weightmentModeNo = -1 
OR weightmentModeNo = -2
OR weightmentModeNo = -31
OR weightmentModeNo = -32
OR weightmentModeNo = -33) THEN
	RETURN RHU4(@Average);
ELSE
	RETURN RHU2(@Average);
END IF;
END */$$
DELIMITER ;

/* Function  structure for function  `getLimitText` */

/*!50003 DROP FUNCTION IF EXISTS `getLimitText` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getLimitText`(lowT1 DECIMAL(10,5),lowT2 DECIMAL(10,5),NMT INT,limitOn INT,dpFromMaster INT,Unit VARCHAR(20),limitFor VARCHAR(10)) RETURNS varchar(50) CHARSET latin1
    DETERMINISTIC
BEGIN
SET @limitText = "NA";
SET @T1 = ROUND(lowT1,dpFromMaster);
SET @T2 = ROUND(lowT2,dpFromMaster);
IF (limitOn = 0) THEN /*ACTUAL*/
	IF (lowT1 = 0) THEN
		IF (limitFor="T1") THEN 
			SET @limitText = "NA";
		ELSE
			SET @limitText = CONCAT('None of Capsule by ± ',@T2,' ',Unit);
		END IF;
	ELSE
		IF (limitFor="T1") THEN 
			SET @limitText = CONCAT('NMT ',NMT,' Capsule by ± ',@T1,' ',Unit);
		ELSE
			SET @limitText = CONCAT('None of Capsule by ± ',@T2,' ',Unit);
		END IF;	
	END IF;
ELSE /*PERCENTAGE*/
	IF (lowT1 = 0) THEN
		IF (limitFor="T1") THEN 
			SET @limitText = "NA";
		ELSE
			SET @limitText = CONCAT('None of Capsule by ± ',@T2,' %');
		END IF;
	ELSE
		IF (limitFor="T1") THEN 
			SET @limitText = CONCAT('NMT ',NMT,' Capsule by ± ',@T1,' %');
		ELSE
			SET @limitText = CONCAT('None of Capsule by ± ',@T2,' %');
		END IF;	
	END IF;
END IF;
RETURN @limitText;
END */$$
DELIMITER ;

/* Function  structure for function  `getMaxDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getMaxDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getMaxDataValue`(repSerNoFromMaster INT, weightmentModeNo INT,strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MAX(`DataValue1`) INTO @MaxDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MAX(`NetWeight`) INTO @MaxDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MAX(`DataValue1`) INTO @MaxDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MAX(`NetWeight`) INTO @MaxDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2_archived` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2_archived` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail2_archived` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MAX(`DataValue1`) INTO @MaxDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MAX(`NetWeight`) INTO @MaxDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MAX(`DataValue`) INTO @MaxDataValue FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getMaxDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
RETURN @MaxDataValue;
END */$$
DELIMITER ;

/* Function  structure for function  `getMaxPercentageOfDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getMaxPercentageOfDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getMaxPercentageOfDataValue`(repSerNoFromMaster INT, weightmentModeNo INT, nominalFromMaster DECIMAL(12,5),strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2` WHERE Side = 'NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2` WHERE Side = 'LHS' AND `RepSerNo`=repSerNoFromMaster;	
		ELSE
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2` WHERE Side = 'RHS' AND `RepSerNo`=repSerNoFromMaster;	
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue1`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((MAX(`NetWeight`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 46','None');	
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue1`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((MAX(`NetWeight`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 74','None');	
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side = 'NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side = 'LHS' AND `RepSerNo`=repSerNoFromMaster;	
		ELSE
			SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side = 'RHS' AND `RepSerNo`=repSerNoFromMaster;	
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue1`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((MAX(`NetWeight`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-nominalFromMaster)/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MaxPercentageOfDataValue FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMaxPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 112','None');	
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getMaxPercentageOfDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
RETURN RHU2(@MaxPercentageOfDataValue);
END */$$
DELIMITER ;

/* Function  structure for function  `getMinDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getMinDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getMinDataValue`(repSerNoFromMaster INT, weightmentModeNo INT,strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MIN(`DataValue1`) INTO @MinDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MIN(`NetWeight`) INTO @MinDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MIN(`DataValue1`) INTO @MinDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MIN(`NetWeight`) INTO @MinDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2_archived` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2_archived` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail2_archived` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT MIN(`DataValue1`) INTO @MinDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT MIN(`NetWeight`) INTO @MinDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT MIN(`DataValue`) INTO @MinDataValue FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getMinDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
RETURN @MinDataValue;
END */$$
DELIMITER ;

/* Function  structure for function  `getMinPercentageOfDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getMinPercentageOfDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getMinPercentageOfDataValue`(repSerNoFromMaster INT, weightmentModeNo INT, nominalFromMaster DECIMAL(12,5),strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2` WHERE Side = 'NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2` WHERE  Side = 'LHS' AND `RepSerNo`=repSerNoFromMaster;	
		ELSE
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2` WHERE Side = 'RHS' AND `RepSerNo`=repSerNoFromMaster;	
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue1`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`NetWeight`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue1`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`NetWeight`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side = 'NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE  Side = 'LHS' AND `RepSerNo`=repSerNoFromMaster;	
		ELSE
			SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side = 'RHS' AND `RepSerNo`=repSerNoFromMaster;	
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue1`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`NetWeight`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT ABS(ROUND(CAST((((nominalFromMaster-MIN(`DataValue`))/nominalFromMaster)*100) AS DECIMAL(20,15)),4)) INTO @MinPercentageOfDataValue FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getMinPercentageOfDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getMinPercentageOfDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
RETURN RHU2(@MinPercentageOfDataValue);
END */$$
DELIMITER ;

/* Function  structure for function  `getProductLimits` */

/*!50003 DROP FUNCTION IF EXISTS `getProductLimits` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getProductLimits`(nominal DECIMAL(10,5),average DECIMAL(10,5),lowT1 DECIMAL(10,5),uppT1 DECIMAL(10,5),lowT2 DECIMAL(10,5),uppT2 DECIMAL(10,5),limitOn INT,graphType VARCHAR(20), strLimitType VARCHAR(10)) RETURNS decimal(10,5)
    DETERMINISTIC
BEGIN
IF (strLimitType = "LOWT1") THEN
	IF (graphType = "Standard") THEN
	/*STANDARD*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((nominal - lowT1) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((nominal - (nominal *(lowT1/100))) AS DECIMAL(10,5));
		END IF;
	ELSE
	/*AVERAGE*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((average - lowT1) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((average - (average *(lowT1/100))) AS DECIMAL(10,5));
		END IF;
	END IF;
ELSEIF (strLimitType = "UPPT1") THEN
	IF (graphType = "Standard") THEN
	/*STANDARD*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((nominal + uppT1) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((nominal + (nominal *(uppT1/100))) AS DECIMAL(10,5));
		END IF;
	ELSE
	/*AVERAGE*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((average + uppT1) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((average + (average *(uppT1/100))) AS DECIMAL(10,5));
		END IF;
	END IF;	
ELSEIF (strLimitType = "LOWT2") THEN
	IF (graphType = "Standard") THEN
	/*STANDARD*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((nominal - lowT2) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((nominal - (nominal *(lowT2/100))) AS DECIMAL(10,5));
		END IF;
	ELSE
	/*AVERAGE*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((average - lowT2) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((average - (average *(lowT2/100))) AS DECIMAL(10,5));
		END IF;
	END IF;	
ELSEIF (strLimitType = "UPPT2") THEN
	IF (graphType = "Standard") THEN
	/*STANDARD*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((nominal + uppT2) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((nominal + (nominal *(uppT2/100))) AS DECIMAL(10,5));
		END IF;
	ELSE
	/*AVERAGE*/
		IF (limitOn = 0) THEN
		/*ACTUAL*/
			SET @limit = CAST((average + uppT2) AS DECIMAL(10,5));
		ELSE
		/*PERCENTAGE*/
			SET @limit = CAST((average + (average *(uppT2/100))) AS DECIMAL(10,5));
		END IF;
	END IF;
ELSE
	SET @limit = 0;
END IF;
RETURN ABS(@limit);
END */$$
DELIMITER ;

/* Function  structure for function  `getRemark` */

/*!50003 DROP FUNCTION IF EXISTS `getRemark` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getRemark`(repSerNoFromMaster INT, weightmentModeNo INT,lowerLimit DECIMAL(12,5),upperLimit DECIMAL(12,5),strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(10) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail1` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail2` WHERE `RepSerNo`= repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail3` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail4` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail5` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail6` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail7` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail1` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail2` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(CASE WHEN `NetWeight` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `NetWeight` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail3` WHERE `RepSerNo`= repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail5` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getRemark` : Incorrect weightmentModeNo Found At Line 36','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(CASE WHEN `NetWeight` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `NetWeight` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getRemark` : Incorrect weightmentModeNo Found At Line 30','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail2_archived` WHERE `RepSerNo`= repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(CASE WHEN `NetWeight` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `NetWeight` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`= repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(CASE WHEN `DataValue` < lowerLimit THEN 1 ELSE 0 END),  SUM(CASE WHEN `DataValue` > upperLimit THEN 1 ELSE 0 END) INTO @belowLimitCount,@aboveLimitCount  FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`= repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getRemark` : Incorrect weightmentModeNo Found At Line 88','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getRemark` : Incorrect Table Name Found At Line 92','None');
END IF;
IF (@belowLimitCount > 0 OR @aboveLimitCount > 0) THEN 
	SET @result = 'Not Ok'; 
ELSE 
	SET @result = 'Ok'; 
END IF; 
RETURN @result;
END */$$
DELIMITER ;

/* Function  structure for function  `getStandardDeviationOfDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getStandardDeviationOfDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getStandardDeviationOfDataValue`(repSerNoFromMaster INT, weightmentModeNo INT,strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
    DETERMINISTIC
BEGIN
IF(LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT STDDEV_SAMP(`DataValue1`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT STDDEV_SAMP(`NetWeight`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;			
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getStandardDeviationOfDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT STDDEV_SAMP(`DataValue1`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT STDDEV_SAMP(`NetWeight`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;			
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getStandardDeviationOfDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail2_archived` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT STDDEV_SAMP(`DataValue1`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT STDDEV_SAMP(`NetWeight`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT STDDEV_SAMP(`DataValue`) INTO @StandardDeviationOfDataValue FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getStandardDeviationOfDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getStandardDeviationOfDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
RETURN @StandardDeviationOfDataValue;
END */$$
DELIMITER ;

/* Function  structure for function  `getSumOfDataValue` */

/*!50003 DROP FUNCTION IF EXISTS `getSumOfDataValue` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `getSumOfDataValue`(repSerNoFromMaster INT, weightmentModeNo INT,strDetailTable VARCHAR(100), reportSide VARCHAR(20)) RETURNS varchar(12) CHARSET latin1
BEGIN
IF (LOCATE('incomplete',strDetailTable)=0 AND LOCATE('archived',strDetailTable)=0) THEN
	/*COMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail1` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail4` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail6` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail7` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail1` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail2` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT SUM(`DataValue1`) INTO @Sum FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(`NetWeight`) INTO @Sum FROM `tbl_cap_detail3` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail5` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getSumOfDataValue` : Incorrect weightmentModeNo Found At Line 46','None');
	END IF;
ELSEIF (LOCATE('incomplete',strDetailTable)>0) THEN
	/*INCOMPLETE REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail4_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail6_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail7_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail1_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT SUM(`DataValue1`) INTO @Sum FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(`NetWeight`) INTO @Sum FROM `tbl_cap_detail3_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail5_incomplete` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getSumOfDataValue` : Incorrect weightmentModeNo Found At Line 74','None');
	END IF;
ELSEIF (LOCATE('archived',strDetailTable)>0) THEN
	/*ARCHIVED REPORT SECTION*/
	IF (weightmentModeNo = 1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 2) THEN
		IF (reportSide  = 'NA') THEN
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2_archived` WHERE Side='NA' AND `RepSerNo`=repSerNoFromMaster;	
		ELSEIF (reportSide  = 'LHS') THEN
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2_archived` WHERE Side='LHS' AND `RepSerNo`=repSerNoFromMaster;
		ELSE
			SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail2_archived` WHERE Side='RHS' AND `RepSerNo`=repSerNoFromMaster;
		END IF;	
	ELSEIF (weightmentModeNo = 3) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 4) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail4_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 6) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail6_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = 7) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_tab_detail7_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -1) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail1_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -2) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail2_archived` WHERE `RepSerNo`=repSerNoFromMaster;	
	ELSEIF (weightmentModeNo = -31) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -32) THEN
		SELECT SUM(`DataValue1`) INTO @Sum FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -33) THEN
		SELECT SUM(`NetWeight`) INTO @Sum FROM `tbl_cap_detail3_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSEIF (weightmentModeNo = -5) THEN
		SELECT SUM(`DataValue`) INTO @Sum FROM `tbl_cap_detail5_archived` WHERE `RepSerNo`=repSerNoFromMaster;
	ELSE
		/*Error to Log: Incorrect weightmentModeNo*/
		SET @EL = `errorLog`('function `getSumOfDataValue` : Incorrect weightmentModeNo Found At Line 112','None');
	END IF;
ELSE
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getSumOfDataValue` : Incorrect Table Name Found At Line 116','None');
END IF;
	/*Error to Log: Incorrect Table Name*/
	SET @EL = `errorLog`('function `getSumOfDataValue` : Incorrect Table Name Found At Line 119','None');
RETURN @Sum;
END */$$
DELIMITER ;

/* Function  structure for function  `RHU1` */

/*!50003 DROP FUNCTION IF EXISTS `RHU1` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `RHU1`(inputDecimal DECIMAL(20,15)) RETURNS decimal(7,1)
    DETERMINISTIC
BEGIN
	SELECT ABS(ROUND(CAST(inputDecimal AS DECIMAL(20,15)),1)) INTO @outputDecimal;
	RETURN @outputDecimal;
END */$$
DELIMITER ;

/* Function  structure for function  `RHU2` */

/*!50003 DROP FUNCTION IF EXISTS `RHU2` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `RHU2`(inputDecimal DECIMAL(20,15)) RETURNS decimal(7,2)
    DETERMINISTIC
BEGIN
	SELECT ABS(ROUND(CAST(inputDecimal AS DECIMAL(20,15)),2)) INTO @outputDecimal;
	RETURN @outputDecimal;
END */$$
DELIMITER ;

/* Function  structure for function  `RHU3` */

/*!50003 DROP FUNCTION IF EXISTS `RHU3` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `RHU3`(inputDecimal DECIMAL(20,15)) RETURNS decimal(7,3)
    DETERMINISTIC
BEGIN
	SELECT ABS(ROUND(CAST(inputDecimal AS DECIMAL(20,15)),3)) INTO @outputDecimal;
	RETURN @outputDecimal;
END */$$
DELIMITER ;

/* Function  structure for function  `RHU4` */

/*!50003 DROP FUNCTION IF EXISTS `RHU4` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `RHU4`(inputDecimal DECIMAL(20,15)) RETURNS decimal(10,4)
    DETERMINISTIC
BEGIN
	SELECT ABS(ROUND(CAST(inputDecimal AS DECIMAL(20,15)),4)) INTO @outputDecimal;
	RETURN @outputDecimal;
END */$$
DELIMITER ;

/* Function  structure for function  `SPLIT_STRING` */

/*!50003 DROP FUNCTION IF EXISTS `SPLIT_STRING` */;
DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` FUNCTION `SPLIT_STRING`(str VARCHAR(255), delim VARCHAR(12), pos INT) RETURNS varchar(255) CHARSET latin1
RETURN REPLACE(SUBSTRING(SUBSTRING_INDEX(str, delim, pos),
       LENGTH(SUBSTRING_INDEX(str, delim, pos-1)) + 1),
       delim, '') */$$
DELIMITER ;

/* Procedure structure for procedure `allbatchSummaryReportCalculation` */

/*!50003 DROP PROCEDURE IF EXISTS  `allbatchSummaryReportCalculation` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `allbatchSummaryReportCalculation`(
IN EqpTabletTester INT,
IN ISHTDTable INT,
IN HMIID INT,
IN projectName VARCHAR(10),
OUT IndividualNom DECIMAL(10,5),
OUT IndividualT1Neg DECIMAL(10,5),
OUT IndividualT1Pos DECIMAL(10,5),
OUT IndividualT2Neg DECIMAL(10,5),
OUT IndividualT2Pos DECIMAL(10,5),
OUT IndividualMin DECIMAL(10,5),
OUT IndividualMax DECIMAL(10,5),
OUT IndividualAvg DECIMAL(10,5),
OUT IndividualUnit VARCHAR(5),
OUT IndividualNMT INT,
OUT IndividualLimitOn VARCHAR(5),
OUT IndividualDP INT,
OUT qtyInd INT,
OUT IndividualLayerNom DECIMAL(10,5),
OUT IndividualLayerT1Neg DECIMAL(10,5),
OUT IndividualLayerT1Pos DECIMAL(10,5),
OUT IndividualLayerT2Neg DECIMAL(10,5),
OUT IndividualLayerT2Pos DECIMAL(10,5),
OUT IndividualLayerMin DECIMAL(10,5),
OUT IndividualLayerMax DECIMAL(10,5),
OUT IndividualLayerAvg DECIMAL(10,5),
OUT IndividualLayerUnit VARCHAR(5),
OUT IndividualLayerNMT INT,
OUT IndividualLayerLimitOn VARCHAR(5),
OUT IndividualLayerDP INT,
OUT qtyIndLayer INT,
OUT IndividualLayer1Nom DECIMAL(10,5),
OUT IndividualLayer1T1Neg DECIMAL(10,5),
OUT IndividualLayer1T1Pos DECIMAL(10,5),
OUT IndividualLayer1T2Neg DECIMAL(10,5),
OUT IndividualLayer1T2Pos DECIMAL(10,5),
OUT IndividualLayer1Min DECIMAL(10,5),
OUT IndividualLayer1Max DECIMAL(10,5),
OUT IndividualLayer1Avg DECIMAL(10,5),
OUT IndividualLayer1Unit VARCHAR(5),
OUT IndividualLayer1NMT INT,
OUT IndividualLayer1LimitOn VARCHAR(5),
OUT IndividualLayer1DP INT,
OUT qtyIndLayer1 INT,
OUT DiffNom DECIMAL(10,5),
OUT DiffT1Neg DECIMAL(10,5),
OUT DiffT1Pos DECIMAL(10,5),
OUT DiffT2Neg DECIMAL(10,5),
OUT DiffT2Pos DECIMAL(10,5),
OUT DiffMin DECIMAL(10,5),
OUT DiffMax DECIMAL(10,5),
OUT DiffAvg DECIMAL(10,5),
OUT DiffUnit VARCHAR(5),
OUT DiffNMT INT,
OUT DiffLimitOn VARCHAR(5),
OUT Diff1DP INT,
OUT qtyDiff INT,
OUT MNetNom DECIMAL(10,5),
OUT MNetT1Neg DECIMAL(10,5),
OUT MNetT1Pos DECIMAL(10,5),
OUT MNetT2Neg DECIMAL(10,5),
OUT MNetT2Pos DECIMAL(10,5),
OUT MNetMin DECIMAL(10,5),
OUT MNetMax DECIMAL(10,5),
OUT MNetAvg DECIMAL(10,5),
OUT MNetUnit VARCHAR(5),
OUT MNetNMT INT,
OUT MNetLimitOn VARCHAR(5),
OUT MNetDP INT,
OUT qtyMNet INT,
OUT MDryCNom DECIMAL(10,5),
OUT MDryCT1Neg DECIMAL(10,5),
OUT MDryCT1Pos DECIMAL(10,5),
OUT MDryCT2Neg DECIMAL(10,5),
OUT MDryCT2Pos DECIMAL(10,5),
OUT MDryCMin DECIMAL(10,5),
OUT MDryCMax DECIMAL(10,5),
OUT MDryCAvg DECIMAL(10,5),
OUT MDryCUnit VARCHAR(5),
OUT MDryCNMT INT,
OUT MDryCLimitOn VARCHAR(5),
OUT MDryCLDP INT,
OUT qtyMDryC INT,
OUT MDryPNom DECIMAL(10,5),
OUT MDryPT1Neg DECIMAL(10,5),
OUT MDryPT1Pos DECIMAL(10,5),
OUT MDryPT2Neg DECIMAL(10,5),
OUT MDryPT2Pos DECIMAL(10,5),
OUT MDryPMin DECIMAL(10,5),
OUT MDryPMax DECIMAL(10,5),
OUT MDryPAvg DECIMAL(10,5),
OUT MDryPUnit VARCHAR(5),
OUT MDryPNMT INT,
OUT MDryPLimitOn VARCHAR(5),
OUT MDryPLDP INT,
OUT qtyMDryP INT,
OUT ThickNom DECIMAL(6,2),
OUT ThickT1Neg DECIMAL(6,2),
OUT ThickT1Pos DECIMAL(6,2),
OUT ThickT2Neg DECIMAL(6,2),
OUT ThickT2Pos DECIMAL(6,2),
OUT ThickMin DECIMAL(6,2),
OUT ThickMax DECIMAL(6,2),
OUT ThickAvg DECIMAL(6,2),
OUT qtyThick INT,
OUT BrdNom DECIMAL(6,2),
OUT BrdT2Neg DECIMAL(6,2),
OUT BrdT2Pos DECIMAL(6,2),
OUT BrdMin DECIMAL(6,2),
OUT BrdMax DECIMAL(6,2),
OUT BrdAvg DECIMAL(6,2),
OUT qtyBrd INT,
OUT LenNom DECIMAL(6,2),
OUT LenT2Neg DECIMAL(6,2),
OUT LenT2Pos DECIMAL(6,2),
OUT LenMin DECIMAL(6,2),
OUT LenMax DECIMAL(6,2),
OUT LenAvg DECIMAL(6,2),
OUT qtyLen INT,
OUT DiaNom DECIMAL(6,2),
OUT DiaT2Neg DECIMAL(6,2),
OUT DiaT2Pos DECIMAL(6,2),
OUT DiaMin DECIMAL(6,2),
OUT DiaMax DECIMAL(6,2),
OUT DiaAvg DECIMAL(6,2),
OUT qtyDia INT,
OUT FriabilityNom DECIMAL(10,5),
OUT FriabilityMin DECIMAL(10,5),
OUT FriabilityMax DECIMAL(10,5),
OUT FriabilityAvg DECIMAL(10,5),
OUT FriabilityDP INT,
OUT qtyFri INT,
OUT DTTemp1 DECIMAL(6,1),
OUT DTTemp2 DECIMAL(6,1),
OUT qtyDT INT,
OUT HardnessNomN DECIMAL(10,1),
OUT HardnessLwrN DECIMAL(10,1),
OUT HardnessUppN DECIMAL(10,1),
OUT HardnessMinN DECIMAL(10,1),
OUT HardnessMaxN DECIMAL(10,1),
OUT HardnessAvgN DECIMAL(10,1),
OUT HardnessNomKp DECIMAL(10,1),
OUT HardnessLwrKp DECIMAL(10,1),
OUT HardnessUppKp DECIMAL(10,1),
OUT HardnessMinKp DECIMAL(10,1),
OUT HardnessMaxKp DECIMAL(10,1),
OUT HardnessAvgKp DECIMAL(10,1),
OUT HardnessUnit VARCHAR(5),
OUT ThicknessNom DECIMAL(10,2),
OUT ThicknessLwr DECIMAL(10,2),
OUT ThicknessUpp DECIMAL(10,2),
OUT ThicknessMin DECIMAL(10,2),
OUT ThicknessMax DECIMAL(10,2),
OUT ThicknessAvg DECIMAL(10,2),
OUT DiaLenBrdNom DECIMAL(10,2),
OUT DiaLenBrdLwr DECIMAL(10,2),
OUT DiaLenBrdUpp DECIMAL(10,2),
OUT DiaLenBrdMin DECIMAL(10,2),
OUT DiaLenBrdMax DECIMAL(10,2),
OUT DiaLenBrdAvg DECIMAL(10,2),
OUT qtyHard INT,
OUT qtyLOD INT,
OUT RecSeqNoInd INT,
OUT RecSeqNoRInd INT,
OUT RecSeqNoIndL INT,
OUT RecSeqNoRIndL INT,
OUT RecSeqNoIndL1 INT,
OUT RecSeqNoRIndL1 INT,
OUT RecSeqNoThick INT,
OUT RecSeqNoRThick INT,
OUT RecSeqNoBrd INT,
OUT RecSeqNoRBrd INT,
OUT RecSeqNoLen INT,
OUT RecSeqNoRLen INT,
OUT RecSeqNoDia INT,
OUT RecSeqNoRDia INT,
OUT RecSeqNoDT INT,
OUT RecSeqNoRDT INT,
OUT RecSeqNoFri INT,
OUT RecSeqNoRFri INT,
OUT RecSeqNoLOD INT,
OUT RecSeqNoRLOD INT,
OUT RecSeqNoHard INT,
OUT RecSeqNoRHard INT,
OUT RecSeqNoDiff INT,
OUT HardDP INT,
OUT NetNom DECIMAL(10,5),
OUT NetT1Neg DECIMAL(10,5),
OUT NetT1Pos DECIMAL(10,5),
OUT NetT2Neg DECIMAL(10,5),
OUT NetT2Pos DECIMAL(10,5),
OUT NetMin DECIMAL(10,5),
OUT NetMax DECIMAL(10,5),
OUT NetAvg DECIMAL(10,5),
OUT NetUnit VARCHAR(5),
OUT NetNMT INT,
OUT NetLimitOn VARCHAR(5),
OUT Content1_Nom DECIMAL(10,5),
OUT Content1_T1Neg DECIMAL(10,5),
OUT Content1_T1Pos DECIMAL(10,5),
OUT Content1_T2Neg DECIMAL(10,5),
OUT Content1_T2Pos DECIMAL(10,5),
OUT Content1_Min DECIMAL(10,5),
OUT Content1_Max DECIMAL(10,5),
OUT Content1_Avg DECIMAL(10,5),
OUT Content1_NMT INT,
OUT Content1_LimitOn VARCHAR(1),
OUT Content2_Nom DECIMAL(10,5),
OUT Content2_T1Neg DECIMAL(10,5),
OUT Content2_T1Pos DECIMAL(10,5),
OUT Content2_T2Neg DECIMAL(10,5),
OUT Content2_T2Pos DECIMAL(10,5),
OUT Content2_Min DECIMAL(10,5),
OUT Content2_Max DECIMAL(10,5),
OUT Content2_Avg DECIMAL(10,5),
OUT Content2_NMT INT,
OUT Content2_LimitOn VARCHAR(1),
OUT Content3_Nom DECIMAL(10,5),
OUT Content3_T1Neg DECIMAL(10,5),
OUT Content3_T1Pos DECIMAL(10,5),
OUT Content3_T2Neg DECIMAL(10,5),
OUT Content3_T2Pos DECIMAL(10,5),
OUT Content3_Min DECIMAL(10,5),
OUT Content3_Max DECIMAL(10,5),
OUT Content3_Avg DECIMAL(10,5),
OUT Content3_NMT INT,
OUT Content3_LimitOn VARCHAR(1),
OUT Content4_Nom DECIMAL(10,5),
OUT Content4_T1Neg DECIMAL(10,5),
OUT Content4_T1Pos DECIMAL(10,5),
OUT Content4_T2Neg DECIMAL(10,5),
OUT Content4_T2Pos DECIMAL(10,5),
OUT Content4_Min DECIMAL(10,5),
OUT Content4_Max DECIMAL(10,5),
OUT Content4_Avg DECIMAL(10,5),
OUT Content4_NMT INT,
OUT Content4_LimitOn VARCHAR(1),
OUT RecSeqNoNet INT,
OUT qtyNet INT,
OUT NetDP INT
)
BEGIN
SET @projectName = projectName;
SET @ISHTDTable = ISHTDTable;
SET @EqpTabletTester = EqpTabletTester;
/** Individual **/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinInd, @DataValueMaxInd, @DataValueAvgInd
	FROM `tbl_batchsummary_detail1_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,
	@Unit, @NMTLimit,@LimitOn,@DP
	 FROM tbl_batchsummary_master1_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(`Min`) INTO @qty FROM tbl_batchsummary_detail1_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoInd FROM tbl_batchsummary_detail1_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRInd FROM tbl_batchsummary_detail1_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	
	SET IndividualNom = @Nom;
	SET IndividualT1Neg = @Tol1Neg;
	SET IndividualT1Pos = @Tol1Pos;
	SET IndividualT2Neg = @Tol2Neg;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualMin = @DataValueMinInd;
	SET IndividualMax = @DataValueMaxInd;
	SET IndividualAvg = @DataValueAvgInd;
	SET IndividualUnit = @Unit;
	SET IndividualNMT = @NMTLimit;
	SET IndividualLimitOn = @LimitOn;
	SET qtyInd = @qty;
	SET RecSeqNoInd = @RecSeqNoInd;
	SET RecSeqNoRInd = @RecSeqNoRInd;
	SET IndividualDP = @DP;
	
/** Individual Layer**/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinIndLayer,@DataValueMaxIndLayer, @DataValueAvgIndLayer
	FROM tbl_batchsummary_detail9_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @NomIndLayer, @Tol1NegIndLayer, @Tol1PosIndLayer, @Tol2NegIndLayer, @Tol2PosIndLayer,
	@UnitIndLayer, @NMTLimitIndLayer,@LimitOnIndLayer,@DP
	 FROM tbl_batchsummary_master9_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail9_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoIndL FROM tbl_batchsummary_detail9_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRIndL FROM tbl_batchsummary_detail9_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET IndividualLayerNom = @NomIndLayer;
	SET IndividualLayerT1Neg = @Tol1NegIndLayer;
	SET IndividualLayerT1Pos = @Tol1PosIndLayer;
	SET IndividualLayerT2Neg = @Tol2NegIndLayer;
	SET IndividualLayerT2Pos = @Tol2PosIndLayer;
	SET IndividualLayerT2Pos = @Tol2PosIndLayer;
	SET IndividualLayerMin = @DataValueMinIndLayer;
	SET IndividualLayerMax = @DataValueMaxIndLayer;
	SET IndividualLayerAvg = @DataValueAvgIndLayer;
	SET IndividualLayerUnit = @UnitIndLayer;
	SET IndividualLayerNMT = @NMTLimitIndLayer;
	SET IndividualLayerLimitOn = @LimitOnIndLayer;
	SET qtyIndLayer = @qty;
	SET RecSeqNoIndL = @RecSeqNoIndL;
	SET RecSeqNoRIndL = @RecSeqNoRIndL;
	SET IndividualLayerDP = @DP;
	
/** Individual Layer1**/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinIndLayer1,@DataValueMaxIndLayer1, @DataValueAvgIndLayer1
	FROM tbl_batchsummary_detail11_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @NomIndLayer1, @Tol1NegIndLayer1, @Tol1PosIndLayer1, @Tol2NegIndLayer1, @Tol2PosIndLayer1, 
	@UnitIndLayer1, @NMTLimitIndLayer1, @LimitOnIndLayer1,@DP
	 FROM tbl_batchsummary_master11_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail11_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoIndL1 FROM tbl_batchsummary_detail11_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRIndL1 FROM tbl_batchsummary_detail11_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET IndividualLayer1Nom = @NomIndLayer1;
	SET IndividualLayer1T1Neg = @Tol1NegIndLayer1;
	SET IndividualLayer1T1Pos = @Tol1PosIndLayer1;
	SET IndividualLayer1T2Neg = @Tol2NegIndLayer1;
	SET IndividualLayer1T2Pos = @Tol2PosIndLayer1;
	SET IndividualLayer1T2Pos = @Tol2PosIndLayer1;
	SET IndividualLayer1Min = @DataValueMinIndLayer1;
	SET IndividualLayer1Max = @DataValueMaxIndLayer1;
	SET IndividualLayer1Avg = @DataValueAvgIndLayer1;
	SET IndividualLayer1Unit = @UnitIndLayer1;
	SET IndividualLayer1NMT = @NMTLimitIndLayer1;
	SET IndividualLayer1LimitOn = @LimitOnIndLayer1;
	SET qtyIndLayer1 = @qty;
	SET RecSeqNoIndL1 = @RecSeqNoIndL1;
	SET RecSeqNoRIndL1 = @RecSeqNoRIndL1;
	SET IndividualLayer1DP = @DP;
	
/** Thickness**/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMinThick, @DataValueMaxThick, @DataValueAvgThick
	FROM `tbl_batchsummary_detail3_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2)
	INTO @NomThick, @Tol1NegThick, @Tol1PosThick,@Tol2NegThick, @Tol2PosThick FROM `tbl_batchsummary_master3_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail3_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoThick FROM tbl_batchsummary_detail3_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRThick FROM tbl_batchsummary_detail3_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET ThickNom = @NomThick;
	SET ThickT1Neg = (@NomThick - @Tol1NegThick);
	SET ThickT1Pos = (@NomThick + @Tol1PosThick);
	SET ThickT2Neg = (@NomThick - @Tol2NegThick);
	SET ThickT2Pos = (@NomThick + @Tol2PosThick);
	SET ThickMin = @DataValueMinThick;
	SET ThickMax = @DataValueMaxThick;
	SET ThickAvg = @DataValueAvgThick;
	SET qtyThick = @qty;
	SET RecSeqNoThick = @RecSeqNoThick;
	SET RecSeqNoRThick = @RecSeqNoRThick;
	
/** Differential **/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinDiff,@DataValueMaxDiff, @DataValueAvgDiff
	FROM `tbl_batchsummary_detaildiff_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,
	@Unit, @NMTLimit,@LimitOn,@DP
	 FROM `tbl_batchsummary_masterdiff_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(`Min`) INTO @qty FROM tbl_batchsummary_detaildiff_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoDiff FROM tbl_batchsummary_detaildiff_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET DiffNom = @Nom;
	SET DiffT1Neg = @Tol1Neg;
	SET DiffT1Pos = @Tol1Pos;
	SET DiffT2Neg = @Tol2Neg;
	SET DiffT2Pos = @Tol2Pos;
	SET DiffT2Pos = @Tol2Pos;
	SET DiffMin = @DataValueMinDiff;
	SET DiffMax = @DataValueMaxDiff;
	SET DiffAvg = @DataValueAvgDiff;
	SET DiffUnit = @Unit;
	SET DiffNMT = @NMTLimit;
	SET DiffLimitOn = @LimitOn;
	SET qtyDiff = @qty;
	SET RecSeqNoDiff = @RecSeqNoDiff;
	SET Diff1DP = @DP;
	
/** Multihaler net **/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinNet,@DataValueMaxNet, @DataValueAvgNet
	FROM `tbl_batchsummary_detailmnet_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,
	@Unit, @NMTLimit,@LimitOn,@DP
	 FROM `tbl_batchsummary_mastermnet_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(`Min`) INTO @qty FROM tbl_batchsummary_detailmnet_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET MNetNom = @Nom;
	SET MNetT1Neg = @Tol1Neg;
	SET MNetT1Pos = @Tol1Pos;
	SET MNetT2Neg = @Tol2Neg;
	SET MNetT2Pos = @Tol2Pos;
	SET MNetT2Pos = @Tol2Pos;
	SET MNetMin = @DataValueMinNet;
	SET MNetMax = @DataValueMaxNet;
	SET MNetAvg = @DataValueAvgNet;
	SET MNetUnit = @Unit;
	SET MNetNMT = @NMTLimit;
	SET MNetLimitOn = @LimitOn;
	SET qtyMNet = @qty;
	SET MNetDP = @DP;
	
/** Multihaler DryC **/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMindryC, @DataValueMaxdryC, @DataValueAvgdryC
	FROM `tbl_batchsummary_detailmdryc_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,
	@Unit, @NMTLimit,@LimitOn,@DP
	 FROM `tbl_batchsummary_mastermdryc_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(`Min`) INTO @qty FROM tbl_batchsummary_detailmdryc_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET MDryCNom = @Nom;
	SET MDryCT1Neg = @Tol1Neg;
	SET MDryCT1Pos = @Tol1Pos;
	SET MDryCT2Neg = @Tol2Neg;
	SET MDryCT2Pos = @Tol2Pos;
	SET MDryCT2Pos = @Tol2Pos;
	SET MDryCMin = @DataValueMindryC;
	SET MDryCMax = @DataValueMaxdryC;
	SET MDryCAvg = @DataValueAvgdryC;
	SET MDryCUnit = @Unit;
	SET MDryCNMT = @NMTLimit;
	SET MDryCLimitOn = @LimitOn;
	SET qtyMDryC = @qty;
	SET MDryCLDP = @DP;
	
	/** Multihaler DryP **/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMindryP, @DataValueMaxdryP, @DataValueAvgdryP
	FROM `tbl_batchsummary_detailmdryp_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;	
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`DP`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,
	@Unit, @NMTLimit,@LimitOn,@DP
	 FROM `tbl_batchsummary_mastermdryp_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(`Min`) INTO @qty FROM tbl_batchsummary_detailmdryp_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET MDryPNom = @Nom;
	SET MDryPT1Neg = @Tol1Neg;
	SET MDryPT1Pos = @Tol1Pos;
	SET MDryPT2Neg = @Tol2Neg;
	SET MDryPT2Pos = @Tol2Pos;
	SET MDryPT2Pos = @Tol2Pos;
	SET MDryPMin = @DataValueMindryP;
	SET MDryPMax = @DataValueMaxdryP;
	SET MDryPAvg = @DataValueAvgdryP;
	SET MDryPUnit = @Unit;
	SET MDryPNMT = @NMTLimit;
	SET MDryPLimitOn = @LimitOn;
	SET qtyMDryP = @qty;
	SET MDryPLDP = @DP;
	
/** Breadth**/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMinBrd, @DataValueMaxBrd, @DataValueAvgBrd
	FROM tbl_batchsummary_detail4_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2)
	INTO @NomBrd, @Tol2NegBrd, @Tol2PosBrd FROM tbl_batchsummary_master4_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail4_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoBrd  FROM tbl_batchsummary_detail4_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRBrd  FROM tbl_batchsummary_detail4_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET BrdNom = @NomBrd;
	SET BrdT2Neg = (@NomBrd - @Tol2NegBrd);
	SET BrdT2Pos = (@NomBrd + @Tol2PosBrd);
	SET BrdMin = @DataValueMinBrd;
	SET BrdMax = @DataValueMaxBrd;
	SET BrdAvg = @DataValueAvgBrd;
	SET qtyBrd = @qty;
	SET RecSeqNoBrd = @RecSeqNoBrd;
	SET RecSeqNoRBrd  = @RecSeqNoRBrd;
	
/** Length**/
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMinLen, @DataValueMaxLen, @DataValueAvgLen
	FROM `tbl_batchsummary_detail5_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2)
	INTO @NomLen, @Tol2NegLen, @Tol2PosLen FROM `tbl_batchsummary_master5_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail5_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoLen  FROM tbl_batchsummary_detail5_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRLen  FROM tbl_batchsummary_detail5_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET LenNom = @NomLen;
	SET LenT2Neg = (@NomLen - @Tol2NegLen);
	SET LenT2Pos = (@NomLen + @Tol2PosLen);
	SET LenMin = @DataValueMinLen;
	SET LenMax = @DataValueMaxLen;
	SET LenAvg = @DataValueAvgLen;
	SET qtyLen = @qty;
	SET RecSeqNoLen = @RecSeqNoLen ;
	SET RecSeqNoRLen  = @RecSeqNoRLen ;
	
/** Diameter**/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMinDiam, @DataValueMaxDiam, @DataValueAvgDiam
	FROM `tbl_batchsummary_detail6_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2)
	INTO @NomDiam, @Tol2NegDiam, @Tol2PosDiam FROM `tbl_batchsummary_master6_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail6_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoDia FROM tbl_batchsummary_detail6_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRDia FROM tbl_batchsummary_detail6_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;	
	
	SET DiaNom = @NomDiam;
	SET DiaT2Neg = (@NomDiam - @Tol2NegDiam);
	SET DiaT2Pos = (@NomDiam + @Tol2PosDiam);
	SET DiaMin = @DataValueMinDiam;
	SET DiaMax = @DataValueMaxDiam;
	SET DiaAvg = @DataValueAvgDiam;
	SET qtyDia = @qty;
	SET RecSeqNoDia = @RecSeqNoDia;
	SET RecSeqNoRDia = @RecSeqNoRDia;
	
/** DT**/
	SET @query = CONCAT('SELECT `Tol1Neg`,`Tol1Pos` 
	INTO @Temp1_DT,@Temp2_DT FROM `tbl_batchsummary_master13_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(MaxTimeDT) INTO @qty FROM `tbl_batchsummary_detail13_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(MaxTimeDT) INTO @RecSeqNoDT FROM tbl_batchsummary_detail13_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL"  and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(MaxTimeDT) INTO @RecSeqNoRDT FROM tbl_batchsummary_detail13_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;	
	
	SET DTTemp1 = @Temp1_DT;
	SET DTTemp2 = @Temp2_DT;
	SET qtyDT = @qty;
	SET RecSeqNoDT = @RecSeqNoDT;
	SET RecSeqNoRDT = @RecSeqNoRDT;
	
/** Fri**/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMinFri, @DataValueMaxFri, @DataValueAvgFri
	FROM `tbl_batchsummary_detail8_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ROUND(CAST(`Nom` AS DECIMAL(20,15)),0),`DP` INTO @NomFri,@DP FROM `tbl_batchsummary_master8_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail8_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoFri FROM tbl_batchsummary_detail8_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRFri FROM tbl_batchsummary_detail8_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET FriabilityNom = @NomFri;
	SET FriabilityMin = @DataValueMinFri;
	SET FriabilityMax = @DataValueMaxFri;
	SET FriabilityAvg = @DataValueAvgFri;
	SET qtyFri = @qty;
	SET RecSeqNoFri = @RecSeqNoFri;
	SET RecSeqNoRFri = @RecSeqNoRFri;
	SET FriabilityDP = @DP;
/** LOD**/
	SET @query = CONCAT('SELECT count(Avg) INTO @qty FROM `tbl_batchsummary_detail16_temp_cons` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(Avg) INTO @RecSeqNoLOD FROM tbl_batchsummary_detail16_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(Avg) INTO @RecSeqNoRLOD FROM tbl_batchsummary_detail16_temp_cons WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET qtyLOD = @qty;
	SET RecSeqNoLOD = @RecSeqNoLOD;
	SET RecSeqNoRLOD = @RecSeqNoRLOD;
	
/** Multicomponent **/
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content4`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content4`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content4`) AS DECIMAL(20,15)),5)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg,@DataValueMin1, @DataValueMax1, @DataValueAvg1,
	@DataValueMin2, @DataValueMax2, @DataValueAvg2,@DataValueMin3, @DataValueMax3, @DataValueAvg3,
	@DataValueMin4, @DataValueMax4, @DataValueAvg4
	FROM `tbl_batchsummary_detail_multicontent_temp` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content4` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`NMTLimit_content1`,`LimitOn_content1`,`NMTLimit_content2`,`LimitOn_content2`,
	`NMTLimit_content3`,`LimitOn_content3`,`NMTLimit_content4`,`LimitOn_content4`,DP
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,@Nom1, @Tol1Neg1, @Tol1Pos1, @Tol2Neg1, @Tol2Pos1,@Nom2, @Tol1Neg2, @Tol1Pos2, @Tol2Neg2, @Tol2Pos2,
	@Nom3, @Tol1Neg3, @Tol1Pos3, @Tol2Neg3, @Tol2Pos3,@Nom4, @Tol1Neg4, @Tol1Pos4, @Tol2Neg4, @Tol2Pos4,
	@Unit, @NMTLimit, @LimitOn, @NMTLimit1, @LimitOn1, @NMTLimit2, @LimitOn2, @NMTLimit3, @LimitOn3, @NMTLimit4, @LimitOn4,@DP FROM `tbl_batchsummary_master_multicontent_temp` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_multicontent_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNA = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_multicontent_temp WHERE Side="NA" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNA;
	EXECUTE stmt;
	
	
	SET NetNom = @Nom;
	SET NetT1Neg = @Tol1Neg;
	SET NetT1Pos = @Tol1Pos;
	SET NetT2Neg = @Tol2Neg;
	SET NetT2Pos = @Tol2Pos;
	SET NetT2Pos = @Tol2Pos;
	SET NetMin = @DataValueMin;
	SET NetMax = @DataValueMax;
	SET NetAvg = @DataValueAvg;
	SET NetUnit = @Unit;
	SET NetNMT = @NMTLimit;
	SET NetLimitOn = @LimitOn;
	SET Content1_Nom = @Nom1;
	SET Content1_T1Neg = @Tol1Neg1;
	SET Content1_T1Pos = @Tol1Pos1;
	SET Content1_T2Neg = @Tol2Neg1;
	SET Content1_T2Pos = @Tol2Pos1;
	SET Content1_T2Pos = @Tol2Pos1;
	SET Content1_Min = @DataValueMin1;
	SET Content1_Max = @DataValueMax1;
	SET Content1_Avg = @DataValueAvg1;
	SET Content1_NMT = @NMTLimit1;
	SET Content1_LimitOn = @LimitOn1;
	SET Content2_Nom = @Nom2;
	SET Content2_T1Neg = @Tol1Neg2;
	SET Content2_T1Pos = @Tol1Pos2;
	SET Content2_T2Neg = @Tol2Neg2;
	SET Content2_T2Pos = @Tol2Pos2;
	SET Content2_T2Pos = @Tol2Pos2;
	SET Content2_Min = @DataValueMin2;
	SET Content2_Max = @DataValueMax2;
	SET Content2_Avg = @DataValueAvg2;
	SET Content2_NMT = @NMTLimit2;
	SET Content2_LimitOn = @LimitOn2;
	SET Content3_Nom = @Nom3;
	SET Content3_T1Neg = @Tol1Neg3;
	SET Content3_T1Pos = @Tol1Pos3;
	SET Content3_T2Neg = @Tol2Neg3;
	SET Content3_T2Pos = @Tol2Pos3;
	SET Content3_T2Pos = @Tol2Pos3;
	SET Content3_Min = @DataValueMin3;
	SET Content3_Max = @DataValueMax3;
	SET Content3_Avg = @DataValueAvg3;
	SET Content3_NMT = @NMTLimit3;
	SET Content3_LimitOn = @LimitOn3;
	SET Content4_Nom = @Nom4;
	SET Content4_T1Neg = @Tol1Neg4;
	SET Content4_T1Pos = @Tol1Pos4;
	SET Content4_T2Neg = @Tol2Neg4;
	SET Content4_T2Pos = @Tol2Pos4;
	SET Content4_T2Pos = @Tol2Pos4;
	SET Content4_Min = @DataValueMin4;
	SET Content4_Max = @DataValueMax4;
	SET Content4_Avg = @DataValueAvg4;
	SET Content4_NMT = @NMTLimit4;
	SET Content4_LimitOn = @LimitOn4;
	SET qtyNet = @qty;
	SET RecSeqNoNet = @RecSeqNo;
	SET NetDP = @DP;
	
/** Hardness**/
	IF (@ISHTDTable = '0') THEN
		SET @query = CONCAT('SELECT `Unit` INTO @UnitHard FROM `tbl_batchsummary_master7_temp_cons` WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
	
		IF @UnitHard = 'N' THEN
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`Nom` AS DECIMAL(20,15)),1),
			ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),1),
			ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master7_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			SET @query = CONCAT('SELECT 
			ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),1),
			ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),1),
			ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),1)
			INTO @MinHard,@MaxHard,@AvgHard
			FROM `tbl_batchsummary_detail7_temp_cons` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomN = @NomHard;
			ELSE
			SET HardnessNomN = 0;
			END IF;
					
			
			SET HardnessLwrN = @LwrHard;
			SET HardnessUppN = @UppHard;
			SET HardnessMinN = @MinHard;
			SET HardnessMaxN = @MaxHard;
			SET HardnessAvgN = @AvgHard;
			SET HardnessUnit = @UnitHard;
			SET HardDP = 0;
		ELSE
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`Nom` AS DECIMAL(20,15)),1),
			ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),1),
			ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master7_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			SET @query = CONCAT('SELECT 
			ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),1),
			ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),1),
			ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),1)
			INTO @MinHard,@MaxHard,@AvgHard
			FROM tbl_batchsummary_detail7_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomKp = @NomHard;
			ELSE
			SET HardnessNomKp = 0;
			END IF;
			
			SET HardnessNomKp = @NomHard;
			SET HardnessLwrKp = @LwrHard;
			SET HardnessUppKp = @UppHard;
			SET HardnessMinKp = @MinHard;
			SET HardnessMaxKp = @MaxHard;
			SET HardnessAvgKp = @AvgHard;
			SET HardnessUnit = @UnitHard;
			SET HardDP = 1;
		END IF;
	
		/**SET @query = CONCAT('SELECT 
		ROUND(CAST(`Nom_Thick1` AS DECIMAL(20,15)),2),
		ROUND(CAST(`Lwr_Thick1` AS DECIMAL(20,15)),2),
		ROUND(CAST(`Upp_Thick1` AS DECIMAL(20,15)),2),
		ROUND(CAST(`Nom_DBL` AS DECIMAL(20,15)),2),
		ROUND(CAST(`Lwr_DBL` AS DECIMAL(20,15)),2),
		ROUND(CAST(`Upp_DBL` AS DECIMAL(20,15)),2)
		INTO @NomThick1, @LwrThick1, @UppThick1, @NomDLB, @LwrDLB, @UppDLB 
		FROM tbl_batchsummary_master_all WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`Min_Thick1`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`Max_Thick1`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`Avg_Thick1`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MIN(`Min_DBL`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`Max_DBL`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`Avg_DBL`) AS DECIMAL(20,15)),2)
		INTO @MinThick1,@MaxThick1,@AvgThick1,@MinDLB,@MaxDLB,@AvgDLB
		FROM tbl_batchsummary_detail_all WHERE TestResult_Hard="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		**/
		
		SET @query = CONCAT('SELECT count(Min) INTO @qty FROM tbl_batchsummary_detail7_temp_cons WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @queryNAL = CONCAT('SELECT count(Min) INTO @RecSeqNoHard FROM tbl_batchsummary_detail7_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryNAL;
		EXECUTE stmt;
		
		SET @queryR = CONCAT('SELECT count(Min) INTO @RecSeqNoRHard FROM tbl_batchsummary_detail7_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryR;
		EXECUTE stmt;
		
		
		SET ThicknessNom = 0;
		SET ThicknessLwr = 0;
		SET ThicknessUpp = 0;
		SET ThicknessMin = 0;
		SET ThicknessMax = 0;
		SET ThicknessAvg = 0;
		SET DiaLenBrdNom = 0;
		SET DiaLenBrdLwr = 0;
		SET DiaLenBrdUpp = 0;
		SET DiaLenBrdMin = 0;
		SET DiaLenBrdMax = 0;
		SET DiaLenBrdAvg = 0;
		SET qtyHard = @qty;
		SET RecSeqNoHard = @RecSeqNoHard;
		SET RecSeqNoRHard = @RecSeqNoRHard;
	
	ELSE 
		SET @query = CONCAT('SELECT `UnitHard` INTO @UnitHard FROM `tbl_batchsummary_masterhdlb_temp_cons` WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @queryD = CONCAT('SELECT distinct `HardDP` INTO @HardDP FROM `tbl_batchsummary_detailhdlb_temp_cons` WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryD;
		EXECUTE stmt;
		
		SET @queryMin = CONCAT('SELECT 
		MIN(ROUND(CAST(MinHard AS DECIMAL(10,5)),1)) INTO @MinHard
		FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" AND MinHard NOT IN("99999","99998") and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryMin;
		EXECUTE stmt;
		
		SET @queryMax = CONCAT('SELECT 
		MAX(ROUND(CAST(MaxHard AS DECIMAL(10,5)),1)) INTO @MaxHard
		FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" AND MaxHard NOT IN("99999","99998")  and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryMax;
		EXECUTE stmt;
		
		SET @queryAvg = CONCAT('SELECT 
		ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1) INTO @AvgHard
		FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" AND AvgHard NOT IN("99999","99998")  and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryAvg;
		EXECUTE stmt;
	
		SET HardDP = @HardDP;
		
		IF @UnitHard = 'N' THEN
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomN = @NomHard;
			ELSE
			SET HardnessNomN = 0;
			END IF;
					
			
			SET HardnessLwrN = @LwrHard;
			SET HardnessUppN = @UppHard;
			SET HardnessMinN = @MinHard;
			SET HardnessMaxN = @MaxHard;
			SET HardnessAvgN = @AvgHard;
			SET HardnessUnit = @UnitHard;
		ELSE
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomKp = @NomHard;
			ELSE
			SET HardnessNomKp = 0;
			END IF;
			
			SET HardnessNomKp = @NomHard;
			SET HardnessLwrKp = @LwrHard;
			SET HardnessUppKp = @UppHard;
			SET HardnessMinKp = @MinHard;
			SET HardnessMaxKp = @MaxHard;
			SET HardnessAvgKp = @AvgHard;
			SET HardnessUnit = @UnitHard;
		END IF;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`LwrThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`UppThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`NomDLB` AS DECIMAL(20,15)),2),
		ROUND(CAST(`LwrDLB` AS DECIMAL(20,15)),2),
		ROUND(CAST(`UppDLB` AS DECIMAL(20,15)),2)
		INTO @NomThick1, @LwrThick1, @UppThick1, @NomDLB, @LwrDLB, @UppDLB 
		FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`MinThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`MaxThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`AvgThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MIN(`MinDLB`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`MaxDLB`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`AvgDLB`) AS DECIMAL(20,15)),2)
		INTO @MinThick1,@MaxThick1,@AvgThick1,@MinDLB,@MaxDLB,@AvgDLB
		FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT count(MinHard) INTO @qty FROM tbl_batchsummary_detailhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @queryNAL = CONCAT('SELECT count(MinHard) INTO @RecSeqNoHard FROM tbl_batchsummary_detailhdlb_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryNAL;
		EXECUTE stmt;
		
		SET @queryR = CONCAT('SELECT count(MinHard) INTO @RecSeqNoRHard FROM tbl_batchsummary_detailhdlb_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryR;
		EXECUTE stmt;
	
		
		
		SET ThicknessNom = @NomThick1;
		SET ThicknessLwr = (@NomThick1 - @LwrThick1);
		SET ThicknessUpp = (@NomThick1 + @UppThick1);
		SET ThicknessMin = @MinThick1;
		SET ThicknessMax = @MaxThick1;
		SET ThicknessAvg = @AvgThick1;
		SET DiaLenBrdNom = @NomDLB;
		SET DiaLenBrdLwr = (@NomDLB - @LwrDLB);
		SET DiaLenBrdUpp = (@NomDLB + @UppDLB);
		SET DiaLenBrdMin = @MinDLB;
		SET DiaLenBrdMax = @MaxDLB;
		SET DiaLenBrdAvg = @AvgDLB;
		SET qtyHard = @qty;
		SET RecSeqNoHard = @RecSeqNoHard;
		SET RecSeqNoRHard = @RecSeqNoRHard;
	
	END IF;
/** Tablet Tester- Not use**/
IF (@EqpTabletTester = 1) THEN
		SET @query = CONCAT('SELECT `UnitHard` INTO @UnitHard FROM `tbl_batchsummary_masterhdlb_temp_cons` WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @queryD = CONCAT('SELECT distinct `HardDP` INTO @HardDP FROM `tbl_batchsummary_detailhdlb_temp_cons` WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryD;
		EXECUTE stmt;
	
		SET HardDP = @HardDP;
		
		IF @UnitHard = 'N' THEN
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			SET @query = CONCAT('SELECT 
			ROUND(CAST(MIN(`MinHard`) AS DECIMAL(20,15)),1),
			ROUND(CAST(MAX(`MaxHard`) AS DECIMAL(20,15)),1),
			ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1)
			INTO @MinHard,@MaxHard,@AvgHard
			FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomN = @NomHard;
			ELSE
			SET HardnessNomN = 0;
			END IF;
					
			
			SET HardnessLwrN = @LwrHard;
			SET HardnessUppN = @UppHard;
			SET HardnessMinN = @MinHard;
			SET HardnessMaxN = @MaxHard;
			SET HardnessAvgN = @AvgHard;
			SET HardnessUnit = @UnitHard;
		ELSE
			SET @query = CONCAT('SELECT 
			ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
			ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
			INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			SET @query = CONCAT('SELECT 
			ROUND(CAST(MIN(`MinHard`) AS DECIMAL(20,15)),1),
			ROUND(CAST(MAX(`MaxHard`) AS DECIMAL(20,15)),1),
			ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1)
			INTO @MinHard,@MaxHard,@AvgHard
			FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
			PREPARE stmt FROM @query;
			EXECUTE stmt;
			
			IF (@NomHard != "99999.00000")  THEN
			SET HardnessNomKp = @NomHard;
			ELSE
			SET HardnessNomKp = 0;
			END IF;
			
			SET HardnessNomKp = @NomHard;
			SET HardnessLwrKp = @LwrHard;
			SET HardnessUppKp = @UppHard;
			SET HardnessMinKp = @MinHard;
			SET HardnessMaxKp = @MaxHard;
			SET HardnessAvgKp = @AvgHard;
			SET HardnessUnit = @UnitHard;
		END IF;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`LwrThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`UppThick` AS DECIMAL(20,15)),2),
		ROUND(CAST(`NomDLB` AS DECIMAL(20,15)),2),
		ROUND(CAST(`LwrDLB` AS DECIMAL(20,15)),2),
		ROUND(CAST(`UppDLB` AS DECIMAL(20,15)),2)
		INTO @NomThick1, @LwrThick1, @UppThick1, @NomDLB, @LwrDLB, @UppDLB 
		FROM tbl_batchsummary_masterhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`MinThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`MaxThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`AvgThick`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MIN(`MinDLB`) AS DECIMAL(20,15)),2),
		ROUND(CAST(MAX(`MaxDLB`) AS DECIMAL(20,15)),2),
		ROUND(CAST(AVG(`AvgDLB`) AS DECIMAL(20,15)),2)
		INTO @MinThick1,@MaxThick1,@AvgThick1,@MinDLB,@MaxDLB,@AvgDLB
		FROM tbl_batchsummary_detailhdlb_temp_cons WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT count(MinHard) INTO @qty FROM tbl_batchsummary_detailhdlb_temp_cons WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @queryNAL = CONCAT('SELECT count(MinHard) INTO @RecSeqNoHard FROM tbl_batchsummary_detailhdlb_temp_cons WHERE Side<>"RIGHT" and Side<>"NULL" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryNAL;
		EXECUTE stmt;
		
		SET @queryR = CONCAT('SELECT count(MinHard) INTO @RecSeqNoRHard FROM tbl_batchsummary_detailhdlb_temp_cons WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @queryR;
		EXECUTE stmt;
	
		
		
		SET ThicknessNom = @NomThick1;
		SET ThicknessLwr = (@NomThick1 - @LwrThick1);
		SET ThicknessUpp = (@NomThick1 + @UppThick1);
		SET ThicknessMin = @MinThick1;
		SET ThicknessMax = @MaxThick1;
		SET ThicknessAvg = @AvgThick1;
		SET DiaLenBrdNom = @NomDLB;
		SET DiaLenBrdLwr = (@NomDLB - @LwrDLB);
		SET DiaLenBrdUpp = (@NomDLB + @UppDLB);
		SET DiaLenBrdMin = @MinDLB;
		SET DiaLenBrdMax = @MaxDLB;
		SET DiaLenBrdAvg = @AvgDLB;
		SET qtyHard = @qty;
		SET RecSeqNoHard = @RecSeqNoHard;
		SET RecSeqNoRHard = @RecSeqNoRHard;
END IF;
	
END */$$
DELIMITER ;

/* Procedure structure for procedure `AllParameterArchived` */

/*!50003 DROP PROCEDURE IF EXISTS  `AllParameterArchived` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `AllParameterArchived`(
       IN tableName_master_archive VARCHAR(100),
      IN tableName_detail_archive VARCHAR(100),
      IN tableName_master VARCHAR(100),
      IN tableName_detail VARCHAR(100)
)
BEGIN
	DECLARE str_tableName_master_archive VARCHAR(100);
	DECLARE str_tableName_detail_archive VARCHAR(100);
	DECLARE str_tableName_master VARCHAR(100);
	DECLARE str_tableName_detail VARCHAR(100);
    
	SET @str_tableName_master_archive = tableName_master_archive;   
	SET @str_tableName_detail_archive = tableName_detail_archive; 
	SET @str_tableName_master = tableName_master;  
	SET @str_tableName_detail = tableName_detail; 
	
	SET @updateArchivedCol = CONCAT("update ",@str_tableName_master," set IsArchived=1  where BatchComplete=1 and
	PrDate<DATE_SUB(CURDATE(),INTERVAL (SELECT tbl_config_ArchivePeriod FROM tbl_setallparameter WHERE config_id=1) DAY)");
	PREPARE stmt1 FROM  @updateArchivedCol; 
	EXECUTE stmt1;   
     
	SET @insertIn_Master_ArchiveTable = CONCAT("insert into ",@str_tableName_master_archive," select * from ",@str_tableName_master," where 
	IsArchived=1"); 
	PREPARE stmt2 FROM  @insertIn_Master_ArchiveTable; 
	EXECUTE stmt2;   
      
	SET @insertIn_Detail_ArchiveTable = CONCAT("insert into ",@str_tableName_detail_archive," select * from ",@str_tableName_detail," where 
	RepSerNo In(select RepSerNo from ",@str_tableName_master," where IsArchived=1)"); 
	PREPARE stmt3 FROM  @insertIn_Detail_ArchiveTable; 
	EXECUTE stmt3; 
        
	SET @deleteFrom_DetailTable = CONCAT("delete from  ",@str_tableName_detail," where RepSerNo IN(select RepSerNo from ",@str_tableName_master," where IsArchived=1)"); 
	PREPARE stmt4 FROM  @deleteFrom_DetailTable; 
	EXECUTE stmt4; 
            
	SET @deleteFrom_MasterTable = CONCAT("delete from  ",@str_tableName_master," where IsArchived=1"); 
	PREPARE stmt5 FROM  @deleteFrom_MasterTable; 
	EXECUTE stmt5;  
    END */$$
DELIMITER ;

/* Procedure structure for procedure `ArchivedTables` */

/*!50003 DROP PROCEDURE IF EXISTS  `ArchivedTables` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `ArchivedTables`(
      IN tableName_master_archive VARCHAR(100),
      IN tableName_detail_archive VARCHAR(100),
      IN tableName_master VARCHAR(100),
      IN tableName_detail VARCHAR(100)
)
BEGIN
    
    DECLARE str_tableName_master_archive VARCHAR(100);
    DECLARE str_tableName_detail_archive VARCHAR(100);
    DECLARE str_tableName_master VARCHAR(100);
    DECLARE str_tableName_detail VARCHAR(100);
    
    SET @str_tableName_master_archive = tableName_master_archive;   
    SET @str_tableName_detail_archive = tableName_detail_archive; 
    SET @str_tableName_master = tableName_master;  
    SET @str_tableName_detail = tableName_detail; 
   
    
     IF (SELECT IF(COUNT(1)>0,'Differences','No Differences') Comparison FROM
	(SELECT
		column_name,ordinal_position,
		data_type,column_type,COUNT(1) rowcount
	    FROM information_schema.columns
	    WHERE table_schema=DATABASE()
	    AND table_name IN (@str_tableName_master,@str_tableName_master_archive)
	    GROUP BY
		column_name,ordinal_position,
		data_type,column_type
	    HAVING COUNT(1)=1
	) A) = "No Differences"
	THEN 
	 
		IF @str_tableName_master = "tbl_tab_friability" THEN
		  CALL FriTapLodArchived(@str_tableName_master_archive,@str_tableName_master);
		ELSEIF @str_tableName_master = "tbl_lodmaster" THEN
		  CALL FriTapLodArchived(@str_tableName_master_archive,@str_tableName_master);
		 ELSEIF @str_tableName_master = "tbl_lodcapmaster" THEN
		  CALL FriTapLodArchived(@str_tableName_master_archive,@str_tableName_master);
		ELSE
		   CALL AllParameterArchived(@str_tableName_master_archive,@str_tableName_detail_archive,@str_tableName_master,@str_tableName_detail);
		END IF;
		
	ELSE
	    INSERT INTO tbl_archive_log VALUES(@str_tableName_master);
	END IF;
    
    END */$$
DELIMITER ;

/* Procedure structure for procedure `batchSummaryReportCalculation` */

/*!50003 DROP PROCEDURE IF EXISTS  `batchSummaryReportCalculation` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `batchSummaryReportCalculation`(
IN IsMulticomponent INT,
IN projectName VARCHAR(10),
IN paramName VARCHAR(200),
IN HMIID INT,
OUT IndividualNom DECIMAL(10,5),
OUT IndividualT1Neg DECIMAL(10,5),
OUT IndividualT1Pos DECIMAL(10,5),
OUT IndividualT2Neg DECIMAL(10,5),
OUT IndividualT2Pos DECIMAL(10,5),
OUT IndividualMin DECIMAL(10,5),
OUT IndividualMax DECIMAL(10,5),
OUT IndividualAvg DECIMAL(10,5),
OUT IndividualUnit VARCHAR(5),
OUT IndividualNMT INT,
OUT IndividualLimitOn VARCHAR(1),
OUT VernierNom DECIMAL(6,2),
OUT VernierT1Neg DECIMAL(6,2),
OUT VernierT1Pos DECIMAL(6,2),
OUT VernierT2Neg DECIMAL(6,2),
OUT VernierT2Pos DECIMAL(6,2),
OUT VernierMin DECIMAL(6,2),
OUT VernierMax DECIMAL(6,2),
OUT VernierAvg DECIMAL(6,2),
OUT FriabilityNom DECIMAL(10,5),
OUT FriabilityMin DECIMAL(10,5),
OUT FriabilityMax DECIMAL(10,5),
OUT FriabilityAvg DECIMAL(10,5),
OUT DTTemp1 DECIMAL(6,1),
OUT DTTemp2 DECIMAL(6,1),
OUT HardnessNomN DECIMAL(10,1),
OUT HardnessLwrN DECIMAL(10,1),
OUT HardnessUppN DECIMAL(10,1),
OUT HardnessMinN DECIMAL(10,1),
OUT HardnessMaxN DECIMAL(10,1),
OUT HardnessAvgN DECIMAL(10,1),
OUT HardnessNomKp DECIMAL(10,1),
OUT HardnessLwrKp DECIMAL(10,1),
OUT HardnessUppKp DECIMAL(10,1),
OUT HardnessMinKp DECIMAL(10,1),
OUT HardnessMaxKp DECIMAL(10,1),
OUT HardnessAvgKp DECIMAL(10,1),
OUT HardnessUnit VARCHAR(5),
OUT ThicknessNom DECIMAL(10,2),
OUT ThicknessLwr DECIMAL(10,2),
OUT ThicknessUpp DECIMAL(10,2),
OUT ThicknessMin DECIMAL(10,2),
OUT ThicknessMax DECIMAL(10,2),
OUT ThicknessAvg DECIMAL(10,2),
OUT DiaLenBrdNom DECIMAL(10,2),
OUT DiaLenBrdLwr DECIMAL(10,2),
OUT DiaLenBrdUpp DECIMAL(10,2),
OUT DiaLenBrdMin DECIMAL(10,2),
OUT DiaLenBrdMax DECIMAL(10,2),
OUT DiaLenBrdAvg DECIMAL(10,2),
OUT DiameterNom DECIMAL(10,2),
OUT DiameterLwr DECIMAL(10,2),
OUT DiameterUpp DECIMAL(10,2),
OUT DiameterMin DECIMAL(10,2),
OUT DiameterMax DECIMAL(10,2),
OUT DiameterAvg DECIMAL(10,2),
OUT qty INT,
OUT RecSeqNo INT,
OUT RecSeqNoR INT,
OUT HardDP INT,
OUT Content1_Nom DECIMAL(10,5),
OUT Content1_T1Neg DECIMAL(10,5),
OUT Content1_T1Pos DECIMAL(10,5),
OUT Content1_T2Neg DECIMAL(10,5),
OUT Content1_T2Pos DECIMAL(10,5),
OUT Content1_Min DECIMAL(10,5),
OUT Content1_Max DECIMAL(10,5),
OUT Content1_Avg DECIMAL(10,5),
OUT Content1_NMT INT,
OUT Content1_LimitOn VARCHAR(1),
OUT Content2_Nom DECIMAL(10,5),
OUT Content2_T1Neg DECIMAL(10,5),
OUT Content2_T1Pos DECIMAL(10,5),
OUT Content2_T2Neg DECIMAL(10,5),
OUT Content2_T2Pos DECIMAL(10,5),
OUT Content2_Min DECIMAL(10,5),
OUT Content2_Max DECIMAL(10,5),
OUT Content2_Avg DECIMAL(10,5),
OUT Content2_NMT INT,
OUT Content2_LimitOn VARCHAR(1),
OUT Content3_Nom DECIMAL(10,5),
OUT Content3_T1Neg DECIMAL(10,5),
OUT Content3_T1Pos DECIMAL(10,5),
OUT Content3_T2Neg DECIMAL(10,5),
OUT Content3_T2Pos DECIMAL(10,5),
OUT Content3_Min DECIMAL(10,5),
OUT Content3_Max DECIMAL(10,5),
OUT Content3_Avg DECIMAL(10,5),
OUT Content3_NMT INT,
OUT Content3_LimitOn VARCHAR(1),
OUT Content4_Nom DECIMAL(10,5),
OUT Content4_T1Neg DECIMAL(10,5),
OUT Content4_T1Pos DECIMAL(10,5),
OUT Content4_T2Neg DECIMAL(10,5),
OUT Content4_T2Pos DECIMAL(10,5),
OUT Content4_Min DECIMAL(10,5),
OUT Content4_Max DECIMAL(10,5),
OUT Content4_Avg DECIMAL(10,5),
OUT Content4_NMT INT,
OUT Content4_LimitOn VARCHAR(1)
)
BEGIN
SET @paramName = paramName;
SET @projectName = projectName;
SET @IsMulticomponent = IsMulticomponent;
IF (@paramName = 'Differential' AND @IsMulticomponent = 1) THEN
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content1`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content2`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content3`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MIN(`Min_content4`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max_content4`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg_content4`) AS DECIMAL(20,15)),5)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg,@DataValueMin1, @DataValueMax1, @DataValueAvg1,
	@DataValueMin2, @DataValueMax2, @DataValueAvg2,@DataValueMin3, @DataValueMax3, @DataValueAvg3,
	@DataValueMin4, @DataValueMax4, @DataValueAvg4
	FROM `tbl_batchsummary_detail_multicontent_temp` WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content1` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content2` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content3` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Nom_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg_content4` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos_content4` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`,`NMTLimit_content1`,`LimitOn_content1`,`NMTLimit_content2`,`LimitOn_content2`,
	`NMTLimit_content3`,`LimitOn_content3`,`NMTLimit_content4`,`LimitOn_content4`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos,@Nom1, @Tol1Neg1, @Tol1Pos1, @Tol2Neg1, @Tol2Pos1,@Nom2, @Tol1Neg2, @Tol1Pos2, @Tol2Neg2, @Tol2Pos2,
	@Nom3, @Tol1Neg3, @Tol1Pos3, @Tol2Neg3, @Tol2Pos3,@Nom4, @Tol1Neg4, @Tol1Pos4, @Tol2Neg4, @Tol2Pos4,
	@Unit, @NMTLimit, @LimitOn, @NMTLimit1, @LimitOn1, @NMTLimit2, @LimitOn2, @NMTLimit3, @LimitOn3, @NMTLimit4, @LimitOn4 FROM `tbl_batchsummary_master_multicontent_temp` WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_multicontent_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNA = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_multicontent_temp WHERE Side="NA" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNA;
	EXECUTE stmt;
	
	SET IndividualNom = @Nom;
	SET IndividualT1Neg = @Tol1Neg;
	SET IndividualT1Pos = @Tol1Pos;
	SET IndividualT2Neg = @Tol2Neg;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualMin = @DataValueMin;
	SET IndividualMax = @DataValueMax;
	SET IndividualAvg = @DataValueAvg;
	SET IndividualUnit = @Unit;
	SET IndividualNMT = @NMTLimit;
	SET IndividualLimitOn = @LimitOn;
	SET Content1_Nom = @Nom1;
	SET Content1_T1Neg = @Tol1Neg1;
	SET Content1_T1Pos = @Tol1Pos1;
	SET Content1_T2Neg = @Tol2Neg1;
	SET Content1_T2Pos = @Tol2Pos1;
	SET Content1_T2Pos = @Tol2Pos1;
	SET Content1_Min = @DataValueMin1;
	SET Content1_Max = @DataValueMax1;
	SET Content1_Avg = @DataValueAvg1;
	SET Content1_NMT = @NMTLimit1;
	SET Content1_LimitOn = @LimitOn1;
	SET Content2_Nom = @Nom2;
	SET Content2_T1Neg = @Tol1Neg2;
	SET Content2_T1Pos = @Tol1Pos2;
	SET Content2_T2Neg = @Tol2Neg2;
	SET Content2_T2Pos = @Tol2Pos2;
	SET Content2_T2Pos = @Tol2Pos2;
	SET Content2_Min = @DataValueMin2;
	SET Content2_Max = @DataValueMax2;
	SET Content2_Avg = @DataValueAvg2;
	SET Content2_NMT = @NMTLimit2;
	SET Content2_LimitOn = @LimitOn2;
	SET Content3_Nom = @Nom3;
	SET Content3_T1Neg = @Tol1Neg3;
	SET Content3_T1Pos = @Tol1Pos3;
	SET Content3_T2Neg = @Tol2Neg3;
	SET Content3_T2Pos = @Tol2Pos3;
	SET Content3_T2Pos = @Tol2Pos3;
	SET Content3_Min = @DataValueMin3;
	SET Content3_Max = @DataValueMax3;
	SET Content3_Avg = @DataValueAvg3;
	SET Content3_NMT = @NMTLimit3;
	SET Content3_LimitOn = @LimitOn3;
	SET Content4_Nom = @Nom4;
	SET Content4_T1Neg = @Tol1Neg4;
	SET Content4_T1Pos = @Tol1Pos4;
	SET Content4_T2Neg = @Tol2Neg4;
	SET Content4_T2Pos = @Tol2Pos4;
	SET Content4_T2Pos = @Tol2Pos4;
	SET Content4_Min = @DataValueMin4;
	SET Content4_Max = @DataValueMax4;
	SET Content4_Avg = @DataValueAvg4;
	SET Content4_NMT = @NMTLimit4;
	SET Content4_LimitOn = @LimitOn4;
	SET qty = @qty;
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = 0;
ELSE IF (@paramName = 'Individual') OR (@paramName = 'Individual Layer1') OR (@paramName = 'Individual Layer2') 
OR (@paramName = 'Differential') OR (@paramName = 'Softshell') OR (@paramName = 'Net Content') OR (@paramName = 'Dry Cartridge') OR (@paramName = 'Dry Powder') THEN
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),5),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),5),
	`Unit`,`NMTLimit`,`LimitOn`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @Unit, @NMTLimit, @LimitOn FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	SET IndividualNom = @Nom;
	SET IndividualT1Neg = @Tol1Neg;
	SET IndividualT1Pos = @Tol1Pos;
	SET IndividualT2Neg = @Tol2Neg;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualT2Pos = @Tol2Pos;
	SET IndividualMin = @DataValueMin;
	SET IndividualMax = @DataValueMax;
	SET IndividualAvg = @DataValueAvg;
	SET IndividualUnit = @Unit;
	SET IndividualNMT = @NMTLimit;
	SET IndividualLimitOn = @LimitOn;
	SET qty = @qty;
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
ELSE IF (@paramName = 'Disintegration Tester') THEN
	SET @query = CONCAT('SELECT `Tol1Neg`,`Tol1Pos` 
	INTO @Tol1Neg,@Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET DTTemp1 = @Tol1Neg;
	SET DTTemp2 = @Tol1Pos;
	SET qty = @qty;
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
ELSE IF (@paramName = 'Friabilator') THEN
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),5),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),5),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),5)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ROUND(CAST(`Nom` AS DECIMAL(20,15)),0) INTO @Nom FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET FriabilityNom = @Nom;
	SET FriabilityMin = @DataValueMin;
	SET FriabilityMax = @DataValueMax;
	SET FriabilityAvg = @DataValueAvg;
	SET qty = @qty;
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
ELSE IF (@paramName = 'Hardness' AND @projectName = '0') THEN
SET @query = CONCAT('SELECT `Unit` INTO @UnitHard FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	IF @UnitHard = 'N' THEN
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`Nom` AS DECIMAL(20,15)),1),
		ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),1),
		ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),1)
		INTO @Nom, @Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),1),
		ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),1),
		ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),1)
		INTO @Min,@Max,@Avg
		FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@Nom != "99999.00000")  THEN
		SET HardnessNomN = @Nom;
		ELSE
		SET HardnessNomN = 0;
		END IF;
		
		SET HardnessLwrN = @Tol1Neg;
		SET HardnessUppN = @Tol1Pos;
		SET HardnessMinN = @Min;
		SET HardnessMaxN = @Max;
		SET HardnessAvgN = @Avg;
		SET HardnessUnit = @UnitHard;
		SET HardDP = 1;
	ELSE
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`Nom` AS DECIMAL(20,15)),1),
		ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),1),
		ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),1)
		INTO @Nom, @Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),1),
		ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),1),
		ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),1)
		INTO @Min,@Max,@Avg
		FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET HardnessNomKp = @Nom;
		SET HardnessLwrKp = @Tol1Neg;
		SET HardnessUppKp = @Tol1Pos;
		SET HardnessMinKp = @Min;
		SET HardnessMaxKp = @Max;
		SET HardnessAvgKp = @Avg;
		SET HardnessUnit = @UnitHard;
		SET HardDP = 1;
	END IF;
	
	SET ThicknessNom = 0;
	SET ThicknessLwr = 0;
	SET ThicknessUpp = 0;
	SET ThicknessMin = 0;
	SET ThicknessMax = 0;
	SET ThicknessAvg = 0;
	SET DiaLenBrdNom = 0;
	SET DiaLenBrdLwr = 0;
	SET DiaLenBrdUpp = 0;
	SET DiaLenBrdMin = 0;
	SET DiaLenBrdMax = 0;
	SET DiaLenBrdAvg = 0;
        SET DiameterNom = 0;
	SET DiameterLwr = 0;
	SET DiameterUpp = 0;
	SET DiameterMin = 0;
	SET DiameterMax = 0;
	SET DiameterAvg = 0;
	SET qty = @qty;		
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
	
	ELSE IF (@paramName = 'Hardness' AND @projectName = '2') THEN
SET @query = CONCAT('SELECT `Unit` INTO @UnitHard FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	IF @UnitHard = 'N' THEN
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`Nom` AS DECIMAL(20,15)),0),
		ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),0),
		ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),0)
		INTO @Nom, @Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),0),
		ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),0),
		ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),0)
		INTO @Min,@Max,@Avg
		FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@Nom != "99999.00000")  THEN
		SET HardnessNomN = @Nom;
		ELSE
		SET HardnessNomN = 0;
		END IF;
		
		SET HardnessLwrN = @Tol1Neg;
		SET HardnessUppN = @Tol1Pos;
		SET HardnessMinN = @Min;
		SET HardnessMaxN = @Max;
		SET HardnessAvgN = @Avg;
		SET HardnessUnit = @UnitHard;
		SET HardDP = 0;
	ELSE
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`Nom` AS DECIMAL(20,15)),0),
		ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),0),
		ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),0)
		INTO @Nom, @Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),0),
		ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),0),
		ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),0)
		INTO @Min,@Max,@Avg
		FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET HardnessNomKp = @Nom;
		SET HardnessLwrKp = @Tol1Neg;
		SET HardnessUppKp = @Tol1Pos;
		SET HardnessMinKp = @Min;
		SET HardnessMaxKp = @Max;
		SET HardnessAvgKp = @Avg;
		SET HardnessUnit = @UnitHard;
		SET HardDP = 0;
	END IF;
	
	SET ThicknessNom = 0;
	SET ThicknessLwr = 0;
	SET ThicknessUpp = 0;
	SET ThicknessMin = 0;
	SET ThicknessMax = 0;
	SET ThicknessAvg = 0;
	SET DiaLenBrdNom = 0;
	SET DiaLenBrdLwr = 0;
	SET DiaLenBrdUpp = 0;
	SET DiaLenBrdMin = 0;
	SET DiaLenBrdMax = 0;
	SET DiaLenBrdAvg = 0;
        SET DiameterNom = 0;
	SET DiameterLwr = 0;
	SET DiameterUpp = 0;
	SET DiameterMin = 0;
	SET DiameterMax = 0;
	SET DiameterAvg = 0;
	SET qty = @qty;		
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
ELSE IF (@paramName = 'Hardness' AND @projectName = '1') THEN
	SET @query = CONCAT('SELECT `UnitHard` INTO @UnitHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp_hdlb WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp_hdlb WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET @queryD = CONCAT('SELECT distinct `HardDP` INTO @HardDP FROM tbl_batchsummary_detail_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryD;
	EXECUTE stmt;
	
	SET HardDP = @HardDP;
	
	SET @queryMin = CONCAT('SELECT
	MIN(ROUND(CAST(MinHard AS DECIMAL(10,5)),1))  INTO @MinHard
	FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" AND MinHard NOT IN("99999","99998") and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryMin;
	EXECUTE stmt;
	
	SET @queryMax = CONCAT('SELECT 
	MAX(ROUND(CAST(MaxHard AS DECIMAL(10,5)),1))  INTO @MaxHard
	FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" AND MaxHard NOT IN("99999","99998")  and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryMax;
	EXECUTE stmt;
	
	SET @queryAvg = CONCAT('SELECT 
	ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1) INTO @AvgHard
	FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" AND AvgHard NOT IN("99999","99998")  and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryAvg;
	EXECUTE stmt;
		
	
	IF @UnitHard = 'N' THEN
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
		INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@NomHard != "99999.00000")  THEN
		SET HardnessNomN = @NomHard;
		ELSE
		SET HardnessNomN = 0;
		END IF;
		
		SET HardnessLwrN = @LwrHard;
		SET HardnessUppN = @UppHard;
		SET HardnessMinN = @MinHard;
		SET HardnessMaxN = @MaxHard;
		SET HardnessAvgN = @AvgHard;
		SET HardnessUnit = @UnitHard;
		
	ELSE
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
		INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@NomHard != "99999.00000")  THEN
		SET HardnessNomKp = @NomHard;
		ELSE
		SET HardnessNomKp = 0;
		END IF;
		
		SET HardnessNomKp = @NomHard;
		SET HardnessLwrKp = @LwrHard;
		SET HardnessUppKp = @UppHard;
		SET HardnessMinKp = @MinHard;
		SET HardnessMaxKp = @MaxHard;
		SET HardnessAvgKp = @AvgHard;
		SET HardnessUnit = @UnitHard;
		
	END IF;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`NomThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`NomDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`NomDiam` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrDiam` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppDiam` AS DECIMAL(20,15)),2)
	INTO @NomThick, @LwrThick, @UppThick, @NomDLB, @LwrDLB, @UppDLB, @NomDiam, @LwrDiam, @UppDiam
	FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`MinThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MIN(`MinDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MIN(`MinDiam`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxDiam`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgDiam`) AS DECIMAL(20,15)),2)
	INTO @MinThick,@MaxThick,@AvgThick,@MinDLB,@MaxDLB,@AvgDLB,@MinDiam,@MaxDiam,@AvgDiam
	FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET ThicknessNom = @NomThick;
	SET ThicknessLwr = (@NomThick - @LwrThick);
	SET ThicknessUpp = (@NomThick + @UppThick);
	SET ThicknessMin = @MinThick;
	SET ThicknessMax = @MaxThick;
	SET ThicknessAvg = @AvgThick;
	SET DiaLenBrdNom = @NomDLB;
	SET DiaLenBrdLwr = (@NomDLB - @LwrDLB);
	SET DiaLenBrdUpp = (@NomDLB + @UppDLB);
	SET DiaLenBrdMin = @MinDLB;
	SET DiaLenBrdMax = @MaxDLB;
	SET DiaLenBrdAvg = @AvgDLB;
	SET DiameterNom = @NomDiam;
	SET DiameterLwr = (@NomDiam - @LwrDiam);
	SET DiameterUpp = (@NomDiam + @UppDiam);
	SET DiameterMin = @MinDiam;
	SET DiameterMax = @MaxDiam;
	SET DiameterAvg = @AvgDiam;
	SET qty = @qty;		
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
ELSE IF (@paramName = 'Tablet Tester') THEN /*Not In Use*/
	SET @query = CONCAT('SELECT `UnitHard` INTO @UnitHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp_hdlb WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp_hdlb WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET @queryD = CONCAT('SELECT distinct `HardDP` INTO @HardDP FROM tbl_batchsummary_detail_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryD;
	EXECUTE stmt;
	
	SET HardDP = @HardDP;
	
	IF @UnitHard = 'N' THEN
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
		INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`MinHard`) AS DECIMAL(20,15)),1),
		ROUND(CAST(MAX(`MaxHard`) AS DECIMAL(20,15)),1),
		ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1)
		INTO @MinHard,@MaxHard,@AvgHard
		FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@NomHard != "99999.00000") THEN
		SET HardnessNomN = @NomHard;
		ELSE
		SET HardnessNomN = 0;
		END IF;
		
		SET HardnessLwrN = @LwrHard;
		SET HardnessUppN = @UppHard;
		SET HardnessMinN = @MinHard;
		SET HardnessMaxN = @MaxHard;
		SET HardnessAvgN = @AvgHard;
		SET HardnessUnit = @UnitHard;
		
	ELSE
		SET @query = CONCAT('SELECT 
		ROUND(CAST(`NomHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`LwrHard` AS DECIMAL(20,15)),1),
		ROUND(CAST(`UppHard` AS DECIMAL(20,15)),1)
		INTO @NomHard, @LwrHard, @UppHard FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		SET @query = CONCAT('SELECT 
		ROUND(CAST(MIN(`MinHard`) AS DECIMAL(20,15)),1),
		ROUND(CAST(MAX(`MaxHard`) AS DECIMAL(20,15)),1),
		ROUND(CAST(AVG(`AvgHard`) AS DECIMAL(20,15)),1)
		INTO @MinHard,@MaxHard,@AvgHard
		FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
		PREPARE stmt FROM @query;
		EXECUTE stmt;
		
		IF (@NomHard != "99999.00000")  THEN
		SET HardnessNomKp = @NomHard;
		ELSE
		SET HardnessNomKp = 0;
		END IF;
		
		SET HardnessNomKp = @NomHard;
		SET HardnessLwrKp = @LwrHard;
		SET HardnessUppKp = @UppHard;
		SET HardnessMinKp = @MinHard;
		SET HardnessMaxKp = @MaxHard;
		SET HardnessAvgKp = @AvgHard;
		SET HardnessUnit = @UnitHard;
		
	END IF;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`NomThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppThick` AS DECIMAL(20,15)),2),
	ROUND(CAST(`NomDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppDLB` AS DECIMAL(20,15)),2),
	ROUND(CAST(`NomDiam` AS DECIMAL(20,15)),2),
	ROUND(CAST(`LwrDiam` AS DECIMAL(20,15)),2),
	ROUND(CAST(`UppDiam` AS DECIMAL(20,15)),2)
	INTO @NomThick, @LwrThick, @UppThick, @NomDLB, @LwrDLB, @UppDLB, @NomDiam, @LwrDiam, @UppDiam
	FROM tbl_batchsummary_master_temp_hdlb WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`MinThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgThick`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MIN(`MinDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgDLB`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MIN(`MinDiam`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`MaxDiam`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`AvgDiam`) AS DECIMAL(20,15)),2)
	INTO @MinThick,@MaxThick,@AvgThick,@MinDLB,@MaxDLB,@AvgDLB,@MinDiam,@MaxDiam,@AvgDiam
	FROM tbl_batchsummary_detail_temp_hdlb WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET ThicknessNom = @NomThick;
	SET ThicknessLwr = (@NomThick - @LwrThick);
	SET ThicknessUpp = (@NomThick + @UppThick);
	SET ThicknessMin = @MinThick;
	SET ThicknessMax = @MaxThick;
	SET ThicknessAvg = @AvgThick;
	SET DiaLenBrdNom = @NomDLB;
	SET DiaLenBrdLwr = (@NomDLB - @LwrDLB);
	SET DiaLenBrdUpp = (@NomDLB + @UppDLB);
	SET DiaLenBrdMin = @MinDLB;
	SET DiaLenBrdMax = @MaxDLB;
	SET DiaLenBrdAvg = @AvgDLB;
	SET DiameterNom = @NomDiam;
	SET DiameterLwr = (@NomDiam - @LwrDiam);
	SET DiameterUpp = (@NomDiam + @UppDiam);
	SET DiameterMin = @MinDiam;
	SET DiameterMax = @MaxDiam;
	SET DiameterAvg = @AvgDiam;
	SET qty = @qty;		
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
	
ELSE
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,10)),2)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail_temp WHERE TestResult="Complies" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),2)
	INTO @Nom, @Tol2Neg, @Tol2Pos,@Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT count(*) INTO @qty FROM tbl_batchsummary_detail_temp WHERE `HMIID`=',HMIID,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @queryNAL = CONCAT('SELECT count(*) INTO @RecSeqNo FROM tbl_batchsummary_detail_temp WHERE Side<>"RIGHT" and`HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryNAL;
	EXECUTE stmt;
	
	SET @queryR = CONCAT('SELECT count(*) INTO @RecSeqNoR FROM tbl_batchsummary_detail_temp WHERE Side="RIGHT" and `HMIID`=',HMIID,';');
	PREPARE stmt FROM @queryR;
	EXECUTE stmt;
	
	SET VernierNom = @Nom;
	SET VernierT1Neg = (@Nom - @Tol1Neg);
	SET VernierT1Pos = (@Nom + @Tol1Pos);
	SET VernierT2Neg = (@Nom - @Tol2Neg);
	SET VernierT2Pos = (@Nom + @Tol2Pos);
	SET VernierMin = @DataValueMin;
	SET VernierMax = @DataValueMax;
	SET VernierAvg = @DataValueAvg;
	SET qty = @qty;
	SET RecSeqNo = @RecSeqNo;
	SET RecSeqNoR = @RecSeqNoR;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `batchSummaryReportCalculationsForInterface` */

/*!50003 DROP PROCEDURE IF EXISTS  `batchSummaryReportCalculationsForInterface` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `batchSummaryReportCalculationsForInterface`(
IN detailTableName VARCHAR(100),
IN RepSerNo INT,
IN weighmentModeNumber INT,
OUT minDataValue VARCHAR(15),
OUT maxDataValue VARCHAR(15),
OUT avgDataValue VARCHAR(15))
BEGIN
/*LOCAL VARIABLE DECLARATION*/
SET @detailTableName = detailTableName;
SET @weighmentModeNumber = weighmentModeNumber;
/*END OF LOCAL VARIABLE DECLARATION*/
/* MAIN SECTION */
IF ((@weighmentModeNumber = 1) 
OR (@weighmentModeNumber = 9) 
OR (@weighmentModeNumber = 11)) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`DataValue`)  AS DECIMAL(10,5)), CAST(MAX(`DataValue`)  AS DECIMAL(10,5)), ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),4) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
ELSE IF ((@weighmentModeNumber = 3) AND (@detailTableName = 'tbl_cap_detail3_incomplete')) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`NetWeight`)  AS DECIMAL(10,5)), CAST(MAX(`NetWeight`)  AS DECIMAL(10,5)), ROUND(CAST(AVG(`NetWeight`) AS DECIMAL(20,15)),4) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
ELSE IF ((@weighmentModeNumber = 3) 
OR (@weighmentModeNumber = 4)
OR (@weighmentModeNumber = 5)
OR (@weighmentModeNumber = 6)) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`DataValue`)  AS DECIMAL(8,2)), CAST(MAX(`DataValue`)  AS DECIMAL(8,2)), ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),2) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
ELSE IF ((@weighmentModeNumber = 7) AND (@detailTableName = 'tbl_cap_detail7_incomplete')) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`NetWeight`)  AS DECIMAL(8,3)), CAST(MAX(`NetWeight`)  AS DECIMAL(8,3)), ROUND(CAST(AVG(`NetWeight`) AS DECIMAL(20,15)),4) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
        EXECUTE stmt;        
ELSE IF ((@weighmentModeNumber = 7) AND (@detailTableName = 'tbl_tab_detailhtd_incomplete')) THEN
        -- SET @query = CONCAT('SELECT MIN(CAST(`DataValueHard`)  AS DECIMAL(6,1)), MAX(CAST(`DataValueHard`)  AS DECIMAL(6,1)), ROUND(CAST(AVG(`DataValueHard`) AS DECIMAL(20,15)),1) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        SET @query = CONCAT('SELECT MIN(ROUND(CAST(DataValueHard AS DECIMAL(10,5)),1)), MAX(ROUND(CAST(DataValueHard AS DECIMAL(10,5)),1)), ROUND(CAST(AVG(`DataValueHard`) AS DECIMAL(20,15)),1) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;
ELSE IF ((@weighmentModeNumber = 7) AND (@detailTableName = 'tbl_tab_detail7_incomplete')) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`DataValue`)  AS DECIMAL(6,1)), CAST(MAX(`DataValue`)  AS DECIMAL(6,1)), ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),1) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;        
ELSE IF ((@weighmentModeNumber = 8) AND (@detailTableName = 'tbl_cap_detail8')) THEN
        SET @query = CONCAT('SELECT CAST(MIN(`DataValue`)  AS DECIMAL(8,3)), CAST(MAX(`DataValue`)  AS DECIMAL(8,3)), ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),4) INTO @MinValue, @MaxValue, @AverageValue FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
        PREPARE stmt FROM @query;
        EXECUTE stmt;                        
ELSE
        SET @MinValue = 'NA'; 
        SET @MaxValue = 'NA';
        SET @AverageValue = 'NA';
END IF;        END IF;        END IF;        END IF;        END IF;        END IF;        END IF;                                
/* END OF MAIN SECTION */
/* OUT PARAM MAPPING */
SET minDataValue = @MinValue;
SET maxDataValue = @MaxValue;
SET avgDataValue = @AverageValue;
/* END OF OUT PARAM MAPPING */
END */$$
DELIMITER ;

/* Procedure structure for procedure `batchSummaryReportCalculation_Combine` */

/*!50003 DROP PROCEDURE IF EXISTS  `batchSummaryReportCalculation_Combine` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `batchSummaryReportCalculation_Combine`(
IN RepSerNo INT,
IN ParamNo INT,
IN ParamName VARCHAR(100),
IN ProjName VARCHAR(100),
OUT BalanceNom DECIMAL(10,4),
OUT BalanceMin DECIMAL(10,4),
OUT BalanceMax DECIMAL(10,4),
OUT BalanceAvg DECIMAL(10,4),
OUT BalanceT1Neg DECIMAL(10,4),
OUT BalanceT1Pos DECIMAL(10,4),
OUT BalanceT2Neg DECIMAL(10,4),
OUT BalanceT2Pos DECIMAL(10,4),
OUT BalanceRemark VARCHAR(50),
OUT VernierNom DECIMAL(6,2),
OUT VernierT2Neg DECIMAL(6,2),
OUT VernierT2Pos DECIMAL(6,2),
OUT VernierMin DECIMAL(6,2),
OUT VernierMax DECIMAL(6,2),
OUT VernierAvg DECIMAL(6,2),
OUT VernierRemark VARCHAR(50),
OUT HardnessNom DECIMAL(6,1),
OUT HardnessLwr DECIMAL(6,1),
OUT HardnessUpp DECIMAL(6,1),
OUT HardnessMin DECIMAL(6,1),
OUT HardnessMax DECIMAL(6,1),
OUT HardnessAvg DECIMAL(6,1),
OUT HardnessRemark VARCHAR(50),
OUT FriabilityNom DECIMAL(6,3),
OUT FriabilityMin DECIMAL(6,3),
OUT FriabilityMax DECIMAL(6,3),
OUT FriabilityAvg DECIMAL(6,3),
OUT FriabilityRemark VARCHAR(50),
OUT softshelldp INT,
OUT softshelldp_param INT,
OUT SoftshellNom DECIMAL(10,4),
OUT SoftshellMin DECIMAL(10,4),
OUT SoftshellMax DECIMAL(10,4),
OUT SoftshellAvg DECIMAL(10,4),
OUT SoftshellT1Neg DECIMAL(10,4),
OUT SoftshellT1Pos DECIMAL(10,4),
OUT SoftshellT2Neg DECIMAL(10,4),
OUT SoftshellT2Pos DECIMAL(10,4),
OUT SoftshellRemark VARCHAR(50)
)
BEGIN
SET @paramName = ParamName;
SET @ProjName = ProjName;
IF (@paramName = 'Individual') OR (@paramName = 'Individual Layer') OR (@paramName = 'Individual Layer-1') THEN
	SET @queryDP = CONCAT('SELECT DP INTO @DP
	FROM tbl_batchsummary_master',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @queryDP;
	EXECUTE stmt;
	
	IF (@ProjName = "MLH") THEN
           SET @query = CONCAT('SELECT 
	       ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),3),
	       ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),3),
	       ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),3)
	       INTO @DataValueMin, @DataValueMax, @DataValueAvg
	       FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	       PREPARE stmt FROM @query;
	       EXECUTE stmt;
		
	   SET @query = CONCAT('SELECT 
	       ROUND(CAST(`Nom` AS DECIMAL(20,15)),3),
	       ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),3),
	       ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),3),
	       ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),3),
	       ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),3),
	       `NMTLimit`,`LimitOn`, `ReportType`
	       INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @NMTLimit, @LimitOn, @ReportType FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	       PREPARE stmt FROM @query;
	       EXECUTE stmt;
       ELSE
           SET @query = CONCAT('SELECT 
	       ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),',@dp,')
	       INTO @DataValueMin, @DataValueMax, @DataValueAvg
	       FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	       PREPARE stmt FROM @query;
	       EXECUTE stmt;
		
	   SET @query = CONCAT('SELECT 
	       ROUND(CAST(`Nom` AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),',@dp,'),
	       ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),',@dp,'),
	       `NMTLimit`,`LimitOn`, `ReportType`
	       INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @NMTLimit, @LimitOn, @ReportType FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	       PREPARE stmt FROM @query;
	       EXECUTE stmt;
         END IF;
	
	IF @ReportType = 0 THEN /* Standard */
		SET @intNo = @Nom;
	ELSE
		SET @intNo = @DataValueAvg;
	END IF;
	
	IF @LimitOn = 0 THEN /* Actual */
	    IF @Tol1Neg = "0.0000" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    ELSE
		SET @Tol1NegVal = @intNo - @Tol1Neg;
		SET @Tol1PosVal = @intNo + @Tol1Pos;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    END IF;
	ELSE
	    IF @Tol1Neg = "0.0000" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
	    ELSE
		SET @Tol1NegVal = @intNo - (@intNo*(@Tol1Neg/100));
		SET @Tol1PosVal = @intNo + (@intNo*(@Tol1Pos/100));
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
		
	    END IF;
	END IF;
	
	IF @Tol2NegVal > @DataValueMin OR @Tol2PosVal < @DataValueMax THEN
		SET @balRemark = "Out of Limit";
	ELSE
		SET @balRemark = "Within Limit";
	END IF;
		
	SET BalanceNom = @Nom;
	SET BalanceMin = @DataValueMin;
	SET BalanceMax = @DataValueMax;
	SET BalanceAvg = @DataValueAvg;
	SET BalanceT1Neg = @Tol1NegVal;
	SET BalanceT1Pos = @Tol1PosVal;
	SET BalanceT2Neg = @Tol2NegVal;
	SET BalanceT2Pos = @Tol2PosVal;
	SET BalanceRemark = @balRemark;
ELSE IF (@paramName = 'Group') OR (@paramName = 'Group Layer') OR (@paramName = 'Group Layer-1') OR (@paramName = 'Dosa Dry Syrup') THEN
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),4),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),4),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),4)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),4),
	`NMTLimit`,`LimitOn`, `ReportType`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @NMTLimit, @LimitOn, @ReportType FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF @ReportType = 0 THEN /* Standard */
		SET @intNo = @Nom;
	ELSE
		SET @intNo = @DataValueAvg;
	END IF;
	
	IF @LimitOn = 0 THEN /* Actual */
	    IF @Tol1Neg = "0.0000" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    ELSE
		SET @Tol1NegVal = @intNo - @Tol1Neg;
		SET @Tol1PosVal = @intNo + @Tol1Pos;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    END IF;
	ELSE
	    IF @Tol1Neg = "0.0000" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
	    ELSE
		SET @Tol1NegVal = @intNo - (@intNo*(@Tol1Neg/100));
		SET @Tol1PosVal = @intNo + (@intNo*(@Tol1Pos/100));
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
		
	    END IF;
	END IF;
	
	IF @Tol2NegVal > @DataValueMin OR @Tol2PosVal < @DataValueMax THEN
		SET @balRemark = "Out of Limit";
	ELSE
		SET @balRemark = "Within Limit";
	END IF;
		
	SET BalanceNom = @Nom;
	SET BalanceMin = @DataValueMin;
	SET BalanceMax = @DataValueMax;
	SET BalanceAvg = @DataValueAvg;
	SET BalanceT1Neg = @Tol1NegVal;
	SET BalanceT1Pos = @Tol1PosVal;
	SET BalanceT2Neg = @Tol2NegVal;
	SET BalanceT2Pos = @Tol2PosVal;
	SET BalanceRemark = @balRemark;
	
ELSE IF (@paramName = 'Differential') THEN
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),4),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),4),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),4)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detaildiff WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),4),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),4),
	`NMTLimit`,`LimitOn`, `ReportType`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @NMTLimit, @LimitOn, @ReportType 
	FROM tbl_batchsummary_masterdiff WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF @ReportType = 0 THEN /* Standard */
		SET @intNo = @Nom;
	ELSE
		SET @intNo = @DataValueAvg;
	END IF;
	
	IF @LimitOn = 0 THEN /* Actual */
	    IF @Tol1Neg = "0.00" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    ELSE
		SET @Tol1NegVal = @intNo - @Tol1Neg;
		SET @Tol1PosVal = @intNo + @Tol1Pos;
		SET @Tol2NegVal = @intNo - @Tol2Neg;
		SET @Tol2PosVal = @intNo + @Tol2Pos;
	    END IF;
	ELSE
	    IF @Tol1Neg = "0.00" THEN
		SET @Tol1NegVal = 0;
		SET @Tol1PosVal = 0;
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
	    ELSE
		SET @Tol1NegVal = @intNo - (@intNo*(@Tol1Neg/100));
		SET @Tol1PosVal = @intNo + (@intNo*(@Tol1Pos/100));
		SET @Tol2NegVal = @intNo - (@intNo*(@Tol2Neg/100));
		SET @Tol2PosVal = @intNo + (@intNo*(@Tol2Pos/100));
		
	    END IF;
	END IF;
	
	IF @Tol2NegVal > @DataValueMin OR @Tol2PosVal < @DataValueMax THEN
		SET @balRemark = "Out of Limit";
	ELSE
		SET @balRemark = "Within Limit";
	END IF;
		
	SET BalanceNom = @Nom;
	SET BalanceMin = @DataValueMin;
	SET BalanceMax = @DataValueMax;
	SET BalanceAvg = @DataValueAvg;
	SET BalanceT1Neg = @Tol1NegVal;
	SET BalanceT1Pos = @Tol1PosVal;
	SET BalanceT2Neg = @Tol2NegVal;
	SET BalanceT2Pos = @Tol2PosVal;
	SET BalanceRemark = @balRemark;
ELSE IF (@paramName = 'Softshell') THEN
	SET @query = CONCAT('SELECT `DP` INTO @DP FROM tbl_batchsummary_masterdiff WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT BFGCode,ProductName,PVersion,Version,BatchNo INTO @BFGCode,@ProductName,@PVersion,@Version,@BatchNo 
	FROM tbl_batchsummary_masterdiff WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query1 = CONCAT('SELECT Param0_Dp INTO @ParamDP FROM tbl_product_softshell WHERE 
	`ProductId`="',@BFGCode,'" and ProductName="',@ProductName,'" and `ProductVersion`="',@PVersion,'" and `Version`="',@Version,'"
	and `BatchNo`="',@BatchNo,'";');
	PREPARE stmt1 FROM @query1;
	EXECUTE stmt1;
	
	SET softshelldp = @DP;
	SET softshelldp_param = @ParamDP;
	
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),',softshelldp_param,'),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),',softshelldp_param,'),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),',softshelldp_param,'),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),',softshelldp_param,'),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),',softshelldp_param,'),
	`NMTLimit`,`LimitOn`, `ReportType`
	INTO @Nom, @Tol1Neg, @Tol1Pos, @Tol2Neg, @Tol2Pos, @NMTLimit, @LimitOn, @ReportType 
	FROM tbl_batchsummary_masterdiff WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
		
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),',softshelldp,'),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),',softshelldp,'),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detaildiff WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		
	IF @ReportType = 0 THEN /* Standard */
		SET @intNo = @Nom;
	ELSE
		SET @intNo = @DataValueAvg;
	END IF;
	
	
	SET @Tol2NegVal = @Tol2Neg;
	SET @Tol2PosVal = @Tol2Pos;
	
	IF @Tol2NegVal > @DataValueMin OR @Tol2PosVal < @DataValueMax THEN
		SET @balRemark = "Out of Limit";
	ELSE
		SET @balRemark = "Within Limit";
	END IF;
		
	SET SoftshellNom = @Nom;
	SET SoftshellMin = @DataValueMin;
	SET SoftshellMax = @DataValueMax;
	SET SoftshellAvg = @DataValueAvg;
	SET SoftshellT1Neg = @Tol1NegVal;
	SET SoftshellT1Pos = @Tol1PosVal;
	SET SoftshellT2Neg = @Tol2NegVal;
	SET SoftshellT2Pos = @Tol2PosVal;
	SET SoftshellRemark = @balRemark;
		
ELSE IF (@paramName = 'Thickness') OR (@paramName = 'Breadth') OR (@paramName = 'Diameter') OR (@paramName = 'Length') OR (@paramName = 'Locked Length') THEN   
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),2),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),2),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),2)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Neg` AS DECIMAL(20,15)),2),
	ROUND(CAST(`Tol2Pos` AS DECIMAL(20,15)),2)
	INTO @Nom, @Tol2Neg, @Tol2Pos FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	IF (@Nom - @Tol2Neg) > @DataValueMin OR (@Nom + @Tol2Pos) < @DataValueMax THEN
		SET @verRemark = "Out of Limit";
	ELSE
		SET @verRemark = "Within Limit";
	END IF;
	SET VernierNom = @Nom;
	SET VernierT2Neg = (@Nom - @Tol2Neg);
	SET VernierT2Pos = (@Nom + @Tol2Pos);
	SET VernierMin = @DataValueMin;
	SET VernierMax = @DataValueMax;
	SET VernierAvg = @DataValueAvg;
	SET VernierRemark = @verRemark;
	
ELSE IF (@paramName = 'Hardness') THEN  
	IF (@ProjName = "MVL") THEN
		SET @dp = 0;
	ELSEIF (@ProjName = "MLH") THEN
		SET @dp = 0;
	ELSE
		SET @dp = 1;
	END IF;
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),',@dp,'),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),',@dp,'),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),',@dp,')
	INTO @Min,@Max,@Avg
	FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	INSERT INTO test(str) VALUES(@query);
	SET @query = CONCAT('SELECT 
	ROUND(CAST(`Nom` AS DECIMAL(20,15)),',@dp,'),
	ROUND(CAST(`Tol1Neg` AS DECIMAL(20,15)),',@dp,'),
	ROUND(CAST(`Tol1Pos` AS DECIMAL(20,15)),',@dp,')
	INTO @Nom, @Tol1Neg, @Tol1Pos FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF (@Nom != "99999.00000")  THEN
		SET HardnessNom = @Nom;
		
		IF @Tol1Neg > @Min OR @Tol1Pos < @Max THEN
			SET @hardRemark = "Out of Limit";
		ELSE
			SET @hardRemark = "Within Limit";
		END IF;
	ELSE
		SET HardnessNom = "0.00";
		SET @hardRemark = "NA";
	END IF;
	
	SET HardnessLwr = @Tol1Neg;
	SET HardnessUpp = @Tol1Pos;
	SET HardnessMin = @Min;
	SET HardnessMax = @Max;
	SET HardnessAvg = @Avg;
	SET HardnessRemark = @hardRemark;
	
ELSE IF (@paramName = 'Friability') THEN   
	SET @query = CONCAT('SELECT 
	ROUND(CAST(MIN(`Min`) AS DECIMAL(20,15)),3),
	ROUND(CAST(MAX(`Max`) AS DECIMAL(20,15)),3),
	ROUND(CAST(AVG(`Avg`) AS DECIMAL(20,15)),3)
	INTO @DataValueMin, @DataValueMax, @DataValueAvg
	FROM tbl_batchsummary_detail',ParamNo,' WHERE RepSerNo=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ROUND(CAST(`Nom` AS DECIMAL(20,15)),3) INTO @Nom FROM tbl_batchsummary_master',ParamNo,' WHERE `RepSerNo`=',RepSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF @Nom < @DataValueMax THEN
		SET @friRemark = "Out of Limit";
	ELSE
		SET @friRemark = "Within Limit";
	END IF;
	
	SET FriabilityNom = @Nom;
	SET FriabilityMin = @DataValueMin;
	SET FriabilityMax = @DataValueMax;
	SET FriabilityAvg = @DataValueAvg;
	SET FriabilityRemark = @friRemark;
END IF;	
END IF;	
END IF;	
END IF;
END IF;
END IF;
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `binSummaryReportCalculation` */

/*!50003 DROP PROCEDURE IF EXISTS  `binSummaryReportCalculation` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `binSummaryReportCalculation`(
IN HMIID INT,
OUT total DECIMAL(10,2)
)
BEGIN
SET @query = CONCAT('SELECT 
	ROUND(CAST(SUM(`bin_NetWeight`) AS DECIMAL(20,15)),2)
	INTO @total
	FROM tbl_bin_summary_temp WHERE `HMIID`=',HMIID,' limit 1;');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
SET total = @total;
END */$$
DELIMITER ;

/* Procedure structure for procedure `calculateMinMaxPercentage` */

/*!50003 DROP PROCEDURE IF EXISTS  `calculateMinMaxPercentage` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `calculateMinMaxPercentage`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT nominal DECIMAL(10,5),
OUT minWeight DECIMAL(10,5),
OUT maxWeight DECIMAL(10,5),
OUT minPercentage DECIMAL(10,2),
OUT maxPercentage DECIMAL(10,2),
OUT average DECIMAL(10,5),
OUT standardDeviation DECIMAL(10,5),
OUT rptHeading VARCHAR(100),
OUT T1Neg DECIMAL(10,5),
OUT T1Pos DECIMAL(10,5),
OUT T2Neg DECIMAL(10,5),
OUT T2Pos DECIMAL(10,5),
OUT limiton INT,
OUT graphType INT,
OUT nmtTab INT,
OUT labelT1 VARCHAR(100),
OUT labelT2 VARCHAR(100),
OUT valueT1 VARCHAR(100),
OUT valueT2 VARCHAR(100),
OUT reportRemark VARCHAR(100),
OUT RecSeqNo INT
)
BEGIN
SET @query = CONCAT('SELECT `Nom`, `limitOn`, `GraphType`, `WgmtModeNo`, `T1NMTTab`, `T1NegTol`, `T1PosTol`, `T2NegTol`, `T2PosTol`,`DecimalPoint` INTO @NominalFromMaster, @limitOnFromMaster, @graphTypeMaster, @WgmtModeNoFromMaster, @T1NMTTabFromMaster, @T1Neg, @T1Pos, @T2Neg, @T2Pos, @DecimalPoint FROM ',strMasterTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),',@DecimalPoint + 1,'),
ROUND(CAST(STDDEV_SAMP(`DataValue`) AS DECIMAL(20,15)),5),
CAST(MIN(`DataValue`)  AS DECIMAL(10,5)),
CAST(MAX(`DataValue`)  AS DECIMAL(10,5)) INTO @AverageFromDetail,@SDFromDetail,@MinFromDetail,@MaxFromDetail FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/* Following Average will be used to calculate Min% and Max% Hence RoundedUP by `DecimalPoint` + 1 in Master*/
SET @AverageFromDetail = ROUND(CAST(@AverageFromDetail AS DECIMAL(20,15)),CAST(@DecimalPoint AS DECIMAL(5,0)) + 1);
SET @query = CONCAT('SELECT COUNT(*) INTO @totalnum FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
IF @graphTypeMaster = 0 THEN /* Standard */
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-@NominalFromMaster)/@NominalFromMaster)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@NominalFromMaster - MIN(`DataValue`))/@NominalFromMaster)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF @limitOnFromMaster = 0 THEN /* Actual */
		SET @T1NegTemp = @NominalFromMaster - @T1Neg;
		SET @T1PosTemp = @T1Pos + @NominalFromMaster;
		SET @T2NegTemp = @NominalFromMaster - @T2Neg;
		SET @T2PosTemp = @T2Pos + @NominalFromMaster; 
		SET @labelT1 = 'NA';
		SET @labelT2 = 'NA';
		IF @T1Neg = 0 THEN /* T1 == 0 */
			SET @valueT1 = CONCAT('NA - NA');
			SET @T1NegTemp = 0;
			SET @T1PosTemp = 0;
		ELSE		/* T1 != 0 */
			SET @valueT1 = CONCAT(@T1NegTemp,' - ',@T1PosTemp);
		END IF;	
		SET @valueT2 = CONCAT(@T2NegTemp,' - ',@T2PosTemp);
	ELSE /*  Percentage */
		SET @T1NegTemp = @NominalFromMaster - (@NominalFromMaster*@T1Neg)/100;
		SET @T1PosTemp = @NominalFromMaster + (@NominalFromMaster*@T1Pos)/100;
		SET @T2NegTemp = @NominalFromMaster - (@NominalFromMaster*@T2Neg)/100;
		SET @T2PosTemp = @NominalFromMaster + (@NominalFromMaster*@T2Pos)/100;
		SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' tablets by ±',@T1Neg);
		SET @labelT2 = CONCAT('None of tablets by ±',@T2Neg);
		IF @T1Neg = 0 THEN /* T1 == 0 */
			SET @valueT1 = CONCAT('NA - NA');
			SET @T1NegTemp = 0;
			SET @T1PosTemp = 0;
		ELSE		/* T1 != 0 */
			SET @valueT1 = CONCAT(@T1NegTemp,' - ',@T1PosTemp);
		END IF;	
		SET @valueT2 = CONCAT(@T2NegTemp,' - ',@T2PosTemp);
	END IF;
ELSE /* Average */
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-@AverageFromDetail)/@AverageFromDetail)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@AverageFromDetail - MIN(`DataValue`))/@AverageFromDetail)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
		IF @limitOnFromMaster = 0 THEN /* Actual */
			SET @T1NegTemp = @AverageFromDetail - @T1Neg;
			SET @T1PosTemp = @T1Pos + @AverageFromDetail;
			SET @T2NegTemp = @AverageFromDetail - @T2Neg;
			SET @T2PosTemp = @T2Pos + @AverageFromDetail; 
			SET @labelT1 = 'NA';
			SET @labelT2 = 'NA';
			IF @T1Neg = 0 THEN /* T1 == 0 */
				SET @valueT1 = CONCAT('NA - NA');
				SET @T1NegTemp = 0;
				SET @T1PosTemp = 0;
			ELSE		/* T1 != 0 */
				SET @valueT1 = CONCAT(@T1NegTemp,' - ',@T1PosTemp);
			END IF;	
			SET @valueT2 = CONCAT(@T2NegTemp,' - ',@T2PosTemp);
	ELSE /*  Percentage */
		SET @T1NegTemp = @AverageFromDetail - (@AverageFromDetail*@T1Neg)/100;
		SET @T1PosTemp = @AverageFromDetail + (@AverageFromDetail*@T1Pos)/100;
		SET @T2NegTemp = @AverageFromDetail - (@AverageFromDetail*@T2Neg)/100;
		SET @T2PosTemp = @AverageFromDetail + (@AverageFromDetail*@T2Pos)/100;
		SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' tablets by ±',@T1Neg);
		SET @labelT2 = CONCAT('None of tablets by ±',@T2Neg);
		IF @T1Neg = 0 THEN /* T1 == 0 */
			SET @valueT1 = CONCAT('NA - NA');
			SET @T1NegTemp = 0;
			SET @T1PosTemp = 0;
		ELSE		/* T1 != 0 */
			SET @valueT1 = CONCAT(@T1NegTemp,' - ',@T1PosTemp);
		END IF;	
		SET @valueT2 = CONCAT(@T2NegTemp,' - ',@T2PosTemp);
END IF;
END IF;
/* Report Heading */
IF @WgmtModeNoFromMaster = 1 THEN
		SET @rptHeading = 'INDIVIDUAL  WEIGHT VARIATION RECORD';
	ELSE IF @WgmtModeNoFromMaster = 9 THEN
		SET @rptHeading = 'INDIVIDUAL LAYER 1 WEIGHT VARIATION RECORD'; 
		ELSE IF @WgmtModeNoFromMaster = 11 THEN
				SET @rptHeading = 'INDIVIDUAL LAYER 2 WEIGHT VARIATION RECORD'; 
			ELSE 
				SET @rptHeading = 'INDIVIDUAL WEIGHT VARIATION RECORD';
		END IF;
	END IF;
END IF;
/* Report Heading */
 /*VARIABLE MAPPING */
SET nominal = @NominalFromMaster;
SET minWeight = @MinFromDetail;
SET maxWeight = @MaxFromDetail;
SET minPercentage = @MinimumPercentage;
SET maxPercentage = @MaximumPercentage; 
SET average = @AverageFromDetail; 
SET rptHeading = @rptHeading;
SET T1Neg = @T1Neg;
SET T1Pos = @T1Pos;
SET T2Neg = @T2Neg;
SET T2Pos = @T2Pos;
SET limiton = @limitOnFromMaster;
SET graphType = @graphTypeMaster;
SET nmtTab = @T1NMTTabFromMaster;
SET labelT1 = @labelT1;
SET labelT2 = @labelT2;
SET valueT1 = @valueT1;
SET valueT2 = @valueT2;
SET reportRemark = 'NA';
SET RecSeqNo = @totalnum;
IF (@SDFromDetail IS NULL OR @SDFromDetail = '')  THEN
	SET standardDeviation 	= 0;
ELSE
	SET standardDeviation 	= @SDFromDetail;
END IF;
/*END OF VARIABLE MAPPING
INSERT INTO test (str) VALUES (rptHeading);*/
END */$$
DELIMITER ;

/* Procedure structure for procedure `calculateReportStatistics` */

/*!50003 DROP PROCEDURE IF EXISTS  `calculateReportStatistics` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `calculateReportStatistics`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
IN columnName VARCHAR(100),
IN graphType VARCHAR(100),
IN nominal DECIMAL(10,5),
IN lowT1Limit DECIMAL(10,5),
IN uppT1Limit DECIMAL(10,5),
IN lowT2Limit DECIMAL(10,5),
IN uppT2Limit DECIMAL(10,5),
OUT averageFromDetail DECIMAL(10,5),
OUT minWeight DECIMAL(10,5),
OUT maxWeight DECIMAL(10,5),
OUT minPercentage DECIMAL(10,5),
OUT maxPercentage DECIMAL(10,5),
OUT standardDeviation DECIMAL(10,5),
OUT aboveLimit1 INT,
OUT belowLimit1 INT,
OUT aboveLimit2 INT,
OUT belowLimit2 INT)
BEGIN
/* -- UPDATE LOG :
	-1- 31052021 : Added condition to override T1 limit counts
*/	
/*CALCULATE AVG,STDDEV,MIN,MAX */
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(',columnName,') AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(',columnName,') AS DECIMAL(20,15)),5),
CAST(MIN(',columnName,')  AS DECIMAL(10,5)),
CAST(MAX(',columnName,')  AS DECIMAL(10,5))
INTO @AverageFromDetail,@Stdev,@MinFromDetail,@MaxFromDetail
FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
IF (@Stdev IS NULL OR @Stdev = '')  THEN
	SET standardDeviation = 0;
ELSE
	SET standardDeviation = @Stdev;
END IF;
/*END OF CALCULATE AVG,STDDEV,MIN,MAX */
/* CALCULATE MIN MAX PERCENTAGE*/
IF graphType = "Standard" THEN /*STANDARD*/
SET @query = CONCAT('SELECT 
ABS(ROUND(CAST((((MAX(',columnName,')-',nominal,')/',nominal,')*100) AS DECIMAL(20,15)),2)),
ABS(ROUND(CAST((((',nominal,' - MIN(',columnName,'))/',nominal,')*100) AS DECIMAL(20,15)),2))
INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
ELSE /*AVERAGE*/
SET @query = CONCAT('SELECT 
ABS(ROUND(CAST((((MAX(',columnName,')-@AverageFromDetail)/@AverageFromDetail)*100) AS DECIMAL(20,15)),2)),
ABS(ROUND(CAST((((@AverageFromDetail - MIN(',columnName,'))/@AverageFromDetail)*100) AS DECIMAL(20,15)),2))
INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
END IF;
/* CALCULATE END OF MIN MAX PERCENTAGE*/
/*CALCULATE ABOVE LIMIT-BELOW LIMIT*/
SET @query = CONCAT('SELECT 
SUM(CASE WHEN (',columnName,' < ',lowT2Limit,') THEN 1 ELSE 0 END),
SUM(CASE WHEN (',columnName,' > ',uppT2Limit,') THEN 1 ELSE 0 END),
SUM(CASE WHEN (',columnName,' < ',lowT1Limit,'  AND ',columnName,' >= ',lowT2Limit,') THEN 1 ELSE 0 END),
SUM(CASE WHEN (',columnName,' > ',uppT1Limit,' AND ',columnName,' <= ',uppT2Limit,') THEN 1 ELSE 0 END)  
INTO @belowLimitT2Count,@aboveLimitT2Count,@belowLimitT1Count,@aboveLimitT1Count
FROM ',strDetailTable,'
WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/* IF (lowT1Limit and uppT1Limit) both are same which means T1 is not applicable for respective product parameter
Hence manually overring to 0 always*/
IF (lowT1Limit=uppT1Limit) THEN
	SET @belowLimitT1Count = 0;
	SET @aboveLimitT1Count = 0;
END IF;
/*END OF CALCULATE ABOVE LIMIT/BELOW LIMIT*/
SET averageFromDetail = @AverageFromDetail;
SET minWeight = @MinFromDetail;
SET maxWeight = @MaxFromDetail;
SET minPercentage = @MinimumPercentage;
SET maxPercentage = @MaximumPercentage;
SET aboveLimit1 = @aboveLimitT1Count;
SET belowLimit1 = @belowLimitT1Count;
SET aboveLimit2 = @aboveLimitT2Count;
SET belowLimit2 = @belowLimitT2Count;
END */$$
DELIMITER ;

/* Procedure structure for procedure `edit_status_counter` */

/*!50003 DROP PROCEDURE IF EXISTS  `edit_status_counter` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `edit_status_counter`(IN tableName VARCHAR(50))
BEGIN
	DECLARE timeoutperiod INT DEFAULT 0;
	DECLARE resetcounter INT DEFAULT 0;
	DECLARE seconds INT DEFAULT 0;
	DECLARE tblName VARCHAR(50);
	DECLARE queryString VARCHAR(50);
    
    SET @tblName = tableName;
	SELECT tbl_config_TimeoutPeriod INTO timeoutperiod FROM tbl_setallparameter WHERE config_id = 1;
	    
	    SET seconds=timeoutperiod*60;
		    
	    SET resetCounter = seconds/5;
	    
        SET @queryString = CONCAT('UPDATE ', @tblName ,' SET editCounter = editCounter+1 WHERE locked=1'); 
		PREPARE stmt1 FROM  @queryString; 
		EXECUTE stmt1; 
		DEALLOCATE PREPARE stmt1;
        
        SET @queryString = CONCAT('UPDATE ', @tblName ,' SET locked=0,editCounter = 0 WHERE editCounter>=',resetCounter); 
		PREPARE stmt1 FROM  @queryString; 
		EXECUTE stmt1; 
		DEALLOCATE PREPARE stmt1; 
	           
    END */$$
DELIMITER ;

/* Procedure structure for procedure `FriTapLodArchived` */

/*!50003 DROP PROCEDURE IF EXISTS  `FriTapLodArchived` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `FriTapLodArchived`(
      IN tableName_master_archive VARCHAR(100),
      IN tableName_master VARCHAR(100)
)
BEGIN
	    DECLARE str_tableName_master_archive VARCHAR(100);
	    DECLARE str_tableName_master VARCHAR(100);
	    
	    SET @str_tableName_master_archive = tableName_master_archive;   
	    SET @str_tableName_master = tableName_master;  
            
	    SET @insertIn_FriTapLod_ArchiveTable = CONCAT("INSERT INTO ",@str_tableName_master_archive," SELECT * FROM ",@str_tableName_master,"  WHERE BatchComplete=1 and 
	    PrDate<DATE_SUB(CURDATE(),INTERVAL (SELECT tbl_config_ArchivePeriod FROM tbl_setallparameter WHERE config_id=1) DAY)"); 
	    PREPARE stmt1 FROM  @insertIn_FriTapLod_ArchiveTable; 
	    EXECUTE stmt1;
	      
	    SET @deleteFrom_FriTapLod_Table = CONCAT("delete from  ",@str_tableName_master," where  BatchComplete=1 and
	    PrDate<DATE_SUB(CURDATE(),INTERVAL (SELECT tbl_config_ArchivePeriod FROM tbl_setallparameter WHERE config_id=1) DAY) "); 
	    PREPARE stmt2 FROM  @deleteFrom_FriTapLod_Table; 
	    EXECUTE stmt2;  
	    
    END */$$
DELIMITER ;

/* Procedure structure for procedure `RepeatabilityPercentage` */

/*!50003 DROP PROCEDURE IF EXISTS  `RepeatabilityPercentage` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `RepeatabilityPercentage`(
IN repSerNo INT,
IN balId VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT balLeastCount DECIMAL(10,5),
OUT balDSNW INT(10),
OUT repeatabilityPer DECIMAL(10,2),
OUT repFormula VARCHAR(20),
OUT result VARCHAR(100)
)
BEGIN
SET @query = CONCAT('SELECT Bal_LeastCnt*0.41,Bal_DSNW  INTO @leastCnt, @dsnw FROM tbl_balance WHERE Bal_ID="',balId,'";'); 
PREPARE stmt FROM @query; 
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(STDDEV_SAMP(Repet_ActualWt) AS DECIMAL(20,15)),4) INTO @SDFromDetail FROM ',strDetailTable,' WHERE Repet_RepNo=',repSerNo,';'); 
PREPARE stmt FROM @query; 
EXECUTE stmt;
/* change in formula as per customer request received from CP4I 040121 */
IF (@SDFromDetail <= @leastCnt) THEN
	SET @repeatabilityPer = ((2*@leastCnt)/@dsnw)*100;
	SET repFormula = "2 x (0.41xd)";
ELSE
	SET @repeatabilityPer = ((2*@SDFromDetail)/@dsnw)*100;
	SET repFormula = "2 x S.D.";
END IF;
IF (@repeatabilityPer <= 0.10) THEN
	SET @result = 'Complies';
ELSE
	SET @result = 'Not Complies';
END IF;
SET balLeastCount = @leastCnt; 
SET balDSNW = @dsnw;
SET repeatabilityPer = @repeatabilityPer;
SET result = @result;
END */$$
DELIMITER ;

/* Procedure structure for procedure `RepeatabilityPeriodicPercentage` */

/*!50003 DROP PROCEDURE IF EXISTS  `RepeatabilityPeriodicPercentage` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `RepeatabilityPeriodicPercentage`(
IN repSerNo INT,
IN balId VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT balLeastCount DECIMAL(10,5),
OUT balDSNW INT(10),
OUT repeatabilityPer DECIMAL(10,2),
OUT result VARCHAR(100),
OUT LResult VARCHAR(100),
OUT EResult VARCHAR(100)
)
BEGIN
SET @query = CONCAT('SELECT `Bal_LeastCnt`*0.41,`Bal_DSNW`  INTO @leastCnt, @dsnw FROM `tbl_balance` WHERE `Bal_ID`="',balId,'";'); 
PREPARE stmt FROM @query; 
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(STDDEV_SAMP(`Repet_ActualWt`) AS DECIMAL(20,15)),4) INTO @SDFromDetail FROM ',strDetailTable,' WHERE `Repet_RepNo`=',repSerNo,';'); 
PREPARE stmt FROM @query; 
EXECUTE stmt;
SET @query = CONCAT('select `Linear_MaxCap` INTO @BalanceCapacity  from `tbl_calibration_linearity_master` WHERE `Linear_RepNo`="',repSerNo,'";'); 
PREPARE stmt FROM @query; 
EXECUTE stmt;
SET @repeatAns=(2*@SDFromDetail*@leastCnt*820);
SET @repeatCondition=(@repeatAns/100);
IF (@BalanceCapacity > 310) THEN 
	IF (@SDFromDetail < @leastCnt) THEN
		SET @repeatabilityPer = ((2*@SDFromDetail*10)*100)/@dsnw;
	ELSE
		SET @repeatabilityPer = ((2*@leastCnt)*100)/@dsnw;
	END IF;
ELSE
	IF (@SDFromDetail < @leastCnt) THEN
		SET @repeatabilityPer = ((2*@leastCnt)*100)/@dsnw;
	ELSE
		SET @repeatabilityPer = ((2*@SDFromDetail*10)*100)/@dsnw;
	END IF;
END IF;
IF (@repeatabilityPer <= @repeatCondition) THEN
	SET @result = 'Complies';
ELSE
	SET @result = 'Not Complies';
END IF;
SET @LResult='Complies';
SET @EResult='Complies';
SET balLeastCount = @leastCnt; 
SET balDSNW = @dsnw;
SET repeatabilityPer = @repeatabilityPer;
SET result = @result;
SET LResult= @LResult;
SET EResult= @EResult;
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationDifferential` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationDifferential` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationDifferential`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
IN strProjectName VARCHAR(100),
OUT nominal DECIMAL(10,5),
OUT minWeight DECIMAL(10,5),
OUT maxWeight DECIMAL(10,5),
OUT minPercentage DECIMAL(10,2),
OUT maxPercentage DECIMAL(10,2),
OUT average DECIMAL(10,5),
OUT T1Neg DECIMAL(10,5),
OUT T1Pos DECIMAL(10,5),
OUT T2Neg DECIMAL(10,5),
OUT T2Pos DECIMAL(10,5),
OUT belowLimitT2Count SMALLINT,
OUT aboveLimitT2Count SMALLINT,
OUT belowLimitT1Count SMALLINT,
OUT aboveLimitT1Count SMALLINT,
OUT labelT2 VARCHAR(100),
OUT labelT1 VARCHAR(100),
OUT Stdev DECIMAL(10,5),
OUT result VARCHAR(20),
OUT minWeightFill DECIMAL(10,5),
OUT maxWeightFill DECIMAL(10,5),
OUT minPercentageFill DECIMAL(10,2),
OUT maxPercentageFill DECIMAL(10,2),
OUT averageFill DECIMAL(10,5),
OUT StdevFill DECIMAL(10,5),
OUT minWeightEmpty DECIMAL(10,5),
OUT maxWeightEmpty DECIMAL(10,5),
OUT minPercentageEmpty DECIMAL(10,2),
OUT maxPercentageEmpty DECIMAL(10,2),
OUT averageEmpty DECIMAL(10,5),
OUT StdevEmpty DECIMAL(10,5),
OUT sumOfNet DECIMAL(10,5),
OUT sumOfFill DECIMAL(10,5),
OUT sumOfEmpty DECIMAL(10,5)
)
BEGIN
SET @strProjectName = strProjectName;
/* CALCULATION 1: Get Nominal, Tolerance(s), limitOn FROM MASTER */
SET @query = CONCAT('SELECT `NomNet`,`NomEmpty`,`Nom`,`GraphTypeNet`,`GraphType`,`GraphTypeEmpty`,
CAST(`T1NegNet` AS DECIMAL(10,5)), CAST(`T1PosNet`  AS DECIMAL(10,5)), CAST(`T2NegNet` AS DECIMAL(10,5)), CAST(`T2PosNet` AS DECIMAL(10,5)), 
`limitOnNet`,`NMTTabNet`, DecimalPoint INTO  
 @NominalFromMaster,@NominalFromMasterEmpty,@NominalFromMasterFill,@graphTypeMaster,@graphTypeMasterFill,@graphTypeMasterEmpty,
 @T1Neg, @T1Pos, @T2Neg, @T2Pos,@limitOnFromMaster,@T1NMTTabFromMaster, @DecimalPoint  FROM ', strMasterTable ,' WHERE `RepSerNo`= ', repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/* END OF CALCULATION 1 */
/* CALCULATION 3: Sum */
SET @query = CONCAT('SELECT ROUND(CAST(SUM(`NetWeight`) AS DECIMAL(20,15)),5),
ROUND(CAST(SUM(`DataValue`) AS DECIMAL(20,15)),5),
ROUND(CAST(SUM(`DataValue1`) AS DECIMAL(20,15)),5)
INTO @SumOfNetWeight,@SumOfFillWeight,@SumOfEmptyWeight
FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/* END OF CALCULATION 3 */
/* CALCULATION 2: Average, Standard Deviation, Min, Max, MaximumPercentage, MinimumPercentage */
SET @query = CONCAT('SELECT ROUND(CAST(AVG(`NetWeight`) AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(`NetWeight`) AS DECIMAL(20,15)),5),
CAST(MIN(`NetWeight`)  AS DECIMAL(10,5)),
CAST(MAX(`NetWeight`)  AS DECIMAL(10,5)),
ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(`DataValue`) AS DECIMAL(20,15)),5),
CAST(MIN(`DataValue`)  AS DECIMAL(10,5)),
CAST(MAX(`DataValue`)  AS DECIMAL(10,5)),
ROUND(CAST(AVG(`DataValue1`) AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(`DataValue1`) AS DECIMAL(20,15)),5),
CAST(MIN(`DataValue1`)  AS DECIMAL(10,5)),
CAST(MAX(`DataValue1`)  AS DECIMAL(10,5))
INTO @AverageFromDetail,@Stdev,@MinFromDetail,@MaxFromDetail,@AverageFromDetailFill,@StdevFill,@MinFromDetailFill,@MaxFromDetailFill,
@AverageFromDetailEmpty,@StdevEmpty,@MinFromDetailEmpty,@MaxFromDetailEmpty
FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
IF (@Stdev IS NULL OR @Stdev = '')  THEN
	SET Stdev 	= 0;
ELSE
	SET Stdev = @Stdev;
END IF;
IF (@StdevFill IS NULL OR @StdevFill = '')  THEN
	SET StdevFill 	= 0;
ELSE
	SET StdevFill = @StdevFill;
END IF;
IF (@StdevEmpty IS NULL OR @StdevEmpty = '')  THEN
	SET StdevEmpty 	= 0;
ELSE
	SET StdevEmpty = @StdevEmpty;
END IF;
/* Following Average will be used to calculate Min% and Max% Hence RoundedUP by `DecimalPoint` + 1 in Master*/
/* Cipla4 indore requirement-  label limits should be displayed in 2 digits 21/04/2021*/
SET @AverageFromDetail = ROUND(CAST(@AverageFromDetail AS DECIMAL(20,15)),CAST(@DecimalPoint AS DECIMAL(5,0)) + 1);
IF (@graphTypeMaster = 0 AND @graphTypeMaster = 'Standard') THEN /* Standard */
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`NetWeight`)-@NominalFromMaster)/@NominalFromMaster)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@NominalFromMaster - MIN(`NetWeight`))/@NominalFromMaster)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-@NominalFromMasterFill)/@NominalFromMasterFill)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@NominalFromMasterFill - MIN(`DataValue`))/@NominalFromMasterFill)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentageFill,@MinimumPercentageFill FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
		SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue1`)-@NominalFromMasterEmpty)/@NominalFromMasterEmpty)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@NominalFromMasterEmpty - MIN(`DataValue1`))/@NominalFromMasterEmpty)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentageEmpty,@MinimumPercentageEmpty FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
/* ABOVE OR BELOW LIMIT CALCULATION */
/* NOTE: T1Neg and T1Pos will be ZERO or NULL Always Hence Not Considered */
IF @limitOnFromMaster = 1 THEN   /*Percentage*/
	/** change 3 to 5 as per client requirement **/
	SET @lowerT2Limit = CAST((@NominalFromMaster - (@NominalFromMaster *(@T2Neg/100))) AS DECIMAL(10,5));
	SET @upperT2Limit = CAST((@NominalFromMaster + (@NominalFromMaster *(@T2Pos/100))) AS DECIMAL(10,5));
	SET @lowerT1Limit = CAST((@NominalFromMaster - (@NominalFromMaster *(@T1Neg/100))) AS DECIMAL(10,5));
	SET @upperT1Limit = CAST((@NominalFromMaster +(@NominalFromMaster *(@T1Pos/100)))  AS DECIMAL(10,5));
	IF @T1Neg = 0  THEN
		SET @valueT1 = CONCAT('NA - NA');
		SET @labelT1 = 'NA';
		SET @lowerT1Limit = 0;
		SET @upperT1Limit = 0;
	ELSE		
		SET @valueT1 = CONCAT(@lowerT1Limit ,' % ',' - ', @upperT1Limit ,' % ');
		IF (@T1Neg = @T1Pos) THEN 
			IF(@strProjectName = 'CB') THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ');
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ');
			END IF;	
		ELSE
			IF(@strProjectName = 'CB') THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ','-',CAST(@T1Pos AS DECIMAL(10,2)) ,' % ');  
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ','-',CAST(@T1Pos AS DECIMAL(10,2)) ,' % ');  
			END IF;
		END IF;
		
	END IF;	
	IF @T2Neg = 0  THEN
		SET @valueT2 = CONCAT('NA - NA');
		SET @labelT2 = 'NA';
	ELSE		/* T1 != 0 */
		SET @valueT2 = CONCAT(@lowerT1Limit ,' % ',' - ', @upperT1Limit ,' % ');
		IF (@T2Neg = @T2Pos) THEN 
		  IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT(' ± '   , CAST(@T2Neg AS DECIMAL(10,2)) ,' % of std wt');
		  ELSE
			SET @labelT2 = CONCAT('None of capsules by ± '   , CAST(@T2Neg AS DECIMAL(10,2)) ,' % ');
	          END IF;		
		ELSE
		  IF strProjectName = 'MLH' THEN
			SET @labelT2 = CONCAT(' ± ' , CAST(@T2Neg AS DECIMAL(10,2)) ,' % ','-',CAST(@T2Pos AS DECIMAL(10,2)) ,' % of std wt'); 
		  ELSE 
			SET @labelT2 = CONCAT('None of capsules by ± ' , CAST(@T2Neg AS DECIMAL(10,2)) ,' % ','-',CAST(@T2Pos AS DECIMAL(10,2)) ,' % '); 
		  END IF;	
		END IF;
	END IF;	
ELSE
	/** change 3 to 5 as per client requirement **/
	SET @lowerT2Limit = CAST((@NominalFromMaster - @T2Neg) AS DECIMAL(10,5));
	SET @upperT2Limit = CAST((@NominalFromMaster + @T2Pos) AS DECIMAL(10,5));
	SET @lowerT1Limit = CAST((@NominalFromMaster - @T1Neg) AS DECIMAL(10,5));
	SET @upperT1Limit = CAST((@NominalFromMaster + @T1Pos) AS DECIMAL(10,5));
	IF @T1Neg = 0  THEN
		SET @valueT1 = CONCAT('NA - NA');
		SET @labelT1 = 'NA';
		SET @lowerT1Limit = 0;
		SET @upperT1Limit = 0;
	ELSE		/* T1 != 0 */
		SET @valueT1 = CONCAT(@lowerT1Limit,' g ','-',@upperT1Limit,' g ');
		IF (@T1Neg = @T1Pos) THEN 
			IF(@strProjectName = 'CB')THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ');
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ');
			END IF;
		ELSE
			IF(@strProjectName = 'CB')THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ','-',CAST(@T1Pos AS DECIMAL(10,2)),' g '); 
			 ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ','-',CAST(@T1Pos AS DECIMAL(10,2)),' g '); 
			END IF;
		END IF;
	END IF;	
	IF @T2Neg = 0  THEN
		SET @valueT2 = CONCAT('NA - NA');
		SET @labelT2 = 'NA';
	ELSE		/* T1 != 0 */
		SET @valueT2 = CONCAT(@lowerT2Limit,' g ',' - ', @upperT2Limit ,' g ');
		IF (@T2Neg = @T2Pos) THEN 
		  IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT(' ± ',CAST(@T2Neg AS DECIMAL(10,2)) ,' g of std wt  ');
		      ELSE
			SET @labelT2 = CONCAT('None of capsules by ± ',CAST(@T2Neg AS DECIMAL(10,2)) ,' g ');
		      END IF;	
		ELSE
		  IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT('± ' ,CAST(@T2Neg AS DECIMAL(10,2)),' g ',' - ',CAST(@T2Pos AS DECIMAL(10,2)) ,' g of std wt ');  
		     ELSE
			SET @labelT2 = CONCAT('None of capsules by ± ' ,CAST(@T2Neg AS DECIMAL(10,2)),' g ',' - ',CAST(@T2Pos AS DECIMAL(10,2)) ,' g ');  
		   END IF;
		END IF;
	END IF;
END IF;
ELSE /* Average */
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`NetWeight`)-@AverageFromDetail)/@AverageFromDetail)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@AverageFromDetail - MIN(`NetWeight`))/@AverageFromDetail)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentage,@MinimumPercentage FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue`)-@AverageFromDetailFill)/@AverageFromDetailFill)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@AverageFromDetailFill - MIN(`DataValue`))/@AverageFromDetailFill)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentageFill,@MinimumPercentageFill FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	SET @query = CONCAT('SELECT ABS(ROUND(CAST((((MAX(`DataValue1`)-@AverageFromDetailEmpty)/@AverageFromDetailEmpty)*100) AS DECIMAL(20,15)),2)),
	ABS(ROUND(CAST((((@AverageFromDetailEmpty - MIN(`DataValue1`))/@AverageFromDetailEmpty)*100) AS DECIMAL(20,15)),2))
	 INTO @MaximumPercentageEmpty,@MinimumPercentageEmpty FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	
	IF @limitOnFromMaster = 1 THEN   /*Percentage*/
	SET @lowerT2Limit = CAST((@AverageFromDetail - (@AverageFromDetail *(@T2Neg/100))) AS DECIMAL(10,5));
	SET @upperT2Limit = CAST((@AverageFromDetail + (@AverageFromDetail *(@T2Pos/100))) AS DECIMAL(10,5));
	SET @lowerT1Limit = CAST((@AverageFromDetail - (@AverageFromDetail *(@T1Neg/100))) AS DECIMAL(10,5));
	SET @upperT1Limit = CAST((@AverageFromDetail + (@AverageFromDetail *(@T1Pos/100))) AS DECIMAL(10,5));
	IF @T1Neg = 0  THEN
		SET @valueT1 = CONCAT('NA - NA');
		SET @labelT1 = 'NA';
		SET @lowerT1Limit = 0;
		SET @upperT1Limit = 0;
	ELSE		
		SET @valueT1 = CONCAT(@lowerT1Limit,' % ',' - ', @upperT1Limit ,' % ');
		IF (@T1Neg = @T1Pos) THEN 
			IF(@strProjectName = 'CB') THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate  by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ');
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' % ');
			END IF;
		ELSE
			IF(@strProjectName = 'CB') THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate  by ± ' , CAST(@T1Neg AS DECIMAL(10,2)) ,' % ','-', CAST(@T1Pos AS DECIMAL(10,2)) ,' % ');  
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ' , CAST(@T1Neg AS DECIMAL(10,2)) ,' % ','-', CAST(@T1Pos AS DECIMAL(10,2)) ,' % ');  
			END IF;
		END IF;
		
	END IF;	
	IF @T2Neg = 0 THEN
		SET @valueT2 = CONCAT('NA - NA');
		SET @labelT2 = 'NA';
	ELSE		/* T1 != 0 */
		SET @valueT2 = CONCAT(@lowerT2Limit,' % ',' - ', @upperT2Limit ,' % ');
		IF (@T2Neg = @T2Pos) THEN 
		  IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT(' ± ',CAST(@T2Neg AS DECIMAL(10,2)) ,' % of std wt ');
		  ELSE
			SET @labelT2 = CONCAT('None of capsules by ± ', CAST(@T2Neg AS DECIMAL(10,2)) ,' % ');
	          END IF;
		ELSE
		 IF strProjectName = 'MLH' THEN
			SET @labelT2 = CONCAT(' ± ' , CAST(@T2Neg AS DECIMAL(10,2)) ,' % ','-',CAST(@T2Pos AS DECIMAL(10,2)) ,' % of std wt'); 
		  ELSE
			SET @labelT2 = CONCAT('None of capsules by ± ', CAST(@T2Neg AS DECIMAL(10,2)) ,' % ',' - ',CAST(@T2Pos AS DECIMAL(10,2)) ,' % ');  
		  END IF;
		END IF;
	END IF;	
ELSE
	SET @lowerT2Limit = CAST((@AverageFromDetail - @T2Neg) AS DECIMAL(10,5));
	SET @upperT2Limit = CAST((@AverageFromDetail + @T2Pos) AS DECIMAL(10,5));
	SET @lowerT1Limit = CAST((@AverageFromDetail - @T1Neg) AS DECIMAL(10,5));
	SET @upperT1Limit = CAST((@AverageFromDetail + @T1Pos) AS DECIMAL(10,5));
	IF @T1Neg = 0 THEN
		SET @valueT1 = CONCAT('NA - NA');
		SET @labelT1 = 'NA';
		SET @lowerT1Limit = 0;
		SET @upperT1Limit = 0;
	ELSE		/* T1 != 0 */
		SET @valueT1 = CONCAT(@lowerT1Limit ,' g ',' - ', @upperT1Limit ,' g ');
		IF (@T1Neg = @T1Pos) THEN 
			IF(@strProjectName = 'CB')THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ');
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ', CAST(@T1Neg AS DECIMAL(10,2)) ,' g ');
			END IF;
		ELSE
			IF(@strProjectName = 'CB') THEN
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules should deviate by ± ' , CAST(@T1Neg AS DECIMAL(10,2)) ,' g ',' - ',CAST(@T1Pos AS DECIMAL(10,2)) ,' g '); 
			ELSE
				SET @labelT1 = CONCAT('NMT ',@T1NMTTabFromMaster,' capsules by ± ' , CAST(@T1Neg AS DECIMAL(10,2)) ,' g ',' - ',CAST(@T1Pos AS DECIMAL(10,2)) ,' g '); 
			 END IF;
		END IF;
	END IF;	
	IF @T2Neg = 0  THEN
		SET @valueT2 = CONCAT('NA - NA');
		SET @labelT2 = 'NA';
	ELSE		/* T1 != 0 */
		SET @valueT2 = CONCAT(@lowerT2Limit ,' g ',' - ', @upperT2Limit ,' g ');
		IF (@T2Neg = @T2Pos) THEN 
		     IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT(' ± ',CAST(@T2Neg AS DECIMAL(10,2)) ,' g of std wt  ');
		      ELSE
		        SET @labelT2 = CONCAT('None of capsules by ± ', CAST(@T2Neg AS DECIMAL(10,2)) ,' g ');
		      END IF;
			
		ELSE
		     IF strProjectName = 'MLH' THEN
		        SET @labelT2 = CONCAT(' ± ', CAST(@T2Neg AS DECIMAL(10,2)) ,' g ',' - ',CAST(@T2Pos AS DECIMAL(10,2)) ,' g of std wt');  
		     ELSE
			SET @labelT2 = CONCAT('None of capsules by ± ', CAST(@T2Neg AS DECIMAL(10,2)) ,' g ',' - ',CAST(@T2Pos AS DECIMAL(10,2)) ,' g ');  
		     END IF;
		END IF;
	END IF;
END IF;
	
	
END IF;	
 
SET @query = CONCAT('SELECT 
SUM(CASE WHEN `NetWeight` < ',@lowerT2Limit,' THEN 1 ELSE 0 END),
SUM(CASE WHEN `NetWeight` > ',@upperT2Limit,' THEN 1 ELSE 0 END),
SUM(CASE `NetWeight` WHEN `NetWeight` < ',@lowerT1Limit,' THEN 0 WHEN `NetWeight` >= ',@lowerT2Limit,' THEN 1 ELSE 0 END),
SUM(CASE `NetWeight` WHEN `NetWeight` > ',@upperT1Limit,' THEN 0 WHEN `NetWeight` <= ',@upperT2Limit,' THEN 1 ELSE 0 END)  
INTO @belowLimitT2Count,@aboveLimitT2Count,@belowLimitT1Count,@aboveLimitT1Count
FROM ',strDetailTable,'
WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/* END OF ABOVE OR BELOW LIMIT CALCULATION */
/*VARIABLE MAPPING TO OUT PARAMETER */
SET nominal = @NominalFromMaster;
SET average = @AverageFromDetail;
SET minWeight = @MinFromDetail;
SET maxWeight = @MaxFromDetail;
SET minPercentage = @MinimumPercentage;
SET maxPercentage = @MaximumPercentage;
SET averageFill = @AverageFromDetailFill;
SET minWeightFill = @MinFromDetailFill;
SET maxWeightFill = @MaxFromDetailFill;
SET minPercentageFill = @MinimumPercentageFill;
SET maxPercentageFill = @MaximumPercentageFill;
SET averageEmpty = @AverageFromDetailEmpty;
SET minWeightEmpty = @MinFromDetailEmpty;
SET maxWeightEmpty = @MaxFromDetailEmpty;
SET minPercentageEmpty = @MinimumPercentageEmpty;
SET maxPercentageEmpty = @MaximumPercentageEmpty;
SET belowLimitT2Count =@belowLimitT2Count;
SET aboveLimitT2Count =@aboveLimitT2Count; 
SET labelT1=@labelT1;
SET labelT2=@labelT2;
SET belowLimitT1Count=@belowLimitT1Count;
SET aboveLimitT1Count=@aboveLimitT1Count;
SET sumOfNet = @SumOfNetWeight;
SET sumOfFill =@SumOfFillWeight;
SET sumOfEmpty = @SumOfEmptyWeight;
IF @T1Neg = 0 THEN
SET belowLimitT1Count=0;
SET aboveLimitT1Count=0;
END IF;
SET result=@result;
SET T1Neg=@lowerT1Limit;
SET T1Pos=@upperT1Limit;
SET T2Neg=@lowerT2Limit;
SET T2Pos=@upperT2Limit;
SET @sumValue1 = @belowLimitT1Count + @aboveLimitT1Count;
SET @sumValue2 = @belowLimitT2Count + @aboveLimitT2Count;
IF ((@sumValue1 > @T1NMTTabFromMaster) OR (@sumValue2 > 0)) THEN 
	SET result = 'Not Complies';
ELSE
	SET result = 'Complies';
END IF;
 
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationForDifferentialContents` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationForDifferentialContents` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationForDifferentialContents`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT nomFilledCont DECIMAL(10,5),
OUT stdLmt1TxtFilledCont VARCHAR(100),
OUT stdLmt2TxtFilledCont VARCHAR(100),
OUT lowT1FilledCont DECIMAL(10,5),
OUT uppT1FilledCont DECIMAL(10,5),
OUT lowT2FilledCont DECIMAL(10,5),
OUT uppT2FilledCont DECIMAL(10,5),
OUT AverageValueFilledCont DECIMAL(10,5),
OUT minWeightFilledCont DECIMAL(10,5),
OUT maxWeightFilledCont DECIMAL(10,5),
OUT minPercentageFilledCont DECIMAL(10,5),
OUT maxPercentageFilledCont DECIMAL(10,5),
OUT CalStandDevFilledCont DECIMAL(10,5),
OUT AbvLmt1NoSideFilledCont DECIMAL(10,5),
OUT BlwLmt1NoSideFilledCont DECIMAL(10,5),
OUT AbvLmt2NoSideFilledCont DECIMAL(10,5),
OUT BlwLmt2NoSideFilledCont DECIMAL(10,5),
OUT nomTab1 DECIMAL(10,5),
OUT stdLmt1TxtTab1 VARCHAR(100),
OUT stdLmt2TxtTab1 VARCHAR(100),
OUT lowT1Tab1 DECIMAL(10,5),
OUT uppT1Tab1 DECIMAL(10,5),
OUT lowT2Tab1 DECIMAL(10,5),
OUT uppT2Tab1 DECIMAL(10,5),
OUT AverageValueTab1 DECIMAL(10,5),
OUT minWeightTab1 DECIMAL(10,5),
OUT maxWeightTab1 DECIMAL(10,5),
OUT minPercentageTab1 DECIMAL(10,5),
OUT maxPercentageTab1 DECIMAL(10,5),
OUT CalStandDevTab1 DECIMAL(10,5),
OUT AbvLmt1NoSideTab1 DECIMAL(10,5),
OUT BlwLmt1NoSideTab1 DECIMAL(10,5),
OUT AbvLmt2NoSideTab1 DECIMAL(10,5),
OUT BlwLmt2NoSideTab1 DECIMAL(10,5),
OUT nomTab2 DECIMAL(10,5),
OUT stdLmt1TxtTab2 VARCHAR(100),
OUT stdLmt2TxtTab2 VARCHAR(100),
OUT lowT1Tab2 DECIMAL(10,5),
OUT uppT1Tab2 DECIMAL(10,5),
OUT lowT2Tab2 DECIMAL(10,5),
OUT uppT2Tab2 DECIMAL(10,5),
OUT AverageValueTab2 DECIMAL(10,5),
OUT minWeightTab2 DECIMAL(10,5),
OUT maxWeightTab2 DECIMAL(10,5),
OUT minPercentageTab2 DECIMAL(10,5),
OUT maxPercentageTab2 DECIMAL(10,5),
OUT CalStandDevTab2 DECIMAL(10,5),
OUT AbvLmt1NoSideTab2 DECIMAL(10,5),
OUT BlwLmt1NoSideTab2 DECIMAL(10,5),
OUT AbvLmt2NoSideTab2 DECIMAL(10,5),
OUT BlwLmt2NoSideTab2 DECIMAL(10,5),
OUT nomTab3 DECIMAL(10,5),
OUT stdLmt1TxtTab3 VARCHAR(100),
OUT stdLmt2TxtTab3 VARCHAR(100),
OUT lowT1Tab3 DECIMAL(10,5),
OUT uppT1Tab3 DECIMAL(10,5),
OUT lowT2Tab3 DECIMAL(10,5),
OUT uppT2Tab3 DECIMAL(10,5),
OUT AverageValueTab3 DECIMAL(10,5),
OUT minWeightTab3 DECIMAL(10,5),
OUT maxWeightTab3 DECIMAL(10,5),
OUT minPercentageTab3 DECIMAL(10,5),
OUT maxPercentageTab3 DECIMAL(10,5),
OUT CalStandDevTab3 DECIMAL(10,5),
OUT AbvLmt1NoSideTab3 DECIMAL(10,5),
OUT BlwLmt1NoSideTab3 DECIMAL(10,5),
OUT AbvLmt2NoSideTab3 DECIMAL(10,5),
OUT BlwLmt2NoSideTab3 DECIMAL(10,5),
OUT nomTab4 DECIMAL(10,5),
OUT stdLmt1TxtTab4 VARCHAR(100),
OUT stdLmt2TxtTab4 VARCHAR(100),
OUT lowT1Tab4 DECIMAL(10,5),
OUT uppT1Tab4 DECIMAL(10,5),
OUT lowT2Tab4 DECIMAL(10,5),
OUT uppT2Tab4 DECIMAL(10,5),
OUT AverageValueTab4 DECIMAL(10,5),
OUT minWeightTab4 DECIMAL(10,5),
OUT maxWeightTab4 DECIMAL(10,5),
OUT minPercentageTab4 DECIMAL(10,5),
OUT maxPercentageTab4 DECIMAL(10,5),
OUT CalStandDevTab4 DECIMAL(10,5),
OUT AbvLmt1NoSideTab4 DECIMAL(10,5),
OUT BlwLmt1NoSideTab4 DECIMAL(10,5),
OUT AbvLmt2NoSideTab4 DECIMAL(10,5),
OUT BlwLmt2NoSideTab4 DECIMAL(10,5),
OUT ICReport VARCHAR(20),
OUT Result VARCHAR(20))
BEGIN
/*GET DATA FROM MASTER*/
SET @query = CONCAT('SELECT 
`NomNet`,`T1NegNet`,`T1PosNet`,`T2NegNet`,`T2PosNet`,`limitOnNet`,`NMTTabNet`,`GraphTypeNet`,
`NomContent1`,`T1NegContent1`,`T1PosContent1`,`T2NegContent1`,`T2PosContent1`,`limitOnContent1`,`NMTTabContent1`,`GraphTypeContent1`,
`NomContent2`,`T1NegContent2`,`T1PosContent2`,`T2NegContent2`,`T2PosContent2`,`limitOnContent2`,`NMTTabContent2`,`GraphTypeContent2`,
`NomContent3`,`T1NegContent3`,`T1PosContent3`,`T2NegContent3`,`T2PosContent3`,`limitOnContent3`,`NMTTabContent3`,`GraphTypeContent3`,
`NomContent4`,`T1NegContent4`,`T1PosContent4`,`T2NegContent4`,`T2PosContent4`,`limitOnContent4`,`NMTTabContent4`,`GraphTypeContent4`,
`DecimalPoint`,`Unit`
INTO  
@NomNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,@limitOnNet,@NMTTabNet,@GraphTypeNet,
@NomContent1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,@limitOnContent1,@NMTTabContent1,@GraphTypeContent1,
@NomContent2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,@limitOnContent2,@NMTTabContent2,@GraphTypeContent2,
@NomContent3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,@limitOnContent3,@NMTTabContent3,@GraphTypeContent3,
@NomContent4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,@limitOnContent4,@NMTTabContent4,@GraphTypeContent4,
@DecimalPoint,@Unit
FROM ', strMasterTable ,' WHERE `RepSerNo`= ', repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
/*END OF GET DATA FROM MASTER*/
/*GET DATA FROM DETAIL*/
SET stdLmt1TxtFilledCont = getLimitText(@T1NegNet,@T2NegNet,@NMTTabNet,@limitOnNet,@DecimalPoint,@Unit,"T1");
SET stdLmt2TxtFilledCont = getLimitText(@T1NegNet,@T2NegNet,@NMTTabNet,@limitOnNet,@DecimalPoint,@Unit,"T2");
SET stdLmt1TxtTab1 = getLimitText(@T1NegContent1,@T2NegContent1,@NMTTabContent1,@limitOnContent1,@DecimalPoint,@Unit,"T1");
SET stdLmt2TxtTab1 = getLimitText(@T1NegContent1,@T2NegContent1,@NMTTabContent1,@limitOnContent1,@DecimalPoint,@Unit,"T2");
SET stdLmt1TxtTab2 = getLimitText(@T1NegContent2,@T2NegContent2,@NMTTabContent2,@limitOnContent2,@DecimalPoint,@Unit,"T1");
SET stdLmt2TxtTab2 = getLimitText(@T1NegContent2,@T2NegContent2,@NMTTabContent2,@limitOnContent2,@DecimalPoint,@Unit,"T2");
SET stdLmt1TxtTab3 = getLimitText(@T1NegContent3,@T2NegContent3,@NMTTabContent3,@limitOnContent3,@DecimalPoint,@Unit,"T1");
SET stdLmt2TxtTab3 = getLimitText(@T1NegContent3,@T2NegContent3,@NMTTabContent3,@limitOnContent3,@DecimalPoint,@Unit,"T2");
SET stdLmt1TxtTab4 = getLimitText(@T1NegContent4,@T2NegContent4,@NMTTabContent4,@limitOnContent4,@DecimalPoint,@Unit,"T1");
SET stdLmt2TxtTab4 = getLimitText(@T1NegContent4,@T2NegContent4,@NMTTabContent4,@limitOnContent4,@DecimalPoint,@Unit,"T2");
/*END OF GET DATA FROM DETAIL*/
/************ CALCULATE AVERAGE FROM DETAIL ********************************/
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(NetWeight) AS DECIMAL(20,15)),5) INTO @averageFromDetailNet FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(Content1) AS DECIMAL(20,15)),5) INTO @averageFromDetailC1 FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(Content2) AS DECIMAL(20,15)),5) INTO @averageFromDetailC2 FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(Content3) AS DECIMAL(20,15)),5) INTO @averageFromDetailC3 FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
SET @query1 = CONCAT('SELECT ROUND(CAST(AVG(Content4) AS DECIMAL(20,15)),5) INTO @averageFromDetailC4 FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query1;
EXECUTE stmt;
/************ END OF CALCULATE AVERAGE FROM DETAIL *************************/
/********************************** CALCULATE LIMITS*************************/
/*NET*/
SET @T1NegNet = `getProductLimits`(@NomNet,@averageFromDetailNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,@limitOnNet,@GraphTypeNet,"LOWT1");
SET @T1PosNet = `getProductLimits`(@NomNet,@averageFromDetailNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,@limitOnNet,@GraphTypeNet,"UPPT1");
SET @T2NegNet = `getProductLimits`(@NomNet,@averageFromDetailNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,@limitOnNet,@GraphTypeNet,"LOWT2");
SET @T2PosNet = `getProductLimits`(@NomNet,@averageFromDetailNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,@limitOnNet,@GraphTypeNet,"UPPT2");
/*CONTENT 1*/
SET @T1NegContent1 = `getProductLimits`(@NomContent1,@averageFromDetailC1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,@limitOnContent1,@GraphTypeContent1,"LOWT1");
SET @T1PosContent1 = `getProductLimits`(@NomContent1,@averageFromDetailC1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,@limitOnContent1,@GraphTypeContent1,"UPPT1");
SET @T2NegContent1 = `getProductLimits`(@NomContent1,@averageFromDetailC1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,@limitOnContent1,@GraphTypeContent1,"LOWT2");
SET @T2PosContent1 = `getProductLimits`(@NomContent1,@averageFromDetailC1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,@limitOnContent1,@GraphTypeContent1,"UPPT2");
/*CONTENT 2*/
SET @T1NegContent2 = `getProductLimits`(@NomContent2,@averageFromDetailC2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,@limitOnContent2,@GraphTypeContent2,"LOWT1");
SET @T1PosContent2 = `getProductLimits`(@NomContent2,@averageFromDetailC2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,@limitOnContent2,@GraphTypeContent2,"UPPT1");
SET @T2NegContent2 = `getProductLimits`(@NomContent2,@averageFromDetailC2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,@limitOnContent2,@GraphTypeContent2,"LOWT2");
SET @T2PosContent2 = `getProductLimits`(@NomContent2,@averageFromDetailC2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,@limitOnContent2,@GraphTypeContent2,"UPPT2");
/*CONTENT 3*/
SET @T1NegContent3 = `getProductLimits`(@NomContent3,@averageFromDetailC3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,@limitOnContent3,@GraphTypeContent3,"LOWT1");
SET @T1PosContent3 = `getProductLimits`(@NomContent3,@averageFromDetailC3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,@limitOnContent3,@GraphTypeContent3,"UPPT1");
SET @T2NegContent3 = `getProductLimits`(@NomContent3,@averageFromDetailC3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,@limitOnContent3,@GraphTypeContent3,"LOWT2");
SET @T2PosContent3 = `getProductLimits`(@NomContent3,@averageFromDetailC3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,@limitOnContent3,@GraphTypeContent3,"UPPT2");
/*CONTENT 4*/
SET @T1NegContent4 = `getProductLimits`(@NomContent4,@averageFromDetailC4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,@limitOnContent4,@GraphTypeContent4,"LOWT1");
SET @T1PosContent4 = `getProductLimits`(@NomContent4,@averageFromDetailC4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,@limitOnContent4,@GraphTypeContent4,"UPPT1");
SET @T2NegContent4 = `getProductLimits`(@NomContent4,@averageFromDetailC4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,@limitOnContent4,@GraphTypeContent4,"LOWT2");
SET @T2PosContent4 = `getProductLimits`(@NomContent4,@averageFromDetailC4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,@limitOnContent4,@GraphTypeContent4,"UPPT2");
/*********************************** END OF CALCULATE LIMITS **************/
/************************************CALCULATE STATISTICS*******************/
/*NET*/
CALL `calculateReportStatistics`(repSerNo,strMasterTable,strDetailTable,'NetWeight',@GraphTypeNet,@NomNet,@T1NegNet,@T1PosNet,@T2NegNet,@T2PosNet,
@averageFromDetailNet,@minWeightNet,@maxWeightNet,@minPercentageNet,@maxPercentageNet,@standardDeviationNet,@aboveLimit1Net,@belowLimit1Net,@aboveLimit2Net,@belowLimit2Net);-- SELECT @averageFromDetailNet,@minWeightNet,@maxWeightNet,@minPercentageNet,@maxPercentageNet,@standardDeviationNet,@aboveLimit1Net,@belowLimit1Net,@aboveLimit2Net,@belowLimit2Net;
/*CONTENT 1*/
CALL `calculateReportStatistics`(repSerNo,strMasterTable,strDetailTable,'Content1',@GraphTypeContent1,@NomContent1,@T1NegContent1,@T1PosContent1,@T2NegContent1,@T2PosContent1,
@averageFromDetailC1,@minWeightC1,@maxWeightC1,@minPercentageC1,@maxPercentageC1,@standardDeviationC1,@aboveLimit1C1,@belowLimit1C1,@aboveLimit2C1,@belowLimit2C1);-- SELECT @averageFromDetailC1,@minWeightC1,@maxWeightC1,@minPercentageC1,@maxPercentageC1,@standardDeviationC1,@aboveLimit1C1,@belowLimit1C1,@aboveLimit2C1,@belowLimit2C1;
/*CONTENT 2*/
CALL `calculateReportStatistics`(repSerNo,strMasterTable,strDetailTable,'Content2',@GraphTypeContent2,@NomContent2,@T1NegContent2,@T1PosContent2,@T2NegContent2,@T2PosContent2,
@averageFromDetailC2,@minWeightC2,@maxWeightC2,@minPercentageC2,@maxPercentageC2,@standardDeviationC2,@aboveLimit1C2,@belowLimit1C2,@aboveLimit2C2,@belowLimit2C2);
/*CONTENT 3*/
CALL `calculateReportStatistics`(repSerNo,strMasterTable,strDetailTable,'Content3',@GraphTypeContent3,@NomContent3,@T1NegContent3,@T1PosContent3,@T2NegContent3,@T2PosContent3,
@averageFromDetailC3,@minWeightC3,@maxWeightC3,@minPercentageC3,@maxPercentageC3,@standardDeviationC3,@aboveLimit1C3,@belowLimit1C3,@aboveLimit2C3,@belowLimit2C3);
/*CONTENT 4*/
CALL `calculateReportStatistics`(repSerNo,strMasterTable,strDetailTable,'Content4',@GraphTypeContent4,@NomContent4,@T1NegContent4,@T1PosContent4,@T2NegContent4,@T2PosContent4,
@averageFromDetailC4,@minWeightC4,@maxWeightC4,@minPercentageC4,@maxPercentageC4,@standardDeviationC4,@aboveLimit1C4,@belowLimit1C4,@aboveLimit2C4,@belowLimit2C4);
/*************************************END OF CALCULATE STATISTICS***********/
SELECT @averageFromDetailNet,@minWeightNet,@maxWeightNet,@minPercentageNet,@maxPercentageNet,@standardDeviationNet,@aboveLimit1Net,@belowLimit1Net,@aboveLimit2Net,@belowLimit2Net,
@averageFromDetailC1,@minWeightC1,@maxWeightC1,@minPercentageC1,@maxPercentageC1,@standardDeviationC1,@aboveLimit1C1,@belowLimit1C1,@aboveLimit2C1,@belowLimit2C1,
@averageFromDetailC2,@minWeightC2,@maxWeightC2,@minPercentageC2,@maxPercentageC2,@standardDeviationC2,@aboveLimit1C2,@belowLimit1C2,@aboveLimit2C2,@belowLimit2C2,
@averageFromDetailC3,@minWeightC3,@maxWeightC3,@minPercentageC3,@maxPercentageC3,@standardDeviationC3,@aboveLimit1C3,@belowLimit1C3,@aboveLimit2C3,@belowLimit2C3,
@averageFromDetailC4,@minWeightC4,@maxWeightC4,@minPercentageC4,@maxPercentageC4,@standardDeviationC4,@aboveLimit1C4,@belowLimit1C4,@aboveLimit2C4,@belowLimit2C4;
/*SET VERIABLES*/
/* NET CALCULATION */
SET nomFilledCont = @NomNet;
SET lowT1FilledCont = @T1NegNet;
SET uppT1FilledCont = @T1PosNet;
SET lowT2FilledCont = @T2NegNet;
SET uppT2FilledCont = @T2PosNet;
SET AverageValueFilledCont = @averageFromDetailNet;
SET minWeightFilledCont = @minWeightNet;
SET maxWeightFilledCont = @maxWeightNet;
SET minPercentageFilledCont = @minPercentageNet;
SET maxPercentageFilledCont = @maxPercentageNet;
SET CalStandDevFilledCont = @standardDeviationNet;
SET AbvLmt1NoSideFilledCont = @aboveLimit1Net;
SET BlwLmt1NoSideFilledCont = @belowLimit1Net;
SET AbvLmt2NoSideFilledCont = @aboveLimit2Net;
SET BlwLmt2NoSideFilledCont = @belowLimit2Net;
SET @sumValue1 = @belowLimit1Net + @aboveLimit1Net;
SET @sumValue2 = @belowLimit2Net + @aboveLimit2Net;
/* Result is always based on NetContent Limits*/
IF ((@sumValue1 > @NMTTabNet) OR (@sumValue2 > 0)) THEN 
	SET Result = 'Not Complies';
ELSE
	SET Result = 'Complies';
END IF;
/* END OF NET CALCULATION */
SET nomTab1 = @NomContent1;
SET lowT1Tab1 = @T1NegContent1;
SET uppT1Tab1 = @T1PosContent1;
SET lowT2Tab1 = @T2NegContent1;
SET uppT2Tab1 = @T2PosContent1;
SET AverageValueTab1 = @averageFromDetailC1;
SET minWeightTab1 = @minWeightC1;
SET maxWeightTab1 = @maxWeightC1;
SET minPercentageTab1 = @minPercentageC1;
SET maxPercentageTab1 = @maxPercentageC1;
SET CalStandDevTab1 = @standardDeviationC1;
SET AbvLmt1NoSideTab1 = @aboveLimit1C1;
SET BlwLmt1NoSideTab1 = @belowLimit1C1;
SET AbvLmt2NoSideTab1 = @aboveLimit2C1;
SET BlwLmt2NoSideTab1 = @belowLimit2C1;
SET nomTab2 = @NomContent2;
SET lowT1Tab2 = @T1NegContent2;
SET uppT1Tab2 = @T1PosContent2;
SET lowT2Tab2 = @T2NegContent2;
SET uppT2Tab2 = @T2PosContent2;
SET AverageValueTab2 = @averageFromDetailC2;
SET minWeightTab2 = @minWeightC2;
SET maxWeightTab2 = @maxWeightC2;
SET minPercentageTab2 = @minPercentageC2;
SET maxPercentageTab2 = @maxPercentageC2;
SET CalStandDevTab2 = @standardDeviationC2;
SET AbvLmt1NoSideTab2 = @aboveLimit1C2;
SET BlwLmt1NoSideTab2 = @belowLimit1C2;
SET AbvLmt2NoSideTab2 = @aboveLimit2C2;
SET BlwLmt2NoSideTab2 = @belowLimit2C2;
SET nomTab3 = @NomContent3;
SET lowT1Tab3 = @T1NegContent3;
SET uppT1Tab3 = @T1PosContent3;
SET lowT2Tab3 = @T2NegContent3;
SET uppT2Tab3 = @T2PosContent3;
SET AverageValueTab3 = @averageFromDetailC3;
SET minWeightTab3 = @minWeightC3;
SET maxWeightTab3 = @maxWeightC3;
SET minPercentageTab3 = @minPercentageC3;
SET maxPercentageTab3 = @maxPercentageC3;
SET CalStandDevTab3 = @standardDeviationC3;
SET AbvLmt1NoSideTab3 = @aboveLimit1C3;
SET BlwLmt1NoSideTab3 = @belowLimit1C3;
SET AbvLmt2NoSideTab3 = @aboveLimit2C3;
SET BlwLmt2NoSideTab3 = @belowLimit2C3;
SET nomTab4 = @NomContent4;
SET lowT1Tab4 = @T1NegContent4;
SET uppT1Tab4 = @T1PosContent4;
SET lowT2Tab4 = @T2NegContent4;
SET uppT2Tab4 = @T2PosContent4;
SET AverageValueTab4 = @averageFromDetailC4;
SET minWeightTab4 = @minWeightC4;
SET maxWeightTab4 = @maxWeightC4;
SET minPercentageTab4 = @minPercentageC4;
SET maxPercentageTab4 = @maxPercentageC4;
SET CalStandDevTab4 = @standardDeviationC4;
SET AbvLmt1NoSideTab4 = @aboveLimit1C4;
SET BlwLmt1NoSideTab4 = @belowLimit1C4;
SET AbvLmt2NoSideTab4 = @aboveLimit2C4;
SET BlwLmt2NoSideTab4 = @belowLimit2C4;
IF (strMasterTable='tbl_tab_master3_incomplete') THEN
	SET ICReport = "Incomplete";
ELSE
	SET ICReport = "Complete";
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationForHTD` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationForHTD` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationForHTD`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT DOLOBOUpp DECIMAL(6,2),
OUT ThkUpp DECIMAL(6,2),
OUT HardUpp DECIMAL(6,2),
OUT DOLOBOLow DECIMAL(6,2),
OUT ThkLow DECIMAL(6,2),
OUT HardLow DECIMAL(6,2),
OUT DOLOBONom DECIMAL(6,2),
OUT ThkNom DECIMAL(6,2),
OUT HardNom DECIMAL(6,2),
OUT DOLOBOMax DECIMAL(6,2),
OUT ThkMax DECIMAL(6,2),
OUT HardMax DECIMAL(6,2),
OUT DOLOBOMin DECIMAL(6,2),
OUT ThkMin DECIMAL(6,2),
OUT HardMin DECIMAL(6,2),
OUT HardStdDev DECIMAL(6,3),
OUT HardAverage DECIMAL(6,2),
OUT ThkStdDev DECIMAL(6,3),
OUT ThikAverage DECIMAL(6,2),
OUT DOLOBOStdDev DECIMAL(6,3),
OUT DOLOBOAverage DECIMAL(6,2)
)
BEGIN
SET @query = CONCAT('SELECT `NomThick`,`PosTolThick`,`NegTolThick`,`NomHard`,`PosTolHard`,`NegTolHard`,`NomDOLOBO`,`PosTolDOLOBO`,`NegTolDOLOBO` 
INTO @NomThick, @PosTolThick, @NegTolThick, @NomHard, @PosTolHard, @NegTolHard, @NomDOLOBO, @PosTolDOLOBO, @NegTolDOLOBO FROM ',strMasterTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(AVG(`DataValueThick`) AS DECIMAL(20,15)),2),
ROUND(CAST(AVG(`DataValueHard`) AS DECIMAL(20,15)),2),
ROUND(CAST(AVG(`DataValueDOLOBO`) AS DECIMAL(20,15)),2),
ROUND(CAST(STDDEV_SAMP(`DataValueThick`) AS DECIMAL(20,15)),3),
ROUND(CAST(STDDEV_SAMP(`DataValueHard`) AS DECIMAL(20,15)),3),
ROUND(CAST(STDDEV_SAMP(`DataValueDOLOBO`) AS DECIMAL(20,15)),3),
CAST(MIN(`DataValueThick`)  AS DECIMAL(6,2)), CAST(MAX(`DataValueThick`)  AS DECIMAL(6,2)),
CAST(MIN(`DataValueHard`)  AS DECIMAL(6,2)), CAST(MAX(`DataValueHard`)  AS DECIMAL(6,2)),
CAST(MIN(`DataValueDOLOBO`)  AS DECIMAL(6,2)), CAST(MAX(`DataValueDOLOBO`)  AS DECIMAL(6,2))
 INTO @ThikAverage,@HardAverage,@DOLOBOAverage,
 @ThkStdDev, @HardStdDev, @DOLOBOStdDev,
 @ThkMin,@ThkMax, @HardMin, @HardMax, @DOLOBOMin, @DOLOBOMax FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET DOLOBOUpp = @NomDOLOBO + @PosTolDOLOBO;
SET DOLOBOLow = @NomDOLOBO - @NegTolDOLOBO;
SET DOLOBONom = @NomDOLOBO;
SET DOLOBOMax = @DOLOBOMax;
SET DOLOBOMin = @DOLOBOMin;
SET DOLOBOAverage = @DOLOBOAverage;
SET ThkUpp = @NomThick + @PosTolThick;
SET ThkLow = @NomThick - @NegTolThick;
SET ThikAverage = @ThikAverage;
SET ThkNom = @NomThick;
SET ThkMax = @ThkMax;
SET ThkMin = @ThkMin;
SET HardNom = @NomHard;
SET HardMax = @HardMax;
SET HardMin = @HardMin;
SET HardAverage = @HardAverage;
SET HardLow = @NomHard - @NegTolHard;
SET HardUpp = @NomHard + @PosTolHard;
IF (@DOLOBOStdDev IS NULL OR @DOLOBOStdDev = '')  THEN
	SET DOLOBOStdDev 	= 0;
ELSE
	SET DOLOBOStdDev = @DOLOBOStdDev;
END IF;
IF (@ThkStdDev IS NULL OR @ThkStdDev = '')  THEN
	SET ThkStdDev 	= 0;
ELSE
	SET ThkStdDev = @ThkStdDev;
END IF;
IF (@HardStdDev IS NULL OR @HardStdDev = '')  THEN
	SET HardStdDev 	= 0;
ELSE
	SET HardStdDev =  @HardStdDev;
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationFriability` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationFriability` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationFriability`(
    IN repSerNo INT,
    IN strMasterTable VARCHAR(100),
    IN intDPs INT,
    OUT FriNMTLimit DECIMAL(6,3),
    OUT NWtBeforeTest DECIMAL(10,4),
    OUT NWtAfterTest DECIMAL(10,4),
    OUT LWtBeforeTest DECIMAL(10,4),
    OUT LWtAfterTest DECIMAL(10,4),
    OUT RWtBeforeTest DECIMAL(10,4),
    OUT RWtAfterTest DECIMAL(10,4),
    OUT FriabilityPercentageNA DECIMAL(10,4),
    OUT FriabilityPercentageRight DECIMAL(10,4),
    OUT FriabilityPercentageLeft DECIMAL(10,4),
    OUT remark VARCHAR(100),
    OUT DP INT
    )
BEGIN
SET @dp = intDPs;
SET @query = CONCAT('SELECT `FriNMTLimit`,`NWtBeforeTest`,`NWtAfterTest`,`LWtBeforeTest`,`LWtAfterTest`,`RWtBeforeTest`,`RWtAfterTest`,`Side`,`NoOfSample`,`DecimalPoint`
INTO @FriNMTLimit, @NWtBeforeTest, @NWtAfterTest, @LWtBeforeTest, @LWtAfterTest, @RWtBeforeTest, @RWtAfterTest, @Side, @NoOfSample, @DecimalPoint  FROM ',strMasterTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
IF @Side = 'Single' THEN
	IF ((@NWtBeforeTest - @NWtAfterTest) = 0 ) THEN
		SET @FriabilityPercentageNA = 0;
	ELSE
		SET @querySingle = CONCAT('SELECT  CAST((((ROUND(`NWtBeforeTest`,',@dp,')-ROUND(`NWtAfterTest`,',@dp,'))/ROUND(`NWtBeforeTest`,',@dp,'))*100) AS DECIMAL(20,15)) INTO @FriabilityPercentageNA FROM  ',strMasterTable,'  WHERE `RepSerNo` = ',repSerNo,';'); 
		PREPARE stm FROM @querySingle;
		EXECUTE stm;
	END IF;
	
	IF (SIGN(@FriabilityPercentageNA) = -1) THEN
	SET @FriabilityPercentageNA = 0;
	END IF;
	
	IF (((@NWtBeforeTest - @NWtAfterTest) = 0) OR (((@FriabilityPercentageNA <= @FriNMTLimit) AND (@FriabilityPercentageNA >= (-1 * @FriNMTLimit))))) THEN
		SET @remark = 'Complies';
	ELSE 
		SET @remark = 'Not Complies';
	END IF;	
ELSE
	IF ((@LWtBeforeTest - @LWtAfterTest) = 0 ) THEN
		SET @FriabilityPercentageLeft = 0;
	ELSE
		SET @queryLeft = CONCAT('SELECT  CAST((((ROUND(`LWtBeforeTest`,',@dp,')-ROUND(`LWtAfterTest`,',@dp,'))/ROUND(`LWtBeforeTest`,',@dp,'))*100) AS DECIMAL(20,15)) INTO @FriabilityPercentageLeft  FROM  ',strMasterTable,'  WHERE `RepSerNo` = ',repSerNo,';');
		PREPARE stmLeft FROM @queryLeft;
		EXECUTE stmLeft;
	END IF;  
	
	IF ((@RWtBeforeTest - @RWtAfterTest) = 0 ) THEN 
		SET @FriabilityPercentageRight = 0;
	ELSE
		SET @queryRight = CONCAT('SELECT  CAST((((ROUND(`RWtBeforeTest`,',@dp,')-ROUND(`RWtAfterTest`,',@dp,'))/ROUND(`RWtBeforeTest`,',@dp,'))*100) AS DECIMAL(20,15)) INTO @FriabilityPercentageRight  FROM  ',strMasterTable,'  WHERE `RepSerNo` = ',repSerNo,';');
		PREPARE stmRight FROM @queryRight;
		EXECUTE stmRight;
	END IF; 
	
	IF (SIGN(@FriabilityPercentageLeft) = -1) THEN
	SET @FriabilityPercentageLeft = 0;
	END IF;
	
	IF (SIGN(@FriabilityPercentageRight) = -1) THEN
	SET @FriabilityPercentageRight = 0;
	END IF;
	
	IF
	(
	 ((@FriabilityPercentageLeft = 0) AND (@FriabilityPercentageRight = 0)) 
	 OR (((@FriabilityPercentageLeft <= @FriNMTLimit) AND (@FriabilityPercentageLeft >= (-1 * @FriNMTLimit))) 
	 AND ((@FriabilityPercentageRight <= @FriNMTLimit) AND (@FriabilityPercentageRight >= (-1 * @FriNMTLimit))))
	) THEN	
		SET @remark = 'Complies';
	ELSE
		SET @remark = 'Not Complies';
	END IF;
    
END IF;
SET FriNMTLimit = @FriNMTLimit;
SET NWtBeforeTest = @NWtBeforeTest;	
SET NWtAfterTest = @NWtAfterTest;
SET LWtBeforeTest = @LWtBeforeTest;
SET LWtAfterTest = @LWtAfterTest;
SET RWtBeforeTest = @RWtBeforeTest;
SET RWtAfterTest = @RWtAfterTest;
SET remark = @remark;
SET FriabilityPercentageNA = @FriabilityPercentageNA;
SET FriabilityPercentageRight = @FriabilityPercentageRight;
SET FriabilityPercentageLeft = @FriabilityPercentageLeft;
SET DP = @DecimalPoint;
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationFriabilityForInterface` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationFriabilityForInterface` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationFriabilityForInterface`(
IN RepSerNo INT,
OUT Remark VARCHAR(100))
BEGIN
	SET @query = CONCAT('SELECT `Side`,`DecimalPoint`,`FriNMTLimit`,`NWtBeforeTest`,`NWtAfterTest`,`LWtBeforeTest`,`LWtAfterTest`,`RWtBeforeTest`,`RWtAfterTest`
	INTO @Side,@DecimalPoint,@FriNMTLimit,@NWtBeforeTest,@NWtAfterTest,@LWtBeforeTest,@LWtAfterTest,@RWtBeforeTest,@RWtAfterTest FROM `tbl_tab_friability` WHERE `RepSerNo`=',RepSerNo);
	PREPARE stmt FROM @query;
	EXECUTE stmt;
	IF(@Side = "Single") THEN
	    SET @querySingle = CONCAT('SELECT  ROUND(CAST((((`NWtBeforeTest`-`NWtAfterTest`)/`NWtBeforeTest`)*100) AS DECIMAL(20,15)),4) INTO @FriabilityPercentageNA FROM  `tbl_tab_friability`  WHERE `RepSerNo` = ',RepSerNo,';'); 
	    PREPARE stm FROM @querySingle;
	    EXECUTE stm;
	   IF (((@NWtBeforeTest - @NWtAfterTest) = 0) OR (((@FriabilityPercentageNA <= @FriNMTLimit) AND (@FriabilityPercentageNA >= (-1 * @FriNMTLimit))))) THEN
		SET @remark = 'Complies';
	    ELSE 
		SET @remark = 'Not Complies';
	    END IF;	
	ELSE IF(@Side = "Double") THEN
	    IF ((@LWtBeforeTest - @LWtAfterTest) = 0 ) THEN
		SET @FriabilityPercentageLeft = 0;
		ELSE
			SET @queryLeft = CONCAT('SELECT  ROUND(CAST((((`LWtBeforeTest`-`LWtAfterTest`)/`LWtBeforeTest`)*100) AS DECIMAL(20,15)),3) INTO @FriabilityPercentageLeft  FROM  `tbl_tab_friability`  WHERE `RepSerNo` = ',RepSerNo,';');
			PREPARE stmLeft FROM @queryLeft;
			EXECUTE stmLeft;
		END IF;  
		
		IF ((@RWtBeforeTest - @RWtAfterTest) = 0 ) THEN 
			SET @FriabilityPercentageRight = 0;
		ELSE
			SET @queryRight = CONCAT('SELECT  ROUND(CAST((((`RWtBeforeTest`-`RWtAfterTest`)/`RWtBeforeTest`)*100) AS DECIMAL(20,15)),3) INTO @FriabilityPercentageRight  FROM  `tbl_tab_friability`  WHERE `RepSerNo` = ',RepSerNo,';');
			PREPARE stmRight FROM @queryRight;
			EXECUTE stmRight;
		END IF; 
		IF
		(
		 ((@FriabilityPercentageLeft = 0) AND (@FriabilityPercentageRight = 0)) 
		 OR (((@FriabilityPercentageLeft <= @FriNMTLimit) AND (@FriabilityPercentageLeft >= (-1 * @FriNMTLimit))) 
		 AND ((@FriabilityPercentageRight <= @FriNMTLimit) AND (@FriabilityPercentageRight >= (-1 * @FriNMTLimit))))
		) THEN	
			SET @remark = 'Complies';
		ELSE
			SET @remark = 'Not Complies';
		END IF;
	ELSE
		SET @remark = 'NA';
	END IF;
	END IF;
SET Remark = @remark;	
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationGroup` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationGroup` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationGroup`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT Nom DECIMAL(10,5),
OUT Low DECIMAL(10,5),
OUT Upp DECIMAL(10,5),
OUT Average DECIMAL(10,5),
OUT Stdev DECIMAL(10,5),
OUT Mininum DECIMAL(10,3),
OUT Maximum DECIMAL(10,3),
OUT limiton INT,
OUT graphType INT,
OUT strLimitText1 DECIMAL(10,3),
OUT strLimitText2 DECIMAL(10,3),
OUT RecSeqNo INT,
OUT RecSeqNoR INT,
OUT MaxAverageWT DECIMAL(10,5),
OUT AverageWT DECIMAL(10,5)
)
BEGIN
SET @query = CONCAT('SELECT `Nom`,CAST(`T2NegTol` AS DECIMAL(10,5)),CAST(`T2PosTol` AS DECIMAL(10,5)),`limiton`,`GraphType` INTO @Nom, @T2NegTol, @T2PosTol, @limiton, @graphType  FROM ',strMasterTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(`DataValue`) AS DECIMAL(20,15)),5),
CAST(MIN(`DataValue`)  AS DECIMAL(10,5)), CAST(MAX(`DataValue`)  AS DECIMAL(10,5)), CAST(MAX(`AvgWt`)  AS DECIMAL(10,5)),ROUND(CAST(AVG(`AvgWt`) AS DECIMAL(10,5)),3) 
INTO @Average, @Stdev, @Mininum, @Maximum, @MaxAvgWt, @AvgWt FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT COUNT(*) INTO @totalnum FROM ',strDetailTable,' WHERE (`RepSerNo`=',repSerNo,' AND Side="LHS") || (`RepSerNo`=',repSerNo,' AND Side="NA") ');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT COUNT(*) INTO @totalnumR FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,' AND Side="RHS"');
PREPARE stmt FROM @query;
EXECUTE stmt;
IF @graphType = 0 THEN
    SET @intNo = @Nom;
ELSE
    SET @intNo = @Average;
END IF;
IF @limiton = 1 THEN
    SET @strLimitText1 = (@intNo - (@intNo *(@T2NegTol/100)));
        SET @strLimitText2 = (@intNo + (@intNo *(@T2PosTol/100)));
ELSE
    SET @strLimitText1 = (@intNo - @T2NegTol);
        SET @strLimitText2 = (@intNo + @T2PosTol);
END IF;
SET Nom = @Nom;
SET Low = (@Nom - @T2NegTol);
SET Upp = (@Nom + @T2PosTol);
SET Average = @Average;
SET Mininum = @Mininum;
SET Maximum = @Maximum;
SET limiton = @limiton;
SET graphType = @graphType;
SET strLimitText1 = @strLimitText1;
SET strLimitText2 = @strLimitText2;
SET RecSeqNo = @totalnum;
SET RecSeqNoR = @totalnumR;
IF (@Stdev IS NULL OR @Stdev = '')  THEN
    SET Stdev     = 0;
ELSE
    SET Stdev = @Stdev;
END IF;
SET MaxAverageWT = @MaxAvgWt;
SET AverageWT = @AvgWt;
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationLODForInterface` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationLODForInterface` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationLODForInterface`()
BEGIN
END */$$
DELIMITER ;

/* Procedure structure for procedure `reportCalculationVernier` */

/*!50003 DROP PROCEDURE IF EXISTS  `reportCalculationVernier` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `reportCalculationVernier`(
IN repSerNo INT,
IN strMasterTable VARCHAR(100),
IN strDetailTable VARCHAR(100),
OUT Nom DECIMAL(6,2),
OUT Low1 DECIMAL(6,2),
OUT Upp1 DECIMAL(6,2),
OUT Low2 DECIMAL(6,2),
OUT Upp2 DECIMAL(6,2),
OUT Average DECIMAL(10,5),
OUT Stdev DECIMAL(10,5),
OUT Mininum DECIMAL(6,2),
OUT Maximum DECIMAL(6,2),
OUT MininumPer DECIMAL(6,2),
OUT MaximumPer DECIMAL(6,2),
OUT RecSeqNo INT,
OUT NMT INT,
OUT T1Pos DECIMAL(6,2)
)
BEGIN
SET @query = CONCAT('SELECT `Nom`,`T1NegTol`,`T1PosTol`,`T2NegTol`,`T2PosTol`,`limitOn`,`GraphType`,`T1NMTTab`
INTO @Nom,@T1NegTol, @T1PosTol, @T2NegTol, @T2PosTol, @limitOnFromMaster, @graphTypeMaster, @nmt
FROM ',strMasterTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT ROUND(CAST(AVG(`DataValue`) AS DECIMAL(20,15)),5),
ROUND(CAST(STDDEV_SAMP(`DataValue`) AS DECIMAL(20,15)),5),
CAST(MIN(`DataValue`)  AS DECIMAL(6,2)), CAST(MAX(`DataValue`)  AS DECIMAL(6,2))
 INTO @Average, @Stdev, @Mininum, @Maximum FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT COUNT(*) INTO @totalnum FROM ',strDetailTable,' WHERE `RepSerNo`=',repSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
IF @T1PosTol = "99999.00000" THEN
	SET @T1PosTol = 0;
ELSE
	SET @T1PosTol = @T1PosTol;
END IF;
SET Nom = @Nom;
SET Low1 = (@Nom - @T1NegTol);
SET Upp1 = (@Nom + @T1PosTol);
SET Low2 = (@Nom - @T2NegTol);
SET Upp2 = (@Nom + @T2PosTol);
SET Average = @Average;
SET Mininum = @Mininum;
SET Maximum = @Maximum;
SET MininumPer = (((@Nom - @Mininum) / @Nom) * 100);
SET MaximumPer = (((@Maximum - @Nom) / @Nom) * 100);
SET RecSeqNo = @totalnum;
SET NMT = @nmt;
SET T1Pos = @T1PosTol;
IF (@Stdev IS NULL OR @Stdev = '')  THEN
	SET Stdev 	= 0;
ELSE
	SET Stdev = @Stdev;
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `Setting` */

/*!50003 DROP PROCEDURE IF EXISTS  `Setting` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `Setting`(
      IN cubicNos INT,
      IN areaName VARCHAR(100),
      IN tableName VARCHAR(100)
    )
BEGIN
/******** VERIABLE DECLARATION *******************/
    DECLARE cntData INT DEFAULT 0;
    DECLARE cubicNo int DEFAULT 0;
    DECLARE int_maxcubicNo INT;
    DECLARE str_tableName VARCHAR(100);
    DECLARE str_areaName VARCHAR(100);
    DECLARE x INT default 0;
/******** END OF VERIABLE DECLARATION ************/ 
/******** SET VERIABLE ***************************/   
    SET @cubicNo = cubicNos;
    SET @str_tableName = tableName;
    SET @str_areaName = areaName;
/******** END OF SET VERIABLE ***************************/    
    select noLmt into int_maxcubicNo from `tbl_cubical_nos`;    
    
    
    SET @queryString = CONCAT("SELECT COUNT(*) INTO @cntData from ",@str_tableName); 
    PREPARE stmt1 FROM  @queryString; 
    EXECUTE stmt1;
    
    
    IF @cntData >= int_maxcubicNo  THEN 
        SET @queryString1 = CONCAT("TRUNCATE ",@str_tableName); 
	PREPARE stmt3 FROM  @queryString1; 
	EXECUTE stmt3;
    END IF;
        
    REPEAT
	SET  X = X + 1; 
	 SET @queryString2 = CONCAT("insert into ",@str_tableName, "(`Sys_Area`) values('",@str_areaName,"')"); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
    UNTIL X = @cubicNo
    END REPEAT;
    END */$$
DELIMITER ;

/* Procedure structure for procedure `tabletTesterReportCalculation` */

/*!50003 DROP PROCEDURE IF EXISTS  `tabletTesterReportCalculation` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `tabletTesterReportCalculation`(
IN masterTableName VARCHAR(100),
IN detailTableName VARCHAR(100),
IN RepSerNo INT,
OUT HardnessNominalN DECIMAL(7,1),
OUT HardnessPosN DECIMAL(7,1),
OUT HardnessNegN DECIMAL(7,1),
OUT HardnessAverageN DECIMAL(7,1),
OUT HardnessStdDevN DECIMAL(7,1),
OUT HardnessMinN DECIMAL(7,1),
OUT HardnessMaxN  DECIMAL(7,1),
OUT HardnessNominalKp DECIMAL(7,1),
OUT HardnessPosKp DECIMAL(7,1),
OUT HardnessNegKp DECIMAL(7,1),
OUT HardnessAverageKp DECIMAL(7,1),
OUT HardnessStdDevKp DECIMAL(7,1),
OUT HardnessMinKp DECIMAL(7,1),
OUT HardnessMaxKp  DECIMAL(7,1),
OUT DOLOBONominal DECIMAL(10,2),
OUT DOLOBOPos DECIMAL(10,2),
OUT DOLOBONeg DECIMAL(10,2),
OUT DOLOBOAverage DECIMAL(10,2),
OUT DOLOBOStdDev DECIMAL(10,3),
OUT DOLOBOMin DECIMAL(10,2),
OUT DOLOBOMax  DECIMAL(10,2),
OUT ThicknessNominal DECIMAL(10,2),
OUT ThicknessPos DECIMAL(10,2),
OUT ThicknessNeg DECIMAL(10,2),
OUT ThicknessAverage DECIMAL(10,2),
OUT ThicknessStdDev DECIMAL(10,3),
OUT ThicknessMin DECIMAL(10,2),
OUT ThicknessMax DECIMAL(10,2),
OUT Unit VARCHAR(5),
OUT RecSeqNo INT,
OUT DiameterNominal DECIMAL(10,2),
OUT DiameterPos DECIMAL(10,2),
OUT DiameterNeg DECIMAL(10,2),
OUT DiameterAverage DECIMAL(10,2),
OUT DiameterStdDev DECIMAL(10,3),
OUT DiameterMin DECIMAL(10,2),
OUT DiameterMax DECIMAL(10,2)
)
BEGIN
SET @query = CONCAT('SELECT `NomThick`,`PosTolThick`,`NegTolThick`,`NomDiam`,`PosTolDiam`,`NegTolDiam`,`NomHard`,`PosTolHard`,`NegTolHard`,`NomDOLOBO`,`PosTolDOLOBO`,`NegTolDOLOBO`,`Unit` INTO 
@NomThick,@PosTolThick,@NegTolThick,@NomDiam,@PosTolDiam,@NegTolDiam,@NomHard,@PosTolHard,@NegTolHard,@NomDOLOBO,@PosTolDOLOBO,@NegTolDOLOBO,@Unit FROM ',masterTableName,' WHERE `RepSerNo`=',RepSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query = CONCAT('SELECT COUNT(*) INTO @totalnum FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,';');
PREPARE stmt FROM @query;
EXECUTE stmt;
SET @query_thick = CONCAT('SELECT 
ROUND(AVG(CAST(`DataValueThick` AS DECIMAL(20,15))),2),
MIN(CAST(`DataValueThick` AS DECIMAL(7,2))),
MAX(CAST(`DataValueThick` AS DECIMAL(7,2))),
ROUND(CAST(STDDEV_SAMP(`DataValueThick`) AS DECIMAL(20,15)),3)
INTO 
@ThicknessAverage,
@ThicknessMin,
@ThicknessMax,
@ThicknessStdDev
FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,' and DataValueThick 
NOT IN ("0", "NA", "FL","--","99999.00","99999","99998.00","99998","9999","9999.00");');
PREPARE stmt FROM @query_thick;
EXECUTE stmt;
SET @query_brlen = CONCAT('SELECT 
ROUND(AVG(CAST(`DataValueDOLOBO` AS DECIMAL(20,15))),2),
MIN(CAST(`DataValueDOLOBO` AS DECIMAL(7,2))),
MAX(CAST(`DataValueDOLOBO` AS DECIMAL(7,2))),
ROUND(CAST(STDDEV_SAMP(`DataValueDOLOBO`) AS DECIMAL(20,15)),3)
INTO 
@DOLOBAverage,
@DOLOBMin,
@DOLOBMax,
@DOLOBStdDev
FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,' and DataValueDOLOBO 
NOT IN ("0", "NA", "FL","--","99999.00","99999","99998.00","99998","9999","9999.00");');
PREPARE stmt FROM @query_brlen;
EXECUTE stmt;
SET @query_diam = CONCAT('SELECT 
ROUND(AVG(CAST(`DataValueDiam` AS DECIMAL(20,15))),2),
MIN(CAST(`DataValueDiam` AS DECIMAL(7,2))),
MAX(CAST(`DataValueDiam` AS DECIMAL(7,2))),
ROUND(CAST(STDDEV_SAMP(`DataValueDiam`) AS DECIMAL(20,15)),3)
INTO 
@DiameterAverage,
@DiameterMin,
@DiameterMax,
@DiameterStdDev 
FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,' and DataValueDiam 
NOT IN ("0", "NA", "FL","--","99999.00","99999","99998.00","99998","9999","9999.00");');
PREPARE stmt FROM @query_diam;
EXECUTE stmt;
SET @query_hard = CONCAT('SELECT 
ROUND(AVG(CAST(`DataValueHard` AS DECIMAL(20,15))),1),
MIN(CAST(`DataValueHard` AS DECIMAL(7,1))),
MAX(CAST(`DataValueHard` AS DECIMAL(7,1))),
ROUND(STDDEV_SAMP(CAST(`DataValueHard` AS DECIMAL(20,15))),1)
INTO 
@HardnessAverage,
@HardnessMin,
@HardnessMax,
@HardnessStdDev 
FROM ',detailTableName,' WHERE `RepSerNo`=',RepSerNo,' and DataValueHard 
NOT IN ("0", "NA", "FL","--","99999.00","99999","99998.00","99998","9999","9999.00");');
PREPARE stmt FROM @query_hard;
EXECUTE stmt;
	
IF (@NomHard != "99999.00000")  THEN
SET HardnessNominalN = @NomHard;
SET HardnessNominalKp = @NomHard;
ELSE
SET HardnessNominalN = 0;
SET HardnessNominalKp = 0;
END IF;
IF (@DOLOBMin != NULL)  THEN
	SET DOLOBOMin = @DOLOBMin;
ELSE
	SET DOLOBOMin = 0;
END IF;
IF (@ThicknessMin != NULL)  THEN
	SET ThicknessMin = @ThicknessMin;
ELSE
	SET ThicknessMin = 0;
END IF;
IF (@DiameterMin != NULL)  THEN
	SET DiameterMin = @DiameterMin;
ELSE
	SET DiameterMin = 0;
END IF;
SET HardnessPosN = @PosTolHard;
SET HardnessNegN = @NegTolHard;
SET HardnessAverageN = @HardnessAverage;
SET HardnessMinN = @HardnessMin;
SET HardnessMaxN = @HardnessMax;
SET HardnessPosKp  = @PosTolHard;
SET HardnessNegKp  = @NegTolHard;
SET HardnessAverageKp  = @HardnessAverage;
SET HardnessMinKp  = @HardnessMin;
SET HardnessMaxKp  = @HardnessMax;
SET HardnessStdDevN  = @HardnessStdDev;
SET HardnessStdDevKp  = @HardnessStdDev;
SET DOLOBONominal = @NomDOLOBO;
SET DOLOBOPos = (@PosTolDOLOBO + @NomDOLOBO);
SET DOLOBONeg = (@NomDOLOBO - @NegTolDOLOBO);
SET DOLOBOAverage = @DOLOBAverage;
SET DOLOBOMin = @DOLOBMin;
SET DOLOBOMax = @DOLOBMax;
SET DOLOBOStdDev = @DOLOBStdDev;
SET ThicknessNominal = @NomThick;
SET ThicknessPos = (@PosTolThick + @NomThick);
SET ThicknessNeg = (@NomThick - @NegTolThick);
SET ThicknessAverage = @ThicknessAverage;
SET ThicknessMin = @ThicknessMin;
SET ThicknessMax = @ThicknessMax;
SET ThicknessStdDev = @ThicknessStdDev;
SET DiameterNominal = @NomDiam;
SET DiameterPos = (@PosTolDiam + @NomDiam);
SET DiameterNeg = (@NomDiam - @NegTolDiam);
SET DiameterAverage = @DiameterAverage;
SET DiameterMin = @DiameterMin;
SET DiameterMax = @DiameterMax;
SET DiameterStdDev = @DiameterStdDev;
SET Unit = @Unit;
SET RecSeqNo = @totalnum;
IF (@HardnessStdDev IS NULL OR @HardnessStdDev = '')  THEN
	SET HardnessStdDevKp = 0;
ELSE
	SET HardnessStdDevKp  = @HardnessStdDev;
END IF;
IF (@DOLOBStdDev IS NULL OR @DOLOBStdDev = '')  THEN
	SET DOLOBOStdDev = 0;
ELSE
	SET DOLOBOStdDev = @DOLOBStdDev;
END IF;
IF (@ThicknessStdDev IS NULL OR @ThicknessStdDev = '')  THEN
	SET ThicknessStdDev = 0;
ELSE
	SET ThicknessStdDev = @ThicknessStdDev;
END IF;
IF (@DiameterStdDev IS NULL OR @DiameterStdDev = '')  THEN
	SET DiameterStdDev = 0;
ELSE
	SET DiameterStdDev = @DiameterStdDev;
END IF;
END */$$
DELIMITER ;

/* Procedure structure for procedure `TruncateAllTables` */

/*!50003 DROP PROCEDURE IF EXISTS  `TruncateAllTables` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `TruncateAllTables`(IN schemaName VARCHAR(255))
BEGIN
    DECLARE done INT DEFAULT 0;
    DECLARE tableName VARCHAR(255);
    -- Declare a cursor to select table names dynamically
    DECLARE cur CURSOR FOR 
        SELECT TABLE_NAME a
        FROM information_schema.tables
        WHERE table_schema = schemaName
        AND table_type = 'BASE TABLE'
        AND TABLE_NAME NOT IN (
            'tbl_users', 'tbl_cubical_nos', 'tbl_area_cubicletype',
            'tbl_cubicle_area', 'tbl_alert_param_duration', 'tbl_calibration_status',
            'tbl_calibration_status_bin', 'tbl_cubical', 'tbl_cubicle_bin_setting',
            'tbl_cubicle_product_sample', 'tbl_recalibration_balance_status',
            'tbl_recalibration_balance_status_bin', 'tbl_recalibration_vernier_status',
            'tbl_system_weighingstatus', 'tbl_rpi', 'tbl_calibration_sequnce',
            'tbl_menulist', 'tbl_nomenclature', 'tbl_pwd_complexity',
            'tbl_rights', 'tbl_role', 'tbl_rpt_path', 'tbl_setallparameter'
        );
    -- Handler for when the cursor is exhausted
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
    -- Open the cursor
    OPEN cur;
    -- Start the loop
    truncate_loop: LOOP
        -- Fetch the next table name
        FETCH cur INTO tableName;
        -- Exit the loop if no more rows
        IF done THEN
            LEAVE truncate_loop;
        END IF;
        -- Construct and execute the TRUNCATE statement
        SET @sql = CONCAT('TRUNCATE TABLE ', schemaName, '.', tableName);
        PREPARE stmt FROM @sql;
        EXECUTE stmt;
        DEALLOCATE PREPARE stmt;
    END LOOP;
    -- Close the cursor
    CLOSE cur;
END */$$
DELIMITER ;

/* Procedure structure for procedure `updateCubicle` */

/*!50003 DROP PROCEDURE IF EXISTS  `updateCubicle` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `updateCubicle`()
BEGIN
/******** VERIABLE DECLARATION *******************/
    DECLARE X INT DEFAULT 0;
    DECLARE Y INT DEFAULT 125;
/******** END OF VERIABLE DECLARATION ************/ 
        
    REPEAT 
	SET  X = X + 1; 
	SET  Y = Y + 1; 
	 SET @queryString2 = CONCAT("update tbl_cubical set Sys_CubicNo=",X," where Sys_CubicNo=",Y); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
	 
	  SET @queryString2 = CONCAT("update tbl_syssettingstatus set CubicleNo=",X," where CubicleNo=",Y); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
	 
	  SET @queryString2 = CONCAT("update tbl_cubical_product_sample set Sys_CubicNo=",X," where Sys_CubicNo=",Y); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
	 
	  SET @queryString2 = CONCAT("update tbl_calibration_status set Cubicle=",X," where Cubicle=",Y); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
	 
	   SET @queryString2 = CONCAT("update tbl_balrecalibstatus set CubicNo=",X," where CubicNo=",Y); 
         PREPARE stmt2 FROM  @queryString2; 
	 EXECUTE stmt2; 
	 
    UNTIL X = 125
    END REPEAT;
    END */$$
DELIMITER ;

/* Procedure structure for procedure `update_CalibrationStatus_RepNo` */

/*!50003 DROP PROCEDURE IF EXISTS  `update_CalibrationStatus_RepNo` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `update_CalibrationStatus_RepNo`(IN BalanceID VARCHAR(50), OUT RunningRepNo INT)
BEGIN
	
	/*DECLARE RepNo INT DEFAULT 0;*/
	/*DECLARE RunningRepNo INT DEFAULT 0;*/
	DECLARE balID VARCHAR(50);
	DECLARE queryString VARCHAR(50);
	SET @balID = BalanceID;
	
	SELECT MAX(RepNo) INTO RunningRepNo FROM tbl_calibration_status;
	
	SET @queryString = CONCAT("UPDATE tbl_calibration_status SET RepNo=",RunningRepNo+1," WHERE BalID='",@balID,"'"); 
	PREPARE stmt1 FROM  @queryString; 
	EXECUTE stmt1; 
	DEALLOCATE PREPARE stmt1;
	
    END */$$
DELIMITER ;

/* Procedure structure for procedure `userManagement` */

/*!50003 DROP PROCEDURE IF EXISTS  `userManagement` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `userManagement`(
IN userID VARCHAR(100),
IN userPassword VARCHAR(100),
IN source VARCHAR(20),
IN HostName VARCHAR(100),
OUT message VARCHAR(500),
OUT userName VARCHAR(50))
BEGIN
	/* -- UPDATE LOG :
	-1- 18082020 : Added Lockuser audit trail.
	-2- 08012021 :  Update Failed AtmptTime if pwd is wrong 
	-3- 01022021 : Activity Log userID -> @userID Change
	-4- 11022021 : Validation Sequence Chged for PDisable and TDisabled*/
	DECLARE PasswordExpiryPeriod SMALLINT(3);
	DECLARE PasswordExpiryReminder SMALLINT(3);
	DECLARE AutoEnableChances SMALLINT(3);
	DECLARE LoginAttempts SMALLINT(3);
	DECLARE AutoDisablePeriod SMALLINT(3);
	DECLARE LockPeriod SMALLINT(3);
	SELECT `tbl_config_PasswordExpPeriod`,`tbl_config_ReminderPassword`,
	`tbl_config_AutoEnableChances`,`tbl_config_LoginAttempts`,
	`tbl_config_AutoDisablePeriod`,`tbl_config_DisabledTime` 
	INTO 
	@PasswordExpiryPeriod, @PasswordExpiryReminder,
	@AutoEnableChances, @LoginAttempts, 
	@AutoDisablePeriod, @LockPeriod 
	FROM `tbl_setallparameter`;
	SET PasswordExpiryPeriod = @PasswordExpiryPeriod;
	SET PasswordExpiryReminder = @PasswordExpiryReminder;
	SET AutoEnableChances = @AutoEnableChances;
	SET LoginAttempts = @LoginAttempts;
	SET AutoDisablePeriod = @AutoDisablePeriod;
	SET LockPeriod = @LockPeriod; 
	START TRANSACTION; -- FROM HERE TRANSACTION WILL START
	SET @query = CONCAT("SELECT UserID,`UserName`,`userType`,`PwdChgDate`,`PwdExpStauts`,`Status`, `loginatmpt`,`suspensionPeriod`,`autoEnbl`,`active`,`Role`,`source`,`PREV_STATUS`,`realPassword`,`locked`,`PwdChg`,`UserInitials`
	INTO @UserID,@userName,@userType,@PwdChgDate,@PwdExpStauts,@Status,@loginatmpt,@suspensionPeriod,@autoEnbl,@active,@Role,@source,@PREV_STATUS,@realPassword,@locked,@PwdChg,@UserInitials
	FROM `tbl_users` WHERE UPPER(`UserID`)=UPPER('",userID,"') FOR UPDATE"); -- ACQUIRED LOCK ON ROW  
	PREPARE stmt FROM  @query; 
	EXECUTE stmt; 
	SET @A =DATE_ADD(@PwdChgDate,INTERVAL @PasswordExpiryPeriod DAY);
	SET @B =DATE_SUB(DATE_ADD(@PwdChgDate, INTERVAL PasswordExpiryPeriod DAY), INTERVAL PasswordExpiryReminder DAY);
	SET @C =DATEDIFF(DATE_ADD(@PwdChgDate,INTERVAL PasswordExpiryPeriod DAY),NOW());
	IF((@active!=0) AND (@source='IDS')) THEN
		SET @outMessage = 'User Already Active On IDS';
	ELSEIF(@active!=0) THEN
		SET @outMessage = 'User Already Active On Software';
	ELSEIF((@locked=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'User Locked, Please contact Admin';
	ELSEIF((@Status=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'User Temporary Disabled, Contact Admin';
	ELSEIF((@Status=2) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'User Permanent Disabled, Contact Admin';	
	ELSEIF((BINARY userPassword != BINARY @realPassword)) THEN
		-- Update Failed Attempt Time
		SET @query = CONCAT("UPDATE `tbl_users` SET `failedAtmpTime`= NOW() WHERE `UserID`='",userID,"'"); 
		PREPARE stmt FROM  @query; 
		EXECUTE stmt;
		IF((@userType=1) OR (@Role='SuperAdmin')) THEN
			SET @outMessage = 'Incorrect Credentials';
		ELSE
			IF((LoginAttempts>@loginatmpt+1) AND (@autoEnbl=0)) THEN
				-- Still few login attempts are remaining
				SET @query = CONCAT("UPDATE `tbl_users` SET `loginatmpt`=`loginatmpt`+1 WHERE `UserID`='",userID,"'"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt; 
				SET @outMessage = 'Incorrect Credentials';
			ELSEIF((@Status=6) AND (@suspensionPeriod>NOW())) THEN
				SET @outMessage = CONCAT('User is Locked for ',TIME_TO_SEC(TIMEDIFF(@suspensionPeriod,NOW())),' Seconds');
			ELSEIF(AutoEnableChances=@autoEnbl) THEN
				SET @query = CONCAT("UPDATE `tbl_users` SET `Status`= 6, `PwdExpStauts`= 1 WHERE `UserID`='",userID,"'"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt;
				
								-- Audit Trail of User Lock
				SET @query = CONCAT("INSERT INTO `tbl_audit_users` (`dt`, `tm`, `userid`, `username`, `ACT`, `KeyCode`, `KeyValue`,`Remark`) VALUES (NOW(), NOW(), '",userID,"', '",@userName,"', 'User Locked','",userID,"', '",@userName,"','Auto Enable Chances Exhausted')"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt;
				
				SET @outMessage = CONCAT('Suspicious Activity Found, Contact Admin');
			ELSEIF((LoginAttempts=@loginatmpt+1) OR (@autoEnbl>0)) THEN
				-- Here login attempts are completed, hence lock user and let him know			
				SET @query = CONCAT("UPDATE `tbl_users` SET `autoEnbl`=`autoEnbl`+1, `Status`= 6, `suspensionPeriod`=DATE_ADD(NOW(), INTERVAL ",@LockPeriod," SECOND) WHERE `UserID`='",userID,"'"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt;
				-- Audit Trail of User Lock
				SET @query = CONCAT("INSERT INTO `tbl_audit_users` (`dt`, `tm`, `userid`, `username`, `ACT`, `KeyCode`, `KeyValue`,`Remark`) VALUES (NOW(), NOW(), '",userID,"', '",@userName,"', 'User Locked','",userID,"', '",@userName,"','Exceeded Max No. of Login Attempts')"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt;
				
				SET @outMessage = CONCAT('User is Locked for ',@LockPeriod,' Seconds');
			ELSE
			
				-- Audit Trail of User Lock
				SET @query = CONCAT("INSERT INTO `tbl_audit_users` (`dt`, `tm`, `userid`, `username`, `ACT`, `KeyCode`, `KeyValue`,`Remark`) VALUES (NOW(), NOW(), '",userID,"', '",@userName,"', 'User Locked','",userID,"', '",@userName,"','Auto Enable Chances Exhausted')"); 
				PREPARE stmt FROM  @query; 
				EXECUTE stmt;
				
				SET @outMessage = CONCAT('Suspicious Activity Found, Contact Admin');	
			END IF;
		END IF;
	ELSEIF((@Status=4) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'User Auto Disabled, Change Password';
	ELSEIF((@Status=6) AND (@PwdExpStauts!=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN	
		SET @outMessage = 'User Locked, Please contact Admin';
	ELSEIF((@Status=6) AND (@PwdExpStauts=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'Suspicious Activity Found, Contact Admin';
	ELSEIF((@PwdChg=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'Please Change Your Password';
	ELSEIF((@PwdExpStauts=1) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @outMessage = 'Password Expired, Please Change Your Password';
	ELSEIF((NOW()>=DATE_ADD(@PwdChgDate,INTERVAL @PasswordExpiryPeriod DAY)) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		SET @query = CONCAT("UPDATE `tbl_users` SET PwdExpStauts=1 WHERE `UserID`='",userID,"'"); 
		PREPARE stmt FROM  @query; 
		EXECUTE stmt;
		SET @outMessage = 'Password Expired, Please Change Your Password';
	ELSEIF(((@A>NOW()) AND (NOW()>=@B)) AND (@Role!='SuperAdmin') AND (@userType!=1)) THEN
		IF(@C<=PasswordExpiryReminder) THEN
			SET @query = CONCAT("UPDATE `tbl_users` SET `Status`=0,`active`=1,`source`='",source,"',`LastLoginDt`=NOW(),`HostName`='",HostName,"',`loginCounter`=0,`loginatmpt`=0,`autoEnbl`=0,`Domain`='0' WHERE `UserID`='",userID,"'"); 
			PREPARE stmt FROM  @query; 
			EXECUTE stmt;
			SET @outMessage = CONCAT('Your Password Expire in ',@C,' Days');
		ELSE
			SET @query = CONCAT("UPDATE `tbl_users` SET `Status`=0,`active`=1,`source`='",source,"',`LastLoginDt`=NOW(),`HostName`='",HostName,"',`loginCounter`=0,`loginatmpt`=0,`autoEnbl`=0,`Domain`='0' WHERE `UserID`='",userID,"'"); 
			PREPARE stmt FROM  @query; 
			EXECUTE stmt;
			SET @outMessage = 'success';
		END IF;
		SET @query = CONCAT("INSERT INTO `tbl_activity_log` (`dt`, `tm`, `userid`, `username`, `activity`) VALUES (NOW(), NOW(), '",@UserID,"', '",@userName,"', 'Logged in on software : ",@UserID,"')"); 
		PREPARE stmt FROM  @query; 
		EXECUTE stmt;
	ELSE
		SET @query = CONCAT("UPDATE `tbl_users` SET `Status`=0,`active`=1,`source`='",source,"',`LastLoginDt`=NOW(),`HostName`='",HostName,"',`loginCounter`=0,`loginatmpt`=0,`autoEnbl`=0,`Domain`='0' WHERE `UserID`='",userID,"'"); 
		PREPARE stmt FROM  @query; 
		EXECUTE stmt;
		
		IF ( source = 'Software') THEN
		SET @query = CONCAT("INSERT INTO `tbl_activity_log` (`dt`, `tm`, `userid`, `username`, `activity`) VALUES (NOW(), NOW(), '",@UserID,"', '",@userName,"', 'Logged in on software : ",@UserID,"')"); 
		PREPARE stmt FROM  @query; 
		EXECUTE stmt;
		END IF;
		
		SET @outMessage = 'success';
		/*SET @outMessage = CONCAT('A=',@A,' B=',@B, ' C=',@C); USED FOR DEBUGGING */
	END IF;
	SET message = @outMessage;
	SET userName = @userName;
	COMMIT; -- END OF TRANSACTION
END */$$
DELIMITER ;

/* Procedure structure for procedure `user_counter_reset` */

/*!50003 DROP PROCEDURE IF EXISTS  `user_counter_reset` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `user_counter_reset`()
BEGIN
        DECLARE timeoutperiod INT DEFAULT 0;
    DECLARE resetcounter INT DEFAULT 0;
    DECLARE cnt INT DEFAULT 0;
    DECLARE Y INT DEFAULT 0;
    SELECT tbl_config_TimeoutPeriod INTO timeoutperiod FROM tbl_setallparameter WHERE config_id = 1;
        SET Y=timeoutperiod*60;
        SET resetCounter = Y/5;
        /*if resetCounter = cnt then */
        UPDATE tbl_users SET loginCounter=loginCounter+1 WHERE active=1 AND loginCounter<resetCounter AND source='SOFTWARE';
        UPDATE tbl_users SET active=1 WHERE Domain='WEB CLIENT SOCKET disconnects' AND loginCounter<resetCounter AND source='SOFTWARE';
       /*ELSE  */
            UPDATE tbl_users SET active=0,loginCounter=0,Domain=0 WHERE loginCounter>=resetCounter;
       /* END IF; */
    END */$$
DELIMITER ;

/* Procedure structure for procedure `user_suspensionPeriod_reset` */

/*!50003 DROP PROCEDURE IF EXISTS  `user_suspensionPeriod_reset` */;

DELIMITER $$

/*!50003 CREATE DEFINER=`TANTRASOFT`@`%` PROCEDURE `user_suspensionPeriod_reset`()
BEGIN
	UPDATE tbl_users 
	SET `Status`=0,suspensionPeriod=NULL 
	WHERE 
	(suspensionPeriod IS NOT NULL) AND
	(TIMESTAMP(NOW())>=TIMESTAMP(suspensionPeriod))
	AND autoEnbl<=(SELECT tbl_config_AutoEnableChances FROM tbl_setallparameter WHERE config_id=1);
END */$$
DELIMITER ;

