Advertisement
Guest User

Untitled

a guest
Feb 19th, 2020
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. DELIMITER ;;
  2. CREATE DEFINER=`root`@`localhost` PROCEDURE `AddUnilevel`(
  3. IN i_subscriber_id VARCHAR(100),
  4. IN i_amount decimal(10,2),
  5. IN i_transaction_type VARCHAR(20),
  6. IN i_trackno VARCHAR(30)
  7. )
  8. BEGIN
  9. DECLARE v_amount INT UNSIGNED;
  10. declare v_price decimal unsigned;
  11. DECLARE v_points DECIMAL UNSIGNED;
  12. declare v_referral_code_price decimal unsigned;
  13. DECLARE v_referral_account_type VARCHAR(30);
  14. DECLARE v_referred_by_id VARCHAR(30);
  15. DECLARE v_statuserror TINYINT(1);
  16. DECLARE done INT DEFAULT FALSE;
  17. DECLARE o_success BOOLEAN;
  18. DECLARE o_message VARCHAR(255);
  19.  
  20. DECLARE EXIT HANDLER FOR SQLEXCEPTION
  21. BEGIN
  22. ROLLBACK;
  23. SELECT o_success AS `status`,o_message AS message, v_statuserror AS error;
  24. END;
  25.  
  26. DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;
  27.  
  28. START TRANSACTION;
  29. -- DECLARE EXIT HANDLER FOR SQLEXCEPTION
  30.  
  31. SET o_success = FALSE;
  32. SET o_message = 'Failed 1 Add Unilevel Points';
  33.  
  34. select referred_by into v_referred_by_id from `user_referrals` where subscriber_id = i_subscriber_id;
  35.  
  36. SELECT `subscription_code` INTO v_referral_account_type
  37. FROM user_subscriptions t1
  38. WHERE t1.`created_at` = (SELECT MAX(t2.`created_at`)
  39. FROM user_subscriptions t2
  40. WHERE t2.`subscriber_id` = t1.`subscriber_id`)
  41. AND t1.`subscriber_id` COLLATE utf8mb4_unicode_ci = v_referred_by_id;
  42.  
  43. SET o_message = 'Failed 2 Add Unilevel Points';
  44.  
  45. if v_referral_account_type = 'master' then
  46.  
  47. case
  48. when i_transaction_type = 'dom_air_travel' then set v_amount = 20;
  49. WHEN i_transaction_type = 'int_air_travel' THEN set v_amount = 20;
  50. WHEN i_transaction_type = 'hotel_transaction' THEN set v_amount = i_amount * 0.03;
  51. else set v_amount = 0;
  52. end case ;
  53. end if;
  54.  
  55. if v_referral_account_type = 'mentor' THEN
  56. CASE
  57. WHEN i_transaction_type = 'dom_air_travel' THEN set v_amount = 10;
  58. WHEN i_transaction_type = 'int_air_travel' THEN set v_amount = 10;
  59. WHEN i_transaction_type = 'hotel_transaction' THEN set v_amount = i_amount * 0.02;
  60. ELSE set v_amount = 0;
  61. END case;
  62.  
  63. end if;
  64.  
  65. select `price` into v_referral_code_price from `subscriptions` where `code` = v_referral_account_type;
  66. select `points` into v_points from `user_points` where `subscriber_id` = v_referred_by_id;
  67.  
  68. IF v_referral_code_price < 9990 or v_amount = 0 then
  69.  
  70. SET o_success = TRUE;
  71. SET o_message = 'Successful';
  72. SET v_statuserror = 1;
  73.  
  74. else
  75. INSERT INTO `user_point_transactions` (
  76. `subscriber_id`,
  77. `reference_number`,
  78. `tracking_number`,
  79. `transaction_type`,
  80. `amount`,
  81. `balance_before`,
  82. `balance_after`,
  83. `type`,
  84. `status`,
  85. `created_at`,
  86. `updated_at`
  87. )
  88. VALUES (
  89. v_referred_by_id,
  90. CONCAT('REF-',v_referred_by_id,'-',CHAR( FLOOR(65 + (RAND() * 25))),CHAR( FLOOR(65 + (RAND() * 25))),SUBSTR(UNIX_TIMESTAMP(),4,6)),
  91. i_trackno,
  92. i_transaction_type,
  93. v_amount,
  94. v_points,
  95. v_points + v_amount,
  96. 2,
  97. 1,
  98. now(),
  99. now()
  100. );
  101.  
  102.  
  103. UPDATE `user_points`
  104. SET `points` = `points` + v_amount
  105. WHERE `subscriber_id` = v_referred_by_id;
  106.  
  107.  
  108. END IF;
  109.  
  110.  
  111. COMMIT;
  112. SET o_success = TRUE;
  113. SET o_message = 'Successful';
  114. SET v_statuserror = 1;
  115. SELECT o_success AS `status`,o_message AS message, v_statuserror AS error;
  116. END;;
  117. DELIMITER ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement