Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
461
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MySQL 323.77 KB | None | 0 0
  1. DELETE FROM mysql.proc WHERE mysql.proc.db='ecc_dev_maint';
  2. USE 'ecc_dev_maint';
  3.  
  4. -- MySQL dump 10.13  Distrib 5.5.15, for Win64 (x86)
  5. --
  6. -- Host: localhost    Database: ecc_dev
  7. -- ------------------------------------------------------
  8. -- Server version   5.5.15-log
  9.  
  10. /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
  11. /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
  12. /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
  13. /*!40101 SET NAMES utf8 */;
  14. /*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */;
  15. /*!40103 SET TIME_ZONE='+00:00' */;
  16. /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
  17. /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
  18. /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
  19. /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */;
  20.  
  21. --
  22. -- Dumping routines for database 'ecc_dev'
  23. --
  24. /*!50003 DROP FUNCTION IF EXISTS `MG_CALC_EPP_FREQUENCY` */;
  25. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  26. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  27. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  28. /*!50003 SET character_set_client  = utf8 */ ;
  29. /*!50003 SET character_set_results = utf8 */ ;
  30. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  31. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  32. /*!50003 SET sql_mode              = '' */ ;
  33. DELIMITER ;;
  34. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 FUNCTION `MG_CALC_EPP_FREQUENCY`(inp_frequency_unit TINYINT(1),inp_frequency TINYINT(3),inp_partner_id INT(10),inp_transaction_description VARCHAR(255),inp_customer_id INT(10),inp_dt_now DATETIME) RETURNS tinyint(1)
  35.     DETERMINISTIC
  36. BEGIN
  37.    
  38.     IF inp_frequency_unit = 1 THEN  
  39.         SELECT COUNT(1) INTO @cnt_trans
  40.             FROM partners_earn_transactions p
  41.             WHERE  p.partner_id = inp_partner_id
  42.                 AND p.description = inp_transaction_description
  43.                 AND  p.TYPE = 20 AND customer_id = inp_customer_id
  44.                 AND  p.TIME >= DATE_SUB(inp_dt_now,INTERVAL 24 HOUR)  ;
  45.    
  46.     END IF ;
  47.     IF inp_frequency_unit = 2 THEN  
  48.         SELECT COUNT(1) INTO @cnt_trans
  49.             FROM partners_earn_transactions p
  50.             WHERE  p.partner_id = inp_partner_id
  51.                 AND p.description = inp_transaction_description
  52.                 AND  p.TYPE = 20 AND customer_id = inp_customer_id
  53.                 AND  p.TIME >= DATE_SUB(inp_dt_now,INTERVAL 1 WEEK )  ;
  54.    
  55.     END IF ;
  56.    
  57.     IF inp_frequency_unit = 3 THEN  
  58.         SELECT COUNT(1) INTO @cnt_trans
  59.             FROM partners_earn_transactions p
  60.             WHERE  p.partner_id = inp_partner_id
  61.                 AND p.description = inp_transaction_description
  62.                 AND  p.TYPE = 20 AND customer_id = inp_customer_id
  63.                 AND  p.TIME >= DATE_SUB(inp_dt_now,INTERVAL 1 MONTH )  ;
  64.    
  65.     END IF ;
  66.    
  67.     IF inp_frequency_unit = 4 THEN  
  68.         SELECT COUNT(1) INTO @cnt_trans
  69.             FROM partners_earn_transactions p
  70.             WHERE  p.partner_id = inp_partner_id
  71.                 AND p.description = inp_transaction_description
  72.                 AND  p.TYPE = 20 AND customer_id = inp_customer_id
  73.                 ;
  74.    
  75.     END IF ;
  76.    
  77.    
  78.     IF inp_frequency > @cnt_trans THEN
  79.         SELECT 1 INTO @ret;
  80.     ELSE
  81.         SELECT 0 INTO @ret;
  82.     END IF;
  83.    
  84. RETURN @ret;
  85. END */;;
  86. DELIMITER ;
  87. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  88. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  89. /*!50003 SET character_set_results = @saved_cs_results */ ;
  90. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  91. /*!50003 DROP FUNCTION IF EXISTS `MG_GET_ZIP_START_WITH_ZIP` */;
  92. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  93. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  94. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  95. /*!50003 SET character_set_client  = utf8 */ ;
  96. /*!50003 SET character_set_results = utf8 */ ;
  97. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  98. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  99. /*!50003 SET sql_mode              = '' */ ;
  100. DELIMITER ;;
  101. /*!50003 CREATE*/ /*!50020 DEFINER=`magento`@`%`*/ /*!50003 FUNCTION `MG_GET_ZIP_START_WITH_ZIP`(inp_product_id INT(10),inp_zip VARCHAR(255) ) RETURNS tinyint(1)
  102.     DETERMINISTIC
  103. BEGIN
  104. --  UX-2997
  105. -- function takes parameters product_id and zip code and returns 1 if attribute 'start_with' for this product has value that like input Zip else return 0
  106. -- yfedorchuk 2011-08-26
  107.        
  108.     SELECT `value` INTO @zip_str  
  109.         FROM catalog_product_entity_varchar WHERE entity_id =  inp_product_id AND attribute_id =  (
  110.         SELECT attribute_id FROM eav_attribute WHERE attribute_code = 'starts_with')
  111.     ;  
  112.     SET @ret =0;
  113.     IF TRIM(@zip_str) <>0 THEN
  114.         SET @cnt = LENGTH(@zip_str)-LENGTH(REPLACE(@zip_str, ',',''));
  115.         SET @i=0;
  116.         w1:WHILE @i <= @cnt DO
  117.    
  118.             IF INSTR(@zip_str,',') >0 THEN
  119.                 SET @str_z = SUBSTRING(@zip_str,1,INSTR(@zip_str,',')-1);
  120.             ELSE
  121.                 SET @str_z = @zip_str;
  122.             END IF;    
  123.             IF TRIM(@str_z)<>'' AND inp_zip LIKE CONCAT(@str_z,'%') THEN
  124.                 SET @ret =1;
  125.                 LEAVE w1;
  126.             END IF;
  127.             SET @zip_str = SUBSTRING(@zip_str,INSTR(@zip_str,',')+1,LENGTH(@zip_str));
  128.             SET @i =@i+1;      
  129.         END WHILE ;
  130.     END IF;
  131. RETURN @ret;
  132. END */;;
  133. DELIMITER ;
  134. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  135. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  136. /*!50003 SET character_set_results = @saved_cs_results */ ;
  137. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  138. /*!50003 DROP FUNCTION IF EXISTS `MG_NY_TIME_TO_UTC` */;
  139. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  140. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  141. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  142. /*!50003 SET character_set_client  = utf8 */ ;
  143. /*!50003 SET character_set_results = utf8 */ ;
  144. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  145. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  146. /*!50003 SET sql_mode              = '' */ ;
  147. DELIMITER ;;
  148. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 FUNCTION `MG_NY_TIME_TO_UTC`(inp_dt DATETIME ) RETURNS datetime
  149.     DETERMINISTIC
  150. BEGIN
  151. SELECT DATE_FORMAT(inp_dt,'%Y-03-01'),DATE_FORMAT(inp_dt,'%Y-03-31') INTO @dstart,@dend;
  152. lbl_start:WHILE @dstart <= @dend
  153. DO
  154. SELECT DATE_ADD(@dstart, INTERVAL 1 DAY) INTO @dstart;
  155. SELECT CASE
  156.     WHEN
  157.         WEEK(@dstart,5) - WEEK(DATE(DATE_FORMAT(@dstart, '%Y-03-01')) ,5)+1 = 2
  158.         AND DAYOFWEEK(@dstart ) = 1
  159.     THEN 1
  160.     ELSE 0 END INTO @dst;
  161.  
  162.  IF @dst = 1 THEN
  163.     SET @start_date = DATE_FORMAT(@dstart,'%Y-03-%d 01:59:59');
  164.     leave lbl_start;
  165.  END IF;
  166. END WHILE;
  167. SELECT DATE_FORMAT(inp_dt,'%Y-11-01'),DATE_FORMAT(inp_dt,'%Y-11-30') INTO @dstart,@dend;
  168. lbl_end:WHILE @dstart <= @dend
  169. DO
  170. SELECT DATE_ADD(@dstart, INTERVAL 1 DAY) INTO @dstart;
  171. SELECT CASE
  172.     WHEN
  173.         WEEK(@dstart,5) - WEEK(DATE(DATE_FORMAT(@dstart, '%Y-11-01')) ,5)+1 = 1
  174.         AND DAYOFWEEK(@dstart ) = 1
  175.     THEN 2 
  176.     ELSE 0 END INTO @dst;
  177.  IF @dst = 2 THEN  
  178.     SET @end_date = DATE_FORMAT(@dstart,'%Y-%m-%d 01:59:59');
  179.     LEAVE lbl_end;
  180.  END IF;
  181. END WHILE;
  182. SELECT CASE
  183.       WHEN inp_dt <= @start_date OR inp_dt >= @end_date
  184.         THEN CONVERT_TZ(inp_dt,'-05:00','+00:00')
  185.       WHEN inp_dt >= @start_date AND inp_dt <= @end_date
  186.         THEN CONVERT_TZ(inp_dt,'-04:00','+00:00')
  187.         END INTO @ret;
  188. RETURN @ret;
  189. END */;;
  190. DELIMITER ;
  191. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  192. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  193. /*!50003 SET character_set_results = @saved_cs_results */ ;
  194. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  195. /*!50003 DROP FUNCTION IF EXISTS `MG_NY_TIME_TO_UTC_NEW` */;
  196. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  197. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  198. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  199. /*!50003 SET character_set_client  = utf8 */ ;
  200. /*!50003 SET character_set_results = utf8 */ ;
  201. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  202. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  203. /*!50003 SET sql_mode              = '' */ ;
  204. DELIMITER ;;
  205. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 FUNCTION `MG_NY_TIME_TO_UTC_NEW`(i_datetime DATETIME ) RETURNS datetime
  206. BEGIN
  207.    
  208.     DECLARE i_month INT(11) DEFAULT EXTRACT(MONTH FROM i_datetime);
  209.     DECLARE i_day INT(11) DEFAULT EXTRACT(DAY FROM i_datetime);
  210.    
  211.     DECLARE summer_date DATETIME DEFAULT DATE_FORMAT(i_datetime, '%Y-03-08 01:59:59');
  212.     DECLARE winter_date DATETIME DEFAULT DATE_FORMAT(i_datetime, '%Y-11-01 01:59:59');
  213.    
  214.     DECLARE use_summer_time TINYINT DEFAULT (i_month BETWEEN 4 AND 10 OR (i_month = 3 AND i_day > 14));
  215.    
  216.        
  217.     IF (i_month = 3 AND i_day BETWEEN 8 AND 14) THEN
  218.        
  219.        
  220.         IF (DAYOFWEEK(summer_date) <> 1) THEN
  221.             SET summer_date = DATE_ADD(summer_date, INTERVAL (8 - DAYOFWEEK(summer_date)) DAY);
  222.         END IF;
  223.        
  224.         SET use_summer_time = i_datetime > summer_date;
  225.    
  226.     ELSEIF (i_month = 11 AND i_day BETWEEN 1 AND 7) THEN
  227.        
  228.  
  229.         IF (DAYOFWEEK(winter_date) <> 1) THEN
  230.             SET winter_date = DATE_ADD(winter_date, INTERVAL (8 - DAYOFWEEK(winter_date)) DAY);
  231.         END IF;
  232.        
  233.         SET use_summer_time = i_datetime <= winter_date;   
  234.        
  235.     END IF;
  236.    
  237.     IF use_summer_time THEN
  238.         RETURN CONVERT_TZ(i_datetime, '-04:00', '+00:00');
  239.     ELSE
  240.         RETURN CONVERT_TZ(i_datetime, '-05:00', '+00:00');
  241.     END IF;
  242.    
  243. END */;;
  244. DELIMITER ;
  245. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  246. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  247. /*!50003 SET character_set_results = @saved_cs_results */ ;
  248. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  249. /*!50003 DROP FUNCTION IF EXISTS `updateCustomerBalanceByAccountNumber` */;
  250. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  251. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  252. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  253. /*!50003 SET character_set_client  = utf8 */ ;
  254. /*!50003 SET character_set_results = utf8 */ ;
  255. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  256. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  257. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  258. DELIMITER ;;
  259. /*!50003 CREATE*/ /*!50020 DEFINER=`magento`@`%`*/ /*!50003 FUNCTION `updateCustomerBalanceByAccountNumber`(accountNumber varchar(255), delta FLOAT, updatedAt DATETIME, reason INT, descriptionText varchar(255), transaktionsOldId INT ) RETURNS int(11)
  260. BEGIN
  261.     DECLARE resultCode INT DEFAULT 0;
  262.  
  263.     CALL MG_CUSTOMER_ADD_TRANSACTION(accountNumber, delta, updatedAt, reason, descriptionText, transaktionsOldId, @result_code);
  264.     SELECT @result_code INTO resultCode;
  265.  
  266.     RETURN resultCode;
  267. END */;;
  268. DELIMITER ;
  269. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  270. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  271. /*!50003 SET character_set_results = @saved_cs_results */ ;
  272. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  273. /*!50003 DROP PROCEDURE IF EXISTS `EARN_ALLOCATE_TRANSACTION` */;
  274. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  275. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  276. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  277. /*!50003 SET character_set_client  = utf8 */ ;
  278. /*!50003 SET character_set_results = utf8 */ ;
  279. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  280. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  281. /*!50003 SET sql_mode              = '' */ ;
  282. DELIMITER ;;
  283. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_ALLOCATE_TRANSACTION`(
  284.                         IN i_g_partner_id INT(10),
  285.                         IN i_account_number VARCHAR(255),
  286.                         IN i_partner_member_account_number VARCHAR(255),
  287.                         IN i_units DECIMAL(12,4),
  288.                         IN i_external_comment VARCHAR(255),
  289.                         IN i_partner_transaction_id VARCHAR(50),
  290.                         IN i_environmental_impact VARCHAR(255),
  291.                         IN i_internal_comment VARCHAR(255),
  292.                         IN i_created_at DATETIME,
  293.                         OUT o_tr_id INT(11),
  294.                         OUT o_reason VARCHAR(255)
  295. )
  296. L_RETURN:
  297. BEGIN
  298.    
  299.     DECLARE c_earn_api_channel_id INT(11) DEFAULT 320;
  300.    
  301.     DECLARE v_customer_id INT(10);
  302.     DECLARE v_partner_id INT(10);
  303.     DECLARE v_balance_id INT(10);
  304.     DECLARE v_balance_amount DECIMAL(12,4);
  305.     DECLARE v_partner_bal_amount DECIMAL(12,4);
  306.     DECLARE v_partner_name VARCHAR(255);
  307.     DECLARE v_tr_desc VARCHAR(255);
  308.    
  309.    
  310.     IF (i_units <= 0) THEN
  311.         SELECT 0, 'The amount of points must be greater than zero' INTO o_tr_id, o_reason;
  312.         LEAVE L_RETURN;
  313.     END IF;
  314.    
  315.    
  316.     SELECT p.id, p.name
  317.     INTO v_partner_id, v_partner_name
  318.     FROM partners_earn per
  319.         INNER JOIN partners_entity p
  320.             ON per.partner_id = p.id
  321.     WHERE per.g_partner_id = i_g_partner_id;
  322.    
  323.    
  324.     IF i_partner_transaction_id IS NOT NULL AND
  325.         EXISTS
  326.         (SELECT 1
  327.          FROM partners_earn_transactions pet
  328.          WHERE pet.partner_id = v_partner_id
  329.             AND pet.external_partner_transaction_id = i_partner_transaction_id
  330.          )
  331.     THEN
  332.         SELECT 0, 'The partner_transaction_id already exists for partner' INTO o_tr_id, o_reason;
  333.         LEAVE L_RETURN;
  334.     END IF;
  335.    
  336.    
  337.     IF NOT EXISTS
  338.         (SELECT 1
  339.          FROM customer_entity ce
  340.          WHERE ce.account_number = i_account_number
  341.             AND ce.is_active = 1
  342.          )
  343.     THEN   
  344.         SELECT 0, 'Account not found' INTO o_tr_id, o_reason;  
  345.         LEAVE L_RETURN;
  346.     END IF;
  347.    
  348.    
  349.     IF NOT EXISTS
  350.         (SELECT 1
  351.          FROM partners_entity p
  352.             INNER JOIN partners_earn pe
  353.                 ON p.id = pe.partner_id AND pe.g_partner_id = i_g_partner_id
  354.             INNER JOIN partner_subscription ps
  355.                 ON pe.partner_id = ps.partner_id
  356.                     AND ps.start_date IS NOT NULL
  357.                     AND ps.end_date IS NULL
  358.             INNER JOIN customer_entity ce
  359.                 ON ps.customer_id = ce.entity_id
  360.                     AND ce.account_number = i_account_number
  361.          WHERE
  362.             CASE  
  363.                 WHEN i_partner_member_account_number IS NOT NULL
  364.                     THEN ps.member_account_number = i_partner_member_account_number
  365.                 ELSE 1=1
  366.             END
  367.         )
  368.    
  369.     THEN
  370.         SELECT 0, 'The customer is not subscribed with this partner' INTO o_tr_id, o_reason;
  371.         LEAVE L_RETURN;
  372.     END IF;
  373.    
  374.    
  375.     IF EXISTS
  376.         (SELECT 1
  377.          FROM partners_earn pe
  378.          WHERE pe.g_partner_id = i_g_partner_id
  379.             AND
  380.                 CASE
  381.                     WHEN pe.has_strict_zero = 0 OR pe.points - i_units > 0 THEN 1=1
  382.                     ELSE 1=0
  383.                 END
  384.         )
  385.        
  386.     THEN
  387.        
  388.        
  389.         SELECT ce.entity_id
  390.         INTO v_customer_id
  391.         FROM customer_entity ce
  392.         WHERE ce.account_number = i_account_number
  393.             AND ce.is_active = 1;
  394.            
  395.         IF EXISTS
  396.             (SELECT 1
  397.              FROM enterprise_customerbalance ecb
  398.              WHERE ecb.customer_id = v_customer_id)
  399.         THEN
  400.    
  401.            
  402.             SELECT ecb.balance_id, ecb.amount
  403.             INTO v_balance_id, v_balance_amount
  404.             FROM enterprise_customerbalance ecb
  405.             WHERE ecb.customer_id = v_customer_id;
  406.        
  407.         ELSE
  408.            
  409.             SET v_balance_amount = 0;
  410.            
  411.             INSERT INTO enterprise_customerbalance (customer_id, website_id, amount)
  412.             VALUES (v_customer_id, 1, v_balance_amount);
  413.            
  414.             SET v_balance_id = LAST_INSERT_ID();
  415.        
  416.         END IF;
  417.        
  418.         SET o_tr_id = 0;
  419.         SET v_tr_desc = CONCAT(v_partner_name, ': ', i_external_comment);
  420.        
  421.         START TRANSACTION;
  422.        
  423.         BEGIN      
  424.             DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
  425.    
  426.            
  427.            
  428.             INSERT INTO enterprise_customerbalance_history(balance_id, updated_at, `action`, balance_amount, balance_delta, additional_info, reason_id, description)
  429.             VALUES (v_balance_id, now(), 1, v_balance_amount + i_units, i_units, i_environmental_impact, 21, v_tr_desc);
  430.                
  431.             SET o_tr_id = LAST_INSERT_ID();
  432.                
  433.            
  434.             INSERT INTO enterprise_customerbalance_history_channel_map(history_id, channel_id) VALUES (o_tr_id, c_earn_api_channel_id);
  435.                
  436.             UPDATE enterprise_customerbalance ecb
  437.             SET ecb.amount = ecb.amount + i_units
  438.             WHERE ecb.balance_id = v_balance_id;
  439.                
  440.            
  441.             SELECT pe.points
  442.             INTO v_partner_bal_amount
  443.             FROM partners_earn pe
  444.             WHERE pe.partner_id = v_partner_id;
  445.                
  446.            
  447.            
  448.             INSERT INTO partners_earn_transactions (partner_id, `type`, `time`, points, balance_amount, customer_id, external_partner_transaction_id, customer_balance_history_id, description)
  449.             VALUES (v_partner_id, 20, now(), i_units, v_partner_bal_amount - i_units, v_customer_id, i_partner_transaction_id, o_tr_id, i_external_comment);
  450.                
  451.            
  452.             UPDATE partners_earn pe
  453.             SET pe.points = pe.points - i_units
  454.             WHERE pe.partner_id = v_partner_id;
  455.                
  456.            
  457.             INSERT INTO enterprise_customerbalance_history_ext_partner(history_id, partner_id)
  458.             VALUES (o_tr_id, v_partner_id);
  459.                
  460.        
  461.         END;
  462.        
  463.         COMMIT;
  464.            
  465.     ELSE
  466.         SELECT 0, 'Partner balance amount is not sufficient to create transaction' INTO o_tr_id, o_reason;
  467.     END IF;
  468.  
  469. END */;;
  470. DELIMITER ;
  471. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  472. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  473. /*!50003 SET character_set_results = @saved_cs_results */ ;
  474. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  475. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CHECK_ACCOUNT_BY_EMAIL` */;
  476. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  477. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  478. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  479. /*!50003 SET character_set_client  = utf8 */ ;
  480. /*!50003 SET character_set_results = utf8 */ ;
  481. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  482. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  483. /*!50003 SET sql_mode              = '' */ ;
  484. DELIMITER ;;
  485. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CHECK_ACCOUNT_BY_EMAIL`(
  486.                         IN i_email VARCHAR(255),
  487.                         IN i_only_active TINYINT(1),
  488.                         OUT o_success TINYINT(1))
  489. BEGIN
  490.  
  491.     SET o_success = EXISTS (
  492.                         SELECT 1
  493.                         FROM customer_entity ce
  494.                         WHERE ce.email = i_email
  495.                             AND
  496.                                 CASE
  497.                                     WHEN i_only_active
  498.                                         THEN ce.is_active = 1
  499.                                     ELSE 1=1
  500.                                 END
  501.                     );
  502.  
  503. END */;;
  504. DELIMITER ;
  505. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  506. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  507. /*!50003 SET character_set_results = @saved_cs_results */ ;
  508. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  509. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CHECK_ACCOUNT_BY_NUMBER` */;
  510. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  511. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  512. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  513. /*!50003 SET character_set_client  = utf8 */ ;
  514. /*!50003 SET character_set_results = utf8 */ ;
  515. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  516. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  517. /*!50003 SET sql_mode              = '' */ ;
  518. DELIMITER ;;
  519. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CHECK_ACCOUNT_BY_NUMBER`(
  520.                         IN i_account_number VARCHAR(255),
  521.                         IN i_only_active TINYINT(1),
  522.                         OUT o_success TINYINT(1))
  523. BEGIN
  524.  
  525.     SET o_success = EXISTS (
  526.                         SELECT 1
  527.                         FROM customer_entity ce
  528.                         WHERE ce.account_number = i_account_number
  529.                             AND
  530.                                 CASE
  531.                                     WHEN i_only_active
  532.                                         THEN ce.is_active = 1
  533.                                     ELSE 1=1
  534.                                 END
  535.                     );
  536.  
  537. END */;;
  538. DELIMITER ;
  539. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  540. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  541. /*!50003 SET character_set_results = @saved_cs_results */ ;
  542. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  543. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CHECK_SUBSCRIPTION` */;
  544. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  545. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  546. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  547. /*!50003 SET character_set_client  = utf8 */ ;
  548. /*!50003 SET character_set_results = utf8 */ ;
  549. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  550. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  551. /*!50003 SET sql_mode              = '' */ ;
  552. DELIMITER ;;
  553. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CHECK_SUBSCRIPTION`(
  554.                         IN i_g_partner_id INT(10),
  555.                         IN i_account_number VARCHAR(255),
  556.                         OUT o_success TINYINT(1))
  557. BEGIN
  558.    
  559.    
  560.     SET o_success = EXISTS (SELECT 1
  561.    
  562.                             FROM partners_entity p
  563.                            
  564.                                
  565.                                 INNER JOIN partners_earn pe
  566.                                     ON p.id = pe.partner_id AND pe.g_partner_id = i_g_partner_id
  567.                                    
  568.                                
  569.                                 INNER JOIN partner_subscription ps
  570.                                     ON pe.partner_id = ps.partner_id
  571.                                         AND ps.start_date IS NOT NULL
  572.                                         AND ps.end_date IS NULL
  573.                                        
  574.                                
  575.                                 INNER JOIN customer_entity ce
  576.                                     ON ps.customer_id = ce.entity_id
  577.                                     AND ce.account_number = i_account_number
  578.                
  579.                             );
  580.                                        
  581. END */;;
  582. DELIMITER ;
  583. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  584. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  585. /*!50003 SET character_set_results = @saved_cs_results */ ;
  586. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  587. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CREATE_ACCOUNT` */;
  588. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  589. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  590. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  591. /*!50003 SET character_set_client  = utf8 */ ;
  592. /*!50003 SET character_set_results = utf8 */ ;
  593. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  594. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  595. /*!50003 SET sql_mode              = '' */ ;
  596. DELIMITER ;;
  597. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CREATE_ACCOUNT`(
  598.                     IN i_first_name VARCHAR(255),
  599.                     IN i_last_name VARCHAR(255),
  600.                     IN i_email VARCHAR(255),
  601.                     IN i_postal_code VARCHAR(255),
  602.                     IN i_agreed_to_tos TINYINT(1),
  603.                     IN i_partner_member_account_number VARCHAR(255),
  604.                     IN i_account_number VARCHAR(255),
  605.                     IN i_status TINYINT(1),
  606.                     IN i_gender TINYINT(1),
  607.                     IN i_is_parent TINYINT(1),
  608.                     IN i_is_home_owner TINYINT(1),
  609.                     IN i_date_of_birth DATE,
  610.                     IN i_g_partner_id INT(10),
  611.                     OUT o_customer_id INT(11),
  612.                     OUT o_reason VARCHAR(255))
  613. BEGIN
  614.  
  615.     DECLARE c_earn_api_channel_id INT(11) DEFAULT 320;
  616.        
  617.     DECLARE v_new_entity_id INT(10) DEFAULT 0;
  618.     DECLARE v_new_addr_entity_id INT(10);
  619.     DECLARE v_ivalue VARCHAR(255);
  620.     DECLARE v_str VARCHAR(1000);
  621.     DECLARE v_ins_rows INT(10);
  622.     DECLARE v_email_already_exist TINYINT(1);
  623.    
  624.    
  625.     CALL EARN_CHECK_ACCOUNT_BY_EMAIL(i_email, 0, v_email_already_exist);
  626.    
  627.     IF (v_email_already_exist) THEN
  628.         SET o_reason = 'email has already been taken';
  629.         SET o_customer_id = 0;
  630.     ELSE
  631.    
  632.        
  633.         IF EXISTS
  634.             (SELECT 1
  635.              FROM partners_earn pe
  636.                 INNER JOIN earns_customers_partners ecp
  637.                     ON pe.partner_id = ecp.partner_id
  638.              WHERE pe.g_partner_id = i_g_partner_id
  639.                 AND ecp.partner_member_account_number = i_partner_member_account_number
  640.             )
  641.         THEN
  642.             SET o_reason = 'partner member account number is already used';
  643.             SET o_customer_id = 0;
  644.        
  645.         ELSE
  646.    
  647.             START TRANSACTION;
  648.        
  649.             INSERT INTO `customer_entity`
  650.             (`entity_type_id`,
  651.              `website_id`,
  652.              `group_id`,
  653.              `email`,
  654.              `store_id`,
  655.              `created_at`,
  656.              `updated_at`,
  657.              `is_active`,
  658.              `account_number`)
  659.             SELECT
  660.                 1 AS entity_type_id,
  661.                 1 AS website_id,
  662.                 1 AS group_id,
  663.                 i_email AS email,
  664.                 1 AS store_id,
  665.                 NOW(),
  666.                 NOW(),
  667.                 i_status AS is_active,
  668.                 i_account_number;
  669.        
  670.             SET v_new_entity_id = LAST_INSERT_ID();
  671.    
  672.             SELECT ROW_COUNT() INTO v_ins_rows;
  673.    
  674.             IF v_ins_rows > 0 THEN
  675.    
  676.                 SET v_str = '';
  677.                 SET @entity_type_id = 1;
  678.                 SET @attribute_code = '';
  679.                 SET @attribute_id = 0;
  680.                 SET @backend_type = '';
  681.    
  682.                 WHILE
  683.                     EXISTS (
  684.                         SELECT attribute_id
  685.                         FROM eav_attribute
  686.                         WHERE attribute_id  > @attribute_id
  687.                             AND entity_type_id = @entity_type_id
  688.                             AND
  689.                                 CASE
  690.                                     WHEN (ISNULL(i_gender) AND ISNULL(i_date_of_birth))
  691.                                         THEN attribute_code IN ('firstname', 'lastname')
  692.                                     WHEN (ISNULL(i_gender) AND ISNULL(i_date_of_birth) = 0)
  693.                                         THEN attribute_code IN ('firstname', 'lastname', 'dob')
  694.                                     WHEN (ISNULL(i_gender) = 0 AND ISNULL(i_date_of_birth))
  695.                                         THEN attribute_code IN ('firstname', 'lastname', 'gender')
  696.                                     ELSE  
  697.                                         attribute_code IN ('firstname', 'lastname', 'dob', 'gender')
  698.                                 END
  699.                         ORDER BY attribute_code ASC
  700.                     )
  701.                 DO
  702.                     SELECT attribute_code, attribute_id, backend_type
  703.                     INTO @attribute_code, @attribute_id, @backend_type
  704.                     FROM eav_attribute
  705.                     WHERE attribute_id > @attribute_id
  706.                         AND entity_type_id = @entity_type_id
  707.                         AND
  708.                             CASE
  709.                                 WHEN (ISNULL(i_gender) AND ISNULL(i_date_of_birth))
  710.                                     THEN attribute_code IN ('firstname', 'lastname')
  711.                                 WHEN (ISNULL(i_gender) AND ISNULL(i_date_of_birth) = 0  )
  712.                                     THEN attribute_code IN ('firstname', 'lastname', 'dob')
  713.                                 WHEN (ISNULL(i_gender) = 0 AND ISNULL(i_date_of_birth))
  714.                                     THEN attribute_code IN ('firstname', 'lastname', 'gender')
  715.                                 ELSE attribute_code IN ('firstname', 'lastname', 'dob', 'gender')
  716.                             END
  717.                     ORDER BY attribute_id ASC
  718.                     LIMIT 1;
  719.                    
  720.                     SET v_ivalue =
  721.                         CASE
  722.                             WHEN @attribute_code = 'firstname' THEN CONCAT('''', i_first_name, '''')
  723.                             WHEN @attribute_code = 'lastname' THEN CONCAT('''', i_last_name, '''')
  724.                             WHEN @attribute_code = 'dob' THEN CONCAT('date(''', i_date_of_birth, ''')')
  725.                             WHEN @attribute_code = 'gender' THEN i_gender
  726.                         END;
  727.                        
  728.                     SET @qwr = concat(
  729.                                 'INSERT INTO customer_entity_',
  730.                                 @backend_type,
  731.                                 '(entity_type_id, attribute_id, entity_id, value)
  732.                                 SELECT ',
  733.                                 @entity_type_id,
  734.                                 ', ',
  735.                                 @attribute_id,
  736.                                 ', ',
  737.                                 v_new_entity_id,
  738.                                 ', '
  739.                                 ,v_ivalue,
  740.                                 ';'
  741.                             );
  742.                            
  743.                     PREPARE stmt FROM @qwr;
  744.                     EXECUTE stmt;
  745.                    
  746.                     SET v_str = CONCAT(v_str, '|', v_ivalue);
  747.                 END WHILE;
  748.                
  749.                
  750.                 IF ISNULL(i_gender) = 0 THEN
  751.                     INSERT INTO customer_personal(customer_id, customer_gender, customer_age_range)
  752.                     VALUES (v_new_entity_id, i_gender, COALESCE(i_date_of_birth, NOW()));
  753.                 END IF;
  754.    
  755.                 SET @entity_type_id = 2;
  756.    
  757.                 INSERT INTO customer_address_entity (entity_type_id, attribute_set_id, parent_id, created_at, updated_at, is_active)
  758.                 VALUES (@entity_type_id, 0, v_new_entity_id, NOW(), NOW(), i_status);
  759.      
  760.                 SET v_new_addr_entity_id = LAST_INSERT_ID();
  761.    
  762.                 INSERT INTO customer_address_entity_varchar (entity_type_id, attribute_id, entity_id, value)
  763.                 VALUES (@entity_type_id, 29, v_new_addr_entity_id, i_postal_code);
  764.    
  765.                
  766.                 INSERT INTO customer_entity_int(entity_type_id, attribute_id, entity_id, value)
  767.                 VALUES (1, 13, v_new_entity_id, v_new_addr_entity_id), (1, 14, v_new_entity_id, v_new_addr_entity_id);
  768.      
  769.                
  770.                 INSERT INTO customer_entity_int(attribute_id, value, entity_type_id, entity_id)
  771.                 SELECT ea.attribute_id, c_earn_api_channel_id, ea.entity_type_id, v_new_entity_id
  772.                 FROM eav_attribute ea
  773.                 WHERE ea.attribute_code = 'channel_id';
  774.    
  775.    
  776.             END IF;
  777.    
  778.             SELECT v_new_entity_id, '' INTO o_customer_id, o_reason;
  779.    
  780.             COMMIT;
  781.    
  782.         END IF;
  783.    
  784.     END IF;
  785.  
  786. END */;;
  787. DELIMITER ;
  788. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  789. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  790. /*!50003 SET character_set_results = @saved_cs_results */ ;
  791. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  792. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CREATE_SESSION` */;
  793. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  794. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  795. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  796. /*!50003 SET character_set_client  = utf8 */ ;
  797. /*!50003 SET character_set_results = utf8 */ ;
  798. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  799. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  800. /*!50003 SET sql_mode              = '' */ ;
  801. DELIMITER ;;
  802. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CREATE_SESSION`(
  803.                     ipartner_id int(10),
  804.                     i_accountNumber varchar(255),
  805.                     i_email varchar(255),
  806.                     i_landingUrl varchar(255),
  807.                     out o_accountNumber varchar(255),
  808.                     out o_securityToken varchar(32),
  809.                     out o_reason varchar(255))
  810. BEGIN
  811.  
  812. DECLARE accountNumber VARCHAR(255) DEFAULT '';
  813. DECLARE success TINYINT(1);
  814.  
  815.  
  816. IF i_accountNumber IS NULL
  817. THEN
  818.  
  819.   IF i_email IS NULL
  820.   THEN
  821.     SET accountNumber = '';
  822.   ELSE
  823.     BEGIN
  824.  
  825.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET accountNumber = '';
  826.  
  827.       SELECT account_number
  828.       FROM customer_entity
  829.       WHERE email = i_email
  830.       INTO accountNumber;
  831.  
  832.     END;
  833.  
  834.   END IF;
  835.  
  836. ELSE
  837.  
  838.   IF NOT EXISTS (SELECT 1 FROM customer_entity WHERE account_number = i_accountNumber)
  839.   THEN
  840.     SET o_reason = 'Account not found';
  841.   ELSE
  842.     SET accountNumber = i_accountNumber;
  843.   END IF;
  844.  
  845. END IF;
  846.  
  847.  
  848. IF accountNumber <> ''
  849. THEN
  850.  
  851.     call EARN_CHECK_SUBSCRIPTION(ipartner_id, accountNumber, success);
  852.  
  853.     IF success
  854.     THEN
  855.  
  856.       BEGIN
  857.    
  858.         DECLARE securityToken VARCHAR(255);
  859.         SET securityToken = REPLACE(UUID(), '-', '');
  860.    
  861.         INSERT INTO direct_login(account_no, login_guid, expiration_datetime, url)
  862.                VALUES(accountNumber, securityToken, ADDDATE(NOW(), INTERVAL 30 MINUTE), i_landingUrl);
  863.    
  864.         SELECT accountNumber, securityToken INTO o_accountNumber, o_securityToken;
  865.    
  866.       END;
  867.      
  868.     ELSE
  869.    
  870.         SET o_reason = 'Account is not subscribed to partner';
  871.        
  872.     END IF;
  873.  
  874. END IF;
  875.  
  876. END */;;
  877. DELIMITER ;
  878. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  879. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  880. /*!50003 SET character_set_results = @saved_cs_results */ ;
  881. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  882. /*!50003 DROP PROCEDURE IF EXISTS `EARN_CREATE_SUBSCRIPTION` */;
  883. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  884. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  885. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  886. /*!50003 SET character_set_client  = utf8 */ ;
  887. /*!50003 SET character_set_results = utf8 */ ;
  888. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  889. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  890. /*!50003 SET sql_mode              = '' */ ;
  891. DELIMITER ;;
  892. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_CREATE_SUBSCRIPTION`(IN i_g_partner_id INT(10),
  893.                                             IN i_account_number VARCHAR(255),
  894.                                             IN i_email VARCHAR(255),
  895.                                             IN i_partner_member_account_number VARCHAR(255),
  896.                                             OUT o_subs_id INT(11),
  897.                                             OUT o_reason VARCHAR(255))
  898. BEGIN
  899.    
  900.     DECLARE c_earn_other_product_type_id VARCHAR(32) DEFAULT 'earn_other';
  901.    
  902.     DECLARE v_customer_id INT(10);
  903.     DECLARE v_partner_id INT(10);
  904.     DECLARE v_product_id INT(10);
  905.     DECLARE v_customer_earn_id INT(10);
  906.     DECLARE v_result TINYINT(1);
  907.    
  908.     BEGIN
  909.         DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_customer_id = -1;
  910.        
  911.        
  912.         IF (i_account_number IS NOT NULL) THEN
  913.             SELECT ce.entity_id
  914.             INTO v_customer_id
  915.             FROM customer_entity ce
  916.             WHERE ce.account_number = i_account_number
  917.                 AND ce.is_active = 1;
  918.                
  919.        
  920.         ELSEIF (i_email IS NOT NULL) THEN
  921.                 SELECT ce.entity_id
  922.                 INTO v_customer_id
  923.                 FROM customer_entity ce
  924.                 WHERE ce.email = i_email
  925.                     AND ce.is_active = 1;
  926.                    
  927.         ELSE
  928.             SET v_customer_id = -1;
  929.             SELECT 0, 'Please pass account_number or email' INTO o_subs_id, o_reason;
  930.            
  931.         END IF;
  932.    
  933.     END;
  934.    
  935.    
  936.     IF (v_customer_id = -1) THEN
  937.         SELECT 0, 'Customer does not exist' INTO o_subs_id, o_reason;
  938.     ELSE
  939.        
  940.         SELECT pe.id
  941.         INTO v_partner_id
  942.         FROM partners_entity pe
  943.             INNER JOIN partners_earn per
  944.                 ON pe.id = per.partner_id
  945.         WHERE per.g_partner_id = i_g_partner_id;
  946.        
  947.        
  948.         IF ISNULL(v_partner_id) THEN
  949.             SELECT 0, 'Partner does not exists' INTO o_subs_id, o_reason;
  950.            
  951.         ELSE
  952.            
  953.             IF EXISTS
  954.                 (SELECT 1
  955.                  FROM partner_subscription ps
  956.                  WHERE ps.partner_id = v_partner_id
  957.                     AND ps.member_account_number = i_partner_member_account_number
  958.                     AND ps.customer_id <> v_customer_id
  959.                 )
  960.        
  961.             THEN
  962.                 SELECT 0, 'Partner member account number is already used' INTO o_subs_id, o_reason;
  963.            
  964.             ELSE
  965.                
  966.                 CALL EARN_CHECK_SUBSCRIPTION(i_g_partner_id, i_account_number, v_result);
  967.                
  968.                 IF (v_result) THEN
  969.                     SELECT 0, 'Customer alredy subscribed to partner' INTO o_subs_id, o_reason;
  970.                 ELSE
  971.                
  972.                     START TRANSACTION;
  973.                     BEGIN      
  974.                         DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
  975.                        
  976.                        
  977.                         INSERT INTO partner_subscription(partner_id, member_account_number, customer_id, start_date, end_date)
  978.                         VALUES(v_partner_id, i_partner_member_account_number, v_customer_id, NOW(), NULL);
  979.                        
  980.                         SET o_subs_id = LAST_INSERT_ID();
  981.                        
  982.                        
  983.                         BEGIN
  984.                            
  985.                             DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_product_id = -1;
  986.                            
  987.                             SELECT cpe.entity_id
  988.                             INTO v_product_id
  989.                             FROM catalog_product_entity cpe
  990.                             WHERE cpe.partner_id = v_partner_id
  991.                                 AND cpe.type_id = c_earn_other_product_type_id
  992.                             LIMIT 1;
  993.                            
  994.                         END;
  995.                        
  996.                        
  997.                         IF (v_product_id <> -1) THEN
  998.                            
  999.                            
  1000.                            
  1001.                             INSERT INTO earns_customers (customer_id, product_id, start_date)
  1002.                             VALUES (v_customer_id, v_product_id, NOW());
  1003.                        
  1004.                             SET v_customer_earn_id = LAST_INSERT_ID();
  1005.                        
  1006.                            
  1007.                             INSERT INTO earns_customers_partners (earns_customers_id, partner_id, partner_member_account_number)
  1008.                             VALUES (v_customer_earn_id, v_partner_id, i_partner_member_account_number);
  1009.                                                        
  1010.                         END IF;
  1011.  
  1012.                     END;   
  1013.                    
  1014.                     COMMIT;
  1015.                    
  1016.                 END IF;
  1017.                
  1018.             END IF;
  1019.            
  1020.         END IF;        
  1021.        
  1022.     END IF;
  1023.  
  1024. END */;;
  1025. DELIMITER ;
  1026. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1027. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1028. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1029. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1030. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GENERATE_CONFIRMATION` */;
  1031. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1032. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1033. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1034. /*!50003 SET character_set_client  = utf8 */ ;
  1035. /*!50003 SET character_set_results = utf8 */ ;
  1036. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1037. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1038. /*!50003 SET sql_mode              = '' */ ;
  1039. DELIMITER ;;
  1040. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GENERATE_CONFIRMATION`(IN iaccount_number varchar(255),
  1041.                                               IN i_key varchar(255),
  1042.                                               OUT o_key varchar(255))
  1043. BEGIN
  1044. declare icustomer_id int(10);
  1045. declare id_confirmation int(10);
  1046.  
  1047. SET o_key = '';
  1048.  
  1049. SELECT uf_ret_attribute_id(1,'confirmation') into id_confirmation;
  1050. IF EXISTS (SELECT 1 FROM customer_entity WHERE account_number = iaccount_number)
  1051.  THEN
  1052.       SELECT entity_id INTO icustomer_id
  1053.         FROM customer_entity ce WHERE account_number = iaccount_number;
  1054.     IF not Exists (SELECT 1 FROM customer_entity_varchar
  1055.         WHERE entity_id = icustomer_id
  1056.             AND attribute_id = id_confirmation)
  1057.     THEN
  1058.         INSERT INTO customer_entity_varchar (entity_type_id,attribute_id,entity_id,`value`)
  1059.             SELECT 1, id_confirmation ,icustomer_id ,concat(icustomer_id, i_key);
  1060.         SELECT concat(icustomer_id, i_key) INTO o_key;
  1061.     END IF;
  1062. END IF;
  1063. END */;;
  1064. DELIMITER ;
  1065. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1066. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1067. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1068. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1069. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_ACCOUNT_BY_EMAIL` */;
  1070. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1071. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1072. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1073. /*!50003 SET character_set_client  = utf8 */ ;
  1074. /*!50003 SET character_set_results = utf8 */ ;
  1075. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1076. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1077. /*!50003 SET sql_mode              = '' */ ;
  1078. DELIMITER ;;
  1079. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_ACCOUNT_BY_EMAIL`(i_email VARCHAR(255))
  1080. BEGIN
  1081.  
  1082.     DECLARE delim CHAR(1) DEFAULT '\n';
  1083.    
  1084.     SELECT
  1085.         ce.account_number AS 'account_number',
  1086.         cev_firstname.value AS 'first_name',
  1087.         cev_lastname.value AS 'last_name',
  1088.         cev_hash_password.value AS 'hash_password',
  1089.         ce.email AS 'email',
  1090.         ce.is_active AS 'status',
  1091.         ecb.amount AS 'point_balance',
  1092.         cp.customer_gender AS 'gender',
  1093.         cp.customer_age_range AS 'date_of_birth',
  1094.         SUBSTRING_INDEX(caet_street.value, delim, 1) AS 'street1',
  1095.         SUBSTRING_INDEX(SUBSTRING(caet_street.value, LENGTH(SUBSTRING_INDEX(caet_street.value, delim, 1)) + LENGTH(delim) + 1), delim, 1) AS 'street2',
  1096.         SUBSTRING_INDEX(SUBSTRING(caet_street.value, LENGTH(SUBSTRING_INDEX(caet_street.value, delim, 2)) + LENGTH(delim) + 1), delim, 1) AS 'street3',
  1097.         caev_city.value AS 'city',
  1098.         caev_region.value AS 'state_county',
  1099.         caev_postcode.value AS 'postal_code',
  1100.         caev_country.value AS 'country',
  1101.         eu.start_date AS 'start_date'
  1102.     FROM customer_entity ce
  1103.         LEFT JOIN enterprise_customerbalance ecb
  1104.             ON ce.entity_id = ecb.customer_id
  1105.         LEFT JOIN customer_entity_varchar cev_firstname
  1106.             ON ce.entity_id = cev_firstname.entity_id AND cev_firstname.attribute_id = 5
  1107.         LEFT JOIN customer_entity_varchar cev_lastname
  1108.             ON ce.entity_id = cev_lastname.entity_id AND cev_lastname.attribute_id = 7
  1109.         LEFT JOIN customer_entity_varchar cev_hash_password
  1110.             ON ce.entity_id = cev_hash_password.entity_id AND cev_hash_password.attribute_id = 12
  1111.         LEFT JOIN customer_personal cp
  1112.             ON cp.customer_id = ce.entity_id
  1113.         LEFT JOIN customer_address_entity cae
  1114.             ON ce.entity_id = cae.parent_id
  1115.         LEFT JOIN customer_address_entity_text caet_street
  1116.             ON cae.entity_id = caet_street.entity_id AND caet_street.attribute_id = 24
  1117.         LEFT JOIN customer_address_entity_varchar caev_city
  1118.             ON cae.entity_id = caev_city.entity_id AND caev_city.attribute_id = 25
  1119.         LEFT JOIN customer_address_entity_varchar caev_region
  1120.             ON cae.entity_id = caev_region.entity_id AND caev_region.attribute_id = 27
  1121.         LEFT JOIN customer_address_entity_varchar caev_postcode
  1122.             ON cae.entity_id = caev_postcode.entity_id AND caev_postcode.attribute_id = 29
  1123.         LEFT JOIN customer_address_entity_varchar caev_country
  1124.             ON cae.entity_id = caev_country.entity_id AND caev_country.attribute_id = 26
  1125.         LEFT JOIN earns_users eu
  1126.             ON ce.entity_id = eu.user_id
  1127.   WHERE ce.email = i_email AND ce.is_active = 1;
  1128.  
  1129. END */;;
  1130. DELIMITER ;
  1131. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1132. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1133. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1134. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1135. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_ACCOUNT_BY_NUMBER` */;
  1136. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1137. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1138. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1139. /*!50003 SET character_set_client  = utf8 */ ;
  1140. /*!50003 SET character_set_results = utf8 */ ;
  1141. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1142. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1143. /*!50003 SET sql_mode              = '' */ ;
  1144. DELIMITER ;;
  1145. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_ACCOUNT_BY_NUMBER`(i_account_number VARCHAR(255))
  1146. BEGIN
  1147.  
  1148.     DECLARE delim CHAR(1) DEFAULT '\n';
  1149.    
  1150.     SELECT
  1151.         ce.account_number AS 'account_number',
  1152.         cev_firstname.value AS 'first_name',
  1153.         cev_lastname.value AS 'last_name',
  1154.         cev_hash_password.value AS 'hash_password',
  1155.         ce.email AS 'email',
  1156.         ce.is_active AS 'status',
  1157.         ecb.amount AS 'point_balance',
  1158.         cp.customer_gender AS 'gender',
  1159.         cp.customer_age_range AS 'date_of_birth',
  1160.         SUBSTRING_INDEX(caet_street.value, delim, 1) AS 'street1',
  1161.         SUBSTRING_INDEX(SUBSTRING(caet_street.value, LENGTH(SUBSTRING_INDEX(caet_street.value, delim, 1)) + LENGTH(delim) + 1), delim, 1) AS 'street2',
  1162.         SUBSTRING_INDEX(SUBSTRING(caet_street.value, LENGTH(SUBSTRING_INDEX(caet_street.value, delim, 2)) + LENGTH(delim) + 1), delim, 1) AS 'street3',
  1163.         caev_city.value AS 'city',
  1164.         caev_region.value AS 'state_county',
  1165.         caev_postcode.value AS 'postal_code',
  1166.         caev_country.value AS 'country',
  1167.         eu.start_date AS 'start_date'
  1168.     FROM customer_entity ce
  1169.         LEFT JOIN enterprise_customerbalance ecb
  1170.             ON ce.entity_id = ecb.customer_id
  1171.         LEFT JOIN customer_entity_varchar cev_firstname
  1172.             ON ce.entity_id = cev_firstname.entity_id AND cev_firstname.attribute_id = 5
  1173.         LEFT JOIN customer_entity_varchar cev_lastname
  1174.             ON ce.entity_id = cev_lastname.entity_id AND cev_lastname.attribute_id = 7
  1175.         LEFT JOIN customer_entity_varchar cev_hash_password
  1176.             ON ce.entity_id = cev_hash_password.entity_id AND cev_hash_password.attribute_id = 12
  1177.         LEFT JOIN customer_personal cp
  1178.             ON cp.customer_id = ce.entity_id
  1179.         LEFT JOIN customer_address_entity cae
  1180.             ON ce.entity_id = cae.parent_id
  1181.         LEFT JOIN customer_address_entity_text caet_street
  1182.             ON cae.entity_id = caet_street.entity_id AND caet_street.attribute_id = 24
  1183.         LEFT JOIN customer_address_entity_varchar caev_city
  1184.             ON cae.entity_id = caev_city.entity_id AND caev_city.attribute_id = 25
  1185.         LEFT JOIN customer_address_entity_varchar caev_region
  1186.             ON cae.entity_id = caev_region.entity_id AND caev_region.attribute_id = 27
  1187.         LEFT JOIN customer_address_entity_varchar caev_postcode
  1188.             ON cae.entity_id = caev_postcode.entity_id AND caev_postcode.attribute_id = 29
  1189.         LEFT JOIN customer_address_entity_varchar caev_country
  1190.             ON cae.entity_id = caev_country.entity_id AND caev_country.attribute_id = 26
  1191.         LEFT JOIN earns_users eu
  1192.             ON ce.entity_id = eu.user_id
  1193.   WHERE ce.account_number = i_account_number AND ce.is_active = 1;
  1194.  
  1195. END */;;
  1196. DELIMITER ;
  1197. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1198. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1199. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1200. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1201. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_PARTNER_BY_G_ID` */;
  1202. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1203. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1204. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1205. /*!50003 SET character_set_client  = utf8 */ ;
  1206. /*!50003 SET character_set_results = utf8 */ ;
  1207. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1208. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1209. /*!50003 SET sql_mode              = '' */ ;
  1210. DELIMITER ;;
  1211. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_PARTNER_BY_G_ID`(i_g_partner_id int(11))
  1212. BEGIN
  1213.  
  1214.     SELECT
  1215.         p.id AS 'id',
  1216.         p.name AS 'name',
  1217.         p.country AS 'country',
  1218.         pe.type AS 'type',
  1219.         pe.g_partner_id AS 'g_partner_id',
  1220.         pe.g_partner_secret_key AS 'g_partner_secret_key',
  1221.         COALESCE(pe.allow_autolink, 0) AS 'allow_autolink'
  1222.     FROM partners_entity p
  1223.         INNER JOIN partners_earn pe ON p.id = pe.partner_id
  1224.     WHERE pe.g_partner_id = i_g_partner_id;
  1225.  
  1226. END */;;
  1227. DELIMITER ;
  1228. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1229. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1230. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1231. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1232. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_PARTNER_SECRET_KEY_BY_ID` */;
  1233. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1234. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1235. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1236. /*!50003 SET character_set_client  = utf8 */ ;
  1237. /*!50003 SET character_set_results = utf8 */ ;
  1238. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1239. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1240. /*!50003 SET sql_mode              = '' */ ;
  1241. DELIMITER ;;
  1242. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_PARTNER_SECRET_KEY_BY_ID`(gpartnerId int(11), out partnerSecretKey varchar(255))
  1243. BEGIN
  1244.  
  1245. DECLARE CONTINUE HANDLER FOR NOT FOUND SET partnerSecretKey = '';
  1246.  
  1247. SELECT g_partner_secret_key
  1248. FROM partners_earn
  1249. WHERE g_partner_id = gpartnerId
  1250. INTO partnerSecretKey;
  1251.  
  1252. END */;;
  1253. DELIMITER ;
  1254. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1255. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1256. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1257. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1258. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_PARTNER_TYPE_BY_ID` */;
  1259. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1260. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1261. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1262. /*!50003 SET character_set_client  = utf8 */ ;
  1263. /*!50003 SET character_set_results = utf8 */ ;
  1264. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1265. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1266. /*!50003 SET sql_mode              = '' */ ;
  1267. DELIMITER ;;
  1268. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_PARTNER_TYPE_BY_ID`(gpartnerId int(11), out partnerType int)
  1269. BEGIN
  1270.  
  1271.  
  1272.  
  1273.  
  1274.  
  1275. DECLARE CONTINUE HANDLER FOR NOT FOUND SET partnerType = -1;
  1276.  
  1277. SELECT type
  1278. FROM partners_earn
  1279. WHERE g_partner_id = gpartnerId
  1280. INTO partnerType;
  1281.  
  1282. END */;;
  1283. DELIMITER ;
  1284. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1285. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1286. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1287. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1288. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_PASSWORD_HASH_BY_EMAIL` */;
  1289. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1290. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1291. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1292. /*!50003 SET character_set_client  = utf8 */ ;
  1293. /*!50003 SET character_set_results = utf8 */ ;
  1294. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1295. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1296. /*!50003 SET sql_mode              = '' */ ;
  1297. DELIMITER ;;
  1298. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_PASSWORD_HASH_BY_EMAIL`(iemail varchar(255), out passwordHash varchar(255))
  1299. BEGIN
  1300.  
  1301. DECLARE passAttrId int(11) DEFAULT 12;
  1302. DECLARE CONTINUE HANDLER FOR NOT FOUND SET passwordHash = '';
  1303.  
  1304. SELECT cev_hash_password.value
  1305. FROM customer_entity ce
  1306.     INNER JOIN customer_entity_varchar cev_hash_password
  1307.               ON ce.entity_id = cev_hash_password.entity_id AND cev_hash_password.attribute_id = passAttrId
  1308. WHERE ce.email = iemail
  1309. INTO passwordHash;
  1310.  
  1311.  
  1312. END */;;
  1313. DELIMITER ;
  1314. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1315. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1316. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1317. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1318. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_PASSWORD_HASH_BY_NUMBER` */;
  1319. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1320. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1321. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1322. /*!50003 SET character_set_client  = utf8 */ ;
  1323. /*!50003 SET character_set_results = utf8 */ ;
  1324. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1325. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1326. /*!50003 SET sql_mode              = '' */ ;
  1327. DELIMITER ;;
  1328. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_PASSWORD_HASH_BY_NUMBER`(accNumber varchar(255), out passwordHash varchar(255))
  1329. BEGIN
  1330.  
  1331. DECLARE passAttrId int(11) DEFAULT 12;
  1332. DECLARE CONTINUE HANDLER FOR NOT FOUND SET passwordHash = '';
  1333.  
  1334. SELECT cev_hash_password.value
  1335. FROM customer_entity ce
  1336.     INNER JOIN customer_entity_varchar cev_hash_password
  1337.               ON ce.entity_id = cev_hash_password.entity_id AND cev_hash_password.attribute_id = passAttrId
  1338. WHERE ce.account_number = accNumber
  1339. INTO passwordHash;
  1340.  
  1341.  
  1342. END */;;
  1343. DELIMITER ;
  1344. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1345. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1346. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1347. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1348. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_TRANSACTION` */;
  1349. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1350. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1351. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1352. /*!50003 SET character_set_client  = utf8 */ ;
  1353. /*!50003 SET character_set_results = utf8 */ ;
  1354. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1355. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1356. /*!50003 SET sql_mode              = '' */ ;
  1357. DELIMITER ;;
  1358. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_TRANSACTION`(
  1359.                         IN i_g_partner_id INT(10),
  1360.                         IN i_partner_transaction_id VARCHAR(50),
  1361.                         OUT o_success TINYINT(1),
  1362.                         OUT o_reason VARCHAR(255))
  1363. BEGIN
  1364.      
  1365.     SELECT
  1366.         ce.account_number AS 'account_number',
  1367.         ecbh.balance_delta AS 'units',
  1368.         ps.member_account_number AS 'partner_member_account_number',
  1369.         pet.external_partner_transaction_id AS 'partner_transaction_id',
  1370.         ecbh.updated_at AS 'date_time',
  1371.         ecbh.description AS 'external_comment'
  1372.     FROM partners_entity p
  1373.         INNER JOIN partners_earn pe
  1374.             ON p.id = pe.partner_id
  1375.                 AND pe.g_partner_id = i_g_partner_id
  1376.         INNER JOIN partners_earn_transactions pet
  1377.             ON pe.partner_id = pet.partner_id
  1378.                 AND pet.external_partner_transaction_id = i_partner_transaction_id
  1379.         INNER JOIN enterprise_customerbalance_history ecbh
  1380.             ON pet.customer_balance_history_id = ecbh.history_id
  1381.         INNER JOIN partner_subscription ps
  1382.             ON ps.partner_id = pe.partner_id
  1383.                 AND ps.customer_id = pet.customer_id
  1384.         INNER JOIN customer_entity ce
  1385.             ON pet.customer_id = ce.entity_id
  1386.                 AND ce.is_active = 1
  1387.     LIMIT 1;
  1388.    
  1389.    
  1390.     IF NOT EXISTS
  1391.         (SELECT 1
  1392.          FROM partners_earn_transactions pet
  1393.          WHERE pet.external_partner_transaction_id = i_partner_transaction_id)
  1394.     THEN
  1395.         SELECT 0, 'Transaction doesn''t exist' INTO o_success, o_reason;
  1396.    
  1397.    
  1398.     ELSEIF NOT EXISTS
  1399.         (SELECT 1
  1400.          FROM partners_entity p
  1401.             INNER JOIN partners_earn pe
  1402.                 ON p.id = pe.partner_id
  1403.             INNER JOIN partners_earn_transactions pet
  1404.                 ON pe.partner_id = pet.partner_id
  1405.          WHERE pet.external_partner_transaction_id = i_partner_transaction_id
  1406.             AND pe.g_partner_id = i_g_partner_id)
  1407.     THEN
  1408.         SELECT 0, 'Transaction doesn''t belong to partner' INTO o_success, o_reason;
  1409.        
  1410.    
  1411.     ELSEIF NOT EXISTS
  1412.         (SELECT 1
  1413.          FROM partners_earn_transactions pet
  1414.             INNER JOIN customer_entity ce
  1415.                 ON pet.customer_id = ce.entity_id
  1416.          WHERE pet.external_partner_transaction_id = i_partner_transaction_id
  1417.             AND ce.is_active = 1)
  1418.     THEN
  1419.         SELECT 0, 'Account not found' INTO o_success, o_reason;
  1420.        
  1421.     ELSE
  1422.         SELECT 1, '' INTO o_success, o_reason;
  1423.        
  1424.     END IF;
  1425.    
  1426.    
  1427.    
  1428.  
  1429. END */;;
  1430. DELIMITER ;
  1431. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1432. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1433. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1434. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1435. /*!50003 DROP PROCEDURE IF EXISTS `EARN_GET_TRANSACTIONS` */;
  1436. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1437. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1438. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1439. /*!50003 SET character_set_client  = utf8 */ ;
  1440. /*!50003 SET character_set_results = utf8 */ ;
  1441. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1442. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1443. /*!50003 SET sql_mode              = '' */ ;
  1444. DELIMITER ;;
  1445. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `EARN_GET_TRANSACTIONS`(
  1446.                         IN i_g_partner_id INT(10),
  1447.                         IN i_partner_member_account_number VARCHAR(255),
  1448.                         IN i_from_date DATETIME,
  1449.                         IN i_to_date DATETIME,
  1450.                         OUT o_success TINYINT(1),
  1451.                         OUT o_reason VARCHAR(255))
  1452. BEGIN
  1453.    
  1454.     SELECT DISTINCT
  1455.         ce.account_number AS 'account_number',
  1456.         ecbh.balance_delta AS 'units',
  1457.         ps.member_account_number AS 'partner_member_account_number',
  1458.         pet.external_partner_transaction_id AS 'partner_transaction_id',
  1459.         ecbh.updated_at AS 'date_time',
  1460.         ecbh.description AS 'external_comment'
  1461.     FROM partners_entity p
  1462.         INNER JOIN partners_earn pe
  1463.             ON p.id = pe.partner_id
  1464.                 AND pe.g_partner_id = i_g_partner_id
  1465.         INNER JOIN partner_subscription ps
  1466.             ON pe.partner_id = ps.partner_id
  1467.                 AND ps.member_account_number = i_partner_member_account_number
  1468.         INNER JOIN customer_entity ce
  1469.             ON ps.customer_id = ce.entity_id
  1470.                 AND ce.is_active = 1
  1471.         INNER JOIN partners_earn_transactions pet
  1472.             ON ps.partner_id = pet.partner_id
  1473.                 AND ps.customer_id = pet.customer_id
  1474.         INNER JOIN enterprise_customerbalance_history ecbh
  1475.             ON pet.customer_balance_history_id = ecbh.history_id
  1476.         INNER JOIN enterprise_customerbalance_reasons ecbr
  1477.             ON ecbh.reason_id = ecbr.id    
  1478.     WHERE ecbr.reason_code IN ('Point Allocation', 'Points_Code_Deposit')
  1479.         AND ecbh.updated_at BETWEEN i_from_date AND i_to_date
  1480.     ORDER BY date_time DESC
  1481.     LIMIT 100;
  1482.    
  1483.    
  1484.     IF NOT EXISTS
  1485.         (SELECT 1
  1486.          FROM partner_subscription ps
  1487.          WHERE ps.member_account_number = i_partner_member_account_number)
  1488.    
  1489.     THEN
  1490.         SELECT 0, 'partner member account number does not exist' INTO o_success, o_reason;
  1491.        
  1492.    
  1493.     ELSEIF NOT EXISTS
  1494.         (SELECT 1
  1495.          FROM partners_earn pe
  1496.             INNER JOIN partner_subscription ps
  1497.                 ON pe.partner_id = ps.partner_id
  1498.          WHERE pe.g_partner_id = i_g_partner_id
  1499.             AND ps.member_account_number = i_partner_member_account_number)
  1500.     THEN
  1501.         SELECT 0, 'partner member account number doesn''t belong to partner' INTO o_success, o_reason;
  1502.    
  1503.    
  1504.     ELSEIF NOT EXISTS
  1505.         (SELECT 1
  1506.          FROM partners_earn pe
  1507.             INNER JOIN partner_subscription ps
  1508.                 ON pe.partner_id = ps.partner_id
  1509.             INNER JOIN customer_entity ce
  1510.                 ON ps.customer_id = ce.entity_id
  1511.          WHERE pe.g_partner_id = i_g_partner_id
  1512.             AND ps.member_account_number = i_partner_member_account_number
  1513.             AND ce.is_active = 1)
  1514.     THEN
  1515.         SELECT 0, 'Account not found' INTO o_success, o_reason;
  1516.        
  1517.     ELSE
  1518.         SELECT 1, '' INTO o_success, o_reason;
  1519.        
  1520.     END IF;
  1521.    
  1522.    
  1523.    
  1524.    
  1525. END */;;
  1526. DELIMITER ;
  1527. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1528. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1529. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1530. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1531. /*!50003 DROP PROCEDURE IF EXISTS `EPP_CREATE_TRANSACTION` */;
  1532. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1533. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1534. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1535. /*!50003 SET character_set_client  = utf8 */ ;
  1536. /*!50003 SET character_set_results = utf8 */ ;
  1537. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1538. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1539. /*!50003 SET sql_mode              = '' */ ;
  1540. DELIMITER ;;
  1541. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `EPP_CREATE_TRANSACTION`(IN inp_hash VARCHAR(255) ,IN inp_customer_id INT(10),IN inp_channel_id INT(10), OUT success TINYINT(1),OUT id_partner_trans INT(10) )
  1542. BEGIN
  1543. DECLARE v_balance_id INT(10);
  1544. DECLARE v_balance_amount DECIMAL(12,4);
  1545. DECLARE v_points_amount DECIMAL(12,4);
  1546. DECLARE v_partner_bal_amount DECIMAL(12,4);
  1547. DECLARE v_partner_id INT(10);
  1548. DECLARE v_transaction_description VARCHAR(255);
  1549. DECLARE v_frequency TINYINT(3);
  1550. DECLARE v_frequency_unit TINYINT(3);
  1551. DECLARE dt_now DATETIME;
  1552. DECLARE v_allow TINYINT(1);
  1553. DECLARE v_start_transaction TINYINT(1);
  1554. DECLARE customer_tr_id INT(10);
  1555. START TRANSACTION;
  1556.        
  1557. BEGIN      
  1558. DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
  1559. SELECT partner_id,transaction_description,frequency,frequency_unit,points_amount
  1560.     INTO v_partner_id,v_transaction_description,v_frequency,v_frequency_unit,v_points_amount
  1561.     FROM earn_plugin_hashes WHERE HASH = inp_hash;
  1562. SET v_start_transaction =  0 ;
  1563. SET customer_tr_id = 0 ;
  1564. SET id_partner_trans = 0;
  1565. SET success =0 ;
  1566. SELECT UTC_TIMESTAMP() INTO dt_now;
  1567. IF EXISTS (SELECT 1 FROM partners_earn_transactions p
  1568.         WHERE  p.partner_id = v_partner_id
  1569.             AND p.description = v_transaction_description
  1570.             AND p.TYPE = 20
  1571.             AND customer_id = inp_customer_id )
  1572. THEN   
  1573.     IF v_frequency_unit IS NULL THEN
  1574.    
  1575.         SELECT 1 INTO v_start_transaction;
  1576.     ELSE       
  1577.         SELECT `MG_CALC_EPP_FREQUENCY`(v_frequency_unit,v_frequency,v_partner_id,v_transaction_description,inp_customer_id,dt_now) INTO v_allow;
  1578.         IF v_allow = 1  THEN
  1579.             SELECT 1 INTO v_start_transaction;
  1580.         ELSE
  1581.             SELECT 0 INTO v_start_transaction;             
  1582.         END IF;
  1583.     END IF;
  1584.    
  1585. ELSE
  1586.     SELECT 1 INTO v_start_transaction;
  1587. END IF ;
  1588. IF v_start_transaction =1 THEN
  1589.         IF EXISTS
  1590.             (SELECT 1
  1591.                 FROM enterprise_customerbalance ecb
  1592.                     WHERE ecb.customer_id = inp_customer_id)
  1593.         THEN               
  1594.             SELECT ecb.balance_id, ecb.amount
  1595.                 INTO v_balance_id, v_balance_amount
  1596.             FROM enterprise_customerbalance ecb
  1597.                 WHERE ecb.customer_id = inp_customer_id;
  1598.        
  1599.         ELSE           
  1600.             SET v_balance_amount = 0;          
  1601.             INSERT INTO enterprise_customerbalance (customer_id, website_id, amount)
  1602.                 VALUES (v_customer_id, 1, v_balance_amount);           
  1603.             SET v_balance_id = LAST_INSERT_ID();       
  1604.         END IF;
  1605.            
  1606.             INSERT INTO enterprise_customerbalance_history(balance_id, updated_at, `action`, balance_amount, balance_delta, additional_info, reason_id, description)
  1607.                 VALUES (v_balance_id, dt_now, 1, v_balance_amount + v_points_amount, v_points_amount, NULL, 21, v_transaction_description);
  1608.                
  1609.             SET customer_tr_id = LAST_INSERT_ID();
  1610.                
  1611.            
  1612.             INSERT INTO enterprise_customerbalance_history_channel_map(history_id, channel_id) VALUES (customer_tr_id, inp_channel_id);
  1613.                
  1614.             UPDATE enterprise_customerbalance ecb
  1615.                 SET ecb.amount = ecb.amount + v_points_amount
  1616.             WHERE ecb.balance_id = v_balance_id;
  1617.                
  1618.            
  1619.             SELECT pe.points
  1620.                 INTO v_partner_bal_amount
  1621.             FROM partners_earn pe
  1622.             WHERE pe.partner_id = v_partner_id;
  1623.                
  1624.            
  1625.            
  1626.             INSERT INTO partners_earn_transactions (partner_id, `type`, `time`, points, balance_amount, customer_id, rep_id, customer_balance_history_id, description)
  1627.             VALUES (v_partner_id, 20,dt_now, v_points_amount, v_partner_bal_amount - v_points_amount, inp_customer_id,'Used by Customer via Earn Plugin' ,customer_tr_id, v_transaction_description);
  1628.                
  1629.             SET id_partner_trans = LAST_INSERT_ID();
  1630.            
  1631.             UPDATE partners_earn pe
  1632.                 SET pe.points = pe.points - v_points_amount
  1633.             WHERE pe.partner_id = v_partner_id;
  1634.                                
  1635.             SET success =1 ;
  1636.        
  1637.        
  1638. ELSE
  1639.     SET success = 0;
  1640. END IF;
  1641. COMMIT;
  1642. END;   
  1643. END */;;
  1644. DELIMITER ;
  1645. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1646. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1647. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1648. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1649. /*!50003 DROP PROCEDURE IF EXISTS `IVR_MAGENTO_CREATE_IRECYCLE_REPORTING` */;
  1650. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1651. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1652. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1653. /*!50003 SET character_set_client  = utf8 */ ;
  1654. /*!50003 SET character_set_results = utf8 */ ;
  1655. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1656. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1657. /*!50003 SET sql_mode              = '' */ ;
  1658. DELIMITER ;;
  1659. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `IVR_MAGENTO_CREATE_IRECYCLE_REPORTING`(IN acc_num VARCHAR(255), IN points DECIMAL(12,4), OUT result TINYINT, OUT reason VARCHAR(255))
  1660. BEGIN
  1661.  
  1662.     DECLARE tr_id INT(11);
  1663.     DECLARE cust_id INT(11);
  1664.     DECLARE str_id INT(5);
  1665.     DECLARE subs_id INT(11);
  1666.     DECLARE bal_id INT(11);
  1667.     DECLARE wbs_id INT(5);
  1668.     DECLARE res_id INT(11);
  1669.     DECLARE ivr_channel_id INT(11) DEFAULT 310;
  1670.    
  1671.     DECLARE bal_amount DECIMAL(12,4);
  1672.    
  1673.     DECLARE datetime_now DATETIME DEFAULT now();
  1674.    
  1675.     DECLARE bal_reason VARCHAR(255) DEFAULT 'curbside_irecycle_report_bonus';
  1676.     DECLARE bal_desc VARCHAR(255) DEFAULT 'Telling us you recycled';
  1677.     DECLARE bal_info VARCHAR(255) DEFAULT 'Telling us you recycled';
  1678.    
  1679.     DECLARE action_updated INT(5) DEFAULT 1;
  1680.     DECLARE action_created INT(5) DEFAULT 2;
  1681.    
  1682.    
  1683.     BEGIN
  1684.         DECLARE CONTINUE HANDLER FOR NOT FOUND SET subs_id = -1;
  1685.        
  1686.        
  1687.         SELECT ce.entity_id, ce.store_id, cs.subscription_id
  1688.         FROM customer_entity ce
  1689.             INNER JOIN curbside_subscription cs ON ce.entity_id = cs.customer_id
  1690.         WHERE ce.account_number = acc_num AND cs.end_date IS NULL
  1691.         LIMIT 1
  1692.         INTO cust_id, str_id, subs_id;
  1693.        
  1694.     END;
  1695.    
  1696.    
  1697.     IF subs_id <> -1 THEN
  1698.        
  1699.        
  1700.         IF NOT EXISTS (SELECT 1
  1701.                        FROM curbside_reports
  1702.                        WHERE subscription_id = subs_id AND YEARWEEK(reported_at, 3) = YEARWEEK(datetime_now, 3))
  1703.         THEN
  1704.        
  1705.            
  1706.             INSERT INTO curbside_reports(subscription_id, reported_at) VALUES(subs_id, datetime_now);  
  1707.                
  1708.            
  1709.             BEGIN
  1710.                 DECLARE CONTINUE HANDLER FOR NOT FOUND SET bal_id = -1;
  1711.                
  1712.                 SELECT balance_id, amount
  1713.                 FROM enterprise_customerbalance
  1714.                 WHERE customer_id = cust_id
  1715.                 INTO bal_id, bal_amount;
  1716.            
  1717.             END;
  1718.            
  1719.            
  1720.             BEGIN
  1721.                 DECLARE CONTINUE HANDLER FOR NOT FOUND SET res_id = null;
  1722.                
  1723.                 SELECT id
  1724.                 FROM enterprise_customerbalance_reasons
  1725.                 WHERE reason_code = bal_reason
  1726.                 INTO res_id;
  1727.            
  1728.             END;
  1729.            
  1730.            
  1731.             IF bal_id = -1 THEN
  1732.            
  1733.                
  1734.                 BEGIN
  1735.                     DECLARE CONTINUE HANDLER FOR NOT FOUND SET wbs_id = null;
  1736.                    
  1737.                     SELECT website_id
  1738.                     FROM core_store
  1739.                     WHERE store_id = str_id
  1740.                     INTO wbs_id;
  1741.                    
  1742.                 END;
  1743.            
  1744.                 INSERT INTO enterprise_customerbalance(customer_id, website_id, amount)
  1745.                 VALUES (cust_id , wbs_id, 0);
  1746.                
  1747.                 SET bal_id = LAST_INSERT_ID();
  1748.             END IF;
  1749.  
  1750.            
  1751.             INSERT INTO enterprise_customerbalance_history
  1752.                             (balance_id, updated_at, `action`, balance_amount, balance_delta, additional_info, reason_id, description)
  1753.                     VALUES  (bal_id, datetime_now, action_created, bal_amount + points, points, bal_info, res_id, bal_desc);
  1754.            
  1755.             SET tr_id = LAST_INSERT_ID();
  1756.            
  1757.            
  1758.             INSERT INTO enterprise_customerbalance_history_channel_map(history_id, channel_id) VALUES (tr_id, ivr_channel_id);
  1759.                    
  1760.            
  1761.             UPDATE enterprise_customerbalance
  1762.             SET amount = amount + points
  1763.             WHERE balance_id = bal_id;
  1764.            
  1765.             SELECT 1, 'Week is successfully reported' INTO result, reason;
  1766.        
  1767.         ELSE
  1768.        
  1769.             SELECT 0, 'Week already reported' INTO result, reason;
  1770.            
  1771.         END IF;
  1772.    
  1773.     ELSE
  1774.    
  1775.         SELECT 0, 'Subscription not found' INTO result, reason;
  1776.        
  1777.     END IF;
  1778.    
  1779.  
  1780. END */;;
  1781. DELIMITER ;
  1782. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1783. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1784. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1785. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1786. /*!50003 DROP PROCEDURE IF EXISTS `MCR_CREATE_ERROR` */;
  1787. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1788. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1789. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1790. /*!50003 SET character_set_client  = utf8 */ ;
  1791. /*!50003 SET character_set_results = utf8 */ ;
  1792. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1793. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1794. /*!50003 SET sql_mode              = '' */ ;
  1795. DELIMITER ;;
  1796. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MCR_CREATE_ERROR`(i_cust_id INT(11), i_error_id VARCHAR(255), i_message VARCHAR(255), i_description VARCHAR(255))
  1797. BEGIN
  1798.  
  1799. INSERT INTO mcr_errors(cust_id, error_id, message, description)
  1800.        VALUES (i_cust_id, i_error_id, i_message, i_description);
  1801.  
  1802. END */;;
  1803. DELIMITER ;
  1804. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1805. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1806. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1807. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1808. /*!50003 DROP PROCEDURE IF EXISTS `MCR_GET_UNCONFIRMED_MEMBERS` */;
  1809. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1810. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1811. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1812. /*!50003 SET character_set_client  = utf8 */ ;
  1813. /*!50003 SET character_set_results = utf8 */ ;
  1814. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1815. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1816. /*!50003 SET sql_mode              = '' */ ;
  1817. DELIMITER ;;
  1818. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MCR_GET_UNCONFIRMED_MEMBERS`()
  1819. BEGIN
  1820.    
  1821.     DECLARE remind_period INT(11) DEFAULT 3;
  1822.     DECLARE remind_period_days_before DATETIME DEFAULT DATE_SUB(NOW(), INTERVAL remind_period DAY);
  1823.    
  1824.     DECLARE start_datetime_criteria DATETIME DEFAULT DATE_FORMAT(remind_period_days_before, '%Y-%m-%d 00:00:00');
  1825.     DECLARE end_datetime_criteria DATETIME DEFAULT DATE_FORMAT(remind_period_days_before, '%Y-%m-%d 23:59:59');
  1826.  
  1827.        
  1828.     SELECT
  1829.         ce.entity_id AS 'id',
  1830.         ce.store_id AS 'store_id',
  1831.         ce.email AS 'email',
  1832.         cev_fn.value AS 'first_name',
  1833.         cev_ln.value AS 'last_name',
  1834.         cev_cnf.value AS 'confirm_key'
  1835.        
  1836.     FROM customer_entity ce
  1837.         INNER JOIN customer_entity_varchar cev_fn ON ce.entity_id = cev_fn.entity_id AND cev_fn.attribute_id = 5
  1838.         INNER JOIN customer_entity_varchar cev_ln ON ce.entity_id = cev_ln.entity_id AND cev_ln.attribute_id = 7
  1839.         INNER JOIN customer_entity_varchar cev_cnf ON ce.entity_id = cev_cnf.entity_id AND cev_cnf.attribute_id = 16
  1840.    
  1841.     WHERE
  1842.         ce.created_at BETWEEN start_datetime_criteria AND end_datetime_criteria
  1843.         AND TRIM(ce.email) <> ''
  1844.          
  1845.         ;
  1846.    
  1847. END */;;
  1848. DELIMITER ;
  1849. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1850. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1851. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1852. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1853. /*!50003 DROP PROCEDURE IF EXISTS `MG_ACTIVITIES_FOR_EARN` */;
  1854. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1855. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1856. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1857. /*!50003 SET character_set_client  = utf8 */ ;
  1858. /*!50003 SET character_set_results = utf8 */ ;
  1859. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1860. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1861. /*!50003 SET sql_mode              = '' */ ;
  1862. DELIMITER ;;
  1863. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_ACTIVITIES_FOR_EARN`(IN inp_product_id INT(10))
  1864. BEGIN
  1865.    
  1866. DROP TABLE IF EXISTS temp_earn_activities ;
  1867. CREATE TEMPORARY TABLE temp_earn_activities (
  1868.   `id` INT(10),
  1869.   `type_activity` TINYINT(1),
  1870.   `customer_id` INT(10),
  1871.   `firstname` VARCHAR(255),
  1872.   `lastname` VARCHAR(255),  
  1873.    `action` VARCHAR(255),  
  1874.   `for_order` DATETIME
  1875. ) ;
  1876. SELECT pe.NAME INTO @partnerName
  1877.     FROM catalog_product_entity cpe
  1878.         INNER JOIN partners_entity pe ON cpe.partner_id = pe.id
  1879.         WHERE cpe.entity_id = inp_product_id;
  1880. INSERT INTO temp_earn_activities
  1881. (id,type_activity,customer_id,`action`,for_order)
  1882. SELECT
  1883.     MAX(ca.id)
  1884.     ,0
  1885.     ,ca.customer_id
  1886.     ,CONCAT('earned with ',@partnerName)
  1887.     ,MAX(ca.updated_at)
  1888. FROM customers_activities ca
  1889.     WHERE ca.product_id = inp_product_id
  1890. GROUP BY ca.customer_id
  1891. ORDER BY ca.id DESC LIMIT 5 ;
  1892. INSERT INTO temp_earn_activities
  1893.     (id,type_activity,customer_id,`action`,for_order)
  1894. SELECT
  1895.     MAX(f.id)
  1896.     ,1
  1897.     ,f.customer_id
  1898.     ,'saved this earn opportunity to favorites'
  1899.     ,MAX(f.created_at)
  1900.     FROM favorite_products_for_users f
  1901.         WHERE f.product_id = inp_product_id
  1902.              AND favorite.is_private = 0
  1903. GROUP BY f.customer_id
  1904. ORDER BY f.id DESC LIMIT 5;
  1905. DROP TABLE IF EXISTS temp_max_date;
  1906. CREATE TEMPORARY TABLE temp_max_date
  1907. AS
  1908. SELECT customer_id,MAX(for_order) AS max_dt
  1909.     FROM temp_earn_activities
  1910. GROUP BY customer_id;
  1911. UPDATE temp_earn_activities t
  1912.     INNER JOIN
  1913.          customer_entity_varchar fname
  1914.             ON fname.entity_id =t.customer_id AND fname.attribute_id =5
  1915.     INNER JOIN
  1916.          customer_entity_varchar lname  
  1917.             ON lname.entity_id =t.customer_id AND lname.attribute_id =7
  1918. SET t.firstname = fname.VALUE , t.lastname = lname.VALUE    
  1919. ;
  1920. SELECT
  1921.     tea.`id`
  1922.     ,tea.`type_activity`
  1923.     ,tea.`customer_id`
  1924.     ,tea.`firstname`
  1925.     ,tea.`lastname`  
  1926.     ,tea.`action`  
  1927.     ,tea.`for_order`  
  1928.     FROM temp_earn_activities tea
  1929.         INNER JOIN temp_max_date tmd
  1930.             ON tea.customer_id = tmd.customer_id AND tea.for_order = tmd.max_dt
  1931. GROUP BY `customer_id`
  1932. ORDER BY for_order DESC
  1933. LIMIT 5;
  1934. DROP TABLE IF EXISTS temp_earn_activities ;
  1935. END */;;
  1936. DELIMITER ;
  1937. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  1938. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  1939. /*!50003 SET character_set_results = @saved_cs_results */ ;
  1940. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  1941. /*!50003 DROP PROCEDURE IF EXISTS `MG_ACTIVITIES_FOR_REWARD` */;
  1942. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  1943. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  1944. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  1945. /*!50003 SET character_set_client  = utf8 */ ;
  1946. /*!50003 SET character_set_results = utf8 */ ;
  1947. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  1948. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  1949. /*!50003 SET sql_mode              = '' */ ;
  1950. DELIMITER ;;
  1951. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_ACTIVITIES_FOR_REWARD`(IN inp_product_id INT(10))
  1952. BEGIN
  1953.    
  1954. DROP TABLE IF EXISTS temp_orders_activities ;
  1955. CREATE TEMPORARY TABLE temp_reward_activities (
  1956.   `id` INT(10),
  1957.   `type_activity` TINYINT(1),
  1958.   `customer_id` INT(10),
  1959.   `firstname` VARCHAR(255),
  1960.   `lastname` VARCHAR(255),  
  1961.    `action` VARCHAR(255),  
  1962.   `for_order` DATETIME
  1963. ) ;
  1964. INSERT INTO temp_reward_activities
  1965. SELECT
  1966.     MAX(sfo.entity_id)
  1967.     ,0
  1968.     ,sfo.customer_id
  1969.     ,sfo.customer_firstname
  1970.     ,sfo.customer_lastname
  1971.     ,'ordered this reward'
  1972.     ,MAX(sfoi.created_at)
  1973.    
  1974. FROM sales_flat_order_item sfoi
  1975.     INNER JOIN  sales_flat_order sfo
  1976.         ON sfoi.order_id = sfo.entity_id   
  1977. WHERE sfoi.product_id = inp_product_id
  1978.     AND sfo.visible_activity = 1
  1979. GROUP BY sfo.customer_id   
  1980. ORDER BY sfo.entity_id DESC LIMIT 5 ;
  1981. INSERT INTO temp_reward_activities
  1982.     (id,type_activity,customer_id,`action`,for_order)
  1983. SELECT
  1984.     MAX(f.id)
  1985.     ,1
  1986.     ,f.customer_id
  1987.     ,'saved this reward to favorites'
  1988.     ,MAX(f.created_at)
  1989.     FROM favorite_products_for_users f
  1990. WHERE f.product_id = inp_product_id
  1991.      AND favorite.is_private = 0   
  1992. GROUP BY f.customer_id
  1993. ORDER BY f.id DESC LIMIT 5;
  1994. DROP TABLE IF EXISTS temp_max_date;
  1995. CREATE TEMPORARY TABLE temp_max_date
  1996. AS
  1997. SELECT customer_id,MAX(for_order) AS max_dt
  1998.     FROM temp_reward_activities
  1999. GROUP BY customer_id;
  2000. UPDATE temp_reward_activities t
  2001.     INNER JOIN
  2002.          customer_entity_varchar fname
  2003.             ON fname.entity_id =t.id AND fname.attribute_id =5
  2004.     INNER JOIN
  2005.          customer_entity_varchar lname  
  2006.             ON lname.entity_id =t.id AND lname.attribute_id =7 
  2007. SET t.firstname = fname.VALUE , t.lastname = lname.VALUE    
  2008. WHERE t.type_activity = 1 ;
  2009. SELECT
  2010.     tra.`id`
  2011.     ,tra.`type_activity`
  2012.     ,tra.`customer_id`
  2013.     ,tra.`firstname`
  2014.     ,tra.`lastname`  
  2015.     ,tra.`action`  
  2016.     ,tra.`for_order`
  2017.     FROM temp_reward_activities tra INNER JOIN temp_max_date tmd
  2018.         ON tra.customer_id = tmd.customer_id AND tra.for_order = tmd.max_dt
  2019. ORDER BY for_order DESC LIMIT 5;
  2020. DROP TABLE IF EXISTS temp_reward_activities ;
  2021. END */;;
  2022. DELIMITER ;
  2023. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2024. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2025. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2026. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2027. /*!50003 DROP PROCEDURE IF EXISTS `MG_CREATE_CUST_ERROR` */;
  2028. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2029. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2030. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2031. /*!50003 SET character_set_client  = utf8 */ ;
  2032. /*!50003 SET character_set_results = utf8 */ ;
  2033. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2034. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2035. /*!50003 SET sql_mode              = '' */ ;
  2036. DELIMITER ;;
  2037. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MG_CREATE_CUST_ERROR`(i_cust_id INT(11), i_error_id VARCHAR(255), i_message VARCHAR(255), i_description VARCHAR(255))
  2038. BEGIN
  2039.  
  2040. INSERT INTO mg_errors(cust_id, error_id, message, description)
  2041.        VALUES (i_cust_id, i_error_id, i_message, i_description);
  2042.  
  2043. END */;;
  2044. DELIMITER ;
  2045. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2046. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2047. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2048. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2049. /*!50003 DROP PROCEDURE IF EXISTS `MG_CUSTOMER_ADD_TRANSACTION` */;
  2050. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2051. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2052. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2053. /*!50003 SET character_set_client  = utf8 */ ;
  2054. /*!50003 SET character_set_results = utf8 */ ;
  2055. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2056. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2057. /*!50003 SET sql_mode              = '' */ ;
  2058. DELIMITER ;;
  2059. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MG_CUSTOMER_ADD_TRANSACTION`(
  2060. IN accountNumber VARCHAR(255),
  2061. IN delta DECIMAL(12,2),
  2062. IN updatedAt DATETIME,
  2063. IN reason INT(10),
  2064. IN descriptionText VARCHAR(255),
  2065. IN itransaktions_old_id INT(11),
  2066.  
  2067. OUT result_code TINYINT(1)
  2068. )
  2069. L_return:
  2070. BEGIN
  2071.  
  2072.    
  2073. DECLARE balanceId INT DEFAULT 0;
  2074. DECLARE customerId INT DEFAULT 0;
  2075. DECLARE oldBalance DECIMAL(12,2);
  2076. DECLARE newBalance DECIMAL(12,2);
  2077. DECLARE balanceAction INT DEFAULT 1;
  2078. DECLARE ret TINYINT(1) DEFAULT 0;
  2079. DECLARE sql_error TINYINT(1) DEFAULT 1;
  2080. DECLARE partner_bal_amount DECIMAL(12,4);
  2081. DECLARE tr_id INT(11);
  2082.  
  2083. IF (accountNumber IS NULL
  2084.     OR delta IS NULL
  2085.     OR updatedAt IS NULL
  2086.     OR reason IS NULL
  2087.     OR descriptionText IS NULL
  2088.     OR itransaktions_old_id IS NULL
  2089.     OR accountNumber = ''
  2090.     OR reason = 0
  2091.     OR descriptionText = ''
  2092.     OR itransaktions_old_id = 0) THEN
  2093.     SET result_code = 4;
  2094.     LEAVE L_return;
  2095. END IF;
  2096.  
  2097. SELECT entity_id INTO customerId
  2098.         FROM customer_entity AS cust_c
  2099.         WHERE account_number = accountNumber;
  2100.        
  2101. IF customerId = 0 THEN
  2102.     SET result_code = 2;
  2103.     LEAVE L_return;
  2104. END IF;
  2105.  
  2106. IF EXISTS(SELECT * FROM enterprise_customerbalance_history WHERE transaktions_old_id = itransaktions_old_id) THEN
  2107.     SET result_code = 3;
  2108.     LEAVE L_return;
  2109. END IF;
  2110.  
  2111. SELECT balance_id, amount INTO balanceId, oldBalance
  2112.         FROM enterprise_customerbalance
  2113.         WHERE `customer_id` = customerId;
  2114.        
  2115. SELECT MG_NY_TIME_TO_UTC(updatedAt) INTO updatedAt;
  2116.    
  2117. START TRANSACTION;
  2118. BEGIN
  2119.     DECLARE EXIT HANDLER FOR SQLEXCEPTION ROLLBACK;
  2120.     SET sql_error = 1;
  2121.     IF balanceId = 0 THEN
  2122.         SET balanceAction = 2;
  2123.         SET oldBalance = 0;
  2124.         INSERT INTO enterprise_customerbalance(customer_id, website_id, amount) VALUES (customerId, 1, 0);
  2125.         SELECT LAST_INSERT_ID() INTO balanceId;
  2126.     END IF;
  2127.    
  2128.     SET newBalance = oldBalance + delta;
  2129.    
  2130.     UPDATE `enterprise_customerbalance` SET `amount` = newBalance WHERE `customer_id` = customerId;
  2131.    
  2132.     INSERT INTO enterprise_customerbalance_history
  2133.             (balance_id,balance_amount, balance_delta,  ACTION,        updated_at, reason_id, description,     additional_info,     is_customer_notified, transaktions_old_id)
  2134.         VALUES
  2135.             (balanceId, newBalance,     delta,          balanceAction, updatedAt,  reason,    descriptionText, 'ongoing migration', 1,                    itransaktions_old_id);
  2136.    
  2137.     SET tr_id = LAST_INSERT_ID();
  2138.    
  2139.    
  2140.  
  2141.    
  2142.     IF reason BETWEEN  25 AND 39 THEN  
  2143.        
  2144.        
  2145.         INSERT INTO partners_earn_transactions
  2146.             (partner_id, `type`, `time`, points, customer_id, customer_balance_history_id, description)
  2147.         VALUES (3162, 20, updatedAt, delta,  customerId, tr_id, descriptionText);
  2148.                    
  2149.     END IF;
  2150.     SET sql_error = 0;
  2151. END;
  2152. COMMIT;
  2153.    
  2154. IF sql_error = 1 THEN
  2155.     SET result_code = 1;
  2156. ELSE
  2157.     SET result_code = 0;
  2158. END IF;
  2159.  
  2160. END */;;
  2161. DELIMITER ;
  2162. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2163. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2164. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2165. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2166. /*!50003 DROP PROCEDURE IF EXISTS `MG_CUSTOMER_SEARCH` */;
  2167. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2168. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2169. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2170. /*!50003 SET character_set_client  = utf8 */ ;
  2171. /*!50003 SET character_set_results = utf8 */ ;
  2172. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2173. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2174. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  2175. DELIMITER ;;
  2176. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_CUSTOMER_SEARCH`(
  2177.  IN ifirstname_ VARCHAR(255)
  2178. ,IN ilastname_ VARCHAR(255)
  2179. ,IN iemail_ VARCHAR(255)
  2180. ,IN iaccount_number_ VARCHAR(255)
  2181. ,IN iphone_ VARCHAR(255)
  2182. ,IN istreet_ VARCHAR(255)
  2183. ,IN izip_ VARCHAR(255)
  2184. ,IN iregion_ VARCHAR(255)
  2185. ,IN icountryid_ VARCHAR(255)
  2186. ,IN sinceFrom_ DATETIME
  2187. ,IN sinceTo_ DATETIME
  2188. ,IN skipNum_ INT(10)
  2189. ,IN getNum_ INT(10)
  2190. )
  2191. BEGIN
  2192.  
  2193. DECLARE cFirst TINYINT(1) DEFAULT 0;
  2194. DECLARE cSecond TINYINT(1) DEFAULT 0;
  2195. DECLARE cThird TINYINT(1) DEFAULT 0;
  2196. DECLARE cFourth TINYINT(1) DEFAULT 0;
  2197. DECLARE ifirstname VARCHAR(255);
  2198. DECLARE ilastname VARCHAR(255) ;
  2199. DECLARE iemail VARCHAR(255);
  2200. DECLARE iaccount_number VARCHAR(255);
  2201. DECLARE iphone VARCHAR(255);
  2202. DECLARE istreet VARCHAR(255);
  2203. DECLARE izip VARCHAR(255);
  2204. DECLARE iregion VARCHAR(255);
  2205. DECLARE icountryid VARCHAR(255);
  2206. DECLARE sinceFrom DATETIME;
  2207. DECLARE sinceTo DATETIME;
  2208. DECLARE skipNum INT(10);
  2209. DECLARE getNum INT(10);
  2210. IF ifirstname_ = '' THEN SET ifirstname = NULL; ELSE SET ifirstname = ifirstname_; END IF;
  2211. IF ilastname_ = '' THEN SET ilastname = NULL; ELSE SET ilastname =ilastname_; END IF;
  2212. IF iemail_ = '' THEN SET iemail = NULL; ELSE SET iemail = iemail_; END IF;
  2213. IF iaccount_number_ = '' THEN SET iaccount_number = NULL; ELSE SET iaccount_number = iaccount_number_; END IF;
  2214. IF iphone_ = '' THEN SET iphone = NULL; ELSE SET iphone = iphone_; END IF;
  2215. IF istreet_ = '' THEN SET istreet = NULL; ELSE SET istreet = istreet_; END IF;
  2216. IF izip_ = '' THEN SET izip = NULL; ELSE SET izip = izip_; END IF;
  2217. IF iregion_ = '' THEN SET iregion = NULL; ELSE SET iregion = iregion_; END IF;
  2218. IF icountryid_ = '' THEN SET icountryid = NULL; ELSE SET icountryid = icountryid_; END IF;
  2219. IF sinceFrom_ = '0000-00-00 00:00:00' OR sinceFrom_ = '' THEN SET sinceFrom = NULL; ELSE SET sinceFrom = sinceFrom_; END IF;
  2220. IF sinceTo_ = '0000-00-00 00:00:00' OR sinceTo_ = '' THEN SET sinceTo = NULL; ELSE SET sinceTo = sinceTo_; END IF;
  2221. IF skipNum_ = '' OR skipNum_ IS NULL  THEN SET skipNum = 0; ELSE SET skipNum = skipNum_; END IF;
  2222. IF getNum_ = '' OR getNum_ IS NULL THEN SET getNum = 100; ELSE SET getNum = getNum_; END IF;
  2223. IF ifirstname IS NOT NULL OR ilastname IS NOT NULL
  2224. THEN SET cFirst = 1;
  2225. END IF;
  2226. IF iphone IS NOT NULL OR izip IS NOT NULL OR iregion IS NOT NULL OR icountryid IS NOT NULL
  2227. THEN SET cSecond = 1;
  2228. END IF;
  2229. IF istreet IS NOT NULL
  2230. THEN SET cThird = 1;
  2231. END IF;
  2232. IF sinceFrom IS NOT NULL  OR sinceTo OR iemail IS NOT NULL OR iaccount_number IS NOT NULL
  2233. THEN SET cFourth =1 ;
  2234. END IF;
  2235. DROP TEMPORARY TABLE IF EXISTS t_search_customers;
  2236. SET @sql_INS = '
  2237. create temporary table  t_search_customers as
  2238.         SELECT ce.entity_id AS customer_id
  2239.             FROM customer_entity ce ';
  2240. SET @sql_INS =CONCAT(@sql_INS
  2241. ,CASE WHEN ifirstname IS NOT NULL THEN
  2242.     ' INNER JOIN customer_entity_varchar cev_firstname
  2243.         ON ce.entity_id = cev_firstname.entity_id AND cev_firstname.attribute_id = 5 '
  2244. ELSE '' END
  2245. ,CASE WHEN ilastname IS NOT NULL THEN
  2246.     ' INNER JOIN customer_entity_varchar cev_lastname
  2247.         ON ce.entity_id = cev_lastname.entity_id AND cev_lastname.attribute_id = 7 '
  2248. ELSE '' END
  2249. , CASE WHEN cSecond =1 OR cThird =1 THEN
  2250.     ' INNER join customer_address_entity ae ON ce.entity_id = ae.parent_id '
  2251. ELSE '' END
  2252. ,CASE WHEN iphone IS NOT NULL THEN
  2253.     ' INNER JOIN customer_mobile aev_phone ON ce.entity_id = aev_phone.customer_id '
  2254. ELSE '' END
  2255. ,CASE WHEN izip IS NOT NULL THEN
  2256.     ' INNER JOIN customer_address_entity_varchar aev_zip ON ae.entity_id = aev_zip.entity_id AND aev_zip.attribute_id = 29 '
  2257. ELSE '' END
  2258. ,CASE WHEN iregion IS NOT NULL THEN
  2259.     ' INNER JOIN customer_address_entity_varchar aev_region ON ae.entity_id = aev_region.entity_id AND aev_region.attribute_id = 27 '
  2260. ELSE '' END
  2261. ,CASE WHEN icountryid IS NOT NULL THEN
  2262.     ' INNER JOIN customer_address_entity_varchar aev_countryid ON ae.entity_id = aev_countryid.entity_id AND aev_countryid.attribute_id = 26 '
  2263. ELSE '' END
  2264. ,CASE WHEN istreet IS NOT NULL THEN
  2265.     ' INNER JOIN customer_address_entity_text aet_street ON ae.entity_id = aet_street.entity_id AND aet_street.attribute_id = 24 '
  2266. ELSE '' END
  2267. ,CASE WHEN cThird =1 THEN
  2268.     ' INNER JOIN customer_address_entity_text aet ON ae.entity_id = aet.entity_id '
  2269. ELSE '' END
  2270. ,' WHERE  1=1 '
  2271. , CASE WHEN iemail IS NOT NULL
  2272.     THEN CONCAT(" AND (ce.email LIKE '",iemail,"%' ) ")
  2273. ELSE '' END
  2274. , CASE WHEN iaccount_number IS NOT NULL
  2275.     THEN CONCAT(" AND (ce.account_number LIKE '",iaccount_number,"%' )")
  2276. ELSE '' END
  2277. , CASE WHEN sinceFrom IS NOT NULL
  2278.     THEN CONCAT(" AND (ce.created_at >= date('",sinceFrom,"') )")
  2279. ELSE '' END
  2280. , CASE WHEN sinceTo IS NOT NULL
  2281.     THEN CONCAT(" AND (ce.created_at <= date('",sinceTo,"') )")
  2282. ELSE '' END
  2283. ,CASE WHEN ifirstname IS NOT NULL
  2284.     THEN CONCAT(" AND (  cev_firstname.`value` LIKE '",ifirstname,"%') ")
  2285. ELSE '' END
  2286. ,CASE WHEN ilastname IS NOT NULL
  2287.     THEN CONCAT(" AND (  cev_lastname.`value`  LIKE '",ilastname,"%') ")
  2288. ELSE '' END
  2289. ,CASE WHEN iphone IS NOT NULL
  2290.     THEN CONCAT(" AND (aev_phone.`phone_number`  LIKE '",iphone,"%') ")
  2291. ELSE '' END
  2292. ,CASE WHEN izip IS NOT NULL
  2293.     THEN CONCAT(" AND (aev_zip.`value`  LIKE '",izip,"%') ")
  2294. ELSE '' END
  2295. ,CASE WHEN iregion IS NOT NULL
  2296.     THEN CONCAT(" AND (aev_region.`value`  LIKE '",iregion,"%') ")
  2297. ELSE '' END
  2298. ,CASE WHEN icountryid IS NOT NULL
  2299.     THEN CONCAT(" AND (aev_countryid.`value`  LIKE '",icountryid,"%') ")
  2300. ELSE '' END
  2301. ,CASE WHEN istreet IS NOT NULL
  2302.     THEN CONCAT(" AND (aet_street.`value`  LIKE '",istreet,"%') ")
  2303. ELSE '' END
  2304. ,' LIMIT '
  2305. ,skipNum
  2306. ,','
  2307. ,getNum
  2308. ,';');
  2309.     PREPARE stmt FROM @sql_INS;
  2310.     EXECUTE stmt;
  2311.    
  2312.     SET @sql_INS ='
  2313.      SELECT
  2314.          a.entity_id AS customer_id
  2315.         , a.email
  2316.         , a.account_number  
  2317.         , firstname.VALUE AS `first_name`
  2318.         , lastname.VALUE AS `last_name`
  2319.         , adr.phone AS `phone`
  2320.         , adr.street AS street
  2321.         , adr.zip AS zip
  2322.         , adr.region
  2323.         , adr.countryid
  2324.        
  2325.       FROM t_search_customers t
  2326.         INNER JOIN customer_entity a ON t.customer_id = a.entity_id
  2327.         left JOIN customer_entity_varchar AS firstname
  2328.             ON a.entity_id=firstname.entity_id AND firstname.attribute_id=5
  2329.         left JOIN customer_entity_varchar AS lastname
  2330.             ON a.entity_id=lastname.entity_id AND lastname.attribute_id=7
  2331.         inner join MG_V_customer_addresses adr on a.entity_id = adr.customer_id;
  2332.        
  2333.     ';
  2334.  PREPARE stmt FROM @sql_INS;
  2335.  EXECUTE stmt;
  2336. DROP  TABLE IF EXISTS t_search_customers;
  2337.  
  2338. END */;;
  2339. DELIMITER ;
  2340. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2341. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2342. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2343. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2344. /*!50003 DROP PROCEDURE IF EXISTS `MG_CUSTOMER_SEARCH_COUNT` */;
  2345. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2346. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2347. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2348. /*!50003 SET character_set_client  = utf8 */ ;
  2349. /*!50003 SET character_set_results = utf8 */ ;
  2350. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2351. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2352. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  2353. DELIMITER ;;
  2354. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_CUSTOMER_SEARCH_COUNT`(
  2355.  IN ifirstname_ VARCHAR(255)
  2356. ,IN ilastname_ VARCHAR(255)
  2357. ,IN iemail_ VARCHAR(255)
  2358. ,IN iaccount_number_ VARCHAR(255)
  2359. ,IN iphone_ VARCHAR(255)
  2360. ,IN istreet_ VARCHAR(255)
  2361. ,IN izip_ VARCHAR(255)
  2362. ,IN iregion_ VARCHAR(255)
  2363. ,IN icountryid_ VARCHAR(255)
  2364. ,IN sinceFrom_ DATETIME
  2365. ,IN sinceTo_ DATETIME
  2366. )
  2367. BEGIN
  2368.  
  2369. DECLARE cFirst TINYINT(1) DEFAULT 0;
  2370. DECLARE cSecond TINYINT(1) DEFAULT 0;
  2371. DECLARE cThird TINYINT(1) DEFAULT 0;
  2372. DECLARE cFourth TINYINT(1) DEFAULT 0;
  2373. DECLARE ifirstname VARCHAR(255);
  2374. DECLARE ilastname VARCHAR(255) ;
  2375. DECLARE iemail VARCHAR(255);
  2376. DECLARE iaccount_number VARCHAR(255);
  2377. DECLARE iphone VARCHAR(255);
  2378. DECLARE istreet VARCHAR(255);
  2379. DECLARE izip VARCHAR(255);
  2380. DECLARE iregion VARCHAR(255);
  2381. DECLARE icountryid VARCHAR(255);
  2382. DECLARE sinceFrom DATETIME;
  2383. DECLARE sinceTo DATETIME;
  2384. IF ifirstname_ = '' THEN SET ifirstname = NULL; ELSE SET ifirstname = ifirstname_; END IF;
  2385. IF ilastname_ = '' THEN SET ilastname = NULL; ELSE SET ilastname =ilastname_; END IF;
  2386. IF iemail_ = '' THEN SET iemail = NULL; ELSE SET iemail = iemail_; END IF;
  2387. IF iaccount_number_ = '' THEN SET iaccount_number = NULL; ELSE SET iaccount_number = iaccount_number_; END IF;
  2388. IF iphone_ = '' THEN SET iphone = NULL; ELSE SET iphone = iphone_; END IF;
  2389. IF istreet_ = '' THEN SET istreet = NULL; ELSE SET istreet = istreet_; END IF;
  2390. IF izip_ = '' THEN SET izip = NULL; ELSE SET izip = izip_; END IF;
  2391. IF iregion_ = '' THEN SET iregion = NULL; ELSE SET iregion = iregion_; END IF;
  2392. IF icountryid_ = '' THEN SET icountryid = NULL; ELSE SET icountryid = icountryid_; END IF;
  2393. IF sinceFrom_ = '0000-00-00 00:00:00' OR sinceFrom_ = '' THEN SET sinceFrom = NULL; ELSE SET sinceFrom = sinceFrom_; END IF;
  2394. IF sinceTo_ = '0000-00-00 00:00:00' OR sinceTo_ = '' THEN SET sinceTo = NULL; ELSE SET sinceTo = sinceTo_; END IF;
  2395. IF ifirstname IS NOT NULL OR ilastname IS NOT NULL
  2396. THEN SET cFirst = 1;
  2397. END IF;
  2398. IF iphone IS NOT NULL OR izip IS NOT NULL OR iregion IS NOT NULL OR icountryid IS NOT NULL
  2399. THEN SET cSecond = 1;
  2400. END IF;
  2401. IF istreet IS NOT NULL
  2402. THEN SET cThird = 1;
  2403. END IF;
  2404. IF sinceFrom IS NOT NULL  OR sinceTo OR iemail IS NOT NULL OR iaccount_number IS NOT NULL
  2405. THEN SET cFourth =1 ;
  2406. END IF;
  2407.  
  2408. SET @sql_INS = '
  2409.         SELECT count(ce.entity_id) AS customer_count
  2410.             FROM customer_entity ce ';
  2411. SET @sql_INS =CONCAT(@sql_INS
  2412. ,CASE WHEN ifirstname IS NOT NULL THEN
  2413.     ' INNER JOIN customer_entity_varchar cev_firstname
  2414.         ON ce.entity_id = cev_firstname.entity_id AND cev_firstname.attribute_id = 5 '
  2415. ELSE '' END
  2416. ,CASE WHEN ilastname IS NOT NULL THEN
  2417.     ' INNER JOIN customer_entity_varchar cev_lastname
  2418.         ON ce.entity_id = cev_lastname.entity_id AND cev_lastname.attribute_id = 7 '
  2419. ELSE '' END
  2420. , CASE WHEN cSecond =1 OR cThird =1 THEN
  2421.     ' INNER join customer_address_entity ae ON ce.entity_id = ae.parent_id '
  2422. ELSE '' END
  2423. ,CASE WHEN iphone IS NOT NULL THEN
  2424.     ' INNER JOIN customer_mobile aev_phone ON ce.entity_id = aev_phone.customer_id '
  2425. ELSE '' END
  2426. ,CASE WHEN izip IS NOT NULL THEN
  2427.     ' INNER JOIN customer_address_entity_varchar aev_zip ON ae.entity_id = aev_zip.entity_id AND aev_zip.attribute_id = 29 '
  2428. ELSE '' END
  2429. ,CASE WHEN iregion IS NOT NULL THEN
  2430.     ' INNER JOIN customer_address_entity_varchar aev_region ON ae.entity_id = aev_region.entity_id AND aev_region.attribute_id = 27 '
  2431. ELSE '' END
  2432. ,CASE WHEN icountryid IS NOT NULL THEN
  2433.     ' INNER JOIN customer_address_entity_varchar aev_countryid ON ae.entity_id = aev_countryid.entity_id AND aev_countryid.attribute_id = 26 '
  2434. ELSE '' END
  2435. ,CASE WHEN istreet IS NOT NULL THEN
  2436.     ' INNER JOIN customer_address_entity_text aet_street ON ae.entity_id = aet_street.entity_id AND aet_street.attribute_id = 24 '
  2437. ELSE '' END
  2438. ,CASE WHEN cThird =1 THEN
  2439.     ' INNER JOIN customer_address_entity_text aet ON ae.entity_id = aet.entity_id '
  2440. ELSE '' END
  2441. ,' WHERE  1=1  '
  2442. , CASE WHEN iemail IS NOT NULL
  2443.     THEN CONCAT(" AND (ce.email LIKE '",iemail,"%' ) ")
  2444. ELSE '' END
  2445. , CASE WHEN iaccount_number IS NOT NULL
  2446.     THEN CONCAT(" AND (ce.account_number LIKE '",iaccount_number,"%' )")
  2447. ELSE '' END
  2448. , CASE WHEN sinceFrom IS NOT NULL
  2449.     THEN CONCAT(" AND (ce.created_at >= date('",sinceFrom,"') )")
  2450. ELSE '' END
  2451. , CASE WHEN sinceTo IS NOT NULL
  2452.     THEN CONCAT(" AND (ce.created_at <= date('",sinceTo,"') )")
  2453. ELSE '' END
  2454. ,CASE WHEN ifirstname IS NOT NULL
  2455.     THEN CONCAT(" AND (  cev_firstname.`value` LIKE '",ifirstname,"%') ")
  2456. ELSE '' END
  2457. ,CASE WHEN ilastname IS NOT NULL
  2458.     THEN CONCAT(" AND (  cev_lastname.`value`  LIKE '",ilastname,"%') ")
  2459. ELSE '' END
  2460. ,CASE WHEN iphone IS NOT NULL
  2461.     THEN CONCAT(" AND (aev_phone.`phone_number`  LIKE '",iphone,"%') ")
  2462. ELSE '' END
  2463. ,CASE WHEN izip IS NOT NULL
  2464.     THEN CONCAT(" AND (aev_zip.`value`  LIKE '",izip,"%') ")
  2465. ELSE '' END
  2466. ,CASE WHEN iregion IS NOT NULL
  2467.     THEN CONCAT(" AND (aev_region.`value`  LIKE '",iregion,"%') ")
  2468. ELSE '' END
  2469. ,CASE WHEN icountryid IS NOT NULL
  2470.     THEN CONCAT(" AND (aev_countryid.`value`  LIKE '",icountryid,"%') ")
  2471. ELSE '' END
  2472. ,CASE WHEN istreet IS NOT NULL
  2473.     THEN CONCAT(" AND (aet_street.`value`  LIKE '",istreet,"%') ")
  2474. ELSE '' END
  2475. ,';');
  2476.     PREPARE stmt FROM @sql_INS;
  2477.     EXECUTE stmt;
  2478.    
  2479.  
  2480. END */;;
  2481. DELIMITER ;
  2482. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2483. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2484. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2485. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2486. /*!50003 DROP PROCEDURE IF EXISTS `MG_GET_ACTIVITIES_BY_ZIP` */;
  2487. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2488. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2489. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2490. /*!50003 SET character_set_client  = utf8 */ ;
  2491. /*!50003 SET character_set_results = utf8 */ ;
  2492. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2493. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2494. /*!50003 SET sql_mode              = '' */ ;
  2495. DELIMITER ;;
  2496. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_GET_ACTIVITIES_BY_ZIP`(IN inp_zip VARCHAR(255),IN inp_store_id SMALLINT(5))
  2497. BEGIN
  2498. DECLARE inp_dist INT(10);
  2499. DECLARE count_returned TINYINT(3);
  2500. DECLARE time_range INT(10);
  2501. SET inp_dist = 30 ;
  2502. SET count_returned =30;
  2503. SET time_range = 14400 ; -- 4H
  2504. -- SET time_range = 28800 ; -- 8H
  2505. SELECT TIME(NOW()) INTO @now_time ;
  2506. SELECT 0 INTO @count_all_rows;
  2507.    
  2508. DROP TABLE IF EXISTS temp_zip_distance;
  2509. CREATE TEMPORARY TABLE temp_zip_distance (
  2510.   `id` INT(10),
  2511.   `store_id` SMALLINT(5) UNSIGNED DEFAULT NULL,
  2512.   `type_id` TINYINT(2) DEFAULT NULL,
  2513.   `customer_id` INT(10) UNSIGNED NOT NULL,  
  2514.    product_id INT(10),  
  2515.   `longitude` FLOAT,
  2516.   `latitude` FLOAT,
  2517.   `content` TEXT DEFAULT NULL,  
  2518.   `updated_at` DATETIME NOT NULL,
  2519.    time_dff INT(10)  
  2520. ) ;
  2521. IF TRIM(IFNULL(inp_zip,0)) <> ''  THEN
  2522. INSERT INTO   temp_zip_distance
  2523. SELECT  SQL_CALC_FOUND_ROWS
  2524.    
  2525.     k.id
  2526.     ,k.store_id
  2527.     ,CASE
  2528.         WHEN k.type_id =1 THEN 1
  2529.         WHEN k.type_id =2 THEN 2
  2530.         WHEN k.type_id =3 THEN 3
  2531.         WHEN k.type_id =4 THEN 4
  2532.         WHEN k.type_id =5 THEN 5
  2533.      END AS type_id
  2534.     ,k.customer_id 
  2535.     ,k.product_id  
  2536.     ,k.longitude
  2537.     ,k.latitude
  2538.     ,k.content
  2539.     ,k.updated_at
  2540.     ,k.time_dff
  2541.     FROM
  2542.     (SELECT ra.id,ra.store_id,ra.type_id,ra.customer_id,ra.content,ra.product_id,ra.updated_at, tl.zip ,ra.longitude AS longitude , ra.latitude AS latitude,TIME_TO_SEC(TIMEDIFF( @now_time ,TIME(ra.updated_at)) ) AS  time_dff
  2543.         , ((( ACOS(SIN(((tl.latitude *PI())/180)) * SIN((ra.`latitude`*PI()/180))
  2544.         + COS(((tl.latitude * PI())/180)) * COS((ra.`latitude`*PI()/180)) * COS((((tl.longitude-ra.`longitude`)*PI())/180))))*180/PI())*60*1.1515
  2545.         ) AS distance  
  2546.         FROM `customers_activities` ra  INNER JOIN (SELECT zip,longitude  ,latitude FROM zip_geo WHERE zip  = inp_zip)  tl     
  2547.     ) k
  2548.      WHERE distance <= inp_dist
  2549.         AND  ABS(time_dff) <= time_range
  2550.     ORDER BY id DESC ;
  2551.    
  2552.  SELECT FOUND_ROWS() INTO @count_all_rows;
  2553.  
  2554. END IF;
  2555. IF @count_all_rows = 0 THEN
  2556. INSERT INTO  temp_zip_distance
  2557. SELECT  SQL_CALC_FOUND_ROWS
  2558.     k.id
  2559.     ,k.store_id
  2560.     ,CASE
  2561.         WHEN k.type_id =1 THEN 1
  2562.         WHEN k.type_id =2 THEN 2
  2563.         WHEN k.type_id =3 THEN 3
  2564.         WHEN k.type_id =4 THEN 4
  2565.         WHEN k.type_id =5 THEN 5
  2566.      END AS type_id
  2567.     ,k.customer_id 
  2568.     ,k.product_id  
  2569.     ,k.longitude
  2570.     ,k.latitude
  2571.     ,k.content
  2572.     ,k.updated_at
  2573.     ,TIME_TO_SEC(TIMEDIFF( @now_time ,TIME(k.updated_at)) ) time_dff
  2574.     FROM `customers_activities` k  
  2575.     WHERE
  2576.         k.store_id  = inp_store_id
  2577.             AND ABS(TIME_TO_SEC(TIMEDIFF( @now_time ,TIME(k.updated_at)) )) <= time_range
  2578.     ORDER BY id DESC ;     
  2579.     SELECT FOUND_ROWS() INTO @count_all_rows;
  2580. END IF;
  2581. DROP TABLE IF EXISTS temp_30_customers_activities ;
  2582. CREATE TEMPORARY TABLE temp_30_customers_activities (
  2583.   `id` INT(10),
  2584.   `store_id` SMALLINT(5) UNSIGNED DEFAULT NULL,
  2585.   `type_id` TINYINT(2) DEFAULT NULL,
  2586.   `customer_id` INT(10) UNSIGNED NOT NULL,
  2587.   `longitude` FLOAT,
  2588.   `latitude` FLOAT,
  2589.   `content` TEXT DEFAULT NULL,  
  2590.   `updated_at` DATETIME NOT NULL,
  2591.   `cnt_in_result` INT(10),
  2592.    time_dff INT(10)  
  2593. ) ;
  2594. IF @count_all_rows >0 THEN
  2595. DROP TABLE IF EXISTS temp_type_count;
  2596. CREATE TEMPORARY TABLE temp_type_count
  2597. AS
  2598. SELECT type_id
  2599.     ,COUNT(1) cnt
  2600.     ,CASE WHEN  ROUND(( COUNT(1)/@count_all_rows )*count_returned) >=1 THEN ROUND(( COUNT(1)/@count_all_rows )*count_returned)
  2601.           WHEN  ROUND(( COUNT(1)/@count_all_rows )*count_returned) >0 THEN 1   
  2602.     ELSE 0 END
  2603.     AS w
  2604.     FROM  temp_zip_distance
  2605. GROUP BY type_id;
  2606. SET @i =1 ;
  2607. WHILE @i <=4 DO
  2608. SET @sql_ = CONCAT('SELECT round(w) into @cnt_a FROM temp_type_count WHERE type_id =',@i);
  2609. PREPARE stmt FROM @sql_;
  2610. EXECUTE stmt;    
  2611. DEALLOCATE PREPARE stmt;
  2612. SET @cnt_limit = @cnt_a;
  2613. IF @cnt_limit IS NOT NULL THEN
  2614. SET @sql_ =
  2615.     CONCAT('insert into temp_30_customers_activities
  2616.         SELECT `id`,`store_id`,`type_id`,`customer_id`,`longitude`,`latitude`,`content`,`updated_at`,',@cnt_a,',time_dff
  2617.         FROM temp_zip_distance
  2618.             WHERE type_id = ',@i,' ORDER BY id desc limit ',@cnt_a
  2619.         );
  2620.        
  2621.     PREPARE stmt FROM @sql_;
  2622.     EXECUTE stmt;      
  2623.     DEALLOCATE PREPARE stmt;   
  2624.    
  2625. END IF;
  2626.    
  2627.     SET @i = @i +1 ;
  2628. END WHILE;
  2629.  
  2630. END IF;
  2631. SELECT
  2632.   id,
  2633.   type_id,
  2634.   content
  2635. FROM temp_30_customers_activities  
  2636.  ORDER BY cnt_in_result DESC ,type_id LIMIT 30;
  2637.  
  2638. DROP TABLE IF EXISTS temp_result_customers_activities ;
  2639. DROP TABLE IF EXISTS temp_zip_distance;
  2640. DROP TABLE IF EXISTS temp_type_count;
  2641. END */;;
  2642. DELIMITER ;
  2643. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2644. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2645. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2646. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2647. /*!50003 DROP PROCEDURE IF EXISTS `MG_GET_MONTH_TRANSACTIONS` */;
  2648. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2649. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2650. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2651. /*!50003 SET character_set_client  = utf8 */ ;
  2652. /*!50003 SET character_set_results = utf8 */ ;
  2653. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2654. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2655. /*!50003 SET sql_mode              = '' */ ;
  2656. DELIMITER ;;
  2657. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MG_GET_MONTH_TRANSACTIONS`(IN `custId` int(11))
  2658. BEGIN
  2659.    
  2660.     DECLARE datetime_criteria DATETIME DEFAULT DATE_SUB(NOW(), INTERVAL 1 MONTH);
  2661.     DECLARE month_criteria INT DEFAULT MONTH(datetime_criteria);
  2662.     DECLARE year_criteria INT DEFAULT YEAR(datetime_criteria);
  2663.    
  2664.     DROP TEMPORARY TABLE IF EXISTS gconomy_month_transactions;
  2665.  
  2666.     CREATE TEMPORARY TABLE gconomy_month_transactions
  2667.     SELECT
  2668.         cbh.balance_id,
  2669.         cbh.reason_id,
  2670.         cbh.balance_delta,
  2671.         CASE cbh.reason_id
  2672.             WHEN 25 THEN 'Points from Purchases'
  2673.             WHEN 26 THEN 'Bonus Points from Reward Partners'
  2674.             WHEN 27 THEN 'Bonus Points from Promotions'
  2675.             WHEN 28 THEN 'First Purchase bonus'
  2676.             WHEN 29 THEN 'Adjustments'
  2677.             ELSE 'Bonus Points'
  2678.         END AS 'reason'
  2679.     FROM enterprise_customerbalance_history cbh
  2680.     WHERE
  2681.         cbh.reason_id BETWEEN 25 AND 39
  2682.         AND year_criteria = YEAR(cbh.updated_at)
  2683.         AND month_criteria = MONTH(cbh.updated_at);
  2684.        
  2685.     ALTER TABLE gconomy_month_transactions ADD INDEX ix_balance_id(balance_id);
  2686.    
  2687.     SELECT
  2688.         ce.entity_id AS 'cust_id',
  2689.         ce.store_id AS 'store_id',
  2690.         ce.email AS 'email',
  2691.         cev_fn.value AS 'first_name',
  2692.         gmt.reason AS 'reason',
  2693.         sum(gmt.balance_delta) AS 'points',
  2694.         max(gmt.reason_id) AS 'order_id'
  2695.     FROM customer_entity ce
  2696.         INNER JOIN enterprise_customerbalance cb ON ce.entity_id = cb.customer_id
  2697.         INNER JOIN gconomy_month_transactions gmt ON cb.balance_id = gmt.balance_id
  2698.         LEFT OUTER JOIN customer_entity_varchar cev_fn ON cev_fn.entity_id = ce.entity_id AND cev_fn.attribute_id = 5
  2699.         LEFT OUTER JOIN customer_entity_varchar cev_confirm ON cev_confirm.entity_id = ce.entity_id AND cev_confirm.attribute_id = 16
  2700.     WHERE
  2701.         CASE
  2702.             WHEN custId IS NOT null
  2703.                 THEN ce.entity_id > custId
  2704.             ELSE 1=1
  2705.         END
  2706.         AND cev_confirm.value_id IS NULL
  2707.         AND TRIM(ce.email) <> ''
  2708.        
  2709.     GROUP BY ce.entity_id, gmt.reason
  2710.     ORDER BY ce.entity_id, order_id;
  2711.  
  2712.     DROP TEMPORARY TABLE gconomy_month_transactions;
  2713.    
  2714. END */;;
  2715. DELIMITER ;
  2716. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2717. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2718. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2719. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2720. /*!50003 DROP PROCEDURE IF EXISTS `MG_GET_PRODUCT_MOST_USED` */;
  2721. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2722. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2723. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2724. /*!50003 SET character_set_client  = utf8 */ ;
  2725. /*!50003 SET character_set_results = utf8 */ ;
  2726. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2727. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2728. /*!50003 SET sql_mode              = '' */ ;
  2729. DELIMITER ;;
  2730. /*!50003 CREATE*/ /*!50020 DEFINER=`magento`@`%`*/ /*!50003 PROCEDURE `MG_GET_PRODUCT_MOST_USED`()
  2731. BEGIN
  2732. DROP TABLE if exists catalog_product_entity_most_used;
  2733. CREATE TABLE catalog_product_entity_most_used(
  2734. product_id INT ,
  2735. orders  INT,
  2736. PRIMARY KEY (product_id)
  2737. );
  2738.  
  2739. INSERT INTO catalog_product_entity_most_used (product_id, orders)
  2740. SELECT `sfoi`.`product_id`,
  2741.     COUNT(order_id) AS `orders`
  2742. FROM `sales_flat_order_item` AS `sfoi`
  2743.     WHERE `sfoi`.`product_id` IS NOT NULL
  2744.         AND (DATEDIFF(CURDATE(), sfoi.`created_at`) < 7)
  2745. GROUP BY `product_id`;
  2746.  
  2747. END */;;
  2748. DELIMITER ;
  2749. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2750. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2751. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2752. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2753. /*!50003 DROP PROCEDURE IF EXISTS `MG_GET_ZIP_MOST_USED` */;
  2754. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2755. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2756. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2757. /*!50003 SET character_set_client  = utf8 */ ;
  2758. /*!50003 SET character_set_results = utf8 */ ;
  2759. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2760. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2761. /*!50003 SET sql_mode              = '' */ ;
  2762. DELIMITER ;;
  2763. /*!50003 CREATE*/ /*!50020 DEFINER=`magento`@`%`*/ /*!50003 PROCEDURE `MG_GET_ZIP_MOST_USED`()
  2764. BEGIN
  2765. SELECT date( date_sub( date(now()), interval  90 DAY)),date(now()) INTO @start_date,@end_date;
  2766. DROP TABLE if exists temp_login_id ;
  2767. CREATE TEMPORARY table temp_login_id
  2768. as
  2769. SELECT distinct customer_id FROM log_customer lc
  2770.     WHERE date(lc.login_at) BETWEEN @start_date AND @end_date ;
  2771. DROP TABLE if exists zip_most_used;
  2772. CREATE TABLE zip_most_used
  2773. as
  2774. SELECT zip_code.value,count(ce.entity_id) as cnt_customers
  2775.     FROM customer_entity ce
  2776.         INNER JOIN customer_address_entity cae ON cae.parent_id = ce.entity_id
  2777.         INNER JOIN customer_address_entity_varchar zip_code
  2778.             ON zip_code.entity_id =cae.entity_id AND zip_code.attribute_id = 29        
  2779.                 INNER JOIN temp_login_id lc ON lc.customer_id = ce.entity_id   
  2780.   WHERE trim(zip_code.value)<>''  
  2781.  GROUP BY zip_code.value
  2782. ORDER by cnt_customers desc limit 5000;
  2783. drop table if exists temp_login_id ;
  2784. END */;;
  2785. DELIMITER ;
  2786. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  2787. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  2788. /*!50003 SET character_set_results = @saved_cs_results */ ;
  2789. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  2790. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_catalog_product` */;
  2791. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  2792. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  2793. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  2794. /*!50003 SET character_set_client  = utf8 */ ;
  2795. /*!50003 SET character_set_results = utf8 */ ;
  2796. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  2797. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  2798. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  2799. DELIMITER ;;
  2800. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_catalog_product`()
  2801. BEGIN
  2802. declare newValue int(10);
  2803. set @entity_type_id = 4;
  2804.  
  2805. truncate table curbside_subscription;
  2806. truncate table curbside_addresses;
  2807.  
  2808. truncate table earns_customers_partners;
  2809. truncate table earns_customers;
  2810.  
  2811. truncate table online_products_line_items;
  2812. truncate table online_products_codes;
  2813. truncate table online_products_deliveries;
  2814.  
  2815. truncate table pah_coupons_printing_offers;
  2816. truncate table pah_coupons_printings;
  2817. truncate table pah_coupons_ci_analytics;
  2818. truncate table pah_coupons_history;
  2819. truncate table pah_coupons;
  2820. truncate table pah_coupons_offers;
  2821.  
  2822. truncate table sales_flat_order_item;
  2823. truncate table sales_flat_order;
  2824. TRUNCATE TABLE catalog_product_entity_media_gallery;
  2825. TRUNCATE TABLE partners_local_rewards_stores;
  2826. delete from catalog_product_entity;
  2827.  
  2828. set newValue = 1;
  2829. select ifnull((max(entity_id) + 1), 1) into newValue from catalog_product_entity;
  2830.  
  2831. set @qwr = concat('ALTER TABLE catalog_product_entity auto_increment = ', newValue, ';');
  2832. PREPARE stmt FROM @qwr;
  2833. EXECUTE stmt;
  2834. ALTER TABLE catalog_product_entity_datetime auto_increment =1;
  2835. ALTER TABLE catalog_product_entity_decimal auto_increment =1;
  2836. ALTER TABLE catalog_product_entity_int auto_increment =1;
  2837. ALTER TABLE catalog_product_entity_text auto_increment =1;
  2838. ALTER TABLE catalog_product_entity_varchar auto_increment =1;
  2839.  
  2840.  
  2841. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  2842.     WHERE table_schema = DATABASE()
  2843.         and table_name = 'catalog_product_entity'
  2844.         and column_name = 'partner_id') then
  2845.     alter table catalog_product_entity add column (partner_id int(10) unsigned null);
  2846. end if;
  2847. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  2848.     WHERE table_schema = DATABASE()
  2849.         and table_name = 'catalog_product_entity'
  2850.         and column_name = 'barcode_url') then
  2851.     alter table catalog_product_entity add column (barcode_url varchar(255) null);
  2852. end if;
  2853. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  2854.     WHERE table_schema = DATABASE()
  2855.         and table_name = 'catalog_product_entity'
  2856.         and column_name = 'has_barcode') then
  2857.     alter table catalog_product_entity add column (has_barcode tinyint(1) null);
  2858. end if;
  2859. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  2860.     WHERE table_schema = DATABASE()
  2861.         and table_name = 'catalog_product_entity'
  2862.         and column_name = 'reward_old_id') then
  2863.     alter table catalog_product_entity add column (reward_old_id int(11) null);
  2864. end if;
  2865. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  2866.     WHERE table_schema = DATABASE()
  2867.         and table_name = 'catalog_product_entity'
  2868.         and column_name = 'is_uk') then
  2869.     alter table catalog_product_entity add column (is_uk tinyint(1) null default 0);
  2870. end if;
  2871.  
  2872. if not exists(SELECT *
  2873.         FROM information_schema.statistics
  2874.         WHERE table_schema = DATABASE()
  2875.                 and table_name = 'catalog_product_entity'
  2876.                 and index_name = 'IDX_catalog_product_entity_reward_old_id') then
  2877.     alter table catalog_product_entity add
  2878.     KEY `IDX_catalog_product_entity_reward_old_id` (`reward_old_id`);
  2879. end if;
  2880.  
  2881. insert into catalog_product_entity
  2882.     (
  2883.     entity_type_id,
  2884.     attribute_set_id,
  2885.     type_id,
  2886.     sku,
  2887.     has_options,
  2888.     required_options,
  2889.     created_at,
  2890.     updated_at,
  2891.     partner_id,
  2892.     reward_old_id,
  2893.     is_uk
  2894.     )
  2895. select @entity_type_id as 'entity_type_id',
  2896.     (select attribute_set_id from eav_attribute_set where attribute_set_name collate utf8_general_ci  = et.attribute_set collate utf8_general_ci and entity_type_id = @entity_type_id) as 'attribute_set_id',
  2897.     et.type as 'type_id',
  2898.     et.sku as 'sku',
  2899.     0 as 'has_options',
  2900.     0 as 'required_options',
  2901.     now() as 'created_at',
  2902.     now() as 'updated_at',
  2903.     l.new_id,
  2904.     et.reward_id,
  2905.     et.store_id
  2906. from tload_reward_export et
  2907.     left join catalog_product_entity ce on et.reward_id = ce.reward_old_id
  2908.     left join tload_partners_entity_map_id l on et.partner_id = l.old_id
  2909.         and case when et.reward_id > 9999990 then l.source_table in (2,6,7)
  2910.         else l.source_table in (1,3,8) end
  2911. where ce.entity_id is null;
  2912. set @attribute_name = '';
  2913.  
  2914. WHILE EXISTS (select column_name from INFORMATION_SCHEMA.COLUMNS
  2915.         where table_schema = DATABASE()
  2916.         and table_name = 'tload_reward_export'
  2917.         and column_name > @attribute_name
  2918.         and column_name not in ('attribute_set', 'partner_id', 'type', 'is_in_stock', 'low_stock_date', 'manage_stock', 'stock_id', 'stock_status_changed_automatically', 'use_config_manage_stock', 'use_config_max_sale_qty', 'use_config_min_sale_qty', 'use_config_notify_stock_qty', 'store_id')) DO
  2919. select column_name into @attribute_name from INFORMATION_SCHEMA.COLUMNS
  2920.         where table_schema = DATABASE()
  2921.         and table_name = 'tload_reward_export'
  2922.         and column_name > @attribute_name
  2923.         and column_name not in ('attribute_set', 'partner_id', 'type', 'is_in_stock', 'low_stock_date', 'manage_stock', 'stock_id', 'stock_status_changed_automatically', 'use_config_manage_stock', 'use_config_max_sale_qty', 'use_config_min_sale_qty', 'use_config_notify_stock_qty', 'store_id')
  2924. order by column_name asc
  2925. limit 1;
  2926. set @attribute_id = 0;
  2927. set @backend_type = '';
  2928. select attribute_id, backend_type into @attribute_id, @backend_type from eav_attribute
  2929. where entity_type_id = @entity_type_id
  2930.     and attribute_code = @attribute_name;
  2931. if (@backend_type = 'datetime') then
  2932.    
  2933.  
  2934.     set @qwr = concat('insert into catalog_product_entity_datetime
  2935.         (entity_type_id,
  2936.         attribute_id,
  2937.         store_id,
  2938.         entity_id,
  2939.         value
  2940.         )
  2941.     select @entity_type_id,
  2942.         ', @attribute_id,',
  2943.         0,
  2944.         e.entity_id,
  2945.         tl.',@attribute_name,'
  2946.     from tload_reward_export tl
  2947.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  2948.     ON DUPLICATE KEY UPDATE
  2949.         `value`=VALUES(`value`);');
  2950.     PREPARE stmt FROM @qwr;
  2951.     EXECUTE stmt;
  2952. end if;
  2953. if (@backend_type = 'decimal') then
  2954.    
  2955.  
  2956.     set @qwr = concat('insert into catalog_product_entity_decimal
  2957.         (entity_type_id,
  2958.         attribute_id,
  2959.         store_id,
  2960.         entity_id,
  2961.         value
  2962.         )
  2963.     select @entity_type_id,
  2964.         ', @attribute_id,',
  2965.         0,
  2966.         e.entity_id,
  2967.         tl.',@attribute_name,'
  2968.     from tload_reward_export tl
  2969.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  2970.     ON DUPLICATE KEY UPDATE
  2971.         `value`=VALUES(`value`);');
  2972.     PREPARE stmt FROM @qwr;
  2973.     EXECUTE stmt;
  2974. end if;
  2975. if (@backend_type = 'int') then
  2976.    
  2977.  
  2978.     set @qwr = concat('insert into catalog_product_entity_int
  2979.         (entity_type_id,
  2980.         attribute_id,
  2981.         store_id,
  2982.         entity_id,
  2983.         value
  2984.         )
  2985.     select @entity_type_id,
  2986.         ', @attribute_id,',
  2987.         0,
  2988.         e.entity_id,
  2989.         tl.',@attribute_name,'
  2990.     from tload_reward_export tl
  2991.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  2992.     ON DUPLICATE KEY UPDATE
  2993.         `value`=VALUES(`value`);');
  2994.     PREPARE stmt FROM @qwr;
  2995.     EXECUTE stmt;
  2996. end if;
  2997. if (@backend_type = 'text') then
  2998.    
  2999.  
  3000.     set @qwr = concat('insert into catalog_product_entity_text
  3001.         (entity_type_id,
  3002.         attribute_id,
  3003.         store_id,
  3004.         entity_id,
  3005.         value
  3006.         )
  3007.     select @entity_type_id,
  3008.         ', @attribute_id,',
  3009.         0,
  3010.         e.entity_id,
  3011.         ifnull(tl.',@attribute_name,','''')
  3012.     from tload_reward_export tl
  3013.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  3014.     ON DUPLICATE KEY UPDATE
  3015.         `value`=VALUES(`value`);');
  3016.     PREPARE stmt FROM @qwr;
  3017.     EXECUTE stmt;
  3018. end if;
  3019. if (@backend_type = 'varchar') then
  3020.  
  3021.  
  3022.     set @qwr = concat('insert into catalog_product_entity_varchar
  3023.         (entity_type_id,
  3024.         attribute_id,
  3025.         store_id,
  3026.         entity_id,
  3027.         value
  3028.         )
  3029.     select @entity_type_id,
  3030.         ', @attribute_id,',
  3031.         0,
  3032.         e.entity_id,
  3033.         ifnull(tl.',@attribute_name,','''')
  3034.     from tload_reward_export tl
  3035.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  3036.     ON DUPLICATE KEY UPDATE
  3037.         `value`=VALUES(`value`);');
  3038.     PREPARE stmt FROM @qwr;
  3039.     EXECUTE stmt;
  3040. end if;
  3041.  
  3042. if (@attribute_name = 'website_id') then
  3043.    
  3044.     INSERT INTO `catalog_product_website` (`product_id`, `website_id`)
  3045.     select e.entity_id, tl.website_id
  3046.     from tload_reward_export tl
  3047.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  3048.         left join catalog_product_website w on e.entity_id = w.product_id
  3049.                             and tl.website_id = w.website_id
  3050.     where w.product_id is null;
  3051. end if;
  3052.  
  3053. if (@attribute_name = 'qty') then
  3054.     DELETE c
  3055.     FROM `catalog_product_entity_tier_price` as c
  3056.         inner join catalog_product_entity e on c.entity_id = e.entity_id
  3057.         inner join tload_reward_export tl on tl.reward_id = e.reward_old_id;
  3058.     INSERT INTO `cataloginventory_stock_item` (`product_id`, `stock_id`, `qty`, `use_config_min_sale_qty`, `use_config_max_sale_qty`, `is_in_stock`, `use_config_notify_stock_qty`, `manage_stock`, `use_config_manage_stock`, `stock_status_changed_automatically`)
  3059.     select e.entity_id, tl.stock_id, tl.qty, tl.use_config_min_sale_qty, tl.use_config_max_sale_qty, tl.is_in_stock, tl.use_config_notify_stock_qty, tl.manage_stock, tl.use_config_manage_stock, tl.stock_status_changed_automatically
  3060.     from tload_reward_export tl
  3061.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  3062.     ON DUPLICATE KEY UPDATE
  3063.         `qty`=VALUES(`qty`),
  3064.         `use_config_min_sale_qty`=VALUES(`use_config_min_sale_qty`),
  3065.         `use_config_max_sale_qty`=VALUES(`use_config_max_sale_qty`),
  3066.         `is_in_stock`=VALUES(`is_in_stock`),
  3067.         `use_config_notify_stock_qty`=VALUES(`use_config_notify_stock_qty`),
  3068.         `manage_stock`=VALUES(`manage_stock`),
  3069.         `use_config_manage_stock`=VALUES(`use_config_manage_stock`),
  3070.         `stock_status_changed_automatically`=VALUES(`stock_status_changed_automatically`);
  3071.     INSERT INTO `cataloginventory_stock_status` (`product_id`, `website_id`, `stock_id`, `qty`, `stock_status`)
  3072.     select e.entity_id, tl.website_id, tl.stock_id, tl.qty, 1
  3073.     from tload_reward_export tl
  3074.         inner join catalog_product_entity e on tl.reward_id = e.reward_old_id
  3075.     ON DUPLICATE KEY UPDATE `qty`=VALUES(`qty`), `stock_status`=VALUES(`stock_status`);
  3076. end if;
  3077.  
  3078. END WHILE;
  3079.  
  3080.  
  3081. INSERT INTO partners_local_rewards_stores (product_id,store_id)
  3082. SELECT  cpe.entity_id,pra.id FROM partners_reward_address  pra
  3083. INNER JOIN  catalog_product_entity cpe on pra.partner_id = cpe.partner_id;
  3084.  
  3085. INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, VALUE)
  3086. SELECT 77, e.entity_id, tl.image
  3087. FROM tload_reward_export tl
  3088.     INNER JOIN catalog_product_entity e ON tl.reward_id = e.reward_old_id
  3089. ON DUPLICATE KEY UPDATE    `value`=VALUES(`value`);
  3090. INSERT INTO catalog_product_entity_media_gallery (attribute_id, entity_id, VALUE)
  3091. SELECT 77, e.entity_id, tl.small_image
  3092. FROM tload_reward_export tl
  3093.     INNER JOIN catalog_product_entity e ON tl.reward_id = e.reward_old_id
  3094. ON DUPLICATE KEY UPDATE    `value`=VALUES(`value`);
  3095. END */;;
  3096. DELIMITER ;
  3097. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3098. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3099. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3100. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3101. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_catalog_product_category` */;
  3102. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3103. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3104. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3105. /*!50003 SET character_set_client  = utf8 */ ;
  3106. /*!50003 SET character_set_results = utf8 */ ;
  3107. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3108. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3109. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3110. DELIMITER ;;
  3111. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_catalog_product_category`()
  3112. BEGIN
  3113. declare newValue int(10);
  3114. set @delete_count = 0;
  3115. select count(*) into @delete_count from catalog_category_entity
  3116. where path = 'new';
  3117. delete from catalog_category_entity
  3118. where path = 'new';
  3119. set @entity_type_id = 3;
  3120. set @attribute_id_name = 33;
  3121. select attribute_set_id into @attribute_set_id from eav_attribute_set where attribute_set_name = 'Default' and entity_type_id = @entity_type_id;
  3122. set @children_count = 0;
  3123. select count(*) into @children_count from tload_reward_category_export;
  3124. set @entity_id = 0;
  3125. select entity_id into @entity_id from `catalog_category_entity_varchar`
  3126. where attribute_id = @attribute_id_name
  3127.     and value = 'Rewards';
  3128. select path, `level` into @path, @level_ from `catalog_category_entity`
  3129. where entity_id = @entity_id;
  3130. select (count(*) + @delete_count) into @delete_count from `catalog_category_entity` e
  3131.     inner join `catalog_category_entity_varchar` ev on e.entity_id = ev.entity_id
  3132. where ev.attribute_id = @attribute_id_name
  3133.     and e.path like concat(@path, '%')
  3134.     and e.entity_id not in (@entity_id, 204, 206, 213, 214, 215, 216)
  3135.     and value not like '\_%';
  3136. delete e from `catalog_category_entity` e
  3137.     inner join `catalog_category_entity_varchar` ev on e.entity_id = ev.entity_id
  3138. where ev.attribute_id = @attribute_id_name
  3139.     and e.path like concat(@path, '%')
  3140.     and e.entity_id not in (@entity_id, 204, 206, 213, 214, 215, 216)
  3141.     and value not like '\_%';
  3142.  
  3143. while (@entity_id <> 0) do
  3144.     select parent_id into @parent_id from `catalog_category_entity`
  3145.     where entity_id = @entity_id;
  3146.    
  3147.    
  3148.    
  3149.     UPDATE `catalog_category_entity` SET `children_count` = `children_count` - @delete_count + @children_count
  3150.     WHERE entity_id = @entity_id;
  3151.     set @entity_id = @parent_id;
  3152. end while;
  3153.  
  3154. set @parent_id = 0;
  3155. select entity_id into @parent_id from `catalog_category_entity_varchar`
  3156. where attribute_id = @attribute_id_name
  3157.     and value = 'Rewards';
  3158.  
  3159. set newValue = 1;
  3160. select ifnull((max(entity_id) + 1), 1) into newValue from catalog_category_entity;
  3161.  
  3162. set @qwr = concat('ALTER TABLE catalog_category_entity auto_increment = ', newValue, ';');
  3163. PREPARE stmt FROM @qwr;
  3164. EXECUTE stmt;
  3165. set @qwr = concat('ALTER TABLE catalog_category_entity_datetime auto_increment = ', newValue, ';');
  3166. PREPARE stmt FROM @qwr;
  3167. EXECUTE stmt;
  3168. set @qwr = concat('ALTER TABLE catalog_category_entity_decimal auto_increment = ', newValue, ';');
  3169. PREPARE stmt FROM @qwr;
  3170. EXECUTE stmt;
  3171. set @qwr = concat('ALTER TABLE catalog_category_entity_int auto_increment = ', newValue, ';');
  3172. PREPARE stmt FROM @qwr;
  3173. EXECUTE stmt;
  3174. set @qwr = concat('ALTER TABLE catalog_category_entity_text auto_increment = ', newValue, ';');
  3175. PREPARE stmt FROM @qwr;
  3176. EXECUTE stmt;
  3177. set @qwr = concat('ALTER TABLE catalog_category_entity_varchar auto_increment = ', newValue, ';');
  3178. PREPARE stmt FROM @qwr;
  3179. EXECUTE stmt;
  3180.  
  3181. INSERT INTO `catalog_category_entity` (`path`, `attribute_set_id`, `entity_type_id`, `created_at`, `updated_at`, `position`, `level`, `parent_id`)
  3182. select concat('new'), @attribute_set_id, @entity_type_id, now(), now(), external_id, (@level_ + 1), @parent_id
  3183. from tload_reward_category_export
  3184. order by external_id asc;
  3185. set @attribute_name = '';
  3186. WHILE EXISTS (select column_name from INFORMATION_SCHEMA.COLUMNS
  3187.         where table_schema = DATABASE()
  3188.         and table_name = 'tload_reward_category_export'
  3189.         and column_name > @attribute_name
  3190.         and column_name not in ('attribute_set', 'partner_id', 'type', 'is_in_stock', 'low_stock_date', 'manage_stock', 'stock_id', 'stock_status_changed_automatically', 'use_config_manage_stock', 'use_config_max_sale_qty', 'use_config_min_sale_qty', 'use_config_notify_stock_qty')) DO
  3191. select column_name into @attribute_name from INFORMATION_SCHEMA.COLUMNS
  3192.         where table_schema = DATABASE()
  3193.         and table_name = 'tload_reward_category_export'
  3194.         and column_name > @attribute_name
  3195.         and column_name not in ('attribute_set', 'partner_id', 'type', 'is_in_stock', 'low_stock_date', 'manage_stock', 'stock_id', 'stock_status_changed_automatically', 'use_config_manage_stock', 'use_config_max_sale_qty', 'use_config_min_sale_qty', 'use_config_notify_stock_qty')
  3196. order by column_name asc
  3197. limit 1;
  3198. set @attribute_id = 0;
  3199. set @backend_type = '';
  3200. select attribute_id, backend_type into @attribute_id, @backend_type from eav_attribute
  3201. where entity_type_id = @entity_type_id
  3202.     and attribute_code = @attribute_name;
  3203. if (@backend_type = 'datetime') then
  3204.     set @qwr = concat('insert into catalog_category_entity_datetime
  3205.         (entity_type_id,
  3206.         attribute_id,
  3207.         store_id,
  3208.         entity_id,
  3209.         value
  3210.         )
  3211.     select ', @entity_type_id, ',
  3212.         ', @attribute_id,',
  3213.         0,
  3214.         e.entity_id,
  3215.         tl.',@attribute_name,'
  3216.     from tload_reward_category_export tl
  3217.         inner join catalog_category_entity e on tl.external_id = e.position and e.path = ''new''
  3218.     ON DUPLICATE KEY UPDATE
  3219.         `value`=VALUES(`value`);');
  3220.     PREPARE stmt FROM @qwr;
  3221.     EXECUTE stmt;
  3222. end if;
  3223. if (@backend_type = 'decimal') then
  3224.     set @qwr = concat('insert into catalog_category_entity_decimal
  3225.         (entity_type_id,
  3226.         attribute_id,
  3227.         store_id,
  3228.         entity_id,
  3229.         value
  3230.         )
  3231.     select ', @entity_type_id, ',
  3232.         ', @attribute_id,',
  3233.         0,
  3234.         e.entity_id,
  3235.         tl.',@attribute_name,'
  3236.     from tload_reward_category_export tl
  3237.         inner join catalog_category_entity e on tl.external_id = e.position and e.path = ''new''
  3238.     ON DUPLICATE KEY UPDATE
  3239.         `value`=VALUES(`value`);');
  3240.     PREPARE stmt FROM @qwr;
  3241.     EXECUTE stmt;
  3242. end if;
  3243. if (@backend_type = 'int') then
  3244.     set @qwr = concat('insert into catalog_category_entity_int
  3245.         (entity_type_id,
  3246.         attribute_id,
  3247.         store_id,
  3248.         entity_id,
  3249.         value
  3250.         )
  3251.     select ', @entity_type_id, ',
  3252.         ', @attribute_id,',
  3253.         0,
  3254.         e.entity_id,
  3255.         tl.',@attribute_name,'
  3256.     from tload_reward_category_export tl
  3257.         inner join catalog_category_entity e on tl.external_id = e.position and e.path = ''new''
  3258.     ON DUPLICATE KEY UPDATE
  3259.         `value`=VALUES(`value`);');
  3260.     PREPARE stmt FROM @qwr;
  3261.     EXECUTE stmt;
  3262. end if;
  3263. if (@backend_type = 'text') then
  3264.     set @qwr = concat('insert into catalog_category_entity_text
  3265.         (entity_type_id,
  3266.         attribute_id,
  3267.         store_id,
  3268.         entity_id,
  3269.         value
  3270.         )
  3271.     select ', @entity_type_id, ',
  3272.         ', @attribute_id,',
  3273.         0,
  3274.         e.entity_id,
  3275.         ifnull(tl.',@attribute_name,','''')
  3276.     from tload_reward_category_export tl
  3277.         inner join catalog_category_entity e on tl.external_id = e.position and e.path = ''new''
  3278.     ON DUPLICATE KEY UPDATE
  3279.         `value`=VALUES(`value`);');
  3280.     PREPARE stmt FROM @qwr;
  3281.     EXECUTE stmt;
  3282. end if;
  3283. if (@backend_type = 'varchar') then
  3284.     set @qwr = concat('insert into catalog_category_entity_varchar
  3285.         (entity_type_id,
  3286.         attribute_id,
  3287.         store_id,
  3288.         entity_id,
  3289.         value
  3290.         )
  3291.     select ', @entity_type_id, ',
  3292.         ', @attribute_id,',
  3293.         0,
  3294.         e.entity_id,
  3295.         ifnull(tl.',@attribute_name,','''')
  3296.     from tload_reward_category_export tl
  3297.         inner join catalog_category_entity e on tl.external_id = e.position and e.path = ''new''
  3298.     ON DUPLICATE KEY UPDATE
  3299.         `value`=VALUES(`value`);');
  3300.     PREPARE stmt FROM @qwr;
  3301.     EXECUTE stmt;
  3302. end if;
  3303. END WHILE;
  3304. INSERT INTO `catalog_category_product` (`category_id`,`product_id`,`position`)
  3305. select e.entity_id, pe.entity_id, 0
  3306. from catalog_category_entity e
  3307.     inner join tload_reward_category_link_export cl on e.position = cl.category_id_new
  3308.     inner join catalog_product_entity pe on cl.reward_id = pe.reward_old_id
  3309. where
  3310.     e.path = 'new'
  3311.     and cl.category_id_new <> 17;
  3312. UPDATE `catalog_category_entity` SET `path` = concat(@path, '/', entity_id) WHERE path = 'new';
  3313.  
  3314. END */;;
  3315. DELIMITER ;
  3316. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3317. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3318. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3319. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3320. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_curbside_subscr` */;
  3321. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3322. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3323. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3324. /*!50003 SET character_set_client  = utf8 */ ;
  3325. /*!50003 SET character_set_results = utf8 */ ;
  3326. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3327. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3328. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3329. DELIMITER ;;
  3330. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_curbside_subscr`()
  3331. BEGIN
  3332. truncate table curbside_subscription ;
  3333. truncate table curbside_addresses ;
  3334. if not exists (select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  3335.     WHERE table_schema = DATABASE()
  3336.         and table_name = 'curbside_subscription'
  3337.         and constraint_name = 'FK_CURBSIDE_SUBSCR_EARN_ID') then
  3338.     alter table curbside_subscription add CONSTRAINT FK_CURBSIDE_SUBSCR_EARN_ID FOREIGN KEY(earn_id)
  3339.     REFERENCES `earns_customers` (`id`) ON DELETE SET NULL ON UPDATE CASCADE;  
  3340. end if;
  3341. if not exists (select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  3342.     WHERE table_schema = DATABASE()
  3343.         and table_name = 'curbside_subscription'
  3344.         and constraint_name = 'FK_CURBSIDE_SUBSCR_CUSTOMER_ID') then
  3345.     alter table curbside_subscription add CONSTRAINT FK_CURBSIDE_SUBSCR_CUSTOMER_ID FOREIGN KEY(customer_id)
  3346.     REFERENCES `customer_entity` (`entity_id`) ON DELETE SET NULL ON UPDATE CASCADE;   
  3347. end if;
  3348. if not exists (select * from INFORMATION_SCHEMA.TABLE_CONSTRAINTS
  3349.     WHERE table_schema = DATABASE()
  3350.         and table_name = 'curbside_addresses'
  3351.         and constraint_name = 'FK_CURBSIDE_ADDR_CUSTOMER_ID') then
  3352.     alter table curbside_addresses add CONSTRAINT FK_CURBSIDE_ADDR_CUSTOMER_ID FOREIGN KEY(customer_id)
  3353.     REFERENCES `customer_entity` (`entity_id`) ON DELETE SET NULL ON UPDATE CASCADE;   
  3354. end if;
  3355. INSERT INTO curbside_addresses
  3356. (id,customer_id,`location_id`,zip,city,state,address,suite,verified_at)
  3357. SELECT id,null as customer_id,location_id,zip,city,state,address,suite,verified_at
  3358. FROM tload_curbside_addresses ;
  3359.  
  3360. if not exists(SELECT *
  3361.         FROM information_schema.statistics
  3362.         WHERE table_schema = DATABASE()
  3363.                 and table_name = 'tload_curbside_subscription'
  3364.                 and index_name = 'ix_tload_curbside_subscription_customer_id') then
  3365.     alter table tload_curbside_subscription add index ix_tload_curbside_subscription_customer_id (customer_id);
  3366. end if;
  3367.  
  3368. if not exists(SELECT *
  3369.         FROM information_schema.statistics
  3370.         WHERE table_schema = DATABASE()
  3371.                 and table_name = 'tload_curbside_addresses'
  3372.                 and index_name = 'ix_tload_curbside_addresses_sid') then
  3373.     alter table tload_curbside_addresses add index ix_tload_curbside_addresses_sid (old_subscription_id);
  3374. end if;
  3375. if not exists(SELECT *
  3376.         FROM information_schema.statistics
  3377.         WHERE table_schema = DATABASE()
  3378.                 and table_name = 'tload_curbside_subscription'
  3379.                 and index_name = 'ix_tload_curbside_subscription_sid') then
  3380.     alter table tload_curbside_subscription add index ix_tload_curbside_subscription_sid (old_subscription_id);
  3381. end if;
  3382. INSERT INTO curbside_subscription
  3383. (subscription_id,earn_id,customer_id,curbside_address_id,activation_code_id,is_self_reporting,created_at,start_date,end_date)
  3384. SELECT distinct subscription_id,earn_id,null as customer_id,tca.id,null as activation_code_id,is_self_reporting,created_at,start_date,end_date
  3385.     FROM tload_curbside_subscription tcs
  3386. INNER JOIN tload_curbside_addresses tca on tcs.old_subscription_id = tca.old_subscription_id ;
  3387. UPDATE curbside_subscription cs
  3388.     INNER JOIN tload_curbside_subscription tcs on cs.subscription_id=tcs.subscription_id
  3389.     INNER JOIN customer_entity  ce on ce.account_old_id = tcs.customer_id
  3390. SET cs.customer_id = ce.entity_id;
  3391. if not exists(SELECT *
  3392.         FROM information_schema.statistics
  3393.         WHERE table_schema = DATABASE()
  3394.                 and table_name = 'tload_curbside_addresses'
  3395.                 and index_name = 'ix_tload_curbside_addresses_customer_id') then
  3396.     alter table tload_curbside_addresses add index ix_tload_curbside_addresses_customer_id (customer_id);
  3397. end if;
  3398. UPDATE curbside_addresses ca
  3399.     INNER JOIN tload_curbside_addresses tca on ca.id=tca.id
  3400.     INNER JOIN customer_entity  ce on ce.account_old_id =tca.customer_id
  3401.  
  3402.    
  3403. SET ca.customer_id = ce.entity_id;
  3404.  
  3405.        
  3406. END */;;
  3407. DELIMITER ;
  3408. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3409. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3410. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3411. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3412. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_customer` */;
  3413. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3414. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3415. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3416. /*!50003 SET character_set_client  = utf8 */ ;
  3417. /*!50003 SET character_set_results = utf8 */ ;
  3418. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3419. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3420. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3421. DELIMITER ;;
  3422. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_customer`()
  3423. BEGIN
  3424. declare newValue int(10);
  3425.  
  3426. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  3427.     WHERE table_schema = DATABASE()
  3428.         and table_name = 'customer_entity'
  3429.         and column_name = 'account_old_id') then
  3430.     alter table customer_entity add column (account_old_id int(11) null);
  3431. end if;
  3432.  
  3433. if not exists(SELECT *
  3434.         FROM information_schema.statistics
  3435.         WHERE table_schema = DATABASE()
  3436.                 and table_name = 'customer_entity'
  3437.                 and index_name = 'IDX_catalog_product_entity_account_old_id') then
  3438.     alter table customer_entity add KEY IDX_catalog_product_entity_account_old_id (account_old_id);
  3439. end if;
  3440.  
  3441. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  3442.     WHERE table_schema = DATABASE()
  3443.         and table_name = 'tload_contact_export'
  3444.         and column_name = 'contact_new_id') then
  3445.     alter table tload_contact_export add column (contact_new_id int(11) null);
  3446. end if;
  3447.  
  3448. if not exists(SELECT *
  3449.         FROM information_schema.statistics
  3450.         WHERE table_schema = DATABASE()
  3451.                 and table_name = 'customer_entity'
  3452.                 and index_name = 'IDX_tload_contact_export_contact_new_id') then
  3453.     alter table tload_contact_export add KEY IDX_tload_contact_export_contact_new_id (contact_new_id);
  3454. end if;
  3455.  
  3456. truncate table online_products_line_items;
  3457. truncate table online_products_codes;
  3458. truncate table online_products_deliveries;
  3459. truncate table pah_coupons_printing_offers;
  3460. truncate table pah_coupons_printings;
  3461. truncate table pah_coupons_ci_analytics;
  3462. truncate table pah_coupons_history;
  3463. truncate table pah_coupons;
  3464. truncate table pah_coupons_offers;
  3465. truncate table customer_mobile;
  3466. truncate table customer_entity_datetime;
  3467. truncate table customer_entity_decimal;
  3468. truncate table customer_entity_int;
  3469. truncate table customer_entity_text;
  3470. truncate table customer_entity_varchar;
  3471. truncate table customer_address_entity_datetime;
  3472. truncate table customer_address_entity_decimal;
  3473. truncate table customer_address_entity_int;
  3474. truncate table customer_address_entity_text;
  3475. truncate table customer_address_entity_varchar;
  3476. truncate table customer_address_entity;
  3477. truncate table enterprise_customerbalance_history;
  3478. truncate table enterprise_customersegment_segment;
  3479. truncate table enterprise_customerbalance;
  3480. truncate table sales_flat_order_item;
  3481. truncate table sales_flat_order;
  3482. truncate table curbside_subscription;
  3483. truncate table earns_users;
  3484. truncate table customer_entity;
  3485.  
  3486. set @entity_type_id = 1;
  3487.  
  3488. INSERT INTO `customer_entity` (`entity_type_id`, `website_id`, `group_id`, `email`, `store_id`, `created_at`, `updated_at`, `account_old_id`,`account_number`)
  3489. SELECT @entity_type_id, website_id, group_id, username, store_id, created_at, updated_at, account_id, account_number
  3490. FROM tload_contact_export;
  3491.  
  3492.  
  3493. UPDATE tload_contact_export tl
  3494.         inner join customer_entity e on tl.account_id = e.account_old_id
  3495. SET tl.contact_new_id = e.entity_id;
  3496.  
  3497. UPDATE customer_entity
  3498. SET increment_id = LPAD(entity_id, 9, '0');
  3499.  
  3500. SELECT LPAD(MAX(entity_id), 9, '0') INTO @increment_id
  3501. FROM customer_entity;
  3502.  
  3503. UPDATE `eav_entity_store`
  3504. SET `increment_last_id` = @increment_id  
  3505. WHERE (entity_store_id='6');
  3506.  
  3507. set @attribute_name = '';
  3508. WHILE EXISTS (select column_name from INFORMATION_SCHEMA.COLUMNS
  3509.         where table_schema = DATABASE()
  3510.         and table_name = 'tload_contact_export'
  3511.         and column_name > @attribute_name
  3512.         and column_name not in ('website_id', 'group_id', 'store_id', 'email','account_number', 'contact_new_id')) DO
  3513.  
  3514. select column_name into @attribute_name from INFORMATION_SCHEMA.COLUMNS
  3515.         where table_schema = DATABASE()
  3516.         and table_name = 'tload_contact_export'
  3517.         and column_name > @attribute_name
  3518.         and column_name not in ('website_id', 'group_id', 'store_id', 'email','account_number', 'contact_new_id')
  3519. order by column_name asc
  3520. limit 1;
  3521. set @attribute_id = 0;
  3522. set @backend_type = '';
  3523. select attribute_id, backend_type into @attribute_id, @backend_type from eav_attribute
  3524. where entity_type_id = @entity_type_id
  3525.     and attribute_code = @attribute_name;
  3526. if (@backend_type = 'datetime') then
  3527.     set @qwr = concat('insert into customer_entity_datetime
  3528.         (entity_type_id,
  3529.         attribute_id,
  3530.         entity_id,
  3531.         value
  3532.         )
  3533.     select ', @entity_type_id, ',
  3534.         ', @attribute_id,',
  3535.         tl.contact_new_id,
  3536.         tl.',@attribute_name,'
  3537.     from tload_contact_export tl
  3538.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3539.     ON DUPLICATE KEY UPDATE
  3540.         `value`=VALUES(`value`);');
  3541.     PREPARE stmt FROM @qwr;
  3542.     EXECUTE stmt;
  3543. end if;
  3544. if (@backend_type = 'decimal') then
  3545.     set @qwr = concat('insert into customer_entity_decimal
  3546.         (entity_type_id,
  3547.         attribute_id,
  3548.         entity_id,
  3549.         value
  3550.         )
  3551.     select ', @entity_type_id, ',
  3552.         ', @attribute_id,',
  3553.         tl.contact_new_id,
  3554.         tl.',@attribute_name,'
  3555.     from tload_contact_export tl
  3556.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3557.     ON DUPLICATE KEY UPDATE
  3558.         `value`=VALUES(`value`);');
  3559.     PREPARE stmt FROM @qwr;
  3560.     EXECUTE stmt;
  3561. end if;
  3562. if (@backend_type = 'int') then
  3563.     set @qwr = concat('insert into customer_entity_int
  3564.         (entity_type_id,
  3565.         attribute_id,
  3566.         entity_id,
  3567.         value
  3568.         )
  3569.     select ', @entity_type_id, ',
  3570.         ', @attribute_id,',
  3571.         tl.contact_new_id,
  3572.         tl.',@attribute_name,'
  3573.     from tload_contact_export tl
  3574.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3575.     ON DUPLICATE KEY UPDATE
  3576.         `value`=VALUES(`value`);');
  3577.     PREPARE stmt FROM @qwr;
  3578.     EXECUTE stmt;
  3579. end if;
  3580. if (@backend_type = 'text') then
  3581.     set @qwr = concat('insert into customer_entity_text
  3582.         (entity_type_id,
  3583.         attribute_id,
  3584.         entity_id,
  3585.         value
  3586.         )
  3587.     select ', @entity_type_id, ',
  3588.         ', @attribute_id,',
  3589.         tl.contact_new_id,
  3590.         ifnull(tl.',@attribute_name,','''')
  3591.     from tload_contact_export tl
  3592.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3593.     ON DUPLICATE KEY UPDATE
  3594.         `value`=VALUES(`value`);');
  3595.     PREPARE stmt FROM @qwr;
  3596.     EXECUTE stmt;
  3597. end if;
  3598. if (@backend_type = 'varchar') then
  3599.     set @qwr = concat('insert into customer_entity_varchar
  3600.         (entity_type_id,
  3601.         attribute_id,
  3602.         entity_id,
  3603.         value
  3604.         )
  3605.     select ', @entity_type_id, ',
  3606.         ', @attribute_id,',
  3607.         tl.contact_new_id,
  3608.         ifnull(tl.',@attribute_name,','''')
  3609.     from tload_contact_export tl
  3610.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3611.     ON DUPLICATE KEY UPDATE
  3612.         `value`=VALUES(`value`);');
  3613.     PREPARE stmt FROM @qwr;
  3614.     EXECUTE stmt;
  3615. end if;
  3616.  
  3617. END WHILE;
  3618.  
  3619.  
  3620. insert into customer_mobile (customer_id, phone_number,  is_active)
  3621. select contact_new_id, customer_mobile, 0
  3622. from tload_contact_export
  3623. WHERE customer_mobile IS NOT NULL;
  3624. END */;;
  3625. DELIMITER ;
  3626. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3627. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3628. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3629. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3630. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_customer_address` */;
  3631. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3632. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3633. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3634. /*!50003 SET character_set_client  = utf8 */ ;
  3635. /*!50003 SET character_set_results = utf8 */ ;
  3636. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3637. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3638. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3639. DELIMITER ;;
  3640. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_customer_address`()
  3641. BEGIN
  3642. declare newValue int(10);
  3643.  
  3644. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  3645.     WHERE table_schema = DATABASE()
  3646.         and table_name = 'customer_address_entity'
  3647.         and column_name = 'address_old_id') then
  3648.     alter table customer_address_entity add column (address_old_id int(11) null);
  3649. end if;
  3650.  
  3651. if not exists(SELECT *
  3652.         FROM information_schema.statistics
  3653.         WHERE table_schema = DATABASE()
  3654.                 and table_name = 'customer_address_entity'
  3655.                 and index_name = 'IDX_customer_address_entity_address_old_id') then
  3656.     alter table customer_address_entity add KEY IDX_customer_address_entity_address_old_id (address_old_id);
  3657. end if;
  3658.  
  3659. IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
  3660.     WHERE table_schema = DATABASE()
  3661.         AND table_name = 'tload_contact_address_export'
  3662.         AND column_name = 'region_id') THEN
  3663.     ALTER TABLE tload_contact_address_export ADD COLUMN (region_id INT(11) NULL);
  3664. END IF;
  3665.  
  3666. IF NOT EXISTS(SELECT *
  3667.         FROM information_schema.statistics
  3668.         WHERE table_schema = DATABASE()
  3669.                 AND table_name = 'tload_contact_address_export'
  3670.                 AND index_name = 'IDX_tload_contact_address_export_region') THEN
  3671.     ALTER TABLE tload_contact_address_export ADD KEY IDX_tload_contact_address_export_region (region);
  3672. END IF;
  3673.  
  3674. UPDATE tload_contact_address_export r
  3675.     INNER JOIN directory_country_region_name rID ON r.`region` = rID.`name`
  3676. SET r.region_id = rID.region_id;
  3677.  
  3678. truncate table customer_address_entity_varchar;
  3679. truncate table customer_address_entity_text;
  3680. truncate table customer_address_entity_int;
  3681. truncate table customer_address_entity_decimal;
  3682. truncate table customer_address_entity_datetime;
  3683. truncate table customer_address_entity;
  3684. set @entity_type_id = 2;
  3685.  
  3686. INSERT INTO `customer_address_entity` (`entity_type_id`, `parent_id`, `created_at`, `updated_at`, address_old_id)
  3687. SELECT @entity_type_id, ce.entity_id, ta.created_at, ta.updated_at, ta.address_id
  3688. from tload_contact_address_export ta
  3689.     INNER JOIN customer_entity ce ON ta.account_id = ce.account_old_id;
  3690.  
  3691. UPDATE tload_contact_address_export ta
  3692.     INNER JOIN customer_address_entity ce ON ta.address_id = ce.address_old_id
  3693. set ta.address_new_id = ce.entity_id;
  3694.  
  3695. INSERT INTO `customer_entity_int` (`entity_type_id`, `entity_id`, `attribute_id`, `value`)
  3696. select 1, ce.entity_id, 13, ta.address_new_id from tload_contact_address_export ta
  3697.     inner join customer_entity ce on ta.account_id = ce.account_old_id
  3698. where ta.is_billing = 1
  3699.  ON DUPLICATE KEY UPDATE `value`=VALUES(`value`);
  3700. INSERT INTO `customer_entity_int` (`entity_type_id`, `entity_id`, `attribute_id`, `value`)
  3701. select 1, ce.entity_id, 14, ta.address_new_id from tload_contact_address_export ta
  3702.     inner join customer_entity ce on ta.account_id = ce.account_old_id
  3703. where ta.is_shipping = 1
  3704.  ON DUPLICATE KEY UPDATE `value`=VALUES(`value`);
  3705.  
  3706. set @attribute_name = '';
  3707. WHILE EXISTS (select column_name from INFORMATION_SCHEMA.COLUMNS
  3708.         where table_schema = DATABASE()
  3709.         and table_name = 'tload_contact_address_export'
  3710.         and column_name > @attribute_name
  3711.         and column_name not in ('address_id','address1','address2','contact_new_id', 'created_at', 'updated_at', 'is_billing', 'is_shipping', 'address_new_id','client_address_id','unit_num','building_id','qas_verify_level','qas_disable','route','address3')) DO
  3712.  
  3713. select column_name into @attribute_name from INFORMATION_SCHEMA.COLUMNS
  3714.         where table_schema = DATABASE()
  3715.         and table_name = 'tload_contact_address_export'
  3716.         and column_name > @attribute_name
  3717.         and column_name not in ('address_id', 'address1','address2','contact_new_id', 'created_at', 'updated_at', 'is_billing', 'is_shipping', 'address_new_id','client_address_id','unit_num','building_id','qas_verify_level','qas_disable','route','address3')
  3718. order by column_name asc
  3719. limit 1;
  3720. set @attribute_id = 0;
  3721. set @backend_type = '';
  3722. select attribute_id, backend_type into @attribute_id, @backend_type from eav_attribute
  3723. where entity_type_id = @entity_type_id
  3724.     and attribute_code = @attribute_name;
  3725. if (@backend_type = 'datetime') then
  3726.     set @qwr = concat('insert into customer_address_entity_datetime
  3727.         (entity_type_id,
  3728.         attribute_id,
  3729.         entity_id,
  3730.         value
  3731.         )
  3732.     select ', @entity_type_id, ',
  3733.         ', @attribute_id,',
  3734.         tl.address_new_id,
  3735.         tl.',@attribute_name,'
  3736.     from tload_contact_address_export tl
  3737.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3738.     ON DUPLICATE KEY UPDATE
  3739.         `value`=VALUES(`value`);');
  3740.     PREPARE stmt FROM @qwr;
  3741.     EXECUTE stmt;
  3742. end if;
  3743. if (@backend_type = 'decimal') then
  3744.     set @qwr = concat('insert into customer_address_entity_decimal
  3745.         (entity_type_id,
  3746.         attribute_id,
  3747.         entity_id,
  3748.         value
  3749.         )
  3750.     select ', @entity_type_id, ',
  3751.         ', @attribute_id,',
  3752.         tl.address_new_id,
  3753.         tl.',@attribute_name,'
  3754.     from tload_contact_address_export tl
  3755.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3756.     ON DUPLICATE KEY UPDATE
  3757.         `value`=VALUES(`value`);');
  3758.     PREPARE stmt FROM @qwr;
  3759.     EXECUTE stmt;
  3760. end if;
  3761. if (@backend_type = 'int') then
  3762.     set @qwr = concat('insert into customer_address_entity_int
  3763.         (entity_type_id,
  3764.         attribute_id,
  3765.         entity_id,
  3766.         value
  3767.         )
  3768.     select ', @entity_type_id, ',
  3769.         ', @attribute_id,',
  3770.         tl.address_new_id,
  3771.         tl.',@attribute_name,'
  3772.     from tload_contact_address_export tl
  3773.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3774.     ON DUPLICATE KEY UPDATE
  3775.         `value`=VALUES(`value`);');
  3776.     PREPARE stmt FROM @qwr;
  3777.     EXECUTE stmt;
  3778. end if;
  3779. if (@backend_type = 'text') then
  3780.     set @qwr = concat('insert into customer_address_entity_text
  3781.         (entity_type_id,
  3782.         attribute_id,
  3783.         entity_id,
  3784.         value
  3785.         )
  3786.     select ', @entity_type_id, ',
  3787.         ', @attribute_id,',
  3788.         tl.address_new_id,
  3789.         ifnull(tl.',@attribute_name,','''')
  3790.     from tload_contact_address_export tl
  3791.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3792.     ON DUPLICATE KEY UPDATE
  3793.         `value`=VALUES(`value`);');
  3794.     PREPARE stmt FROM @qwr;
  3795.     EXECUTE stmt;
  3796. end if;
  3797. if (@backend_type = 'varchar') then
  3798.     set @qwr = concat('insert into customer_address_entity_varchar
  3799.         (entity_type_id,
  3800.         attribute_id,
  3801.         entity_id,
  3802.         value
  3803.         )
  3804.     select ', @entity_type_id, ',
  3805.         ', @attribute_id,',
  3806.         tl.address_new_id,
  3807.         ifnull(tl.',@attribute_name,','''')
  3808.     from tload_contact_address_export tl
  3809.     where tl.',@attribute_name,' is not null and tl.',@attribute_name,' <> ''''
  3810.     ON DUPLICATE KEY UPDATE
  3811.         `value`=VALUES(`value`);');
  3812.     PREPARE stmt FROM @qwr;
  3813.     EXECUTE stmt;
  3814. end if;
  3815.  
  3816. END WHILE;
  3817.  
  3818.  
  3819. drop table if exists tload_customer_address_empty;
  3820. create table tload_customer_address_empty as
  3821. select c.entity_id as customer_id, cvf.value as firstname, cvl.value as lastname  from customer_entity c
  3822.     left join customer_address_entity a on c.entity_id = a.parent_id
  3823.     left join customer_entity_varchar cvf on c.entity_id = cvf.entity_id and cvf.attribute_id = 5
  3824.     left join customer_entity_varchar cvl on c.entity_id = cvl.entity_id and cvl.attribute_id = 7
  3825. where a.entity_id is null;
  3826. alter table tload_customer_address_empty add key ix_tload_customer_address_empty_customer_id (customer_id);
  3827.  
  3828. INSERT INTO `customer_address_entity` (`entity_type_id`, `parent_id`, `created_at`, `updated_at`)
  3829. SELECT 2, customer_id, now(), now()
  3830. from tload_customer_address_empty;
  3831.  
  3832. INSERT INTO `customer_entity_int` (`entity_type_id`, `entity_id`, `attribute_id`, `value`)
  3833. select 1, c.customer_id, 13, ca.entity_id
  3834. from tload_customer_address_empty c
  3835.     inner join customer_address_entity ca on c.customer_id = ca.parent_id
  3836.  ON DUPLICATE KEY UPDATE `value`=VALUES(`value`);
  3837. INSERT INTO `customer_entity_int` (`entity_type_id`, `entity_id`, `attribute_id`, `value`)
  3838. select 1, c.customer_id, 14, ca.entity_id
  3839. from tload_customer_address_empty c
  3840.     inner join customer_address_entity ca on c.customer_id = ca.parent_id
  3841.  ON DUPLICATE KEY UPDATE `value`=VALUES(`value`);
  3842.  
  3843. insert into customer_address_entity_varchar
  3844.     (entity_type_id,
  3845.     attribute_id,
  3846.     entity_id,
  3847.     value
  3848.     )
  3849. select @entity_type_id,
  3850.     19,
  3851.     ca.entity_id,
  3852.     ifnull(c.firstname,'')
  3853. from tload_customer_address_empty c
  3854. inner join customer_address_entity ca on c.customer_id = ca.parent_id
  3855. ON DUPLICATE KEY UPDATE
  3856.         `value`=VALUES(`value`);
  3857.  
  3858. insert into customer_address_entity_varchar
  3859.     (entity_type_id,
  3860.     attribute_id,
  3861.     entity_id,
  3862.     value
  3863.     )
  3864. select @entity_type_id,
  3865.     21,
  3866.     ca.entity_id,
  3867.     ifnull(c.lastname,'')
  3868. from tload_customer_address_empty c
  3869. inner join customer_address_entity ca on c.customer_id = ca.parent_id
  3870. ON DUPLICATE KEY UPDATE
  3871.         `value`=VALUES(`value`);
  3872.  
  3873. INSERT INTO customer_address_entity_varchar
  3874.     (entity_type_id,
  3875.     attribute_id,
  3876.     entity_id,
  3877.     VALUE
  3878.     )
  3879. SELECT @entity_type_id,
  3880.     26,
  3881.     ca.entity_id,
  3882.     'US'
  3883. FROM tload_customer_address_empty c
  3884. INNER JOIN customer_address_entity ca ON c.customer_id = ca.parent_id
  3885. ON DUPLICATE KEY UPDATE
  3886.         `value`=VALUES(`value`);
  3887. drop table if exists tload_customer_address_empty;
  3888. END */;;
  3889. DELIMITER ;
  3890. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3891. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3892. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3893. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3894. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_earn_users` */;
  3895. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3896. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3897. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3898. /*!50003 SET character_set_client  = utf8 */ ;
  3899. /*!50003 SET character_set_results = utf8 */ ;
  3900. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3901. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3902. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3903. DELIMITER ;;
  3904. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_earn_users`()
  3905. BEGIN
  3906. declare rb_UK_partner_id int(10);
  3907. declare rb_US_partner_id int(10);
  3908.  
  3909.  
  3910. TRUNCATE TABLE earns_customers_partners;
  3911. truncate table earns_customers;
  3912.  
  3913.  
  3914. INSERT INTO earns_customers
  3915. (id,customer_id,product_id,success_count,fail_count,extra,start_date,end_date,old_subscription_id)
  3916. SELECT
  3917. teu.id,null as user_id,cpev.entity_id, teu.success_count,0 as fail_count,null as extra
  3918. ,teu.start_date,teu.end_date,old_subscription_id
  3919. FROM tload_earns_customers teu
  3920.     INNER JOIN catalog_product_entity_varchar cpev
  3921.         on cpev.attribute_id = 60 and cpev.value = teu.product_id_name
  3922. WHERE cpev.value in ('Earn Curbside','Earn Curbside UK','Earn GConomy Card','Earn Other CUB Energy Saver','Earn Other WMECO','Earn Points Codes');
  3923.  
  3924. UPDATE earns_customers eu
  3925.     INNER JOIN tload_earns_customers teu on eu.id = teu.id
  3926. INNER JOIN customer_entity  ce on ce.account_old_id = teu.customer_id
  3927. SET eu.customer_id = ce.entity_id;
  3928. SELECT id INTO rb_UK_partner_id FROM partners_entity WHERE name ='RecycleBank UK';
  3929. SELECT id INTO rb_US_partner_id FROM partners_entity WHERE name ='RecycleBank US';
  3930. INSERT INTO earns_customers_partners (earns_customers_id,partner_id,partner_member_account_number)
  3931.     SELECT eu.id
  3932.         , case  
  3933.             WHEN teu.product_id_name= 'Earn Curbside'  THEN rb_UK_partner_id
  3934.             WHEN teu.product_id_name = 'Earn Curbside UK' THEN rb_US_partner_id                    
  3935.             ELSE
  3936.                 cpe.partner_id
  3937.             END as partner_id
  3938.         , teu.partner_member_account_number
  3939.                            
  3940.     FROM earns_customers eu
  3941.         INNER JOIN tload_earns_customers teu on eu.id= teu.id
  3942.                 INNER JOIN catalog_product_entity cpe on cpe.entity_id = eu.product_id;
  3943.        
  3944. END */;;
  3945. DELIMITER ;
  3946. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  3947. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  3948. /*!50003 SET character_set_results = @saved_cs_results */ ;
  3949. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  3950. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_online_online_code_table` */;
  3951. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  3952. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  3953. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  3954. /*!50003 SET character_set_client  = utf8 */ ;
  3955. /*!50003 SET character_set_results = utf8 */ ;
  3956. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  3957. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  3958. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  3959. DELIMITER ;;
  3960. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_online_online_code_table`()
  3961. BEGIN
  3962.  
  3963.  
  3964.  
  3965. truncate table online_products_line_items;
  3966. truncate table online_products_codes;
  3967. truncate table online_products_deliveries;
  3968. INSERT INTO online_products_line_items
  3969. (  `id`,`order_id`,`quantity_ordered`,`quantity_remain`,`line_status`,`promotion_id`,`online_code`,
  3970.   `date_used`,`date_shipped`,`price`,`created_at`,`updated_at`,`partner_id`,`product_id`,`quantity_voided`,
  3971.   `quantity_returned`,`type`,`source_redemption_line_item_id`,`reward_description`,`original_line_item_id`,
  3972.   `order_return_id`,`points_credited`,`source_catalog_number`,`return_reason_code`,`channel`,`old_id`
  3973. )
  3974. SELECT
  3975.   `id`,null,`quantity_ordered`,`quantity_remain`,`line_status`,`promotion_id`,`online_code`,
  3976.   `date_used`,`date_shipped`,`price`,`created_at`,`updated_at`,null,null,`quantity_voided`,
  3977.   `quantity_returned`,`type`,`source_redemption_line_item_id`,`reward_description`,`original_line_item_id`,
  3978.   `order_return_id`,`points_credited`,`source_catalog_number`,`return_reason_code`,`channel`,`old_id`
  3979. FROM tload_online_products_line_items ;
  3980. UPDATE online_products_line_items li
  3981. inner join tload_online_products_line_items tli on li.id = tli.id
  3982. inner JOIN tload_partners_entity_map_id p on tli.partner_id = p.old_id
  3983.             and p.source_table in (1,3,5)
  3984. SET li.partner_id = p.new_id;
  3985. UPDATE online_products_line_items li
  3986.     inner join tload_online_products_line_items tli on li.id = tli.id
  3987.     inner join catalog_product_entity pe on tli.product_id = pe.reward_old_id
  3988. SET li.product_id = pe.entity_id;
  3989. UPDATE online_products_line_items li
  3990.     inner join tload_online_products_line_items tli on li.id = tli.id
  3991.     inner join sales_flat_order o on tli.order_id = o.old_id
  3992. SET li.order_id = o.entity_id;
  3993.  
  3994.  INSERT INTO `online_products_deliveries`
  3995.  (`id`,`date_delivered`,`quantity`,`added_by_id`,`created_at`,`updated_at`,`supplier_type`,
  3996.   `unlimited`,`status`,`product_id`,`total_cost`,`date_expired`,`coupon_codes`,`auto_gen_codes`, `type_id`,`old_id` )
  3997.  SELECT
  3998.     `id`,`date_delivered`,`quantity`,`added_by_id`,`created_at`,`updated_at`,case when `supplier_type` = 0 then null else `supplier_type` end,
  3999.     `unlimited`,`status`,null,`total_cost`,`date_expired`,`coupon_codes`,`auto_gen_codes`, `type_id`,`old_id`
  4000.  FROM `tload_online_products_deliveries`;
  4001. UPDATE online_products_deliveries pd
  4002.     inner join tload_online_products_deliveries tpd on pd.id = tpd.id
  4003.     inner join catalog_product_entity pe on tpd.product_id = pe.reward_old_id
  4004. SET pd.product_id = pe.entity_id;
  4005.  
  4006. INSERT INTO online_products_codes
  4007. (`id`,`code`,`used`,`delivery_id`,`product_id`,`partner_id`,`expired_at`,`used_at`,`used_by`,`created_at`,`updated_at`,`line_item_id`,`old_id` )
  4008. SELECT `id`,`code`,`used`,`delivery_id`,null,null,`expired_at`,`used_at`,`used_by`,`created_at`,`updated_at`,`line_item_id`,`old_id`
  4009.     FROM  tload_online_products_codes;
  4010. UPDATE online_products_codes pc
  4011.     inner join tload_online_products_codes tpc on pc.id = tpc.id
  4012.     inner JOIN tload_partners_entity_map_id p on tpc.partner_id = p.old_id
  4013.             and p.source_table in (1,3,5)
  4014. SET pc.partner_id = p.new_id;
  4015. UPDATE online_products_codes pc
  4016.     inner join tload_online_products_codes tpc on pc.id = tpc.id
  4017.     inner join catalog_product_entity pe on tpc.product_id = pe.reward_old_id
  4018. SET pc.product_id = pe.entity_id;
  4019. END */;;
  4020. DELIMITER ;
  4021. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4022. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4023. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4024. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4025. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_ORDERS` */;
  4026. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4027. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4028. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4029. /*!50003 SET character_set_client  = utf8 */ ;
  4030. /*!50003 SET character_set_results = utf8 */ ;
  4031. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4032. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4033. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4034. DELIMITER ;;
  4035. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_ORDERS`()
  4036. BEGIN
  4037. truncate table online_products_line_items;
  4038. truncate table online_products_codes;
  4039. truncate table online_products_deliveries;
  4040. truncate table pah_coupons_printing_offers;
  4041. truncate table pah_coupons_printings;
  4042. truncate table pah_coupons_ci_analytics;
  4043. truncate table pah_coupons_history;
  4044. truncate table pah_coupons;
  4045. truncate table pah_coupons_offers;
  4046. truncate table sales_flat_order_item;
  4047. truncate table `sales_flat_order`;
  4048.  
  4049. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  4050.     WHERE table_schema = DATABASE()
  4051.         and table_name = 'sales_flat_order'
  4052.         and column_name = 'old_id') then
  4053.     alter table sales_flat_order add column (old_id int(11) null);
  4054. end if;
  4055.  
  4056. if not exists(SELECT *
  4057.         FROM information_schema.statistics
  4058.         WHERE table_schema = DATABASE()
  4059.                 and table_name = 'sales_flat_order'
  4060.                 and index_name = 'IDX_sales_flat_order_old_id') then
  4061.     alter table sales_flat_order add KEY IDX_sales_flat_order_old_id (old_id);
  4062. end if;
  4063. alter table `tload_sales_flat_order` add index (customer_id);
  4064. alter table `tload_sales_flat_order_item` add index (product_id);
  4065.  
  4066. INSERT INTO `sales_flat_order` (`entity_id`,`status`,`customer_id`,`subtotal`,`total_qty_ordered`,`created_at`,`updated_at`,`date_ordered`, old_id, increment_id)
  4067.     SELECT tor.`entity_id`,`status`,null as customer_id,`subtotal`,`total_qty_ordered`,`created_at`,`updated_at`,`date_ordered`, old_id, old_id
  4068. FROM `tload_sales_flat_order` tor ;
  4069.  
  4070.  
  4071. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  4072.     WHERE table_schema = DATABASE()
  4073.         and table_name = 'sales_flat_order_item'
  4074.         and column_name = 'old_id') then
  4075.     alter table sales_flat_order_item add column (old_id int(11) null);
  4076. end if;  
  4077.  
  4078.     INSERT INTO sales_flat_order_item (`item_id`,`order_id`,`store_id`,`created_at`,`updated_at`,`product_id`,`sku`,`product_type`,`weight`,`name`
  4079.      ,`price`,`qty_backordered`,`qty_canceled`,`qty_invoiced`,`qty_ordered`,`qty_refunded`,`qty_shipped`,`line_status`,`batch_id`,`old_id`, `description`)
  4080.     SELECT `item_id`,`order_id`,`store_id`,`created_at`,`updated_at`,null as product_id,`sku`,`product_type`,`weight`,`name`
  4081.      ,`price`,`qty_backordered`,`qty_canceled`,`qty_invoiced`,1,`qty_refunded`,`qty_shipped`,tori.`line_status`,tori.`batch_id`, tori.`old_id`, tori.`description`
  4082.     FROM tload_sales_flat_order_item tori;
  4083.      
  4084.  
  4085. create temporary table temp_customer_sales_flat_order as
  4086. SELECT ce.account_old_id as customer_id, ce.entity_id
  4087.   FROM customer_entity ce ;
  4088.  
  4089. alter table temp_customer_sales_flat_order add index (customer_id);
  4090.  
  4091. update  sales_flat_order  so
  4092.  INNER JOIN tload_sales_flat_order tso on so.entity_id = tso.entity_id
  4093.  INNER JOIN temp_customer_sales_flat_order tc on tc.customer_id = tso.customer_id
  4094. SET so.customer_id = tc.entity_id;
  4095.  
  4096. create temporary table temp_product_sales_flat_order_item
  4097. as
  4098.     SELECT cpe.entity_id,cpe.reward_old_id  as reward_old_id  
  4099.         FROM catalog_product_entity cpe;
  4100.  
  4101. alter table temp_product_sales_flat_order_item add index (reward_old_id);
  4102.  
  4103. update sales_flat_order_item soi
  4104.     INNER JOIN `tload_sales_flat_order_item` tsoi on soi.item_id = tsoi.item_id
  4105.     INNER JOIN temp_product_sales_flat_order_item ti on tsoi.product_id = ti.reward_old_id
  4106. SET soi.product_id = ti.entity_id;
  4107.  
  4108. drop table if exists temp_customer_sales_flat_order;
  4109. drop table if exists temp_product_sales_flat_order_item;
  4110.        
  4111. END */;;
  4112. DELIMITER ;
  4113. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4114. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4115. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4116. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4117. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_pah_coupons_table` */;
  4118. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4119. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4120. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4121. /*!50003 SET character_set_client  = utf8 */ ;
  4122. /*!50003 SET character_set_results = utf8 */ ;
  4123. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4124. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4125. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4126. DELIMITER ;;
  4127. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_pah_coupons_table`()
  4128. BEGIN
  4129.  
  4130. truncate table pah_coupons_printing_offers;
  4131. truncate table pah_coupons_printings;
  4132. truncate table pah_coupons_ci_analytics;
  4133. truncate table pah_coupons_history;
  4134. truncate table pah_coupons;
  4135. truncate table pah_coupons_offers;
  4136.  
  4137.     insert into pah_coupons_offers
  4138.         (id,
  4139.         code,
  4140.         created_at,
  4141.         updated_at,
  4142.         status_code,
  4143.         employee_id,
  4144.         product_id,
  4145.         old_id
  4146.         )
  4147.     select  id,
  4148.         code,
  4149.         created_at,
  4150.         updated_at,
  4151.         status_code,
  4152.         employee_id,
  4153.         null,
  4154.         old_id
  4155.     from tload_pah_coupons_offers;
  4156. UPDATE pah_coupons_offers co
  4157.     inner join tload_pah_coupons_offers tco on co.id = tco.id
  4158.     inner join catalog_product_entity pe on tco.product_id = pe.reward_old_id
  4159. SET co.product_id = pe.entity_id;
  4160.  
  4161.     insert into pah_coupons
  4162.         (id,
  4163.         code,
  4164.         type_code,
  4165.         created_at,
  4166.         updated_at,
  4167.         status_code,
  4168.         employee_id,
  4169.         product_id,
  4170.         old_id
  4171.         )
  4172.     select  id,
  4173.         code,
  4174.         type_code,
  4175.         created_at,
  4176.         updated_at,
  4177.         status_code,
  4178.         employee_id,
  4179.         null,
  4180.         old_id
  4181.     from tload_pah_coupons;
  4182. UPDATE pah_coupons c
  4183.     inner join tload_pah_coupons tc on c.id = tc.id
  4184.     inner join catalog_product_entity pe on tc.product_id = pe.reward_old_id
  4185. SET c.product_id = pe.entity_id;
  4186.  
  4187.     insert into pah_coupons_history
  4188.         (id,
  4189.         action,
  4190.         offer_id,
  4191.         coupon_id,
  4192.         employee_id,
  4193.         created_at,
  4194.         updated_at,
  4195.         old_id
  4196.         )
  4197.     select  id,
  4198.         action,
  4199.         null,
  4200.         null,
  4201.         employee_id,
  4202.         created_at,
  4203.         updated_at,
  4204.         old_id
  4205.     from tload_pah_coupons_history;
  4206.  
  4207. if not exists(SELECT *
  4208.         FROM information_schema.statistics
  4209.         WHERE table_schema = DATABASE()
  4210.                 and table_name = 'pah_coupons_offers'
  4211.                 and index_name = 'IDX_pah_coupons_offers_old_id') then
  4212.     alter table pah_coupons_offers add KEY `IDX_pah_coupons_offers_old_id` (`old_id`);
  4213. end if;
  4214. UPDATE pah_coupons_history ch
  4215.     inner join tload_pah_coupons_history tch on ch.id = tch.id
  4216.     inner join pah_coupons_offers co on tch.offer_id = co.old_id
  4217. SET ch.offer_id = co.id;
  4218.  
  4219. if not exists(SELECT *
  4220.         FROM information_schema.statistics
  4221.         WHERE table_schema = DATABASE()
  4222.                 and table_name = 'pah_coupons'
  4223.                 and index_name = 'IDX_pah_coupons_old_id') then
  4224.     alter table pah_coupons add KEY `IDX_pah_coupons_old_id` (`old_id`);
  4225. end if;
  4226. UPDATE pah_coupons_history ch
  4227.     inner join tload_pah_coupons_history tch on ch.id = tch.id
  4228.     inner join pah_coupons c on tch.coupon_id = c.old_id
  4229. SET ch.coupon_id = c.id;
  4230.  
  4231.     insert into pah_coupons_ci_analytics
  4232.         (id,
  4233.         offer_code,
  4234.         coupon_id,
  4235.         attempted,
  4236.         prints,
  4237.         first_print,
  4238.         redeemed,
  4239.         deny_dev,
  4240.         deny_pin,
  4241.         deny_fraud,
  4242.         deny_network,
  4243.         deny_platform,
  4244.         coupon_desc,
  4245.         created_at,
  4246.         updated_at,
  4247.         old_id
  4248.         )
  4249.     select  id,
  4250.         offer_code,
  4251.         null,
  4252.         attempted,
  4253.         prints,
  4254.         first_print,
  4255.         redeemed,
  4256.         deny_dev,
  4257.         deny_pin,
  4258.         deny_fraud,
  4259.         deny_network,
  4260.         deny_platform,
  4261.         coupon_desc,
  4262.         created_at,
  4263.         updated_at,
  4264.         old_id
  4265.     from tload_pah_coupons_ci_analytics;
  4266. UPDATE pah_coupons_ci_analytics cca
  4267.     inner join tload_pah_coupons_ci_analytics tcca on cca.id = tcca.id
  4268.     inner join pah_coupons c on tcca.coupon_id = c.old_id
  4269. SET cca.coupon_id = c.id;
  4270.  
  4271.     insert into pah_coupons_printings
  4272.         (id,
  4273.         pin,
  4274.         url_string,
  4275.         created_at,
  4276.         updated_at,
  4277.         status_code,
  4278.         product_id,
  4279.         ci_analytic_id,
  4280.         order_id,
  4281.         old_id
  4282.         )
  4283.     select  id,
  4284.         pin,
  4285.         url_string,
  4286.         created_at,
  4287.         updated_at,
  4288.         status_code,
  4289.         null,
  4290.         null,
  4291.         null,
  4292.         old_id
  4293.     from tload_pah_coupons_printings;
  4294. UPDATE pah_coupons_printings cp
  4295.     inner join tload_pah_coupons_printings tcp on cp.id = tcp.id
  4296.     inner join catalog_product_entity pe on tcp.product_id = pe.reward_old_id
  4297. SET cp.product_id = pe.entity_id;
  4298. if not exists(SELECT *
  4299.         FROM information_schema.statistics
  4300.         WHERE table_schema = DATABASE()
  4301.                 and table_name = 'pah_coupons_ci_analytics'
  4302.                 and index_name = 'IDX_pah_coupons_ci_analytics_old_id') then
  4303.     alter table pah_coupons_ci_analytics add KEY `IDX_pah_coupons_ci_analytics_old_id` (`old_id`);
  4304. end if;
  4305. UPDATE pah_coupons_printings cp
  4306.     inner join tload_pah_coupons_printings tcp on cp.id = tcp.id
  4307.     inner join pah_coupons_ci_analytics cca on tcp.ci_analitic_id = cca.old_id
  4308. SET cp.ci_analytic_id = cca.id;
  4309. UPDATE pah_coupons_printings cp
  4310.     inner join tload_pah_coupons_printings tcp on cp.id = tcp.id
  4311.     inner join sales_flat_order o on tcp.order_id = o.old_id
  4312. SET cp.order_id = o.entity_id;
  4313.  
  4314.     insert into pah_coupons_printing_offers
  4315.         (id,
  4316.         created_at,
  4317.         updated_at,
  4318.         coupons_inc_printing_id,
  4319.         coupons_inc_offer_id,
  4320.         coupons_inc_coupon_id,
  4321.         ci_analytic_id,
  4322.         old_id
  4323.         )
  4324.     select  id,
  4325.         created_at,
  4326.         updated_at,
  4327.         coupons_inc_printing_id,
  4328.         coupons_inc_offer_id,
  4329.         coupons_inc_coupon_id,
  4330.         null,
  4331.         old_id
  4332.     from tload_pah_coupons_printing_offers;
  4333. UPDATE pah_coupons_printing_offers cpo
  4334.     inner join tload_pah_coupons_printing_offers tcpo on cpo.id = tcpo.id
  4335.     inner join pah_coupons_ci_analytics cca on tcpo.ci_analitic_id = cca.old_id
  4336. SET cpo.ci_analytic_id = cca.id;
  4337.  
  4338. alter table pah_coupons_offers drop KEY `IDX_pah_coupons_offers_old_id`;
  4339. alter table pah_coupons_ci_analytics drop KEY `IDX_pah_coupons_ci_analytics_old_id`;
  4340. alter table pah_coupons drop KEY `IDX_pah_coupons_old_id`;
  4341. END */;;
  4342. DELIMITER ;
  4343. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4344. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4345. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4346. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4347. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_PARTNERS` */;
  4348. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4349. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4350. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4351. /*!50003 SET character_set_client  = utf8 */ ;
  4352. /*!50003 SET character_set_results = utf8 */ ;
  4353. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4354. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4355. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4356. DELIMITER ;;
  4357. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_PARTNERS`()
  4358. BEGIN
  4359. truncate table curbside_subscription;
  4360. truncate table curbside_addresses;
  4361.  
  4362. truncate table earns_customers_partners;
  4363. truncate table earns_customers;
  4364.  
  4365. truncate table sales_flat_order_item;
  4366. truncate table enterprise_customerbalance_history_ext_partner;
  4367. truncate table online_products_line_items;
  4368. truncate table online_products_codes;
  4369. truncate table online_products_deliveries;
  4370. truncate table online_products_codes;
  4371. truncate table pah_coupons_printing_offers;
  4372. truncate table pah_coupons_printings;
  4373. truncate table pah_coupons_ci_analytics;
  4374. truncate table pah_coupons_history;
  4375. truncate table pah_coupons;
  4376. truncate table pah_coupons_offers;
  4377.  
  4378. truncate table sales_flat_order;
  4379. TRUNCATE TABLE catalog_product_entity_media_gallery;
  4380. TRUNCATE TABLE partners_local_rewards_stores;
  4381. delete from catalog_product_entity;
  4382.  
  4383. truncate table partners_reward_address_merchant_data;
  4384. truncate table partners_reward_merchant_data;
  4385. truncate table partners_reward_address;
  4386. truncate table partners_reward;
  4387. truncate table partners_earn_transactions;
  4388. truncate table partners_earn;
  4389. truncate table partners_entity;
  4390. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  4391.     WHERE table_schema = DATABASE()
  4392.         and table_name = 'partners_entity'
  4393.         and column_name = 'old_id') then
  4394.     alter table partners_entity add column (old_id int(11) null);  
  4395. end if;
  4396.  
  4397. INSERT INTO partners_entity
  4398.     (id,`name`,is_national,country,description,logo,url_1,url_text_1,`status`,page_url,old_id)
  4399. SELECT id,trim(`name`),is_national,country,description,logo,url_1,url_text_1,`status`,page_url,tm.old_id
  4400. FROM tload_partners_entity tpe
  4401.         INNER JOIN tload_partners_entity_map_id tm on tpe.id = tm.new_id and tm.source_table in (1,2,3,6,7);
  4402.  
  4403. INSERT INTO partners_earn (id,partner_id,type,points,has_strict_zero,allow_autolink,g_partner_id,g_partner_secret_key)
  4404. SELECT id,partner_id,type,points,strict_zero,automatic_link,g_partner_id,g_partner_secret_key
  4405.     FROM tload_partners_earn where partner_id <> 0;
  4406.  
  4407. INSERT INTO partners_reward (id,partner_id,irs_tax_id)
  4408. SELECT id,partner_id,irs_tax_id
  4409.     FROM tload_partners_reward;
  4410.  
  4411. INSERT INTO partners_reward_address
  4412.     (id,partner_id,address_1,address_2,city,state,post_code,country,phone_number,`status`,lat,lng )
  4413. SELECT id,partner_id,address_1,address_2,city,state,post_code,country,phone_number,`status`,lat,lng
  4414.     FROM tload_partners_reward_address;
  4415.  
  4416. INSERT INTO partners_reward_merchant_data (id,partner_id,bonus_factor,merchant_id,merchant_dba,merchant_category_id)
  4417. SELECT id,partner_id,bonus_factor,merchant_id,merchant_dba,merchant_category_id
  4418.     FROM tload_partners_reward_merchant_data;
  4419.  
  4420. INSERT INTO partners_reward_address_merchant_data (id,partner_reward_address_id,bonus_factor,merchant_id)
  4421. SELECT id,partner_reward_address_id,bonus_factor,merchant_id
  4422.     FROM tload_partners_reward_address_merchant_data ;
  4423.  
  4424. INSERT INTO `partners_earn_transactions`
  4425.     (id,partner_id,`type`,`time`,points,description,customer_id)
  4426.     SELECT id,partner_id,`type`,`time`,points,description,null as customer_id
  4427.         FROM `tload_partners_earn_transactions` ;
  4428. if not exists(SELECT *
  4429.         FROM information_schema.statistics
  4430.         WHERE table_schema = DATABASE()
  4431.                 and table_name = 'tload_partners_earn_transactions'
  4432.                 and index_name = 'ix_tload_partners_earn_transactions_customer_id') then
  4433.     alter table tload_partners_earn_transactions add KEY ix_tload_partners_earn_transactions_customer_id (customer_id);
  4434. end if;
  4435.  UPDATE partners_earn_transactions pet
  4436.         INNER JOIN `tload_partners_earn_transactions` tpet on pet.id = tpet.id
  4437.         INNER JOIN customer_entity ce on tpet.customer_id = ce.account_old_id
  4438.     SET pet.customer_id = ce.entity_id;
  4439. END */;;
  4440. DELIMITER ;
  4441. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4442. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4443. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4444. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4445. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_Transaktions` */;
  4446. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4447. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4448. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4449. /*!50003 SET character_set_client  = utf8 */ ;
  4450. /*!50003 SET character_set_results = utf8 */ ;
  4451. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4452. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4453. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4454. DELIMITER ;;
  4455. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_Transaktions`()
  4456. BEGIN
  4457. truncate table enterprise_customerbalance_history_ext_partner;
  4458. truncate table `enterprise_customerbalance_history`;
  4459. truncate table `enterprise_customerbalance`;
  4460. INSERT INTO `enterprise_customerbalance` (`balance_id`,customer_id,website_id,amount,base_currency_code)
  4461. SELECT `balance_id` ,ce.entity_id as customer_id,tcb.website_id,tcb.amount,tcb.base_currency_code
  4462. FROM  tload_enterprise_customerbalance tcb
  4463. INNER JOIN customer_entity ce on ce.account_old_id = tcb.customer_id;
  4464.  
  4465.  
  4466. if not exists (select * from INFORMATION_SCHEMA.COLUMNS
  4467.     WHERE table_schema = DATABASE()
  4468.         and table_name = 'enterprise_customerbalance_history'
  4469.         and column_name = 'transaktions_old_id') then
  4470.     alter table enterprise_customerbalance_history add column (transaktions_old_id int(11) null);
  4471. end if;
  4472. INSERT INTO `enterprise_customerbalance_history`
  4473. (history_id,balance_id,updated_at,`action`,balance_amount,balance_delta,additional_info,is_customer_notified,reason_id, description, transaktions_old_id)
  4474. SELECT tcbh.history_id,tcbh.balance_id,tcbh.updated_at,tcbh.`action`,tcbh.balance_amount,tcbh.balance_delta,tcbh.additional_info,tcbh.is_customer_notified,tcbh.reason_id, tcbh.description, tcbh.old_id  
  4475. FROM tload_enterprise_customerbalance_history tcbh ;
  4476.  
  4477. INSERT INTO enterprise_customerbalance_history_ext_partner (history_id, partner_id)
  4478. SELECT h.history_id, l.new_id
  4479. FROM tload_enterprise_customerbalance_history h
  4480.     inner join tload_partners_entity_map_id l on h.client_old_id = l.old_id
  4481.                     and l.source_table in (2,4)
  4482. WHERE h.client_old_id is not null;
  4483. END */;;
  4484. DELIMITER ;
  4485. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4486. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4487. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4488. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4489. /*!50003 DROP PROCEDURE IF EXISTS `MG_import_truncate_tables` */;
  4490. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4491. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4492. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4493. /*!50003 SET character_set_client  = utf8 */ ;
  4494. /*!50003 SET character_set_results = utf8 */ ;
  4495. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4496. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4497. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4498. DELIMITER ;;
  4499. /*!50003 CREATE*/ /*!50020 DEFINER=`front_ecs`@`%`*/ /*!50003 PROCEDURE `MG_import_truncate_tables`()
  4500. BEGIN
  4501.  
  4502.  
  4503. truncate table enterprise_customerbalance_history_ext_partner;
  4504. truncate table enterprise_customerbalance_history_ext_code;
  4505. truncate table enterprise_customerbalance_history;
  4506. truncate table enterprise_customersegment_segment;
  4507. truncate table enterprise_customerbalance;
  4508.  
  4509. truncate table curbside_subscription;
  4510. truncate table curbside_addresses;
  4511.  
  4512. truncate table earns_customers_partners;
  4513. truncate table earns_customers;
  4514.  
  4515. truncate table online_products_line_items;
  4516. truncate table online_products_codes;
  4517. truncate table online_products_deliveries;
  4518.  
  4519. truncate table pah_coupons_printing_offers;
  4520. truncate table pah_coupons_printings;
  4521. truncate table pah_coupons_ci_analytics;
  4522. truncate table pah_coupons_history;
  4523. truncate table pah_coupons;
  4524. truncate table pah_coupons_offers;
  4525.  
  4526. truncate table sales_flat_order_item;
  4527. truncate table sales_flat_order;
  4528.  
  4529.  
  4530.  
  4531. TRUNCATE TABLE partners_local_rewards_stores;
  4532. TRUNCATE TABLE catalog_product_entity_media_gallery;
  4533. truncate table catalog_product_entity_datetime;
  4534. truncate table catalog_product_entity_decimal;
  4535. truncate table catalog_product_entity_int;
  4536. truncate table catalog_product_entity_text;
  4537. truncate table catalog_product_entity_varchar;
  4538. truncate table catalog_product_entity;
  4539.  
  4540. truncate table partners_reward_merchant_data;
  4541. truncate table partners_reward;
  4542. truncate table partners_reward_address_merchant_data;
  4543. truncate table partners_reward_address;
  4544. truncate table partners_earn_transactions;
  4545. truncate table partners_earn;
  4546. truncate table partners_entity;
  4547.  
  4548. truncate table customer_address_entity_datetime;
  4549. truncate table customer_address_entity_decimal;
  4550. truncate table customer_address_entity_int;
  4551. truncate table customer_address_entity_text;
  4552. truncate table customer_address_entity_varchar;
  4553. truncate table customer_address_entity;
  4554.  
  4555. truncate table customer_mobile;
  4556. truncate table customer_entity_datetime;
  4557. truncate table customer_entity_decimal;
  4558. truncate table customer_entity_int;
  4559. truncate table customer_entity_text;
  4560. truncate table customer_entity_varchar;
  4561. truncate table customer_entity;
  4562. END */;;
  4563. DELIMITER ;
  4564. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4565. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4566. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4567. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4568. /*!50003 DROP PROCEDURE IF EXISTS `MG_SELFREPORTING_REMINDER_USERS` */;
  4569. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4570. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4571. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4572. /*!50003 SET character_set_client  = utf8 */ ;
  4573. /*!50003 SET character_set_results = utf8 */ ;
  4574. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4575. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4576. /*!50003 SET sql_mode              = '' */ ;
  4577. DELIMITER ;;
  4578. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_SELFREPORTING_REMINDER_USERS`()
  4579. BEGIN
  4580.  
  4581. SELECT  now() INTO @curr_dt;
  4582. SELECT case WHEN DAYOFWEEK(@curr_dt) =1 THEN 7 ELSE DAYOFWEEK(@curr_dt)-1 END INTO @dofw ;
  4583. SELECT  
  4584. case
  4585. WHEN mod((WEEK(@curr_dt, 5) -
  4586.         WEEK(DATE_SUB(@curr_dt, INTERVAL DAYOFMONTH(@curr_dt) - 1 DAY), 5) + 1) ,2 ) = 0
  4587. THEN 1 ELSE 0 END INTO @d_even_odd;
  4588. SELECT `sr`.*
  4589. , `ce`.`email`
  4590. , `fname_table`.`value` AS `first_name`
  4591. , `lname_table`.`value` AS `la
  4592. st_name`
  4593. FROM `customer_self_reporting` AS `sr`
  4594.  LEFT JOIN `customer_entity` ce on `sr`.customer_id = ce.entity_id
  4595.  LEFT JOIN `customer_entity_varchar` AS `fname_table`
  4596.     ON fname_table.entity_id = sr.customer_id AND fname_table.attribute_id = 5
  4597.  LEFT JOIN `customer_entity_varchar` AS `lname_table`
  4598.     ON lname_table.entity_id = sr.customer_id AND lname_table.attribute_id = 7
  4599. WHERE
  4600.     (sr.is_active = 1
  4601. AND
  4602. (  
  4603. (sr.frequency_type = 1 AND sr.day_of_week = @dofw)
  4604.     OR (sr.frequency_type = 2 AND sr.num = @d_even_odd AND sr.day_of_week =@dofw)
  4605.     OR (sr.frequency_type = 3
  4606.         AND
  4607.         (sr.day_of_month = DAYOFMONTH(@curr_dt)
  4608.             OR case
  4609.                 WHEN DAYOFMONTH(LAST_DAY(@curr_dt)) > 27 AND DAYOFMONTH(LAST_DAY(@curr_dt))< 31
  4610.                 THEN sr.day_of_month > date(@curr_dt)
  4611.                 ELSE 1=0 END
  4612.         )
  4613.          )
  4614. ));
  4615. END */;;
  4616. DELIMITER ;
  4617. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4618. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4619. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4620. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4621. /*!50003 DROP PROCEDURE IF EXISTS `MG_TRANSACTIONS_LAST_WEEK` */;
  4622. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4623. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4624. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4625. /*!50003 SET character_set_client  = utf8 */ ;
  4626. /*!50003 SET character_set_results = utf8 */ ;
  4627. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4628. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4629. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  4630. DELIMITER ;;
  4631. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_TRANSACTIONS_LAST_WEEK`()
  4632. BEGIN
  4633.  
  4634. drop table if exists ttemp_search_transactions ;
  4635. create TEMPORARY table ttemp_search_transactions
  4636. SELECT
  4637.         cbh.history_id,cbh.balance_id,cbh.additional_info,cbh.reason_id,cbh.description,cbh.balance_delta,cbh.balance_amount,cbh.updated_at
  4638.     FROM enterprise_customerbalance_history cbh
  4639. WHERE cbh.updated_at
  4640. between DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -7 DAY)
  4641.                         and DATE_FORMAT(NOW(), '%Y-%m-%d')  ;
  4642. alter table ttemp_search_transactions add index ix_ttttt_tr_balance (balance_id);
  4643. SELECT  
  4644.     cb.amount,ce.store_id,ce.email,cbh.additional_info,cbh.reason_id,cbh.description,cbh.balance_delta,cbh.balance_amount,cbh.updated_at,ce.store_id
  4645. FROM ttemp_search_transactions  cbh
  4646.  INNER JOIN enterprise_customerbalance cb on cb.balance_id = cbh.balance_id
  4647.      INNER JOIN customer_entity ce on ce.entity_id = cb.customer_id  order by email, cbh.history_id desc;
  4648. drop table ttemp_search_transactions;
  4649. END */;;
  4650. DELIMITER ;
  4651. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4652. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4653. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4654. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4655. /*!50003 DROP PROCEDURE IF EXISTS `MG_UPDATE_PRODUCT_STATUS_DAILY` */;
  4656. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4657. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4658. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4659. /*!50003 SET character_set_client  = utf8 */ ;
  4660. /*!50003 SET character_set_results = utf8 */ ;
  4661. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4662. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4663. /*!50003 SET sql_mode              = '' */ ;
  4664. DELIMITER ;;
  4665. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_UPDATE_PRODUCT_STATUS_DAILY`(IN iis_uk int )
  4666. BEGIN
  4667. DECLARE done INT DEFAULT 0;
  4668. DECLARE c_entity_id int(10);
  4669. DECLARE cur1
  4670. CURSOR FOR
  4671.     SELECT  entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =1 ORDER BY entity_id;
  4672. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  4673. SELECT now() INTO @curr_date;
  4674. SELECT case
  4675.     WHEN iis_uk =1 THEN date(DATE_ADD( @curr_date, INTERVAL 10 HOUR ))
  4676.     ELSE date(@curr_date) END INTO @dnow;
  4677. DROP table if exists ttemp_upd_status_daily_;
  4678. CREATE temporary table ttemp_upd_status_daily_
  4679. AS
  4680. SELECT
  4681. cpe_status.entity_id  
  4682. ,case WHEN cpe_start.value <= cpe_end.value
  4683. THEN
  4684.     case WHEN cpe_start.value > @dnow
  4685.         THEN 2  
  4686.             WHEN cpe_start.value = @dnow AND cpe_end.value > @dnow AND ifnull(csi.is_in_stock,0) = 1
  4687.                 THEN 1
  4688.             WHEN cpe_start.value = @dnow AND cpe_end.value > @dnow AND ifnull(csi.is_in_stock,0) = 0
  4689.                 THEN cpe_status.value
  4690.     WHEN cpe_end.value <= @dnow
  4691.         THEN 2
  4692.     ELSE
  4693.         cpe_status.value
  4694.     END
  4695. ELSE
  4696.     2
  4697. END as upd_status
  4698. FROM catalog_product_entity_int cpe_status
  4699.     INNER JOIN catalog_product_entity cpe
  4700.         on cpe_status.entity_id = cpe.entity_id AND cpe.is_uk = iis_uk
  4701.     LEFT JOIN catalog_product_entity_datetime cpe_start
  4702.         on cpe_status.entity_id = cpe_start.entity_id
  4703.         AND cpe_start.attribute_id = 444
  4704.         AND cpe_status.store_id = cpe_start.store_id
  4705.     LEFT JOIN catalog_product_entity_datetime cpe_end
  4706.         on cpe_status.entity_id = cpe_end.entity_id
  4707.         AND cpe_end.attribute_id = 463
  4708.         AND cpe_status.store_id = cpe_end.store_id
  4709.     LEFT JOIN cataloginventory_stock_item csi
  4710.         ON csi.product_id = cpe.entity_id
  4711. WHERE cpe_status.attribute_id = 495
  4712.     and cpe_status.store_id =0
  4713.     and (cpe_start.value is not null AND cpe_end.value is not null)
  4714.     and (cpe_start.value > @dnow or cpe_start.value = @dnow or cpe_end.value <= @dnow or cpe_start.value > cpe_end.value )
  4715. AND
  4716. case WHEN cpe_start.value <= cpe_end.value
  4717. THEN
  4718.     case WHEN cpe_start.value > @dnow
  4719.         THEN 2  
  4720.     WHEN cpe_start.value = @dnow AND cpe_end.value > @dnow
  4721.         THEN 1
  4722.     WHEN cpe_end.value <= @dnow
  4723.         THEN 2
  4724.     ELSE
  4725.         cpe_status.value
  4726.     END
  4727. ELSE
  4728.     2
  4729. END <> cpe_status.value;
  4730. UPDATE  catalog_product_entity_int cpe_status
  4731.     INNER JOIN ttemp_upd_status_daily_ t
  4732.         on cpe_status.entity_id = t.entity_id
  4733. SET cpe_status.value = t.upd_status
  4734. WHERE cpe_status.store_id = 0 AND  cpe_status.attribute_id = 495;
  4735.  
  4736. OPEN cur1;
  4737. read_loop: LOOP
  4738.     FETCH cur1 INTO c_entity_id;    
  4739.     IF  done THEN
  4740.         LEAVE read_loop;
  4741.     END IF;
  4742.     call MG_UPSERT_CATALOG_PRODUCT_FLAT(c_entity_id);
  4743. END LOOP;
  4744. CLOSE cur1;
  4745.  
  4746. DELETE FROM catalog_product_flat_1
  4747.     WHERE entity_id in (SELECT entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =2);
  4748. DELETE FROM catalog_product_flat_2
  4749.     WHERE entity_id in (SELECT entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =2);
  4750. DROP table if exists ttemp_upd_status_daily_;
  4751. end */;;
  4752. DELIMITER ;
  4753. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4754. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4755. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4756. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4757. /*!50003 DROP PROCEDURE IF EXISTS `MG_UPDATE_PRODUCT_STATUS_DAILY_TEST` */;
  4758. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4759. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4760. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4761. /*!50003 SET character_set_client  = utf8 */ ;
  4762. /*!50003 SET character_set_results = utf8 */ ;
  4763. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4764. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4765. /*!50003 SET sql_mode              = '' */ ;
  4766. DELIMITER ;;
  4767. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_UPDATE_PRODUCT_STATUS_DAILY_TEST`(IN iis_uk int )
  4768. BEGIN
  4769. DECLARE done INT DEFAULT 0;
  4770. DECLARE c_entity_id int(10);
  4771. DECLARE cur1
  4772. CURSOR FOR
  4773.     SELECT  entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =1 ORDER BY entity_id;
  4774. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  4775. SELECT now() INTO @curr_date;
  4776. SELECT case
  4777.     WHEN iis_uk =1 THEN date(DATE_ADD( @curr_date, INTERVAL 5 HOUR ))
  4778.     ELSE date(@curr_date) END INTO @dnow;
  4779. DROP table if exists ttemp_upd_status_daily_;
  4780. CREATE temporary table ttemp_upd_status_daily_
  4781. AS
  4782. SELECT
  4783. cpe_status.entity_id  
  4784. ,case WHEN cpe_start.value <= cpe_end.value
  4785. THEN
  4786.     case WHEN cpe_start.value > @dnow
  4787.         THEN 2  
  4788.     WHEN cpe_start.value = @dnow AND cpe_end.value > @dnow
  4789.         THEN 1
  4790.     WHEN cpe_end.value < @dnow
  4791.         THEN 2
  4792.     ELSE
  4793.         cpe_status.value
  4794.     END
  4795. ELSE
  4796.     2
  4797. END as upd_status
  4798. FROM catalog_product_entity_int cpe_status
  4799.     INNER JOIN catalog_product_entity cpe
  4800.         on cpe_status.entity_id = cpe.entity_id AND cpe.is_uk = iis_uk
  4801.     LEFT JOIN catalog_product_entity_datetime cpe_start
  4802.         on cpe_status.entity_id = cpe_start.entity_id
  4803.         AND cpe_start.attribute_id = 444
  4804.         AND cpe_status.store_id = cpe_start.store_id
  4805.     LEFT JOIN catalog_product_entity_datetime cpe_end
  4806.         on cpe_status.entity_id = cpe_end.entity_id
  4807.         AND cpe_end.attribute_id = 463
  4808.         AND cpe_status.store_id = cpe_end.store_id
  4809. WHERE cpe_status.attribute_id = 495
  4810.     and cpe_status.store_id =0
  4811.     and (cpe_start.value is not null AND cpe_end.value is not null)
  4812.     and (cpe_start.value > @dnow or cpe_start.value = @dnow or cpe_end.value < @dnow or cpe_start.value > cpe_end.value )
  4813. AND
  4814. case WHEN cpe_start.value <= cpe_end.value
  4815. THEN
  4816.     case WHEN cpe_start.value > @dnow
  4817.         THEN 2  
  4818.     WHEN cpe_start.value = @dnow AND cpe_end.value > @dnow
  4819.         THEN 1
  4820.     WHEN cpe_end.value < @dnow
  4821.         THEN 2
  4822.     ELSE
  4823.         cpe_status.value
  4824.     END
  4825. ELSE
  4826.     2
  4827. END <> cpe_status.value;
  4828. UPDATE  catalog_product_entity_int cpe_status
  4829.     INNER JOIN ttemp_upd_status_daily_ t
  4830.         on cpe_status.entity_id = t.entity_id
  4831. SET cpe_status.value = t.upd_status
  4832. WHERE cpe_status.store_id = 0 AND  cpe_status.attribute_id = 495;
  4833.  
  4834. OPEN cur1;
  4835. read_loop: LOOP
  4836.     FETCH cur1 INTO c_entity_id;    
  4837.     IF  done THEN
  4838.         LEAVE read_loop;
  4839.     END IF;
  4840.     call MG_UPSERT_CATALOG_PRODUCT_FLAT(c_entity_id);
  4841. END LOOP;
  4842. CLOSE cur1;
  4843.  
  4844. DELETE FROM catalog_product_flat_1
  4845.     WHERE entity_id in (SELECT entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =2);
  4846. DELETE FROM catalog_product_flat_2
  4847.     WHERE entity_id in (SELECT entity_id FROM ttemp_upd_status_daily_ WHERE upd_status =2);
  4848. DROP table if exists ttemp_upd_status_daily_;
  4849. end */;;
  4850. DELIMITER ;
  4851. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4852. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4853. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4854. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4855. /*!50003 DROP PROCEDURE IF EXISTS `MG_UPSERT_CATALOG_PRODUCT_FLAT` */;
  4856. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4857. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4858. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4859. /*!50003 SET character_set_client  = utf8 */ ;
  4860. /*!50003 SET character_set_results = utf8 */ ;
  4861. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4862. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4863. /*!50003 SET sql_mode              = '' */ ;
  4864. DELIMITER ;;
  4865. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `MG_UPSERT_CATALOG_PRODUCT_FLAT`(IN inp_entity_id int(10))
  4866. BEGIN
  4867.  DECLARE str_all_fields VARCHAR(100000) DEFAULT '';
  4868.  DECLARE str_ins_fields VARCHAR(10000) DEFAULT '';
  4869.  DECLARE str_dupl_upd VARCHAR(10000) DEFAULT '';
  4870.  DECLARE fld_name VARCHAR(10000) DEFAULT '';
  4871.  DECLARE ins_fld_name VARCHAR(255) DEFAULT '';
  4872.  DECLARE fld_dupl_upd VARCHAR(10000) DEFAULT '';   
  4873. DECLARE done INT DEFAULT 0;
  4874. DECLARE cur1
  4875. CURSOR FOR select
  4876.     concat("max(case When a.attribute_code='", a.attribute_code,"' THEN ",case when a.backend_type='varchar' THEN "cev.value" WHEN a.backend_type='datetime' THEN "ced.value" WHEN a.backend_type='decimal' THEN "cedc.value" WHEN a.backend_type = 'int' THEN "cei.value" WHEN a.backend_type = 'text' THEN "cet.value" WHEN a.backend_type = 'static' THEN concat("ce.",a.attribute_code) END," END ) as ",a.attribute_code) as  a1,         
  4877.     a.attribute_code as a2,
  4878.     concat("`",a.attribute_code,"`=VALUES(`",a.attribute_code,"`)") as a3
  4879.     FROM `eav_attribute` a
  4880.     INNER JOIN information_schema.COLUMNS c on c.TABLE_SCHEMA =DATABASE()
  4881.         AND c.TABLE_NAME ='catalog_product_flat_1' AND c.column_name = a.attribute_code
  4882. WHERE a.entity_type_id = 4 and a.attribute_code <>'required_options'
  4883. order BY a.attribute_id;
  4884. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  4885. OPEN cur1;
  4886. read_loop: LOOP
  4887.     FETCH cur1 INTO fld_name,ins_fld_name,fld_dupl_upd;    
  4888.     IF  done THEN
  4889.      LEAVE read_loop;
  4890.     END IF;
  4891.     SET str_all_fields = CONCAT(str_all_fields,fld_name,",");  
  4892.     SET str_ins_fields = CONCAT(str_ins_fields,ins_fld_name,",");  
  4893.     SET str_dupl_upd = CONCAT(str_dupl_upd,fld_dupl_upd,",");  
  4894. END LOOP;
  4895. CLOSE cur1;
  4896. SET str_all_fields = substring(str_all_fields,1,length(str_all_fields)-1);
  4897. SET str_ins_fields = substring(str_ins_fields,1,length(str_ins_fields)-1);
  4898. SET str_dupl_upd = substring(str_dupl_upd,1,length(str_dupl_upd)-1);
  4899. SET @ins_table = 'catalog_product_flat_2';
  4900. SET @str_sql = concat(" FROM `eav_attribute` a  \n                  
  4901.                   INNER JOIN `catalog_product_entity` ce on a.entity_type_id = ce.entity_type_id  \n
  4902.                   LEFT JOIN `catalog_product_entity_varchar` cev  \n
  4903.                      on a.attribute_id = cev.attribute_id and ce.entity_id = cev.entity_id and cev.store_id =0  \n
  4904.                   LEFT JOIN `catalog_product_entity_datetime` ced \n
  4905.                      on a.attribute_id = ced.attribute_id and ce.entity_id = ced.entity_id and ced.store_id =0  \n
  4906.                   LEFT JOIN `catalog_product_entity_decimal` cedc \n  
  4907.                      on a.attribute_id = cedc.attribute_id and ce.entity_id = cedc.entity_id and cedc.store_id =0 \n
  4908.                   LEFT JOIN `catalog_product_entity_int` cei \n  
  4909.                      on a.attribute_id = cei.attribute_id and ce.entity_id = cei.entity_id and cei.store_id =0 \n
  4910.                   LEFT JOIN `catalog_product_entity_text` cet \n  
  4911.                      on a.attribute_id = cet.attribute_id and ce.entity_id = cet.entity_id and cet.store_id =0 \n
  4912.                WHERE  ce.entity_id = ",inp_entity_id," AND  a.entity_type_id = 4 GROUP BY ce.entity_id order BY a.attribute_id ");
  4913. SET @str_ins_sql =  concat("INSERT INTO ",@ins_table,"(entity_id,",str_ins_fields,") SELECT ce.entity_id,",str_all_fields,@str_sql,
  4914.             " ON DUPLICATE KEY UPDATE `entity_id`=VALUES(`entity_id`),",str_dupl_upd ) ;
  4915.  
  4916.  PREPARE s1 FROM @str_ins_sql;
  4917.  EXECUTE s1;
  4918.  DEALLOCATE PREPARE s1;
  4919. SET @ins_table = 'catalog_product_flat_1';
  4920. SET @str_ins_sql =  concat("INSERT INTO ",@ins_table,"(entity_id,",str_ins_fields,") SELECT ce.entity_id,",str_all_fields,@str_sql,
  4921.             " ON DUPLICATE KEY UPDATE `entity_id`=VALUES(`entity_id`),",str_dupl_upd ) ;
  4922.  PREPARE s1 FROM @str_ins_sql;
  4923.  EXECUTE s1;
  4924.  DEALLOCATE PREPARE s1;
  4925. end */;;
  4926. DELIMITER ;
  4927. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4928. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4929. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4930. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4931. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_CREATE_AD_HISTORY` */;
  4932. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4933. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4934. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4935. /*!50003 SET character_set_client  = utf8 */ ;
  4936. /*!50003 SET character_set_results = utf8 */ ;
  4937. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4938. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4939. /*!50003 SET sql_mode              = '' */ ;
  4940. DELIMITER ;;
  4941. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_CREATE_AD_HISTORY`(in loginId int(11),
  4942.                                             in imageUrl varchar(255),
  4943.                                             in showTime int(11),
  4944.                                             out recordId int(11))
  4945. BEGIN
  4946.  
  4947.   INSERT INTO mobile_ad_histories (login_id, image_url, show_time, created_at, updated_at)
  4948.          VALUES (loginId, imageUrl, showTime, now(), now());
  4949.  
  4950.   SET recordId = LAST_INSERT_ID();
  4951.  
  4952. END */;;
  4953. DELIMITER ;
  4954. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  4955. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  4956. /*!50003 SET character_set_results = @saved_cs_results */ ;
  4957. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  4958. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_CREATE_DEVICE_DETAILS` */;
  4959. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  4960. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  4961. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  4962. /*!50003 SET character_set_client  = utf8 */ ;
  4963. /*!50003 SET character_set_results = utf8 */ ;
  4964. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  4965. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  4966. /*!50003 SET sql_mode              = '' */ ;
  4967. DELIMITER ;;
  4968. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_CREATE_DEVICE_DETAILS`(in loginId int(11),
  4969.                                                 in deviceId varchar(255),
  4970.                                                 in deviceName varchar(255),
  4971.                                                 in osName varchar(255),
  4972.                                                 in osVersion varchar(255),
  4973.                                                 in appVersion varchar(100),
  4974.                                                 out success int)
  4975. BEGIN
  4976.  
  4977.     DECLARE deviceCount INT DEFAULT 0;
  4978.  
  4979.     SELECT count(*) FROM mobile_device_details dd
  4980.                   WHERE dd.login_id = loginId
  4981.                                               and dd.device_id = deviceId
  4982.                                               and dd.device_name = deviceName
  4983.                                               and dd.os_name = osName
  4984.                                               and dd.os_version = osVersion
  4985.                         and dd.app_version = appVersion
  4986.                                         into deviceCount;
  4987.  
  4988.     IF deviceCount = 0 THEN
  4989.         INSERT INTO mobile_device_details   (login_id, device_id, device_name, os_name, os_version, app_version, created_at)
  4990.                VALUES               (loginId, deviceId, deviceName, osName, osVersion, appVersion, now());
  4991.     SELECT 1 INTO success;
  4992.   ELSE
  4993.     SELECT 0 INTO success;
  4994.     END IF;
  4995.  
  4996.  
  4997. END */;;
  4998. DELIMITER ;
  4999. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  5000. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  5001. /*!50003 SET character_set_results = @saved_cs_results */ ;
  5002. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  5003. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_CREATE_REWARD_ORDER` */;
  5004. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  5005. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  5006. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  5007. /*!50003 SET character_set_client  = utf8 */ ;
  5008. /*!50003 SET character_set_results = utf8 */ ;
  5009. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  5010. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  5011. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  5012. DELIMITER ;;
  5013. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_CREATE_REWARD_ORDER`(IN icustomer_id int(10),
  5014.                                               IN iproduct_id int(10),
  5015.                                               IN iamount decimal(12,4),
  5016.                                               OUT success tinyint(1),
  5017.                                               OUT reason varchar(255))
  5018. BEGIN
  5019. declare o_id int(10);
  5020. declare reward_description text;
  5021. declare istore_id tinyint(1);
  5022. declare tr_id int(10);
  5023. IF EXISTS (SELECT 1 FROM customer_entity WHERE entity_id = icustomer_id)
  5024. AND EXISTS (SELECT 1 FROM catalog_product_entity WHERE entity_id = iproduct_id)
  5025. AND EXISTS (SELECT 1 FROM enterprise_customerbalance
  5026.         WHERE customer_id = icustomer_id )
  5027. THEN   
  5028. SET istore_id = 1;
  5029.                    
  5030. SELECT   ce_dba.value
  5031.     ,ce_dsa.value
  5032.     ,ce.group_id
  5033.     ,ce_dob.value
  5034.     ,ce.email
  5035.     ,ce_firstname.value
  5036.     ,ce_lastname.value
  5037.     ,ce.account_number
  5038.     ,cae_region.value
  5039.     ,cae_postcode.value
  5040.     ,ecb.balance_id
  5041.     ,ecb.amount
  5042.     ,ce.store_id
  5043.     INTO    @id_default_billing_address
  5044.         ,@id_default_shipping_address
  5045.         ,@icustomer_group_id
  5046.         ,@idob
  5047.         ,@iemail
  5048.         ,@ifirstname
  5049.         ,@ilastname
  5050.         ,@iaccount_number
  5051.         ,@iregion
  5052.         ,@ipostcode
  5053.         ,@ibalance_id
  5054.         ,@balance_amount
  5055.         ,istore_id
  5056.     FROM customer_entity ce
  5057.     LEFT JOIN customer_entity_int ce_dba
  5058.         on ce.entity_id = ce_dba.entity_id  AND ce_dba.attribute_id = 13
  5059.     LEFT JOIN customer_entity_int ce_dsa
  5060.         on ce.entity_id = ce_dsa.entity_id  AND ce_dsa.attribute_id = 14
  5061.     LEFT JOIN customer_entity_datetime ce_dob
  5062.         on ce.entity_id = ce_dob.entity_id  AND ce_dob.attribute_id = 11
  5063.     LEFT JOIN customer_entity_varchar ce_firstname
  5064.         on ce.entity_id = ce_firstname.entity_id  AND ce_firstname.attribute_id = 5
  5065.     LEFT JOIN customer_entity_varchar ce_lastname
  5066.         on ce.entity_id = ce_lastname.entity_id  AND ce_lastname.attribute_id = 7
  5067.     LEFT JOIN customer_address_entity cae
  5068.         on ce.entity_id = cae.parent_id
  5069.     LEFT JOIN customer_address_entity_varchar cae_region
  5070.         on cae.entity_id = cae_region.entity_id and cae_region.attribute_id = 27
  5071.     LEFT JOIN customer_address_entity_varchar cae_postcode
  5072.         on cae.entity_id = cae_postcode.entity_id and cae_postcode.attribute_id = 29
  5073.     LEFT JOIN enterprise_customerbalance ecb on ecb.customer_id = ce.entity_id         
  5074. WHERE ce.entity_id = icustomer_id;
  5075.                  
  5076. SELECT cpe_name.value,cpe_price.value,cpe.type_id ,cpe.sku,css.qty
  5077.     INTO @reward_name,@reward_price,@reward_type,@reward_sku,@reward_qty
  5078.     FROM catalog_product_entity cpe
  5079.         INNER JOIN catalog_product_entity_varchar cpe_name
  5080.             on cpe_name.entity_id = cpe.entity_id AND cpe_name.attribute_id = 60
  5081.         INNER JOIN catalog_product_entity_decimal cpe_price
  5082.             on cpe_price.entity_id = cpe.entity_id AND cpe_price.attribute_id = 64
  5083.         INNER JOIN cataloginventory_stock_status css
  5084.             on cpe.entity_id = css.product_id
  5085.     WHERE cpe.entity_id = iproduct_id;
  5086. IF (@balance_amount >= @reward_price*iamount ) AND (iamount <= @reward_qty) THEN
  5087.                 START TRANSACTION;
  5088.         BEGIN      
  5089.                                                                
  5090.                          
  5091. INSERT INTO `sales_flat_quote`
  5092. (`store_id`
  5093. , `created_at`
  5094. , `updated_at`
  5095. , `is_active`
  5096. , `store_to_base_rate`
  5097. , `store_to_quote_rate`
  5098. , `base_currency_code`
  5099. , `store_currency_code`
  5100. , `quote_currency_code`
  5101. , `customer_id`
  5102. , `customer_tax_class_id`
  5103. , `customer_group_id`
  5104. , `customer_email`
  5105. , `customer_prefix`
  5106. , `customer_firstname`
  5107. , `customer_middlename`
  5108. , `customer_lastname`
  5109. , `customer_suffix`
  5110. , `customer_dob`
  5111. , `global_currency_code`
  5112. , `base_to_global_rate`
  5113. , `base_to_quote_rate`
  5114. , `customer_taxvat`
  5115. , `customer_gender`
  5116. , `is_changed`)
  5117. VALUES (
  5118. '1'
  5119. , now()
  5120. , now()
  5121. ,1
  5122. , '1'
  5123. , '1'
  5124. , 'USD'
  5125. , 'USD'
  5126. , 'USD'
  5127. , icustomer_id
  5128. , '3'
  5129. , @icustomer_group_id
  5130. , @iemail
  5131. , NULL
  5132. , @ifirstname
  5133. , NULL
  5134. , @ilastname
  5135. , NULL
  5136. , @idob
  5137. , 'USD'
  5138. , '1'
  5139. , '1'
  5140. , NULL
  5141. , NULL
  5142. , '1'
  5143. );
  5144. SET @nsales_flat_quote = LAST_INSERT_ID();
  5145.                                            
  5146. INSERT INTO `enterprise_customer_sales_flat_quote`
  5147. (`entity_id`
  5148. , `customer_language_code`
  5149. , `customer_reset_code`
  5150. , `customer_date_last_login`
  5151. , `customer_subbed_to_recyclebank_news`
  5152. , `customer_subbed_to_partner_offers`
  5153. , `customer_village_green_opt_in`
  5154. , `customer_rewards_email_opt_in`
  5155. , `customer_allow_api_access`
  5156. , `customer_points_alert_opt_in`
  5157. , `customer_whats_new_opt_in`
  5158. , `customer_hot_picks_opt_in`
  5159. , `customer_self_reporting_reminder_code`
  5160. , `customer_self_reporting_reminder_day`
  5161. , `customer_self_reporting_reminder_frequency`
  5162. , `customer_self_reporting_reminder_starts_at`
  5163. , `customer_count_showed_print_at_home_lightbox`
  5164. , `customer_contact_id`
  5165. , `customer_home_phone`
  5166. , `customer_fax_number`
  5167. , `customer_cell_phone`
  5168. , `customer_sequrity_question_id`
  5169. , `customer_sequrity_answer`
  5170. , `customer_hear_about_rb_code`
  5171. , `customer_account_id`
  5172. , `customer_account_number`
  5173. , `customer_balance`
  5174. , `customer_bonus_reward_status_code`
  5175. , `customer_subscription_type_code`
  5176. , `customer_barclays_credit_card`
  5177. , `customer_activated_online`
  5178. , `customer_login_id`
  5179. , `customer_username`
  5180. , `customer_date_username_selected`
  5181. , `customer_survey_answered`
  5182. , `customer_picture_id`
  5183. , `customer_rb_name`
  5184. , `customer_prescreen_opt_out`
  5185. , `customer_greenteam_opt_in`
  5186. , `customer_contact_status`
  5187. , `customer_job_title`
  5188. , `customer_pin_number`
  5189. , `customer_date_agreed_to_tos`
  5190. , `customer_email_hash`
  5191. , `customer_status_code`
  5192. , `customer_lock_version`
  5193. , `customer_parent_account_id`
  5194. , `customer_facebook_permissions`
  5195. , `customer_password_reset_key`
  5196. , `customer_work_phone`
  5197. , `customer_title`
  5198. , `customer_updated_at`
  5199. , `customer_security_answer`
  5200. , `customer_security_question_id`)
  5201. VALUES (
  5202. @nsales_flat_quote
  5203. , 'US'
  5204. , NULL
  5205. , NULL
  5206. , NULL
  5207. , NULL
  5208. , NULL
  5209. , NULL
  5210. , NULL
  5211. , NULL
  5212. , NULL
  5213. , NULL
  5214. , NULL
  5215. , NULL
  5216. , NULL
  5217. , NULL
  5218. , NULL
  5219. , NULL
  5220. , NULL
  5221. , NULL
  5222. , NULL
  5223. , NULL
  5224. , NULL
  5225. , NULL
  5226. , NULL
  5227. , @iaccount_number
  5228. , NULL
  5229. , NULL
  5230. , NULL
  5231. , NULL
  5232. , NULL
  5233. , NULL
  5234. , NULL
  5235. , NULL
  5236. , NULL
  5237. , NULL
  5238. , NULL
  5239. , NULL
  5240. , NULL
  5241. , NULL
  5242. , NULL
  5243. , NULL
  5244. , NULL
  5245. , NULL
  5246. , NULL
  5247. , NULL
  5248. , NULL
  5249. , NULL
  5250. , NULL
  5251. , NULL
  5252. , NULL
  5253. , '0000-00-00 00:00:00'
  5254. , NULL
  5255. , NULL);
  5256.                                
  5257. INSERT INTO `sales_flat_quote_address`
  5258. (`quote_id`
  5259. , `created_at`
  5260. , `updated_at`
  5261. , `customer_id`
  5262. , `customer_address_id`
  5263. , `address_type`
  5264. , `email`
  5265. , `prefix`
  5266. , `firstname`
  5267. , `middlename`
  5268. , `lastname`
  5269. , `suffix`
  5270. , `company`
  5271. , `street`
  5272. , `city`
  5273. , `region`
  5274. , `region_id`
  5275. , `postcode`
  5276. , `country_id`
  5277. , `telephone`
  5278. , `fax`
  5279. , `giftregistry_item_id`)
  5280. VALUES (
  5281. @nsales_flat_quote
  5282. , now()
  5283. , now()
  5284. , icustomer_id
  5285. ,@id_default_billing_address
  5286. , 'billing'
  5287. , @iemail
  5288. , NULL
  5289. , @ifirstname
  5290. , NULL
  5291. , @ilastname
  5292. , NULL
  5293. , NULL
  5294. , NULL
  5295. , NULL
  5296. , NULL
  5297. , NULL
  5298. , @ipostcode
  5299. , NULL
  5300. , NULL
  5301. , NULL
  5302. , NULL
  5303. );
  5304. SET @nsales_flat_qoute_address_id = LAST_INSERT_ID();
  5305.                                                  
  5306. INSERT INTO `enterprise_customer_sales_flat_quote_address`
  5307. (`entity_id`
  5308. , `lat`
  5309. , `lng`
  5310. , `address_id`
  5311. , `address2`
  5312. , `building_id`
  5313. , `qas_verify_level`
  5314. , `route`
  5315. , `qas_disable`
  5316. , `address3`
  5317. , `client_address_id`
  5318. , `unit_num`
  5319. , `address1`)
  5320. VALUES
  5321. (@nsales_flat_qoute_address_id
  5322. , NULL
  5323. , NULL
  5324. , @nsales_flat_qoute_address_id
  5325. , NULL
  5326. , NULL
  5327. , NULL
  5328. , NULL
  5329. , NULL
  5330. , NULL
  5331. , NULL
  5332. , NULL
  5333. , NULL);
  5334.                              
  5335. INSERT INTO `sales_flat_quote_payment`
  5336. (`quote_id`, `created_at`, `updated_at`, `method`, `additional_information`)
  5337. VALUES (@nsales_flat_quote, now(), now(), 'free', NULL);
  5338.                        
  5339. SELECT max(increment_last_id)+1
  5340.       INTO @new_order_increment_last_id
  5341.     FROM  `eav_entity_store` WHERE entity_store_id='1' AND `entity_type_id` = '5';
  5342. UPDATE `eav_entity_store`
  5343. SET
  5344.  `increment_last_id` = @new_order_increment_last_id
  5345.     WHERE entity_store_id='1' AND `entity_type_id` = '5';
  5346.                        
  5347. INSERT INTO `sales_flat_order`
  5348. (`status`
  5349. ,`state`
  5350. ,`coupon_code`
  5351. , `protect_code`
  5352. , `shipping_description`
  5353. , `is_virtual`
  5354. , `store_id`
  5355. , `customer_id`
  5356. , `base_discount_amount`
  5357. , `base_grand_total`
  5358. , `base_shipping_amount`
  5359. , `base_shipping_tax_amount`
  5360. , `base_subtotal`
  5361. , `base_tax_amount`
  5362. , `base_to_global_rate`
  5363. , `base_to_order_rate`
  5364. , `discount_amount`
  5365. , `grand_total`
  5366. , `shipping_amount`
  5367. , `shipping_tax_amount`
  5368. , `store_to_base_rate`
  5369. , `store_to_order_rate`
  5370. , `subtotal`
  5371. , `tax_amount`
  5372. , `total_qty_ordered`
  5373. , `customer_is_guest`
  5374. , `customer_note_notify`
  5375. , `customer_group_id`
  5376. , `gift_message_id`
  5377. , `quote_id`
  5378. , `base_shipping_discount_amount`
  5379. , `base_subtotal_incl_tax`
  5380. , `shipping_discount_amount`
  5381. , `subtotal_incl_tax`
  5382. , `weight`
  5383. , `customer_dob`
  5384. , `increment_id`
  5385. , `applied_rule_ids`
  5386. , `base_currency_code`
  5387. , `customer_email`
  5388. , `customer_firstname`
  5389. , `customer_lastname`
  5390. , `customer_middlename`
  5391. , `customer_prefix`
  5392. , `customer_suffix`
  5393. , `customer_taxvat`
  5394. , `discount_description`
  5395. , `global_currency_code`
  5396. , `order_currency_code`
  5397. , `remote_ip`
  5398. , `shipping_method`
  5399. , `store_currency_code`
  5400. , `store_name`
  5401. , `x_forwarded_for`
  5402. , `customer_note`
  5403. , `created_at`
  5404. , `updated_at`
  5405. , `total_item_count`
  5406. , `customer_gender`
  5407. , `hidden_tax_amount`
  5408. , `base_hidden_tax_amount`
  5409. , `shipping_hidden_tax_amount`
  5410. , `base_shipping_hidden_tax_amount`
  5411. , `shipping_incl_tax`
  5412. , `base_shipping_incl_tax`
  5413. , `base_customer_balance_amount`
  5414. , `customer_balance_amount`
  5415. , `gift_cards`
  5416. , `base_gift_cards_amount`
  5417. , `gift_cards_amount`
  5418. , `reward_points_balance`
  5419. , `base_reward_currency_amount`
  5420. , `reward_currency_amount`)
  5421. VALUES
  5422. ('complete'
  5423. ,'complete'
  5424. ,null
  5425. ,'prot'
  5426. ,''
  5427. ,0
  5428. ,1
  5429. , icustomer_id
  5430. ,0
  5431. ,@reward_price*iamount
  5432. ,0
  5433. ,0
  5434. ,@reward_price*iamount
  5435. ,0
  5436. ,1
  5437. ,1
  5438. ,0
  5439. ,@reward_price*iamount
  5440. ,'0'
  5441. ,'0'
  5442. ,'1'
  5443. ,'1'
  5444. ,@reward_price*iamount
  5445. ,0
  5446. ,iamount
  5447. ,0
  5448. ,0
  5449. , @icustomer_group_id
  5450. ,'1'
  5451. , NULL
  5452. ,@nsales_flat_quote
  5453. ,0
  5454. ,@reward_price*iamount
  5455. ,0
  5456. ,0
  5457. , @idob
  5458. ,@new_order_increment_last_id
  5459. ,''
  5460. , 'USD'
  5461. , @iemail
  5462. , @ifirstname
  5463. , @ilastname
  5464. ,null
  5465. ,null
  5466. ,null
  5467. ,null
  5468. ,null
  5469. ,'USD'
  5470. , 'USD'
  5471. ,null
  5472. ,'freeshipping_freeshipping'
  5473. , 'USD'
  5474. ,'Main Website\nMain Website Store\nUS'
  5475. ,null
  5476. ,''
  5477. ,now()
  5478. ,now()
  5479. ,1
  5480. ,null
  5481. , '0'
  5482. , '0'
  5483. , '0'
  5484. , '0'
  5485. , '0'
  5486. , '0'
  5487. , '0'
  5488. , '0'
  5489. , 'a:0:{}'
  5490. , '0'
  5491. , '0'
  5492. , '0'
  5493. , '0'
  5494. , '0'
  5495. );
  5496. SET o_id = LAST_INSERT_ID();
  5497.      
  5498.        
  5499. INSERT INTO `sales_flat_order_address`
  5500. (`parent_id`
  5501. , `customer_address_id`
  5502. , `region_id`
  5503. , `customer_id`
  5504. , `fax`
  5505. , `region`
  5506. , `postcode`
  5507. , `lastname`
  5508. , `street`
  5509. , `city`
  5510. , `telephone`
  5511. , `country_id`
  5512. , `firstname`
  5513. , `address_type`
  5514. , `prefix`
  5515. , `middlename`
  5516. , `suffix`
  5517. , `company`
  5518. , `giftregistry_item_id`)
  5519. VALUES (
  5520. o_id
  5521. , @id_default_billing_address
  5522. , NULL
  5523. , icustomer_id
  5524. , NULL
  5525. , ''
  5526. , @ipostcode
  5527. , @ilastname
  5528. , ''
  5529. , ''
  5530. , ''
  5531. , ''
  5532. , @ifirstname
  5533. , 'billing'
  5534. , NULL
  5535. , NULL
  5536. , NULL
  5537. , NULL
  5538. , NULL);
  5539. SET @new_billing_sales_flat_order_address_id = LAST_INSERT_ID();
  5540.        
  5541. INSERT INTO `enterprise_customer_sales_flat_order_address`
  5542. (`entity_id`
  5543. , `lat`
  5544. , `lng`
  5545. , `address_id`
  5546. , `address2`
  5547. , `building_id`
  5548. , `qas_verify_level`
  5549. , `route`
  5550. , `qas_disable`
  5551. , `address3`
  5552. , `client_address_id`
  5553. , `unit_num`
  5554. , `address1`)
  5555. VALUES (
  5556. @new_billing_sales_flat_order_address_id
  5557. , NULL
  5558. , NULL
  5559. ,@id_default_billing_address
  5560. , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  5561. ;
  5562. UPDATE `sales_flat_order`
  5563. SET `billing_address_id` = @new_billing_sales_flat_order_address_id
  5564.  WHERE (entity_id=o_id);
  5565.        
  5566. INSERT INTO `sales_flat_order_address`
  5567. (`parent_id`
  5568. , `customer_address_id`
  5569. , `region_id`
  5570. , `customer_id`
  5571. , `fax`
  5572. , `region`
  5573. , `postcode`
  5574. , `lastname`
  5575. , `street`
  5576. , `city`
  5577. , `telephone`
  5578. , `country_id`
  5579. , `firstname`
  5580. , `address_type`
  5581. , `prefix`
  5582. , `middlename`
  5583. , `suffix`
  5584. , `company`
  5585. , `giftregistry_item_id`)
  5586. VALUES (
  5587. o_id
  5588. , @id_default_shipping_address
  5589. , NULL
  5590. , icustomer_id
  5591. , NULL
  5592. , ''
  5593. , @ipostcode
  5594. , @ilastname
  5595. , ''
  5596. , ''
  5597. , ''
  5598. , ''
  5599. , @ifirstname
  5600. , 'shipping'
  5601. , NULL
  5602. , NULL
  5603. , NULL
  5604. , NULL
  5605. , NULL);
  5606. SET @new_shipping_sales_flat_order_address_id = LAST_INSERT_ID();
  5607.        
  5608. INSERT INTO `enterprise_customer_sales_flat_order_address`
  5609. (`entity_id`
  5610. , `lat`
  5611. , `lng`
  5612. , `address_id`
  5613. , `address2`
  5614. , `building_id`
  5615. , `qas_verify_level`
  5616. , `route`
  5617. , `qas_disable`
  5618. , `address3`
  5619. , `client_address_id`
  5620. , `unit_num`
  5621. , `address1`)
  5622. VALUES (
  5623. @new_shipping_sales_flat_order_address_id
  5624. , NULL
  5625. , NULL
  5626. ,@id_default_shipping_address
  5627. , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)
  5628. ;
  5629. UPDATE `sales_flat_order`
  5630. SET `shipping_address_id` = @new_shipping_sales_flat_order_address_id
  5631.  WHERE (entity_id=o_id);
  5632.      
  5633. INSERT INTO `sales_flat_order_item`
  5634. (`order_id`
  5635. , `quote_item_id`
  5636. , `store_id`
  5637. , `created_at`
  5638. , `updated_at`
  5639. , `product_id`
  5640. , `product_type`
  5641. , `product_options`
  5642. , `weight`
  5643. , `is_virtual`
  5644. , `sku`
  5645. , `name`
  5646. , `description`
  5647. , `applied_rule_ids`
  5648. , `additional_data`
  5649. , `is_qty_decimal`
  5650. , `qty_backordered`
  5651. , `qty_ordered`
  5652. , `base_cost`
  5653. , `price`
  5654. , `base_price`
  5655. , `original_price`
  5656. , `base_original_price`
  5657. , `tax_percent`
  5658. , `tax_amount`
  5659. , `base_tax_amount`
  5660. , `row_total`
  5661. , `base_row_total`
  5662. , `row_weight`
  5663. , `gift_message_id`
  5664. , `gift_message_available`
  5665. , `base_tax_before_discount`
  5666. , `tax_before_discount`
  5667. , `weee_tax_applied`
  5668. , `weee_tax_applied_amount`
  5669. , `weee_tax_applied_row_amount`
  5670. , `base_weee_tax_applied_amount`
  5671. , `base_weee_tax_applied_row_amount`
  5672. , `weee_tax_disposition`
  5673. , `weee_tax_row_disposition`
  5674. , `base_weee_tax_disposition`
  5675. , `base_weee_tax_row_disposition`
  5676. , `price_incl_tax`
  5677. , `base_price_incl_tax`
  5678. , `row_total_incl_tax`
  5679. , `base_row_total_incl_tax`
  5680. , `hidden_tax_amount`
  5681. , `base_hidden_tax_amount`
  5682. , `is_nominal`
  5683. , `event_id`)
  5684. VALUES (o_id
  5685. ,@n_sales_flat_quote_item
  5686. ,istore_id
  5687. , now()
  5688. , now()
  5689. ,iproduct_id
  5690. ,@reward_type
  5691. , 'a:5:{s:15:\"info_buyRequest\";a:2:{s:3:\"qty\";i:1;s:7:\"options\";a:0:{}}s:17:\"giftcard_lifetime\";N;s:22:\"giftcard_is_redeemable\";i:0;s:23:\"giftcard_email_template\";N;s:13:\"giftcard_type\";N;}'
  5692. , NULL
  5693. , 0
  5694. ,@reward_sku
  5695. ,@reward_name
  5696. , NULL
  5697. , ''
  5698. , NULL
  5699. , '0'
  5700. , NULL
  5701. ,iamount
  5702. , NULL
  5703. ,@reward_price
  5704. ,@reward_price
  5705. ,@reward_price
  5706. ,@reward_price
  5707. , '0'
  5708. , '0'
  5709. , '0'
  5710. ,@reward_price*iamount
  5711. ,@reward_price*iamount
  5712. , '0'
  5713. , NULL
  5714. , NULL
  5715. , NULL
  5716. , NULL
  5717. , 'a:0:{}'
  5718. , '0'
  5719. , '0'
  5720. , '0'
  5721. , '0'
  5722. , '0'
  5723. , '0'
  5724. , '0'
  5725. , '0'
  5726. ,@reward_price
  5727. ,@reward_price
  5728. ,@reward_price*iamount
  5729. ,@reward_price*iamount
  5730. , '0'
  5731. , '0'
  5732. , '0'
  5733. , NULL
  5734. );
  5735. SET @new_sales_flat_order_item = LAST_INSERT_ID();
  5736.      
  5737. INSERT INTO `sales_flat_order_payment`
  5738. (`parent_id`
  5739. , `additional_data`
  5740. , `cc_exp_month`
  5741. , `cc_ss_start_year`
  5742. , `method`
  5743. , `cc_last4`
  5744. , `cc_ss_start_month`
  5745. , `cc_owner`
  5746. , `cc_type`
  5747. , `po_number`
  5748. , `cc_exp_year`
  5749. , `cc_ss_issue`
  5750. , `cc_number_enc`
  5751. , `additional_information`)
  5752. VALUES (o_id, NULL, '0', '0', 'free', '', '0', '', '', '', '0', NULL, '', NULL);
  5753.      
  5754. INSERT INTO `sales_flat_order_grid`
  5755. (`entity_id`
  5756. ,`status`
  5757. ,`store_id`
  5758. ,`customer_id`
  5759. ,`base_grand_total`
  5760. ,`base_total_paid`
  5761. ,`grand_total`
  5762. ,`total_paid`
  5763. ,`increment_id`
  5764. ,`base_currency_code`
  5765. ,`order_currency_code`
  5766. ,`store_name`
  5767. ,`created_at`
  5768. ,`updated_at`
  5769. ,`billing_name`
  5770. ,`shipping_name`)
  5771. SELECT
  5772. `main_table`.`entity_id`
  5773. , `main_table`.`status`, `main_table`.`store_id`, `main_table`.`customer_id`, `main_table`.`base_grand_total`, `main_table`.`base_total_paid`, `main_table`.`grand_total`, `main_table`.`total_paid`, `main_table`.`increment_id`, `main_table`.`base_currency_code`, `main_table`.`order_currency_code`, `main_table`.`store_name`, `main_table`.`created_at`, `main_table`.`updated_at`, CONCAT(IFNULL(_table_billing_name.firstname, ""), " ", IFNULL(_table_billing_name.lastname, "")) AS `billing_name`, CONCAT(IFNULL(_table_shipping_name.firstname, ""), " ", IFNULL(_table_shipping_name.lastname, "")) AS `shipping_name`
  5774.  FROM `sales_flat_order` AS `main_table`
  5775.  LEFT JOIN `sales_flat_order_address` AS `_table_billing_name` ON `main_table`.`billing_address_id`=`_table_billing_name`.`entity_id`
  5776.  LEFT JOIN `sales_flat_order_address` AS `_table_shipping_name` ON `main_table`.`shipping_address_id`=`_table_shipping_name`.`entity_id`
  5777. WHERE (main_table.entity_id = o_id ) ;
  5778.      
  5779. INSERT INTO `enterprise_customer_sales_flat_order`
  5780. (`entity_id`
  5781. , `customer_language_code`
  5782. , `customer_reset_code`
  5783. , `customer_date_last_login`
  5784. , `customer_subbed_to_recyclebank_news`
  5785. , `customer_subbed_to_partner_offers`
  5786. , `customer_village_green_opt_in`
  5787. , `customer_rewards_email_opt_in`
  5788. , `customer_allow_api_access`
  5789. , `customer_points_alert_opt_in`
  5790. , `customer_whats_new_opt_in`
  5791. , `customer_hot_picks_opt_in`
  5792. , `customer_self_reporting_reminder_code`
  5793. , `customer_self_reporting_reminder_day`
  5794. , `customer_self_reporting_reminder_frequency`
  5795. , `customer_self_reporting_reminder_starts_at`
  5796. , `customer_count_showed_print_at_home_lightbox`
  5797. , `customer_contact_id`, `customer_home_phone`
  5798. , `customer_fax_number`, `customer_cell_phone`
  5799. , `customer_sequrity_question_id`
  5800. , `customer_sequrity_answer`
  5801. , `customer_hear_about_rb_code`
  5802. , `customer_account_id`
  5803. , `customer_account_number`
  5804. , `customer_balance`
  5805. , `customer_bonus_reward_status_code`
  5806. , `customer_subscription_type_code`
  5807. , `customer_barclays_credit_card`
  5808. , `customer_activated_online`
  5809. , `customer_login_id`, `customer_username`
  5810. , `customer_date_username_selected`
  5811. , `customer_survey_answered`
  5812. , `customer_picture_id`
  5813. , `customer_rb_name`
  5814. , `customer_prescreen_opt_out`
  5815. , `customer_greenteam_opt_in`
  5816. , `customer_contact_status`
  5817. , `customer_job_title`
  5818. , `customer_pin_number`
  5819. , `customer_date_agreed_to_tos`
  5820. , `customer_email_hash`
  5821. , `customer_status_code`
  5822. , `customer_lock_version`
  5823. , `customer_parent_account_id`
  5824. , `customer_facebook_permissions`
  5825. , `customer_password_reset_key`
  5826. , `customer_work_phone`
  5827. , `customer_title`
  5828. , `customer_updated_at`
  5829. , `customer_security_answer`
  5830. , `customer_security_question_id`)
  5831. VALUES (o_id
  5832. , 'US'
  5833. , NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, @iaccount_number, NULL, 'B3121356', NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, '0000-00-00 00:00:00', NULL, NULL)
  5834. ;
  5835.      
  5836. SELECT max(increment_last_id)+1
  5837.       INTO @new_inv_increment_last_id
  5838.     FROM  `eav_entity_store` WHERE entity_store_id='3' AND `entity_type_id` = '6';
  5839. UPDATE `eav_entity_store`
  5840. SET
  5841.  `increment_last_id` = @new_inv_increment_last_id  
  5842.     WHERE entity_store_id='3' AND `entity_type_id` = '6';
  5843. INSERT INTO `sales_flat_invoice`
  5844. (`store_id`
  5845. , `base_grand_total`
  5846. , `shipping_tax_amount`
  5847. , `tax_amount`
  5848. , `base_tax_amount`
  5849. , `store_to_order_rate`
  5850. , `base_shipping_tax_amount`
  5851. , `base_discount_amount`
  5852. , `base_to_order_rate`
  5853. , `grand_total`
  5854. , `shipping_amount`
  5855. , `subtotal_incl_tax`
  5856. , `base_subtotal_incl_tax`
  5857. , `store_to_base_rate`
  5858. , `base_shipping_amount`
  5859. , `base_to_global_rate`
  5860. , `subtotal`
  5861. , `base_subtotal`
  5862. , `discount_amount`
  5863. , `billing_address_id`
  5864. , `order_id`
  5865. , `state`
  5866. , `shipping_address_id`
  5867. , `store_currency_code`
  5868. , `order_currency_code`
  5869. , `base_currency_code`
  5870. , `global_currency_code`
  5871. , `increment_id`
  5872. , `created_at`
  5873. , `updated_at`
  5874. , `hidden_tax_amount`
  5875. , `base_hidden_tax_amount`
  5876. , `shipping_hidden_tax_amount`
  5877. , `base_shipping_hidden_tax_amount`
  5878. , `base_customer_balance_amount`
  5879. , `customer_balance_amount`)
  5880. VALUES (
  5881. istore_id
  5882. , '0'
  5883. , '0'
  5884. , '0'
  5885. , '0'
  5886. , istore_id
  5887. , '0'
  5888. , '0'
  5889. , '1'
  5890. , '0'
  5891. , '0'
  5892. ,@reward_price*iamount
  5893. ,@reward_price*iamount
  5894. , istore_id
  5895. , '0'
  5896. , '1'
  5897. ,@reward_price*iamount
  5898. ,@reward_price*iamount
  5899. , '0'
  5900. ,@new_billing_sales_flat_order_address_id
  5901. , o_id
  5902. , '1'
  5903. , @new_shipping_sales_flat_order_address_id
  5904. , 'USD'
  5905. , 'USD'
  5906. , 'USD'
  5907. , 'USD'
  5908. , @new_inv_increment_last_id
  5909. , now()
  5910. , now()
  5911. , '0'
  5912. , '0'
  5913. , '0'
  5914. , '0'
  5915. ,@reward_price*iamount
  5916. ,@reward_price*iamount
  5917. );
  5918. SET @new_sales_flat_invoice_id = LAST_INSERT_ID();
  5919.      
  5920. INSERT INTO `sales_flat_invoice_item`
  5921. (`parent_id`
  5922. , `base_price`
  5923. , `base_weee_tax_row_disposition`
  5924. , `weee_tax_applied_row_amount`
  5925. , `base_weee_tax_applied_amount`
  5926. , `base_row_total`
  5927. , `row_total`
  5928. , `weee_tax_row_disposition`
  5929. , `base_weee_tax_disposition`
  5930. , `price_incl_tax`
  5931. , `weee_tax_applied_amount`
  5932. , `base_price_incl_tax`
  5933. , `qty`
  5934. , `weee_tax_disposition`
  5935. , `base_cost`
  5936. , `base_weee_tax_applied_row_amount`
  5937. , `price`
  5938. , `base_row_total_incl_tax`
  5939. , `row_total_incl_tax`
  5940. , `product_id`
  5941. , `order_item_id`
  5942. , `description`
  5943. , `weee_tax_applied`
  5944. , `sku`
  5945. , `name`
  5946. )
  5947. VALUES (
  5948. @new_sales_flat_invoice_id
  5949. , @reward_price
  5950. , '0'
  5951. , '0'
  5952. , '0'
  5953. , @reward_price*iamount
  5954. , @reward_price*iamount
  5955. , '0'
  5956. , '0'
  5957. , '50'
  5958. , '0'
  5959. , @reward_price*iamount
  5960. , iamount
  5961. , '0'
  5962. , NULL
  5963. , '0'
  5964. , @reward_price
  5965. ,@reward_price*iamount
  5966. , @reward_price*iamount
  5967. , iproduct_id
  5968. , o_id
  5969. , NULL
  5970. , 'a:0:{}'
  5971. , @reward_sku
  5972. ,@reward_name
  5973. );
  5974.      
  5975. INSERT INTO `sales_flat_invoice_comment` (`parent_id`, `is_customer_notified`, `comment`, `created_at`)
  5976. VALUES (@new_sales_flat_invoice_id, null, 'Auto invoice', now());
  5977.      
  5978. INSERT INTO `sales_flat_invoice_grid` (`entity_id`,`store_id`,`base_grand_total`,`grand_total`,`order_id`,`state`,`store_currency_code`,`order_currency_code`,`base_currency_code`,`global_currency_code`,`increment_id`,`created_at`,`billing_name`,`order_increment_id`,`order_created_at`) SELECT `main_table`.`entity_id`, `main_table`.`store_id`, `main_table`.`base_grand_total`, `main_table`.`grand_total`, `main_table`.`order_id`, `main_table`.`state`, `main_table`.`store_currency_code`, `main_table`.`order_currency_code`, `main_table`.`base_currency_code`, `main_table`.`global_currency_code`, `main_table`.`increment_id`, `main_table`.`created_at`, CONCAT(IFNULL(_table_billing_name.firstname, ""), " ", IFNULL(_table_billing_name.lastname, "")) AS `billing_name`, `_table_order_increment_id`.`increment_id` AS `order_increment_id`, `_table_order_created_at`.`created_at` AS `order_created_at`
  5979. FROM `sales_flat_invoice` AS `main_table`
  5980.  LEFT JOIN `sales_flat_order_address` AS `_table_billing_name`
  5981. ON `main_table`.`billing_address_id`=`_table_billing_name`.`entity_id`
  5982.  LEFT JOIN `sales_flat_order` AS `_table_order_increment_id`
  5983. ON `main_table`.`order_id`=`_table_order_increment_id`.`entity_id`
  5984.  LEFT JOIN `sales_flat_order` AS `_table_order_created_at`
  5985. ON `main_table`.`order_id`=`_table_order_created_at`.`entity_id`
  5986. WHERE (main_table.entity_id = @new_sales_flat_invoice_id) ;
  5987.      
  5988. INSERT INTO `sales_flat_order_status_history`
  5989. (`parent_id`, `is_customer_notified`, `comment`, `status`, `created_at`)
  5990. VALUES (o_id, '', '', 'pending', now());
  5991.      
  5992. INSERT INTO `sales_flat_order_status_history`
  5993. (`parent_id`, `is_customer_notified`, `comment`, `status`, `created_at`)
  5994. VALUES (o_id, '', '', 'processing', now());
  5995.      
  5996. SELECT max(increment_last_id)+1
  5997.       INTO @new_ship_increment_last_id
  5998.     FROM  `eav_entity_store` WHERE entity_store_id='4' AND `entity_type_id` = '8';
  5999. UPDATE `eav_entity_store`
  6000. SET
  6001.  `increment_last_id` = @new_ship_increment_last_id  
  6002.     WHERE entity_store_id='4' AND `entity_type_id` = '8';
  6003. INSERT INTO `sales_flat_shipment`
  6004. (`store_id`, `total_qty`, `order_id`, `customer_id`, `shipping_address_id`, `billing_address_id`, `increment_id`, `created_at`, `updated_at`)
  6005. VALUES (istore_id, iamount, o_id, icustomer_id, @new_shipping_sales_flat_order_address_id, @new_billing_sales_flat_order_address_id
  6006. , @new_ship_increment_last_id  , now(), now() );
  6007. SET @new_sales_flat_shipment = LAST_INSERT_ID();
  6008.      
  6009. INSERT INTO `sales_flat_shipment_item` (`parent_id`, `price`, `weight`, `qty`, `product_id`, `order_item_id`, `description`, `name`, `sku`)
  6010. VALUES (@new_sales_flat_shipment, @reward_price, NULL, iamount, iproduct_id, @new_sales_flat_order_item, NULL, @reward_name, @reward_sku);
  6011. SET @new_sales_flat_shipment_item =LAST_INSERT_ID();
  6012.      
  6013. INSERT INTO `sales_flat_shipment_comment` (`parent_id`, `is_customer_notified`, `comment`, `created_at`)
  6014. VALUES (@new_sales_flat_shipment, '', 'Auto shippment', now());
  6015.      
  6016. INSERT INTO `sales_flat_shipment_grid` (`entity_id`,`store_id`,`total_qty`,`order_id`,`shipment_status`,`increment_id`,`created_at`,`shipping_name`,`order_increment_id`,`order_created_at`) SELECT `main_table`.`entity_id`, `main_table`.`store_id`, `main_table`.`total_qty`, `main_table`.`order_id`, `main_table`.`shipment_status`, `main_table`.`increment_id`, `main_table`.`created_at`, CONCAT(IFNULL(_table_shipping_name.firstname, ""), " ", IFNULL(_table_shipping_name.lastname, "")) AS `shipping_name`, `_table_order_increment_id`.`increment_id` AS `order_increment_id`, `_table_order_created_at`.`created_at` AS `order_created_at`
  6017. FROM `sales_flat_shipment` AS `main_table`
  6018.  LEFT JOIN `sales_flat_order_address` AS `_table_shipping_name`
  6019. ON `main_table`.`shipping_address_id`=`_table_shipping_name`.`entity_id`
  6020.  LEFT JOIN `sales_flat_order` AS `_table_order_increment_id`
  6021. ON `main_table`.`order_id`=`_table_order_increment_id`.`entity_id`
  6022.  LEFT JOIN `sales_flat_order` AS `_table_order_created_at` ON `main_table`.`order_id`=`_table_order_created_at`.`entity_id`
  6023. WHERE (main_table.entity_id = @new_sales_flat_shipment) ;
  6024.      
  6025. INSERT INTO `sales_flat_order_status_history` (`parent_id`, `is_customer_notified`, `comment`, `status`, `created_at`)
  6026. VALUES (o_id, '', '', 'complete', now());
  6027.      
  6028. INSERT INTO `sales_flat_order_grid` (`entity_id`,`status`,`store_id`,`customer_id`,`base_grand_total`,`base_total_paid`,`grand_total`,`total_paid`,`increment_id`,`base_currency_code`,`order_currency_code`,`store_name`,`created_at`,`updated_at`,`billing_name`,`shipping_name`) SELECT `main_table`.`entity_id`, `main_table`.`status`, `main_table`.`store_id`, `main_table`.`customer_id`, `main_table`.`base_grand_total`, `main_table`.`base_total_paid`, `main_table`.`grand_total`, `main_table`.`total_paid`, `main_table`.`increment_id`, `main_table`.`base_currency_code`, `main_table`.`order_currency_code`, `main_table`.`store_name`, `main_table`.`created_at`, `main_table`.`updated_at`, CONCAT(IFNULL(_table_billing_name.firstname, ""), " ", IFNULL(_table_billing_name.lastname, "")) AS `billing_name`, CONCAT(IFNULL(_table_shipping_name.firstname, ""), " ", IFNULL(_table_shipping_name.lastname, "")) AS `shipping_name`
  6029.  FROM `sales_flat_order` AS `main_table`
  6030.  LEFT JOIN `sales_flat_order_address` AS `_table_billing_name`
  6031. ON `main_table`.`billing_address_id`=`_table_billing_name`.`entity_id`
  6032.  LEFT JOIN `sales_flat_order_address` AS `_table_shipping_name`
  6033. ON `main_table`.`shipping_address_id`=`_table_shipping_name`.`entity_id`
  6034. WHERE (main_table.entity_id = o_id ) ON DUPLICATE KEY UPDATE `entity_id`=VALUES(`entity_id`), `status`=VALUES(`status`), `store_id`=VALUES(`store_id`), `customer_id`=VALUES(`customer_id`), `base_grand_total`=VALUES(`base_grand_total`), `base_total_paid`=VALUES(`base_total_paid`), `grand_total`=VALUES(`grand_total`), `total_paid`=VALUES(`total_paid`), `increment_id`=VALUES(`increment_id`), `base_currency_code`=VALUES(`base_currency_code`), `order_currency_code`=VALUES(`order_currency_code`), `store_name`=VALUES(`store_name`), `created_at`=VALUES(`created_at`), `updated_at`=VALUES(`updated_at`), `billing_name`=VALUES(`billing_name`), `shipping_name`=VALUES(`shipping_name`);
  6035.      
  6036. INSERT INTO `enterprise_customerbalance_history`
  6037. (`balance_id`, `updated_at`, `action`, `balance_amount`, `balance_delta`, `additional_info`, `reason_id`)
  6038. VALUES (@ibalance_id, now(), '3', @balance_amount - (@reward_price*iamount), -(@reward_price*iamount), concat('Order ',@new_order_increment_last_id), '12');
  6039.      
  6040. UPDATE enterprise_customerbalance ecb
  6041. SET ecb.amount= ecb.amount - (@reward_price*iamount)
  6042. WHERE ecb.balance_id = @ibalance_id;
  6043. UPDATE `cataloginventory_stock_status` css
  6044. SET css.`qty` = css.`qty` - iamount
  6045. WHERE `product_id` = iproduct_id AND `stock_id` =1 ;
  6046. UPDATE cataloginventory_stock_item csi
  6047.     SET csi.`qty` = csi.`qty` - iamount
  6048. WHERE csi.`product_id` = iproduct_id AND csi.`stock_id` =1 ;
  6049. INSERT INTO `sales_flat_quote_item`
  6050. (`quote_id`
  6051. , `created_at`
  6052. , `updated_at`
  6053. , `product_id`
  6054. , `store_id`
  6055. , `is_virtual`
  6056. , `sku`
  6057. , `name`
  6058. , `is_qty_decimal`
  6059. , `weight`
  6060. , `qty`
  6061. , `product_type`
  6062. , `base_cost`
  6063. )
  6064. VALUES (
  6065. @nsales_flat_quote
  6066. , now()
  6067. , now()
  6068. ,iproduct_id
  6069. ,istore_id
  6070. , NULL
  6071. ,@reward_sku
  6072. ,@reward_name
  6073. , '0'
  6074. , NULL
  6075. ,iamount
  6076. ,@reward_type
  6077. , NULL
  6078. );
  6079.                                                    
  6080.         END;       
  6081.         COMMIT;
  6082.         SELECT case when o_id is not null THEN 1 ELSE 0 END
  6083.                ,case when o_id is not null THEN "order created" ELSE "order not created"  END
  6084.         INTO success,reason;
  6085. ELSE
  6086.     SELECT 0 ,"insufficient customer balance amount or catalog product qty" INTO success,reason;
  6087. END IF;    
  6088. ELSE
  6089.     SELECT 0 ,"customer entity or product entity does not exists" INTO success,reason;
  6090. END IF;
  6091. END */;;
  6092. DELIMITER ;
  6093. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6094. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6095. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6096. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6097. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_CUSTOMER_TRANSACTIONS` */;
  6098. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6099. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6100. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6101. /*!50003 SET character_set_client  = utf8 */ ;
  6102. /*!50003 SET character_set_results = utf8 */ ;
  6103. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6104. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6105. /*!50003 SET sql_mode              = '' */ ;
  6106. DELIMITER ;;
  6107. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_CUSTOMER_TRANSACTIONS`(customerId int(11))
  6108. BEGIN
  6109.  
  6110. SELECT ecbh.balance_delta as 'point_change',
  6111.        CASE
  6112.          WHEN ecbh.description IS NOT NULL
  6113.            THEN ecbh.description
  6114.          WHEN ecbh.additional_info IS NOT NULL
  6115.            THEN CONCAT('Reward ', ecbh.additional_info)
  6116.          ELSE
  6117.             ecbr.reason_code
  6118.        END as 'reason',
  6119.        ecbh.updated_at as 'date'
  6120. FROM enterprise_customerbalance_history ecbh
  6121.   INNER JOIN enterprise_customerbalance ecb ON ecbh.balance_id = ecb.balance_id
  6122.   LEFT OUTER JOIN enterprise_customerbalance_reasons ecbr ON ecbr.id = ecbh.reason_id
  6123. WHERE ecb.customer_id = customerId AND ecbr.reason_code <> 'Order'
  6124.  
  6125. UNION ALL
  6126.  
  6127. SELECT -o.subtotal AS 'point_change',
  6128.        CONCAT('Reward Order #', o.increment_id) AS 'reason',
  6129.        o.updated_at AS 'date'
  6130. FROM customer_entity c
  6131.     INNER JOIN sales_flat_order o ON c.entity_id = o.customer_id
  6132. WHERE c.entity_id = customerId
  6133.  
  6134. ORDER BY 3 DESC;
  6135.  
  6136. END */;;
  6137. DELIMITER ;
  6138. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6139. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6140. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6141. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6142. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_DEALS_REWARDS` */;
  6143. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6144. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6145. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6146. /*!50003 SET character_set_client  = utf8 */ ;
  6147. /*!50003 SET character_set_results = utf8 */ ;
  6148. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6149. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6150. /*!50003 SET sql_mode              = '' */ ;
  6151. DELIMITER ;;
  6152. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_DEALS_REWARDS`(custId int(11), ilt FLOAT, ilg FLOAT)
  6153. BEGIN
  6154. DECLARE zips_seq VARCHAR(8000) DEFAULT '\'X\'';
  6155. DECLARE one_zip VARCHAR(255);
  6156. DECLARE one_distance DOUBLE;
  6157. DECLARE l_last_row_fetched INT;
  6158. DECLARE storeId INT;
  6159. DECLARE near_zip_cur CURSOR FOR
  6160. SELECT zip, distance
  6161. FROM near_zip;
  6162. DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row_fetched=1;
  6163. drop temporary table if exists near_zip;
  6164. create temporary table near_zip(zip varchar(10), distance double);
  6165. insert into near_zip(zip, distance)
  6166. SELECT ra.zip AS zip, (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) AS distance
  6167. FROM zip_geo ra
  6168. WHERE (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) <= 30
  6169. ORDER BY 2
  6170. LIMIT 200;
  6171. SET l_last_row_fetched=0;
  6172. OPEN near_zip_cur;
  6173. zip_loop: LOOP
  6174.            FETCH near_zip_cur INTO one_zip, one_distance;
  6175.            IF l_last_row_fetched = 1 THEN
  6176.              LEAVE zip_loop;
  6177.            END IF;
  6178.            SET zips_seq = CONCAT(zips_seq, ',\'', one_zip, '\'');
  6179. END LOOP zip_loop;
  6180. SET l_last_row_fetched = 0;
  6181. CLOSE near_zip_cur;
  6182. SELECT store_id
  6183. FROM customer_entity
  6184. WHERE entity_id = custId
  6185. INTO storeId;
  6186. SET @str_sql=concat(
  6187. "SELECT cpe.entity_id as `id`
  6188.     ,cpev_name.value as `name`
  6189.     ,cpev_image.value as `image`
  6190.     ,cped_price.value as `price`
  6191.     ,pe.name as `sponsor_name`
  6192. FROM
  6193.     catalog_product_entity cpe
  6194. INNER JOIN  catalog_category_product ccp on cpe.entity_id = ccp.product_id
  6195. INNER JOIN partners_entity pe on cpe.partner_id = pe.id
  6196. INNER JOIN catalog_category_entity cce on ccp.category_id = cce.entity_id
  6197. INNER JOIN catalog_product_entity_varchar cpev_name
  6198.     on cpe.entity_id = cpev_name.entity_id AND cpev_name.attribute_id = 60
  6199. INNER JOIN catalog_category_entity_varchar ccev_name
  6200.     on cce.entity_id = ccev_name.entity_id AND ccev_name.attribute_id = 33 AND ccev_name.value = '_Deals'
  6201. INNER JOIN catalog_product_entity_int cpei ON cpe.entity_id = cpei.entity_id AND cpei.attribute_id = 442 AND cpei.store_id = 0 AND
  6202. (EXISTS (SELECT * FROM partners_local_rewards_stores AS plrs INNER JOIN partners_reward_address AS pra ON plrs.store_id = pra.id WHERE cpe.entity_id = plrs.product_id AND pra.post_code IN (", zips_seq, ")) or cpei.value = 1)
  6203. INNER JOIN cataloginventory_stock_item csi ON cpe.entity_id = csi.product_id AND csi.qty > 0
  6204. LEFT JOIN catalog_product_entity_varchar cpev_image
  6205.     on cpe.entity_id = cpev_image.entity_id AND cpev_image.attribute_id = 74
  6206. LEFT JOIN catalog_product_entity_decimal cped_price
  6207.     on cpe.entity_id = cped_price.entity_id AND cped_price.attribute_id = 64
  6208. WHERE cpe.is_uk = ", case storeId WHEN 2 THEN "1 " ELSE "0 " END,
  6209. "ORDER by cped_price.value ");
  6210.     PREPARE stmt FROM @str_sql;
  6211.     EXECUTE stmt;
  6212. drop temporary table near_zip;
  6213. END */;;
  6214. DELIMITER ;
  6215. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6216. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6217. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6218. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6219. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_FEATURES_REWARDS` */;
  6220. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6221. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6222. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6223. /*!50003 SET character_set_client  = utf8 */ ;
  6224. /*!50003 SET character_set_results = utf8 */ ;
  6225. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6226. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6227. /*!50003 SET sql_mode              = '' */ ;
  6228. DELIMITER ;;
  6229. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_FEATURES_REWARDS`(custId int(11), ilt FLOAT, ilg FLOAT)
  6230. BEGIN
  6231. DECLARE zips_seq VARCHAR(8000) DEFAULT '\'X\'';
  6232. DECLARE one_zip VARCHAR(255);
  6233. DECLARE one_distance DOUBLE;
  6234. DECLARE l_last_row_fetched INT;
  6235. DECLARE storeId INT;
  6236. DECLARE near_zip_cur CURSOR FOR
  6237. SELECT zip, distance
  6238. FROM near_zip;
  6239. DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row_fetched=1;
  6240. drop temporary table if exists near_zip;
  6241. create temporary table near_zip(zip varchar(10), distance double);
  6242. insert into near_zip(zip, distance)
  6243. SELECT ra.zip AS zip, (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) AS distance
  6244. FROM zip_geo ra
  6245. WHERE (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) <= 30
  6246. ORDER BY 2
  6247. LIMIT 200;
  6248. SET l_last_row_fetched=0;
  6249. OPEN near_zip_cur;
  6250. zip_loop: LOOP
  6251.            FETCH near_zip_cur INTO one_zip, one_distance;
  6252.            IF l_last_row_fetched = 1 THEN
  6253.              LEAVE zip_loop;
  6254.            END IF;
  6255.            SET zips_seq = CONCAT(zips_seq, ',\'', one_zip, '\'');
  6256. END LOOP zip_loop;
  6257. SET l_last_row_fetched = 0;
  6258. CLOSE near_zip_cur;
  6259. SELECT store_id
  6260. FROM customer_entity
  6261. WHERE entity_id = custId
  6262. INTO storeId;
  6263. SET @str_sql=concat(
  6264. "SELECT cpe.entity_id as `id`
  6265.     ,cpev_name.value as `name`
  6266.     ,cpev_image.value as `image`
  6267.     ,cped_price.value as `price`
  6268.     ,pe.name as `sponsor_name`
  6269. FROM
  6270.     catalog_product_entity cpe
  6271. INNER JOIN  catalog_category_product ccp on cpe.entity_id = ccp.product_id
  6272. INNER JOIN partners_entity pe on cpe.partner_id = pe.id
  6273. INNER JOIN catalog_category_entity cce on ccp.category_id = cce.entity_id
  6274. INNER JOIN catalog_product_entity_varchar cpev_name
  6275.     on cpe.entity_id = cpev_name.entity_id AND cpev_name.attribute_id = 60
  6276. INNER JOIN catalog_category_entity_varchar ccev_name
  6277.     on cce.entity_id = ccev_name.entity_id AND ccev_name.attribute_id = 33 AND ccev_name.value = '_Featured'
  6278. INNER JOIN catalog_product_entity_int cpei ON cpe.entity_id = cpei.entity_id AND cpei.attribute_id = 442 AND cpei.store_id = 0 AND
  6279. (EXISTS (SELECT * FROM partners_local_rewards_stores AS plrs INNER JOIN partners_reward_address AS pra ON plrs.store_id = pra.id WHERE cpe.entity_id = plrs.product_id AND pra.post_code IN (", zips_seq, ")) or cpei.value = 1)
  6280. INNER JOIN cataloginventory_stock_item csi ON cpe.entity_id = csi.product_id AND csi.qty > 0
  6281. LEFT JOIN catalog_product_entity_varchar cpev_image
  6282.     on cpe.entity_id = cpev_image.entity_id AND cpev_image.attribute_id = 74
  6283. LEFT JOIN catalog_product_entity_decimal cped_price
  6284.     on cpe.entity_id = cped_price.entity_id AND cped_price.attribute_id = 64
  6285. WHERE cpe.is_uk = ", case storeId WHEN 2 THEN "1 " ELSE "0 " END,
  6286. "ORDER by cped_price.value ");
  6287.     PREPARE stmt FROM @str_sql;
  6288.     EXECUTE stmt;
  6289. drop temporary table near_zip;
  6290. END */;;
  6291. DELIMITER ;
  6292. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6293. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6294. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6295. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6296. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_LAST_REWARD` */;
  6297. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6298. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6299. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6300. /*!50003 SET character_set_client  = utf8 */ ;
  6301. /*!50003 SET character_set_results = utf8 */ ;
  6302. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6303. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6304. /*!50003 SET sql_mode              = '' */ ;
  6305. DELIMITER ;;
  6306. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_LAST_REWARD`(custId int(11))
  6307. BEGIN
  6308.  
  6309.   SELECT sfo.date_ordered AS 'date_ordered', pe.name AS 'vendor_name'
  6310.   FROM customer_entity ce
  6311.       INNER JOIN sales_flat_order sfo ON ce.entity_id = sfo.customer_id
  6312.       INNER JOIN sales_flat_order_item sfoi ON sfo.entity_id = sfoi.order_id
  6313.       LEFT OUTER JOIN catalog_product_entity cpe ON sfoi.product_id = cpe.entity_id
  6314.       LEFT OUTER JOIN partners_entity pe ON cpe.partner_id = pe.id
  6315.   WHERE ce.entity_id = custId
  6316.   ORDER BY sfo.date_ordered DESC, sfoi.created_at DESC
  6317.   LIMIT 0, 1;
  6318.  
  6319.  
  6320.  
  6321.  
  6322.  
  6323.  
  6324.  
  6325.  
  6326.  
  6327.  
  6328.  
  6329.  
  6330. END */;;
  6331. DELIMITER ;
  6332. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6333. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6334. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6335. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6336. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_POINTSCODES_PARTNERS` */;
  6337. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6338. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6339. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6340. /*!50003 SET character_set_client  = utf8 */ ;
  6341. /*!50003 SET character_set_results = utf8 */ ;
  6342. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6343. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6344. /*!50003 SET sql_mode              = '' */ ;
  6345. DELIMITER ;;
  6346. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_POINTSCODES_PARTNERS`(custId int(11))
  6347. BEGIN
  6348.    
  6349.     DECLARE earn_pointcodes_type VARCHAR(32) DEFAULT 'earn_pointscodes';
  6350.     DECLARE country_us VARCHAR(255) DEFAULT 'USA';
  6351.     DECLARE country_uk VARCHAR(255) DEFAULT 'GBR';
  6352.    
  6353.    
  6354.     SELECT
  6355.         pe.id as 'id',
  6356.         pe.name as 'name',
  6357.         pe.country as 'country',
  6358.         cpe.is_uk as 'is_uk'
  6359.     FROM partners_entity pe
  6360.         INNER JOIN partners_earn per
  6361.             ON pe.id = per.partner_id
  6362.         INNER JOIN catalog_product_entity cpe
  6363.             ON pe.id = cpe.partner_id
  6364.         INNER JOIN catalog_product_entity_varchar cpe_name
  6365.             ON cpe.entity_id = cpe_name.entity_id AND cpe_name.attribute_id = 60
  6366.         INNER JOIN customer_entity ce
  6367.             ON ce.entity_id = custId
  6368.     WHERE
  6369.         cpe.type_id = earn_pointcodes_type
  6370.       AND
  6371.         CASE
  6372.             WHEN ce.store_id = 1
  6373.                 THEN pe.country = country_us
  6374.             WHEN ce.store_id = 2
  6375.                 THEN pe.country = country_uk
  6376.             ELSE 1=1
  6377.         END
  6378.     GROUP BY pe.id;
  6379.    
  6380. END */;;
  6381. DELIMITER ;
  6382. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6383. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6384. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6385. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6386. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARDS_BY_CATEGORY` */;
  6387. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6388. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6389. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6390. /*!50003 SET character_set_client  = utf8 */ ;
  6391. /*!50003 SET character_set_results = utf8 */ ;
  6392. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6393. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6394. /*!50003 SET sql_mode              = '' */ ;
  6395. DELIMITER ;;
  6396. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARDS_BY_CATEGORY`(custId int(11), categoryId int(11), getNum int(11), skipNum int(11), priceOrder bool, ilt FLOAT, ilg FLOAT)
  6397. BEGIN
  6398. DECLARE zips_seq VARCHAR(8000) DEFAULT '\'X\'';
  6399. DECLARE one_zip VARCHAR(255);
  6400. DECLARE one_distance DOUBLE;
  6401. DECLARE l_last_row_fetched INT;
  6402. DECLARE storeId INT;
  6403. DECLARE near_zip_cur CURSOR FOR
  6404. SELECT zip, distance
  6405. FROM near_zip;
  6406. DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row_fetched=1;
  6407. drop temporary table if exists near_zip;
  6408. create temporary table near_zip(zip varchar(10), distance double);
  6409. insert into near_zip(zip, distance)
  6410. SELECT ra.zip AS zip, (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) AS distance
  6411. FROM zip_geo ra
  6412. WHERE (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) <= 30
  6413. ORDER BY 2
  6414. LIMIT 200;
  6415. SET l_last_row_fetched=0;
  6416. OPEN near_zip_cur;
  6417. zip_loop: LOOP
  6418.            FETCH near_zip_cur INTO one_zip, one_distance;
  6419.            IF l_last_row_fetched = 1 THEN
  6420.              LEAVE zip_loop;
  6421.            END IF;
  6422.            SET zips_seq = CONCAT(zips_seq, ',\'', one_zip, '\'');
  6423. END LOOP zip_loop;
  6424. SET l_last_row_fetched = 0;
  6425. CLOSE near_zip_cur;
  6426. SELECT store_id
  6427. FROM customer_entity
  6428. WHERE entity_id = custId
  6429. INTO storeId;
  6430. SET @str_sql=concat(
  6431. "SELECT cpe.entity_id as `id`
  6432.     ,cpev_name.value as `name`
  6433.     ,cpev_image.value as `image`
  6434.     ,cped_price.value as `price`
  6435.     ,pe.name as `sponsor_name`
  6436. FROM
  6437.     catalog_product_entity cpe
  6438. INNER JOIN  catalog_category_product ccp on cpe.entity_id = ccp.product_id
  6439. INNER JOIN partners_entity pe on cpe.partner_id = pe.id
  6440. INNER JOIN catalog_category_entity cce on ccp.category_id = cce.entity_id AND cce.entity_id=",categoryId,"
  6441. INNER JOIN catalog_product_entity_varchar cpev_name
  6442.     on cpe.entity_id = cpev_name.entity_id AND cpev_name.attribute_id = 60
  6443. INNER JOIN catalog_product_entity_int cpei ON cpe.entity_id = cpei.entity_id AND cpei.attribute_id = 442 AND cpei.store_id = 0 AND
  6444. (EXISTS (SELECT * FROM partners_local_rewards_stores AS plrs INNER JOIN partners_reward_address AS pra ON plrs.store_id = pra.id WHERE cpe.entity_id = plrs.product_id AND pra.post_code IN (", zips_seq, ")) or cpei.value = 1)
  6445. INNER JOIN cataloginventory_stock_item csi ON cpe.entity_id = csi.product_id AND csi.qty > 0
  6446. LEFT JOIN catalog_product_entity_varchar cpev_image
  6447.     on cpe.entity_id = cpev_image.entity_id AND cpev_image.attribute_id = 74
  6448. LEFT JOIN catalog_product_entity_decimal cped_price
  6449.     on cpe.entity_id = cped_price.entity_id AND cped_price.attribute_id = 64
  6450. WHERE cpe.is_uk = ", case storeId WHEN 2 THEN "1 " ELSE "0 " END,
  6451. "ORDER by cped_price.value ",case priceOrder WHEN 0 THEN "asc " ELSE  "desc " END,
  6452. "LIMIT ",skipNum,",",getNum);
  6453.     PREPARE stmt FROM @str_sql;
  6454.     EXECUTE stmt;
  6455. drop temporary table near_zip;
  6456. END */;;
  6457. DELIMITER ;
  6458. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6459. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6460. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6461. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6462. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARDS_BY_KEYWORD` */;
  6463. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6464. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6465. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6466. /*!50003 SET character_set_client  = utf8 */ ;
  6467. /*!50003 SET character_set_results = utf8 */ ;
  6468. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6469. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6470. /*!50003 SET sql_mode              = '' */ ;
  6471. DELIMITER ;;
  6472. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARDS_BY_KEYWORD`(custId int(11), keyword varchar(255), getNum int(11), skipNum int(11), priceOrder bool, ilt FLOAT, ilg FLOAT)
  6473. BEGIN
  6474. DECLARE zips_seq VARCHAR(8000) DEFAULT '\'X\'';
  6475. DECLARE one_zip VARCHAR(255);
  6476. DECLARE one_distance DOUBLE;
  6477. DECLARE l_last_row_fetched INT;
  6478. DECLARE storeId INT;
  6479. DECLARE near_zip_cur CURSOR FOR
  6480. SELECT zip, distance
  6481. FROM near_zip;
  6482. DECLARE CONTINUE HANDLER FOR NOT FOUND SET l_last_row_fetched=1;
  6483. drop temporary table if exists near_zip;
  6484. create temporary table near_zip(zip varchar(10), distance double);
  6485. insert into near_zip(zip, distance)
  6486. SELECT ra.zip AS zip, (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) AS distance
  6487. FROM zip_geo ra
  6488. WHERE (((acos(sin((ilt*pi()/180)) * sin((latitude*pi()/180)) + cos((ilt*pi()/180)) * cos((latitude*pi()/180)) * cos(((ilg-longitude)*pi()/180))))*180/pi())*60*1.1515) <= 30
  6489. ORDER BY 2
  6490. LIMIT 200;
  6491. SET l_last_row_fetched=0;
  6492. OPEN near_zip_cur;
  6493. zip_loop: LOOP
  6494.            FETCH near_zip_cur INTO one_zip, one_distance;
  6495.            IF l_last_row_fetched = 1 THEN
  6496.              LEAVE zip_loop;
  6497.            END IF;
  6498.            SET zips_seq = CONCAT(zips_seq, ',\'', one_zip, '\'');
  6499. END LOOP zip_loop;
  6500. SET l_last_row_fetched = 0;
  6501. CLOSE near_zip_cur;
  6502. SELECT store_id
  6503. FROM customer_entity
  6504. WHERE entity_id = custId
  6505. INTO storeId;
  6506. SET @str_sql=concat(
  6507. "SELECT cpe.entity_id as `id`
  6508.     ,cpev_name.value as `name`
  6509.     ,cpev_image.value as `image`
  6510.     ,cped_price.value as `price`
  6511.     ,pe.name as `sponsor_name`
  6512. FROM
  6513.     catalog_product_entity cpe
  6514. INNER JOIN partners_entity pe on cpe.partner_id = pe.id
  6515. INNER JOIN catalog_product_entity_varchar cpev_name
  6516.     on cpe.entity_id = cpev_name.entity_id AND cpev_name.attribute_id = 60 AND cpev_name.value like '%",keyword,"%'
  6517. INNER JOIN catalog_product_entity_int cpei ON cpe.entity_id = cpei.entity_id AND cpei.attribute_id = 442 AND cpei.store_id = 0 AND
  6518. (EXISTS (SELECT * FROM partners_local_rewards_stores AS plrs INNER JOIN partners_reward_address AS pra ON plrs.store_id = pra.id WHERE cpe.entity_id = plrs.product_id AND pra.post_code IN (", zips_seq, ")) or cpei.value = 1)
  6519. INNER JOIN cataloginventory_stock_item csi ON cpe.entity_id = csi.product_id AND csi.qty > 0
  6520. LEFT JOIN catalog_product_entity_varchar cpev_image
  6521.     on cpe.entity_id = cpev_image.entity_id AND cpev_image.attribute_id = 74
  6522. LEFT JOIN catalog_product_entity_decimal cped_price
  6523.     on cpe.entity_id = cped_price.entity_id AND cped_price.attribute_id = 64
  6524. WHERE cpe.is_uk = ", case storeId WHEN 2 THEN "1 " ELSE "0 " END,
  6525. "ORDER by cped_price.value ",case priceOrder WHEN 0 THEN "asc " ELSE  "desc " END,
  6526. "LIMIT ",skipNum,",",getNum);
  6527.     PREPARE stmt FROM @str_sql;
  6528.     EXECUTE stmt;
  6529. drop temporary table near_zip;
  6530. END */;;
  6531. DELIMITER ;
  6532. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6533. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6534. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6535. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6536. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARDS_CATEGORIES` */;
  6537. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6538. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6539. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6540. /*!50003 SET character_set_client  = utf8 */ ;
  6541. /*!50003 SET character_set_results = utf8 */ ;
  6542. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6543. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6544. /*!50003 SET sql_mode              = '' */ ;
  6545. DELIMITER ;;
  6546. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARDS_CATEGORIES`()
  6547. BEGIN
  6548. DECLARE storeId smallint(5) DEFAULT 0;
  6549. DECLARE category_name_attribute_id int DEFAULT 33;
  6550. DECLARE category_image_attribute_id int DEFAULT 37;
  6551. SELECT cce.entity_id AS 'category_id',
  6552.        ccev_name.value AS 'category_name',
  6553.        ccev_image.value AS 'category_image'
  6554. FROM catalog_category_entity cce
  6555.     INNER JOIN catalog_category_entity_varchar ccev_name
  6556.           ON cce.entity_id = ccev_name.entity_id
  6557.                 AND ccev_name.attribute_id = category_name_attribute_id
  6558.                 AND ccev_name.store_id = storeId
  6559.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_image
  6560.           ON cce.entity_id = ccev_image.entity_id
  6561.                 AND ccev_image.attribute_id = category_image_attribute_id
  6562.                 AND ccev_image.store_id = storeId
  6563. WHERE cce.parent_id = 11 and ccev_name.value NOT RLIKE '^_';
  6564. END */;;
  6565. DELIMITER ;
  6566. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6567. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6568. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6569. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6570. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARDS_CATEGORIES_TEST` */;
  6571. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6572. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6573. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6574. /*!50003 SET character_set_client  = utf8 */ ;
  6575. /*!50003 SET character_set_results = utf8 */ ;
  6576. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6577. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6578. /*!50003 SET sql_mode              = '' */ ;
  6579. DELIMITER ;;
  6580. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARDS_CATEGORIES_TEST`(custId int(11))
  6581. BEGIN
  6582.  
  6583. DECLARE defStoreId SMALLINT(6) DEFAULT 0;
  6584. DECLARE custStoreId SMALLINT(6) DEFAULT 0;
  6585.  
  6586. DECLARE category_name_attribute_id int DEFAULT 33;
  6587. DECLARE category_image_attribute_id int DEFAULT 37;
  6588.  
  6589.  
  6590. SELECT ce.store_id
  6591. INTO custStoreId
  6592. FROM customer_entity ce
  6593. WHERE entity_id = custId;
  6594.  
  6595.  
  6596. SELECT cce.entity_id AS 'category_id',
  6597.        COALESCE(ccev_name_cust_store.value, ccev_name_def_store.value) AS 'category_name',
  6598.        COALESCE(ccev_image_cust_store.value, ccev_image_def_store.value) AS 'category_image'
  6599. FROM catalog_category_entity cce
  6600.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_name_def_store
  6601.           ON cce.entity_id = ccev_name_def_store.entity_id
  6602.                 AND ccev_name_def_store.attribute_id = category_name_attribute_id
  6603.                 AND ccev_name_def_store.store_id = defStoreId
  6604.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_name_cust_store
  6605.           ON cce.entity_id = ccev_name_cust_store.entity_id
  6606.                 AND ccev_name_cust_store.attribute_id = category_name_attribute_id
  6607.                 AND ccev_name_def_store.store_id = custStoreId
  6608.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_image_def_store
  6609.           ON cce.entity_id = ccev_image_def_store.entity_id
  6610.                 AND ccev_image_def_store.attribute_id = category_image_attribute_id
  6611.                 AND ccev_image_def_store.store_id = defStoreId
  6612.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_image_cust_store
  6613.           ON cce.entity_id = ccev_image_cust_store.entity_id
  6614.                 AND ccev_image_cust_store.attribute_id = category_image_attribute_id
  6615.                 AND ccev_image_cust_store.store_id = custStoreId
  6616. WHERE cce.parent_id = 11
  6617.         AND COALESCE(ccev_name_cust_store.value, ccev_name_def_store.value) NOT RLIKE '^_'
  6618.         AND EXISTS (SELECT 1
  6619.                     FROM catalog_product_entity cpe
  6620.                         INNER JOIN catalog_category_product ccp on cpe.entity_id = ccp.product_id
  6621.                     WHERE ccp.category_id = cce.entity_id)
  6622. ;
  6623.  
  6624. END */;;
  6625. DELIMITER ;
  6626. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6627. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6628. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6629. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6630. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARDS_THEMES` */;
  6631. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6632. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6633. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6634. /*!50003 SET character_set_client  = utf8 */ ;
  6635. /*!50003 SET character_set_results = utf8 */ ;
  6636. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6637. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6638. /*!50003 SET sql_mode              = '' */ ;
  6639. DELIMITER ;;
  6640. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARDS_THEMES`()
  6641. BEGIN
  6642.  
  6643. DECLARE storeId smallint(5) DEFAULT 0;
  6644. DECLARE name_attribute_id int DEFAULT 33;
  6645. DECLARE image_attribute_id int DEFAULT 37;
  6646.  
  6647.  
  6648. SELECT cce_child.entity_id AS 'theme_id',
  6649.        ccev_child_name.value AS 'theme_name',
  6650.        ccev_child_image.value AS 'theme_image'
  6651. FROM catalog_category_entity cce_parent
  6652.     INNER JOIN catalog_category_entity_varchar ccev_parent_name
  6653.           ON cce_parent.entity_id = ccev_parent_name.entity_id
  6654.                 AND ccev_parent_name.attribute_id = name_attribute_id
  6655.                 AND ccev_parent_name.store_id = storeId
  6656.     INNER JOIN catalog_category_entity cce_child
  6657.           ON cce_parent.entity_id = cce_child.parent_id
  6658.     INNER JOIN catalog_category_entity_varchar ccev_child_name
  6659.           ON cce_child.entity_id = ccev_child_name.entity_id
  6660.                 AND ccev_child_name.attribute_id = name_attribute_id
  6661.                 AND ccev_child_name.store_id = storeId
  6662.     LEFT OUTER JOIN catalog_category_entity_varchar ccev_child_image
  6663.           ON cce_child.entity_id = ccev_child_image.entity_id
  6664.                 AND ccev_child_image.attribute_id = image_attribute_id
  6665.                 AND ccev_child_image.store_id = storeId
  6666. WHERE cce_parent.parent_id = 11 and ccev_parent_name.value = '_Themes';
  6667.  
  6668.  
  6669. END */;;
  6670. DELIMITER ;
  6671. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6672. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6673. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6674. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6675. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_REWARD_DETAILS_BY_ID` */;
  6676. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6677. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6678. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6679. /*!50003 SET character_set_client  = utf8 */ ;
  6680. /*!50003 SET character_set_results = utf8 */ ;
  6681. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6682. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6683. /*!50003 SET sql_mode              = '' */ ;
  6684. DELIMITER ;;
  6685. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_REWARD_DETAILS_BY_ID`(rewardId int(11))
  6686. BEGIN
  6687.    
  6688.     DECLARE today DATE DEFAULT DATE(NOW());
  6689.  
  6690.     SELECT cpe.entity_id AS `id`,
  6691.            CASE cpe.type_id
  6692.              WHEN 'reward_printathome' THEN 'Print At Home'
  6693.              WHEN 'reward_rewardmailer' THEN 'Reward Mailer'
  6694.              WHEN 'reward_online' THEN 'Online'
  6695.              WHEN 'reward_partnerfulfilled' THEN 'Donations'
  6696.              ELSE cpe.type_id
  6697.            END AS `type`,
  6698.            cpev_name.value AS `name`,
  6699.            cpev_image.value AS `image`,
  6700.            CASE
  6701.              WHEN
  6702.                 cped_sp.value IS NOT NULL
  6703.                     AND (cped_sp_sd.value IS NULL OR cped_sp_sd.value <= today)
  6704.                     AND (cped_sp_ed.value IS NULL OR cped_sp_ed.value >= today)
  6705.              THEN cped_sp.value
  6706.              ELSE cped_p.value
  6707.            END AS `price`,
  6708.            pe.name AS `sponsor_name`,
  6709.            cpev_shdesc.value AS `short_description`,
  6710.            cpev_desc.value AS `description`,
  6711.            cpev_term.value AS `terms`
  6712.     FROM
  6713.         catalog_product_entity cpe
  6714.     INNER JOIN partners_entity pe ON cpe.partner_id = pe.id
  6715.     INNER JOIN catalog_product_entity_varchar cpev_name
  6716.         ON cpe.entity_id = cpev_name.entity_id AND cpev_name.attribute_id = 60
  6717.     LEFT JOIN catalog_product_entity_varchar cpev_image
  6718.         ON cpe.entity_id = cpev_image.entity_id AND cpev_image.attribute_id = 74
  6719.     LEFT JOIN catalog_product_entity_text cpev_desc
  6720.         ON cpe.entity_id = cpev_desc.entity_id AND cpev_desc.attribute_id = 61
  6721.     LEFT JOIN catalog_product_entity_text cpev_shdesc
  6722.         ON cpe.entity_id = cpev_shdesc.entity_id AND cpev_shdesc.attribute_id = 62
  6723.     LEFT JOIN catalog_product_entity_text cpev_term
  6724.         ON cpe.entity_id = cpev_term.entity_id AND cpev_term.attribute_id = 498
  6725.     LEFT JOIN catalog_product_entity_decimal cped_p
  6726.         ON cpe.entity_id = cped_p.entity_id AND cped_p.attribute_id = 64
  6727.     LEFT JOIN catalog_product_entity_decimal cped_sp
  6728.         ON cpe.entity_id = cped_sp.entity_id AND cped_sp.attribute_id = 468
  6729.     LEFT JOIN catalog_product_entity_datetime cped_sp_sd
  6730.         ON cpe.entity_id = cped_sp_sd.entity_id AND cped_sp_sd.attribute_id = 466
  6731.     LEFT JOIN catalog_product_entity_datetime cped_sp_ed
  6732.         ON cpe.entity_id = cped_sp_ed.entity_id AND cped_sp_ed.attribute_id = 467
  6733.     WHERE cpe.entity_id = rewardId;
  6734.  
  6735.  
  6736. END */;;
  6737. DELIMITER ;
  6738. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6739. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6740. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6741. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6742. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_STATES` */;
  6743. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6744. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6745. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6746. /*!50003 SET character_set_client  = utf8 */ ;
  6747. /*!50003 SET character_set_results = utf8 */ ;
  6748. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6749. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6750. /*!50003 SET sql_mode              = '' */ ;
  6751. DELIMITER ;;
  6752. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_STATES`(custId int(11))
  6753. BEGIN
  6754.    
  6755.     DECLARE default_store_id INT(11) DEFAULT 0;
  6756.     DECLARE country_config_path VARCHAR(255) DEFAULT 'general/country/default';
  6757.     DECLARE country_stores_scope VARCHAR(255) DEFAULT 'stores';
  6758.     DECLARE country_default_scope VARCHAR(255) DEFAULT 'default';
  6759.    
  6760.     DECLARE cust_country_id VARCHAR(4) DEFAULT '';
  6761.     DECLARE cust_store_id INT(6);
  6762.     DECLARE country_scope VARCHAR(255) DEFAULT country_stores_scope;
  6763.    
  6764.     BEGIN
  6765.         DECLARE CONTINUE HANDLER FOR NOT FOUND SET cust_country_id = '';
  6766.        
  6767.         SELECT cae_country.value
  6768.         INTO cust_country_id
  6769.         FROM customer_entity ce
  6770.             INNER JOIN customer_address_entity cae
  6771.                 ON ce.entity_id = cae.parent_id
  6772.             INNER JOIN customer_address_entity_varchar cae_country
  6773.                 ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  6774.         WHERE ce.entity_id = custId;
  6775.     END;
  6776.    
  6777.    
  6778.     IF (cust_country_id IS NULL OR TRIM(cust_country_id) = '') THEN
  6779.        
  6780.        
  6781.         SELECT ce.store_id
  6782.         INTO cust_store_id
  6783.         FROM customer_entity ce
  6784.         WHERE ce.entity_id = custId;
  6785.    
  6786.        
  6787.         IF NOT EXISTS
  6788.             (
  6789.             SELECT 1
  6790.                 FROM core_config_data ccd
  6791.                 WHERE
  6792.                     ccd.path = country_config_path
  6793.                         AND ccd.scope = country_scope
  6794.                         AND ccd.scope_id = cust_store_id
  6795.             )
  6796.         THEN
  6797.             SET cust_store_id = default_store_id;
  6798.             SET country_scope = country_default_scope;
  6799.         END IF;
  6800.    
  6801.        
  6802.         SELECT ccd.value
  6803.         INTO cust_country_id
  6804.         FROM core_config_data ccd
  6805.         WHERE ccd.path = country_config_path AND ccd.scope_id = cust_store_id AND ccd.scope = country_scope;
  6806.        
  6807.     END IF;
  6808.    
  6809.     SELECT
  6810.         dcr.region_id AS 'state_id',
  6811.         dcr.default_name AS 'state_name'
  6812.     FROM directory_country_region dcr
  6813.     WHERE dcr.country_id = cust_country_id
  6814.     ORDER BY dcr.default_name ASC;
  6815.    
  6816. END */;;
  6817. DELIMITER ;
  6818. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6819. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6820. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6821. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6822. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_BY_FACEBOOK_ID` */;
  6823. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6824. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6825. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6826. /*!50003 SET character_set_client  = utf8 */ ;
  6827. /*!50003 SET character_set_results = utf8 */ ;
  6828. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6829. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6830. /*!50003 SET sql_mode              = '' */ ;
  6831. DELIMITER ;;
  6832. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_BY_FACEBOOK_ID`(fbId varchar(255))
  6833. BEGIN
  6834.  
  6835. SELECT  ce.entity_id AS 'id',
  6836.         ce.email AS 'username',
  6837.         ce.account_number AS 'account_number',
  6838.         NOT EXISTS (SELECT 1 FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 16) AS 'is_confirmed',
  6839.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 12) AS 'password_hash',
  6840.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 5) AS 'first_name',
  6841.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 7) AS 'last_name',
  6842.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 217) AS 'contact_id',
  6843.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 224) AS 'account_id',
  6844.         (SELECT cb.amount FROM enterprise_customerbalance cb WHERE cb.customer_id = ce.entity_id) AS 'points',
  6845.         cae_country.value AS 'country',
  6846.         ce.is_active AS 'account_status',
  6847.         19.47 AS 'saved_trees',
  6848.         24.93 AS 'saved_oil'
  6849. FROM customer_entity ce
  6850. INNER JOIN customer_entity_varchar cev_fb ON ce.entity_id = cev_fb.entity_id AND cev_fb.attribute_id = 303
  6851. LEFT JOIN customer_address_entity cae ON ce.entity_id = cae.parent_id
  6852. LEFT JOIN customer_address_entity_varchar cae_country ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  6853. WHERE cev_fb.value = fbId AND ce.is_active = 1;
  6854.  
  6855. END */;;
  6856. DELIMITER ;
  6857. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6858. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6859. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6860. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6861. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_BY_ID` */;
  6862. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6863. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6864. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6865. /*!50003 SET character_set_client  = utf8 */ ;
  6866. /*!50003 SET character_set_results = utf8 */ ;
  6867. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6868. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6869. /*!50003 SET sql_mode              = '' */ ;
  6870. DELIMITER ;;
  6871. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_BY_ID`(userId int(11))
  6872. BEGIN
  6873.  
  6874. SELECT  ce.entity_id AS 'id',
  6875.         ce.email AS 'username',
  6876.         ce.account_number AS 'account_number',
  6877.         NOT EXISTS (SELECT 1 FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 16) AS 'is_confirmed',
  6878.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 12) AS 'password_hash',
  6879.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 5) AS 'first_name',
  6880.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 7) AS 'last_name',
  6881.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 217) AS 'contact_id',
  6882.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 224) AS 'account_id',
  6883.         (SELECT cb.amount FROM enterprise_customerbalance cb WHERE cb.customer_id = ce.entity_id) AS 'points',
  6884.         cae_country.value AS 'country',
  6885.         ce.is_active AS 'account_status',
  6886.         19.47 AS 'saved_trees',
  6887.         24.93 AS 'saved_oil'
  6888. FROM customer_entity ce
  6889. LEFT JOIN customer_address_entity cae ON ce.entity_id = cae.parent_id
  6890. LEFT JOIN customer_address_entity_varchar cae_country ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  6891. WHERE ce.entity_id = userId AND ce.is_active = 1;
  6892.  
  6893. END */;;
  6894. DELIMITER ;
  6895. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6896. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6897. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6898. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6899. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_BY_LOGIN` */;
  6900. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6901. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6902. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6903. /*!50003 SET character_set_client  = utf8 */ ;
  6904. /*!50003 SET character_set_results = utf8 */ ;
  6905. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6906. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6907. /*!50003 SET sql_mode              = '' */ ;
  6908. DELIMITER ;;
  6909. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_BY_LOGIN`(login VARCHAR(255))
  6910. BEGIN
  6911.    
  6912. SELECT  ce.entity_id AS 'id',
  6913.         ce.email AS 'username',
  6914.         ce.account_number AS 'account_number',
  6915.         NOT EXISTS (SELECT 1 FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 16) AS 'is_confirmed',
  6916.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 12) AS 'password_hash',
  6917.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 5) AS 'first_name',
  6918.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 7) AS 'last_name',
  6919.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 217) AS 'contact_id',
  6920.         (SELECT cev.value FROM customer_entity_varchar cev WHERE cev.entity_id = ce.entity_id AND cev.attribute_id = 224) AS 'account_id',
  6921.         (SELECT cb.amount FROM enterprise_customerbalance cb WHERE cb.customer_id = ce.entity_id) AS 'points',
  6922.         cae_country.value AS 'country',
  6923.         ce.is_active AS 'account_status',
  6924.         19.47 AS 'saved_trees',
  6925.         24.93 AS 'saved_oil'
  6926. FROM customer_entity ce
  6927. LEFT JOIN customer_address_entity cae ON ce.entity_id = cae.parent_id
  6928. LEFT JOIN customer_address_entity_varchar cae_country ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  6929. WHERE ce.email = login AND ce.is_active = 1;
  6930.  
  6931. END */;;
  6932. DELIMITER ;
  6933. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6934. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6935. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6936. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6937. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_COORD` */;
  6938. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6939. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6940. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6941. /*!50003 SET character_set_client  = utf8 */ ;
  6942. /*!50003 SET character_set_results = utf8 */ ;
  6943. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6944. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6945. /*!50003 SET sql_mode              = '' */ ;
  6946. DELIMITER ;;
  6947. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_COORD`(izip VARCHAR(10), OUT lt FLOAT, OUT lg FLOAT)
  6948. BEGIN
  6949. DECLARE CONTINUE HANDLER FOR NOT FOUND
  6950. BEGIN
  6951.   SET lt = 0;
  6952.   SET lg = 0;
  6953. END;
  6954. SELECT zg.latitude, zg.longitude
  6955. FROM zip_geo zg
  6956. WHERE zg.zip = izip
  6957. INTO lt, lg;
  6958. END */;;
  6959. DELIMITER ;
  6960. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  6961. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  6962. /*!50003 SET character_set_results = @saved_cs_results */ ;
  6963. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  6964. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_DETAILS` */;
  6965. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  6966. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  6967. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  6968. /*!50003 SET character_set_client  = utf8 */ ;
  6969. /*!50003 SET character_set_results = utf8 */ ;
  6970. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  6971. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  6972. /*!50003 SET sql_mode              = '' */ ;
  6973. DELIMITER ;;
  6974. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_DETAILS`(userId int(11))
  6975. BEGIN
  6976.  
  6977. SELECT
  6978.    ce.entity_id AS 'id',
  6979.    ce.email AS 'username',
  6980.    ce.store_id AS 'store_id',
  6981.    cae_country.value AS 'country',
  6982.    cae_region.value AS 'state',
  6983.    cae_city.value AS 'city',
  6984.    cae_postcode.value AS 'postal_code',
  6985.    YEAR(CURDATE()) - YEAR(cp.customer_age_range) -
  6986.    CASE
  6987.      WHEN MONTH(CURDATE()) > MONTH(cp.customer_age_range) THEN 0
  6988.      WHEN MONTH(CURDATE()) < MONTH(cp.customer_age_range) THEN 1
  6989.      WHEN DAYOFMONTH(CURDATE()) >= DAYOFMONTH(cp.customer_age_range) THEN 0
  6990.      ELSE 1
  6991.    END AS 'age',
  6992.    CASE
  6993.      WHEN ca.customer_children > 0 THEN true
  6994.      WHEN ca.customer_children = 0 THEN false
  6995.      ELSE null
  6996.    END AS 'is_parent',
  6997.    CASE
  6998.      WHEN ca.customer_homeowner = 0 THEN false
  6999.      WHEN ca.customer_homeowner = 1 THEN true
  7000.      ELSE null
  7001.    END AS 'is_home_owner',
  7002.    CASE
  7003.      WHEN cp.customer_gender = 0 THEN 'female'
  7004.      WHEN cp.customer_gender = 1 THEN 'male'
  7005.      ELSE null
  7006.    END AS 'gender'
  7007. FROM customer_entity ce
  7008.   LEFT JOIN customer_address_entity cae
  7009.     ON ce.entity_id = cae.parent_id
  7010.   LEFT JOIN customer_address_entity_varchar cae_city
  7011.     ON cae.entity_id = cae_city.entity_id AND cae_city.attribute_id = 25
  7012.   LEFT JOIN customer_address_entity_varchar cae_region
  7013.     ON cae.entity_id = cae_region.entity_id AND cae_region.attribute_id = 27
  7014.   LEFT JOIN customer_address_entity_varchar cae_postcode
  7015.     ON cae.entity_id = cae_postcode.entity_id AND cae_postcode.attribute_id = 29
  7016.   LEFT JOIN customer_address_entity_varchar cae_country
  7017.     ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  7018.   LEFT JOIN customer_additional ca ON ce.entity_id = ca.customer_id
  7019.   LEFT JOIN customer_personal cp ON ce.entity_id = cp.customer_id
  7020. WHERE ce.entity_id = userId;
  7021.  
  7022. END */;;
  7023. DELIMITER ;
  7024. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7025. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7026. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7027. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7028. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_MAILING_ADDRESS` */;
  7029. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7030. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7031. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7032. /*!50003 SET character_set_client  = utf8 */ ;
  7033. /*!50003 SET character_set_results = utf8 */ ;
  7034. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7035. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7036. /*!50003 SET sql_mode              = '' */ ;
  7037. DELIMITER ;;
  7038. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_MAILING_ADDRESS`(accNumber varchar(255))
  7039. BEGIN
  7040.  
  7041.  
  7042. DECLARE delim CHAR(1) DEFAULT '\n';
  7043.  
  7044. SELECT
  7045.      SUBSTRING_INDEX(cae_street.value, delim, 1) AS 'street',
  7046.    SUBSTRING_INDEX(SUBSTRING(cae_street.value, LENGTH(SUBSTRING_INDEX(cae_street.value, delim, 1)) + LENGTH(delim) + 1), delim, 1) AS 'unit',
  7047.      cae_city.value AS 'city',
  7048.      cae_region.value AS 'state',
  7049.      cae_postcode.value AS 'postal_code',
  7050.      cae_country.value AS 'country'
  7051.     FROM customer_entity ce
  7052.         LEFT JOIN customer_address_entity cae
  7053.       ON ce.entity_id = cae.parent_id
  7054.     LEFT JOIN customer_address_entity_text cae_street
  7055.       ON cae.entity_id = cae_street.entity_id AND cae_street.attribute_id = 24
  7056.     LEFT JOIN customer_address_entity_varchar cae_city
  7057.       ON cae.entity_id = cae_city.entity_id AND cae_city.attribute_id = 25
  7058.     LEFT JOIN customer_address_entity_varchar cae_region
  7059.       ON cae.entity_id = cae_region.entity_id AND cae_region.attribute_id = 27
  7060.     LEFT JOIN customer_address_entity_varchar cae_postcode
  7061.       ON cae.entity_id = cae_postcode.entity_id AND cae_postcode.attribute_id = 29
  7062.     LEFT JOIN customer_address_entity_varchar cae_country
  7063.       ON cae.entity_id = cae_country.entity_id AND cae_country.attribute_id = 26
  7064.   WHERE ce.account_number = accNumber;
  7065.  
  7066. END */;;
  7067. DELIMITER ;
  7068. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7069. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7070. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7071. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7072. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_GET_USER_ZIP_CODE` */;
  7073. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7074. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7075. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7076. /*!50003 SET character_set_client  = utf8 */ ;
  7077. /*!50003 SET character_set_results = utf8 */ ;
  7078. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7079. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7080. /*!50003 SET sql_mode              = '' */ ;
  7081. DELIMITER ;;
  7082. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_GET_USER_ZIP_CODE`(custId INT(11), OUT ozip VARCHAR(255))
  7083. BEGIN
  7084. DECLARE CONTINUE HANDLER FOR NOT FOUND SET ozip = "";
  7085. SELECT cae_postcode.value
  7086. FROM customer_entity ce
  7087.   INNER JOIN customer_address_entity cae
  7088.     ON ce.entity_id = cae.parent_id
  7089.   INNER JOIN customer_address_entity_varchar cae_postcode
  7090.     ON cae.entity_id = cae_postcode.entity_id AND cae_postcode.attribute_id = 29
  7091. WHERE ce.entity_id = custId
  7092. INTO ozip;
  7093. END */;;
  7094. DELIMITER ;
  7095. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7096. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7097. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7098. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7099. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_SET_ZIP_COORD` */;
  7100. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7101. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7102. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7103. /*!50003 SET character_set_client  = utf8 */ ;
  7104. /*!50003 SET character_set_results = utf8 */ ;
  7105. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7106. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7107. /*!50003 SET sql_mode              = '' */ ;
  7108. DELIMITER ;;
  7109. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_SET_ZIP_COORD`(izip varchar(10), ilt FLOAT, ilg FLOAT)
  7110. BEGIN
  7111. INSERT INTO zip_geo (`zip`, `latitude`, `longitude`) VALUES (izip, ilt, ilg);
  7112. END */;;
  7113. DELIMITER ;
  7114. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7115. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7116. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7117. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7118. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_UPDATE_AD_SHOWN_TIME` */;
  7119. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7120. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7121. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7122. /*!50003 SET character_set_client  = utf8 */ ;
  7123. /*!50003 SET character_set_results = utf8 */ ;
  7124. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7125. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7126. /*!50003 SET sql_mode              = '' */ ;
  7127. DELIMITER ;;
  7128. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_UPDATE_AD_SHOWN_TIME`(in recordId int(11), in showTime int(11))
  7129. BEGIN
  7130.  
  7131.   UPDATE mobile_ad_histories
  7132.   SET show_time = showTime,
  7133.       updated_at = now()
  7134.   WHERE record_id = recordId;
  7135.  
  7136. END */;;
  7137. DELIMITER ;
  7138. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7139. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7140. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7141. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7142. /*!50003 DROP PROCEDURE IF EXISTS `MOBILE_UPDATE_USER_MAILING_ADDRESS` */;
  7143. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7144. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7145. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7146. /*!50003 SET character_set_client  = utf8 */ ;
  7147. /*!50003 SET character_set_results = utf8 */ ;
  7148. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7149. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7150. /*!50003 SET sql_mode              = '' */ ;
  7151. DELIMITER ;;
  7152. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MOBILE_UPDATE_USER_MAILING_ADDRESS`(
  7153.                               iacc_num varchar(255),
  7154.                               icountry varchar(255),
  7155.                               istate varchar(255),
  7156.                               icity varchar(255),
  7157.                               istreet varchar(255),
  7158.                               iunit varchar(255),
  7159.                               ipostal_code varchar(255),
  7160.                               out success boolean)
  7161. BEGIN
  7162.  
  7163.  
  7164. DECLARE delim CHAR(1) DEFAULT '\n';
  7165.  
  7166. DECLARE custId INT(11);
  7167. DECLARE addrId INT(11);
  7168.  
  7169. DECLARE CONTINUE HANDLER FOR NOT FOUND SET custId = -1;
  7170.  
  7171.  
  7172. SET success = 0;
  7173.  
  7174. SELECT ce.entity_id
  7175. FROM customer_entity ce
  7176. WHERE ce.account_number = iacc_num
  7177. INTO custId;
  7178.  
  7179. IF custId <> -1
  7180. THEN
  7181.  
  7182.   BEGIN
  7183.  
  7184.     DECLARE CONTINUE HANDLER FOR NOT FOUND SET addrId = -1;
  7185.  
  7186.     SELECT cae.entity_id
  7187.     FROM customer_entity ce
  7188.       INNER JOIN customer_address_entity cae ON ce.entity_id = cae.parent_id
  7189.     WHERE ce.entity_id = custId
  7190.     INTO addrId;
  7191.  
  7192.    
  7193.     IF addrId = -1
  7194.     THEN
  7195.      
  7196.      
  7197.       INSERT INTO customer_address_entity(entity_type_id, attribute_set_id, increment_id, parent_id, is_active)
  7198.              VALUES(2, 0, '', custId, 1);
  7199.  
  7200.      
  7201.       SELECT LAST_INSERT_ID() INTO addrId;
  7202.  
  7203.      
  7204.       INSERT INTO customer_entity_int(entity_type_id, attribute_id, entity_id, value)
  7205.       VALUES (1, 13, custId, addrId),(1, 14, custId, addrId);
  7206.  
  7207.     END IF;
  7208.  
  7209.   END;
  7210.  
  7211.  
  7212.  
  7213.   IF icountry IS NOT NULL
  7214.   THEN
  7215.  
  7216.     BEGIN
  7217.  
  7218.       DECLARE countryId INT(11);
  7219.       DECLARE countryAttrId SMALLINT(6) DEFAULT 26;
  7220.  
  7221.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET countryId = -1;
  7222.  
  7223.       SELECT value_id
  7224.       FROM customer_address_entity_varchar
  7225.       WHERE entity_id = addrId AND attribute_id = countryAttrId
  7226.       INTO countryId;
  7227.  
  7228.      
  7229.       IF countryId <> - 1
  7230.       THEN
  7231.        
  7232.         UPDATE customer_address_entity_varchar
  7233.         SET value = icountry
  7234.         WHERE value_id = countryId;
  7235.       ELSE
  7236.        
  7237.         INSERT INTO customer_address_entity_varchar(entity_type_id, attribute_id, entity_id, value)
  7238.                VALUES(2, countryAttrId, addrId, icountry);
  7239.       END IF;
  7240.  
  7241.     END;
  7242.  
  7243.   END IF;
  7244.  
  7245.  
  7246.  
  7247.   IF istate IS NOT NULL
  7248.   THEN
  7249.  
  7250.     BEGIN
  7251.  
  7252.       DECLARE stateId INT(11);
  7253.       DECLARE stateAttrId SMALLINT(6) DEFAULT 27;
  7254.  
  7255.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET stateId = -1;
  7256.  
  7257.       SELECT value_id
  7258.       FROM customer_address_entity_varchar
  7259.       WHERE entity_id = addrId AND attribute_id = stateAttrId
  7260.       INTO stateId;
  7261.  
  7262.      
  7263.       IF stateId <> - 1
  7264.       THEN
  7265.        
  7266.         UPDATE customer_address_entity_varchar
  7267.         SET value = istate
  7268.         WHERE value_id = stateId;
  7269.       ELSE
  7270.        
  7271.         INSERT INTO customer_address_entity_varchar(entity_type_id, attribute_id, entity_id, value)
  7272.                VALUES(2, stateAttrId, addrId, istate);
  7273.       END IF;
  7274.  
  7275.     END;
  7276.  
  7277.   END IF;
  7278.  
  7279.  
  7280.  
  7281.   IF icity IS NOT NULL
  7282.   THEN
  7283.  
  7284.     BEGIN
  7285.  
  7286.       DECLARE cityId INT(11);
  7287.       DECLARE cityAttrId SMALLINT(6) DEFAULT 25;
  7288.  
  7289.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET cityId = -1;
  7290.  
  7291.       SELECT value_id
  7292.       FROM customer_address_entity_varchar
  7293.       WHERE entity_id = addrId AND attribute_id = cityAttrId
  7294.       INTO cityId;
  7295.  
  7296.      
  7297.       IF cityId <> - 1
  7298.       THEN
  7299.        
  7300.         UPDATE customer_address_entity_varchar
  7301.         SET value = icity
  7302.         WHERE value_id = cityId;
  7303.       ELSE
  7304.        
  7305.         INSERT INTO customer_address_entity_varchar(entity_type_id, attribute_id, entity_id, value)
  7306.                VALUES(2, cityAttrId, addrId, icity);
  7307.       END IF;
  7308.  
  7309.     END;
  7310.  
  7311.   END IF;
  7312.  
  7313.  
  7314.  
  7315.   IF istreet IS NOT NULL OR iunit IS NOT NULL
  7316.   THEN
  7317.  
  7318.     BEGIN
  7319.  
  7320.       DECLARE streetId INT(11);
  7321.       DECLARE streetAttrId SMALLINT(6) DEFAULT 24;
  7322.       DECLARE streetUnit VARCHAR(255) DEFAULT '';
  7323.  
  7324.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET streetId = -1;
  7325.  
  7326.       SELECT value_id
  7327.       FROM customer_address_entity_text
  7328.       WHERE entity_id = addrId AND attribute_id = streetAttrId
  7329.       INTO streetId;
  7330.  
  7331.      
  7332.       IF streetId <> - 1
  7333.       THEN
  7334.        
  7335.        
  7336.         IF istreet IS NOT NULL AND iunit IS NOT NULL
  7337.         THEN
  7338.            
  7339.        
  7340.             UPDATE customer_address_entity_text
  7341.             SET value = CONCAT(istreet, delim, iunit, SUBSTRING(value, LENGTH(SUBSTRING_INDEX(value, delim, 2)) + 1))
  7342.             WHERE value_id = streetId;
  7343.        
  7344.          ELSE
  7345.             IF istreet IS NOT NULL
  7346.             THEN
  7347.                
  7348.            
  7349.                 UPDATE customer_address_entity_text
  7350.                 SET value = CONCAT(istreet, SUBSTRING(value, LENGTH(SUBSTRING_INDEX(value, delim, 1)) + 1))
  7351.                 WHERE value_id = streetId;
  7352.  
  7353.           ELSE
  7354.                    IF iunit IS NOT NULL
  7355.                    THEN
  7356.                        
  7357.                    
  7358.                         UPDATE customer_address_entity_text
  7359.                      SET value = CONCAT(SUBSTRING_INDEX(value, delim, 1), delim, iunit, SUBSTRING(value, LENGTH(SUBSTRING_INDEX(value, delim, 2)) + 1))
  7360.                      WHERE value_id = streetId;
  7361.                
  7362.             END IF;
  7363.                
  7364.             END IF;
  7365.            
  7366.         END IF;
  7367.        
  7368.       ELSE
  7369.        
  7370.        
  7371.         IF istreet IS NOT NULL
  7372.         THEN
  7373.             SET streetUnit = CONCAT(streetUnit, istreet);
  7374.         END IF;
  7375.        
  7376.         SET streetUnit = CONCAT(streetUnit, delim);
  7377.        
  7378.         IF iunit IS NOT NULL
  7379.         THEN
  7380.             SET streetUnit = CONCAT(streetUnit, iunit);
  7381.         END IF;
  7382.      
  7383.        
  7384.         INSERT INTO customer_address_entity_text(entity_type_id, attribute_id, entity_id, value)
  7385.                VALUES(2, streetAttrId, addrId, streetUnit);
  7386.                
  7387.       END IF;
  7388.  
  7389.     END;
  7390.  
  7391.   END IF;
  7392.  
  7393.  
  7394.  
  7395.   IF ipostal_code IS NOT NULL
  7396.   THEN
  7397.  
  7398.     BEGIN
  7399.  
  7400.       DECLARE postalCodeId INT(11);
  7401.       DECLARE postalCodeAttrId SMALLINT(6) DEFAULT 29;
  7402.  
  7403.       DECLARE CONTINUE HANDLER FOR NOT FOUND SET postalCodeId = -1;
  7404.  
  7405.       SELECT value_id
  7406.       FROM customer_address_entity_varchar
  7407.       WHERE entity_id = addrId AND attribute_id = postalCodeAttrId
  7408.       INTO postalCodeId;
  7409.  
  7410.      
  7411.       IF postalCodeId <> - 1
  7412.       THEN
  7413.        
  7414.         UPDATE customer_address_entity_varchar
  7415.         SET value = ipostal_code
  7416.         WHERE value_id = postalCodeId;
  7417.       ELSE
  7418.        
  7419.         INSERT INTO customer_address_entity_varchar(entity_type_id, attribute_id, entity_id, value)
  7420.                VALUES(2, postalCodeAttrId, addrId, ipostal_code);
  7421.       END IF;
  7422.  
  7423.     END;
  7424.  
  7425.   END IF;
  7426.  
  7427.   SET success = 1;
  7428.  
  7429.  
  7430. END IF;
  7431.  
  7432. END */;;
  7433. DELIMITER ;
  7434. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7435. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7436. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7437. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7438. /*!50003 DROP PROCEDURE IF EXISTS `MS_CREATE_CUST_ERROR` */;
  7439. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7440. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7441. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7442. /*!50003 SET character_set_client  = utf8 */ ;
  7443. /*!50003 SET character_set_results = utf8 */ ;
  7444. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7445. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7446. /*!50003 SET sql_mode              = '' */ ;
  7447. DELIMITER ;;
  7448. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MS_CREATE_CUST_ERROR`(i_cust_id INT(11), i_error_id VARCHAR(255), i_message VARCHAR(255), i_description VARCHAR(255))
  7449. BEGIN
  7450.  
  7451. INSERT INTO ms_errors(cust_id, error_id, message, description)
  7452.        VALUES (i_cust_id, i_error_id, i_message, i_description);
  7453.  
  7454. END */;;
  7455. DELIMITER ;
  7456. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7457. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7458. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7459. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7460. /*!50003 DROP PROCEDURE IF EXISTS `MS_GET_REMINDER_USERS` */;
  7461. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7462. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7463. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7464. /*!50003 SET character_set_client  = utf8 */ ;
  7465. /*!50003 SET character_set_results = utf8 */ ;
  7466. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7467. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7468. /*!50003 SET sql_mode              = '' */ ;
  7469. DELIMITER ;;
  7470. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MS_GET_REMINDER_USERS`(IN `custId` int(11))
  7471. BEGIN
  7472. SELECT now() INTO @curr_dt;
  7473. SELECT case WHEN DAYOFWEEK(@curr_dt) = 1 THEN 7 ELSE DAYOFWEEK(@curr_dt)-1 END INTO @dofw;
  7474. SELECT
  7475. case
  7476.     WHEN mod((WEEK(@curr_dt, 5) -
  7477.         WEEK(DATE_SUB(@curr_dt, INTERVAL DAYOFMONTH(@curr_dt) - 1 DAY), 5) + 1) ,2 ) = 0
  7478.     THEN 1
  7479.     ELSE 0
  7480. END
  7481. INTO @d_even_odd;
  7482. SELECT  `sr`.`frequency_type` AS `frequency_type`,
  7483.         `sr`.`day_of_week` AS `day_of_week`,
  7484.         `sr`.`day_of_month` AS `day_of_month`,
  7485.         `ce`.`entity_id` AS `id`,
  7486.         `ce`.`email`,
  7487.         `ce`.`store_id`,
  7488.         `fname_table`.`value` AS `first_name`,
  7489.         `lname_table`.`value` AS `last_name`
  7490. FROM `customer_self_reporting` AS `sr`
  7491.     LEFT JOIN `customer_entity` ce ON `sr`.customer_id = ce.entity_id
  7492.     LEFT JOIN `customer_entity_varchar` AS `fname_table` ON fname_table.entity_id = sr.customer_id AND fname_table.attribute_id = 5
  7493.     LEFT JOIN `customer_entity_varchar` AS `lname_table` ON lname_table.entity_id = sr.customer_id AND lname_table.attribute_id = 7
  7494.     LEFT JOIN customer_entity_varchar cev_confirm ON cev_confirm.entity_id = ce.entity_id AND cev_confirm.attribute_id = 16
  7495. WHERE
  7496.     coalesce(sr.start_date,date('0000-00-00'))<> date('0000-00-00') AND sr.start_date<=now()
  7497.     AND
  7498.     CASE
  7499.         WHEN custId IS NOT null
  7500.             THEN ce.entity_id > custId
  7501.         ELSE 1=1
  7502.     END
  7503.     AND cev_confirm.value_id IS NULL
  7504.    
  7505.     AND (sr.is_active = 1
  7506. AND
  7507. (
  7508. (sr.frequency_type = 1 AND sr.day_of_week = @dofw)
  7509.     OR (sr.frequency_type = 2 AND  
  7510.         case
  7511.         WHEN
  7512.             MOD( DATEDIFF(@curr_dt ,sr.start_date ) ,14) =0  
  7513.             THEN 1
  7514.             ELSE 0
  7515.         END  = 1
  7516.          
  7517.     )
  7518.     OR (sr.frequency_type = 3
  7519.         AND
  7520.         (sr.day_of_month = DAYOFMONTH(@curr_dt)
  7521.             OR case
  7522.                 WHEN DAYOFMONTH(LAST_DAY(@curr_dt)) > 27 AND DAYOFMONTH(LAST_DAY(@curr_dt)) < 31
  7523.                 THEN sr.day_of_month > date(@curr_dt)
  7524.                 ELSE 1=0 END
  7525.         )
  7526.          )
  7527. ))
  7528. ORDER BY ce.entity_id ASC;
  7529. END */;;
  7530. DELIMITER ;
  7531. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7532. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7533. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7534. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7535. /*!50003 DROP PROCEDURE IF EXISTS `MT_CREATE_CUST_ERROR` */;
  7536. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7537. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7538. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7539. /*!50003 SET character_set_client  = utf8 */ ;
  7540. /*!50003 SET character_set_results = utf8 */ ;
  7541. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7542. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7543. /*!50003 SET sql_mode              = '' */ ;
  7544. DELIMITER ;;
  7545. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MT_CREATE_CUST_ERROR`(i_cust_id INT(11), i_error_id VARCHAR(255), i_message VARCHAR(255), i_description VARCHAR(255))
  7546. BEGIN
  7547.  
  7548. INSERT INTO mt_errors(cust_id, error_id, message, description)
  7549.        VALUES (i_cust_id, i_error_id, i_message, i_description);
  7550.  
  7551. END */;;
  7552. DELIMITER ;
  7553. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7554. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7555. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7556. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7557. /*!50003 DROP PROCEDURE IF EXISTS `MT_GET_LAST_WEEK_TRANSACTIONS` */;
  7558. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7559. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7560. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7561. /*!50003 SET character_set_client  = utf8 */ ;
  7562. /*!50003 SET character_set_results = utf8 */ ;
  7563. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7564. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7565. /*!50003 SET sql_mode              = '' */ ;
  7566. DELIMITER ;;
  7567. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `MT_GET_LAST_WEEK_TRANSACTIONS`(IN `custId` INT(11))
  7568. BEGIN
  7569.    
  7570.     DECLARE start_date_criteria DATETIME DEFAULT DATE_ADD(DATE_FORMAT(NOW(), '%Y-%m-%d'), INTERVAL -7 DAY);
  7571.     DECLARE end_date_criteria DATETIME DEFAULT DATE_FORMAT(NOW(), '%Y-%m-%d');
  7572.    
  7573.     DROP TABLE IF EXISTS ttemp_all_transactions;
  7574.  
  7575.     CREATE TEMPORARY TABLE ttemp_all_transactions
  7576.     SELECT
  7577.         cbh.history_id,
  7578.         cbh.balance_id,
  7579.         cbh.additional_info,
  7580.         REPLACE(cbh.additional_info, 'Order #', '') AS increment_id,
  7581.         cbh.reason_id,
  7582.         cbh.description,
  7583.         cbh.balance_delta,
  7584.         cbh.balance_amount,
  7585.         cbh.updated_at
  7586.     FROM enterprise_customerbalance_history cbh
  7587.     WHERE cbh.updated_at BETWEEN start_date_criteria AND end_date_criteria;
  7588.    
  7589.     DROP TABLE IF EXISTS ttemp_search_transactions;
  7590.    
  7591.     CREATE TEMPORARY TABLE ttemp_search_transactions AS
  7592.     SELECT * FROM ttemp_all_transactions tst
  7593.     WHERE  
  7594.         CASE WHEN reason_id = 12
  7595.             THEN
  7596.                 NOT EXISTS (
  7597.                     SELECT 1
  7598.                     FROM sales_flat_order sfo
  7599.                     WHERE sfo.increment_id = tst.increment_id
  7600.                         AND EXISTS (
  7601.                             SELECT 1
  7602.                             FROM sales_flat_quote sfq
  7603.                             WHERE sfq.entity_id = sfo.quote_id AND sfq.is_reordered = 1)  
  7604.                 )      
  7605.             ELSE 1= 1
  7606.         END
  7607.     ;
  7608.    
  7609.     ALTER TABLE ttemp_search_transactions ADD INDEX ix_ttttt_tr_balance(balance_id);
  7610.    
  7611.     SELECT ce.entity_id AS 'cust_id',
  7612.            ce.store_id AS 'store_id',
  7613.            ce.email AS 'email',
  7614.            cb.amount AS 'balance',
  7615.            cbh.history_id AS 'trans_id',
  7616.            cbh.balance_delta AS 'points',
  7617.            CASE
  7618.              WHEN cbh.description IS NOT NULL
  7619.                THEN cbh.description
  7620.              WHEN cbh.additional_info IS NOT NULL
  7621.                THEN CONCAT('Reward ', cbh.additional_info)
  7622.              ELSE
  7623.                 cbr.reason_code
  7624.            END AS 'reason',
  7625.            cbh.updated_at AS 'date'
  7626.     FROM ttemp_search_transactions  cbh
  7627.         INNER JOIN enterprise_customerbalance cb ON cb.balance_id = cbh.balance_id
  7628.         INNER JOIN customer_entity ce ON ce.entity_id = cb.customer_id
  7629.         LEFT OUTER JOIN enterprise_customerbalance_reasons cbr ON cbr.id = cbh.reason_id
  7630.         LEFT OUTER JOIN customer_entity_varchar cev_confirm ON cev_confirm.entity_id = ce.entity_id AND cev_confirm.attribute_id = 16
  7631.     WHERE
  7632.         CASE
  7633.             WHEN custId IS NOT NULL
  7634.                 THEN ce.entity_id > custId
  7635.             ELSE 1=1
  7636.         END
  7637.         AND cev_confirm.value_id IS NULL
  7638.         AND TRIM(ce.email) <> ''
  7639.        
  7640.        
  7641.         ORDER BY ce.entity_id, cbh.updated_at DESC;
  7642.    
  7643.     DROP TABLE IF EXISTS ttemp_search_transactions;
  7644.     DROP TABLE IF EXISTS ttemp_all_transactions;
  7645.  
  7646. END */;;
  7647. DELIMITER ;
  7648. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7649. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7650. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7651. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7652. /*!50003 DROP PROCEDURE IF EXISTS `rsp_delete_transactions` */;
  7653. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7654. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7655. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7656. /*!50003 SET character_set_client  = utf8 */ ;
  7657. /*!50003 SET character_set_results = utf8 */ ;
  7658. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7659. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7660. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  7661. DELIMITER ;;
  7662. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `rsp_delete_transactions`(
  7663.  IN ihistory_id int(10)
  7664. )
  7665. BEGIN
  7666.     SELECT ecb.balance_id,ecb.balance_delta
  7667.     INTO @sbalance_id,@sbalance_delta
  7668.         FROM enterprise_customerbalance_history ecb
  7669.         WHERE ecb.history_id = ihistory_id;
  7670.         START TRANSACTION;
  7671.         BEGIN      
  7672.             declare exit handler for sqlexception rollback;
  7673.             UPDATE enterprise_customerbalance ec
  7674.                 SET ec.amount = ec.amount - @sbalance_delta
  7675.             WHERE ec.balance_id = @sbalance_id;
  7676.             DELETE FROM enterprise_customerbalance_history
  7677.                 WHERE history_id = ihistory_id;
  7678.             UPDATE enterprise_customerbalance_history ecb
  7679.                 SET ecb.balance_amount = ecb.balance_amount - @sbalance_delta
  7680.             WHERE ecb.balance_id = @sbalance_id AND ecb.history_id > ihistory_id;          
  7681.         END;   
  7682.         COMMIT;
  7683. END */;;
  7684. DELIMITER ;
  7685. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7686. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7687. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7688. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7689. /*!50003 DROP PROCEDURE IF EXISTS `rsp_del_transactions_cursor` */;
  7690. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7691. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7692. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7693. /*!50003 SET character_set_client  = utf8 */ ;
  7694. /*!50003 SET character_set_results = utf8 */ ;
  7695. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7696. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7697. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  7698. DELIMITER ;;
  7699. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `rsp_del_transactions_cursor`()
  7700. BEGIN
  7701. declare done smallint;
  7702. declare chistory_id int(10);
  7703. DECLARE cursTR CURSOR FOR
  7704.     SELECT t.history_id FROM enterprise_customerbalance_history t
  7705. WHERE  history_id > 29695827
  7706. AND (t.reason_id BETWEEN 7 AND 11 OR t.reason_id BETWEEN 25 AND 39)
  7707. AND t.additional_info = 'ongoing migration'
  7708. AND t.balance_id = 714220
  7709. ORDER BY t.balance_id;
  7710. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  7711. OPEN cursTR;
  7712. read_loop: LOOP
  7713.     FETCH cursTR INTO chistory_id;
  7714.     IF  done THEN
  7715.      LEAVE read_loop;
  7716.     END IF;
  7717.     call rsp_delete_transactions(chistory_id);
  7718.        
  7719. END LOOP;
  7720. CLOSE cursTR;
  7721. END */;;
  7722. DELIMITER ;
  7723. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7724. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7725. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7726. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7727. /*!50003 DROP PROCEDURE IF EXISTS `rsp_earn_partner_points_allocation` */;
  7728. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7729. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7730. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7731. /*!50003 SET character_set_client  = utf8 */ ;
  7732. /*!50003 SET character_set_results = utf8 */ ;
  7733. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7734. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7735. /*!50003 SET sql_mode              = '' */ ;
  7736. DELIMITER ;;
  7737. /*!50003 CREATE*/ /*!50020 DEFINER=`rb`@`%`*/ /*!50003 PROCEDURE `rsp_earn_partner_points_allocation`(IN inp_earn_partner TEXT, IN inp_start_date DATE, IN inp_end_date DATE)
  7738. BEGIN
  7739.     DROP TABLE IF EXISTS temp_earn_partner;
  7740.     CREATE TEMPORARY TABLE temp_earn_partner(partner_id INT(10));
  7741.    
  7742.     CALL rsp_split_string(inp_earn_partner, ',');
  7743.     INSERT INTO temp_earn_partner
  7744.     SELECT * FROM temp_values;
  7745.     DROP TABLE temp_values;
  7746.    
  7747.    
  7748.    
  7749.     SELECT
  7750.         pe.NAME AS `earn_partner`
  7751.         ,pett.type_name AS `transaction_type`
  7752.         ,eph.transaction_description AS  `earn_opportunity`
  7753.         ,ce.account_number     
  7754.         ,ce.email
  7755.         ,cae_city.`value` AS `city`
  7756.         ,cae_region.`value` AS `state`
  7757.         ,pet.points AS `rb_points_allocated`
  7758.         ,DATE(pet.`time`) AS `transaction_date`
  7759.         ,TIME(pet.`time`) AS `transaction_time`
  7760.         ,DATE(inp_start_date)  AS inp_start_date
  7761.         ,DATE(inp_end_date) AS inp_end_date
  7762.    
  7763.         FROM partners_earn_transactions pet
  7764.             INNER JOIN partners_earn_trans_type pett
  7765.                 ON pett.`type` = pet.`type`
  7766.             INNER JOIN enterprise_customerbalance_history ecbh
  7767.                 ON ecbh.history_id = pet.customer_balance_history_id   
  7768.             INNER JOIN partners_entity  pe  
  7769.                 ON pe.id = pet.partner_id
  7770.                 AND pe.id IN (SELECT partner_id FROM temp_earn_partner )
  7771.             INNER JOIN partners_earn per
  7772.                 ON per.partner_id = pe.id
  7773.             INNER JOIN customer_entity ce
  7774.                 ON ce.entity_id = pet.customer_id
  7775.             INNER JOIN customer_address_entity cae
  7776.                 ON cae.parent_id =ce.entity_id
  7777.             LEFT JOIN customer_address_entity_varchar cae_city
  7778.                 ON cae_city.entity_id = cae.entity_id
  7779.                     AND cae_city.attribute_id = 25
  7780.             LEFT JOIN customer_address_entity_varchar cae_region
  7781.                 ON cae_region.entity_id = cae.entity_id
  7782.                     AND cae_region.attribute_id = 28   
  7783.             LEFT JOIN earn_plugin_hashes eph
  7784.                 ON  eph.transaction_description = pet.description
  7785.     WHERE
  7786.         DATE(pet.`time`) BETWEEN inp_start_date AND inp_end_date
  7787.         /*AND EXISTS ( SELECT 1 FROM enterprise_customerbalance_history ecbh
  7788.                     WHERE ecbh.history_id = pet.customer_balance_history_id )
  7789.     */
  7790.    
  7791.     GROUP BY pet.id
  7792.     ;
  7793.        
  7794.     DROP TABLE IF EXISTS temp_earn_partner;
  7795.    
  7796. END */;;
  7797. DELIMITER ;
  7798. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7799. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7800. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7801. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7802. /*!50003 DROP PROCEDURE IF EXISTS `SBB_GET_EMAIL_BY_NUMBER` */;
  7803. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7804. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7805. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7806. /*!50003 SET character_set_client  = utf8 */ ;
  7807. /*!50003 SET character_set_results = utf8 */ ;
  7808. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7809. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7810. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  7811. DELIMITER ;;
  7812. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `SBB_GET_EMAIL_BY_NUMBER`(accNumber varchar(255), out iemail varchar(255))
  7813. BEGIN
  7814.  
  7815. DECLARE CONTINUE HANDLER FOR NOT FOUND SET iemail = '';
  7816.  
  7817. SELECT email
  7818. FROM customer_entity
  7819. WHERE account_number = accNumber
  7820. INTO iemail;
  7821.  
  7822.  
  7823. END */;;
  7824. DELIMITER ;
  7825. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7826. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7827. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7828. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7829. /*!50003 DROP PROCEDURE IF EXISTS `SBB_LOOKUP_GNOMY_MEMBER` */;
  7830. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7831. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7832. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7833. /*!50003 SET character_set_client  = utf8 */ ;
  7834. /*!50003 SET character_set_results = utf8 */ ;
  7835. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7836. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7837. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  7838. DELIMITER ;;
  7839. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `SBB_LOOKUP_GNOMY_MEMBER`(IN iaccount_number varchar(255)
  7840. ,IN ibaccount_number varchar(255)
  7841. ,IN iemail varchar(255)
  7842. ,IN iaddress1 varchar(255)
  7843. ,IN iaddress2 varchar(255)
  7844. ,IN icity varchar(255)
  7845. ,IN istate_province varchar(255)
  7846. ,IN ilocality varchar(255)
  7847. )
  7848. BEGIN
  7849. DECLARE rrowcount int(10);
  7850. declare oaccount_number varchar(255);
  7851. declare opartner_member_account_number varchar(255);
  7852.                      
  7853. SET rrowcount =0;
  7854. IF isnull(iaccount_number)=0 THEN
  7855. SELECT SQL_CALC_FOUND_ROWS
  7856.     ce.account_number
  7857.     ,case WHEN cpe_name.value  = 'Earn GConomy Card' THEN ecp.partner_member_account_number END
  7858.       INTO oaccount_number,opartner_member_account_number
  7859. FROM
  7860. customer_entity ce
  7861. LEFT JOIN earns_customers ec
  7862.             on ce.entity_id = ec.customer_id
  7863.         LEFT JOIN catalog_product_entity cpe on cpe.entity_id = ec.product_id
  7864.         LEFT JOIN catalog_product_entity_varchar cpe_name
  7865.             on cpe.entity_id = cpe_name.entity_id AND cpe_name.attribute_id =60            
  7866.         LEFT JOIN earns_customers_partners ecp
  7867.             on ec.id = ecp.earns_customers_id AND ecp.partner_id = cpe.partner_id      
  7868.       WHERE ce.account_number = iaccount_number
  7869. GROUP BY ce.account_number ;
  7870. SET rrowcount = FOUND_ROWS();
  7871. END IF;
  7872. IF rrowcount > 0 THEN
  7873.                                                                        
  7874.     SELECT oaccount_number,opartner_member_account_number;
  7875. ELSE
  7876.                                                                  
  7877.    
  7878.     IF isnull(ibaccount_number)=0 THEN
  7879.     SELECT SQL_CALC_FOUND_ROWS
  7880.         ce.account_number
  7881.         ,case WHEN cpe_name.value  = 'Earn GConomy Card' THEN ecp.partner_member_account_number END
  7882.     INTO oaccount_number,opartner_member_account_number
  7883.     FROM
  7884.         customer_entity ce
  7885.             LEFT JOIN earns_customers ec
  7886.                 on ce.entity_id = ec.customer_id
  7887.             LEFT JOIN catalog_product_entity cpe on cpe.entity_id = ec.product_id
  7888.             LEFT JOIN catalog_product_entity_varchar cpe_name
  7889.                 on cpe.entity_id = cpe_name.entity_id AND cpe_name.attribute_id =60            
  7890.             LEFT JOIN earns_customers_partners ecp
  7891.                 on ec.id = ecp.earns_customers_id AND ecp.partner_id = cpe.partner_id      
  7892.     WHERE ecp.partner_member_account_number = ibaccount_number
  7893.         GROUP BY ce.account_number
  7894.     ;
  7895.     SET rrowcount = FOUND_ROWS();
  7896.     END IF;
  7897.     IF rrowcount > 0 THEN
  7898.                                                                                            
  7899.         SELECT oaccount_number,opartner_member_account_number;
  7900.     ELSE
  7901.         IF isnull(iemail)=0 THEN
  7902.         SELECT SQL_CALC_FOUND_ROWS distinct
  7903.             ce.account_number
  7904.             ,case WHEN cpe_name.value  = 'Earn GConomy Card' THEN ecp.partner_member_account_number END
  7905.             INTO oaccount_number,opartner_member_account_number
  7906.         FROM
  7907.             customer_entity ce
  7908.                 LEFT JOIN earns_customers ec
  7909.                     on ce.entity_id = ec.customer_id
  7910.                 LEFT JOIN catalog_product_entity cpe on cpe.entity_id = ec.product_id
  7911.                 LEFT JOIN catalog_product_entity_varchar cpe_name
  7912.                     on cpe.entity_id = cpe_name.entity_id AND cpe_name.attribute_id =60            
  7913.                 LEFT JOIN earns_customers_partners ecp
  7914.                     on ec.id = ecp.earns_customers_id AND ecp.partner_id = cpe.partner_id      
  7915.             WHERE ce.email = iemail
  7916.         GROUP BY  ce.account_number;
  7917.         SET rrowcount = FOUND_ROWS();
  7918.         END IF;
  7919.         IF rrowcount > 0 THEN
  7920.                                                                
  7921.             SELECT oaccount_number,opartner_member_account_number;
  7922.         ELSE
  7923.                    IF  iaddress1 is not null or iaddress2 is not null or icity is not null or istate_province is not null or ilocality is not null THEN
  7924.            
  7925.             SELECT
  7926.                 ce.account_number as oaccount_number
  7927.                 ,case WHEN cpe_name.value  = 'Earn GConomy Card' THEN ecp.partner_member_account_number END
  7928.                 as opartner_member_account_number              
  7929.             FROM
  7930.             customer_entity ce
  7931.                 LEFT JOIN earns_customers ec
  7932.                     on ce.entity_id = ec.customer_id
  7933.                 LEFT JOIN catalog_product_entity cpe on cpe.entity_id = ec.product_id
  7934.                 LEFT JOIN catalog_product_entity_varchar cpe_name
  7935.                     on cpe.entity_id = cpe_name.entity_id AND cpe_name.attribute_id =60            
  7936.                 LEFT JOIN earns_customers_partners ecp
  7937.                     on ec.id = ecp.earns_customers_id AND ecp.partner_id = cpe.partner_id      
  7938.                 INNER JOIN customer_address_entity cae on ce.entity_id = cae.parent_id     
  7939.                
  7940.                 INNER JOIN magento_dev.customer_address_entity_text cae_street
  7941.                     on cae.entity_id = cae_street.entity_id and cae_street.attribute_id = 24
  7942.                 INNER JOIN customer_address_entity_varchar cae_city
  7943.                     on cae.entity_id = cae_city.entity_id AND cae_city.attribute_id = 25
  7944.                 INNER JOIN customer_address_entity_varchar cae_region
  7945.                     on cae.entity_id = cae_region.entity_id AND cae_region.attribute_id = 27
  7946.                 INNER JOIN customer_address_entity_varchar cae_postcode
  7947.                     on cae.entity_id = cae_postcode.entity_id AND cae_postcode.attribute_id = 29
  7948.             WHERE
  7949.             (  case WHEN  iaddress1 is not null and  iaddress2 is not null
  7950.                 THEN cae_street.value like concat('%',iaddress1,'%') and cae_street.value like concat('%',iaddress2,'%')
  7951.                 WHEN iaddress1 is not null and  iaddress2 is null
  7952.                 THEN cae_street.value like concat('%',iaddress1,'%')
  7953.                 WHEN iaddress1 is null and  iaddress2 is not null
  7954.                 THEN cae_street.value like concat('%',iaddress2,'%') ELSE 1=1 END
  7955.            
  7956.                         )
  7957.                 and case WHEN icity is not null THEN cae_city.value = icity ELSE 1=1 END
  7958.                 and case WHEN istate_province is not null THEN cae_region.value = istate_province ELSE 1=1 END
  7959.                 and case WHEN ilocality is not null THEN cae_postcode.value = ilocality  ELSE 1=1 END
  7960.             GROUP BY ce.account_number;
  7961.             ELSE
  7962.                 SELECT oaccount_number,opartner_member_account_number from dual where 1=0;
  7963.             END IF;
  7964.         END IF;
  7965.        
  7966.     END IF;
  7967.    
  7968. END IF;
  7969. END */;;
  7970. DELIMITER ;
  7971. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  7972. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  7973. /*!50003 SET character_set_results = @saved_cs_results */ ;
  7974. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  7975. /*!50003 DROP PROCEDURE IF EXISTS `SBB_LOOKUP_MERCHANT` */;
  7976. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  7977. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  7978. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  7979. /*!50003 SET character_set_client  = utf8 */ ;
  7980. /*!50003 SET character_set_results = utf8 */ ;
  7981. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  7982. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  7983. /*!50003 SET sql_mode              = '' */ ;
  7984. DELIMITER ;;
  7985. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `SBB_LOOKUP_MERCHANT`(
  7986. IN igpartner_id int(11),
  7987. IN istore_account_number VARCHAR(255),
  7988. IN iname VARCHAR(255),
  7989. IN isic INT(11)
  7990. )
  7991. BEGIN
  7992. DECLARE rrowcount int(10);
  7993. SET rrowcount =0;
  7994. IF isnull(istore_account_number)=0  THEN
  7995.     SELECT SQL_CALC_FOUND_ROWS
  7996.         pramd_bonus.bonus_factor INTO @o_bonus_factor
  7997.     FROM partners_reward_address pra       
  7998.        
  7999.         INNER JOIN partners_reward_address_merchant_data pramd
  8000.             on pramd.partner_reward_address_id = pra.id and pramd.merchant_id is not null
  8001.        
  8002.         INNER JOIN partners_reward_address_merchant_data pramd_bonus
  8003.             on pramd_bonus.partner_reward_address_id = pra.id and pramd_bonus.bonus_factor is not null
  8004.     WHERE pra.status =1
  8005.         AND pramd.merchant_id = istore_account_number
  8006.         AND pramd_bonus.bonus_factor <>0
  8007.     limit 1
  8008.     ;
  8009.     SET rrowcount = FOUND_ROWS();
  8010. END IF;
  8011. IF rrowcount =0 THEN
  8012.     IF (isnull(iname)=0 AND ISNULL(isic)=0 ) THEN
  8013.         SELECT SQL_CALC_FOUND_ROWS                     
  8014.             prmd_bonus.bonus_factor INTO @o_bonus_factor
  8015.         FROM partners_reward_merchant_data prmd_name
  8016.         INNER JOIN partners_reward_merchant_data prmd_category
  8017.             on prmd_category.partner_id = prmd_name.partner_id AND prmd_category.merchant_category_id is not null
  8018.         INNER JOIN partners_reward_merchant_data prmd_bonus
  8019.             on prmd_bonus.partner_id = prmd_name.partner_id AND prmd_bonus.bonus_factor is not null
  8020.         WHERE   prmd_name.merchant_dba is not null
  8021.             AND
  8022.             prmd_category.merchant_category_id = isic
  8023.             AND locate(iname,prmd_name.merchant_dba)>0
  8024.             AND prmd_bonus.bonus_factor <> 0
  8025.                 ORDER BY locate(iname,prmd_name.merchant_dba) asc
  8026.         limit 1
  8027.         ;      
  8028.         SET rrowcount = FOUND_ROWS();          
  8029.     END IF;
  8030. END IF;
  8031. IF rrowcount <>0 THEN
  8032.     SELECT @o_bonus_factor as bonus_factor;
  8033. ELSE
  8034.     SELECT '' as bonus_factor FROM dual WHERE 1=0;
  8035. END IF;
  8036.    
  8037. END */;;
  8038. DELIMITER ;
  8039. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8040. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8041. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8042. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8043. /*!50003 DROP PROCEDURE IF EXISTS `SBB_SUBSCRIBE` */;
  8044. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  8045. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  8046. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  8047. /*!50003 SET character_set_client  = utf8 */ ;
  8048. /*!50003 SET character_set_results = utf8 */ ;
  8049. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  8050. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  8051. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  8052. DELIMITER ;;
  8053. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `SBB_SUBSCRIBE`(IN ipartner_id int(10),
  8054.                                             IN iaccount_number varchar(255),
  8055.                                             IN iemail varchar(255),
  8056.                                             IN ipartner_member_account_number varchar(255),
  8057.                                             IN irb_member_pwd varchar(255),
  8058.                                             OUT subs_id int(11),
  8059.                         OUT reason varchar(255))
  8060. BEGIN
  8061. declare ipartner_name varchar(255);
  8062. declare icustomer_id int(10);
  8063. declare spartner_id int(10);
  8064. declare sproduct_id int(10);
  8065. declare searn_id int(10);
  8066. SET searn_id =0;
  8067.                                                              
  8068. IF iaccount_number is not NULL THEN
  8069.     SELECT entity_id INTO icustomer_id
  8070.         FROM customer_entity ce WHERE account_number = iaccount_number;
  8071. ELSE
  8072.     IF iemail is not NULL THEN
  8073.         SELECT entity_id INTO icustomer_id FROM customer_entity ce WHERE email = iemail;
  8074.     ELSE
  8075.         SET searn_id =0;
  8076.         SELECT 'please put account_number or email'  INTO reason;
  8077.         SELECT searn_id INTO subs_id;
  8078.     END IF;
  8079. END IF;
  8080. IF isnull(icustomer_id) THEN
  8081.     SELECT searn_id INTO subs_id;
  8082.     SET searn_id =0;
  8083.                            
  8084.         SELECT 'customer does not exists'  INTO reason;
  8085.     SELECT searn_id INTO subs_id;
  8086. ELSE   
  8087.     SELECT pe.id INTO spartner_id FROM partners_entity pe
  8088.         INNER JOIN partners_earn per on pe.id = per.partner_id WHERE  per.g_partner_id = ipartner_id ;
  8089.     IF isnull(spartner_id) THEN
  8090.         SET searn_id =0;
  8091.                                                
  8092.         SELECT 'partner does not exists'  INTO reason;
  8093.         SELECT searn_id INTO subs_id;
  8094.     ELSE
  8095.                                                                                                                                
  8096.         SELECT case spartner_id
  8097.             WHEN 2910 THEN 2954
  8098.             WHEN 2911 THEN 2955 END INTO sproduct_id ;
  8099.         IF isnull(sproduct_id) THEN
  8100.             SET searn_id =0;
  8101.                                                      
  8102.             SELECT 'product for g_partner does not exists'  INTO reason;
  8103.             SELECT searn_id INTO subs_id;
  8104.         ELSE
  8105.             IF not EXISTS (SELECT 1 FROM earns_customers ec
  8106.                         INNER JOIN earns_customers_partners ecp ON ec.id = ecp.earns_customers_id
  8107.                         WHERE ec.customer_id = icustomer_id AND ecp.partner_id = spartner_id                           
  8108.                             AND case when ipartner_member_account_number is not null THEN
  8109.                                                               ecp.partner_member_account_number = ipartner_member_account_number ELSE 1=1 END
  8110.                     AND ec.end_date is null)
  8111.             THEN
  8112.                 START TRANSACTION;
  8113.                 BEGIN      
  8114.                 declare exit handler for sqlexception rollback;
  8115.                                        
  8116.                      INSERT INTO earns_customers (customer_id,product_id,start_date)
  8117.                      VALUES (icustomer_id,sproduct_id,now());
  8118.                      SET searn_id = LAST_INSERT_ID();              
  8119.                      INSERT INTO earns_customers_partners
  8120.                         (earns_customers_id,partner_id,partner_member_account_number)
  8121.                      VALUES (searn_id,spartner_id,ipartner_member_account_number);
  8122.                      SET searn_id = LAST_INSERT_ID();
  8123.                 END;   
  8124.                     COMMIT;
  8125.                 SELECT searn_id INTO subs_id;
  8126.             ELSE
  8127.                                              
  8128.                 SET searn_id =0;
  8129.                 SELECT 'customer alredy subscribed'  INTO reason;
  8130.                                 SELECT searn_id INTO subs_id;
  8131.             END IF;
  8132.            
  8133.         END IF;
  8134.        
  8135.     END IF;        
  8136. END IF;
  8137. END */;;
  8138. DELIMITER ;
  8139. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8140. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8141. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8142. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8143. /*!50003 DROP PROCEDURE IF EXISTS `SBB_UNSUBSCRIBE` */;
  8144. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  8145. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  8146. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  8147. /*!50003 SET character_set_client  = utf8 */ ;
  8148. /*!50003 SET character_set_results = utf8 */ ;
  8149. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  8150. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  8151. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  8152. DELIMITER ;;
  8153. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `SBB_UNSUBSCRIBE`( IN ipartner_id int(10),
  8154.                                             IN iaccount_number varchar(255),
  8155.                                             IN ipartner_member_account_number varchar(255),
  8156.                                             OUT success tinyint(1),
  8157.                                             OUT reason varchar(255))
  8158. BEGIN
  8159. declare ipartner_name varchar(255);
  8160. declare icustomer_id int(10);
  8161. declare spartner_id int(10);
  8162.                                                              
  8163. IF iaccount_number is not NULL THEN
  8164.     SELECT entity_id INTO icustomer_id
  8165.         FROM customer_entity ce WHERE account_number = iaccount_number;
  8166. ELSE   
  8167.     SELECT 0,'please put account_number'  INTO success,reason; 
  8168. END IF;
  8169. IF isnull(icustomer_id) THEN
  8170.                            
  8171.         SELECT 0,'customer does not exists'  INTO success,reason;  
  8172. ELSE   
  8173.     SELECT pe.id INTO spartner_id FROM partners_entity pe
  8174.         INNER JOIN partners_earn per on pe.id = per.partner_id WHERE  per.g_partner_id = ipartner_id ;
  8175.     IF isnull(spartner_id) THEN        
  8176.                                                
  8177.         SELECT 0,'partner does not exists'  INTO success,reason;       
  8178.     ELSE
  8179.         IF EXISTS (SELECT 1 FROM earns_customers ec
  8180.                 INNER JOIN earns_customers_partners ecp
  8181.                     ON ec.id = ecp.earns_customers_id
  8182.                 WHERE ec.customer_id = icustomer_id AND ecp.partner_id = spartner_id
  8183.                     AND case when ipartner_member_account_number is not null THEN
  8184.                                         ecp.partner_member_account_number = ipartner_member_account_number ELSE 1=1 END
  8185.                     AND ec.end_date is null)
  8186.         THEN
  8187.                         UPDATE
  8188.                 earns_customers ec
  8189.             INNER JOIN earns_customers_partners ecp ON ec.id = ecp.earns_customers_id
  8190.                 SET ec.end_date = now()
  8191.             WHERE ec.customer_id = icustomer_id AND ecp.partner_id = spartner_id                           
  8192.                     AND case when ipartner_member_account_number is not null THEN
  8193.                                         ecp.partner_member_account_number = ipartner_member_account_number ELSE 1=1 END
  8194.                     AND ec.end_date is null;
  8195.             SELECT ROW_COUNT() into @upd_rows;
  8196.             IF @upd_rows>0 THEN
  8197.                 SELECT 1,'customer unsubscribed' INTO success,reason;
  8198.             ELSE
  8199.                 SELECT 0,'unsucsess' INTO success,reason;
  8200.             END IF;
  8201.         ELSE
  8202.                                          
  8203.            
  8204.             SELECT 0,'can not find customer for unsubscribe'  INTO success,reason;
  8205.                        
  8206.         END IF;
  8207.                            
  8208.     END IF;        
  8209. END IF;
  8210.    
  8211. END */;;
  8212. DELIMITER ;
  8213. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8214. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8215. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8216. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8217. /*!50003 DROP PROCEDURE IF EXISTS `scramble_fields` */;
  8218. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  8219. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  8220. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  8221. /*!50003 SET character_set_client  = utf8 */ ;
  8222. /*!50003 SET character_set_results = utf8 */ ;
  8223. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  8224. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  8225. /*!50003 SET sql_mode              = '' */ ;
  8226. DELIMITER ;;
  8227. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `scramble_fields`()
  8228. BEGIN
  8229.     DECLARE letterBig nvarchar(100);
  8230.     DECLARE letterSmall nvarchar(100);
  8231. drop table if exists temp_email_not_modified;
  8232. create temporary table temp_email_not_modified(email nvarchar(255));
  8233. insert into temp_email_not_modified(email)
  8234. values(
  8235. 'rb2.moc@gmail.com'),
  8236. ('rb4.moc@gmail.com'),
  8237. ('rbtest59@val.co.ua'),
  8238. ('rbtest60@val.co.ua'),
  8239. ('testovy.email@gmail.com'),
  8240. ('v.yarmolatiy@gmail.com'),
  8241. ('rbtest28@val.co.ua'),
  8242. ('rbtest37@val.co.ua'),
  8243. ('rbtest46@val.co.ua'),
  8244. ('irynchatko@ukr.net'),
  8245. ('irynchatko@gmail.com'),
  8246. ('irynchatko123@gmail.com'),
  8247. ('irynchatko1234@gmail.com'),
  8248. ('somyk83@mail.ru'),
  8249. ('mrs.hrystyna1@gmail.com'),
  8250. ('hrystyna@gmail.com'),
  8251. ('mrs.hrystyna@gmail.com'),
  8252. ('heleneperekopskaya-uk@gmail.com'),
  8253. ('heleneperekopskaya-us@gmail.com'),
  8254. ('heleneperekopskaya+us@gmail.com'),
  8255. ('heleneperekopskaya+uk@gmail.com'),
  8256. ('petvova@yahoo.com'),
  8257. ('petvova1@yahoo.com'),
  8258. ('petrushkevych@yahoo.com');
  8259.    
  8260.         UPDATE contacts c
  8261.             INNER JOIN `contactings` l ON c.id = l.contact_id
  8262.             INNER JOIN `accounts` a ON l.contactable_id = a.id
  8263.         SET c.first_name = CONCAT('fname',a.account_number)
  8264.             WHERE l.contactable_type = 'Account' and l.contact_type_id = 1
  8265.                 AND c.first_name IS NOT NULL AND c.first_name != ''
  8266.              AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8267.            
  8268.         UPDATE contacts c
  8269.             INNER JOIN `contactings` l ON c.id = l.contact_id
  8270.             INNER JOIN `accounts` a ON l.contactable_id = a.id
  8271.         SET c.last_name = CONCAT('lname',a.account_number)
  8272.             WHERE l.contactable_type = 'Account' and l.contact_type_id = 1
  8273.                 AND c.last_name IS NOT NULL AND c.last_name != ''
  8274.          AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8275.        
  8276.         UPDATE customer_entity_varchar ce_fname
  8277.             INNER JOIN customer_entity ce
  8278.                 ON ce_fname.entity_id = ce.entity_id
  8279.         SET ce_fname.value = CONCAT('fname',ce.account_number)
  8280.         WHERE ce_fname.attribute_id = 5 AND ce_fname.value<>'' AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com'  ;
  8281.         UPDATE customer_entity_varchar ce_lname
  8282.             INNER JOIN customer_entity ce
  8283.                 ON ce_lname.entity_id = ce.entity_id
  8284.         SET ce_lname.value = CONCAT('lname',ce.account_number)
  8285.         WHERE ce_lname.attribute_id = 7 AND ce_lname.value<>'' AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com';
  8286.        
  8287.         SET letterBig = 'QWERTYUIOPLKJHGFDSAZXCVBNMKLPOIJNBHUYGVCFTRDXZSEWQASDEWQZXCVBNMKLOPIUYTREWQASDFGHJKLMNBVCXZASDFGHJKL';
  8288.         SET letterSmall = 'qwertyuioplkjhgfdsazxcvbnmlpokijuhygtfrdeswqazxcvbnmklpoijbhuygvcftrdxzsewqasdrftgcvbnjuikolpmjhytrf';
  8289.         UPDATE addresses a
  8290.             INNER JOIN addressings adr ON adr.address_id = a.id and adr.addressable_type = 'Contact' and adr.is_mailing_addr = 1
  8291.             INNER JOIN contacts c ON c.id = adr.addressable_id
  8292.             INNER JOIN contactings co ON co.contact_id = c.id
  8293.             INNER JOIN accounts aa ON co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8294.         SET  a.street_name = CONCAT(LEFT(SUBSTRING(letterBig, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8295.         LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8296.         LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8297.         SUBSTRING(street_name, 4, length( street_name))
  8298.         )
  8299.         where  a.street_name is not null and  a.street_name <> ''  
  8300.         and length(a.street_name) > 3
  8301.         AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8302.        
  8303.        
  8304.        
  8305.         UPDATE addresses a
  8306.             INNER JOIN addressings adr ON adr.address_id = a.id and adr.addressable_type = 'Contact' and adr.is_mailing_addr = 1
  8307.             INNER JOIN contacts c ON c.id = adr.addressable_id
  8308.             INNER JOIN contactings co ON co.contact_id = c.id
  8309.             INNER JOIN accounts aa ON co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8310.         SET a.street_num = left(TRUNCATE((rand()* 1000000), 0), length(a.street_num))
  8311.         where a.street_num is not null and a.street_num <> ''
  8312.             AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8313.        
  8314.         UPDATE addresses a
  8315.             INNER JOIN addressings adr ON adr.address_id = a.id and adr.addressable_type = 'Contact' and adr.is_mailing_addr = 1
  8316.             INNER JOIN contacts c ON c.id = adr.addressable_id
  8317.             INNER JOIN contactings co ON co.contact_id = c.id
  8318.             INNER JOIN accounts aa ON co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8319.         SET a.address1 = CONCAT(ifnull(a.street_num,''), ' ', ifnull(a.street_name,''))
  8320.         where ((a.street_num is not null) or (a.street_name is not null) )
  8321.             AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8322.         UPDATE addresses a
  8323.             INNER JOIN addressings adr ON adr.address_id = a.id and adr.addressable_type = 'Contact' and adr.is_mailing_addr = 1
  8324.             INNER JOIN contacts c ON c.id = adr.addressable_id
  8325.             INNER JOIN contactings co ON co.contact_id = c.id
  8326.             INNER JOIN accounts aa ON co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8327.         SET a.address1 = CONCAT(
  8328.         TRUNCATE((rand()* 1000), 0),
  8329.         ' ',
  8330.         LEFT(SUBSTRING(letterBig, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8331.         LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8332.         LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8333.         SUBSTRING(a.address1, 4, length(a.address1))
  8334.         )
  8335.         where a.street_num is null and a.street_name is null
  8336.             and a.address1 is not null and a.address1 <> ''
  8337.         AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com' ;
  8338.        
  8339.         UPDATE customer_address_entity_text cae_street
  8340.             INNER JOIN customer_address_entity cae ON cae_street.entity_id = cae.entity_id
  8341.             INNER JOIN customer_entity ce ON cae.parent_id = ce.entity_id
  8342.             INNER JOIN accounts aa ON aa.account_number = ce.account_number
  8343.             INNER JOIN contactings co ON  co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8344.             INNER JOIN contacts c ON  c.id = co.contact_id
  8345.             INNER JOIN addressings adr ON  adr.addressable_id = c.id AND adr.addressable_type = 'Contact' AND adr.is_mailing_addr = 1
  8346.             INNER JOIN addresses a ON a.id = adr.address_id
  8347.         SET
  8348.             cae_street.value = CONCAT(REPLACE(REPLACE(coalesce(a.address1,''), CHAR(10), ' '), CHAR(13), ' '), CHAR(13), CHAR(10), REPLACE(REPLACE(a.address2, CHAR(10), ' '), CHAR(13), ' '))                 
  8349.         WHERE cae_street.attribute_id = 24 AND cae_street.value <>''
  8350.             AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com';
  8351.         UPDATE customer_address_entity_text cae_street
  8352.             INNER JOIN customer_address_entity cae ON cae_street.entity_id = cae.entity_id
  8353.             INNER JOIN customer_entity ce ON cae.parent_id = ce.entity_id
  8354.             LEFT JOIN accounts aa ON aa.account_number = ce.account_number
  8355.             LEFT JOIN contactings co ON  co.contactable_id = aa.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8356.             LEFT JOIN contacts c ON  c.id = co.contact_id
  8357.             LEFT JOIN addressings adr ON  adr.addressable_id = c.id AND adr.addressable_type = 'Contact' AND adr.is_mailing_addr = 1
  8358.             LEFT JOIN addresses a ON a.id = adr.address_id
  8359.         SET
  8360.             cae_street.value = CONCAT(
  8361.             TRUNCATE((rand()* 1000), 0),
  8362.             ' ',
  8363.             LEFT(SUBSTRING(letterBig, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8364.             LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8365.             LEFT(SUBSTRING(letterSmall, TRUNCATE((rand()* 100), 0)+1, 1) , 10),
  8366.             SUBSTRING(cae_street.value, 4, length(cae_street.value))
  8367.             )
  8368.         WHERE   cae_street.attribute_id = 24 AND cae_street.value <>''
  8369.             AND a.id is null
  8370.             AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com';
  8371.  
  8372.        
  8373.         update contacts c
  8374.         set c.home_phone =
  8375.         CONCAT(
  8376.         left(TRUNCATE((rand()* 100000), 0), 3),
  8377.         '-',
  8378.         left(TRUNCATE((rand()* 100000), 0), 3),
  8379.         '-',
  8380.         left(TRUNCATE((rand()* 100000), 0), 4)
  8381.         )
  8382.         where c.home_phone is not null and c.home_phone <> ''
  8383.             AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com';
  8384.        
  8385.        
  8386.         UPDATE customer_mobile ce_m
  8387.             INNER JOIN customer_entity ce ON ce.entity_id =  ce_m.customer_id
  8388.             INNER JOIN accounts a ON a.account_number = ce.account_number
  8389.             INNER JOIN contactings co ON  co.contactable_id = a.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8390.             INNER JOIN contacts c ON  c.id = co.contact_id
  8391.         SET
  8392.             ce_m.phone_number = case WHEN c.home_phone is not null THEN  replace(c.home_phone,'-','') ELSE TRUNCATE( ( rand()* 10000000000), 0) END
  8393.         WHERE
  8394.             ce_m.phone_number <> '' AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com';
  8395.         UPDATE customer_mobile ce_m
  8396.             INNER JOIN customer_entity ce ON ce.entity_id =  ce_m.customer_id
  8397.             LEFT JOIN accounts a ON a.account_number = ce.account_number
  8398.             LEFT JOIN contactings co ON  co.contactable_id = a.id AND co.contactable_type = 'Account' AND co.contact_type_id = 1
  8399.             LEFT JOIN contacts c ON  c.id = co.contact_id
  8400.         SET
  8401.             ce_m.phone_number = TRUNCATE( ( rand()* 10000000000), 0)
  8402.         WHERE
  8403.               c.id is null AND ce_m.phone_number <> '' AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com';
  8404.        
  8405.        
  8406.         update contacts c
  8407.         set c.work_phone =
  8408.         CONCAT(
  8409.         left(TRUNCATE((rand()* 100000), 0), 3),
  8410.         '-',
  8411.         left(TRUNCATE((rand()* 100000), 0), 3),
  8412.         '-',
  8413.         left(TRUNCATE((rand()* 100000), 0), 4)
  8414.         )
  8415.         where c.work_phone is not null and c.work_phone <> ''
  8416.         AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com';
  8417.        
  8418.         update contacts c
  8419.         set c.cell_phone =
  8420.         CONCAT(
  8421.         left(TRUNCATE((rand()* 100000), 0), 3),
  8422.         '-',
  8423.         left(TRUNCATE((rand()* 100000), 0), 3),
  8424.         '-',
  8425.         left(TRUNCATE((rand()* 100000), 0), 4)
  8426.         )
  8427.         where c.cell_phone is not null and c.cell_phone <> ''
  8428.         AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com';
  8429.         UPDATE contacts c
  8430.             INNER JOIN `contactings` l ON c.id = l.contact_id
  8431.             INNER JOIN `accounts` a ON l.contactable_id = a.id
  8432.         SET c.email = CONCAT('qa',a.account_number,'@recyclebank.com')
  8433.         WHERE l.contactable_type = 'Account' and l.contact_type_id = 1
  8434.             AND c.email IS NOT NULL AND c.email != '' AND c.email not in (SELECT email from temp_email_not_modified) AND c.email not like '%@recyclebank.com';
  8435.         UPDATE customer_entity ce
  8436.             SET ce.email = CONCAT('qa',ce.account_number,'@recyclebank.com')
  8437.         WHERE ce.email is not null and ce.email !=''
  8438.             AND ce.email not in (SELECT email from temp_email_not_modified) AND ce.email not like '%@recyclebank.com' ;
  8439. drop table temp_email_not_modified;
  8440. END */;;
  8441. DELIMITER ;
  8442. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8443. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8444. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8445. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8446. /*!50003 DROP PROCEDURE IF EXISTS `sel_all_attribute` */;
  8447. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  8448. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  8449. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  8450. /*!50003 SET character_set_client  = utf8 */ ;
  8451. /*!50003 SET character_set_results = utf8 */ ;
  8452. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  8453. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  8454. /*!50003 SET sql_mode              = '' */ ;
  8455. DELIMITER ;;
  8456. /*!50003 CREATE*/ /*!50020 DEFINER=`root`@`%`*/ /*!50003 PROCEDURE `sel_all_attribute`(IN inp_entity_id int(10))
  8457. BEGIN
  8458.  
  8459.  DECLARE str_all_fields VARCHAR(100000) DEFAULT '';
  8460.  DECLARE str_ins_fields VARCHAR(10000) DEFAULT '';
  8461.  DECLARE fld_name VARCHAR(10000) DEFAULT '';
  8462.  DECLARE ins_fld_name VARCHAR(255) DEFAULT '';
  8463. DECLARE done INT DEFAULT 0;
  8464. DECLARE cur1
  8465. CURSOR FOR select
  8466.     concat("max(case When a.attribute_code='", a.attribute_code,"' THEN case when a.backend_type='varchar' THEN cev.value WHEN a.backend_type='datetime' THEN ced.value WHEN a.backend_type='decimal' THEN cedc.value WHEN a.backend_type = 'int' THEN cei.value WHEN a.backend_type = 'text' THEN cet.value END END ) as ",a.attribute_code) as  a1,
  8467.     a.attribute_code as a2
  8468.     FROM `eav_attribute` a
  8469.     INNER JOIN information_schema.COLUMNS c on c.TABLE_SCHEMA =DATABASE()
  8470.         AND c.TABLE_NAME ='catalog_product_flat_1' AND c.column_name = a.attribute_code
  8471. WHERE a.entity_type_id = 4
  8472. order BY a.attribute_id;
  8473. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = 1;
  8474. OPEN cur1;
  8475. read_loop: LOOP
  8476.     FETCH cur1 INTO fld_name,ins_fld_name;    
  8477.     IF  done THEN
  8478.      LEAVE read_loop;
  8479.     END IF;
  8480.     SET str_all_fields = CONCAT(str_all_fields,fld_name,",");  
  8481.     SET str_ins_fields = CONCAT(str_ins_fields,ins_fld_name,",");  
  8482. END LOOP;
  8483. CLOSE cur1;
  8484. SET str_all_fields = substring(str_all_fields,1,length(str_all_fields)-1);
  8485. SET str_ins_fields = substring(str_ins_fields,1,length(str_ins_fields)-1);
  8486. SET @str_sql = concat(" FROM `eav_attribute` a  \n                  
  8487.                   INNER JOIN `catalog_product_entity` ce on a.entity_type_id = ce.entity_type_id  \n
  8488.                   LEFT JOIN `catalog_product_entity_varchar` cev  \n
  8489.                      on a.attribute_id = cev.attribute_id and ce.entity_id = cev.entity_id  \n
  8490.                   LEFT JOIN `catalog_product_entity_datetime` ced \n
  8491.                      on a.attribute_id = ced.attribute_id and ce.entity_id = ced.entity_id  \n
  8492.                   LEFT JOIN `catalog_product_entity_decimal` cedc \n  
  8493.                      on a.attribute_id = cedc.attribute_id and ce.entity_id = cedc.entity_id  \n
  8494.                   LEFT JOIN `catalog_product_entity_int` cei \n  
  8495.                      on a.attribute_id = cei.attribute_id and ce.entity_id = cei.entity_id  \n
  8496.                   LEFT JOIN `catalog_product_entity_text` cet \n  
  8497.                      on a.attribute_id = cet.attribute_id and ce.entity_id = cet.entity_id  \n
  8498.                WHERE  ce.entity_id = ",inp_entity_id," AND  a.entity_type_id = 4 GROUP BY ce.entity_id order BY a.attribute_id ");
  8499. SET @str_sql =  concat("INSERT INTO catalog_product_flat_2(entity_id,",str_ins_fields,") SELECT ce.entity_id,",str_all_fields,@str_sql ) ;
  8500. SELECT @str_sql;
  8501.  
  8502.  
  8503.  
  8504. end */;;
  8505. DELIMITER ;
  8506. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8507. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8508. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8509. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8510. /*!50003 DROP PROCEDURE IF EXISTS `SOLR_INDEX_CUSTOMERS` */;
  8511. /*!50003 SET @saved_cs_client      = @@character_set_client */ ;
  8512. /*!50003 SET @saved_cs_results     = @@character_set_results */ ;
  8513. /*!50003 SET @saved_col_connection = @@collation_connection */ ;
  8514. /*!50003 SET character_set_client  = utf8 */ ;
  8515. /*!50003 SET character_set_results = utf8 */ ;
  8516. /*!50003 SET collation_connection  = utf8_general_ci */ ;
  8517. /*!50003 SET @saved_sql_mode       = @@sql_mode */ ;
  8518. /*!50003 SET sql_mode              = 'NO_AUTO_VALUE_ON_ZERO' */ ;
  8519. DELIMITER ;;
  8520. /*!50003 CREATE*/ /*!50020 DEFINER=`RB_qa`@`%`*/ /*!50003 PROCEDURE `SOLR_INDEX_CUSTOMERS`()
  8521. BEGIN
  8522.  
  8523. SELECT `e`.`entity_id`, `e`.`email`, `e`.`account_number`, `_table_firstname`.`value` AS `firstname`, `_table_lastname`.`value` AS `lastname`, `_table_billing_postcode`.`value` AS `zipcode`, `_table_billing_city`.`value` AS `city`, `_table_billing_street`.`value` AS `street`, `cmb`.`phone_number` AS `phone`, `cmb`.`is_active` AS `is_phone_active`, `_table_billing_region`.`value` AS `region_state`, `_table_billing_country_id`.`value` AS `country` FROM `customer_entity` AS `e`
  8524. LEFT JOIN `customer_mobile` as `cmb` ON `e`.`entity_id` = `cmb`.`customer_id`
  8525. LEFT JOIN `customer_entity_varchar` AS `_table_prefix` ON (_table_prefix.entity_id = e.entity_id) AND (_table_prefix.attribute_id='4')
  8526. LEFT JOIN `customer_entity_varchar` AS `_table_firstname` ON (_table_firstname.entity_id = e.entity_id) AND (_table_firstname.attribute_id='5')
  8527. LEFT JOIN `customer_entity_varchar` AS `_table_middlename` ON (_table_middlename.entity_id = e.entity_id) AND (_table_middlename.attribute_id='6')
  8528. LEFT JOIN `customer_entity_varchar` AS `_table_lastname` ON (_table_lastname.entity_id = e.entity_id) AND (_table_lastname.attribute_id='7')
  8529. LEFT JOIN `customer_entity_varchar` AS `_table_suffix` ON (_table_suffix.entity_id = e.entity_id) AND (_table_suffix.attribute_id='8')
  8530. LEFT JOIN `customer_entity_int` AS `_table_default_billing` ON (_table_default_billing.entity_id = e.entity_id) AND (_table_default_billing.attribute_id='13')
  8531. LEFT JOIN `customer_address_entity_varchar` AS `_table_billing_postcode` ON (_table_billing_postcode.entity_id = _table_default_billing.value) AND (_table_billing_postcode.attribute_id='29')
  8532. LEFT JOIN `customer_address_entity_varchar` AS `_table_billing_city` ON (_table_billing_city.entity_id = _table_default_billing.value) AND (_table_billing_city.attribute_id='25')
  8533. LEFT JOIN `customer_address_entity_text` AS `_table_billing_street` ON (_table_billing_street.entity_id = _table_default_billing.value) AND (_table_billing_street.attribute_id='24')
  8534. LEFT JOIN `customer_address_entity_varchar` AS `_table_billing_telephone` ON (_table_billing_telephone.entity_id = _table_default_billing.value) AND (_table_billing_telephone.attribute_id='30')
  8535. LEFT JOIN `customer_address_entity_varchar` AS `_table_billing_region` ON (_table_billing_region.entity_id = _table_default_billing.value) AND (_table_billing_region.attribute_id='27')
  8536. LEFT JOIN `customer_address_entity_varchar` AS `_table_billing_country_id` ON (_table_billing_country_id.entity_id = _table_default_billing.value) AND (_table_billing_country_id.attribute_id='26') WHERE (e.entity_type_id = '1')  ORDER BY `e`.`entity_id` desc, `e`.`entity_id`;
  8537.  
  8538. END */;;
  8539. DELIMITER ;
  8540. /*!50003 SET sql_mode              = @saved_sql_mode */ ;
  8541. /*!50003 SET character_set_client  = @saved_cs_client */ ;
  8542. /*!50003 SET character_set_results = @saved_cs_results */ ;
  8543. /*!50003 SET collation_connection  = @saved_col_connection */ ;
  8544. /*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */;
  8545.  
  8546. /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
  8547. /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
  8548. /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
  8549. /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
  8550. /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
  8551. /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
  8552. /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;
  8553.  
  8554. -- Dump completed on 2011-10-26 18:43:39
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement