text: USE `Ratemanagement3`;
  CREATE TABLE IF NOT EXISTS `tblAuditHeader` (
  	`AuditHeaderID` INT(11) UNSIGNED NOT NULL AUTO_INCREMENT,
  	`UserID` INT(11) NOT NULL,
  	`CompanyID` INT(11) NOT NULL,
  	`Date` DATE NULL DEFAULT NULL COMMENT 'account, product etc',
  	`ParentColumnName` TEXT NULL COLLATE 'utf8_unicode_ci',
  	`ParentColumnID` INT(11) NOT NULL,
  	`Type` VARCHAR(255) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
  	`IP` TEXT NULL COLLATE 'utf8_unicode_ci',
  	`UserType` INT(11) NULL DEFAULT '0',
  	PRIMARY KEY (`AuditHeaderID`)
  )
  COLLATE='utf8_unicode_ci'
  ENGINE=InnoDB
  ;
  
  
  CREATE TABLE IF NOT EXISTS `tblAuditDetails` (
  	`AuditDetailID` BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
  	`AuditHeaderID` INT(11) UNSIGNED NOT NULL,
  	`ColumnName` VARCHAR(255) NOT NULL COLLATE 'utf8_unicode_ci',
  	`OldValue` LONGTEXT NULL COLLATE 'utf8_unicode_ci',
  	`NewValue` LONGTEXT NULL COLLATE 'utf8_unicode_ci',
  	`created_at` DATETIME NULL DEFAULT NULL,
  	`created_by` VARCHAR(100) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
  	PRIMARY KEY (`AuditDetailID`),
  	INDEX `IX_AuditHeaderID` (`AuditHeaderID`)
  )
  COLLATE='utf8_unicode_ci'
  ENGINE=InnoDB
  ;
  
  DROP PROCEDURE IF EXISTS `prc_WSProcessVendorRate`;
  DELIMITER |
  CREATE PROCEDURE `prc_WSProcessVendorRate`(
  	IN `p_accountId` INT,
  	IN `p_trunkId` INT,
  	IN `p_replaceAllRates` INT,
  	IN `p_effectiveImmediately` INT,
  	IN `p_processId` VARCHAR(200),
  	IN `p_addNewCodesToCodeDeck` INT,
  	IN `p_companyId` INT,
  	IN `p_forbidden` INT,
  	IN `p_preference` INT,
  	IN `p_dialstringid` INT,
  	IN `p_dialcodeSeparator` VARCHAR(50)
  )
  BEGIN
  
      DECLARE v_AffectedRecords_ INT DEFAULT 0;
  	 DECLARE     v_CodeDeckId_ INT ;
      DECLARE totaldialstringcode INT(11) DEFAULT 0;	 
      DECLARE newstringcode INT(11) DEFAULT 0;
  	 DECLARE totalduplicatecode INT(11);	 
  	 DECLARE errormessage longtext;
  	 DECLARE errorheader longtext;
      
  	 SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;    
  	 
  	 
      DROP TEMPORARY TABLE IF EXISTS tmp_JobLog_;
      CREATE TEMPORARY TABLE tmp_JobLog_ (
          Message longtext
      );
      
      
      
      DROP TEMPORARY TABLE IF EXISTS tmp_split_VendorRate_;
      CREATE TEMPORARY TABLE tmp_split_VendorRate_ (
      		`TempVendorRateID` int,
  			`CodeDeckId` int ,
  			`Code` varchar(50) ,
  			`Description` varchar(200) ,
  			`Rate` decimal(18, 6) ,
  			`EffectiveDate` Datetime ,
  			`Change` varchar(100) ,
  			`ProcessId` varchar(200) ,
  			`Preference` varchar(100) ,
  			`ConnectionFee` decimal(18, 6),
  			`Interval1` int,
  			`IntervalN` int,
  			`Forbidden` varchar(100) ,
  			`DialStringPrefix` varchar(500) ,
  			INDEX tmp_EffectiveDate (`EffectiveDate`),
  			INDEX tmp_Code (`Code`),
              INDEX tmp_CC (`Code`,`Change`),
  			INDEX tmp_Change (`Change`)
      );
      
      DROP TEMPORARY TABLE IF EXISTS tmp_TempVendorRate_;
      CREATE TEMPORARY TABLE tmp_TempVendorRate_ (
  			`CodeDeckId` int ,
  			`Code` varchar(50) ,
  			`Description` varchar(200) ,
  			`Rate` decimal(18, 6) ,
  			`EffectiveDate` Datetime ,
  			`Change` varchar(100) ,
  			`ProcessId` varchar(200) ,
  			`Preference` varchar(100) ,
  			`ConnectionFee` decimal(18, 6),
  			`Interval1` int,
  			`IntervalN` int,
  			`Forbidden` varchar(100) ,
  			`DialStringPrefix` varchar(500) ,
  			INDEX tmp_EffectiveDate (`EffectiveDate`),
  			INDEX tmp_Code (`Code`),
              INDEX tmp_CC (`Code`,`Change`),
  			INDEX tmp_Change (`Change`)
      );
      
      
      
      DROP TEMPORARY TABLE IF EXISTS tmp_Delete_VendorRate;
  	CREATE TEMPORARY TABLE tmp_Delete_VendorRate (
       	VendorRateID INT,
        AccountId INT,
        TrunkID INT,
        RateId INT,
        Code VARCHAR(50),
        Description VARCHAR(200),
        Rate DECIMAL(18, 6),
        EffectiveDate DATETIME,
  		Interval1 INT,
  		IntervalN INT,
  		ConnectionFee DECIMAL(18, 6),
  		deleted_at DATETIME,
          INDEX tmp_VendorRateDiscontinued_VendorRateID (`VendorRateID`)
  	);
  
      
      		CALL  prc_checkDialstringAndDupliacteCode(p_companyId,p_processId,p_dialstringid,p_effectiveImmediately,p_dialcodeSeparator);
      		
      		SELECT COUNT(*) AS COUNT INTO newstringcode from tmp_JobLog_; 
  	
  	
      IF newstringcode = 0
      THEN
     		
  	         IF  p_addNewCodesToCodeDeck = 1
              THEN
  
  					
                  INSERT INTO tblRate (CompanyID,
                  Code,
                  Description,
                  CreatedBy,
                  CountryID,
                  CodeDeckId,
                  Interval1,
                  IntervalN)
                      SELECT DISTINCT
                          p_companyId,
                          vc.Code,
                          vc.Description,
                          'WindowsService',
                         
                         fnGetCountryIdByCodeAndCountry (vc.Code ,vc.Description) AS CountryID,
                          CodeDeckId,
                          Interval1,
                          IntervalN
                      FROM
                      (
                          SELECT DISTINCT
                              tblTempVendorRate.Code,
                              tblTempVendorRate.Description,
                              tblTempVendorRate.CodeDeckId,
                              tblTempVendorRate.Interval1,
                              tblTempVendorRate.IntervalN
  
                          FROM tmp_TempVendorRate_  as tblTempVendorRate
                     	     LEFT JOIN tblRate
  						             ON tblRate.Code = tblTempVendorRate.Code
  						             AND tblRate.CompanyID = p_companyId
  						             AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  	                        WHERE tblRate.RateID IS NULL
  	                     	   AND tblTempVendorRate.`Change` NOT IN ('Delete', 'R', 'D', 'Blocked', 'Block')) vc; 
                    						
  
  
                 	SELECT GROUP_CONCAT(Code) into errormessage FROM(
                      SELECT DISTINCT
                          tblTempVendorRate.Code as Code, 1 as a
                      FROM tmp_TempVendorRate_  as tblTempVendorRate 
  	                    INNER JOIN tblRate
  					             ON tblRate.Code = tblTempVendorRate.Code
  					             AND tblRate.CompanyID = p_companyId
  					             AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  							  WHERE tblRate.CountryID IS NULL
  	                 		   AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block')) as tbl GROUP BY a;
                      
                    IF errormessage IS NOT NULL
                    THEN
                    
                      INSERT INTO tmp_JobLog_ (Message)
                      	 SELECT DISTINCT
                          CONCAT(tblTempVendorRate.Code , ' INVALID CODE - COUNTRY NOT FOUND')
                          FROM tmp_TempVendorRate_  as tblTempVendorRate 
                      INNER JOIN tblRate
  				             ON tblRate.Code = tblTempVendorRate.Code
  				             AND tblRate.CompanyID = p_companyId
  				             AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  						  WHERE tblRate.CountryID IS NULL
                      AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block');
                          
  					 	END IF;		
  					 
              ELSE
                  
                  SELECT GROUP_CONCAT(code) into errormessage FROM(
                      SELECT DISTINCT
                          c.Code as code, 1 as a
                      FROM
                      (
                          SELECT DISTINCT
                              tblTempVendorRate.Code,
                              tblTempVendorRate.Description
                          FROM tmp_TempVendorRate_  as tblTempVendorRate 
                        	  LEFT JOIN tblRate
  				                ON tblRate.Code = tblTempVendorRate.Code
  				          	      AND tblRate.CompanyID = p_companyId
  				         	       AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  									WHERE tblRate.RateID IS NULL
  	                  	      AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked', 'Block')) c) as tbl GROUP BY a;
                          
                    IF errormessage IS NOT NULL
                    THEN
                      INSERT INTO tmp_JobLog_ (Message)
                      		SELECT DISTINCT
                          CONCAT(tbl.Code , ' CODE DOES NOT EXIST IN CODE DECK')
                          FROM
                      (
                          SELECT DISTINCT
                              tblTempVendorRate.Code,
                              tblTempVendorRate.Description
                          FROM tmp_TempVendorRate_  as tblTempVendorRate 
                          LEFT JOIN tblRate
  			                ON tblRate.Code = tblTempVendorRate.Code
  			                AND tblRate.CompanyID = p_companyId
  			                AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  								WHERE tblRate.RateID IS NULL
                          AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked', 'Block')) as tbl;
                          
  					 	END IF;		
  
  
              END IF;
  
              IF  p_replaceAllRates = 1
              THEN
  
  
                  DELETE FROM tblVendorRate
                  WHERE AccountId = p_accountId
                      AND TrunkID = p_trunkId;
  
              END IF;
              				
  				
         
  		 
  		          
              	INSERT INTO tmp_Delete_VendorRate(
  			 	VendorRateID,
  		      AccountId,
  		      TrunkID,
  		      RateId,
  		      Code,
  		      Description,
  		      Rate,
  		      EffectiveDate,
  				Interval1,
  				IntervalN,
  				ConnectionFee,
  				deleted_at
  			 )
      		SELECT tblVendorRate.VendorRateID,
      			    p_accountId AS AccountId,
  					 p_trunkId AS TrunkID,
  					 tblVendorRate.RateId,
  					 tblRate.Code,
  					 tblRate.Description,
  					 tblVendorRate.Rate,
  					 tblVendorRate.EffectiveDate,
  					 tblVendorRate.Interval1,
  					 tblVendorRate.IntervalN,
  					 tblVendorRate.ConnectionFee,
  					 now() AS deleted_at  		
                  FROM tblVendorRate
                  JOIN tblRate
                      ON tblRate.RateID = tblVendorRate.RateId
                      AND tblRate.CompanyID = p_companyId
                      JOIN tmp_TempVendorRate_ as tblTempVendorRate
                          ON tblRate.Code = tblTempVendorRate.Code
              WHERE tblVendorRate.AccountId = p_accountId
                  AND tblVendorRate.TrunkId = p_trunkId
                  AND tblTempVendorRate.Change IN ('Delete', 'R', 'D', 'Blocked', 'Block');    
             
             
  			  	CALL prc_InsertDiscontinuedVendorRate(p_accountId,p_trunkId); 
  
              UPDATE tblVendorRate
  					INNER JOIN tblRate
  						ON tblVendorRate.RateId = tblRate.RateId
  						AND tblVendorRate.AccountId = p_accountId
  						AND tblVendorRate.TrunkId = p_trunkId
  					INNER JOIN tmp_TempVendorRate_ as tblTempVendorRate
  						ON tblRate.Code = tblTempVendorRate.Code
  						AND tblRate.CompanyID = p_companyId
  						AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  						AND tblVendorRate.RateId = tblRate.RateId
  					SET tblVendorRate.ConnectionFee = tblTempVendorRate.ConnectionFee,
  						tblVendorRate.Interval1 = tblTempVendorRate.Interval1,
  							tblVendorRate.IntervalN = tblTempVendorRate.IntervalN
  					WHERE tblVendorRate.AccountId = p_accountId
  			            AND tblVendorRate.TrunkId = p_trunkId ;
  			    
  				 
              IF  p_forbidden = 1 OR p_dialstringid > 0
  				THEN
  					
  					INSERT INTO tblVendorBlocking
  					(
  						 `AccountId`
  						 ,`RateId`
  						 ,`TrunkID`
  						 ,`BlockedBy`
  					)
  					SELECT distinct
  					   p_accountId as AccountId,
  					   tblRate.RateID as RateId,						
  						p_trunkId as TrunkID,
  						'RMService' as BlockedBy
  					 FROM tmp_TempVendorRate_ as tblTempVendorRate
  					 INNER JOIN tblRate 
  						ON tblRate.Code = tblTempVendorRate.Code
  						AND tblRate.CompanyID = p_companyId
  			         AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  			       LEFT JOIN tblVendorBlocking vb 			       		
  					 	ON vb.AccountId=p_accountId
  						 AND vb.RateId = tblRate.RateID
  						 AND vb.TrunkID = p_trunkId   
  					WHERE tblTempVendorRate.Forbidden IN('B')
  					 AND vb.VendorBlockingId is null;
  					 
  					 DELETE tblVendorBlocking 
  					 FROM tblVendorBlocking 
  					INNER JOIN(
  						select VendorBlockingId 
  						FROM `tblVendorBlocking` tv
  							INNER JOIN(
  							 SELECT 
  							 	tblRate.RateId as RateId
  							 FROM tmp_TempVendorRate_ as tblTempVendorRate
  							INNER JOIN tblRate 
  								ON tblRate.Code = tblTempVendorRate.Code
  								AND tblRate.CompanyID = p_companyId
  					         AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  							WHERE tblTempVendorRate.Forbidden IN('UB')
  					     )tv1 on  tv.AccountId=p_accountId
  						  	AND tv.TrunkID=p_trunkId
  						  	AND tv.RateId = tv1.RateID
  					 )vb2 on vb2.VendorBlockingId = tblVendorBlocking.VendorBlockingId;
  	
  				END IF;
  				
  				
  				
  				IF  p_preference = 1
  				THEN
  				
  				INSERT INTO tblVendorPreference
  					(
  						 `AccountId`
  						 ,`Preference`
  						 ,`RateId`
  						 ,`TrunkID`
  						 ,`CreatedBy`
  						 ,`created_at`
  					)
  				SELECT 
  					   p_accountId AS AccountId,
  					   tblTempVendorRate.Preference as Preference,
  					   tblRate.RateID AS RateId,						
  						p_trunkId AS TrunkID,
  						'RMService' AS CreatedBy,
  						NOW() AS created_at
  					 FROM tmp_TempVendorRate_ as tblTempVendorRate
  					INNER JOIN tblRate 
  						ON tblRate.Code = tblTempVendorRate.Code
  						AND tblRate.CompanyID = p_companyId
  			         AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
  					LEFT JOIN tblVendorPreference vp 
  						ON vp.RateId=tblRate.RateID
  						AND vp.AccountId = p_accountId 	
  						AND vp.TrunkID = p_trunkId
  					WHERE  tblTempVendorRate.Preference IS NOT NULL
  					 AND  tblTempVendorRate.Preference > 0
  					 AND  vp.VendorPreferenceID IS NULL;
  					 
  					 
  					 UPDATE tblVendorPreference
  					 	INNER JOIN tblRate 
  					 		ON tblVendorPreference.RateId=tblRate.RateID
  				      INNER JOIN tmp_TempVendorRate_ as tblTempVendorRate
  							ON tblTempVendorRate.Code = tblRate.Code							
  				         AND tblTempVendorRate.CodeDeckId = tblRate.CodeDeckId   
  				         AND tblRate.CompanyID = p_companyId
  				      SET tblVendorPreference.Preference = tblTempVendorRate.Preference
  						WHERE tblVendorPreference.AccountId = p_accountId  
  							AND tblVendorPreference.TrunkID = p_trunkId
  							AND  tblTempVendorRate.Preference IS NOT NULL
  							AND  tblTempVendorRate.Preference > 0
  							AND tblVendorPreference.VendorPreferenceID IS NOT NULL; 
  							
  						DELETE tblVendorPreference
  							from	tblVendorPreference
  					 	INNER JOIN tblRate 
  					 		ON tblVendorPreference.RateId=tblRate.RateID
  				      INNER JOIN tmp_TempVendorRate_ as tblTempVendorRate
  							ON tblTempVendorRate.Code = tblRate.Code							
  				         AND tblTempVendorRate.CodeDeckId = tblRate.CodeDeckId   
  				         AND tblRate.CompanyID = p_companyId
  						WHERE tblVendorPreference.AccountId = p_accountId  
  							AND tblVendorPreference.TrunkID = p_trunkId
  							AND  tblTempVendorRate.Preference IS NOT NULL
  							AND  tblTempVendorRate.Preference = '' 
  							AND tblVendorPreference.VendorPreferenceID IS NOT NULL; 
  					 
  				END IF; 
  				         
  
              DELETE tblTempVendorRate
                  FROM tmp_TempVendorRate_ as tblTempVendorRate
                  JOIN tblRate
                      ON tblRate.Code = tblTempVendorRate.Code
                      JOIN tblVendorRate
                          ON tblVendorRate.RateId = tblRate.RateId
                          AND tblRate.CompanyID = p_companyId
                          AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
                          AND tblVendorRate.AccountId = p_accountId
                          AND tblVendorRate.TrunkId = p_trunkId
                          AND tblTempVendorRate.Rate = tblVendorRate.Rate
                          AND (
                          tblVendorRate.EffectiveDate = tblTempVendorRate.EffectiveDate 
                          OR  
                          (
                          DATE_FORMAT (tblVendorRate.EffectiveDate, '%Y-%m-%d') = DATE_FORMAT (tblTempVendorRate.EffectiveDate, '%Y-%m-%d')
                          )
                          OR 1 = (CASE
                              WHEN tblTempVendorRate.EffectiveDate > NOW() THEN 1 
                              ELSE 0
                          END)
                          )
              WHERE  tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked', 'Block'); 
  
              SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS();
  
  				
              UPDATE tmp_TempVendorRate_ as tblTempVendorRate
              JOIN tblRate
                  ON tblRate.Code = tblTempVendorRate.Code
                  AND tblRate.CompanyID = p_companyId
                  AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
                  JOIN tblVendorRate
                      ON tblVendorRate.RateId = tblRate.RateId
                      AND tblVendorRate.AccountId = p_accountId
                      AND tblVendorRate.TrunkId = p_trunkId
  				SET tblVendorRate.Rate = tblTempVendorRate.Rate
              WHERE tblTempVendorRate.Rate <> tblVendorRate.Rate
              AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked', 'Block')
              AND DATE_FORMAT (tblVendorRate.EffectiveDate, '%Y-%m-%d') = DATE_FORMAT (tblTempVendorRate.EffectiveDate, '%Y-%m-%d');
  
  
  
              SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS();
  
  				
              INSERT INTO tblVendorRate (AccountId,
              TrunkID,
              RateId,
              Rate,
              EffectiveDate,
              ConnectionFee,
              Interval1,
              IntervalN
              )
                  SELECT DISTINCT
                      p_accountId,
                      p_trunkId,
                      tblRate.RateID,
                      tblTempVendorRate.Rate,
                      tblTempVendorRate.EffectiveDate,
                      tblTempVendorRate.ConnectionFee,
                      tblTempVendorRate.Interval1,
                      tblTempVendorRate.IntervalN
                  FROM tmp_TempVendorRate_ as tblTempVendorRate
                  JOIN tblRate
                      ON tblRate.Code = tblTempVendorRate.Code
                      AND tblRate.CompanyID = p_companyId
                      AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
                  LEFT JOIN tblVendorRate
  					      ON tblRate.RateID = tblVendorRate.RateId
  					      AND tblVendorRate.AccountId = p_accountId
  					      AND tblVendorRate.trunkid = p_trunkId
  					      AND tblTempVendorRate.EffectiveDate = tblVendorRate.EffectiveDate
  					WHERE tblVendorRate.VendorRateID IS NULL
                  AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block')
                  AND tblTempVendorRate.EffectiveDate >= DATE_FORMAT (NOW(), '%Y-%m-%d');
  
              SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS(); 
  
  	 
   END IF;	 
  	 
  	 					 INSERT INTO tmp_JobLog_ (Message)
  	 	SELECT CONCAT(v_AffectedRecords_ , ' Records Uploaded \n\r ' );
  	 
   	 SELECT * FROM tmp_JobLog_;
       DELETE  FROM tblTempVendorRate WHERE  ProcessId = p_processId; 
  	 
  	 SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  END|
  DELIMITER ;
  
  
  DROP PROCEDURE IF EXISTS `prc_checkDialstringAndDupliacteCode`;
  DELIMITER |
  CREATE PROCEDURE `prc_checkDialstringAndDupliacteCode`(
  	IN `p_companyId` INT,
  	IN `p_processId` VARCHAR(200) ,
  	IN `p_dialStringId` INT,
  	IN `p_effectiveImmediately` INT,
  	IN `p_dialcodeSeparator` VARCHAR(50)
  )
  BEGIN
  	
      DECLARE totaldialstringcode INT(11) DEFAULT 0;	 
      DECLARE     v_CodeDeckId_ INT ;
    	 DECLARE totalduplicatecode INT(11);	 
    	 DECLARE errormessage longtext;
  	 DECLARE errorheader longtext;
  
  
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorRateDialString_ ;			
  	 CREATE TEMPORARY TABLE `tmp_VendorRateDialString_` (						
  						`CodeDeckId` int ,
  						`Code` varchar(50) ,
  						`Description` varchar(200) ,
  						`Rate` decimal(18, 6) ,
  						`EffectiveDate` Datetime ,
  						`Change` varchar(100) ,
  						`ProcessId` varchar(200) ,
  						`Preference` varchar(100) ,
  						`ConnectionFee` decimal(18, 6),
  						`Interval1` int,
  						`IntervalN` int,
  						`Forbidden` varchar(100) ,
  						`DialStringPrefix` varchar(500)
  					);
  		
  		
  		
  		CALL prc_SplitVendorRate(p_processId,p_dialcodeSeparator);							
  		
  		DROP TEMPORARY TABLE IF EXISTS tmp_split_VendorRate_2;
  		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_split_VendorRate_2 as (SELECT * FROM tmp_split_VendorRate_);
                                               
  	     DELETE n1 FROM tmp_split_VendorRate_ n1, tmp_split_VendorRate_2 n2 
       WHERE n1.EffectiveDate < n2.EffectiveDate 
  	 	AND n1.CodeDeckId = n2.CodeDeckId
  		AND  n1.Code = n2.Code
  		AND  n1.ProcessId = n2.ProcessId
   		AND  n1.ProcessId = p_processId AND n2.ProcessId = p_processId;
  
  		  INSERT INTO tmp_TempVendorRate_
  	        SELECT DISTINCT `CodeDeckId`,
  			  						`Code`,
  									`Description`,
  									`Rate`,
  									`EffectiveDate`,
  									`Change`,
  									`ProcessId`,
  									`Preference`,
  									`ConnectionFee`,
  									`Interval1`,
  									`IntervalN`,
  									`Forbidden`,
  									`DialStringPrefix`
  						 FROM tmp_split_VendorRate_
  						 	 WHERE tmp_split_VendorRate_.ProcessId = p_processId;
  			
  		
  	 	     SELECT CodeDeckId INTO v_CodeDeckId_ 
  					FROM tmp_TempVendorRate_
  						 WHERE ProcessId = p_processId  LIMIT 1;
  
              UPDATE tmp_TempVendorRate_ as tblTempVendorRate
              LEFT JOIN tblRate
                  ON tblRate.Code = tblTempVendorRate.Code
                  AND tblRate.CompanyID = p_companyId
                  AND tblRate.CodeDeckId = tblTempVendorRate.CodeDeckId
                  AND tblRate.CodeDeckId =  v_CodeDeckId_
              SET  
                  tblTempVendorRate.Interval1 = CASE WHEN tblTempVendorRate.Interval1 is not null  and tblTempVendorRate.Interval1 > 0
                                              THEN    
                                                  tblTempVendorRate.Interval1
                                              ELSE
                                              CASE WHEN tblRate.Interval1 is not null  
                                              THEN    
                                                  tblRate.Interval1
                                              ELSE CASE WHEN tblTempVendorRate.Interval1 is null and (tblTempVendorRate.Description LIKE '%gambia%' OR tblTempVendorRate.Description LIKE '%mexico%')
                                                   THEN 
                                                      60
                                              ELSE CASE WHEN tblTempVendorRate.Description LIKE '%USA%' 
                                                   THEN 
                                                      6
                                                   ELSE 
                                                      1
                                                  END
                                              END
  
                                              END
                                              END,
                  tblTempVendorRate.IntervalN = CASE WHEN tblTempVendorRate.IntervalN is not null  and tblTempVendorRate.IntervalN > 0
                                              THEN    
                                                  tblTempVendorRate.IntervalN
                                              ELSE
                                                  CASE WHEN tblRate.IntervalN is not null 
                                            THEN
                                              tblRate.IntervalN
                                            ElSE
                                              CASE
                                                  WHEN tblTempVendorRate.Description LIKE '%mexico%' THEN 60
                                              ELSE CASE
                                                  WHEN tblTempVendorRate.Description LIKE '%USA%' THEN 6
                                                  
                                              ELSE 
                                              1
                                              END
                                              END
                                            END
                                            END;
  
   			
  			IF  p_effectiveImmediately = 1
              THEN
              
              	
              
                  UPDATE tmp_TempVendorRate_
             	     SET EffectiveDate = DATE_FORMAT (NOW(), '%Y-%m-%d')
             		     WHERE EffectiveDate < DATE_FORMAT (NOW(), '%Y-%m-%d');
  
              END IF;
  			
  			
  			SELECT count(*) INTO totalduplicatecode FROM(
  				SELECT count(code) as c,code FROM tmp_TempVendorRate_  GROUP BY Code,EffectiveDate,DialStringPrefix HAVING c>1) AS tbl;
  				
  			
  			IF  totalduplicatecode > 0
  			THEN	
  			
  			
  				SELECT GROUP_CONCAT(code) into errormessage FROM(
  					SELECT DISTINCT code, 1 as a FROM(
  						SELECT   count(code) as c,code FROM tmp_TempVendorRate_  GROUP BY Code,EffectiveDate,DialStringPrefix HAVING c>1) AS tbl) as tbl2 GROUP by a;				
  				
  				INSERT INTO tmp_JobLog_ (Message)
  				  SELECT DISTINCT 
  				  CONCAT(code , ' DUPLICATE CODE')
  				  	FROM(
  						SELECT   count(code) as c,code FROM tmp_TempVendorRate_  GROUP BY Code,EffectiveDate,DialStringPrefix HAVING c>1) AS tbl;				
  					
  			END IF;
  							
     IF	totalduplicatecode = 0
     THEN
  						
  	 
      IF p_dialstringid >0
      THEN
      		
      		DROP TEMPORARY TABLE IF EXISTS tmp_DialString_;
      		CREATE TEMPORARY TABLE tmp_DialString_ (
  				`DialStringID` INT,
  				`DialString` VARCHAR(250),
  				`ChargeCode` VARCHAR(250),
  				`Description` VARCHAR(250),
  				`Forbidden` VARCHAR(50),
  				INDEX tmp_DialStringID (`DialStringID`),			
  	         INDEX tmp_DialStringID_ChargeCode (`DialStringID`,`ChargeCode`)
           );
  
           INSERT INTO tmp_DialString_ 
  			SELECT DISTINCT
  				`DialStringID`,
  				`DialString`,
  				`ChargeCode`,
  				`Description`,
  				`Forbidden`
  			FROM tblDialStringCode
  				WHERE DialStringID = p_dialstringid;
           
           SELECT  COUNT(*) as count INTO totaldialstringcode
  			FROM tmp_TempVendorRate_ vr
  				LEFT JOIN tmp_DialString_ ds
  					ON ((vr.Code = ds.ChargeCode and vr.DialStringPrefix = '') OR (vr.DialStringPrefix != '' and vr.DialStringPrefix =  ds.DialString and vr.Code = ds.ChargeCode  )) 
  					-- ON vr.Code = ds.ChargeCode 
  				WHERE vr.ProcessId = p_processId 
  					AND ds.DialStringID IS NULL
  					AND vr.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block');
  		   
           IF totaldialstringcode > 0
           THEN
           
  				INSERT INTO tmp_JobLog_ (Message)
  				  SELECT DISTINCT CONCAT(Code ,' ', vr.DialStringPrefix , ' No PREFIX FOUND')
  				  	FROM tmp_TempVendorRate_ vr
  						LEFT JOIN tmp_DialString_ ds
  							-- ON vr.Code = ds.ChargeCode
  							ON ((vr.Code = ds.ChargeCode and vr.DialStringPrefix = '') OR (vr.DialStringPrefix != '' and vr.DialStringPrefix =  ds.DialString and vr.Code = ds.ChargeCode  )) 
  						WHERE vr.ProcessId = p_processId
  							AND ds.DialStringID IS NULL				
  							AND vr.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block');
           
           END IF;
  
           IF totaldialstringcode = 0
           THEN
  				 
  				 	INSERT INTO tmp_VendorRateDialString_ 	
  					 SELECT DISTINCT
  							`CodeDeckId`,
  							`DialString`,
  							CASE WHEN ds.Description IS NULL OR ds.Description = ''
  								THEN    
  									tblTempVendorRate.Description									
  								ELSE
  									ds.Description
  							END
  								AS Description,
  							`Rate`,
  							`EffectiveDate`,
  							`Change`,
  							`ProcessId`,
  							`Preference`,
  							`ConnectionFee`,
  							`Interval1`,
  							`IntervalN`,
  							tblTempVendorRate.Forbidden as Forbidden ,
  							tblTempVendorRate.DialStringPrefix as DialStringPrefix
  					   FROM tmp_TempVendorRate_ as tblTempVendorRate
  							INNER JOIN tmp_DialString_ ds
  							  --	 ON tblTempVendorRate.Code = ds.ChargeCode
  							  	ON ( (tblTempVendorRate.Code = ds.ChargeCode AND tblTempVendorRate.DialStringPrefix = '') OR (tblTempVendorRate.DialStringPrefix != '' AND tblTempVendorRate.DialStringPrefix =  ds.DialString AND tblTempVendorRate.Code = ds.ChargeCode  ))
  							  -- on (tblTempVendorRate.DialStringPrefix != '' and tblTempVendorRate.DialStringPrefix =  ds.DialString and tblTempVendorRate.Code = ds.ChargeCode  )
  						 WHERE tblTempVendorRate.ProcessId = p_processId
  							AND tblTempVendorRate.Change NOT IN ('Delete', 'R', 'D', 'Blocked','Block');							
  		
  						
  					DROP TEMPORARY TABLE IF EXISTS tmp_VendorRateDialString_2;
  					CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorRateDialString_2 as (SELECT * FROM tmp_VendorRateDialString_);
  					
  					DROP TEMPORARY TABLE IF EXISTS tmp_VendorRateDialString_3;
  					CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorRateDialString_3 as (					
  					 SELECT vrs1.* from tmp_VendorRateDialString_2 vrs1
  					 LEFT JOIN tmp_VendorRateDialString_ vrs2 ON vrs1.Code=vrs2.Code AND vrs1.CodeDeckId=vrs2.CodeDeckId AND vrs1.Description=vrs2.Description AND vrs1.EffectiveDate=vrs2.EffectiveDate AND vrs1.DialStringPrefix != vrs2.DialStringPrefix
  					 WHERE ((vrs1.DialStringPrefix ='' AND vrs2.Code IS NULL) OR (vrs1.DialStringPrefix!='' AND vrs2.Code IS NOT NULL))						 
  					);
  					
  					
  					DELETE  FROM tmp_TempVendorRate_ WHERE  ProcessId = p_processId; 
  					
  					INSERT INTO tmp_TempVendorRate_(
  							CodeDeckId,
  							Code,
  							Description,
  							Rate,
  							EffectiveDate,
  							`Change`,
  							ProcessId,
  							Preference,
  							ConnectionFee,
  							Interval1,
  							IntervalN,
  							Forbidden,
  							DialStringPrefix
  						)
  					SELECT DISTINCT 
  						`CodeDeckId`,
  						`Code`,
  						`Description`,
  						`Rate`,
  						`EffectiveDate`,
  						`Change`,
  						`ProcessId`,
  						`Preference`,
  						`ConnectionFee`,
  						`Interval1`,
  						`IntervalN`,
  						`Forbidden`,
  						DialStringPrefix 
  					 FROM tmp_VendorRateDialString_3;
  					   
  				  	UPDATE tmp_TempVendorRate_ as tblTempVendorRate
  					JOIN tmp_DialString_ ds
  					-- ON tblTempVendorRate.Code = ds.DialString
  					  ON ( (tblTempVendorRate.Code = ds.ChargeCode and tblTempVendorRate.DialStringPrefix = '') OR (tblTempVendorRate.DialStringPrefix != '' and tblTempVendorRate.DialStringPrefix =  ds.DialString and tblTempVendorRate.Code = ds.ChargeCode  )) 
  							AND tblTempVendorRate.ProcessId = p_processId
  							AND ds.Forbidden = 1
  					SET tblTempVendorRate.Forbidden = 'B';
  					
  					UPDATE tmp_TempVendorRate_ as  tblTempVendorRate
  					JOIN tmp_DialString_ ds
  						-- ON tblTempVendorRate.Code = ds.DialString
  						ON ( (tblTempVendorRate.Code = ds.ChargeCode and tblTempVendorRate.DialStringPrefix = '') OR (tblTempVendorRate.DialStringPrefix != '' and tblTempVendorRate.DialStringPrefix =  ds.DialString and tblTempVendorRate.Code = ds.ChargeCode  ))
  							AND tblTempVendorRate.ProcessId = p_processId
  							AND ds.Forbidden = 0
  					SET tblTempVendorRate.Forbidden = 'UB';
  					        
  			END IF;	  
  
      END IF;	
     
  END IF; 
  
   	
  
  END|
  DELIMITER ;
  
  
  DROP PROCEDURE IF EXISTS `prc_SplitVendorRate`;
  DELIMITER |
  CREATE PROCEDURE `prc_SplitVendorRate`(
  	IN `p_processId` VARCHAR(200),
  	IN `p_dialcodeSeparator` VARCHAR(50)
  )
  BEGIN
  
  	DECLARE i INTEGER;
  	DECLARE v_rowCount_ INT;
  	DECLARE v_pointer_ INT;	
  	DECLARE v_TempVendorRateID_ INT;
  	DECLARE v_Code_ VARCHAR(500);	
  	DECLARE newcodecount INT(11) DEFAULT 0;
  	
  	IF p_dialcodeSeparator !='null'
  	THEN
  	
  	
  	
  	
  	
  	DROP TEMPORARY TABLE IF EXISTS `my_splits`;
  	CREATE TEMPORARY TABLE `my_splits` (
  		`TempVendorRateID` INT(11) NULL DEFAULT NULL,
  		`Code` Text NULL DEFAULT NULL
  	);
      
    SET i = 1;
    REPEAT
      INSERT INTO my_splits (TempVendorRateID, Code)
        SELECT TempVendorRateID , FnStringSplit(Code, p_dialcodeSeparator, i)  FROM tblTempVendorRate
        WHERE FnStringSplit(Code, p_dialcodeSeparator , i) IS NOT NULL
  			 AND ProcessId = p_processId;
      SET i = i + 1;
      UNTIL ROW_COUNT() = 0
    END REPEAT;
    
    UPDATE my_splits SET Code = trim(Code);
    
  	
    
    
    DROP TEMPORARY TABLE IF EXISTS tmp_newvendor_splite_;
  	CREATE TEMPORARY TABLE tmp_newvendor_splite_  (
  		RowID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  		TempVendorRateID INT(11) NULL DEFAULT NULL,
  		Code VARCHAR(500) NULL DEFAULT NULL
  	);
  	
  	INSERT INTO tmp_newvendor_splite_(TempVendorRateID,Code)
  	SELECT 
  		TempVendorRateID,
  		Code
  	FROM my_splits
  	WHERE Code like '%-%'
  		AND TempVendorRateID IS NOT NULL;
  
    
    
  	SET v_pointer_ = 1;
  	SET v_rowCount_ = (SELECT COUNT(*)FROM tmp_newvendor_splite_);
  	
  	WHILE v_pointer_ <= v_rowCount_
  	DO
  		SET v_TempVendorRateID_ = (SELECT TempVendorRateID FROM tmp_newvendor_splite_ t WHERE t.RowID = v_pointer_); 
  		SET v_Code_ = (SELECT Code FROM tmp_newvendor_splite_ t WHERE t.RowID = v_pointer_);
  		
  		Call prc_SplitAndInsertVendorRate(v_TempVendorRateID_,v_Code_);
  		
  	SET v_pointer_ = v_pointer_ + 1;
  	END WHILE;
  	
  	
  	
  	DELETE FROM my_splits
  		WHERE Code like '%-%'
  			AND TempVendorRateID IS NOT NULL;
  
  	
  
  	
  	
  	 INSERT INTO tmp_split_VendorRate_
  	SELECT DISTINCT
  		   my_splits.TempVendorRateID as `TempVendorRateID`,
  		   `CodeDeckId`,
  		   my_splits.Code as Code,
  		   `Description`,
  			`Rate`,
  			`EffectiveDate`,
  			`Change`,
  			`ProcessId`,
  			`Preference`,
  			`ConnectionFee`,
  			`Interval1`,
  			`IntervalN`,
  			`Forbidden`,
  			`DialStringPrefix`
  		 FROM my_splits
  		   INNER JOIN tblTempVendorRate 
  				ON my_splits.TempVendorRateID = tblTempVendorRate.TempVendorRateID
  		  WHERE	tblTempVendorRate.ProcessId = p_processId;	
  		  
  	END IF;
  	
  	IF p_dialcodeSeparator = 'null'
  	THEN
  	
  		INSERT INTO tmp_split_VendorRate_
  		SELECT DISTINCT
  			  `TempVendorRateID`,
  			  `CodeDeckId`,
  			   `Code`,
  			   `Description`,
  				`Rate`,
  				`EffectiveDate`,
  				`Change`,
  				`ProcessId`,
  				`Preference`,
  				`ConnectionFee`,
  				`Interval1`,
  				`IntervalN`,
  				`Forbidden`,
  				`DialStringPrefix`
  			 FROM tblTempVendorRate
  			  WHERE ProcessId = p_processId;	
  	
  	END IF;	
  		
  END|
  DELIMITER ;	
  
  DROP PROCEDURE IF EXISTS `prc_GetLCRwithPrefix`;
  DELIMITER |
  CREATE PROCEDURE `prc_GetLCRwithPrefix`(
  	IN `p_companyid` INT,
  	IN `p_trunkID` INT,
  	IN `p_codedeckID` INT,
  	IN `p_CurrencyID` INT,
  	IN `p_code` VARCHAR(50),
  	IN `p_Description` VARCHAR(250),
  	IN `p_AccountIds` TEXT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_SortOrder` VARCHAR(50),
  	IN `p_Preference` INT,
  	IN `p_Position` INT,
  	IN `p_isExport` INT
  )
  BEGIN
  
  DECLARE v_OffSet_ int;
  DECLARE v_Code VARCHAR(50) ;
  DECLARE v_pointer_ int;
  DECLARE v_rowCount_ int;
  DECLARE v_p_code VARCHAR(50);
  DECLARE v_Codlen_ int;
  DECLARE v_position int;
  DECLARE v_p_code__ VARCHAR(50);
  DECLARE v_has_null_position int ;
  DECLARE v_next_position1 VARCHAR(200) ;
  DECLARE v_CompanyCurrencyID_ INT; 	
  	
      SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
       SET @@session.collation_connection='utf8_unicode_ci';
       SET @@session.character_set_results='utf8';
  
      SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
    
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_stage_;
         CREATE TEMPORARY TABLE tmp_VendorRate_stage_ (
            RowCode VARCHAR(50) ,
            AccountId INT ,
            AccountName VARCHAR(100) ,
            Code VARCHAR(50) ,
            Rate DECIMAL(18,6) ,
            ConnectionFee DECIMAL(18,6) ,
            EffectiveDate DATETIME ,
            Description VARCHAR(255),
            Preference INT,
            MaxMatchRank int ,
            prev_prev_RowCode VARCHAR(50),
            prev_AccountID int
          ) 
    ;
  
       DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_stage2_;
         CREATE TEMPORARY TABLE tmp_VendorRate_stage2_ (
            RowCode VARCHAR(50) ,
            AccountId INT ,
            AccountName VARCHAR(100) ,
            Code VARCHAR(50) ,
            Rate DECIMAL(18,6) ,
            ConnectionFee DECIMAL(18,6) ,
            EffectiveDate DATETIME ,
            Description VARCHAR(255),
            Preference INT,
            MaxMatchRank int ,
            prev_prev_RowCode VARCHAR(50),
            prev_AccountID int
          ) 
    ;
  
    DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_;
    CREATE TEMPORARY TABLE tmp_VendorRate_ (
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       RowCode VARCHAR(50) 
     ) 
     ;
       
     DROP TEMPORARY TABLE IF EXISTS tmp_final_VendorRate_;
    CREATE TEMPORARY TABLE tmp_final_VendorRate_ (
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       RowCode VARCHAR(50),
       FinalRankNumber int
     ) 
    ;
     
  
  DROP TEMPORARY TABLE IF EXISTS tmp_search_code_;
    CREATE TEMPORARY TABLE tmp_search_code_ (
       Code  varchar(50),
       INDEX Index1 (Code)
  );
  
  
  DROP TEMPORARY TABLE IF EXISTS tmp_code_;
    CREATE TEMPORARY TABLE tmp_code_ (
       RowCode  varchar(50),
       Code  varchar(50),
       RowNo int,
       INDEX Index1 (Code)
     );
  
  
  DROP TEMPORARY TABLE IF EXISTS tmp_all_code_;
    CREATE TEMPORARY TABLE tmp_all_code_ ( 
       RowCode  varchar(50),
       Code  varchar(50),
       
       INDEX Index2 (Code)
     )
     ;
  
          
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorCurrentRates_;
  CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorCurrentRates_(
  			AccountId int,
           AccountName varchar(200),
  			Code varchar(50),
  			Description varchar(200),
  			Rate DECIMAL(18,6) ,
           ConnectionFee DECIMAL(18,6) , 
  			EffectiveDate date,
  			TrunkID int,
  			CountryID int,
  			RateID int,
  			Preference int,
  			INDEX IX_Code (Code)
  	) 
    ;
  
       DROP TEMPORARY TABLE IF EXISTS tmp_VendorCurrentRates1_;
  CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorCurrentRates1_(
  			AccountId int,
           AccountName varchar(200),
  			Code varchar(50),
  			Description varchar(200),
  			Rate DECIMAL(18,6) ,
                 ConnectionFee DECIMAL(18,6) , 
  			EffectiveDate date,
  			TrunkID int,
  			CountryID int,
  			RateID int,
  			Preference int,
  			INDEX IX_Code (Code),
  			INDEX tmp_VendorCurrentRates_AccountId (`AccountId`,`TrunkID`,`RateId`,`EffectiveDate`)
  	) 
    ;
  
   DROP TEMPORARY TABLE IF EXISTS tmp_VendorRateByRank_;
    CREATE TEMPORARY TABLE tmp_VendorRateByRank_ (      
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       rankname INT,
       INDEX IX_Code (Code,rankname)
     )
     ;
  
  	SELECT CurrencyId INTO v_CompanyCurrencyID_ FROM  tblCompany WHERE CompanyID = p_companyid;
       
       INSERT INTO tmp_VendorCurrentRates1_ 
  	  Select DISTINCT AccountId,AccountName,Code,Description, Rate,ConnectionFee,EffectiveDate,TrunkID,CountryID,RateID,Preference 
        FROM (
  				SELECT distinct tblVendorRate.AccountId,tblAccount.AccountName, tblRate.Code, tblRate.Description, 
  				 CASE WHEN  tblAccount.CurrencyId = p_CurrencyID
  								THEN
  									   tblVendorRate.Rate
  					 WHEN  v_CompanyCurrencyID_ = p_CurrencyID  
  							 THEN
  								  (
  								   ( tblVendorRate.rate  / (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = tblAccount.CurrencyId and  CompanyID = p_companyid ) )
  								 )
  							  ELSE 
  							  (
  										
  								  (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = p_CurrencyID and  CompanyID = p_companyid )
  									* (tblVendorRate.rate  / (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = tblAccount.CurrencyId and  CompanyID = p_companyid ))
  							  )
  							 END
  								as  Rate,
  								 ConnectionFee,
  			  DATE_FORMAT (tblVendorRate.EffectiveDate, '%Y-%m-%d') AS EffectiveDate, 
  			    tblVendorRate.TrunkID, tblRate.CountryID, tblRate.RateID,IFNULL(vp.Preference, 5) AS Preference 
  			  FROM      tblVendorRate
  					 Inner join tblVendorTrunk vt on vt.CompanyID = p_companyid AND vt.AccountID = tblVendorRate.AccountID and vt.Status =  1 and vt.TrunkID =  p_trunkID
  							 
  						INNER JOIN tblAccount   ON  tblAccount.CompanyID = p_companyid AND tblVendorRate.AccountId = tblAccount.AccountID 
  						INNER JOIN tblRate ON tblRate.CompanyID = p_companyid   AND    tblVendorRate.RateId = tblRate.RateID
  						
  						
  						
  						LEFT JOIN tblVendorPreference vp
  										 ON vp.AccountId = tblVendorRate.AccountId
  										 AND vp.TrunkID = tblVendorRate.TrunkID
  										 AND vp.RateId = tblVendorRate.RateId
  						LEFT OUTER JOIN tblVendorBlocking AS blockCode   ON tblVendorRate.RateId = blockCode.RateId
  																	  AND tblVendorRate.AccountId = blockCode.AccountId
  																	  AND tblVendorRate.TrunkID = blockCode.TrunkID
  						LEFT OUTER JOIN tblVendorBlocking AS blockCountry    ON tblRate.CountryID = blockCountry.CountryId
  																	  AND tblVendorRate.AccountId = blockCountry.AccountId
  																	  AND tblVendorRate.TrunkID = blockCountry.TrunkID
  			  WHERE      
  						( CHAR_LENGTH(RTRIM(p_code)) = 0 OR tblRate.Code LIKE REPLACE(p_code,'*', '%') )  
  						AND (p_Description='' OR tblRate.Description LIKE REPLACE(p_Description,'*','%'))
  						AND (p_AccountIds='' OR FIND_IN_SET(tblAccount.AccountID,p_AccountIds) != 0 )
  						AND EffectiveDate <= NOW() 
  						AND tblAccount.IsVendor = 1
  						AND tblAccount.Status = 1
  						AND tblAccount.CurrencyId is not NULL
  						AND tblVendorRate.TrunkID = p_trunkID
  						AND blockCode.RateId IS NULL
  					   AND blockCountry.CountryId IS NULL
  			
  		) tbl
  		order by Code asc;
           
       
       INSERT INTO tmp_VendorCurrentRates_ 
  	  Select AccountId,AccountName,Code,Description, Rate,ConnectionFee,EffectiveDate,TrunkID,CountryID,RateID,Preference 
        FROM (
  			  SELECT * ,
  				@row_num := IF(@prev_AccountId = AccountID AND @prev_TrunkID = TrunkID AND @prev_RateId = RateID AND @prev_EffectiveDate >= EffectiveDate, @row_num + 1, 1) AS RowID,
  				@prev_AccountId := AccountID,
  				@prev_TrunkID := TrunkID,
  				@prev_RateId := RateID,
  				@prev_EffectiveDate := EffectiveDate
  			  FROM tmp_VendorCurrentRates1_
  			  ,(SELECT @row_num := 1,  @prev_AccountId := '',@prev_TrunkID := '', @prev_RateId := '', @prev_EffectiveDate := '') x
             ORDER BY AccountId, TrunkID, RateId, EffectiveDate DESC	
  		) tbl
  		 WHERE RowID = 1 
  		order by Code asc;
  
  	IF p_Preference = 1 THEN
  	
  		INSERT IGNORE INTO tmp_VendorRateByRank_
  		  SELECT
  			AccountID,
  			AccountName,
  			Code,
  			Rate,
  			ConnectionFee,
  			EffectiveDate,
  			Description,
  			Preference,
  			preference_rank
  		  FROM (SELECT
  			  AccountID,
  			  AccountName,
  			  Code,
  			  Rate,
  			  ConnectionFee,
  			  EffectiveDate,
  			  Description,
  			  Preference,
  			  @preference_rank := CASE WHEN (@prev_Code     = Code AND @prev_Preference > Preference  ) THEN @preference_rank + 1 
  									   WHEN (@prev_Code     = Code AND @prev_Preference = Preference AND @prev_Rate < Rate) THEN @preference_rank + 1 
  									   WHEN (@prev_Code    = Code AND @prev_Preference = Preference AND @prev_Rate = Rate) THEN @preference_rank 
  									   ELSE 1 
  									   END AS preference_rank,
  			  @prev_Code := Code,
  			  @prev_Preference := IFNULL(Preference, 5),
  			  @prev_Rate := Rate
  			FROM tmp_VendorCurrentRates_ AS preference,
  				 (SELECT @preference_rank := 0 , @prev_Code := ''  , @prev_Preference := 5,  @prev_Rate := 0) x
  			ORDER BY preference.Code ASC, preference.Preference DESC, preference.Rate ASC,preference.AccountId ASC
  			   
  			 ) tbl
  		  WHERE preference_rank <= p_Position
  		  ORDER BY Code, preference_rank;
  
  	ELSE
  	
  		INSERT IGNORE INTO tmp_VendorRateByRank_
  		  SELECT
  			AccountID,
  			AccountName,
  			Code,
  			Rate,
  			ConnectionFee,
  			EffectiveDate,
  			Description,
  			Preference,
  			RateRank
  		  FROM (SELECT
  			  AccountID,
  			  AccountName,
  			  Code,
  			  Rate,
  			  ConnectionFee,
  			  EffectiveDate,
  			  Description,
  			  Preference,
  			  @rank := CASE WHEN (@prev_Code    = Code AND @prev_Rate < Rate) THEN @rank + 1 
  							WHEN (@prev_Code    = Code AND @prev_Rate = Rate) THEN @rank 
  							ELSE 1 
  							END AS RateRank,
  			  @prev_Code := Code,
  			  @prev_Rate := Rate
  			FROM tmp_VendorCurrentRates_ AS rank,
  				 (SELECT @rank := 0 , @prev_Code := '' , @prev_Rate := 0) f
  			ORDER BY rank.Code ASC, rank.Rate,rank.AccountId ASC) tbl
  		  WHERE RateRank <= p_Position
  		  ORDER BY Code, RateRank;
  
  	END IF;
  	
            insert ignore into tmp_VendorRate_
            select
               distinct 
  				 AccountId ,
  				 AccountName ,
  				 Code ,
  				 Rate ,
                   ConnectionFee,                                                           
  				 EffectiveDate ,
  				 Description ,
  				 Preference,
  				 Code as RowCode
  		   from tmp_VendorRateByRank_ 
   			  order by RowCode desc; 
                                     
  	IF( p_Preference = 0 )
  	THEN
  			
  		insert into tmp_final_VendorRate_
  		SELECT
  			 AccountId ,
  			 AccountName ,
  			 Code ,
  			 Rate ,
  				ConnectionFee,
  			 EffectiveDate ,
  			 Description ,
  			 Preference, 
  			 RowCode,
  			 FinalRankNumber
  			from 
  			( 				
  				SELECT
  				AccountId ,
  				 AccountName ,
  				 Code ,
  				 Rate ,
  					 ConnectionFee,
  				 EffectiveDate ,
  				 Description ,
  				 Preference,
  				 RowCode,
  				 @rank := CASE WHEN ( @prev_RowCode     = RowCode AND @prev_Rate <  Rate ) THEN @rank+1
  							   WHEN ( @prev_RowCode    = RowCode AND @prev_Rate = Rate ) THEN @rank 
  					   ELSE 
  					   1
  					   END 
  					  AS FinalRankNumber,
  				@prev_RowCode  := RowCode,
  				@prev_Rate  := Rate
  				from tmp_VendorRate_ 
  				,( SELECT @rank := 0 , @prev_RowCode := '' , @prev_Rate := 0 ) x 
  				order by RowCode,Rate,AccountId ASC
  			 
  			) tbl1
  			where 
  			FinalRankNumber <= p_Position;
  			 
  	 ELSE 
  	 
  		 insert into tmp_final_VendorRate_
  		SELECT
  			AccountId ,
  			 AccountName ,
  			 Code ,
  			 Rate ,
  				ConnectionFee,
  			 EffectiveDate ,
  			 Description ,
  			 Preference, 
  			 RowCode,
  			 FinalRankNumber
  			from 
  			( 				
  				SELECT
  					AccountId ,
  					 AccountName ,
  					 Code ,
  					 Rate ,
  						 ConnectionFee,
  					 EffectiveDate ,
  					 Description ,
  					 Preference,
  					 RowCode,
  						  @preference_rank := CASE WHEN (@prev_Code     = RowCode AND @prev_Preference > Preference  )   THEN @preference_rank + 1 
  												   WHEN (@prev_Code     = RowCode AND @prev_Preference = Preference AND @prev_Rate < Rate) THEN @preference_rank + 1 
  												   WHEN (@prev_Code    = RowCode AND @prev_Preference = Preference AND @prev_Rate = Rate) THEN @preference_rank 
  												   ELSE 1 END AS FinalRankNumber,
  						  @prev_Code := RowCode,
  						  @prev_Preference := Preference,
  						  @prev_Rate := Rate
  				from tmp_VendorRate_ 
  					  ,(SELECT @preference_rank := 0 , @prev_Code := ''  , @prev_Preference := 5,  @prev_Rate := 0) x
  				 order by RowCode ASC ,Preference DESC ,Rate ASC ,AccountId ASC
  					 
  			) tbl1
  			where 
  			FinalRankNumber <= p_Position;
  	 
  	 END IF;
            
  
      IF (p_Position = 5)
      THEN      
  
  		IF (p_isExport = 0)
  		THEN    	
  			
  			SELECT
  	  	      CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`
  			FROM tmp_final_VendorRate_  t
  			  GROUP BY  RowCode   
  			  ORDER BY RowCode ASC
  			LIMIT p_RowspPage OFFSET v_OffSet_ ;
  	   
  			SELECT count(distinct RowCode) as totalcount from tmp_final_VendorRate_ where ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR RowCode LIKE REPLACE(p_code,'*', '%') );
  		   
  	   ELSE 
  			SELECT
  			  CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`
  			FROM tmp_final_VendorRate_  t
  			  GROUP BY  RowCode   
  			  ORDER BY RowCode ASC;
  			
  	   END IF; 
     
      END IF;
      
      IF (p_Position = 10)
      THEN       
  
  		IF (p_isExport = 0)
  		THEN    	
  			
  			SELECT
  				CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 6, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 6`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 7, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 7`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 8, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 8`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 9, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 9`,
  				GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 10,CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 10`
  			FROM tmp_final_VendorRate_  t
  			  GROUP BY  RowCode   
  			  ORDER BY RowCode ASC
  			LIMIT p_RowspPage OFFSET v_OffSet_ ;
  	   
  			SELECT count(distinct RowCode) as totalcount from tmp_final_VendorRate_ where    ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR RowCode LIKE REPLACE(p_code,'*', '%') );
  		   
  		ELSE 
  			SELECT
  			  CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 6, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 6`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 7, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 7`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 8, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 8`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 9, CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 9`,
  			  GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 10,CONCAT(  ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName) , '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 10`
  			FROM tmp_final_VendorRate_  t
  			  GROUP BY  RowCode   
  			  ORDER BY RowCode ASC;     	
  		END IF; 
     
      END IF;
  
  
  SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_GetLCR`;
  DELIMITER |
  CREATE PROCEDURE `prc_GetLCR`(
  	IN `p_companyid` INT,
  	IN `p_trunkID` INT,
  	IN `p_codedeckID` INT,
  	IN `p_CurrencyID` INT,
  	IN `p_code` VARCHAR(50),
  	IN `p_Description` VARCHAR(250),
  	IN `p_AccountIds` TEXT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_SortOrder` VARCHAR(50),
  	IN `p_Preference` INT,
  	IN `p_Position` INT,
  	IN `p_isExport` INT
  )
  BEGIN
  
      
      DECLARE v_OffSet_ int;
  
      DECLARE v_Code VARCHAR(50) ;
  	DECLARE v_pointer_ int;
  	DECLARE v_rowCount_ int;
  	DECLARE v_p_code VARCHAR(50);
  	DECLARE v_Codlen_ int;
  	DECLARE v_position int;
  	DECLARE v_p_code__ VARCHAR(50);
  	DECLARE v_has_null_position int ;
  	DECLARE v_next_position1 VARCHAR(200) ;
  	DECLARE v_CompanyCurrencyID_ INT; 	
  	
      SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
       SET @@session.collation_connection='utf8_unicode_ci';
       SET @@session.character_set_results='utf8';
  
  	-- just for taking codes -  
  	 
      SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;  
    
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_stage_;
         CREATE TEMPORARY TABLE tmp_VendorRate_stage_ (
            RowCode VARCHAR(50) ,
            AccountId INT ,
            AccountName VARCHAR(100) ,
            Code VARCHAR(50) ,
            Rate DECIMAL(18,6) ,
            ConnectionFee DECIMAL(18,6) ,
            EffectiveDate DATETIME ,
            Description VARCHAR(255),
            Preference INT,
            MaxMatchRank int ,
            prev_prev_RowCode VARCHAR(50),
            prev_AccountID int
          ) 
    ;
  
       DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_stage2_;
         CREATE TEMPORARY TABLE tmp_VendorRate_stage2_ (
            RowCode VARCHAR(50) ,
            AccountId INT ,
            AccountName VARCHAR(100) ,
            Code VARCHAR(50) ,
            Rate DECIMAL(18,6) ,
            ConnectionFee DECIMAL(18,6) ,
            EffectiveDate DATETIME ,
            Description VARCHAR(255),
            Preference INT,
            MaxMatchRank int ,
            prev_prev_RowCode VARCHAR(50),
            prev_AccountID int
          ) 
    ;
  
    DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_;
    CREATE TEMPORARY TABLE tmp_VendorRate_ (
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       RowCode VARCHAR(50) 
     ) 
     ;
       
     DROP TEMPORARY TABLE IF EXISTS tmp_final_VendorRate_;
    CREATE TEMPORARY TABLE tmp_final_VendorRate_ (
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       RowCode VARCHAR(50),
       FinalRankNumber int
     ) 
    ;
  	
  	-- Loop codes 
  
  DROP TEMPORARY TABLE IF EXISTS tmp_search_code_;
    CREATE TEMPORARY TABLE tmp_search_code_ (
       Code  varchar(50),
       INDEX Index1 (Code)
  );
  
  	-- searched codes.
  
  DROP TEMPORARY TABLE IF EXISTS tmp_code_;
    CREATE TEMPORARY TABLE tmp_code_ (
       RowCode  varchar(50),
       Code  varchar(50),
       RowNo int,
       INDEX Index1 (Code)
     );
  
  
  DROP TEMPORARY TABLE IF EXISTS tmp_all_code_;
    CREATE TEMPORARY TABLE tmp_all_code_ ( 
       RowCode  varchar(50),
       Code  varchar(50),
       RowNo int,
       INDEX Index2 (Code)
     )
     ;
  
          
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorCurrentRates_;
  CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorCurrentRates_(
  			AccountId int,
           AccountName varchar(200),
  			Code varchar(50),
  			Description varchar(200),
  			Rate DECIMAL(18,6) ,
           ConnectionFee DECIMAL(18,6) , 
  			EffectiveDate date,
  			TrunkID int,
  			CountryID int,
  			RateID int,
  			Preference int,
  			INDEX IX_Code (Code)
  	) 
    ;
  
       DROP TEMPORARY TABLE IF EXISTS tmp_VendorCurrentRates1_;
  CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorCurrentRates1_(
  			AccountId int,
           AccountName varchar(200),
  			Code varchar(50),
  			Description varchar(200),
  			Rate DECIMAL(18,6) ,
                 ConnectionFee DECIMAL(18,6) , 
  			EffectiveDate date,
  			TrunkID int,
  			CountryID int,
  			RateID int,
  			Preference int,
  			INDEX IX_Code (Code),
  			INDEX tmp_VendorCurrentRates_AccountId (`AccountId`,`TrunkID`,`RateId`,`EffectiveDate`)
  	) 
    ;
  
   DROP TEMPORARY TABLE IF EXISTS tmp_VendorRateByRank_;
    CREATE TEMPORARY TABLE tmp_VendorRateByRank_ (      
       AccountId INT ,
       AccountName VARCHAR(100) ,
       Code VARCHAR(50) ,
       Rate DECIMAL(18,6) ,
       ConnectionFee DECIMAL(18,6) ,
       EffectiveDate DATETIME ,
       Description VARCHAR(255),
       Preference INT,
       rankname INT,
       INDEX IX_Code (Code,rankname)
     )
     ;
  
  	SELECT CurrencyId INTO v_CompanyCurrencyID_ FROM  tblCompany WHERE CompanyID = p_companyid;
  	
      -- Search code based on p_code 
            
            insert into tmp_search_code_
            SELECT  DISTINCT LEFT(f.Code, x.RowNo) as loopCode FROM (
            SELECT @RowNo  := @RowNo + 1 as RowNo
            FROM mysql.help_category 
            ,(SELECT @RowNo := 0 ) x
            limit 15
            ) x
            INNER JOIN tblRate AS f
            ON f.CompanyID = p_companyid  AND f.CodeDeckId = p_codedeckID
            AND ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR f.Code LIKE REPLACE(p_code,'*', '%') )
            AND ( p_Description = ''  OR f.Description LIKE REPLACE(p_Description,'*', '%') )
            AND x.RowNo   <= LENGTH(f.Code) 
            order by loopCode   desc;
  		  
      -- distinct vendor rates	  
       
        INSERT INTO tmp_VendorCurrentRates1_ 
        Select DISTINCT AccountId,AccountName,Code,Description, Rate,ConnectionFee,EffectiveDate,TrunkID,CountryID,RateID,Preference 
        FROM (
  				SELECT distinct tblVendorRate.AccountId,tblAccount.AccountName, tblRate.Code, tblRate.Description, 
  				 CASE WHEN  tblAccount.CurrencyId = p_CurrencyID
  								THEN
  									   tblVendorRate.Rate
  					 WHEN  v_CompanyCurrencyID_ = p_CurrencyID  
  							 THEN
  								  (
  								   ( tblVendorRate.rate  / (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = tblAccount.CurrencyId and  CompanyID = p_companyid ) )
  								 )
  							  ELSE 
  							  (
  							    -- Convert to base currrncy and x by RateGenerator Exhange
  										
  								  (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = p_CurrencyID and  CompanyID = p_companyid )
  									* (tblVendorRate.rate  / (Select Value from tblCurrencyConversion where tblCurrencyConversion.CurrencyId = tblAccount.CurrencyId and  CompanyID = p_companyid ))
  							  )
  							 END 
  								as  Rate,
  								 ConnectionFee,
                      		DATE_FORMAT (tblVendorRate.EffectiveDate, '%Y-%m-%d') AS EffectiveDate, tblVendorRate.TrunkID, tblRate.CountryID, tblRate.RateID,IFNULL(vp.Preference, 5) AS Preference 
                      		FROM      tblVendorRate
  					     Inner join tblVendorTrunk vt on vt.CompanyID = p_companyid AND vt.AccountID = tblVendorRate.AccountID and vt.Status =  1 and vt.TrunkID =  p_trunkID
  							
  						INNER JOIN tblAccount   ON  tblAccount.CompanyID = p_companyid AND tblVendorRate.AccountId = tblAccount.AccountID and tblAccount.IsVendor = 1
  						INNER JOIN tblRate ON tblRate.CompanyID = p_companyid     AND    tblVendorRate.RateId = tblRate.RateID  
  						INNER JOIN tmp_search_code_  SplitCode   on tblRate.Code = SplitCode.Code 
  						LEFT JOIN tblVendorPreference vp
  										 ON vp.AccountId = tblVendorRate.AccountId
  										 AND vp.TrunkID = tblVendorRate.TrunkID
  										 AND vp.RateId = tblVendorRate.RateId
  						LEFT OUTER JOIN tblVendorBlocking AS blockCode   ON tblVendorRate.RateId = blockCode.RateId
  																	  AND tblVendorRate.AccountId = blockCode.AccountId
  																	  AND tblVendorRate.TrunkID = blockCode.TrunkID
  						LEFT OUTER JOIN tblVendorBlocking AS blockCountry    ON tblRate.CountryID = blockCountry.CountryId
  																	  AND tblVendorRate.AccountId = blockCountry.AccountId
  																	  AND tblVendorRate.TrunkID = blockCountry.TrunkID
  			  WHERE      
  						( EffectiveDate <= NOW() )
  						AND (p_AccountIds='' OR FIND_IN_SET(tblAccount.AccountID,p_AccountIds) != 0 )
  						AND tblAccount.IsVendor = 1
  						AND tblAccount.Status = 1
  						AND tblAccount.CurrencyId is not NULL
  						AND tblVendorRate.TrunkID = p_trunkID
  						AND blockCode.RateId IS NULL
  					   AND blockCountry.CountryId IS NULL
  			
  		) tbl
  		order by Code asc;
  		
  		
      -- filter by Effective Dates
           
       
       INSERT INTO tmp_VendorCurrentRates_ 
  	  Select AccountId,AccountName,Code,Description, Rate,ConnectionFee,EffectiveDate,TrunkID,CountryID,RateID,Preference 
        FROM (
  			  SELECT * ,
  				@row_num := IF(@prev_AccountId = AccountID AND @prev_TrunkID = TrunkID AND @prev_RateId = RateID AND @prev_EffectiveDate >= EffectiveDate, @row_num + 1, 1) AS RowID,
  				@prev_AccountId := AccountID,
  				@prev_TrunkID := TrunkID,
  				@prev_RateId := RateID,
  				@prev_EffectiveDate := EffectiveDate
  			  FROM tmp_VendorCurrentRates1_
  			  ,(SELECT @row_num := 1,  @prev_AccountId := '',@prev_TrunkID := '', @prev_RateId := '', @prev_EffectiveDate := '') x
             ORDER BY AccountId, TrunkID, RateId, EffectiveDate DESC	
  		) tbl
  		 WHERE RowID = 1 
  		order by Code asc;        
  
          -- Collect Codes pressent in vendor Rates from above query.
          /*
                     9372     9372    1
                     9372     937     2
                     9372     93      3
                     9372     9       4  
                                                                 
          */
  	
            
          insert into tmp_all_code_ (RowCode,Code,RowNo)
                 select RowCode , loopCode,RowNo
                 from (
                      select   RowCode , loopCode,
                      @RowNo := ( CASE WHEN ( @prev_Code = tbl1.RowCode  ) THEN @RowNo + 1
                                     ELSE 1
                                     END
                                  
                                )      as RowNo,
                      @prev_Code := tbl1.RowCode
  
                      from (
                                SELECT distinct f.Code as RowCode, LEFT(f.Code, x.RowNo) as loopCode FROM (
                                  SELECT @RowNo  := @RowNo + 1 as RowNo
                                  FROM mysql.help_category 
                                  ,(SELECT @RowNo := 0 ) x
                                  limit 15
                                ) x
                                INNER JOIN tmp_search_code_ AS f
                                ON  x.RowNo   <= LENGTH(f.Code) 
  											 AND ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR Code LIKE REPLACE(p_code,'*', '%') )
                                order by RowCode desc,  LENGTH(loopCode) DESC 
                      ) tbl1
                      , ( Select @RowNo := 0 ) x
                  ) tbl order by RowCode desc,  LENGTH(loopCode) DESC ;
            
      
          
          IF (p_isExport = 0)
          THEN
   			        
  					insert into tmp_code_
                           select * from tmp_all_code_
                           order by RowCode	LIMIT p_RowspPage OFFSET v_OffSet_ ;
  		
  		ELSE
  
       					insert into tmp_code_
                                select * from tmp_all_code_
                                order by RowCode	  ;
  			
  		END IF;
  
  
  IF p_Preference = 1 THEN
  
      -- Sort by Preference 
  	
  	INSERT IGNORE INTO tmp_VendorRateByRank_
  	  SELECT
  		AccountID,
  		AccountName,
  		Code,
  		Rate,
  		ConnectionFee,
  		EffectiveDate,
  		Description,
  		Preference,
  		preference_rank
  	  FROM (SELECT
  		  AccountID,
  		  AccountName,
  		  Code,
  		  Rate,
  		  ConnectionFee,
  		  EffectiveDate,
  		  Description,
  		  Preference,
  		  @preference_rank := CASE WHEN (@prev_Code     = Code AND @prev_Preference > Preference  ) THEN @preference_rank + 1 
  								   WHEN (@prev_Code     = Code AND @prev_Preference = Preference AND @prev_Rate < Rate) THEN @preference_rank + 1 
  								   WHEN (@prev_Code    = Code AND @prev_Preference = Preference AND @prev_Rate = Rate) THEN @preference_rank 
  								   ELSE 1 
  								   END AS preference_rank,
  		  @prev_Code := Code,
  		  @prev_Preference := IFNULL(Preference, 5),
  		  @prev_Rate := Rate
  		FROM tmp_VendorCurrentRates_ AS preference,
  			 (SELECT @preference_rank := 0 , @prev_Code := ''  , @prev_Preference := 5,  @prev_Rate := 0) x
  		ORDER BY preference.Code ASC, preference.Preference DESC, preference.Rate ASC,preference.AccountId ASC
  		   
  		 ) tbl
  	  WHERE preference_rank <= p_Position
  	  ORDER BY Code, preference_rank;
  
  	ELSE
  
      -- Sort by Rate 
  	
  	INSERT IGNORE INTO tmp_VendorRateByRank_
  	  SELECT
  		AccountID,
  		AccountName,
  		Code,
  		Rate,
  		ConnectionFee,
  		EffectiveDate,
  		Description,
  		Preference,
  		RateRank
  	  FROM (SELECT
  		  AccountID,
  		  AccountName,
  		  Code,
  		  Rate,
  		  ConnectionFee,
  		  EffectiveDate,
  		  Description,
  		  Preference,
  		  @rank := CASE WHEN (@prev_Code    = Code AND @prev_Rate < Rate) THEN @rank + 1 
  						WHEN (@prev_Code    = Code AND @prev_Rate = Rate) THEN @rank 
  						ELSE 1 
  						END AS RateRank,
  		  @prev_Code := Code,
  		  @prev_Rate := Rate
  		FROM tmp_VendorCurrentRates_ AS rank,
  			 (SELECT @rank := 0 , @prev_Code := '' , @prev_Rate := 0) f
  		ORDER BY rank.Code ASC, rank.Rate,rank.AccountId ASC) tbl
  	  WHERE RateRank <= p_Position
  	  ORDER BY Code, RateRank;
  
  	END IF;
  
  	-- --------- Split Logic ----------
  	/* DESC             MaxMatchRank 1  MaxMatchRank 2
  	923 Pakistan :       *923 V1          92 V1
  	923 Pakistan :       *92 V2            -
  
  	now take only where  MaxMatchRank =  1                                                        
  	*/             
  
   
  DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_stage_1;
   CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorRate_stage_1 as (select * from tmp_VendorRate_stage_);
  
   insert ignore into tmp_VendorRate_stage_1 (
     	     RowCode,
       	AccountId ,
       	AccountName ,
       	Code ,
       	Rate ,
       	ConnectionFee,                                                           
       	EffectiveDate , 
       	Description ,
       	Preference
  )
            SELECT  
            distinct                                
       	RowCode,
       	v.AccountId ,
       	v.AccountName ,
       	v.Code ,
       	v.Rate ,
       	v.ConnectionFee,                                                           
       	v.EffectiveDate , 
       	v.Description ,
       	v.Preference
            FROM tmp_VendorRateByRank_ v  
            left join  tmp_all_code_
  			SplitCode   on v.Code = SplitCode.Code 
           where  SplitCode.Code is not null and rankname <= p_Position 
           order by AccountID,SplitCode.RowCode desc ,LENGTH(SplitCode.RowCode), v.Code desc, LENGTH(v.Code)  desc;
         
  
       insert ignore into tmp_VendorRate_stage_
            SELECT  
            distinct                                
       	RowCode,
       	v.AccountId ,
       	v.AccountName ,
       	v.Code ,
       	v.Rate ,
       	v.ConnectionFee,                                                           
       	v.EffectiveDate , 
       	v.Description ,
       	v.Preference, 
       	@rank := ( CASE WHEN( @prev_AccountID = v.AccountId  and @prev_RowCode     = RowCode   ) 
                                     THEN  @rank + 1 
                                     ELSE 1 
                                END 
            ) AS MaxMatchRank,
  	     @prev_RowCode := RowCode	 ,															
            @prev_AccountID := v.AccountId   
            FROM tmp_VendorRate_stage_1 v  
            , (SELECT  @prev_RowCode := '',  @rank := 0 , @prev_Code := '' , @prev_AccountID := Null) f
            order by AccountID,RowCode desc ; 
           
      
      
  
                insert ignore into tmp_VendorRate_
                select
                      distinct 
  				 AccountId ,
  				 AccountName ,
  				 Code ,
  				 Rate ,
                      ConnectionFee,                                                           
  				 EffectiveDate ,
  				 Description ,
  				 Preference,
  				 RowCode
  		   from tmp_VendorRate_stage_ 
             where MaxMatchRank = 1 
            
  			  			  order by RowCode desc; 
  
  
       
                                     
  		IF( p_Preference = 0 )
       	THEN
       			
  			insert into tmp_final_VendorRate_
  			SELECT
  			     AccountId ,
  			     AccountName ,
  			     Code ,
  			     Rate ,
                      ConnectionFee,
  			     EffectiveDate ,
  			     Description ,
  			     Preference, 
  			     RowCode,
  	  		     FinalRankNumber
  				from 
  				( 				
  					SELECT
  					AccountId ,
  				     AccountName ,
  				     Code ,
  				     Rate ,
                           ConnectionFee,
  				     EffectiveDate ,
  				     Description ,
  				     Preference,
  				     RowCode,
     			         @rank := CASE WHEN ( @prev_RowCode     = RowCode AND @prev_Rate <  Rate ) THEN @rank+1
                                     WHEN ( @prev_RowCode    = RowCode AND @prev_Rate = Rate ) THEN @rank 
  						   ELSE 
  						   1
  						   END 
  				  		  AS FinalRankNumber,
  				  	@prev_RowCode  := RowCode,
  					@prev_Rate  := Rate
  					from tmp_VendorRate_ 
  					,( SELECT @rank := 0 , @prev_RowCode := '' , @prev_Rate := 0 ) x 
  				 	order by RowCode,Rate,AccountId ASC
  				 
  				) tbl1
  				where 
  				FinalRankNumber <= p_Position;
  				 
  		 ELSE 
  		 
  		 	 insert into tmp_final_VendorRate_
  			SELECT
  				AccountId ,
  			     AccountName ,
  			     Code ,
  			     Rate ,
                      ConnectionFee,
  			     EffectiveDate ,
  			     Description ,
  			     Preference, 
  			     RowCode,
  	  		     FinalRankNumber
  				from 
  				( 				
  					SELECT
       					AccountId ,
       				     AccountName ,
       				     Code ,
       				     Rate ,
                               ConnectionFee,
       				     EffectiveDate ,
       				     Description ,
       				     Preference,
       				     RowCode,
                                @preference_rank := CASE WHEN (@prev_Code     = RowCode AND @prev_Preference > Preference  )   THEN @preference_rank + 1 
                                                         WHEN (@prev_Code     = RowCode AND @prev_Preference = Preference AND @prev_Rate < Rate) THEN @preference_rank + 1 
                                                         WHEN (@prev_Code    = RowCode AND @prev_Preference = Preference AND @prev_Rate = Rate) THEN @preference_rank 
                                                         ELSE 1 END AS FinalRankNumber,
                                @prev_Code := RowCode,
                                @prev_Preference := Preference,
                                @prev_Rate := Rate
  					from tmp_VendorRate_ 
                            ,(SELECT @preference_rank := 0 , @prev_Code := ''  , @prev_Preference := 5,  @prev_Rate := 0) x
       			 	 order by RowCode ASC ,Preference DESC ,Rate ASC ,AccountId ASC
                           
  				) tbl1
  				where 
  				FinalRankNumber <= p_Position;
  		 
  		 END IF;
  
     IF (p_Position = 5)
  THEN
  	IF (p_isExport = 0)
      THEN     
      	SELECT
            CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`
          FROM tmp_final_VendorRate_  t
            GROUP BY  RowCode   
            ORDER BY RowCode ASC
       	LIMIT p_RowspPage OFFSET v_OffSet_ ;
  
          SELECT count(distinct RowCode) as totalcount from tmp_final_VendorRate_ 
  		 	WHERE  ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR RowCode LIKE REPLACE(p_code,'*', '%') ) ;
  	END IF; 
     
     IF p_isExport = 1
      THEN
  		SELECT
            CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`
          FROM tmp_final_VendorRate_  t
            GROUP BY  RowCode   
            ORDER BY RowCode ASC;
  	END IF;				
  END IF;
  
  IF (p_Position = 10)
  THEN
  	IF (p_isExport = 0)
      THEN     
      	SELECT
            CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 6,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 6`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 7,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 7`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 8,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 8`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 9,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 9`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 10, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 10`
          FROM tmp_final_VendorRate_  t
            GROUP BY  RowCode   
            ORDER BY RowCode ASC
       	LIMIT p_RowspPage OFFSET v_OffSet_ ;
  
          SELECT count(distinct RowCode) as totalcount from tmp_final_VendorRate_ 
  		 	WHERE  ( CHAR_LENGTH(RTRIM(p_code)) = 0  OR RowCode LIKE REPLACE(p_code,'*', '%') ) ;
  	END IF; 
     
     IF p_isExport = 1
      THEN
  		SELECT
            CONCAT(ANY_VALUE(t.RowCode) , ' : ' , ANY_VALUE(t.Description)) as Destination,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 1,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 1`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 2,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 2`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 3,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 3`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 4,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 4`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 5,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 5`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 6,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 6`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 7,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 7`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 8,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 8`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 9,  CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 9`,
            GROUP_CONCAT(if(ANY_VALUE(FinalRankNumber) = 10, CONCAT(ANY_VALUE(t.Code), '<br>', ANY_VALUE(t.Rate), '<br>', ANY_VALUE(t.AccountName), '<br>', DATE_FORMAT (ANY_VALUE(t.EffectiveDate), '%d/%m/%Y'),'<br>'), NULL))AS `POSITION 10`
          FROM tmp_final_VendorRate_  t
            GROUP BY  RowCode   
            ORDER BY RowCode ASC;
  	END IF;
  END IF;
  
  SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  	
  DROP PROCEDURE IF EXISTS `prc_GetAccounts`;
  DELIMITER |	
  CREATE PROCEDURE `prc_GetAccounts`(
  	IN `p_CompanyID` int,
  	IN `p_userID` int ,
  	IN `p_IsVendor` int ,
  	IN `p_isCustomer` int ,
  	IN `p_activeStatus` int,
  	IN `p_VerificationStatus` int,
  	IN `p_AccountNo` VARCHAR(100),
  	IN `p_ContactName` VARCHAR(50),
  	IN `p_AccountName` VARCHAR(50),
  	IN `p_tags` VARCHAR(50),
  	IN `p_IPCLI` VARCHAR(50),
  	IN `p_low_balance` INT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_lSortCol` VARCHAR(50),
  	IN `p_SortOrder` VARCHAR(5),
  	IN `p_isExport` INT
  )
  BEGIN
  	DECLARE v_OffSet_ int;
  	DECLARE v_Round_ int;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  	SELECT fnGetRoundingPoint(p_CompanyID) INTO v_Round_;
  
  	IF p_isExport = 0
  	THEN
  
  		SELECT
  			tblAccount.AccountID,
  			tblAccount.Number,
  			tblAccount.AccountName,
  			CONCAT(tblAccount.FirstName,' ',tblAccount.LastName) as Ownername,
  			tblAccount.Phone,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.SOAOffset,0),v_Round_)) as OutStandingAmount,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.UnbilledAmount,0),v_Round_) - ROUND(COALESCE(abc.VendorUnbilledAmount,0),v_Round_)) as UnbilledAmount,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.PermanentCredit,0),v_Round_)) as PermanentCredit,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.BalanceAmount,0),v_Round_)) as AccountExposure,
  			tblAccount.Email,
  			tblAccount.IsCustomer,
  			tblAccount.IsVendor,
  			tblAccount.VerificationStatus,
  			tblAccount.Address1,
  			tblAccount.Address2,
  			tblAccount.Address3,
  			tblAccount.City,
  			tblAccount.Country,
  			tblAccount.PostCode,
  			tblAccount.Picture,
  			IF ( (CASE WHEN abc.BalanceThreshold LIKE '%p' THEN REPLACE(abc.BalanceThreshold, 'p', '')/ 100 * abc.PermanentCredit ELSE abc.BalanceThreshold END) < abc.BalanceAmount AND abc.BalanceThreshold <> 0 ,1,0) as BalanceWarning,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.UnbilledAmount,0),v_Round_)) as CUA,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.VendorUnbilledAmount,0),v_Round_)) as VUA,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.BalanceAmount,0),v_Round_)) as AE,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,IF(ROUND(COALESCE(abc.PermanentCredit,0),v_Round_) - ROUND(COALESCE(abc.BalanceAmount,0),v_Round_)<0,0,ROUND(COALESCE(abc.PermanentCredit,0),v_Round_) - ROUND(COALESCE(abc.BalanceAmount,0),v_Round_))) as ACL,
  			abc.BalanceThreshold,
  			tblAccount.Blocked
  		FROM tblAccount
  		LEFT JOIN tblAccountBalance abc
  			ON abc.AccountID = tblAccount.AccountID
  		LEFT JOIN tblUser
  			ON tblAccount.Owner = tblUser.UserID
  		LEFT JOIN tblContact
  			ON tblContact.Owner=tblAccount.AccountID
  		LEFT JOIN tblAccountAuthenticate
  			ON tblAccountAuthenticate.AccountID = tblAccount.AccountID
  		LEFT JOIN tblCLIRateTable
  			ON tblCLIRateTable.AccountID = tblAccount.AccountID
  		WHERE   tblAccount.CompanyID = p_CompanyID
  			AND tblAccount.AccountType = 1
  			AND tblAccount.Status = p_activeStatus
  			AND tblAccount.VerificationStatus = p_VerificationStatus
  			AND (p_userID = 0 OR tblAccount.Owner = p_userID)
  			AND ((p_IsVendor = 0 OR tblAccount.IsVendor = 1))
  			AND ((p_isCustomer = 0 OR tblAccount.IsCustomer = 1))
  			AND ((p_AccountNo = '' OR tblAccount.Number LIKE p_AccountNo))
  			AND ((p_AccountName = '' OR tblAccount.AccountName LIKE Concat('%',p_AccountName,'%')))
  			AND ((p_IPCLI = '' OR tblCLIRateTable.CLI LIKE CONCAT('%',p_IPCLI,'%') OR CONCAT(IFNULL(tblAccountAuthenticate.CustomerAuthValue,''),',',IFNULL(tblAccountAuthenticate.VendorAuthValue,'')) LIKE CONCAT('%',p_IPCLI,'%')))
  			AND ((p_tags = '' OR tblAccount.tags LIKE Concat(p_tags,'%')))
  			AND ((p_ContactName = '' OR (CONCAT(IFNULL(tblContact.FirstName,'') ,' ', IFNULL(tblContact.LastName,''))) LIKE CONCAT('%',p_ContactName,'%')))
  			AND (p_low_balance = 0 OR ( p_low_balance = 1 AND abc.BalanceThreshold <> 0 AND (CASE WHEN abc.BalanceThreshold LIKE '%p' THEN REPLACE(abc.BalanceThreshold, 'p', '')/ 100 * abc.PermanentCredit ELSE abc.BalanceThreshold END) < abc.BalanceAmount) )
  		GROUP BY tblAccount.AccountID
  		ORDER BY
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AccountNameASC') THEN tblAccount.AccountName
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AccountNameDESC') THEN tblAccount.AccountName
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'NumberDESC') THEN tblAccount.Number
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'NumberASC') THEN tblAccount.Number
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'OwnernameDESC') THEN tblUser.FirstName
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'OwnernameASC') THEN tblUser.FirstName
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'PhoneDESC') THEN tblAccount.Phone
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'PhoneASC') THEN tblAccount.Phone
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'OutStandingAmountDESC') THEN abc.SOAOffset
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'OutStandingAmountASC') THEN abc.SOAOffset
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'PermanentCreditDESC') THEN abc.PermanentCredit
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'PermanentCreditASC') THEN abc.PermanentCredit
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'UnbilledAmountDESC') THEN (ROUND(COALESCE(abc.UnbilledAmount,0),v_Round_) - ROUND(COALESCE(abc.VendorUnbilledAmount,0),v_Round_))
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'UnbilledAmountASC') THEN (ROUND(COALESCE(abc.UnbilledAmount,0),v_Round_) - ROUND(COALESCE(abc.VendorUnbilledAmount,0),v_Round_))
  			END ASC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'EmailDESC') THEN tblAccount.Email
  			END DESC,
  			CASE
  				WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'EmailASC') THEN tblAccount.Email
  			END ASC
  		LIMIT p_RowspPage OFFSET v_OffSet_;
  
  		SELECT
  			COUNT(DISTINCT tblAccount.AccountID) AS totalcount
  		FROM tblAccount
  		LEFT JOIN tblAccountBalance abc
  			ON abc.AccountID = tblAccount.AccountID
  		LEFT JOIN tblUser
  			ON tblAccount.Owner = tblUser.UserID
  		LEFT JOIN tblContact
  			ON tblContact.Owner=tblAccount.AccountID
  		LEFT JOIN tblAccountAuthenticate
  			ON tblAccountAuthenticate.AccountID = tblAccount.AccountID
  		LEFT JOIN tblCLIRateTable
  			ON tblCLIRateTable.AccountID = tblAccount.AccountID
  		WHERE   tblAccount.CompanyID = p_CompanyID
  			AND tblAccount.AccountType = 1
  			AND tblAccount.Status = p_activeStatus
  			AND tblAccount.VerificationStatus = p_VerificationStatus
  			AND (p_userID = 0 OR tblAccount.Owner = p_userID)
  			AND ((p_IsVendor = 0 OR tblAccount.IsVendor = 1))
  			AND ((p_isCustomer = 0 OR tblAccount.IsCustomer = 1))
  			AND ((p_AccountNo = '' OR tblAccount.Number LIKE p_AccountNo))
  			AND ((p_AccountName = '' OR tblAccount.AccountName LIKE Concat('%',p_AccountName,'%')))
  			AND ((p_IPCLI = '' OR tblCLIRateTable.CLI LIKE CONCAT('%',p_IPCLI,'%') OR CONCAT(IFNULL(tblAccountAuthenticate.CustomerAuthValue,''),',',IFNULL(tblAccountAuthenticate.VendorAuthValue,'')) LIKE CONCAT('%',p_IPCLI,'%')))
  			AND ((p_tags = '' OR tblAccount.tags LIKE Concat(p_tags,'%')))
  			AND ((p_ContactName = '' OR (CONCAT(IFNULL(tblContact.FirstName,'') ,' ', IFNULL(tblContact.LastName,''))) LIKE CONCAT('%',p_ContactName,'%')))
  			AND (p_low_balance = 0 OR ( p_low_balance = 1 AND abc.BalanceThreshold <> 0 AND (CASE WHEN abc.BalanceThreshold LIKE '%p' THEN REPLACE(abc.BalanceThreshold, 'p', '')/ 100 * abc.PermanentCredit ELSE abc.BalanceThreshold END) < abc.BalanceAmount) );
  
  	END IF;
  	IF p_isExport = 1
  	THEN
  		SELECT
  			tblAccount.Number as NO,
  			tblAccount.AccountName,
  			CONCAT(tblAccount.FirstName,' ',tblAccount.LastName) as Name,
  			tblAccount.Phone,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.SOAOffset,0),v_Round_)) as 'OutStanding',
  			tblAccount.Email,
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.UnbilledAmount,0),v_Round_)  - ROUND(COALESCE(abc.VendorUnbilledAmount,0),v_Round_)) as 'Unbilled Amount',
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.PermanentCredit,0),v_Round_)) as 'Credit Limit',
  			CONCAT(tblUser.FirstName,' ',tblUser.LastName) as 'Account Owner',
  			CONCAT((SELECT Symbol FROM tblCurrency WHERE tblCurrency.CurrencyId = tblAccount.CurrencyId) ,ROUND(COALESCE(abc.BalanceAmount,0),v_Round_)) as AccountExposure
  		FROM tblAccount
  		LEFT JOIN tblAccountBalance abc
  			ON abc.AccountID = tblAccount.AccountID
  		LEFT JOIN tblUser
  			ON tblAccount.Owner = tblUser.UserID
  		LEFT JOIN tblContact
  			ON tblContact.Owner=tblAccount.AccountID
  		LEFT JOIN tblAccountAuthenticate
  			ON tblAccountAuthenticate.AccountID = tblAccount.AccountID
  		LEFT JOIN tblCLIRateTable
  			ON tblCLIRateTable.AccountID = tblAccount.AccountID
  		WHERE   tblAccount.CompanyID = p_CompanyID
  			AND tblAccount.AccountType = 1
  			AND tblAccount.Status = p_activeStatus
  			AND tblAccount.VerificationStatus = p_VerificationStatus
  			AND (p_userID = 0 OR tblAccount.Owner = p_userID)
  			AND ((p_IsVendor = 0 OR tblAccount.IsVendor = 1))
  			AND ((p_isCustomer = 0 OR tblAccount.IsCustomer = 1))
  			AND ((p_AccountNo = '' OR tblAccount.Number LIKE p_AccountNo))
  			AND ((p_AccountName = '' OR tblAccount.AccountName LIKE Concat('%',p_AccountName,'%')))
  			AND ((p_IPCLI = '' OR tblCLIRateTable.CLI LIKE CONCAT('%',p_IPCLI,'%') OR CONCAT(IFNULL(tblAccountAuthenticate.CustomerAuthValue,''),',',IFNULL(tblAccountAuthenticate.VendorAuthValue,'')) LIKE CONCAT('%',p_IPCLI,'%')))
  			AND ((p_tags = '' OR tblAccount.tags LIKE Concat(p_tags,'%')))
  			AND ((p_ContactName = '' OR (CONCAT(IFNULL(tblContact.FirstName,'') ,' ', IFNULL(tblContact.LastName,''))) LIKE CONCAT('%',p_ContactName,'%')))
  			AND (p_low_balance = 0 OR ( p_low_balance = 1 AND abc.BalanceThreshold <> 0 AND (CASE WHEN abc.BalanceThreshold LIKE '%p' THEN REPLACE(abc.BalanceThreshold, 'p', '')/ 100 * abc.PermanentCredit ELSE abc.BalanceThreshold END) < abc.BalanceAmount) )
  		GROUP BY tblAccount.AccountID;
  	END IF;
  	IF p_isExport = 2
  	THEN
  		SELECT
  			tblAccount.AccountID,
  			tblAccount.AccountName
  		FROM tblAccount
  		LEFT JOIN tblAccountBalance abc
  			ON abc.AccountID = tblAccount.AccountID
  		LEFT JOIN tblUser
  			ON tblAccount.Owner = tblUser.UserID
  		LEFT JOIN tblContact
  			ON tblContact.Owner=tblAccount.AccountID
  		LEFT JOIN tblAccountAuthenticate
  			ON tblAccountAuthenticate.AccountID = tblAccount.AccountID
  		LEFT JOIN tblCLIRateTable
  			ON tblCLIRateTable.AccountID = tblAccount.AccountID
  		WHERE   tblAccount.CompanyID = p_CompanyID
  			AND tblAccount.AccountType = 1
  			AND tblAccount.Status = p_activeStatus
  			AND tblAccount.VerificationStatus = p_VerificationStatus
  			AND (p_userID = 0 OR tblAccount.Owner = p_userID)
  			AND ((p_IsVendor = 0 OR tblAccount.IsVendor = 1))
  			AND ((p_isCustomer = 0 OR tblAccount.IsCustomer = 1))
  			AND ((p_AccountNo = '' OR tblAccount.Number LIKE p_AccountNo))
  			AND ((p_AccountName = '' OR tblAccount.AccountName LIKE Concat('%',p_AccountName,'%')))
  			AND ((p_IPCLI = '' OR tblCLIRateTable.CLI LIKE CONCAT('%',p_IPCLI,'%') OR CONCAT(IFNULL(tblAccountAuthenticate.CustomerAuthValue,''),',',IFNULL(tblAccountAuthenticate.VendorAuthValue,'')) LIKE CONCAT('%',p_IPCLI,'%')))
  			AND ((p_tags = '' OR tblAccount.tags LIKE Concat(p_tags,'%')))
  			AND ((p_ContactName = '' OR (CONCAT(IFNULL(tblContact.FirstName,'') ,' ', IFNULL(tblContact.LastName,''))) LIKE CONCAT('%',p_ContactName,'%')))
  			AND (p_low_balance = 0 OR ( p_low_balance = 1 AND abc.BalanceThreshold <> 0 AND (CASE WHEN abc.BalanceThreshold LIKE '%p' THEN REPLACE(abc.BalanceThreshold, 'p', '')/ 100 * abc.PermanentCredit ELSE abc.BalanceThreshold END) < abc.BalanceAmount) )
  		GROUP BY tblAccount.AccountID;
  	END IF;
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_getTrunkByMaxMatch`;
  DELIMITER |
  CREATE PROCEDURE `prc_getTrunkByMaxMatch`(
  	IN `p_CompanyID` INT,
  	IN `p_Trunk` VARCHAR(200)
  )
  BEGIN
  
  
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	select TrunkID , p_Trunk  as Trunk   from tblTrunk where ( p_Trunk like concat('%' , Trunk  ) ) order by length(Trunk) desc limit 1;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_GetSingleTicket`;
  DELIMITER |	
  CREATE PROCEDURE `prc_GetSingleTicket`(
  	IN `p_TicketID` INT
  )
  BEGIN
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  
  	select t.* , eal.EmailTo from tblTickets t
  	left join AccountEmailLog eal on t.TicketID = eal.TicketID and eal.EmailParent = 0
  	where t.TicketID =  p_TicketID limit 1;
  
  
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ ;
  
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_CustomerRatesFileImport`;
  DELIMITER |
  CREATE PROCEDURE `prc_CustomerRatesFileImport`(
  	IN `p_ProcessID` VARCHAR(200),
  	IN `p_tbltemp_name` VARCHAR(200)
  )
  BEGIN
  
  	DECLARE v_pointer_ INT;
  	DECLARE v_rowCount_ INT;
  	DECLARE v_TrunkID_ INT;
  	DECLARE v_AccountID_ INT;
  	DECLARE v_codedeckid_ INT;
  	DECLARE v_companyid_ INT;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_AccountTrunk_;
  	CREATE TEMPORARY TABLE tmp_AccountTrunk_  (
  		RowID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  		AccountID INT,
  		TrunkID INT
  	);
  	SET @stm = CONCAT('
  	INSERT INTO tmp_AccountTrunk_(AccountID,TrunkID)
  	SELECT DISTINCT AccountID,TrunkID FROM `' , p_tbltemp_name , '` ud WHERE ProcessID="' , p_ProcessID , '" AND AccountID IS NOT NULL AND TrunkID IS NOT NULL;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	SET v_pointer_ = 1;
  	SET v_rowCount_ = (SELECT COUNT(*)FROM tmp_AccountTrunk_);
  
  	WHILE v_pointer_ <= v_rowCount_
  	DO
  
  		SET v_TrunkID_ = (SELECT TrunkID FROM tmp_AccountTrunk_ t WHERE t.RowID = v_pointer_); 
  		SET v_AccountID_ = (SELECT AccountID FROM tmp_AccountTrunk_ t WHERE t.RowID = v_pointer_);
  		SET v_codedeckid_ = (SELECT CodeDeckId FROM tblCustomerTrunk WHERE tblCustomerTrunk.TrunkID = v_TrunkID_ AND tblCustomerTrunk.AccountID = v_AccountID_ /*AND tblCustomerTrunk.Status = 1*/);
  
  		IF v_codedeckid_ IS NOT NULL AND (SELECT COUNT(*) FROM tblCodeDeck WHERE CodeDeckId = v_codedeckid_)>0
  		THEN
  
  			SET v_companyid_ = (SELECT CompanyId FROM tblCodeDeck WHERE CodeDeckId = v_codedeckid_);			
  
  			-- code insert and update rate id in temp table
  			CALL prc_updateRateID(v_AccountID_,v_codedeckid_,p_tbltemp_name,p_ProcessID);
  
  			-- CALL prc_GetCustomerRate(v_companyid_,v_AccountID_,v_TrunkID_,null,null,null,'All',1,0,0,0,'','',-1);
  			
  			-- customerrate insert,update and delete
  			CALL prc_putCustomerCodeRate(v_AccountID_,v_TrunkID_,p_tbltemp_name,p_ProcessID);
  
  		END IF;
  
  		SET v_pointer_ = v_pointer_ + 1;
  
  	END WHILE;
  
  	SET @stm = CONCAT('
  	DELETE FROM `' , p_tbltemp_name , '` WHERE ProcessID="' , p_ProcessID , '" ;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm; 
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_putCustomerCodeRate`;
  DELIMITER |
  CREATE PROCEDURE `prc_putCustomerCodeRate`(
  	IN `p_AccountID` INT,
  	IN `p_TrunkID` INT,
  	IN `p_tbltemp_name` VARCHAR(200),
  	IN `p_ProcessID` VARCHAR(200)
  )
  BEGIN
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_tblTempLog_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_tblTempLog_(
  		`Message` VARCHAR(500) NOT NULL	
  	);
  	/* delete codes which are not exist in temp table*/
  	SET @stm = CONCAT('
  	DELETE cr FROM `' , p_tbltemp_name , '` cr 
  	LEFT JOIN (SELECT AccountID,TrunkID,RateID,MAX(EffectiveDate) as EffectiveDate FROM `' , p_tbltemp_name , '`  WHERE ProcessID = "' , p_ProcessID , '" AND EffectiveDate <= NOW()  GROUP BY  AccountID,TrunkID,RateID )tbl
  		ON tbl.AccountID = cr.AccountID  
  		AND tbl.TrunkID = cr.TrunkID
  		AND tbl.RateID = cr.RateID
  		AND tbl.EffectiveDate = cr.EffectiveDate
  	WHERE tbl.EffectiveDate IS NULL AND cr.EffectiveDate <= NOW() AND cr.ProcessID = "' , p_ProcessID , '";');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Old Effective Dates Records Deleted ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	/* delete codes which are not exist in temp table*/
  	SET @stm = CONCAT('
  	DELETE tblCustomerRate FROM tblCustomerRate
  	LEFT JOIN `' , p_tbltemp_name , '` temp 
  		ON tblCustomerRate.RateID = temp.RateID
  		AND tblCustomerRate.TrunkID = temp.TrunkID
  		AND CustomerID  = AccountID
  		AND tblCustomerRate.EffectiveDate = temp.EffectiveDate
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE TempRatesImportID IS NULL AND CustomerID = "' , p_AccountID , '" AND tblCustomerRate.TrunkID = "' , p_TrunkID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Deleted ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	-- 	 update codes which are exist in temp table
  	SET @stm = CONCAT('
  	UPDATE tblCustomerRate 
  	INNER JOIN `' , p_tbltemp_name , '` temp 
  		ON tblCustomerRate.RateID = temp.RateID
  		AND tblCustomerRate.TrunkID = temp.TrunkID
  		AND CustomerID  = AccountID
  		AND tblCustomerRate.EffectiveDate = temp.EffectiveDate
  	SET tblCustomerRate.PreviousRate = tblCustomerRate.Rate,tblCustomerRate.Rate = temp.Rate,tblCustomerRate.ConnectionFee = temp.ConnectionFee
  	WHERE CustomerID = "' , p_AccountID , '" AND tblCustomerRate.TrunkID = "' , p_TrunkID , '" AND ProcessID = "' , p_ProcessID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Updated ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	-- insert codes which are not exist in customer table
  	SET @stm = CONCAT('
  	INSERT INTO tblCustomerRate (RateID,CustomerID,TrunkID,LastModifiedDate,LastModifiedBy,Rate,EffectiveDate,Interval1,IntervalN,ConnectionFee)
  	SELECT temp.RateID,temp.AccountID,temp.TrunkID,now(),"SYSTEM IMPORTED",temp.Rate,temp.EffectiveDate,temp.Interval1,temp.IntervalN,temp.ConnectionFee FROM `' , p_tbltemp_name , '` temp
  	LEFT JOIN tblCustomerRate
  		ON tblCustomerRate.RateID = temp.RateID
  		AND tblCustomerRate.TrunkID = temp.TrunkID
  		AND CustomerID  = AccountID
  		AND tblCustomerRate.EffectiveDate = temp.EffectiveDate
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE CustomerRateID IS NULL AND AccountID = "' , p_AccountID , '" AND temp.TrunkID = "' , p_TrunkID , '" AND temp.RateID IS NOT NULL;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Inserted ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	SELECT * FROM tmp_tblTempLog_;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_putVendorCodeRate`;
  DELIMITER |
  CREATE PROCEDURE `prc_putVendorCodeRate`(
  	IN `p_AccountID` INT,
  	IN `p_TrunkID` INT,
  	IN `p_tbltemp_name` VARCHAR(200),
  	IN `p_ProcessID` VARCHAR(200)
  )
  BEGIN
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_tblTempLog_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_tblTempLog_(
  		`Message` VARCHAR(500) NOT NULL	
  	);
  	
  	/* delete old dates rate */
  	SET @stm = CONCAT('
  	DELETE cr FROM `' , p_tbltemp_name , '` cr 
  	LEFT JOIN (SELECT AccountID,TrunkID,RateID,MAX(EffectiveDate) as EffectiveDate FROM `' , p_tbltemp_name , '`  WHERE ProcessID = "' , p_ProcessID , '" AND EffectiveDate <= NOW() GROUP BY  AccountID,TrunkID,RateID )tbl
  		ON tbl.AccountID = cr.AccountID  
  		AND tbl.TrunkID = cr.TrunkID
  		AND tbl.RateID = cr.RateID
  		AND tbl.EffectiveDate = cr.EffectiveDate
  	WHERE tbl.EffectiveDate IS NULL AND cr.EffectiveDate <= NOW() AND cr.ProcessID = "' , p_ProcessID , '";');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Old Effective Dates Records Deleted ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	/* delete codes which are not exist in temp table*/
  	SET @stm = CONCAT('
  	DELETE tblVendorRate FROM tblVendorRate
  	LEFT JOIN `' , p_tbltemp_name , '` temp 
  		ON tblVendorRate.RateId = temp.RateID
  		AND tblVendorRate.TrunkID = temp.TrunkID
  		AND tblVendorRate. AccountId  = temp.AccountID
  		AND tblVendorRate.EffectiveDate = temp.EffectiveDate
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE TempRatesImportID IS NULL AND tblVendorRate.AccountId = "' , p_AccountID , '" AND tblVendorRate.TrunkID = "' , p_TrunkID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Deleted  ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	/* update codes which are exist in temp table*/
  	SET @stm = CONCAT('
  	UPDATE tblVendorRate 
  	INNER JOIN `' , p_tbltemp_name , '` temp 
  		ON tblVendorRate.RateId = temp.RateID
  		AND tblVendorRate.TrunkID = temp.TrunkID
  		AND tblVendorRate.AccountId  = temp.AccountID
  		AND tblVendorRate.EffectiveDate = temp.EffectiveDate
  	SET tblVendorRate.Interval1 = temp.Interval1,tblVendorRate.IntervalN = temp.IntervalN,tblVendorRate.Rate = temp.Rate,tblVendorRate.ConnectionFee = temp.ConnectionFee,updated_at=NOW(),updated_by="SYSTEM IMPORTED"
  	WHERE tblVendorRate.AccountId = "' , p_AccountID , '" AND tblVendorRate.TrunkID = "' , p_TrunkID , '" AND ProcessID = "' , p_ProcessID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  	
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Updated ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	/* insert codes which are not exist in customer table*/
  	SET @stm = CONCAT('
  	INSERT INTO tblVendorRate (RateId,AccountId,TrunkID,created_at,created_by,Rate,EffectiveDate,Interval1,IntervalN,ConnectionFee)
  	SELECT temp.RateID,temp.AccountID,temp.TrunkID,now(),"SYSTEM IMPORTED",temp.Rate,temp.EffectiveDate,temp.Interval1,temp.IntervalN,temp.ConnectionFee FROM `' , p_tbltemp_name , '` temp
  	LEFT JOIN tblVendorRate
  		ON tblVendorRate.RateId = temp.RateID
  		AND tblVendorRate.TrunkID = temp.TrunkID
  		AND tblVendorRate.AccountId  = temp.AccountID
  		AND tblVendorRate.EffectiveDate = temp.EffectiveDate
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE VendorRateID IS NULL AND temp.AccountID = "' , p_AccountID , '" AND temp.TrunkID = "' , p_TrunkID , '" AND temp.RateID IS NOT NULL;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_tblTempLog_ (Message)
  	SELECT CONCAT(AccountName,' Records Inserted ',FOUND_ROWS()) FROM tblAccount WHERE AccountID = p_AccountID;
  
  	SELECT * FROM tmp_tblTempLog_;
  	
  	CALL prc_putVendorPreference(p_AccountID,p_TrunkID,p_tbltemp_name,p_ProcessID);
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_putVendorPreference`;
  DELIMITER |
  CREATE PROCEDURE `prc_putVendorPreference`(
  	IN `p_AccountID` INT,
  	IN `p_TrunkID` INT,
  	IN `p_tbltemp_name` VARCHAR(200),
  	IN `p_ProcessID` VARCHAR(200)
  )
  BEGIN
  
  	/* delete codes which are not exist in temp table*/
  	SET @stm = CONCAT('
  	DELETE tblVendorPreference FROM tblVendorPreference
  	LEFT JOIN `' , p_tbltemp_name , '` temp 
  		ON tblVendorPreference.RateId = temp.RateID
  		AND tblVendorPreference.TrunkID = temp.TrunkID
  		AND tblVendorPreference. AccountId  = temp.AccountID
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE TempRatesImportID IS NULL AND tblVendorPreference.AccountId = "' , p_AccountID , '" AND tblVendorPreference.TrunkID = "' , p_TrunkID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	/* update codes which are exist in temp table*/
  	SET @stm = CONCAT('
  	UPDATE tblVendorPreference 
  	INNER JOIN `' , p_tbltemp_name , '` temp 
  		ON tblVendorPreference.RateId = temp.RateID
  		AND tblVendorPreference.TrunkID = temp.TrunkID
  		AND tblVendorPreference.AccountId  = temp.AccountID
  	SET tblVendorPreference.Preference = temp.Interval1,created_at=NOW(),CreatedBy="SYSTEM IMPORTED"
  	WHERE tblVendorPreference.AccountId = "' , p_AccountID , '" AND tblVendorPreference.TrunkID = "' , p_TrunkID , '" AND ProcessID = "' , p_ProcessID , ' AND tblVendorPreference.Preference <> 0";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  	
  	/* insert codes which are not exist in customer table*/
  	SET @stm = CONCAT('
  	INSERT INTO tblVendorPreference (AccountId,RateId,TrunkID,created_at,CreatedBy)
  	SELECT DISTINCT temp.AccountID,temp.RateID,temp.TrunkID,now(),"SYSTEM IMPORTED" FROM `' , p_tbltemp_name , '` temp
  	LEFT JOIN tblVendorPreference
  		ON tblVendorPreference.RateId = temp.RateID
  		AND tblVendorPreference.TrunkID = temp.TrunkID
  		AND tblVendorPreference.AccountId  = temp.AccountID
  		AND ProcessID = "' , p_ProcessID , '"
  	WHERE VendorRateID IS NULL AND temp.AccountID = "' , p_AccountID , '" AND temp.TrunkID = "' , p_TrunkID , '" AND tblVendorPreference.Preference <> 0 AND temp.RateID IS NOT NULL;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_updateRateID`;
  DELIMITER |
  CREATE PROCEDURE `prc_updateRateID`(
  	IN `p_AccountID` INT,
  	IN `p_CodeDeckID` INT,
  	IN `p_tbltemp_name` VARCHAR(200),
  	IN `p_ProcessID` VARCHAR(200)
  )
  BEGIN
  
  	SET @rowcount = 1;
  
  	WHILE @rowcount  > 0 DO
  
  		SET @stm = CONCAT('
  		INSERT IGNORE INTO tblRate (CountryID,Description,CompanyID,CodeDeckId,Code,Interval1,IntervalN,CreatedBy)
  		SELECT DISTINCT fnGetCountryIdByCodeAndCountry(temp.Code,temp.Description),temp.Description,temp.CompanyID,"' , p_CodeDeckID , '",temp.Code,temp.Interval1,temp.IntervalN,"SYSTEM IMPOERTED"
  		FROM `' , p_tbltemp_name , '` temp 
  		LEFT JOIN tblRate code ON code.CompanyID = temp.CompanyID AND code.Code = temp.Code AND code.CodeDeckId="' , p_CodeDeckID , '"
  		WHERE ProcessID="' , p_ProcessID , '" AND code.RateID IS NULL
  		LIMIT 1000;
  		');
  
  		PREPARE stm FROM @stm;
  		EXECUTE stm;
  		DEALLOCATE PREPARE stm;
  		SET @stm = CONCAT('
  
  		SELECT COUNT(DISTINCT temp.Code) INTO @rowcount
  		FROM `' , p_tbltemp_name , '` temp 
  		LEFT JOIN tblRate code ON code.CompanyID = temp.CompanyID AND code.Code = temp.Code AND code.CodeDeckId="' , p_CodeDeckID , '"
  		WHERE ProcessID="' , p_ProcessID , '" AND code.RateID IS NULL;
  		');
  
  		PREPARE stm FROM @stm;
  		EXECUTE stm;
  		DEALLOCATE PREPARE stm;
  
  	END WHILE;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_codes_;
  	CREATE TEMPORARY TABLE tmp_codes_ (
  		RateID INT,
  		Code VARCHAR(50),
  		INDEX tmp_codes_RateID (`RateID`),
  		INDEX tmp_codes_Code (`Code`)
  	);
  	INSERT INTO tmp_codes_
  	SELECT
  	DISTINCT
  		tblRate.RateID,
  		tblRate.Code
  	FROM tblRate
  	WHERE
  		 tblRate.CodeDeckId = p_CodeDeckID;
  
  	SET @stm = CONCAT('
  	UPDATE `' , p_tbltemp_name , '` temp 
  	INNER JOIN tmp_codes_ code ON code.Code = temp.Code
  		SET temp.RateID = code.RateID
  	WHERE ProcessID="' , p_ProcessID , '";
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_VendorRatesFileImport`;
  DELIMITER |
  CREATE PROCEDURE `prc_VendorRatesFileImport`(
  	IN `p_ProcessID` VARCHAR(200),
  	IN `p_tbltemp_name` VARCHAR(200)
  )
  BEGIN
  
  	DECLARE v_pointer_ INT;
  	DECLARE v_rowCount_ INT;
  	DECLARE v_TrunkID_ INT;
  	DECLARE v_AccountID_ INT;
  	DECLARE v_codedeckid_ INT;
  	DECLARE v_companyid_ INT;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_AccountTrunk_;
  	CREATE TEMPORARY TABLE tmp_AccountTrunk_  (
  		RowID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  		AccountID INT,
  		TrunkID INT
  	);
  	SET @stm = CONCAT('
  	INSERT INTO tmp_AccountTrunk_(AccountID,TrunkID)
  	SELECT DISTINCT AccountID,TrunkID FROM `' , p_tbltemp_name , '` ud WHERE ProcessID="' , p_processId , '" AND AccountID IS NOT NULL AND TrunkID IS NOT NULL;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	SET v_pointer_ = 1;
  	SET v_rowCount_ = (SELECT COUNT(*)FROM tmp_AccountTrunk_);
  
  	WHILE v_pointer_ <= v_rowCount_
  	DO
  
  		SET v_TrunkID_ = (SELECT TrunkID FROM tmp_AccountTrunk_ t WHERE t.RowID = v_pointer_); 
  		SET v_AccountID_ = (SELECT AccountID FROM tmp_AccountTrunk_ t WHERE t.RowID = v_pointer_);
  		SET v_codedeckid_ = (SELECT CodeDeckId FROM tblVendorTrunk WHERE tblVendorTrunk.TrunkID = v_TrunkID_ AND tblVendorTrunk.AccountID = v_AccountID_ /*AND tblVendorTrunk.Status = 1*/);
  
  		IF v_codedeckid_ IS NOT NULL AND (SELECT COUNT(*) FROM tblCodeDeck WHERE CodeDeckId = v_codedeckid_)>0
  		THEN
  
  			SET v_companyid_ = (SELECT CompanyId FROM tblCodeDeck WHERE CodeDeckId = v_codedeckid_);			
  
  			-- code insert and update rate id in temp table
  			CALL prc_updateRateID(v_AccountID_,v_codedeckid_,p_tbltemp_name,p_ProcessID);
  
  			-- CALL prc_GetCustomerRate(v_companyid_,v_AccountID_,v_TrunkID_,null,null,null,'All',1,0,0,0,'','',-1);
  			
  			-- vendorrate insert,update and delete
  			CALL prc_putVendorCodeRate(v_AccountID_,v_TrunkID_,p_tbltemp_name,p_ProcessID);
  
  		END IF;
  
  		SET v_pointer_ = v_pointer_ + 1;
  
  	END WHILE;
  
  	SET @stm = CONCAT('
  	DELETE FROM `' , p_tbltemp_name , '` WHERE ProcessID="' , p_processId , '" ;
  	');
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm; 
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  CREATE TABLE IF NOT EXISTS `tblDynamicFields` (
    `DynamicFieldsID` int(11) NOT NULL AUTO_INCREMENT,
    `CompanyID` int(11) NOT NULL DEFAULT '0',
    `Type` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'account, product etc',
    `FieldDomType` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `FieldName` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `FieldSlug` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `FieldDescription` varchar(200) COLLATE utf8_unicode_ci DEFAULT NULL,
    `FieldOrder` int(11) NOT NULL DEFAULT '0',
    `Status` tinyint(4) NOT NULL DEFAULT '1',
    `created_at` datetime DEFAULT NULL,
    `created_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    `updated_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`DynamicFieldsID`),
    KEY `IX_Type` (`Type`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  INSERT INTO `tblDynamicFields` (`DynamicFieldsID`, `CompanyID`, `Type`, `FieldDomType`, `FieldName`, `FieldSlug`, `FieldDescription`, `FieldOrder`, `Status`, `created_at`, `created_by`, `updated_at`, `updated_by`) VALUES
  	(1, 1, 'account', 'multiselect', 'Gateway', 'accountgateway', 'Account Gateway', 0, 0, '2017-07-04 13:05:54', 'System', NULL, NULL),
  	(2, 1, 'account', 'text', 'Vendor Name', 'vendorname', 'Vendor Name', 0, 0, '2017-07-04 13:05:54', 'System', NULL, NULL),
  	(3, 1, 'product', 'text', 'Barcode', 'barcode', 'Product Barcode', 0, 0, '2017-07-05 18:04:45', 'System', NULL, NULL);
  
  CREATE TABLE IF NOT EXISTS `tblDynamicFieldsDetail` (
    `DynamicFieldsDetailID` int(11) NOT NULL AUTO_INCREMENT,
    `CompanyID` int(11) NOT NULL DEFAULT '0',
    `DynamicFieldsID` int(11) NOT NULL DEFAULT '0',
    `FieldType` varchar(50) COLLATE utf8_unicode_ci NOT NULL DEFAULT '0' COMMENT 'gateway, item, account, users etc',
    `Options` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL COMMENT 'json = numeric field , limit etc',
    `FieldOrder` int(11) NOT NULL DEFAULT '0',
    `created_at` datetime DEFAULT NULL,
    `created_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    `updated_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`DynamicFieldsDetailID`),
    KEY `IX_DynamicFieldsID` (`DynamicFieldsID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  INSERT INTO `tblDynamicFieldsDetail` (`DynamicFieldsDetailID`, `CompanyID`, `DynamicFieldsID`, `FieldType`, `Options`, `FieldOrder`, `created_at`, `created_by`, `updated_at`, `updated_by`) VALUES
  	(1, 1, 1, 'gateway', NULL, 0, NULL, NULL, NULL, NULL),
  	(2, 1, 3, 'is_unique', '1', 0, NULL, NULL, NULL, NULL);
  
  CREATE TABLE IF NOT EXISTS `tblDynamicFieldsValue` (
    `DynamicFieldsValueID` int(11) NOT NULL AUTO_INCREMENT,
    `CompanyID` int(11) NOT NULL DEFAULT '0',
    `ParentID` int(11) NOT NULL DEFAULT '0',
    `DynamicFieldsID` int(11) NOT NULL DEFAULT '0',
    `FieldValue` text COLLATE utf8_unicode_ci,
    `FieldOrder` int(11) NOT NULL DEFAULT '0',
    `created_at` datetime DEFAULT NULL,
    `created_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    `updated_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`DynamicFieldsValueID`),
    UNIQUE KEY `IXUnique_ParentID_DynamicFieldsID` (`ParentID`,`DynamicFieldsID`),
    KEY `IX_ParentID_DynamicFieldsID` (`DynamicFieldsID`,`ParentID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  
  
  DROP PROCEDURE IF EXISTS `prc_WSProcessImportAccount`;
  DELIMITER |
  CREATE PROCEDURE `prc_WSProcessImportAccount`(
  	IN `p_processId` VARCHAR(200) ,
  	IN `p_companyId` INT,
  	IN `p_companygatewayid` INT,
  	IN `p_tempaccountid` TEXT,
  	IN `p_option` INT,
  	IN `p_importdate` DATETIME
  )
  BEGIN
      DECLARE v_AffectedRecords_ INT DEFAULT 0;
  	DECLARE totalduplicatecode INT(11);
  	DECLARE errormessage longtext;
  	DECLARE errorheader longtext;
  	DECLARE v_accounttype INT DEFAULT 0;
  
  	SET sql_mode = '';
      SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
      SET SESSION sql_mode='';
  
  	 DROP TEMPORARY TABLE IF EXISTS tmp_JobLog_;
      CREATE TEMPORARY TABLE tmp_JobLog_  (
          Message longtext
      );
  
      DROP TEMPORARY TABLE IF EXISTS tmp_accountimport;
  				CREATE TEMPORARY TABLE tmp_accountimport (
  				  `AccountType` tinyint(3) default 0,
  				  `CompanyId` INT,
  				  `Title` VARCHAR(100),
  				  `Owner` INT,
  				  `Number` VARCHAR(50),
  				  `AccountName` VARCHAR(100),
  				  `NamePrefix` VARCHAR(50),
  				  `FirstName` VARCHAR(50),
  				  `LastName` VARCHAR(50),
  				  `LeadSource` VARCHAR(50),
  				  `Email` VARCHAR(100),
  				  `Phone` VARCHAR(50),
  				  `Address1` VARCHAR(100),
  				  `Address2` VARCHAR(100),
  				  `Address3` VARCHAR(100),
  				  `City` VARCHAR(50),
  				  `PostCode` VARCHAR(50),
  				  `Country` VARCHAR(50),
  				  `Status` INT,
  				  `tags` VARCHAR(250),
  				  `Website` VARCHAR(100),
  				  `Mobile` VARCHAR(50),
  				  `Fax` VARCHAR(50),
  				  `Skype` VARCHAR(50),
  				  `Twitter` VARCHAR(50),
  				  `Employee` VARCHAR(50),
  				  `Description` longtext,
  				  `BillingEmail` VARCHAR(200),
  				  `CurrencyId` INT,
  				  `VatNumber` VARCHAR(50),
  				  `created_at` datetime,
  				  `created_by` VARCHAR(100),
  				  `VerificationStatus` tinyint(3) default 0,
  				   IsVendor INT,
  				   IsCustomer INT
  				) ENGINE=InnoDB;
  
     IF p_option = 0 
     THEN
  
     SELECT DISTINCT(AccountType) INTO v_accounttype from tblTempAccount WHERE ProcessID=p_processId;
  
  	DELETE n1 FROM tblTempAccount n1, tblTempAccount n2 WHERE n1.tblTempAccountID < n2.tblTempAccountID
  		AND  n1.CompanyId = n2.CompanyId
  		AND  n1.AccountName = n2.AccountName
  		AND  n1.ProcessID = n2.ProcessID
   		AND  n1.ProcessID = p_processId and n2.ProcessID = p_processId;
  
  		 select count(*) INTO totalduplicatecode FROM(
  				SELECT count(`Number`) as n,`Number` FROM tblTempAccount where ProcessID = p_processId  GROUP BY `Number` HAVING n>1) AS tbl;
  
  
  		 IF  totalduplicatecode > 0
  				THEN
  						SELECT GROUP_CONCAT(Number) into errormessage FROM(
  							select distinct Number, 1 as a FROM(
  								SELECT count(`Number`) as n,`Number` FROM tblTempAccount where ProcessID = p_processId  GROUP BY `Number` HAVING n>1) AS tbl) as tbl2 GROUP by a;
  
  						SELECT 'DUPLICATE AccountNumber : \n\r' INTO errorheader;
  
  						INSERT INTO tmp_JobLog_ (Message)
  							 SELECT CONCAT(errorheader ,errormessage);
  
  							 delete FROM tblTempAccount WHERE Number IN (
  								  SELECT Number from(
  								  	SELECT count(`Number`) as n,`Number` FROM tblTempAccount where ProcessID = p_processId  GROUP BY `Number` HAVING n>1
  									  ) as tbl
  								);
  
  			END IF;
  
  			INSERT  INTO tblAccount
  				  (	AccountType ,
  					CompanyId ,
  					Title,
  					Owner ,
  					`Number`,
  					AccountName,
  					NamePrefix,
  					FirstName,
  					LastName,
  					LeadStatus,
  					LeadSource,
  					Email,
  					Phone,
  					Address1,
  					Address2,
  					Address3,
  					City,
  					PostCode,
  					Country,
  					Status,
  					tags,
  					Website,
  					Mobile,
  					Fax,
  					Skype,
  					Twitter,
  					Employee,
  					Description,
  					BillingEmail,
  					CurrencyId,
  					VatNumber,
  					created_at,
  					created_by,
  					VerificationStatus,
  					IsVendor,
  				   IsCustomer
                     )
  
  				   SELECT  DISTINCT
  					ta.AccountType,
  					ta.CompanyId,
  					ta.Title,
  					ta.Owner,
  					ta.Number as Number,
  					ta.AccountName,
  					ta.NamePrefix,
  					ta.FirstName,
  					ta.LastName,
  					ta.LeadStatus,
  					ta.LeadSource,
  					ta.Email,
  					ta.Phone,
  					ta.Address1,
  					ta.Address2,
  					ta.Address3,
  					ta.City,
  					ta.PostCode,
  					ta.Country,
  					ta.Status,
  					ta.tags,
  					ta.Website,
  					ta.Mobile,
  					ta.Fax,
  					ta.Skype,
  					ta.Twitter,
  					ta.Employee,
  					ta.Description,
  					ta.BillingEmail,
  					ta.Currency as CurrencyId,
  					ta.VatNumber,
  					p_importdate AS created_at,
  					ta.created_by,
  					2 as VerificationStatus,
  					ta.IsVendor,
  				   ta.IsCustomer
  					from tblTempAccount ta
  						left join tblAccount a on ta.AccountName = a.AccountName
  						 	AND ta.CompanyId = a.CompanyId
  							AND ta.AccountType = a.AccountType
  						where ta.ProcessID = p_processId
  						   AND ta.AccountType = v_accounttype
  							AND a.AccountID is null
  							AND ta.CompanyID = p_companyId;
  
  
        SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS();
  
  	 INSERT INTO tmp_JobLog_ (Message)
  	 SELECT CONCAT(v_AffectedRecords_, ' Records Uploaded \n\r ' );
  
  
  	END IF;
  
  
  	IF p_option = 1 
     THEN
  
     		INSERT INTO tmp_accountimport
     		(	AccountType ,
  					CompanyId ,
  					Title,
  					Owner ,
  					`Number`,
  					AccountName,
  					NamePrefix,
  					FirstName,
  					LastName,
  					LeadSource,
  					Email,
  					Phone,
  					Address1,
  					Address2,
  					Address3,
  					City,
  					PostCode,
  					Country,
  					Status,
  					tags,
  					Website,
  					Mobile,
  					Fax,
  					Skype,
  					Twitter,
  					Employee,
  					Description,
  					BillingEmail,
  					CurrencyId,
  					VatNumber,
  					created_at,
  					created_by,
  					VerificationStatus,
  					IsCustomer,
  					IsVendor
                     )
  				select ta.AccountType ,
  					ta.CompanyId ,
  					ta.Title,
  					ta.Owner ,
  					ta.Number,
  					ta.AccountName,
  					ta.NamePrefix,
  					ta.FirstName,
  					ta.LastName,
  					ta.LeadSource,
  					ta.Email,
  					ta.Phone,
  					ta.Address1,
  					ta.Address2,
  					ta.Address3,
  					ta.City,
  					ta.PostCode,
  					ta.Country,
  					ta.Status,
  					ta.tags,
  					ta.Website,
  					ta.Mobile,
  					ta.Fax,
  					ta.Skype,
  					ta.Twitter,
  					ta.Employee,
  					ta.Description,
  					ta.BillingEmail,
  					ta.Currency as CurrencyId,
  					ta.VatNumber,
  					p_importdate AS created_at,
  					ta.created_by,
  					2 as VerificationStatus,
  					ta.IsCustomer,
  					ta.IsVendor
  				 FROM tblTempAccount ta
  				left join tblAccount a on ta.AccountName=a.AccountName
  					AND ta.CompanyId = a.CompanyId
  					AND ta.AccountType = a.AccountType
  				where ta.CompanyID = p_companyId
  				AND ta.ProcessID = p_processId
  				AND ta.AccountType = 1
  				AND a.AccountID is null
  				AND (p_tempaccountid = '' OR ( p_tempaccountid != '' AND FIND_IN_SET(ta.tblTempAccountID,p_tempaccountid) ))
  				AND (p_companygatewayid = 0 OR ( ta.CompanyGatewayID = p_companygatewayid))
  				group by ta.AccountName;
  
  
  			SELECT GROUP_CONCAT(Number) into errormessage FROM(
  			SELECT distinct ta.Number as Number,1 as an  from tblTempAccount ta
  				left join tblAccount a on ta.Number = a.Number
  					AND ta.CompanyId = a.CompanyId
  					AND ta.AccountType = a.AccountType
  				where ta.CompanyID = p_companyId
  				AND ta.ProcessID = p_processId
  				AND ta.AccountType = 1
  				AND a.AccountID is not null
  				AND (p_tempaccountid = '' OR ( p_tempaccountid != '' AND FIND_IN_SET(ta.tblTempAccountID,p_tempaccountid) ))
  				AND (p_companygatewayid = 0 OR ( ta.CompanyGatewayID = p_companygatewayid)))tbl GROUP by an;
  
  		  IF errormessage is not null
  		  THEN
  
  		  		SELECT 'AccountNumber Already EXISTS : \n\r' INTO errorheader;
  						INSERT INTO tmp_JobLog_ (Message)
  							 SELECT CONCAT(errorheader ,errormessage);
  
  				delete FROM tmp_accountimport WHERE Number IN (
  								  SELECT Number from(
  								  	SELECT distinct ta.Number as Number,1 as an  from tblTempAccount ta
  										left join tblAccount a on ta.Number = a.Number
  											AND ta.CompanyId = a.CompanyId
  											AND ta.AccountType = a.AccountType
  										where ta.CompanyID = p_companyId
  										AND ta.ProcessID = p_processId
  										AND ta.AccountType = 1
  										AND a.AccountID is not null
  										AND (p_tempaccountid = '' OR ( p_tempaccountid != '' AND FIND_IN_SET(ta.tblTempAccountID,p_tempaccountid) ))
  										AND (p_companygatewayid = 0 OR ( ta.CompanyGatewayID = p_companygatewayid))
  									  ) as tbl
  								);
  
  		  END IF;
  
  		INSERT  INTO tblAccount
  				  (	AccountType ,
  					CompanyId ,
  					Title,
  					Owner ,
  					`Number`,
  					AccountName,
  					NamePrefix,
  					FirstName,
  					LastName,
  					LeadSource,
  					Email,
  					Phone,
  					Address1,
  					Address2,
  					Address3,
  					City,
  					PostCode,
  					Country,
  					Status,
  					tags,
  					Website,
  					Mobile,
  					Fax,
  					Skype,
  					Twitter,
  					Employee,
  					Description,
  					BillingEmail,
  				    CurrencyId,
  					VatNumber,
  					created_at,
  					created_by,
  					VerificationStatus,
  					IsCustomer,
  					IsVendor
                     )
  			SELECT
  					AccountType ,
  					CompanyId ,
  					Title,
  					Owner ,
  					`Number`,
  					AccountName,
  					NamePrefix,
  					FirstName,
  					LastName,
  					LeadSource,
  					Email,
  					Phone,
  					Address1,
  					Address2,
  					Address3,
  					City,
  					PostCode,
  					Country,
  					Status,
  					tags,
  					Website,
  					Mobile,
  					Fax,
  					Skype,
  					Twitter,
  					Employee,
  					Description,
  					BillingEmail,
  				    CurrencyId,
  					VatNumber,
  					p_importdate AS created_at,
  					created_by,
  					VerificationStatus,
  					IsCustomer,
  					IsVendor
  				from tmp_accountimport;
  
  
  			SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS();
  
  			INSERT INTO tmp_JobLog_ (Message)
  			SELECT CONCAT(v_AffectedRecords_, ' Records Uploaded \n\r ' );
  
  		
  
     END IF;
  
  	DELETE  FROM tblTempAccount WHERE ProcessID = p_processId;
   	 SELECT * from tmp_JobLog_;
  
      SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  
  
  
  
  
  
  
  DROP PROCEDURE IF EXISTS `prc_CronJobAllPending`;
  DELIMITER |
  CREATE PROCEDURE `prc_CronJobAllPending`(
  	IN `p_CompanyID` INT
  )
  BEGIN
  	SET SESSION TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
  
      SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'CDR'
  		AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  	ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'CDR'
  		AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID,
  	   TBL1.JobLoggedUserID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'BI'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'BI'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'CD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Porta"%'
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'CD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Porta"%'
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		tblCronJobCommand.Command,
  		tblCronJob.CronJobID
  	FROM tblCronJob
  	INNER JOIN tblCronJobCommand
  		ON tblCronJobCommand.CronJobCommandID = tblCronJob.CronJobCommandID
  	WHERE tblCronJob.CompanyID = p_CompanyID
  	AND tblCronJob.Status = 1
  	AND tblCronJob.Active = 0;
  
  
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'BIS'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'BIS'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'VD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Porta"%'
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'VD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Porta"%'
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'RCC'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'RCC'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'INU'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'INU'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'CD'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  			AND j.Options like '%"Format":"Rate Sheet"%'
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'CD'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  			AND j.Options like '%"Format":"Rate Sheet"%'
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'BIR'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'BIR'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'BLE'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'BLE'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'BAE'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'BAE'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'VU'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'VU'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
      SELECT
      	  "CodeDeckUpload",
          TBL1.JobID,
          TBL1.Options,
          TBL1.AccountID
      FROM
      (
          SELECT
              j.Options,
              j.AccountID,
              j.JobID,
              j.JobLoggedUserID,
              @row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			   @prev_JobLoggedUserID  := j.JobLoggedUserID,
   			   @prev_created_at  := created_at
          FROM tblJob j
          INNER JOIN tblJobType jt
              ON j.JobTypeID = jt.JobTypeID
          INNER JOIN tblJobStatus js
              ON j.JobStatusID = js.JobStatusID
           ,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
          WHERE jt.Code = 'CDU'
              AND js.Code = 'p'
              AND j.CompanyID = p_CompanyID
           ORDER BY j.JobLoggedUserID,j.created_at ASC
      ) TBL1
      LEFT JOIN
      (
          SELECT
              JobLoggedUserID
          FROM tblJob j
          INNER JOIN tblJobType jt
              ON j.JobTypeID = jt.JobTypeID
          INNER JOIN tblJobStatus js
              ON j.JobStatusID = js.JobStatusID
          WHERE jt.Code = 'CDU'
              AND js.Code = 'I'
              AND j.CompanyID = p_CompanyID
      ) TBL2
          ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
      WHERE TBL1.rowno = 1
      AND TBL2.JobLoggedUserID IS NULL;
  
  
      SELECT
          TBL1.JobID,
          TBL1.Options,
          TBL1.AccountID
      FROM
      (
          SELECT
              j.Options,
              j.AccountID,
              j.JobID,
              j.JobLoggedUserID,
              @row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  				@prev_JobLoggedUserID  := j.JobLoggedUserID,
  				@prev_created_at  := created_at
          FROM tblJob j
          INNER JOIN tblJobType jt
              ON j.JobTypeID = jt.JobTypeID
          INNER JOIN tblJobStatus js
              ON j.JobStatusID = js.JobStatusID
           ,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
          WHERE jt.Code = 'IR'
              AND js.Code = 'p'
              AND j.CompanyID = p_CompanyID
           ORDER BY j.JobLoggedUserID,j.created_at ASC
      ) TBL1
      LEFT JOIN
      (
          SELECT
              JobLoggedUserID
          FROM tblJob j
          INNER JOIN tblJobType jt
              ON j.JobTypeID = jt.JobTypeID
          INNER JOIN tblJobStatus js
              ON j.JobStatusID = js.JobStatusID
          WHERE jt.Code = 'IR'
              AND js.Code = 'I'
              AND j.CompanyID = p_CompanyID
      ) TBL2
          ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
      WHERE TBL1.rowno = 1
      AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'CD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Sippy"%'
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'CD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Sippy"%'
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'VD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Sippy"%'
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'VD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND j.Options like '%"Format":"Sippy"%'
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'CD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND (j.Options like '%"Format":"Vos 3.2"%' OR j.Options like '%"Format":"Vos 2.0"%')
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'CD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND (j.Options like '%"Format":"Vos 3.2"%' OR j.Options like '%"Format":"Vos 2.0"%')
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'VD'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		AND (j.Options like '%"Format":"Vos 3.2"%' OR j.Options like '%"Format":"Vos 2.0"%')
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'VD'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  		AND (j.Options like '%"Format":"Vos 3.2"%' OR j.Options like '%"Format":"Vos 2.0"%')
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'GRT'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'GRT'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'RTU'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'RTU'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
      SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'VDR'
  		AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  	ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'VDR'
  		AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'MGA'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'MGA'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'DSU'
          AND js.Code = 'P'
  		AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'DSU'
          AND js.Code = 'I'
  		AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  	
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'QIP'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'QIP'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  	
  
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'ICU'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'ICU'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  
  	-- product upload
  	SELECT
  		TBL1.JobID,
  		TBL1.Options,
  		TBL1.AccountID
  	FROM
  	(
  		SELECT
  			j.Options,
  			j.AccountID,
  			j.JobID,
  			j.JobLoggedUserID,
  			@row_num := IF(@prev_JobLoggedUserID=j.JobLoggedUserID and @prev_created_at <= j.created_at ,@row_num+1,1) AS rowno,
  			@prev_JobLoggedUserID  := j.JobLoggedUserID,
  			@prev_created_at  := created_at
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		,(SELECT @row_num := 1) x,(SELECT @prev_JobLoggedUserID := '') y,(SELECT @prev_created_at := '') z
  		WHERE jt.Code = 'IU'
  			AND js.Code = 'p'
  			AND j.CompanyID = p_CompanyID
  		ORDER BY j.JobLoggedUserID,j.created_at ASC
  	) TBL1
  	LEFT JOIN
  	(
  		SELECT
  			JobLoggedUserID
  		FROM tblJob j
  		INNER JOIN tblJobType jt
  			ON j.JobTypeID = jt.JobTypeID
  		INNER JOIN tblJobStatus js
  			ON j.JobStatusID = js.JobStatusID
  		WHERE jt.Code = 'IU'
  			AND js.Code = 'I'
  			AND j.CompanyID = p_CompanyID
  	) TBL2
  		ON TBL1.JobLoggedUserID = TBL2.JobLoggedUserID
  	WHERE TBL1.rowno = 1
  	AND TBL2.JobLoggedUserID IS NULL;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  END|
  DELIMITER ;
  
  
  
  
  DROP PROCEDURE IF EXISTS `prc_getAccountAuditExportLog`;
  DELIMITER |
  CREATE PROCEDURE `prc_getAccountAuditExportLog`(
  	IN `p_CompanyID` INT,
  	IN `p_GatewayID` INT
  )
  BEGIN
  	DECLARE v_last_time_exist INT DEFAULT 0;
  	DECLARE v_last_time DATETIME;
  	DECLARE v_start_time DATETIME;
  	DECLARE v_end_time DATETIME;
  	DECLARE v_cur_date DATE DEFAULT CURDATE();
  	DECLARE v_cur_time DATETIME DEFAULT NOW();
  
     	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  
     	SELECT end_time INTO v_last_time FROM tblAccountAuditExportLog WHERE CompanyID=p_CompanyID AND CompanyGatewayID=p_GatewayID AND Status=1 ORDER BY AccountAuditExportLogID DESC LIMIT 1;
  
     	IF (v_last_time IS NOT NULL AND v_last_time != '')
     	THEN
  	   		-- SELECT end_time INTO v_last_time FROM tblAccountAuditExportLog WHERE CompanyID=p_CompanyID AND CompanyGatewayID=p_GatewayID AND Status=1 ORDER BY AccountAuditExportLogID DESC LIMIT 1;
  
  	   		SELECT
  	   			 MIN(tad.created_at), MAX(tad.created_at) INTO v_start_time, v_end_time
  	   		FROM
  	   			tblAuditHeader AS tah
  	   		LEFT JOIN
  	   			tblAuditDetails AS tad
  	   		ON
  	   			tah.AuditHeaderID=tad.AuditHeaderID
  	   		WHERE
  	   			tah.Type='account' AND tah.Date>=DATE(v_last_time) AND tah.Date<=DATE(v_last_time + INTERVAL 1 DAY) AND tad.created_at>v_last_time;
  
  	   		INSERT INTO tblAccountAuditExportLog (CompanyID,CompanyGatewayID,start_time,end_time,created_at) VALUES (p_CompanyID, p_GatewayID, v_start_time, v_end_time, v_cur_time);
  
  	   		SELECT
  	   			tah.ParentColumnID,tah.ParentColumnName,tad.ColumnName,tad.OldValue,tad.NewValue,v_start_time AS start_time,v_end_time AS end_time,v_cur_time AS created_at
  	   		FROM
  	   			tblAuditHeader AS tah
  	   		LEFT JOIN
  	   			tblAuditDetails AS tad
  	   		ON
  	   			tah.AuditHeaderID=tad.AuditHeaderID
  	   		WHERE
  	   			tah.Type='account' AND tah.Date>=DATE(v_last_time) AND tah.Date<=DATE(v_last_time + INTERVAL 1 DAY) AND tad.created_at>v_last_time;
  
  	ELSE
  		SELECT
  	   		MIN(tad.created_at), MAX(tad.created_at) INTO v_start_time, v_end_time
     		FROM
     			tblAuditHeader AS tah
     		LEFT JOIN
     			tblAuditDetails AS tad
     		ON
     			tah.AuditHeaderID=tad.AuditHeaderID
     		WHERE
     			tah.Type='account' AND tah.Date>=(v_cur_date - INTERVAL 1 DAY) AND tah.Date<=v_cur_date AND tad.created_at<=v_cur_time;
  
  		INSERT INTO tblAccountAuditExportLog (CompanyID,CompanyGatewayID,start_time,end_time,created_at) VALUES (p_CompanyID, p_GatewayID, v_start_time, v_end_time, v_cur_time);
  		
  		SELECT
  	   		tah.ParentColumnID,tah.ParentColumnName,tad.ColumnName,tad.OldValue,tad.NewValue,v_start_time AS start_time,v_end_time AS end_time,v_cur_time AS created_at
     		FROM
     			tblAuditHeader AS tah
     		LEFT JOIN
     			tblAuditDetails AS tad
     		ON
     			tah.AuditHeaderID=tad.AuditHeaderID
     		WHERE
     			tah.Type='account' AND tah.Date>=(v_cur_date - INTERVAL 1 DAY) AND tah.Date<=v_cur_date AND tad.created_at<=v_cur_time;
     	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_CustomerRateForExport`;
  DELIMITER |
  CREATE PROCEDURE `prc_CustomerRateForExport`(
  	IN `p_CompanyID` INT,
  	IN `p_CustomerID` INT ,
  	IN `p_TrunkID` INT,
  	IN `p_NameFormat` VARCHAR(50),
  	IN `p_Account` VARCHAR(200),
  	IN `p_Trunk` VARCHAR(200) ,
  	IN `p_TrunkPrefix` VARCHAR(50),
  	IN `p_Effective` VARCHAR(50)
  )
  BEGIN
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	CALL prc_GetCustomerRate(p_CompanyID,p_CustomerID,p_TrunkID,null,null,null,p_Effective,1,0,0,0,'','',-1);
  
  	SELECT
  		p_NameFormat AS AuthRule, 
  		p_Account AS AccountName,
  		p_Trunk AS Trunk,
  		p_TrunkPrefix AS CustomerTrunkPrefix,
  		Code,
  		Description,
  		Rate,
  		EffectiveDate,
  		ConnectionFee,
  		Interval1,
  		IntervalN,
  		Prefix AS TrunkPrefix,
  		RatePrefix AS TrunkRatePrefix,
  		AreaPrefix AS TrunkAreaPrefix
  	FROM tmp_customerrate_;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_VendorRateForExport`;
  DELIMITER |
  CREATE PROCEDURE `prc_VendorRateForExport`(
  	IN `p_CompanyID` INT,
  	IN `p_AccountID` INT ,
  	IN `p_TrunkID` INT,
  	IN `p_NameFormat` VARCHAR(50),
  	IN `p_Account` VARCHAR(200),
  	IN `p_Trunk` VARCHAR(200) ,
  	IN `p_TrunkPrefix` VARCHAR(50),
  	IN `p_Effective` VARCHAR(50),
  	IN `p_DiscontinueRate` VARCHAR(50)
  )
  BEGIN
  
  	DECLARE TrunkRatePrefix VARCHAR(50);
  	DECLARE TrunkAreaPrefix VARCHAR(50);
  	DECLARE TrunkPrefix VARCHAR(50);
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	SELECT 
  		RatePrefix,
  		AreaPrefix,
  		Prefix
  	INTO
  		TrunkRatePrefix,
  		TrunkAreaPrefix,
  		TrunkPrefix
  	FROM tblTrunk
  	WHERE TrunkID = p_TrunkID;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate_;
  	CREATE TEMPORARY TABLE tmp_VendorRate_ (
  		TrunkId INT,
  		RateId INT,
  		Rate DECIMAL(18,6),
  		EffectiveDate DATE,
  		Interval1 INT,
  		IntervalN INT,
  		ConnectionFee DECIMAL(18,6),
  		INDEX IX_tmp_VendorRate_ (`RateId`)
  	);
  	INSERT INTO tmp_VendorRate_
  	SELECT
  		TrunkID,
  		RateId,
  		Rate,
  		EffectiveDate,
  		Interval1,
  		IntervalN,
  		ConnectionFee
  	FROM tblVendorRate
  	WHERE tblVendorRate.AccountId = p_AccountID
  		AND tblVendorRate.TrunkId = p_TrunkID
  		AND
  		(
  			(p_Effective = 'Now' AND EffectiveDate <= NOW())
  			OR 
  			(p_Effective = 'Future' AND EffectiveDate > NOW())
  			OR 
  			(p_Effective = 'All')
  		);
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_VendorRate2_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_VendorRate2_ AS (SELECT * from tmp_VendorRate_);
  	DELETE n1 FROM tmp_VendorRate_ n1, tmp_VendorRate2_ n2 WHERE n1.EffectiveDate < n2.EffectiveDate
  	AND n1.TrunkID = n2.TrunkID
  	AND  n1.RateId = n2.RateId
  	AND n1.EffectiveDate <= NOW()
  	AND n2.EffectiveDate <= NOW();
  
  	IF p_DiscontinueRate = 'no'
  	THEN
  
  		SELECT DISTINCT
  			p_NameFormat AS AuthRule,
  			p_Account AS AccountName,
  			p_Trunk AS Trunk,
  			p_TrunkPrefix AS VendorTrunkPrefix,
  			TrunkRatePrefix,
  			TrunkAreaPrefix,
  			TrunkPrefix,
  			tblRate.Code ,
  			tblRate.Description ,
  			CASE WHEN tblVendorRate.Interval1 IS NOT NULL
  			THEN
  				tblVendorRate.Interval1
  			ElSE
  				tblRate.Interval1
  			END AS Interval1,
  			CASE WHEN tblVendorRate.IntervalN IS NOT NULL
  			THEN
  				tblVendorRate.IntervalN
  			ELSE
  				tblRate.IntervalN
  			END  AS IntervalN ,
  			tblVendorRate.Rate,
  			tblVendorRate.EffectiveDate,
  			tblVendorRate.ConnectionFee,
  			IFNULL(Preference,5) as `Preference`,
  			CASE WHEN 
  				(blockCode.VendorBlockingId IS NOT NULL AND FIND_IN_SET(tblVendorRate.TrunkId,blockCode.TrunkId) != 0) 
  				OR
  				(blockCountry.VendorBlockingId IS NOT NULL AND FIND_IN_SET(tblVendorRate.TrunkId,blockCountry.TrunkId) != 0 ) 
  			THEN 
  				'1'
  			ELSE 
  				'0'
  			END AS `Blocked`
  		FROM    tmp_VendorRate_ AS tblVendorRate
  		INNER JOIN tblRate
  			ON tblVendorRate.RateId =tblRate.RateID
  		LEFT JOIN tblVendorBlocking AS blockCode
  			ON tblVendorRate.RateID = blockCode.RateId
  			AND blockCode.AccountId = p_AccountID
  			AND blockCode.TrunkID = p_TrunkID
  			AND tblVendorRate.TrunkID = blockCode.TrunkID
  		LEFT JOIN tblVendorBlocking AS blockCountry
  			ON tblRate.CountryID = blockCountry.CountryId
  			AND blockCountry.AccountId = p_AccountID
  			AND blockCountry.TrunkID = p_TrunkID
  			AND tblVendorRate.TrunkID = blockCountry.TrunkID
  		LEFT JOIN tblVendorPreference 
  			ON tblVendorPreference.AccountId = p_AccountID
  			AND tblVendorPreference.TrunkID = p_TrunkID
  			AND tblVendorPreference.TrunkID = tblVendorRate.TrunkID
  			AND tblVendorPreference.RateId = tblVendorRate.RateId;
  
  	ELSE
  
  
  		SELECT DISTINCT
  			p_NameFormat AS AuthRule,
  			p_Account AS AccountName,
  			p_Trunk AS Trunk,
  			p_TrunkPrefix AS VendorTrunkPrefix,
  			TrunkRatePrefix,
  			TrunkAreaPrefix,
  			TrunkPrefix,
  			tblRate.Code,
  			tblRate.Description,
  			CASE WHEN tblVendorRate.Interval1 IS NOT NULL
  			THEN
  				tblVendorRate.Interval1
  			ElSE
  				tblRate.Interval1
  			END AS Interval1,
  			CASE WHEN tblVendorRate.IntervalN IS NOT NULL
  			THEN
  				tblVendorRate.IntervalN
  			ElSE
  				tblRate.IntervalN
  			END  AS IntervalN,
  			tblVendorRate.Rate,
  			tblVendorRate.EffectiveDate,
  			tblVendorRate.ConnectionFee,
  			IFNULL(Preference,5) as `Preference`,
  			CASE WHEN 
  				(blockCode.VendorBlockingId IS NOT NULL AND FIND_IN_SET(tblVendorRate.TrunkId,blockCode.TrunkId) != 0 )
  				OR
  				(blockCountry.VendorBlockingId IS NOT NULL AND FIND_IN_SET(tblVendorRate.TrunkId,blockCountry.TrunkId) != 0	) 
  			THEN
  				'1'
  			ELSE
  				'0'
  			END AS `Blocked`,
  			'N' AS `Discontinued`
  		FROM tmp_VendorRate_ AS tblVendorRate 
  		INNER JOIN tblRate
  			ON tblVendorRate.RateId = tblRate.RateID
  		LEFT JOIN tblVendorBlocking AS blockCode
  			ON tblVendorRate.RateID = blockCode.RateId
  			AND blockCode.AccountId = p_AccountID
  			AND blockCode.TrunkID = p_TrunkID
  			AND tblVendorRate.TrunkID = blockCode.TrunkID
  		LEFT JOIN tblVendorBlocking AS blockCountry
  			ON tblRate.CountryID = blockCountry.CountryId
  			AND blockCountry.AccountId = p_AccountID
  			AND blockCountry.TrunkID = p_TrunkID
  			AND tblVendorRate.TrunkID = blockCountry.TrunkID
  		LEFT JOIN tblVendorPreference
  			ON tblVendorPreference.AccountId = p_AccountID
  			AND tblVendorPreference.TrunkID = p_TrunkID
  			AND tblVendorPreference.TrunkID = tblVendorRate.TrunkID
  			AND tblVendorPreference.RateId = tblVendorRate.RateId
  
  		UNION ALL
  
  		SELECT
  			p_NameFormat AS AuthRule,
  			p_Account AS AccountName,
  			p_Trunk AS Trunk,
  			p_TrunkPrefix AS VendorTrunkPrefix,
  			TrunkRatePrefix,
  			TrunkAreaPrefix,
  			TrunkPrefix, 
  			vrd.Code,
  			vrd.Description,
  			vrd.Interval1,
  			vrd.IntervalN,
  			vrd.Rate,
  			vrd.EffectiveDate,
  			vrd.ConnectionFee,
  			'' AS `Preference`,
  			'' AS `Forbidden`,
  			'Y' AS `Discontinued`
  		FROM tblVendorRateDiscontinued vrd
  		LEFT JOIN tblVendorRate vr
  			ON vrd.AccountId = vr.AccountId 
  			AND vrd.TrunkID = vr.TrunkID
  			AND vrd.RateId = vr.RateId
  		WHERE vrd.AccountId = p_AccountID
  		AND vrd.TrunkID = p_TrunkID
  		AND vr.VendorRateID IS NULL ;
  
  	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  USE `RMBilling3`;
  DROP PROCEDURE IF EXISTS `prc_getPaymentPendingInvoice`;
  DELIMITER |
  CREATE PROCEDURE `prc_getPaymentPendingInvoice`(
  	IN `p_CompanyID` INT,
  	IN `p_AccountID` INT,
  	IN `p_PaymentDueInDays` INT,
  	IN `p_AutoPay` INT
  )
  BEGIN
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	IF p_AutoPay=1
  	THEN
  	
  	SELECT
  		MAX(i.InvoiceID) AS InvoiceID,
  		(IFNULL(MAX(i.GrandTotal), 0) - IFNULL(SUM(p.Amount), 0)) AS RemaingAmount
  	FROM tblInvoice i
  	INNER JOIN Ratemanagement3.tblAccount a
  		ON i.AccountID = a.AccountID
  	INNER JOIN Ratemanagement3.tblAccountBilling ab 
  		ON ab.AccountID = a.AccountID AND ab.ServiceID = i.ServiceID
  	INNER JOIN Ratemanagement3.tblBillingClass b
  		ON b.BillingClassID = ab.BillingClassID
  	LEFT JOIN tblPayment p
  		ON p.AccountID = i.AccountID
  		AND p.InvoiceID = i.InvoiceID AND p.Status = 'Approved' AND p.AccountID = i.AccountID
  		AND p.Status = 'Approved'
  		AND p.Recall = 0
  	WHERE i.CompanyID = p_CompanyID
  	AND i.InvoiceStatus NOT IN ( 'awaiting','cancel' , 'draft' , 'paid','post')
  	AND ( (i.ItemInvoice IS NULL) OR (i.ItemInvoice=1 AND i.RecurringInvoiceID IS NOT NULL))
  	AND i.InvoiceType =1
  	AND i.AccountID = p_AccountID
  	AND (p_PaymentDueInDays =0  OR (p_PaymentDueInDays =1 AND TIMESTAMPDIFF(DAY, i.IssueDate, NOW()) >= IFNULL(b.PaymentDueInDays,0) ) )
  
  	GROUP BY i.InvoiceID,
  			 p.AccountID
  	HAVING (IFNULL(MAX(i.GrandTotal), 0) - IFNULL(SUM(p.Amount), 0)) > 0;
  	
  	END IF;
  	
  	IF p_AutoPay =0
  	THEN
  	
  	SELECT
  		MAX(i.InvoiceID) AS InvoiceID,
  		(IFNULL(MAX(i.GrandTotal), 0) - IFNULL(SUM(p.Amount), 0)) AS RemaingAmount
  	FROM tblInvoice i
  	INNER JOIN Ratemanagement3.tblAccount a
  		ON i.AccountID = a.AccountID
  	INNER JOIN Ratemanagement3.tblAccountBilling ab 
  		ON ab.AccountID = a.AccountID AND ab.ServiceID = i.ServiceID
  	INNER JOIN Ratemanagement3.tblBillingClass b
  		ON b.BillingClassID = ab.BillingClassID
  	LEFT JOIN tblPayment p
  		ON p.AccountID = i.AccountID
  		AND p.InvoiceID = i.InvoiceID AND p.Status = 'Approved' AND p.AccountID = i.AccountID
  		AND p.Status = 'Approved'
  		AND p.Recall = 0
  	WHERE i.CompanyID = p_CompanyID
  	AND i.InvoiceStatus != 'cancel'
  	AND i.AccountID = p_AccountID
  	AND (p_PaymentDueInDays =0  OR (p_PaymentDueInDays =1 AND TIMESTAMPDIFF(DAY, i.IssueDate, NOW()) >= IFNULL(b.PaymentDueInDays,0) ) )
  
  	GROUP BY i.InvoiceID,
  			 p.AccountID
  	HAVING (IFNULL(MAX(i.GrandTotal), 0) - IFNULL(SUM(p.Amount), 0)) > 0;
  	
  	END IF;
  	
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_updateDefaultPrefix`;
  DELIMITER |
  CREATE PROCEDURE `prc_updateDefaultPrefix`(
  	IN `p_processId` INT,
  	IN `p_tbltempusagedetail_name` VARCHAR(200)
  )
  BEGIN
  	DECLARE v_pointer_ INT;
  	DECLARE v_partition_limit_ INT;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail_(
  		TempUsageDetailID int,
  		prefix varchar(50),
  		INDEX IX_TempUsageDetailID(`TempUsageDetailID`)
  	);
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail2_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail2_(
  		TempUsageDetailID int,
  		prefix varchar(50),
  		INDEX IX_TempUsageDetailID2(`TempUsageDetailID`)
  	);
  
  	/* find prefix from default codes and accounts doen't have active trunks */
  
  	SET v_pointer_ = 0;
  	SET v_partition_limit_ = 1000;
  	SET @stm = CONCAT('
  		SET @rowCount = (SELECT   COUNT(*)   FROM RMCDR3.' , p_tbltempusagedetail_name , '  ud LEFT JOIN tmp_Accounts_ a ON a.AccountID = ud.AccountID WHERE a.AccountID IS NULL AND area_prefix = "Other" AND ProcessID = "' , p_processId , '"  );
  	');
  
  	PREPARE stmt FROM @stm;
  	EXECUTE stmt;
  	DEALLOCATE PREPARE stmt;
  	
  	WHILE v_pointer_ <= @rowCount
  	DO
  
  		DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetailPart_;
  		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetailPart_(
  			TempUsageDetailID int,
  			cld varchar(500),
  			INDEX IX_TempUsageDetailID(`TempUsageDetailID`),
  			INDEX IX_cld(`cld`)
  		);
  		SET @stm = CONCAT('
  		INSERT INTO tmp_TempUsageDetailPart_
  		SELECT 
  			TempUsageDetailID,
  			cld
  		FROM RMCDR3.' , p_tbltempusagedetail_name , ' ud
  		LEFT JOIN tmp_Accounts_ a
  			ON a.AccountID = ud.AccountID
  		WHERE a.AccountID IS NULL 
  			AND ProcessID = "' , p_processId , '"
  			AND area_prefix = "Other"  
  		LIMIT ',v_partition_limit_,' OFFSET ',v_pointer_,';
  		');
  
  		PREPARE stmt FROM @stm;
  		EXECUTE stmt;
  		DEALLOCATE PREPARE stmt;
  
  		INSERT INTO tmp_TempUsageDetail_
  		SELECT
  			TempUsageDetailID,
  			c.code AS prefix
  		FROM tmp_TempUsageDetailPart_ ud
  		INNER JOIN Ratemanagement3.tmp_codes_ c 
  			ON  cld like  CONCAT(c.Code,"%");
  			
  		SET v_pointer_ = v_pointer_ + v_partition_limit_;
  		
  	END WHILE;
  
  	INSERT INTO tmp_TempUsageDetail2_
  	SELECT tbl.TempUsageDetailID,MAX(tbl.prefix)  
  	FROM tmp_TempUsageDetail_ tbl
  	GROUP BY tbl.TempUsageDetailID;
  
  	SET @stm = CONCAT('UPDATE RMCDR3.' , p_tbltempusagedetail_name , ' tbl2
  	INNER JOIN tmp_TempUsageDetail2_ tbl
  		ON tbl2.TempUsageDetailID = tbl.TempUsageDetailID
  	SET area_prefix = prefix
  	WHERE tbl2.processId = "' , p_processId , '"
  	');
  
  	PREPARE stmt FROM @stm;
  	EXECUTE stmt;
  	DEALLOCATE PREPARE stmt;
  
  END|
  DELIMITER ;
  
  
  DROP PROCEDURE IF EXISTS `prc_updateDefaultVendorPrefix`;
  DELIMITER |
  CREATE PROCEDURE `prc_updateDefaultVendorPrefix`(
  	IN `p_processId` INT,
  	IN `p_tbltempusagedetail_name` VARCHAR(200)
  )
  BEGIN
  	DECLARE v_pointer_ INT;
  	DECLARE v_partition_limit_ INT;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail_(
  		TempVendorCDRID int,
  		prefix varchar(50),
  		INDEX IX_TempVendorCDRID(`TempVendorCDRID`)
  	);
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail2_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail2_(
  		TempVendorCDRID int,
  		prefix varchar(50),
  		INDEX IX_TempVendorCDRID2(`TempVendorCDRID`)
  	);
  
  	/* find prefix from default codes and accounts doen't have active trunks */	
  
  	SET v_pointer_ = 0;
  	SET v_partition_limit_ = 1000;
  	SET @stm = CONCAT('
  		SET @rowCount = (SELECT   COUNT(*)   FROM RMCDR3.' , p_tbltempusagedetail_name , '  ud LEFT JOIN tmp_Accounts_ a ON a.AccountID = ud.AccountID WHERE a.AccountID IS NULL AND area_prefix = "Other" AND ProcessID = "' , p_processId , '"  );
  	');
  
  	PREPARE stmt FROM @stm;
  	EXECUTE stmt;
  	DEALLOCATE PREPARE stmt;
  
  	WHILE v_pointer_ <= @rowCount
  	DO
  
  		DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetailPart_;
  		CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetailPart_(
  			TempVendorCDRID int,
  			cld varchar(500),
  			INDEX IX_TempVendorCDRID(`TempVendorCDRID`),
  			INDEX IX_cld(`cld`)
  		);
  		SET @stm = CONCAT('
  		INSERT INTO tmp_TempUsageDetailPart_
  		SELECT 
  			TempVendorCDRID,
  			cld
  		FROM RMCDR3.' , p_tbltempusagedetail_name , ' ud
  		LEFT JOIN tmp_Accounts_ a
  			ON a.AccountID = ud.AccountID
  		WHERE a.AccountID IS NULL 
  			AND ProcessID = "' , p_processId , '"
  			AND area_prefix = "Other"  
  		LIMIT ',v_partition_limit_,' OFFSET ',v_pointer_,';
  		');
  
  		PREPARE stmt FROM @stm;
  		EXECUTE stmt;
  		DEALLOCATE PREPARE stmt;
  
  		INSERT INTO tmp_TempUsageDetail_
  		SELECT
  			TempVendorCDRID,
  			c.code AS prefix
  		FROM tmp_TempUsageDetailPart_ ud
  		INNER JOIN Ratemanagement3.tmp_codes_ c 
  			ON  cld like  CONCAT(c.Code,"%");
  			
  		SET v_pointer_ = v_pointer_ + v_partition_limit_;
  
  	END WHILE;
  
  	INSERT INTO tmp_TempUsageDetail2_
  	SELECT tbl.TempVendorCDRID,MAX(tbl.prefix)  
  	FROM tmp_TempUsageDetail_ tbl
  	GROUP BY tbl.TempVendorCDRID;
  
  	SET @stm = CONCAT('UPDATE RMCDR3.' , p_tbltempusagedetail_name , ' tbl2
  	INNER JOIN tmp_TempUsageDetail2_ tbl
  		ON tbl2.TempVendorCDRID = tbl.TempVendorCDRID
  	SET area_prefix = prefix
  	WHERE tbl2.processId = "' , p_processId , '"
  	');
  
  	PREPARE stmt FROM @stm;
  	EXECUTE stmt;
  	DEALLOCATE PREPARE stmt;
  
  END|
  DELIMITER ;
  
  
  DROP PROCEDURE IF EXISTS `prc_updatePrefix`;
  DELIMITER |
  CREATE PROCEDURE `prc_updatePrefix`(
  	IN `p_AccountID` INT,
  	IN `p_TrunkID` INT,
  	IN `p_processId` INT,
  	IN `p_tbltempusagedetail_name` VARCHAR(200),
  	IN `p_ServiceID` INT
  )
  BEGIN
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail_(
  		TempUsageDetailID int,
  		prefix varchar(50),
  		INDEX IX_TempUsageDetailID(`TempUsageDetailID`)
  	);
  	DROP TEMPORARY TABLE IF EXISTS tmp_TempUsageDetail2_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_TempUsageDetail2_(
  		TempUsageDetailID int,
  		prefix varchar(50),
  		INDEX IX_TempUsageDetailID2(`TempUsageDetailID`)
  	);
  
  	IF p_TrunkID > 0
  	THEN
  
  		SET @stm = CONCAT('
  		INSERT INTO tmp_TempUsageDetail_
  		SELECT
  			TempUsageDetailID,
  			c.code AS prefix
  		FROM RMCDR3.' , p_tbltempusagedetail_name , ' ud
  		INNER JOIN Ratemanagement3.tmp_codes_ c 
  		ON ud.ProcessID = ' , p_processId , '
  			AND ud.is_inbound = 0 
  			AND ud.AccountID = ' , p_AccountID , '
  			AND ud.TrunkID = ' , p_TrunkID , '
  			AND ud.UseInBilling = 0
  			AND ud.area_prefix = "Other"
  			AND ( extension <> cld or extension IS NULL)
  			AND cld REGEXP "^[0-9]+$"
  			AND cld like  CONCAT(c.Code,"%");
  		');
  
  		PREPARE stmt FROM @stm;
  		EXECUTE stmt;
  		DEALLOCATE PREPARE stmt;
  
  		SET @stm = CONCAT('
  		INSERT INTO tmp_TempUsageDetail_
  		SELECT
  			TempUsageDetailID,
  			c.code AS prefix
  		FROM RMCDR3.' , p_tbltempusagedetail_name , ' ud
  		INNER JOIN Ratemanagement3.tmp_codes_ c 
  		ON ud.ProcessID = ' , p_processId , '
  			AND ud.is_inbound = 0
  			AND ud.AccountID = ' , p_AccountID , '
  			AND ud.TrunkID = ' , p_TrunkID , '
  			AND ud.UseInBilling = 1 
  			AND ud.area_prefix = "Other"
  			AND ( extension <> cld or extension IS NULL)
  			AND REPLACE(cld,ud.TrunkPrefix,"") REGEXP "^[0-9]+$"
  			AND cld like  CONCAT(ud.TrunkPrefix,c.Code,"%");
  		');
  
  		PREPARE stmt FROM @stm;
  		EXECUTE stmt;
  		DEALLOCATE PREPARE stmt;
  
  	ELSE
  
  		SET @stm = CONCAT('
  		INSERT INTO tmp_TempUsageDetail_
  		SELECT
  			TempUsageDetailID,
  			c.code AS prefix
  		FROM RMCDR3.' , p_tbltempusagedetail_name , ' ud
  		INNER JOIN Ratemanagement3.tmp_codes_ c 
  		ON ud.ProcessID = ' , p_processId , '
  			AND ud.is_inbound = 0 
  			AND ud.AccountID = ' , p_AccountID , '
  			AND ud.ServiceID = ' , p_ServiceID , '
  			AND ud.area_prefix = "Other"
  			AND ( extension <> cld or extension IS NULL)
  			AND cld REGEXP "^[0-9]+$"
  			AND cld like  CONCAT(c.Code,"%");
  		');
  
  		PREPARE stmt FROM @stm;
  		EXECUTE stmt;
  		DEALLOCATE PREPARE stmt;
  
  	END IF;
  
  	PREPARE stm FROM @stm;
  	EXECUTE stm;
  	DEALLOCATE PREPARE stm;
  
  	INSERT INTO tmp_TempUsageDetail2_
  	SELECT tbl.TempUsageDetailID,MAX(tbl.prefix)
  	FROM tmp_TempUsageDetail_ tbl
  	GROUP BY tbl.TempUsageDetailID;
  
  	SET @stm = CONCAT('UPDATE RMCDR3.' , p_tbltempusagedetail_name , ' tbl2
  	INNER JOIN tmp_TempUsageDetail2_ tbl
  		ON tbl2.TempUsageDetailID = tbl.TempUsageDetailID
  	SET area_prefix = prefix
  	WHERE tbl2.processId = "' , p_processId , '"
  	');
  
  	PREPARE stmt FROM @stm;
  	EXECUTE stmt;
  	DEALLOCATE PREPARE stmt;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_importFromTempPaymentImportExport`;
  DELIMITER |
  CREATE PROCEDURE `prc_importFromTempPaymentImportExport`(
  	IN `p_ProcessID` VARCHAR(50),
  	IN `p_CurrentDate` DATETIME
  )
  BEGIN
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	DELETE
  		tmpp
  	FROM tblTempPaymentImportExport tmpp
  	INNER JOIN tblPayment p
  	ON p.CompanyID = tmpp.CompanyID
  	AND p.TransactionID = tmpp.TransactionID
  	WHERE tmpp.ProcessID= p_ProcessID ;
  
  	INSERT INTO tblPayment (
  			CompanyID,
  			AccountID,
  			CurrencyID,
  			Amount,
  			PaymentDate,
  			PaymentType,
  			TransactionID,
  			`Status`,
  			PaymentMethod,
  			Notes,
  			created_at,
  			CreatedBy
  		)
  	SELECT
  		a.CompanyID,
  		a.AccountID,
  		a.CurrencyId,
  		tmpp.Amount,
  		tmpp.PaymentDate,
  		IF(tmpp.Amount >= 0 , 'Payment in', 'Payment out' ) AS PaymentType,
  		tmpp.TransactionID,
  		'Approved' AS `Status`,
  		'Cash' AS PaymentMethod,
  		Notes,
  		p_CurrentDate AS created_at,
  		'System Imported' AS CreatedBy
  	FROM tblTempPaymentImportExport tmpp
  	INNER JOIN Ratemanagement3.tblAccount a
  	ON a.CompanyID = tmpp.CompanyID
  	AND (tmpp.AccountNumber = a.Number OR tmpp.AccountNumber = a.AccountName) AND a.CurrencyId > 0
  	WHERE tmpp.ProcessID= p_ProcessID ;
  
  	SELECT AccountNumber AS `Account Number` ,Amount,PaymentDate AS `Payment Date` ,PaymentType AS `Payment Type`,TransactionID AS `Transaction ID`,`Action`
  	FROM
  	(
  
  		SELECT
  			tmpp.* ,
  			'Imported' AS `Action`
  		FROM tblTempPaymentImportExport tmpp
  		INNER JOIN Ratemanagement3.tblAccount a
  		ON a.CompanyID = tmpp.CompanyID
  		AND (tmpp.AccountNumber = a.Number OR tmpp.AccountNumber = a.AccountName) AND a.CurrencyId IS NOT NULL
  		WHERE tmpp.ProcessID= p_ProcessID
  
  		UNION
  
  		SELECT
  			tmpp.* ,
  			CONCAT('Skipped (',IF(a.AccountID,"Currency is not setup against Account" ,"Account doesn't exists in System" ),' )') as `Action`
  		FROM tblTempPaymentImportExport tmpp
  		LEFT JOIN Ratemanagement3.tblAccount a
  		ON a.CompanyID = tmpp.CompanyID
  		AND (tmpp.AccountNumber = a.Number OR tmpp.AccountNumber = a.AccountName)
  		WHERE tmpp.ProcessID= p_ProcessID AND ( a.AccountID IS NULL OR a.CurrencyId IS NULL)
  
  	) tmp;
  
  	DELETE FROM tblTempPaymentImportExport WHERE ProcessID = p_ProcessID;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  CREATE TABLE IF NOT EXISTS `tblTempProduct` (
    `ProductID` bigint(20) NOT NULL AUTO_INCREMENT,
    `CompanyId` int(11) DEFAULT NULL,
    `Name` varchar(50) COLLATE utf8_unicode_ci NOT NULL,
    `Code` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    `Description` longtext COLLATE utf8_unicode_ci,
    `Amount` decimal(18,2) DEFAULT NULL,
    `Active` tinyint(3) unsigned DEFAULT '1',
    `Note` longtext COLLATE utf8_unicode_ci,
    `BarCode` longtext COLLATE utf8_unicode_ci,
    `Change` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `ProcessID` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    `created_at` datetime DEFAULT CURRENT_TIMESTAMP,
    `created_by` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`ProductID`),
    KEY `IX_ProcessID` (`ProcessID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  CREATE TABLE IF NOT EXISTS `tblTempDynamicFieldsValue` (
    `DynamicFieldsValueID` int(11) NOT NULL AUTO_INCREMENT,
    `CompanyID` int(11) NOT NULL DEFAULT '0',
    `ProcessID` varchar(50) COLLATE utf8_unicode_ci DEFAULT NULL,
    `ParentID` int(11) NOT NULL DEFAULT '0',
    `DynamicFieldsID` int(11) NOT NULL DEFAULT '0',
    `FieldValue` text COLLATE utf8_unicode_ci,
    `FieldOrder` int(11) NOT NULL DEFAULT '0',
    `created_at` datetime DEFAULT NULL,
    `created_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    `updated_at` datetime DEFAULT NULL,
    `updated_by` varchar(100) COLLATE utf8_unicode_ci DEFAULT NULL,
    PRIMARY KEY (`DynamicFieldsValueID`),
    UNIQUE KEY `IXUnique_ParentID_DynamicFieldsID` (`ParentID`,`DynamicFieldsID`),
    KEY `IX_ParentID_DynamicFieldsID` (`DynamicFieldsID`,`ParentID`)
  ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
  
  
  DROP PROCEDURE IF EXISTS `prc_WSProcessItemUpload`;
  DELIMITER |
  CREATE PROCEDURE `prc_WSProcessItemUpload`(
  	IN `p_processId` VARCHAR(50),
  	IN `p_companyId` INT
  	)
  BEGIN
     	DECLARE v_AffectedRecords_ INT DEFAULT 0;
  	DECLARE totalexistingcode INT(11) DEFAULT 0;
  	DECLARE duplicate_c_records INT DEFAULT 0;
  	DECLARE dynamic_columns_count INT DEFAULT 0;
  	DECLARE dynamic_column_type VARCHAR(20) DEFAULT 'product';
  	DECLARE duplicate_f_records INT DEFAULT 0;
  	DECLARE current_datetime DATETIME DEFAULT NOW();
  	DECLARE updated_records INT DEFAULT 0;
  		
  	SET sql_mode = '';	    
     	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
     	SET SESSION sql_mode='';
      
  	DROP TEMPORARY TABLE IF EXISTS tmp_JobLog_;
     	CREATE TEMPORARY TABLE tmp_JobLog_  ( 
  			Message longtext     
     	);
      
  	-- starts delete duplicate Code record from temp table
  	SELECT COUNT(*) INTO duplicate_c_records FROM (SELECT count(Code)
  		FROM tblTempProduct 
  		GROUP BY Code
  		HAVING COUNT(*)>1) AS tbl;
  		
  	IF duplicate_c_records > 0
  	THEN
  		INSERT INTO tmp_JobLog_ (Message)
  			  SELECT DISTINCT 
  			  CONCAT( 'Duplicate Code in excel file - (',c_duplicate_count,' occurences) - ', Code)
  			  		FROM(
  						SELECT count(Code) AS c_duplicate_count, Code AS Code
  						FROM tblTempProduct 
  						GROUP BY Code
  						HAVING COUNT(*)>1) AS tbl;
  	END IF;
      
  	DELETE n1,fv
  		FROM tblTempProduct n1
  		INNER JOIN (
  			SELECT MIN(ProductID) as minid,Code FROM tblTempProduct WHERE ProcessID = p_processId
  			GROUP BY Code
  			HAVING COUNT(1)>1
  		) n2 ON n2.Code = n1.Code AND minid <> n1.ProductID
  		LEFT JOIN tblTempDynamicFieldsValue AS fv
  		ON fv.ParentID = n1.ProductID
  		WHERE n1.ProcessID = p_processId;
  	-- ends delete duplicate Code record from temp table
  	
  	-- starts disable products which has delete action in csv or excel file
  	UPDATE tblProduct p
  	LEFT JOIN tblTempProduct tp ON tp.Code=p.Code
  	SET p.Active=0, p.updated_at=current_datetime, p.ModifiedBy='system'
  	WHERE tp.Code=p.Code AND tp.Change='D' AND tp.ProcessID=p_processId;
  
  	-- log updated status records
  	SELECT COUNT(*) INTO updated_records
  		FROM tblProduct p
  		LEFT JOIN tblTempProduct tp ON p.Code=tp.Code
  		WHERE tp.Code=p.Code AND tp.Change='D' AND tp.ProcessID=p_processId;
  		
  	IF updated_records > 0
  	THEN
  		INSERT INTO tmp_JobLog_ (Message) VALUES(CONCAT(updated_records, ' Item(s) has been deleted!'));
  	END IF;
  	-- ends disable products which has delete action in csv or excel file
  
  	-- starts delete all duplicate records from temp table if dynamic column is unique
  	-- check if there is any dynamic columns for product table
  	SELECT count(*) INTO dynamic_columns_count FROM Ratemanagement3.tblDynamicFields WHERE Type = dynamic_column_type AND Status = 1;
  
  	IF dynamic_columns_count > 0
  	THEN
  		SELECT COUNT(*) INTO duplicate_f_records FROM (SELECT count(FieldValue)
  			FROM 
  				tblTempDynamicFieldsValue 
  			WHERE
  				DynamicFieldsID IN (
  					SELECT
  						f.DynamicFieldsID
  					FROM 
  						Ratemanagement3.tblDynamicFields AS f
  					LEFT JOIN
  						Ratemanagement3.tblDynamicFieldsDetail AS fd
  					ON
  						f.DynamicFieldsID = fd.DynamicFieldsID
  					WHERE 
  						f.Type = dynamic_column_type AND 
  						f.Status = 1 AND
  						fd.FieldType = 'is_unique' AND
  						fd.Options = 1
  				)
  			GROUP BY FieldValue,DynamicFieldsID
  			HAVING COUNT(*)>1) AS tbl;
  			
  		IF duplicate_f_records > 0
  		THEN
  			INSERT INTO tmp_JobLog_ (Message)
  			  	SELECT DISTINCT 
  				  	CONCAT( 'Duplicate ',FieldName,' in excel file - (',f_duplicate_count,' occurences) - ', FieldValue)
  				  		FROM(
  							SELECT 
  								count(fv.FieldValue) AS f_duplicate_count, fv.FieldValue AS FieldValue, f.FieldName AS FieldName
  							FROM 
  								tblTempDynamicFieldsValue AS fv
  							LEFT JOIN
  								Ratemanagement3.tblDynamicFields AS f
  							ON
  								fv.DynamicFieldsID = f.DynamicFieldsID
  							WHERE
  								fv.DynamicFieldsID IN (
  									SELECT
  										f1.DynamicFieldsID
  									FROM 
  										Ratemanagement3.tblDynamicFields AS f1
  									LEFT JOIN
  										Ratemanagement3.tblDynamicFieldsDetail AS fd
  									ON
  										f1.DynamicFieldsID = fd.DynamicFieldsID
  									WHERE 
  										f1.Type = dynamic_column_type AND 
  										f1.Status = 1 AND
  										fd.FieldType = 'is_unique' AND
  										fd.Options = 1
  								)
  							GROUP BY fv.FieldValue,fv.DynamicFieldsID
  							HAVING COUNT(*)>1) AS tbl;
  			-- if dynamic column is unique than delete all duplicate records from temp table
  			DELETE fv1, p
  				FROM tblTempDynamicFieldsValue fv1 
  				INNER JOIN (
  					SELECT MIN(DynamicFieldsValueID) AS minid, DynamicFieldsID, FieldValue FROM tblTempDynamicFieldsValue
  					WHERE ProcessID = p_processId
  			     	GROUP BY FieldValue,DynamicFieldsID
  					HAVING COUNT(1) > 1
  				) AS fv2
  			   ON (fv2.FieldValue = fv1.FieldValue
  			   AND fv1.DynamicFieldsID = fv2.DynamicFieldsID
  			   AND fv2.minid <> fv1.DynamicFieldsValueID)
  			   INNER JOIN
  					tblTempProduct AS p
  				ON
  					fv1.ParentID = p.ProductID
  				LEFT JOIN
  					Ratemanagement3.tblDynamicFields AS f
  				ON
  					fv1.DynamicFieldsID = f.DynamicFieldsID
  				WHERE
  					fv1.DynamicFieldsID IN (
  						SELECT
  							f1.DynamicFieldsID
  						FROM 
  							Ratemanagement3.tblDynamicFields AS f1
  						LEFT JOIN
  							Ratemanagement3.tblDynamicFieldsDetail AS fd
  						ON
  							f1.DynamicFieldsID = fd.DynamicFieldsID
  						WHERE 
  							f1.Type = dynamic_column_type AND 
  							f1.Status = 1 AND
  							fd.FieldType = 'is_unique' AND
  							fd.Options = 1
  					)
  				AND
  					fv1.ProcessID = p_processId;
  
  		END IF;
  	END IF;
  	-- ends delete all duplicate records from temp table if dynamic column is unique
  
  	-- starts check unique dynamic column and delete it if exist in tblDynamicFieldsValue
  	-- check unique dynamic column (if exist in tblDynamicFieldsValue)
  	SELECT 
  		count(fv1.FieldValue) INTO duplicate_f_records
  	FROM 
  		tblTempDynamicFieldsValue fv1
  	LEFT JOIN
  		Ratemanagement3.tblDynamicFieldsValue fv2 
  	ON 
  		fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  		fv1.FieldValue = fv2.FieldValue
  	WHERE
  		fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  		fv1.FieldValue = fv2.FieldValue AND
  		fv1.DynamicFieldsID IN (
  								SELECT
  									f1.DynamicFieldsID
  								FROM 
  									Ratemanagement3.tblDynamicFields AS f1
  								LEFT JOIN
  									Ratemanagement3.tblDynamicFieldsDetail AS fd
  								ON
  									f1.DynamicFieldsID = fd.DynamicFieldsID
  								WHERE 
  									f1.Type = dynamic_column_type AND 
  									f1.Status = 1 AND
  									fd.FieldType = 'is_unique' AND
  									fd.Options = 1
  							);
  
  	IF duplicate_f_records > 0
  	THEN
  		INSERT INTO tmp_JobLog_ (Message)
  			  SELECT DISTINCT 
  			  CONCAT( 'Existing ',FieldName,' - ', FieldValue)
  			  		FROM(
  						SELECT 
  							fv1.FieldValue AS FieldValue, f.FieldName AS FieldName
  						FROM 
  							tblTempDynamicFieldsValue fv1
  						LEFT JOIN
  							Ratemanagement3.tblDynamicFieldsValue fv2
  						ON 
  							fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  							fv1.FieldValue = fv2.FieldValue
  						LEFT JOIN
  							Ratemanagement3.tblDynamicFields AS f
  						ON
  							fv1.DynamicFieldsID = f.DynamicFieldsID
  						WHERE
  							fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  							fv1.FieldValue = fv2.FieldValue AND
  							fv1.DynamicFieldsID IN (
  													SELECT
  														f1.DynamicFieldsID
  													FROM 
  														Ratemanagement3.tblDynamicFields AS f1
  													LEFT JOIN
  														Ratemanagement3.tblDynamicFieldsDetail AS fd
  													ON
  														f1.DynamicFieldsID = fd.DynamicFieldsID
  													WHERE 
  														f1.Type = dynamic_column_type AND 
  														f1.Status = 1 AND
  														fd.FieldType = 'is_unique' AND
  														fd.Options = 1
  												)
  						) AS tbl;
  	END IF;
  
  	-- delete duplicate data from temp table which is already exist in main table (dynamic column which is unique)
  	DELETE
  		fv1, p
  	FROM
  		tblTempDynamicFieldsValue fv1
  	LEFT JOIN
  		Ratemanagement3.tblDynamicFieldsValue fv2
  	ON 
  		fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  		fv1.FieldValue = fv2.FieldValue
  	LEFT JOIN
  		Ratemanagement3.tblDynamicFields AS f
  	ON
  		fv1.DynamicFieldsID = f.DynamicFieldsID
  	INNER JOIN
  		tblTempProduct AS p
  	WHERE
  		fv1.ParentID = p.ProductID AND
  		fv1.DynamicFieldsID = fv2.DynamicFieldsID AND
  		fv1.FieldValue = fv2.FieldValue AND
  		fv1.DynamicFieldsID IN (
  								SELECT
  									f1.DynamicFieldsID
  								FROM 
  									Ratemanagement3.tblDynamicFields AS f1
  								LEFT JOIN
  									Ratemanagement3.tblDynamicFieldsDetail AS fd
  								ON
  									f1.DynamicFieldsID = fd.DynamicFieldsID
  								WHERE 
  									f1.Type = 'product' AND 
  									f1.Status = 1 AND
  									fd.FieldType = 'is_unique' AND
  									fd.Options = 1
  							);
  	-- ends check unique dynamic column and delete it if exist in tblDynamicFieldsValue
  
  	-- start product update if already exist
  	UPDATE 
  		tblProduct p
  	LEFT JOIN
  		tblTempProduct tp ON tp.Code = p.Code
  	SET 
  		p.Name=tp.Name,p.Description=tp.Description,p.Amount=tp.Amount,p.Active=tp.Active,p.Note=tp.Note,p.ModifiedBy='system',p.updated_at=current_datetime
  	WHERE 
  		tp.Code = p.Code AND  tp.Change!='D' AND tp.ProcessID = p_processId;
  	-- ends product update if already exist
  
  	-- starts count and log updated records
  	SELECT 
  		count(ttp1.Code) INTO totalexistingcode
  	FROM 
  		tblTempProduct ttp1
  	LEFT JOIN
  		tblProduct ttp2 ON ttp1.Code = ttp2.Code
  	WHERE
  		ttp1.Code = ttp2.Code;
  
  	IF totalexistingcode > 0
  	THEN
  		INSERT INTO tmp_JobLog_ (Message)
  			  SELECT DISTINCT 
  			  CONCAT(record_to_update, ' Records updated!')
  			  		FROM(
  						SELECT 
  							count(ttp3.Code) AS record_to_update
  						FROM 
  							tblTempProduct ttp3
  						LEFT JOIN
  							tblProduct ttp4 ON ttp3.Code = ttp4.Code
  						WHERE
  							ttp3.Code = ttp4.Code) AS tbl;
  	END IF;
  	-- ends count and log updated records
  
  	-- start insert dynamic columns if not exist of item to be updated
  	INSERT INTO
  		Ratemanagement3.tblDynamicFieldsValue (`CompanyId`,`ParentID`,`DynamicFieldsID`,`FieldValue`,`created_at`,`created_by`)
  	SELECT
  		ttdfv.CompanyId,ttp4.ProductID,ttdfv.DynamicFieldsID,ttdfv.FieldValue,ttdfv.created_at,ttdfv.created_by
  	FROM
  		tblTempDynamicFieldsValue ttdfv
  	LEFT JOIN
  		tblTempProduct ttp3 ON ttp3.ProductID = ttdfv.ParentID
  	LEFT JOIN
  		tblProduct ttp4 ON ttp3.Code = ttp4.Code
  	WHERE
  		NOT EXISTS (
  		    SELECT * FROM Ratemanagement3.tblDynamicFieldsValue WHERE ParentID = ttp4.ProductID
  		) AND
  		ttp3.ProductID = ttdfv.ParentID AND
  		ttp3.ProcessID = ttdfv.ProcessID AND
  		ttp3.Code = ttp4.Code AND
  		ttdfv.ProcessID = p_processId;
  	-- ends insert dynamic columns if not exist of item to be updated
  
  	-- start update dynamic columns if exist of item to be updated
  	DROP TEMPORARY TABLE IF EXISTS tmp_DynamicFieldsValue_;
  	CREATE TEMPORARY TABLE tmp_DynamicFieldsValue_  ( 
  		ProductID INT,
  		DynamicFieldsID INT,
  		FieldValue LONGTEXT,
  		ProcessID  LONGTEXT
  	);
  
  	INSERT INTO tmp_DynamicFieldsValue_ (ProductID,DynamicFieldsID,FieldValue,ProcessID)
  	SELECT
  		ttp4.ProductID,ttdfv.DynamicFieldsID,ttdfv.FieldValue,ttdfv.ProcessID
  	FROM
  		tblTempDynamicFieldsValue ttdfv
  	LEFT JOIN
  		tblTempProduct ttp3 ON ttp3.ProductID=ttdfv.ParentID
  	LEFT JOIN
  		tblProduct ttp4 ON ttp3.Code=ttp4.Code
  	WHERE
  		EXISTS (
  		    SELECT * FROM Ratemanagement3.tblDynamicFieldsValue WHERE ParentID=ttp4.ProductID AND DynamicFieldsID=ttdfv.DynamicFieldsID
  		) AND
  		ttp3.ProductID=ttdfv.ParentID AND
  		ttp3.ProcessID=ttdfv.ProcessID AND
  		ttp3.Code=ttp4.Code AND
  		ttdfv.ProcessID=p_processId;
  
  	UPDATE
  		Ratemanagement3.tblDynamicFieldsValue fv
  	LEFT JOIN
  		tmp_DynamicFieldsValue_ tfv
  	ON 
  		tfv.ProductID=fv.ParentID AND tfv.DynamicFieldsID=fv.DynamicFieldsID
  	SET
  		fv.FieldValue=tfv.FieldValue,fv.updated_at=current_datetime,fv.updated_by='system'
  	WHERE
  		tfv.ProductID=fv.ParentID AND tfv.DynamicFieldsID=fv.DynamicFieldsID AND tfv.ProcessID=p_processId;
  	-- ends update dynamic columns if exist of item to be updated
  
  
  	-- starts dynamic column insert of products to be inserted
  	INSERT INTO
  		Ratemanagement3.tblDynamicFieldsValue (`CompanyId`,`ParentID`,`DynamicFieldsID`,`FieldValue`,`created_at`,`created_by`)
  	SELECT
  		ttdfv.CompanyId,ttdfv.ParentID,ttdfv.DynamicFieldsID,ttdfv.FieldValue,ttdfv.created_at,ttdfv.created_by
  	FROM
  		tblTempDynamicFieldsValue ttdfv
  	LEFT JOIN
  		tblTempProduct ttp3 ON ttp3.ProductID = ttdfv.ParentID
  	LEFT JOIN
  		tblProduct ttp4 ON ttp3.Code = ttp4.Code
  	WHERE
  		ttp3.ProductID = ttdfv.ParentID AND
  		ttp3.ProcessID = ttdfv.ProcessID AND
  		ttp4.Code IS NULL AND
  		ttdfv.ProcessID = p_processId;
  	-- ends dynamic column insert of products to be inserted
  
  	-- start product insert
  	INSERT INTO 
  		tblProduct (`CompanyId`,`Name`,`Code`,`Description`,`Amount`,`Active`,`Note`,`created_at`,`CreatedBy`,`ModifiedBy`,`updated_at`)
  	SELECT 
  		tp3.CompanyId,tp3.Name,tp3.Code,tp3.Description,tp3.Amount,tp3.Active,tp3.Note,tp3.created_at,tp3.Created_By,tp3.Created_By,tp3.created_at
  	FROM 
  		tblTempProduct tp3
  	LEFT JOIN 
  		tblProduct tp2 ON tp3.Code = tp2.Code
  	WHERE 
  		tp2.Code IS NULL AND ProcessID = p_processId;
  	-- ends product insert
  
  	SET v_AffectedRecords_ = v_AffectedRecords_ + FOUND_ROWS();
  
  	INSERT INTO tmp_JobLog_ (Message)
  	SELECT CONCAT(v_AffectedRecords_, ' Records Uploaded!' );	
  	
  	-- starts dynamic column update ParentID of inserted products
  	UPDATE 
  		Ratemanagement3.tblDynamicFieldsValue tdfv
  	LEFT JOIN
  		tblTempProduct ttp ON tdfv.ParentID = ttp.ProductID
  	LEFT JOIN
  		tblProduct tp ON tp.Code = ttp.Code
  	SET 
  		ParentID = tp.ProductID
  	WHERE
  		tp.Name = ttp.Name AND 
  		tp.Code = ttp.Code AND 
  		tp.CompanyId = ttp.CompanyId AND 
  		tp.Description = ttp.Description AND 
  		tp.Amount = ttp.Amount AND
  		tp.Note = ttp.Note AND
  		tp.created_at = ttp.created_at AND
  		tp.CreatedBy = ttp.created_by AND
  		ttp.ProcessID = p_processId AND
  		tdfv.ParentID = ttp.ProductID;
  	-- ends dynamic column update ParentID of inserted products
  
  
  		DELETE  FROM tblTempProduct WHERE ProcessID = p_processId;
  		DELETE  FROM tblTempDynamicFieldsValue WHERE ProcessID = p_processId;
  		
  		SELECT * from tmp_JobLog_; 					
  	      
      SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  
  
  
  DROP PROCEDURE IF EXISTS `prc_getProductByBarCode`;
  DELIMITER |
  CREATE PROCEDURE `prc_getProductByBarCode`(
  	IN `p_fieldvalue` LONGTEXT,
  	IN `p_dynamicfieldsid` INT
  )
  BEGIN
  	
  	SELECT 
  		`P`.`ProductID`, `P`.`Name`, `P`.`Description`, `P`.`Amount` 
  		FROM 
  			`tblProduct` AS `P` 
  		LEFT JOIN 
  			`Ratemanagement3`.`tblDynamicFieldsValue` AS `B` 
  		ON 
  			`P`.`ProductID` = `B`.`ParentID` 
  		WHERE 
  			`B`.`FieldValue` = p_fieldvalue AND 
  			`B`.`DynamicFieldsID` = p_dynamicfieldsid;
  	
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_getProducts`;
  DELIMITER |
  CREATE PROCEDURE `prc_getProducts`(
  	IN `p_CompanyID` INT,
  	IN `p_Name` VARCHAR(50),
  	IN `p_Code` VARCHAR(50),
  	IN `p_Active` VARCHAR(1),
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_lSortCol` VARCHAR(50),
  	IN `p_SortOrder` VARCHAR(5),
  	IN `p_Export` INT
  )
  BEGIN
       DECLARE v_OffSet_ int;
       SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	      
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  
  
  	if p_Export = 0
  	THEN
  
      SELECT   
  			tblProduct.ProductID,
  			tblProduct.Name,
  			tblProduct.Code,
  			tblProduct.Amount,
  			tblProduct.updated_at,
  			tblProduct.Active,
  			tblProduct.Description,
  			tblProduct.Note
              from tblProduct
              where tblProduct.CompanyID = p_CompanyID
  			AND(p_Name ='' OR tblProduct.Name like Concat('%',p_Name,'%'))
              AND((p_Code ='' OR tblProduct.Code like CONCAT(p_Code,'%')))
              AND((p_Active = '' OR tblProduct.Active = p_Active))
           ORDER BY
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'NameDESC') THEN Name
                  END DESC,
                  CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'NameASC') THEN Name
                  END ASC,
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CodeDESC') THEN Code
                  END DESC,
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CodeASC') THEN Code
                  END ASC,
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AmountDESC') THEN Amount
                  END DESC,
                  CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AmountASC') THEN Amount
                  END ASC,
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'updated_atDESC') THEN tblProduct.updated_at
                  END DESC,
                  CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'updated_atASC') THEN tblProduct.updated_at
                  END ASC,
  				CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ActiveDESC') THEN Active
                  END DESC,
                  CASE
                      WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ActiveASC') THEN Active
                  END ASC
              LIMIT p_RowspPage OFFSET v_OffSet_;
  
  			SELECT
              COUNT(tblProduct.ProductID) AS totalcount
              from tblProduct
              where tblProduct.CompanyID = p_CompanyID
  			AND(p_Name ='' OR tblProduct.Name like Concat('%',p_Name,'%'))
              AND((p_Code ='' OR tblProduct.Code like CONCAT(p_Code,'%')))
              AND((p_Active = '' OR tblProduct.Active = p_Active));
  
  	ELSE
  
  			SELECT
  			tblProduct.ProductID,
  			tblProduct.Name,
  			tblProduct.Code,
  			tblProduct.Amount,
  			tblProduct.updated_at,
  			tblProduct.Active,
  			tblProduct.Description,
  			tblProduct.Note
              from tblProduct
  			where tblProduct.CompanyID = p_CompanyID
  			AND(p_Name ='' OR tblProduct.Name like Concat('%',p_Name,'%'))
              AND((p_Code ='' OR tblProduct.Code like CONCAT(p_Code,'%')))
              AND((p_Active = '' OR tblProduct.Active = p_Active));
  
  	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  	
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_getInvoice`;
  DELIMITER |
  CREATE PROCEDURE `prc_getInvoice`(
  	IN `p_CompanyID` INT,
  	IN `p_AccountID` INT,
  	IN `p_InvoiceNumber` VARCHAR(50),
  	IN `p_IssueDateStart` DATETIME,
  	IN `p_IssueDateEnd` DATETIME,
  	IN `p_InvoiceType` INT,
  	IN `p_InvoiceStatus` VARCHAR(50),
  	IN `p_IsOverdue` INT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_lSortCol` VARCHAR(50),
  	IN `p_SortOrder` VARCHAR(5),
  	IN `p_CurrencyID` INT,
  	IN `p_isExport` INT,
  	IN `p_sageExport` INT,
  	IN `p_zerovalueinvoice` INT,
  	IN `p_InvoiceID` LONGTEXT
  )
  BEGIN
  	DECLARE v_OffSet_ int;
  	DECLARE v_Round_ int;
  	DECLARE v_CurrencyCode_ VARCHAR(50);
  	DECLARE v_TotalCount int;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  	SET  sql_mode='ONLY_FULL_GROUP_BY,NO_ZERO_IN_DATE,NO_ZERO_DATE,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION';
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  	SELECT cr.Symbol INTO v_CurrencyCode_ from Ratemanagement3.tblCurrency cr where cr.CurrencyId =p_CurrencyID;
  	SELECT fnGetRoundingPoint(p_CompanyID) INTO v_Round_;
  
  	DROP TEMPORARY TABLE IF EXISTS tmp_Invoices_;
  	CREATE TEMPORARY TABLE IF NOT EXISTS tmp_Invoices_(
  		InvoiceType tinyint(1),
  		AccountName varchar(100),
  		InvoiceNumber varchar(100),
  		IssueDate datetime,
  		InvoicePeriod varchar(100),
  		CurrencySymbol varchar(5),
  		GrandTotal decimal(18,6),
  		TotalPayment decimal(18,6),
  		PendingAmount decimal(18,6),
  		InvoiceStatus varchar(50),
  		InvoiceID int,
  		Description varchar(500),
  		Attachment varchar(255),
  		AccountID int,
  		ItemInvoice tinyint(1),
  		BillingEmail varchar(255),
  		AccountNumber varchar(100),
  		PaymentDueInDays int,
  		PaymentDate datetime,
  		SubTotal decimal(18,6),
  		TotalTax decimal(18,6),
  		NominalAnalysisNominalAccountNumber varchar(100)
  	);
  
  		INSERT INTO tmp_Invoices_
  		SELECT inv.InvoiceType ,
  			ac.AccountName,
  			FullInvoiceNumber as InvoiceNumber,
  			inv.IssueDate,
  			IF(invd.StartDate IS NULL ,'',CONCAT('From ',date(invd.StartDate) ,'<br> To ',date(invd.EndDate))) as InvoicePeriod,
  			IFNULL(cr.Symbol,'') as CurrencySymbol,
  			inv.GrandTotal as GrandTotal,
  			(SELECT IFNULL(sum(p.Amount),0) FROM tblPayment p WHERE p.InvoiceID = inv.InvoiceID AND p.Status = 'Approved' AND p.AccountID = inv.AccountID AND p.Recall =0) as TotalPayment,
  			(inv.GrandTotal -  (SELECT IFNULL(sum(p.Amount),0) FROM tblPayment p WHERE p.InvoiceID = inv.InvoiceID AND p.Status = 'Approved' AND p.AccountID = inv.AccountID AND p.Recall =0) ) as `PendingAmount`,
  			inv.InvoiceStatus,
  			inv.InvoiceID,
  			inv.Description,
  			inv.Attachment,
  			inv.AccountID,
  			inv.ItemInvoice,
  			IFNULL(ac.BillingEmail,'') as BillingEmail,
  			ac.Number,
  			(SELECT IFNULL(b.PaymentDueInDays,0) FROM Ratemanagement3.tblAccountBilling ab INNER JOIN Ratemanagement3.tblBillingClass b ON b.BillingClassID =ab.BillingClassID WHERE ab.AccountID = ac.AccountID AND ab.ServiceID = inv.ServiceID LIMIT 1) as PaymentDueInDays,
  			(SELECT PaymentDate FROM tblPayment p WHERE p.InvoiceID = inv.InvoiceID AND p.Status = 'Approved' AND p.Recall =0 AND p.AccountID = inv.AccountID ORDER BY PaymentID DESC LIMIT 1) AS PaymentDate,
  			inv.SubTotal,
  			inv.TotalTax,
  			ac.NominalAnalysisNominalAccountNumber
  			FROM tblInvoice inv
  			INNER JOIN Ratemanagement3.tblAccount ac ON ac.AccountID = inv.AccountID
  			LEFT JOIN tblInvoiceDetail invd ON invd.InvoiceID = inv.InvoiceID AND (invd.ProductType = 5 OR inv.InvoiceType = 2)
  			LEFT JOIN Ratemanagement3.tblCurrency cr ON inv.CurrencyID   = cr.CurrencyId
  			WHERE ac.CompanyID = p_CompanyID
  			AND (p_AccountID = 0 OR ( p_AccountID != 0 AND inv.AccountID = p_AccountID))
  			AND (p_InvoiceNumber = '' OR (inv.FullInvoiceNumber like Concat('%',p_InvoiceNumber,'%')))
  			AND (p_IssueDateStart = '0000-00-00 00:00:00' OR ( p_IssueDateStart != '0000-00-00 00:00:00' AND inv.IssueDate >= p_IssueDateStart))
  			AND (p_IssueDateEnd = '0000-00-00 00:00:00' OR ( p_IssueDateEnd != '0000-00-00 00:00:00' AND inv.IssueDate <= p_IssueDateEnd))
  			AND (p_InvoiceType = 0 OR ( p_InvoiceType != 0 AND inv.InvoiceType = p_InvoiceType))
  			AND (p_InvoiceStatus = '' OR ( p_InvoiceStatus != '' AND FIND_IN_SET(inv.InvoiceStatus,p_InvoiceStatus) ))
  			AND (p_zerovalueinvoice = 0 OR ( p_zerovalueinvoice = 1 AND inv.GrandTotal != 0))
  			AND (p_InvoiceID = '' OR (p_InvoiceID !='' AND FIND_IN_SET (inv.InvoiceID,p_InvoiceID)!= 0 ))
  			AND (p_CurrencyID = '' OR ( p_CurrencyID != '' AND inv.CurrencyID = p_CurrencyID));
  
  	IF p_isExport = 0 and p_sageExport = 0
  	THEN
  
  		SELECT
  			InvoiceType ,
  			AccountName,
  			InvoiceNumber,
  			IssueDate,
  			InvoicePeriod,
  			CONCAT(CurrencySymbol, ROUND(GrandTotal,v_Round_)) as GrandTotal2,
  			CONCAT(CurrencySymbol,ROUND(TotalPayment,v_Round_),'/',ROUND(PendingAmount,v_Round_)) as `PendingAmount`,
  			InvoiceStatus,
  			InvoiceID,
  			Description,
  			Attachment,
  			AccountID,
  			PendingAmount as OutstandingAmount,
  			ItemInvoice,
  			BillingEmail,
  			GrandTotal
  		FROM tmp_Invoices_
  		WHERE (p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				)
  		ORDER BY
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AccountNameDESC') THEN AccountName
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'AccountNameASC') THEN AccountName
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceTypeDESC') THEN InvoiceType
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceTypeASC') THEN InvoiceType
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceStatusDESC') THEN InvoiceStatus
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceStatusASC') THEN InvoiceStatus
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceNumberASC') THEN InvoiceNumber
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceNumberDESC') THEN InvoiceNumber
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'IssueDateASC') THEN IssueDate
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'IssueDateDESC') THEN IssueDate
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoicePeriodASC') THEN InvoicePeriod
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoicePeriodDESC') THEN InvoicePeriod
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'GrandTotalDESC') THEN GrandTotal
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'GrandTotalASC') THEN GrandTotal
  				END ASC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceIDDESC') THEN InvoiceID
  				END DESC,
  			CASE WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'InvoiceIDASC') THEN InvoiceID
  				END ASC
  		LIMIT p_RowspPage OFFSET v_OffSet_;
  
  		SELECT COUNT(*) INTO v_TotalCount
  		FROM tmp_Invoices_
  		WHERE (p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				);
  
  		SELECT
  			v_TotalCount AS totalcount,
  			ROUND(sum(GrandTotal),v_Round_) as total_grand,
  			ROUND(sum(TotalPayment),v_Round_) as `TotalPayment`,
  			ROUND(sum(PendingAmount),v_Round_) as `TotalPendingAmount`,
  			v_CurrencyCode_ as currency_symbol
  		FROM tmp_Invoices_
  			WHERE ((InvoiceStatus IS NULL) OR (InvoiceStatus NOT IN('draft','Cancel')))
  			AND (p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				);
  
  	END IF;
  	IF p_isExport = 1
  	THEN
  
  		SELECT
  			AccountName ,
  			InvoiceNumber,
  			IssueDate,
  			REPLACE(InvoicePeriod, '<br>', '') as InvoicePeriod,
  			CONCAT(CurrencySymbol, ROUND(GrandTotal,v_Round_)) as GrandTotal,
  			CONCAT(CurrencySymbol,ROUND(TotalPayment,v_Round_),'/',ROUND(PendingAmount,v_Round_)) as `Paid/OS`,
  			InvoiceStatus,
  			InvoiceType,
  			ItemInvoice
  		FROM tmp_Invoices_
  		WHERE
  				(p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				);
  		END IF;
  	IF p_isExport = 2
  	THEN
  
  		
  
  		SELECT
  			AccountName ,
  			InvoiceNumber,
  			IssueDate,
  			REPLACE(InvoicePeriod, '<br>', '') as InvoicePeriod,
  			CONCAT(CurrencySymbol, ROUND(GrandTotal,v_Round_)) as GrandTotal,
  			CONCAT(CurrencySymbol,ROUND(TotalPayment,v_Round_),'/',ROUND(PendingAmount,v_Round_)) as `Paid/OS`,
  			InvoiceStatus,
  			InvoiceType,
  			ItemInvoice,
  			InvoiceID
  		FROM tmp_Invoices_
  		WHERE
  				(p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				);
  
  	END IF;
  
  	IF p_sageExport =1 OR p_sageExport =2
  	THEN
  			
  
  		IF p_sageExport = 2
  		THEN
  			UPDATE tblInvoice  inv
  			INNER JOIN Ratemanagement3.tblAccount ac
  				ON ac.AccountID = inv.AccountID
  			INNER JOIN Ratemanagement3.tblAccountBilling ab
  				ON ab.AccountID = ac.AccountID AND ab.ServiceID = inv.ServiceID
  			INNER JOIN Ratemanagement3.tblBillingClass b
  				ON ab.BillingClassID = b.BillingClassID
  			INNER JOIN Ratemanagement3.tblCurrency c
  				ON c.CurrencyId = ac.CurrencyId
  			SET InvoiceStatus = 'paid'
  			WHERE ac.CompanyID = p_CompanyID
  				AND (p_AccountID = 0 OR ( p_AccountID != 0 AND inv.AccountID = p_AccountID))
  				AND (p_InvoiceNumber = '' OR (inv.FullInvoiceNumber like Concat('%',p_InvoiceNumber,'%')))
  				AND (p_IssueDateStart = '0000-00-00 00:00:00' OR ( p_IssueDateStart != '0000-00-00 00:00:00' AND inv.IssueDate >= p_IssueDateStart))
  				AND (p_IssueDateEnd = '0000-00-00 00:00:00' OR ( p_IssueDateEnd != '0000-00-00 00:00:00' AND inv.IssueDate <= p_IssueDateEnd))
  				AND (p_InvoiceType = 0 OR ( p_InvoiceType != 0 AND inv.InvoiceType = p_InvoiceType))
  				AND (p_InvoiceStatus = '' OR ( p_InvoiceStatus != '' AND FIND_IN_SET(inv.InvoiceStatus,p_InvoiceStatus) ))
  				AND (p_zerovalueinvoice = 0 OR ( p_zerovalueinvoice = 1 AND inv.GrandTotal != 0))
  				AND (p_InvoiceID = '' OR (p_InvoiceID !='' AND FIND_IN_SET (inv.InvoiceID,p_InvoiceID)!= 0 ))
  				AND (p_CurrencyID = '' OR ( p_CurrencyID != '' AND inv.CurrencyID = p_CurrencyID))
  				AND (p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > IFNULL(b.PaymentDueInDays,0)
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND((inv.GrandTotal -  (select IFNULL(sum(p.Amount),0) from tblPayment p where p.InvoiceID = inv.InvoiceID AND p.Status = 'Approved' AND p.AccountID = inv.AccountID AND p.Recall =0) )>0)
  						)
  				);
  		END IF;
  		SELECT
  			AccountNumber,
  			DATE_FORMAT(DATE_ADD(IssueDate,INTERVAL PaymentDueInDays DAY), '%Y-%m-%d') AS DueDate,
  			GrandTotal AS GoodsValueInAccountCurrency,
  			GrandTotal AS SalControlValueInBaseCurrency,
  			1 AS DocumentToBaseCurrencyRate,
  			1 AS DocumentToAccountCurrencyRate,
  			DATE_FORMAT(IssueDate, '%Y-%m-%d') AS PostedDate,
  			InvoiceNumber AS TransactionReference,
  			'' AS SecondReference,
  			'' AS Source,
  			4 AS SYSTraderTranType, 
  			DATE_FORMAT(PaymentDate ,'%Y-%m-%d') AS TransactionDate,
  			TotalTax AS TaxValue,
  			SubTotal AS `NominalAnalysisTransactionValue/1`,
  			NominalAnalysisNominalAccountNumber AS `NominalAnalysisNominalAccountNumber/1`,
  			'NEON' AS `NominalAnalysisNominalAnalysisNarrative/1`,
  			'' AS `NominalAnalysisTransactionAnalysisCode/1`,
  			1 AS `TaxAnalysisTaxRate/1`,
  			SubTotal AS `TaxAnalysisGoodsValueBeforeDiscount/1`,
  			TotalTax as   `TaxAnalysisTaxOnGoodsValue/1`
  		FROM tmp_Invoices_
  		WHERE
  				(p_IsOverdue = 0
  					OR ((To_days(NOW()) - To_days(IssueDate)) > PaymentDueInDays
  							AND(InvoiceStatus NOT IN('awaiting','draft','Cancel'))
  							AND(PendingAmount>0)
  						)
  				);
  
  	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  END|
  DELIMITER ;
  
  USE `StagingReport`;
  
  DROP PROCEDURE IF EXISTS `prc_getDailyReport`;
  DELIMITER |
  CREATE PROCEDURE `prc_getDailyReport`(
  	IN `p_CompanyID` INT,
  	IN `p_AccountID` INT,
  	IN `p_StartDate` DATE,
  	IN `p_EndDate` DATE,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_isExport` INT
  )
  BEGIN
  	DECLARE v_OffSet_ int;
  	DECLARE v_Round_ int;
  	SET sql_mode = 'ALLOW_INVALID_DATES';
  	SET @PreviosBalance := 0;
  	SET @TotalPreviosBalance := 0;
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  	SELECT fnGetRoundingPoint(p_CompanyID) INTO v_Round_;
  
  	IF p_StartDate <> '0000-00-00'
  	THEN
  
  	SET @PreviosBalance := 
  	(SELECT 
  		COALESCE(SUM(Amount),0)
  	FROM RMBilling3.tblPayment 
  	WHERE CompanyID = p_CompanyID
  		AND AccountID = p_AccountID
  		AND Status = 'Approved'
  		AND Recall =0
  		AND PaymentType = 'Payment In'
  		AND PaymentDate < p_StartDate) - 
  		
  		(SELECT 
  		COALESCE(SUM(TotalCharges),0)
  	FROM tblHeader
  	INNER JOIN tblDimDate 
  		ON tblDimDate.DateID = tblHeader.DateID
  	WHERE CompanyID = p_CompanyID
  		AND AccountID = p_AccountID
  		AND date < p_StartDate);
  	
  	SET @TotalPreviosBalance := @PreviosBalance;
  
  	END IF;
  
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  	
  	DROP TEMPORARY TABLE IF EXISTS tmp_dates_;
  	CREATE TEMPORARY TABLE tmp_dates_  (
  		RowID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  		Dates Date,
  		UNIQUE INDEX `date` (`Dates`)
  	);
  	INSERT INTO tmp_dates_ (Dates)
  	SELECT 
  		DISTINCT DATE(PaymentDate) 
  	FROM RMBilling3.tblPayment 
  	WHERE CompanyID = p_CompanyID
  		AND AccountID = p_AccountID
  		AND Status = 'Approved'
  		AND Recall =0
  		AND PaymentType = 'Payment In'
  		AND (p_StartDate = '0000-00-00' OR ( p_StartDate != '0000-00-00' AND PaymentDate >= p_StartDate) )
  		AND (p_EndDate = '0000-00-00' OR ( p_EndDate != '0000-00-00' AND PaymentDate <= p_EndDate));
  
  	INSERT IGNORE INTO tmp_dates_ (Dates)
  	SELECT 
  		DISTINCT date 
  	FROM tblHeader
  	INNER JOIN tblDimDate 
  		ON tblDimDate.DateID = tblHeader.DateID
  	WHERE CompanyID = p_CompanyID
  		AND AccountID = p_AccountID
  		AND (p_StartDate = '0000-00-00' OR ( p_StartDate != '0000-00-00' AND date >= p_StartDate) )
  		AND (p_EndDate = '0000-00-00' OR ( p_EndDate != '0000-00-00' AND date <= p_EndDate));
  
  	IF p_isExport = 0
  	THEN
  
  		SELECT 
  			Dates,
  			ROUND(SUM(Amount),v_Round_),
  			ROUND(SUM(TotalCharges),v_Round_),
  			ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_),
  			@PreviosBalance:= ROUND(@PreviosBalance,v_Round_)+ ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_) AS Balance 
  		FROM tmp_dates_
  		LEFT JOIN RMBilling3.tblPayment 
  			ON date(PaymentDate) = Dates 
  			AND tblPayment.AccountID = p_AccountID
  			AND Status = 'Approved'
  			AND Recall =0
  			AND PaymentType = 'Payment In'
  		LEFT JOIN tblDimDate 
  			ON date = Dates
  		LEFT JOIN tblHeader 
  			ON tblDimDate.DateID = tblHeader.DateID 
  			AND tblHeader.AccountID = p_AccountID
  		GROUP BY Dates 
  		LIMIT p_RowspPage OFFSET v_OffSet_;
  
  		SELECT
  			COUNT(DISTINCT Dates) AS totalcount,
  			ROUND(COALESCE(SUM(Amount),0),v_Round_) AS TotalPayment,
  			ROUND(COALESCE(SUM(TotalCharges),0),v_Round_) AS TotalCharge,
  			ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_) AS Total,
  			ROUND(@TotalPreviosBalance,v_Round_)+ ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_) AS Balance 
  		FROM tmp_dates_
  		LEFT JOIN RMBilling3.tblPayment 
  			ON date(PaymentDate) = Dates 
  			AND tblPayment.AccountID = p_AccountID
  			AND Status = 'Approved'
  			AND Recall =0
  			AND PaymentType = 'Payment In'
  		LEFT JOIN tblDimDate 
  			ON date = Dates
  		LEFT JOIN tblHeader 
  			ON tblDimDate.DateID = tblHeader.DateID 
  			AND tblHeader.AccountID = p_AccountID;
  
  	END IF;
  
  	IF p_isExport = 1
  	THEN
  
  		SELECT 
  			Dates AS `Date`,
  			ROUND(SUM(Amount),v_Round_) AS `Payments`,
  			ROUND(SUM(TotalCharges),v_Round_) AS `Consumption`,
  			ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_) AS `Total`,
  			@PreviosBalance:= ROUND(@PreviosBalance,v_Round_)+ ROUND(COALESCE(SUM(Amount),0)-COALESCE(SUM(TotalCharges),0),v_Round_) AS `Balance`
  		FROM tmp_dates_
  		LEFT JOIN RMBilling3.tblPayment 
  			ON date(PaymentDate) = Dates 
  			AND tblPayment.AccountID = p_AccountID
  			AND Status = 'Approved'
  			AND Recall =0
  			AND PaymentType = 'Payment In'
  		LEFT JOIN tblDimDate 
  			ON date = Dates
  		LEFT JOIN tblHeader 
  			ON tblDimDate.DateID = tblHeader.DateID 
  			AND tblHeader.AccountID = p_AccountID
  		GROUP BY Dates;
  
  	END IF;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `prc_getDescReportAll`;
  DELIMITER |
  CREATE PROCEDURE `prc_getDescReportAll`(
  	IN `p_CompanyID` INT,
  	IN `p_CompanyGatewayID` INT,
  	IN `p_AccountID` INT,
  	IN `p_CurrencyID` INT,
  	IN `p_StartDate` DATETIME,
  	IN `p_EndDate` DATETIME,
  	IN `p_AreaPrefix` VARCHAR(50),
  	IN `p_Trunk` VARCHAR(50),
  	IN `p_CountryID` INT,
  	IN `p_UserID` INT,
  	IN `p_isAdmin` INT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_lSortCol` VARCHAR(50),
  	IN `p_SortOrder` VARCHAR(5),
  	IN `p_isExport` INT
  )
  BEGIN
  
  	DECLARE v_Round_ INT;
  	DECLARE v_OffSet_ INT;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  
  	SELECT fnGetRoundingPoint(p_CompanyID) INTO v_Round_;
  
  	CALL fngetDefaultCodes(p_CompanyID);
  
  	CALL fnUsageSummary(p_CompanyID,p_CompanyGatewayID,p_AccountID,p_CurrencyID,p_StartDate,p_EndDate,p_AreaPrefix,p_Trunk,p_CountryID,p_UserID,p_isAdmin,2);
  
  	/* grid display*/
  	IF p_isExport = 0
  	THEN
  
  		/* Description by call count */	
  		SELECT IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , IF(SUM(NoOfCalls)>0,ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_),0) AS ASR
  		FROM tmp_tblUsageSummary_ us
  		LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY c.Description
  		ORDER BY
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CallCountDESC') THEN SUM(NoOfCalls)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CallCountASC') THEN SUM(NoOfCalls)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalMinutesDESC') THEN COALESCE(SUM(TotalBilledDuration),0)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalMinutesASC') THEN COALESCE(SUM(TotalBilledDuration),0)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'DescriptionDESC') THEN Description
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'DescriptionASC') THEN Description
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalCostDESC') THEN ROUND(COALESCE(SUM(TotalCharges),0), v_Round_)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalCostASC') THEN ROUND(COALESCE(SUM(TotalCharges),0), v_Round_)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ACDDESC') THEN (COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls))
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ACDASC') THEN (COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls))
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ASRDESC') THEN SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ASRASC') THEN SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100
  		END ASC
  		LIMIT p_RowspPage OFFSET v_OffSet_;
  
  		SELECT COUNT(*) AS totalcount,SUM(CallCount) AS TotalCall,SUM(TotalSeconds) AS TotalDuration,SUM(TotalCost) AS TotalCost FROM(
  			SELECT IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , IF(SUM(NoOfCalls)>0,ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_),0) AS ASR
  			FROM tmp_tblUsageSummary_ us
  			LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  			GROUP BY c.Description
  		)tbl;
  
  	END IF;
  
  	/* export data*/
  	IF p_isExport = 1
  	THEN
  
  		SELECT SQL_CALC_FOUND_ROWS IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageSummary_ us
  		LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description;
  
  	END IF;
  
  	/* chart display*/
  	IF p_isExport = 2
  	THEN
  
  		/* top 10 Description by call count */
  		SELECT Description AS ChartVal ,SUM(NoOfCalls) AS CallCount,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(NoOfCalls) > 0 ORDER BY CallCount DESC LIMIT 10;
  
  		/* top 10 Description by call cost */
  		SELECT Description AS ChartVal,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(TotalCharges) > 0 ORDER BY TotalCost DESC LIMIT 10;
  
  		/* top 10 Description by call minutes */
  		SELECT Description AS ChartVal,ROUND(COALESCE(SUM(TotalBilledDuration),0)/ 60,0) AS TotalMinutes,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(TotalBilledDuration) > 0  ORDER BY TotalMinutes DESC LIMIT 10;
  
  	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  
  DROP PROCEDURE IF EXISTS `prc_getVendorDescReportAll`;
  DELIMITER |
  CREATE PROCEDURE `prc_getVendorDescReportAll`(
  	IN `p_CompanyID` INT,
  	IN `p_CompanyGatewayID` INT,
  	IN `p_AccountID` INT,
  	IN `p_CurrencyID` INT,
  	IN `p_StartDate` DATETIME,
  	IN `p_EndDate` DATETIME,
  	IN `p_AreaPrefix` VARCHAR(50),
  	IN `p_Trunk` VARCHAR(50),
  	IN `p_CountryID` INT,
  	IN `p_UserID` INT,
  	IN `p_isAdmin` INT,
  	IN `p_PageNumber` INT,
  	IN `p_RowspPage` INT,
  	IN `p_lSortCol` VARCHAR(50),
  	IN `p_SortOrder` VARCHAR(5),
  	IN `p_isExport` INT
  )
  BEGIN
  
  	DECLARE v_Round_ INT;
  	DECLARE v_OffSet_ INT;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL READ COMMITTED;
  
  	SET v_OffSet_ = (p_PageNumber * p_RowspPage) - p_RowspPage;
  
  	SELECT fnGetRoundingPoint(p_CompanyID) INTO v_Round_;
  
  	CALL fngetDefaultCodes(p_CompanyID);
  
  	CALL fnUsageVendorSummary(p_CompanyID,p_CompanyGatewayID,p_AccountID,p_CurrencyID,p_StartDate,p_EndDate,p_AreaPrefix,p_Trunk,p_CountryID,p_UserID,p_isAdmin,2);
  
  	/* grid display*/
  	IF p_isExport = 0
  	THEN
  
  		/* Description by call count */	
  		SELECT IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , IF(SUM(NoOfCalls)>0,ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_),0) AS ASR
  		FROM tmp_tblUsageVendorSummary_ us
  		LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY c.Description   
  		ORDER BY
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CallCountDESC') THEN SUM(NoOfCalls)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'CallCountASC') THEN SUM(NoOfCalls)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalMinutesDESC') THEN COALESCE(SUM(TotalBilledDuration),0)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalMinutesASC') THEN COALESCE(SUM(TotalBilledDuration),0)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'DescriptionDESC') THEN Description
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'DescriptionASC') THEN Description
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalCostDESC') THEN ROUND(COALESCE(SUM(TotalCharges),0), v_Round_)
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'TotalCostASC') THEN ROUND(COALESCE(SUM(TotalCharges),0), v_Round_)
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ACDDESC') THEN (COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls))
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ACDASC') THEN (COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls))
  		END ASC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ASRDESC') THEN SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100
  		END DESC,
  		CASE
  			WHEN (CONCAT(p_lSortCol,p_SortOrder) = 'ASRASC') THEN SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100
  		END ASC
  		LIMIT p_RowspPage OFFSET v_OffSet_;
  
  		SELECT COUNT(*) AS totalcount,SUM(CallCount) AS TotalCall,SUM(TotalSeconds) AS TotalDuration,SUM(TotalCost) AS TotalCost FROM (
  			SELECT IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , IF(SUM(NoOfCalls)>0,ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_),0) AS ASR
  			FROM tmp_tblUsageVendorSummary_ us
  			LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  			GROUP BY c.Description
  		)tbl;
  
  	END IF;
  
  	/* export data*/
  	IF p_isExport = 1
  	THEN
  
  		SELECT SQL_CALC_FOUND_ROWS IFNULL(Description,'Other') AS Description ,SUM(NoOfCalls) AS CallCount,COALESCE(SUM(TotalBilledDuration),0) AS TotalSeconds,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageVendorSummary_ us
  		LEFT JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description;
  
  	END IF;
  
  	/* chart display*/
  	IF p_isExport = 2
  	THEN
  
  		/* top 10 Description by call count */
  		SELECT Description AS ChartVal ,SUM(NoOfCalls) AS CallCount,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageVendorSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(NoOfCalls) > 0 ORDER BY CallCount DESC LIMIT 10;
  
  		/* top 10 Description by call cost */
  		SELECT Description AS ChartVal,ROUND(COALESCE(SUM(TotalCharges),0), v_Round_) AS TotalCost,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageVendorSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(TotalCharges) > 0 ORDER BY TotalCost DESC LIMIT 10;
  
  		/* top 10 Description by call minutes */
  		SELECT Description AS ChartVal,ROUND(COALESCE(SUM(TotalBilledDuration),0)/ 60,0) AS TotalMinutes,IF(SUM(NoOfCalls)>0,fnDurationmmss(COALESCE(SUM(TotalBilledDuration),0)/SUM(NoOfCalls)),0) AS ACD , ROUND(SUM(NoOfCalls)/(SUM(NoOfCalls)+SUM(NoOfFailCalls))*100,v_Round_) AS ASR
  		FROM tmp_tblUsageVendorSummary_ us
  		INNER JOIN tmp_codes_ c ON c.Code = us.AreaPrefix
  		GROUP BY Description HAVING SUM(TotalBilledDuration) > 0  ORDER BY TotalMinutes DESC LIMIT 10;
  
  	END IF;
  
  	SET SESSION TRANSACTION ISOLATION LEVEL REPEATABLE READ;
  
  END|
  DELIMITER ;
  
  DROP PROCEDURE IF EXISTS `fngetDefaultCodes`;
  DELIMITER |
  CREATE PROCEDURE `fngetDefaultCodes`(
  	IN `p_CompanyID` INT
  )
  BEGIN
  	
  	DROP TEMPORARY TABLE IF EXISTS tmp_codes_;
  	CREATE TEMPORARY TABLE tmp_codes_ (
  		CountryID INT,
  		Code VARCHAR(50),
  		Description VARCHAR(200),
  		INDEX tmp_codes_CountryID (`CountryID`),
  		INDEX tmp_codes_Code (`Code`)
  	);
  
  	INSERT INTO tmp_codes_
  	SELECT
  	DISTINCT
  		tblRate.CountryID,
  		tblRate.Code,
  		tblRate.Description
  	FROM Ratemanagement3.tblRate
  	INNER JOIN Ratemanagement3.tblCodeDeck
  		ON tblCodeDeck.CodeDeckId = tblRate.CodeDeckId
  	WHERE tblCodeDeck.CompanyId = p_CompanyID
  	AND tblCodeDeck.DefaultCodedeck = 1 ;
  	
  END|
  DELIMITER ;
  
  
  
  USE `Ratemanagement3`;
  
  ALTER TABLE `tblCronJob`
  	CHANGE COLUMN `JobTitle` `JobTitle` VARCHAR(200) NULL DEFAULT NULL COLLATE 'utf8_unicode_ci';
  
  ALTER TABLE `tblAccountAuthenticate`
  	CHANGE COLUMN `CustomerAuthValue` `CustomerAuthValue` TEXT NULL DEFAULT NULL COLLATE 'utf8_unicode_ci',
  	CHANGE COLUMN `VendorAuthValue` `VendorAuthValue` TEXT NULL DEFAULT NULL COLLATE 'utf8_unicode_ci';	
  	
  ALTER TABLE `tblAccountBilling`
  	ADD COLUMN `AutoPaymentSetting` VARCHAR(50) NULL DEFAULT NULL;
  
  ALTER TABLE `tblAccountService`
  	ADD COLUMN `ServiceDescription` TEXT NULL DEFAULT NULL;
  	
  ALTER TABLE `tblAccountService`
  	ADD COLUMN `ServiceTitleShow` INT NOT NULL DEFAULT '1';	
  
  ALTER TABLE `tblAccount`
  	ADD COLUMN `ShowAllPaymentMethod` INT NULL DEFAULT '1';	
  	
  ALTER TABLE `tblTempVendorRate`
  	ADD COLUMN `DialStringPrefix` VARCHAR(500) NULL DEFAULT NULL;	
  
  INSERT INTO `tblIntegration` (`IntegrationID`, `CompanyId`, `Title`, `Slug`, `ParentID`, `MultiOption`) VALUES (20, 1, 'Stripe ACH', 'stripeach', 4, 'N');
  	
  INSERT INTO `tblEmailTemplate` (`CompanyID`, `TemplateName`, `Subject`, `TemplateBody`, `created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`, `userID`, `Type`, `EmailFrom`, `StaticType`, `SystemType`, `Status`, `StatusDisabled`, `TicketTemplate`) VALUES ( 1, 'Auto Invoice Paid', '{{InvoiceNumber}} Invoice Paid', '<p>Hi</p><p>We hereby confirm that we have received payment..</p><h4>Payment Detail</h4><p>Account Name : {{AccountName}}</p><p>Paid Amount : {{PaidAmount}}</p><p>Status : {{PaidStatus}}</p><p>Payment Method : {{PaymentMethod}}</p><p>Payment Notes : {{PaymentNotes}}</p><p><br></p><h4>Best Regards</h4><p>{{CompanyName}}<br></p><p><br></p>', '2017-07-28 11:12:25', NULL, '2017-07-28 16:02:36', 'System', NULL, 10, '', 1, 'AutoInvoicePayment', 0, 0, 0);
  
  
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'CALLSHOP_CRONJOB', '{"MaxInterval":"1440","CdrBehindDuration":"200","CdrBehindDurationEmail":"120","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"error@neon-soft.com","JobTime":"MINUTE","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_DOWNLOAD_CDR_CRONJOB', '{"MaxInterval":"1440","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"MINUTE","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_CUSTOMER_RATE_FILE_GEN_CRONJOB', '{"customers":[],"FileLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"DAILY","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":"","Effective":"Now"}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_VENDOR_RATE_FILE_GEN_CRONJOB', '{"vendors":[],"FileLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"DAILY","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":"","Effective":"Now","AddDiscontinueRates":"no"}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_RATE_FILE_DOWNLOAD_CRONJOB', '{"gateway":"","vendors":"","FilesDownloadLimit":"","FileLocationFrom":"","FileLocationTo":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"MINUTE","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_RATE_FILE_PROCESS_CRONJOB', '{"gateway":"","FilesMaxProcess":"","FilesDownloadLimit":"","FileLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"MINUTE","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'STREAMCO_ACCOUNT_IMPORT', '{"ThresholdTime":"60","SuccessEmail":"","ErrorEmail":"","JobTime":"MINUTE","JobInterval":"10","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'CUSTOMER_RATE_FILE_IMPORT_CRONJOB', '{"customers":[],"ScriptLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"DAILY","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'VENDOR_RATE_FILE_IMPORT_CRONJOB', '{"vendors":[],"ScriptLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"DAILY","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'RATE_FILE_EXPORT_CRONJOB', '{"ScriptLocation":"","ThresholdTime":"30","SuccessEmail":"","ErrorEmail":"","JobTime":"MINUTE","JobInterval":"1","JobDay":["SUN","MON","TUE","WED","THU","FRI","SAT"],"JobStartTime":"12:00:00 AM","CompanyGatewayID":""}');
  INSERT INTO `tblCompanyConfiguration` (`CompanyID`, `Key`, `Value`) VALUES ( 1, 'CUSTOMER_MOVEMENT_REPORT_DISPLAY', '0');
  
  INSERT INTO `tblGateway` (`GatewayID`, `Title`, `Name`, `Status`, `CreatedBy`, `created_at`, `ModifiedBy`, `updated_at`) VALUES (9, 'Locutorios', 'CallShop', 1, 'RateManagementSystem', '2017-07-14 00:00:00', NULL, NULL);
  INSERT INTO `tblGateway` (`GatewayID`, `Title`, `Name`, `Status`, `CreatedBy`, `created_at`, `ModifiedBy`, `updated_at`) VALUES (10, 'Streamco', 'Streamco', 1, 'RateManagementSystem', '2017-07-22 11:09:43', NULL, NULL);
  
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (88, 9, 'Locutorios Server', 'dbserver', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (89, 9, 'Locutorios Username', 'username', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (90, 9, 'Locutorios Password', 'password', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (91, 9, 'Authentication Rule', 'NameFormat', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (92, 9, 'CDR ReRate', 'RateCDR', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (93, 9, 'Rate Format', 'RateFormat', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (94, 9, 'CLI Translation Rule', 'CLITranslationRule', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (95, 9, 'CLD Translation Rule', 'CLDTranslationRule', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (96, 9, 'Prefix Translation Rule', 'PrefixTranslationRule', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (97, 9, 'Allow Account Import', 'AllowAccountImport', 1, '2017-07-14 00:00:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (100, 10, 'Host', 'host', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (101, 10, 'Database Username', 'dbusername', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (102, 10, 'Database Password', 'dbpassword', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (103, 10, 'SSH Host', 'sshhost', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (104, 10, 'SSH Username', 'sshusername', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (105, 10, 'SSH Password', 'sshpassword', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (106, 10, 'Authentication Rule', 'NameFormat', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (107, 10, 'Rate Format', 'RateFormat', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (108, 10, 'CDR ReRate', 'RateCDR', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (109, 10, 'CLI Translation Rule', 'CLITranslationRule', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (110, 10, 'CLD Translation Rule', 'CLDTranslationRule', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (111, 10, 'Prefix Translation Rule', 'PrefixTranslationRule', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  INSERT INTO `tblGatewayConfig` (`GatewayConfigID`, `GatewayID`, `Title`, `Name`, `Status`, `Created_at`, `CreatedBy`, `updated_at`, `ModifiedBy`) VALUES (112, 10, 'Allow Account Import', 'AllowAccountImport', 1, '2017-07-22 07:15:00', 'RateManagementSystem', NULL, NULL);
  
  INSERT INTO `tblCronJobCommand` ( `CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES ( 1, 9, 'Download Locutorios CDR', 'callshopaccountusage', '[[{"title":"Locutorios Max Interval","type":"text","value":"","name":"MaxInterval"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-14 00:00:00', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Vendor Rate File Process', 'vendorratefileprocess', '[[{"title":"Files Max Process","type":"text","value":"","name":"FilesMaxProcess"},{"title":"File Location (Path)","type":"text","value":"","name":"FileLocation"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Customer Rate File Process', 'customerratefileprocess', '[[{"title":"Files Max Process","type":"text","value":"","name":"FilesMaxProcess"},{"title":"File Location (Path)","type":"text","value":"","name":"FileLocation"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Vendor Rate File Download', 'vendorratefiledownload', '[[{"title":"Max File Download Limit","type":"text","value":"","name":"FilesDownloadLimit"},{"title":"File Download From Location (Path)","type":"text","value":"","name":"FileLocationFrom"},{"title":"File Download To Location (Path)","type":"text","value":"","name":"FileLocationTo"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Customer Rate File Download', 'customerratefiledownload', '[[{"title":"Max File Download Limit","type":"text","value":"","name":"FilesDownloadLimit"},{"title":"File Download From Location (Path)","type":"text","value":"","name":"FileLocationFrom"},{"title":"File Download To Location (Path)","type":"text","value":"","name":"FileLocationTo"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Vendor Rate File Export', 'vendorratefileexport', '[[{"title":"File Generation Location (Path)","type":"text","value":"","name":"FileLocation"},{"title":"Effective","type":"select","value":{"Now":"Now","Future":"Future","All":"All"},"name":"Effective"},{"title":"Add Discontinue Rates","type":"select","value":{"no":"No","yes":"Yes"},"name":"AddDiscontinueRates"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Customer Rate File Export', 'customerratefileexport', '[[{"title":"File Generation Location (Path)","type":"text","value":"","name":"FileLocation"},{"title":"Effective","type":"select","value":{"Now":"Now","Future":"Future","All":"All"},"name":"Effective"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2015-04-21 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Download Streamco CDR', 'streamcoaccountusage', '[[{"title":"STREMCO Max Interval","type":"text","value":"","name":"MaxInterval"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-22 08:22:13', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Streamco Account Import', 'streamcoaccountimport', '[[{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-27 15:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Customer Rate File Generation', 'customerratefilegeneration', '[[{"title":"Script Location (Path)","type":"text","value":"","name":"ScriptLocation"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-29 14:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Vendor Rate File Generation', 'vendorratefilegeneration', '[[{"title":"Script Location (Path)","type":"text","value":"","name":"ScriptLocation"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-29 14:11:06', 'RateManagementSystem');
  INSERT INTO `tblCronJobCommand` (`CompanyID`, `GatewayID`, `Title`, `Command`, `Settings`, `Status`, `created_at`, `created_by`) VALUES (1, 10, 'Rate File Export', 'ratefileexport', '[[{"title":"Script Location (Path)","type":"text","value":"","name":"ScriptLocation"},{"title":"Threshold Time (Minute)","type":"text","value":"","name":"ThresholdTime"},{"title":"Success Email","type":"text","value":"","name":"SuccessEmail"},{"title":"Error Email","type":"text","value":"","name":"ErrorEmail"}]]', 1, '2017-07-29 14:11:06', 'RateManagementSystem');
  
  INSERT INTO `tblJobType` (`Code`, `Title`, `Description`, `CreatedDate`, `CreatedBy`, `ModifiedDate`, `ModifiedBy`) VALUES ('IU', 'Item Upload', NULL, '2017-07-04 12:25:46', 'System', NULL, NULL);
  
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.upload', 'ProductsController.upload', 1, 'System', NULL, '2017-07-18 16:45:52.000', '2017-07-18 16:45:52.000', 53);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.check_upload', 'ProductsController.check_upload', 1, 'System', NULL, '2017-07-18 16:45:52.000', '2017-07-18 16:45:52.000', 53);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.ajaxfilegrid', 'ProductsController.ajaxfilegrid', 1, 'System', NULL, '2017-07-18 16:45:53.000', '2017-07-18 16:45:53.000', 53);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.storeTemplate', 'ProductsController.storeTemplate', 1, 'System', NULL, '2017-07-18 16:45:53.000', '2017-07-18 16:45:53.000', 53);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.getProductByBarCode', 'ProductsController.getProductByBarCode', 1, 'System', NULL, '2017-07-18 16:45:53.000', '2017-07-18 16:45:53.000', 36);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.getProductByBarCode', 'ProductsController.getProductByBarCode', 1, 'System', NULL, '2017-07-18 16:45:53.000', '2017-07-18 16:45:53.000', 41);
  INSERT INTO `tblResource` (`ResourceName`, `ResourceValue`, `CompanyID`, `CreatedBy`, `ModifiedBy`, `created_at`, `updated_at`, `CategoryID`) VALUES ('Products.download_sample_excel_file', 'ProductsController.download_sample_excel_file', 1, 'System', NULL, '2017-07-18 16:45:54.000', '2017-07-18 16:45:54.000', 53);
  
  USE `RMBilling3`;
  
  INSERT INTO tblInvoiceDetail (InvoiceID,ProductID,Description,StartDate,EndDate,Price,Qty,Discount,TaxAmount,LineTotal,CreatedBy,created_at,ProductType,ServiceID)
  SELECT InvoiceID,0,'Invoice Period',StartDate,EndDate,0,0,0,0,0,CreatedBy,created_at,5,ServiceID FROM tblInvoiceDetail WHERE ProductType = 2;
  