ReceiptService.java 30 KB

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