period.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. package restcontroller.webmanage.sale.period;
  2. import beans.data.BatchDeleteErr;
  3. import beans.datacontrllog.DataContrlLog;
  4. import com.alibaba.fastjson.JSONArray;
  5. import com.alibaba.fastjson.JSONObject;
  6. import common.Controller;
  7. import common.YosException;
  8. import common.annotation.API;
  9. import common.annotation.CACHEING;
  10. import common.annotation.CACHEING_CLEAN;
  11. import common.data.*;
  12. import org.apache.commons.lang.StringUtils;
  13. import restcontroller.R;
  14. import java.time.LocalDate;
  15. import java.time.YearMonth;
  16. import java.util.ArrayList;
  17. import java.util.Calendar;
  18. @API(title = "会计期间")
  19. public class period extends Controller {
  20. public period(JSONObject content) throws YosException {
  21. super(content);
  22. }
  23. @API(title = "新增会计期间", apiversion = R.ID2025072314205903.v1.class)
  24. @CACHEING_CLEAN(apiClass = {period.class})
  25. public String insertormodify_period() throws YosException {
  26. ArrayList<String> sqlList = new ArrayList<>();
  27. // 表名
  28. String tableName = "st_period";
  29. Long st_periodid = content.getLong("st_periodid");
  30. Long month = content.getLongValue("month");
  31. long year= content.getLongValue("year");
  32. String enddate = content.getStringValue("enddate");
  33. String begdate = content.getStringValue("begdate");
  34. if (st_periodid <= 0) {
  35. st_periodid = createTableID(tableName);
  36. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, tableName);
  37. insertSQL.setUniqueid(st_periodid);
  38. insertSQL.setSiteid(siteid);
  39. insertSQL.setValue("month", month);
  40. insertSQL.setValue("year", year);
  41. insertSQL.setValue("enddate", StringUtils.isBlank(enddate) ?"null":enddate);
  42. insertSQL.setValue("begdate", StringUtils.isBlank(begdate) ?"null":begdate);
  43. sqlList.add(insertSQL.getSQL());
  44. sqlList.add(DataContrlLog.createLog(this, "st_period", st_periodid, "新增", "会计期间新增成功").getSQL());
  45. } else {
  46. Rows rows = dbConnect.runSqlQuery(
  47. "SELECT isclose from st_period WHERE st_periodid = "
  48. + st_periodid);
  49. if (rows.isNotEmpty()) {
  50. if (!rows.get(0).getBoolean("isclose")) {
  51. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, tableName);
  52. updateSQL.setUniqueid(st_periodid);
  53. updateSQL.setSiteid(siteid);
  54. updateSQL.setValue("month", month);
  55. updateSQL.setValue("year", year);
  56. updateSQL.setValue("enddate", StringUtils.isBlank(enddate) ?"null":enddate);
  57. updateSQL.setValue("begdate", StringUtils.isBlank(begdate) ?"null":begdate);
  58. sqlList.add(updateSQL.getSQL());
  59. sqlList.add(DataContrlLog.createLog(this, "st_period", st_periodid, "更新", "会计期间更新成功").getSQL());
  60. } else {
  61. return getErrReturnObject().setErrMsg("已结账的会计期间无法编辑").toString();
  62. }
  63. } else {
  64. return getErrReturnObject().setErrMsg("该会计期间不存在").toString();
  65. }
  66. }
  67. dbConnect.runSqlUpdate(sqlList);
  68. content.put("st_periodid", st_periodid);
  69. return queryperiodMain();
  70. }
  71. @API(title = "会计期间详情", apiversion = R.ID2025072314211003.v1.class)
  72. @CACHEING
  73. public String queryperiodMain() throws YosException {
  74. Long st_periodid = content.getLong("st_periodid");
  75. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "st_period","*");
  76. querySQL.setTableAlias("t1");
  77. querySQL.setWhere("t1.siteid", siteid);
  78. querySQL.setWhere("t1.st_periodid", st_periodid);
  79. Rows rows = querySQL.query();
  80. Row row = rows.isNotEmpty() ? rows.get(0) : new Row();
  81. return getSucReturnObject().setData(row).toString();
  82. }
  83. @API(title = "查询会计期间列表", apiversion = R.ID2025072314211703.v1.class)
  84. @CACHEING
  85. public String queryperiodList() throws YosException {
  86. StringBuffer where = new StringBuffer(" 1=1 ");
  87. QuerySQL querySQL = SQLFactory.createQuerySQL(this, "st_period","*");
  88. querySQL.setTableAlias("t1");
  89. querySQL.setWhere("t1.siteid", siteid);
  90. querySQL.setWhere(where);
  91. if (pageSorting.equals("''")) {
  92. pageSorting = "concat(t1.year,'-', LPAD(t1.month, 2, '0')) desc";
  93. }
  94. querySQL.setOrderBy(pageSorting);
  95. querySQL.setPage(pageSize, pageNumber);
  96. Rows rows = querySQL.query();
  97. return getSucReturnObject().setData(rows).toString();
  98. }
  99. @API(title = "删除", apiversion = R.ID2025072314212603.v1.class)
  100. @CACHEING_CLEAN(apiClass = {period.class})
  101. public String delete() throws YosException {
  102. JSONArray st_periodids = content.getJSONArray("st_periodids");
  103. BatchDeleteErr batchDeleteErr = BatchDeleteErr.create(this, st_periodids.size());
  104. for (Object o : st_periodids) {
  105. long st_periodid = Long.parseLong(o.toString());
  106. Rows RowsStatus = dbConnect.runSqlQuery("select st_periodid,isclose from st_period where siteid='"
  107. + siteid + "' and st_periodid='" + st_periodid + "'");
  108. if (RowsStatus.isNotEmpty()) {
  109. if (RowsStatus.get(0).getBoolean("isclose")) {
  110. batchDeleteErr.addErr(st_periodid, "已结账的会计期间无法删除");
  111. continue;
  112. }
  113. }
  114. dbConnect.runSqlUpdate(
  115. "delete from st_period where siteid='" + siteid + "' and st_periodid=" + st_periodid);
  116. }
  117. return batchDeleteErr.getReturnObject().toString();
  118. }
  119. @API(title = "账户结账", apiversion = R.ID2025091310230303.v1.class)
  120. @CACHEING_CLEAN( apiClass = {period.class})
  121. public String cashbeginbalclose() throws YosException {
  122. Long st_periodid = content.getLong("st_periodid");
  123. boolean isclose = content.getBoolean("isclose");
  124. Rows rows = dbConnect.runSqlQuery("select * from st_period where siteid='"+siteid+"' and st_periodid="+st_periodid);
  125. if(rows.isEmpty()){
  126. return getErrReturnObject().setErrMsg("会计期间不存在").toString();
  127. }
  128. Rows accountrows = dbConnect.runSqlQuery("select sa_accountclassid from sa_accountclass where siteid='"+siteid+"' and accountnoa='01'");
  129. if(accountrows.isEmpty()){
  130. return getErrReturnObject().setErrMsg("01账户不存在").toString();
  131. }
  132. ArrayList<String> sqlList = new ArrayList<>();
  133. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "st_period");
  134. updateSQL.setUniqueid(st_periodid);
  135. updateSQL.setSiteid(siteid);
  136. updateSQL.setValue("isclose",isclose);
  137. updateSQL.setValue("closeby", isclose?username:"null");
  138. if(isclose){
  139. updateSQL.setDateValue("closedate");
  140. }else{
  141. updateSQL.setValue("closedate","null");
  142. }
  143. sqlList.add(updateSQL.getSQL());
  144. LocalDate today = LocalDate.of(rows.get(0).getInteger("year"), rows.get(0).getInteger("month"), 1);
  145. // 获取下一个月的第一天
  146. LocalDate nextMonthFirstDay = today.with(today.plusMonths(1)).withDayOfMonth(1);
  147. if (isclose) {
  148. SQLFactory sqlFactory = new SQLFactory(this, "获取暂存账户数据");
  149. sqlFactory.addParameter_SQL("begindate", nextMonthFirstDay.getYear() +"-12-31");
  150. sqlFactory.addParameter_SQL("enddate", nextMonthFirstDay.getYear()+"-12-31");
  151. Rows cashbeginbalRows =dbConnect.runSqlQuery(sqlFactory);
  152. if(cashbeginbalRows.isNotEmpty()){
  153. for (Row row :cashbeginbalRows){
  154. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_cashbeginbal");
  155. insertSQL.setUniqueid(createTableID("sa_cashbeginbal"));
  156. insertSQL.setSiteid(siteid);
  157. insertSQL.setValue("month", nextMonthFirstDay.getMonthValue());
  158. insertSQL.setValue("year", nextMonthFirstDay.getYear() );
  159. insertSQL.setValue("sys_enterpriseid", row.getLong("sys_enterpriseid"));
  160. insertSQL.setValue("balance", row.getBigDecimal("bqjy"));
  161. insertSQL.setValue("creditquota", 0);
  162. insertSQL.setValue("discountamount", row.getBigDecimal("kyyh"));
  163. insertSQL.setValue("sa_accountclassid", accountrows.get(0).getLong("sa_accountclassid"));
  164. insertSQL.setValue("remarks", "年结转入");
  165. insertSQL.setValue("createby", username);
  166. insertSQL.setDateValue("createdate");
  167. insertSQL.setValue("changeby", username);
  168. insertSQL.setDateValue("changedate");
  169. sqlList.add(insertSQL.getSQL());
  170. }
  171. }
  172. sqlList.add(
  173. DataContrlLog.createLog(this, "st_period", st_periodid, "账户结账", "会计期间账户结账成功").getSQL());
  174. } else {
  175. sqlList.add("delete from sa_cashbeginbal where year="+nextMonthFirstDay.getYear()+" and month="+nextMonthFirstDay.getMonthValue());
  176. sqlList.add(
  177. DataContrlLog.createLog(this, "st_period", st_periodid, "反账户结账", "会计期间反账户结账成功").getSQL());
  178. }
  179. dbConnect.runSqlUpdate(sqlList);
  180. return getSucReturnObject().toString();
  181. }
  182. @API(title = "库存结账", apiversion = R.ID2025072314213503.v1.class)
  183. @CACHEING_CLEAN( apiClass = {period.class})
  184. public String invbalclose() throws YosException {
  185. Long st_periodid = content.getLong("st_periodid");
  186. boolean isclose = content.getBoolean("isclose");
  187. Rows rows = dbConnect.runSqlQuery("select * from st_period where siteid='"+siteid+"' and st_periodid="+st_periodid);
  188. if(rows.isEmpty()){
  189. return getErrReturnObject().setErrMsg("会计期间不存在").toString();
  190. }
  191. ArrayList<String> sqlList = new ArrayList<>();
  192. UpdateSQL updateSQL = SQLFactory.createUpdateSQL(this, "st_period");
  193. updateSQL.setUniqueid(st_periodid);
  194. updateSQL.setSiteid(siteid);
  195. updateSQL.setValue("isclose",isclose);
  196. updateSQL.setValue("closeby", isclose?username:"null");
  197. if(isclose){
  198. updateSQL.setDateValue("closedate");
  199. }else{
  200. updateSQL.setValue("closedate","null");
  201. }
  202. sqlList.add(updateSQL.getSQL());
  203. if (isclose) {
  204. YearMonth specificYearMonth = YearMonth.of(rows.get(0).getInteger("year"), rows.get(0).getInteger("month"));
  205. // 获取上一个月
  206. YearMonth previousMonth = specificYearMonth.minusMonths(1);
  207. System.err.println("\n" +
  208. "\t\t\t\t\tselect t1.itemid,t3.stockid,ifnull(t2.qty,0) + ifnull(t3.qty,0) qty from plm_item t1 left join st_stock t0 on t1.stockno=t0.stockno and t1.siteid=t0.siteid and t0.isused=1 \n" +
  209. "\t\t\t\t\tleft join (\n" +
  210. "\t\t\t\t\tselect itemid,stockid,qty,siteid from sa_inventoryear\n" +
  211. "\t\t\t\t\twhere year ='"+previousMonth.getYear()+"' and month ='"+ previousMonth.getMonthValue()+"'\n" +
  212. "\t\t\t\t\t)t2 on t1.itemid = t2.itemid and t1.siteid = t2.siteid\n" +
  213. "\t\t\t\tleft join (\n" +
  214. "\t\t\t\t\tselect itemid,stockid,siteid,sum(qty) qty from ( \n" +
  215. "\t\t\t\t\t\tselect t2.itemid,t2.stockid,t1.siteid, \n" +
  216. "\t\t\t\t\t\tcase when type like '%入%' and rb ='1' then t2.qty\n" +
  217. "\t\t\t\t\t\t\t when type like '%入%' and rb ='0' then -t2.qty\n" +
  218. "\t\t\t\t\t\t\t when type like '%出%' and rb ='1' then -t2.qty\n" +
  219. "\t\t\t\t\t\t\t when type like '%出%' and rb ='0' then t2.qty\n" +
  220. "\t\t\t\t\t\tend qty\n" +
  221. "\t\t\t\t\t\t from st_stockbill t1\n" +
  222. "\t\t\t\t\t\tjoin st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid \n" +
  223. "\t\t\t\t\t\twhere t1.status ='审核' and ifnull(t1.typemx,'') != '正品入库'\n" +
  224. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') >='"+rows.get(0).getString("begdate")+"'\n" +
  225. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') <='"+rows.get(0).getString("enddate")+"'\n" +
  226. "\t\t\t\t\t\tunion all \n" +
  227. "\t\t\t\t\t\tselect t2.itemid,t2.stockid,t1.siteid, \n" +
  228. "\t\t\t\t\t\tcase when type like '%入%' and rb ='1' then t2.qty\n" +
  229. "\t\t\t\t\t\t\t when type like '%入%' and rb ='0' then -t2.qty\n" +
  230. "\t\t\t\t\t\tend qty\n" +
  231. "\t\t\t\t\t\t from st_stockbill t1\n" +
  232. "\t\t\t\t\t\tjoin st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid\n" +
  233. "\t\t\t\t\t\twhere t1.status ='审核' and ifnull(t1.typemx,'') = '正品入库' and ifnull(t2.amount,0) != 0\n" +
  234. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') >='"+rows.get(0).getString("begdate")+"'\n" +
  235. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') <='"+rows.get(0).getString("enddate")+"'\n" +
  236. "\t\t\t\t\t)t group by itemid,stockid,siteid\n" +
  237. "\t\t\t\t)t3 on t1.itemid = t3.itemid and t0.stockid = t3.stockid and t1.siteid = t3.siteid \n" +
  238. "\t\t\t\twhere ifnull(t2.qty,0) + ifnull(t3.qty,0)!=0 ");
  239. Rows invbalRows= dbConnect.runSqlQuery("\n" +
  240. "\t\t\t\t\tselect t1.itemid,t3.stockid,ifnull(t2.qty,0) + ifnull(t3.qty,0) qty from plm_item t1 left join st_stock t0 on t1.stockno=t0.stockno and t1.siteid=t0.siteid and t0.isused=1 \n" +
  241. "\t\t\t\t\tleft join (\n" +
  242. "\t\t\t\t\tselect itemid,stockid,qty,siteid from sa_inventoryear\n" +
  243. "\t\t\t\t\twhere year ='"+previousMonth.getYear()+"' and month ='"+ previousMonth.getMonthValue()+"'\n" +
  244. "\t\t\t\t\t)t2 on t1.itemid = t2.itemid and t1.siteid = t2.siteid\n" +
  245. "\t\t\t\tleft join (\n" +
  246. "\t\t\t\t\tselect itemid,stockid,siteid,sum(qty) qty from ( \n" +
  247. "\t\t\t\t\t\tselect t2.itemid,t2.stockid,t1.siteid, \n" +
  248. "\t\t\t\t\t\tcase when type like '%入%' and rb ='1' then t2.qty\n" +
  249. "\t\t\t\t\t\t\t when type like '%入%' and rb ='0' then -t2.qty\n" +
  250. "\t\t\t\t\t\t\t when type like '%出%' and rb ='1' then -t2.qty\n" +
  251. "\t\t\t\t\t\t\t when type like '%出%' and rb ='0' then t2.qty\n" +
  252. "\t\t\t\t\t\tend qty\n" +
  253. "\t\t\t\t\t\t from st_stockbill t1\n" +
  254. "\t\t\t\t\t\tjoin st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid \n" +
  255. "\t\t\t\t\t\twhere t1.status ='审核' and ifnull(t1.typemx,'') != '正品入库'\n" +
  256. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') >='"+rows.get(0).getString("begdate")+"'\n" +
  257. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') <='"+rows.get(0).getString("enddate")+"'\n" +
  258. "\t\t\t\t\t\tunion all \n" +
  259. "\t\t\t\t\t\tselect t2.itemid,t2.stockid,t1.siteid, \n" +
  260. "\t\t\t\t\t\tcase when type like '%入%' and rb ='1' then t2.qty\n" +
  261. "\t\t\t\t\t\t\t when type like '%入%' and rb ='0' then -t2.qty\n" +
  262. "\t\t\t\t\t\tend qty\n" +
  263. "\t\t\t\t\t\t from st_stockbill t1\n" +
  264. "\t\t\t\t\t\tjoin st_stockbill_items t2 on t1.st_stockbillid = t2.st_stockbillid and t1.siteid = t2.siteid\n" +
  265. "\t\t\t\t\t\twhere t1.status ='审核' and ifnull(t1.typemx,'') = '正品入库' and ifnull(t2.amount,0) != 0\n" +
  266. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') >='"+rows.get(0).getString("begdate")+"'\n" +
  267. "\t\t\t\t\t\tand DATE_FORMAT(t1.billdate, '%Y-%m-%d') <='"+rows.get(0).getString("enddate")+"'\n" +
  268. "\t\t\t\t\t)t group by itemid,stockid,siteid\n" +
  269. "\t\t\t\t)t3 on t1.itemid = t3.itemid and t0.stockid = t3.stockid and t1.siteid = t3.siteid \n" +
  270. "\t\t\t\twhere ifnull(t2.qty,0) + ifnull(t3.qty,0)!=0 ");
  271. if(invbalRows.isNotEmpty()){
  272. for (Row row :invbalRows){
  273. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "sa_inventoryear");
  274. insertSQL.setUniqueid(createTableID("sa_inventoryear"));
  275. insertSQL.setSiteid(siteid);
  276. insertSQL.setValue("month", rows.get(0).getLong("month"));
  277. insertSQL.setValue("year", rows.get(0).getLong("year") );
  278. insertSQL.setValue("itemid", row.getLong("itemid"));
  279. insertSQL.setValue("stockid", row.getLong("stockid"));
  280. insertSQL.setValue("qty",row.getBigDecimal("qty"));
  281. insertSQL.setValue("remarks", "月结转入");
  282. insertSQL.setValue("createby", username);
  283. insertSQL.setDateValue("createdate");
  284. insertSQL.setValue("changeby", username);
  285. insertSQL.setDateValue("changedate");
  286. sqlList.add(insertSQL.getSQL());
  287. }
  288. }
  289. sqlList.add(
  290. DataContrlLog.createLog(this, "st_period", st_periodid, "库存结账", "会计期间库存结账成功").getSQL());
  291. } else {
  292. sqlList.add("delete from sa_inventoryear where year="+rows.get(0).getLong("year")+" and month="+rows.get(0).getLong("month"));
  293. sqlList.add(
  294. DataContrlLog.createLog(this, "st_period", st_periodid, "反库存结账", "会计期间反库存结账成功").getSQL());
  295. }
  296. dbConnect.runSqlUpdate(sqlList);
  297. return getSucReturnObject().toString();
  298. }
  299. @API(title = "生成下一年会计期间", apiversion = R.ID2025072314214503.v1.class)
  300. @CACHEING_CLEAN( apiClass = {period.class})
  301. public String createnewyearperiod() throws YosException {
  302. ArrayList<String> sqlList = new ArrayList<>();
  303. Calendar calendar = Calendar.getInstance();
  304. int currentYear = calendar.get(Calendar.YEAR);
  305. int nextYear = currentYear + 1;
  306. for(int i=1;i<13;i++){
  307. YearMonth yearMonth = YearMonth.of(nextYear, i);
  308. LocalDate firstDayOfMonth = yearMonth.atDay(1);
  309. LocalDate lastDayOfMonth = yearMonth.atEndOfMonth();
  310. InsertSQL insertSQL = SQLFactory.createInsertSQL(this, "st_period");
  311. insertSQL.setUniqueid(createTableID("st_period"));
  312. insertSQL.setSiteid(siteid);
  313. insertSQL.setValue("month", i);
  314. insertSQL.setValue("year", nextYear);
  315. insertSQL.setValue("enddate",firstDayOfMonth);
  316. insertSQL.setValue("begdate", lastDayOfMonth);
  317. sqlList.add(insertSQL.getSQL());
  318. }
  319. dbConnect.runSqlUpdate(sqlList);
  320. return getSucReturnObject().toString();
  321. }
  322. }