BasicBill.java 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. package beans.enterprise_stockbill.bills;
  2. import beans.datacontrllog.DataContrlLog;
  3. import common.Controller;
  4. import common.UserInfo;
  5. import common.YosException;
  6. import common.data.*;
  7. import common.data.db.DBConnect;
  8. import common.data.db.SQLList;
  9. import org.apache.commons.lang.StringUtils;
  10. import java.math.BigDecimal;
  11. import java.util.ArrayList;
  12. public abstract class BasicBill {
  13. Controller controller;
  14. DBConnect dbConnect;
  15. UserInfo userInfo;
  16. long sys_enterprise_stockbillid;
  17. long sys_enterprise_stockid;
  18. boolean issale;//是否销售出库
  19. String billType;//单据类型
  20. String billTypeMX;//单据类型
  21. String billno;
  22. String siteid;
  23. String status;
  24. int rb;
  25. Row billRow;
  26. Rows itemRows;
  27. Rows codeRows;
  28. long sys_enterpriseid;
  29. public static BasicBill getBill(Controller controller, long sys_enterprise_stockbillid) throws YosException {
  30. Rows billRows = controller.dbConnect.runSqlQuery("select type from sys_enterprise_stockbill where sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
  31. if (billRows.isEmpty()) {
  32. throw new YosException("该出入库单不存在");
  33. }
  34. String type = billRows.get(0).getString("type");
  35. switch (type) {
  36. case "销售出库":
  37. return new XSCK(controller, sys_enterprise_stockbillid);
  38. case "其他出库":
  39. return new QTCK(controller, sys_enterprise_stockbillid);
  40. case "其他入库":
  41. return new QTRK(controller, sys_enterprise_stockbillid);
  42. case "报货入库":
  43. return new BHRK(controller, sys_enterprise_stockbillid);
  44. default: {
  45. throw new YosException("不支持的出入库单类型");
  46. }
  47. }
  48. }
  49. /**
  50. * 构造函数
  51. *
  52. * @param controller
  53. * @param sys_enterprise_stockbillid 出入库单ID
  54. * @throws YosException
  55. */
  56. public BasicBill(Controller controller, long sys_enterprise_stockbillid) throws YosException {
  57. this.controller = controller;
  58. this.dbConnect = this.controller.dbConnect;
  59. this.userInfo = controller.userInfo;
  60. this.sys_enterprise_stockbillid = sys_enterprise_stockbillid;
  61. queryBills();
  62. }
  63. /**
  64. * 单据审核校验
  65. *
  66. * @param ischeck
  67. * @throws YosException
  68. */
  69. public void checkValidate(boolean ischeck) throws YosException {
  70. String billdate = billRow.getString("billdate");
  71. if (billdate.isBlank()) {
  72. throw new YosException("单据日期不能为空");
  73. }
  74. QuerySQL querySQL = SQLFactory.createQuerySQL(controller, "sys_enterprise_stockbill");
  75. querySQL.setWhere("sys_enterprise_stockbillid", sys_enterprise_stockbillid);
  76. querySQL.setWhere("DATE_FORMAT(billdate, '%Y-%m')>= DATE_FORMAT(NOW(), '%Y-%m')");
  77. if (querySQL.query().isEmpty()) {
  78. throw new YosException("单号为:【" + billno + "】的出入库单所在单据月份已关账,不可进行" + (ischeck ? "审核" : "反审核") + "操作!");
  79. }
  80. if (ischeck) {
  81. if (!status.equals("新建")) {
  82. throw new YosException("单号为:【" + billno + "】的出入库单为非新建状态,无法审核");
  83. }
  84. if (itemRows.isEmpty()) {
  85. throw new YosException("表体为空");
  86. }
  87. if (itemRows.min("qty").doubleValue() <= 0) {
  88. throw new YosException("表体数量必须大于0");
  89. }
  90. RowsMap skuRowsMap = codeRows.toRowsMap("sys_enterprise_stockbill_itemsid");
  91. for (Row itemRow : itemRows) {
  92. if (itemRow.getBoolean("skucontrol")) {
  93. String sys_enterprise_stockbill_itemsid = itemRow.getString("sys_enterprise_stockbill_itemsid");
  94. BigDecimal qty = itemRow.getBigDecimal("qty");
  95. if (!skuRowsMap.containsKey(sys_enterprise_stockbill_itemsid)) {
  96. throw new YosException("行号为:" + itemRow.getString("rowno") + "的物料没有指定序列号");
  97. }
  98. if (qty.doubleValue() != skuRowsMap.get(sys_enterprise_stockbill_itemsid).size()) {
  99. throw new YosException("行号为:" + itemRow.getString("rowno") + "的物料序列号数量不匹配");
  100. }
  101. }
  102. }
  103. if (rb == 0) {
  104. Rows olditemRows = dbConnect.runSqlQuery("select * from sys_enterprise_stockbill_items where siteid='" + siteid + "' and sys_enterprise_stockbillid=" + billRow.getLong("sourceid"));
  105. RowsMap olditemRowsMap = olditemRows.toRowsMap("sys_enterprise_stockbill_itemsid");
  106. for (Row row : itemRows) {
  107. if (StringUtils.isNotBlank(row.getString("sourceobject")) && row.getString("sourceobject").equals("sys_enterprise_stockbill_items")) {
  108. if (olditemRowsMap.containsKey(row.getString("sourceid"))) {
  109. Row row1 = olditemRowsMap.get(row.getString("sourceid")).get(0);
  110. if (row.getBigDecimal("qty").compareTo((row1.getBigDecimal("qty").subtract(row1.getBigDecimal("hongqty")))) > 0) {
  111. throw new YosException("行号为:" + row.getString("rowno") + "的商品红冲数量超过原单可红冲数量" + row1.getBigDecimal("qty").subtract(row1.getBigDecimal("hongqty")));
  112. }
  113. } else {
  114. throw new YosException("行号为:" + row.getString("rowno") + "的物料不在原单中,不可进行红冲");
  115. }
  116. }
  117. }
  118. }
  119. } else {
  120. if (!status.equals("审核")) {
  121. throw new YosException("单号为:【" + billno + "】的出入库单为非审核状态,无法反审核");
  122. }
  123. if (billRow.getBoolean("issystem")) {
  124. throw new YosException("系统生成单据不能反审核");
  125. }
  126. }
  127. RowsMap itemskuRowsMap = SQLFactory.createQuerySQL(dbConnect, "sa_itemsku", "sys_enterpriseid", "sys_enterprise_stockid", "stockid", "sku").setWhere("sku", codeRows.toArrayList("sku")).query().toRowsMap("sku");
  128. ArrayList<String> warrantycardsList = SQLFactory.createQuerySQL(dbConnect, "sa_warrantycard", "sku").setWhere("sku", codeRows.toArrayList("sku")).setWhere("isvoid", 0).query().toArrayList("sku");
  129. for (Row codeRow : codeRows) {
  130. String sku = codeRow.getString("sku");
  131. if (!itemskuRowsMap.containsKey(sku)) {
  132. throw new YosException("序列号【" + sku + "】不存在");
  133. }
  134. if (!isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("stockid") != getAgentStockID()) {
  135. throw new YosException("序列号【" + sku + "】在正处于美大仓库中");
  136. }
  137. if (itemskuRowsMap.get(sku).get(0).getLong("sys_enterpriseid") != sys_enterpriseid) {
  138. throw new YosException("序列号【" + sku + "】非当前经销商所属");
  139. }
  140. if (isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("stockid") != 0) {
  141. throw new YosException("序列号【" + sku + "】总部未出库,无法录入!");
  142. }
  143. if (isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("sys_enterprise_stockid") != 0) {
  144. throw new YosException("序列号【" + sku + "】正在仓库中");
  145. }
  146. if (!isInStock(ischeck) && itemskuRowsMap.get(sku).get(0).getLong("sys_enterprise_stockid") != sys_enterprise_stockid) {
  147. throw new YosException("序列号【" + sku + "】不在仓库中");
  148. }
  149. if (warrantycardsList.contains(sku)) {
  150. throw new YosException("序列号【" + sku + "】已存在保修卡!");
  151. }
  152. }
  153. }
  154. public SQLList getCheckSql(boolean ischeck) throws YosException {
  155. SQLList sqlList = new SQLList();
  156. sqlList.add(updateInvbal(ischeck));//更新库存
  157. sqlList.add(updateMCode(ischeck));//更新序列号
  158. sqlList.add(updateHongqty(ischeck));//更新红冲数量
  159. sqlList.add(updateStatus(ischeck));//更新状态
  160. if (ischeck) {
  161. sqlList.add(DataContrlLog.createLog(controller, "sys_enterprise_stockbill", sys_enterprise_stockbillid, "审核", billRow.getString("type") + "单审核成功").getSQL());
  162. } else {
  163. sqlList.add(DataContrlLog.createLog(controller, "sys_enterprise_stockbill", sys_enterprise_stockbillid, "反审核", billRow.getString("type") + "单反审核成功").getSQL());
  164. }
  165. return sqlList;
  166. }
  167. /**
  168. * 更新库存
  169. *
  170. * @param ischeck
  171. * @return
  172. * @throws YosException
  173. */
  174. private SQLList updateInvbal(boolean ischeck) throws YosException {
  175. for (String sys_enterprise_itemid : itemRows.toArrayList("sys_enterprise_itemid")) {
  176. InsertSQL invbalInsert = SQLFactory.createInsertSQL(controller, "sys_enterprise_invbal");
  177. invbalInsert.setValue("siteid", siteid);
  178. invbalInsert.setValue("sys_enterpriseid", sys_enterpriseid);
  179. invbalInsert.setValue("sys_enterprise_stockid", sys_enterprise_stockid);
  180. invbalInsert.setValue("sys_enterprise_itemid", sys_enterprise_itemid);
  181. invbalInsert.setValue("qty", 0);
  182. 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 + " )");
  183. invbalInsert.insert();
  184. }
  185. SQLList sqlDump = new SQLList();
  186. for (Row row : itemRows) {
  187. BigDecimal qty = isInStock(ischeck) ? row.getBigDecimal("qty") : row.getBigDecimal("qty").negate();
  188. UpdateSQL invbalUpdate = SQLFactory.createUpdateSQL(controller, "sys_enterprise_invbal");
  189. invbalUpdate.addValue("qty", qty.doubleValue());
  190. invbalUpdate.setWhere("sys_enterpriseid", sys_enterpriseid);
  191. invbalUpdate.setWhere("sys_enterprise_stockid", sys_enterprise_stockid);
  192. invbalUpdate.setWhere("sys_enterprise_itemid", row.getLong("sys_enterprise_itemid"));
  193. invbalUpdate.setWhere("siteid", siteid);
  194. sqlDump.add(invbalUpdate.getSQL());
  195. }
  196. return sqlDump;
  197. }
  198. /**
  199. * 更新已红冲数量
  200. *
  201. * @param ischeck
  202. * @return
  203. * @throws YosException
  204. */
  205. private SQLList updateHongqty(boolean ischeck) throws YosException {
  206. SQLList sqlDump = new SQLList();
  207. if (rb == 0) {
  208. for (Row row : itemRows) {
  209. if (row.getString("sourceobject").equals("sys_enterprise_stockbill_items")) {
  210. long sourceid = row.getLong("sourceid");
  211. BigDecimal qty = ischeck ? row.getBigDecimal("qty") : row.getBigDecimal("qty").negate();
  212. sqlDump.add(SQLFactory.createUpdateSQL(controller, "sys_enterprise_stockbill_items").addValue("hongqty", qty).setWhere("sys_enterpriseid", sys_enterpriseid).setUniqueid(sourceid).getSQL());
  213. }
  214. }
  215. }
  216. return sqlDump;
  217. }
  218. /**
  219. * 更新状态
  220. *
  221. * @param ischeck
  222. * @return
  223. * @throws YosException
  224. */
  225. private SQLList updateStatus(boolean ischeck) throws YosException {
  226. SQLList sqlDump = new SQLList();
  227. if (ischeck) {
  228. 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());
  229. } else {
  230. sqlDump.add(SQLFactory.createUpdateSQL(controller, "sys_enterprise_stockbill").setValue("status", "新建").setNull("checkby", "checkdate").setWhere("sys_enterpriseid", sys_enterpriseid).setUniqueid(sys_enterprise_stockbillid).getSQL());
  231. }
  232. return sqlDump;
  233. }
  234. /**
  235. * 更新序列号
  236. *
  237. * @param ischeck
  238. * @return
  239. * @throws YosException
  240. */
  241. public SQLList updateMCode(boolean ischeck) throws YosException {
  242. SQLList sqlDump = new SQLList();
  243. for (Row row : codeRows) {
  244. String sku = row.getString("sku");
  245. UpdateSQL saItemsku = SQLFactory.createUpdateSQL(controller, "sa_itemsku");
  246. if (isInStock(ischeck)) {
  247. saItemsku.setValue("stockid", getAgentStockID());
  248. saItemsku.setValue("sys_enterprise_stockid", sys_enterprise_stockid);
  249. saItemsku.setValue("status", "售出");
  250. saItemsku.setValue("sys_enterpriseid", sys_enterpriseid);
  251. } else {
  252. saItemsku.setValue("stockid", 0);
  253. saItemsku.setValue("sys_enterprise_stockid", 0);
  254. saItemsku.setValue("status", "终端客户售出");
  255. saItemsku.setValue("sys_enterpriseid", sys_enterpriseid);
  256. }
  257. saItemsku.setWhere("sku", sku);
  258. saItemsku.setSiteid(siteid);
  259. sqlDump.add(saItemsku.getSQL());
  260. }
  261. return sqlDump;
  262. }
  263. /**
  264. * 单据查询
  265. *
  266. * @throws YosException
  267. */
  268. private void queryBills() throws YosException {
  269. Rows billRows = dbConnect.runSqlQuery("select * from sys_enterprise_stockbill where sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
  270. if (billRows.isNotEmpty()) {
  271. billRow = billRows.get(0);
  272. } else {
  273. throw new YosException("该出入库单不存在");
  274. }
  275. this.billno = billRow.getString("billno");
  276. this.issale = billRow.getString("type").equals("销售出库单");
  277. this.billType = billRow.getString("type");
  278. this.billTypeMX = billRow.getString("typemx");
  279. this.rb = billRow.getInteger("rb");
  280. this.siteid = billRow.getString("siteid");
  281. this.status = billRow.getString("status");
  282. this.sys_enterpriseid = billRow.getLong("sys_enterpriseid");
  283. this.sys_enterprise_stockid = billRow.getLong("sys_enterprise_stockid");
  284. itemRows = dbConnect.runSqlQuery("select t1.* from sys_enterprise_stockbill_items t1 where sys_enterpriseid=" + sys_enterpriseid + " and t1.sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
  285. codeRows = dbConnect.runSqlQuery("select t1.* from sys_enterprise_stockbill_skus t1 where sys_enterpriseid=" + sys_enterpriseid + " and t1.sys_enterprise_stockbillid ='" + sys_enterprise_stockbillid + "'");
  286. }
  287. public abstract boolean isInStock(boolean fischeck);
  288. public long getAgentStockID() throws YosException {
  289. Rows rows = dbConnect.runSqlQuery("select stockid from st_stock where type='经销商仓' and siteid='" + siteid + "'");
  290. if (rows.isEmpty()) {
  291. throw new YosException("更新序列号所在仓库没有找到经销商仓");
  292. }
  293. return rows.get(0).getLong("stockid");
  294. }
  295. }