So you checked that once you change the quantity in any row of a Goods receipt document, the platform automatically recalculates the total.
You also need the Price field to behave in a similar manner. Taking a look ahead though, you will see that similar automatic calculation of Total fields might come in handy for other documents as well.
Therefore, it is better to move the calculation of totals to some universally accessible location, so that various documents with similar attributes in their tabular sections will be able to use the algorithm.
The Common module configuration objects serve for defining these universally accessible locations. You can add them in the Common / Common modules branch of the configuration object tree. The procedures and functions stored in these modules are accessible from all the configuration objects.
So you will create a common module and move the total calculation procedure to this module. And in the document you will simply call that procedure from the common module.
In Designer mode
Let us add a Common module configuration object.
- In the configuration object tree, expand the Common branch by clicking the icon to the left of the branch name.
- Right-click the Common modules branch and click the Add button in the command bar of the configuration window (fig. 4.24).
Fig. 4.24. Creating a common module in the configuration object tree
This opens the window where you can enter the module text and the module property palette.
- In the property palette, name the module DocumentProcessing, select the Client (managed application) check box, and clear the Server check box.
It means that instances of this module will be compiled in the thin client context and in the web client context (fig. 4.25).
Fig. 4.25. Common module properties
- Add the following script to the module (listing 4.2).
Listing 4.2. Procedure CalculateTotal()
Procedure CalculateTotal (TabularSectionRow) Export TabularSectionRow.Total = TabularSectionRow.Quantity * TabularSectionRow.Price; EndProcedureNow let us examine this script. The TabularSectionRow variable that you defined in the OnChange event handler of the Quantity field is passed to the CalculateTotal() procedure. This variable stores data of the tabular section row of the Goods receipt document that is being edited.
You can use this variable to access tabular section column data and calculate the total by multiplying the price and quantity.
The Export keyword in the procedure title indicates that the procedure can be accessed from other applied solution modules.
- Change the handler script in the form module (listing 4.3).
Listing 4.3. Procedure MaterialsQuantityOnChange()
&AtClient Procedure MaterialsQuantityOnChange(Item) TabularSectionRow = Items.Materials.CurrentData; DocumentProcessing.CalculateTotal(TabularSectionRow); EndProcedureYou can see that the first line of the procedure remains unchanged. And in the second line instead of directly calculating the total the CalculateTotal() procedure from the DocumentProcessing common module is called. The current tabular section row is passed to the procedure as a parameter.
- Start 1C:Enterprise in the debug mode and ensure that nothing has changed from the user perspective.
Finally, let us implement a similar handler for the Price field. Since you have already coded the required procedure in the form module, you might want to reuse it with a different form control. However, the 1C Company configuration development standards do not permit that approach.
Learn more! According to the 1C Company development standards, each event must have its own handler. If identical actions are to be performed in response to changes to different controls (for example, in response to clicking any of the several buttons), these should be handled as follows:
- A dedicated procedure or function that performs the required actions is created.
- For each control, a separate handler with a default name is created.
- The procedure or function is called from each handler.
- Add the OnChange event with the following script to the Price field (listing 4.4).
Listing 4.4. Procedure MaterialsPriceOnChange()
&AtClient Procedure MaterialsPriceOnChange(Item) TabularSectionRow = Items.Materials.CurrentData; DocumentProcessing.CalculateTotal(TabularSectionRow); EndProcedure
In 1C:Enterpise mode
Start 1C:Enterprise in the debug mode and ensure that the tabular section row totals in Goods receipt documents are recalculated in response to changes in both quantity and price.