开发者

Implementing nightly process using ASP.Net , C# and Sql Server

开发者 https://www.devze.com 2023-02-18 15:37 出处:网络
I need to implement functionality of nightly process in my applications billing module. See the image of the tables involved.

I need to implement functionality of nightly process in my applications billing module. See the image of the tables involved.

Implementing nightly process using ASP.Net , C# and Sql Server

Let me explain tables by their numbers in red. Basically this is an house allotment application database where the tenants can choose the billing plans and addons in it. Like we have inour mobile service.

Step: 1. This is the first most table, tbl_MSTBilling, where the enduser will create a plan with some addon. Addon is known as transaction type here. There billing plan general details are saved here in the same details where as the addons will get saved into table no 2 with reference of the billing plan, that is the billing plan id. Also here in table 1 we have two entities BillingDayOfMonth as integer and LatefeeAppliedDayOfMonth as int. Which takes input in between 1-31 (days of months).

Step 2: Now at the time of agreeement, tenant opts some billing plan that suits him and that billing plan is then attached with the agreement. In table 3 , we have agreement id and billingplanid. Now we agreement is generated the addons of billing plan from table 2 will get inserted into table 4. Here Agreement and BillingPlan has 1:1 relationship.

Sterp3: Now the scenario of nightly process come, which will occur every night. First it will check the Billing day of month in table 3, It will have to pull out the agreements id of all user having their billing day of month as today. Now on the basis of all agreements the rows from table 4 will get populated into table 5. Here TransactionDate will be null for each row and Statementdate will become current date. TransactionType of 4 is as same as TransactionTypeId of 5. Say we got 100 rows of 20 different AgreementId's.Also in the mean while we have to check in table 6 whether there is any bill due for agreement on the basis of includedinstatement, which is bit field. If this is included then it will get set to true.

Step 4: Taking table 5 and 7 a single row will get inserted to table 7 which will be the original bill.

So this is my entire query. I got stucked at step 3, where nightly process starts. I hope i am clear to you. Any queries please put it.

My Current Sql Script

    BEGIN TRY

        BEGIN TRANSACTION
            DECLARE @Today AS INT
            --
            SET @Today =12-- (SELECT DATEPART(DAY,GETDATE()))

            -- First of all check for the agreements 
            INSERT INTO tbl_AccountTranscation (AgreementID,TranscationDate,TranscationTypeID,Amount,StatementDate)
            SELECT AgreementId, NULL, TransactionType,Amount,GETDATE() FROM tbl_AgreementTransaction
            WHERE AgreementId IN (SELECT AgreementId From tbl_MSTAgreement WHERE BillingDayOfMonth = @Today)


            Declare @Count AS INT
            SET @Count = (Select COUNT(*) FROM tbl_BillDue WHERE IncludedinStatement=0 AND AgreementID IN (SELECT AgreementId 
                                                                                                           From tbl_MSTAgreement 
                                                                                                           WHERE BillingDayOfMonth = @Today) )
            IF @Count >0 
                BEGIN
                    INSERT INTO tbl_AccountTranscation (AgreementID,TranscationDate,TranscationTypeID,Amount,StatementDate)
                    Select  A开发者_StackOverflow社区greementId,NULL,-1,ISNULL(((ISNULL(TotalDueAmount,0)+ ISNULL(LateFeeAmount,0)) - ISNULL(AdjustmentAmount,0)),0) as Amount, 
                            GETDATE() as StatementDate
                    From tbl_BillDue
                    Where IncludedinStatement = 0 and AgreementID IN (  SELECT AgreementId 
                                                                        From tbl_MSTAgreement 
                                                                        WHERE BillingDayOfMonth = @Today
                                                                    )
                END                 
            --INSERT INTO tbl_MSTBill
            --(AgreementId,OutStandingPayment,BillDate,BillDueDate,PaymentDate,AmountPaid)
            --SELECT AgreementID, SUM(Amount) as Amount,StatementDate,NULL,NULL,NULL  
            --FROM  tbl_AccountTranscation WHERE AgreementId IN ( SELECT AgreementId 
            --                                                  From tbl_MSTAgreement 
            --                                                  WHERE BillingDayOfMonth = 12)

            --GROUP BY AgreementID, Amount,StatementDate

            SELECT     tbl_MSTAgreement.AgreementID, tbl_MSTAgreement.LateFeeApplyDayOfMonth, tbl_AccountTranscation.StatementDate, 
                     SUM(tbl_AccountTranscation.Amount) as Amount
FROM         tbl_AccountTranscation INNER JOIN
                      tbl_MSTAgreement ON tbl_AccountTranscation.AgreementID = tbl_MSTAgreement.AgreementID
                      GROUP BY tbl_MSTAgreement.AgreementID,Amount,tbl_MSTAgreement.LateFeeApplyDayOfMonth, tbl_AccountTranscation.StatementDate
        COMMIT TRANSACTION
    END TRY
    BEGIN CATCH
        ROLLBACK TRANSACTION

    END CATCH
END


If it's the scheduling which is the hard part for you, you can either use either a scheduled job with the SQL Agent to do your nightly work in a stored procedure, or you can write a console application which does the same which you execute via Windows built-in scheduled tasks.

The code itself shouldn't be that hard from browsing your requirements.

Good luck :)

0

精彩评论

暂无评论...
验证码 换一张
取 消