ExpenseAccountServiceImpl.java 85 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915
  1. package com.goafanti.expenseAccount.service.impl;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.goafanti.admin.bo.AdminListBo;
  4. import com.goafanti.admin.service.DepartmentService;
  5. import com.goafanti.common.bo.AdminNoticeBo;
  6. import com.goafanti.common.bo.EmailBo;
  7. import com.goafanti.common.constant.AFTConstants;
  8. import com.goafanti.common.dao.*;
  9. import com.goafanti.common.enums.NoticeStatus;
  10. import com.goafanti.common.enums.NoticeTypes;
  11. import com.goafanti.common.error.BusinessException;
  12. import com.goafanti.common.model.*;
  13. import com.goafanti.common.utils.*;
  14. import com.goafanti.common.utils.excel.FileUtils;
  15. import com.goafanti.common.utils.excel.NewExcelUtil;
  16. import com.goafanti.common.utils.pdf.PDFUtils;
  17. import com.goafanti.core.mybatis.BaseMybatisDao;
  18. import com.goafanti.core.mybatis.JDBCIdGenerator;
  19. import com.goafanti.core.mybatis.page.Pagination;
  20. import com.goafanti.core.shiro.token.TokenManager;
  21. import com.goafanti.expenseAccount.Enums.EAProcessStatus;
  22. import com.goafanti.expenseAccount.Enums.EATypes;
  23. import com.goafanti.expenseAccount.Enums.EAsecondaryTypes;
  24. import com.goafanti.expenseAccount.bo.*;
  25. import com.goafanti.expenseAccount.service.ExpenseAccountService;
  26. import com.goafanti.order.service.OrderNewService;
  27. import com.goafanti.organization.bo.OrganizationListOut;
  28. import com.itextpdf.text.Document;
  29. import com.itextpdf.text.DocumentException;
  30. import com.itextpdf.text.Paragraph;
  31. import com.itextpdf.text.Phrase;
  32. import com.itextpdf.text.pdf.PdfPCell;
  33. import com.itextpdf.text.pdf.PdfPRow;
  34. import com.itextpdf.text.pdf.PdfPTable;
  35. import com.itextpdf.text.pdf.PdfWriter;
  36. import org.springframework.beans.factory.annotation.Autowired;
  37. import org.springframework.beans.factory.annotation.Value;
  38. import org.springframework.cache.annotation.CacheEvict;
  39. import org.springframework.cache.annotation.Cacheable;
  40. import org.springframework.http.MediaType;
  41. import org.springframework.stereotype.Service;
  42. import org.springframework.transaction.annotation.Transactional;
  43. import javax.servlet.http.HttpServletResponse;
  44. import java.io.FileOutputStream;
  45. import java.io.IOException;
  46. import java.math.BigDecimal;
  47. import java.util.*;
  48. import java.util.stream.Collectors;
  49. @Service
  50. public class ExpenseAccountServiceImpl extends BaseMybatisDao<ExpenseAccountMapper> implements ExpenseAccountService {
  51. @Autowired
  52. private AdminMapper adminMapper;
  53. @Autowired
  54. private ExpenseAccountMapper expenseAccountMapper;
  55. @Autowired
  56. private PublicReleaseMapper publicReleaseMapper;
  57. @Autowired
  58. private ExpenseAccountLogMapper expenseAccountLogMapper;
  59. @Autowired
  60. private AsyncUtils asyncUtils;
  61. @Autowired
  62. private WeChatUtils weChatUtils;
  63. @Autowired
  64. private ExpenseAccountDetailsMapper expenseAccountDetailsMapper;
  65. @Autowired
  66. private ExpenseAccountPrivateMapper expenseAccountPrivateMapper;
  67. @Autowired
  68. private ExpenseAccountExamineMapper expenseAccountExamineMapper;
  69. @Autowired
  70. private JDBCIdGenerator idGenerator;
  71. @Autowired
  72. private FinanceCountMapper financeCountMapper;
  73. @Autowired
  74. private ExpenseRelationshipMapper expenseRelationshipMapper;
  75. @Autowired
  76. private DepartmentService departmentService;
  77. @Autowired
  78. private DepartmentMapper departmentMapper;
  79. @Autowired
  80. private ExpenseConfigDetailsMapper expenseConfigDetailsMapper;
  81. @Autowired
  82. private TOrderPublicReleaseCountMapper tOrderPublicReleaseCountMapper;
  83. @Autowired
  84. private OrderNewService orderNewService;
  85. @Value(value = "${upload.path}")
  86. private final String uploadPath = null;
  87. @Override
  88. public Integer add(InputExpenseAccount in) {
  89. //如果关联公出,获取公出信息
  90. if (in.getPrid()!=null){
  91. PublicRelease pr = publicReleaseMapper.selectByPrimaryKey(in.getPrid());
  92. in.setReleaseStart(pr.getReleaseStart());
  93. in.setReleaseEnd(pr.getReleaseEnd());
  94. in.setDistrictName(pr.getDistrictName());
  95. in.setUserNames(pr.getUserNames());
  96. in.setDuration(pr.getDuration());
  97. if (in.getOrderNo()==null)in.setOrderNo(pr.getOrderNo());
  98. }
  99. //判断报销类型,0=固定费用,1=报销到订单
  100. if (in.getOrderNo()==null){
  101. in.setTargetType(0);
  102. }else {
  103. in.setTargetType(1);
  104. }
  105. if (in.getAid()==null)in.setAid(TokenManager.getAdminId());
  106. Admin admin = adminMapper.selectByPrimaryKey(in.getAid());
  107. in.setAname(admin.getName());
  108. in.setApplyDep(admin.getDepartmentId());
  109. in.setPayDep(admin.getDepartmentId());
  110. expenseAccountMapper.insertSelective(in);
  111. return in.getId();
  112. }
  113. /**
  114. * 新增报销审核流程
  115. */
  116. private void addExpenseAccountExamine(Integer id,ExpenseAccount ea) {
  117. List<ExpenseAccountExamine> list=new ArrayList<>();
  118. expenseAccountExamineMapper.deleteByEaid(ea.getId());
  119. String aid = TokenManager.getAdminId();
  120. OrganizationListOut dep = departmentMapper.selectAllById(ea.getApplyDep());
  121. AdminNoticeBo a = adminMapper.selectMyAndSuperBySuperId(aid);
  122. //暂时只发起到财务
  123. for (int i=1;i<4;i++){
  124. ExpenseAccountExamine eae=new ExpenseAccountExamine(id,i);
  125. if (i==1){
  126. if (a.getExpenseSuperExamine()==1){
  127. //新增一级
  128. String examineId=null;
  129. String examineName=null;
  130. //正常用上级,没有的情况下就是自己
  131. if (a.getSuperId()!=null){
  132. examineId=a.getSuperId();
  133. examineName=a.getSuperName();
  134. }else {
  135. examineId=a.getId();
  136. examineName=a.getName();
  137. }
  138. eae.setAuditor(examineId);
  139. eae.setAuditorname(examineName);
  140. ea.setExamineName(examineName);
  141. }
  142. if(a.getExpenseSuperExamine()==1) {
  143. eae.setStatus(0);
  144. } else {
  145. eae.setStatus(1);
  146. }
  147. }else if (i==2){
  148. if (StringUtils.isBlank(dep.getManagerName())){
  149. throw new BusinessException("报销申请部门[管理员]不存在。");
  150. }
  151. eae.setAuditor(dep.getManagerId());
  152. eae.setAuditorname(dep.getManagerName());
  153. }else if (i==3){
  154. if (StringUtils.isBlank(dep.getExpenseFinanceName()))throw new BusinessException("报销部门财务审核人不存在。");
  155. eae.setAuditor(dep.getExpenseFinanceId());
  156. eae.setAuditorname(dep.getExpenseFinanceName());
  157. }
  158. if (StringUtils.isNotEmpty(eae.getAuditor())){
  159. if (eae.getAuditor().equals(aid))eae.setStatus(1);
  160. else eae.setStatus(0);
  161. list.add(eae);
  162. }
  163. }
  164. expenseAccountExamineMapper.insertBatch(list);
  165. }
  166. /**
  167. * 新增报销日志
  168. * @param eaid 报销编号
  169. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  170. * @param str 备注
  171. */
  172. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str) {
  173. addExpenseAccountLog(eaid,status,processStatus,aid,str,null);
  174. }
  175. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str,Date date){
  176. addExpenseAccountLog(eaid,status,processStatus,aid,str,date,null);
  177. }
  178. /**
  179. * 新增报销日志
  180. * @param eaid 报销编号
  181. * @param status 状态 0发起 1通过 2完成 3驳回 4修改
  182. * @param str 备注
  183. */
  184. private void addExpenseAccountLog(Integer eaid, Integer status,Integer processStatus,String aid, String str,Date date,Integer showStatus) {
  185. ExpenseAccountLog eaLog=new ExpenseAccountLog();
  186. eaLog.setEaid(eaid);
  187. eaLog.setStatus(status);
  188. eaLog.setRemarks(str);
  189. eaLog.setProcessStatus(processStatus);
  190. eaLog.setAuditor(aid);
  191. if (showStatus!=null){
  192. eaLog.setShowStatus(showStatus);
  193. }else {
  194. eaLog.setShowStatus(0);
  195. }
  196. if (date !=null)eaLog.setCreateTime(date);
  197. expenseAccountLogMapper.insertSelective(eaLog);
  198. }
  199. /**
  200. * 发起、驳回发起流程切换和日志站内信发送
  201. */
  202. private void addExpenseAccountLogAndNoticEmail(ExpenseAccount in, Integer status, String str) {
  203. String aid=TokenManager.getAdminId();
  204. addExpenseAccountLog(in.getId(),status,in.getProcessStatus(),aid,str);
  205. if (status==0)in.setProcessStatus(1);
  206. Admin admin = adminMapper.selectByPrimaryKey(aid);
  207. if (admin.getExpenseSuperExamine()==0){
  208. String str2=String.format("未设置[%s]审核。",EAProcessStatus.getDesc(in.getProcessStatus()));
  209. Date date=new Date();
  210. date.setTime(date.getTime()+1000L);
  211. addExpenseAccountLog(in.getId(),1,in.getProcessStatus(),aid,str2,date,2);
  212. in.setProcessStatus(in.getProcessStatus()+1);
  213. }else {
  214. if (admin.getSuperiorId()==null||admin.getId().equals(admin.getSuperiorId())){
  215. String str2=String.format("%s跳过[%s]。",admin.getSuperiorId()==null?"发起人无上级":"发起人与上级同人",EAProcessStatus.getDesc(in.getProcessStatus()));
  216. Date date=new Date();
  217. date.setTime(date.getTime()+1000L);
  218. addExpenseAccountLog(in.getId(),1,in.getProcessStatus(),aid,str2,date,2);
  219. in.setProcessStatus(in.getProcessStatus()+1);
  220. }
  221. }
  222. createExamineByProcess(in);
  223. }
  224. /**
  225. * 判断审核流程与发送日志与邮件
  226. */
  227. private void createExamineByProcess(ExpenseAccount in) {
  228. //审核流程推送到最新
  229. AdminListBo admin = adminMapper.getDeptNameByAid(in.getAid());
  230. OrganizationListOut dep = departmentMapper.selectAllById(in.getApplyDep());
  231. List<ExpenseConfigDetails> configDetailsList = expenseConfigDetailsMapper.selectByDepId(in.getApplyDep());
  232. if (in.getApproval()==null)in.setApproval(0);
  233. examineByprocess(in,admin,dep,configDetailsList);
  234. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(in.getId(), in.getProcessStatus());
  235. List<Admin> aids=new ArrayList<>();
  236. StringBuffer aname =new StringBuffer();
  237. for (ExpenseAccountExamine e : list) {
  238. if (e.getProcessStatus()==in.getProcessStatus()&&e.getStatus()==0){
  239. Admin ad = adminMapper.selectByPrimaryKey(e.getAuditor());
  240. aids.add(ad);
  241. aname=aname.append(ad.getName()).append(",");
  242. }
  243. }
  244. in.setExamineName(aname.substring(0,aname.length()-1));
  245. StringBuffer str=new StringBuffer().append("编号[").append(in.getCheckNo()).append("]需要审核").append(",请查看并审核。");
  246. addNoticeAndEmail(in, EAProcessStatus.SJSH.getCode(),str.toString(), admin.getName(),aids);
  247. }
  248. @Override
  249. @Transactional
  250. public int update(InputExpenseAccount in) {
  251. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  252. if (useEA.getStatus()!=0&&useEA.getStatus()!=3){
  253. throw new BusinessException("不能操作审核中与完成的订单");
  254. }
  255. if (in.getStartTimeStr()!=null&&in.getEndTimeStr()!=null){
  256. in.setReleaseStart(DateUtils.StringToDate(in.getStartTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  257. in.setReleaseEnd(DateUtils.StringToDate(in.getEndTimeStr(), AFTConstants.YYYYMMDDHHMMSS));
  258. }
  259. //计算总金额
  260. if (in.getTotalAmount()==null){
  261. pushExpenseAccountAmount(in);
  262. }
  263. in.setProcessStatus(0);
  264. if (in.getOrderNo()==null){
  265. in.setTargetType(0);
  266. }else {
  267. in.setTargetType(1);
  268. }
  269. //计算本单已扣金额 如果有选择借支单,则需要修改借支状态
  270. if (in.getDebitId()!=null){
  271. //如果以前没有选择借支,修改报销和借支数据
  272. if (useEA.getDebitId()==null){
  273. pushExpenseAccountDebit(in);
  274. }else if (useEA.getDebitId()!=in.getDebitId()) {
  275. //如果是修改编号
  276. pushExpenseAccountDebit(in);
  277. //并且要处理原编号的问题
  278. pushResettingDebit(in,useEA.getDebitId());
  279. }
  280. }
  281. if (in.getSettlementAmount()==null)in.setSettlementAmount(useEA.getSettlementAmount());
  282. in.setStatus(0);
  283. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  284. }
  285. private void pushExpenseAccountAmount(ExpenseAccount in) {
  286. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  287. List<OutExpenseAccountDetails> list = expenseAccountDetailsMapper.selectByEAid(in.getId());
  288. BigDecimal totalAmount = BigDecimal.ZERO;
  289. BigDecimal realAmount = BigDecimal.ZERO;
  290. for (OutExpenseAccountDetails e : list) {
  291. totalAmount=totalAmount.add(e.getAmount());
  292. realAmount=realAmount.add(e.getRealAmount());
  293. }
  294. in.setTotalAmount(totalAmount);
  295. if (useEA==null||useEA.getSettlementAmount()==null){
  296. in.setAmount(realAmount);
  297. }else {
  298. in.setAmount(realAmount.subtract(useEA.getSettlementAmount()));
  299. }
  300. in.setRealAmount(in.getAmount());
  301. }
  302. @Override
  303. public Object updateDebitId(InputExpenseAccount in) {
  304. OutExpenseAccount useEA = expenseAccountMapper.selectByid(in.getId());
  305. if (useEA.getDebitId()!=null)pushResettingDebit(in,useEA.getDebitId());
  306. expenseAccountMapper.updateDeleteDebitId(in.getId());
  307. return 1;
  308. }
  309. @Override
  310. public Object delete(String ids) {
  311. String[] split = ids.split(",");
  312. for (String s : split) {
  313. Integer id=Integer.valueOf(s);
  314. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(id);
  315. if (expenseAccount.getStatus()!=0&&expenseAccount.getStatus()!=3){
  316. throw new BusinessException("该费用单状态无法删除");
  317. }
  318. List<OutExpenseAccountLog> logs = expenseAccountLogMapper.selectByEaid(id);
  319. if (logs.isEmpty()){
  320. expenseAccountMapper.deleteByPrimaryKey(id);
  321. }else {
  322. ExpenseAccount update =new ExpenseAccount();
  323. update.setId(id);
  324. update.setStatus(4);
  325. expenseAccountMapper.updateByPrimaryKeySelective(update);
  326. }
  327. }
  328. return 1;
  329. }
  330. @Override
  331. public Object pageListExport(InputPageListBo in) {
  332. List<OutExpenseAccountBo> list = (List<OutExpenseAccountBo>) pageList(in).getList();
  333. for (OutExpenseAccountBo e : list) {
  334. if (e.getType()!=0){
  335. e.setTypeOther(EATypes.getDescByCode(e.getType()));
  336. }else{
  337. e.setTypeOther(String.format("其他(%s)",e.getTypeOther()));
  338. }
  339. }
  340. NewExcelUtil excelUtil=new NewExcelUtil(OutExpenseAccountBo.class);
  341. return excelUtil.exportExcel(list,"费用单列表",uploadPath);
  342. }
  343. @Override
  344. public Object detailsListExport(InputDetailsListBo in) {
  345. List<OutExpenseAccountDetailsList> list = (List<OutExpenseAccountDetailsList>) detailsList(in).getList();
  346. for (OutExpenseAccountDetailsList e : list) {
  347. if (e.getTargetType()!=null){
  348. if (e.getTargetType()==0){
  349. e.setTargetName("固定费用");
  350. }else {
  351. e.setTargetName(e.getContractNo()+"/"+e.getBuyerName());
  352. }
  353. }
  354. if (e.getType()!=0){
  355. e.setTypeOther(EAsecondaryTypes.getDescByCode(e.getType()));
  356. }else{
  357. //如果是其他,又说明显示说明,没有说明显示费用类型
  358. if (StringUtils.isBlank(e.getTypeOther())){
  359. if (e.getSonType()!=0){
  360. e.setTypeOther(EATypes.getDescByCode(e.getSonType()));
  361. }else{
  362. e.setTypeOther(String.format("其他(%s)",e.getSonTypeOther()));
  363. }
  364. }else {
  365. e.setTypeOther(String.format("其他(%s)",e.getTypeOther()));
  366. }
  367. }
  368. if(e.getReleaseStart()!=null&&e.getReleaseEnd()!=null){
  369. e.setReleaseTimeStr(DateUtils.formatDate(e.getReleaseStart(),AFTConstants.YYYYMMDDHHMM)+"~"+DateUtils.formatDate(e.getReleaseEnd(),AFTConstants.YYYYMMDDHHMM));
  370. }
  371. }
  372. NewExcelUtil excelUtil=new NewExcelUtil(OutExpenseAccountDetailsList.class);
  373. return excelUtil.exportExcel(list,"费用详细列表",uploadPath);
  374. }
  375. @Override
  376. public Object getDepDetails(Integer id) {
  377. OutExpenseAccount outExpenseAccount = expenseAccountMapper.selectByid(id);
  378. OrganizationListOut organizationListOut = departmentMapper.selectAllById(outExpenseAccount.getApplyDep());
  379. return organizationListOut;
  380. }
  381. @Override
  382. public Object updateTypeOther(Integer id, String typeOther,String remarks,String orderNo) {
  383. ExpenseAccount ea = new ExpenseAccount();
  384. ea.setId(id);
  385. if (StringUtils.isNotEmpty(typeOther)){
  386. ea.setTypeOther(typeOther);
  387. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  388. }
  389. if (StringUtils.isNotEmpty(remarks)){
  390. ea.setRemarks(remarks);
  391. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  392. }
  393. if (StringUtils.isBlank(orderNo)){
  394. ea.setTargetType(0);
  395. expenseAccountMapper.updateRemoveOrderNo(ea);
  396. }else {
  397. ea.setOrderNo(orderNo);
  398. ea.setTargetType(1);
  399. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  400. }
  401. if (StringUtils.isNotEmpty(typeOther)){
  402. MainExpenseAccount mainExpenseAccount = expenseAccountMapper.selectbySonId(id);
  403. StringBuffer types=new StringBuffer();
  404. for (SonExpenseAccount e : mainExpenseAccount.getSonList()) {
  405. if (e.getType()!=0){
  406. types=types.append(EATypes.getDescByCode(e.getType()));
  407. }else {
  408. types=types.append(e.getTypeOther());
  409. }
  410. if (e.getType()==2){
  411. if (e.getSecondaryType()==0){
  412. types=types.append("-").append(e.getSecondaryTypeOther());
  413. }else{
  414. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  415. types=types.append("-").append(descByCode);
  416. }
  417. }
  418. types=types.append(",");
  419. }
  420. expenseAccountMapper.updateSonTypeOther(id,types.substring(0,types.length()-1));
  421. }
  422. return 1;
  423. }
  424. @Override
  425. public Object updateDetailsTypeOther(Integer id, String typeOther) {
  426. ExpenseAccountDetails in =new ExpenseAccountDetails();
  427. in.setId(id);
  428. in.setTypeOther(typeOther);
  429. return expenseAccountDetailsMapper.updateByPrimaryKeySelective(in);
  430. }
  431. @Override
  432. public Object mainExpenseExport(Integer id, HttpServletResponse response) {
  433. MainExpenseAccount mainExpenseAccount = mainExpense(id);
  434. pushMainExpenseExport(mainExpenseAccount,response,uploadPath);
  435. return 1;
  436. }
  437. @Override
  438. public void pushExpenseTotalAmount() {
  439. List<TOrderNew> tOrderNews = orderNewService.selectGetAll();
  440. for (TOrderNew e : tOrderNews) {
  441. orderNewService.pushOrderPublicReleaseCount(e.getOrderNo());
  442. }
  443. }
  444. public void pushMainExpenseExport(MainExpenseAccount mainExpenseAccount,HttpServletResponse response, String uploadPath) {
  445. String title = "报销明细表";
  446. String attName = String.format("报销明细表_%s.pdf",new Date().getTime());
  447. String realFileName = uploadPath+"/tmp/"+new Date().getTime() + ".pdf";
  448. response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
  449. Document document=new Document();
  450. try {
  451. FileUtils.setAttachmentResponseHeader(response, title,attName);
  452. FileOutputStream outputStream = new FileOutputStream(realFileName);
  453. PdfWriter.getInstance(document, outputStream);
  454. document.open();
  455. Paragraph paragraph = new Paragraph(title, PDFUtils.getTitleFont());
  456. paragraph.setAlignment(1);
  457. document.add(paragraph);
  458. //默认算60
  459. PDFUtils.pushExpenseTitle(document,mainExpenseAccount);
  460. PdfPTable table = new PdfPTable(12);
  461. table.setWidthPercentage(100);
  462. ArrayList<PdfPRow> rows = table.getRows();
  463. float[] columnWidths ={12,24,14,50,50,42,28,18,12,12,50,50};
  464. table.setWidths(columnWidths);
  465. table.setSpacingBefore(10f);
  466. table.setSpacingAfter(10f);
  467. //填充表格头部
  468. addTitle(rows);
  469. //获取填充数据
  470. List<Map<Integer,String>> mapList = pushMainExpenseAccount(mainExpenseAccount);
  471. //填充数据写入表格
  472. pushTable(rows,mapList);
  473. document.add(table);
  474. document.close();
  475. outputStream.close();
  476. // FileUtils.writeBytes(realFileName, response.getOutputStream());
  477. // FileUtils.deleteFile(realFileName);
  478. } catch (DocumentException | IOException e) {
  479. e.printStackTrace();
  480. }
  481. }
  482. private static void addTitle(ArrayList<PdfPRow> rows) {
  483. PdfPCell[] cells =new PdfPCell[12];
  484. PdfPRow row=new PdfPRow(cells);
  485. cells[0] = new PdfPCell(new Phrase("序号",PDFUtils.getFont(8)));
  486. cells[1] = new PdfPCell(new Phrase("费用时间",PDFUtils.getFont(8)));
  487. cells[2] = new PdfPCell(new Phrase("时长",PDFUtils.getFont(8)));
  488. cells[3] = new PdfPCell(new Phrase("出差地址",PDFUtils.getFont(8)));
  489. cells[4] = new PdfPCell(new Phrase("出差企业",PDFUtils.getFont(8)));
  490. cells[5] = new PdfPCell(new Phrase("合同编号",PDFUtils.getFont(8)));
  491. cells[6] = new PdfPCell(new Phrase("类型",PDFUtils.getFont(8)));
  492. cells[7] = new PdfPCell(new Phrase("详情",PDFUtils.getFont(8)));
  493. cells[8] = new PdfPCell(new Phrase("费用",PDFUtils.getFont(8)));
  494. cells[9] = new PdfPCell(new Phrase("实报",PDFUtils.getFont(8)));
  495. cells[10] = new PdfPCell(new Phrase("出差事由",PDFUtils.getFont(8)));
  496. cells[11] = new PdfPCell(new Phrase("报销订单/部门",PDFUtils.getFont(8)));
  497. rows.add(row);
  498. }
  499. private void pushTable(ArrayList<PdfPRow> rows, List<Map<Integer, String>> mapList) {
  500. for (Map<Integer, String> map : mapList) {
  501. PdfPCell[] cells =new PdfPCell[12];
  502. PdfPRow row=new PdfPRow(cells);
  503. for (int i = 0; i < 12; i++) {
  504. cells[i] = new PdfPCell(new Phrase(map.get(i),PDFUtils.getFont(8)));
  505. }
  506. rows.add(row);
  507. }
  508. }
  509. private List<Map<Integer, String>> pushMainExpenseAccount(MainExpenseAccount mainExpenseAccount) {
  510. List<Map<Integer,String>> mapList = new ArrayList<>();
  511. if (mainExpenseAccount.getSonList()!=null){
  512. int index=0;
  513. for (SonExpenseAccount e : mainExpenseAccount.getSonList()) {
  514. List<SonExpenseAccountDetails> detList = e.getDetList();
  515. //详情计算,详情完成后需要一个统计
  516. int detIndex = 0;
  517. for (SonExpenseAccountDetails det : detList) {
  518. index++;
  519. detIndex++;
  520. Map<Integer, String> map = new HashMap<>();
  521. map.put(0, String.valueOf(index));
  522. if (detIndex==1){
  523. if (e.getPublicReleaseType()!=null){
  524. map.put(1,DateUtils.formatDate(e.getReleaseStart(),AFTConstants.YYYYMMDDHHMM)+"~"+DateUtils.formatDate(e.getReleaseEnd(),AFTConstants.YYYYMMDDHHMM));
  525. map.put(2,e.getDuration());
  526. map.put(3,e.getDistrictName());
  527. map.put(4,e.getUserNames());
  528. map.put(6,getExpenseType(e));
  529. }else {
  530. map.put(1,det.getAgreeTime());
  531. map.put(2,"");
  532. map.put(3,"");
  533. map.put(4,"");
  534. map.put(6,"");
  535. }
  536. }else {
  537. map.put(1,det.getAgreeTime());
  538. map.put(2,"");
  539. map.put(3,"");
  540. map.put(4,"");
  541. map.put(6,"");
  542. }
  543. map.put(5,e.getContractNo());
  544. map.put(7,getExpenseSecondaryType(e,det));
  545. map.put(8,getBigDecimalZeros(det.getAmount()));
  546. map.put(9,getBigDecimalZeros(det.getRealAmount()));
  547. map.put(10,e.getPlan());
  548. if (index==1){
  549. map.put(11,getOrderNoAndDepartment(e,mainExpenseAccount.getApplyDepName()));
  550. }else {
  551. map.put(11,"");
  552. }
  553. mapList.add(map);
  554. if (detIndex==detList.size()){
  555. index++;
  556. addCount(e.getAmount(),e.getRealAmount(),mapList,index);
  557. }
  558. }
  559. }
  560. //循环结束新增合计
  561. index++;
  562. addCount(mainExpenseAccount.getAmount(),mainExpenseAccount.getRealAmount(),mapList,index,1);
  563. }
  564. return mapList;
  565. }
  566. private String getBigDecimalZeros(BigDecimal amount) {
  567. return amount.stripTrailingZeros().toEngineeringString();
  568. }
  569. private void addCount(BigDecimal amount,BigDecimal realAmount,List<Map<Integer, String>> mapList,int index) {
  570. addCount(amount,realAmount,mapList,index,0);
  571. }
  572. private void addCount(BigDecimal amount,BigDecimal realAmount,List<Map<Integer, String>> mapList,int index,int type) {
  573. String str="小计";
  574. if (type==1)str="合计";
  575. Map<Integer, String> map = new HashMap<>();
  576. for (int i = 0; i < 12; i++){
  577. if (i==0){
  578. map.put(i, String.valueOf(index));
  579. }else if (i==7){
  580. map.put(i, str);
  581. }else if (i==8){
  582. map.put(i, getBigDecimalZeros(amount));
  583. }else if (i==9){
  584. map.put(i, getBigDecimalZeros(realAmount));
  585. }else {
  586. map.put(i, "");
  587. }
  588. }
  589. mapList.add(map);
  590. }
  591. private String getOrderNoAndDepartment(SonExpenseAccount e,String applyDepName) {
  592. return e.getOrderNo()+"\n"+applyDepName;
  593. }
  594. private String getExpenseSecondaryType(SonExpenseAccount e,SonExpenseAccountDetails det) {
  595. if (e.getType()==1){
  596. if (det.getType()==1){
  597. //交通工具待完善
  598. }
  599. }
  600. if (e.getSecondaryType()==0){
  601. return EAsecondaryTypes.getDescByCode(e.getSecondaryType())+"("+e.getSecondaryTypeOther()+")";
  602. }
  603. return EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  604. }
  605. private String getExpenseType(SonExpenseAccount e) {
  606. if (e.getType()==0){
  607. return EATypes.getDescByCode(e.getType())+"("+e.getTypeOther()+")";
  608. }
  609. return EATypes.getDescByCode(e.getType());
  610. }
  611. private void pushResettingDebit(InputExpenseAccount in, Integer debitId) {
  612. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(debitId);
  613. List<ExpenseAccount> list = expenseAccountMapper.selectByDebitId(debitId);
  614. ExpenseAccount newDebit=new ExpenseAccount();
  615. newDebit.setId(debitId);
  616. //计算除开当前单,已选择抵扣金额
  617. BigDecimal count=new BigDecimal(0);
  618. for (ExpenseAccount e : list) {
  619. if (!in.getId().equals(e.getId())){
  620. count=count.add(e.getSettlementAmount());
  621. }
  622. }
  623. newDebit.setSettlementAmount(count);
  624. if (count.compareTo(new BigDecimal(0))==0){
  625. newDebit.setLiquidationStatus(0);
  626. }else if (count.compareTo(debit.getRealAmount())<0){
  627. newDebit.setLiquidationStatus(1);
  628. }else if (count.compareTo(debit.getRealAmount())==0){
  629. newDebit.setLiquidationStatus(2);
  630. }
  631. expenseAccountMapper.updateByPrimaryKeySelective(newDebit);
  632. }
  633. private void pushExpenseAccountDebit(InputExpenseAccount in) {
  634. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(in.getDebitId());
  635. ExpenseAccount newDebit=new ExpenseAccount();
  636. newDebit.setId(debit.getId());
  637. //计算剩余需要支付金额
  638. BigDecimal decbitAmount=debit.getRealAmount().subtract(debit.getSettlementAmount());
  639. //本次金额大于等于需要抵扣金额,则变成完全抵扣,抵扣金额变成本次已付金额
  640. if (in.getRealAmount().compareTo(decbitAmount)>=0){
  641. in.setSettlementAmount(decbitAmount);
  642. newDebit.setSettlementAmount(debit.getRealAmount());
  643. newDebit.setLiquidationStatus(2);
  644. }else {
  645. // 本次金额小于需要抵扣,则本次变更全抵扣,借支已付加上本次金额
  646. in.setSettlementAmount(in.getRealAmount());
  647. BigDecimal setAmount=new BigDecimal(0).add(debit.getSettlementAmount()).add(in.getRealAmount());
  648. newDebit.setSettlementAmount(setAmount);
  649. newDebit.setLiquidationStatus(1);
  650. }
  651. expenseAccountMapper.updateByPrimaryKeySelective(newDebit);
  652. }
  653. @Override
  654. public int addDetails(InputExpenseAccountDetails in) {
  655. if (in.getAgreeTimes()!=null){
  656. in.setAgreeTime(DateUtils.StringToDate(in.getAgreeTimes(),AFTConstants.YYYYMMDD));
  657. }
  658. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(in.getEaid());
  659. if (expenseAccount.getType()==2){
  660. in.setType(expenseAccount.getSecondaryType());
  661. if (in.getType()==0){
  662. in.setTypeOther(expenseAccount.getSecondaryTypeOther());
  663. }
  664. }
  665. in.setRealAmount(in.getAmount());
  666. expenseAccountDetailsMapper.insertSelective(in);
  667. ExpenseAccount ea =new ExpenseAccount();
  668. ea.setId(in.getEaid());
  669. pushExpenseAccountAmount(ea);
  670. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  671. return 1;
  672. }
  673. @Override
  674. public int deleteDetails(Integer id) {
  675. expenseAccountDetailsMapper.deleteByPrimaryKey(id);
  676. ExpenseAccount ea =new ExpenseAccount();
  677. ea.setId(id);
  678. pushExpenseAccountAmount(ea);
  679. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  680. return 1;
  681. }
  682. @Override
  683. public List<OutExpenseAccountDetails> detailsList(Integer eaid) {
  684. return expenseAccountDetailsMapper.selectByEAid(eaid);
  685. }
  686. @Override
  687. public Object logList(Integer eaid) {
  688. List<OutExpenseAccountLog> list = expenseAccountLogMapper.selectByEaid(eaid);
  689. if (!list.isEmpty()) {
  690. OutExpenseAccountLog lastout = list.get(list.size() - 1);
  691. ExpenseAccount expenseAccount = expenseAccountMapper.selectByPrimaryKey(eaid);
  692. Integer processStatus = lastout.getProcessStatus();
  693. if (lastout.getStatus() < 2) {
  694. if (processStatus <3){
  695. List<OutExpenseAccountLog> outExpenseAccountLogs = expenseAccountExamineMapper.selectNotExamineByEaidAndProcessStatus(eaid, processStatus);
  696. list.addAll(outExpenseAccountLogs);
  697. }else {
  698. List<ExpenseAccountExamine> expenseAccountExamines = expenseAccountExamineMapper.selectByEaidAndProcessStatus(eaid, processStatus);
  699. for (ExpenseAccountExamine e : expenseAccountExamines) {
  700. if (e.getStatus()==0){
  701. OutExpenseAccountLog out =new OutExpenseAccountLog();
  702. out.setEaid(eaid);
  703. out.setProcessStatus(e.getProcessStatus());
  704. out.setAuditorName(e.getAuditorname());
  705. list.add(out);
  706. }
  707. }
  708. }
  709. if (processStatus<8)addDetailsLog(list, expenseAccount,processStatus);
  710. }
  711. }
  712. return list;
  713. }
  714. private void addDetailsLog(List<OutExpenseAccountLog> list, ExpenseAccount expenseAccount,Integer useStatus) {
  715. OrganizationListOut dep = departmentMapper.selectAllById(expenseAccount.getApplyDep());
  716. List<ExpenseConfigDetails> configDetails = expenseConfigDetailsMapper.selectByDepId(expenseAccount.getApplyDep());
  717. for (int processStatus = useStatus; processStatus < 8; processStatus++) {
  718. if (processStatus ==3){
  719. if (dep.getExpenseRetrialFinanceExamine()==1){
  720. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 1).collect(Collectors.toList());
  721. for (ExpenseConfigDetails e : collect) {
  722. addDetailsTypeLog(list, expenseAccount, e);
  723. }
  724. }
  725. }else if (processStatus ==4){
  726. if (expenseAccount.getApproval()==1&&dep.getCeoExamine()==1){
  727. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 2).collect(Collectors.toList());
  728. for (ExpenseConfigDetails e : collect) {
  729. addDetailsTypeLog(list, expenseAccount, e);
  730. }
  731. }
  732. }else if (processStatus ==5){
  733. if (dep.getCfoExamine()==1){
  734. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 3).collect(Collectors.toList());
  735. for (ExpenseConfigDetails e : collect) {
  736. addDetailsTypeLog(list, expenseAccount, e);
  737. }
  738. }
  739. }else if (processStatus ==6){
  740. if (dep.getViceCeoExamine()==1){
  741. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 4).collect(Collectors.toList());
  742. for (ExpenseConfigDetails e : collect) {
  743. addDetailsTypeLog(list, expenseAccount, e);
  744. }
  745. }
  746. }else if (processStatus ==7){
  747. if (dep.getCashierExamine()==1){
  748. List<ExpenseConfigDetails> collect = configDetails.stream().filter(e -> e.getType() == 5).collect(Collectors.toList());
  749. for (ExpenseConfigDetails e : collect) {
  750. addDetailsTypeLog(list, expenseAccount, e);
  751. }
  752. }
  753. }
  754. }
  755. }
  756. private static void addDetailsTypeLog(List<OutExpenseAccountLog> list, ExpenseAccount expenseAccount, ExpenseConfigDetails e) {
  757. OutExpenseAccountLog addLog=new OutExpenseAccountLog();
  758. addLog.setEaid(expenseAccount.getId());
  759. addLog.setAuditor(e.getAid());
  760. addLog.setAuditorName(e.getAdminName());
  761. list.add(addLog);
  762. }
  763. @Override
  764. public int updateExamine(InputExpenseAccount in) {
  765. ExpenseAccount useEa = expenseAccountMapper.selectByPrimaryKey(in.getId());
  766. ExpenseAccount newEa=new ExpenseAccount();
  767. newEa.setId(useEa.getId());
  768. newEa.setApproval(in.getApproval());
  769. in.setCheckNo(useEa.getCheckNo());
  770. in.setProcessStatus(useEa.getProcessStatus());
  771. //审核通知 获取发送人
  772. AdminListBo admin = adminMapper.getDeptNameByAid(useEa.getAid());
  773. //项目推送流程及添加日志
  774. addExamineExpenseAccountLog(admin,in, useEa, newEa);
  775. //完成申请
  776. if (newEa.getStatus()==2){
  777. //如果是借支则订单新增
  778. List<ExpenseAccount> expenseAccounts1 = expenseAccountMapper.selectListByMainId(in.getId());
  779. for (ExpenseAccount e : expenseAccounts1) {
  780. if (e.getType()!=4){
  781. //不然就看是否挂载借支订单进行抵扣
  782. if (e.getDebitId()!=null){
  783. ExpenseAccount useDebit=expenseAccountMapper.selectByPrimaryKey(e.getDebitId());
  784. if (useDebit.getLiquidationStatus()==2){
  785. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectByDebitId(e.getDebitId());
  786. boolean flag=true;
  787. for (ExpenseAccount expenseAccount : expenseAccounts) {
  788. if(!expenseAccount.getId().equals(in.getId())&&expenseAccount.getStatus()!=2){
  789. flag=false;
  790. break;
  791. }
  792. }
  793. if (flag){
  794. ExpenseAccount debit=new ExpenseAccount();
  795. debit.setId(e.getDebitId());
  796. debit.setLiquidationStatus(3);
  797. expenseAccountMapper.updateByPrimaryKeySelective(debit);
  798. }
  799. }
  800. }
  801. }
  802. ExpenseAccount updateSon=new ExpenseAccount();
  803. updateSon.setId(e.getId());
  804. updateSon.setStatus(2);
  805. expenseAccountMapper.updateByPrimaryKeySelective(updateSon);
  806. }
  807. }else if (newEa.getStatus()==1){
  808. if (newEa.getProcessStatus()==EAProcessStatus.BMFZRSH.getCode()){
  809. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),0);
  810. //处理金额修改
  811. if(in.getRealList()!=null){
  812. List<Map> maps = JSONObject.parseArray(in.getRealList(), Map.class);
  813. for (Map map: maps) {
  814. Integer type= (Integer) map.get("type");
  815. Integer id= (Integer) map.get("id");
  816. BigDecimal amount= new BigDecimal(map.get("amount").toString());
  817. //type=1总计,type=2报销金额
  818. if(type==1){
  819. ExpenseAccount ea =new ExpenseAccount();
  820. ea.setId(id);
  821. ea.setRealAmount(amount);
  822. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  823. }else if (type==2){
  824. ExpenseAccountDetails ead=new ExpenseAccountDetails();
  825. ead.setId(id);
  826. ead.setRealAmount(amount);
  827. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ead);
  828. }
  829. }
  830. }
  831. }else if (newEa.getProcessStatus()==EAProcessStatus.CWSH.getCode()){
  832. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),1);
  833. }
  834. }else if (newEa.getStatus()==3){
  835. if (useEa.getProcessStatus()==EAProcessStatus.BMFZRSH.getCode()){
  836. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),2);
  837. }else if (newEa.getProcessStatus()>EAProcessStatus.CWSH.getCode()){
  838. pushFinanceCount(admin.getDepFinance(),useEa.getCreateTime(),3);
  839. }
  840. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),null,0,0);
  841. }
  842. expenseAccountMapper.updateByPrimaryKeySelective(newEa);
  843. expenseAccountMapper.updateSonByMainId(newEa.getId());
  844. return 1;
  845. }
  846. private void addFinanceCount(String aid,Date startTime ) {
  847. String forDate=DateUtils.formatDate(startTime,AFTConstants.YYYYMMDD);
  848. addFinanceCount(aid,forDate);
  849. }
  850. private void addFinanceCount(String aid,String startTime ) {
  851. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  852. if (financeCount ==null){
  853. financeCount =FinanceCount.initialization(aid, startTime);
  854. financeCount.setExpenseCount(1);
  855. financeCount.setExpenseUnauditedCount(1);
  856. financeCountMapper.insertSelective(financeCount);
  857. }else {
  858. FinanceCount newFinanceCount =new FinanceCount();
  859. newFinanceCount.setId(financeCount.getId());
  860. newFinanceCount.setExpenseCount(financeCount.getExpenseCount()+1);
  861. newFinanceCount.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  862. financeCountMapper.updateByPrimaryKeySelective(newFinanceCount);
  863. }
  864. }
  865. private void addExamineExpenseAccountLog(AdminListBo admin,InputExpenseAccount in, ExpenseAccount useEa, ExpenseAccount newEa) {
  866. List<Admin> aids=new ArrayList<>();
  867. StringBuffer str=new StringBuffer();
  868. Integer status=in.getStatus();
  869. if (status==1){
  870. //审核处理审核表
  871. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),TokenManager.getAdminId(),1,0);
  872. str=str.append("编号[").append(in.getCheckNo()).append("]需要审核").append(",请查看并审核。");
  873. newEa.setAid(useEa.getAid());
  874. newEa.setCreateTime(useEa.getCreateTime());
  875. newEa.setApplyDep(useEa.getApplyDep());
  876. //在跳入董事长审核的时候非特批直接跳过,原则上下个流程不可再跳过
  877. OrganizationListOut dep = departmentMapper.selectAllById(newEa.getApplyDep());
  878. List<ExpenseConfigDetails> configDetailsList = expenseConfigDetailsMapper.selectByDepId(newEa.getApplyDep());
  879. if(useEa.getProcessStatus()==EAProcessStatus.CNSH.getCode()){
  880. expenseAccountExamineMapper.updateStatusByEaidAndAid(in.getId(),TokenManager.getAdminId(),1,1);
  881. boolean expenseExamine = pushExpenseExamine(configDetailsList, newEa, EAProcessStatus.CNSH.getCode());
  882. if (expenseExamine){
  883. //完成后的流程
  884. status =2;
  885. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  886. newEa.setStatus(status);
  887. newEa.setExamineName("");
  888. }else {
  889. status =1;
  890. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), useEa.getProcessStatus());
  891. StringBuffer aname =new StringBuffer();
  892. for (ExpenseAccountExamine e : list) {
  893. if (e.getStatus()==0){
  894. Admin ad=adminMapper.selectByPrimaryKey(e.getAuditor());
  895. aids.add(ad);
  896. aname=aname.append(ad.getName()).append(",");
  897. }
  898. }
  899. newEa.setExamineName(aname.substring(0,aname.length()-1));
  900. }
  901. addExpenseAccountLog(in.getId(),status,useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  902. }else {
  903. //小于为固定单审核,大于可能被配置多人审核
  904. if (useEa.getProcessStatus()<EAProcessStatus.CWFS.getCode()){
  905. boolean flag=false;
  906. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), useEa.getProcessStatus());
  907. for (ExpenseAccountExamine e : list) {
  908. if (e.getAuditor().equals(TokenManager.getAdminId())){
  909. flag=true;
  910. }
  911. }
  912. if (!flag)throw new BusinessException("审核角色错误。请刷新页面");
  913. newEa.setProcessStatus(useEa.getProcessStatus()+1);
  914. }else {
  915. newEa.setProcessStatus(useEa.getProcessStatus());
  916. }
  917. addExpenseAccountLog(in.getId(),status,useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  918. newEa.setStatus(1);
  919. if (newEa.getApproval()==null)newEa.setApproval(useEa.getApproval());
  920. //流程的推动
  921. examineByprocess(newEa,admin,dep,configDetailsList);
  922. //推动到出纳如果设置无需则完成不设置下个人,否则就要设置
  923. if (newEa.getProcessStatus()==EAProcessStatus.CNSH.getCode()){
  924. if(dep.getCashierExamine()==1){
  925. pushExamineName(newEa, aids);
  926. }else {
  927. //完成后的流程
  928. status =2;
  929. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  930. newEa.setStatus(status);
  931. newEa.setExamineName("");
  932. }
  933. }else {
  934. pushExamineName(newEa, aids);
  935. }
  936. }
  937. }else {
  938. aids.add(adminMapper.selectByPrimaryKey(admin.getId()));
  939. newEa.setStatus(3);
  940. newEa.setProcessStatus(0);
  941. str.setLength(0);
  942. addExpenseAccountLog(in.getId(),newEa.getStatus(),useEa.getProcessStatus(),TokenManager.getAdminId(),in.getReason());
  943. str=str.append("您的报销审核已驳回,报销编号[").append(in.getCheckNo()).append("],请注意查看。");
  944. }
  945. if (newEa.getStatus()==null)newEa.setStatus(1);
  946. addNoticeAndEmail(in, status,str.toString(),admin.getName(),aids);
  947. }
  948. private void pushExamineName(ExpenseAccount newEa, List<Admin> aids) {
  949. List<ExpenseAccountExamine> list=expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), newEa.getProcessStatus());
  950. StringBuffer aname =new StringBuffer();
  951. for (ExpenseAccountExamine e : list) {
  952. if (e.getProcessStatus()== newEa.getProcessStatus()&&e.getStatus()==0){
  953. Admin ad=adminMapper.selectByPrimaryKey(e.getAuditor());
  954. aids.add(ad);
  955. aname=aname.append(ad.getName()).append(",");
  956. }
  957. }
  958. newEa.setExamineName(aname.substring(0,aname.length()-1));
  959. }
  960. /**
  961. *
  962. * @param aid
  963. * @param createTime
  964. * @param type 0 双新增 1 未审核减一 2双减 3总数减一
  965. */
  966. private void pushFinanceCount(String aid, Date createTime, Integer type) {
  967. String startTime= DateUtils.formatDate(createTime,AFTConstants.YYYYMMDD);
  968. FinanceCount financeCount = financeCountMapper.selectByAidAndDates(aid, startTime);
  969. FinanceCount newFinance=new FinanceCount();
  970. if (financeCount==null){
  971. newFinance=FinanceCount.initialization(aid,createTime);
  972. newFinance.setExpenseCount(1);
  973. newFinance.setExpenseUnauditedCount(1);
  974. financeCountMapper.insertSelective(newFinance);
  975. }else {
  976. newFinance.setId(financeCount.getId());
  977. if(type==0){
  978. newFinance.setExpenseCount(financeCount.getExpenseCount()+1);
  979. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()+1);
  980. }else if(type==1){
  981. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  982. }else if(type==2){
  983. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  984. newFinance.setExpenseUnauditedCount(financeCount.getExpenseUnauditedCount()-1);
  985. }else if(type==3){
  986. newFinance.setExpenseCount(financeCount.getExpenseCount()-1);
  987. }
  988. financeCountMapper.updateByPrimaryKeySelective(newFinance);
  989. }
  990. }
  991. /**
  992. * 审核流程推送,只要不是完成状态就继续判断是否跳过
  993. * @param newEa
  994. */
  995. private void examineByprocess( ExpenseAccount newEa,AdminListBo admin,OrganizationListOut dep,List<ExpenseConfigDetails> list) {
  996. List<ExpenseAccountExamine> examineList=new ArrayList<>();
  997. //小于财务复审的,走单审核
  998. if (newEa.getProcessStatus()<EAProcessStatus.CWFS.getCode()){
  999. examineList = expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), newEa.getProcessStatus());
  1000. boolean flag=false;
  1001. String aid="";
  1002. if (examineList.size()>0){
  1003. for (ExpenseAccountExamine e : examineList) {
  1004. if (e.getStatus()==1){
  1005. flag=true;
  1006. aid=e.getAuditor();
  1007. break;
  1008. }
  1009. }
  1010. }
  1011. //如果当前审核人已经审核,则跳过当前审核
  1012. if (flag){
  1013. String str=String.format("重复审核人跳过[%s]。",EAProcessStatus.getDesc(newEa.getProcessStatus()));
  1014. Date date=new Date();
  1015. date.setTime(date.getTime()+1000l);
  1016. addExpenseAccountLog(newEa.getId(),1,newEa.getProcessStatus(),aid,str,date,2);
  1017. if (newEa.getProcessStatus()==EAProcessStatus.BMFZRSH.getCode()){
  1018. addFinanceCount(admin.getDepFinance(),newEa.getCreateTime());
  1019. }
  1020. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1021. examineByprocess(newEa,admin,dep,list);
  1022. }
  1023. }else {
  1024. //大于等于财务复审的,走多审核
  1025. String str=null;
  1026. //跳过标记
  1027. boolean flag2= false;
  1028. //审核通过标记
  1029. boolean flag3= false;
  1030. Date date2=new Date();
  1031. date2.setTime(date2.getTime()+1000l);
  1032. String tip=null;
  1033. String aid=null;
  1034. if (newEa.getProcessStatus()==EAProcessStatus.CWFS.getCode()){
  1035. //财务复审财务复审
  1036. if (dep.getExpenseRetrialFinanceExamine()==0){
  1037. flag2=true;
  1038. tip=EAProcessStatus.CWFS.getDesc();
  1039. }else {
  1040. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.CWFS.getCode());
  1041. if (expenseExamine){
  1042. flag3=true;
  1043. }
  1044. }
  1045. }else if (newEa.getProcessStatus()==EAProcessStatus.DSZSH.getCode()){
  1046. if (newEa.getApproval()!=1){
  1047. flag2=true;
  1048. tip="特批(财务选择)";
  1049. }else {
  1050. if (dep.getCeoExamine()==1){
  1051. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.DSZSH.getCode());
  1052. if (expenseExamine){
  1053. flag3=true;
  1054. }
  1055. }else {
  1056. flag2=true;
  1057. tip="特批(部门设置)";
  1058. }
  1059. }
  1060. }else if (newEa.getProcessStatus()==EAProcessStatus.CWGGLYSH.getCode()){
  1061. if (dep.getExpenseRetrialFinanceExamine()==0){
  1062. flag2=true;
  1063. tip=EAProcessStatus.CWGGLYSH.getDesc();
  1064. aid=null;
  1065. }else {
  1066. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.CWGGLYSH.getCode());
  1067. if (expenseExamine){
  1068. flag3=true;
  1069. }
  1070. }
  1071. }else if (newEa.getProcessStatus()==EAProcessStatus.GSFSSH.getCode()){
  1072. if (dep.getExpenseRetrialFinanceExamine()==0){
  1073. flag2=true;
  1074. tip=EAProcessStatus.GSFSSH.getDesc();
  1075. }else {
  1076. boolean expenseExamine = pushExpenseExamine(list, newEa, EAProcessStatus.GSFSSH.getCode());
  1077. if (expenseExamine){
  1078. flag3=true;
  1079. }
  1080. }
  1081. }else if (newEa.getProcessStatus()==EAProcessStatus.CNSH.getCode()){
  1082. if (dep.getExpenseRetrialFinanceExamine()==0){
  1083. flag2=true;
  1084. tip=EAProcessStatus.CNSH.getDesc();
  1085. }else {
  1086. //财务出纳在审核时不与前置审核做同步
  1087. pushExpenseExamine(list, newEa, EAProcessStatus.CNSH.getCode());
  1088. }
  1089. }
  1090. if (flag2){
  1091. if (str==null)str=String.format("未设置[%s]审核,跳过此流程",tip);
  1092. if (newEa.getProcessStatus()<EAProcessStatus.CNSH.getCode()){
  1093. date2.setTime(date2.getTime()+100);
  1094. addExpenseAccountLog(newEa.getId(),1,newEa.getProcessStatus(),aid,str,date2,2);
  1095. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1096. examineByprocess(newEa,admin,dep,list);
  1097. }else {
  1098. date2.setTime(date2.getTime()+100);
  1099. addExpenseAccountLog(newEa.getId(),2,newEa.getProcessStatus(),aid,str,date2,2);
  1100. newEa.setProcessStatus(newEa.getProcessStatus());
  1101. newEa.setStatus(2);
  1102. }
  1103. }else if (flag3){
  1104. newEa.setProcessStatus(newEa.getProcessStatus()+1);
  1105. examineByprocess(newEa,admin,dep,list);
  1106. }
  1107. }
  1108. }
  1109. private boolean pushExpenseExamine(List<ExpenseConfigDetails> list, ExpenseAccount newEa, Integer status) {
  1110. int configDetails=0;
  1111. if (status==EAProcessStatus.CWFS.getCode())configDetails=1;
  1112. else if (status==EAProcessStatus.DSZSH.getCode())configDetails=2;
  1113. else if (status==EAProcessStatus.CWGGLYSH.getCode())configDetails=3;
  1114. else if (status==EAProcessStatus.GSFSSH.getCode())configDetails=4;
  1115. else if (status==EAProcessStatus.CNSH.getCode())configDetails=5;
  1116. int finalConfigDetails = configDetails;
  1117. //查询审核表,如果为空就新增,不然直接判定审核
  1118. List<ExpenseAccountExamine> expenseAccountExamines = expenseAccountExamineMapper.selectByEaidAndProcessStatus(newEa.getId(), status);
  1119. int count = 0;
  1120. int size=10;
  1121. if (expenseAccountExamines.isEmpty()){
  1122. List<ExpenseConfigDetails> collect = list.stream().filter(e -> e.getType() == finalConfigDetails).collect(Collectors.toList());
  1123. size= collect.size();
  1124. for (ExpenseConfigDetails e : collect) {
  1125. ExpenseAccountExamine eae=new ExpenseAccountExamine(newEa.getId(),status);
  1126. eae.setAuditor(e.getAid());
  1127. eae.setAuditorname(e.getAdminName());
  1128. eae.setStatus(0);
  1129. Integer i = expenseAccountExamineMapper.selectYesByParam(newEa.getId(), e.getAid());
  1130. if (status==EAProcessStatus.CNSH.getCode()){
  1131. eae.setStatus(0);
  1132. count++;
  1133. }else {
  1134. if (i>0||e.getAid().equals(TokenManager.getAdminId())) {
  1135. eae.setStatus(1);
  1136. count++;
  1137. }
  1138. }
  1139. expenseAccountExamineMapper.insertSelective(eae);
  1140. }
  1141. }else {
  1142. size = expenseAccountExamines.size();
  1143. for (ExpenseAccountExamine e : expenseAccountExamines) {
  1144. if (e.getStatus()==1){
  1145. count++;
  1146. }
  1147. }
  1148. }
  1149. if (count==size){
  1150. return true;
  1151. }
  1152. return false;
  1153. }
  1154. private void addNoticeAndEmail(ExpenseAccount in, Integer status, String str,String aname, List<Admin> aids) {
  1155. Date date= new Date();
  1156. Integer noticeStatus=NoticeStatus.EXPENSE_NOTICE.getCode();
  1157. if (status==2){
  1158. noticeStatus=NoticeStatus.EXPENSE_COMPLETE.getCode();
  1159. str="您提交的费用报销单已通过审核,请及时查看。";
  1160. }
  1161. List<Notice> list=new ArrayList<>();
  1162. if (aids!=null){
  1163. for (Admin a : aids) {
  1164. Notice notice=new Notice();
  1165. notice.setId(UUID.randomUUID().toString());
  1166. notice.setNoticeType(noticeStatus);
  1167. notice.setType(NoticeTypes.getType(noticeStatus));
  1168. notice.setAid(a.getId());
  1169. notice.setCreateTime(date);
  1170. notice.setContent(str);
  1171. notice.setReaded(0);
  1172. list.add(notice);
  1173. EmailBo emailBo=new EmailBo(NoticeStatus.getValueByCode(noticeStatus),a.getEmail(),str);
  1174. asyncUtils.send(emailBo);
  1175. try {
  1176. weChatUtils.addExpenseAccountWeChatNotice(a.getExpenseOpenId(),status,in.getId(),date,aname,str);
  1177. }catch (Exception e){
  1178. throw new BusinessException("网络通信异常,请稍后再试。");
  1179. }
  1180. }
  1181. asyncUtils.addNoticeBatch(list);
  1182. }
  1183. }
  1184. @Override
  1185. public Pagination<?> pageList(InputPageListBo in) {
  1186. Map<String ,Object> param=new HashMap<>();
  1187. addParam(in, param);
  1188. Pagination<?> pagePlus = findPagePlus("selectExpenseAccountList",
  1189. "selectExpenseAccountCount",
  1190. "selectExpenseAccountTotalAmount", param, in.getPageNo(), in.getPageSize());
  1191. List<OutExpenseAccountBo> list = (List<OutExpenseAccountBo>) pagePlus.getList();
  1192. for (OutExpenseAccountBo e : list) {
  1193. e.setExamine(pushExpenseAccountGetExamine(e.getId(), e.getProcessStatus()));
  1194. }
  1195. return pagePlus;
  1196. }
  1197. @Override
  1198. public Object selectByPrid(InputExpenseAccount in) {
  1199. in.setAid(TokenManager.getAdminId());
  1200. OutExpenseAccount useEa = expenseAccountMapper.selectByaidAndPrid(in);
  1201. if (useEa!=null){
  1202. ExpenseRelationship er = expenseRelationshipMapper.selectSonId(useEa.getId());
  1203. if (er!=null){
  1204. throw new BusinessException("公出已经发起报销");
  1205. }
  1206. }
  1207. return useEa;
  1208. }
  1209. @Override
  1210. public Object selectById(Integer id) {
  1211. OutExpenseAccount useEa = expenseAccountMapper.selectByid(id);
  1212. //判断当前角色是否审核
  1213. useEa.setExamine(pushExpenseAccountGetExamine( id, useEa.getProcessStatus()));;
  1214. if (useEa.getDebitId()!=null){
  1215. pushExpenseAccountGetDebitAmount(useEa);
  1216. }
  1217. return useEa;
  1218. }
  1219. @Override
  1220. public Object selectByCheckNo(String checkNo) {
  1221. checkNo=checkNo.trim();
  1222. OutExpenseAccount useEa = expenseAccountMapper.selectByCheckNo(checkNo);
  1223. if (useEa==null){
  1224. throw new BusinessException("报销编号不存在,请客对后再查询。");
  1225. }
  1226. if (useEa.getProcessStatus()<3||useEa.getStatus()!=2){
  1227. throw new BusinessException("报销单财务未审核,不可以根据编号查看打印。");
  1228. }
  1229. return useEa;
  1230. }
  1231. @Override
  1232. public Object pushMerge(List<ExpenseAccount> list) {
  1233. //设置付款部门和支付部门
  1234. InputExpenseAccount in =new InputExpenseAccount();
  1235. in.setCheckNo(idGenerator.getNOgenerateId());
  1236. if (in.getAid()==null){
  1237. in.setAid(TokenManager.getAdminId());
  1238. }
  1239. Admin admin = adminMapper.selectByPrimaryKey(in.getAid());
  1240. in.setAname(admin.getName());
  1241. in.setApplyDep(admin.getDepartmentId());
  1242. in.setPayDep(admin.getDepartmentId());
  1243. Integer eaaid = expenseAccountPrivateMapper.selectByaidDefault(TokenManager.getAdminId());
  1244. in.setEaaid(eaaid);
  1245. BigDecimal count = new BigDecimal(0);
  1246. BigDecimal amount = new BigDecimal(0);
  1247. BigDecimal realAmount = new BigDecimal(0);
  1248. List<String> typeList = new ArrayList<String>();
  1249. for (ExpenseAccount e : list) {
  1250. count=count.add(e.getTotalAmount());
  1251. amount=amount.add(e.getAmount());
  1252. realAmount=realAmount.add(e.getRealAmount());
  1253. StringBuffer types=new StringBuffer();
  1254. if (e.getType()!=0){
  1255. types=types.append(EATypes.getDescByCode(e.getType()));
  1256. }else {
  1257. types=types.append(e.getTypeOther());
  1258. }
  1259. if (e.getType()==2){
  1260. if (e.getSecondaryType()==0){
  1261. types=types.append("-").append(e.getSecondaryTypeOther());
  1262. }else{
  1263. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  1264. types=types.append("-").append(descByCode);
  1265. }
  1266. }
  1267. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  1268. }
  1269. in.setTotalAmount(count);
  1270. in.setAmount(amount);
  1271. in.setRealAmount(realAmount);
  1272. in.setExpenseMain(1);
  1273. in.setTypeOther(String.join(",",typeList));
  1274. expenseAccountMapper.insertSelective(in);
  1275. List<OutExpenseAccountLog> newLog=new ArrayList<>();
  1276. list.stream().forEach(e -> {
  1277. ExpenseRelationship er=new ExpenseRelationship();
  1278. er.setId(in.getId());
  1279. er.setEaId(e.getId());
  1280. expenseRelationshipMapper.insertSelective(er);
  1281. List<OutExpenseAccountLog> listLog = expenseAccountLogMapper.selectByEaid(e.getId());
  1282. for (OutExpenseAccountLog log : listLog) {
  1283. if (!newLog.contains(log)){
  1284. log.setEaid(in.getId());
  1285. log.setShowStatus(1);
  1286. newLog.add(log);
  1287. }
  1288. }
  1289. expenseAccountLogMapper.deleteByeaid(e.getId());
  1290. });
  1291. if (!newLog.isEmpty())expenseAccountLogMapper.insertBatch(newLog);
  1292. expenseAccountMapper.updateByIds(list,1);
  1293. return in.getId();
  1294. }
  1295. @Override
  1296. public MainExpenseAccount mainExpense(Integer id) {
  1297. MainExpenseAccount res = expenseAccountMapper.selectByMainId(id);
  1298. pushData(res);
  1299. res.setExamine(0);
  1300. if (res.getAid().equals(TokenManager.getAdminId())){
  1301. res.setExamine(2);
  1302. }else {
  1303. res.setExamine(pushExpenseAccountGetExamine( id, res.getProcessStatus()));;
  1304. }
  1305. return res;
  1306. }
  1307. private void pushData(MainExpenseAccount res) {
  1308. List<SonExpenseAccount> sonList = res.getSonList();
  1309. for (SonExpenseAccount e : sonList) {
  1310. if (StringUtils.isNotEmpty(e.getUserNames())){
  1311. String userNames = e.getUserNames();
  1312. String districtName = e.getDistrictName();
  1313. int start=userNames.indexOf("[");
  1314. if (start>0){
  1315. userNames = userNames.substring(0,start);
  1316. districtName=districtName.replace(userNames,"");
  1317. e.setUserNames(userNames);
  1318. e.setDistrictName(districtName);
  1319. }else {
  1320. districtName=districtName.replace(userNames,"");
  1321. e.setDistrictName(districtName);
  1322. }
  1323. }
  1324. if (e.getOrderNo()!=null){
  1325. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectByOrderNo(e.getOrderNo());
  1326. if(!expenseAccounts.isEmpty()){
  1327. TOrderPublicReleaseCount out = tOrderPublicReleaseCountMapper.selectByOrderNo(e.getOrderNo());
  1328. BigDecimal count = BigDecimal.ZERO;
  1329. BigDecimal yxyCount = BigDecimal.ZERO;
  1330. BigDecimal zxsCount = BigDecimal.ZERO;
  1331. BigDecimal qtCount = BigDecimal.ZERO;
  1332. for (ExpenseAccount ea : expenseAccounts) {
  1333. count=count.add(ea.getAmount());
  1334. Admin admin = adminMapper.selectByPrimaryKey(ea.getAid());
  1335. if (admin.getPosition()!=null){
  1336. if (admin.getPosition().equals("1")){
  1337. yxyCount=yxyCount.add(ea.getAmount());
  1338. }else if (admin.getPosition().equals("2")){
  1339. zxsCount=zxsCount.add(ea.getAmount());
  1340. }else {
  1341. qtCount=qtCount.add(ea.getAmount());
  1342. }
  1343. }
  1344. }
  1345. String str = getString(e, out, count, yxyCount, zxsCount,qtCount);
  1346. e.setCountStr(str);
  1347. }
  1348. }
  1349. }
  1350. }
  1351. private String getString(SonExpenseAccount e, TOrderPublicReleaseCount out, BigDecimal count, BigDecimal yxyCount, BigDecimal zxsCount, BigDecimal qtCount) {
  1352. if (out == null){
  1353. return null;
  1354. }
  1355. String maxString=":应公出" + out.getMaxDuration() + "小时";
  1356. String actualString=",超过" + out.getExceedDuration() + "小时,";
  1357. if (out.getMaxDuration().equals(-1d)){
  1358. maxString=":不限制公出小时";
  1359. actualString="";
  1360. }
  1361. StringBuilder expenseString=new StringBuilder().append("报销").append(count.stripTrailingZeros().toPlainString()).append("元");
  1362. if (yxyCount != null || zxsCount != null||qtCount != null){
  1363. expenseString.append("(");
  1364. if (zxsCount != null && zxsCount.compareTo(BigDecimal.ZERO) > 0) {
  1365. expenseString.append("技术").append(zxsCount.stripTrailingZeros().toPlainString()).append("元,");
  1366. }
  1367. if (yxyCount != null && yxyCount.compareTo(BigDecimal.ZERO) > 0){
  1368. expenseString.append("营销").append(yxyCount.stripTrailingZeros().toPlainString()).append("元,");
  1369. }
  1370. if (qtCount != null && qtCount.compareTo(BigDecimal.ZERO) > 0){
  1371. expenseString.append("其他").append(qtCount.stripTrailingZeros().toPlainString()).append("元,");
  1372. }
  1373. expenseString.deleteCharAt(expenseString.length() - 1);
  1374. expenseString.append(")");
  1375. }
  1376. if (out.getActualDuration()<=out.getMaxDuration()){
  1377. actualString="";
  1378. }
  1379. return e.getBuyerName() + e.getContractNo() +
  1380. maxString +
  1381. ",已" + out.getPeopleCount() + "人公出," +
  1382. out.getFrequency() + "次" +
  1383. "," + out.getActualDuration() + "小时" +
  1384. actualString+expenseString;
  1385. }
  1386. @Override
  1387. public Object updateMain(MainExpenseAccount in) {
  1388. OutExpenseAccount outExpenseAccount = expenseAccountMapper.selectByid(in.getId());
  1389. if (outExpenseAccount != null&&outExpenseAccount.getStatus()!=0&&outExpenseAccount.getStatus()!=3) {
  1390. throw new BusinessException("主项目已发起,不可修改!");
  1391. }
  1392. ExpenseAccount update =new ExpenseAccount();
  1393. if (in.getStatus()==null)in.setStatus(1);
  1394. if (in.getStatus()==1){
  1395. if (in.getPayDep()!=null)update.setPayDep(in.getPayDep());
  1396. if (in.getApplyDep()!=null)update.setApplyDep(in.getApplyDep());
  1397. if (in.getRemarks()!=null)update.setRemarks(in.getRemarks());
  1398. if (in.getEaaid()!=null)update.setEaaid(in.getEaaid());
  1399. addExpenseAccountExamine(in.getId(),update);
  1400. if (outExpenseAccount.getStatus()==0){
  1401. addExpenseAccountLogAndNoticEmail(outExpenseAccount,outExpenseAccount.getStatus(),"发起报销审核");
  1402. }else if (outExpenseAccount.getStatus()==3){
  1403. addExpenseAccountLogAndNoticEmail(outExpenseAccount,4,"修改并发起审核");
  1404. }
  1405. update.setStatus(in.getStatus());
  1406. update.setProcessStatus(outExpenseAccount.getProcessStatus());
  1407. update.setId(in.getId());
  1408. update.setExamineName(outExpenseAccount.getExamineName());
  1409. expenseAccountMapper.updateByPrimaryKeySelective(update);
  1410. // expenseAccountMapper.updateByOrderRemarks(in.getId());
  1411. }else if (in.getStatus()==4){
  1412. expenseAccountMapper.updateByMainId(in.getId(),0);
  1413. addExpenseAccountLog(in.getId(),4,0,TokenManager.getAdminId(),"撤销报销审核");
  1414. List<ExpenseRelationship> list = expenseRelationshipMapper.selectById(in.getId());
  1415. //撤销日志处理,先将日志改在挂子编号上,在新发起的时候再从自编号加到新编号上
  1416. if (list.size()==1){
  1417. ExpenseRelationship e =list.get(0);
  1418. expenseAccountLogMapper.updateUseLogByUseId(e.getEaId(),e.getId());
  1419. }else {
  1420. List<OutExpenseAccountLog> outExpenseAccountLogs = expenseAccountLogMapper.selectByEaid(in.getId());
  1421. for (int i=0;i<list.size();i++){
  1422. ExpenseRelationship e =list.get(i);
  1423. for (OutExpenseAccountLog eal : outExpenseAccountLogs) {
  1424. eal.setEaid(e.getEaId());
  1425. }
  1426. expenseAccountLogMapper.insertBatch(outExpenseAccountLogs);
  1427. }
  1428. }
  1429. expenseAccountLogMapper.deleteByeaid(in.getId());
  1430. expenseRelationshipMapper.deleteByPrimaryKey(in.getId());
  1431. expenseAccountMapper.deleteByPrimaryKey(in.getId());
  1432. }
  1433. return 1;
  1434. }
  1435. @Override
  1436. public Object mainList(ExpenseMainListInput in) {
  1437. Map<String,Object> params = new HashMap<>();
  1438. if (in.getStatus()!=null) params.put("status",in.getStatus());
  1439. if (in.getCheckNo()!=null) params.put("checkNo",in.getCheckNo());
  1440. if (in.getListStatus()!=null) params.put("listStatus",in.getListStatus());
  1441. if (in.getUsername() != null)params.put("username",in.getUsername());
  1442. if (in.getStartTime()!=null && in.getEndTime() !=null) {
  1443. params.put("startTime",in.getStartTime());
  1444. params.put("endTime",in.getEndTime()+" 23:59:59");
  1445. }
  1446. //1-3s是不同人的审核
  1447. if (in.getProcessStatus()!=null&&in.getProcessStatus()==1){
  1448. // 财务审核,复审,出纳都是财务与经理
  1449. if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){
  1450. in.setProcessStatus(6);
  1451. }else if (TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)||TokenManager.hasRole(AFTConstants.APPROVAL_DECISION_ASSISTANT)){
  1452. in.setProcessStatus(5);
  1453. }else if(TokenManager.hasRole(AFTConstants.VICE_CEO)){
  1454. //先不做财务查看权限
  1455. in.setProcessStatus(7);
  1456. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_MANAGER)){
  1457. in.setProcessStatus(1);
  1458. }else if (TokenManager.hasRole(AFTConstants.SALESMAN_ADMIN)){
  1459. in.setProcessStatus(2);
  1460. }else if (TokenManager.hasRole(AFTConstants.FINANCE)||TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1461. in.setProcessStatus(3);
  1462. }
  1463. }else {
  1464. in.setProcessStatus(0);
  1465. }
  1466. if (in.getProcessStatus()!=null){
  1467. params.put("aid",TokenManager.getAdminId());
  1468. params.put("roleType",in.getProcessStatus());
  1469. if (in.getProcessStatus()==3){
  1470. params.put("deps",departmentService.selectMyDeps());
  1471. }
  1472. }
  1473. Pagination<?> page = findPage("ExpenseMainList", "ExpenseMainCount", params, in.getPageNo(), in.getPageSize());
  1474. List<MainExpenseAccount> list = (List<MainExpenseAccount>) page.getList();
  1475. if (!list.isEmpty()){
  1476. List<Integer> collect = list.stream().map(MainExpenseAccount::getId).collect(Collectors.toList());
  1477. List<MainExpenseAccount> mainExpenseAccounts = expenseAccountMapper.selectSonByMainIds(collect);
  1478. for (MainExpenseAccount e : list) {
  1479. for (MainExpenseAccount me : mainExpenseAccounts) {
  1480. if (e.getId().equals(me.getId())){
  1481. e.setSonList(me.getSonList());
  1482. }
  1483. }
  1484. }
  1485. }
  1486. return page;
  1487. }
  1488. @Override
  1489. public Pagination<?> detailsList(InputDetailsListBo in) {
  1490. Map<String,Object> params = new HashMap<>();
  1491. if(in.getStatus()!=null)params.put("status",in.getStatus());
  1492. if(in.getType()!=null)params.put("type",in.getType());
  1493. if (in.getDepId()!=null){
  1494. ArrayList<Object> deps = new ArrayList<>();
  1495. deps.add(in.getDepId());
  1496. params.put("deps",deps);
  1497. }
  1498. if (in.getContractNo()!=null)params.put("contractNo",in.getContractNo());
  1499. if (in.getBuyerName()!=null)params.put("buyerName",in.getBuyerName());
  1500. if (in.getAid()!=null)params.put("aid",in.getAid());
  1501. if (in.getApplyDep()!=null)params.put("applyDep",in.getApplyDep());
  1502. if (in.getPayDep() != null)params.put("payDep",in.getPayDep());
  1503. if (in.getStartTime()!=null)params.put("startTime",in.getStartTime());
  1504. if (in.getEndTime()!=null)params.put("endTime",in.getEndTime());
  1505. if (in.getCheckNo()!=null)params.put("checkNo",in.getCheckNo());
  1506. params.put("current",TokenManager.getAdminId());
  1507. if (in.getMyType()!=null){
  1508. params.put("myType",in.getMyType());
  1509. }else {
  1510. params.put("myType",0);
  1511. }
  1512. //0=自己 1=财务 2=财务经理 3=超管
  1513. if (TokenManager.hasRole(AFTConstants.SUPERADMIN)||TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)){
  1514. params.put("roleType",3);
  1515. }else if (TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1516. params.put("roleType",2);
  1517. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  1518. params.put("roleType",1);
  1519. params.put("current",TokenManager.getAdminId());
  1520. }else{
  1521. params.put("roleType",0);
  1522. params.put("aid",TokenManager.getAdminId());
  1523. }
  1524. return findPagePlus("ExpenseDetailsList","ExpenseDetailsCount","ExpenseDetailsTotalAmount",params,in.getPageNo(),in.getPageSize());
  1525. }
  1526. @Override
  1527. public List<ExpenseAccount> selectByIds(List<String> list) {
  1528. return expenseAccountMapper.selectByIds(list);
  1529. }
  1530. @Override
  1531. public int selectCountByIdsAndType(List<String> list, Integer i) {
  1532. return expenseAccountMapper.selectCountByIdsAndType(list,i);
  1533. }
  1534. @Override
  1535. public Object updateRealAmount(Integer type, Integer id, BigDecimal amount) {
  1536. ExpenseAccountDetails ea= new ExpenseAccountDetails();
  1537. ea.setId(id);
  1538. ea.setRealAmount(amount);
  1539. String name=TokenManager.getAdminToken().getName();
  1540. Integer eaid = expenseAccountDetailsMapper.selectExpenseAccountById(id);
  1541. addExpenseAccountLog(eaid,5,3,TokenManager.getAdminId(),String.format("财务[%s]修改实际报销金额为%s。",name,amount));
  1542. expenseAccountDetailsMapper.updateByPrimaryKeySelective(ea);
  1543. expenseAccountMapper.updateSonByDetId(id);
  1544. expenseAccountMapper.updateMainByDetId(id);
  1545. return 1;
  1546. }
  1547. /**
  1548. * 计算当前报销显示抵扣金额
  1549. * @param useEa
  1550. */
  1551. private void pushExpenseAccountGetDebitAmount(OutExpenseAccount useEa) {
  1552. ExpenseAccount debit = expenseAccountMapper.selectByPrimaryKey(useEa.getDebitId());
  1553. List<ExpenseAccount> list1 = expenseAccountMapper.selectByDebitId(useEa.getDebitId());
  1554. BigDecimal count =debit.getRealAmount();
  1555. for (ExpenseAccount e : list1) {
  1556. if (!e.getId().equals(useEa.getId())){
  1557. count=count.subtract(e.getSettlementAmount());
  1558. }
  1559. }
  1560. if (count.compareTo(new BigDecimal(0))<0)count=new BigDecimal(0);
  1561. useEa.setDebitTotalAmount(count);
  1562. }
  1563. /**
  1564. * 判断角色是否已经审核
  1565. * @param id 编号
  1566. * @param processStatus 审核状态
  1567. * @return 0 无需审核 1 需要审核
  1568. */
  1569. private Integer pushExpenseAccountGetExamine(Integer id,Integer processStatus) {
  1570. List<ExpenseAccountExamine> list = expenseAccountExamineMapper.selectByEaidAndProcessStatus(id, processStatus);
  1571. boolean flag=false;
  1572. for (ExpenseAccountExamine e : list) {
  1573. if (e.getAuditor().equals(TokenManager.getAdminId())){
  1574. if (e.getStatus()==0){
  1575. flag=true;
  1576. break;
  1577. }
  1578. }
  1579. }
  1580. if (flag){
  1581. return 1;
  1582. }else {
  1583. return 0;
  1584. }
  1585. }
  1586. @Override
  1587. public Object updateType(InputExpenseAccount in) {
  1588. expenseAccountDetailsMapper.deleteByeaid(in.getId());
  1589. if (in.getType()!=2)in.setSecondaryType(0);
  1590. in.setSettlementAmount(new BigDecimal(0));
  1591. return expenseAccountMapper.updateByPrimaryKeySelective(in);
  1592. }
  1593. @Override
  1594. public List<OutExpenseAccount> selectDebitOrder(String depId,Integer id) {
  1595. String aid= TokenManager.getAdminId();
  1596. List<OutExpenseAccount> list = expenseAccountMapper.selectByaidAndType(aid, depId, 4,id);
  1597. return list;
  1598. }
  1599. private void addParam(InputPageListBo in, Map<String, Object> param) {
  1600. if (in.getStartTime()!=null&&in.getEndTime()!=null){
  1601. param.put("startTime",in.getStartTime());
  1602. param.put("endTime",in.getEndTime()+" 23:59:59");
  1603. }
  1604. if (in.getStatus()!=null)param.put("status",in.getStatus());
  1605. //如果前端未传 0自己 1审核的参数,默认自己,在审核列表的时候判断Aid,在
  1606. param.put("auditor",TokenManager.getAdminId());
  1607. if(in.getProcessStatus()==null){
  1608. if(TokenManager.hasRole(AFTConstants.FINANCE_MANAGER)){
  1609. //财务经理查看权限
  1610. in.setProcessStatus(5);
  1611. param.put("auditor",TokenManager.getAdminId());
  1612. }else if(TokenManager.hasRole(AFTConstants.FINANCE_ADMIN)||TokenManager.hasRole(AFTConstants.SUPERADMIN)||
  1613. TokenManager.hasRole(AFTConstants.VICE_CEO)|| TokenManager.hasRole(AFTConstants.APPROVAL_DECISION)){
  1614. //财务管理员和超级管理员查看全部
  1615. in.setProcessStatus(6);
  1616. param.put("auditor",TokenManager.getAdminId());
  1617. if(StringUtils.isNotBlank(in.getAid()))param.put("aid",in.getAid());
  1618. }else if (TokenManager.hasRole(AFTConstants.FINANCE)){
  1619. //0 设置为财务查看
  1620. param.put("auditor",TokenManager.getAdminId());
  1621. //如果是空设置为
  1622. if (in.getProcessStatus()==null)in.setProcessStatus(2);
  1623. }else {
  1624. in.setProcessStatus(0);
  1625. }
  1626. }
  1627. param.put("processStatus",in.getProcessStatus());
  1628. if (in.getRoleType()==null){
  1629. param.put("roleType",0);
  1630. }else {
  1631. param.put("roleType",in.getRoleType());
  1632. }
  1633. if(in.getProcessStatus()==2||in.getProcessStatus()==1){
  1634. if (in.getAid()!=null){
  1635. param.put("aid",in.getAid());
  1636. }
  1637. }
  1638. if (in.getExamineStatus()!=null){
  1639. param.put("examineStatus",in.getExamineStatus());
  1640. }else {
  1641. param.put("examineStatus",0);
  1642. }
  1643. if (in.getContractNo()!=null)param.put("contractNo",in.getContractNo());
  1644. if (in.getDepId()!=null)param.put("depId",in.getDepId());
  1645. if (in.getApplyDep()!=null)param.put("applyDep",in.getApplyDep());
  1646. if (in.getPayDep()!=null)param.put("payDep",in.getPayDep());
  1647. if (in.getType()!=null)param.put("type",in.getType());
  1648. if (in.getUsername()!=null)param.put("username",in.getUsername());
  1649. if (in.getCheckNo()!=null)param.put("checkNo",in.getCheckNo());
  1650. if (in.getSecondaryType()!=null)param.put("secondaryType",in.getSecondaryType());
  1651. //固定传0查看子列表
  1652. if (in.getExpenseMain()==null)in.setExpenseMain(0);
  1653. if (in.getExpenseMain()!=null)param.put("expenseMain",in.getExpenseMain());
  1654. }
  1655. @Override
  1656. @Cacheable(value = "expenseAccountStatistics#300", key = "'page:'+#name+',depId'+#depId+',startTime'+#startTime+',endTime'+#endTime+'lander'+#lander")
  1657. public List<OutExpenseAccountStatistics> statistics(String name,String depId,String startTime,String endTime,String lander) {
  1658. if (endTime != null) endTime = endTime + " 23:59:59";
  1659. List<OutMyEAStatistics> list = expenseAccountMapper.selectStatistics(name, depId, startTime, endTime, lander);
  1660. List<OutExpenseAccountStatistics> results = new ArrayList<>();
  1661. Map<String, Object> map = new HashMap<>();
  1662. for (OutMyEAStatistics e : list) {
  1663. OutExpenseAccountStatistics out = new OutExpenseAccountStatistics(e.getAid());
  1664. out.setAname(e.getAname());
  1665. if (e.getStatus() == 2) {
  1666. if (e.getType() < 4) {
  1667. out.setBx(1);
  1668. out.setBxAmount(e.getTotalAmount());
  1669. if (!e.getDebitId().equals(0)) {
  1670. out.setDk(1);
  1671. out.setDkAmount(e.getBxdkAmount());
  1672. }
  1673. } else if (e.getType() == 4) {
  1674. out.setJz(1);
  1675. out.setJzAmount(e.getTotalAmount());
  1676. } else if (e.getType() == 5) {
  1677. out.setDk(1);
  1678. out.setDkAmount(e.getTotalAmount());
  1679. }
  1680. if (e.getTargetType() == 0) {
  1681. out.setGdfyAmount(e.getTotalAmount());
  1682. } else if (e.getTargetType() == 1) {
  1683. out.setBxddAmount(e.getTotalAmount());
  1684. }
  1685. } else if (e.getStatus() == 1) {
  1686. out.setWshs(1);
  1687. } else if (e.getStatus() == 3) {
  1688. out.setBhs(1);
  1689. }
  1690. //如果不存在就新增
  1691. if (!map.keySet().contains(out.getAid())) {
  1692. List<OutExpenseAccountStatistics> list2 = new ArrayList<>();
  1693. list2.add(out);
  1694. map.put(out.getAid(), list2);
  1695. } else {
  1696. List<OutExpenseAccountStatistics> list2 = (List<OutExpenseAccountStatistics>) map.get(e.getAid());
  1697. list2.add(out);
  1698. }
  1699. }
  1700. for (String s : map.keySet()) {
  1701. List<OutExpenseAccountStatistics> list3 = (List<OutExpenseAccountStatistics>) map.get(s);
  1702. if (!list3.isEmpty()) {
  1703. OutExpenseAccountStatistics out = new OutExpenseAccountStatistics(s);
  1704. for (OutExpenseAccountStatistics e : list3) {
  1705. if (out.getAname() == null) out.setAname(e.getAname());
  1706. if (e.getBx() != null) out.setBx(out.getBx() + e.getBx());
  1707. if (e.getJz() != null) out.setJz(out.getJz() + e.getJz());
  1708. if (e.getDk() != null) out.setDk(out.getDk() + e.getDk());
  1709. if (e.getBxAmount() != null) out.setBxAmount(out.getBxAmount().add(e.getBxAmount()));
  1710. if (e.getJzAmount() != null) out.setJzAmount(out.getJzAmount().add(e.getJzAmount()));
  1711. if (e.getDkAmount() != null) out.setDkAmount(out.getDkAmount().add(e.getDkAmount()));
  1712. if (e.getGdfyAmount() != null) out.setGdfyAmount(out.getGdfyAmount().add(e.getGdfyAmount()));
  1713. if (e.getBxddAmount() != null) out.setBxddAmount(out.getBxddAmount().add(e.getBxddAmount()));
  1714. if (e.getWshs() != null) out.setWshs(out.getWshs() + e.getWshs());
  1715. if (e.getBhs() != null) out.setBhs(out.getBhs() + e.getBhs());
  1716. }
  1717. BigDecimal totalAmount = new BigDecimal(0);
  1718. totalAmount = totalAmount.add(out.getBxAmount()).add(out.getJzAmount()).subtract(out.getDkAmount());
  1719. out.setTotalAmount(totalAmount);
  1720. results.add(out);
  1721. }
  1722. }
  1723. return results;
  1724. }
  1725. @Override
  1726. @CacheEvict(value = "expenseAccountStatistics#300", allEntries = true)
  1727. public void statisticsEvict(String name, String depId, String startTime, String endTime) {
  1728. LoggerUtils.debug(getClass(), "报销统计缓存清除。");
  1729. }
  1730. @Override
  1731. public Object pushSupplementCheckNo() {
  1732. List<ExpenseAccount> list = expenseAccountMapper.selectByCheckNoNot();
  1733. if (!list.isEmpty()){
  1734. list.forEach(e ->{
  1735. ExpenseAccount ea=new ExpenseAccount();
  1736. ea.setId(e.getId());
  1737. ea.setCheckNo(idGenerator.getNOgenerateId());
  1738. expenseAccountMapper.updateByPrimaryKeySelective(ea);
  1739. });
  1740. return 1;
  1741. }
  1742. return 0;
  1743. }
  1744. @Override
  1745. public void pushAll() {
  1746. List<ExpenseAccount> list = expenseAccountMapper.selectAllMain();
  1747. for (ExpenseAccount ea : list) {
  1748. List<ExpenseAccount> expenseAccounts = expenseAccountMapper.selectListByMainId(ea.getId());
  1749. List<String> typeList=new ArrayList<>();
  1750. for (ExpenseAccount e : expenseAccounts) {
  1751. StringBuffer types=new StringBuffer();
  1752. if (e.getType()!=0){
  1753. types=types.append(EATypes.getDescByCode(e.getType()));
  1754. }else {
  1755. types=types.append(e.getTypeOther());
  1756. }
  1757. if (e.getType()==2){
  1758. if (e.getSecondaryType()==0){
  1759. types=types.append("-").append(e.getSecondaryTypeOther());
  1760. }else{
  1761. String descByCode = EAsecondaryTypes.getDescByCode(e.getSecondaryType());
  1762. types=types.append("-").append(descByCode);
  1763. }
  1764. }
  1765. if (!typeList.contains(types.toString()))typeList.add(types.toString());
  1766. }
  1767. String typeStr = String.join(",", typeList);
  1768. ExpenseAccount update=new ExpenseAccount();
  1769. update.setId(ea.getId());
  1770. update.setTypeOther(typeStr);
  1771. expenseAccountMapper.updateByPrimaryKeySelective(update);
  1772. }
  1773. }
  1774. }