AccountService.java 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833
  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.*;
  13. import com.dk.mdm.mapper.mst.MoneyAccountItemMapper;
  14. import com.dk.mdm.mapper.mst.MoneyAccountMapper;
  15. import com.dk.mdm.mapper.sale.OrderMapper;
  16. import com.dk.mdm.model.pojo.ivt.Inbound;
  17. import com.dk.mdm.model.pojo.ivt.Outbound;
  18. import com.dk.mdm.model.pojo.mac.*;
  19. import com.dk.common.service.BaseService;
  20. import com.dk.common.mapper.BaseMapper;
  21. import com.dk.mdm.model.pojo.mst.MoneyAccount;
  22. import com.dk.mdm.model.pojo.sale.Order;
  23. import com.dk.mdm.model.query.mac.AccountItemQuery;
  24. import com.dk.mdm.model.response.mac.AccountItemResponse;
  25. import com.dk.mdm.model.response.mac.AccountResponse;
  26. import com.dk.mdm.service.common.CommonService;
  27. import com.dk.mdm.service.mst.MoneyAccountService;
  28. import org.springframework.stereotype.Service;
  29. import org.springframework.beans.factory.annotation.Autowired;
  30. import org.springframework.transaction.annotation.Transactional;
  31. import java.math.BigDecimal;
  32. import java.time.LocalDate;
  33. import java.util.ArrayList;
  34. import java.util.List;
  35. import java.util.Map;
  36. import java.util.UUID;
  37. @Service
  38. @Transactional
  39. public class AccountService extends BaseService<Account> {
  40. @Override
  41. public String getPrimaryKey() {
  42. return "object_id";
  43. }
  44. @Override
  45. public BaseMapper<Account> getRepository() {
  46. return accountMapper;
  47. }
  48. @Autowired
  49. private AccountMapper accountMapper;
  50. @Autowired
  51. private AccountItemService accountItemService;
  52. @Autowired
  53. private AccountItemMapper accountItemMapper;
  54. @Autowired
  55. private MoneyAccountService moneyAccountService;
  56. @Autowired
  57. private MoneyAccountMapper moneyAccountMapper;
  58. @Autowired
  59. private MoneyAccountItemMapper moneyAccountItemMapper;
  60. @Autowired
  61. private OutboundMapper outboundMapper;
  62. @Autowired
  63. private InboundMapper inboundMapper;
  64. @Autowired
  65. private OtherReceivableMapper otherReceivableMapper;
  66. @Autowired
  67. private OtherPayableMapper otherPayableMapper;
  68. @Autowired
  69. private OrderMapper orderMapper;
  70. @Autowired
  71. private RecPayMapper recPayMapper;
  72. @Autowired
  73. private ReceiptService receiptService;
  74. @Autowired
  75. private RecPayItemMapper recPayItemMapper;
  76. @Autowired
  77. private RecPayHandleItemMapper recPayHandleItemMapper;
  78. @Autowired
  79. private CommonService commonService;
  80. /**
  81. * @desc : 查看来源单据,总单加明细
  82. * @author : 姜永辉
  83. * @date : 2024/3/6 10:36
  84. */
  85. public ResponseResultVO selectById(String id) {
  86. //根据id查询
  87. AccountResponse accountResponse = accountMapper.selectById(id);
  88. List<AccountItemResponse> accountItemResponses = accountItemMapper.getReceivableAccountItem(new AccountItemQuery().setObjectId(id));
  89. accountResponse.setList(accountItemResponses);
  90. return ResponseResultUtil.success(accountResponse);
  91. }
  92. /**
  93. * @desc : 只查询总单, 不包含总单加明细
  94. * @author : 姜永辉
  95. * @date : 2024/3/6 10:36
  96. */
  97. public ResponseResultVO selectAccountById(String id) {
  98. //根据id查询
  99. AccountResponse accountResponse = accountMapper.selectById(id);
  100. return ResponseResultUtil.success(accountResponse);
  101. }
  102. /**
  103. * @desc : 查询明细的总数量
  104. * @author : 姜永辉
  105. * @date : 2024/3/6 10:36
  106. */
  107. public ResponseResultVO countByCond(AccountItemQuery accountItemQuery) {
  108. //根据id查询
  109. Long aLong = accountItemMapper.countByCond(accountItemQuery);
  110. return ResponseResultUtil.success(aLong);
  111. }
  112. /**
  113. * @desc : 查询应收账款明细
  114. * @author : 付斌
  115. * @date : 2024-02-28 13:25
  116. */
  117. @Pagination
  118. public ResponseResultVO<?> getReceivableAccountItem(AccountItemQuery accountItemQuery) {
  119. List<AccountItemResponse> accountItemResponse = accountItemMapper.getReceivableAccountItem(accountItemQuery);
  120. return ResponseResultUtil.success(accountItemResponse);
  121. }
  122. /**
  123. * @desc : 查询应付账款明细
  124. * @author : 付斌
  125. * @date : 2024-02-28 13:25
  126. */
  127. @Pagination
  128. public ResponseResultVO<?> getPayableAccountItem(AccountItemQuery accountItemQuery) {
  129. List<AccountItemResponse> accountItemResponse = accountItemMapper.getPayableAccountItem(accountItemQuery);
  130. return ResponseResultUtil.success(accountItemResponse);
  131. }
  132. /********************* 账款部分共通方法 begin **********************/
  133. /**
  134. * @desc : 获取账款总表,没有则新建(客户)
  135. * @author : 付斌
  136. * @date : 2024-03-23 16:32
  137. */
  138. public Account getCusAccountForUpdate(String objectId) {
  139. return getAccountForUpdate(objectId, Constant.ObjectType.CUS.getName());
  140. }
  141. /**
  142. * @desc : 获取账款总表,没有则新建(供应商)
  143. * @author : 付斌
  144. * @date : 2024-03-23 16:32
  145. */
  146. public Account getSupAccountForUpdate(String objectId) {
  147. return getAccountForUpdate(objectId, Constant.ObjectType.SUP.getName());
  148. }
  149. /**
  150. * @desc : 更新总帐上收款类字段
  151. * @author : 付斌
  152. * @date : 2024-03-22 11:08
  153. */
  154. public void updateReceipt(String objectId) {
  155. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  156. Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRec(objectId);
  157. BigDecimal sumAmtRec = new BigDecimal(mapSumAmtRecPay.get("sumAmtRec").toString());
  158. // 可退金额 = 总收款额-应收应款额+优惠金额
  159. BigDecimal sumReceiptResidue = sumAmtRec.subtract(accountForUpdate.getReceivableHandle()).add(accountForUpdate.getReceivableWaive());
  160. // 如果可退金额小于0 ,则提示余额不足
  161. if (sumReceiptResidue.compareTo(BigDecimal.ZERO) == -1) {
  162. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  163. }
  164. // 更新账款总表上的收款总额和可用金额
  165. Account accountUpdate = new Account();
  166. accountUpdate.setReceipt(sumAmtRec).setReceiptResidue(sumReceiptResidue).setObjectId(objectId);
  167. super.updateByUuid(accountUpdate);
  168. }
  169. /**
  170. * @desc : 更新总帐上收款类字段
  171. * @author : 付斌
  172. * @date : 2024-03-22 11:08
  173. */
  174. public void updateReceiptLock(String objectId) {
  175. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  176. Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtRecLock(objectId);
  177. BigDecimal sumAmtRecLock = new BigDecimal(mapSumAmtRecPay.get("sumAmtRecLock").toString());
  178. // 更新账款总表上的收款总额和可用金额
  179. Account accountUpdate = new Account();
  180. accountUpdate.setReceiptLock(sumAmtRecLock).setObjectId(accountForUpdate.getObjectId());
  181. super.updateByUuid(accountUpdate);
  182. }
  183. /**
  184. * @desc : 更新总帐上付款类字段
  185. * @author : 付斌
  186. * @date : 2024-03-22 11:08
  187. */
  188. public void updatePayment(String objectId) {
  189. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  190. Map<String, Object> mapSumAmtRecPay = accountItemMapper.getSumAmtPay(objectId);
  191. BigDecimal sumAmtPay = new BigDecimal(mapSumAmtRecPay.get("sumAmtPay").toString());
  192. // 可退金额 = 总收款额-应收应款额+优惠金额
  193. BigDecimal sumPaymentResidue = sumAmtPay.subtract(accountForUpdate.getPayableHandle()).add(accountForUpdate.getPayableWaive());
  194. // 如果可退金额小于0 ,则提示余额不足
  195. if (sumPaymentResidue.compareTo(BigDecimal.ZERO) == -1) {
  196. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  197. }
  198. // 更新账款总表上的收款总额和可用金额
  199. Account accountUpdate = new Account();
  200. accountUpdate.setPayment(sumAmtPay).setPaymentResidue(sumPaymentResidue).setObjectId(objectId);
  201. super.updateByUuid(accountUpdate);
  202. }
  203. /**
  204. * @desc : 更新资金账户余额
  205. * @author : 付斌
  206. * @date : 2024-03-22 11:08
  207. */
  208. public void updateMac(String macId) {
  209. if (macId == null) {
  210. return;
  211. }
  212. // 查询当前账户流水合计
  213. Map<String, Object> mapSumAmtInflow = moneyAccountItemMapper.getSumAmtInflow(macId);
  214. BigDecimal sumAmtInflow = new BigDecimal(mapSumAmtInflow.get("sumAmtInflow").toString());
  215. // 更新资金账户
  216. MoneyAccount moneyAccountForUpdate = moneyAccountMapper.selectByIdForUpdate(macId);
  217. // 如果账户不允许为负数
  218. if (!moneyAccountForUpdate.getFlgNegative()) {
  219. // 如果余额小于0 ,则提示余额不足
  220. if (sumAmtInflow.compareTo(BigDecimal.ZERO) == -1) {
  221. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.MAC_BALANCE_NO_LESS.getMessage());
  222. }
  223. }
  224. // 更新账款总表上的收款总额和可用金额
  225. MoneyAccount moneyAccountUpdate = new MoneyAccount();
  226. moneyAccountUpdate.setBalance(sumAmtInflow).setMacId(macId);
  227. moneyAccountService.updateByUuid(moneyAccountUpdate);
  228. }
  229. /**
  230. * @desc : 更新合同资产
  231. * @author : 付斌
  232. * @date : 2024-03-22 11:08
  233. */
  234. public void updateContractAssets(String objectId) {
  235. if (objectId == null) {
  236. return;
  237. }
  238. // 更新账款总表上的总应收账款和总剩余应收
  239. Account accountForUpdate = getCusAccountForUpdate(objectId);
  240. // 查询订单出库金额
  241. Map<String, Object> mapSumAmtOrder = accountItemMapper.getSumAmtOrder(objectId);
  242. BigDecimal sumAmtOrder = new BigDecimal(mapSumAmtOrder.get("sumAmtOrder").toString());
  243. // 更新账款总表上的收款总额和可用金额
  244. Account accountUpdate = new Account();
  245. accountUpdate.setContractAssets(sumAmtOrder).setObjectId(accountForUpdate.getObjectId());
  246. super.updateByUuid(accountUpdate);
  247. }
  248. /**
  249. * @desc : 更新采购暂估
  250. * @author : 付斌
  251. * @date : 2024-03-22 11:08
  252. */
  253. public void updatePurEstimate(String objectId) {
  254. if (objectId == null) {
  255. return;
  256. }
  257. // 更新账款总表上的总应收账款和总剩余应收
  258. Account accountForUpdate = getSupAccountForUpdate(objectId);
  259. // 查询订单出库金额
  260. Map<String, Object> mapSumAmtPur = accountItemMapper.getSumAmtPur(objectId);
  261. BigDecimal sumAmtPur = new BigDecimal(mapSumAmtPur.get("sumAmtPur").toString());
  262. // 更新账款总表上的收款总额和可用金额
  263. Account accountUpdate = new Account();
  264. accountUpdate.setPurEstimate(sumAmtPur).setObjectId(accountForUpdate.getObjectId());
  265. super.updateByUuid(accountUpdate);
  266. }
  267. /**
  268. * @desc : 应收记账
  269. * @author : 付斌
  270. * @date : 2024-03-22 11:08
  271. */
  272. public void accReceivable(String invoiceId, String biznisType) {
  273. // 账款明细
  274. AccountItem accountItemInsert = new AccountItem();
  275. // 账务对象Id
  276. String objectId = null;
  277. if ("t_psi_outbound".equals(biznisType)) {
  278. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  279. objectId = outbound.getCusId();
  280. // 当前单据已经记账
  281. if (outbound.getReceivableId() != null) {
  282. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  283. }
  284. // 账务日期不能为空
  285. if (outbound.getOutDate() == null) {
  286. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  287. }
  288. // 插入账款明细
  289. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  290. .setObjectId(objectId).setOrgId(outbound.getOrgId()).setStaffId(outbound.getStaffId())
  291. .setAccDate(outbound.getOutDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  292. .setAmtShould(outbound.getOutAmt()).setAmtResidue(outbound.getOutAmt())
  293. .setBiznisType(biznisType).setBiznisId(outbound.getOutId()).setBiznisNo(outbound.getOutNo())
  294. .setMakeStaff(outbound.getMakeStaff()).setCpId(outbound.getCpId());
  295. accountItemMapper.insert(accountItemInsert);
  296. // 更新源单上的账款明细Id
  297. LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
  298. updateWrapper.set(Outbound::getReceivableId, UUID.fromString(accountItemInsert.getItemId())).eq(Outbound::getOutId, UUID.fromString(invoiceId));
  299. outboundMapper.update(null, updateWrapper);
  300. // 更新账款总表上的总应收账款和总剩余应收
  301. Account accountForUpdate = getCusAccountForUpdate(objectId);
  302. Account accountUpdate = new Account();
  303. accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
  304. .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
  305. .setObjectId(objectId);
  306. super.updateByUuid(accountUpdate);
  307. // 如果当前出库单对应的订单全部出库了,并且订单有收款(锁定金额),则自动做应收核销
  308. // 如果是销售出库
  309. if (outbound.getOutType().equals(Constant.OutType.SALE.getName())) {
  310. Order orderForUpdate = orderMapper.selectByIdForUpdate(outbound.getFromId());
  311. // 如果当前订单已经全部出库
  312. if (orderForUpdate != null && orderForUpdate.getOutQty().compareTo(orderForUpdate.getSumQuantity()) == 0) {
  313. RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(orderForUpdate.getOrderId(),false);
  314. if (recPayForUpdate != null && recPayForUpdate.getFlgLock()) {
  315. // 先把收款单解锁
  316. RecPay recPayUpdate = new RecPay();
  317. recPayUpdate.setFlgLock(false).setRpId(recPayForUpdate.getRpId());
  318. receiptService.updateByUuid(recPayUpdate);
  319. // 账款明细解锁
  320. List<RecPayItem> recPayItemList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  321. for (RecPayItem recPayItem : recPayItemList) {
  322. AccountItem accountItemUpdate = new AccountItem();
  323. accountItemUpdate.setFlgLock(false).setItemId(recPayItem.getAccItemId());
  324. accountItemService.updateByUuid(accountItemUpdate);
  325. }
  326. // 钱进到现金池中
  327. accountUpdate = new Account();
  328. accountUpdate.setReceipt(accountForUpdate.getReceipt().add(recPayForUpdate.getSumAmtRec()))
  329. .setReceiptResidue(accountForUpdate.getReceiptResidue().add(recPayForUpdate.getSumAmtRec()))
  330. .setReceiptLock(accountForUpdate.getReceiptLock().subtract(recPayForUpdate.getSumAmtRec()))
  331. .setObjectId(objectId);
  332. super.updateByUuid(accountUpdate);
  333. // 查出当前订单需要核销的应收单据
  334. List<AccountItemResponse> accountItemResponseList = accountItemMapper.getReceivableAccountItemForUpdate(
  335. new AccountItemQuery().setObjectId(objectId).setOrderId(orderForUpdate.getOrderId()));
  336. if (accountItemResponseList.size() > 0) {
  337. // 生成核销总单
  338. RecPay recPayHandle = new RecPay();
  339. // 获取单号
  340. Map<String, Object> codeMap = commonService.getUniqueNoteCode(Constant.docNameConstant.RECPAY.getName(), false);
  341. recPayHandle.setRpId(codeMap.get("outId").toString()).setRpNo(codeMap.get("outNote").toString())
  342. .setRpType(Constant.RpType.SHOU_KUAN.getName()).setObjectId(objectId)
  343. .setOrgId(recPayForUpdate.getOrgId()).setStaffId(recPayForUpdate.getStaffId()).setAccDate(LocalDate.now())
  344. .setBiznisType(Constant.InventoryDocCode.ORDER.getTableName()).setBiznisId(recPayForUpdate.getBiznisId()).setBiznisNo(recPayForUpdate.getBiznisNo())
  345. .setMakeStaff(recPayForUpdate.getMakeStaff()).setCpId(recPayForUpdate.getCpId()).setFlgOrderHandle(true);
  346. recPayMapper.insert(recPayHandle);
  347. // 记录核销了哪些应收账
  348. List<RecPayHandleItem> receivableList = new ArrayList<>();
  349. // 锁定金额
  350. BigDecimal lockAmt = recPayForUpdate.getSumAmtRec();
  351. for (AccountItemResponse accountItemResponse : accountItemResponseList) {
  352. RecPayHandleItem recPayHandleItem = new RecPayHandleItem();
  353. recPayHandleItem.setRpId(recPayHandle.getRpId()).setCpId(recPayHandle.getCpId()).setAccDate(recPayHandle.getAccDate())
  354. .setAccItemId(accountItemResponse.getAccItemId());
  355. // 如果定金比应收金额大
  356. if (lockAmt.compareTo(accountItemResponse.getAmtResidue()) == 1) {
  357. recPayHandleItem.setAmtReceivableHandle(accountItemResponse.getAmtResidue());
  358. lockAmt = lockAmt.subtract(accountItemResponse.getAmtResidue());
  359. } else {
  360. recPayHandleItem.setAmtReceivableHandle(lockAmt);
  361. lockAmt = BigDecimal.ZERO;
  362. }
  363. recPayHandleItemMapper.insert(recPayHandleItem);
  364. receivableList.add(recPayHandleItem);
  365. // 账款明细的核销金额和优惠金额
  366. AccountItem accountItemForUpdate = accountItemMapper.selectByIdForUpdate(recPayHandleItem.getAccItemId());
  367. AccountItem accountItemUpdate = new AccountItem();
  368. // 核销金额,超出剩余应收金额
  369. if (accountItemForUpdate.getAmtResidue().compareTo(recPayHandleItem.getAmtReceivableHandle()) == -1) {
  370. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.AMT_HANDLE_NO_LESS_AMT_SHOULD.getMessage());
  371. }
  372. accountItemUpdate.setAmtHandle(accountItemForUpdate.getAmtHandle().add(recPayHandleItem.getAmtReceivableHandle()))
  373. .setItemId(recPayHandleItem.getAccItemId());
  374. // 剩余金额 = 应收金额-应收收款金额
  375. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  376. accountItemService.updateByUuid(accountItemUpdate);
  377. // 如果定金都用完了,跳出循环
  378. if (lockAmt.compareTo(BigDecimal.ZERO) == 0) {
  379. break;
  380. }
  381. }
  382. // 计算明细的核销金额,优惠金额合计
  383. RecPayHandleItem recPayHandleItem = receivableList.stream().reduce((x, y) -> {
  384. RecPayHandleItem item = new RecPayHandleItem();
  385. item.setAmtReceivableHandle(x.getAmtReceivableHandle().add(y.getAmtReceivableHandle()));
  386. return item;
  387. }).get();
  388. // 更新收款单
  389. recPayUpdate = new RecPay();
  390. recPayUpdate.setSumAmtReceivableHandle(recPayHandleItem.getAmtReceivableHandle())
  391. .setSumShouldHandle(recPayHandleItem.getAmtReceivableHandle())
  392. .setRpId(recPayHandle.getRpId());
  393. receiptService.updateByUuid(recPayUpdate);
  394. // 更新总账上
  395. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  396. accountUpdate = new Account();
  397. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().add(recPayHandleItem.getAmtReceivableHandle()))// 总应收收款金额
  398. .setObjectId(accountForUpdate.getObjectId());
  399. // 剩余应收 = 总应收账款-总应收收款金额
  400. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  401. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  402. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
  403. // 可用金额为负数,则不能保存
  404. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  405. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  406. }
  407. super.updateByUuid(accountUpdate);
  408. }
  409. }
  410. }
  411. }
  412. }
  413. // 其他收入单
  414. else if ("t_mac_other_receivable".equals(biznisType)) {
  415. OtherReceivable otherReceivable = otherReceivableMapper.selectByIdForUpdate(invoiceId);
  416. objectId = otherReceivable.getObjectId();
  417. // 当前单据已经记账
  418. if (otherReceivable.getAccItemId() != null) {
  419. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  420. }
  421. // 账务日期不能为空
  422. if (otherReceivable.getAccDate() == null) {
  423. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  424. }
  425. // 插入账款明细
  426. accountItemInsert.setAccItemType(Constant.accItemType.YING_SHOU.getName())
  427. .setObjectId(objectId).setOrgId(otherReceivable.getOrgId()).setStaffId(otherReceivable.getStaffId())
  428. .setAccDate(otherReceivable.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  429. .setAmtShould(otherReceivable.getSumAmtReceivable()).setAmtResidue(otherReceivable.getSumAmtReceivable())
  430. .setBiznisType(biznisType).setBiznisId(otherReceivable.getReceivableId()).setBiznisNo(otherReceivable.getReceivableNo())
  431. .setMakeStaff(otherReceivable.getMakeStaff()).setCpId(otherReceivable.getCpId());
  432. accountItemMapper.insert(accountItemInsert);
  433. // 更新源单上的账款明细Id
  434. LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
  435. updateWrapper.set(OtherReceivable::getAccItemId, UUID.fromString(accountItemInsert.getItemId())).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
  436. otherReceivableMapper.update(null, updateWrapper);
  437. // 更新账款总表上的总应收账款和总剩余应收
  438. Account accountForUpdate = getCusAccountForUpdate(objectId);
  439. Account accountUpdate = new Account();
  440. accountUpdate.setReceivable(accountForUpdate.getReceivable().add(accountItemInsert.getAmtShould()))
  441. .setReceivableResidue(accountForUpdate.getReceivableResidue().add(accountItemInsert.getAmtShould()))
  442. .setObjectId(objectId);
  443. super.updateByUuid(accountUpdate);
  444. }
  445. }
  446. /**
  447. * @desc : 应收反记账
  448. * @author : 付斌
  449. * @date : 2024-03-22 11:08
  450. */
  451. public void reverseReceivable(String invoiceId, String biznisType) {
  452. // 更新账款明细应收收款
  453. AccountItem accountItemForUpdate;
  454. // 账务对象Id
  455. String objectId;
  456. if ("t_psi_outbound".equals(biznisType)) {
  457. Outbound outbound = outboundMapper.selectByIdForUpdate(invoiceId);
  458. objectId = outbound.getCusId();
  459. // 当前单据已经记账
  460. if (outbound.getReceivableId() == null) {
  461. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  462. }
  463. Account accountForUpdate;
  464. Account accountUpdate;
  465. // 如果当前出库单对应的订单做了应收收款,则自动作废应收核销,并且锁定金额订单收款(锁定金额)
  466. // 如果是销售出库
  467. if (outbound.getOutType().equals(Constant.OutType.SALE.getName())) {
  468. RecPay recPayForUpdate = recPayMapper.selectByBiznisIdForUpdate(outbound.getFromId(),false); // 款
  469. RecPay recPayHandleForUpdate = recPayMapper.selectByBiznisIdForUpdate(outbound.getFromId(),true); // 账
  470. if (recPayForUpdate != null && recPayHandleForUpdate != null) {
  471. // 查出并锁定所有应收核销明细
  472. List<RecPayHandleItem> recPayHandleItemForUpdateList = recPayHandleItemMapper.selectByZIdForUpdate(recPayHandleForUpdate.getRpId());
  473. if (recPayHandleItemForUpdateList.size() > 0) {
  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. .setItemId(recPayHandleItemForUpdate.getAccItemId());
  480. accountItemUpdate.setAmtResidue(accountItemForUpdate.getAmtShould().subtract(accountItemUpdate.getAmtHandle()));
  481. accountItemService.updateByUuid(accountItemUpdate);
  482. // 删掉核销明细
  483. recPayHandleItemMapper.deleteById(recPayHandleItemForUpdate.getItemId());
  484. }
  485. // 把总帐上的钱加回来
  486. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  487. accountUpdate = new Account();
  488. accountUpdate.setReceivableHandle(accountForUpdate.getReceivableHandle().subtract(recPayHandleForUpdate.getSumAmtReceivableHandle()))// 总应收收款金额
  489. .setObjectId(objectId);
  490. // 剩余应收 = 总应收账款-总应收收款金额
  491. accountUpdate.setReceivableResidue(accountForUpdate.getReceivable().subtract(accountUpdate.getReceivableHandle()));
  492. // 可退金额 = 总收款金额-总应收收款金额+总应收优惠金额
  493. accountUpdate.setReceiptResidue(accountForUpdate.getReceipt().subtract(accountUpdate.getReceivableHandle()));
  494. // 更新前的最后校验
  495. // 可用金额为负数,则不能保存
  496. if (accountUpdate.getReceiptResidue().compareTo(BigDecimal.ZERO) == -1) {
  497. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.RESIDUE_NO_LESS.getMessage());
  498. }
  499. super.updateByUuid(accountUpdate);
  500. // 删掉收款核销单
  501. recPayMapper.deleteById(recPayHandleForUpdate.getRpId());
  502. // 先把收款单锁定
  503. RecPay recPayUpdate = new RecPay();
  504. recPayUpdate.setFlgLock(true).setRpId(recPayForUpdate.getRpId());
  505. receiptService.updateByUuid(recPayUpdate);
  506. // 账款明细解锁
  507. List<RecPayItem> recPayItemList = recPayItemMapper.selectByZIdForUpdate(recPayForUpdate.getRpId());
  508. for (RecPayItem recPayItem : recPayItemList) {
  509. AccountItem accountItemUpdate = new AccountItem();
  510. accountItemUpdate.setFlgLock(true).setItemId(recPayItem.getAccItemId());
  511. accountItemService.updateByUuid(accountItemUpdate);
  512. }
  513. // 钱进到锁定金额中
  514. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  515. accountUpdate = new Account();
  516. accountUpdate.setReceipt(accountForUpdate.getReceipt().subtract(recPayForUpdate.getSumAmtRec()))
  517. .setReceiptResidue(accountForUpdate.getReceiptResidue().subtract(recPayForUpdate.getSumAmtRec()))
  518. .setReceiptLock(accountForUpdate.getReceiptLock().add(recPayForUpdate.getSumAmtRec()))
  519. .setObjectId(objectId);
  520. super.updateByUuid(accountUpdate);
  521. }
  522. }
  523. }
  524. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(outbound.getReceivableId());
  525. // 如果核销金额不为0,说明当前单据已核销
  526. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  527. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  528. }
  529. // 将源单上的账款明细Id更为null
  530. LambdaUpdateWrapper<Outbound> updateWrapper = new LambdaUpdateWrapper<>();
  531. updateWrapper.set(Outbound::getReceivableId, null).eq(Outbound::getOutId, UUID.fromString(invoiceId));
  532. outboundMapper.update(null, updateWrapper);
  533. // 删除账款明细
  534. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  535. // 更新账款总表上的总应收账款和总剩余应收
  536. accountForUpdate = getCusAccountForUpdate(objectId);
  537. accountUpdate = new Account();
  538. accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
  539. .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
  540. .setObjectId(objectId);
  541. super.updateByUuid(accountUpdate);
  542. }
  543. // 其他收入单
  544. else if ("t_mac_other_receivable".equals(biznisType)) {
  545. OtherReceivable otherReceivable = otherReceivableMapper.selectByIdForUpdate(invoiceId);
  546. objectId = otherReceivable.getObjectId();
  547. // 当前单据已经记账
  548. if (otherReceivable.getAccItemId() == null) {
  549. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  550. }
  551. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(otherReceivable.getAccItemId());
  552. // 如果核销金额不为0,说明当前单据已核销
  553. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  554. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  555. }
  556. // 更新源单上的账款明细Id
  557. LambdaUpdateWrapper<OtherReceivable> updateWrapper = new LambdaUpdateWrapper<>();
  558. updateWrapper.set(OtherReceivable::getAccItemId, null).eq(OtherReceivable::getReceivableId, UUID.fromString(invoiceId));
  559. otherReceivableMapper.update(null, updateWrapper);
  560. // 删除账款明细
  561. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  562. // 更新账款总表上的总应收账款和总剩余应收
  563. Account accountForUpdate = getCusAccountForUpdate(objectId);
  564. Account accountUpdate = new Account();
  565. accountUpdate.setReceivable(accountForUpdate.getReceivable().subtract(accountItemForUpdate.getAmtShould()))
  566. .setReceivableResidue(accountForUpdate.getReceivableResidue().subtract(accountItemForUpdate.getAmtShould()))
  567. .setObjectId(objectId);
  568. super.updateByUuid(accountUpdate);
  569. }
  570. }
  571. /**
  572. * @desc : 应付记账
  573. * @author : 付斌
  574. * @date : 2024-03-22 11:08
  575. */
  576. public void accPayable(String invoiceId, String biznisType) {
  577. // 账款明细
  578. AccountItem accountItemInsert = new AccountItem();
  579. // 账务对象Id
  580. String objectId = null;
  581. if ("t_psi_inbound".equals(biznisType)) {
  582. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  583. objectId = inbound.getSupId();
  584. // 当前单据已经记账
  585. if (inbound.getReceivableId() != null) {
  586. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  587. }
  588. // 账务日期不能为空
  589. if (inbound.getIntoDate() == null) {
  590. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  591. }
  592. // 插入账款明细
  593. accountItemInsert.setAccItemType(Constant.accItemType.YING_FU.getName())
  594. .setObjectId(objectId).setOrgId(inbound.getOrgId()).setStaffId(inbound.getStaffId())
  595. .setAccDate(inbound.getIntoDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  596. .setAmtShould(inbound.getIntoAmt()).setAmtResidue(inbound.getIntoAmt())
  597. .setBiznisType(biznisType).setBiznisId(inbound.getIntoId()).setBiznisNo(inbound.getIntoNo())
  598. .setMakeStaff(inbound.getMakeStaff()).setCpId(inbound.getCpId());
  599. accountItemMapper.insert(accountItemInsert);
  600. // 更新源单上的账款明细Id
  601. LambdaUpdateWrapper<Inbound> updateWrapper = new LambdaUpdateWrapper<>();
  602. updateWrapper.set(Inbound::getReceivableId, UUID.fromString(accountItemInsert.getItemId())).eq(Inbound::getIntoId, UUID.fromString(invoiceId));
  603. inboundMapper.update(null, updateWrapper);
  604. }
  605. // 其他支出单
  606. else if ("".equals(biznisType)) {
  607. OtherPayable otherPayable = otherPayableMapper.selectByIdForUpdate(invoiceId);
  608. objectId = otherPayable.getObjectId();
  609. // 当前单据已经记账
  610. if (otherPayable.getAccItemId() != null) {
  611. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISACC.getMessage());
  612. }
  613. // 账务日期不能为空
  614. if (otherPayable.getAccDate() == null) {
  615. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.ACC_DATE_ISNULL.getMessage());
  616. }
  617. // 插入账款明细
  618. accountItemInsert.setAccItemType(Constant.accItemType.YING_FU.getName())
  619. .setObjectId(objectId).setOrgId(otherPayable.getOrgId()).setStaffId(otherPayable.getStaffId())
  620. .setAccDate(otherPayable.getAccDate()).setRecStatus(Constant.recStatuse.QUE_DING.getName())
  621. .setAmtShould(otherPayable.getSumAmtPayable()).setAmtResidue(otherPayable.getSumAmtPayable())
  622. .setBiznisType(biznisType).setBiznisId(otherPayable.getPayableId()).setBiznisNo(otherPayable.getPayableNo())
  623. .setMakeStaff(otherPayable.getMakeStaff()).setCpId(otherPayable.getCpId());
  624. accountItemMapper.insert(accountItemInsert);
  625. // 更新源单上的账款明细Id
  626. LambdaUpdateWrapper<OtherPayable> updateWrapper = new LambdaUpdateWrapper<>();
  627. updateWrapper.set(OtherPayable::getAccItemId, UUID.fromString(accountItemInsert.getItemId())).eq(OtherPayable::getPayableId, UUID.fromString(invoiceId));
  628. otherPayableMapper.update(null, updateWrapper);
  629. }
  630. // 更新账款总表上的总应收账款和总剩余应收
  631. Account accountForUpdate = getSupAccountForUpdate(objectId);
  632. Account accountUpdate = new Account();
  633. accountUpdate.setPayable(accountForUpdate.getPayable().add(accountItemInsert.getAmtShould()))
  634. .setPayableResidue(accountForUpdate.getPayableResidue().add(accountItemInsert.getAmtShould()))
  635. .setObjectId(objectId);
  636. super.updateByUuid(accountUpdate);
  637. }
  638. /**
  639. * @desc : 应付反记账
  640. * @author : 付斌
  641. * @date : 2024-03-22 11:08
  642. */
  643. public void reversePayable(String invoiceId, String biznisType) {
  644. // 更新账款明细应收收款
  645. AccountItem accountItemForUpdate = null;
  646. // 账务对象Id
  647. String objectId = null;
  648. if ("t_psi_inbound".equals(biznisType)) {
  649. Inbound inbound = inboundMapper.selectByIdForUpdate(invoiceId);
  650. objectId = inbound.getSupId();
  651. // 当前单据已经记账
  652. if (inbound.getReceivableId() == null) {
  653. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  654. }
  655. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(inbound.getReceivableId());
  656. // 如果核销金额不为0,说明当前单据已核销
  657. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  658. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  659. }
  660. // 将入库单上的账款明细Id更为null
  661. LambdaUpdateWrapper<Inbound> updateWrapper = new LambdaUpdateWrapper<>();
  662. updateWrapper.set(Inbound::getReceivableId, null).eq(Inbound::getIntoId, UUID.fromString(invoiceId));
  663. inboundMapper.update(null, updateWrapper);
  664. }
  665. // 其他支出单
  666. else if ("".equals(biznisType)) {
  667. OtherPayable otherPayable = otherPayableMapper.selectByIdForUpdate(invoiceId);
  668. objectId = otherPayable.getObjectId();
  669. // 当前单据已经记账
  670. if (otherPayable.getAccItemId() == null) {
  671. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISREVERSE.getMessage());
  672. }
  673. accountItemForUpdate = accountItemMapper.selectByIdForUpdate(otherPayable.getAccItemId());
  674. // 如果核销金额不为0,说明当前单据已核销
  675. if (accountItemForUpdate.getAmtHandle().compareTo(BigDecimal.ZERO) != 0) {
  676. throw new BaseBusinessException(ResponseCodeEnum.OPERATE_FAIL.getCode(), ErrorCodeEnum.CURRENT_INVOICE_ISHANDLE.getMessage());
  677. }
  678. // 更新源单上的账款明细Id
  679. LambdaUpdateWrapper<OtherPayable> updateWrapper = new LambdaUpdateWrapper<>();
  680. updateWrapper.set(OtherPayable::getAccItemId, null).eq(OtherPayable::getPayableId, UUID.fromString(invoiceId));
  681. otherPayableMapper.update(null, updateWrapper);
  682. }
  683. // 删除账款明细
  684. accountItemMapper.deleteById(accountItemForUpdate.getItemId());
  685. // 更新账款总表上的总应收账款和总剩余应收
  686. Account accountForUpdate = getSupAccountForUpdate(objectId);
  687. Account accountUpdate = new Account();
  688. accountUpdate.setPayable(accountForUpdate.getPayable().subtract(accountItemForUpdate.getAmtShould()))
  689. .setPayableResidue(accountForUpdate.getPayableResidue().subtract(accountItemForUpdate.getAmtShould()))
  690. .setObjectId(objectId);
  691. super.updateByUuid(accountUpdate);
  692. }
  693. /**
  694. * @desc : 获取应收,收款汇总(制单员权限)
  695. * @author : 周兴
  696. * @date : 2024-04-03 16:32
  697. */
  698. public ResponseResultVO<?> getReceivableAccountSum(AccountItemQuery accountItemQuery) {
  699. Map<String, Object> receivableAccountMap = accountItemMapper.getReceivableAccountSum(accountItemQuery);
  700. return ResponseResultUtil.success(receivableAccountMap);
  701. }
  702. /********************* 账款部分共通方法 end **********************/
  703. /********************* 账款部分私有方法 begin **********************/
  704. /**
  705. * @desc : 获取账款总表,没有则新建(私有方法)
  706. * @author : 付斌
  707. * @date : 2024-03-23 16:32
  708. */
  709. private Account getAccountForUpdate(String objectId, String objectType) {
  710. // 查询账款总表
  711. Account accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  712. // 没有账款对象,需要新建
  713. if (accountForUpdate == null) {
  714. accountForUpdate = new Account();
  715. accountForUpdate.setObjectId(objectId).setObjectType(objectType);
  716. accountMapper.insert(accountForUpdate);
  717. accountForUpdate = accountMapper.selectByIdForUpdate(objectId);
  718. }
  719. return accountForUpdate;
  720. }
  721. /********************* 账款部分私有方法 end **********************/
  722. }