import java.sql.Timestamp
import java.math.BigDecimal
import java.sql.PreparedStatement
import java.sql.ResultSet
import org.compiere.util.DB
import org.compiere.model.MUser
import org.compiere.util.Env
//@script:groovy:getQuotationForOrder
def now = new Timestamp(System.currentTimeMillis());
int client=Env.getContextAsInt(A_Ctx, "AD_Client_ID");
int org=Env.getContextAsInt(A_Ctx, "AD_Org_ID");
int uid=Env.getContextAsInt(A_Ctx, "#AD_User_ID");
// int parent_id=((Integer)A_PO.get_Value("出貨通知_ID")).intValue();
if (A_Value != null ){
int id = ((Integer) A_Value).intValue();
if (id==1000000 && (Integer)A_Tab.getValue("入庫單_ID")!=null ) { // 拉到 [確認] 才做
int ordid=((Integer)A_Tab.getValue("入庫單_ID")).intValue();
String sql1 ="SELECT a.\"品號_id\", a.\"入庫數量\", a.\"庫別_id\", a.\"儲位_id\", a.\"入庫單明細_id\" FROM \"入庫單明細\" a WHERE a.\"入庫單_id\"=? ";
PreparedStatement pstmt1 = DB.prepareStatement(sql1, null);
pstmt1.setInt(1, ordid);
ResultSet rs1 = pstmt1.executeQuery();
while (rs1.next()) {
int lineid = rs1. getInt ("入庫單明細_id");
int pdid = rs1.getInt ("品號_id");
int whid = rs1.getInt("庫別_id");
int locid = rs1.getInt("儲位_id");
BigDecimal qty1 = rs1. getBigDecimal ("入庫數量");
//-----2------
String sql2 ="SELECT a.\"數量\", \"儲位內容_id\" FROM \"儲位內容\" a INNER JOIN \"儲位\" b ON (a.\"儲位_id\"=b.\"儲位_id\") INNER JOIN \"庫別\" c ON (b.\"庫別_id\"=c.\"庫別_id\") WHERE a.\"品號_id\"=? AND c.\"庫別_id\"=? ";
PreparedStatement pstmt2 = DB.prepareStatement(sql2, null);
pstmt2.setInt(1, pdid );
pstmt2.setInt(2, whid);
ResultSet rs2 = pstmt2.executeQuery();
int locqtyid= 0;
BigDecimal qty2 = null;
if (rs2.next()) {
locqtyid= rs2. getInt ("儲位內容_id");
qty2 = rs2. getBigDecimal ("數量");
}
rs2.close();
pstmt2.close();
//----2---- End
if (qty2==null) {
qty2 = new BigDecimal(0);
if (whid > 0 && locid > 0){
// DB.getSQLValueString(null,"select \"序號\" from \"報價單明細\" where \"報價單明細_id\"= ?", id);
String uu1=DB.getSQLValueString(null, "SELECT adempiere.Generate_UUID() AS uu1 " );
int id1= DB.getSQLValue(null, "SELECT adempiere.NextID('儲位內容', 'N') AS id1 " );
String ins="INSERT INTO \"儲位內容\" (\"數量\", \"儲位內容_id\", \"儲位內容_uu\", \"儲位_id\", AD_Client_ID, AD_Org_ID, CreatedBy, UpdatedBy, Created, Updated, IsActive) VALUES ("+qty2+","+id1+", '"+uu1+"',"+locid+","+client+","+org+","+uid+","+uid+",'"+now+"','"+now+"','Y' )"
DB.executeUpdate (ins );
// VALUES( ?,?,?,?,?,?,?,?,?,?,? )";
// DB.executeUpdate(sql, new Object[] { shipper_id, inout_id }, false, null);
// DB.executeUpdate (ins, Object[] { qty2, id1, uu1, locid, client, org, uid, uid, now, now, "Y" }, false, null);
BigDecimal qty3 = qty2.add(qty1);
sqlx = "UPDATE \"入庫單明細\" SET \"原庫存數量\"='"+ qty2 +"',\"更新後庫存數量\"='"+qty3 +"' WHERE \"入庫單明細_id\"="+lineid +" ";
DB.executeUpdate (sqlx );
}
}else{
BigDecimal qty3 = qty2.add(qty1);
sqlx = "UPDATE \"入庫單明細\" SET \"原庫存數量\"='"+ qty2 +"',\"更新後庫存數量\"='"+qty3 +"' WHERE \"入庫單明細_id\"="+lineid +" ";
DB.executeUpdate (sqlx );
sqly = "UPDATE \"儲位內容\" SET \"數量\"='"+ qty3 +"' WHERE \"儲位內容_id\"="+locid +" ";
DB.executeUpdate (sqly );
} // if (qty2==null)
} // while (rs1.next()) {
rs1.close();
pstmt1.close();
} // if (id==1000000
} // if (A_Value != null ){
result=""
沒有留言:
張貼留言