ReceiptService.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568
  1. package com.dk.mdm.service.mac;
  2. import com.dk.common.exception.BaseBusinessException;
  3. import com.dk.common.infrastructure.annotaiton.Pagination;
  4. import com.dk.common.infrastructure.constant.Constant;
  5. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  6. import com.dk.common.model.pojo.PageList;
  7. import com.dk.common.response.ResponseCodeEnum;
  8. import com.dk.common.response.ResponseResultUtil;
  9. import com.dk.common.response.ResponseResultVO;
  10. import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
  11. import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
  12. import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
  13. import com.dk.mdm.mapper.mac.*;
  14. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  15. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  16. import com.dk.mdm.model.pojo.mac.*;
  17. import com.dk.common.service.BaseService;
  18. import com.dk.common.mapper.BaseMapper;
  19. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  20. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  21. import com.dk.mdm.model.query.mac.RecPayHandleItemQuery;
  22. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  23. import com.dk.mdm.model.query.mac.RecPayQuery;
  24. import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
  25. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  26. import com.dk.mdm.model.response.mac.RecPayResponse;
  27. import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
  28. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  29. import com.dk.mdm.model.vo.mac.RecPayVO;
  30. import com.dk.mdm.service.common.CommonService;
  31. import com.dk.mdm.service.mst.MoneyAccountService;
  32. import org.springframework.stereotype.Service;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.math.BigDecimal;
  36. import java.util.*;
  37. @Service
  38. @Transactional
  39. public class ReceiptService extends BaseService<RecPay> {
  40. @Override
  41. public String getPrimaryKey() {
  42. return "rp_id";
  43. }
  44. @Override
  45. public BaseMapper<RecPay> getRepository() {
  46. return recPayMapper;
  47. }
  48. @Autowired
  49. private RecPayMapper recPayMapper;
  50. @Autowired
  51. private RecPayItemService recPayItemService;
  52. @Autowired
  53. private RecPayItemMapper recPayItemMapper;
  54. @Autowired
  55. private AccountService accountService;
  56. @Autowired
  57. private AccountMapper accountMapper;
  58. @Autowired
  59. private AccountItemService accountItemService;
  60. @Autowired
  61. private MoneyAccountMapper moneyAccountMapper;
  62. @Autowired
  63. private AccountItemMapper accountItemMapper;
  64. @Autowired
  65. private MoneyAccountService moneyAccountService;
  66. @Autowired
  67. private MoneyAccountItemMapper moneyAccountItemMapper;
  68. @Autowired
  69. private RecPayHandleItemService recPayHandleItemService;
  70. @Autowired
  71. private RecPayHandleItemMapper recPayHandleItemMapper;
  72. @Autowired
  73. private CommonService commonService;
  74. @Autowired
  75. private RecPayConvert recPayConvert;
  76. @Autowired
  77. private RecPayItemConvert recPayItemConvert;
  78. @Autowired
  79. private RecPayHandleItemConvert recPayHandleItemConvert;
  80. /**
  81. * @desc : 条件查询
  82. * @author : 付斌
  83. * @date : 2023/1/9 10:40
  84. */
  85. @Pagination
  86. public ResponseResultVO<PageList<RecPayResponse>> selectByCond(RecPayQuery recPayQuery) {
  87. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCond(recPayQuery),
  88. recPayMapper.countByCond(recPayQuery));
  89. }
  90. /**
  91. * @desc : 查询收款明细
  92. * @author : 付斌
  93. * @date : 2024-02-28 13:25
  94. */
  95. @Pagination
  96. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  97. Map<String, Object> result = new HashMap<>();
  98. // 收款明细
  99. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  100. result.put("recPayItem", recPayItem);
  101. // 应收核销明细
  102. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  103. result.put("recPayHandleItem", recPayHandleItem);
  104. // 附件
  105. return ResponseResultUtil.success(result);
  106. }
  107. /**
  108. * @desc : 新建客户收款
  109. * @author : 付斌
  110. * @date : 2023/1/9 10:49
  111. */
  112. @Transactional(
  113. rollbackFor = {Exception.class}
  114. )
  115. public ResponseResultVO<?> insertReceipt(RecPayVO recPayVO) {
  116. // 获取单号
  117. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  118. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  119. // 转化实体
  120. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  121. // 总单保存
  122. super.insert(recPay);
  123. // 明细保存
  124. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  125. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  126. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  127. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  128. recPayItemMapper.insert(recPayItem);
  129. // 插入账款明细
  130. AccountItem accountItem = new AccountItem();
  131. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  132. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  133. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  134. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  135. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  136. accountItemMapper.insert(accountItem);
  137. // 更新收款单上的账款明细Id
  138. RecPayItem recPayItemUpdate = new RecPayItem();
  139. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  140. recPayItemService.updateByUuid(recPayItemUpdate);
  141. // 插入资金流水
  142. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  143. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  144. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  145. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  146. moneyAccountItemMapper.insert(moneyAccountItem);
  147. // 更新资金账户
  148. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  149. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  150. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  151. .setMacId(moneyAccountForUpdate.getMacId());
  152. moneyAccountService.updateByUuid(moneyAccountUpdate);
  153. }
  154. }
  155. // 插入账款总表
  156. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  157. // 更新账款总表上收款的相关字段
  158. Account accountUpdate = new Account();
  159. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  160. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  161. .setObjectId(accountForUpdate.getObjectId());
  162. accountService.updateByUuid(accountUpdate);
  163. return ResponseResultUtil.success();
  164. }
  165. /**
  166. * @desc : 新建客户退款
  167. * @author : 付斌
  168. * @date : 2023/1/9 10:49
  169. */
  170. @Transactional(
  171. rollbackFor = {Exception.class}
  172. )
  173. public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
  174. // 查总账,看可退金额是否满足
  175. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  176. if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
  177. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  178. }
  179. // 获取单号
  180. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  181. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  182. // 转化实体
  183. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  184. // 总单保存
  185. super.insert(recPay);
  186. // 明细保存
  187. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  188. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  189. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  190. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  191. recPayItemMapper.insert(recPayItem);
  192. // 插入账款明细
  193. AccountItem accountItem = new AccountItem();
  194. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  195. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  196. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  197. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  198. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  199. accountItemMapper.insert(accountItem);
  200. // 更新收款单上的账款明细Id
  201. RecPayItem recPayItemUpdate = new RecPayItem();
  202. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  203. recPayItemService.updateByUuid(recPayItemUpdate);
  204. // 插入资金流水
  205. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  206. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  207. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  208. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  209. moneyAccountItemMapper.insert(moneyAccountItem);
  210. // 更新资金账户
  211. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  212. // 如果转账之后账户余额小于0,则提示资金账户余额不足,不能退款
  213. if ((moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec())).compareTo(BigDecimal.ZERO) == -1) {
  214. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  215. }
  216. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  217. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  218. .setMacId(moneyAccountForUpdate.getMacId());
  219. moneyAccountService.updateByUuid(moneyAccountUpdate);
  220. }
  221. }
  222. // 更新账款总表上收款的相关字段
  223. Account accountUpdate = new Account();
  224. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  225. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  226. .setObjectId(accountForUpdate.getObjectId());
  227. accountService.updateByUuid(accountUpdate);
  228. return ResponseResultUtil.success();
  229. }
  230. /**
  231. * @desc : 编辑客户收款/退款
  232. * @author : 付斌
  233. * @date : 2023/1/9 10:49
  234. */
  235. @Transactional(
  236. rollbackFor = {Exception.class}
  237. )
  238. public ResponseResultVO<?> update(RecPayVO recPayVO) {
  239. // 将之前的明细全部删除
  240. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  241. // 需要重新计算的资金账户
  242. List<String> macList = new ArrayList<>();
  243. for (RecPayItem recPayItem : recPayItemOriginalList) {
  244. // 删除收付款明细
  245. recPayItemMapper.deleteById(recPayItem.getItemId());
  246. // 删除账款明细
  247. accountItemMapper.deleteById(recPayItem.getAccItemId());
  248. // 删除账户流水
  249. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  250. if (!macList.contains(recPayItem.getMacId())) {
  251. macList.add(recPayItem.getMacId());
  252. }
  253. }
  254. // 新增明细
  255. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  256. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  257. recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  258. recPayItemMapper.insert(recPayItem);
  259. // 插入账款明细
  260. AccountItem accountItem = new AccountItem();
  261. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  262. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  263. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  264. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  265. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  266. accountItemMapper.insert(accountItem);
  267. // 更新收款单上的账款明细Id
  268. RecPayItem recPayItemUpdate = new RecPayItem();
  269. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  270. recPayItemService.updateByUuid(recPayItemUpdate);
  271. // 插入资金流水
  272. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  273. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  274. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  275. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  276. moneyAccountItemMapper.insert(moneyAccountItem);
  277. if (!macList.contains(recPayItem.getMacId())) {
  278. macList.add(recPayItem.getMacId());
  279. }
  280. }
  281. // 更新总账表的总收款额和可用额
  282. accountService.updateReceipt(recPayVO.getObjectId());
  283. // 更新账户余额
  284. for (String macId : macList) {
  285. accountService.updateMac(macId);
  286. }
  287. // 更新收款单总表
  288. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  289. return ResponseResultUtil.success(super.updateByUuid(recPay));
  290. }
  291. /**
  292. * @desc : 获取订单信息(编辑用)
  293. * @author : 付斌
  294. * @date : 2024-03-02 17:27
  295. */
  296. public ResponseResultVO<?> getRpForUpdate(String id) {
  297. Map<String, Object> dataInfo = new HashMap<>();
  298. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  299. dataInfo.put("data", recPayResponse);
  300. // 收款明细
  301. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  302. dataInfo.put("dataItem", recPayItemResponse);
  303. return ResponseResultUtil.success(dataInfo);
  304. }
  305. /**
  306. * @desc : 新建应收核销(收款+核销应收)
  307. * @author : 付斌
  308. * @date : 2023/1/9 10:49
  309. */
  310. @Transactional(
  311. rollbackFor = {Exception.class}
  312. )
  313. public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
  314. /********************* 收款的处理 begin **********************/
  315. // 获取单号
  316. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  317. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  318. // 转化实体
  319. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  320. // 总单保存
  321. super.insert(recPay);
  322. // 明细保存
  323. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  324. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  325. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  326. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  327. recPayItemMapper.insert(recPayItem);
  328. // 插入账款明细
  329. AccountItem accountItem = new AccountItem();
  330. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  331. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  332. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  333. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  334. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  335. accountItemMapper.insert(accountItem);
  336. // 更新收款单上的账款明细Id
  337. RecPayItem recPayItemUpdate = new RecPayItem();
  338. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  339. recPayItemService.updateByUuid(recPayItemUpdate);
  340. // 插入资金流水
  341. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  342. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  343. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  344. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  345. moneyAccountItemMapper.insert(moneyAccountItem);
  346. // 更新资金账户
  347. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  348. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  349. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  350. .setMacId(moneyAccountForUpdate.getMacId());
  351. moneyAccountService.updateByUuid(moneyAccountUpdate);
  352. }
  353. }
  354. // 插入账款总表
  355. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  356. // 更新账款总表上收款的相关字段
  357. Account accountUpdate = new Account();
  358. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  359. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  360. .setObjectId(accountForUpdate.getObjectId());
  361. accountService.updateByUuid(accountUpdate);
  362. /********************* 收款的处理 end **********************/
  363. /********************* 应收收款的处理 begin **********************/
  364. // 应收收款的处理
  365. if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
  366. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
  367. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  368. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  369. recPayHandleItemMapper.insert(recPayHandleItem);
  370. // 账款明细的核销金额和优惠金额
  371. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  372. AccountItem accountItemUpdate = new AccountItem();
  373. // 核销金额,超出剩余应收金额
  374. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  375. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  376. }
  377. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  378. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  379. .setItemId(recPayHandleItem.getAccItemId());
  380. // 剩余金额 = 应收金额-应收收款金额
  381. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  382. accountItemService.updateByUuid(accountItemUpdate);
  383. }
  384. // 计算明细的核销金额,优惠金额合计
  385. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
  386. RecPayHandleItemVO item = new RecPayHandleItemVO();
  387. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  388. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  389. return item;
  390. }).get();
  391. // 更新总账上
  392. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  393. accountUpdate = new Account();
  394. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
  395. .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
  396. .setObjectId(accountForUpdate.getObjectId());
  397. // 剩余应收 = 总应收账款-总应收收款金额
  398. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  399. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  400. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  401. // 更新前的最后校验
  402. // 剩余应收为负数,则不能保存
  403. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  404. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  405. }
  406. // 可用金额为负数,则不能保存
  407. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  408. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  409. }
  410. accountService.updateByUuid(accountUpdate);
  411. }
  412. /********************* 应收收款的处理 end **********************/
  413. return ResponseResultUtil.success();
  414. }
  415. /**
  416. * @desc : 作废
  417. * @author : 付斌
  418. * @date : 2024-03-08 16:38
  419. */
  420. public ResponseResultVO<?> invalid(String id) {
  421. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  422. // 并发校验
  423. if (!recPayForUpdate.getFlgValid()) {
  424. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  425. }
  426. // 如果所在月份已结账,则不能作废 todo
  427. // 查出并锁定所有应收核销明细
  428. AccountItem accountItemForUpdate;
  429. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  430. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  431. // 更新账款明细应收收款
  432. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  433. AccountItem accountItemUpdate = new AccountItem();
  434. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  435. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  436. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  437. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  438. accountItemService.updateByUuid(accountItemUpdate);
  439. // 将核销明细有效标识置为false
  440. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  441. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  442. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  443. }
  444. // 把总帐上的钱加回来
  445. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  446. Account accountUpdate = new Account();
  447. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  448. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  449. .setObjectId(accountForUpdate.getObjectId());
  450. // 剩余应收 = 总应收账款-总应收收款金额
  451. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  452. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  453. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  454. // 更新前的最后校验
  455. // 剩余应收为负数,则不能保存
  456. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  457. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  458. }
  459. // 可用金额为负数,则不能保存
  460. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  461. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  462. }
  463. accountService.updateByUuid(accountUpdate);
  464. // 将之前的明细全部删除
  465. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  466. // 需要重新计算的资金账户
  467. List<String> macList = new ArrayList<>();
  468. for (RecPayItem recPayItem : recPayItemOriginalList) {
  469. // 删除收付款明细
  470. recPayItemMapper.deleteById(recPayItem.getItemId());
  471. // 删除账款明细
  472. accountItemMapper.deleteById(recPayItem.getAccItemId());
  473. // 删除账户流水
  474. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  475. if (!macList.contains(recPayItem.getMacId())) {
  476. macList.add(recPayItem.getMacId());
  477. }
  478. }
  479. // 更新总账表的总收款额和可用额
  480. accountService.updateReceipt(recPayForUpdate.getObjectId());
  481. // 更新账户余额
  482. for (String macId : macList) {
  483. accountService.updateMac(macId);
  484. }
  485. // 作废
  486. RecPay recPayUpdate = new RecPay();
  487. recPayUpdate.setFlgValid(false).setRpId(id);
  488. super.updateByUuid(recPayUpdate);
  489. return ResponseResultUtil.success();
  490. }
  491. }