Difference between revisions of "Rule snippets"
| Line 47: | Line 47: | ||
Tested with groovy | Tested with groovy | ||
| + | |||
| + | ==Create price list version with one shot scheduler== | ||
| + | |||
| + | To be able to create long running price list versions after work hours a schedule schema was created, running daily at 18:00. This schedule than was disable at the first run by the following script which also starts the price list generation. | ||
| + | |||
| + | import org.compiere.util.Env; | ||
| + | import org.compiere.server.AdempiereServerMgr; | ||
| + | import org.compiere.model.MPInstance; | ||
| + | import org.compiere.model.MProcess; | ||
| + | import org.compiere.process.ProcessInfo; | ||
| + | import org.compiere.process.ProcessInfoParameter; | ||
| + | import org.compiere.process.ProcessCall; | ||
| + | import org.adempiere.base.Core; | ||
| + | |||
| + | SvrMgr = AdempiereServerMgr.get(false); | ||
| + | SvrMgr.stop("Scheduler" + P_AD_Scheduler_ID); | ||
| + | |||
| + | ProcessInfo pi = new ProcessInfo("Pricelistcreation",103); | ||
| + | pi.setRecord_ID(P_Record_ID); | ||
| + | |||
| + | MPInstance mpi = new MPInstance(Env.getCtx(), 0, null); | ||
| + | mpi.setAD_Process_ID(103); | ||
| + | mpi.setRecord_ID(P_Record_ID); | ||
| + | mpi.save(); | ||
| + | pi.setAD_PInstance_ID(mpi.getAD_PInstance_ID()); | ||
| + | |||
| + | ProcessCall process = Core.getProcess("org.compiere.process.M_PriceList_Create"); | ||
| + | |||
| + | boolean success = process.startProcess(Env.getCtx(), pi, null); | ||
| + | |||
| + | return success ? "OK" : "failed"; | ||
| + | |||
| + | The process starting this script must have params AD_Scheduler_ID and Record_ID. The params than have to be filled manually with the correct IDs of the scheduler entry and the price list version entry. | ||
| + | |||
| + | tested with groovy | ||
=See Also= | =See Also= | ||
Revision as of 22:51, 28 February 2021
Model Validator
Enforce current date
To enforce POS-orders to be completed only with the current date the following snippet was attached to c_order on before complete:
import org.compiere.util.Env; today = Env.getContextAsDate(A_Ctx, "#Date"); today.setHours(0); today.setMinutes(0); today.setSeconds(0); today.setNanos(0); if (A_PO.getDateOrdered().before(today) && A_PO.getC_DocTypeTarget_ID() == 1000041) msg = "POS-Aufträge nicht für zurückliegende Daten"; else msg = ""; return msg;
Of course the doctype ID must be adjusted to your installation. The usage of #Date instead of the systemtime allows to use an older date by setting it at login if required.
Tested with groovy
Processes
Remove unknown products from import
When importing products for example from price lists of a vendor it may be necessary only to import those products which are already present in the database. This script could be executed as process to remove all products from i_product which are not known by either upc/ean or the combination of vendor and vendorproductno.
import org.compiere.model.MTable;
import org.compiere.util.DB;
import org.compiere.util.Msg;
int no = DB.executeUpdate ("delete from I_Product where ad_client_id = " + A_AD_Client_ID +
" and I_Product_ID not in " +
" (select ip.I_Product_ID from I_Product ip " +
" join c_bpartner cb on cb.value = ip.bpartner_value or cb.c_bpartner_id = ip.c_bpartner_id " +
" join M_Product_po mpo on mpo.vendorproductno = ip.vendorproductno and mpo.c_bpartner_id = cb.c_bpartner_id " +
" join M_Product mp on mp.M_Product_ID = mpo.M_Product_ID and mp.isactive = 'Y' " +
" union select ip.I_Product_ID from I_Product ip " +
" join m_product mp on mp.upc = ip.upc and mp.isactive = 'Y')", A_TrxName);
A_ProcessInfo.addLog(0,null,null, "" + no + " Datensätze gelöscht");
result = "OK";
Tested with groovy
Create price list version with one shot scheduler
To be able to create long running price list versions after work hours a schedule schema was created, running daily at 18:00. This schedule than was disable at the first run by the following script which also starts the price list generation.
import org.compiere.util.Env;
import org.compiere.server.AdempiereServerMgr;
import org.compiere.model.MPInstance;
import org.compiere.model.MProcess;
import org.compiere.process.ProcessInfo;
import org.compiere.process.ProcessInfoParameter;
import org.compiere.process.ProcessCall;
import org.adempiere.base.Core;
SvrMgr = AdempiereServerMgr.get(false);
SvrMgr.stop("Scheduler" + P_AD_Scheduler_ID);
ProcessInfo pi = new ProcessInfo("Pricelistcreation",103);
pi.setRecord_ID(P_Record_ID);
MPInstance mpi = new MPInstance(Env.getCtx(), 0, null);
mpi.setAD_Process_ID(103);
mpi.setRecord_ID(P_Record_ID);
mpi.save();
pi.setAD_PInstance_ID(mpi.getAD_PInstance_ID());
ProcessCall process = Core.getProcess("org.compiere.process.M_PriceList_Create");
boolean success = process.startProcess(Env.getCtx(), pi, null);
return success ? "OK" : "failed";
The process starting this script must have params AD_Scheduler_ID and Record_ID. The params than have to be filled manually with the correct IDs of the scheduler entry and the price list version entry.
tested with groovy
See Also
- Script Process for event rule example from a Report & Process window.
- Script ModelValidatorLogin
- Script Callout
