This is a SQL script that creates or alters a stored procedure named IndividualTariffStatusSet in the Clients schema of the forex_test database. The stored procedure takes four input parameters:
@IndividualTariffId: an integer that represents the ID of the individual tariff plan whose status needs to be updated.
@SourceId: a tinyint that represents the source of the status change request.
@StatusId: a tinyint that represents the new status to be set for the individual tariff plan.
@Debug: an optional bit parameter that determines whether debugging information should be displayed. If set to 1, the stored procedure will print out some additional information.
The stored procedure begins by setting the transaction isolation level to READ UNCOMMITTED and turning on the NOCOUNT option. This helps to improve performance by reducing the amount of information sent back to the client and preventing phantom reads.
Next, the stored procedure declares several local variables that will be used throughout the procedure. These include variables to store the current status and source codes, the new name for the tariff plan (if applicable), the initiator of the status change request, the current date and time, and a flag to determine whether a new tax plan should be created.
The stored procedure then checks whether the current status of the individual tariff plan is 1 (Terminal) and whether the main tariff plan associated with the client is expired. If either of these conditions is true, the stored procedure will not allow the status to be changed to 5 (Active) and will raise an error.
If the status change request is valid, the stored procedure updates the IndividualTariffStatus table with the new status and source codes, as well as the new name for the tariff plan (if applicable). It also updates the IndividualTariff table to set the StatusId column to the new status and the LastChangeDate column to the current date and time.
Finally, the stored procedure performs some additional cleanup tasks, such as recalculating the tariff plan's taxes and sending out notifications to the client and/or initiator of the status change request.
Overall, this stored procedure is used to update the status of individual tariff plans in the forex_test database. It includes several checks and validations to ensure that the status change request is valid and that the tariff plan's data is updated correctly.