OtherReceivableService.java 36 KB

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