ExpenseAccountServiceImpl.java 81 KB

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