📄 vouchernumserviceimpl.java
字号:
/**
* =============================================
* Copyright 2006 szmx
*
* Change Revision
* --------------------------------
* Date Author Remarks
* 2006-4-14 Allen.Zeng Create com.szmx.tlms.finance.service.impl.VoucherNumServiceImpl
* =============================================
*/
package com.szmx.tlms.finance.service.impl;
import com.szmx.tlms.TlmsServiceErrorCodes;
import com.szmx.tlms.TlmsServiceException;
import com.szmx.tlms.admin.service.CodeService;
import com.szmx.tlms.admin.model.Code;
import com.szmx.tlms.finance.dao.VoucherNumDao;
import com.szmx.tlms.finance.model.CalendarPeriod;
import com.szmx.tlms.finance.model.VoucherHeader;
import com.szmx.tlms.finance.model.VoucherNum;
import com.szmx.tlms.finance.service.VoucherHeaderService;
import com.szmx.tlms.finance.service.VoucherNumService;
import com.szmx.tlms.supplychain.model.SalesOrg;
import com.szmx.tlms.supplychain.service.SalesOrgService;
import com.szmx.framework.base.service.impl.BaseServiceImpl;
import com.szmx.framework.util.StringUtil;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
* @author Allen.Zeng
* @since 2006-4-14
*/
public class VoucherNumServiceImpl extends BaseServiceImpl implements VoucherNumService {
private VoucherHeaderService voucherHeaderService = null;
private SalesOrgService salesOrgService = null;
private CodeService codeService = null;
/**
* This method used by IOC
*
* @param voucherHeaderService
*/
public void setVoucherHeaderService(VoucherHeaderService voucherHeaderService) {
this.voucherHeaderService = voucherHeaderService;
}
/**
* This method used by IOC
*
* @param salesOrgService
*/
public void setSalesOrgService(SalesOrgService salesOrgService) {
this.salesOrgService = salesOrgService;
}
/**
* This method used by IOC
*
* @param codeService
*/
public void setCodeService(CodeService codeService) {
this.codeService = codeService;
}
/**
* @see com.szmx.tlms.finance.service.VoucherNumService#updateFlowNo(String, com.szmx.tlms.finance.model.CalendarPeriod, String, Long)
*/
public void updateFlowNo(String companySysId, CalendarPeriod calendarPeriod,
String voucherType, Long userid) throws TlmsServiceException {
if (StringUtil.isNull(companySysId) || calendarPeriod == null
|| StringUtil.isNull(voucherType) || userid == null) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to update flow number ==> Check following parameters !" +
" companySysId : " + companySysId +
" calendarPeriod : " + calendarPeriod +
" voucherType : " + voucherType +
" userId : " + userid);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI020);
}
SalesOrg company = salesOrgService.getSalesOrg(companySysId);
if (company == null) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to update flow number. Invalid companySysId : " + companySysId);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI020);
}
((VoucherNumDao) baseDao).updateFlowNo(company, calendarPeriod, voucherType, userid);
}
/**
* @see com.szmx.tlms.finance.service.VoucherNumService#generateVoucherNum(String, com.szmx.tlms.finance.model.CalendarPeriod, String, Long)
*/
public String generateVoucherNum(String companySysId, CalendarPeriod calendarPeriod,
String voucherType, Long userid) throws TlmsServiceException {
if (companySysId == null || calendarPeriod == null || StringUtil.isNull(voucherType) || userid == null) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to generate voucher number ==> Check following parameters !" +
" companySysId : " + companySysId +
" calendarPeriod : " + calendarPeriod +
" voucherType : " + voucherType +
" userId : " + userid);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI021);
}
SalesOrg company = salesOrgService.getSalesOrg(companySysId);
if (company == null) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to generate voucher number! Invalid companySysId : " + companySysId);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI021);
}
String voucherNum = ((VoucherNumDao) baseDao).generateVoucherNum(company, calendarPeriod, voucherType, userid);
if (StringUtil.isNull(voucherNum)) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to generate voucher number !");
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI021);
}
return voucherNum;
}
/**
* @see com.szmx.tlms.finance.service.VoucherNumService#initPeriodVoucherNum(com.szmx.tlms.finance.model.CalendarPeriod, Long)
*/
public void initPeriodVoucherNum(CalendarPeriod calendarPeriod, Long userid) throws TlmsServiceException {
if (calendarPeriod == null || userid == null) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Cannot initiate period voucher number ==> Check following parameters !" +
" calendarPeriod : " + calendarPeriod +
" userId : " + userid);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI022);
}
// get voucher type list
List voucherTypeCodeList = codeService.getCodesByCodeType("VOUCHER_TYPE");
List voucherTypeList = new ArrayList();
if (voucherTypeCodeList != null) {
Code code;
for (int i = 0; i < voucherTypeCodeList.size(); i ++) {
code = (Code) voucherTypeCodeList.get(i);
voucherTypeList.add(code.getCodeValue());
}
}
// In the return value, the voucherHeaderList has been ordered by companySysId, voucherType & voucherNum in hql
// Example of returned list:
/*
companySysId voucherType voucherNum
1 PE 06-PE01-1
1 PE 06-PE01-2
1 PE 06-PE01-2
1 PE 06-PE01-10
1 PE 06-PE01-12
1 PE 06-PE01-12
2 AE 06-AE01-2
2 AE 06-AE01-3
*/
List voucherHeaderList = voucherHeaderService.searchVoucherHeader(calendarPeriod, new VoucherHeader());
if (voucherHeaderList == null || voucherHeaderList.size() <= 0) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Needn't to initiate period voucher number. " +
"No voucher to deal with during period '" + calendarPeriod.getPeriodName() + "'");
}
return ;
}
// validate the pattern of voucher number.
this.validateVoucherNumPattern(voucherHeaderList);
// split voucherHeaderList by companyid.
// In the returned map,
// key : company id
// value : a list whose elems are voucherheaders with the companyid of the key value
// Example of returned map:
/*
key value
1 list(companyId voucherType voucherNum)
1 PE 06-PE01-1
1 PE 06-PE01-2
1 PE 06-PE01-2
1 PE 06-PE01-10
1 PE 06-PE01-12
1 PE 06-PE01-12
2 list(companyId voucherType voucherNum)
2 AE 06-AE01-2
2 AE 06-AE01-3
*/
Map voucherHeaderByCompanyIdMap = this.splistVoucherHeaderListByCompanyId(voucherHeaderList);
// one company by one company, veirfy voucher number
Set companyIdSet = voucherHeaderByCompanyIdMap.keySet();
Iterator companyIdSetIt = companyIdSet.iterator();
Long companyId;
List oneCompanyVoucherHeaderList;
while (companyIdSetIt.hasNext()) {
companyId = (Long) companyIdSetIt.next();
oneCompanyVoucherHeaderList = (List) voucherHeaderByCompanyIdMap.get(companyId);
// split one company voucherHeaderList by voucher type
// Key : voucher type
// Value : a list of voucherHeaders whose voucher type is the key value
// Example of returned map for companyId 1:
/*
key value
PE list(companyId voucherType voucherNum)
1 PE 06-PE01-1
1 PE 06-PE01-2
1 PE 06-PE01-2
1 PE 06-PE01-10
1 PE 06-PE01-12
1 PE 06-PE01-12
*/
Map oneCompanyVoucherHeaderByVoucherTypeMap =
this.splitOneCompanyVoucherHeaderListByVoucherType(oneCompanyVoucherHeaderList);
// validate voucher number for two rules
// 1) There must be not a duplicated number
// 2) There must be not any unused number before this one
// Example of returned map for companyId 1:
/*
key value
PE list(companyId voucherType voucherNum)
1 PE 06-PE01-1
1 PE 06-PE01-2
1 PE 06-PE01-3
1 PE 06-PE01-4
1 PE 06-PE01-5
1 PE 06-PE01-6
*/
oneCompanyVoucherHeaderByVoucherTypeMap=
this.verifyOneCompanyVoucherNumByVoucherType(oneCompanyVoucherHeaderByVoucherTypeMap);
SalesOrg company = salesOrgService.getSalesOrg(String.valueOf(companyId));
if (company == null) {
if (this.logger.isWarnEnabled()) {
this.logger.warn("Needn't to initiate period voucher number. Invalid company Id : " + companyId);
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI022);
}
// init flow number of every voucher type to zero in TB_VOUCHER_NUM for special company
int status = ((VoucherNumDao)baseDao).setFlowNoZero(company, calendarPeriod, voucherTypeList, userid);
// validate status
if (status != 0) {
if (this.logger.isErrorEnabled()) {
this.logger.error("Fail to initiate period voucher number.");
}
throw new TlmsServiceException(TlmsServiceErrorCodes.SERVICE_ERROR_FI022);
}
// save validated voucher number into TB_VOUCHER_NUM
this.saveOneCompanyVoucherNumByVoucherType(oneCompanyVoucherHeaderByVoucherTypeMap,
calendarPeriod, companyId);
// update validated voucher number into TB_VOUCHER_HEADER
voucherHeaderService.updateVoucherHeader(calendarPeriod, oneCompanyVoucherHeaderList);
}
}
/**
* This method validates voucher type pattern against the predefined pattern with regular expression.
*
* @param voucherHeaderList list of voucher headers
* @throws TlmsServiceException
*/
private void validateVoucherNumPattern(List voucherHeaderList) throws TlmsServiceException {
if (voucherHeaderList == null || voucherHeaderList.size() <= 0) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -