You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

638 lines
31 KiB

1 year ago
  1. USE [FastMoneyPro_Remit]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[proc_RewardPointOperation] Script Date: 9/27/2019 1:30:14 PM ******/
  4. SET ANSI_NULLS ON
  5. GO
  6. SET QUOTED_IDENTIFIER ON
  7. GO
  8. CREATE OR ALTER PROCEDURE [dbo].[proc_RewardPointOperation]
  9. @flag VARCHAR(30),
  10. @orderId VARCHAR(50) = null,
  11. @customerId VARCHAR(30) = null,
  12. @productCode VARCHAR(50) = null,
  13. @branchCode INT = null,
  14. @recvType INT = null,
  15. @recvAddress VARCHAR(200) = null,
  16. @recvZipCode VARCHAR(20) = null,
  17. @recvPhoneNumber VARCHAR(30) = null,
  18. @recvName VARCHAR(30) = null,
  19. @startDate DateTime = null,
  20. @endDate DateTime = null,
  21. @createdBy VARCHAR(20) = null,
  22. @modifiedBy VARCHAR(20) = null,
  23. @orderType VARCHAR(3) = null,
  24. @sortBy VARCHAR(50) = null,
  25. @sortOrder VARCHAR(5) = null,
  26. @table VARCHAR(max)=null,
  27. @sql_filter VARCHAR(max) = null,
  28. @searchCriteria VARCHAR(30) = null,
  29. @searchValue VARCHAR(50) = null,
  30. @select_field_list VARCHAR(max) = null,
  31. @extra_field_list VARCHAR(max) = null,
  32. @pageSize int = null,
  33. @pageNumber int = NULL,
  34. @user VARCHAR(100) = null,
  35. @email VARCHAR(100) = null,
  36. @schemeId INT = null
  37. AS
  38. SET NOCOUNT ON
  39. -------------------------------- l`  ǔ Ȉ ¸
  40. IF @flag='productlist'
  41. BEGIN
  42. SELECT PL.productCode,PL.productName,PL.pointPrice,PL.deliveryYN, PL.productImgPath, PL.buyType, PL.couponType
  43. FROM ProductList PL(NOLOCK)
  44. ORDER BY PL.productCode ASC
  45. END
  46. --------------------------------END l`  ǔ Ȉ ¸
  47. -------------------------------- Ȉ l ƭ
  48. IF @flag='buyproduct'
  49. BEGIN
  50. DECLARE
  51. @pointPrice money,
  52. @bounsPoint money,
  53. @productname VARCHAR(20),
  54. @mobilenumber VARCHAR(20)
  55. -- 9 Ѕ l
  56. IF @recvType != '1'
  57. BEGIN
  58. IF @recvType != '2'
  59. BEGIN
  60. IF @recvType != '3'
  61. BEGIN
  62. EXEC proc_errorHandler 1, 'Wrong recv Type.', NULL
  63. RETURN
  64. END
  65. END
  66. END
  67. --8 Ѕ l
  68. IF @orderType != '1'
  69. BEGIN
  70. EXEC proc_errorHandler 1, 'Wrong order Type .', NULL
  71. RETURN
  72. END
  73. --  <t $X %
  74. IF NOT EXISTS(
  75. SELECT '1' FROM customerMaster(NOLOCK) CM
  76. WHERE @email = CM.email
  77. )
  78. BEGIN
  79. EXEC proc_errorHandler 1, 'No User Information.', NULL
  80. RETURN
  81. END
  82. --  9  l
  83. IF @recvType = '2' -- 9(1:0, 2:)
  84. BEGIN
  85. IF ISNULL(@branchCode,'') = ''
  86. BEGIN
  87. EXEC proc_errorHandler 1, 'input branchCode.', NULL
  88. RETURN
  89. END
  90. -- X | >XD L,
  91. IF NOT EXISTS(
  92. SELECT '1'
  93. FROM agentMaster(NOLOCK) AM
  94. WHERE AM.agentId = @branchCode
  95. )
  96. BEGIN
  97. EXEC proc_errorHandler 1, 'No Branch Information.', NULL
  98. RETURN
  99. END
  100. END
  101. --0 9  l
  102. IF @recvType = '1'
  103. BEGIN
  104. IF ISNULL(@recvAddress,'') = ''
  105. BEGIN
  106. EXEC proc_errorHandler 1, 'input recv Address.', NULL
  107. RETURN
  108. END
  109. IF ISNULL(@recvZipCode,'') = ''
  110. BEGIN
  111. EXEC proc_errorHandler 1, 'input recv ZipCode.', NULL
  112. RETURN
  113. END
  114. IF ISNULL(@recvPhoneNumber,'') = ''
  115. BEGIN
  116. EXEC proc_errorHandler 1, 'input recv PhoneNumber.', NULL
  117. RETURN
  118. END
  119. IF ISNULL(@recvName,'') = ''
  120. BEGIN
  121. EXEC proc_errorHandler 1, 'input recv Name.', NULL
  122. RETURN
  123. END
  124. END
  125. IF NOT EXISTS
  126. (SELECT '1'
  127. FROM ProductList(nolock) PL
  128. WHERE @productCode = PL.productCode
  129. )
  130. BEGIN
  131. EXEC proc_errorHandler 1, 'No Product.', NULL
  132. RETURN
  133. END
  134. SELECT @pointPrice = PL.pointPrice,
  135. @productname = productName
  136. FROM ProductList(nolock) PL
  137. WHERE @productCode = PL.productCode
  138. --- Ȉ t <t $X %
  139. IF ISNULL(@pointPrice, '') = ''
  140. BEGIN
  141. EXEC proc_errorHandler 1, 'No Product Price.', NULL
  142. RETURN
  143. END
  144. --- Ȉ t <t $X %
  145. IF ISNULL(@productname, '') = ''
  146. BEGIN
  147. EXEC proc_errorHandler 1, 'No Product Name.', NULL
  148. RETURN
  149. END
  150. -- t Ȉ l p | ǔ
  151. SELECT @customerId = customerId,@bounsPoint = bonusPoint
  152. FROM customerMaster(NOLOCK)
  153. WHERE @email = email AND bonusPoint >= @pointPrice
  154. -- t $X %
  155. IF ISNULL(@bounsPoint,'') = ''
  156. BEGIN
  157. EXEC proc_errorHandler 1, 'Point is not enough to purchase.', NULL
  158. RETURN
  159. END
  160. BEGIN TRY
  161. BEGIN TRAN
  162. --  | D p \.
  163. UPDATE customerMaster
  164. SET bonusPoint = (bonusPoint - @pointPrice)
  165. WHERE @email = email
  166. --- 8  %
  167. INSERT INTO ProductOrder
  168. (
  169. orderId,
  170. customerId,
  171. productCode,
  172. usePoint,
  173. orderType,
  174. recvType,
  175. branchCode,
  176. recvAddress,
  177. recvZipCode,
  178. recvPhoneNumber,
  179. recvName,
  180. createdBy,
  181. createdDate,
  182. modifiedBy,
  183. modifiedDate,
  184. orderStatus,
  185. recvDate,
  186. recvBy
  187. )
  188. SELECT
  189. @orderId,
  190. @customerId,
  191. @productCode,
  192. @pointPrice,
  193. @orderType,
  194. @recvType,
  195. @branchCode,
  196. @recvAddress,
  197. @recvZipCode,
  198. @recvPhoneNumber,
  199. @recvName,
  200. @email,
  201. GETDATE(),
  202. null,
  203. null,
  204. '1', --(1:8, 9:͌)
  205. null,
  206. null
  207. -- 8 X \| 0]\
  208. INSERT INTO PointHistory
  209. (
  210. customerId,
  211. availablePoint,
  212. tranId,
  213. pointType,
  214. point,
  215. createdBy,
  216. createdDate
  217. )
  218. SELECT
  219. CM.customerId,
  220. CM.bonusPoint,
  221. @orderId,
  222. '9', -- (1: \ Ƚ, 2: a<\ Ƚ, 9: , 11:  Ƚ͌, 21:a Ƚ͌, 91: ͌)
  223. @pointPrice,
  224. CM.email,
  225. GETDATE()
  226. FROM customerMaster(NOLOCK) CM
  227. WHERE @email = CM.email
  228. EXEC proc_errorHandler 0, 'The Order is Success.', NULL
  229. SELECT @mobilenumber = mobile
  230. FROM customerMaster(NOLOCK)
  231. WHERE customerId = @customerId
  232. SELECT
  233. agentEmail1,
  234. agentName,
  235. @productname AS productname,
  236. @mobilenumber AS mobile
  237. FROM agentMaster(NOLOCK)
  238. WHERE agentId = @branchCode
  239. SELECT buyType AS productType, schemeId AS CouponId FROM ProductList WHERE productCode = @productCode
  240. COMMIT TRAN
  241. END TRY
  242. BEGIN CATCH
  243. IF @@TRANCOUNT > 0
  244. ROLLBACK TRANSACTION;
  245. PRINT ERROR_MESSAGE()
  246. END CATCH
  247. RETURN
  248. END
  249. --------------------------------END Ȉ l ƭ
  250. -------------------------------- 8
  251. --orderType (1:8, 9:͌)
  252. IF @flag='orderedlist'
  253. BEGIN
  254. --  <t $X %
  255. IF NOT EXISTS(
  256. SELECT '1' FROM customerMaster(NOLOCK) CM
  257. WHERE @email = CM.email
  258. )
  259. BEGIN
  260. EXEC proc_errorHandler 1, 'No User Information.', NULL
  261. RETURN
  262. END
  263. SELECT @customerId = customerId FROM customerMaster(NOLOCK) CM
  264. WHERE @email = CM.email
  265. EXEC proc_errorHandler 0, 'Success', NULL
  266. SELECT
  267. PO.orderId,
  268. PO.productCode,
  269. PL.productName,
  270. PO.usePoint,
  271. PO.orderType,
  272. PO.recvType,
  273. PO.branchCode,
  274. AM.agentName,
  275. PO.recvAddress,
  276. PO.recvZipCode,
  277. PO.recvPhoneNumber,
  278. PO.recvName,
  279. PO.orderStatus,
  280. PO.createdDate,
  281. PO.modifiedDate,
  282. PO.recvDate
  283. FROM ProductOrder PO(NOLOCK)
  284. LEFT JOIN ProductList PL
  285. ON PO.productCode = PL.productCode
  286. LEFT JOIN agentMaster AM
  287. ON PO.branchCode = AM.agentId
  288. WHERE
  289. @customerId = PO.customerId
  290. AND
  291. @startDate <= CONVERT(date, PO.createdDate)
  292. AND
  293. @endDate >= CONVERT(date, PO.createdDate)
  294. ORDER BY PO.createdDate DESC
  295. RETURN
  296. END
  297. --------------------------------END 8
  298. -------------------------------- <t 9
  299. ----orderStatus (1:8D̸, 2:D̸, 3:9D̸, 9:̬͌)
  300. IF @flag='receiptorder'
  301. BEGIN
  302. IF EXISTS(
  303. SELECT 'X'
  304. FROM ProductOrder(NOLOCK)
  305. WHERE @orderId = orderId
  306. )
  307. BEGIN
  308. --select * from ProductOrder where orderId = '20190522531018896'
  309. --select * from PointHistory where tranId = '20190522531018896'
  310. BEGIN TRY
  311. BEGIN TRAN
  312. UPDATE ProductOrder
  313. SET orderType = '1', -- 1:8, 9:͌
  314. orderStatus = '3',
  315. recvBy = @user,
  316. recvDate = GETDATE()
  317. WHERE @orderId = orderId
  318. ---- <t 9 X \| 0]\
  319. --INSERT INTO PointHistory
  320. --(
  321. --customerId,
  322. --availablePoint,
  323. --tranId,
  324. --pointType,
  325. --point,
  326. --createdBy,
  327. --createdDate
  328. --)
  329. --SELECT
  330. --CM.customerId,
  331. --CM.bonusPoint,
  332. --@orderId,
  333. --'9', -- (1: \ Ƚ, 2: a<\ Ƚ, 9: , 11:  Ƚ͌, 21:a Ƚ͌, 91: ͌)
  334. --@pointPrice,
  335. --@createdBy,
  336. --GETDATE()
  337. --FROM customerMaster(NOLOCK) CM
  338. --WHERE @email = CM.email -- t|h
  339. EXEC proc_errorHandler 0, 'Receipt Success.', NULL
  340. COMMIT
  341. RETURN
  342. END TRY
  343. BEGIN CATCH
  344. IF @@TRANCOUNT > 0
  345. ROLLBACK TRANSACTION;
  346. PRINT ERROR_MESSAGE()
  347. END CATCH
  348. END
  349. -- 8 | >XD L
  350. BEGIN
  351. EXEC proc_errorHandler 1, 'CustomerId or OrderId is wrong.', NULL
  352. RETURN
  353. END
  354. END
  355. --------------------------------END <t 9
  356. -------------------------------- 8 ͌
  357. IF @flag='canceluse'
  358. BEGIN
  359. DECLARE @V_RET_POINT MONEY, @availablePoint MONEY
  360. BEGIN TRY
  361. BEGIN TRAN
  362. IF EXISTS(SELECT '1' FROM ProductOrder WHERE orderId = @orderId )
  363. BEGIN
  364. --|L ͌| Xɻ\.
  365. SELECT @recvType = recvType FROM ProductOrder WHERE orderId = @orderId
  366. IF @recvType = '3'
  367. BEGIN
  368. EXEC proc_errorHandler 1, 'It is an item that can not be cancelled.', NULL
  369. END
  370. ELSE
  371. BEGIN
  372. ----CANCEL Product in Order Table
  373. UPDATE ProductOrder
  374. SET orderType = '9',
  375. orderStatus = '9', --orderStatus (1:8D̸, 2:D̸, 3:9D̸, 9:̬͌)
  376. modifiedBy = @user,
  377. modifiedDate = GETDATE()
  378. WHERE orderId = @orderId
  379. --------------------------------------
  380. ----Find use Point in PointHistory Table
  381. SELECT @customerId = customerId, @V_RET_POINT = point
  382. FROM PointHistory
  383. WHERE tranId = @orderId
  384. --------------------------------------
  385. SELECT @availablePoint = bonusPoint, @email = email
  386. FROM customerMaster
  387. WHERE customerId = @customerId
  388. ------ recover bonusPoint
  389. UPDATE
  390. customerMaster
  391. SET bonusPoint = @availablePoint + @V_RET_POINT
  392. WHERE customerId =@customerId
  393. --------------------------------------
  394. INSERT INTO
  395. dbo.PointHistory(
  396. customerId,
  397. availablePoint,
  398. tranId,
  399. pointType,
  400. point,
  401. createdBy,
  402. createdDate
  403. )
  404. SELECT
  405. @customerId,
  406. @availablePoint + @V_RET_POINT,
  407. @orderId,
  408. '91',
  409. @V_RET_POINT,
  410. @user,
  411. GETDATE()
  412. EXEC proc_errorHandler 0, 'The Order Cancel is Success.', NULL
  413. END
  414. END
  415. COMMIT
  416. RETURN
  417. END TRY
  418. BEGIN CATCH
  419. IF @@TRANCOUNT > 0
  420. ROLLBACK TRANSACTION;
  421. PRINT ERROR_MESSAGE()
  422. END CATCH
  423. END
  424. --------------------------------END 8 ͌
  425. -------------------------------- Dܴ -- ̈ Dܴ @searchCriteria @ |XXt .
  426. IF @flag='searchCriteria'
  427. BEGIN
  428. SELECT '' value, 'Select' [text] UNION ALL
  429. SELECT 'orderId', 'Order ID' UNION ALL
  430. SELECT 'email', 'E-Mail'
  431. RETURN
  432. END
  433. If @flag='Status'
  434. begin
  435. SELECT '' [value], 'Select' [text] UNION ALL
  436. SELECT '1' , 'Order' UNION ALL
  437. SELECT '3' , 'Receipt' UNION ALL
  438. SELECT '9' , 'Cancel'
  439. return
  440. END
  441. If @flag='branch'
  442. begin
  443. SELECT '' [value], 'Select' [text] UNION ALL
  444. select cast(agentid as varchar(10)),AGENTNAME from agentmaster(nolock) where parentid=1008
  445. return
  446. END
  447. -------------------------------------------
  448. IF @flag='grid'
  449. BEGIN
  450. IF @sortBy IS NULL
  451. SET @sortBy = 'orderid'
  452. IF @sortOrder IS NULL
  453. SET @sortOrder = 'DESC'
  454. --------------
  455. -- (1:8D̸, 2:D̸, 3:9D̸, 9:̬͌)
  456. --SELECT rowid,
  457. --orderId AS orderid,
  458. --customerId,
  459. --productCode AS productname,
  460. --usePoint,
  461. --orderStatus,
  462. --(CASE
  463. --WHEN orderStatus = '1'
  464. --THEN '&nbsp;<a class=\"btn btn-xs btn-primary\" title=\"Edit\" href=\"RewardPointManagement.aspx?orderid=@orderid&method=cancel\"><i class=\"fa fa-pencil\"></i></a>&nbsp;<a class=\"btn btn-xs btn-success\" title=\"Approve\" href=\"RewardPointManagement.aspx?orderid=@orderid&method=receipt\"><i class=\"fa fa-check\"></i></a>'
  465. --WHEN orderStatus = '3'
  466. --THEN '&nbsp;<a class=\"btn btn-xs btn-primary\" title=\"Edit\" href=\"RewardPointManagement.aspx?orderid=@orderid&method=cancel\"><i class=\"fa fa-pencil\"></i></a>&nbsp;'
  467. --WHEN orderStatus = '9'
  468. --THEN ''
  469. --ELSE ''
  470. -- END) AS set
  471. --FROM dbo.ProductOrder
  472. --where 1=1
  473. -------
  474. SET @table = '(SELECT PO.rowid,
  475. orderId AS orderid,
  476. orderStatus,
  477. CM.email,
  478. PL.productName AS productname,
  479. CONVERT(INT, usePoint) AS usePoint,
  480. (CASE
  481. WHEN orderStatus = ''1''
  482. THEN ''Order''
  483. WHEN orderStatus = ''3''
  484. THEN ''Receipt''
  485. WHEN orderStatus = ''9''
  486. THEN ''Cancel''
  487. ELSE ''''
  488. END) AS orderStat
  489. ,
  490. (CASE
  491. WHEN orderStatus = ''1''
  492. THEN ''&nbsp;<a class="btn btn-xs btn-primary" title="cancel" href="RewardPointManagement.aspx?orderid=@orderid&method=cancel"><i class="fa fa-remove"></i></a>&nbsp;<a class="btn btn-xs btn-success" title="receipt" href="RewardPointManagement.aspx?orderid=@orderid&method=receipt"><i class="fa fa-check"></i></a>''
  493. WHEN orderStatus = ''3''
  494. THEN ''''
  495. WHEN orderStatus = ''9''
  496. THEN ''''
  497. ELSE ''''
  498. END) AS tagBtn
  499. ,po.createdDate as orderdate
  500. ,AM.AGENTNAME AS RequestBranch
  501. ,AM.AGENTID
  502. FROM dbo.ProductOrder PO
  503. JOIN dbo.customerMaster CM
  504. ON PO.customerId = CM.customerId
  505. JOIN dbo.ProductList PL
  506. ON PL.productCode = PO.productCode
  507. INNER JOIN AGENTMASTER AM
  508. ON AM.AGENTID=PO.BRANCHCODE
  509. where AM.parentid=1008
  510. ) x'
  511. -------
  512. --- AS t codeX GridColumn X ̈ |0@ |XXt .
  513. --SET @table = '(SELECT rowid,orderId AS orderid,customerId,productCode AS productname,usePoint,orderStatus FROM dbo.ProductOrder
  514. -- where 1=1
  515. -- ) x'
  516. PRINT @table
  517. SET @sql_filter = ''
  518. IF ISNULL(@searchCriteria,'')<>'' AND ISNULL(@searchValue,'')<>''
  519. BEGIN
  520. IF @searchCriteria='orderId'
  521. SET @sql_Filter=@sql_Filter + ' AND orderId like ''' +@searchValue+'%'''
  522. ELSE IF @searchCriteria='email'
  523. SET @sql_Filter=@sql_Filter + ' AND email like ''' +@searchValue+'%'''
  524. END
  525. if @branchCode is not null
  526. SET @sql_Filter=@sql_Filter + 'and AGENTID =' +@branchCode
  527. if @orderType is not null
  528. SET @sql_Filter=@sql_Filter + 'and orderStatus =' + @orderType
  529. --- codeX GridColumn X ̈ |0@ |XXt .
  530. SET @select_field_list ='rowid,RequestBranch,orderid,email,productname,usePoint,orderStatus,orderStat,orderdate,tagBtn'
  531. EXEC dbo.proc_paging
  532. @table
  533. ,@sql_filter
  534. ,@select_field_list
  535. ,@extra_field_list
  536. ,@sortBy
  537. ,@sortOrder
  538. ,@pageSize
  539. ,@pageNumber
  540. END
  541. -- l \ ȩ
  542. IF @flag = 'RewardCoupon'
  543. BEGIN
  544. IF EXISTS (SELECT 'X' FROM CouponSetup CS
  545. WHERE rowId = @schemeId AND endDate >= GETDATE() AND isActive = 'Y')
  546. BEGIN
  547. SELECT @customerId = customerId FROM customerMaster WHERE email = @email
  548. -- ̸̹0@ L K K\ 0<\\.
  549. SELECT @endDate = endDate FROM CouponSetup WHERE rowId = @schemeId
  550. UPDATE ProductOrder
  551. SET orderType = '1', -- 1:8, 9:͌
  552. orderStatus = '3',
  553. recvBy = @user,
  554. recvDate = GETDATE()
  555. WHERE orderId = @orderId
  556. INSERT INTO CouponIssue (customerId,couponId,startDate,endDate,createdDate,createdBy,isActive,usedCount)
  557. VALUES (@customerId,@schemeId,GETDATE(),@endDate,GETDATE(),'SYSTEM','Y',0)
  558. EXEC proc_errorHandler 0, 'The Order is Success.', @@IDENTITY
  559. RETURN
  560. END
  561. EXEC proc_errorHandler 1, 'CouponSetUp Error.', NULL
  562. END