|
|
@@ -1,7 +1,10 @@
|
|
|
package com.uas.eis.core;
|
|
|
|
|
|
+import com.uas.eis.config.SynaConfig;
|
|
|
import com.uas.eis.dao.BaseDao;
|
|
|
import com.uas.eis.entity.Purchase;
|
|
|
+import com.uas.eis.entity.PurchaseChange;
|
|
|
+import com.uas.eis.entity.PurchaseChangeDetail;
|
|
|
import com.uas.eis.entity.PurchaseDetail;
|
|
|
import io.xlate.edi.schema.EDISchemaException;
|
|
|
import io.xlate.edi.schema.Schema;
|
|
|
@@ -10,6 +13,7 @@ import io.xlate.edi.stream.EDIOutputFactory;
|
|
|
import io.xlate.edi.stream.EDIStreamConstants;
|
|
|
import io.xlate.edi.stream.EDIStreamException;
|
|
|
import io.xlate.edi.stream.EDIStreamWriter;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
|
import java.io.ByteArrayOutputStream;
|
|
|
import java.io.OutputStream;
|
|
|
@@ -18,28 +22,57 @@ import java.util.Date;
|
|
|
|
|
|
public class X12Converter {
|
|
|
private BaseDao baseDao;
|
|
|
- public String convertToX12(Purchase po,String ediId) throws EDISchemaException, EDIStreamException {
|
|
|
+
|
|
|
+ private SynaConfig synaConfig;
|
|
|
+
|
|
|
+ static final String X12_VERSION = "00401";
|
|
|
+
|
|
|
+ private EDIStreamWriter newEDIWriter(OutputStream stream) throws EDISchemaException {
|
|
|
EDIOutputFactory factory = EDIOutputFactory.newFactory();
|
|
|
- OutputStream stream=new ByteArrayOutputStream();
|
|
|
SchemaFactory schemaFactory = SchemaFactory.newFactory();
|
|
|
- Schema controlSchema = schemaFactory.getControlSchema(EDIStreamConstants.Standards.X12, new String[] { "00401" });
|
|
|
+ Schema controlSchema = schemaFactory.getControlSchema(EDIStreamConstants.Standards.X12, new String[] { X12_VERSION });
|
|
|
EDIStreamWriter writer = factory.createEDIStreamWriter(stream);
|
|
|
-
|
|
|
writer.setControlSchema(controlSchema);
|
|
|
+ return writer;
|
|
|
+ }
|
|
|
+ private void startSegment(EDIStreamWriter writer,String ediId) throws EDIStreamException {
|
|
|
+ writer.writeStartSegment("ISA")
|
|
|
+ .writeElement("00")
|
|
|
+ .writeElement(" ")
|
|
|
+ .writeElement("00")
|
|
|
+ .writeElement(" ")
|
|
|
+ .writeElement("ZZ")
|
|
|
+ .writeElement("WORLDSHINE ")
|
|
|
+ .writeElement("ZZ")
|
|
|
+ .writeElement("SYNAPTICST ")
|
|
|
+ .writeElement(new SimpleDateFormat("yyMMdd").format(new Date())) // GS04 (日期)
|
|
|
+ .writeElement(new SimpleDateFormat("HHmm").format(new Date())) // GS05 (时间)
|
|
|
+ .writeElement("U")
|
|
|
+ .writeElement(X12_VERSION)
|
|
|
+ .writeElement(ediId)
|
|
|
+ .writeElement("0")
|
|
|
+ .writeElement("T")//T,表示测试数据;P,表示生产数据,这里是T
|
|
|
+ .writeElement("^")
|
|
|
+ .writeEndSegment();
|
|
|
+ }
|
|
|
+
|
|
|
+ public String convertToX12(Purchase po,String ediId) throws EDISchemaException, EDIStreamException {
|
|
|
+ OutputStream stream=new ByteArrayOutputStream();
|
|
|
+ EDIStreamWriter writer = newEDIWriter(stream);
|
|
|
+ //开始
|
|
|
writer.startInterchange();
|
|
|
+
|
|
|
startSegment(writer,ediId);
|
|
|
- writer.writeStartSegment("GS");
|
|
|
- System.out.println("日期:"+new SimpleDateFormat("yyyyMMdd").format(new Date()));
|
|
|
- System.out.println("时间:"+new SimpleDateFormat("HHmm").format(new Date()));
|
|
|
- writer.writeElement("PO")
|
|
|
+ //PO
|
|
|
+ writer.writeStartSegment("GS").writeElement("PO")
|
|
|
.writeElement("WORLDSHINE") // GS02
|
|
|
.writeElement("SYNAPTICSD") // GS03
|
|
|
.writeElement(new SimpleDateFormat("yyyyMMdd").format(new Date())) // GS04 (日期)
|
|
|
.writeElement(new SimpleDateFormat("HHmm").format(new Date())) // GS05 (时间)
|
|
|
.writeElement(String.valueOf(po.getPu_id())) // GS06
|
|
|
- .writeElement("T") // GS07
|
|
|
- .writeElement("004010")
|
|
|
- .writeEndSegment();; // GS08
|
|
|
+ .writeElement("T") // T-运输数据协调委员会(TDCC),X-公认标准委员会
|
|
|
+ .writeElement("004010")//X12版本
|
|
|
+ .writeEndSegment(); // GS08
|
|
|
int txCount = 1;
|
|
|
writer.writeStartSegment("ST").writeElement("850").writeElement("4221").writeEndSegment(); // 事务集控制编号
|
|
|
txCount++; // 开始采购订单 (BEG)
|
|
|
@@ -49,12 +82,12 @@ public class X12Converter {
|
|
|
.writeElement("DS") // 采购订单类型
|
|
|
.writeElement(po.getPu_code()) // 采购订单编号
|
|
|
.writeElement("") // 释放编号
|
|
|
- .writeElement(new SimpleDateFormat("yyyyMMdd").format(po.getPu_date())).writeEndSegment();; // 日期
|
|
|
+ .writeElement(new SimpleDateFormat("yyyyMMdd").format(po.getPu_date())).writeEndSegment(); // 日期
|
|
|
txCount++;
|
|
|
|
|
|
writer.writeStartSegment("CUR")
|
|
|
.writeElement("SE")
|
|
|
- .writeElement(po.getPu_currency()).writeEndSegment();; // 日期
|
|
|
+ .writeElement(po.getPu_currency()).writeEndSegment();
|
|
|
txCount++;
|
|
|
|
|
|
writer.writeStartSegment("N1")
|
|
|
@@ -66,21 +99,18 @@ public class X12Converter {
|
|
|
|
|
|
writer.writeStartSegment("N1")
|
|
|
.writeElement("BT")
|
|
|
- .writeElement("香港新界沙田小沥源安平街8号伟达中心902室")
|
|
|
+ .writeElement(synaConfig.getBill_to_address())//收票地址
|
|
|
.writeElement("92")
|
|
|
- .writeElement("WTECH-67908").writeEndSegment();
|
|
|
+ .writeElement(synaConfig.getBTEDILocationCode()).writeEndSegment();
|
|
|
txCount++;
|
|
|
|
|
|
writer.writeStartSegment("REF")
|
|
|
.writeElement("SA")
|
|
|
.writeElement(po.getPu_isr()) .writeEndSegment();
|
|
|
txCount++;
|
|
|
- // 采购订单行项
|
|
|
- int groupCount=0;
|
|
|
for(PurchaseDetail pd:po.getItems()){
|
|
|
- groupCount++;
|
|
|
writer.writeStartSegment("PO1")
|
|
|
- .writeElement(String.valueOf(groupCount)) // 分配编号
|
|
|
+ .writeElement(String.valueOf(pd.getPd_detno())) // 分配编号
|
|
|
.writeElement(String.valueOf(pd.getPd_qty())) // 数量
|
|
|
.writeElement("EA") // 单位
|
|
|
.writeElement(String.valueOf(pd.getPd_price())) // 单价
|
|
|
@@ -88,11 +118,16 @@ public class X12Converter {
|
|
|
.writeElement("VP") // 产品ID限定符
|
|
|
.writeElement(pd.getPr_orispeccode()) // 供应商零件编号
|
|
|
.writeElement("BP")
|
|
|
- .writeElement(pd.getPd_prodcode()).writeEndSegment();; // 产品描述
|
|
|
+ .writeElement(pd.getPd_prodcode()).writeEndSegment();
|
|
|
txCount++;
|
|
|
writer.writeStartSegment("REF")
|
|
|
.writeElement("PR")
|
|
|
- .writeElement(pd.getPd_quote()) .writeEndSegment();; // 产品描述
|
|
|
+ .writeElement(pd.getPd_quote()).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+ writer.writeStartSegment("FOB")
|
|
|
+ .writeElement("DF")
|
|
|
+ .writeElement("ZZ")
|
|
|
+ .writeElement(po.getPu_article()).writeEndSegment();// Shipping Instruction
|
|
|
txCount++;
|
|
|
writer.writeStartSegment("SCH")
|
|
|
.writeElement(String.valueOf(pd.getPd_qty()))
|
|
|
@@ -105,7 +140,7 @@ public class X12Converter {
|
|
|
}
|
|
|
// 订单小计 (CTT)
|
|
|
writer.writeStartSegment("CTT")
|
|
|
- .writeElement(String.valueOf(groupCount)).writeEndSegment();// 明细行数
|
|
|
+ .writeElement(String.valueOf(po.getItems().size())).writeEndSegment();// 明细行数
|
|
|
txCount++;
|
|
|
// 结束事务集 (SE) - 需要手动计算段数量
|
|
|
writer.writeStartSegment("SE")
|
|
|
@@ -123,27 +158,110 @@ public class X12Converter {
|
|
|
writer.close();
|
|
|
return stream.toString();
|
|
|
}
|
|
|
- private void startSegment(EDIStreamWriter writer,String ediId) throws EDIStreamException {
|
|
|
- writer.writeStartSegment("ISA")
|
|
|
- .writeElement("00")
|
|
|
- .writeElement(" ")
|
|
|
- .writeElement("00")
|
|
|
- .writeElement(" ")
|
|
|
- .writeElement("ZZ")
|
|
|
- .writeElement("WORLDSHINE ")
|
|
|
- .writeElement("ZZ")
|
|
|
- .writeElement("SYNAPTICST ")
|
|
|
- .writeElement(new SimpleDateFormat("yyMMdd").format(new Date())) // GS04 (日期)
|
|
|
+
|
|
|
+ public String convertToX12(PurchaseChange pc, String ediId) throws EDISchemaException, EDIStreamException {
|
|
|
+ OutputStream stream=new ByteArrayOutputStream();
|
|
|
+ EDIStreamWriter writer = newEDIWriter(stream);
|
|
|
+
|
|
|
+ writer.startInterchange();
|
|
|
+ startSegment(writer,ediId);
|
|
|
+
|
|
|
+ writer.writeStartSegment("GS").writeElement("PC")
|
|
|
+ .writeElement("WORLDSHINE") // GS02
|
|
|
+ .writeElement("SYNAPTICSD") // GS03
|
|
|
+ .writeElement(new SimpleDateFormat("yyyyMMdd").format(new Date())) // GS04 (日期)
|
|
|
.writeElement(new SimpleDateFormat("HHmm").format(new Date())) // GS05 (时间)
|
|
|
- .writeElement("U")
|
|
|
- .writeElement("00501")
|
|
|
- .writeElement(ediId)
|
|
|
- .writeElement("0")
|
|
|
- .writeElement("T")//T,表示测试数据;P,表示生产数据,这里是T
|
|
|
- .writeElement("^")
|
|
|
- .writeEndSegment();
|
|
|
- }
|
|
|
- public void convertToX12() throws EDISchemaException, EDIStreamException {
|
|
|
+ .writeElement(String.valueOf(pc.getPc_id())) // String.valueOf(po.getPu_id())
|
|
|
+ .writeElement("X") // T-运输数据协调委员会(TDCC),X-公认标准委员会
|
|
|
+ .writeElement("004010")//X12版本
|
|
|
+ .writeEndSegment(); // GS08
|
|
|
+ int txCount = 1;
|
|
|
+ writer.writeStartSegment("ST").writeElement("860").writeElement("0001").writeEndSegment(); // 事务集控制编号
|
|
|
+ txCount++;
|
|
|
+ writer.writeStartSegment("BCH")
|
|
|
+ .writeElement("04") // 变更
|
|
|
+ .writeElement("CP") // Change to Purchase Order
|
|
|
+ .writeElement(pc.getPu_code()) // 采购订单编号
|
|
|
+ .writeElement("") // 释放编号
|
|
|
+ .writeElement("") // 释放编号
|
|
|
+ .writeElement(new SimpleDateFormat("yyyyMMdd").format(pc.getPu_date())).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+
|
|
|
+ writer.writeStartSegment("N1")
|
|
|
+ .writeElement("ST")
|
|
|
+ .writeElement(pc.getPu_shipaddresscode())
|
|
|
+ .writeElement("92")
|
|
|
+ .writeElement(pc.getPc_deliveryparty()).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+
|
|
|
+ writer.writeStartSegment("N1")
|
|
|
+ .writeElement("BT")
|
|
|
+ .writeElement(synaConfig.getBill_to_address())
|
|
|
+ .writeElement("92")
|
|
|
+ .writeElement(synaConfig.getBTEDILocationCode()).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+
|
|
|
+ writer.writeStartSegment("REF")
|
|
|
+ .writeElement("SA")
|
|
|
+ .writeElement(pc.getPc_isr()) .writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+ for(PurchaseChangeDetail pcd:pc.getItems()){
|
|
|
+ writer.writeStartSegment("POC")
|
|
|
+ .writeElement(pcd.getPcd_so())//新思单号 香港华商龙做变更单时填写
|
|
|
+ /*Code specifying the type of change to the line item 变更类型
|
|
|
+ RS 交期变更
|
|
|
+ PC Price Change
|
|
|
+ QD Quantity Decrease 数量调增
|
|
|
+ QI Quantity Increase 数量调减
|
|
|
+
|
|
|
+ PQ Unit Price/Quantity Change 价格+数量
|
|
|
+ PR Unit Price/Reschedule Change 价格+交期
|
|
|
+ RQ Reschedule/Quantity Change 交期+数量
|
|
|
|
|
|
+ MU (Multiple) For Unit Price Quantity Reschedule Change 价格+数量+交期
|
|
|
+ * */
|
|
|
+ .writeElement(pcd.getChangetype())
|
|
|
+ .writeElement(String.valueOf(pcd.getPcd_newqty()))//数量
|
|
|
+ .writeElement(String.valueOf(pcd.getPd_leftqty()))//Quantity Left to Receive 剩余数量
|
|
|
+ .writeElement("EA")
|
|
|
+ .writeElement(String.valueOf(pcd.getPcd_newprice())) // 单价
|
|
|
+ .writeElement("VP")
|
|
|
+ .writeElement(pcd.getPr_orispeccode())
|
|
|
+ .writeElement("BP")
|
|
|
+ .writeElement(pcd.getPcd_newprodcode()).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+ writer.writeStartSegment("REF")
|
|
|
+ .writeElement("PR")
|
|
|
+ .writeElement(pcd.getQuote()).writeEndSegment();
|
|
|
+ txCount++;
|
|
|
+ writer.writeStartSegment("SCH")
|
|
|
+ .writeElement(String.valueOf(pcd.getPcd_newqty()))//数量
|
|
|
+ .writeElement("EA")
|
|
|
+ .writeElement("")//SCH03
|
|
|
+ .writeElement("")//SCH04
|
|
|
+ /*SCH05 Date/Time Qualifier
|
|
|
+ * */
|
|
|
+ .writeElement("002")//Delivery Requested
|
|
|
+ .writeElement(new SimpleDateFormat("yyyyMMdd").format(pcd.getPcd_newdelivery())).writeEndSegment();//交期
|
|
|
+ txCount++;
|
|
|
+ }
|
|
|
+ // 订单小计 (CTT)
|
|
|
+ writer.writeStartSegment("CTT")
|
|
|
+ .writeElement(String.valueOf(pc.getItems().size())).writeEndSegment();// 明细行数
|
|
|
+ txCount++;
|
|
|
+ // 结束事务集 (SE) - 需要手动计算段数量
|
|
|
+ writer.writeStartSegment("SE")
|
|
|
+ .writeElement(String.valueOf(txCount)) // 段数量 (ST到SE之间的段数,包括ST和SE)
|
|
|
+ .writeElement("301").writeEndSegment(); // 事务集控制编号 (与ST中的一致)
|
|
|
+
|
|
|
+ writer.writeStartSegment("GE")
|
|
|
+ .writeElement("1")
|
|
|
+ .writeElement(String.valueOf(pc.getPc_id())) .writeEndSegment();
|
|
|
+ writer.writeStartSegment("IEA")
|
|
|
+ .writeElement("1")
|
|
|
+ .writeElement(ediId) .writeEndSegment();
|
|
|
+ writer.endInterchange();
|
|
|
+ writer.close();
|
|
|
+ return stream.toString();
|
|
|
}
|
|
|
}
|