package restcontroller.webmanage.sale.dispatch; import beans.data.BatchDeleteErr; import beans.parameter.Parameter; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import common.Controller; import common.YosException; import common.annotation.API; import common.annotation.CACHEING; import common.annotation.CACHEING_CLEAN; import common.data.*; import org.apache.commons.lang.StringUtils; import org.apache.poi.ss.usermodel.CellStyle; import org.apache.poi.ss.usermodel.DataFormat; import org.apache.poi.xssf.usermodel.XSSFCellStyle; import org.apache.poi.xssf.usermodel.XSSFSheet; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import restcontroller.R; import restcontroller.webmanage.sale.order.Order; import restcontroller.webmanage.sale.order.OrderItems; import utility.ERPDocking; import java.io.IOException; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; public class dispatchItems extends Controller { public dispatchItems(JSONObject arg0) throws YosException { super(arg0); // TODO Auto-generated constructor stub } @API(title = "审核订单列表查询", apiversion = R.ID20221114165903.v1.class) @CACHEING public String queryCheckOrderList() throws YosException { /* * 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append("t1.sonum like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.abbreviation like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.enterprisename like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t1.remarks like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } if (whereObject.containsKey("sonum") && !"".equals(whereObject.getString("sonum"))) { where.append(" and t1.sonum ='").append(whereObject.getString("sonum")).append("' "); } if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) { where.append(" and t1.checkdate >='").append(whereObject.getString("begindate")).append("' "); } if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) { where.append(" and t1.checkdate <='").append(whereObject.getString("enddate")).append("' "); } } // String hrid = content.getString("hrid"); SQLFactory sqlFactory = new SQLFactory(this, "审核订单列表查询", pageSize, pageNumber, pageSorting); sqlFactory.addParameter_SQL("where", where); sqlFactory.addParameter("sys_enterpriseid", content.getLong("sys_enterpriseid")); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false)); return getSucReturnObject().setData(rows).toString(); } @API(title = "发货商品列表查询", apiversion = R.ID20221114170003.v1.class) @CACHEING public String queryOrderItemgList() throws YosException { /* * 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append("t1.itemname like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t1.itemno like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t1.unit like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t3.sonum like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } if (whereObject.containsKey("sa_orderid") && !"".equals(whereObject.getString("sa_orderid"))) { where.append(" and t1.sa_orderid ='").append(whereObject.getString("sa_orderid")).append("' "); } if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) { where.append(" and t3.checkdate >='").append(whereObject.getString("begindate")).append("' "); } if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) { where.append(" and t3.checkdate <='").append(whereObject.getString("enddate")).append("' "); } } SQLFactory sqlFactory = new SQLFactory(this, "发货商品列表查询", pageSize, pageNumber, pageSorting); sqlFactory.addParameter_SQL("where", where); sqlFactory.addParameter("sys_enterpriseid", content.getLongValue("sys_enterpriseid")); sqlFactory.addParameter("sa_dispatchid", content.getLongValue("sa_dispatchid")); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory.getSQL(false)); return getSucReturnObject().setData(rows).toString(); } @API(title = "erp批次库存查询", apiversion = R.ID20230427101303.v1.class) @CACHEING public String queryErpIcinvbalBatchList() throws YosException { JSONArray jsonArray = content.getJSONArray("itemnos"); ERPDocking erpDocking = new ERPDocking(); JSONArray rows = erpDocking.getErpIcinvbalBatchRows(1000, 1, jsonArray); return getSucReturnObject().setData(rows).toString(); } @API(title = "新建或修改发货单商品明细", apiversion = R.ID20221115104603.v1.class) @CACHEING_CLEAN(apiClass = {dispatchItems.class, dispatch.class}) public String insertormodify_dispatchItems() throws YosException { Long sa_dispatchid = content.getLong("sa_dispatchid"); JSONArray iteminfos = content.getJSONArray("iteminfos"); ArrayList sqlList = new ArrayList<>(); Rows rowscount = dbConnect.runSqlQuery("select billno,status from sa_dispatch where sa_dispatchid=" + sa_dispatchid); if (!rowscount.isEmpty()) { if (!rowscount.get(0).getString("status").equals("新建")) { return getErrReturnObject().setErrMsg("非新建状态的发货单无法新增修改").toString(); } } int i = 0; long maxid = 0; long[] sa_dispatch_itemsid = createTableID("sa_dispatch_items", iteminfos.size()); Rows maxidRows = dbConnect .runSqlQuery("select MAX(rowno) rowno from sa_dispatch_items where sa_dispatchid=" + sa_dispatchid); if (!maxidRows.isEmpty()) { maxid = maxidRows.get(0).getLong("rowno"); } for (Object obj : iteminfos) { JSONObject iteminfo = (JSONObject) obj; if (iteminfo.getLong("sa_dispatch_itemsid") <= 0 || dbConnect .runSqlQuery("select sa_dispatch_itemsid from sa_dispatch_items where sa_dispatch_itemsid=" + iteminfo.getLong("sa_dispatch_itemsid")) .isEmpty()) { Rows sa_dispatch_itemsrows = dbConnect.runSqlQuery("select sa_dispatch_itemsid,qty,remarks from sa_dispatch_items where sa_dispatchid=" + sa_dispatchid + " and sa_orderitemsid=" + iteminfo.getLong("sa_orderitemsid")); if (!sa_dispatch_itemsrows.isEmpty()) { SQLFactory saleFactory = new SQLFactory(this, "发货单明细更新"); saleFactory.addParameter("sa_dispatch_itemsid", sa_dispatch_itemsrows.get(0).getLong("sa_dispatch_itemsid")); // saleFactory.addParameter("itemno", iteminfo.getString("itemno")); saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty").add(sa_dispatch_itemsrows.get(0).getBigDecimal("qty"))); if (!StringUtils.isBlank(iteminfo.getString("remarks"))) { saleFactory.addParameter("remarks", iteminfo.getString("remarks")); } else { saleFactory.addParameter("remarks", sa_dispatch_itemsrows.get(0).getString("remarks")); } saleFactory.addParameter("userid", userid); saleFactory.addParameter("username", username); saleFactory.addParameter("billno", rowscount.get(0).getString("billno")); sqlList.add(saleFactory.getSQL()); } else { SQLFactory saleFactory = new SQLFactory(this, "发货单明细新增"); saleFactory.addParameter("siteid", siteid); saleFactory.addParameter("rowno", maxid + i + 1); saleFactory.addParameter("sa_dispatch_itemsid", sa_dispatch_itemsid[i]); saleFactory.addParameter("sa_dispatchid", sa_dispatchid); saleFactory.addParameter("sa_orderitemsid", iteminfo.getLong("sa_orderitemsid")); saleFactory.addParameter("itemid", iteminfo.getString("itemid")); saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty")); saleFactory.addParameter("batchcontrol", iteminfo.getBooleanValue("batchcontrol")); saleFactory.addParameter("batchno", ""); saleFactory.addParameter("remarks", iteminfo.getString("remarks")); saleFactory.addParameter("userid", userid); saleFactory.addParameter("username", username); saleFactory.addParameter("billno", rowscount.get(0).getString("billno")); sqlList.add(saleFactory.getSQL()); } i++; } else { SQLFactory saleFactory = new SQLFactory(this, "发货单明细更新"); saleFactory.addParameter("sa_dispatch_itemsid", iteminfo.getLong("sa_dispatch_itemsid")); // saleFactory.addParameter("itemno", iteminfo.getString("itemno")); saleFactory.addParameter("qty", iteminfo.getBigDecimal("qty")); saleFactory.addParameter("remarks", iteminfo.getString("remarks")); saleFactory.addParameter("batchno", iteminfo.getString("batchno")); saleFactory.addParameter("userid", userid); saleFactory.addParameter("username", username); saleFactory.addParameter("billno", rowscount.get(0).getString("billno")); sqlList.add(saleFactory.getSQL()); } } dbConnect.runSqlUpdate(sqlList); return queryDispatchItemsList(); } @API(title = "发货单商品明细列表", apiversion = R.ID20221115104703.v1.class) @CACHEING public String queryDispatchItemsList() throws YosException { /* * 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append("t2.itemname like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.model like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.spec like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } } Long sa_dispatchid = content.getLong("sa_dispatchid"); // String hrid = content.getString("hrid"); SQLFactory sqlFactory = new SQLFactory(this, "发货单明细列表查询", pageSize, pageNumber, pageSorting); sqlFactory.addParameter_SQL("where", where); sqlFactory.addParameter("sa_dispatchid", sa_dispatchid); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory); RowsMap rowsMap = rows.toRowsMap("itemno"); ERPDocking erpDocking = new ERPDocking(); JSONArray jsonArray = new JSONArray(); if (rows.toJsonArray("itemno").size() != 0) { if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) { jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno")); } } SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总"); sqlFactory1.addParameter("siteid", siteid); Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1); RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno"); if (!jsonArray.isEmpty()) { for (Object object : jsonArray) { JSONObject jsonObject = (JSONObject) object; if (rowsMap.containsKey(jsonObject.getString("fitemno"))) { if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) { if (sumQtyRowsMap.containsKey(jsonObject.getString("fitemno"))) { rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty").subtract(sumQtyRowsMap.get(jsonObject.getString("fitemno")).get(0).getBigDecimal("qty"))); } else { rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty")); } rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty")); } } } } for (Row row : rows) { if (!row.containsKey("delinvbalqty")) { row.put("delinvbalqty", row.getBigDecimal("undeliqty").negate()); row.put("invbalqty", 0); } } return getSucReturnObject().setData(rows).toString(); } @API(title = "发货单商品所有明细列表", apiversion = R.ID20230508113003.v1.class) @CACHEING public String queryDispatchAllItemsList() throws YosException, IOException { /* * 过滤条件设置 */ StringBuffer where = new StringBuffer(" 1=1 "); if (sys_enterpriseid > 0) { where.append(" and t4.sys_enterpriseid ='").append(sys_enterpriseid).append("' "); } if (content.containsKey("where")) { JSONObject whereObject = content.getJSONObject("where"); if (whereObject.containsKey("condition") && !"".equals(whereObject.getString("condition"))) { where.append(" and("); where.append(" t4.billno like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t8.sonum like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t9.agentnum like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t10.enterprisename like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.itemno like'%").append(whereObject.getString("condition")).append("%' "); where.append("or t2.itemname like'%").append(whereObject.getString("condition")).append("%' "); where.append(")"); } if (whereObject.containsKey("agentinfo") && !"".equals(whereObject.getString("agentinfo"))) { where.append(" and("); where.append("t9.agentnum like'%").append(whereObject.getString("agentinfo")).append("%' "); where.append("or t10.enterprisename like'%").append(whereObject.getString("agentinfo")).append("%' "); where.append(")"); } if (whereObject.containsKey("iteminfo") && !"".equals(whereObject.getString("iteminfo"))) { where.append(" and("); where.append("t2.itemno like'%").append(whereObject.getString("iteminfo")).append("%' "); where.append("or t2.itemname like'%").append(whereObject.getString("iteminfo")).append("%' "); where.append(")"); } if (whereObject.containsKey("begindate") && !"".equals(whereObject.getString("begindate"))) { where.append(" and t4.billdate >='").append(whereObject.getString("begindate")).append("' "); } if (whereObject.containsKey("enddate") && !"".equals(whereObject.getString("enddate"))) { where.append(" and t4.billdate <='").append(whereObject.getString("enddate")).append("' "); } if (whereObject.containsKey("status") && !"".equals(whereObject.getString("status"))) { where.append(" and t4.status ='").append(whereObject.getString("status")).append("' "); } if (whereObject.containsKey("isnotsent") && !"".equals(whereObject.getString("isnotsent"))) { if(whereObject.getBooleanValue("isnotsent")){ //System.out.println(" and t4.status ='复核' and ifnull(t1.outwarehouseqty, 0)< ifnull(t1.qty, 0)"); where.append(" and ifnull(t1.qty, 0)> ifnull(t1.outwarehouseqty, 0) and t1.sa_dispatchid in(select t1.sa_dispatchid from sa_dispatch_items t1 LEFT JOIN sa_dispatch t4 ON t4.sa_dispatchid = t1.sa_dispatchid AND t4.siteid = t1.siteid where t4.status ='复核' and ifnull(t1.outwarehouseqty, 0)< ifnull(t1.qty, 0) and ifnull(t1.outwarehouseqty, 0)>0)"); } } } boolean isExport = content.getBooleanValue("isExport"); SQLFactory sqlFactory = new SQLFactory(this, "发货单明细所有列表查询", pageSize, pageNumber, pageSorting); if (isExport) { sqlFactory = new SQLFactory(this, "发货单明细所有列表查询"); } sqlFactory.addParameter_SQL("where", where); sqlFactory.addParameter("siteid", siteid); Rows rows = dbConnect.runSqlQuery(sqlFactory); RowsMap rowsMap = rows.toRowsMap("itemno"); ERPDocking erpDocking = new ERPDocking(); JSONArray jsonArray = new JSONArray(); if (rows.toJsonArray("itemno").size() != 0) { if (Parameter.get("system.ccerp_dockswitch").equalsIgnoreCase("true")) { jsonArray = erpDocking.getErpIcinvbalRows(200000, 1, rows.toJsonArray("itemno")); } } SQLFactory sqlFactory1 = new SQLFactory(this, "商品发货数量汇总"); sqlFactory1.addParameter("siteid", siteid); Rows sumQtyRows = dbConnect.runSqlQuery(sqlFactory1); RowsMap sumQtyRowsMap = sumQtyRows.toRowsMap("itemno"); if (!jsonArray.isEmpty()) { for (Object object : jsonArray) { JSONObject jsonObject = (JSONObject) object; if (rowsMap.containsKey(jsonObject.getString("fitemno"))) { if (rowsMap.get(jsonObject.getString("fitemno")).isNotEmpty()) { if (sumQtyRowsMap.containsKey(jsonObject.getString("fitemno"))) { rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty").subtract(sumQtyRowsMap.get(jsonObject.getString("fitemno")).get(0).getBigDecimal("qty"))); } else { rowsMap.get(jsonObject.getString("fitemno")).get(0).put("delinvbalqty", jsonObject.getBigDecimalValue("fqty")); } rowsMap.get(jsonObject.getString("fitemno")).get(0).put("invbalqty", jsonObject.getBigDecimalValue("fqty")); } } } } for (Row row : rows) { if (!row.containsKey("delinvbalqty")) { row.put("delinvbalqty", row.getBigDecimal("undeliqty").negate()); row.put("invbalqty", 0); } } if (isExport) { ExcelFactory excelFactory = new ExcelFactory("发货单明细所有列表"); XSSFSheet sheet = excelFactory.getXssfWorkbook().createSheet("Sheet1"); XSSFWorkbook xssfFWorkbook = excelFactory.getXssfWorkbook(); CellStyle style = xssfFWorkbook.createCellStyle(); // 设置为文本格式,防止身份证号变成科学计数法 DataFormat format = xssfFWorkbook.createDataFormat(); style.setDataFormat(format.getFormat("@")); // 对单独某一列进行样式赋值,第一个参数为列数,第二个参数为样式 sheet.setDefaultColumnStyle(0, style); ExportDown.setBatchDetailSheetColumn1(sheet);// 设置工作薄列宽 XSSFCellStyle titleCellStyle2 = ExportDown.createTitleCellStyle2(xssfFWorkbook); XSSFCellStyle titleCellStyle3 = ExportDown.createBodyCellStyle(xssfFWorkbook); ExportDown.batchDetail(sheet, titleCellStyle2, titleCellStyle3, rows);// 写入标题 Rows uploadRows = uploadExcelToObs(excelFactory); String url = ""; if (!uploadRows.isEmpty()) { url = uploadRows.get(0).getString("url"); } return getSucReturnObject().setData(url).toString(); } return getSucReturnObject().setData(rows).toString(); } //返回导出的标题 public HashMap getTitleMap() { HashMap titleMap = new HashMap<>(); titleMap.put("billno", "发货单号"); titleMap.put("status", "状态"); titleMap.put("sonum", "订单号"); titleMap.put("enterprisename", "企业名称"); titleMap.put("agentnum", "经销商编号"); titleMap.put("billdate", "发货日期"); titleMap.put("orderRowno", "订单行号"); titleMap.put("itemno", "产品编码"); titleMap.put("itemname", "品名"); titleMap.put("unitname", "单位"); titleMap.put("qty", "发货数量"); titleMap.put("remarks", "发货行备注"); return titleMap; } @API(title = "删除明细", apiversion = R.ID20221115104803.v1.class) @CACHEING_CLEAN(apiClass = {dispatchItems.class, dispatch.class}) public String deletemx() throws YosException { JSONArray sa_dispatch_itemsids = content.getJSONArray("sa_dispatch_itemsids"); BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, sa_dispatch_itemsids.size()); long sa_dispatchid = 0; for (Object o : sa_dispatch_itemsids) { long sa_dispatch_itemsid = Long.parseLong(o.toString()); Rows RowsStatus = dbConnect.runSqlQuery("select t1.sa_dispatch_itemsid,t2.status,t1.sa_dispatchid from sa_dispatch_items t1 left join sa_dispatch t2 on t1.sa_dispatchid=t2.sa_dispatchid and t1.siteid=t2.siteid where t1.siteid='" + siteid + "' and t1.sa_dispatch_itemsid='" + sa_dispatch_itemsid + "'"); if (RowsStatus.isNotEmpty()) { sa_dispatchid = RowsStatus.get(0).getLong("sa_dispatchid"); if (!RowsStatus.get(0).getString("status").equals("新建")) { batchDeleteErr.addErr(sa_dispatch_itemsid, "非新建状态的发货单明细无法删除"); continue; } } ArrayList list = new ArrayList<>(); SQLFactory deletesql = new SQLFactory("sql:delete from sa_dispatch_items where siteid='" + siteid + "' and sa_dispatch_itemsid=" + sa_dispatch_itemsid); list.add(deletesql.getSQL()); dbConnect.runSqlUpdate(list); } //重新排序 updateRowNo(sa_dispatchid); return batchDeleteErr.getReturnObject().toString(); } /** * 重新对发货单行进行排序 * * @param * @throws YosException */ public void updateRowNo(Long sa_dispatchid) throws YosException { String sql = "SELECT sa_dispatch_itemsid from sa_dispatch_items WHERE sa_dispatchid= " + sa_dispatchid + " and siteid = '" + siteid + "' ORDER BY sa_dispatch_itemsid asc "; ArrayList sa_dispatch_itemsids = dbConnect.runSqlQuery(sql).toArrayList("sa_dispatch_itemsid", new ArrayList<>()); ArrayList sqlList = new ArrayList<>(); int rowno = 1; for (Long id : sa_dispatch_itemsids) { sqlList.add("UPDATE sa_dispatch_items SET rowno=" + rowno + " WHERE sa_dispatch_itemsid = " + id); rowno++; } dbConnect.runSqlUpdate(sqlList); } }