OtherReceivableService.java 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621
  1. package com.dk.mdm.service.mac;
  2. import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
  3. import com.dk.common.exception.BaseBusinessException;
  4. import com.dk.common.infrastructure.annotaiton.Pagination;
  5. import com.dk.common.infrastructure.constant.Constant;
  6. import com.dk.common.infrastructure.enums.ErrorCodeEnum;
  7. import com.dk.common.model.pojo.PageList;
  8. import com.dk.common.response.ResponseCodeEnum;
  9. import com.dk.common.response.ResponseResultUtil;
  10. import com.dk.common.response.ResponseResultVO;
  11. import com.dk.mdm.infrastructure.convert.mac.OtherReceivableConvert;
  12. import com.dk.mdm.infrastructure.convert.mac.OtherReceivableItemConvert;
  13. import com.dk.mdm.infrastructure.convert.mac.RecPayItemConvert;
  14. import com.dk.mdm.mapper.mac.*;
  15. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  16. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  17. import com.dk.mdm.model.pojo.mac.*;
  18. import com.dk.common.service.BaseService;
  19. import com.dk.common.mapper.BaseMapper;
  20. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  21. import com.dk.mdm.model.pojo.mst.MoneyAccountItem;
  22. import com.dk.mdm.model.query.mac.OtherReceivableItemQuery;
  23. import com.dk.mdm.model.query.mac.OtherReceivableQuery;
  24. import com.dk.mdm.model.query.mac.RecPayItemQuery;
  25. import com.dk.mdm.model.response.mac.OtherReceivableItemResponse;
  26. import com.dk.mdm.model.response.mac.OtherReceivableResponse;
  27. import com.dk.mdm.model.response.mac.RecPayItemResponse;
  28. import com.dk.mdm.model.vo.mac.OtherReceivableItemVO;
  29. import com.dk.mdm.model.vo.mac.OtherReceivableVO;
  30. import com.dk.mdm.model.vo.mac.RecPayItemVO;
  31. import com.dk.mdm.service.common.CommonService;
  32. import com.dk.mdm.service.mst.MoneyAccountService;
  33. import org.springframework.stereotype.Service;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import java.math.BigDecimal;
  37. import java.util.*;
  38. @Service
  39. @Transactional
  40. public class OtherReceivableService extends BaseService<OtherReceivable> {
  41. @Override
  42. public String getPrimaryKey() {
  43. return "receivable_id";
  44. }
  45. @Override
  46. public BaseMapper<OtherReceivable> getRepository() {
  47. return otherReceivableMapper;
  48. }
  49. @Autowired
  50. private OtherReceivableMapper otherReceivableMapper;
  51. @Autowired
  52. private OtherReceivableItemMapper otherReceivableItemMapper;
  53. @Autowired
  54. private CommonService commonService;
  55. @Autowired
  56. private AccountService accountService;
  57. @Autowired
  58. private AccountMapper accountMapper;
  59. @Autowired
  60. private AccountItemService accountItemService;
  61. @Autowired
  62. private MoneyAccountMapper moneyAccountMapper;
  63. @Autowired
  64. private AccountItemMapper accountItemMapper;
  65. @Autowired
  66. private MoneyAccountService moneyAccountService;
  67. @Autowired
  68. private MoneyAccountItemMapper moneyAccountItemMapper;
  69. @Autowired
  70. private RecPayHandleItemMapper recPayHandleItemMapper;
  71. @Autowired
  72. private RecPayMapper recPayMapper;
  73. @Autowired
  74. private RecPayHandleItemService recPayHandleItemService;
  75. @Autowired
  76. private ReceiptService receiptService;
  77. @Autowired
  78. private RecPayItemService recPayItemService;
  79. @Autowired
  80. private RecPayItemMapper recPayItemMapper;
  81. @Autowired
  82. private RecPayItemConvert recPayItemConvert;
  83. @Autowired
  84. private OtherReceivableConvert otherReceivableConvert;
  85. @Autowired
  86. private OtherReceivableItemConvert otherReceivableItemConvert;
  87. /**
  88. * @desc : 条件查询
  89. * @author : 付斌
  90. * @date : 2023/1/9 10:40
  91. */
  92. @Pagination
  93. public ResponseResultVO<PageList<OtherReceivableResponse>> selectByCond(OtherReceivableQuery otherReceivableQuery) {
  94. return super.mergeListWithCount(otherReceivableQuery, otherReceivableMapper.selectByCond(otherReceivableQuery),
  95. otherReceivableMapper.countByCond(otherReceivableQuery));
  96. }
  97. /**
  98. * @desc : 查询明细(货物、收款、附件)
  99. * @author : 付斌
  100. * @date : 2024-02-28 13:25
  101. */
  102. @Pagination
  103. public ResponseResultVO<Map<String, Object>> selectOtherReceivableInfoById(String id) {
  104. Map<String, Object> result = new HashMap<>();
  105. // 商品明细
  106. List<OtherReceivableItemResponse> otherReceivableItem = otherReceivableItemMapper.selectByCond(new OtherReceivableItemQuery().setReceivableId(id));
  107. result.put("otherReceivableItem", otherReceivableItem);
  108. // 附件
  109. return ResponseResultUtil.success(result);
  110. }
  111. /**
  112. * @desc : 新建方法
  113. * @author : 付斌
  114. * @date : 2023/1/9 10:49
  115. */
  116. @Transactional(
  117. rollbackFor = {Exception.class}
  118. )
  119. public ResponseResultVO<?> insert(OtherReceivableVO otherReceivableVO) {
  120. // 收款金额不能大于收入金额
  121. if(otherReceivableVO.getSumAmtRec().compareTo(otherReceivableVO.getSumAmtReceivable()) == 1){
  122. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RECEIVABLE_NO_LESS_RECEIPT.getMessage());
  123. }
  124. // 获取单号
  125. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.OTHERRECEIVABLE.getName(), false);
  126. otherReceivableVO.setReceivableId(codeMap.get("outId").toString()).setReceivableNo(codeMap.get("outNote").toString())
  127. .setObjectType(Constant.ObjectType.CUS.getName());
  128. // 转化实体
  129. OtherReceivable otherReceivable = otherReceivableConvert.convertToPo(otherReceivableVO);
  130. // 总单保存
  131. super.insert(otherReceivable);
  132. // 明细保存
  133. if (otherReceivableVO.getItemList() != null && otherReceivableVO.getItemList().size() > 0) {
  134. for (OtherReceivableItemVO otherReceivableItemVO : otherReceivableVO.getItemList()) {
  135. OtherReceivableItem otherReceivableItem = otherReceivableItemConvert.convertToPo(otherReceivableItemVO);
  136. otherReceivableItem.setReceivableId(otherReceivable.getReceivableId()).setCpId(otherReceivable.getCpId());
  137. otherReceivableItemMapper.insert(otherReceivableItem);
  138. }
  139. }
  140. // 应收记账
  141. accountService.accReceivable(otherReceivable.getReceivableId(), Constant.InventoryDocCode.OTHER_RECEIVABLE.getTableName());
  142. // 如果有收款,再做应收付款
  143. if (otherReceivableVO.getReceiptList() != null && otherReceivableVO.getReceiptList().size() > 0) {
  144. /*********************************** 收款的处理 begin ************************************/
  145. RecPay recPay = new RecPay();
  146. // 获取单号
  147. codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  148. recPay.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString())
  149. .setRpType(Constant.RpType.SHOU_KUAN.getName()).setObjectId(otherReceivableVO.getObjectId())
  150. .setOrgId(otherReceivableVO.getOrgId()).setStaffId(otherReceivableVO.getStaffId())
  151. .setSumAmtRec(otherReceivableVO.getSumAmtRec()).setSumAmtReceivableHandle(otherReceivableVO.getSumAmtRec())
  152. .setAccDate(otherReceivableVO.getAccDate()).setBiznisType(Constant.InventoryDocCode.OTHER_RECEIVABLE.getTableName())
  153. .setBiznisId(otherReceivableVO.getReceivableId()).setBiznisNo(otherReceivableVO.getReceivableNo())
  154. .setMakeStaff(otherReceivableVO.getMakeStaff()).setCpId(otherReceivableVO.getCpId());
  155. // 总单保存
  156. recPayMapper.insert(recPay);
  157. for (RecPayItemVO recPayItemVO : otherReceivableVO.getReceiptList()) {
  158. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  159. recPayItem.setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  160. recPayItemMapper.insert(recPayItem);
  161. // 插入账款明细
  162. AccountItem accountItem = new AccountItem();
  163. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  164. .setObjectId(recPay.getObjectId()).setOrgId(recPay.getOrgId()).setStaffId(recPay.getStaffId())
  165. .setAccDate(recPay.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  166. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPay.getRpNo())
  167. .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId());
  168. accountItemMapper.insert(accountItem);
  169. // 更新收款单上的账款明细Id
  170. RecPayItem recPayItemUpdate = new RecPayItem();
  171. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  172. recPayItemService.updateByUuid(recPayItemUpdate);
  173. // 插入资金流水
  174. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  175. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  176. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPay.getAccDate())
  177. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  178. moneyAccountItemMapper.insert(moneyAccountItem);
  179. // 更新资金账户
  180. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  181. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  182. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  183. .setMacId(moneyAccountForUpdate.getMacId());
  184. moneyAccountService.updateByUuid(moneyAccountUpdate);
  185. }
  186. // 插入账款总表
  187. Account accountForUpdate = accountService.getCusAccountForUpdate(recPay.getObjectId());
  188. // 更新账款总表上收款的相关字段
  189. Account accountUpdate = new Account();
  190. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPay.getSumAmtRec()))// 总收款金额
  191. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPay.getSumAmtRec()))// 可退金额
  192. .setObjectId(accountForUpdate.getObjectId());
  193. accountService.updateByUuid(accountUpdate);
  194. /*********************************** 收款的处理 end ************************************/
  195. /*********************************** 应收收款的处理 begin ************************************/
  196. OtherReceivable otherReceivableForUpdate = otherReceivableMapper.selectByIdForUpdate(otherReceivableVO.getReceivableId());
  197. // 应收收款的处理
  198. RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
  199. recPayHandleItem.setRpId(recPay.getRpId()).setAccItemId(otherReceivableForUpdate.getAccItemId())
  200. .setAmtReceivableHandle(recPay.getSumAmtRec()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  201. recPayHandleItemMapper.insert(recPayHandleItem);
  202. // 账款明细的核销金额和优惠金额
  203. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  204. AccountItem accountItemUpdate = new AccountItem();
  205. // 核销金额,超出剩余应收金额
  206. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  207. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  208. }
  209. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  210. .setAmtWaive(accountItemForUpdate.getAmtWaive())
  211. .setItemId(recPayHandleItem.getAccItemId());
  212. // 剩余金额 = 应收金额-应收收款金额
  213. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  214. accountItemService.updateByUuid(accountItemUpdate);
  215. // 更新总账上
  216. accountForUpdate = accountMapper.selectByIdForUpdate(recPay.getObjectId());
  217. accountUpdate = new Account();
  218. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
  219. .setReceivableWaive(accountForUpdate.getReceivableWaive())// 总应收优惠金额
  220. .setObjectId(accountForUpdate.getObjectId());
  221. // 剩余应收 = 总应收账款-总应收收款金额
  222. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  223. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  224. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  225. // 更新前的最后校验
  226. // 剩余应收为负数,则不能保存
  227. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  228. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  229. }
  230. // 可用金额为负数,则不能保存
  231. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  232. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  233. }
  234. accountService.updateByUuid(accountUpdate);
  235. /*********************************** 应收收款的处理 end ************************************/
  236. }
  237. return ResponseResultUtil.success();
  238. }
  239. /**
  240. * @desc : 编辑方法
  241. * @author : 付斌
  242. * @date : 2023/1/9 10:49
  243. */
  244. @Transactional(
  245. rollbackFor = {Exception.class}
  246. )
  247. public ResponseResultVO<?> update(OtherReceivableVO otherReceivableVO) {
  248. // 收款金额不能大于收入金额
  249. if(otherReceivableVO.getSumAmtRec().compareTo(otherReceivableVO.getSumAmtReceivable()) == 1){
  250. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RECEIVABLE_NO_LESS_RECEIPT.getMessage());
  251. }
  252. /*********************************** 应收收款的处理 begin ************************************/
  253. // 如果收款了,先把收款作废掉
  254. RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(otherReceivableVO.getReceivableId());
  255. if(recPayForUpdate != null && recPayForUpdate.getFlgValid()){
  256. // 查出并锁定所有应收核销明细
  257. AccountItem accountItemForUpdate;
  258. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  259. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  260. // 更新账款明细应收收款
  261. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  262. AccountItem accountItemUpdate = new AccountItem();
  263. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  264. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  265. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  266. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  267. accountItemService.updateByUuid(accountItemUpdate);
  268. // 将核销明细有效标识置为false
  269. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  270. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  271. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  272. }
  273. // 把总帐上的钱加回来
  274. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  275. Account accountUpdate = new Account();
  276. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  277. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  278. .setObjectId(accountForUpdate.getObjectId());
  279. // 剩余应收 = 总应收账款-总应收收款金额
  280. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  281. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  282. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  283. // 更新前的最后校验
  284. // 剩余应收为负数,则不能保存
  285. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  286. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  287. }
  288. // 可用金额为负数,则不能保存
  289. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  290. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  291. }
  292. accountService.updateByUuid(accountUpdate);
  293. // 将之前的明细全部删除
  294. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  295. // 需要重新计算的资金账户
  296. List<String> macList = new ArrayList<>();
  297. for (RecPayItem recPayItem : recPayItemOriginalList) {
  298. // 删除收付款明细
  299. recPayItemMapper.deleteById(recPayItem.getItemId());
  300. // 删除账款明细
  301. accountItemMapper.deleteById(recPayItem.getAccItemId());
  302. // 删除账户流水
  303. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  304. if (!macList.contains(recPayItem.getMacId())) {
  305. macList.add(recPayItem.getMacId());
  306. }
  307. }
  308. // 更新总账表的总收款额和可用额
  309. accountService.updateReceipt(recPayForUpdate.getObjectId());
  310. // 更新账户余额
  311. for (String macId : macList) {
  312. accountService.updateMac(macId);
  313. }
  314. // 作废
  315. RecPay recPayUpdate = new RecPay();
  316. recPayUpdate.setFlgValid(false).setRpId(recPayForUpdate.getRpId());
  317. receiptService.updateByUuid(recPayUpdate);
  318. }
  319. /*********************************** 应收收款的处理 end ************************************/
  320. // 先更新收款单总表,否则id又更错了
  321. OtherReceivable otherReceivable = otherReceivableConvert.convertToPo(otherReceivableVO);
  322. super.update(otherReceivable, new UpdateWrapper<OtherReceivable>().lambda().eq(OtherReceivable::getReceivableId,
  323. UUID.fromString(otherReceivable.getReceivableId())));
  324. // 应收反记账
  325. accountService.reverseReceivable(otherReceivableVO.getReceivableId(), Constant.InventoryDocCode.OTHER_RECEIVABLE.getTableName());
  326. // 将之前的明细全部删除
  327. List<OtherReceivableItem> otherReceivableItemOriginalList = otherReceivableItemMapper.selectByZIdForUpdate(otherReceivableVO.getReceivableId());
  328. for (OtherReceivableItem otherReceivableItem : otherReceivableItemOriginalList) {
  329. otherReceivableItemMapper.deleteById(otherReceivableItem.getItemId());
  330. }
  331. // 明细保存
  332. if (otherReceivableVO.getItemList() != null && otherReceivableVO.getItemList().size() > 0) {
  333. for (OtherReceivableItemVO otherReceivableItemVO : otherReceivableVO.getItemList()) {
  334. OtherReceivableItem otherReceivableItem = otherReceivableItemConvert.convertToPo(otherReceivableItemVO);
  335. otherReceivableItem.setItemId(null).setReceivableId(otherReceivableVO.getReceivableId()).setCpId(otherReceivableVO.getCpId());
  336. otherReceivableItemMapper.insert(otherReceivableItem);
  337. }
  338. }
  339. // 应收记账
  340. accountService.accReceivable(otherReceivableVO.getReceivableId(), Constant.InventoryDocCode.OTHER_RECEIVABLE.getTableName());
  341. // 如果有收款,再做应收收款
  342. if (otherReceivableVO.getReceiptList() != null && otherReceivableVO.getReceiptList().size() > 0) {
  343. /*********************************** 收款的处理 begin ************************************/
  344. RecPay recPay = new RecPay();
  345. // 获取单号
  346. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  347. recPay.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString())
  348. .setRpType(Constant.RpType.SHOU_KUAN.getName()).setObjectId(otherReceivableVO.getObjectId())
  349. .setOrgId(otherReceivableVO.getOrgId()).setStaffId(otherReceivableVO.getStaffId())
  350. .setSumAmtRec(otherReceivableVO.getSumAmtRec()).setSumAmtReceivableHandle(otherReceivableVO.getSumAmtRec())
  351. .setAccDate(otherReceivableVO.getAccDate()).setBiznisType(Constant.InventoryDocCode.OTHER_RECEIVABLE.getTableName())
  352. .setBiznisId(otherReceivableVO.getReceivableId()).setBiznisNo(otherReceivableVO.getReceivableNo())
  353. .setMakeStaff(otherReceivableVO.getMakeStaff()).setCpId(otherReceivableVO.getCpId());
  354. // 总单保存
  355. recPayMapper.insert(recPay);
  356. for (RecPayItemVO recPayItemVO : otherReceivableVO.getReceiptList()) {
  357. RecPayItem recPayItem = recPayItemConvert.convertToPo(recPayItemVO);
  358. recPayItem.setItemId(null).setRpId(recPay.getRpId()).setCpId(recPay.getCpId()).setMakeStaff(recPay.getMakeStaff()).setAccDate(recPay.getAccDate());
  359. recPayItemMapper.insert(recPayItem);
  360. // 插入账款明细
  361. AccountItem accountItem = new AccountItem();
  362. accountItem.setAccItemType(Constant.accItemType.SHOU_KUAN.getName())
  363. .setObjectId(recPay.getObjectId()).setOrgId(recPay.getOrgId()).setStaffId(recPay.getStaffId())
  364. .setAccDate(recPay.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName()).setAmtRec(recPayItem.getAmtRec())
  365. .setBiznisType("t_mac_rec_pay_item").setBiznisId(recPayItem.getItemId()).setBiznisNo(recPay.getRpNo())
  366. .setMakeStaff(recPay.getMakeStaff()).setCpId(recPay.getCpId());
  367. accountItemMapper.insert(accountItem);
  368. // 更新收款单上的账款明细Id
  369. RecPayItem recPayItemUpdate = new RecPayItem();
  370. recPayItemUpdate.setAccItemId(accountItem.getItemId()).setItemId(recPayItem.getItemId());
  371. recPayItemService.updateByUuid(recPayItemUpdate);
  372. // 插入资金流水
  373. MoneyAccountItem moneyAccountItem = new MoneyAccountItem();
  374. moneyAccountItem.setMacId(recPayItem.getMacId()).setFlowType(Constant.FlowType.SHOU_FU_KUAN.getName())
  375. .setInvoiceId(recPayItem.getItemId()).setAmtInflow(recPayItem.getAmtRec()).setAccDate(recPay.getAccDate())
  376. .setMakeStaff(recPayItem.getMakeStaff()).setCpId(recPayItem.getCpId());
  377. moneyAccountItemMapper.insert(moneyAccountItem);
  378. // 更新资金账户
  379. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(recPayItem.getMacId());
  380. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  381. moneyAccountUpdate.setBalance(moneyAccountForUpdate.getBalance().add(recPayItem.getAmtRec()))
  382. .setMacId(moneyAccountForUpdate.getMacId());
  383. moneyAccountService.updateByUuid(moneyAccountUpdate);
  384. }
  385. // 插入账款总表
  386. Account accountForUpdate = accountService.getCusAccountForUpdate(recPay.getObjectId());
  387. // 更新账款总表上收款的相关字段
  388. Account accountUpdate = new Account();
  389. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPay.getSumAmtRec()))// 总收款金额
  390. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPay.getSumAmtRec()))// 可退金额
  391. .setObjectId(accountForUpdate.getObjectId());
  392. accountService.updateByUuid(accountUpdate);
  393. /*********************************** 收款的处理 end ************************************/
  394. /*********************************** 应收收款的处理 begin ************************************/
  395. OtherReceivable otherReceivableForUpdate = otherReceivableMapper.selectByIdForUpdate(otherReceivableVO.getReceivableId());
  396. // 应收收款的处理
  397. RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
  398. recPayHandleItem.setRpId(recPay.getRpId()).setAccItemId(otherReceivableForUpdate.getAccItemId())
  399. .setAmtReceivableHandle(recPay.getSumAmtRec()).setCpId(recPay.getCpId()).setAccDate(recPay.getAccDate());
  400. recPayHandleItemMapper.insert(recPayHandleItem);
  401. // 账款明细的核销金额和优惠金额
  402. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  403. AccountItem accountItemUpdate = new AccountItem();
  404. // 核销金额,超出剩余应收金额
  405. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  406. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  407. }
  408. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  409. .setAmtWaive(accountItemForUpdate.getAmtWaive())
  410. .setItemId(recPayHandleItem.getAccItemId());
  411. // 剩余金额 = 应收金额-应收收款金额
  412. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  413. accountItemService.updateByUuid(accountItemUpdate);
  414. // 更新总账上
  415. accountForUpdate = accountMapper.selectByIdForUpdate(recPay.getObjectId());
  416. accountUpdate = new Account();
  417. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
  418. .setReceivableWaive(accountForUpdate.getReceivableWaive())// 总应收优惠金额
  419. .setObjectId(accountForUpdate.getObjectId());
  420. // 剩余应收 = 总应收账款-总应收收款金额
  421. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  422. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  423. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  424. // 更新前的最后校验
  425. // 剩余应收为负数,则不能保存
  426. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  427. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  428. }
  429. // 可用金额为负数,则不能保存
  430. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  431. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  432. }
  433. accountService.updateByUuid(accountUpdate);
  434. /*********************************** 应收收款的处理 end ************************************/
  435. }
  436. return ResponseResultUtil.success();
  437. }
  438. /**
  439. * @desc : 获取信息(编辑用)
  440. * @author : 付斌
  441. * @date : 2024-03-02 17:27
  442. */
  443. public ResponseResultVO<?> getOtherReceivableForUpdate(String id) {
  444. Map<String, Object> dataInfo = new HashMap<>();
  445. OtherReceivableResponse otherReceivableResponse = otherReceivableMapper.selectById(id);
  446. dataInfo.put("data", otherReceivableResponse);
  447. // 收入明细
  448. List<OtherReceivableItemResponse> otherReceivableItemResponseList = otherReceivableItemMapper.selectByCond(new OtherReceivableItemQuery().setReceivableId(id));
  449. dataInfo.put("dataItem", otherReceivableItemResponseList);
  450. // 收款明细
  451. RecPay recPayForUpdate = recPayMapper.selectByBiznisId(id);
  452. List<RecPayItemResponse> recPayItemResponseList = recPayItemMapper.selectByCond(new RecPayItemQuery().setRpId(recPayForUpdate.getRpId()));
  453. dataInfo.put("receiptItem", recPayItemResponseList);
  454. return ResponseResultUtil.success(dataInfo);
  455. }
  456. /**
  457. * @desc : 作废
  458. * @author : 付斌
  459. * @date : 2024-03-08 16:38
  460. */
  461. public ResponseResultVO<?> invalid(String id) {
  462. OtherReceivable otherReceivableForUpdate = otherReceivableMapper.selectByIdForUpdate(id);
  463. // 并发校验
  464. if (!otherReceivableForUpdate.getFlgValid()) {
  465. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ISFLGVALID_FALSE.getMessage());
  466. }
  467. // 如果所在月份已结账,则不能作废 todo
  468. /*********************************** 应收收款的处理 begin ************************************/
  469. RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(id);
  470. if(recPayForUpdate != null && recPayForUpdate.getFlgValid()){
  471. // 查出并锁定所有应收核销明细
  472. AccountItem accountItemForUpdate;
  473. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  474. for (RecPayHandleItem recPayHandleItemForUpdate : recPayHandleItemForUpdateList) {
  475. // 更新账款明细应收收款
  476. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItemForUpdate.getAccItemId());
  477. AccountItem accountItemUpdate = new AccountItem();
  478. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().subtract(recPayHandleItemForUpdate.getAmtReceivableHandle()))
  479. .setAmtWaive(accountItemForUpdate.getAmtWaive().subtract(recPayHandleItemForUpdate.getAmtWaive()))
  480. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  481. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()).subtract(accountItemUpdate.getAmtWaive()));
  482. accountItemService.updateByUuid(accountItemUpdate);
  483. // 将核销明细有效标识置为false
  484. RecPayHandleItem recPayHandleItemUpdate = new RecPayHandleItem();
  485. recPayHandleItemUpdate.setFlgValid(false).setItemId(recPayHandleItemForUpdate.getItemId());
  486. recPayHandleItemService.updateByUuid(recPayHandleItemUpdate);
  487. }
  488. // 把总帐上的钱加回来
  489. Account accountForUpdate = accountMapper.selectByIdForUpdate(recPayForUpdate.getObjectId());
  490. Account accountUpdate = new Account();
  491. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  492. .setReceivableWaive(accountForUpdate.getReceivableWaive().subtract(recPayForUpdate.getSumWaiveAmt()))// 总应收优惠金额
  493. .setObjectId(accountForUpdate.getObjectId());
  494. // 剩余应收 = 总应收账款-总应收收款金额
  495. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  496. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  497. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()).add(accountUpdate.getReceivableWaive()));
  498. // 更新前的最后校验
  499. // 剩余应收为负数,则不能保存
  500. if (accountUpdate.getReceivableResidue().compareTo(BigDecimal.ZERO) == -1) {
  501. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  502. }
  503. // 可用金额为负数,则不能保存
  504. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  505. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  506. }
  507. accountService.updateByUuid(accountUpdate);
  508. // 将之前的明细全部删除
  509. List<RecPayItem> recPayItemOriginalList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  510. // 需要重新计算的资金账户
  511. List<String> macList = new ArrayList<>();
  512. for (RecPayItem recPayItem : recPayItemOriginalList) {
  513. // 删除收付款明细
  514. recPayItemMapper.deleteById(recPayItem.getItemId());
  515. // 删除账款明细
  516. accountItemMapper.deleteById(recPayItem.getAccItemId());
  517. // 删除账户流水
  518. moneyAccountItemMapper.deleteByInvoiceId(recPayItem.getItemId());
  519. if (!macList.contains(recPayItem.getMacId())) {
  520. macList.add(recPayItem.getMacId());
  521. }
  522. }
  523. // 更新总账表的总收款额和可用额
  524. accountService.updateReceipt(recPayForUpdate.getObjectId());
  525. // 更新账户余额
  526. for (String macId : macList) {
  527. accountService.updateMac(macId);
  528. }
  529. // 作废
  530. RecPay recPayUpdate = new RecPay();
  531. recPayUpdate.setFlgValid(false).setRpId(recPayForUpdate.getRpId());
  532. receiptService.updateByUuid(recPayUpdate);
  533. }
  534. /*********************************** 应收收款的处理 end ************************************/
  535. // 应收反记账
  536. accountService.reverseReceivable(id, "t_mac_other_receivable");
  537. // 作废
  538. OtherReceivable otherReceivableUpdate = new OtherReceivable();
  539. otherReceivableUpdate.setFlgValid(false).setReceivableId(id);
  540. super.updateByUuid(otherReceivableUpdate);
  541. return ResponseResultUtil.success();
  542. }
  543. }