根据订单获取可退货项流程分析
退货的时候,调用 services_return.xml 中的获取可进行退货的退货项  getReturnableItems  ,该服务调用了Java类 org.ofbiz.order.order.OrderReturnServices 中的getReturnableItems方法。
通过获取到的orderId 获得相对应的订单,通过查询条件,(orderId = 'DEMO1002' AND orderItemStatusId IN ('ITEM_APPROVED', 'ITEM_COMPLETED'))
去执行 OrderItemQuantityReportGroupByItem 视图查询,获取 根据item分组得到的数据列表 ,
根据订单的类型判断订单是否为销售订单(SALES_ORDER),
根据上面分组得到的数据列表进行循环调用 getReturnableQuantity service服务,该服务调用的同样是上述java类中的getReturnableQuantity方法,
根据orderItem中的productId 获取对应的Product实体对象,根据该商品的returned属性,去判断该商品是否支持退货操作。
在根据该商品的supportDiscontinuationDate 的支持终止日期与当前时间做比较,如果小于当前时间,则不能进行退货操作。
定义一个orderQty变量,如果可以进行退货操作,获取orderItem的数量并进行赋值,如果orderItem中的cancelQuantity数目存在的话,将cancelQuantity与orderQty相减获取订单的数目,赋值给orderQty变量,
获取orderItem的statusId状态,如果状态存在于ITEM_APPROVED 或者 ITEM_COMPLETED中,进行下面的循环。
根据OrderItem获取对应的退货项列表,如果退货项为空的话,可退货数目则为上面定义的orderQty的值,
如果退货项列表不为空的话,定义一个returnedQty变量,对列表进行循环,如果退货项的状态不为RETURN_CANCELLED,就对returnedQty 进行 增加的操作,最终将订单的orderQty数目与 returnedQty 数目相减,获取该orderitem可归还的数目。
 
 
 
生成退货单流程分析
因为我个人是打算用的ofbiz里面的service服务的,因此,要直接使用这个服务的时候,要求界面上的所要提交的参数值要保证跟ofbiz原生的页面相差不多,因此,直接对使用ofbiz原生的页面进行样式修改。
调用上面的获取可退货项的服务之后,获取了页面之后,然后进行提交,提交的target为createReturnAndItemOrAdjustment这个服务(control里面的调用事件type为service-multi),这个服务是调用simplemethod方法,method定义的文件为component://order/script/org/ofbiz/order/order/OrderReturnServices.xml,调用的方法为createReturnAndItemOrAdjustment,
先判断参数里面是否包含returnId,如果returnId为空,则创建退货单,然后根据返回回来的returnId,去调用createReturnItemOrAdjustment这个服务,这个服务的实现方法是使用java的方式,java类为org.ofbiz.order.order.OrderReturnServices,调用的方法为createReturnItemOrAdjustment,判断orderItemSeqId是否存在,如果存在,则调用createReturnItem,如果不存在,则调用createReturnAdjustment服务。
 
createReturnItem 服务是使用simplemethod的 方式,method的定义文件为component://order/script/org/ofbiz/order/order/OrderReturnServices.xml,调用方法为createReturnItem,这个服务里面有很多判断,例如是否有权限,是否有returntypeId,是否有归还数目等等,具体的要到对应的文件里面去看具体实现方法。
 
受理退货单
首先调用updateReturnHeader服务,服务定义在services_return.xml文件中,服务的实现方法为simple-method,定义文件为component://order/script/org/ofbiz/order/order/OrderReturnServices.xml,调用的方法为updateReturnHeader
在该服务中,没有明显的其他操作,但是很明显的在执行updateReturnHeader这个服务的时候,在后台能看到执行了好多其他的方法,说明在secas.xml中定义了该服务执行时会触发别的服务的执行。该文件一般存在于/emt/applications/order/servicedef/secas.xml   即跟服务文件同目录下,然后然后去看对应的服务在某些condition条件的满足下,会触发哪些别的服务。
 <eca service="updateReturnHeader" event="return">
