Advertisement
Guest User

Untitled

a guest
Mar 23rd, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 19.14 KB | None | 0 0
  1. Users
  2.  
  3. CREATE view [dbo].[UsersOverview]
  4. as
  5. select u.Name,u.UserName,a.AccountNumber as CashAccountNumber,c.Name as ClientName
  6. from dbo.Users u
  7. left join dbo.Accounts a on a.Id = u.CashAccountId
  8. left join dbo.Clients c on c.Id = u.ClientId
  9.  
  10. GO
  11.  
  12. CREATE proc [dbo].[InsertUser]
  13. (
  14. @Name nvarchar(400)
  15. ,@UserName varchar(30)
  16. ,@Password varchar(30)
  17. ,@CashAccountId bigint = null
  18. ,@ClientId bigint = null
  19. ,@res bigint output
  20. )
  21. AS
  22. begin
  23. set nocount on
  24. if(ISNULL(@Name,'')<>'')
  25. begin
  26. if exists(select * from Accounts where Id = @CashAccountId) or @CashAccountId is NULL
  27. and exists(select * from Clients where Id = @ClientId) or @ClientId is NULL
  28. begin
  29. insert into Users(Name,UserName,[Password],CashAccountId,ClientId)
  30. values (@Name,@UserName, @Password,@CashAccountId,@ClientId)
  31.  
  32. set @res = @@ROWCOUNT
  33. end
  34. else
  35. Raiserror('Invalid CashAccountId or ClientId',16,1)
  36. end
  37. else
  38. begin
  39. Raiserror('Name is null!',16,1)
  40. end
  41.  
  42. set nocount off
  43. end
  44. GO
  45. create procedure UpdateUser
  46. (
  47. @Id bigint,
  48. @Name nvarchar(400)
  49. ,@UserName varchar(30)
  50. ,@Password varchar(30)
  51. ,@CashAccountId bigint = null
  52. ,@ClientId bigint = null
  53. )
  54. as
  55. BEGIN
  56. set nocount on
  57. if(ISNULL(@Name,'')<>'')
  58. begin
  59. update Users
  60. set Name=ISNULL(@Name,Name)
  61. ,UserName = ISNULL(@UserName,UserName)
  62. ,[Password] = ISNULL(@Password,[Password])
  63. ,CashAccountId = ISNULL(@CashAccountId,CashAccountId)
  64. ,ClientId = ISNULL(@ClientId,ClientId)
  65. where Id = @Id
  66. end
  67. else
  68. begin
  69. Raiserror('Name is null!',16,1)
  70. end
  71.  
  72. set nocount off
  73. END
  74.  
  75. Clients
  76. CREATE view dbo.ClientsOverview
  77. as
  78. select Name
  79. ,case when [Type] = 0 then 'Individual' else 'Organization' end as [Type]
  80. ,IdentificationNumber
  81. ,[Address]
  82. ,Tel
  83. ,Mob
  84. ,Email
  85. ,Web
  86. from dbo.Clients
  87.  
  88. GO
  89.  
  90. create procedure InsertClient
  91. (
  92. @Name nvarchar(400)
  93. ,@Type bit
  94. ,@IdentificationNumber varchar(36)
  95. ,@Address varchar(100)
  96. ,@Tel varchar(30)
  97. ,@Mob varchar(30)
  98. ,@Email varchar(30)
  99. ,@Web varchar(100)
  100. )
  101. AS
  102. begin
  103. set nocount on
  104.  
  105. insert into Clients(Name,[Type],IdentificationNumber,[Address],Tel,Mob,Email,Web)
  106. values (@Name,@Type, @IdentificationNumber,@Address,@Tel,@Mob,@Email,@Web)
  107.  
  108. set nocount off
  109. end
  110.  
  111. GO
  112.  
  113.  
  114.  
  115. create procedure UpdateClient
  116. (
  117. @Id bigint
  118. ,@Name nvarchar(400)
  119. ,@Type bit
  120. ,@IdentificationNumber varchar(36)
  121. ,@Address varchar(100)
  122. ,@Tel varchar(30)
  123. ,@Mob varchar(30)
  124. ,@Email varchar(30)
  125. ,@Web varchar(100)
  126. )
  127. as
  128. BEGIN
  129. set nocount on
  130.  
  131. update Clients
  132. set Name=ISNULL(@Name,Name)
  133. ,[Type] = ISNULL(@Type,[Type])
  134. ,IdentificationNumber = ISNULL(@IdentificationNumber,IdentificationNumber)
  135. ,[Address] = ISNULL(@Address,[Address])
  136. ,Tel = ISNULL(@Tel,Tel)
  137. ,Mob = ISNULL(@Mob,Mob)
  138. ,Email = ISNULL(@Email,Email)
  139. ,Web = ISNULL(@Web,Web)
  140. where Id = @Id
  141.  
  142. set nocount off
  143. END
  144.  
  145. Currencies
  146. CREATE view dbo.CurrenciesOverview
  147. as
  148. select Currency, Name, [Image], Symbol
  149. from dbo.Currencies
  150.  
  151. GO
  152.  
  153. create procedure InsertCurrencies
  154. (
  155. @Currency varchar(3)
  156. ,@Name varchar(30)
  157. ,@Image varbinary(8000)
  158. ,@Symbol nvarchar(10)
  159. )
  160. AS
  161. begin
  162. set nocount on
  163.  
  164. if(ISNULL(@Currency,'')<>'')
  165. begin
  166. if exists(select * from Currencies where Currency = @Currency)
  167. begin
  168. Raiserror('Currency already exists!',16,1)
  169. end
  170. else
  171. begin
  172. insert into Currencies(Currency,Name,[Image],Symbol)
  173. values (@Currency,@Name, @Image,@Symbol)
  174. end
  175. end
  176. else
  177. Raiserror('Invalid Currency!',16,1)
  178.  
  179. set nocount off
  180. end
  181.  
  182. GO
  183.  
  184. create procedure UpdateCurrencies
  185. (
  186. @Currency varchar(3)
  187. ,@Name varchar(30)
  188. ,@Image varbinary(8000)
  189. ,@Symbol nvarchar(10)
  190. )
  191. as
  192. BEGIN
  193. set nocount on
  194.  
  195. update Currencies
  196. set Name = ISNULL(@Name,Name)
  197. ,[Image] = ISNULL(@Image,[Image])
  198. ,Symbol = ISNULL(@Symbol,Symbol)
  199. where Currency = @Currency
  200.  
  201. set nocount off
  202. END
  203.  
  204.  
  205. AccountClasses
  206. CREATE view dbo.AccountClassesOverview
  207. as
  208. select Name
  209. ,[Description]
  210. ,case when [Type] = 1 then 'Cash Account'
  211. when [Type] = 2 then 'Current Account'
  212. when [Type]=3 then 'Deposit Account'
  213. else 'Credit Account'
  214. end as [Type]
  215. from dbo.AccountClasses
  216.  
  217. GO
  218.  
  219. create procedure InsertAccountClasses
  220. (
  221. @Name varchar(30)
  222. ,@Description nvarchar(200)
  223. ,@Type int
  224. )
  225. AS
  226. begin
  227. set nocount on
  228.  
  229. if(@Type in (1,2,3,4))
  230. begin
  231. if exists(select * from AccountClasses where [Type] = @Type)
  232. begin
  233. Raiserror('Account class exists!',16,1)
  234. end
  235. else
  236. begin
  237. insert into AccountClasses(Name,[Description],[Type])
  238. values (@Name,@Description, @Type)
  239. end
  240. end
  241. else
  242. Raiserror('Invalid Type value!',16,1)
  243.  
  244. set nocount off
  245. end
  246.  
  247. GO
  248.  
  249. create procedure UpdateAccountClasses
  250. (
  251. @Id bigint
  252. ,@Name varchar(30)
  253. ,@Description nvarchar(200)
  254. ,@Type int
  255. )
  256. as
  257. BEGIN
  258. set nocount on
  259.  
  260. if(@Type in (1,2,3,4))
  261. begin
  262. if exists(select * from AccountClasses where [Type] = @Type and Id<>@Id)
  263. begin
  264. Raiserror('Account class exists!',16,1)
  265. end
  266. else
  267. begin
  268. update AccountClasses
  269. set Name = ISNULL(@Name,Name)
  270. ,[Description] = ISNULL(@Description,[Description])
  271. ,[Type] = ISNULL(@Type,[Type])
  272. where Id = @Id
  273. end
  274. end
  275. else
  276. Raiserror('Invalid Type value!',16,1)
  277.  
  278.  
  279.  
  280. set nocount off
  281. END
  282.  
  283. AccountTypes
  284. CREATE view dbo.AccountTypesOverview
  285. as
  286. select ac.Name as AccountClass, at.Name,at.[Description]
  287. from dbo.AccountTypes at
  288. left join dbo.AccountClasses ac on ac.Id = at.ClassId
  289.  
  290. GO
  291.  
  292. create procedure UpdateAccountTypes
  293. (
  294. @Id bigint
  295. ,@ClassId bigint = null
  296. ,@Description nvarchar(200)
  297. ,@Name varchar(30)
  298. )
  299. as
  300. BEGIN
  301. set nocount on
  302.  
  303. update AccountTypes
  304. set Name = ISNULL(@Name,Name)
  305. ,[Description] = ISNULL(@Description,[Description])
  306. ,ClassId = ISNULL(@ClassId,ClassId)
  307. where Id = @Id
  308.  
  309. set nocount off
  310. END
  311.  
  312. GO
  313.  
  314. create procedure InsertAccountTypes
  315. (
  316. @ClassId bigint = null
  317. ,@Description nvarchar(200)
  318. ,@Name varchar(30)
  319. )
  320. AS
  321. begin
  322. set nocount on
  323.  
  324. insert into AccountTypes(Name,[Description],ClassId)
  325. values (@Name,@Description, @ClassId)
  326.  
  327. set nocount off
  328. end
  329.  
  330.  
  331. Accounts
  332. CREATE view dbo.AccountsOverview
  333. as
  334. select at.Name as AccountType, a.AccountNumber, a.Currency, c.Name as Client,
  335. case when a.[Status] = 'A' then 'Active' when a.[Status] = 'C' then 'Closed' when a.[Status] = 'O' then 'Opened' else NULL end as [Status],
  336. a.Blocked, a.Balance, a.OverdraftLimit, a.AmountOnHold, a.AvailableBalance
  337. from dbo.Accounts a
  338. left join dbo.AccountTypes at on at.Id = a.TypeId
  339. left join dbo.Clients c on c.Id = a.ClientId
  340.  
  341. GO
  342.  
  343. create procedure InsertAccount
  344. (
  345. @TypeId bigint = null
  346. ,@AccountNumber varchar(30)
  347. ,@Currency varchar(3) = null
  348. ,@ClientId bigint = null
  349. ,@Status varchar(1)
  350. ,@Blocked bit
  351. ,@Balance decimal(19,2)
  352. ,@OverdraftLimit decimal(19,2)
  353. ,@AmountOnHold decimal(19,2)
  354. )
  355. AS
  356. begin
  357. set nocount on
  358.  
  359. if(isnull(@Status,'') in ('A','C','O') or @Status is null)
  360. begin
  361.  
  362. insert into Accounts (TypeId,AccountNumber,Currency,ClientId,[Status],Blocked,Balance,OverdraftLimit,AmountOnHold)
  363. values
  364. (@TypeId,@AccountNumber, @Currency,@ClientId,@Status,@Blocked,@Balance,@OverdraftLimit,@AmountOnHold)
  365. end
  366. else
  367. begin
  368. raiserror('Invalid Status!',16,1)
  369. end
  370.  
  371.  
  372. set nocount off
  373. end
  374.  
  375. GO
  376.  
  377. create procedure UpdateAccount
  378. (
  379. @Id bigint
  380. ,@TypeId bigint = null
  381. ,@AccountNumber varchar(30)
  382. ,@Currency varchar(3) = null
  383. ,@ClientId bigint = null
  384. ,@Status varchar(1)
  385. ,@Blocked bit
  386. ,@Balance decimal(19,2)
  387. ,@OverdraftLimit decimal(19,2)
  388. ,@AmountOnHold decimal(19,2)
  389. )
  390. as
  391. BEGIN
  392. set nocount on
  393.  
  394. if(isnull(@Status,'') in ('A','C','O') or @Status is null)
  395. begin
  396. update Accounts
  397. set TypeId = ISNULL(@Name,TypeId)
  398. ,AccountNumber = ISNULL(@Description,AccountNumber)
  399. ,Currency = ISNULL(@ClassId,Currency)
  400. ,ClientId = ISNULL(@Description,ClientId)
  401. ,[Status] = ISNULL(@ClassId,[Status])
  402. ,Blocked = ISNULL(@Description,Blocked)
  403. ,Balance = ISNULL(@ClassId,Balance)
  404. ,OverdraftLimit = ISNULL(@Description,OverdraftLimit)
  405. ,AmountOnHold = ISNULL(@ClassId,AmountOnHold)
  406. where Id = @Id
  407. end
  408. else
  409. begin
  410. raiserror('Invalid Status!',16,1)
  411. end
  412.  
  413.  
  414. set nocount off
  415. END
  416.  
  417. GO
  418.  
  419.  
  420. create proc CreateNewStatement
  421. (
  422. @AccountId bigint
  423. )
  424. AS
  425. BEGIN
  426. if exists (select * from PostingEntries where AccountId = @AccountId)
  427. begin
  428. declare @stmNo int
  429.  
  430. select @stmNo = isnull(StatementNo,0) from PostingEntries where AccountId = @AccountId
  431.  
  432. update PostingEntries
  433. set StatementNo = @stmNo+1
  434. where AccountId = @AccountId
  435.  
  436. select @stmNo
  437. end
  438. END
  439.  
  440. GO
  441.  
  442. create proc GetStatement
  443. (
  444. @AccountId bigint
  445. )
  446. AS
  447. BEGIN
  448. select t.[Description] as [Transaction], a.AccountNumber as Account,
  449. case when pe.DebitCredit = -1 then 'Debit' when DebitCredit = 1 then 'Credit' when DebitCredit = 0 then 'Nonfinancial' else NULL end as DebitCredit,
  450. ca.AccountNumber as ContraAccount, pe.Currency, pe.Amount, pe.StatementNo
  451. from PostingEntries pe
  452. left join Transactions t on t.Id = pe.TransactionId
  453. left join Accounts a on a.Id = pe.AccountId
  454. left join Accounts ca on ca.Id = pe.ContraAccountId
  455. where pe.AccountId = @AccountId
  456. END
  457.  
  458.  
  459.  
  460. TransactionTypes
  461. CREATE view dbo.TransactionTypesOverview
  462. as
  463. select tt.Name,tt.[Description], acd.Name as AccountClassDebit, acc.Name as AccountClassCredit, tt.ReflectsAmountOnHold, tt.HasFee
  464. from dbo.TransactionTypes tt
  465. left join dbo.AccountClasses acd on acd.Id = tt.AccountClassDebit
  466. left join dbo.AccountClasses acc on acc.Id = tt.AccountClassCredit
  467.  
  468. GO
  469.  
  470. create procedure UpdateTransactionTypes
  471. (
  472. @Id bigint
  473. ,@AccountClassDebit bigint = null
  474. ,@AccountClassCredit bigint = null
  475. ,@Description nvarchar(200)
  476. ,@Name varchar(30)
  477. ,@ReflectsAmountOnHold bit
  478. ,@HasFee bit
  479. )
  480. as
  481. BEGIN
  482. set nocount on
  483.  
  484. update TransactionTypes
  485. set Name = ISNULL(@Name,Name)
  486. ,[Description] = ISNULL(@Description,[Description])
  487. ,AccountClassDebit = ISNULL(@AccountClassDebit,AccountClassDebit)
  488. ,AccountClassCredit = ISNULL(@AccountClassCredit,AccountClassCredit)
  489. ,ReflectsAmountOnHold = ISNULL(@ReflectsAmountOnHold,ReflectsAmountOnHold)
  490. ,HasFee = ISNULL(@HasFee,HasFee)
  491. where Id = @Id
  492.  
  493. set nocount off
  494. END
  495.  
  496. GO
  497.  
  498. create procedure InsertTransactionTypes
  499. (
  500. @AccountClassDebit bigint = null
  501. ,@AccountClassCredit bigint = null
  502. ,@Description nvarchar(200)
  503. ,@Name varchar(30)
  504. ,@ReflectsAmountOnHold bit
  505. ,@HasFee bit
  506. )
  507. AS
  508. begin
  509. set nocount on
  510.  
  511. insert into TransactionTypes(Name,[Description],AccountClassDebit,AccountClassCredit,ReflectsAmountOnHold,HasFee)
  512. values (@Name,@Description, @AccountClassDebit,@AccountClassCredit,@ReflectsAmountOnHold,@HasFee)
  513.  
  514. set nocount off
  515. end
  516.  
  517.  
  518. Transactions
  519. CREATE view dbo.TransactionsOverview
  520. as
  521. select tt.Name as TransactionType, u.Name as [User], t.EntryDate, t.[Date], t.Currency, ad.AccountNumber as AccountDebit, ac.AccountNumber as AccountCredit,t.Amount, t.Fee, t.[Description],
  522. case when t.[Status] = 'A' then 'Active' when t.[Status] = 'C' then 'Cancelled' else NULL end as [Status]
  523. from dbo.Transactions t
  524. left join dbo.TransactionTypes tt on tt.Id = t.TransactionTypeId
  525. left join dbo.Users u on u.Id = t.UserId
  526. left join dbo.Accounts ad on ad.Id = t.AccountIdDebit
  527. left join dbo.Accounts ac on ac.Id = t.AccountIdDebit
  528.  
  529. GO
  530.  
  531. create procedure UpdateTransactions
  532. (
  533. @Id bigint
  534. ,@TransactionTypeId bigint = NULL
  535. ,@UserId bigint = NULL
  536. ,@EntryDate date
  537. ,@Date date
  538. ,@Currency varchar(3) = NULL
  539. ,@AccountIdDebit bigint = NULL
  540. ,@AccountIdCredit bigint = NULL
  541. ,@Fee decimal(19,2)
  542. ,@Amount decimal(19,2)
  543. ,@Description nvarchar(200)
  544. ,@Status varchar(1)
  545. )
  546. as
  547. BEGIN
  548. set nocount on
  549.  
  550. if(@Status is null or @Status in ('A', 'C'))
  551. begin
  552. update Transactions
  553. set TransactionTypeId = ISNULL(@TransactionTypeId,TransactionTypeId)
  554. ,UserId = ISNULL(@UserId,UserId)
  555. ,EntryDate = ISNULL(@EntryDate,EntryDate)
  556. ,[Date] = ISNULL(@Date,[Date])
  557. ,Currency = ISNULL(@Currency,Currency)
  558. ,AccountIdDebit = ISNULL(@AccountIdDebit,AccountIdDebit)
  559. ,AccountIdCredit = ISNULL(@AccountIdCredit,AccountIdCredit)
  560. ,Fee = ISNULL(@Fee,Fee)
  561. ,Amount = ISNULL(@Amount,Amount)
  562. ,[Description] = ISNULL(@Description,[Description])
  563. ,[Status] = ISNULL(@Status,[Status])
  564. where Id = @Id
  565. end
  566. else
  567. begin
  568. raiserror('Invalid status!',16,1)
  569. end
  570.  
  571. set nocount off
  572. END
  573.  
  574. GO
  575.  
  576. create procedure InsertTransactions
  577. (
  578. @TransactionTypeId bigint = NULL
  579. ,@UserId bigint = NULL
  580. ,@EntryDate date
  581. ,@Date date
  582. ,@Currency varchar(3) = NULL
  583. ,@AccountIdDebit bigint = NULL
  584. ,@AccountIdCredit bigint = NULL
  585. ,@Fee decimal(19,2)
  586. ,@Amount decimal(19,2)
  587. ,@Description nvarchar(200)
  588. ,@Status varchar(1)
  589. )
  590. AS
  591. begin
  592. set nocount on
  593.  
  594. if(@Status is null or @Status in ('A', 'C'))
  595. begin
  596. insert into Transactions(TransactionTypeId,UserId,EntryDate,[Date],Currency, AccountIdDebit, AccountIdCredit, Fee, Amount, [Description], [Status])
  597. values (@TransactionTypeId,@UserId, @EntryDate,@Date, @Currency,@AccountIdDebit,@AccountIdCredit,@Fee,@Amount,@Description,@Status)
  598. end
  599. else
  600. begin
  601. raiserror('Invalid status!',16,1)
  602. end
  603.  
  604. set nocount off
  605. end
  606.  
  607.  
  608.  
  609. PostingEntries
  610.  
  611. CREATE view dbo.PostingEntriesOverview
  612. as
  613. select t.[Description] as [Transaction], a.AccountNumber as Account,
  614. case when pe.DebitCredit = -1 then 'Debit' when DebitCredit = 1 then 'Credit' when DebitCredit = 0 then 'Nonfinancial' else NULL end as DebitCredit,
  615. ca.AccountNumber as ContraAccount, pe.Currency, pe.Amount, pe.StatementNo
  616. from dbo.PostingEntries pe
  617. left join Transactions t on t.Id = pe.TransactionId
  618. left join Accounts a on a.Id = pe.AccountId
  619. left join Accounts ca on ca.Id = pe.ContraAccountId
  620.  
  621. GO
  622.  
  623. create procedure InsertPostingEntries
  624. (
  625. @TransactionId bigint = NULL
  626. ,@AccountId bigint = NULL
  627. ,@DebitCredit smallint
  628. ,@ContraAccountId bigint = NULL
  629. ,@Currency varchar(3) = NULL
  630. ,@Amount decimal(19,2)
  631. ,@StatementNo int
  632. )
  633. AS
  634. begin
  635. set nocount on
  636.  
  637. if(@DebitCredit is null or @DebitCredit in (0,1,-1))
  638. begin
  639. insert into PostingEntries(TransactionId,AccountId,DebitCredit,ContraAccountId,Currency, Amount, StatementNo)
  640. values (@TransactionId,@AccountId, @DebitCredit,@ContraAccountId, @Currency,@Amount,@StatementNo)
  641. end
  642. else
  643. begin
  644. raiserror('Invalid DebitCredit!',16,1)
  645. end
  646.  
  647. set nocount off
  648. end
  649.  
  650. GO
  651.  
  652. create procedure UpdatePostingEntries
  653. (
  654. @Id bigint
  655. ,@TransactionId bigint = NULL
  656. ,@AccountId bigint = NULL
  657. ,@DebitCredit smallint
  658. ,@ContraAccountId bigint = NULL
  659. ,@Currency varchar(3) = NULL
  660. ,@Amount decimal(19,2)
  661. ,@StatementNo int
  662. )
  663. as
  664. BEGIN
  665. set nocount on
  666.  
  667. if(@DebitCredit is null or @DebitCredit in (0,1,-1))
  668. begin
  669. update PostingEntries
  670. set TransactionId = ISNULL(@TransactionId,TransactionId)
  671. ,AccountId = ISNULL(@AccountId,AccountId)
  672. ,DebitCredit = ISNULL(@DebitCredit,DebitCredit)
  673. ,ContraAccountId = ISNULL(@ContraAccountId,ContraAccountId)
  674. ,Currency = ISNULL(@Currency,Currency)
  675. ,Amount = ISNULL(@Amount,Amount)
  676. ,StatementNo = ISNULL(@StatementNo,StatementNo)
  677. where Id = @Id
  678. end
  679. else
  680. begin
  681. raiserror('Invalid status!',16,1)
  682. end
  683.  
  684. set nocount off
  685. END
  686.  
  687.  
  688.  
  689.  
  690. 1. PostingEntries, IUD, Update Balance in Accounts table
  691.  
  692. create trigger Insert_PostingEntriesAccount
  693. on PostingEntries
  694. after insert
  695. as
  696. begin
  697. update a
  698. set a.Balance = ISNULL(a.Balance,0)+ (select sum(DebitCredit*Amount) from inserted where Id = i.Id)
  699. from Accounts a
  700. join inserted i on i.AccountId = a.Id
  701. end
  702.  
  703. GO
  704.  
  705. create trigger Update_PostingEntriesAccount
  706. on PostingEntries
  707. after update
  708. as
  709. begin
  710. if update(Amount)
  711. begin
  712. update a
  713. set a.Balance = ISNULL(a.Balance,0)+ (select sum(DebitCredit*Amount) from inserted where Id = i.Id) - (select sum(DebitCredit*Amount) from deleted where Id = d.Id)
  714. from Accounts a
  715. join inserted i on i.AccountId = a.Id
  716. join deleted d on d.AccountId = a.Id
  717. end
  718. end
  719.  
  720. GO
  721.  
  722. create trigger Delete_PostingEntriesAccount
  723. on PostingEntries
  724. after delete
  725. as
  726. begin
  727. update a
  728. set a.Balance = ISNULL(a.Balance,0) - (select sum(DebitCredit*Amount) from deleted where Id = d.Id) from Accounts a
  729. join deleted d on d.AccountId = a.Id
  730. end
  731.  
  732.  
  733.  
  734.  
  735.  
  736.  
  737.  
  738.  
  739.  
  740.  
  741.  
  742.  
  743.  
  744.  
  745.  
  746.  
  747.  
  748.  
  749.  
  750.  
  751. 2. Posting Entries, IUD, Update AmountOnHold in Accounts table
  752.  
  753. create trigger Insert_PostingEntriesAccountAmountOnHold
  754. on PostingEntries
  755. after insert
  756. as
  757. begin
  758. update a
  759. set a.AmountOnHold = ISNULL(a.AmountOnHold,0)+ (select sum(Amount) from inserted where Id = i.Id and i.DebitCredit = 0)
  760. from Accounts a
  761. join inserted i on i.AccountId = a.Id
  762. where i.DebitCredit = 0
  763. end
  764.  
  765. GO
  766.  
  767. create trigger Update_PostingEntriesAccountAmountOnHold
  768. on PostingEntries
  769. after update
  770. as
  771. begin
  772. if update(Amount)
  773. begin
  774. update a
  775. set a.AmountOnHold = ISNULL(a.AmountOnHold,0)+ (select sum(Amount) from inserted where Id = i.Id and i.DebitCredit = 0) - (select sum(Amount) from deleted where Id = d.Id and d.DebitCredit = 0)
  776. from Accounts a
  777. join inserted i on i.AccountId = a.Id
  778. join deleted d on d.AccountId = a.Id
  779. where i.DebitCredit = 0 and d.DebitCredit = 0
  780. end
  781. end
  782.  
  783. GO
  784.  
  785. create trigger Delete_PostingEntriesAccountAmountOnHold
  786. on PostingEntries
  787. after delete
  788. as
  789. begin
  790. update a
  791. set a.AmountOnHold = ISNULL(a.AmountOnHold,0) - (select sum(Amount) from deleted where Id = d.Id and d.DebitCredit = 0)
  792. from Accounts a
  793. join deleted d on d.AccountId = a.Id
  794. where d.DebitCredit = 0
  795. end
  796.  
  797.  
  798.  
  799.  
  800.  
  801.  
  802.  
  803.  
  804.  
  805.  
  806.  
  807.  
  808.  
  809.  
  810.  
  811.  
  812.  
  813.  
  814.  
  815. 3. Transactions, IUD, Generate entries in PostingEntries
  816.  
  817. create trigger Insert_PostingEntries
  818. on Transactions
  819. after insert
  820. as
  821. begin
  822.  
  823. select * into #TransactionTmp from inserted
  824.  
  825. alter table #TransactionTmp add ReflectsAmountOnHold bit
  826.  
  827. update t
  828. set t.ReflectsAmountOnHold = tt.ReflectsAmountOnHold
  829. from #TransactionTmp t
  830. join TransactionTypes tt on tt.id = t.TransactionTypeId
  831.  
  832. insert into PostingEntries(TransactionId,AccountId,DebitCredit,ContraAccountId,Currency,Amount,StatementNo)
  833. select Id,AccountIdDebit,0,AccountIdCredit,Currency,Amount,null from #temp
  834. where ReflectsAmountOnHold = 1
  835.  
  836. insert into PostingEntries (TransactionId,AccountId,DebitCredit,ContraAccountId,Currency,Amount,StatementNo)
  837. select Id,AccountIdDebit,-1,AccountIdCredit,Currency,Amount,null from #temp
  838. where ReflectsAmountOnHold = 0
  839.  
  840. insert into PostingEntries (TransactionId,AccountId,DebitCredit,ContraAccountId,Currency,Amount,StatementNo)
  841. select Id,AccountIdCredit,1,AccountIdDebit,Currency,Amount,null from #temp
  842. where ReflectsAmountOnHold = 0
  843.  
  844. end
  845.  
  846. GO
  847.  
  848. create trigger Update_PostingEntries
  849. on Transactions
  850. after update
  851. as
  852. begin
  853.  
  854. if exists(select * from deleted d join inserted i on i.Id = d.Id and i.Amount<>d.Amount)
  855. begin
  856.  
  857. update pe
  858. set pe.Amount = t.Amount
  859. from inserted t
  860. join PostingEntries pe on pe.TransactionId = t.Id
  861.  
  862. end
  863.  
  864. end
  865.  
  866. GO
  867.  
  868. create trigger Delete_PostingEntries
  869. on Transactions
  870. after delete
  871. as
  872. begin
  873. delete pe
  874. from deleted d
  875. join PostingEntries pe on pe.TransactionId = d.Id
  876. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement