1.無需設Discount,LineNetAmt,因爲OrderLine beforeSave()會設定。
2.setC_UOM_ID放到setPrice()之後,如果UOM不想從產品取。
3.如果價格不是自定義,可以直接調用setPrice().
-----
POLineCreate .java
package org.compiere.process;
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigDecimal;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MPriceList;
import org.compiere.model.MProduct;
import org.compiere.model.MUOMConversion;
import org.compiere.util.DB;
import org.compiere.util.Env;
/** */ /**
* Delete PO Lines
*
* @author Stephy Shum
* @version $Id: POLineCreate.java,v 1.2 2007/10/17 trifonnt Exp $
*/
public class POLineCreate extends SvrProcess ... {
/** *//**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
...{
}
/** *//**
* Perrform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws Exception
...{
String directory = org.compiere.Compiere.getCompiereHome();
BufferedReader in = new BufferedReader(new FileReader(directory+"/data/import/POLinesCreate.csv")); //read POLinesDelete.csv
String s=null;
String documentno="";
MOrder po=null;
int C_Order_ID=0;
int AD_Client_ID=Env.getAD_Client_ID(Env.getCtx());
int created=0;
int nocreated=0;
while ((s = in.readLine()) != null)...{
System.out.println("===s==="+s);
String splits[]=s.split(",");
//splits[0] - Document no
//splits[1] - Purchase For
//splits[2] - Product
//splits[3] - UOM
//splits[4] - Quantity
//splits[5] - Price
if(!splits[0].equals(documentno))...{ //diffrence documentno
documentno=splits[0];
C_Order_ID=DB.getSQLValue(get_TrxName(),"SELECT C_Order_ID FROM C_Order WHERE Documentno='"+documentno+"' AND AD_Client_ID=?",AD_Client_ID);
if(C_Order_ID<=0)
continue; //May be line 1
po=new MOrder(Env.getCtx(),C_Order_ID,get_TrxName());
}
//Create PO Lines
MOrderLine poline=new MOrderLine(po);
int C_PurchaseFor_ID=DB.getSQLValue(get_TrxName(),"SELECT C_PurchaseFor_ID FROM C_PurchaseFor where Name='"+splits[1]+"' AND AD_Client_ID=?",AD_Client_ID);
int M_Product_ID=DB.getSQLValue(get_TrxName(),"SELECT M_Product_ID FROM M_Product WHERE value='"+splits[2]+"' AND AD_Client_ID=?",AD_Client_ID);
int C_UOM_ID=DB.getSQLValue(get_TrxName(),"SELECT C_UOM_ID FROM C_UOM WHERE name='"+splits[3]+"' AND AD_Client_ID=?",AD_Client_ID);
BigDecimal qty=new BigDecimal(splits[4]);
BigDecimal price=new BigDecimal(splits[5]);
poline.setM_Product_ID(M_Product_ID);//1
//poline.setM_AttributeSetInstance_ID();
poline.setQtyEntered(qty);
poline.setQtyOrdered(qty);
poline.setPriceActual(price);
poline.setPriceList(price);
poline.setPriceLimit(price);
poline.setPriceEntered(price.setScale(2, BigDecimal.ROUND_HALF_UP));
poline.setC_PurchaseFor_ID(C_PurchaseFor_ID); // @Trifon
poline.setLine(DB.getSQLValue(get_TrxName(),"SELECT NVL(MAX(Line),0)+10 AS DefaultValue FROM C_OrderLine WHERE C_Order_ID=?",C_Order_ID));
poline.setC_UOM_ID(C_UOM_ID);
// set OK
// UOM Conversion?
MProduct product=new MProduct(Env.getCtx(),M_Product_ID,get_TrxName());
if(product.getC_UOM_ID()!=C_UOM_ID)...{
BigDecimal PriceActual=MUOMConversion.convertProductFrom (Env.getCtx(), M_Product_ID,
C_UOM_ID, poline.getPriceActual());
BigDecimal QtyOrdered = MUOMConversion.convertProductFrom (Env.getCtx(), M_Product_ID,
C_UOM_ID, qty);
if(PriceActual!=null)
poline.setPriceEntered(PriceActual.setScale(2, BigDecimal.ROUND_HALF_UP));
if(QtyOrdered!=null)
poline.setQtyOrdered(QtyOrdered);
//no check price limited
}
if(poline.save())...{
System.out.println("====OK====");
created++;
}
else...{
System.out.println("====save error!====");
nocreated++;
}
}
return "Created "+created+" no Created "+nocreated;
}
}
package org.compiere.process;
import java.io.BufferedReader;
import java.io.FileReader;
import java.math.BigDecimal;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MPriceList;
import org.compiere.model.MProduct;
import org.compiere.model.MUOMConversion;
import org.compiere.util.DB;
import org.compiere.util.Env;
/** */ /**
* Delete PO Lines
*
* @author Stephy Shum
* @version $Id: POLineCreate.java,v 1.2 2007/10/17 trifonnt Exp $
*/
public class POLineCreate extends SvrProcess ... {
/** *//**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
...{
}
/** *//**
* Perrform process.
* @return Message
* @throws Exception
*/
protected String doIt() throws Exception
...{
String directory = org.compiere.Compiere.getCompiereHome();
BufferedReader in = new BufferedReader(new FileReader(directory+"/data/import/POLinesCreate.csv")); //read POLinesDelete.csv
String s=null;
String documentno="";
MOrder po=null;
int C_Order_ID=0;
int AD_Client_ID=Env.getAD_Client_ID(Env.getCtx());
int created=0;
int nocreated=0;
while ((s = in.readLine()) != null)...{
System.out.println("===s==="+s);
String splits[]=s.split(",");
//splits[0] - Document no
//splits[1] - Purchase For
//splits[2] - Product
//splits[3] - UOM
//splits[4] - Quantity
//splits[5] - Price
if(!splits[0].equals(documentno))...{ //diffrence documentno
documentno=splits[0];
C_Order_ID=DB.getSQLValue(get_TrxName(),"SELECT C_Order_ID FROM C_Order WHERE Documentno='"+documentno+"' AND AD_Client_ID=?",AD_Client_ID);
if(C_Order_ID<=0)
continue; //May be line 1
po=new MOrder(Env.getCtx(),C_Order_ID,get_TrxName());
}
//Create PO Lines
MOrderLine poline=new MOrderLine(po);
int C_PurchaseFor_ID=DB.getSQLValue(get_TrxName(),"SELECT C_PurchaseFor_ID FROM C_PurchaseFor where Name='"+splits[1]+"' AND AD_Client_ID=?",AD_Client_ID);
int M_Product_ID=DB.getSQLValue(get_TrxName(),"SELECT M_Product_ID FROM M_Product WHERE value='"+splits[2]+"' AND AD_Client_ID=?",AD_Client_ID);
int C_UOM_ID=DB.getSQLValue(get_TrxName(),"SELECT C_UOM_ID FROM C_UOM WHERE name='"+splits[3]+"' AND AD_Client_ID=?",AD_Client_ID);
BigDecimal qty=new BigDecimal(splits[4]);
BigDecimal price=new BigDecimal(splits[5]);
poline.setM_Product_ID(M_Product_ID);//1
//poline.setM_AttributeSetInstance_ID();
poline.setQtyEntered(qty);
poline.setQtyOrdered(qty);
poline.setPriceActual(price);
poline.setPriceList(price);
poline.setPriceLimit(price);
poline.setPriceEntered(price.setScale(2, BigDecimal.ROUND_HALF_UP));
poline.setC_PurchaseFor_ID(C_PurchaseFor_ID); // @Trifon
poline.setLine(DB.getSQLValue(get_TrxName(),"SELECT NVL(MAX(Line),0)+10 AS DefaultValue FROM C_OrderLine WHERE C_Order_ID=?",C_Order_ID));
poline.setC_UOM_ID(C_UOM_ID);
// set OK
// UOM Conversion?
MProduct product=new MProduct(Env.getCtx(),M_Product_ID,get_TrxName());
if(product.getC_UOM_ID()!=C_UOM_ID)...{
BigDecimal PriceActual=MUOMConversion.convertProductFrom (Env.getCtx(), M_Product_ID,
C_UOM_ID, poline.getPriceActual());
BigDecimal QtyOrdered = MUOMConversion.convertProductFrom (Env.getCtx(), M_Product_ID,
C_UOM_ID, qty);
if(PriceActual!=null)
poline.setPriceEntered(PriceActual.setScale(2, BigDecimal.ROUND_HALF_UP));
if(QtyOrdered!=null)
poline.setQtyOrdered(QtyOrdered);
//no check price limited
}
if(poline.save())...{
System.out.println("====OK====");
created++;
}
else...{
System.out.println("====save error!====");
nocreated++;
}
}
return "Created "+created+" no Created "+nocreated;
}
}