ReceiptService.java 54 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  1. package com.dk.mdm.service.mac;
  2. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  3. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  4. import com.dk.common.exception.BaseBusinessException;
  5. import com.dk.common.infrastructure.annotaiton.Pagination;
  6. import com.dk.common.infrastructure.constant.Constant;
  7. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  8. import com.dk.common.model.pojo.PageList;
  9. import com.dk.common.response.ResponseCodeEnum;
  10. import com.dk.common.response.ResponseResultUtil;
  11. import com.dk.common.response.ResponseResultVO;
  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.infrastructure.util.AuthUtils;
  16. import com.dk.mdm.mapper.mac.*;
  17. import com.dk.mdm.mapper.mst.CustomerMapper;
  18. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  19. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  20. import com.dk.mdm.mapper.mst.SaleChannelMapper;
  21. import com.dk.mdm.model.pojo.ivt.Inbound;
  22. import com.dk.mdm.model.pojo.ivt.OutboundItem;
  23. import com.dk.mdm.model.pojo.mac.*;
  24. import com.dk.common.service.BaseService;
  25. import com.dk.common.mapper.BaseMapper;
  26. import com.dk.mdm.model.pojo.mst.Customer;
  27. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  28. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  29. import com.dk.mdm.model.query.mac.RecPayHandleItemQuery;
  30. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  31. import com.dk.mdm.model.query.mac.RecPayQuery;
  32. import com.dk.mdm.model.query.mst.CustomerQuery;
  33. import com.dk.mdm.model.query.mst.SaleChannelQuery;
  34. import com.dk.mdm.model.response.mac.RecPayHandleItemResponse;
  35. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  36. import com.dk.mdm.model.response.mac.RecPayResponse;
  37. import com.dk.mdm.model.response.mst.CustomerResponse;
  38. import com.dk.mdm.model.response.mst.SaleChannelResponse;
  39. import com.dk.mdm.model.vo.mac.RecPayHandleItemVO;
  40. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  41. import com.dk.mdm.model.vo.mac.RecPayVO;
  42. import com.dk.mdm.service.common.CommonService;
  43. import com.dk.mdm.service.mst.CustomerService;
  44. import com.dk.mdm.service.mst.MoneyAccountService;
  45. import org.springframework.stereotype.Service;
  46. import org.springframework.beans.factory.annotation.Autowired;
  47. import org.springframework.transaction.annotation.Transactional;
  48. import java.math.BigDecimal;
  49. import java.util.*;
  50. @Service
  51. @Transactional
  52. public class ReceiptService extends BaseService<RecPay> {
  53. @Override
  54. public String getPrimaryKey() {
  55. return "rp_id";
  56. }
  57. @Override
  58. public BaseMapper<RecPay> getRepository() {
  59. return recPayMapper;
  60. }
  61. @Autowired
  62. private RecPayMapper recPayMapper;
  63. @Autowired
  64. private RecPayItemService recPayItemService;
  65. @Autowired
  66. private RecPayItemMapper recPayItemMapper;
  67. @Autowired
  68. private AccountService accountService;
  69. @Autowired
  70. private AccountMapper accountMapper;
  71. @Autowired
  72. private AccountItemService accountItemService;
  73. @Autowired
  74. private MoneyAccountMapper moneyAccountMapper;
  75. @Autowired
  76. private AccountItemMapper accountItemMapper;
  77. @Autowired
  78. private MoneyAccountService moneyAccountService;
  79. @Autowired
  80. private MoneyAccountItemMapper moneyAccountItemMapper;
  81. @Autowired
  82. private RecPayHandleItemService recPayHandleItemService;
  83. @Autowired
  84. private RecPayHandleItemMapper recPayHandleItemMapper;
  85. @Autowired
  86. private CommonService commonService;
  87. @Autowired
  88. private CustomerMapper customerMapper;
  89. @Autowired
  90. private SaleChannelMapper saleChannelMapper;
  91. @Autowired
  92. private CustomerService customerService;
  93. @Autowired
  94. private RecPayConvert recPayConvert;
  95. @Autowired
  96. private RecPayItemConvert recPayItemConvert;
  97. @Autowired
  98. private RecPayHandleItemConvert recPayHandleItemConvert;
  99. @Autowired
  100. private AuthUtils authUtils;
  101. /**
  102. * @desc : 条件查询
  103. * @author : 付斌
  104. * @date : 2023/1/9 10:40
  105. */
  106. @Pagination
  107. public ResponseResultVO<PageList<RecPayResponse>> selectByCond(RecPayQuery recPayQuery) {
  108. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCond(recPayQuery),
  109. recPayMapper.countByCond(recPayQuery));
  110. }
  111. /**
  112. * @desc : 条件查询-客户详情的收款列表
  113. * @author : 姜永辉
  114. * @date : 2023/1/9 10:40
  115. */
  116. @Pagination
  117. public ResponseResultVO<PageList<RecPayResponse>> selectByCondCus(RecPayQuery recPayQuery) {
  118. return super.mergeListWithCount(recPayQuery, recPayMapper.selectByCondCus(recPayQuery),
  119. recPayMapper.countByCondCus(recPayQuery));
  120. }
  121. /**
  122. * @desc : 查询收款明细
  123. * @author : 付斌
  124. * @date : 2024-02-28 13:25
  125. */
  126. @Pagination
  127. public ResponseResultVO selectById(String id) {
  128. Map<String, Object> result = new HashMap<>();
  129. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  130. result.put("data", recPayResponse);
  131. // 收款明细
  132. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  133. result.put("recPayItem", recPayItem);
  134. // 应收核销明细
  135. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  136. result.put("recPayHandleItem", recPayHandleItem);
  137. // 附件
  138. return ResponseResultUtil.success(result);
  139. }
  140. /**
  141. * @desc : 查询收款明细
  142. * @author : 付斌
  143. * @date : 2024-02-28 13:25
  144. */
  145. @Pagination
  146. public ResponseResultVO<Map<String, Object>> selectRpInfoById(String id) {
  147. Map<String, Object> result = new HashMap<>();
  148. // 收款明细
  149. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  150. result.put("recPayItem", recPayItem);
  151. // 应收核销明细
  152. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  153. result.put("recPayHandleItem", recPayHandleItem);
  154. // 附件
  155. return ResponseResultUtil.success(result);
  156. }
  157. /**
  158. * @desc : 新建客户收款
  159. * @author : 付斌
  160. * @date : 2023/1/9 10:49
  161. */
  162. @Transactional(
  163. rollbackFor = {Exception.class}
  164. )
  165. public ResponseResultVO<?> insertReceipt(RecPayVO recPayVO) {
  166. // 获取单号
  167. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  168. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  169. // 转化实体
  170. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  171. // 客户的新增和更新跟进人
  172. // 如果没有客户id,要新建
  173. if (recPay.getObjectId() == null) {
  174. List<CustomerResponse> listCustomer = customerMapper.selectByCond(new CustomerQuery().setCpId(recPay.getCpId()).setCusPhone(recPayVO.getCusPhone()));
  175. // 如果客户电话已存在
  176. if (listCustomer == null || listCustomer.size() == 0) {
  177. String channelId;
  178. List<SaleChannelResponse> saleChannelResponses = saleChannelMapper.selectByCond(new SaleChannelQuery());
  179. channelId = (saleChannelResponses!=null && saleChannelResponses.size() > 0)?saleChannelResponses.get(0).getChannelId():"";
  180. // 创建客户,获取编码和主键UuId
  181. Map<String, Object> codeMapCus = commonService.getUniqueNoteCode(Constant.docNameConstant.CUSTOMER.getName(), true);
  182. Customer customer = new Customer();
  183. List<String> followStaffs = new ArrayList<>();
  184. followStaffs.add(authUtils.getStaff().getStaffId());
  185. customer.setCusId(codeMapCus.get("outId").toString()).setCusCode(codeMapCus.get("outNote").toString()).setCusName(recPayVO.getCusName())
  186. .setCusPhone(recPayVO.getCusPhone()).setAddressArea(recPayVO.getAddressArea()).setAddressName(recPayVO.getAddressName())
  187. .setAddressNo(recPayVO.getAddressNo()).setAddressGcj02(recPayVO.getAddressGcj02()).setAddressFull(recPayVO.getAddressFull())
  188. .setChannelId(channelId).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId()).setFollowStaffs(followStaffs)
  189. .setReportStaff(recPayVO.getStaffId()).setSaleStatus(Constant.SaleStatus.QIANKE.getName()).setCpId(recPayVO.getCpId());
  190. customerMapper.insert(customer);
  191. recPayVO.setObjectId(customer.getCusId());
  192. recPay.setObjectId(customer.getCusId());
  193. } else{
  194. recPayVO.setObjectId(listCustomer.get(0).getCusId());
  195. recPay.setObjectId(listCustomer.get(0).getCusId());
  196. }
  197. }else{ //如果有客户id 更新 客户信息
  198. Customer customer = new Customer();
  199. customer.setCusId(recPayVO.getObjectId()).setCusName(recPayVO.getCusName())
  200. .setCusPhone(recPayVO.getCusPhone()).setAddressArea(recPayVO.getAddressArea()).setAddressName(recPayVO.getAddressName())
  201. .setAddressNo(recPayVO.getAddressNo()).setAddressGcj02(recPayVO.getAddressGcj02()).setAddressFull(recPayVO.getAddressFull());
  202. //修改
  203. customerMapper.update(customer, new UpdateWrapper<Customer>().lambda().eq(Customer::getCusId, UUID.fromString(customer.getCusId())));
  204. }
  205. // 总单保存
  206. super.insert(recPay);
  207. // 明细保存
  208. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  209. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  210. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  211. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  212. recPayItemMapper.insert(recPayItem);
  213. // 插入账款明细
  214. AccountItem accountItem = new AccountItem();
  215. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  216. .setObjectId(recPayVO.getObjectId())
  217. .setOrgId(recPayVO.getOrgId())
  218. .setStaffId(recPayVO.getStaffId())
  219. .setAccDate(recPayVO.getAccDate())
  220. .setRecStatus(Constant.recStatuse.QUE_DING.getName())
  221. .setAmtRec(recPayItem.getAmtRec())
  222. .setBiznisType("t_mac_rec_pay_item")
  223. .setBiznisId(recPayItem.getRpId())
  224. .setBiznisNo(recPayVO.getRpNo())
  225. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  226. accountItemMapper.insert(accountItem);
  227. // 更新收款单上的账款明细Id
  228. RecPayItem recPayItemUpdate = new RecPayItem();
  229. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  230. recPayItemService.updateByUuid(recPayItemUpdate);
  231. // 插入资金流水
  232. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  233. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  234. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  235. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  236. moneyAccountItemMapper.insert(moneyAccountItem);
  237. // 更新资金账户
  238. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  239. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  240. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  241. .setMacId(moneyAccountForUpdate.getMacId());
  242. moneyAccountService.updateByUuid(moneyAccountUpdate);
  243. }
  244. }
  245. // 插入账款总表
  246. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  247. // 更新账款总表上收款的相关字段
  248. Account accountUpdate = new Account();
  249. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  250. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  251. .setObjectId(accountForUpdate.getObjectId());
  252. accountService.updateByUuid(accountUpdate);
  253. return ResponseResultUtil.success();
  254. }
  255. /**
  256. * @desc : 新建客户退款
  257. * @author : 付斌
  258. * @date : 2023/1/9 10:49
  259. */
  260. @Transactional(
  261. rollbackFor = {Exception.class}
  262. )
  263. public ResponseResultVO<?> insertRefund(RecPayVO recPayVO) {
  264. // 查总账,看可退金额是否满足
  265. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  266. // if (accountForUpdate == null || accountForUpdate.getReceiptResidue().compareTo(recPayVO.getSumAmtRec().abs()) == -1) {
  267. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  268. // }
  269. // 获取单号
  270. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  271. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  272. // 转化实体
  273. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  274. // 总单保存
  275. super.insert(recPay);
  276. // 明细保存
  277. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  278. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  279. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  280. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  281. recPayItemMapper.insert(recPayItem);
  282. // 插入账款明细
  283. AccountItem accountItem = new AccountItem();
  284. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  285. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  286. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  287. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  288. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  289. accountItemMapper.insert(accountItem);
  290. // 更新收款单上的账款明细Id
  291. RecPayItem recPayItemUpdate = new RecPayItem();
  292. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  293. recPayItemService.updateByUuid(recPayItemUpdate);
  294. // 插入资金流水
  295. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  296. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  297. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  298. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  299. moneyAccountItemMapper.insert(moneyAccountItem);
  300. // 更新资金账户
  301. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  302. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  303. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  304. .setMacId(moneyAccountForUpdate.getMacId());
  305. // 如果账户不允许为负数
  306. if (!moneyAccountForUpdate.getFlgNegative()) {
  307. // 如果余额小于0 ,则提示余额不足
  308. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  309. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  310. }
  311. }
  312. moneyAccountService.updateByUuid(moneyAccountUpdate);
  313. }
  314. }
  315. // 更新账款总表上收款的相关字段
  316. Account accountUpdate = new Account();
  317. // 新建
  318. if(accountForUpdate == null ){
  319. // TODO 退货新客户插入的问题
  320. }else{
  321. // 说明已经存在
  322. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  323. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  324. .setObjectId(accountForUpdate.getObjectId());
  325. accountService.updateByUuid(accountUpdate);
  326. }
  327. return ResponseResultUtil.success();
  328. }
  329. /**
  330. * @desc : 编辑客户收款/退款
  331. * @author : 付斌
  332. * @date : 2023/1/9 10:49
  333. */
  334. @Transactional(
  335. rollbackFor = {Exception.class}
  336. )
  337. public ResponseResultVO<?> update(RecPayVO recPayVO) {
  338. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(recPayVO.getRpId());
  339. // 并发校验
  340. if (!recPayForUpdate.getFlgValid()) {
  341. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  342. }
  343. // if(recPayForUpdate.getBiznisId() != null){
  344. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  345. // }
  346. // 将之前的明细全部删除
  347. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  348. // 需要重新计算的资金账户
  349. List<String> macList = new ArrayList<>();
  350. for (RecPayItem recPayItem : recPayItemOriginalList) {
  351. // 删除收付款明细
  352. // recPayItemMapper.deleteById(recPayItem.getItemId());
  353. // 改为更新false
  354. RecPayItem recPayItemNew = new RecPayItem();
  355. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  356. recPayItemService.updateByUuid(recPayItemNew);
  357. // 删除账款明细
  358. accountItemMapper.deleteById(recPayItem.getAccItemId());
  359. // 删除账户流水
  360. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  361. if (!macList.contains(recPayItem.getMacId())) {
  362. macList.add(recPayItem.getMacId());
  363. }
  364. }
  365. // 新增明细
  366. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  367. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  368. recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  369. recPayItemMapper.insert(recPayItem);
  370. // 插入账款明细
  371. AccountItem accountItem = new AccountItem();
  372. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  373. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  374. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  375. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  376. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  377. accountItemMapper.insert(accountItem);
  378. // 更新收款单上的账款明细Id
  379. RecPayItem recPayItemUpdate = new RecPayItem();
  380. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  381. recPayItemService.updateByUuid(recPayItemUpdate);
  382. // 插入资金流水
  383. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  384. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  385. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  386. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  387. moneyAccountItemMapper.insert(moneyAccountItem);
  388. if (!macList.contains(recPayItem.getMacId())) {
  389. macList.add(recPayItem.getMacId());
  390. }
  391. }
  392. // 更新总账表的总收款额和可用额
  393. accountService.updateReceipt(recPayVO.getObjectId());
  394. // 更新账户余额
  395. for (String macId : macList) {
  396. accountService.updateMac(macId);
  397. }
  398. // 更新收款单总表
  399. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  400. return ResponseResultUtil.success(super.updateByUuid(recPay));
  401. }
  402. /**
  403. * @desc : 获取订单信息(编辑用)
  404. * @author : 付斌
  405. * @date : 2024-03-02 17:27
  406. */
  407. public ResponseResultVO<?> getRpForUpdate(String id) {
  408. Map<String, Object> dataInfo = new HashMap<>();
  409. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  410. dataInfo.put("data", recPayResponse);
  411. // 收款明细
  412. List<RecPayItemResponse> recPayItemResponse = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  413. dataInfo.put("dataItem", recPayItemResponse);
  414. return ResponseResultUtil.success(dataInfo);
  415. }
  416. /**
  417. * @desc : 新建应收核销(收款+核销应收)
  418. * @author : 付斌
  419. * @date : 2023/1/9 10:49
  420. */
  421. @Transactional(
  422. rollbackFor = {Exception.class}
  423. )
  424. public ResponseResultVO<?> insertReceivableReceipt(RecPayVO recPayVO) {
  425. /********************* 收款的处理 begin **********************/
  426. // 获取单号
  427. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  428. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  429. // 转化实体
  430. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  431. // 总单保存
  432. super.insert(recPay);
  433. // 明细保存
  434. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  435. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  436. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  437. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  438. recPayItemMapper.insert(recPayItem);
  439. // 插入账款明细
  440. AccountItem accountItem = new AccountItem();
  441. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  442. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  443. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  444. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  445. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  446. accountItemMapper.insert(accountItem);
  447. // 更新收款单上的账款明细Id
  448. RecPayItem recPayItemUpdate = new RecPayItem();
  449. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  450. recPayItemService.updateByUuid(recPayItemUpdate);
  451. // 插入资金流水
  452. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  453. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  454. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPayVO.getAccDate())
  455. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  456. moneyAccountItemMapper.insert(moneyAccountItem);
  457. // 更新资金账户
  458. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  459. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  460. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  461. .setMacId(moneyAccountForUpdate.getMacId());
  462. // 如果账户不允许为负数
  463. if (!moneyAccountForUpdate.getFlgNegative()) {
  464. // 如果余额小于0 ,则提示余额不足
  465. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  466. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  467. }
  468. }
  469. moneyAccountService.updateByUuid(moneyAccountUpdate);
  470. }
  471. }
  472. // 插入账款总表
  473. Account accountForUpdate = accountService.getCusAccountForUpdate(recPayVO.getObjectId());
  474. // 更新账款总表上收款的相关字段
  475. Account accountUpdate = new Account();
  476. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayVO.getSumAmtRec()))// 总收款金额
  477. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayVO.getSumAmtRec()))// 可退金额
  478. .setObjectId(accountForUpdate.getObjectId());
  479. accountService.updateByUuid(accountUpdate);
  480. /********************* 收款的处理 end **********************/
  481. /********************* 应收收款的处理 begin **********************/
  482. // 应收收款的处理
  483. if (recPayVO.getReceivableList() != null && recPayVO.getReceivableList().size() > 0) {
  484. for (RecPayHandleItemVO recPayHandleItemVO : recPayVO.getReceivableList()) {
  485. RecPayHandleItem recPayHandleItem = recPayHandleItemConvert.convertToPo(recPayHandleItemVO);
  486. recPayHandleItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  487. recPayHandleItemMapper.insert(recPayHandleItem);
  488. // 账款明细的核销金额和优惠金额
  489. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  490. AccountItem accountItemUpdate = new AccountItem();
  491. // 核销金额,超出剩余应收金额
  492. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  493. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  494. }
  495. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  496. .setAmtWaive(accountItemForUpdate.getAmtWaive().add(recPayHandleItem.getAmtWaive()))
  497. .setItemId(recPayHandleItem.getAccItemId());
  498. // 剩余金额 = 应收金额-应收收款金额
  499. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  500. accountItemService.updateByUuid(accountItemUpdate);
  501. }
  502. // 计算明细的核销金额,优惠金额合计
  503. RecPayHandleItemVO recPayHandleItemVO = recPayVO.getReceivableList().stream().reduce((x, y) -> {
  504. RecPayHandleItemVO item = new RecPayHandleItemVO();
  505. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  506. item.setAmtWaive(x.getAmtWaive().add(y.getAmtWaive()));
  507. return item;
  508. }).get();
  509. // 更新总账上
  510. accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  511. accountUpdate = new Account();
  512. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItemVO.getAmtReceivableHandle()))// 总应收收款金额
  513. .setReceivableWaive(accountForUpdate.getReceivableWaive().add(recPayHandleItemVO.getAmtWaive()))// 总应收优惠金额
  514. .setObjectId(accountForUpdate.getObjectId());
  515. // 剩余应收 = 总应收账款-总应收收款金额
  516. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  517. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  518. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  519. // 更新前的最后校验
  520. // 剩余应收为负数,则不能保存
  521. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  522. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  523. // }
  524. // 可用金额为负数,则不能保存
  525. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  526. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  527. }
  528. accountService.updateByUuid(accountUpdate);
  529. }
  530. /********************* 应收收款的处理 end **********************/
  531. return ResponseResultUtil.success();
  532. }
  533. /**
  534. * @desc : 更新应收核销(小编辑)
  535. * @author : 付斌
  536. * @date : 2023/1/9 10:49
  537. */
  538. @Transactional(
  539. rollbackFor = {Exception.class}
  540. )
  541. public ResponseResultVO<?> updateReceivableReceipt(RecPayVO recPayVO) {
  542. // 先只改备注和附件
  543. RecPay recPay = new RecPay();
  544. recPay.setRemarks(recPayVO.getRemarks()).setAnnexPaths(recPayVO.getAnnexPaths()).setRpId(recPayVO.getRpId());
  545. super.updateByUuid(recPay);
  546. return ResponseResultUtil.success();
  547. }
  548. /**
  549. * @desc : 作废
  550. * @author : 付斌
  551. * @date : 2024-03-08 16:38
  552. */
  553. public ResponseResultVO<?> invalid(String id) {
  554. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  555. // 并发校验
  556. if (!recPayForUpdate.getFlgValid()) {
  557. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  558. }
  559. // 240624 问完付斌后
  560. // if(recPayForUpdate.getBiznisId() != null){
  561. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  562. // }
  563. // 如果所在月份已结账,则不能作废 todo
  564. // 查出并锁定所有应收核销明细
  565. AccountItem accountItemForUpdate;
  566. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  567. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  568. // 更新账款明细应收收款
  569. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  570. AccountItem accountItemUpdate = new AccountItem();
  571. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  572. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  573. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  574. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  575. accountItemService.updateByUuid(accountItemUpdate);
  576. // 将核销明细有效标识置为false
  577. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  578. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  579. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  580. }
  581. // 把总帐上的钱加回来
  582. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  583. Account accountUpdate = new Account();
  584. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  585. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  586. .setObjectId(accountForUpdate.getObjectId());
  587. // 剩余应收 = 总应收账款-总应收收款金额
  588. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  589. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  590. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  591. // 更新前的最后校验
  592. // 剩余应收为负数,则不能保存
  593. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  594. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  595. // }
  596. // 可用金额为负数,则不能保存
  597. // if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  598. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  599. // }
  600. accountService.updateByUuid(accountUpdate);
  601. // 将之前的明细全部删除
  602. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  603. // 需要重新计算的资金账户
  604. List<String> macList = new ArrayList<>();
  605. for (RecPayItem recPayItem : recPayItemOriginalList) {
  606. // 删除收付款明细
  607. // recPayItemMapper.deleteById(recPayItem.getItemId());
  608. // 改为更新false
  609. RecPayItem recPayItemNew = new RecPayItem();
  610. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  611. recPayItemService.updateByUuid(recPayItemNew);
  612. // 删除账款明细
  613. accountItemMapper.deleteById(recPayItem.getAccItemId());
  614. // 删除账户流水
  615. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  616. if (!macList.contains(recPayItem.getMacId())) {
  617. macList.add(recPayItem.getMacId());
  618. }
  619. }
  620. // 更新总账表的总收款额和可用额
  621. accountService.updateReceipt(recPayForUpdate.getObjectId());
  622. // 更新账户余额
  623. for (String macId : macList) {
  624. accountService.updateMac(macId);
  625. }
  626. // 作废
  627. RecPay recPayUpdate = new RecPay();
  628. recPayUpdate.setFlgValid(false).setRpId(id);
  629. super.updateByUuid(recPayUpdate);
  630. return ResponseResultUtil.success();
  631. }
  632. /**
  633. * @desc : 获取应收收款(编辑用)
  634. * @author : 付斌
  635. * @date : 2024-02-28 13:25
  636. */
  637. @Pagination
  638. public ResponseResultVO getReceivableReceiptForUpdate(String id) {
  639. Map<String, Object> result = new HashMap<>();
  640. RecPayResponse recPayResponse = recPayMapper.selectById(id);
  641. result.put("data", recPayResponse);
  642. // 收款明细
  643. List<RecPayItemResponse> recPayItem = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(id));
  644. result.put("dataItem", recPayItem);
  645. // 应收核销明细
  646. List<RecPayHandleItemResponse> recPayHandleItem = recPayHandleItemMapper.selectByCond(new RecPayHandleItemQuery().setRpId(id));
  647. result.put("recPayHandleItem", recPayHandleItem);
  648. // 附件
  649. return ResponseResultUtil.success(result);
  650. }
  651. /**
  652. * @desc : 供应商收退款查询
  653. * @author : 于继渤
  654. * @date : 2024/7/5 10:36
  655. */
  656. @Pagination
  657. public ResponseResultVO<PageList<RecPayResponse>> getRpSupList(RecPayQuery recPayQuery) {
  658. return super.mergeListWithCount(recPayQuery, recPayMapper.getRpSupList(recPayQuery),
  659. recPayMapper.getRpSupListCount(recPayQuery));
  660. }
  661. /**
  662. * @desc : 供应商收退款查询
  663. * @author : 于继渤
  664. * @date : 2024/7/5 10:36
  665. */
  666. @Pagination
  667. public ResponseResultVO<PageList<RecPayResponse>> getRpSupListDetail(RecPayQuery recPayQuery) {
  668. return super.mergeListWithCount(recPayQuery, recPayMapper.getRpSupListDetail(recPayQuery),
  669. recPayMapper.getRpSupListCountDetail(recPayQuery));
  670. }
  671. /**
  672. * @desc : 新建供应商收款
  673. * @author : 于继渤
  674. * @date : 2024/7/5 10:48
  675. */
  676. @Transactional(
  677. rollbackFor = {Exception.class}
  678. )
  679. public ResponseResultVO<?> insertSupReceipt(RecPayVO recPayVO) {
  680. // 获取单号
  681. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  682. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  683. // 转化实体
  684. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  685. // 总单保存
  686. super.insert(recPay);
  687. // 明细保存
  688. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  689. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  690. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  691. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  692. recPayItemMapper.insert(recPayItem);
  693. // 插入账款明细
  694. AccountItem accountItem = new AccountItem();
  695. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  696. .setObjectId(recPayVO.getObjectId())
  697. .setOrgId(recPayVO.getOrgId())
  698. .setStaffId(recPayVO.getStaffId())
  699. .setAccDate(recPayVO.getAccDate())
  700. .setRecStatus(Constant.recStatuse.QUE_DING.getName())
  701. .setAmtPay(recPayItem.getAmtPay())
  702. .setBiznisType("t_mac_rec_pay_item")
  703. .setBiznisId(recPayItem.getRpId())
  704. .setBiznisNo(recPayVO.getRpNo())
  705. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  706. accountItemMapper.insert(accountItem);
  707. // 更新收款单上的账款明细Id
  708. RecPayItem recPayItemUpdate = new RecPayItem();
  709. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  710. recPayItemService.updateByUuid(recPayItemUpdate);
  711. // 插入资金流水
  712. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  713. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  714. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
  715. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  716. moneyAccountItemMapper.insert(moneyAccountItem);
  717. // 更新资金账户
  718. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  719. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  720. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay()))
  721. .setMacId(moneyAccountForUpdate.getMacId());
  722. moneyAccountService.updateByUuid(moneyAccountUpdate);
  723. }
  724. }
  725. // 插入账款总表
  726. Account accountForUpdate = accountService.getSupAccountForUpdate(recPayVO.getObjectId());
  727. // 更新账款总表上收款的相关字段
  728. Account accountUpdate = new Account();
  729. accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总收款金额
  730. .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
  731. .setObjectId(accountForUpdate.getObjectId());
  732. accountService.updateByUuid(accountUpdate);
  733. return ResponseResultUtil.success();
  734. }
  735. /**
  736. * @desc : 新建供应商退款
  737. * @author : 于继渤
  738. * @date : 2024/7/5 10:48
  739. */
  740. @Transactional(
  741. rollbackFor = {Exception.class}
  742. )
  743. public ResponseResultVO<?> insertSupRefund(RecPayVO recPayVO) {
  744. // 查总账,看可退金额是否满足
  745. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayVO.getObjectId());
  746. // 获取单号
  747. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  748. recPayVO.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString());
  749. // 转化实体
  750. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  751. // 总单保存
  752. super.insert(recPay);
  753. // 明细保存
  754. if (recPayVO.getItemList() != null && recPayVO.getItemList().size() > 0) {
  755. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  756. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  757. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  758. recPayItemMapper.insert(recPayItem);
  759. // 插入账款明细
  760. AccountItem accountItem = new AccountItem();
  761. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  762. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  763. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
  764. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  765. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  766. accountItemMapper.insert(accountItem);
  767. // 更新收款单上的账款明细Id
  768. RecPayItem recPayItemUpdate = new RecPayItem();
  769. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  770. recPayItemService.updateByUuid(recPayItemUpdate);
  771. // 插入资金流水
  772. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  773. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  774. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay().negate()).setAccDate(recPayVO.getAccDate())
  775. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  776. moneyAccountItemMapper.insert(moneyAccountItem);
  777. // 更新资金账户
  778. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  779. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  780. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtPay()))
  781. .setMacId(moneyAccountForUpdate.getMacId());
  782. // 如果账户不允许为负数
  783. if (!moneyAccountForUpdate.getFlgNegative()) {
  784. // 如果余额小于0 ,则提示余额不足
  785. if (moneyAccountUpdate.getBalance().compareTo(BigDecimal.ZERO) == -1) {
  786. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  787. }
  788. }
  789. moneyAccountService.updateByUuid(moneyAccountUpdate);
  790. }
  791. }
  792. // 更新账款总表上收款的相关字段
  793. Account accountUpdate = new Account();
  794. // 新建
  795. if(accountForUpdate == null ){
  796. // TODO 退货新客户插入的问题
  797. }else{
  798. // 说明已经存在
  799. accountUpdate.setPayment(accountForUpdate.getPayment().add(recPayVO.getSumAmtPay()))// 总收款金额
  800. .setPaymentResidue(accountForUpdate.getPaymentResidue().add(recPayVO.getSumAmtPay()))// 可退金额
  801. .setObjectId(accountForUpdate.getObjectId());
  802. accountService.updateByUuid(accountUpdate);
  803. }
  804. return ResponseResultUtil.success();
  805. }
  806. /**
  807. * @desc : 编辑供应商收款/退款
  808. * @author : 于继渤
  809. * @date : 2023/1/9 10:49
  810. */
  811. @Transactional(
  812. rollbackFor = {Exception.class}
  813. )
  814. public ResponseResultVO<?> updateSupReceipt(RecPayVO recPayVO) {
  815. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(recPayVO.getRpId());
  816. // 并发校验
  817. if (!recPayForUpdate.getFlgValid()) {
  818. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  819. }
  820. // 将之前的明细全部删除
  821. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayVO.getRpId());
  822. // 需要重新计算的资金账户
  823. List<String> macList = new ArrayList<>();
  824. for (RecPayItem recPayItem : recPayItemOriginalList) {
  825. // 删除收付款明细
  826. // recPayItemMapper.deleteById(recPayItem.getItemId());
  827. // 改为更新false
  828. RecPayItem recPayItemNew = new RecPayItem();
  829. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  830. recPayItemService.updateByUuid(recPayItemNew);
  831. // 删除账款明细
  832. accountItemMapper.deleteById(recPayItem.getAccItemId());
  833. // 删除账户流水
  834. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  835. if (!macList.contains(recPayItem.getMacId())) {
  836. macList.add(recPayItem.getMacId());
  837. }
  838. }
  839. // 新增明细
  840. for (RecPayItemVO recPayItemVO : recPayVO.getItemList()) {
  841. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  842. recPayItem.setItemId(null).setRpId(recPayVO.getRpId()).setCpId(recPayVO.getCpId());
  843. recPayItemMapper.insert(recPayItem);
  844. // 插入账款明细
  845. AccountItem accountItem = new AccountItem();
  846. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  847. .setObjectId(recPayVO.getObjectId()).setOrgId(recPayVO.getOrgId()).setStaffId(recPayVO.getStaffId())
  848. .setAccDate(recPayVO.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtPay(recPayItem.getAmtPay())
  849. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPayVO.getRpNo())
  850. .setMakeStaff(recPayVO.getMakeStaff()).setCpId(recPayVO.getCpId());
  851. accountItemMapper.insert(accountItem);
  852. // 更新收款单上的账款明细Id
  853. RecPayItem recPayItemUpdate = new RecPayItem();
  854. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  855. recPayItemService.updateByUuid(recPayItemUpdate);
  856. // 插入资金流水
  857. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  858. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  859. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtPay()).setAccDate(recPayVO.getAccDate())
  860. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  861. moneyAccountItemMapper.insert(moneyAccountItem);
  862. if (!macList.contains(recPayItem.getMacId())) {
  863. macList.add(recPayItem.getMacId());
  864. }
  865. }
  866. // 更新总账表的总收款额和可用额
  867. accountService.updatePayment(recPayVO.getObjectId());
  868. // 更新账户余额
  869. for (String macId : macList) {
  870. accountService.updateMac(macId);
  871. }
  872. // 更新收款单总表
  873. RecPay recPay = recPayConvert.convertToPo(recPayVO);
  874. return ResponseResultUtil.success(super.updateByUuid(recPay));
  875. }
  876. /**
  877. * @desc : 作废
  878. * @author : 于继渤
  879. * @date : 2024-03-08 16:38
  880. */
  881. public ResponseResultVO<?> invalidSup(String id) {
  882. RecPay recPayForUpdate = recPayMapper.selectByIdForUpdate(id);
  883. // 并发校验
  884. if (!recPayForUpdate.getFlgValid()) {
  885. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  886. }
  887. // 240624 问完付斌后
  888. // if(recPayForUpdate.getBiznisId() != null){
  889. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.INVOICE_FORBID_EDIT.getMessage());
  890. // }
  891. // 如果所在月份已结账,则不能作废 todo
  892. // 查出并锁定所有应收核销明细
  893. AccountItem accountItemForUpdate;
  894. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(id);
  895. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  896. // 更新账款明细应收收款
  897. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  898. AccountItem accountItemUpdate = new AccountItem();
  899. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtPayableHandle()))
  900. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  901. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  902. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  903. accountItemService.updateByUuid(accountItemUpdate);
  904. // 将核销明细有效标识置为false
  905. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  906. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  907. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  908. }
  909. // 把总帐上的钱加回来
  910. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  911. Account accountUpdate = new Account();
  912. accountUpdate.setPayableHandle(accountForUpdate.getPayableHandle().subtract(recPayForUpdate.getSumAmtPayableHandle()))// 总应收收款金额
  913. .setPayableWaive(accountForUpdate.getPayableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  914. .setObjectId(accountForUpdate.getObjectId());
  915. // 剩余应收 = 总应收账款-总应收收款金额
  916. accountUpdate.setPaymentResidue(accountForUpdate.getPayable().subtract(accountUpdate.getPayableHandle()));
  917. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  918. accountUpdate.setPaymentResidue(accountForUpdate.getPayment().subtract(accountUpdate.getPayableHandle()).add(accountUpdate.getPayableWaive()));
  919. // 更新前的最后校验
  920. // 剩余应收为负数,则不能保存
  921. // if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  922. // throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  923. // }
  924. // 可用金额为负数,则不能保存
  925. if (accountUpdate.getPaymentResidue().compareTo(BigDecimal.ZERO) == -1) {
  926. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  927. }
  928. accountService.updateByUuid(accountUpdate);
  929. // 将之前的明细全部删除
  930. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(id);
  931. // 需要重新计算的资金账户
  932. List<String> macList = new ArrayList<>();
  933. for (RecPayItem recPayItem : recPayItemOriginalList) {
  934. // 删除收付款明细
  935. // recPayItemMapper.deleteById(recPayItem.getItemId());
  936. // 改为更新false
  937. RecPayItem recPayItemNew = new RecPayItem();
  938. recPayItemNew.setItemId(recPayItem.getItemId()).setFlgValid(false);
  939. recPayItemService.updateByUuid(recPayItemNew);
  940. // 删除账款明细
  941. accountItemMapper.deleteById(recPayItem.getAccItemId());
  942. // 删除账户流水
  943. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  944. if (!macList.contains(recPayItem.getMacId())) {
  945. macList.add(recPayItem.getMacId());
  946. }
  947. }
  948. // 更新总账表的总收款额和可用额
  949. accountService.updatePayment(recPayForUpdate.getObjectId());
  950. // 更新账户余额
  951. for (String macId : macList) {
  952. accountService.updateMac(macId);
  953. }
  954. // 作废
  955. RecPay recPayUpdate = new RecPay();
  956. recPayUpdate.setFlgValid(false).setRpId(id);
  957. super.updateByUuid(recPayUpdate);
  958. return ResponseResultUtil.success();
  959. }
  960. }