ContractSaleInvoiceController.java 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. package cn.com.lzt.contractsaleinvoice.controller;
  2. import java.io.File;
  3. import java.io.FileNotFoundException;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. import java.io.UnsupportedEncodingException;
  8. import java.net.URI;
  9. import java.text.DecimalFormat;
  10. import java.text.ParseException;
  11. import java.util.ArrayList;
  12. import java.util.Date;
  13. import java.util.HashMap;
  14. import java.util.List;
  15. import java.util.Map;
  16. import java.util.Set;
  17. import javax.servlet.http.HttpServletRequest;
  18. import javax.servlet.http.HttpServletResponse;
  19. import javax.validation.ConstraintViolation;
  20. import javax.validation.Validator;
  21. import javax.xml.parsers.DocumentBuilder;
  22. import javax.xml.parsers.DocumentBuilderFactory;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.apache.log4j.Logger;
  25. import org.jeecgframework.core.beanvalidator.BeanValidators;
  26. import org.jeecgframework.core.common.controller.BaseController;
  27. import org.jeecgframework.core.common.exception.BusinessException;
  28. import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
  29. import org.jeecgframework.core.common.model.json.AjaxJson;
  30. import org.jeecgframework.core.common.model.json.DataGrid;
  31. import org.jeecgframework.core.constant.Globals;
  32. import org.jeecgframework.core.util.DateUtils;
  33. import org.jeecgframework.core.util.ExceptionUtil;
  34. import org.jeecgframework.core.util.MyBeanUtils;
  35. import org.jeecgframework.core.util.ResourceUtil;
  36. import org.jeecgframework.core.util.StringUtil;
  37. import org.jeecgframework.poi.excel.ExcelImportUtil;
  38. import org.jeecgframework.poi.excel.entity.ExportParams;
  39. import org.jeecgframework.poi.excel.entity.ImportParams;
  40. import org.jeecgframework.poi.excel.entity.vo.NormalExcelConstants;
  41. import org.jeecgframework.tag.core.easyui.TagUtil;
  42. import org.jeecgframework.tag.vo.datatable.SortDirection;
  43. import org.jeecgframework.web.system.pojo.base.DictEntity;
  44. import org.jeecgframework.web.system.pojo.base.TSUser;
  45. import org.jeecgframework.web.system.service.SystemService;
  46. import org.springframework.beans.factory.annotation.Autowired;
  47. import org.springframework.http.HttpHeaders;
  48. import org.springframework.http.HttpStatus;
  49. import org.springframework.http.MediaType;
  50. import org.springframework.http.ResponseEntity;
  51. import org.springframework.stereotype.Controller;
  52. import org.springframework.ui.ModelMap;
  53. import org.springframework.util.FileCopyUtils;
  54. import org.springframework.web.bind.annotation.PathVariable;
  55. import org.springframework.web.bind.annotation.RequestBody;
  56. import org.springframework.web.bind.annotation.RequestMapping;
  57. import org.springframework.web.bind.annotation.RequestMethod;
  58. import org.springframework.web.bind.annotation.ResponseBody;
  59. import org.springframework.web.bind.annotation.ResponseStatus;
  60. import org.springframework.web.multipart.MultipartFile;
  61. import org.springframework.web.multipart.MultipartHttpServletRequest;
  62. import org.springframework.web.servlet.ModelAndView;
  63. import org.springframework.web.util.UriComponentsBuilder;
  64. import org.w3c.dom.Document;
  65. import org.w3c.dom.Node;
  66. import org.w3c.dom.NodeList;
  67. import com.xcgl.utils.XcglDateUtils;
  68. import cn.com.lzt.contractsale.entity.ContractSaleEntity;
  69. import cn.com.lzt.contractsaleinvoice.entity.ContractSaleInvoiceEntity;
  70. import cn.com.lzt.contractsaleinvoice.service.ContractSaleInvoiceServiceI;
  71. import cn.com.lzt.goodsinfo.entity.TBGoodsInfoEntity;
  72. import cn.com.lzt.priceinfo.entity.TBPriceInfoEntity;
  73. import cn.com.lzt.tools.DecimalUtils;
  74. /**
  75. * @Title: Controller
  76. * @Description: 销售开票
  77. * @author onlineGenerator
  78. * @date 2019-05-12 11:45:01
  79. * @version V1.0
  80. *
  81. */
  82. @Controller
  83. @RequestMapping("/contractSaleInvoiceController")
  84. public class ContractSaleInvoiceController extends BaseController {
  85. /**
  86. * Logger for this class
  87. */
  88. private static final Logger logger = Logger.getLogger(ContractSaleInvoiceController.class);
  89. @Autowired
  90. private ContractSaleInvoiceServiceI contractSaleInvoiceService;
  91. @Autowired
  92. private SystemService systemService;
  93. @Autowired
  94. private Validator validator;
  95. /**
  96. * 销售开票列表 页面跳转
  97. *
  98. * @return
  99. */
  100. @RequestMapping(params = "list")
  101. public ModelAndView list(HttpServletRequest request) {
  102. return new ModelAndView("cn/com/lzt/contractsaleinvoice/contractSaleInvoiceList");
  103. }
  104. /**
  105. * 销售开票列表 页面跳转
  106. *
  107. * @return
  108. */
  109. @RequestMapping(params = "listByContractid")
  110. public ModelAndView listByContractid(String contractid,HttpServletRequest request) {
  111. if(StringUtils.isEmpty(contractid))
  112. contractid = "1";
  113. request.setAttribute("contractid", contractid);
  114. return new ModelAndView("cn/com/lzt/contractsaleinvoice/contractSaleInvoiceListReadonly");
  115. }
  116. /**
  117. * easyui AJAX请求数据
  118. *
  119. * @param request
  120. * @param response
  121. * @param dataGrid
  122. * @param user
  123. */
  124. @RequestMapping(params = "datagrid")
  125. public void datagrid(ContractSaleInvoiceEntity contractSaleInvoice,HttpServletRequest request, HttpServletResponse response, DataGrid dataGrid) {
  126. CriteriaQuery cq = new CriteriaQuery(ContractSaleInvoiceEntity.class, dataGrid);
  127. //查询条件组装器
  128. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, contractSaleInvoice, request.getParameterMap());
  129. try{
  130. //自定义追加查询条件
  131. }catch (Exception e) {
  132. throw new BusinessException(e.getMessage());
  133. }
  134. cq.add();
  135. this.contractSaleInvoiceService.getDataGridReturn(cq, true);
  136. if (StringUtils.isNotEmpty(contractSaleInvoice.getContractid())) {
  137. List<ContractSaleInvoiceEntity> resualtlist = dataGrid.getResults();
  138. Double sum = 0.0;
  139. if (resualtlist.size() != 0) {
  140. for (ContractSaleInvoiceEntity aa : resualtlist) {
  141. sum = sum + Double.valueOf(aa.getInvoiceamount());
  142. }
  143. }
  144. // 格式化为小数点2位数
  145. DecimalFormat df = new DecimalFormat("#.00");
  146. String total_salary = df.format(sum);
  147. dataGrid.setFooter("invoiceamount:累计开票:,periodBegin:"+total_salary);
  148. }
  149. TagUtil.datagrid(response, dataGrid);
  150. }
  151. /**
  152. * 删除销售开票
  153. *
  154. * @return
  155. */
  156. @RequestMapping(params = "doDel")
  157. @ResponseBody
  158. public AjaxJson doDel(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest request) {
  159. String message = null;
  160. AjaxJson j = new AjaxJson();
  161. contractSaleInvoice = systemService.getEntity(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  162. message = "销售开票删除成功";
  163. try{
  164. contractSaleInvoiceService.delete(contractSaleInvoice);
  165. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  166. }catch(Exception e){
  167. e.printStackTrace();
  168. message = "销售开票删除失败";
  169. throw new BusinessException(e.getMessage());
  170. }
  171. j.setMsg(message);
  172. return j;
  173. }
  174. /**
  175. * 批量删除销售开票
  176. *
  177. * @return
  178. */
  179. @RequestMapping(params = "doBatchDel")
  180. @ResponseBody
  181. public AjaxJson doBatchDel(String ids,HttpServletRequest request){
  182. String message = null;
  183. AjaxJson j = new AjaxJson();
  184. message = "销售开票删除成功";
  185. try{
  186. for(String id:ids.split(",")){
  187. ContractSaleInvoiceEntity contractSaleInvoice = systemService.getEntity(ContractSaleInvoiceEntity.class,
  188. id
  189. );
  190. contractSaleInvoiceService.delete(contractSaleInvoice);
  191. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  192. }
  193. }catch(Exception e){
  194. e.printStackTrace();
  195. message = "销售开票删除失败";
  196. throw new BusinessException(e.getMessage());
  197. }
  198. j.setMsg(message);
  199. return j;
  200. }
  201. /**
  202. * 添加销售开票
  203. *
  204. * @param ids
  205. * @return
  206. */
  207. @RequestMapping(params = "doAdd")
  208. @ResponseBody
  209. public AjaxJson doAdd(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest request) {
  210. String message = null;
  211. AjaxJson j = new AjaxJson();
  212. message = "销售开票添加成功";
  213. try{
  214. String sql = " FROM ContractSaleInvoiceEntity where invoiceno = ?";
  215. List<ContractSaleInvoiceEntity> hisIncoiceList = systemService.findHql(sql, contractSaleInvoice.getInvoiceno());
  216. if(hisIncoiceList.size() > 0) {
  217. j.setSuccess(false);
  218. j.setMsg("发票号已经存在,请重新填写");
  219. return j;
  220. }
  221. //设置初始状态
  222. contractSaleInvoice.setBusstatus(Globals.CONTRACT_STATUS_INVOICE);
  223. contractSaleInvoice.setIncomeamount(DecimalUtils.ZERO);
  224. TSUser user = ResourceUtil.getSessionUser();
  225. contractSaleInvoice.setInvoiceopt(user.getId());
  226. contractSaleInvoice.setInvoiceoptname(user.getRealName());
  227. setFinancialData(contractSaleInvoice);
  228. contractSaleInvoiceService.save(contractSaleInvoice);
  229. systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
  230. //回写合同开票合计
  231. updateContractSaleTotalInvoice(contractSaleInvoice.getContractid(), contractSaleInvoice.getInvoiceamount());
  232. }catch(Exception e){
  233. e.printStackTrace();
  234. message = "销售开票添加失败";
  235. throw new BusinessException(e.getMessage());
  236. }
  237. j.setMsg(message);
  238. return j;
  239. }
  240. /**
  241. * 更新销售开票
  242. *
  243. * @param ids
  244. * @return
  245. */
  246. @RequestMapping(params = "doUpdate")
  247. @ResponseBody
  248. public AjaxJson doUpdate(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest request) {
  249. String message = null;
  250. AjaxJson j = new AjaxJson();
  251. message = "销售开票更新成功";
  252. String msg = checkEntity(contractSaleInvoice);
  253. if(StringUtils.isNotEmpty(msg)) {
  254. j.setSuccess(false);
  255. j.setMsg(msg);
  256. return j;
  257. }
  258. ContractSaleInvoiceEntity t = contractSaleInvoiceService.get(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  259. try {
  260. Double changeAmount = DecimalUtils.ZERO;
  261. if(StringUtils.isEmpty(t.getContractid())) {
  262. changeAmount = contractSaleInvoice.getInvoiceamount();
  263. }else {
  264. changeAmount = contractSaleInvoice.getInvoiceamount() - t.getInvoiceamount();
  265. }
  266. MyBeanUtils.copyBeanNotNull2Bean(contractSaleInvoice, t);
  267. setFinancialData(t);
  268. contractSaleInvoiceService.saveOrUpdate(t);
  269. updateContractSaleTotalInvoice(t.getContractid(), changeAmount);
  270. systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
  271. } catch (Exception e) {
  272. e.printStackTrace();
  273. message = "销售开票更新失败";
  274. throw new BusinessException(e.getMessage());
  275. }
  276. j.setMsg(message);
  277. return j;
  278. }
  279. /**
  280. * 销售开票新增页面跳转
  281. *
  282. * @return
  283. */
  284. @RequestMapping(params = "goAdd")
  285. public ModelAndView goAdd(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest req) {
  286. if (StringUtil.isNotEmpty(contractSaleInvoice.getId())) {
  287. contractSaleInvoice = contractSaleInvoiceService.getEntity(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  288. }else{
  289. ContractSaleEntity contractSale = systemService.getEntity(ContractSaleEntity.class, contractSaleInvoice.getContractid());
  290. contractSaleInvoice.setContractname(contractSale.getName());
  291. contractSaleInvoice.setProjectid(contractSale.getProjectid());
  292. contractSaleInvoice.setProjectname(contractSale.getProjectname());
  293. contractSaleInvoice.setCustomerid(contractSale.getCustomerid());
  294. contractSaleInvoice.setCustomername(contractSale.getCustomername());
  295. contractSaleInvoice.setFeetype("wuyefee");
  296. contractSaleInvoice.setInvoicetype("special");
  297. contractSaleInvoice.setInvoicedate(new Date());
  298. }
  299. req.setAttribute("contractSaleInvoicePage", contractSaleInvoice);
  300. return new ModelAndView("cn/com/lzt/contractsaleinvoice/contractSaleInvoice-add");
  301. }
  302. /**
  303. * 销售开票编辑页面跳转
  304. *
  305. * @return
  306. */
  307. @RequestMapping(params = "goUpdate")
  308. public ModelAndView goUpdate(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest req) {
  309. if (StringUtil.isNotEmpty(contractSaleInvoice.getId())) {
  310. contractSaleInvoice = contractSaleInvoiceService.getEntity(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  311. req.setAttribute("contractSaleInvoicePage", contractSaleInvoice);
  312. }
  313. return new ModelAndView("cn/com/lzt/contractsaleinvoice/contractSaleInvoice-update");
  314. }
  315. /**
  316. * 导入功能跳转
  317. *
  318. * @return
  319. */
  320. @RequestMapping(params = "upload")
  321. public ModelAndView upload(HttpServletRequest req) {
  322. req.setAttribute("controller_name","contractSaleInvoiceController");
  323. return new ModelAndView("common/upload/pub_excel_upload");
  324. }
  325. /**
  326. * 导出excel
  327. *
  328. * @param request
  329. * @param response
  330. */
  331. @RequestMapping(params = "exportXls")
  332. public String exportXls(ContractSaleInvoiceEntity contractSaleInvoice,HttpServletRequest request,HttpServletResponse response
  333. , DataGrid dataGrid,ModelMap modelMap) {
  334. CriteriaQuery cq = new CriteriaQuery(ContractSaleInvoiceEntity.class, dataGrid);
  335. cq.addOrder("createDate", SortDirection.desc);
  336. cq.add();
  337. org.jeecgframework.core.extend.hqlsearch.HqlGenerateUtil.installHql(cq, contractSaleInvoice, request.getParameterMap());
  338. List<ContractSaleInvoiceEntity> contractSaleInvoices = this.contractSaleInvoiceService.getListByCriteriaQuery(cq,false);
  339. modelMap.put(NormalExcelConstants.FILE_NAME,"销售合同开票收款"+DateUtils.yyyymmddhhmmss.format(new Date()));
  340. modelMap.put(NormalExcelConstants.CLASS,ContractSaleInvoiceEntity.class);
  341. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("销售合同开票收款列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  342. "导出信息"));
  343. modelMap.put(NormalExcelConstants.DATA_LIST,contractSaleInvoices);
  344. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  345. }
  346. /**
  347. * 导出excel 使模板
  348. *
  349. * @param request
  350. * @param response
  351. */
  352. @RequestMapping(params = "exportXlsByT")
  353. public String exportXlsByT(ContractSaleInvoiceEntity contractSaleInvoice,HttpServletRequest request,HttpServletResponse response
  354. , DataGrid dataGrid,ModelMap modelMap) {
  355. modelMap.put(NormalExcelConstants.FILE_NAME,"销售开票");
  356. modelMap.put(NormalExcelConstants.CLASS,ContractSaleInvoiceEntity.class);
  357. modelMap.put(NormalExcelConstants.PARAMS,new ExportParams("销售开票列表", "导出人:"+ResourceUtil.getSessionUser().getRealName(),
  358. "导出信息"));
  359. modelMap.put(NormalExcelConstants.DATA_LIST,new ArrayList());
  360. return NormalExcelConstants.JEECG_EXCEL_VIEW;
  361. }
  362. @SuppressWarnings("unchecked")
  363. @RequestMapping(params = "importExcel", method = RequestMethod.POST)
  364. @ResponseBody
  365. public AjaxJson importExcel(HttpServletRequest request, HttpServletResponse response) {
  366. AjaxJson j = new AjaxJson();
  367. MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
  368. Map<String, MultipartFile> fileMap = multipartRequest.getFileMap();
  369. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  370. MultipartFile file = entity.getValue();// 获取上传文件对象
  371. ImportParams params = new ImportParams();
  372. params.setTitleRows(2);
  373. params.setHeadRows(1);
  374. params.setNeedSave(true);
  375. try {
  376. List<ContractSaleInvoiceEntity> listContractSaleInvoiceEntitys = ExcelImportUtil.importExcel(file.getInputStream(),ContractSaleInvoiceEntity.class,params);
  377. for (ContractSaleInvoiceEntity contractSaleInvoice : listContractSaleInvoiceEntitys) {
  378. contractSaleInvoiceService.save(contractSaleInvoice);
  379. }
  380. j.setMsg("文件导入成功!");
  381. } catch (Exception e) {
  382. j.setMsg("文件导入失败!");
  383. logger.error(ExceptionUtil.getExceptionMessage(e));
  384. }finally{
  385. try {
  386. file.getInputStream().close();
  387. } catch (IOException e) {
  388. e.printStackTrace();
  389. }
  390. }
  391. }
  392. return j;
  393. }
  394. @RequestMapping(method = RequestMethod.GET)
  395. @ResponseBody
  396. public List<ContractSaleInvoiceEntity> list() {
  397. List<ContractSaleInvoiceEntity> listContractSaleInvoices=contractSaleInvoiceService.getList(ContractSaleInvoiceEntity.class);
  398. return listContractSaleInvoices;
  399. }
  400. @RequestMapping(value = "/{id}", method = RequestMethod.GET)
  401. @ResponseBody
  402. public ResponseEntity<?> get(@PathVariable("id") String id) {
  403. ContractSaleInvoiceEntity task = contractSaleInvoiceService.get(ContractSaleInvoiceEntity.class, id);
  404. if (task == null) {
  405. return new ResponseEntity(HttpStatus.NOT_FOUND);
  406. }
  407. return new ResponseEntity(task, HttpStatus.OK);
  408. }
  409. @RequestMapping(method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
  410. @ResponseBody
  411. public ResponseEntity<?> create(@RequestBody ContractSaleInvoiceEntity contractSaleInvoice, UriComponentsBuilder uriBuilder) {
  412. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  413. Set<ConstraintViolation<ContractSaleInvoiceEntity>> failures = validator.validate(contractSaleInvoice);
  414. if (!failures.isEmpty()) {
  415. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  416. }
  417. //保存
  418. try{
  419. contractSaleInvoiceService.save(contractSaleInvoice);
  420. } catch (Exception e) {
  421. e.printStackTrace();
  422. return new ResponseEntity(HttpStatus.NO_CONTENT);
  423. }
  424. //按照Restful风格约定,创建指向新任务的url, 也可以直接返回id或对象.
  425. String id = contractSaleInvoice.getId();
  426. URI uri = uriBuilder.path("/rest/contractSaleInvoiceController/" + id).build().toUri();
  427. HttpHeaders headers = new HttpHeaders();
  428. headers.setLocation(uri);
  429. return new ResponseEntity(headers, HttpStatus.CREATED);
  430. }
  431. @RequestMapping(value = "/{id}", method = RequestMethod.PUT, consumes = MediaType.APPLICATION_JSON_VALUE)
  432. public ResponseEntity<?> update(@RequestBody ContractSaleInvoiceEntity contractSaleInvoice) {
  433. //调用JSR303 Bean Validator进行校验,如果出错返回含400错误码及json格式的错误信息.
  434. Set<ConstraintViolation<ContractSaleInvoiceEntity>> failures = validator.validate(contractSaleInvoice);
  435. if (!failures.isEmpty()) {
  436. return new ResponseEntity(BeanValidators.extractPropertyAndMessage(failures), HttpStatus.BAD_REQUEST);
  437. }
  438. //保存
  439. try{
  440. setFinancialData(contractSaleInvoice);
  441. contractSaleInvoiceService.saveOrUpdate(contractSaleInvoice);
  442. } catch (Exception e) {
  443. e.printStackTrace();
  444. return new ResponseEntity(HttpStatus.NO_CONTENT);
  445. }
  446. //按Restful约定,返回204状态码, 无内容. 也可以返回200状态码.
  447. return new ResponseEntity(HttpStatus.NO_CONTENT);
  448. }
  449. @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
  450. @ResponseStatus(HttpStatus.NO_CONTENT)
  451. public void delete(@PathVariable("id") String id) {
  452. contractSaleInvoiceService.deleteEntityById(ContractSaleInvoiceEntity.class, id);
  453. }
  454. /**
  455. * 作废开票
  456. *
  457. * @return
  458. */
  459. @RequestMapping(params = "doCancel")
  460. @ResponseBody
  461. public AjaxJson doCancel(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest request) {
  462. String message = null;
  463. AjaxJson j = new AjaxJson();
  464. contractSaleInvoice = systemService.getEntity(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  465. Double invoiceamount = contractSaleInvoice.getInvoiceamount();
  466. message = "作废发票成功";
  467. try{
  468. contractSaleInvoice.setInvoiceamount(DecimalUtils.ZERO);
  469. contractSaleInvoice.setInvoicedate(null);
  470. contractSaleInvoice.setInvoiceopt(null);
  471. contractSaleInvoice.setInvoiceoptname(null);
  472. contractSaleInvoice.setInvoiceno(null);
  473. contractSaleInvoice.setBusstatus(Globals.CONTRACT_STATUS_CANCELINVOICE);
  474. contractSaleInvoiceService.save(contractSaleInvoice);
  475. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  476. //回写合同开票合计
  477. updateContractSaleTotalInvoice(contractSaleInvoice.getContractid(), 0-invoiceamount);
  478. }catch(Exception e){
  479. e.printStackTrace();
  480. message = "作废发票失败";
  481. throw new BusinessException(e.getMessage());
  482. }
  483. j.setMsg(message);
  484. return j;
  485. }
  486. /**
  487. * 收款
  488. *
  489. * @return
  490. */
  491. @RequestMapping(params = "doIncome")
  492. @ResponseBody
  493. public AjaxJson doIncome(ContractSaleInvoiceEntity contractSaleInvoice, HttpServletRequest request) {
  494. String message = null;
  495. AjaxJson j = new AjaxJson();
  496. contractSaleInvoice = systemService.getEntity(ContractSaleInvoiceEntity.class, contractSaleInvoice.getId());
  497. message = "收款成功";
  498. try{
  499. contractSaleInvoice.setIncomeamount(contractSaleInvoice.getInvoiceamount());
  500. contractSaleInvoice.setIncomedate(new Date());
  501. contractSaleInvoice.setBusstatus(Globals.CONTRACT_STATUS_INCOME);
  502. TSUser user = ResourceUtil.getSessionUser();
  503. contractSaleInvoice.setIncomeopt(user.getId());
  504. contractSaleInvoice.setIncomeoptname(user.getRealName());
  505. contractSaleInvoiceService.save(contractSaleInvoice);
  506. systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
  507. }catch(Exception e){
  508. e.printStackTrace();
  509. message = "收款失败";
  510. throw new BusinessException(e.getMessage());
  511. }
  512. j.setMsg(message);
  513. return j;
  514. }
  515. private void updateContractSaleTotalInvoice(String cid,Double invoiceamount) {
  516. if(StringUtils.isNotEmpty(cid)){
  517. String updateSql = "update t_b_contract_sale c set c.totalinvoice = c.totalinvoice+? where c.id =? ";
  518. systemService.executeSql(updateSql,invoiceamount,cid);
  519. }
  520. }
  521. /**
  522. * 文件上传通用跳转
  523. *
  524. * @param req
  525. * @return
  526. */
  527. @RequestMapping(params = "commonUpload")
  528. public ModelAndView commonUpload(HttpServletRequest req) {
  529. String extend = req.getParameter("extend");
  530. req.setAttribute("extend", "xml");
  531. req.setAttribute("uploader", "contractSaleInvoiceController.do?ajaxSaveFile");
  532. return new ModelAndView("cn/com/lzt/publicpage/uploadView");
  533. }
  534. @RequestMapping(params = "ajaxSaveFile")
  535. @ResponseBody
  536. public AjaxJson ajaxSaveFile(MultipartHttpServletRequest request) {
  537. AjaxJson ajaxJson = new AjaxJson();
  538. Map<String, Object> attributes = new HashMap<String, Object>();
  539. try {
  540. Map<String, MultipartFile> fileMap = request.getFileMap();
  541. String uploadbasepath = ResourceUtil.getConfigByName("uploadpath");
  542. // 文件数据库保存路径
  543. String path = uploadbasepath + "/";// 文件保存在硬盘的相对路径
  544. String realPath = request.getSession().getServletContext().getRealPath("/") + "/" + path;// 文件的硬盘真实路径
  545. realPath += DateUtils.getDataString(DateUtils.yyyyMMdd) + "/";
  546. path += DateUtils.getDataString(DateUtils.yyyyMMdd) + "/";
  547. File file = new File(realPath);
  548. if (!file.exists()) {
  549. file.mkdirs();// 创建文件时间子目录
  550. }
  551. if(fileMap != null && !fileMap.isEmpty()){
  552. for (Map.Entry<String, MultipartFile> entity : fileMap.entrySet()) {
  553. MultipartFile mf = entity.getValue();// 获取上传文件对象
  554. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  555. DocumentBuilder db = dbf.newDocumentBuilder();
  556. Document document = db.parse(mf.getInputStream());
  557. NodeList data = document.getChildNodes();
  558. //检查文件有效性,文件data info=“YIKAIFAPIAO”
  559. if(data.getLength() == 1 && data.item(0).getAttributes().getLength() == 1
  560. && data.item(0).getAttributes().item(0).getNodeValue().equals("YIKAIFAPIAO")) {
  561. //缓存客户档案
  562. List<Map<String, Object>> curstomerList = systemService.findForJdbc(" select id,name from t_b_partner");
  563. HashMap<String, String> custMap = new HashMap<String, String>();
  564. for(Map<String, Object> map:curstomerList) {
  565. custMap.put(map.get("name").toString(), map.get("id").toString());
  566. }
  567. NodeList rows = data.item(0).getChildNodes().item(0).getChildNodes();
  568. ContractSaleInvoiceEntity newInvoice;
  569. for (int i = 0; i < rows.getLength(); i++) {
  570. Node row = rows.item(i);
  571. //作废发票不处理,非物业管理费不处理
  572. if((!row.getAttributes().getNamedItem("作废日期").getNodeValue().equals(""))
  573. || row.getAttributes().getNamedItem("主要商品名称").getNodeValue().indexOf("物业管理费") == -1)
  574. continue;
  575. newInvoice = new ContractSaleInvoiceEntity();
  576. newInvoice.setInvoicedate( DateUtils.parseDate(row.getAttributes().getNamedItem("开票日期").getNodeValue(),"yyyy-MM-dd"));
  577. newInvoice.setInvoicetype(row.getAttributes().getNamedItem("发票类型").getNodeValue().equals("增值税普票")
  578. ? Globals.INVOICE_TYPE_ordinary:Globals.INVOICE_TYPE_special);
  579. newInvoice.setInvoiceno(row.getAttributes().getNamedItem("发票号码").getNodeValue());
  580. if(custMap.containsKey(row.getAttributes().getNamedItem("客户名称").getNodeValue())) {
  581. newInvoice.setCustomername(row.getAttributes().getNamedItem("客户名称").getNodeValue());
  582. newInvoice.setCustomerid(custMap.get(row.getAttributes().getNamedItem("客户名称").getNodeValue()));
  583. }
  584. newInvoice.setRemark("发票内容:"+row.getAttributes().getNamedItem("主要商品名称").getNodeValue()
  585. +"\n开票人:"+row.getAttributes().getNamedItem("开票人").getNodeValue());
  586. newInvoice.setTaxamount(new Double(row.getAttributes().getNamedItem("税额").getNodeValue()));
  587. newInvoice.setSaleamount(new Double(row.getAttributes().getNamedItem("合计金额").getNodeValue()));
  588. newInvoice.setInvoiceamount(new Double(row.getAttributes().getNamedItem("价税合计").getNodeValue()));
  589. newInvoice.setFeetype("wuyefee");
  590. doAdd(newInvoice, request);
  591. }
  592. }
  593. // String fileName = mf.getOriginalFilename();// 获取文件名
  594. // String swfName = PinyinUtil.getPinYinHeadChar(oConvertUtils.replaceBlank(FileUtils.getFilePrefix(fileName)));// 取文件名首字母作为SWF文件名
  595. // String extend = FileUtils.getExtend(fileName);// 获取文件扩展名
  596. // String noextfilename=DateUtils.getDataString(DateUtils.yyyymmddhhmmss)+StringUtil.random(8);//自定义文件名称
  597. // String myfilename=noextfilename+"."+extend;//自定义文件名称
  598. // String savePath = realPath + myfilename;// 文件保存全路径
  599. // write2Disk(mf, extend, savePath);
  600. // TSAttachment attachment = new TSAttachment();
  601. // attachment.setId(UUID.randomUUID().toString().replace("-", ""));
  602. // attachment.setAttachmenttitle(fileName);
  603. // attachment.setCreatedate(new Timestamp(new Date().getTime()));
  604. // attachment.setExtend(extend);
  605. // attachment.setRealpath(path + myfilename);
  606. // attachment.setSwfpath(swfName);
  607. // systemService.save(attachment);
  608. // attributes.put("url", path + myfilename);
  609. // attributes.put("name", fileName);
  610. ajaxJson.setMsg("上传发票xml完成");
  611. systemService.addLog("上传发票xml完成", Globals.Log_Leavel_INFO, Globals.Log_Type_OTHER);
  612. }
  613. }
  614. // ajaxJson.setAttributes(attributes);
  615. } catch (Exception e) {
  616. ajaxJson.setSuccess(false);
  617. ajaxJson.setMsg(e.getMessage());
  618. }
  619. return ajaxJson;
  620. }
  621. /**
  622. * 保存文件的具体操作
  623. * @param mf
  624. * @param extend
  625. * @param savePath
  626. * @throws IOException
  627. * @throws UnsupportedEncodingException
  628. * @throws FileNotFoundException
  629. */
  630. private void write2Disk(MultipartFile mf, String extend, String savePath)
  631. throws IOException, UnsupportedEncodingException, FileNotFoundException {
  632. File savefile = new File(savePath);
  633. if("txt".equals(extend)){
  634. //利用utf-8字符集的固定首行隐藏编码原理
  635. //Unicode:FF FE UTF-8:EF BB
  636. byte[] allbytes = mf.getBytes();
  637. try{
  638. String head1 = toHexString(allbytes[0]);
  639. String head2 = toHexString(allbytes[1]);
  640. if("ef".equals(head1) && "bb".equals(head2)){
  641. //UTF-8
  642. String contents = new String(mf.getBytes(),"UTF-8");
  643. if(StringUtils.isNotBlank(contents)){
  644. OutputStream out = new FileOutputStream(savePath);
  645. out.write(contents.getBytes());
  646. out.close();
  647. }
  648. } else {
  649. //GBK
  650. String contents = new String(mf.getBytes(),"GBK");
  651. OutputStream out = new FileOutputStream(savePath);
  652. out.write(contents.getBytes());
  653. out.close();
  654. }
  655. } catch(Exception e){
  656. String contents = new String(mf.getBytes(),"UTF-8");
  657. if(StringUtils.isNotBlank(contents)){
  658. OutputStream out = new FileOutputStream(savePath);
  659. out.write(contents.getBytes());
  660. out.close();
  661. }
  662. }
  663. } else {
  664. FileCopyUtils.copy(mf.getBytes(), savefile);
  665. }
  666. }
  667. private String toHexString(int index){
  668. String hexString = Integer.toHexString(index);
  669. // 1个byte变成16进制的,只需要2位就可以表示了,取后面两位,去掉前面的符号填充
  670. hexString = hexString.substring(hexString.length() -2);
  671. return hexString;
  672. }
  673. /**
  674. * 设置开上年、开下年、开本年金额
  675. * @throws ParseException
  676. * */
  677. private ContractSaleInvoiceEntity setFinancialData(ContractSaleInvoiceEntity invoice) throws ParseException {
  678. if(StringUtil.isEmpty(invoice.getPeriodBegin()))
  679. return invoice;
  680. if(invoice.getPeriodBegin().substring(0,4).equals(invoice.getPeriodEnd().substring(0,4))) {
  681. //不跨年
  682. invoice.setC2cinvoice(invoice.getInvoiceamount());
  683. invoice.setC2ninvoice(DecimalUtils.ZERO);
  684. invoice.setC2linvoice(DecimalUtils.ZERO);
  685. }else {
  686. //跨年时,需要计算开上一年或开下一年金额
  687. Date begin = DateUtils.datetimeFormat.parse(invoice.getPeriodBegin()+" 00:00:00");
  688. Date end = DateUtils.datetimeFormat.parse(invoice.getPeriodEnd()+" 00:00:00");
  689. //属于上一年的天数
  690. int firstHalf = XcglDateUtils.differentDays2YearEnd(begin)+1;
  691. //属于下一年的天数
  692. int secondHalf = XcglDateUtils.differentDays2YearStart(end)+1;
  693. double firstAmount = DecimalUtils.formatToDouble((invoice.getInvoiceamount()*firstHalf)/(firstHalf+secondHalf));
  694. double secondAmount = DecimalUtils.formatToDouble(invoice.getInvoiceamount()-firstAmount);
  695. //如果开票日期属于上半个阶段
  696. if(invoice.getPeriodBegin().substring(0,4).equals(
  697. DateUtils.date_sdf.format(invoice.getInvoicedate()).substring(0,4))) {
  698. invoice.setC2cinvoice(firstAmount);//开本年
  699. invoice.setC2ninvoice(secondAmount);//开下年
  700. invoice.setC2linvoice(DecimalUtils.ZERO);//开上年
  701. }else {
  702. //开票日期属于下半个阶段
  703. invoice.setC2cinvoice(firstAmount);//开本年
  704. invoice.setC2ninvoice(DecimalUtils.ZERO);//开下年
  705. invoice.setC2linvoice(secondAmount);//开上年
  706. }
  707. }
  708. return invoice;
  709. }
  710. private String checkEntity(ContractSaleInvoiceEntity invoice) {
  711. String msg = "";
  712. if(StringUtils.isEmpty(invoice.getContractid()) || StringUtils.isEmpty(invoice.getContractname())){
  713. msg = "合同不能为空";
  714. }
  715. if(StringUtils.isEmpty(invoice.getCustomerid()) || StringUtils.isEmpty(invoice.getCustomername())){
  716. msg = "抬头不能为空";
  717. }
  718. if(StringUtils.isEmpty(invoice.getProjectid()) || StringUtils.isEmpty(invoice.getProjectname())){
  719. msg = "项目不能为空";
  720. }
  721. return msg;
  722. }
  723. @SuppressWarnings({ "unchecked", "rawtypes" })
  724. @RequestMapping(params = "getProjectInfo", method = RequestMethod.POST)
  725. @ResponseBody
  726. public Object getProjectInfo(String goodsCode,String contractid){
  727. Map reJson = new HashMap<String, Object>();
  728. TBGoodsInfoEntity goodInfo = null;
  729. List<Map<String, Object>> resList = new ArrayList<Map<String,Object>>();
  730. if(StringUtils.isNotBlank(contractid) ){
  731. try {
  732. String sql = "select id as projectid,name as projectname from t_b_project where id in (select projectid from t_b_contract_sale where id = ?)";
  733. resList = systemService.findForJdbc(sql, contractid);
  734. if(resList.size() == 0)
  735. throw new BusinessException("查询不到项目");
  736. }catch (Exception e) {
  737. reJson.put("status", "fail");
  738. logger.info(e.getMessage());
  739. return reJson;
  740. }
  741. reJson.put("status", "success");
  742. reJson.put("projectinfo", resList.get(0));
  743. }else{
  744. reJson.put("status", "success");
  745. reJson.put("projectinfo", null);
  746. }
  747. return reJson;
  748. }
  749. }