Create One Purchase Order per Recommended Supplier From a Purchase Request
This Groovy script turns a purchase request into purchase orders. Each request line names its supplier in Recommended Supplier, and every time the request is saved, the script keeps one purchase order per supplier in step with it, holding that supplier's lines.
Customer-specific example
This script was written for one customer's setup. It is hard-wired to these line fields; adapt it before you use it elsewhere:
- The quantity ordered is the request line's Number 2, not its Quantity. The customer typed the approved quantity there.
- The supplier comes from Recommended Supplier. A line without one is not ordered.
- Each request line used gets its purchase order in Reference 4, and each purchase order points at the request in Reference 5.
| Class name | EAGenPurchaseOrdersPerRecommendedSupplier |
| Script | EAGenPurchaseOrdersPerRecommendedSupplier.groovy |
| Runs on | Purchase Request, on Post Commit. |
| Uses | Parameter 2. Leave Parameter 1 empty. |
| Shipped as | Source to paste. It is not built into the system. |
When to use it
A department asks for several items on one purchase request (Purchases → Documents → Purchase Request). The buyer reviews it, picks the supplier for each line in Recommended Supplier, and enters the quantity to order. Instead of making a purchase order per supplier by hand, the buyer saves the request and the orders appear, one per supplier.
When the buyer changes the request later, for example a quantity or a line's supplier, saving it again updates the orders to match.
This is the purchase request version of Create One Purchase Order per Supplier From a Purchase Quotation.
How it works
- It groups the lines by Recommended Supplier. Lines without one are left out.
- For each supplier it finds or creates the purchase order. A purchase order whose Reference 5 is this request and whose supplier is this supplier is reused. If there is none, a new one is created.
- It fills the order. The field map in Parameter 2 is applied, the header is copied from the request (except the warehouse and the locator), and the field map is applied once more so that it wins. The supplier is set, and Reference 5 and From Document point at the request.
- It replaces the order's lines with copies of the supplier's request lines. Each copied line's quantity is the request line's Number 2.
- It saves the order, and writes the order into Reference 4 of each request line it used.
- It deletes the leftovers. Any purchase order whose Reference 5 is this request but whose supplier no longer appears on the request is deleted.
The script runs on every save that commits the request, not only the first one.
Reference 5 belongs to the script
Step 6 deletes every purchase order whose Reference 5 points at the request and whose supplier is gone. Do not use Reference 5 of those orders for anything else, and do not link a hand-made order to the request through it.
The orders must save on their own
Each order is saved with the full checks of a normal save. If one of them is refused, the request's save is refused too, with the order's reason after Error while running entity flow. The script sets no prices of its own, so check the prices on the orders before you send them.
Setting it up
- Open Administration → Display Customization → Entity Flow and create a flow with Target Type set to Purchase Request.
- In the Details grid, add a line with the action Post Commit, and paste the whole script from the file above into Groovy Script. Leave Class Name empty: it is filled in when you save.
- Fill in the parameters:
| Parameter | Title shown after saving | What to enter |
|---|---|---|
| Parameter 1 | (empty) | Nothing. |
| Parameter 2 | Fields Map | A field map from the request (source) to the purchase order (target), one target=source pair per line. It must at least set the order's book and term. See Field Values Calculator. |
- Save the flow. Class Name now shows
com.namasoft.modules.supplychain.domain.utils.plugnplay.groovy.EAGenPurchaseOrdersPerRecommendedSupplier.
Number 2 and Reference 4 are not columns of the standard purchase request grid. Add them with a Screen Modifier so the buyer can type the quantity to order and see each line's order.
Example
Parameter 2 sets the order's book and term:
book='PO01'
term='PO'A purchase request has four lines:
| Item | Quantity | Number 2 | Recommended Supplier |
|---|---|---|---|
| A3 paper | 10 | 4 | Supplier A |
| A4 paper | 5 | 5 | Supplier B |
| Folders | 3 | 3 | (empty) |
| Coloured paper | 2 | 2 | Supplier A |
Saving it creates two purchase orders: one for supplier A with A3 paper ×4 and coloured paper ×2, and one for supplier B with A4 paper ×5. The folders are not ordered. Reference 4 of the three ordered lines shows their order.
The buyer then changes the A3 paper's Number 2 to 6, clears the A4 paper's Recommended Supplier, and saves again. Supplier A's order now has A3 paper ×6, and supplier B's order is deleted.
The script
package com.namasoft.modules.supplychain.domain.utils.plugnplay.groovy
import com.namasoft.common.fieldids.NotGeneratedFields
import com.namasoft.common.utilities.CollectionsUtility
import com.namasoft.infor.domainbase.util.actions.EAGenerateEntityFromEntityAction
import com.namasoft.infra.domainbase.common.criteria.CriteriaBuilder
import com.namasoft.infra.domainbase.datafields.LongTextDF
import com.namasoft.infra.domainbase.entity.base.EntityAction
import com.namasoft.infra.domainbase.entity.base.EntityMediator
import com.namasoft.infra.domainbase.entity.generic.GenericReference
import com.namasoft.infra.domainbase.persistence.repos.FilterOnDimensions
import com.namasoft.infra.domainbase.persistence.repos.ListPageMatchingParameters
import com.namasoft.infra.domainbase.persistence.repos.Persister
import com.namasoft.infra.domainbase.util.Result
import com.namasoft.modules.supplychain.domain.details.ItemRequestLine
import com.namasoft.modules.supplychain.domain.details.PurchaseOrderLine
import com.namasoft.modules.supplychain.domain.entities.ItemRequest
import com.namasoft.modules.supplychain.domain.entities.PurchaseOrder
import com.namasoft.modules.supplychain.domain.utils.SCDocFromDocCopier
import com.namasoft.modules.supplychain.utils.SCEntityTypes
import static com.namasoft.common.fieldids.AllModulesFieldIDs.PurchaseOrder_supplier
public class EAGenPurchaseOrdersPerRecommendedSupplier implements EntityAction<ItemRequest> {
@Override
Result doAction(ItemRequest doc, LongTextDF... parameters) {
Result result = Result.createAccumulatingResult();
Map<GenericReference, List<ItemRequestLine>> map = CollectionsUtility.mapList(doc.getDetails(), { it.getRecommendedSupplier()?.toGenericReference() });
List<PurchaseOrder> orders = listOrders(doc)
map.remove(null);
for (Map.Entry<GenericReference, List<ItemRequestLine>> e : map.entrySet()) {
PurchaseOrder order = findOrder(e.getKey(), doc);
if (order == null) {
order = EntityMediator.createEntity(SCEntityTypes.PurchaseOrder());
} else {
order.startEditing().addToAccumulatingResult(result);
}
EAGenerateEntityFromEntityAction.copyFieldsFromSourceToTarget(doc, order, parameters[1].getPrimitiveValue());
EntityMediator.copyCommonDataFromSourceToGeneraed(doc, order, order.getTerm(), order.getBook());
SCDocFromDocCopier.copyHeaderDataExceptWareAndLocator(order, doc);
EAGenerateEntityFromEntityAction.copyFieldsFromSourceToTarget(doc, order, parameters[1].getPrimitiveValue());
order.setRef5(GenericReference.fromEntity(doc));
order.setSupplier(GenericReference.toReal(e.getKey()));
order.setFromDoc(GenericReference.fromEntity(doc));
List<PurchaseOrderLine> old = new ArrayList<>(order.getDetails());
order.getDetails().clear();
for (ItemRequestLine src : e.getValue()) {
PurchaseOrderLine copy = old.isEmpty() ? order.createLine() : old.remove(0);
SCDocFromDocCopier.copyLine(src, copy, doc);
copy.getQuantity().getQuantity().getPrimeQty().setValue(src.getN2());
order.getDetails().add(copy);
}
EntityMediator.commitFromBusinessAction(order).addToAccumulatingResult(result);
Persister.flush();
for (ItemRequestLine src : e.getValue()) {
src.setRef4(order.toGenericReference());
}
orders.remove(order);
}
deleteOrders(orders, result);
result
}
private List<PurchaseOrder> listOrders(ItemRequest doc) {
List<PurchaseOrder> existing = Persister.listPageMatching(PurchaseOrder.class,
ListPageMatchingParameters.create().criteria(CriteriaBuilder.create().field(NotGeneratedFields.ref5 + ".id").equal(doc.getId()).build()));
return existing;
}
private void deleteOrders(List<PurchaseOrder> orders, Result result) {
for (PurchaseOrder order : orders) {
EntityMediator.deleteEntityFromBusinessAction(order).addToAccumulatingResult(result);
}
}
private PurchaseOrder findOrder(GenericReference supplier, ItemRequest doc) {
return Persister.findFirstRecordMatching(PurchaseOrder.class, CriteriaBuilder.create().field(NotGeneratedFields.ref5 + ".id").equal(doc.getId()).and().field(PurchaseOrder_supplier).equal(GenericReference.toReal(supplier)).build(), FilterOnDimensions.No);
}
@Override
List<String> columnNames() {
return Arrays.asList("","Fields Map")
}
}Related
- EAGroovyAction: how Groovy scripts are attached to entity flows and task schedules.
- Create One Purchase Order per Supplier From a Purchase Quotation: the same idea for purchase quotations, with the supplier in Reference 3.
- EAGenSCDocFromDocWithFieldsMap: the built-in element that creates one document from another through a field map.