Lesson 10. Enumerations
Estimated duration of the lesson is 30 minutes.
CONTENT:
- Understanding the Enumeration
Adding an Enumeration
Connecting Products to the Values of ProductType Enumeration
Recording Expenses for Materials Only
Quiz
Up to this point we have ignored the fact that we have no sure way of determining what a specific item in the Products catalog is: a material or a service. The fact that we have arranged all catalog items into certain groups is not a reliable basis for such a decision, since groups can be deleted or renamed, items can be grouped by another principle, etc.
Therefore, we need some kind of a marker that will allow us to know for sure whether a catalog item belongs to materials or services, irrespective of changes to the catalogs hierarchical structure.
During this lesson we will create a special attribute for the Products catalog using a data type that will represent a configuration object that we have not encountered yet, an Enumeration. As we move forward, this will allow us to easily classify catalog items of the Products catalog as services or materials.
Additionally we will update the procedure for posting Services Rendered documents and will demonstrate how to work with an enumeration using 1C:Enterprise script tools.
Understanding the Enumeration
An Enumeration configuration object is intended to declare a structure for storage of constant value sets that cannot be modified in the process of configuration operation. On the basis of an Enumeration object, the platform will create a database table that will store a given set of constant values.
In practice, this object can correspond to such things as an enumeration of options for calculating prices (i.e. "VAT included", "without VAT"). The assortment of all possible values for an enumeration is defined during configuration development and a user cannot add, delete or modify the values.
This leads us to an important feature of enumerations: the enumerated values are significant to the configuration, and program algorithms may rely on them.
LEARN MORE!
For details on the structure of the 1C:Enterprise script objects intended to work with enumerations, see "Developer's Quick Reference. Enumerations".
Adding an Enumeration
In the Designer Mode
Open the Designer and start by creating a new Enumeration configuration object named ProductTypes.
On the Data tab, we will add two values for the enumeration: Material and Service.
To do so, click Add over the list of enumeration values (fig. 10.1).
Fig. 10.1. Data of the ProductTypes enumeration
Connecting Products to the Values of ProductType Enumeration
In order to connect products to enumeration values, use the following steps:
- In the Designer mode create an attribute for the Products catalog that will store the enumeration values;
- In the 1C:Enterprise mode apply the required values of this attribute for all the items in the Products catalog.
In the Designer Mode
Add a new attribute named ProductType to the Products catalog and assign it the type EnumRef.ProductTypes.
To do so, open the editor for the Products Catalog configuration object and on the Data tab click Add over the list of catalog attributes (fig. 10.2).
In the 1C:Enterprise Mode
Next run 1C:Enterprise in the debugging mode.
A warning message will be displayed informing you that the enumeration has not been included into any subsystem. Ignore this message and confirm the configuration modifications.
In the 1C:Enterprise mode select the required values of the Product Type attribute for all the items in the Products catalog (fig. 10.3).
Now we will discuss the possible uses for the new data obtained using the ProductTypes enumeration.
Recording Expenses for Materials Only
If you recall, during our sixth lesson when we created register records for the ServicesRendered document for the BalanceOfMaterials accumulation register, we mentioned that these records were not entirely correct, since the register will include the records about services rendered in addition to the records about expended materials (listing 10.1).
Listing 10.1. Posting() Procedure of the ServicesRendered Document
Procedure Posting(Cancel, Mode) //{{__REGISTER_REGISTERRECORDS_WIZARD // This fragment was built by the wizard. // Warning! All manually made changes will be lost next time you use the wizard. RegisterRecords.BalanceOfMaterials.Write = True; For Each CurRowProductList In ProductList Do // register BalanceOfMaterials Expense Record = RegisterRecords.BalanceOfMaterials.Add(); Record.RecordType = AccumulationRecordType.Expense; Record.Period = Date; Record.Material = CurRowProductList.Product; Record.Warehouse = Warehouse; Record.Quantity = CurRowProductList.Quantity; EndDo; //}}__REGISTER_REGISTERRECORDS_WIZARD EndProcedure
Now we adjust the document so that only records on expenses of materials will end up in the register.
To do so, we will begin in the Designer mode where we will modify the document posting procedure so that the register only includes the records about products that are materials and then proceed in the 1C:Enterprise mode to repost all the Services Rendered documents so that the data in the register are modified in compliance with the new document posting algorithm.
This enhancement will not be optimal in terms of performance but it will allow us to get the required data from the BalanceOfMaterials register.
NOTE
We will discuss a more efficient means of posting this document after we study the lesson covering how queries are handled in the 1C:Enterprise (lesson No. 14).
In the Designer Mode
We will refine the document register records by excluding any tabular section rows that contain services.
To do so, open the ServicesRendered document module in the Designer (document context menu - Open object module) and add this condition to the handler of the Posting event.
The code should be added to the beginning of the document tabular section search loop after the line For Each CurRowProductList In ProductList Do.
This results in the following appearance of the Posting procedure (listing 10.2):
Listing 10.2. Register Records of ServicesRendered Document
Procedure Posting(Cancel, Mode) //{{__REGISTER_REGISTERRECORDS_WIZARD // This fragment was built by the wizard. // Warning! All manually made changes will be lost next time you use the wizard. RegisterRecords.BalanceOfMaterials.Write = True; For Each CurRowProductList In ProductList Do If CurRowProductList.Product.ProductType = Enums.ProductTypes.Material Then // register BalanceOfMaterials Expense Record = RegisterRecords.BalanceOfMaterials.Add(); Record.RecordType = AccumulationRecordType.Expense; Record.Period = Date; Record.Material = CurRowProductList.Product; Record.Warehouse = Warehouse; Record.Quantity = CurRowProductList.Quantity; EndIf; EndDo; //}}__REGISTER_REGISTERRECORDS_WIZARD EndProcedure
The inserted code excludes from the loop all the document tabular section rows where a product is not a material. Now we will discuss this condition in more details.
On every loop step the CurRowProductList variable contains the data of the current row of the ProductList tabular section.
If we separate a column name Product with a dot (CurRowPro- ductList.Product), we call for a link to the Products item that is contained in this row of the tabular section.
Next if we separate ProductType with a dot (CurRowProductList.Pro- duct.ProductType), we call for the ProductType attribute of this item of the Products catalog.
Using the comparison operator (=) we compare the obtained value with the value Material of the ProductType enumeration (Enums.ProductTy- pes.Material).
If the values match, the operators of this loop are executed. If no, we proceed to the next loop iteration, i.e. to the next row of the tabular section.
In the 1C:Enterprise Mode
Launch 1C:Enterprise in the debugging mode and test the posting procedure for the Services Rendered document.
Open the list of documents using the Services Rendered command in the navigation panel of the Services Rendered section.
Open the document named Services Rendered No. 1 and modify it as follows:
- Remove the row containing Philips Transistor from the tabular section,
- Add a service named Water hookup,
- Add a material Tubing, rubber (fig. 10.4).
Fig. 10.4. Modified Document Services Rendered No. 1
Note that prices are populated automatically using the values from the Prices information register.
Click Post in the document form command bar.
Next use the Balance Of Materials command in the form navigation panel in order to navigate to the records of the Balance Of Materials register related to this document (fig. 10.5).
Fig. 10.5. Records of the Balance of Materials Register
As you can see, the records for the Balance Of Materials register only include the rows containing the Materials. The record describing the Water hookup service has not been included in the register records.
Quiz
- What are the uses of the Enumeration configuration object?
- How does one create a new enumeration?
- How does one use enumeration to declare that catalog items belong to a specific logical group?
- How does one reference an enumeration value using 1C:Enterprise script?