|
@@ -0,0 +1,286 @@
|
|
|
+package service;
|
|
|
+
|
|
|
+import beans.accountbalance.Accountbalance;
|
|
|
+import beans.accountbalance.CashbillEntity;
|
|
|
+import beans.parameter.Parameter;
|
|
|
+import beans.remind.Remind;
|
|
|
+import beans.uploaderpdata.UploadDataToERP_HY;
|
|
|
+import com.alibaba.fastjson.JSONArray;
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import common.Controller;
|
|
|
+import common.ServiceController;
|
|
|
+import common.YosException;
|
|
|
+import common.data.Row;
|
|
|
+import common.data.Rows;
|
|
|
+import common.data.RowsMap;
|
|
|
+import common.data.SQLFactory;
|
|
|
+import common.parameter.properties;
|
|
|
+import org.apache.commons.lang.StringUtils;
|
|
|
+import org.apache.log4j.Logger;
|
|
|
+import restcontroller.webmanage.executorService.Executor;
|
|
|
+import utility.ERPDocking;
|
|
|
+import utility.tools.WebRequest;
|
|
|
+
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+public class StockAutoDockDispatchService extends ServiceController {
|
|
|
+
|
|
|
+ private static Logger logger = Logger.getLogger(DispatchAutoCloseService.class);
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void serviceRun() throws YosException {
|
|
|
+ //
|
|
|
+ System.out.println("销售出库单自动同步任务");
|
|
|
+ Properties yosProperties = properties.getYosProperties();
|
|
|
+ if (yosProperties.getProperty("system.runmode.debug").equalsIgnoreCase("false")) {
|
|
|
+
|
|
|
+ ArrayList<String> sqlList = new ArrayList<>();
|
|
|
+ ERPDocking erpDocking = new ERPDocking();
|
|
|
+ JSONArray ErpSainvoices = erpDocking.queryErpStockBill(10000, 1,"销售出库");
|
|
|
+ JSONArray flags=new JSONArray();
|
|
|
+ String siteid = "ccyosg";
|
|
|
+ if (!ErpSainvoices.isEmpty()) {
|
|
|
+ for (Object ErpSainvoice : ErpSainvoices) {
|
|
|
+ JSONObject erpSainvoice = (JSONObject) ErpSainvoice;
|
|
|
+ // 经销商编号
|
|
|
+ String fagentnum = erpSainvoice.getString("fagentnum");
|
|
|
+ long sys_enterpriseid = 0;
|
|
|
+ if (dbConnect.runSqlQuery("select sys_enterpriseid from sa_agents where siteid='ccyosg' and agentnum='" + fagentnum + "'").isNotEmpty()) {
|
|
|
+ sys_enterpriseid = dbConnect.runSqlQuery("select sys_enterpriseid from sa_agents where siteid='ccyosg' and agentnum='" + fagentnum + "'").get(0).getLong("sys_enterpriseid");
|
|
|
+ } else {
|
|
|
+ JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "4");
|
|
|
+ flag.put("fupdatewarnlog", "yos中不存在【" + fagentnum + "】经销商");
|
|
|
+ flags.add(flag);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ //单号
|
|
|
+ String fbillnum = erpSainvoice.getString("fbillnum");
|
|
|
+ // 单据日期
|
|
|
+ String fdate = erpSainvoice.getString("fdate");
|
|
|
+ // 会计期间
|
|
|
+ // String fperiod = content.getString("fperiod");
|
|
|
+ // 红蓝字
|
|
|
+ String frb = erpSainvoice.getString("frb");
|
|
|
+ // 表头仓库
|
|
|
+ // String fstockno = content.getString("fstockno");
|
|
|
+ // 单据类型明细
|
|
|
+ //String ftypemx = content.getString("ftypemx");
|
|
|
+ // 单据状态
|
|
|
+ String fstatus = erpSainvoice.getString("fstatus");
|
|
|
+ // 制单人
|
|
|
+ String createby = erpSainvoice.getString("createby");
|
|
|
+ // 制单日期
|
|
|
+ String createdate = erpSainvoice.getString("createdate");
|
|
|
+// // 审核人
|
|
|
+// String checkby = erpSainvoice.getString("checkby");
|
|
|
+// // 审核日期
|
|
|
+// String checkdate = erpSainvoice.getString("checkdate");
|
|
|
+ // 备注
|
|
|
+ String fnotes = erpSainvoice.getString("fnote");
|
|
|
+ String fsourcebillno = erpSainvoice.getString("fsourcebillno");
|
|
|
+ String fsourcetype = erpSainvoice.getString("fsourcetype");
|
|
|
+ long sa_dispatchid=0;
|
|
|
+ if(StringUtils.isNotBlank(fsourcebillno)){
|
|
|
+ if (dbConnect.runSqlQuery("select sa_dispatchid from sa_dispatch where siteid='ccyosg' and billno='" + fsourcebillno + "'").isNotEmpty()) {
|
|
|
+ sa_dispatchid = dbConnect.runSqlQuery("select sa_dispatchid from sa_dispatch where siteid='ccyosg' and billno='" + fsourcebillno + "'").get(0).getLong("sa_dispatchid");
|
|
|
+ } else {
|
|
|
+ JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "4");
|
|
|
+ flag.put("fupdatewarnlog", "yos中不存在对应的发货单");
|
|
|
+ flags.add(flag);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+ }else {
|
|
|
+ JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "4");
|
|
|
+ flag.put("fupdatewarnlog", "yos中不存在对应的发货单");
|
|
|
+ flags.add(flag);
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ Long sa_cashbillid = 0L;
|
|
|
+
|
|
|
+ Rows stockbillRows = dbConnect.runSqlQuery("select * from st_stockbill where siteid='ccyosg' and status='审核' and billno='" + fbillnum + "'");
|
|
|
+ Long st_stockbillid = 0L;
|
|
|
+ if (stockbillRows.isEmpty() && "审核".equals(fstatus)) {
|
|
|
+ /**
|
|
|
+ * DRP不存在销售出库单,且中间表状态为审核且不在删除状态时,DRP中需新增该销售出库单
|
|
|
+ */
|
|
|
+ SQLFactory sqlFactory = new SQLFactory(this, "销售出库单新增");
|
|
|
+ st_stockbillid = createTableID("st_stockbill");
|
|
|
+ sqlFactory.addParameter("siteid", "ccyosg");
|
|
|
+ sqlFactory.addParameter("st_stockbillid", st_stockbillid);
|
|
|
+ sqlFactory.addParameter("sys_enterpriseid", sys_enterpriseid);
|
|
|
+ sqlFactory.addParameter("billno", fbillnum);
|
|
|
+ sqlFactory.addParameter("type", "销售出库");
|
|
|
+ sqlFactory.addParameter("rb", frb.equals("蓝") ? 1 : 0);
|
|
|
+ sqlFactory.addParameter("billdate", fdate);
|
|
|
+ sqlFactory.addParameter("createby", createby);
|
|
|
+ sqlFactory.addParameter("createdate", getDate(createdate));
|
|
|
+ sqlFactory.addParameter("changeby", createby);
|
|
|
+ sqlFactory.addParameter("changedate", getDate(createdate));
|
|
|
+ sqlFactory.addParameter("checkby", createby);
|
|
|
+ sqlFactory.addParameter("checkdate", getDate(createdate));
|
|
|
+ sqlFactory.addParameter("remarks", fnotes);
|
|
|
+ sqlList.add(sqlFactory.getSQL());
|
|
|
+
|
|
|
+ JSONArray detailarray = erpSainvoice.getJSONArray("details");
|
|
|
+ StringBuffer str = new StringBuffer();
|
|
|
+
|
|
|
+ for (Object object : detailarray) {
|
|
|
+ JSONObject jsonObject = (JSONObject) object;
|
|
|
+ if (str.toString().equals("")) {
|
|
|
+ str.append("'" + jsonObject.getString("fitemno") + "'");
|
|
|
+ } else {
|
|
|
+ str.append(",'" + jsonObject.getString("fitemno") + "'");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ Rows rows;
|
|
|
+ if (!str.toString().equals("")) {
|
|
|
+ rows = dbConnect.runSqlQuery("select * from plm_item where siteid='ccyosg' and itemno in " + "(" + str.toString() + ")");
|
|
|
+ } else {
|
|
|
+ rows = dbConnect.runSqlQuery("select * from plm_item where siteid='ccyosg' and itemno in('')");
|
|
|
+ }
|
|
|
+ RowsMap itemRowsMap = rows.toRowsMap("itemno");
|
|
|
+ Rows distiptchRows = dbConnect.runSqlQuery("select t2.price,t1.sa_dispatch_itemsid,t1.rowno,t1.sa_orderitemsid,t2.sa_orderid,t3.sys_enterpriseid,t4.sonum,t5.itemno,t5.itemname from sa_dispatch_items t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid inner join sa_dispatch t3 on t1.sa_dispatchid=t3.sa_dispatchid and t1.siteid=t3.siteid left join sa_order t4 on t2.sa_orderid=t4.sa_orderid and t2.siteid=t4.siteid left join plm_item t5 on t1.itemid=t5.itemid and t1.siteid=t5.siteid where t3.status in('审核','复核','关闭') and t1.siteid='ccyosg' and t1.sa_dispatchid = " + sa_dispatchid);
|
|
|
+ RowsMap distiptchRowsMap = distiptchRows.toRowsMap("rowno");
|
|
|
+
|
|
|
+// Rows aftersalesmagRows = dbConnect.runSqlQuery("select t1.price,t1.sa_aftersalesmag_itemsid,t1.sa_orderitemsid,t2.sa_orderid from sa_aftersalesmag_items t1 inner join sa_orderitems t2 on t1.sa_orderitemsid=t2.sa_orderitemsid and t1.siteid=t2.siteid where t1.siteid='ccyosg' and t1.sa_aftersalesmag_itemsid in " + sa_dispatch_itemsids.toString().replace("[", "(").replace("]", ")"));
|
|
|
+// RowsMap aftersalesmagRowsMap = aftersalesmagRows.toRowsMap("rowno");
|
|
|
+ BigDecimal amountTotal = BigDecimal.ZERO;
|
|
|
+ for (Object object : detailarray) {
|
|
|
+ JSONObject jsonObject = (JSONObject) object;
|
|
|
+ String frownum = jsonObject.getString("fentryno");
|
|
|
+ String fitemno = jsonObject.getString("fitemno");
|
|
|
+ String fbatchno = jsonObject.getString("fbatchno");
|
|
|
+
|
|
|
+ System.err.println(frownum + ":" + fitemno);
|
|
|
+ BigDecimal fqty = new BigDecimal(jsonObject.getString("fqty"));
|
|
|
+ fqty = fqty.compareTo(BigDecimal.ZERO) < 0 ? fqty.negate() : fqty;
|
|
|
+ //String fdetailstockno = jsonObject.getString("fstockno");
|
|
|
+ //String fbatchno = jsonObject.getString("fbatchno");
|
|
|
+ String fdetailnotes = jsonObject.getString("fnote");
|
|
|
+ SQLFactory sqlFactorydetail = new SQLFactory(this, "销售出库单明细新增");
|
|
|
+ sqlFactorydetail.addParameter("siteid", "ccyosg");
|
|
|
+ sqlFactorydetail.addParameter("st_stockbill_itemsid", createTableID("st_stockbill_items"));
|
|
|
+ sqlFactorydetail.addParameter("st_stockbillid", st_stockbillid);
|
|
|
+ sqlFactorydetail.addParameter("rowno", frownum);
|
|
|
+ sqlFactorydetail.addParameter("remarks", fnotes);
|
|
|
+ if (itemRowsMap.containsKey(fitemno) && itemRowsMap.get(fitemno).isNotEmpty()) {
|
|
|
+ sqlFactorydetail.addParameter("itemid", itemRowsMap.get(fitemno).get(0).getLong("itemid"));
|
|
|
+ } else {
|
|
|
+ sqlFactorydetail.addParameter("itemid", 0);
|
|
|
+ }
|
|
|
+ sqlFactorydetail.addParameter("remarks", fdetailnotes);
|
|
|
+ sqlFactorydetail.addParameter("qty", fqty);
|
|
|
+
|
|
|
+ if (frb.equals("蓝")) {
|
|
|
+ if (distiptchRowsMap.containsKey(String.valueOf(frownum)) && distiptchRowsMap.get(frownum).isNotEmpty()) {
|
|
|
+ sqlFactorydetail.addParameter("sa_dispatch_itemsid", distiptchRowsMap.get(frownum).get(0).getLong("sa_dispatch_itemsid"));
|
|
|
+ sqlFactorydetail.addParameter("price", distiptchRowsMap.get(frownum).get(0).getBigDecimal("price"));
|
|
|
+ amountTotal = amountTotal.add(fqty.multiply(distiptchRowsMap.get(frownum).get(0).getBigDecimal("price")));
|
|
|
+ sqlFactorydetail.addParameter("amount", fqty.multiply(distiptchRowsMap.get(frownum).get(0).getBigDecimal("price")));
|
|
|
+ sqlFactorydetail.addParameter("sa_orderitemsid", distiptchRowsMap.get(frownum).get(0).getLong("sa_orderitemsid"));
|
|
|
+ sqlFactorydetail.addParameter("sa_orderid", distiptchRowsMap.get(frownum).get(0).getLong("sa_orderid"));
|
|
|
+ System.out.println(amountTotal);
|
|
|
+ //您的订单【XXX】商品【XXX】工厂已发货,出库单号【XXX】
|
|
|
+ String message = "您的订单【" + distiptchRowsMap.get(frownum).get(0).getString("sonum") + "】商品【" + distiptchRowsMap.get(frownum).get(0).getString("itemname") + "】工厂已发货,出库单号【" + fbillnum + "】!";
|
|
|
+ sendMsg(message, st_stockbillid, distiptchRowsMap.get(frownum).get(0).getLong("sys_enterpriseid"));
|
|
|
+ } else {
|
|
|
+ sqlFactorydetail.addParameter("price", 0);
|
|
|
+ sqlFactorydetail.addParameter("amount", 0);
|
|
|
+ sqlFactorydetail.addParameter("sa_orderitemsid", 0);
|
|
|
+ sqlFactorydetail.addParameter("sa_orderid", 0);
|
|
|
+ }
|
|
|
+ }
|
|
|
+// else {
|
|
|
+// if (aftersalesmagRowsMap.containsKey(String.valueOf(sainvoicedetailid)) && aftersalesmagRowsMap.get(sainvoicedetailid).isNotEmpty()) {
|
|
|
+// logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).toJsonObject().toString());
|
|
|
+// sqlFactorydetail.addParameter("price", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price"));
|
|
|
+// sqlFactorydetail.addParameter("amount", fqty.multiply(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("price")));
|
|
|
+// logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("sa_orderitemsid"));
|
|
|
+// logger.info(aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getBigDecimal("sa_orderid"));
|
|
|
+// sqlFactorydetail.addParameter("sa_orderitemsid", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderitemsid"));
|
|
|
+// sqlFactorydetail.addParameter("sa_orderid", aftersalesmagRowsMap.get(sainvoicedetailid).get(0).getLong("sa_orderid"));
|
|
|
+// } else {
|
|
|
+// sqlFactorydetail.addParameter("price", 0);
|
|
|
+// sqlFactorydetail.addParameter("amount", 0);
|
|
|
+// sqlFactorydetail.addParameter("sa_orderitemsid", 0);
|
|
|
+// sqlFactorydetail.addParameter("sa_orderid", 0);
|
|
|
+// }
|
|
|
+// }
|
|
|
+
|
|
|
+ sqlList.add(sqlFactorydetail.getSQL());
|
|
|
+ }
|
|
|
+ sqlList.add("update st_stockbill set status='审核',checkby='" + createby + "',checkdate=CURRENT_TIMESTAMP where st_stockbillid = " + st_stockbillid + " and siteid='ccyosg'"); JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "1");
|
|
|
+ flag.put("fupdatewarnlog", "");
|
|
|
+ flags.add(flag);
|
|
|
+
|
|
|
+ } else if (!stockbillRows.isEmpty() && "审核".equals(fstatus)) {
|
|
|
+ /**
|
|
|
+ * DRP存在销售出库单,且中间表状态为审核且不在删除状态时,DRP中需将当前销售出库单先反审核,修改内容后再进行审核
|
|
|
+ */
|
|
|
+ sqlList.add("update st_stockbill set status='新建',checkby='',checkdate=null where st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and siteid='ccyosg'");
|
|
|
+ JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "99");
|
|
|
+ flag.put("fupdatewarnlog", "");
|
|
|
+ flags.add(flag);
|
|
|
+ continue;
|
|
|
+ } else if (!stockbillRows.isEmpty() && "新建".equals(fstatus)) {
|
|
|
+ /**
|
|
|
+ * DRP存在收入凭证,且中间表状态为新建或在删除状态时,DRP中需将当前收入销售出库单先反审核
|
|
|
+ */
|
|
|
+ sqlList.add("update st_stockbill set status='新建',checkby='',checkdate=null where st_stockbillid = " + stockbillRows.get(0).getLong("st_stockbillid") + " and siteid='ccyosg'");
|
|
|
+ JSONObject flag =new JSONObject();
|
|
|
+ flag.put("syncyosid",erpSainvoice.getStringValue("tstockbillid"));
|
|
|
+ flag.put("fupdateflag", "1");
|
|
|
+ flag.put("fupdatewarnlog", "");
|
|
|
+ flags.add(flag);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if(erpDocking.updateErpupdateFlag(flags).equals("true")){
|
|
|
+ dbConnect.runSqlUpdate(sqlList);
|
|
|
+ /**
|
|
|
+ * 如单据为新建状态,则,先把单据强行删除掉
|
|
|
+ */
|
|
|
+ dbConnect.runSqlUpdate("delete from st_stockbill_items where st_stockbillid in(select st_stockbillid from st_stockbill where status='新建')");
|
|
|
+ dbConnect.runSqlUpdate("delete from st_stockbill where status='新建'");
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getDate(String date) {
|
|
|
+ try {
|
|
|
+ Date a = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").parse(date);
|
|
|
+ return new SimpleDateFormat("yyyy-MM-dd hh:mm:ss").format(a);
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ public void sendMsg(String content, Long st_stockbillid, Long sys_enterpriseid) throws YosException {
|
|
|
+
|
|
|
+ ArrayList<Long> userList = getEnterpriseHrs(sys_enterpriseid,"ccyosg").toArrayList("userid", new ArrayList<>());
|
|
|
+ Remind remind = new Remind(new Controller(new JSONObject()));
|
|
|
+ remind.setTitle("销售出库单消息");
|
|
|
+ remind.setContent(content);
|
|
|
+ remind.setType("应用");
|
|
|
+ remind.setObjectid(st_stockbillid);
|
|
|
+ remind.setObjectname("st_stockbill");
|
|
|
+ remind.setTouserid(userList);
|
|
|
+ remind.sendByDialogMsg().createSys_message();
|
|
|
+ }
|
|
|
+}
|