| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314 |
- package beans.enterprise_stockbill.bills;
- import beans.datacontrllog.DataContrlLog;
- import common.Controller;
- import common.UserInfo;
- import common.YosException;
- import common.data.*;
- import common.data.db.DBConnect;
- import common.data.db.SQLList;
- import org.apache.commons.lang.StringUtils;
- import java.math.BigDecimal;
- import java.util.ArrayList;
- public abstract class BasicBill {
- Controller controller;
- DBConnect dbConnect;
- UserInfo userInfo;
- long sys_enterprise_stockbillid;
- long sys_enterprise_stockid;
- boolean issale;//是否销售出库
- String billType;//单据类型
- String billTypeMX;//单据类型
- String billno;
- String siteid;
- String status;
- int rb;
- Row billRow;
- Rows itemRows;
- Rows codeRows;
- long sys_enterpriseid;
- public static BasicBill getBill(Controller controller, long sys_enterprise_stockbillid) throws YosException {
- Rows billRows = controller.dbConnect.runSqlQuery("select type from sys_enterprise_stockbill where sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
- if (billRows.isEmpty()) {
- throw new YosException("该出入库单不存在");
- }
- String type = billRows.get(0).getString("type");
- switch (type) {
- case "销售出库":
- return new XSCK(controller, sys_enterprise_stockbillid);
- case "其他出库":
- return new QTCK(controller, sys_enterprise_stockbillid);
- case "其他入库":
- return new QTRK(controller, sys_enterprise_stockbillid);
- case "报货入库":
- return new BHRK(controller, sys_enterprise_stockbillid);
- default: {
- throw new YosException("不支持的出入库单类型");
- }
- }
- }
- /**
- * 构造函数
- *
- * @param controller
- * @param sys_enterprise_stockbillid 出入库单ID
- * @throws YosException
- */
- public BasicBill(Controller controller, long sys_enterprise_stockbillid) throws YosException {
- this.controller = controller;
- this.dbConnect = this.controller.dbConnect;
- this.userInfo = controller.userInfo;
- this.sys_enterprise_stockbillid = sys_enterprise_stockbillid;
- queryBills();
- }
- /**
- * 单据审核校验
- *
- * @param ischeck
- * @throws YosException
- */
- public void checkValidate(boolean ischeck) throws YosException {
- String billdate = billRow.getString("billdate");
- if (billdate.isBlank()) {
- throw new YosException("单据日期不能为空");
- }
- QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise_stockbill");
- querySQL.setWhere("sys_enterprise_stockbillid", sys_enterprise_stockbillid);
- querySQL.setWhere("DATE_FORMAT(billdate, '%Y-%m')>= DATE_FORMAT(NOW(), '%Y-%m')");
- if (querySQL.query().isEmpty()) {
- throw new YosException("单号为:【" + billno + "】的出入库单所在单据月份已关账,不可进行" + (ischeck ? "审核" : "反审核") + "操作!");
- }
- if (ischeck) {
- if (!status.equals("新建")) {
- throw new YosException("单号为:【" + billno + "】的出入库单为非新建状态,无法审核");
- }
- if (itemRows.isEmpty()) {
- throw new YosException("表体为空");
- }
- if (itemRows.min("qty").doubleValue() <= 0) {
- throw new YosException("表体数量必须大于0");
- }
- RowsMap skuRowsMap = codeRows.toRowsMap("sys_enterprise_stockbill_itemsid");
- for (Row itemRow : itemRows) {
- if (itemRow.getBoolean("skucontrol")) {
- String sys_enterprise_stockbill_itemsid = itemRow.getString("sys_enterprise_stockbill_itemsid");
- BigDecimal qty = itemRow.getBigDecimal("qty");
- if (!skuRowsMap.containsKey(sys_enterprise_stockbill_itemsid)) {
- throw new YosException("行号为:" + itemRow.getString("rowno") + "的物料没有指定序列号");
- }
- if (qty.doubleValue() != skuRowsMap.get(sys_enterprise_stockbill_itemsid).size()) {
- throw new YosException("行号为:" + itemRow.getString("rowno") + "的物料序列号数量不匹配");
- }
- }
- }
- if (rb == 0) {
- Rows olditemRows = dbConnect.runSqlQuery("select * from sys_enterprise_stockbill_items where siteid='" + siteid + "' and sys_enterprise_stockbillid=" + billRow.getLong("sourceid"));
- RowsMap olditemRowsMap = olditemRows.toRowsMap("sys_enterprise_stockbill_itemsid");
- for (Row row : itemRows) {
- if (StringUtils.isNotBlank(row.getString("sourceobject")) && row.getString("sourceobject").equals("sys_enterprise_stockbill_items")) {
- if (olditemRowsMap.containsKey(row.getString("sourceid"))) {
- Row row1 = olditemRowsMap.get(row.getString("sourceid")).get(0);
- if (row.getBigDecimal("qty").compareTo((row1.getBigDecimal("qty").subtract(row1.getBigDecimal("hongqty")))) > 0) {
- throw new YosException("行号为:" + row.getString("rowno") + "的商品红冲数量超过原单可红冲数量" + row1.getBigDecimal("qty").subtract(row1.getBigDecimal("hongqty")));
- }
- } else {
- throw new YosException("行号为:" + row.getString("rowno") + "的物料不在原单中,不可进行红冲");
- }
- }
- }
- }
- } else {
- if (!status.equals("审核")) {
- throw new YosException("单号为:【" + billno + "】的出入库单为非审核状态,无法反审核");
- }
- if (billRow.getBoolean("issystem")) {
- throw new YosException("系统生成单据不能反审核");
- }
- }
- RowsMap itemskuRowsMap = SQLFactory.createQuerySQL(dbConnect, "sa_itemsku", "sys_enterpriseid", "sys_enterprise_stockid", "stockid", "sku").setWhere("sku", codeRows.toArrayList("sku")).query().toRowsMap("sku");
- ArrayList<String> warrantycardsList = SQLFactory.createQuerySQL(dbConnect, "sa_warrantycard", "sku").setWhere("sku", codeRows.toArrayList("sku")).setWhere("isvoid", 0).query().toArrayList("sku");
- for (Row codeRow : codeRows) {
- String sku = codeRow.getString("sku");
- if (!itemskuRowsMap.containsKey(sku)) {
- throw new YosException("序列号【" + sku + "】不存在");
- }
- if (!isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("stockid") != getAgentStockID()) {
- throw new YosException("序列号【" + sku + "】在正处于美大仓库中");
- }
- if (itemskuRowsMap.get(sku).get(0).getLong("sys_enterpriseid") != sys_enterpriseid) {
- throw new YosException("序列号【" + sku + "】非当前经销商所属");
- }
- if (isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("stockid") != 0) {
- throw new YosException("序列号【" + sku + "】总部未出库,无法录入!");
- }
- if (isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("sys_enterprise_stockid") != 0) {
- throw new YosException("序列号【" + sku + "】正在仓库中");
- }
- if (!isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("sys_enterprise_stockid") != sys_enterprise_stockid) {
- throw new YosException("序列号【" + sku + "】不在仓库中");
- }
- if (warrantycardsList.contains(sku)) {
- throw new YosException("序列号【" + sku + "】已存在保修卡!");
- }
- }
- }
- public SQLList getCheckSql(boolean ischeck) throws YosException {
- SQLList sqlList = new SQLList();
- sqlList.add(updateInvbal(ischeck));//更新库存
- sqlList.add(updateMCode(ischeck));//更新序列号
- sqlList.add(updateHongqty(ischeck));//更新红冲数量
- sqlList.add(updateStatus(ischeck));//更新状态
- if (ischeck) {
- sqlList.add(DataContrlLog.createLog(controller, "sys_enterprise_stockbill", sys_enterprise_stockbillid, "审核", billRow.getString("type") + "单审核成功").getSQL());
- } else {
- sqlList.add(DataContrlLog.createLog(controller, "sys_enterprise_stockbill", sys_enterprise_stockbillid, "反审核", billRow.getString("type") + "单反审核成功").getSQL());
- }
- return sqlList;
- }
- /**
- * 更新库存
- *
- * @param ischeck
- * @return
- * @throws YosException
- */
- private SQLList updateInvbal(boolean ischeck) throws YosException {
- for (String sys_enterprise_itemid : itemRows.toArrayList("sys_enterprise_itemid")) {
- InsertSQL invbalInsert = SQLFactory.createInsertSQL(controller, "sys_enterprise_invbal");
- invbalInsert.setValue("siteid", siteid);
- invbalInsert.setValue("sys_enterpriseid", sys_enterpriseid);
- invbalInsert.setValue("sys_enterprise_stockid", sys_enterprise_stockid);
- invbalInsert.setValue("sys_enterprise_itemid", sys_enterprise_itemid);
- invbalInsert.setValue("qty", 0);
- invbalInsert.setWhere("not exists(select * from sys_enterprise_invbal where siteid='" + siteid + "' and sys_enterpriseid=" + sys_enterpriseid + " and sys_enterprise_stockid=" + sys_enterprise_stockid + " and sys_enterprise_itemid=" + sys_enterprise_itemid + " )");
- invbalInsert.insert();
- }
- SQLList sqlDump = new SQLList();
- for (Row row : itemRows) {
- BigDecimal qty = isInStock(ischeck) ? row.getBigDecimal("qty") : row.getBigDecimal("qty").negate();
- UpdateSQL invbalUpdate = SQLFactory.createUpdateSQL(controller, "sys_enterprise_invbal");
- invbalUpdate.addValue("qty", qty.doubleValue());
- invbalUpdate.setWhere("sys_enterpriseid", sys_enterpriseid);
- invbalUpdate.setWhere("sys_enterprise_stockid", sys_enterprise_stockid);
- invbalUpdate.setWhere("sys_enterprise_itemid", row.getLong("sys_enterprise_itemid"));
- invbalUpdate.setWhere("siteid", siteid);
- sqlDump.add(invbalUpdate.getSQL());
- }
- return sqlDump;
- }
- /**
- * 更新已红冲数量
- *
- * @param ischeck
- * @return
- * @throws YosException
- */
- private SQLList updateHongqty(boolean ischeck) throws YosException {
- SQLList sqlDump = new SQLList();
- if (rb == 0) {
- for (Row row : itemRows) {
- if (row.getString("sourceobject").equals("sys_enterprise_stockbill_items")) {
- long sourceid = row.getLong("sourceid");
- BigDecimal qty = ischeck ? row.getBigDecimal("qty") : row.getBigDecimal("qty").negate();
- sqlDump.add(SQLFactory.createUpdateSQL(controller, "sys_enterprise_stockbill_items").addValue("hongqty", qty).setWhere("sys_enterpriseid", sys_enterpriseid).setUniqueid(sourceid).getSQL());
- }
- }
- }
- return sqlDump;
- }
- /**
- * 更新状态
- *
- * @param ischeck
- * @return
- * @throws YosException
- */
- private SQLList updateStatus(boolean ischeck) throws YosException {
- SQLList sqlDump = new SQLList();
- if (ischeck) {
- sqlDump.add(SQLFactory.createUpdateSQL(controller, "sys_enterprise_stockbill").setValue("status", "审核").setValue("checkby", controller.username).setDateValue("checkdate").setWhere("sys_enterpriseid", sys_enterpriseid).setUniqueid(sys_enterprise_stockbillid).getSQL());
- } else {
- sqlDump.add(SQLFactory.createUpdateSQL(controller, "sys_enterprise_stockbill").setValue("status", "新建").setNull("checkby", "checkdate").setWhere("sys_enterpriseid", sys_enterpriseid).setUniqueid(sys_enterprise_stockbillid).getSQL());
- }
- return sqlDump;
- }
- /**
- * 更新序列号
- *
- * @param ischeck
- * @return
- * @throws YosException
- */
- public SQLList updateMCode(boolean ischeck) throws YosException {
- SQLList sqlDump = new SQLList();
- for (Row row : codeRows) {
- String sku = row.getString("sku");
- UpdateSQL saItemsku = SQLFactory.createUpdateSQL(controller, "sa_itemsku");
- if (isInStock(ischeck)) {
- saItemsku.setValue("stockid", getAgentStockID());
- saItemsku.setValue("sys_enterprise_stockid", sys_enterprise_stockid);
- saItemsku.setValue("status", "售出");
- saItemsku.setValue("sys_enterpriseid", sys_enterpriseid);
- } else {
- saItemsku.setValue("stockid", 0);
- saItemsku.setValue("sys_enterprise_stockid", 0);
- saItemsku.setValue("status", "终端客户售出");
- saItemsku.setValue("sys_enterpriseid", sys_enterpriseid);
- }
- saItemsku.setWhere("sku", sku);
- saItemsku.setSiteid(siteid);
- sqlDump.add(saItemsku.getSQL());
- }
- return sqlDump;
- }
- /**
- * 单据查询
- *
- * @throws YosException
- */
- private void queryBills() throws YosException {
- Rows billRows = dbConnect.runSqlQuery("select * from sys_enterprise_stockbill where sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
- if (billRows.isNotEmpty()) {
- billRow = billRows.get(0);
- } else {
- throw new YosException("该出入库单不存在");
- }
- this.billno = billRow.getString("billno");
- this.issale = billRow.getString("type").equals("销售出库单");
- this.billType = billRow.getString("type");
- this.billTypeMX = billRow.getString("typemx");
- this.rb = billRow.getInteger("rb");
- this.siteid = billRow.getString("siteid");
- this.status = billRow.getString("status");
- this.sys_enterpriseid = billRow.getLong("sys_enterpriseid");
- this.sys_enterprise_stockid = billRow.getLong("sys_enterprise_stockid");
- itemRows = dbConnect.runSqlQuery("select t1.* from sys_enterprise_stockbill_items t1 where sys_enterpriseid=" + sys_enterpriseid + " and t1.sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
- codeRows = dbConnect.runSqlQuery("select t1.* from sys_enterprise_stockbill_skus t1 where sys_enterpriseid=" + sys_enterpriseid + " and t1.sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
- }
- public abstract boolean isInStock(boolean fischeck);
- public long getAgentStockID() throws YosException {
- Rows rows = dbConnect.runSqlQuery("select stockid from st_stock where type='经销商仓' and siteid='" + siteid + "'");
- if (rows.isEmpty()) {
- throw new YosException("更新序列号所在仓库没有找到经销商仓");
- }
- return rows.get(0).getLong("stockid");
- }
- }
|