<condition field-name="statusId" operator="equals" value="RETURN_ACCEPTED"/>
<!-- The quickReceiveReturn service checks this, and some status update calls may not pass it in, so don't
check it here or the option may be ignored.
<condition field-name="needsInventoryReceive" operator="equals" value="Y"/> -->
<action service="quickReceiveReturn" mode="sync"/>
</eca>
<eca service="updateReturnHeader" event="commit">
<condition field-name="statusId" operator="equals" value="RETURN_ACCEPTED"/>
<condition field-name="oldStatusId" operator="not-equals" value="RETURN_ACCEPTED"/>
<action service="processWaitReplacementReservedReturn" mode="sync"/>
<action service="processReplaceImmediatelyReturn" mode="sync"/>
<action service="createShipmentAndItemsForReturn" mode="sync"/>
<action service="processCrossShipReplacementReturn" mode="sync"/>
<action service="createTrackingCodeOrderReturns" mode="sync" run-as-user="system"/>
<action service="sendReturnAcceptNotification" mode="async" persist="true"/>
<action service="processRefundImmediatelyReturn" mode="sync"/>
<action service="createReturnStatus" mode="sync"/>
</eca>

首先当触发event事件并且statusId状态为RETURN_ACCEPTED的时候,就会调用quickReceiveReturn服务,下面为该服务的逻辑判断,如果嫌长可以快速掠过。

 <simple-method method-name="quickReceiveReturn" short-description="Quick Receive Entire Return">
