knz
Starting Member
1 Post |
Posted - 2008-04-01 : 07:10:09
|
My aim is to do something like what I have explained below and I was planning on building this logic at the Database level only rather than on the frontend code. There are certain allocations(transactions) that happen on a periodic basis and I am storing these transactions in the PurchaseTranMaster and PurchaseTranDetail table. These transactions are categorized as 'Main' type and the amount could be allocated for one or many categories in a single transaction. Below is how it will be saved in the 2 tablesPurchaseTranMasterTranID TranDate TranType1 14-March-2008 Main 2 17-March-2008 Main3 19-March-2008 MainPurchaseTranDetailTranID Amount Category Debit_TranId1 1000 A 1 1000 B 2 2000 B3 300 A3 400 CNow what happens is users of my application can make purchases under all these categories only until the Balance under these categories is > than purchase amount. The Balance is calculated as sum of all transactions. It means that w.r.t the above data the balances for each category is(this is not stored in the database)A 1300B 3000C 400So lets say a user does make a purchase(Trantype is 'SUB') of 300 under A and 400 under B in a single transaction. The data would then be stored in the tables as PurchaseTranMasterTranID TranDate TranType1 14-March-2008 Main 2 17-March-2008 Main3 19-March-2008 Main4 20-March-2008 SUBPurchaseTranDetailTranID Amount Category Debit_TranId1 1000 A 1 1000 B 2 2000 B3 300 A3 400 C4 300 A 14 400 B 1In the PurchaseTranDetail the Debit_TranId value means that the amount has been marked against the TranID 1. This TranId is not handpicked by the user and the system should allocate it accordingly based on the amount available for a particualar category for a Main Transaction. It means that before TranId 4 was saved in the database then the system would first check whether the Total available balance for A >=300 and B>=400 (in our example above it is 1300 and 3000 resp)Then if the Balance is > than the puchase amount then the allocation would be done by the system and this would be done against the TranID whose TranDate was the earliest, so thats why the Debit_TranId column has 1 as TranId 1 was the earliest.so logically now the balance for the categories would be (this is not saved in the database)A 1000B 2600C 400So next time again when a user would make a purchase(transaction) under A for 800 and under B for 1000 then if the balance is greater than the purchase amount(which in this case it is) the allocation would happen according to the earliest TranId and this time amount would be partly marked against TranId 1 , TranID 2 and TranID 3. The data would look like thisPurchaseTranMasterTranID TranDate TranType1 14-March-2008 Main 2 17-March-2008 Main3 19-March-2008 Main4 20-March-2008 SUB5 21-March-2008 SUBTranID Amount Category Debit_TranId1 1000 A 1 1000 B 2 2000 B3 300 A3 400 C4 300 A 14 400 B 15 700 A 15 100 A 35 600 B 15 400 B 2I need to do the above taking into consideration that there could be multiple users making purchases(concurrency). Also I was building my logic on doing the above whether to use cursors or loops. I just need to know how do I write my stored procedure and what would be the most efficeint way of doing the above.The design for creating the above sample tables is below/*CREATE TABLE PurchaseTranMaster ( TranID int IDENTITY(1,1) PRIMARY KEY CLUSTERED, TranDate Datetime, TranType varchar(30) )CREATE TABLE PurchaseTranDetail( TranID int,Amount int, Category Varchar(20), Debit_TranId int)insert into PurchaseTranMaster values(convert(datetime,' 14-March-2008',103),'Main')insert into PurchaseTranMaster values(convert(datetime,' 17-March-2008',103),'Main')insert into PurchaseTranMaster values(convert(datetime,' 19-March-2008',103),'Main')insert into PurchaseTranMaster values(convert(datetime,' 20-March-2008',103),'SUB')insert into PurchaseTranMaster values(convert(datetime,' 21-March-2008',103),'SUB')insert into PurchaseTranDetail values(1,1000,'A',0)insert into PurchaseTranDetail values(1,1000,'B',0)insert into PurchaseTranDetail values(2,2000,'B',0)insert into PurchaseTranDetail values(3,300,'A',0)insert into PurchaseTranDetail values(3,400,'C',0)insert into PurchaseTranDetail values(4,300,'A',1)insert into PurchaseTranDetail values(4,400,'B',1)insert into PurchaseTranDetail values(5,700,'A',1)insert into PurchaseTranDetail values(5,100,'A',3)insert into PurchaseTranDetail values(5,600,'B',1)insert into PurchaseTranDetail values(5,400,'B',2)*/ |
|