AccountService.java 40 KB

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