-
Notifications
You must be signed in to change notification settings - Fork 567
/
HUReportService.java
340 lines (289 loc) · 11.4 KB
/
HUReportService.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
package de.metas.handlingunits.report;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import javax.annotation.Nullable;
import org.adempiere.service.ISysConfigBL;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.slf4j.Logger;
import com.google.common.collect.ImmutableList;
import ch.qos.logback.classic.Level;
import de.metas.handlingunits.model.X_M_HU_PI_Version;
import de.metas.handlingunits.process.api.HUProcessDescriptor;
import de.metas.handlingunits.process.api.IMHUProcessDAO;
import de.metas.logging.LogManager;
import de.metas.process.AdProcessId;
import de.metas.util.ILoggable;
import de.metas.util.Loggables;
import de.metas.util.Services;
import lombok.NonNull;
/*
* #%L
* de.metas.handlingunits.base
* %%
* Copyright (C) 2017 metas GmbH
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
/**
* This service facade offers useful methods around to handling unit related jasper report processed.
*
* @author metas-dev <dev@metasfresh.com>
*
*/
public class HUReportService
{
public static HUReportService get()
{
return INSTANCE;
}
private static final transient Logger logger = LogManager.getLogger(HUReportService.class);
public static final String SYSCONFIG_RECEIPT_LABEL_PROCESS_ID = "de.metas.handlingunits.MaterialReceiptLabel.AD_Process_ID";
public static final String SYSCONFIG_FINISHEDGOODS_LABEL_PROCESS_ID = "de.metas.handlingunits.FinishedGoodsLabel.AD_Process_ID";
public static final String SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED = "de.metas.handlingunits.MaterialReceiptLabel.AutoPrint.Enabled";
public static final String SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED_C_BPARTNER_ID = SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED + ".C_BPartner_ID_";
public static final String SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_COPIES = "de.metas.handlingunits.MaterialReceiptLabel.AutoPrint.Copies";
// 895 webui
public static final String SYSCONFIG_PICKING_LABEL_AUTO_PRINT_ENABLED = "de.metas.ui.web.picking.PickingLabel.AutoPrint.Enabled";
public static final String SYSCONFIG_PICKING_LABEL_PROCESS_ID = "de.metas.ui.web.picking.PickingLabel.AD_Process_ID";
public static final String SYSCONFIG_PICKING_LABEL_AUTO_PRINT_COPIES = "de.metas.ui.web.picking.PickingLabel.AutoPrint.Copies";
private static final HUReportService INSTANCE = new HUReportService();
private HUReportService()
{
}
/**
* Return the AD_Process_ID that was configured via {@code SysConfig} {@value #SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_PROCESS_ID}, if any.
*
* @return AD_Process_ID or <code>-1</code>
*/
public AdProcessId retrievePrintReceiptLabelProcessIdOrNull()
{
return retrieveProcessIdBySysConfig(SYSCONFIG_RECEIPT_LABEL_PROCESS_ID);
}
public AdProcessId retrievePrintFinishedGoodsLabelProcessIdOrNull()
{
return retrieveProcessIdBySysConfig(SYSCONFIG_FINISHEDGOODS_LABEL_PROCESS_ID);
}
private AdProcessId retrievePickingLabelProcessIdOrNull()
{
return retrieveProcessIdBySysConfig(SYSCONFIG_PICKING_LABEL_PROCESS_ID);
}
private AdProcessId retrieveProcessIdBySysConfig(final String sysConfigName)
{
final ISysConfigBL sysConfigBL = Services.get(ISysConfigBL.class);
final Properties ctx = Env.getCtx();
final int reportProcessId = sysConfigBL.getIntValue(sysConfigName, -1, Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx));
return AdProcessId.ofRepoIdOrNull(reportProcessId);
}
/**
* @return those HUs from the given {@code huToReport} what match the given {@code adProcessId}'s {@link HUProcessDescriptor}.
* If there is no {@link HUProcessDescriptor}, then it returns an empty list.
*/
public List<HUToReport> getHUsToProcess(
@NonNull final HUToReport huToReport,
@NonNull final AdProcessId adProcessId)
{
final IMHUProcessDAO huProcessDAO = Services.get(IMHUProcessDAO.class);
final HUProcessDescriptor huProcessDescriptor = huProcessDAO.getByProcessIdOrNull(adProcessId);
if (huProcessDescriptor == null)
{
return ImmutableList.of();
}
return huToReport.streamRecursively()
.filter(currentHU -> currentHU.isTopLevel() || !huProcessDescriptor.isAcceptOnlyTopLevelHUs()) // if acceptOnlyTopLevelHUs then only accept topLevel-HUs
.filter(currentHU -> huProcessDescriptor.appliesToHUUnitType(currentHU.getHUUnitType()))
.collect(ImmutableList.toImmutableList());
}
public int getReceiptLabelAutoPrintCopyCount()
{
return getAutoPrintCopyCountForSysConfig(SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_COPIES);
}
public int getPickingLabelAutoPrintCopyCount()
{
return getAutoPrintCopyCountForSysConfig(SYSCONFIG_PICKING_LABEL_AUTO_PRINT_COPIES);
}
public int getAutoPrintCopyCountForSysConfig(final String sysConfigName)
{
final ISysConfigBL sysConfigBL = Services.get(ISysConfigBL.class);
final Properties ctx = Env.getCtx();
final int copies = sysConfigBL.getIntValue(sysConfigName, 1, Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx));
return copies;
}
/**
* Checks the sysconfig and returns true or if receipt label auto printing is enabled in general or for the given HU's C_BPartner_ID.
*
* @param vendorBPartnerId the original vendor. By now, might not be the same as M_HU.C_BPartner_ID anymore
* @return
*/
public boolean isReceiptLabelAutoPrintEnabled(final int vendorBPartnerId)
{
final ISysConfigBL sysConfigBL = Services.get(ISysConfigBL.class);
final Properties ctx = Env.getCtx();
final String vendorSysconfigName = SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED_C_BPARTNER_ID + vendorBPartnerId;
final String valueForBPartner = sysConfigBL.getValue(vendorSysconfigName, "NOT_SET", Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx));
logger.info("SysConfig {}={};", vendorSysconfigName, valueForBPartner);
if (!"NOT_SET".equals(valueForBPartner))
{
return DisplayType.toBoolean(valueForBPartner, false);
}
final String genericValue = sysConfigBL.getValue(SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED, "N", Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx));
logger.info("SysConfig {}={};", SYSCONFIG_RECEIPT_LABEL_AUTO_PRINT_ENABLED, genericValue);
return DisplayType.toBoolean(genericValue, false);
}
public boolean isPickingLabelAutoPrintEnabled()
{
final Properties ctx = Env.getCtx();
final ISysConfigBL sysConfigBL = Services.get(ISysConfigBL.class);
final String genericValue = sysConfigBL.getValue(SYSCONFIG_PICKING_LABEL_AUTO_PRINT_ENABLED, "N", Env.getAD_Client_ID(ctx), Env.getAD_Org_ID(ctx));
logger.info("SysConfig {}={};", SYSCONFIG_PICKING_LABEL_AUTO_PRINT_ENABLED, genericValue);
return DisplayType.toBoolean(genericValue, false);
}
public List<HUToReport> getHUsToProcess(@NonNull final Set<HUToReport> husToCheck)
{
if (husToCheck.isEmpty())
{
return ImmutableList.of();
}
final List<HUToReport> tuHUs = new ArrayList<>();
final List<HUToReport> luHUs = new ArrayList<>();
final List<HUToReport> cuHUs = new ArrayList<>();
for (final HUToReport hu : husToCheck)
{
final String huUnitType = hu.getHUUnitType();
if (X_M_HU_PI_Version.HU_UNITTYPE_VirtualPI.equals(huUnitType))
{
cuHUs.add(hu);
}
else if (X_M_HU_PI_Version.HU_UNITTYPE_LoadLogistiqueUnit.equals(huUnitType))
{
luHUs.add(hu);
}
else if (X_M_HU_PI_Version.HU_UNITTYPE_TransportUnit.equals(huUnitType))
{
tuHUs.add(hu);
}
}
final String huUnitTypeToReport;
if (!tuHUs.isEmpty())
{
huUnitTypeToReport = X_M_HU_PI_Version.HU_UNITTYPE_TransportUnit;
}
else if (!luHUs.isEmpty())
{
huUnitTypeToReport = X_M_HU_PI_Version.HU_UNITTYPE_LoadLogistiqueUnit;
}
else
{
huUnitTypeToReport = X_M_HU_PI_Version.HU_UNITTYPE_VirtualPI;
}
if (!luHUs.isEmpty() || !tuHUs.isEmpty() || !cuHUs.isEmpty())
{
return extractHUsToProcess(huUnitTypeToReport, luHUs, tuHUs, cuHUs);
}
else
{
return ImmutableList.of();
}
}
/**
* In case at least one TU was selected, we will deliver the processes for TUs.
*
* This will happen even though we have, for instance, just 1 TU and some LUs selected.
*
* The HUs to have the processes applied will be the 1 TU and the included TUs of the selected LUs
*
* @param huUnitType
* @param luHUs
* @param tuHUs
*/
private static List<HUToReport> extractHUsToProcess(final String huUnitType, final List<HUToReport> luHUs, final List<HUToReport> tuHUs, final List<HUToReport> cuHUs)
{
// In case the unit type is Virtual PI we don't have to return anything, since we don't have processes for virtual PIs
if (X_M_HU_PI_Version.HU_UNITTYPE_VirtualPI.equals(huUnitType))
{
return cuHUs;
}
// In case we the unit type is LU we just have to process the LUs
else if (X_M_HU_PI_Version.HU_UNITTYPE_LoadLogistiqueUnit.equals(huUnitType))
{
return luHUs;
}
// In case the unit type is TU we have 2 possibilities:
else
{
// In case the are no selected LUs, simply return the TUs
if (luHUs.isEmpty())
{
return tuHUs;
}
// if this point is reached, it means we have both TUs and LUs selected
else
{
final ImmutableList.Builder<HUToReport> husToProcess = ImmutableList.builder();
// first, add all the selected TUs
husToProcess.addAll(tuHUs);
for (final HUToReport lu : luHUs)
{
final List<HUToReport> includedHUs = lu.getIncludedHUs();
husToProcess.addAll(includedHUs);
}
return husToProcess.build();
}
}
}
/**
* @param onlyIfAutoPrintIsEnabled if {@code false}, then do the printing even if {@link #isPickingLabelAutoPrintEnabled()} is {@code false}.
*/
public void printPickingLabel(@Nullable final HUToReportWrapper huToReport, final boolean onlyIfAutoPrintIsEnabled)
{
final ILoggable loggable = Loggables.withLogger(logger, Level.INFO);
if (huToReport == null)
{
loggable.addLog("Param 'huToReport'==null; nothing to do");
return;
}
if (onlyIfAutoPrintIsEnabled && !isPickingLabelAutoPrintEnabled())
{
loggable.addLog("Auto printing receipt labels is not enabled via SysConfig; nothing to do");
return;
}
if (!huToReport.isTopLevel())
{
loggable.addLog("We only print top level HUs; nothing to do; huToReport={}", huToReport);
return;
}
final AdProcessId adProcessId = retrievePickingLabelProcessIdOrNull();
if (adProcessId == null)
{
loggable.addLog("No process configured via SysConfig {}; nothing to do", SYSCONFIG_PICKING_LABEL_PROCESS_ID);
return;
}
final List<HUToReport> husToProcess = getHUsToProcess(huToReport, adProcessId);
if (husToProcess.isEmpty())
{
loggable.addLog("The selected hu does not match AD_Process_ID={}; nothing to do; huToReport={}", adProcessId, huToReport);
return;
}
final int copies = getPickingLabelAutoPrintCopyCount();
loggable.addLog("Going to invoke HUReportExecutor to run AD_Process_ID={} with copies={} on husToProcess={}", adProcessId, copies, husToProcess);
final Properties ctx = Env.getCtx();
HUReportExecutor.newInstance(ctx)
.numberOfCopies(copies)
.executeHUReportAfterCommit(adProcessId, husToProcess);
}
}