PaymentService.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  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.mapper.BaseMapper;
  7. import com.dk.common.model.pojo.PageList;
  8. import com.dk.common.response.ResponseCodeEnum;
  9. import com.dk.common.response.ResponseResultUtil;
  10. import com.dk.common.response.ResponseResultVO;
  11. import com.dk.common.service.BaseService;
  12. import com.dk.mdm.infrastructure.convert.mac.RecPayConvert;
  13. import com.dk.mdm.infrastructure.convert.mac.RecPayHandleItemConvert;
  14. import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
  15. import com.dk.mdm.mapper.mac.*;
  16. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  17. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  18. import com.dk.mdm.model.pojo.mac.*;
  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.beans.factory.annotation.Autowired;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.transaction.annotation.Transactional;
  35. import java.math.BigDecimal;
  36. import java.util.ArrayList;
  37. import java.util.HashMap;
  38. import java.util.List;
  39. import java.util.Map;
  40. @Service
  41. @Transactional
  42. public class PaymentService extends BaseService<RecPay> {
  43. @Override
  44. public String getPrimaryKey() {
  45. return "rp_id";
  46. }
  47. @Override
  48. public BaseMapper<RecPay> getRepository() {
  49. return recPayMapper;
  50. }
  51. @Autowired
  52. private RecPayMapper recPayMapper;
  53. @Autowired
  54. private RecPayItemService recPayItemService;
  55. @Autowired
  56. private RecPayItemMapper recPayItemMapper;
  57. @Autowired
  58. private AccountService accountService;
  59. @Autowired
  60. private AccountMapper accountMapper;
  61. @Autowired
  62. private AccountItemService accountItemService;
  63. @Autowired
  64. private MoneyAccountMapper moneyAccountMapper;
  65. @Autowired
  66. private AccountItemMapper accountItemMapper;
  67. @Autowired
  68. private MoneyAccountService moneyAccountService;
  69. @Autowired
  70. private MoneyAccountItemMapper moneyAccountItemMapper;
  71. @Autowired
  72. private RecPayHandleItemService recPayHandleItemService;
  73. @Autowired
  74. private RecPayHandleItemMapper recPayHandleItemMapper;
  75. @Autowired
  76. private CommonService commonService;
  77. @Autowired
  78. private RecPayConvert recPayConvert;
  79. @Autowired
  80. private RecPayItemConvert recPayItemConvert;
  81. @Autowired
  82. private RecPayHandleItemConvert recPayHandleItemConvert;
  83. /**
  84. * @desc : 条件查询
  85. * @author : 付斌
  86. * @date : 2023/1/9 10:40
  87. */
  88. @Pagination
  89. public ResponseResultVO<PageList<RecPayResponse>> selectPaymentByCond(RecPayQuery recPayQuery) {
  90. return super.mergeListWithCount(recPayQuery, recPayMapper.selectPaymentByCond(recPayQuery),
  91. recPayMapper.countPaymentByCond(recPayQuery));
  92. }
  93. /**
  94. * @desc : 查询收款明细
  95. * @author : 付斌
  96. * @date : 2024-02-28 13:25
  97. */
  98. @Pagination
  99. public ResponseResultVO selectById(String id) {
  100. Map<String, Object> result = new HashMap<>();
  101. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  102. result.put("data", recPayResponse);
  103. // 付款明细
  104. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  105. result.put("recPayItem", recPayItem);
  106. // 应付核销明细
  107. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  108. result.put("recPayHandleItem", recPayHandleItem);
  109. // 附件
  110. return ResponseResultUtil.success(result);
  111. }
  112. /**
  113. * @desc : 查询付款明细
  114. * @author : 付斌
  115. * @date : 2024-02-28 13:25
  116. */
  117. @Pagination
  118. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  119. Map<String, Object> result = new HashMap<>();
  120. // 付款明细
  121. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  122. result.put("recPayItem", recPayItem);
  123. // 应付核销明细
  124. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  125. result.put("recPayHandleItem", recPayHandleItem);
  126. // 附件
  127. return ResponseResultUtil.success(result);
  128. }
  129. /**
  130. * @desc : 新建应付核销(付款+核销应付)
  131. * @author : 付斌
  132. * @date : 2023/1/9 10:49
  133. */
  134. @Transactional(
  135. rollbackFor = {Exception.class}
  136. )
  137. public ResponseResultVO<?> insertPayablePayment(RecPayVO recPayVO) {
  138. /********************* 付款的处理 begin **********************/
  139. // 获取单号
  140. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  141. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  142. // 转化实体
  143. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  144. // 总单保存
  145. super.insert(recPay);
  146. // 明细保存
  147. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  148. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  149. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  150. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  151. recPayItemMapper.insert(recPayItem);
  152. // 插入账款明细
  153. AccountItem accountItem = new AccountItem();
  154. accountItem.setAccItemType(Constant.accItemType.FU_KUAN.getName())
  155. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  156. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
  157. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  158. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  159. accountItemMapper.insert(accountItem);
  160. // 更新付款单上的账款明细Id
  161. RecPayItem recPayItemUpdate = new RecPayItem();
  162. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  163. recPayItemService.updateByUuid(recPayItemUpdate);
  164. // 插入资金流水
  165. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  166. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  167. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
  168. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  169. moneyAccountItemMapper.insert(moneyAccountItem);
  170. // 更新资金账户
  171. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  172. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  173. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay().negate()))
  174. .setMacId(moneyAccountForUpdate.getMacId());
  175. moneyAccountService.updateByUuid(moneyAccountUpdate);
  176. }
  177. }
  178. // 插入账款总表
  179. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  180. // 更新账款总表上付款的相关字段
  181. Account accountUpdate = new Account();
  182. accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总付款金额
  183. .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
  184. .setObjectId(accountForUpdate.getObjectId());
  185. accountService.updateByUuid(accountUpdate);
  186. /********************* 付款的处理 end **********************/
  187. /********************* 应付付款的处理 begin **********************/
  188. // 应付付款的处理
  189. if (recPayVO.getPayableList() != null && recPayVO.getPayableList().size() > 0) {
  190. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getPayableList()) {
  191. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  192. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  193. recPayHandleItemMapper.insert(recPayHandleItem);
  194. // 账款明细的核销金额和优惠金额
  195. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  196. AccountItem accountItemUpdate = new AccountItem();
  197. // 核销金额,超出剩余应付金额
  198. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtPayableHandle()) == -1) {
  199. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  200. }
  201. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtPayableHandle()))
  202. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  203. .setItemId(recPayHandleItem.getAccItemId());
  204. // 剩余金额 = 应付金额-应付付款金额
  205. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  206. accountItemService.updateByUuid(accountItemUpdate);
  207. }
  208. // 计算明细的核销金额,优惠金额合计
  209. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getPayableList().stream().reduce((x, y) -> {
  210. RecPayHandleItemVO item = new RecPayHandleItemVO();
  211. item.setAmtPayableHandle(x.getAmtPayableHandle().add(y.getAmtPayableHandle()));
  212. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  213. return item;
  214. }).get();
  215. // 更新总账上
  216. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  217. accountUpdate = new Account();
  218. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().add(recPayHandleItemVO.getAmtPayableHandle()))// 总应付付款金额
  219. .setPayableWaive(accountForUpdate.getPayableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应付优惠金额
  220. .setObjectId(accountForUpdate.getObjectId());
  221. // 剩余应付 = 总应付账款-总应付付款金额
  222. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  223. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  224. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  225. // 更新前的最后校验
  226. // 剩余应付为负数,则不能保存
  227. // if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  228. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  229. // }
  230. // 可用金额为负数,则不能保存
  231. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  232. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  233. }
  234. accountService.updateByUuid(accountUpdate);
  235. }
  236. /********************* 应付付款的处理 end **********************/
  237. return ResponseResultUtil.success();
  238. }
  239. /**
  240. * @desc : 更新应收核销(小编辑)
  241. * @author : 付斌
  242. * @date : 2023/1/9 10:49
  243. */
  244. @Transactional(
  245. rollbackFor = {Exception.class}
  246. )
  247. public ResponseResultVO<?> updatePayablePayment(RecPayVO recPayVO) {
  248. // 先只改备注和附件
  249. RecPay recPay = new RecPay();
  250. recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
  251. super.updateByUuid(recPay);
  252. return ResponseResultUtil.success();
  253. }
  254. /**
  255. * @desc : 作废
  256. * @author : 付斌
  257. * @date : 2024-03-08 16:38
  258. */
  259. public ResponseResultVO<?> invalid(String id) {
  260. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  261. // 并发校验
  262. if (!recPayForUpdate.getFlgValid()) {
  263. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  264. }
  265. if(recPayForUpdate.getBiznisId() != null){
  266. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  267. }
  268. // 如果所在月份已结账,则不能作废 todo
  269. // 查出并锁定所有应付核销明细
  270. AccountItem accountItemForUpdate;
  271. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  272. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  273. // 更新账款明细应付付款
  274. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  275. AccountItem accountItemUpdate = new AccountItem();
  276. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtPayableHandle()))
  277. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  278. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  279. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  280. accountItemService.updateByUuid(accountItemUpdate);
  281. // 将核销明细有效标识置为false
  282. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  283. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  284. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  285. }
  286. // 把总帐上的钱加回来
  287. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  288. Account accountUpdate = new Account();
  289. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().subtract(recPayForUpdate.getSumAmtPayableHandle()))// 总应付付款金额
  290. .setPayableWaive(accountForUpdate.getPayableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应付优惠金额
  291. .setObjectId(accountForUpdate.getObjectId());
  292. // 剩余应付 = 总应付账款-总应付付款金额
  293. accountUpdate.setPayableResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  294. // 可退金额 = 总付款金额-总应付付款金额+总应付优惠金额
  295. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  296. // 更新前的最后校验
  297. // 剩余应付为负数,则不能保存
  298. if (accountUpdate.getPayableResidue().compareTo(BigDecimal.ZERO) == -1) {
  299. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  300. }
  301. // 可用金额为负数,则不能保存
  302. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  303. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  304. }
  305. accountService.updateByUuid(accountUpdate);
  306. // 将之前的明细全部删除
  307. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  308. // 需要重新计算的资金账户
  309. List<String> macList = new ArrayList<>();
  310. for (RecPayItem recPayItem : recPayItemOriginalList) {
  311. // 删除收付款明细
  312. recPayItemMapper.deleteById(recPayItem.getItemId());
  313. // 删除账款明细
  314. accountItemMapper.deleteById(recPayItem.getAccItemId());
  315. // 删除账户流水
  316. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  317. if (!macList.contains(recPayItem.getMacId())) {
  318. macList.add(recPayItem.getMacId());
  319. }
  320. }
  321. // 更新总账表的总付款额和可用额
  322. accountService.updatePayment(recPayForUpdate.getObjectId());
  323. // 更新账户余额
  324. for (String macId : macList) {
  325. accountService.updateMac(macId);
  326. }
  327. // 作废
  328. RecPay recPayUpdate = new RecPay();
  329. recPayUpdate.setFlgValid(false).setRpId(id);
  330. super.updateByUuid(recPayUpdate);
  331. return ResponseResultUtil.success();
  332. }
  333. /**
  334. * @desc : 获取应付付款(编辑用)
  335. * @author : 付斌
  336. * @date : 2024-02-28 13:25
  337. */
  338. @Pagination
  339. public ResponseResultVO getPayablePaymentForUpdate(String id) {
  340. Map<String, Object> result = new HashMap<>();
  341. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  342. result.put("data", recPayResponse);
  343. // 付款明细
  344. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  345. result.put("dataItem", recPayItem);
  346. // 应付核销明细
  347. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  348. result.put("recPayHandleItem", recPayHandleItem);
  349. // 附件
  350. return ResponseResultUtil.success(result);
  351. }
  352. }