@Override
protected String doIt() throws Exception
{
// Load RMA
MRMA rma = new MRMA(getCtx(), rmaId, get_TrxName());
// Load Original Order
MOrder originalOrder = rma.getOriginalOrder();
if (rma.get_ID() == 0)
{
throw new Exception("No RMA defined");
}
if (originalOrder == null)
{
throw new Exception("Could not load the original order");
}
// Create new order and set the different values based on original order/RMA doc
MOrder order = new MOrder(getCtx(), 0, get_TrxName());
order.setAD_Org_ID(rma.getAD_Org_ID());
order.setC_BPartner_ID(originalOrder.getC_BPartner_ID());
order.setC_BPartner_Location_ID(originalOrder.getC_BPartner_Location_ID());
order.setAD_User_ID(originalOrder.getAD_User_ID());
order.setBill_BPartner_ID(originalOrder.getBill_BPartner_ID());
order.setBill_Location_ID(originalOrder.getBill_Location_ID());
order.setBill_User_ID(originalOrder.getBill_User_ID());
order.setSalesRep_ID(rma.getSalesRep_ID());
order.setM_PriceList_ID(originalOrder.getM_PriceList_ID());
order.setM_Warehouse_ID(originalOrder.getM_Warehouse_ID());
// 25-09-2009 rgliddon: Made doc type target explicitly reference Return Material
order.setC_DocTypeTarget_ID(1000073); // Return Material
order.setC_PaymentTerm_ID(originalOrder.getC_PaymentTerm_ID());
order.setDeliveryRule(originalOrder.getDeliveryRule());
if (!order.save())
{
throw new IllegalStateException("Could not create order");
}
MRMALine lines[] = rma.getLines(true);
for (MRMALine line : lines)
{
if (line.getShipLine() != null && line.getShipLine().getC_OrderLine_ID() != 0)
{
// Create order lines if the RMA Doc line has a shipment line
MOrderLine orderLine = new MOrderLine(order);
MOrderLine originalOLine = new MOrderLine(getCtx(), line.getShipLine().getC_OrderLine_ID(), null);
orderLine.setAD_Org_ID(line.getAD_Org_ID());
orderLine.setM_Product_ID(originalOLine.getM_Product_ID());
orderLine.setM_AttributeSetInstance_ID(originalOLine.getM_AttributeSetInstance_ID());
orderLine.setC_UOM_ID(originalOLine.getC_UOM_ID());
orderLine.setC_Tax_ID(originalOLine.getC_Tax_ID());
orderLine.setM_Warehouse_ID(originalOLine.getM_Warehouse_ID());
orderLine.setC_Currency_ID(originalOLine.getC_Currency_ID());
orderLine.setQty(line.getQty());
orderLine.setC_Project_ID(originalOLine.getC_Project_ID());
orderLine.setC_Activity_ID(originalOLine.getC_Activity_ID());
orderLine.setC_Campaign_ID(originalOLine.getC_Campaign_ID());
orderLine.setPrice();
orderLine.setPrice(line.getAmt());
if (!orderLine.save())
{
throw new IllegalStateException("Could not create Order Line");
}
}
}
rma.setC_Order_ID(order.getC_Order_ID());
if (!rma.save())
{
throw new IllegalStateException("Could not update RMA document");
}
return "Order Created: " + order.getDocumentNo();