<entity-one entity-name="ReturnHeader" value-field="returnHeader">
<field-map field-name="returnId" from-field="parameters.returnId"/>
</entity-one> <if-compare field="returnHeader.needsInventoryReceive" operator="equals" value="Y">
<!-- before receiving inventory, check to see if there is inventory information in this database -->
<entity-count entity-name="InventoryItem" count-field="iiCount">
<condition-expr field-name="facilityId" operator="equals" from-field="returnHeader.destinationFacilityId"/>
</entity-count> <if-compare field="iiCount" operator="greater" value="0" type="Integer">
<!-- create a return shipment for this return -->
<set field="shipmentCtx.returnId" from-field="parameters.returnId"/>
<call-service service-name="createShipmentForReturn" in-map-name="shipmentCtx">
<result-to-field result-name="shipmentId"/>
</call-service>
<log level="info" message="Created new shipment ${shipmentId}"/> <entity-condition entity-name="ReturnItem" list="returnItems">
<condition-expr field-name="returnId" operator="equals" from-field="returnHeader.returnId"/>
</entity-condition> <!-- if no inventory item type specified, get default from facility -->
<if-empty field="parameters.inventoryItemTypeId">
<get-related-one value-field="returnHeader" relation-name="Facility" to-value-field="facility"/>
<set field="parameters.inventoryItemTypeId" from-field="facility.defaultInventoryItemTypeId" default-value="NON_SERIAL_INV_ITEM"/>
</if-empty> <now-timestamp field="nowTimestamp"/> <entity-count entity-name="ReturnItem" count-field="returnItemCount">
<condition-expr field-name="returnId" operator="equals" from-field="returnHeader.returnId"/>
</entity-count>
<set field="nonProductItems" type="Long" value="0"/> <iterate entry="returnItem" list="returnItems">
<!-- record this return item on the return shipment as well. not sure if this is actually necessary... -->
<clear-field field="shipItemCtx"/>
<set from-field="shipmentId" field="shipItemCtx.shipmentId"/>
<set from-field="returnItem.productId" field="shipItemCtx.productId"/>
<set from-field="returnItem.returnQuantity" field="shipItemCtx.quantity"/>
<log level="info" message="calling create shipment item with ${shipItemCtx}"/>
<call-service service-name="createShipmentItem" in-map-name="shipItemCtx">
<result-to-field result-name="shipmentItemSeqId"/>
</call-service>
</iterate>
<iterate entry="returnItem" list="returnItems">
<clear-field field="receiveCtx"/> <if-empty field="returnItem.expectedItemStatus">
<set value="INV_RETURNED" field="returnItem.expectedItemStatus" type="String"/>
</if-empty>
<get-related-one value-field="returnItem" relation-name="OrderItem" to-value-field="orderItem"/>
<if-not-empty field="orderItem.productId">
<set field="costCtx.returnItemSeqId" from-field="returnItem.returnItemSeqId"/>
<set field="costCtx.returnId" from-field="returnItem.returnId"/>
<call-service service-name="getReturnItemInitialCost" in-map-name="costCtx">
<result-to-field result-name="initialItemCost" field="receiveCtx.unitCost"/>
</call-service>
<!--check if the items already have SERIALIZED inventory. If so, it still puts them back as SERIALIZED with status "Accepted."-->
<entity-count entity-name="InventoryItem" count-field="serializedItemCount">
<condition-list combine="and">
<condition-expr field-name="productId" operator="equals" from-field="returnItem.productId"/>
<condition-expr field-name="facilityId" operator="equals" from-field="returnHeader.destinationFacilityId"/>
<condition-expr field-name="inventoryItemTypeId" operator="equals" value="SERIALIZED_INV_ITEM"/>
</condition-list>
</entity-count>
<set field="setNonSerial" value="false"/>
<if-compare field="parameters.inventoryItemTypeId" value="NON_SERIAL_INV_ITEM" operator="equals">
<if-compare field="serializedItemCount" value="0" operator="equals">
<set field="parameters.inventoryItemTypeId" value="NON_SERIAL_INV_ITEM"/>
<set field="setNonSerial" value="true"/>
</if-compare>
</if-compare>
<if-compare field="setNonSerial" value="false" operator="equals">
<set field="parameters.inventoryItemTypeId" value="SERIALIZED_INV_ITEM"/>
<set field="returnItem.returnQuantity" value="1" type="BigDecimal"/>
</if-compare> <set from-field="parameters.inventoryItemTypeId" field="receiveCtx.inventoryItemTypeId"/>
<set from-field="returnItem.expectedItemStatus" field="receiveCtx.statusId"/>
<set from-field="returnItem.productId" field="receiveCtx.productId"/>
<set from-field="returnItem.returnItemSeqId" field="receiveCtx.returnItemSeqId"/>
<set from-field="returnItem.returnId" field="receiveCtx.returnId"/>
<set from-field="returnItem.returnQuantity" field="receiveCtx.quantityAccepted"/>
<set from-field="returnHeader.destinationFacilityId" field="receiveCtx.facilityId"/>
<!-- important: associate ShipmentReceipt with return shipment created -->
<set field="receiveCtx.shipmentId" from-field="shipmentId"/> <set field="receiveCtx.comments" value="Returned Item RA# ${returnItem.returnId}"/>
<set field="receiveCtx.datetimeReceived" from-field="nowTimestamp"/>
<set field="receiveCtx.quantityRejected" value="0" type="BigDecimal"/> <call-service service-name="receiveInventoryProduct" in-map-name="receiveCtx"/>
<else>
<calculate field="nonProductItems" type="Long">
<calcop operator="add">
<number value="1"/>
</calcop>
</calculate>
</else>
</if-not-empty>
</iterate> <!-- now that the receive is done; set the need flag to N -->
<refresh-value value-field="returnHeader"/>
<set field="returnHeader.needsInventoryReceive" value="N"/>
<store-value value-field="returnHeader"/> <!-- always check/update the ReturnHeader status, even though it might have been from the receiving above, just make sure -->
<if-compare field="returnHeader.statusId" operator="not-equals" value="RETURN_RECEIVED">
<set field="retStCtx.returnId" from-field="returnHeader.returnId"/>
<set field="retStCtx.statusId" value="RETURN_RECEIVED"/>
<call-service service-name="updateReturnHeader" in-map-name="retStCtx"/>
</if-compare>
<else>
<log level="info" message="Not receiving inventory for returnId ${returnHeader.returnId}, no inventory information available."/>
</else>
</if-compare>
</if-compare>
</simple-method>

大概分析下该simple-method方法,判断returnHeader退货头的needsInventoryReceive字段(需要仓库接收 ),如果是的话,判断退货头的DESTINATION_FACILITY_ID是否等于facilityId,如果等于,就获取库存项的数目,如果库存量大于0,则调用createShipmentForReturn(创建针对退货的装运头)的服务,生成shipmentId,然后判断参数parameters.inventoryItemTypeId是否为空,如果为空,就给默认值NON_SERIAL_INV_ITEM,获取退货项的数目,循环退货项的列表,调用createShipmentItem(装运项)服务,接着继续根据退货项列表循环调用receiveInventoryProduct(创建新的库存项目接收库存(S))服务。

然后还会调好多的别的服务,我就实在没有心思一一仔细阅读了,最后还需要调用这个createReturnStatus服务,这个服务就跟流程日志一样,不管是创建还是针对该对象进行的任何修改操作,都会新增一条这样的状态,用于跟踪退货单的流程。

04-14 19:38