icstockbill.java 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. package common.crm.bean;
  2. import com.alibaba.fastjson2.JSONArray;
  3. import com.alibaba.fastjson2.JSONObject;
  4. import common.YosException;
  5. import common.YosLogger;
  6. import common.data.*;
  7. import common.data.db.DBConnect;
  8. import org.apache.logging.log4j.LogManager;
  9. import org.apache.logging.log4j.Logger;
  10. import common.crm.bean.core.CrmBase;
  11. import java.util.ArrayList;
  12. import java.util.Calendar;
  13. public class icstockbill extends CrmBase {
  14. public icstockbill(Logger logger) {
  15. super(logger);
  16. }
  17. /**
  18. * 超过30天的销售出库单自动收货
  19. *
  20. * @throws YosException
  21. */
  22. @Override
  23. public void autoAction() throws YosException {
  24. //到期自动收货确认
  25. Calendar calendar = Calendar.getInstance();
  26. calendar.add(Calendar.DATE, -30);
  27. String enddate = getDate_Str(calendar.getTime());
  28. Rows billRows = dbConnect.runSqlQuery("select t1.st_stockbillid,count(t2.sku) mcodeqty from st_stockbill t1 \n" +
  29. "inner join st_stockbill_items_sku t2 on t1.st_stockbillid=t2.st_stockbillid\n" +
  30. "inner join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t3.type='普通'\n" +
  31. "where t1.siteid='MD' and t1.type='销售出库'and t1.rb = 1 and t1.isreceiver = 0 and t1.status = '审核' and t1.iswx=0 and t1.checkdate>='2022-01-01' and t1.checkdate<'" + enddate + "' \n" +
  32. "group by t1.st_stockbillid\n" +
  33. "order by t1.st_stockbillid desc\n" +
  34. "limit 100");
  35. QuerySQL itemQuery = SQLFactory.createQuerySQL(dbConnect, "st_stockbill_items", "st_stockbillid");
  36. itemQuery.addQueryFields("itemqty", "sum(qty)");
  37. itemQuery.addGroupBy("st_stockbillid");
  38. itemQuery.setSiteid("MD").setWhere("skucontrol", true).setWhere("st_stockbillid", billRows.toArrayList("st_stockbillid"));
  39. RowsMap itemRowsMap = itemQuery.query().toRowsMap("st_stockbillid");
  40. for (Row billRow : billRows) {
  41. long st_stockbillid = billRow.getLong("st_stockbillid");
  42. if (itemRowsMap.containsKey(String.valueOf(st_stockbillid)) && itemRowsMap.get(String.valueOf(st_stockbillid)).get(0).getInteger("itemqty") == billRow.getInteger("mcodeqty")) {
  43. oneAction(st_stockbillid);
  44. }
  45. }
  46. }
  47. /**
  48. * 指定单据进行收货确认
  49. *
  50. * @param st_stockbillid
  51. * @throws YosException
  52. */
  53. public void oneAction(long st_stockbillid) throws YosException {
  54. Rows billRows = new DBConnect().runSqlQuery("select * from st_stockbill where st_stockbillid=" + st_stockbillid);
  55. if (billRows.isEmpty()) {
  56. throw new YosException("未找到该出库单");
  57. }
  58. Row billRow = billRows.get(0);
  59. String billno = billRow.getString("billno");
  60. String remarks = billRow.getString("remarks");
  61. String checkdate = billRow.getString("checkdate");
  62. String agentnum = billRow.getRow("sa_agents", "sys_enterpriseid=:sys_enterpriseid").getString("agentnum");
  63. Rows detailRows = dbConnect.runSqlQuery("select itemno as fitemno,itemname as fitemname,model as fmodel,sum(qty)fqty from st_stockbill_items where st_stockbillid=" + st_stockbillid + " group by itemno,itemname,model");
  64. Rows codeRows = dbConnect.runSqlQuery("select t2.itemno as fitemno,t1.sku as fmachinecode from st_stockbill_items_sku t1 inner join plm_item t2 on t1.itemid=t2.itemid where st_stockbillid=" + st_stockbillid);
  65. JSONObject dataObject = new JSONObject();
  66. dataObject.put("icstockbillid", st_stockbillid);
  67. dataObject.put("fbillnum", billno);
  68. dataObject.put("fnotes", remarks);
  69. dataObject.put("fagentnum", agentnum);
  70. dataObject.put("checkdate", checkdate);
  71. dataObject.put("details", detailRows.toJsonArray());
  72. dataObject.put("machinecodes", codeRows.toJsonArray());
  73. String result = postCRM(dataObject.toJSONString(), "/ext/whicstockbill/confirmReceiveBill/1");
  74. JSONObject jsonObject = JSONObject.parseObject(result);
  75. if (jsonObject.getBooleanValue("success")) {
  76. logger.info("销售出库单" + billno + "自动收货成功");
  77. } else {
  78. logger.error("销售出库单" + billno + "自动收货失败;" + jsonObject);
  79. }
  80. }
  81. /**
  82. * crm到货确认反馈
  83. *
  84. * @param fagentnum
  85. * @param fcrmbillnum
  86. * @param icstockbillids
  87. * @return
  88. */
  89. public JSONObject confirmReceiveBill(String fagentnum, String fcrmbillnum, String[] icstockbillids) {
  90. JSONObject resultObject = new JSONObject();
  91. try {
  92. QuerySQL billQuery = SQLFactory.createQuerySQL(new DBConnect(), "st_stockbill");
  93. billQuery.setSiteid("MD").setWhere("st_stockbillid", icstockbillids);
  94. Rows billRows = billQuery.query();
  95. RowsMap billRowsMap = billRows.toRowsMap("st_stockbillid");
  96. Rows agentrows = new DBConnect().runSqlQuery("select sa_agentsid from sa_agents where agentnum='" + fagentnum + "' and siteid='MD'");
  97. if (agentrows.isEmpty()) {
  98. resultObject.put("code", 0);
  99. resultObject.put("status", "err");
  100. resultObject.put("msg", "找不到编号为" + fagentnum + "的经销商");
  101. return resultObject;
  102. }
  103. Rows stockrows = new DBConnect().runSqlQuery("select stockid from st_stock where stockno='00' and siteid='MD'");
  104. if (stockrows.isEmpty()) {
  105. resultObject.put("code", 0);
  106. resultObject.put("status", "err");
  107. resultObject.put("msg", "经销商仓00不存在");
  108. return resultObject;
  109. }
  110. SQLDump sqlDump = new SQLDump();
  111. for (String icstockbillid : icstockbillids) {
  112. if (!billRowsMap.containsKey(icstockbillid)) {
  113. resultObject.put("code", 0);
  114. resultObject.put("msg", "找不到ID为" + icstockbillid + "的销售出库单");
  115. return resultObject;
  116. }
  117. Row billrow = billRowsMap.get(icstockbillid).get(0);
  118. if (billrow.getBoolean("isreceiver")) {
  119. resultObject.put("code", 0);
  120. resultObject.put("msg", "销售出库单" + billrow.getString("billno") + "已收货确认,不可重复确认");
  121. return resultObject;
  122. }
  123. if (!billrow.getString("status").equals("审核")) {
  124. resultObject.put("code", 0);
  125. resultObject.put("msg", "销售出库单" + billrow.getString("billno") + "处于非审核状态,不可确认");
  126. return resultObject;
  127. }
  128. QuerySQL itemQuery = SQLFactory.createQuerySQL(new DBConnect(), "st_stockbill_items", "st_stockbillid");
  129. itemQuery.addQueryFields("itemqty", "sum(qty)");
  130. itemQuery.setSiteid("MD").setWhere("skucontrol", true).setWhere("st_stockbillid", icstockbillid);
  131. int qty = new DBConnect().runSqlQuery("select sum(qty) as qty from st_stockbill_items where siteid='MD' and skucontrol=1 and st_stockbillid=" + icstockbillid).get(0).getInteger("qty");
  132. ArrayList<String> skuList = new DBConnect().runSqlQuery("select sku from st_stockbill_items_sku where siteid='MD' and st_stockbillid=" + icstockbillid).toArrayList("sku");
  133. if (qty != skuList.size()) {
  134. resultObject.put("code", 0);
  135. resultObject.put("msg", "销售出库单" + billrow.getString("billno") + ",序列号数量和商品数量不符");
  136. return resultObject;
  137. }
  138. sqlDump.add(SQLFactory.createUpdateSQL(new DBConnect(), "st_stockbill").setValue("isreceiver", true).setDateValue("receiverdate").setValue("crmbillno", fcrmbillnum).setValue("receiverby", "crm").setSiteid("MD").setWhere("st_stockbillid", icstockbillid));
  139. sqlDump.add(SQLFactory.createUpdateSQL(new DBConnect(), "sa_itemsku").setValue("sa_agentsid", agentrows.get(0).getLong("sa_agentsid")).setValue("stockid", stockrows.get(0).getLong("stockid")).setSiteid("MD").setWhere("sku", skuList));
  140. }
  141. sqlDump.commit();
  142. resultObject.put("msg", "");
  143. resultObject.put("code", 1);
  144. } catch (Exception e) {
  145. resultObject.put("msg", e.getMessage());
  146. resultObject.put("code", 0);
  147. }
  148. return resultObject;
  149. }
  150. /**
  151. * crm查询未到货确认单
  152. *
  153. * @param fagentnum
  154. * @param fbegdate
  155. * @param fenddate
  156. * @param returnMaxCount
  157. * @return
  158. */
  159. public JSONArray getDRPReceiveBill(String fagentnum, String fbegdate, String fenddate, int returnMaxCount) {
  160. try {
  161. Rows billRows = dbConnect.runSqlQuery("select t1.st_stockbillid,count(t2.sku) mcodeqty from st_stockbill t1 \n" +
  162. "inner join st_stockbill_items_sku t2 on t1.st_stockbillid=t2.st_stockbillid\n" +
  163. "inner join sa_agents t3 on t1.sys_enterpriseid=t3.sys_enterpriseid and t3.type='普通' and t3.agentnum='" + fagentnum + "' \n" +
  164. "where t1.siteid='MD' and t1.type='销售出库'and t1.rb = 1 and t1.isreceiver = 0 and t1.status = '审核' and t1.iswx=0 and t1.checkdate>='" + fbegdate + "' and t1.checkdate<='" + fenddate + "' \n" +
  165. "group by t1.st_stockbillid\n" +
  166. "order by t1.st_stockbillid\n" +
  167. "limit " + returnMaxCount);
  168. QuerySQL itemQuery = SQLFactory.createQuerySQL(dbConnect, "st_stockbill_items", "st_stockbillid");
  169. itemQuery.addQueryFields("itemqty", "sum(qty)");
  170. itemQuery.addGroupBy("st_stockbillid");
  171. itemQuery.setSiteid("MD").setWhere("skucontrol", true).setWhere("st_stockbillid", billRows.toArrayList("st_stockbillid"));
  172. RowsMap itemRowsMap = itemQuery.query().toRowsMap("st_stockbillid");
  173. JSONArray array = new JSONArray();
  174. for (Row billRow : billRows) {
  175. long st_stockbillid = billRow.getLong("st_stockbillid");
  176. if (itemRowsMap.containsKey(String.valueOf(st_stockbillid)) && itemRowsMap.get(String.valueOf(st_stockbillid)).get(0).getInteger("itemqty") == billRow.getInteger("mcodeqty")) {
  177. Row bill = new DBConnect().runSqlQuery("select * from st_stockbill where st_stockbillid=" + st_stockbillid).get(0);
  178. String billno = bill.getString("billno");
  179. String remarks = bill.getString("remarks");
  180. String checkdate = bill.getString("checkdate");
  181. String agentnum = bill.getRow("sa_agents", "sys_enterpriseid=:sys_enterpriseid").getString("agentnum");
  182. Rows detailRows = dbConnect.runSqlQuery("select itemno as fitemno,itemname as fitemname,model as fmodel,sum(qty)fqty from st_stockbill_items where st_stockbillid=" + st_stockbillid + " group by itemno,itemname,model");
  183. Rows codeRows = dbConnect.runSqlQuery("select t2.itemno as fitemno,t1.sku as fmachinecode from st_stockbill_items_sku t1 inner join plm_item t2 on t1.itemid=t2.itemid where st_stockbillid=" + st_stockbillid);
  184. JSONObject dataObject = new JSONObject();
  185. dataObject.put("icstockbillid", st_stockbillid);
  186. dataObject.put("fbillnum", billno);
  187. dataObject.put("fnotes", remarks);
  188. dataObject.put("fagentnum", agentnum);
  189. dataObject.put("checkdate", checkdate);
  190. dataObject.put("details", detailRows.toJsonArray());
  191. dataObject.put("machinecodes", codeRows.toJsonArray());
  192. array.add(dataObject);
  193. }
  194. }
  195. return array;
  196. } catch (Exception e) {
  197. }
  198. return new JSONArray();
  199. }
  200. public static void main(String[] args) throws YosException {
  201. new icstockbill(LogManager.getLogger(YosLogger.class)).oneAction(2973233);
  202. }
  203. }