Fill the Default Size and Color When a Committed Document Is Edited
This Groovy script repairs the size and color on the lines of a document that has already been committed once. When such a document is saved again, a line whose size or color is empty, or is not one the item has, gets the item's default; an item that does not use sizes or colors has them cleared.
Customer-specific example
This script was written for one customer's workflow and has one built-in rule you cannot change through parameters: it does nothing on a document's first save. It acts only when a document that has been committed before is saved again. Adapt the script if you need it on new documents too.
| Class name | EAFillDefaultSizeAndColor |
| Script | EAFillDefaultSizeAndColor.groovy |
| Runs on | Any supply chain document with lines. Target action Pre Update Calculated Fields. |
| Uses | No parameters. Leave Parameter 1 empty. |
| Shipped as | Source to paste. It is not built into the system. |
When to use it
Suppose a business changes its items' size and color grids after documents have already been committed: some sizes are renamed, and some items stop using colors altogether. Opening one of those documents and saving it again then trips over values the items no longer have. With this script, re-saving such a document brings each line back in line with its item.
How it works
If the document has never been committed, the script stops at once. Otherwise, for each line with an item:
- Size. When the item has Has Size on and the line's size is empty or is not one of the item's sizes, the line takes the size of the first row in the item's Sizes and Colors grid that has Default ticked and a size filled in. When the item has Has Size off, the line's size is cleared.
- Color. The same rule, using Has Colors and the first Default row that has a color.
A line whose size or color is valid for the item keeps it. If no row of the item is ticked as Default, a wrong size or color is left as it is. The script never refuses the save.
Setting it up
- On each item with sizes or colors (Inventory → Master Files → Item), tick Default on the row of the Sizes and Colors grid that should be used.
- Open Administration → Display Customization → Entity Flow and create a flow whose target type is the document to cover.
- In the Details grid, add a line with Target Action set to Pre Update Calculated Fields, and paste the whole script into its Groovy Script column. Leave Class Name empty: it is filled in when you save.
- Leave all parameters empty, and save the flow.
Example
Item TSHIRT-01 has the rows S / RED and M / BLUE in its Sizes and Colors grid, and M / BLUE is ticked as Default. A committed document has a TSHIRT-01 line with size XL and no color. When the document is saved again, the line becomes size M and color BLUE. A line for an item without sizes or colors ends up with both cleared.
The same TSHIRT-01 line on a brand-new document is saved exactly as typed.
The script
package com.namasoft.modules.supplychain.domain.utils.plugnplay.groovy
import com.namasoft.common.utilities.ObjectChecker
import com.namasoft.infra.domainbase.datafields.BooleanDF
import com.namasoft.infra.domainbase.datafields.LongTextDF
import com.namasoft.infra.domainbase.entity.base.EntityAction
import com.namasoft.infra.domainbase.util.Result
import com.namasoft.modules.supplychain.domain.details.BasicSCDocumentLine
import com.namasoft.modules.supplychain.domain.entities.BasicSCDocument
import com.namasoft.modules.supplychain.domain.primitives.ItemColorDF
import com.namasoft.modules.supplychain.domain.primitives.ItemSizeDF
import com.namasoft.modules.supplychain.domain.valueobjects.ItemSpecificDimensions
public class EAFillDefaultSizeAndColor implements EntityAction<BasicSCDocument> {
@Override
Result doAction(BasicSCDocument doc, LongTextDF... parameters) {
Result result = Result.createAccumulatingResult();
if (BooleanDF.isFalse(doc.getCommitedBefore()))
return result;
List<BasicSCDocumentLine> details = doc.getDetails();
for (BasicSCDocumentLine detail : details) {
if (detail.fetchItem() == null)
continue;
if (detail.getSpecificDimensions() == null)
detail.setSpecificDimensions(new ItemSpecificDimensions());
if (BooleanDF.isTrue(detail.fetchItem().getHasSize())) {
if (ObjectChecker.isEmptyOrNull(detail.fetchSize()) || !detail.fetchItem().containsSize(detail.fetchSize())) {
for (def sz : detail.fetchItem().getSizesAndColors()) {
if (ObjectChecker.isNotEmptyOrNull(sz.getSize()) && BooleanDF.isTrue(sz.getDefaultSizeAndColor())) {
detail.getSpecificDimensions().setSize(sz.getSize());
break;
}
}
}
} else {
detail.getSpecificDimensions().setSize(ItemSizeDF.createEmpty());
}
if (BooleanDF.isTrue(detail.fetchItem().getHasColor())) {
if (ObjectChecker.isEmptyOrNull(detail.fetchColor()) || !detail.fetchItem().containsColor(detail.fetchColor())) {
for (def sz : detail.fetchItem().getSizesAndColors()) {
if (ObjectChecker.isNotEmptyOrNull(sz.getColor()) && BooleanDF.isTrue(sz.getDefaultSizeAndColor())) {
detail.getSpecificDimensions().setColor(sz.getColor());
break;
}
}
}
} else {
detail.getSpecificDimensions().setColor(ItemColorDF.createEmpty());
}
}
return result;
}
@Override
String describe() {
return "Works only on a document that has been committed before. For each line: when the item has sizes and the line size is " +
"empty or is not one of the item's sizes, the line takes the size of the first Sizes and Colors row of the item that is " +
"marked Default and has a size; when the item has no sizes, the line size is cleared. Colors are handled the same way. " +
"Takes no parameters.";
}
@Override
List<String> columnNames() {
return Arrays.asList("Not used - leave empty");
}
}Related
- EAGroovyAction: how Groovy scripts are attached to entity flows.
- Understanding Inventory Items: sizes and colors on the item.
- EAGenAndItemSizesAndColorsAndRevisions: a built-in flow that takes the other route and adds a document's missing sizes and colors to the item.