001: /*
002: Licensed to the Apache Software Foundation (ASF) under one
003: or more contributor license agreements. See the NOTICE file
004: distributed with this work for additional information
005: regarding copyright ownership. The ASF licenses this file
006: to you under the Apache License, Version 2.0 (the
007: "License"); you may not use this file except in compliance
008: with the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing,
013: software distributed under the License is distributed on an
014: "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: KIND, either express or implied. See the License for the
016: specific language governing permissions and limitations
017: under the License.
018: */
019:
020: package org.ofbiz.order.order;
021:
022: import javolution.util.FastList;
023: import org.ofbiz.base.util.*;
024: import org.ofbiz.entity.GenericDelegator;
025: import org.ofbiz.entity.GenericEntityException;
026: import org.ofbiz.entity.GenericValue;
027: import org.ofbiz.entity.condition.*;
028: import org.ofbiz.entity.model.DynamicViewEntity;
029: import org.ofbiz.entity.model.ModelKeyMap;
030: import org.ofbiz.entity.util.EntityFindOptions;
031: import org.ofbiz.entity.util.EntityListIterator;
032: import org.ofbiz.security.Security;
033: import org.ofbiz.service.DispatchContext;
034: import org.ofbiz.service.GenericServiceException;
035: import org.ofbiz.service.LocalDispatcher;
036: import org.ofbiz.service.ServiceUtil;
037:
038: import java.math.BigDecimal;
039: import java.util.Iterator;
040: import java.util.List;
041: import java.util.Map;
042:
043: /**
044: * OrderLookupServices
045: */
046: public class OrderLookupServices {
047:
048: public static final String module = OrderLookupServices.class
049: .getName();
050:
051: public static Map findOrders(DispatchContext dctx, Map context) {
052: LocalDispatcher dispatcher = dctx.getDispatcher();
053: GenericDelegator delegator = dctx.getDelegator();
054: Security security = dctx.getSecurity();
055:
056: GenericValue userLogin = (GenericValue) context
057: .get("userLogin");
058: Integer viewIndex = (Integer) context.get("viewIndex");
059: Integer viewSize = (Integer) context.get("viewSize");
060: String showAll = (String) context.get("showAll");
061: String useEntryDate = (String) context.get("useEntryDate");
062: if (showAll == null) {
063: showAll = "N";
064: }
065:
066: // list of fields to select (initial list)
067: List fieldsToSelect = FastList.newInstance();
068: fieldsToSelect.add("orderId");
069: fieldsToSelect.add("statusId");
070: fieldsToSelect.add("orderTypeId");
071: fieldsToSelect.add("orderDate");
072: fieldsToSelect.add("currencyUom");
073: fieldsToSelect.add("grandTotal");
074: fieldsToSelect.add("remainingSubTotal");
075:
076: // sorting by order date newest first
077: List orderBy = UtilMisc.toList("-orderDate", "-orderId");
078:
079: // list to hold the parameters
080: List paramList = FastList.newInstance();
081:
082: // list of conditions
083: List conditions = FastList.newInstance();
084:
085: // check security flag for purchase orders
086: boolean canViewPo = security.hasEntityPermission("ORDERMGR",
087: "_PURCHASE_VIEW", userLogin);
088: if (!canViewPo) {
089: conditions.add(new EntityExpr("orderTypeId",
090: EntityOperator.NOT_EQUAL, "PURCHASE_ORDER"));
091: }
092:
093: // dynamic view entity
094: DynamicViewEntity dve = new DynamicViewEntity();
095: dve.addMemberEntity("OH", "OrderHeader");
096: dve.addAliasAll("OH", ""); // no prefix
097: dve.addRelation("one-nofk", "", "OrderType", UtilMisc
098: .toList(new ModelKeyMap("orderTypeId", "orderTypeId")));
099: dve.addRelation("one-nofk", "", "StatusItem", UtilMisc
100: .toList(new ModelKeyMap("statusId", "statusId")));
101:
102: // start the lookup
103: String orderId = (String) context.get("orderId");
104: if (UtilValidate.isNotEmpty(orderId)) {
105: paramList.add("orderId=" + orderId);
106: conditions.add(makeExpr("orderId", orderId));
107: }
108:
109: // the base order header fields
110: List orderTypeList = (List) context.get("orderTypeId");
111: if (orderTypeList != null) {
112: Iterator i = orderTypeList.iterator();
113: List orExprs = FastList.newInstance();
114: while (i.hasNext()) {
115: String orderTypeId = (String) i.next();
116: paramList.add("orderTypeId=" + orderTypeId);
117:
118: if (!"PURCHASE_ORDER".equals(orderTypeId)
119: || ("PURCHASE_ORDER".equals(orderTypeId) && canViewPo)) {
120: orExprs.add(new EntityExpr("orderTypeId",
121: EntityOperator.EQUALS, orderTypeId));
122: }
123: }
124: conditions.add(new EntityConditionList(orExprs,
125: EntityOperator.OR));
126: }
127:
128: String orderName = (String) context.get("orderName");
129: if (UtilValidate.isNotEmpty(orderName)) {
130: paramList.add("orderName=" + orderName);
131: conditions.add(makeExpr("orderName", orderName, true));
132: }
133:
134: List orderStatusList = (List) context.get("orderStatusId");
135: if (orderStatusList != null) {
136: Iterator i = orderStatusList.iterator();
137: List orExprs = FastList.newInstance();
138: while (i.hasNext()) {
139: String orderStatusId = (String) i.next();
140: paramList.add("orderStatusId=" + orderStatusId);
141: if ("PENDING".equals(orderStatusId)) {
142: List pendExprs = FastList.newInstance();
143: pendExprs.add(new EntityExpr("statusId",
144: EntityOperator.EQUALS, "ORDER_CREATED"));
145: pendExprs.add(new EntityExpr("statusId",
146: EntityOperator.EQUALS, "ORDER_PROCESSING"));
147: pendExprs.add(new EntityExpr("statusId",
148: EntityOperator.EQUALS, "ORDER_APPROVED"));
149: orExprs.add(new EntityConditionList(pendExprs,
150: EntityOperator.OR));
151: } else {
152: orExprs.add(new EntityExpr("statusId",
153: EntityOperator.EQUALS, orderStatusId));
154: }
155: }
156: conditions.add(new EntityConditionList(orExprs,
157: EntityOperator.OR));
158: }
159:
160: List productStoreList = (List) context.get("productStoreId");
161: if (productStoreList != null) {
162: Iterator i = productStoreList.iterator();
163: List orExprs = FastList.newInstance();
164: while (i.hasNext()) {
165: String productStoreId = (String) i.next();
166: paramList.add("productStoreId=" + productStoreId);
167: orExprs.add(new EntityExpr("productStoreId",
168: EntityOperator.EQUALS, productStoreId));
169: }
170: conditions.add(new EntityConditionList(orExprs,
171: EntityOperator.OR));
172: }
173:
174: List webSiteList = (List) context.get("orderWebSiteId");
175: if (webSiteList != null) {
176: Iterator i = webSiteList.iterator();
177: List orExprs = FastList.newInstance();
178: while (i.hasNext()) {
179: String webSiteId = (String) i.next();
180: paramList.add("webSiteId=" + webSiteId);
181: orExprs.add(new EntityExpr("webSiteId",
182: EntityOperator.EQUALS, webSiteId));
183: }
184: conditions.add(new EntityConditionList(orExprs,
185: EntityOperator.OR));
186: }
187:
188: List saleChannelList = (List) context.get("salesChannelEnumId");
189: if (saleChannelList != null) {
190: Iterator i = saleChannelList.iterator();
191: List orExprs = FastList.newInstance();
192: while (i.hasNext()) {
193: String salesChannelEnumId = (String) i.next();
194: paramList.add("salesChannelEnumId="
195: + salesChannelEnumId);
196: orExprs.add(new EntityExpr("salesChannelEnumId",
197: EntityOperator.EQUALS, salesChannelEnumId));
198: }
199: conditions.add(new EntityConditionList(orExprs,
200: EntityOperator.OR));
201: }
202:
203: String createdBy = (String) context.get("createdBy");
204: if (UtilValidate.isNotEmpty(createdBy)) {
205: paramList.add("createdBy=" + createdBy);
206: conditions.add(makeExpr("createdBy", createdBy));
207: }
208:
209: String terminalId = (String) context.get("terminalId");
210: if (UtilValidate.isNotEmpty(terminalId)) {
211: paramList.add("terminalId=" + terminalId);
212: conditions.add(makeExpr("terminalId", terminalId));
213: }
214:
215: String transactionId = (String) context.get("transactionId");
216: if (UtilValidate.isNotEmpty(transactionId)) {
217: paramList.add("transactionId=" + transactionId);
218: conditions.add(makeExpr("transactionId", transactionId));
219: }
220:
221: String externalId = (String) context.get("externalId");
222: if (UtilValidate.isNotEmpty(externalId)) {
223: paramList.add("externalId=" + externalId);
224: conditions.add(makeExpr("externalId", externalId));
225: }
226:
227: String internalCode = (String) context.get("internalCode");
228: if (UtilValidate.isNotEmpty(internalCode)) {
229: paramList.add("internalCode=" + internalCode);
230: conditions.add(makeExpr("internalCode", internalCode));
231: }
232:
233: String dateField = "Y".equals(useEntryDate) ? "entryDate"
234: : "orderDate";
235: String minDate = (String) context.get("minDate");
236: if (UtilValidate.isNotEmpty(minDate) && minDate.length() > 8) {
237: minDate = minDate.trim();
238: if (minDate.length() < 14)
239: minDate = minDate + " " + "00:00:00.000";
240: paramList.add("minDate=" + minDate);
241:
242: try {
243: Object converted = ObjectType.simpleTypeConvert(
244: minDate, "Timestamp", null, null);
245: if (converted != null) {
246: conditions.add(new EntityExpr(dateField,
247: EntityOperator.GREATER_THAN_EQUAL_TO,
248: converted));
249: }
250: } catch (GeneralException e) {
251: Debug.logWarning(e.getMessage(), module);
252: }
253: }
254:
255: String maxDate = (String) context.get("maxDate");
256: if (UtilValidate.isNotEmpty(maxDate) && maxDate.length() > 8) {
257: maxDate = maxDate.trim();
258: if (maxDate.length() < 14)
259: maxDate = maxDate + " " + "23:59:59.999";
260: paramList.add("maxDate=" + maxDate);
261:
262: try {
263: Object converted = ObjectType.simpleTypeConvert(
264: maxDate, "Timestamp", null, null);
265: if (converted != null) {
266: conditions.add(new EntityExpr("orderDate",
267: EntityOperator.LESS_THAN_EQUAL_TO,
268: converted));
269: }
270: } catch (GeneralException e) {
271: Debug.logWarning(e.getMessage(), module);
272: }
273: }
274:
275: // party (role) fields
276: String userLoginId = (String) context.get("userLoginId");
277: String partyId = (String) context.get("partyId");
278: List roleTypeList = (List) context.get("roleTypeId");
279:
280: if (UtilValidate.isNotEmpty(userLoginId)
281: && UtilValidate.isEmpty(partyId)) {
282: GenericValue ul = null;
283: try {
284: ul = delegator.findByPrimaryKeyCache("UserLogin",
285: UtilMisc.toMap("userLoginId", userLoginId));
286: } catch (GenericEntityException e) {
287: Debug.logWarning(e.getMessage(), module);
288: }
289: if (ul != null) {
290: partyId = ul.getString("partyId");
291: }
292: }
293:
294: // add the role data to the view
295: if (roleTypeList != null || partyId != null) {
296: dve.addMemberEntity("OT", "OrderRole");
297: dve.addAlias("OT", "partyId");
298: dve.addAlias("OT", "roleTypeId");
299: dve.addViewLink("OH", "OT", Boolean.FALSE, UtilMisc
300: .toList(new ModelKeyMap("orderId", "orderId")));
301: }
302:
303: if (UtilValidate.isNotEmpty(partyId)) {
304: paramList.add("partyId=" + partyId);
305: fieldsToSelect.add("partyId");
306: conditions.add(makeExpr("partyId", partyId));
307: }
308:
309: if (roleTypeList != null) {
310: fieldsToSelect.add("roleTypeId");
311: Iterator i = roleTypeList.iterator();
312: List orExprs = FastList.newInstance();
313: while (i.hasNext()) {
314: String roleTypeId = (String) i.next();
315: paramList.add("roleTypeId=" + roleTypeId);
316: orExprs.add(makeExpr("roleTypeId", roleTypeId));
317: }
318: conditions.add(new EntityConditionList(orExprs,
319: EntityOperator.OR));
320: }
321:
322: // order item fields
323: String correspondingPoId = (String) context
324: .get("correspondingPoId");
325: String subscriptionId = (String) context.get("subscriptionId");
326: String productId = (String) context.get("productId");
327: String budgetId = (String) context.get("budgetId");
328: String quoteId = (String) context.get("quoteId");
329:
330: if (correspondingPoId != null || subscriptionId != null
331: || productId != null || budgetId != null
332: || quoteId != null) {
333: dve.addMemberEntity("OI", "OrderItem");
334: dve.addAlias("OI", "correspondingPoId");
335: dve.addAlias("OI", "subscriptionId");
336: dve.addAlias("OI", "productId");
337: dve.addAlias("OI", "budgetId");
338: dve.addAlias("OI", "quoteId");
339: dve.addViewLink("OH", "OI", Boolean.FALSE, UtilMisc
340: .toList(new ModelKeyMap("orderId", "orderId")));
341: }
342:
343: if (UtilValidate.isNotEmpty(correspondingPoId)) {
344: paramList.add("correspondingPoId=" + correspondingPoId);
345: conditions.add(makeExpr("correspondingPoId",
346: correspondingPoId));
347: }
348:
349: if (UtilValidate.isNotEmpty(subscriptionId)) {
350: paramList.add("subscriptionId=" + subscriptionId);
351: conditions.add(makeExpr("subscriptionId", subscriptionId));
352: }
353:
354: if (UtilValidate.isNotEmpty(productId)) {
355: paramList.add("productId=" + productId);
356: if (productId.startsWith("%") || productId.startsWith("*")
357: || productId.endsWith("%")
358: || productId.endsWith("*")) {
359: conditions.add(makeExpr("productId", productId));
360: } else {
361: GenericValue product = null;
362: try {
363: product = delegator.findByPrimaryKey("Product",
364: UtilMisc.toMap("productId", productId));
365: } catch (GenericEntityException e) {
366: Debug.logWarning(e.getMessage(), module);
367: }
368: if (product != null) {
369: String isVirtual = product.getString("isVirtual");
370: if (isVirtual != null && "Y".equals(isVirtual)) {
371: List orExprs = FastList.newInstance();
372: orExprs.add(new EntityExpr("productId",
373: EntityOperator.EQUALS, productId));
374:
375: Map varLookup = null;
376: try {
377: varLookup = dispatcher.runSync(
378: "getAllProductVariants", UtilMisc
379: .toMap("productId",
380: productId));
381: } catch (GenericServiceException e) {
382: Debug.logWarning(e.getMessage(), module);
383: }
384: List variants = (List) varLookup
385: .get("assocProducts");
386: if (variants != null) {
387: Iterator i = variants.iterator();
388: while (i.hasNext()) {
389: GenericValue v = (GenericValue) i
390: .next();
391: orExprs.add(new EntityExpr("productId",
392: EntityOperator.EQUALS,
393: v.getString("productIdTo")));
394: }
395: }
396: conditions.add(new EntityConditionList(orExprs,
397: EntityOperator.OR));
398: } else {
399: conditions.add(new EntityExpr("productId",
400: EntityOperator.EQUALS, productId));
401: }
402: }
403: }
404: }
405:
406: if (UtilValidate.isNotEmpty(budgetId)) {
407: paramList.add("budgetId=" + budgetId);
408: conditions.add(makeExpr("budgetId", budgetId));
409: }
410:
411: if (UtilValidate.isNotEmpty(quoteId)) {
412: paramList.add("quoteId=" + quoteId);
413: conditions.add(makeExpr("quoteId", quoteId));
414: }
415:
416: // payment preference fields
417: String billingAccountId = (String) context
418: .get("billingAccountId");
419: String finAccountId = (String) context.get("finAccountId");
420: String cardNumber = (String) context.get("cardNumber");
421: String accountNumber = (String) context.get("accountNumber");
422:
423: if (billingAccountId != null || finAccountId != null
424: || cardNumber != null || accountNumber != null) {
425: dve.addMemberEntity("OP", "OrderPaymentPreference");
426: dve.addAlias("OP", "billingAccountId");
427: dve.addAlias("OP", "finAccountId");
428: dve.addAlias("OP", "paymentMethodId");
429: dve.addViewLink("OH", "OP", Boolean.FALSE, UtilMisc
430: .toList(new ModelKeyMap("orderId", "orderId")));
431: }
432:
433: // search by billing account ID
434: if (UtilValidate.isNotEmpty(billingAccountId)) {
435: paramList.add("billingAccountId=" + billingAccountId);
436: conditions.add(makeExpr("billingAccountId",
437: billingAccountId));
438: }
439:
440: // search by fin account ID
441: if (UtilValidate.isNotEmpty(finAccountId)) {
442: paramList.add("finAccountId=" + finAccountId);
443: conditions.add(makeExpr("finAccountId", finAccountId));
444: }
445:
446: // search by card number
447: if (UtilValidate.isNotEmpty(cardNumber)) {
448: dve.addMemberEntity("CC", "CreditCard");
449: dve.addAlias("CC", "cardNumber");
450: dve.addViewLink("OP", "CC", Boolean.FALSE, UtilMisc
451: .toList(new ModelKeyMap("paymentMethodId",
452: "paymentMethodId")));
453:
454: paramList.add("cardNumber=" + cardNumber);
455: conditions.add(makeExpr("cardNumber", cardNumber));
456: }
457:
458: // search by eft account number
459: if (UtilValidate.isNotEmpty(accountNumber)) {
460: dve.addMemberEntity("EF", "EftAccount");
461: dve.addAlias("EF", "accountNumber");
462: dve.addViewLink("OP", "EF", Boolean.FALSE, UtilMisc
463: .toList(new ModelKeyMap("paymentMethodId",
464: "paymentMethodId")));
465:
466: paramList.add("accountNumber=" + accountNumber);
467: conditions.add(makeExpr("accountNumber", accountNumber));
468: }
469:
470: // shipment/inventory item
471: String inventoryItemId = (String) context
472: .get("inventoryItemId");
473: String softIdentifier = (String) context.get("softIdentifier");
474: String serialNumber = (String) context.get("serialNumber");
475: String shipmentId = (String) context.get("shipmentId");
476:
477: if (shipmentId != null || inventoryItemId != null
478: || softIdentifier != null || serialNumber != null) {
479: dve.addMemberEntity("II", "ItemIssuance");
480: dve.addAlias("II", "shipmentId");
481: dve.addAlias("II", "inventoryItemId");
482: dve.addViewLink("OH", "II", Boolean.FALSE, UtilMisc
483: .toList(new ModelKeyMap("orderId", "orderId")));
484:
485: if (softIdentifier != null || serialNumber != null) {
486: dve.addMemberEntity("IV", "InventoryItem");
487: dve.addAlias("IV", "softIdentifier");
488: dve.addAlias("IV", "serialNumber");
489: dve.addViewLink("II", "IV", Boolean.FALSE, UtilMisc
490: .toList(new ModelKeyMap("inventoryItemId",
491: "inventoryItemId")));
492: }
493: }
494:
495: if (UtilValidate.isNotEmpty(inventoryItemId)) {
496: paramList.add("inventoryItemId=" + inventoryItemId);
497: conditions
498: .add(makeExpr("inventoryItemId", inventoryItemId));
499: }
500:
501: if (UtilValidate.isNotEmpty(softIdentifier)) {
502: paramList.add("softIdentifier=" + softIdentifier);
503: conditions.add(makeExpr("softIdentifier", softIdentifier,
504: true));
505: }
506:
507: if (UtilValidate.isNotEmpty(serialNumber)) {
508: paramList.add("serialNumber=" + serialNumber);
509: conditions
510: .add(makeExpr("serialNumber", serialNumber, true));
511: }
512:
513: if (UtilValidate.isNotEmpty(shipmentId)) {
514: paramList.add("shipmentId=" + shipmentId);
515: conditions.add(makeExpr("shipmentId", shipmentId));
516: }
517:
518: // back order checking
519: String hasBackOrders = (String) context.get("hasBackOrders");
520: if (UtilValidate.isNotEmpty(hasBackOrders)) {
521: dve.addMemberEntity("IR", "OrderItemShipGrpInvRes");
522: dve.addAlias("IR", "quantityNotAvailable");
523: dve.addViewLink("OH", "IR", Boolean.FALSE, UtilMisc
524: .toList(new ModelKeyMap("orderId", "orderId")));
525:
526: paramList.add("hasBackOrders=" + hasBackOrders);
527: if ("Y".equals(hasBackOrders)) {
528: conditions.add(new EntityExpr("quantityNotAvailable",
529: EntityOperator.NOT_EQUAL, null));
530: conditions.add(new EntityExpr("quantityNotAvailable",
531: EntityOperator.GREATER_THAN, new Double(0)));
532: } else if ("N".equals(hasBackOrders)) {
533: List orExpr = FastList.newInstance();
534: orExpr.add(new EntityExpr("quantityNotAvailable",
535: EntityOperator.EQUALS, null));
536: orExpr.add(new EntityExpr("quantityNotAvailable",
537: EntityOperator.EQUALS, new Double(0)));
538: conditions.add(new EntityConditionList(orExpr,
539: EntityOperator.OR));
540: }
541: }
542:
543: // set distinct on so we only get one row per order
544: EntityFindOptions findOpts = new EntityFindOptions(true,
545: EntityFindOptions.TYPE_SCROLL_INSENSITIVE,
546: EntityFindOptions.CONCUR_READ_ONLY, true);
547:
548: // create the main condition
549: EntityCondition cond = null;
550: if (conditions.size() > 0 || showAll.equalsIgnoreCase("Y")) {
551: cond = new EntityConditionList(conditions,
552: EntityOperator.AND);
553: }
554:
555: if (Debug.verboseOn()) {
556: Debug.log("Find order query: " + cond.toString());
557: }
558:
559: List orderList = FastList.newInstance();
560: int orderCount = 0;
561:
562: // get the index for the partial list
563: int lowIndex = (((viewIndex.intValue() - 1) * viewSize
564: .intValue()) + 1);
565: int highIndex = viewIndex.intValue() * viewSize.intValue();
566:
567: if (cond != null) {
568: EntityListIterator eli = null;
569: try {
570: // do the lookup
571: eli = delegator.findListIteratorByCondition(dve, cond,
572: null, fieldsToSelect, orderBy, findOpts);
573:
574: // attempt to get the full size
575: eli.last();
576: orderCount = eli.currentIndex();
577:
578: // get the partial list for this page
579: eli.beforeFirst();
580: if (orderCount > viewSize.intValue()) {
581: orderList = eli.getPartialList(lowIndex, viewSize
582: .intValue());
583: } else if (orderCount > 0) {
584: orderList = eli.getCompleteList();
585: }
586:
587: if (highIndex > orderCount) {
588: highIndex = orderCount;
589: }
590: } catch (GenericEntityException e) {
591: Debug.logError(e, module);
592: return ServiceUtil.returnError(e.getMessage());
593: } finally {
594: if (eli != null) {
595: try {
596: eli.close();
597: } catch (GenericEntityException e) {
598: Debug.logWarning(e, e.getMessage(), module);
599: }
600: }
601: }
602: }
603:
604: // create the result map
605: Map result = ServiceUtil.returnSuccess();
606:
607: // filter out requested inventory problems
608: filterInventoryProblems(context, result, orderList, paramList);
609:
610: // format the param list
611: String paramString = StringUtil.join(paramList, "&");
612:
613: result.put("highIndex", new Integer(highIndex));
614: result.put("lowIndex", new Integer(lowIndex));
615: result.put("viewIndex", viewIndex);
616: result.put("viewSize", viewSize);
617: result.put("showAll", showAll);
618:
619: result.put("paramList",
620: (paramString != null ? paramString : ""));
621: result.put("orderList", orderList);
622: result.put("orderListSize", new Integer(orderCount));
623:
624: return result;
625: }
626:
627: public static void filterInventoryProblems(Map context, Map result,
628: List orderList, List paramList) {
629: List filterInventoryProblems = FastList.newInstance();
630:
631: String doFilter = (String) context
632: .get("filterInventoryProblems");
633: if (doFilter == null) {
634: doFilter = "N";
635: }
636:
637: if ("Y".equals(doFilter) && orderList.size() > 0) {
638: paramList.add("filterInventoryProblems=Y");
639: Iterator i = orderList.iterator();
640: while (i.hasNext()) {
641: GenericValue orderHeader = (GenericValue) i.next();
642: OrderReadHelper orh = new OrderReadHelper(orderHeader);
643: BigDecimal backorderQty = orh
644: .getOrderBackorderQuantityBd();
645: if (backorderQty.compareTo(new BigDecimal("0")) == 1) {
646: filterInventoryProblems.add(orh.getOrderId());
647: }
648: }
649: }
650:
651: List filterPOsOpenPastTheirETA = FastList.newInstance();
652: List filterPOsWithRejectedItems = FastList.newInstance();
653: List filterPartiallyReceivedPOs = FastList.newInstance();
654:
655: String filterPOReject = (String) context
656: .get("filterPOsWithRejectedItems");
657: String filterPOPast = (String) context
658: .get("filterPOsOpenPastTheirETA");
659: String filterPartRec = (String) context
660: .get("filterPartiallyReceivedPOs");
661: if (filterPOReject == null) {
662: filterPOReject = "N";
663: }
664: if (filterPOPast == null) {
665: filterPOPast = "N";
666: }
667: if (filterPartRec == null) {
668: filterPartRec = "N";
669: }
670:
671: boolean doPoFilter = false;
672: if ("Y".equals(filterPOReject)) {
673: paramList.add("filterPOsWithRejectedItems=Y");
674: doPoFilter = true;
675: }
676: if ("Y".equals(filterPOPast)) {
677: paramList.add("filterPOsOpenPastTheirETA=Y");
678: doPoFilter = true;
679: }
680: if ("Y".equals(filterPartRec)) {
681: paramList.add("filterPartiallyReceivedPOs=Y");
682: doPoFilter = true;
683: }
684:
685: if (doPoFilter && orderList.size() > 0) {
686: Iterator i = orderList.iterator();
687: while (i.hasNext()) {
688: GenericValue orderHeader = (GenericValue) i.next();
689: OrderReadHelper orh = new OrderReadHelper(orderHeader);
690: String orderType = orh.getOrderTypeId();
691: String orderId = orh.getOrderId();
692:
693: if ("PURCHASE_ORDER".equals(orderType)) {
694: if ("Y".equals(filterPOReject)
695: && orh.getRejectedOrderItems()) {
696: filterPOsWithRejectedItems.add(orderId);
697: } else if ("Y".equals(filterPOPast)
698: && orh.getPastEtaOrderItems(orderId)) {
699: filterPOsOpenPastTheirETA.add(orderId);
700: } else if ("Y".equals(filterPartRec)
701: && orh.getPartiallyReceivedItems()) {
702: filterPartiallyReceivedPOs.add(orderId);
703: }
704: }
705: }
706: }
707:
708: result.put("filterInventoryProblemsList",
709: filterInventoryProblems);
710: result.put("filterPOsWithRejectedItemsList",
711: filterPOsWithRejectedItems);
712: result.put("filterPOsOpenPastTheirETAList",
713: filterPOsOpenPastTheirETA);
714: result.put("filterPartiallyReceivedPOsList",
715: filterPartiallyReceivedPOs);
716: }
717:
718: protected static EntityExpr makeExpr(String fieldName, String value) {
719: return makeExpr(fieldName, value, false);
720: }
721:
722: protected static EntityExpr makeExpr(String fieldName,
723: String value, boolean forceLike) {
724: EntityComparisonOperator op = forceLike ? EntityOperator.LIKE
725: : EntityOperator.EQUALS;
726:
727: if (value.startsWith("*")) {
728: op = EntityOperator.LIKE;
729: value = "%" + value.substring(1);
730: } else if (value.startsWith("%")) {
731: op = EntityOperator.LIKE;
732: }
733:
734: if (value.endsWith("*")) {
735: op = EntityOperator.LIKE;
736: value = value.substring(0, value.length() - 1) + "%";
737: } else if (value.endsWith("%")) {
738: op = EntityOperator.LIKE;
739: }
740:
741: if (forceLike) {
742: if (!value.startsWith("%")) {
743: value = "%" + value;
744: }
745: if (!value.endsWith("%")) {
746: value = value + "%";
747: }
748: }
749:
750: return new EntityExpr(fieldName, op, value);
751: }
752: }
|