AccountService.java 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. package com.dk.mdm.service.mac;
  2. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  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.response.ResponseCodeEnum;
  8. import com.dk.common.response.ResponseResultUtil;
  9. import com.dk.common.response.ResponseResultVO;
  10. import com.dk.mdm.mapper.ivt.InboundMapper;
  11. import com.dk.mdm.mapper.ivt.OutboundMapper;
  12. import com.dk.mdm.mapper.mac.AccountItemMapper;
  13. import com.dk.mdm.mapper.mac.OtherPayableMapper;
  14. import com.dk.mdm.mapper.mac.OtherReceivableMapper;
  15. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  16. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  17. import com.dk.mdm.model.pojo.ivt.Inbound;
  18. import com.dk.mdm.model.pojo.ivt.Outbound;
  19. import com.dk.mdm.model.pojo.mac.Account;
  20. import com.dk.mdm.mapper.mac.AccountMapper;
  21. import com.dk.common.service.BaseService;
  22. import com.dk.common.mapper.BaseMapper;
  23. import com.dk.mdm.model.pojo.mac.AccountItem;
  24. import com.dk.mdm.model.pojo.mac.OtherPayable;
  25. import com.dk.mdm.model.pojo.mac.OtherReceivable;
  26. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  27. import com.dk.mdm.model.query.mac.AccountItemQuery;
  28. import com.dk.mdm.model.response.mac.AccountItemResponse;
  29. import com.dk.mdm.model.response.mac.AccountResponse;
  30. import com.dk.mdm.service.mst.MoneyAccountService;
  31. import org.springframework.stereotype.Service;
  32. import org.springframework.beans.factory.annotation.Autowired;
  33. import org.springframework.transaction.annotation.Transactional;
  34. import java.math.BigDecimal;
  35. import java.util.List;
  36. import java.util.Map;
  37. import java.util.UUID;
  38. @Service
  39. @Transactional
  40. public class AccountService extends BaseService<Account> {
  41. @Override
  42. public String getPrimaryKey() {
  43. return "object_id";
  44. }
  45. @Override
  46. public BaseMapper<Account> getRepository() {
  47. return accountMapper;
  48. }
  49. @Autowired
  50. private AccountMapper accountMapper;
  51. @Autowired
  52. private AccountItemMapper accountItemMapper;
  53. @Autowired
  54. private MoneyAccountService moneyAccountService;
  55. @Autowired
  56. private MoneyAccountMapper moneyAccountMapper;
  57. @Autowired
  58. private MoneyAccountItemMapper moneyAccountItemMapper;
  59. @Autowired
  60. private OutboundMapper outboundMapper;
  61. @Autowired
  62. private InboundMapper inboundMapper;
  63. @Autowired
  64. private OtherReceivableMapper otherReceivableMapper;
  65. @Autowired
  66. private OtherPayableMapper otherPayableMapper;
  67. /**
  68. * @desc : 查看来源单据,总单加明细
  69. * @author : 姜永辉
  70. * @date : 2024/3/6 10:36
  71. */
  72. public ResponseResultVO selectById(String id) {
  73. //根据id查询
  74. AccountResponse accountResponse = accountMapper.selectById(id);
  75. List<AccountItemResponse> accountItemResponses = accountItemMapper.getReceivableAccountItem(new AccountItemQuery().setObjectId(id));
  76. accountResponse.setList(accountItemResponses);
  77. return ResponseResultUtil.success(accountResponse);
  78. }
  79. /**
  80. * @desc : 只查询总单, 不包含总单加明细
  81. * @author : 姜永辉
  82. * @date : 2024/3/6 10:36
  83. */
  84. public ResponseResultVO selectAccountById(String id) {
  85. //根据id查询
  86. AccountResponse accountResponse = accountMapper.selectById(id);
  87. return ResponseResultUtil.success(accountResponse);
  88. }
  89. /**
  90. * @desc : 查询明细的总数量
  91. * @author : 姜永辉
  92. * @date : 2024/3/6 10:36
  93. */
  94. public ResponseResultVO countByCond(AccountItemQuery accountItemQuery) {
  95. //根据id查询
  96. Long aLong = accountItemMapper.countByCond(accountItemQuery);
  97. return ResponseResultUtil.success(aLong);
  98. }
  99. /**
  100. * @desc : 查询应收账款明细
  101. * @author : 付斌
  102. * @date : 2024-02-28 13:25
  103. */
  104. @Pagination
  105. public ResponseResultVO<?> getReceivableAccountItem(AccountItemQuery accountItemQuery) {
  106. List<AccountItemResponse> accountItemResponse = accountItemMapper.getReceivableAccountItem(accountItemQuery);
  107. return ResponseResultUtil.success(accountItemResponse);
  108. }
  109. /**
  110. * @desc : 查询应付账款明细
  111. * @author : 付斌
  112. * @date : 2024-02-28 13:25
  113. */
  114. @Pagination
  115. public ResponseResultVO<?> getPayableAccountItem(AccountItemQuery accountItemQuery) {
  116. List<AccountItemResponse> accountItemResponse = accountItemMapper.getPayableAccountItem(accountItemQuery);
  117. return ResponseResultUtil.success(accountItemResponse);
  118. }
  119. /********************* 账款部分共通方法 begin **********************/
  120. /**
  121. * @desc : 获取账款总表,没有则新建(客户)
  122. * @author : 付斌
  123. * @date : 2024-03-23 16:32
  124. */
  125. public Account getCusAccountForUpdate(String objectId) {
  126. return getAccountForUpdate(objectId, Constant.ObjectType.CUS.getName());
  127. }
  128. /**
  129. * @desc : 获取账款总表,没有则新建(供应商)
  130. * @author : 付斌
  131. * @date : 2024-03-23 16:32
  132. */
  133. public Account getSupAccountForUpdate(String objectId) {
  134. return getAccountForUpdate(objectId, Constant.ObjectType.SUP.getName());
  135. }
  136. /**
  137. * @desc : 更新总帐上收款类字段
  138. * @author : 付斌
  139. * @date : 2024-03-22 11:08
  140. */
  141. public void updateReceipt(String objectId) {
  142. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  143. Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecPay(objectId);
  144. BigDecimal sumAmtRec = new BigDecimal(mapSumAmtRecPay.get("sumAmtRec").toString());
  145. // 可退金额 = 总收款额-应收应款额+优惠金额
  146. BigDecimal sumReceiptResidue = sumAmtRec.subtract(accountForUpdate.getReceivableHandle()).add(accountForUpdate.getReceivableWaive());
  147. // 如果可退金额小于0 ,则提示余额不足
  148. if (sumReceiptResidue.compareTo(BigDecimal.ZERO) == -1) {
  149. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  150. }
  151. // 更新账款总表上的收款总额和可用金额
  152. Account accountUpdate = new Account();
  153. accountUpdate.setReceipt(sumAmtRec).setReceiptResidue(sumReceiptResidue).setObjectId(objectId);
  154. super.updateByUuid(accountUpdate);
  155. }
  156. /**
  157. * @desc : 更新总帐上付款类字段
  158. * @author : 付斌
  159. * @date : 2024-03-22 11:08
  160. */
  161. public void updatePayment(String objectId) {
  162. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  163. Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecPay(objectId);
  164. BigDecimal sumAmtPay = new BigDecimal(mapSumAmtRecPay.get("sumAmtPay").toString());
  165. // 可退金额 = 总收款额-应收应款额+优惠金额
  166. BigDecimal sumPaymentResidue = sumAmtPay.subtract(accountForUpdate.getPayableHandle()).add(accountForUpdate.getPayableWaive());
  167. // 如果可退金额小于0 ,则提示余额不足
  168. if (sumPaymentResidue.compareTo(BigDecimal.ZERO) == -1) {
  169. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  170. }
  171. // 更新账款总表上的收款总额和可用金额
  172. Account accountUpdate = new Account();
  173. accountUpdate.setPayment(sumAmtPay).setPaymentResidue(sumPaymentResidue).setObjectId(objectId);
  174. super.updateByUuid(accountUpdate);
  175. }
  176. /**
  177. * @desc : 更新资金账户余额
  178. * @author : 付斌
  179. * @date : 2024-03-22 11:08
  180. */
  181. public void updateMac(String macId) {
  182. if(macId == null){
  183. return;
  184. }
  185. // 查询当前账户流水合计
  186. Map<String, Object> mapSumAmtInflow = moneyAccountItemMapper.getSumAmtInflow(macId);
  187. BigDecimal sumAmtInflow = new BigDecimal(mapSumAmtInflow.get("sumAmtInflow").toString());
  188. // 更新资金账户
  189. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(macId);
  190. // 如果账户不允许为负数
  191. if(!moneyAccountForUpdate.getFlgNegative()) {
  192. // 如果余额小于0 ,则提示余额不足
  193. if (sumAmtInflow.compareTo(BigDecimal.ZERO) == -1) {
  194. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  195. }
  196. }
  197. // 更新账款总表上的收款总额和可用金额
  198. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  199. moneyAccountUpdate.setBalance(sumAmtInflow).setMacId(macId);
  200. moneyAccountService.updateByUuid(moneyAccountUpdate);
  201. }
  202. /**
  203. * @desc : 应收记账
  204. * @author : 付斌
  205. * @date : 2024-03-22 11:08
  206. */
  207. public void accReceivable(String invoiceId, String biznisType) {
  208. // 账款明细
  209. AccountItem accountItemInsert = new AccountItem();
  210. // 账务对象Id
  211. String objectId = null;
  212. if ("t_psi_outbound".equals(biznisType)) {
  213. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  214. objectId = outbound.getCusId();
  215. // 当前单据已经记账
  216. if (outbound.getReceivableId() != null) {
  217. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  218. }
  219. // 账务日期不能为空
  220. if (outbound.getOutDate() == null) {
  221. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  222. }
  223. // 插入账款明细
  224. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  225. .setObjectId(objectId).setOrgId(outbound.getOrgId()).setStaffId(outbound.getStaffId())
  226. .setAccDate(outbound.getOutDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  227. .setAmtShould(outbound.getOutAmt()).setAmtResidue(outbound.getOutAmt())
  228. .setBiznisType(biznisType).setBiznisId(outbound.getOutId()).setBiznisNo(outbound.getOutNo())
  229. .setMakeStaff(outbound.getMakeStaff()).setCpId(outbound.getCpId());
  230. accountItemMapper.insert(accountItemInsert);
  231. // 更新源单上的账款明细Id
  232. LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
  233. updateWrapper.set(Outbound::getReceivableId, UUID.fromString(accountItemInsert.getItemId())).eq(Outbound::getOutId, UUID.fromString(invoiceId));
  234. outboundMapper.update(null, updateWrapper);
  235. }
  236. // 其他收入单
  237. else if ("t_mac_other_receivable".equals(biznisType)) {
  238. OtherReceivable otherReceivable = otherReceivableMapper.selectByIdForUpdate(invoiceId);
  239. objectId = otherReceivable.getObjectId();
  240. // 当前单据已经记账
  241. if (otherReceivable.getAccItemId() != null) {
  242. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  243. }
  244. // 账务日期不能为空
  245. if (otherReceivable.getAccDate() == null) {
  246. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  247. }
  248. // 插入账款明细
  249. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  250. .setObjectId(objectId).setOrgId(otherReceivable.getOrgId()).setStaffId(otherReceivable.getStaffId())
  251. .setAccDate(otherReceivable.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  252. .setAmtShould(otherReceivable.getSumAmtReceivable()).setAmtResidue(otherReceivable.getSumAmtReceivable())
  253. .setBiznisType(biznisType).setBiznisId(otherReceivable.getReceivableId()).setBiznisNo(otherReceivable.getReceivableNo())
  254. .setMakeStaff(otherReceivable.getMakeStaff()).setCpId(otherReceivable.getCpId());
  255. accountItemMapper.insert(accountItemInsert);
  256. // 更新源单上的账款明细Id
  257. LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
  258. updateWrapper.set(OtherReceivable::getAccItemId, UUID.fromString(accountItemInsert.getItemId())).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
  259. otherReceivableMapper.update(null, updateWrapper);
  260. }
  261. // 更新账款总表上的总应收账款和总剩余应收
  262. Account accountForUpdate = getCusAccountForUpdate(objectId);
  263. Account accountUpdate = new Account();
  264. accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
  265. .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
  266. .setObjectId(objectId);
  267. super.updateByUuid(accountUpdate);
  268. }
  269. /**
  270. * @desc : 应收反记账
  271. * @author : 付斌
  272. * @date : 2024-03-22 11:08
  273. */
  274. public void reverseReceivable(String invoiceId, String biznisType) {
  275. // 更新账款明细应收收款
  276. AccountItem accountItemForUpdate = null;
  277. // 账务对象Id
  278. String objectId = null;
  279. if ("t_psi_outbound".equals(biznisType)) {
  280. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  281. objectId = outbound.getCusId();
  282. // 当前单据已经记账
  283. if (outbound.getReceivableId() == null) {
  284. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  285. }
  286. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(outbound.getReceivableId());
  287. // 如果核销金额不为0,说明当前单据已核销
  288. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  289. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  290. }
  291. // 将源单上的账款明细Id更为null
  292. LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
  293. updateWrapper.set(Outbound::getReceivableId, null).eq(Outbound::getOutId, UUID.fromString(invoiceId));
  294. outboundMapper.update(null, updateWrapper);
  295. }
  296. // 其他收入单
  297. else if ("t_mac_other_receivable".equals(biznisType)) {
  298. OtherReceivable otherReceivable = otherReceivableMapper.selectByIdForUpdate(invoiceId);
  299. objectId = otherReceivable.getObjectId();
  300. // 当前单据已经记账
  301. if (otherReceivable.getAccItemId() == null) {
  302. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  303. }
  304. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(otherReceivable.getAccItemId());
  305. // 如果核销金额不为0,说明当前单据已核销
  306. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  307. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  308. }
  309. // 更新源单上的账款明细Id
  310. LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
  311. updateWrapper.set(OtherReceivable::getAccItemId, null).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
  312. otherReceivableMapper.update(null, updateWrapper);
  313. }
  314. // 删除账款明细
  315. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  316. // 更新账款总表上的总应收账款和总剩余应收
  317. Account accountForUpdate = getCusAccountForUpdate(objectId);
  318. Account accountUpdate = new Account();
  319. accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
  320. .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
  321. .setObjectId(objectId);
  322. super.updateByUuid(accountUpdate);
  323. }
  324. /**
  325. * @desc : 应付记账
  326. * @author : 付斌
  327. * @date : 2024-03-22 11:08
  328. */
  329. public void accPayable(String invoiceId, String biznisType) {
  330. // 账款明细
  331. AccountItem accountItemInsert = new AccountItem();
  332. // 账务对象Id
  333. String objectId = null;
  334. if ("t_psi_inbound".equals(biznisType)) {
  335. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  336. objectId = inbound.getSupId();
  337. // 当前单据已经记账
  338. if (inbound.getReceivableId() != null) {
  339. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  340. }
  341. // 账务日期不能为空
  342. if (inbound.getIntoDate() == null) {
  343. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  344. }
  345. // 插入账款明细
  346. accountItemInsert.setAccItemType(Constant.accItemType.YING_FU.getName())
  347. .setObjectId(objectId).setOrgId(inbound.getOrgId()).setStaffId(inbound.getStaffId())
  348. .setAccDate(inbound.getIntoDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  349. .setAmtShould(inbound.getIntoAmt()).setAmtResidue(inbound.getIntoAmt())
  350. .setBiznisType(biznisType).setBiznisId(inbound.getIntoId()).setBiznisNo(inbound.getIntoNo())
  351. .setMakeStaff(inbound.getMakeStaff()).setCpId(inbound.getCpId());
  352. accountItemMapper.insert(accountItemInsert);
  353. // 更新源单上的账款明细Id
  354. LambdaUpdateWrapper<Inbound> updateWrapper = new LambdaUpdateWrapper<>();
  355. updateWrapper.set(Inbound::getReceivableId, UUID.fromString(accountItemInsert.getItemId())).eq(Inbound::getIntoId, UUID.fromString(invoiceId));
  356. inboundMapper.update(null, updateWrapper);
  357. }
  358. // 其他支出单
  359. else if ("t_mac_other_payable".equals(biznisType)) {
  360. OtherPayable otherPayable = otherPayableMapper.selectByIdForUpdate(invoiceId);
  361. objectId = otherPayable.getObjectId();
  362. // 当前单据已经记账
  363. if (otherPayable.getAccItemId() != null) {
  364. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  365. }
  366. // 账务日期不能为空
  367. if (otherPayable.getAccDate() == null) {
  368. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  369. }
  370. // 插入账款明细
  371. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  372. .setObjectId(objectId).setOrgId(otherPayable.getOrgId()).setStaffId(otherPayable.getStaffId())
  373. .setAccDate(otherPayable.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  374. .setAmtShould(otherPayable.getSumAmtPayable()).setAmtResidue(otherPayable.getSumAmtPayable())
  375. .setBiznisType(biznisType).setBiznisId(otherPayable.getPayableId()).setBiznisNo(otherPayable.getPayableNo())
  376. .setMakeStaff(otherPayable.getMakeStaff()).setCpId(otherPayable.getCpId());
  377. accountItemMapper.insert(accountItemInsert);
  378. // 更新源单上的账款明细Id
  379. LambdaUpdateWrapper<OtherPayable> updateWrapper = new LambdaUpdateWrapper<>();
  380. updateWrapper.set(OtherPayable::getAccItemId, UUID.fromString(accountItemInsert.getItemId())).eq(OtherPayable::getPayableId, UUID.fromString(invoiceId));
  381. otherPayableMapper.update(null, updateWrapper);
  382. }
  383. // 更新账款总表上的总应收账款和总剩余应收
  384. Account accountForUpdate = getSupAccountForUpdate(objectId);
  385. Account accountUpdate = new Account();
  386. accountUpdate.setPayable(accountForUpdate.getPayable().add(accountItemInsert.getAmtShould()))
  387. .setPayableResidue(accountForUpdate.getPayableResidue().add(accountItemInsert.getAmtShould()))
  388. .setObjectId(objectId);
  389. super.updateByUuid(accountUpdate);
  390. }
  391. /**
  392. * @desc : 应付反记账
  393. * @author : 付斌
  394. * @date : 2024-03-22 11:08
  395. */
  396. public void reversePayable(String invoiceId, String biznisType) {
  397. // 更新账款明细应收收款
  398. AccountItem accountItemForUpdate = null;
  399. // 账务对象Id
  400. String objectId = null;
  401. if ("t_psi_inbound".equals(biznisType)) {
  402. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  403. objectId = inbound.getSupId();
  404. // 当前单据已经记账
  405. if (inbound.getReceivableId() == null) {
  406. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  407. }
  408. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(inbound.getReceivableId());
  409. // 如果核销金额不为0,说明当前单据已核销
  410. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  411. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  412. }
  413. // 将入库单上的账款明细Id更为null
  414. LambdaUpdateWrapper<Inbound> updateWrapper = new LambdaUpdateWrapper<>();
  415. updateWrapper.set(Inbound::getReceivableId, null).eq(Inbound::getIntoId, UUID.fromString(invoiceId));
  416. inboundMapper.update(null, updateWrapper);
  417. }
  418. // 其他支出单
  419. else if ("t_mac_other_payable".equals(biznisType)) {
  420. OtherPayable otherPayable = otherPayableMapper.selectByIdForUpdate(invoiceId);
  421. objectId = otherPayable.getObjectId();
  422. // 当前单据已经记账
  423. if (otherPayable.getAccItemId() == null) {
  424. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  425. }
  426. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(otherPayable.getAccItemId());
  427. // 如果核销金额不为0,说明当前单据已核销
  428. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  429. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  430. }
  431. // 更新源单上的账款明细Id
  432. LambdaUpdateWrapper<OtherPayable> updateWrapper = new LambdaUpdateWrapper<>();
  433. updateWrapper.set(OtherPayable::getAccItemId, null).eq(OtherPayable::getPayableId, UUID.fromString(invoiceId));
  434. otherPayableMapper.update(null, updateWrapper);
  435. }
  436. // 删除账款明细
  437. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  438. // 更新账款总表上的总应收账款和总剩余应收
  439. Account accountForUpdate = getSupAccountForUpdate(objectId);
  440. Account accountUpdate = new Account();
  441. accountUpdate.setPayable(accountForUpdate.getPayable().subtract(accountItemForUpdate.getAmtShould()))
  442. .setPayableResidue(accountForUpdate.getPayableResidue().subtract(accountItemForUpdate.getAmtShould()))
  443. .setObjectId(objectId);
  444. super.updateByUuid(accountUpdate);
  445. }
  446. /**
  447. * @desc : 获取应收,收款汇总(制单员权限)
  448. * @author : 中兴
  449. * @date : 2024-04-03 16:32
  450. */
  451. public ResponseResultVO<?> getReceivableAccountSum(AccountItemQuery accountItemQuery){
  452. Map<String, Object> receivableAccountMap= accountItemMapper.getReceivableAccountSum(accountItemQuery);
  453. return ResponseResultUtil.success(receivableAccountMap);
  454. }
  455. /********************* 账款部分共通方法 end **********************/
  456. /********************* 账款部分私有方法 begin **********************/
  457. /**
  458. * @desc : 获取账款总表,没有则新建(私有方法)
  459. * @author : 付斌
  460. * @date : 2024-03-23 16:32
  461. */
  462. private Account getAccountForUpdate(String objectId, String objectType) {
  463. // 查询账款总表
  464. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  465. // 没有账款对象,需要新建
  466. if (accountForUpdate == null) {
  467. accountForUpdate = new Account();
  468. accountForUpdate.setObjectId(objectId).setObjectType(objectType);
  469. accountMapper.insert(accountForUpdate);
  470. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  471. }
  472. return accountForUpdate;
  473. }
  474. /********************* 账款部分私有方法 end **********************/
  475. }