ReceiptService.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  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.infrastructure.util.AuthUtils;
  14. import com.dk.mdm.mapper.mac.*;
  15. import com.dk.mdm.mapper.mst.CustomerMapper;
  16. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  17. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  18. import com.dk.mdm.mapper.mst.SaleChannelMapper;
  19. import com.dk.mdm.model.pojo.mac.*;
  20. import com.dk.common.service.BaseService;
  21. import com.dk.common.mapper.BaseMapper;
  22. import com.dk.mdm.model.pojo.mst.Customer;
  23. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  24. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  25. import com.dk.mdm.model.query.mac.RecPayHandleItemQuery;
  26. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  27. import com.dk.mdm.model.query.mac.RecPayQuery;
  28. import com.dk.mdm.model.query.mst.CustomerQuery;
  29. import com.dk.mdm.model.query.mst.SaleChannelQuery;
  30. import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
  31. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  32. import com.dk.mdm.model.response.mac.RecPayResponse;
  33. import com.dk.mdm.model.response.mst.CustomerResponse;
  34. import com.dk.mdm.model.response.mst.SaleChannelResponse;
  35. import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
  36. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  37. import com.dk.mdm.model.vo.mac.RecPayVO;
  38. import com.dk.mdm.service.common.CommonService;
  39. import com.dk.mdm.service.mst.CustomerService;
  40. import com.dk.mdm.service.mst.MoneyAccountService;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.beans.factory.annotation.Autowired;
  43. import org.springframework.transaction.annotation.Transactional;
  44. import java.math.BigDecimal;
  45. import java.util.*;
  46. @Service
  47. @Transactional
  48. public class ReceiptService extends BaseService<RecPay> {
  49. @Override
  50. public String getPrimaryKey() {
  51. return "rp_id";
  52. }
  53. @Override
  54. public BaseMapper<RecPay> getRepository() {
  55. return recPayMapper;
  56. }
  57. @Autowired
  58. private RecPayMapper recPayMapper;
  59. @Autowired
  60. private RecPayItemService recPayItemService;
  61. @Autowired
  62. private RecPayItemMapper recPayItemMapper;
  63. @Autowired
  64. private AccountService accountService;
  65. @Autowired
  66. private AccountMapper accountMapper;
  67. @Autowired
  68. private AccountItemService accountItemService;
  69. @Autowired
  70. private MoneyAccountMapper moneyAccountMapper;
  71. @Autowired
  72. private AccountItemMapper accountItemMapper;
  73. @Autowired
  74. private MoneyAccountService moneyAccountService;
  75. @Autowired
  76. private MoneyAccountItemMapper moneyAccountItemMapper;
  77. @Autowired
  78. private RecPayHandleItemService recPayHandleItemService;
  79. @Autowired
  80. private RecPayHandleItemMapper recPayHandleItemMapper;
  81. @Autowired
  82. private CommonService commonService;
  83. @Autowired
  84. private CustomerMapper customerMapper;
  85. @Autowired
  86. private SaleChannelMapper saleChannelMapper;
  87. @Autowired
  88. private CustomerService customerService;
  89. @Autowired
  90. private RecPayConvert recPayConvert;
  91. @Autowired
  92. private RecPayItemConvert recPayItemConvert;
  93. @Autowired
  94. private RecPayHandleItemConvert recPayHandleItemConvert;
  95. @Autowired
  96. private AuthUtils authUtils;
  97. /**
  98. * @desc : 条件查询
  99. * @author : 付斌
  100. * @date : 2023/1/9 10:40
  101. */
  102. @Pagination
  103. public ResponseResultVO<PageList<RecPayResponse>> selectByCond(RecPayQuery recPayQuery) {
  104. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCond(recPayQuery),
  105. recPayMapper.countByCond(recPayQuery));
  106. }
  107. /**
  108. * @desc : 条件查询-客户详情的收款列表
  109. * @author : 姜永辉
  110. * @date : 2023/1/9 10:40
  111. */
  112. @Pagination
  113. public ResponseResultVO<PageList<RecPayResponse>> selectByCondCus(RecPayQuery recPayQuery) {
  114. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCondCus(recPayQuery),
  115. recPayMapper.countByCondCus(recPayQuery));
  116. }
  117. /**
  118. * @desc : 查询收款明细
  119. * @author : 付斌
  120. * @date : 2024-02-28 13:25
  121. */
  122. @Pagination
  123. public ResponseResultVO selectById(String id) {
  124. Map<String, Object> result = new HashMap<>();
  125. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  126. result.put("data", recPayResponse);
  127. // 收款明细
  128. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  129. result.put("recPayItem", recPayItem);
  130. // 应收核销明细
  131. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  132. result.put("recPayHandleItem", recPayHandleItem);
  133. // 附件
  134. return ResponseResultUtil.success(result);
  135. }
  136. /**
  137. * @desc : 查询收款明细
  138. * @author : 付斌
  139. * @date : 2024-02-28 13:25
  140. */
  141. @Pagination
  142. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  143. Map<String, Object> result = new HashMap<>();
  144. // 收款明细
  145. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  146. result.put("recPayItem", recPayItem);
  147. // 应收核销明细
  148. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  149. result.put("recPayHandleItem", recPayHandleItem);
  150. // 附件
  151. return ResponseResultUtil.success(result);
  152. }
  153. /**
  154. * @desc : 新建客户收款
  155. * @author : 付斌
  156. * @date : 2023/1/9 10:49
  157. */
  158. @Transactional(
  159. rollbackFor = {Exception.class}
  160. )
  161. public ResponseResultVO<?> insertReceipt(RecPayVO recPayVO) {
  162. // 获取单号
  163. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  164. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  165. // 转化实体
  166. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  167. // 客户的新增和更新跟进人
  168. // 如果没有客户id,要新建
  169. if (recPay.getObjectId() == null) {
  170. List<CustomerResponse> listCustomer = customerMapper.selectByCond(new CustomerQuery().setCpId(recPay.getCpId()).setCusPhone(recPayVO.getCusPhone()));
  171. // 如果客户电话已存在
  172. if (listCustomer == null || listCustomer.size() == 0) {
  173. String channelId;
  174. List<SaleChannelResponse> saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery());
  175. channelId = (saleChannelResponses!=null && saleChannelResponses.size() > 0)?saleChannelResponses.get(0).getChannelId():"";
  176. // 创建客户,获取编码和主键UuId
  177. Map<String, Object> codeMapCus = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
  178. Customer customer = new Customer();
  179. List<String> followStaffs = new ArrayList<>();
  180. followStaffs.add(authUtils.getStaff().getStaffId());
  181. customer.setCusId(codeMapCus.get("outId").toString()).setCusCode(codeMapCus.get("outNote").toString()).setCusName(recPayVO.getCusName())
  182. .setCusPhone(recPayVO.getCusPhone()).setAddressArea(recPayVO.getAddressArea()).setAddressName(recPayVO.getAddressName())
  183. .setAddressNo(recPayVO.getAddressNo()).setAddressGcj02(recPayVO.getAddressGcj02()).setAddressFull(recPayVO.getAddressFull())
  184. .setChannelId(channelId).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId()).setFollowStaffs(followStaffs)
  185. .setReportStaff(recPayVO.getStaffId()).setSaleStatus(Constant.SaleStatus.QIANKE.getName()).setCpId(recPayVO.getCpId());
  186. customerMapper.insert(customer);
  187. recPayVO.setObjectId(customer.getCusId());
  188. recPay.setObjectId(customer.getCusId());
  189. } else{
  190. recPayVO.setObjectId(listCustomer.get(0).getCusId());
  191. recPay.setObjectId(listCustomer.get(0).getCusId());
  192. }
  193. }
  194. // 总单保存
  195. super.insert(recPay);
  196. // 明细保存
  197. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  198. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  199. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  200. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  201. recPayItemMapper.insert(recPayItem);
  202. // 插入账款明细
  203. AccountItem accountItem = new AccountItem();
  204. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  205. .setObjectId(recPayVO.getObjectId())
  206. .setOrgId(recPayVO.getOrgId())
  207. .setStaffId(recPayVO.getStaffId())
  208. .setAccDate(recPayVO.getAccDate())
  209. .setRecStatus(Constant.recStatuse.QUE_DING.getName())
  210. .setAmtRec(recPayItem.getAmtRec())
  211. .setBiznisType("t_mac_rec_pay_item")
  212. .setBiznisId(recPayItem.getRpId())
  213. .setBiznisNo(recPayVO.getRpNo())
  214. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  215. accountItemMapper.insert(accountItem);
  216. // 更新收款单上的账款明细Id
  217. RecPayItem recPayItemUpdate = new RecPayItem();
  218. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  219. recPayItemService.updateByUuid(recPayItemUpdate);
  220. // 插入资金流水
  221. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  222. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  223. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  224. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  225. moneyAccountItemMapper.insert(moneyAccountItem);
  226. // 更新资金账户
  227. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  228. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  229. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  230. .setMacId(moneyAccountForUpdate.getMacId());
  231. moneyAccountService.updateByUuid(moneyAccountUpdate);
  232. }
  233. }
  234. // 插入账款总表
  235. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  236. // 更新账款总表上收款的相关字段
  237. Account accountUpdate = new Account();
  238. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  239. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  240. .setObjectId(accountForUpdate.getObjectId());
  241. accountService.updateByUuid(accountUpdate);
  242. return ResponseResultUtil.success();
  243. }
  244. /**
  245. * @desc : 新建客户退款
  246. * @author : 付斌
  247. * @date : 2023/1/9 10:49
  248. */
  249. @Transactional(
  250. rollbackFor = {Exception.class}
  251. )
  252. public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
  253. // 查总账,看可退金额是否满足
  254. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  255. // if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
  256. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  257. // }
  258. // 获取单号
  259. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  260. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  261. // 转化实体
  262. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  263. // 总单保存
  264. super.insert(recPay);
  265. // 明细保存
  266. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  267. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  268. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  269. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  270. recPayItemMapper.insert(recPayItem);
  271. // 插入账款明细
  272. AccountItem accountItem = new AccountItem();
  273. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  274. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  275. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  276. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  277. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  278. accountItemMapper.insert(accountItem);
  279. // 更新收款单上的账款明细Id
  280. RecPayItem recPayItemUpdate = new RecPayItem();
  281. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  282. recPayItemService.updateByUuid(recPayItemUpdate);
  283. // 插入资金流水
  284. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  285. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  286. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  287. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  288. moneyAccountItemMapper.insert(moneyAccountItem);
  289. // 更新资金账户
  290. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  291. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  292. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  293. .setMacId(moneyAccountForUpdate.getMacId());
  294. // 如果账户不允许为负数
  295. if (!moneyAccountForUpdate.getFlgNegative()) {
  296. // 如果余额小于0 ,则提示余额不足
  297. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  298. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  299. }
  300. }
  301. moneyAccountService.updateByUuid(moneyAccountUpdate);
  302. }
  303. }
  304. // 更新账款总表上收款的相关字段
  305. Account accountUpdate = new Account();
  306. // 新建
  307. if(accountForUpdate == null ){
  308. // TODO 退货新客户插入的问题
  309. }else{
  310. // 说明已经存在
  311. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  312. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  313. .setObjectId(accountForUpdate.getObjectId());
  314. accountService.updateByUuid(accountUpdate);
  315. }
  316. return ResponseResultUtil.success();
  317. }
  318. /**
  319. * @desc : 编辑客户收款/退款
  320. * @author : 付斌
  321. * @date : 2023/1/9 10:49
  322. */
  323. @Transactional(
  324. rollbackFor = {Exception.class}
  325. )
  326. public ResponseResultVO<?> update(RecPayVO recPayVO) {
  327. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(recPayVO.getRpId());
  328. // 并发校验
  329. if (!recPayForUpdate.getFlgValid()) {
  330. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  331. }
  332. // if(recPayForUpdate.getBiznisId() != null){
  333. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  334. // }
  335. // 将之前的明细全部删除
  336. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  337. // 需要重新计算的资金账户
  338. List<String> macList = new ArrayList<>();
  339. for (RecPayItem recPayItem : recPayItemOriginalList) {
  340. // 删除收付款明细
  341. recPayItemMapper.deleteById(recPayItem.getItemId());
  342. // 删除账款明细
  343. accountItemMapper.deleteById(recPayItem.getAccItemId());
  344. // 删除账户流水
  345. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  346. if (!macList.contains(recPayItem.getMacId())) {
  347. macList.add(recPayItem.getMacId());
  348. }
  349. }
  350. // 新增明细
  351. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  352. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  353. recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  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. if (!macList.contains(recPayItem.getMacId())) {
  374. macList.add(recPayItem.getMacId());
  375. }
  376. }
  377. // 更新总账表的总收款额和可用额
  378. accountService.updateReceipt(recPayVO.getObjectId());
  379. // 更新账户余额
  380. for (String macId : macList) {
  381. accountService.updateMac(macId);
  382. }
  383. // 更新收款单总表
  384. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  385. return ResponseResultUtil.success(super.updateByUuid(recPay));
  386. }
  387. /**
  388. * @desc : 获取订单信息(编辑用)
  389. * @author : 付斌
  390. * @date : 2024-03-02 17:27
  391. */
  392. public ResponseResultVO<?> getRpForUpdate(String id) {
  393. Map<String, Object> dataInfo = new HashMap<>();
  394. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  395. dataInfo.put("data", recPayResponse);
  396. // 收款明细
  397. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  398. dataInfo.put("dataItem", recPayItemResponse);
  399. return ResponseResultUtil.success(dataInfo);
  400. }
  401. /**
  402. * @desc : 新建应收核销(收款+核销应收)
  403. * @author : 付斌
  404. * @date : 2023/1/9 10:49
  405. */
  406. @Transactional(
  407. rollbackFor = {Exception.class}
  408. )
  409. public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
  410. /********************* 收款的处理 begin **********************/
  411. // 获取单号
  412. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  413. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  414. // 转化实体
  415. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  416. // 总单保存
  417. super.insert(recPay);
  418. // 明细保存
  419. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  420. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  421. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  422. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  423. recPayItemMapper.insert(recPayItem);
  424. // 插入账款明细
  425. AccountItem accountItem = new AccountItem();
  426. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  427. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  428. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  429. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  430. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  431. accountItemMapper.insert(accountItem);
  432. // 更新收款单上的账款明细Id
  433. RecPayItem recPayItemUpdate = new RecPayItem();
  434. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  435. recPayItemService.updateByUuid(recPayItemUpdate);
  436. // 插入资金流水
  437. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  438. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  439. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  440. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  441. moneyAccountItemMapper.insert(moneyAccountItem);
  442. // 更新资金账户
  443. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  444. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  445. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  446. .setMacId(moneyAccountForUpdate.getMacId());
  447. // 如果账户不允许为负数
  448. if (!moneyAccountForUpdate.getFlgNegative()) {
  449. // 如果余额小于0 ,则提示余额不足
  450. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  451. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  452. }
  453. }
  454. moneyAccountService.updateByUuid(moneyAccountUpdate);
  455. }
  456. }
  457. // 插入账款总表
  458. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  459. // 更新账款总表上收款的相关字段
  460. Account accountUpdate = new Account();
  461. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  462. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  463. .setObjectId(accountForUpdate.getObjectId());
  464. accountService.updateByUuid(accountUpdate);
  465. /********************* 收款的处理 end **********************/
  466. /********************* 应收收款的处理 begin **********************/
  467. // 应收收款的处理
  468. if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
  469. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
  470. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  471. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  472. recPayHandleItemMapper.insert(recPayHandleItem);
  473. // 账款明细的核销金额和优惠金额
  474. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  475. AccountItem accountItemUpdate = new AccountItem();
  476. // 核销金额,超出剩余应收金额
  477. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  478. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  479. }
  480. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  481. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  482. .setItemId(recPayHandleItem.getAccItemId());
  483. // 剩余金额 = 应收金额-应收收款金额
  484. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  485. accountItemService.updateByUuid(accountItemUpdate);
  486. }
  487. // 计算明细的核销金额,优惠金额合计
  488. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
  489. RecPayHandleItemVO item = new RecPayHandleItemVO();
  490. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  491. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  492. return item;
  493. }).get();
  494. // 更新总账上
  495. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  496. accountUpdate = new Account();
  497. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
  498. .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
  499. .setObjectId(accountForUpdate.getObjectId());
  500. // 剩余应收 = 总应收账款-总应收收款金额
  501. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  502. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  503. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  504. // 更新前的最后校验
  505. // 剩余应收为负数,则不能保存
  506. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  507. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  508. // }
  509. // 可用金额为负数,则不能保存
  510. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  511. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  512. }
  513. accountService.updateByUuid(accountUpdate);
  514. }
  515. /********************* 应收收款的处理 end **********************/
  516. return ResponseResultUtil.success();
  517. }
  518. /**
  519. * @desc : 更新应收核销(小编辑)
  520. * @author : 付斌
  521. * @date : 2023/1/9 10:49
  522. */
  523. @Transactional(
  524. rollbackFor = {Exception.class}
  525. )
  526. public ResponseResultVO<?> updateReceivableReceipt(RecPayVO recPayVO) {
  527. // 先只改备注和附件
  528. RecPay recPay = new RecPay();
  529. recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
  530. super.updateByUuid(recPay);
  531. return ResponseResultUtil.success();
  532. }
  533. /**
  534. * @desc : 作废
  535. * @author : 付斌
  536. * @date : 2024-03-08 16:38
  537. */
  538. public ResponseResultVO<?> invalid(String id) {
  539. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  540. // 并发校验
  541. if (!recPayForUpdate.getFlgValid()) {
  542. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  543. }
  544. // 240624 问完付斌后
  545. // if(recPayForUpdate.getBiznisId() != null){
  546. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  547. // }
  548. // 如果所在月份已结账,则不能作废 todo
  549. // 查出并锁定所有应收核销明细
  550. AccountItem accountItemForUpdate;
  551. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  552. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  553. // 更新账款明细应收收款
  554. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  555. AccountItem accountItemUpdate = new AccountItem();
  556. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  557. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  558. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  559. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  560. accountItemService.updateByUuid(accountItemUpdate);
  561. // 将核销明细有效标识置为false
  562. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  563. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  564. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  565. }
  566. // 把总帐上的钱加回来
  567. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  568. Account accountUpdate = new Account();
  569. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  570. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  571. .setObjectId(accountForUpdate.getObjectId());
  572. // 剩余应收 = 总应收账款-总应收收款金额
  573. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  574. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  575. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  576. // 更新前的最后校验
  577. // 剩余应收为负数,则不能保存
  578. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  579. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  580. // }
  581. // 可用金额为负数,则不能保存
  582. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  583. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  584. }
  585. accountService.updateByUuid(accountUpdate);
  586. // 将之前的明细全部删除
  587. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  588. // 需要重新计算的资金账户
  589. List<String> macList = new ArrayList<>();
  590. for (RecPayItem recPayItem : recPayItemOriginalList) {
  591. // 删除收付款明细
  592. // recPayItemMapper.deleteById(recPayItem.getItemId());
  593. // 改为更新false
  594. RecPayItem recPayItemNew = new RecPayItem();
  595. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  596. recPayItemMapper.updateById(recPayItemNew);
  597. // 删除账款明细
  598. accountItemMapper.deleteById(recPayItem.getAccItemId());
  599. // 删除账户流水
  600. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  601. if (!macList.contains(recPayItem.getMacId())) {
  602. macList.add(recPayItem.getMacId());
  603. }
  604. }
  605. // 更新总账表的总收款额和可用额
  606. accountService.updateReceipt(recPayForUpdate.getObjectId());
  607. // 更新账户余额
  608. for (String macId : macList) {
  609. accountService.updateMac(macId);
  610. }
  611. // 作废
  612. RecPay recPayUpdate = new RecPay();
  613. recPayUpdate.setFlgValid(false).setRpId(id);
  614. super.updateByUuid(recPayUpdate);
  615. return ResponseResultUtil.success();
  616. }
  617. /**
  618. * @desc : 获取应收收款(编辑用)
  619. * @author : 付斌
  620. * @date : 2024-02-28 13:25
  621. */
  622. @Pagination
  623. public ResponseResultVO getReceivableReceiptForUpdate(String id) {
  624. Map<String, Object> result = new HashMap<>();
  625. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  626. result.put("data", recPayResponse);
  627. // 收款明细
  628. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  629. result.put("dataItem", recPayItem);
  630. // 应收核销明细
  631. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  632. result.put("recPayHandleItem", recPayHandleItem);
  633. // 附件
  634. return ResponseResultUtil.success(result);
  635. }
  636. }