Advertisement
Guest User

Untitled

a guest
Dec 7th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.18 KB | None | 0 0
  1. ALTER TRIGGER [inv].[Trig_AU_InventoryStock]
  2. ON [inv].[InventoryStock]
  3. after UPDATE
  4. AS
  5. DECLARE trig_cursor CURSOR FOR
  6. SELECT [CalculateDate],
  7. [activitytypeid],
  8. Cast([number]AS NVARCHAR(50)),
  9. inserted.[id],
  10. [warehouseid],
  11. inserted.[companyid],
  12. [financialyearid],
  13. [status]
  14. FROM inserted
  15. INNER JOIN [inv].[recordinventory] RI
  16. ON RI.id = inserted.[recordinventoryid]
  17. DECLARE @TransDate DATE,
  18. @ActivtyTypeId BIGINT,
  19. @strRef NVARCHAR(50),
  20. @RefId BIGINT,
  21. @WarehouseId BIGINT,
  22. @CompanyId BIGINT,
  23. @FinancialYearId BIGINT,
  24. --@CurrencyId BIGINT,
  25. --@Rate DECIMAL ,
  26. @Status BIT
  27.  
  28. OPEN trig_cursor;
  29.  
  30. FETCH next FROM trig_cursor INTO @TransDate, @ActivtyTypeId, @strRef, @RefId
  31. , @WarehouseId, @CompanyId, @FinancialYearId, @Status
  32.  
  33. WHILE @@FETCH_STATUS = 0
  34. BEGIN
  35. DECLARE @oldWarehouseId BIGINT
  36. --select warehouse from the previous inserted item transaction before updating it
  37. DECLARE @ItemTransactionId BIGINT
  38. --select @ItemTransactionId from the previous inserted item transaction before updating it
  39. DECLARE @OldTransactionDate DATETIME
  40.  
  41. --select @OldTransactionDate from the previous inserted item transaction before updating it
  42. SELECT @oldWarehouseId = it.[warehouseid],
  43. @ItemTransactionId = it.id,
  44. @OldTransactionDate = it.transactiondate
  45. FROM [inv].[itemtransaction] it
  46. INNER JOIN inserted
  47. ON it.[refid] = inserted.id
  48. AND it.[activitytypeid] = inserted.[activitytypeid]
  49. AND inserted.[companyid] = it.[companyid]
  50. INNER JOIN [inv].[recordinventory] RI
  51. ON RI.id = inserted.[recordinventoryid]
  52. AND RI.[financialyearid] = it.[financialyearid]
  53.  
  54. DECLARE @TransIDIfExists BIGINT
  55. --check if this record has been inserted as a transaction before or not (avoid error of : can't insert null into itemtransactionID)
  56.  
  57. SELECT @TransIDIfExists = it.id
  58. FROM inv.itemtransaction it
  59. WHERE it.[transactiondate] = @TransDate
  60. AND it.[activitytypeid] = @ActivtyTypeId
  61. AND it.[refrancenumber] = @strRef
  62. AND it.[refid] = @RefId
  63. AND it.[companyid] = @CompanyId
  64. AND it.[financialyearid] = @FinancialYearId
  65.  
  66. IF @Status = 1
  67. BEGIN
  68. IF @TransIDIfExists IS NOT NULL --exists
  69. BEGIN
  70. IF ( @oldWarehouseId <> @WarehouseId )
  71. --warehouse has been changed
  72. BEGIN
  73. DECLARE @Firsttime BIT
  74. SET @Firsttime = 1
  75. --update old warehouse in itemledger
  76. --delete all trans det of the old warehouse and recalculate ledger for old
  77. EXEC [dbo].[Process_recalculate_itemledgerwhenchangewarehouse]
  78. @RefId,
  79. @TransDate,
  80. @ActivtyTypeId,
  81. @oldWarehouseId,
  82. @CompanyId,
  83. @FinancialYearId,
  84. @ItemTransactionId,
  85. @Firsttime,
  86. NULL,--@IsItemTransfer
  87. NULL;--@IsDebitWarehouse
  88. --here we removed the last itemtransaction detail and recalculated the old warehouse
  89. --insert into itemtransaction detail case update warehouse and not update any of sale or puchase detail's item
  90. EXEC [dbo].[Process_update_itemtransaction]
  91. @TransDate,
  92. @ActivtyTypeId,
  93. @strRef,
  94. @RefId,
  95. @WarehouseId,
  96. @CompanyId,
  97. @FinancialYearId,
  98. NULL,
  99. NULL;
  100. END
  101. ELSE --@oldWarehouseId = @WarehouseId
  102. BEGIN
  103. EXEC [dbo].[Process_update_itemtransaction]
  104. @TransDate,
  105. @ActivtyTypeId,
  106. @strRef,
  107. @RefId,
  108. @WarehouseId,
  109. @CompanyId,
  110. @FinancialYearId,
  111. NULL,
  112. NULL;
  113. END
  114. END
  115. ELSE IF @TransIDIfExists IS NULL--the transaction doesn't exist
  116. BEGIN
  117. EXEC [dbo].[Process_insert_itemtransaction]
  118. @TransDate,
  119. @ActivtyTypeId,
  120. @strRef,
  121. @RefId,
  122. @WarehouseId,
  123. @CompanyId,
  124. @FinancialYearId,
  125. NULL,
  126. NULL;
  127. END
  128.  
  129.  
  130.  
  131. EXEC [dbo].[Process_insert_itemtransactiondetailcasenotupdated]
  132. @RefId,
  133. @TransDate,
  134. @ActivtyTypeId,
  135. @WarehouseId,
  136. @CompanyId,
  137. @FinancialYearId,
  138. @ItemTransactionId,
  139. 0
  140.  
  141. END
  142. ELSE --@Status = False
  143. BEGIN
  144. IF @TransIDIfExists IS NOT NULL--the transaction exist
  145. BEGIN
  146. --delete this trans and its detail because the status is false
  147. DECLARE @ItemId7 BIGINT
  148. DECLARE trig_cursor7 CURSOR FOR
  149. SELECT [itemid]
  150. FROM inserted
  151. INNER JOIN [inv].[inventorystockdetails] isd
  152. ON inserted.id = isd.[inventorystockid]
  153. WHERE isd.[inventorystockid] = inserted.id
  154.  
  155. OPEN trig_cursor7
  156.  
  157. FETCH next FROM trig_cursor7 INTO @ItemId7
  158.  
  159. WHILE @@FETCH_STATUS = 0
  160. BEGIN
  161. EXEC [dbo].[Process_delete_itemtransactiondetail]
  162. @ItemId7,
  163. @ActivtyTypeId,
  164. @RefId,
  165. NULL,
  166. NULL--delete details first
  167. EXEC [dbo].[Process_delete_itemtransaction]
  168. @ActivtyTypeId,
  169. @RefId,
  170. @CompanyId,
  171. @FinancialYearId,
  172. NULL
  173.  
  174. FETCH next FROM trig_cursor7 INTO @ItemId7
  175. END
  176.  
  177. CLOSE trig_cursor7
  178.  
  179. DEALLOCATE trig_cursor7
  180. END
  181. END
  182.  
  183. FETCH next FROM trig_cursor INTO @TransDate, @ActivtyTypeId, @strRef,
  184. @RefId
  185. , @WarehouseId, @CompanyId, @FinancialYearId, @Status
  186. END;
  187.  
  188. CLOSE trig_cursor;
  189.  
  190. DEALLOCATE trig_cursor;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement