ReceiptService.java 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735
  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. // 改为更新false
  343. RecPayItem recPayItemNew = new RecPayItem();
  344. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  345. recPayItemService.updateByUuid(recPayItemNew);
  346. // 删除账款明细
  347. accountItemMapper.deleteById(recPayItem.getAccItemId());
  348. // 删除账户流水
  349. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  350. if (!macList.contains(recPayItem.getMacId())) {
  351. macList.add(recPayItem.getMacId());
  352. }
  353. }
  354. // 新增明细
  355. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  356. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  357. recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  358. recPayItemMapper.insert(recPayItem);
  359. // 插入账款明细
  360. AccountItem accountItem = new AccountItem();
  361. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  362. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  363. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  364. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  365. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  366. accountItemMapper.insert(accountItem);
  367. // 更新收款单上的账款明细Id
  368. RecPayItem recPayItemUpdate = new RecPayItem();
  369. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  370. recPayItemService.updateByUuid(recPayItemUpdate);
  371. // 插入资金流水
  372. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  373. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  374. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  375. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  376. moneyAccountItemMapper.insert(moneyAccountItem);
  377. if (!macList.contains(recPayItem.getMacId())) {
  378. macList.add(recPayItem.getMacId());
  379. }
  380. }
  381. // 更新总账表的总收款额和可用额
  382. accountService.updateReceipt(recPayVO.getObjectId());
  383. // 更新账户余额
  384. for (String macId : macList) {
  385. accountService.updateMac(macId);
  386. }
  387. // 更新收款单总表
  388. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  389. return ResponseResultUtil.success(super.updateByUuid(recPay));
  390. }
  391. /**
  392. * @desc : 获取订单信息(编辑用)
  393. * @author : 付斌
  394. * @date : 2024-03-02 17:27
  395. */
  396. public ResponseResultVO<?> getRpForUpdate(String id) {
  397. Map<String, Object> dataInfo = new HashMap<>();
  398. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  399. dataInfo.put("data", recPayResponse);
  400. // 收款明细
  401. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  402. dataInfo.put("dataItem", recPayItemResponse);
  403. return ResponseResultUtil.success(dataInfo);
  404. }
  405. /**
  406. * @desc : 新建应收核销(收款+核销应收)
  407. * @author : 付斌
  408. * @date : 2023/1/9 10:49
  409. */
  410. @Transactional(
  411. rollbackFor = {Exception.class}
  412. )
  413. public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
  414. /********************* 收款的处理 begin **********************/
  415. // 获取单号
  416. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  417. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  418. // 转化实体
  419. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  420. // 总单保存
  421. super.insert(recPay);
  422. // 明细保存
  423. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  424. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  425. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  426. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  427. recPayItemMapper.insert(recPayItem);
  428. // 插入账款明细
  429. AccountItem accountItem = new AccountItem();
  430. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  431. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  432. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  433. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  434. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  435. accountItemMapper.insert(accountItem);
  436. // 更新收款单上的账款明细Id
  437. RecPayItem recPayItemUpdate = new RecPayItem();
  438. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  439. recPayItemService.updateByUuid(recPayItemUpdate);
  440. // 插入资金流水
  441. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  442. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  443. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  444. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  445. moneyAccountItemMapper.insert(moneyAccountItem);
  446. // 更新资金账户
  447. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  448. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  449. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  450. .setMacId(moneyAccountForUpdate.getMacId());
  451. // 如果账户不允许为负数
  452. if (!moneyAccountForUpdate.getFlgNegative()) {
  453. // 如果余额小于0 ,则提示余额不足
  454. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  455. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  456. }
  457. }
  458. moneyAccountService.updateByUuid(moneyAccountUpdate);
  459. }
  460. }
  461. // 插入账款总表
  462. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  463. // 更新账款总表上收款的相关字段
  464. Account accountUpdate = new Account();
  465. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  466. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  467. .setObjectId(accountForUpdate.getObjectId());
  468. accountService.updateByUuid(accountUpdate);
  469. /********************* 收款的处理 end **********************/
  470. /********************* 应收收款的处理 begin **********************/
  471. // 应收收款的处理
  472. if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
  473. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
  474. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  475. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  476. recPayHandleItemMapper.insert(recPayHandleItem);
  477. // 账款明细的核销金额和优惠金额
  478. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  479. AccountItem accountItemUpdate = new AccountItem();
  480. // 核销金额,超出剩余应收金额
  481. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  482. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  483. }
  484. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  485. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  486. .setItemId(recPayHandleItem.getAccItemId());
  487. // 剩余金额 = 应收金额-应收收款金额
  488. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  489. accountItemService.updateByUuid(accountItemUpdate);
  490. }
  491. // 计算明细的核销金额,优惠金额合计
  492. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
  493. RecPayHandleItemVO item = new RecPayHandleItemVO();
  494. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  495. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  496. return item;
  497. }).get();
  498. // 更新总账上
  499. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  500. accountUpdate = new Account();
  501. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
  502. .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
  503. .setObjectId(accountForUpdate.getObjectId());
  504. // 剩余应收 = 总应收账款-总应收收款金额
  505. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  506. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  507. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  508. // 更新前的最后校验
  509. // 剩余应收为负数,则不能保存
  510. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  511. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  512. // }
  513. // 可用金额为负数,则不能保存
  514. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  515. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  516. }
  517. accountService.updateByUuid(accountUpdate);
  518. }
  519. /********************* 应收收款的处理 end **********************/
  520. return ResponseResultUtil.success();
  521. }
  522. /**
  523. * @desc : 更新应收核销(小编辑)
  524. * @author : 付斌
  525. * @date : 2023/1/9 10:49
  526. */
  527. @Transactional(
  528. rollbackFor = {Exception.class}
  529. )
  530. public ResponseResultVO<?> updateReceivableReceipt(RecPayVO recPayVO) {
  531. // 先只改备注和附件
  532. RecPay recPay = new RecPay();
  533. recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
  534. super.updateByUuid(recPay);
  535. return ResponseResultUtil.success();
  536. }
  537. /**
  538. * @desc : 作废
  539. * @author : 付斌
  540. * @date : 2024-03-08 16:38
  541. */
  542. public ResponseResultVO<?> invalid(String id) {
  543. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  544. // 并发校验
  545. if (!recPayForUpdate.getFlgValid()) {
  546. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  547. }
  548. // 240624 问完付斌后
  549. // if(recPayForUpdate.getBiznisId() != null){
  550. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  551. // }
  552. // 如果所在月份已结账,则不能作废 todo
  553. // 查出并锁定所有应收核销明细
  554. AccountItem accountItemForUpdate;
  555. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  556. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  557. // 更新账款明细应收收款
  558. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  559. AccountItem accountItemUpdate = new AccountItem();
  560. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  561. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  562. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  563. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  564. accountItemService.updateByUuid(accountItemUpdate);
  565. // 将核销明细有效标识置为false
  566. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  567. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  568. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  569. }
  570. // 把总帐上的钱加回来
  571. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  572. Account accountUpdate = new Account();
  573. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  574. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  575. .setObjectId(accountForUpdate.getObjectId());
  576. // 剩余应收 = 总应收账款-总应收收款金额
  577. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  578. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  579. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  580. // 更新前的最后校验
  581. // 剩余应收为负数,则不能保存
  582. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  583. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  584. // }
  585. // 可用金额为负数,则不能保存
  586. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  587. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  588. }
  589. accountService.updateByUuid(accountUpdate);
  590. // 将之前的明细全部删除
  591. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  592. // 需要重新计算的资金账户
  593. List<String> macList = new ArrayList<>();
  594. for (RecPayItem recPayItem : recPayItemOriginalList) {
  595. // 删除收付款明细
  596. // recPayItemMapper.deleteById(recPayItem.getItemId());
  597. // 改为更新false
  598. RecPayItem recPayItemNew = new RecPayItem();
  599. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  600. recPayItemService.updateByUuid(recPayItemNew);
  601. // 删除账款明细
  602. accountItemMapper.deleteById(recPayItem.getAccItemId());
  603. // 删除账户流水
  604. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  605. if (!macList.contains(recPayItem.getMacId())) {
  606. macList.add(recPayItem.getMacId());
  607. }
  608. }
  609. // 更新总账表的总收款额和可用额
  610. accountService.updateReceipt(recPayForUpdate.getObjectId());
  611. // 更新账户余额
  612. for (String macId : macList) {
  613. accountService.updateMac(macId);
  614. }
  615. // 作废
  616. RecPay recPayUpdate = new RecPay();
  617. recPayUpdate.setFlgValid(false).setRpId(id);
  618. super.updateByUuid(recPayUpdate);
  619. return ResponseResultUtil.success();
  620. }
  621. /**
  622. * @desc : 获取应收收款(编辑用)
  623. * @author : 付斌
  624. * @date : 2024-02-28 13:25
  625. */
  626. @Pagination
  627. public ResponseResultVO getReceivableReceiptForUpdate(String id) {
  628. Map<String, Object> result = new HashMap<>();
  629. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  630. result.put("data", recPayResponse);
  631. // 收款明细
  632. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  633. result.put("dataItem", recPayItem);
  634. // 应收核销明细
  635. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  636. result.put("recPayHandleItem", recPayHandleItem);
  637. // 附件
  638. return ResponseResultUtil.success(result);
  639. }
  640. }