12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240 |
- package com.jsh.erp.service.impl;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
- import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
- import com.jsh.erp.constants.BusinessConstants;
- import com.jsh.erp.constants.ExceptionConstants;
- import com.jsh.erp.datasource.entities.*;
- import com.jsh.erp.datasource.mappers.DepotHeadMapper;
- import com.jsh.erp.datasource.mappers.DepotHeadMapperEx;
- import com.jsh.erp.datasource.mappers.DepotItemMapperEx;
- import com.jsh.erp.datasource.pda.dto.PDADepotHeadDTO;
- import com.jsh.erp.datasource.pda.dto.PDADepotMaterialDto;
- import com.jsh.erp.datasource.pda.vo.PDADepotHeadVO;
- import com.jsh.erp.datasource.vo.*;
- import com.jsh.erp.exception.BusinessRunTimeException;
- import com.jsh.erp.exception.JshException;
- import com.jsh.erp.exception.ServiceException;
- import com.jsh.erp.query.LambdaQueryWrapperX;
- import com.jsh.erp.query.QueryWrapperX;
- import com.jsh.erp.service.*;
- import com.jsh.erp.utils.*;
- import jxl.Workbook;
- import jxl.write.WritableWorkbook;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import org.springframework.web.context.request.RequestContextHolder;
- import org.springframework.web.context.request.ServletRequestAttributes;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.File;
- import java.math.BigDecimal;
- import java.sql.Timestamp;
- import java.util.*;
- import java.util.stream.Collectors;
- import static com.jsh.erp.utils.Tools.getCenternTime;
- import static com.jsh.erp.utils.Tools.getNow3;
- @Service
- public class DepotHeadServiceImpl extends ServiceImpl<DepotHeadMapper, DepotHead> implements DepotHeadService {
- private Logger logger = LoggerFactory.getLogger(DepotHeadServiceImpl.class);
- @Resource
- private DepotHeadMapper depotHeadMapper;
- @Resource
- private DepotHeadMapperEx depotHeadMapperEx;
- @Resource
- DepotItemMapperEx depotItemMapperEx;
- @Resource
- private UserService userService;
- @Resource
- private RoleService roleService;
- @Resource
- private DepotService depotService;
- @Resource
- DepotItemService depotItemService;
- @Resource
- private SupplierService supplierService;
- @Resource
- private UserBusinessService userBusinessService;
- @Resource
- private SystemConfigService systemConfigService;
- @Resource
- private SerialNumberService serialNumberService;
- @Resource
- private OrgaUserRelService orgaUserRelService;
- @Resource
- private PersonService personService;
- @Resource
- private AccountService accountService;
- @Resource
- private AccountHeadService accountHeadService;
- @Resource
- private AccountItemService accountItemService;
- @Resource
- private SequenceService sequenceService;
- @Resource
- private LogService logService;
- @Resource
- private MaterialService materialService;
- @Resource
- private MaterialExtendService materialExtendService;
- @Resource
- private SyncTescoSystemService syncTescoSystemService;
- @Resource
- private MaterialBatchService materialBatchService;
- @Resource
- private MaterialCategoryService materialCategoryService;
- @Resource
- private AuditService auditService;
- @Resource
- private MsgService msgService;
- /**
- * PDA查询订单
- * @param pdaDepotHeadDTO 筛选条件
- * @return
- */
- @Override
- public List<PDADepotHeadVO> pdaList(PDADepotHeadDTO pdaDepotHeadDTO) {
- return depotHeadMapper.pdaList(pdaDepotHeadDTO);
- }
- @Override
- public PDADepotHeadVO pdaDetail(Long id) {
- return depotHeadMapper.pdaDetail(id);
- }
- /**
- * pda复核任务列表
- * @param pdaDepotHeadDTO 筛选条件
- */
- @Override
- public List<PDADepotHeadVO> reviewTaskList(PDADepotHeadDTO pdaDepotHeadDTO) {
- pdaDepotHeadDTO.setUid(userService.getCurrentUser().getId());
- PageUtils.startPage();
- return depotHeadMapper.reviewTaskList(pdaDepotHeadDTO);
- }
- /**
- * pda设置复核任务状态
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public boolean setReviewStatus(PDADepotHeadDTO pdaDepotHeadDTO) {
- try{
- User user = userService.getCurrentUser();
- DepotHead head = getOne(new LambdaQueryWrapperX<DepotHead>().eq(DepotHead::getLinkNumber,pdaDepotHeadDTO.getNumber()).eq(DepotHead::getDeleteFlag,false));
- if (pdaDepotHeadDTO.getStatus().equals("2")){
- //复核成功
- update(new UpdateWrapper<DepotHead>().set("status","2").eq("id",head.getId()));
- //生成批次
- List<DepotItem> list = depotItemService.list(new LambdaQueryWrapperX<DepotItem>().eq(DepotItem::getHeaderId, head.getId()));
- //处理批次商品信息
- if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(head.getType())) {
- for (DepotItem depotItem : list) {
- //表单入库,新增批次商品信息
- materialBatchService.generateMaterialBatchByDepotItemId(depotItem, head.getOrganId());
- //更新当前库存
- depotItemService.updateCurrentStock(depotItem);
- }
- } else if (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(head.getType())) {
- for (DepotItem depotItem : list) {
- //表单出库,修改商品库存
- materialBatchService.handleMaterialBatchByDepotItemId(depotItem.getId());
- }
- }
- }else {
- //复核驳回
- //删除对应的出入库订单
- depotItemService.deleteDepotItemHeadId(head.getId());
- update(new UpdateWrapper<DepotHead>().set("delete_flag","1").eq("id",head.getId()));
- //发送消息
- Msg msg = new Msg();
- msg.setMsgTitle(pdaDepotHeadDTO.getId().toString());
- msg.setMsgContent(String.format("您的订单任务: %s 已驳回,请重新核对并提交",pdaDepotHeadDTO.getNumber()));
- msg.setCreateTime(new Date());
- msg.setType("复核任务");
- msg.setUserId(head.getCreator());
- msg.setStatus(1);
- msgService.save(msg);
- }
- this.update(new UpdateWrapper<DepotHead>()
- .set("status",pdaDepotHeadDTO.getStatus())
- .set("reject",pdaDepotHeadDTO.getReject())
- .set("auditor",user.getId())
- .set("reject_time",new Date())
- .eq("id",pdaDepotHeadDTO.getId()));
- }catch (Exception e){
- logger.info("复核任务设置失败!",e);
- throw new ServiceException(e.getMessage());
- }
- return true;
- }
- /**
- * 根据id获取订单主表信息
- */
- @Override
- public DepotHead getDepotHead(long id) {
- DepotHead result=null;
- try{
- result=depotHeadMapper.selectByPrimaryKey(id);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotHead> getDepotHead()throws Exception {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list=null;
- try{
- list=depotHeadMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<DepotHeadVo4List> select(String type, String subType, String hasDebt, String status, String purchaseStatus, String number, String linkApply, String linkNumber,
- String beginTime, String endTime, String materialParam, Long organId, Long creator, Long depotId, Long accountId, String remark) throws Exception {
- List<DepotHeadVo4List> list = new ArrayList<>();
- try{
- HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
- Long userId = userService.getUserId(request);
- Role role = userService.getRoleTypeByUserId(userId);
- String priceLimit = role.getPriceLimit();
- String billCategory = getBillCategory(subType);
- String [] depotArray = getDepotArray(subType);
- String [] creatorArray = getCreatorArray();
- String [] statusArray = StringUtil.isNotEmpty(status) ? status.split(",") : null;
- String [] purchaseStatusArray = StringUtil.isNotEmpty(purchaseStatus) ? purchaseStatus.split(",") : null;
- String [] organArray = getOrganArray(subType, purchaseStatus);
- if ("采购订单".equals(subType) && "供应商".equals(role.getName())) {
- organArray = getOrganArrayByCurrentUser(userId);
- }
- //以销定购,查看全部数据
- creatorArray = StringUtil.isNotEmpty(purchaseStatus) ? null: creatorArray;
- Map<Long,String> personMap = personService.getPersonMap();
- Map<Long,String> accountMap = accountService.getAccountMap();
- beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
- if (null != depotArray && depotArray.length > 0){
- PageUtils.startPage();
- list = depotHeadMapperEx.selectByConditionDepotHead(type, subType, creatorArray, hasDebt,
- statusArray, purchaseStatusArray, number, linkApply, linkNumber, beginTime, endTime,
- materialParam, organId, organArray, creator, depotId, depotArray, accountId, remark);
- }
- if (null != list) {
- List<Long> idList = new ArrayList<>();
- List<String> numberList = new ArrayList<>();
- for (DepotHeadVo4List dh : list) {
- idList.add(dh.getId());
- numberList.add(dh.getNumber());
- }
- //通过批量查询去构造map
- Map<String,BigDecimal> finishDepositMap = getFinishDepositMapByNumberList(numberList);
- Map<Long,Integer> financialBillNoMap = getFinancialBillNoMapByBillIdList(idList);
- Map<String,Integer> billSizeMap = getBillSizeMapByLinkNumberList(numberList);
- Map<Long,String> materialsListMap = findMaterialsListMapByHeaderIdList(idList);
- Map<Long,BigDecimal> materialCountListMap = getMaterialCountListMapByHeaderIdList(idList);
- for (DepotHeadVo4List dh : list) {
- if(accountMap!=null && StringUtil.isNotEmpty(dh.getAccountIdList()) && StringUtil.isNotEmpty(dh.getAccountMoneyList())) {
- String accountStr = accountService.getAccountStrByIdAndMoney(accountMap, dh.getAccountIdList(), dh.getAccountMoneyList());
- dh.setAccountName(accountStr);
- }
- if(dh.getAccountIdList() != null) {
- String accountidlistStr = dh.getAccountIdList().replace("[", "").replace("]", "").replaceAll("\"", "");
- dh.setAccountIdList(accountidlistStr);
- }
- if(dh.getAccountMoneyList() != null) {
- String accountmoneylistStr = dh.getAccountMoneyList().replace("[", "").replace("]", "").replaceAll("\"", "");
- dh.setAccountMoneyList(accountmoneylistStr);
- }
- if(dh.getChangeAmount() != null) {
- dh.setChangeAmount(roleService.parseBillPriceByLimit(dh.getChangeAmount().abs(), billCategory, priceLimit, request));
- } else {
- dh.setChangeAmount(BigDecimal.ZERO);
- }
- if(dh.getTotalPrice() != null) {
- BigDecimal lastTotalPrice = BusinessConstants.SUB_TYPE_CHECK_ENTER.equals(dh.getSubType())||
- BusinessConstants.SUB_TYPE_REPLAY.equals(dh.getSubType())?dh.getTotalPrice():dh.getTotalPrice().abs();
- dh.setTotalPrice(roleService.parseBillPriceByLimit(lastTotalPrice, billCategory, priceLimit, request));
- }
- BigDecimal discountLastMoney = dh.getDiscountLastMoney()!=null?dh.getDiscountLastMoney():BigDecimal.ZERO;
- dh.setDiscountLastMoney(roleService.parseBillPriceByLimit(discountLastMoney, billCategory, priceLimit, request));
- BigDecimal backAmount = dh.getBackAmount()!=null?dh.getBackAmount():BigDecimal.ZERO;
- dh.setBackAmount(roleService.parseBillPriceByLimit(backAmount, billCategory, priceLimit, request));
- if(dh.getDeposit() == null) {
- dh.setDeposit(BigDecimal.ZERO);
- } else {
- dh.setDeposit(roleService.parseBillPriceByLimit(dh.getDeposit(), billCategory, priceLimit, request));
- }
- //已经完成的欠款
- if(finishDepositMap!=null) {
- BigDecimal finishDeposit = finishDepositMap.get(dh.getNumber()) != null ? finishDepositMap.get(dh.getNumber()) : BigDecimal.ZERO;
- dh.setFinishDeposit(roleService.parseBillPriceByLimit(finishDeposit, billCategory, priceLimit, request));
- }
- //欠款计算
- BigDecimal otherMoney = dh.getOtherMoney()!=null?dh.getOtherMoney():BigDecimal.ZERO;
- BigDecimal deposit = dh.getDeposit()!=null?dh.getDeposit():BigDecimal.ZERO;
- BigDecimal changeAmount = dh.getChangeAmount()!=null?dh.getChangeAmount():BigDecimal.ZERO;
- BigDecimal debt = discountLastMoney.add(otherMoney).subtract((deposit.add(changeAmount)));
- dh.setDebt(roleService.parseBillPriceByLimit(debt, billCategory, priceLimit, request));
- //是否有付款单或收款单
- if(financialBillNoMap!=null) {
- Integer financialBillNoSize = financialBillNoMap.get(dh.getId());
- dh.setHasFinancialFlag(financialBillNoSize!=null && financialBillNoSize>0);
- }
- //是否有退款单
- if(billSizeMap!=null) {
- Integer billListSize = billSizeMap.get(dh.getNumber());
- dh.setHasBackFlag(billListSize!=null && billListSize>0);
- }
- if(StringUtil.isNotEmpty(dh.getSalesMan())) {
- dh.setSalesManStr(personService.getPersonByMapAndIds(personMap,dh.getSalesMan()));
- }
- if(dh.getOperTime() != null) {
- dh.setOperTimeStr(getCenternTime(dh.getOperTime()));
- }
- //商品信息简述
- if(materialsListMap!=null) {
- dh.setMaterialsList(materialsListMap.get(dh.getId()));
- }
- //商品总数量
- if(materialCountListMap!=null) {
- dh.setMaterialCount(materialCountListMap.get(dh.getId()));
- }
- //以销定购的情况(不能显示销售单据的金额和客户名称)
- if(StringUtil.isNotEmpty(purchaseStatus)) {
- dh.setOrganName("****");
- dh.setTotalPrice(null);
- dh.setDiscountLastMoney(null);
- }
- }
- }
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- /**
- * 根据单据类型获取仓库数组
- * @param subType
- * @return
- * @throws Exception
- */
- @Override
- public String[] getDepotArray(String subType) throws Exception {
- String [] depotArray = null;
- if(!BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(subType)) {
- String depotIds = depotService.findDepotStrByCurrentUser();
- depotArray = StringUtil.isNotEmpty(depotIds) ? depotIds.split(",") : null;
- }
- return depotArray;
- }
- /**
- * 根据角色类型获取操作员数组
- * @return
- * @throws Exception
- */
- @Override
- public String[] getCreatorArray() throws Exception {
- String creator = getCreatorByCurrentUser();
- String [] creatorArray=null;
- if(StringUtil.isNotEmpty(creator)){
- creatorArray = creator.split(",");
- }
- return creatorArray;
- }
- /**
- * 根据角色类型获取操作员数组
- * @param organizationId
- * @return
- * @throws Exception
- */
- @Override
- public String[] getCreatorArrayByOrg(Long organizationId) throws Exception {
- List<Long> userIdList = orgaUserRelService.getUserIdListByOrgId(organizationId);
- if(userIdList.size()>0) {
- List<String> userIdStrList = userIdList.stream().map(Object::toString).collect(Collectors.toList());
- return StringUtil.listToStringArray(userIdStrList);
- } else {
- return "-1".split(",");
- }
- }
- /**
- * 获取机构数组
- * @return
- */
- @Override
- public String[] getOrganArray(String subType, String purchaseStatus) throws Exception {
- String [] organArray = null;
- String type = "UserCustomer";
- Long userId = userService.getCurrentUser().getId();
- //获取权限信息
- String ubValue = userBusinessService.getUBValueByTypeAndKeyId(type, userId.toString());
- List<Supplier> supplierList = supplierService.findBySelectCus();
- if(BusinessConstants.SUB_TYPE_SALES_ORDER.equals(subType) || BusinessConstants.SUB_TYPE_SALES.equals(subType)
- ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(subType) ) {
- //采购订单里面选择销售订单的时候不要过滤
- if(StringUtil.isEmpty(purchaseStatus)) {
- if (null != supplierList && supplierList.size() > 0) {
- boolean customerFlag = systemConfigService.getCustomerFlag();
- List<String> organList = new ArrayList<>();
- for (Supplier supplier : supplierList) {
- boolean flag = ubValue.contains("[" + supplier.getId().toString() + "]");
- if (!customerFlag || flag) {
- organList.add(supplier.getId().toString());
- }
- }
- if(organList.size() > 0) {
- organArray = StringUtil.listToStringArray(organList);
- }
- }
- }
- }
- return organArray;
- }
- public String [] getOrganArrayByCurrentUser(Long userId) throws Exception {
- String[] organArray = userBusinessService.getUBValuByTypeAndKeyIdToArray("UserSupplier", userId.toString());
- return organArray;
- }
- /**
- * 根据角色类型获取操作员
- * @return
- * @throws Exception
- */
- @Override
- public String getCreatorByCurrentUser() throws Exception {
- String creator = "";
- User user = userService.getCurrentUser();
- String roleType = userService.getRoleTypeByUserId(user.getId()).getType(); //角色类型
- if(BusinessConstants.ROLE_TYPE_PRIVATE.equals(roleType)) {
- creator = user.getId().toString();
- } else if(BusinessConstants.ROLE_TYPE_THIS_ORG.equals(roleType)) {
- creator = orgaUserRelService.getUserIdListByUserId(user.getId());
- }
- return creator;
- }
- @Override
- public Map<String, BigDecimal> getFinishDepositMapByNumberList(List<String> numberList) {
- Map<String,BigDecimal> finishDepositMap = new HashMap<>();
- if(numberList.size()>0) {
- List<FinishDepositVo> list = depotHeadMapperEx.getFinishDepositByNumberList(numberList);
- if(list!=null && list.size()>0) {
- for (FinishDepositVo finishDepositVo : list) {
- if(finishDepositVo!=null) {
- finishDepositMap.put(finishDepositVo.getNumber(), finishDepositVo.getFinishDeposit());
- }
- }
- }
- }
- return finishDepositMap;
- }
- @Override
- public Map<String, Integer> getBillSizeMapByLinkNumberList(List<String> numberList) throws Exception {
- Map<String, Integer> billListMap = new HashMap<>();
- if(numberList.size()>0) {
- List<DepotHead> list = getBillListByLinkNumberList(numberList);
- if(list!=null && list.size()>0) {
- for (DepotHead depotHead : list) {
- if(depotHead!=null) {
- billListMap.put(depotHead.getLinkNumber(), list.size());
- }
- }
- }
- }
- return billListMap;
- }
- @Override
- public Map<Long,Integer> getFinancialBillNoMapByBillIdList(List<Long> idList) {
- Map<Long, Integer> billListMap = new HashMap<>();
- if(idList.size()>0) {
- List<AccountItem> list = accountHeadService.getFinancialBillNoByBillIdList(idList);
- if(list!=null && list.size()>0) {
- for (AccountItem accountItem : list) {
- if(accountItem!=null) {
- billListMap.put(accountItem.getBillId(), list.size());
- }
- }
- }
- }
- return billListMap;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int insertDepotHead(JSONObject obj, HttpServletRequest request)throws Exception {
- DepotHead depotHead = JSONObject.parseObject(obj.toJSONString(), DepotHead.class);
- depotHead.setCreateTime(new Timestamp(System.currentTimeMillis()));
- depotHead.setStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
- User user = userService.getCurrentUser();
- depotHead.setTenantId(user.getId());
- int result=0;
- try{
- result=depotHeadMapper.insert(depotHead);
- logService.insertLog("单据", BusinessConstants.LOG_OPERATION_TYPE_ADD, request);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateDepotHead(JSONObject obj, HttpServletRequest request) throws Exception{
- DepotHead depotHead = JSONObject.parseObject(obj.toJSONString(), DepotHead.class);
- DepotHead dh=null;
- try{
- dh = depotHeadMapper.selectByPrimaryKey(depotHead.getId());
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- depotHead.setStatus(dh.getStatus());
- depotHead.setCreateTime(dh.getCreateTime());
- int result=0;
- try{
- result = depotHeadMapper.updateByPrimaryKey(depotHead);
- logService.insertLog("单据",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(depotHead.getId()).toString(), request);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int deleteDepotHead(Long id, HttpServletRequest request)throws Exception {
- return batchDeleteBillByIds(id.toString());
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteDepotHead(String ids, HttpServletRequest request)throws Exception {
- return batchDeleteBillByIds(ids);
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteBillByIds(String ids)throws Exception {
- StringBuffer sb = new StringBuffer();
- sb.append(BusinessConstants.LOG_OPERATION_TYPE_DELETE);
- List<DepotHead> dhList = getDepotHeadListByIds(ids);
- for(DepotHead depotHead: dhList){
- //只有未审核的单据才能被删除
- if(!"0".equals(depotHead.getStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_UN_AUDIT_DELETE_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_UN_AUDIT_DELETE_FAILED_MSG));
- }
- }
- for(DepotHead depotHead: dhList){
- sb.append("[").append(depotHead.getNumber()).append("]");
- User userInfo = userService.getCurrentUser();
- //删除入库单据,先校验序列号是否出库,如果未出库则同时删除序列号,如果已出库则不能删除单据
- if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType())) {
- List<DepotItem> depotItemList = depotItemMapperEx.findDepotItemListBydepotheadId(depotHead.getId(), BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED);
- if (depotItemList != null && depotItemList.size() > 0) {
- //单据明细里面存在序列号商品
- int serialNumberSellCount = depotHeadMapperEx.getSerialNumberBySell(depotHead.getNumber());
- if (serialNumberSellCount > 0) {
- //已出库则不能删除单据
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_SERIAL_IS_SELL_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_SERIAL_IS_SELL_MSG, depotHead.getNumber()));
- } else {
- //删除序列号
- SerialNumberExample example = new SerialNumberExample();
- example.createCriteria().andInBillNoEqualTo(depotHead.getNumber());
- serialNumberService.deleteByExample(example);
- }
- }
- }
- //删除出库数据回收序列号
- if (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())
- && !BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
- //查询单据子表列表
- List<DepotItem> depotItemList = depotItemMapperEx.findDepotItemListBydepotheadId(depotHead.getId(), BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED);
- /**回收序列号*/
- if (depotItemList != null && depotItemList.size() > 0) {
- for (DepotItem depotItem : depotItemList) {
- //BasicNumber=OperNumber*ratio
- serialNumberService.cancelSerialNumber(depotItem.getMaterialId(), depotHead.getNumber(), (depotItem.getBasicNumber() == null ? 0 : depotItem.getBasicNumber()).intValue(), userInfo);
- }
- }
- }
- List<DepotItem> list = depotItemService.getListByHeaderId(depotHead.getId());
- //删除单据子表数据
- depotItemMapperEx.batchDeleteDepotItemByDepotHeadIds(new Long[]{depotHead.getId()});
- //删除单据主表信息
- batchDeleteDepotHeadByIds(depotHead.getId().toString());
- //将关联的单据置为审核状态-针对采购入库、销售出库、盘点复盘、其它入库、其它出库
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber())){
- if((BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType()))
- || (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType()))
- || (BusinessConstants.DEPOTHEAD_TYPE_OTHER.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_REPLAY.equals(depotHead.getSubType()))
- || (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_OTHER.equals(depotHead.getSubType()))
- || (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_OTHER.equals(depotHead.getSubType()))) {
- String status = BusinessConstants.BILLS_STATUS_AUDIT;
- //查询除当前单据之外的关联单据列表
- List<DepotHead> exceptCurrentList = getListByLinkNumberExceptCurrent(depotHead.getLinkNumber(), depotHead.getNumber(), depotHead.getType());
- if(exceptCurrentList!=null && exceptCurrentList.size()>0) {
- status = BusinessConstants.BILLS_STATUS_SKIPING;
- }
- DepotHead dh = new DepotHead();
- dh.setStatus(status);
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andNumberEqualTo(depotHead.getLinkNumber());
- depotHeadMapper.updateByExampleSelective(dh, example);
- }
- }
- //将关联的单据置为审核状态-针对请购单转采购订单的情况
- if(StringUtil.isNotEmpty(depotHead.getLinkApply())){
- if(BusinessConstants.DEPOTHEAD_TYPE_OTHER.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
- String status = BusinessConstants.BILLS_STATUS_AUDIT;
- //查询除当前单据之外的关联单据列表
- List<DepotHead> exceptCurrentList = getListByLinkApplyExceptCurrent(depotHead.getLinkApply(), depotHead.getNumber(), depotHead.getType());
- if(exceptCurrentList!=null && exceptCurrentList.size()>0) {
- status = BusinessConstants.BILLS_STATUS_SKIPING;
- }
- DepotHead dh = new DepotHead();
- dh.setStatus(status);
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andNumberEqualTo(depotHead.getLinkApply());
- depotHeadMapper.updateByExampleSelective(dh, example);
- }
- }
- //将关联的销售订单单据置为未采购状态-针对销售订单转采购订单的情况
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber())){
- if(BusinessConstants.DEPOTHEAD_TYPE_OTHER.equals(depotHead.getType()) &&
- BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
- DepotHead dh = new DepotHead();
- //获取分批操作后单据的商品和商品数量(汇总)
- List<DepotItemVo4MaterialAndSum> batchList = depotItemMapperEx.getBatchBillDetailMaterialSum(depotHead.getLinkNumber(), "normal", depotHead.getType());
- if(batchList.size()>0) {
- dh.setPurchaseStatus(BusinessConstants.PURCHASE_STATUS_SKIPING);
- } else {
- dh.setPurchaseStatus(BusinessConstants.PURCHASE_STATUS_UN_AUDIT);
- }
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andNumberEqualTo(depotHead.getLinkNumber());
- depotHeadMapper.updateByExampleSelective(dh, example);
- }
- }
- //对于零售出库单据,更新会员的预收款信息
- if (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())
- && BusinessConstants.SUB_TYPE_RETAIL.equals(depotHead.getSubType())){
- if(BusinessConstants.PAY_TYPE_PREPAID.equals(depotHead.getPayType())) {
- if (depotHead.getOrganId() != null) {
- //更新会员预付款
- supplierService.updateAdvanceIn(depotHead.getOrganId());
- }
- }
- }
- for (DepotItem depotItem : list) {
- //更新当前库存
- depotItemService.updateCurrentStock(depotItem);
- //更新当前成本价
- depotItemService.updateCurrentUnitPrice(depotItem);
- }
- }
- //路径列表
- List<String> pathList = new ArrayList<>();
- for(DepotHead depotHead: dhList){
- if(StringUtil.isNotEmpty(depotHead.getFileName())) {
- pathList.add(depotHead.getFileName());
- }
- }
- //逻辑删除文件
- systemConfigService.deleteFileByPathList(pathList);
- logService.insertLog("单据", sb.toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- return 1;
- }
- /**
- * 删除单据主表信息
- * @param ids
- * @return
- * @throws Exception
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteDepotHeadByIds(String ids)throws Exception {
- User userInfo=userService.getCurrentUser();
- String [] idArray=ids.split(",");
- int result=0;
- try{
- result = depotHeadMapperEx.batchDeleteDepotHeadByIds(new Date(),userInfo==null?null:userInfo.getId(),idArray);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotHead> getDepotHeadListByIds(String ids)throws Exception {
- List<Long> idList = StringUtil.strToLongList(ids);
- List<DepotHead> list = new ArrayList<>();
- try{
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andIdIn(idList);
- list = depotHeadMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- /**
- * 校验单据编号是否存在
- * @param id
- * @param number
- * @return
- * @throws Exception
- */
- @Override
- public int checkIsBillNumberExist(Long id, String number) {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andIdNotEqualTo(id).andNumberEqualTo(number).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list = null;
- try{
- list = depotHeadMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list==null?0:list.size();
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchSetStatus(String status, String depotHeadIDs)throws Exception {
- int result = 0;
- List<Long> dhIds = new ArrayList<>();
- List<Long> ids = StringUtil.strToLongList(depotHeadIDs);
- for(Long id: ids) {
- DepotHead depotHead = getDepotHead(id);
- if("0".equals(status)){
- //进行反审核操作
- if("1".equals(depotHead.getStatus()) && "0".equals(depotHead.getPurchaseStatus())) {
- dhIds.add(id);
- } else if("2".equals(depotHead.getPurchaseStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_PURCHASE_STATUS_TWO_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_PURCHASE_STATUS_TWO_MSG));
- } else if("3".equals(depotHead.getPurchaseStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_PURCHASE_STATUS_THREE_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_PURCHASE_STATUS_THREE_MSG));
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_AUDIT_TO_UN_AUDIT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_AUDIT_TO_UN_AUDIT_FAILED_MSG));
- }
- } else if("1".equals(status)){
- //进行审核操作
- if("0".equals(depotHead.getStatus())) {
- dhIds.add(id);
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_UN_AUDIT_TO_AUDIT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_UN_AUDIT_TO_AUDIT_FAILED_MSG));
- }
- }
- }
- if(dhIds.size()>0) {
- DepotHead depotHead = new DepotHead();
- depotHead.setStatus(status);
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andIdIn(dhIds);
- result = depotHeadMapper.updateByExampleSelective(depotHead, example);
- if(systemConfigService.getForceApprovalFlag()) {
- for(Long dhId: dhIds) {
- List<DepotItem> list = depotItemService.list(new LambdaQueryWrapperX<DepotItem>().eq(DepotItem::getHeaderId,dhId));
- DepotHead head = depotHeadMapper.selectById(dhId);
- for (DepotItem depotItem : list) {
- if ("1".equals(status)){
- //单据审核,处理批次商品信息
- if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(head.getType())){
- //表单入库,新增批次商品信息
- materialBatchService.generateMaterialBatchByDepotItemId(depotItem,head.getOrganId());
- }else if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(head.getType())){
- //表单出库,修改商品库存
- //materialExtend.setInventory(materialExtend.getInventory().subtract(depotItem.getBasicNumber()));
- materialBatchService.handleMaterialBatchByDepotItemId(depotItem.getId());
- }
- }
- //更新当前库存
- depotItemService.updateCurrentStock(depotItem);
- }
- }
- }
- }
- return result;
- }
- @Override
- public Map<Long,String> findMaterialsListMapByHeaderIdList(List<Long> idList)throws Exception {
- Map<Long,String> materialsListMap = new HashMap<>();
- if(idList.size()>0) {
- List<MaterialsListVo> list = depotHeadMapperEx.findMaterialsListMapByHeaderIdList(idList);
- for (MaterialsListVo materialsListVo : list) {
- String materialsList = materialsListVo.getMaterialsList();
- if(StringUtil.isNotEmpty(materialsList)) {
- materialsList = materialsList.replace(",",",");
- }
- materialsListMap.put(materialsListVo.getHeaderId(), materialsList);
- }
- }
- return materialsListMap;
- }
- /**
- * 根据单据主表id获取商品数量
- * @param idList 单据主表id集合
- */
- @Override
- public Map<Long,BigDecimal> getMaterialCountListMapByHeaderIdList(List<Long> idList)throws Exception {
- Map<Long,BigDecimal> materialCountListMap = new HashMap<>();
- if(idList.size()>0) {
- List<MaterialCountVo> list = depotHeadMapperEx.getMaterialCountListByHeaderIdList(idList);
- for(MaterialCountVo materialCountVo : list){
- materialCountListMap.put(materialCountVo.getHeaderId(), materialCountVo.getMaterialCount());
- }
- }
- return materialCountListMap;
- }
- /**
- * 按条件查询出入库明细
- */
- @Override
- public List<DepotHeadVo4InDetail> findInOutDetail(String beginTime, String endTime, String type, String[] creatorArray,
- String[] organArray, List<Long> categoryList, Boolean forceFlag, Boolean inOutManageFlag,
- String materialParam, List<Long> depotList, Integer oId, String number,
- Long creator, String remark, String column, String order, Integer offset, Integer rows) throws Exception{
- List<DepotHeadVo4InDetail> list = null;
- try{
- list =depotHeadMapperEx.findInOutDetail(beginTime, endTime, type, creatorArray, organArray, categoryList, forceFlag, inOutManageFlag,
- materialParam, depotList, oId, number, creator, remark, column, order, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int findInOutDetailCount(String beginTime, String endTime, String type, String[] creatorArray,
- String[] organArray, List<Long> categoryList, Boolean forceFlag, Boolean inOutManageFlag, String materialParam, List<Long> depotList, Integer oId, String number,
- Long creator, String remark) throws Exception{
- int result = 0;
- try{
- result =depotHeadMapperEx.findInOutDetailCount(beginTime, endTime, type, creatorArray, organArray, categoryList, forceFlag, inOutManageFlag,
- materialParam, depotList, oId, number, creator, remark);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public DepotHeadVo4InDetail findInOutDetailStatistic(String beginTime, String endTime, String type, String[] creatorArray,
- String[] organArray, List<Long> categoryList, Boolean forceFlag, Boolean inOutManageFlag,
- String materialParam, List<Long> depotList, Integer oId, String number,
- Long creator, String remark) throws Exception{
- DepotHeadVo4InDetail item = new DepotHeadVo4InDetail();
- try{
- List<DepotHeadVo4InDetail> list =depotHeadMapperEx.findInOutDetailStatistic(beginTime, endTime, type, creatorArray, organArray, categoryList, forceFlag, inOutManageFlag,
- materialParam, depotList, oId, number, creator, remark);
- if(list.size()>0) {
- item.setOperNumber(list.get(0).getOperNumber());
- item.setAllPrice(list.get(0).getAllPrice());
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return item;
- }
- @Override
- public List<DepotHeadVo4InOutMCount> findInOutMaterialCount(String beginTime, String endTime, String type, List<Long> categoryList,
- Boolean forceFlag, Boolean inOutManageFlag, String materialParam,
- List<Long> depotList, Long organizationId, Integer oId, String column, String order,
- Integer offset, Integer rows)throws Exception {
- List<DepotHeadVo4InOutMCount> list = null;
- try{
- String [] creatorArray = getCreatorArray();
- if(creatorArray == null && organizationId != null) {
- creatorArray = getCreatorArrayByOrg(organizationId);
- }
- String subType = "出库".equals(type)? "销售" : "";
- String [] organArray = getOrganArray(subType, "");
- list =depotHeadMapperEx.findInOutMaterialCount(beginTime, endTime, type, categoryList, forceFlag, inOutManageFlag, materialParam, depotList, oId,
- creatorArray, organArray, column, order, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int findInOutMaterialCountTotal(String beginTime, String endTime, String type, List<Long> categoryList,
- Boolean forceFlag, Boolean inOutManageFlag, String materialParam,
- List<Long> depotList, Long organizationId, Integer oId)throws Exception {
- int result = 0;
- try{
- String [] creatorArray = getCreatorArray();
- if(creatorArray == null && organizationId != null) {
- creatorArray = getCreatorArrayByOrg(organizationId);
- }
- String subType = "出库".equals(type)? "销售" : "";
- String [] organArray = getOrganArray(subType, "");
- result =depotHeadMapperEx.findInOutMaterialCountTotal(beginTime, endTime, type, categoryList, forceFlag, inOutManageFlag, materialParam, depotList, oId,
- creatorArray, organArray);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public DepotHeadVo4InOutMCount findInOutMaterialCountStatistic(String beginTime, String endTime, String type, List<Long> categoryList,
- Boolean forceFlag, Boolean inOutManageFlag, String materialParam,
- List<Long> depotList, Long organizationId, Integer oId) throws Exception {
- DepotHeadVo4InOutMCount item = new DepotHeadVo4InOutMCount();
- try{
- String [] creatorArray = getCreatorArray();
- if(creatorArray == null && organizationId != null) {
- creatorArray = getCreatorArrayByOrg(organizationId);
- }
- String subType = "出库".equals(type)? "销售" : "";
- String [] organArray = getOrganArray(subType, "");
- List<DepotHeadVo4InOutMCount> list = depotHeadMapperEx.findInOutMaterialCountStatistic(beginTime, endTime, type, categoryList,
- forceFlag, inOutManageFlag, materialParam, depotList, oId, creatorArray, organArray);
- if(list.size()>0) {
- item.setNumSum(list.get(0).getNumSum());
- item.setPriceSum(list.get(0).getPriceSum());
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return item;
- }
- @Override
- public List<DepotHeadVo4InDetail> findAllocationDetail(String beginTime, String endTime, String subType, String number,
- String[] creatorArray, List<Long> categoryList, Boolean forceFlag, String materialParam, List<Long> depotList, List<Long> depotFList,
- String remark, String column, String order, Integer offset, Integer rows) throws Exception{
- List<DepotHeadVo4InDetail> list = null;
- try{
- list =depotHeadMapperEx.findAllocationDetail(beginTime, endTime, subType, number, creatorArray, categoryList, forceFlag,
- materialParam, depotList, depotFList, remark, column, order, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int findAllocationDetailCount(String beginTime, String endTime, String subType, String number,
- String[] creatorArray, List<Long> categoryList, Boolean forceFlag, String materialParam, List<Long> depotList, List<Long> depotFList,
- String remark) throws Exception{
- int result = 0;
- try{
- result =depotHeadMapperEx.findAllocationDetailCount(beginTime, endTime, subType, number, creatorArray, categoryList, forceFlag,
- materialParam, depotList, depotFList, remark);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public DepotHeadVo4InDetail findAllocationStatistic(String beginTime, String endTime, String subType, String number,
- String[] creatorArray, List<Long> categoryList, Boolean forceFlag, String materialParam, List<Long> depotList, List<Long> depotFList,
- String remark) throws Exception{
- DepotHeadVo4InDetail item = new DepotHeadVo4InDetail();
- try{
- List<DepotHeadVo4InDetail> list =depotHeadMapperEx.findAllocationStatistic(beginTime, endTime, subType, number, creatorArray, categoryList, forceFlag,
- materialParam, depotList, depotFList, remark);
- if(list.size()>0) {
- item.setOperNumber(list.get(0).getOperNumber());
- item.setAllPrice(list.get(0).getAllPrice());
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return item;
- }
- @Override
- public List<DepotHeadVo4StatementAccount> getStatementAccount(String beginTime, String endTime, Integer organId, String[] organArray,
- Integer hasDebt, String supplierType, String type, String subType, String typeBack,
- String subTypeBack, String billType, Integer offset, Integer rows) {
- List<DepotHeadVo4StatementAccount> list = null;
- try{
- list = depotHeadMapperEx.getStatementAccount(beginTime, endTime, organId, organArray, hasDebt, supplierType, type, subType,typeBack, subTypeBack, billType, offset, rows);
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int getStatementAccountCount(String beginTime, String endTime, Integer organId, String[] organArray,
- Integer hasDebt, String supplierType, String type, String subType, String typeBack, String subTypeBack, String billType) {
- int result = 0;
- try{
- result = depotHeadMapperEx.getStatementAccountCount(beginTime, endTime, organId, organArray, hasDebt, supplierType, type, subType,typeBack, subTypeBack, billType);
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotHeadVo4StatementAccount> getStatementAccountTotalPay(String beginTime, String endTime, Integer organId, String[] organArray,
- Integer hasDebt, String supplierType, String type, String subType,
- String typeBack, String subTypeBack, String billType) {
- List<DepotHeadVo4StatementAccount> list = null;
- try{
- list = depotHeadMapperEx.getStatementAccountTotalPay(beginTime, endTime, organId, organArray, hasDebt, supplierType, type, subType,typeBack, subTypeBack, billType);
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<DepotHeadVo4List> getDetailByNumber(String number, HttpServletRequest request)throws Exception {
- List<DepotHeadVo4List> resList = new ArrayList<>();
- try{
- Long userId = userService.getUserId(request);
- String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
- Map<Long,String> personMap = personService.getPersonMap();
- Map<Long,String> accountMap = accountService.getAccountMap();
- List<DepotHeadVo4List> list = depotHeadMapperEx.getDetailByNumber(number);
- if (null != list) {
- List<Long> idList = new ArrayList<>();
- List<String> numberList = new ArrayList<>();
- for (DepotHeadVo4List dh : list) {
- idList.add(dh.getId());
- numberList.add(dh.getNumber());
- }
- //通过批量查询去构造map
- Map<Long,Integer> financialBillNoMap = getFinancialBillNoMapByBillIdList(idList);
- Map<String,Integer> billSizeMap = getBillSizeMapByLinkNumberList(numberList);
- Map<Long,String> materialsListMap = findMaterialsListMapByHeaderIdList(idList);
- DepotHeadVo4List dh = list.get(0);
- String billCategory = getBillCategory(dh.getSubType());
- if(accountMap!=null && StringUtil.isNotEmpty(dh.getAccountIdList()) && StringUtil.isNotEmpty(dh.getAccountMoneyList())) {
- String accountStr = accountService.getAccountStrByIdAndMoney(accountMap, dh.getAccountIdList(), dh.getAccountMoneyList());
- dh.setAccountName(accountStr);
- }
- if(dh.getAccountIdList() != null) {
- String accountidlistStr = dh.getAccountIdList().replace("[", "").replace("]", "").replaceAll("\"", "");
- dh.setAccountIdList(accountidlistStr);
- }
- if(dh.getAccountMoneyList() != null) {
- String accountmoneylistStr = dh.getAccountMoneyList().replace("[", "").replace("]", "").replaceAll("\"", "");
- dh.setAccountMoneyList(accountmoneylistStr);
- }
- if(dh.getChangeAmount() != null) {
- dh.setChangeAmount(roleService.parseBillPriceByLimit(dh.getChangeAmount().abs(), billCategory, priceLimit, request));
- } else {
- dh.setChangeAmount(BigDecimal.ZERO);
- }
- if(dh.getTotalPrice() != null) {
- dh.setTotalPrice(roleService.parseBillPriceByLimit(dh.getTotalPrice().abs(), billCategory, priceLimit, request));
- }
- BigDecimal discountLastMoney = dh.getDiscountLastMoney()!=null?dh.getDiscountLastMoney():BigDecimal.ZERO;
- dh.setDiscountLastMoney(roleService.parseBillPriceByLimit(discountLastMoney, billCategory, priceLimit, request));
- BigDecimal backAmount = dh.getBackAmount()!=null?dh.getBackAmount():BigDecimal.ZERO;
- dh.setBackAmount(roleService.parseBillPriceByLimit(backAmount, billCategory, priceLimit, request));
- if(dh.getDeposit() == null) {
- dh.setDeposit(BigDecimal.ZERO);
- } else {
- dh.setDeposit(roleService.parseBillPriceByLimit(dh.getDeposit(), billCategory, priceLimit, request));
- }
- //欠款计算
- BigDecimal otherMoney = dh.getOtherMoney()!=null?dh.getOtherMoney():BigDecimal.ZERO;
- BigDecimal deposit = dh.getDeposit()!=null?dh.getDeposit():BigDecimal.ZERO;
- BigDecimal changeAmount = dh.getChangeAmount()!=null?dh.getChangeAmount():BigDecimal.ZERO;
- BigDecimal debt = discountLastMoney.add(otherMoney).subtract((deposit.add(changeAmount)));
- dh.setDebt(roleService.parseBillPriceByLimit(debt, billCategory, priceLimit, request));
- //是否有付款单或收款单
- if(financialBillNoMap!=null) {
- Integer financialBillNoSize = financialBillNoMap.get(dh.getId());
- dh.setHasFinancialFlag(financialBillNoSize!=null && financialBillNoSize>0);
- }
- //是否有退款单
- if(billSizeMap!=null) {
- Integer billListSize = billSizeMap.get(dh.getNumber());
- dh.setHasBackFlag(billListSize!=null && billListSize>0);
- }
- if(StringUtil.isNotEmpty(dh.getSalesMan())) {
- dh.setSalesManStr(personService.getPersonByMapAndIds(personMap,dh.getSalesMan()));
- }
- if(dh.getOperTime() != null) {
- dh.setOperTimeStr(getCenternTime(dh.getOperTime()));
- }
- //商品信息简述
- if(materialsListMap!=null) {
- dh.setMaterialsList(materialsListMap.get(dh.getId()));
- }
- User creatorUser = userService.getUser(dh.getCreator());
- if(creatorUser != null){
- dh.setCreatorName(creatorUser.getUsername());
- }
- resList.add(dh);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return resList;
- }
- /**
- * 查询除当前单据之外的关联单据列表
- * @param linkNumber
- * @param number
- * @return
- * @throws Exception
- */
- @Override
- public List<DepotHead> getListByLinkNumberExceptCurrent(String linkNumber, String number, String type)throws Exception {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andLinkNumberEqualTo(linkNumber).andNumberNotEqualTo(number).andTypeEqualTo(type)
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- return depotHeadMapper.selectByExample(example);
- }
- /**
- * 查询除当前单据之外的关联单据列表
- * @param linkApply
- * @param number
- * @return
- * @throws Exception
- */
- @Override
- public List<DepotHead> getListByLinkApplyExceptCurrent(String linkApply, String number, String type)throws Exception {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andLinkApplyEqualTo(linkApply).andNumberNotEqualTo(number).andTypeEqualTo(type)
- .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- return depotHeadMapper.selectByExample(example);
- }
- /**
- * 根据原单号查询关联的单据列表(批量)
- * @param linkNumberList
- * @return
- * @throws Exception
- */
- @Override
- public List<DepotHead> getBillListByLinkNumberList(List<String> linkNumberList)throws Exception {
- if(linkNumberList!=null && linkNumberList.size()>0) {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andLinkNumberIn(linkNumberList).andSubTypeLike("退货").andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- return depotHeadMapper.selectByExample(example);
- } else {
- return new ArrayList<>();
- }
- }
- /**
- * 根据原单号查询关联的单据列表
- * @param linkNumber
- * @return
- * @throws Exception
- */
- @Override
- public List<DepotHead> getBillListByLinkNumber(String linkNumber)throws Exception {
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andLinkNumberEqualTo(linkNumber).andSubTypeLike("退货").andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- return depotHeadMapper.selectByExample(example);
- }
- /**
- * 新增单据主表及单据子表信息
- * @param beanJson
- * @param rows
- * @param request
- * @throws Exception
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void addDepotHeadAndDetail(String beanJson, String rows,
- HttpServletRequest request) throws Exception {
- /**处理单据主表数据*/
- DepotHead depotHead = JSONObject.parseObject(beanJson, DepotHead.class);
- //校验单号是否重复
- if(checkIsBillNumberExist(0L, depotHead.getNumber())>0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_MSG));
- }
- //校验是否同时录入关联请购单号和关联订单号
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber()) && StringUtil.isNotEmpty(depotHead.getLinkApply())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_MSG));
- }
- String subType = depotHead.getSubType();
- //结算账户校验
- if("采购订单".equals(subType) || "采购".equals(subType) || "采购退货".equals(subType) || "销售订单".equals(subType) || "销售".equals(subType) || "销售退货".equals(subType)) {
- if (StringUtil.isEmpty(depotHead.getAccountIdList()) && depotHead.getAccountId() == null) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_MSG));
- }
- }
- //判断用户是否已经登录过,登录过不再处理
- User userInfo=userService.getCurrentUser();
- //创建人
- depotHead.setCreator(userInfo==null?null:userInfo.getId());
- //修改操作人
- depotHead.setOperId(userInfo.getId());
- //创建时间
- depotHead.setCreateTime(new Timestamp(System.currentTimeMillis()));
- //订单状态
- if(StringUtil.isEmpty(depotHead.getStatus())) {
- depotHead.setStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
- }
- //采购状态
- depotHead.setPurchaseStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
- //付款类型
- depotHead.setPayType(depotHead.getPayType() == null ? "现付" : depotHead.getPayType());
- if(StringUtil.isNotEmpty(depotHead.getAccountIdList())){
- depotHead.setAccountIdList(depotHead.getAccountIdList().replace("[", "").replace("]", "").replaceAll("\"", ""));
- }
- if(StringUtil.isNotEmpty(depotHead.getAccountMoneyList())) {
- //校验多账户的结算金额
- String accountMoneyList = depotHead.getAccountMoneyList().replace("[", "").replace("]", "").replaceAll("\"", "");
- BigDecimal sum = StringUtil.getArrSum(accountMoneyList.split(","));
- BigDecimal manyAccountSum = sum.abs();
- if(manyAccountSum.compareTo(depotHead.getChangeAmount().abs())!=0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_MSG));
- }
- depotHead.setAccountMoneyList(accountMoneyList);
- }
- //校验累计扣除订金是否超出订单中的金额
- if(depotHead.getDeposit()!=null && StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
- BigDecimal finishDeposit = depotHeadMapperEx.getFinishDepositByNumberExceptCurrent(depotHead.getLinkNumber(), depotHead.getNumber());
- //订单中的订金金额
- BigDecimal changeAmount = getDepotHead(depotHead.getLinkNumber()).getChangeAmount();
- if(changeAmount!=null) {
- BigDecimal preDeposit = changeAmount.abs();
- if(depotHead.getDeposit().add(finishDeposit).compareTo(preDeposit)>0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_DEPOSIT_OVER_PRE_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_DEPOSIT_OVER_PRE_MSG));
- }
- }
- }
- //校验附件的数量
- if(StringUtil.isNotEmpty(depotHead.getFileName())) {
- String[] fileArr = depotHead.getFileName().split(",");
- if(fileArr.length>4) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_MSG, 4));
- }
- }
- JSONArray rowArr = JSONArray.parseArray(rows);
- //商品数量
- int operNumber = 0;
- for (int i = 0; i < rowArr.size(); i++) {
- JSONObject rowObj = JSONObject.parseObject(rowArr.getString(i));
- operNumber = operNumber + rowObj.getInteger("operNumber");
- }
- depotHead.setGoodsQuantity(operNumber);
- //商品总类
- depotHead.setGoodsTypeCount(rowArr.size());
- //添加主表
- depotHeadMapper.insertSelective(depotHead);
- /**入库和出库处理预付款信息*/
- if(BusinessConstants.PAY_TYPE_PREPAID.equals(depotHead.getPayType())){
- if(depotHead.getOrganId()!=null) {
- BigDecimal currentAdvanceIn = supplierService.getSupplier(depotHead.getOrganId()).getAdvanceIn();
- if(currentAdvanceIn.compareTo(depotHead.getTotalPrice()) >= 0) {
- //更新会员的预付款
- supplierService.updateAdvanceIn(depotHead.getOrganId());
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_MEMBER_PAY_LACK_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_MEMBER_PAY_LACK_MSG));
- }
- }
- }
- //根据单据编号查询单据id
- DepotHeadExample dhExample = new DepotHeadExample();
- dhExample.createCriteria().andNumberEqualTo(depotHead.getNumber()).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list = depotHeadMapper.selectByExample(dhExample);
- if(list != null) {
- Long headId = list.get(0).getId();
- /**入库和出库处理单据子表信息*/
- depotItemService.saveDetails(rows,headId, "add",request);
- //订单申请审核
- applyAudit(depotHead);
- }
- //修改订单总额
- //updateTotalPriceById(list.get(0));
- logService.insertLog("单据",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(depotHead.getNumber()).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- }
- /**
- * 更新单据主表及单据子表信息
- * @param beanJson
- * @param rows
- * @param request
- * @throws Exception
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateDepotHeadAndDetail(String beanJson, String rows, HttpServletRequest request)throws Exception {
- /**更新单据主表信息*/
- DepotHead depotHead = JSONObject.parseObject(beanJson, DepotHead.class);
- //校验单号是否重复
- if(checkIsBillNumberExist(depotHead.getId(), depotHead.getNumber())>0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_MSG));
- }
- //校验是否同时录入关联请购单号和关联订单号
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber()) && StringUtil.isNotEmpty(depotHead.getLinkApply())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_MSG));
- }
- //校验单据状态,如果不是未审核则提示
- if(!"0".equals(getDepotHead(depotHead.getId()).getStatus()) && !"8".equals(getDepotHead(depotHead.getId()).getStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BILL_CANNOT_EDIT_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_BILL_CANNOT_EDIT_MSG));
- }
- //获取之前的会员id
- Long preOrganId = getDepotHead(depotHead.getId()).getOrganId();
- String subType = depotHead.getSubType();
- //结算账户校验
- if("采购".equals(subType) || "采购退货".equals(subType) || "销售".equals(subType) || "销售退货".equals(subType)) {
- if (StringUtil.isEmpty(depotHead.getAccountIdList()) && depotHead.getAccountId() == null) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_MSG));
- }
- }
- if(StringUtil.isNotEmpty(depotHead.getAccountIdList())){
- depotHead.setAccountIdList(depotHead.getAccountIdList().replace("[", "").replace("]", "").replaceAll("\"", ""));
- }
- if(StringUtil.isNotEmpty(depotHead.getAccountMoneyList())) {
- //校验多账户的结算金额
- String accountMoneyList = depotHead.getAccountMoneyList().replace("[", "").replace("]", "").replaceAll("\"", "");
- BigDecimal sum = StringUtil.getArrSum(accountMoneyList.split(","));
- BigDecimal manyAccountSum = sum.abs();
- if(manyAccountSum.compareTo(depotHead.getChangeAmount().abs())!=0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_MSG));
- }
- depotHead.setAccountMoneyList(accountMoneyList);
- }
- //校验累计扣除订金是否超出订单中的金额
- if(depotHead.getDeposit()!=null && StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
- BigDecimal finishDeposit = depotHeadMapperEx.getFinishDepositByNumberExceptCurrent(depotHead.getLinkNumber(), depotHead.getNumber());
- //订单中的订金金额
- BigDecimal changeAmount = getDepotHead(depotHead.getLinkNumber()).getChangeAmount();
- if(changeAmount!=null) {
- BigDecimal preDeposit = changeAmount.abs();
- if(depotHead.getDeposit().add(finishDeposit).compareTo(preDeposit)>0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_DEPOSIT_OVER_PRE_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_DEPOSIT_OVER_PRE_MSG));
- }
- }
- }
- //校验附件的数量
- if(StringUtil.isNotEmpty(depotHead.getFileName())) {
- String[] fileArr = depotHead.getFileName().split(",");
- if(fileArr.length>4) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_MSG, 4));
- }
- }
- JSONArray rowArr = JSONArray.parseArray(rows);
- //商品数量
- int operNumber = 0;
- for (int i = 0; i < rowArr.size(); i++) {
- JSONObject rowObj = JSONObject.parseObject(rowArr.getString(i));
- operNumber = operNumber + rowObj.getInteger("operNumber");
- }
- depotHead.setGoodsQuantity(operNumber);
- //商品总类
- depotHead.setGoodsTypeCount(rowArr.size());
- //修改时间
- depotHead.setOperTime(new Date());
- //修改操作人
- depotHead.setOperId(userService.getCurrentUser().getId());
- //修改单据主表
- depotHeadMapper.updateByPrimaryKeySelective(depotHead);
- //如果存在多账户结算需要将原账户的id置空
- if(StringUtil.isNotEmpty(depotHead.getAccountIdList())) {
- depotHeadMapperEx.setAccountIdToNull(depotHead.getId());
- }
- /**入库和出库处理预付款信息*/
- if(BusinessConstants.PAY_TYPE_PREPAID.equals(depotHead.getPayType())){
- if(depotHead.getOrganId()!=null){
- BigDecimal currentAdvanceIn = supplierService.getSupplier(depotHead.getOrganId()).getAdvanceIn();
- if(currentAdvanceIn.compareTo(depotHead.getTotalPrice())>=0) {
- //更新会员的预付款
- supplierService.updateAdvanceIn(depotHead.getOrganId());
- if(null != preOrganId && !preOrganId.equals(depotHead.getOrganId())) {
- //更新之前会员的预付款
- supplierService.updateAdvanceIn(preOrganId);
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_MEMBER_PAY_LACK_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_MEMBER_PAY_LACK_MSG));
- }
- }
- }
- /**入库和出库处理单据子表信息*/
- depotItemService.saveDetails(rows,depotHead.getId(), "update",request);
- //保存并申请审核
- applyAudit(depotHead);
- logService.insertLog("单据",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_EDIT).append(depotHead.getNumber()).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- }
- @Override
- public Map<String, Object> getBuyAndSaleStatistics(String today, String monthFirstDay, String yesterdayBegin, String yesterdayEnd,
- String yearBegin, String yearEnd, HttpServletRequest request) throws Exception {
- Long userId = userService.getUserId(request);
- String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- String[] creatorArray = getCreatorArray();
- List<InOutPriceVo> inOutPriceVoList = depotHeadMapperEx.getBuyAndSaleStatisticsList(yearBegin, yearEnd, creatorArray, forceFlag);
- String[] periods = {"today", "month", "yesterday", "year"};
- String[] types = {"Buy", "BuyBack", "Sale", "SaleBack", "RetailSale", "RetailSaleBack"};
- Map<String, BigDecimal> statistics = new HashMap<>();
- // 初始化 statistics Map
- for (String period : periods) {
- for (String type : types) {
- statistics.put(period + type, BigDecimal.ZERO);
- }
- }
- Date todayDate = Tools.strToDate(today);
- Date monthFirstDate = Tools.strToDate(monthFirstDay);
- Date yesterdayStartDate = Tools.strToDate(yesterdayBegin);
- Date yesterdayEndDate = Tools.strToDate(yesterdayEnd);
- Date yearStartDate = Tools.strToDate(yearBegin);
- Date yearEndDate = Tools.strToDate(yearEnd);
- for (InOutPriceVo item : inOutPriceVoList) {
- Date operTime = item.getOperTime();
- BigDecimal discountLastMoney = item.getDiscountLastMoney();
- BigDecimal totalPriceAbs = item.getTotalPrice().abs();
- if (isWithinRange(operTime, todayDate, Tools.strToDate(getNow3()))) {
- updateStatistics(statistics, item, "today", discountLastMoney, totalPriceAbs);
- }
- if (isWithinRange(operTime, monthFirstDate, Tools.strToDate(getNow3()))) {
- updateStatistics(statistics, item, "month", discountLastMoney, totalPriceAbs);
- }
- if (isWithinRange(operTime, yesterdayStartDate, yesterdayEndDate)) {
- updateStatistics(statistics, item, "yesterday", discountLastMoney, totalPriceAbs);
- }
- if (isWithinRange(operTime, yearStartDate, yearEndDate)) {
- updateStatistics(statistics, item, "year", discountLastMoney, totalPriceAbs);
- }
- }
- Map<String, Object> result = new HashMap<>();
- for (String period : periods) {
- result.put(period + "Buy", roleService.parseHomePriceByLimit(statistics.get(period + "Buy").subtract(statistics.get(period + "BuyBack")), "buy", priceLimit, "***", request));
- result.put(period + "Sale", roleService.parseHomePriceByLimit(statistics.get(period + "Sale").subtract(statistics.get(period + "SaleBack")), "sale", priceLimit, "***", request));
- result.put(period + "RetailSale", roleService.parseHomePriceByLimit(statistics.get(period + "RetailSale").subtract(statistics.get(period + "RetailSaleBack")), "retail", priceLimit, "***", request));
- }
- return result;
- }
- private boolean isWithinRange(Date operTime, Date startDate, Date endDate) {
- return operTime.compareTo(startDate) >= 0 && operTime.compareTo(endDate) <= 0;
- }
- private void updateStatistics(Map<String, BigDecimal> statistics, InOutPriceVo item, String period, BigDecimal discountLastMoney, BigDecimal totalPriceAbs) {
- switch (item.getType()) {
- case "入库":
- switch (item.getSubType()) {
- case "采购":
- statistics.put(period + "Buy", statistics.get(period + "Buy").add(discountLastMoney));
- break;
- case "销售退货":
- statistics.put(period + "SaleBack", statistics.get(period + "SaleBack").add(discountLastMoney));
- break;
- case "零售退货":
- statistics.put(period + "RetailSaleBack", statistics.get(period + "RetailSaleBack").add(totalPriceAbs));
- break;
- }
- break;
- case "出库":
- switch (item.getSubType()) {
- case "采购退货":
- statistics.put(period + "BuyBack", statistics.get(period + "BuyBack").add(discountLastMoney));
- break;
- case "销售":
- statistics.put(period + "Sale", statistics.get(period + "Sale").add(discountLastMoney));
- break;
- case "零售":
- statistics.put(period + "RetailSale", statistics.get(period + "RetailSale").add(totalPriceAbs));
- break;
- }
- break;
- }
- }
- /**
- * 根据单据号获取单据主表信息
- */
- @Override
- public DepotHead getDepotHead(String number) {
- DepotHead depotHead = new DepotHead();
- try{
- DepotHeadExample example = new DepotHeadExample();
- example.createCriteria().andNumberEqualTo(number).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list = depotHeadMapper.selectByExample(example);
- if(null!=list && list.size()>0) {
- depotHead = list.get(0);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return depotHead;
- }
- @Override
- public List<DepotHeadVo4List> debtList(Long organId, String materialParam, String number, String beginTime, String endTime,
- String status, Integer offset, Integer rows) {
- List<DepotHeadVo4List> resList = new ArrayList<>();
- try{
- String depotIds = depotService.findDepotStrByCurrentUser();
- String [] depotArray=depotIds.split(",");
- String [] creatorArray = getCreatorArray();
- beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
- List<DepotHeadVo4List> list=depotHeadMapperEx.debtList(organId, creatorArray, status, number,
- beginTime, endTime, materialParam, depotArray, offset, rows);
- if (null != list) {
- resList = parseDebtBillList(list);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return resList;
- }
- @Override
- public int debtListCount(Long organId, String materialParam, String number, String beginTime, String endTime,
- String status) {
- int total = 0;
- try {
- String depotIds = depotService.findDepotStrByCurrentUser();
- String[] depotArray = depotIds.split(",");
- String[] creatorArray = getCreatorArray();
- beginTime = Tools.parseDayToTime(beginTime, BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime, BusinessConstants.DAY_LAST_TIME);
- total = depotHeadMapperEx.debtListCount(organId, creatorArray, status, number,
- beginTime, endTime, materialParam, depotArray);
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- return total;
- }
- @Override
- public void debtExport(Long organId, String materialParam, String number, String type, String subType,
- String beginTime, String endTime, String status, String mpList,
- HttpServletRequest request, HttpServletResponse response) {
- try {
- Long userId = userService.getUserId(request);
- String priceLimit = userService.getRoleTypeByUserId(userId).getPriceLimit();
- String billCategory = getBillCategory(subType);
- String depotIds = depotService.findDepotStrByCurrentUser();
- String[] depotArray = depotIds.split(",");
- String[] creatorArray = getCreatorArray();
- status = StringUtil.isNotEmpty(status) ? status : null;
- beginTime = Tools.parseDayToTime(beginTime, BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime, BusinessConstants.DAY_LAST_TIME);
- List<DepotHeadVo4List> dhList = new ArrayList<>();
- List<DepotHeadVo4List> list = depotHeadMapperEx.debtList(organId, creatorArray, status, number,
- beginTime, endTime, materialParam, depotArray, null, null);
- if (null != list) {
- dhList = parseDebtBillList(list);
- }
- //生成Excel文件
- String fileName = "单据信息";
- File file = new File("/opt/"+ fileName);
- WritableWorkbook wtwb = Workbook.createWorkbook(file);
- String oneTip = "";
- String sheetOneStr = "";
- if("采购".equals(subType)) {
- oneTip = "供应商对账列表";
- sheetOneStr = "供应商,单据编号,关联单据,商品信息,单据日期,操作员,单据金额,本单欠款,已付欠款,待付欠款,备注";
- } else if("出库".equals(type) && "销售".equals(subType)) {
- oneTip = "客户对账列表";
- sheetOneStr = "客户,单据编号,关联单据,商品信息,单据日期,操作员,单据金额,本单欠款,已收欠款,待收欠款,备注";
- }
- if(StringUtil.isNotEmpty(beginTime) && StringUtil.isNotEmpty(endTime)) {
- oneTip = oneTip + "(" + beginTime + "至" + endTime + ")";
- }
- List<String> sheetOneList = StringUtil.strToStringList(sheetOneStr);
- String[] sheetOneArr = StringUtil.listToStringArray(sheetOneList);
- List<Long> idList = new ArrayList<>();
- List<String[]> billList = new ArrayList<>();
- Map<Long, BillListCacheVo> billListCacheVoMap = new HashMap<>();
- for (DepotHeadVo4List dh : dhList) {
- idList.add(dh.getId());
- BillListCacheVo billListCacheVo = new BillListCacheVo();
- billListCacheVo.setNumber(dh.getNumber());
- billListCacheVo.setOrganName(dh.getOrganName());
- billListCacheVo.setOperTimeStr(getCenternTime(dh.getOperTime()));
- billListCacheVoMap.put(dh.getId(), billListCacheVo);
- String[] objs = new String[sheetOneArr.length];
- objs[0] = dh.getOrganName();
- objs[1] = dh.getNumber();
- objs[2] = dh.getLinkNumber();
- objs[3] = dh.getMaterialsList();
- objs[4] = dh.getOperTimeStr();
- objs[5] = dh.getUserName();
- BigDecimal discountLastMoney = dh.getDiscountLastMoney() == null ? BigDecimal.ZERO : dh.getDiscountLastMoney();
- BigDecimal otherMoney = dh.getOtherMoney() == null ? BigDecimal.ZERO : dh.getOtherMoney();
- BigDecimal deposit = dh.getDeposit() == null ? BigDecimal.ZERO : dh.getDeposit();
- objs[6] = parseDecimalToStr(discountLastMoney.add(otherMoney).subtract(deposit), 2);
- objs[7] = parseDecimalToStr(dh.getNeedDebt(), 2);
- objs[8] = parseDecimalToStr(dh.getFinishDebt(), 2);
- objs[9] = parseDecimalToStr(dh.getDebt(), 2);
- objs[10] = dh.getRemark();
- billList.add(objs);
- }
- ExcelUtils.exportObjectsManySheet(wtwb, oneTip, sheetOneArr, "单据列表", 0, billList);
- //导出明细数据
- if(idList.size()>0) {
- List<DepotItemVo4WithInfoEx> dataList = depotItemMapperEx.getBillDetailListByIds(idList);
- String[] mpArr = mpList.split(",");
- String twoTip = "";
- String sheetTwoStr = "";
- if ("采购".equals(subType)) {
- twoTip = "供应商单据明细";
- sheetTwoStr = "供应商,单据编号,单据日期,仓库名称,条码,名称,规格,型号,颜色,品牌,制造商,扩展信息,单位,序列号,批号,有效期,多属性,数量,单价,金额,税率(%),税额,价税合计,重量,备注";
- } else if ("销售".equals(subType)) {
- twoTip = "客户单据明细";
- sheetTwoStr = "客户,单据编号,单据日期,仓库名称,条码,名称,规格,型号,颜色,品牌,制造商,扩展信息,单位,序列号,批号,有效期,多属性,数量,单价,金额,税率(%),税额,价税合计,重量,备注";
- }
- if (StringUtil.isNotEmpty(beginTime) && StringUtil.isNotEmpty(endTime)) {
- twoTip = twoTip + "(" + beginTime + "至" + endTime + ")";
- }
- List<String> sheetTwoList = StringUtil.strToStringList(sheetTwoStr);
- String[] sheetTwoArr = StringUtil.listToStringArray(sheetTwoList);
- List<String[]> billDetail = new ArrayList<>();
- for (DepotItemVo4WithInfoEx diEx : dataList) {
- String[] objs = new String[sheetTwoArr.length];
- BillListCacheVo billListCacheVo = billListCacheVoMap.get(diEx.getHeaderId());
- objs[0] = billListCacheVo != null ? billListCacheVo.getOrganName() : "";
- objs[1] = billListCacheVo != null ? billListCacheVo.getNumber() : "";
- objs[2] = billListCacheVo != null ? billListCacheVo.getOperTimeStr() : "";
- objs[3] = diEx.getDepotId() == null ? "" : diEx.getDepotName();
- objs[4] = diEx.getBarCode();
- objs[5] = diEx.getMName();
- objs[6] = diEx.getMStandard();
- objs[7] = diEx.getMModel();
- objs[8] = diEx.getMColor();
- objs[9] = diEx.getBrand();
- objs[10] = diEx.getMMfrs();
- objs[11] = depotItemService.getOtherInfo(mpArr, diEx);
- objs[12] = diEx.getMaterialUnit();
- objs[13] = diEx.getSnList();
- objs[14] = diEx.getBatchNumber();
- objs[15] = Tools.parseDateToStr(diEx.getExpirationDate());
- objs[16] = diEx.getSku();
- objs[17] = parseDecimalToStr(diEx.getOperNumber(), 2);
- objs[18] = parseDecimalToStr(roleService.parseBillPriceByLimit(diEx.getUnitPrice(), billCategory, priceLimit, request), 2);
- objs[19] = parseDecimalToStr(roleService.parseBillPriceByLimit(diEx.getAllPrice(), billCategory, priceLimit, request), 2);
- objs[20] = parseDecimalToStr(roleService.parseBillPriceByLimit(diEx.getTaxRate(), billCategory, priceLimit, request), 2);
- objs[21] = parseDecimalToStr(roleService.parseBillPriceByLimit(diEx.getTaxMoney(), billCategory, priceLimit, request), 2);
- objs[22] = parseDecimalToStr(roleService.parseBillPriceByLimit(diEx.getTaxLastMoney(), billCategory, priceLimit, request), 2);
- BigDecimal allWeight = diEx.getBasicNumber() == null || diEx.getWeight() == null ? BigDecimal.ZERO : diEx.getBasicNumber().multiply(diEx.getWeight());
- objs[23] = parseDecimalToStr(allWeight, 2);
- objs[24] = diEx.getRemark();
- billDetail.add(objs);
- }
- ExcelUtils.exportObjectsManySheet(wtwb, twoTip, sheetTwoArr, "单据明细", 1, billDetail);
- }
- wtwb.write();
- wtwb.close();
- ExcelUtils.downloadExcel(file, file.getName(), response);
- } catch(Exception e){
- JshException.readFail(logger, e);
- }
- }
- @Override
- public List<DepotHeadVo4List> parseDebtBillList(List<DepotHeadVo4List> list) throws Exception {
- List<Long> idList = new ArrayList<>();
- List<DepotHeadVo4List> dhList = new ArrayList<>();
- for (DepotHeadVo4List dh : list) {
- idList.add(dh.getId());
- }
- //通过批量查询去构造map
- Map<Long,String> materialsListMap = findMaterialsListMapByHeaderIdList(idList);
- for (DepotHeadVo4List dh : list) {
- if(dh.getChangeAmount() != null) {
- dh.setChangeAmount(dh.getChangeAmount().abs());
- }
- if(dh.getTotalPrice() != null) {
- dh.setTotalPrice(dh.getTotalPrice().abs());
- }
- if(dh.getDeposit() == null) {
- dh.setDeposit(BigDecimal.ZERO);
- }
- if(dh.getOperTime() != null) {
- dh.setOperTimeStr(getCenternTime(dh.getOperTime()));
- }
- BigDecimal discountLastMoney = dh.getDiscountLastMoney()!=null?dh.getDiscountLastMoney():BigDecimal.ZERO;
- BigDecimal otherMoney = dh.getOtherMoney()!=null?dh.getOtherMoney():BigDecimal.ZERO;
- BigDecimal deposit = dh.getDeposit()!=null?dh.getDeposit():BigDecimal.ZERO;
- BigDecimal changeAmount = dh.getChangeAmount()!=null?dh.getChangeAmount().abs():BigDecimal.ZERO;
- //本单欠款(如果退货则为负数)
- dh.setNeedDebt(discountLastMoney.add(otherMoney).subtract(deposit.add(changeAmount)));
- if(BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(dh.getSubType()) || BusinessConstants.SUB_TYPE_SALES_RETURN.equals(dh.getSubType())) {
- dh.setNeedDebt(BigDecimal.ZERO.subtract(dh.getNeedDebt()));
- }
- BigDecimal needDebt = dh.getNeedDebt()!=null?dh.getNeedDebt():BigDecimal.ZERO;
- BigDecimal finishDebt = accountItemService.getEachAmountByBillId(dh.getId());
- finishDebt = finishDebt!=null?finishDebt:BigDecimal.ZERO;
- //已收欠款
- dh.setFinishDebt(finishDebt);
- //待收欠款
- dh.setDebt(needDebt.subtract(finishDebt));
- //商品信息简述
- if(materialsListMap!=null) {
- dh.setMaterialsList(materialsListMap.get(dh.getId()));
- }
- dhList.add(dh);
- }
- return dhList;
- }
- @Override
- public String getBillCategory(String subType) {
- if(subType.equals("零售") || subType.equals("零售退货")) {
- return "retail";
- } else if(subType.equals("销售订单") || subType.equals("销售") || subType.equals("销售退货")) {
- return "sale";
- } else {
- return "buy";
- }
- }
- /**
- * 格式化金额样式
- * @param decimal
- * @param num
- * @return
- */
- private String parseDecimalToStr(BigDecimal decimal, Integer num) {
- return decimal == null ? "" : decimal.setScale(num, BigDecimal.ROUND_HALF_UP).toString();
- }
- private String parseStatusToStr(String status, String type) {
- if(StringUtil.isNotEmpty(status)) {
- if("purchase".equals(type)) {
- switch (status) {
- case "2":
- return "完成采购";
- case "3":
- return "部分采购";
- }
- } else if("sale".equals(type)) {
- switch (status) {
- case "2":
- return "完成销售";
- case "3":
- return "部分销售";
- }
- }
- switch (status) {
- case "0":
- return "未审核";
- case "1":
- return "已审核";
- case "9":
- return "审核中";
- }
- }
- return "";
- }
- @Override
- public List<DepotHeadVo4List> waitBillList(String number, String materialParam, String type, String subType,
- String beginTime, String endTime, String status, int offset, int rows) {
- List<DepotHeadVo4List> resList = new ArrayList<>();
- try{
- String [] depotArray = getDepotArray("其它");
- //给仓管可以看全部的单据(此时可以通过分配仓库去控制权限)
- String [] creatorArray = null;
- String [] subTypeArray = StringUtil.isNotEmpty(subType) ? subType.split(",") : null;
- String [] statusArray = StringUtil.isNotEmpty(status) ? status.split(",") : null;
- Map<Long,String> accountMap = accountService.getAccountMap();
- beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
- List<DepotHeadVo4List> list = depotHeadMapperEx.waitBillList(type, subTypeArray, creatorArray, statusArray, number, beginTime, endTime,
- materialParam, depotArray, offset, rows);
- if (null != list) {
- List<Long> idList = new ArrayList<>();
- for (DepotHeadVo4List dh : list) {
- idList.add(dh.getId());
- }
- //通过批量查询去构造map
- Map<Long,String> materialsListMap = findMaterialsListMapByHeaderIdList(idList);
- Map<Long,BigDecimal> materialCountListMap = getMaterialCountListMapByHeaderIdList(idList);
- for (DepotHeadVo4List dh : list) {
- if(accountMap!=null && StringUtil.isNotEmpty(dh.getAccountIdList()) && StringUtil.isNotEmpty(dh.getAccountMoneyList())) {
- String accountStr = accountService.getAccountStrByIdAndMoney(accountMap, dh.getAccountIdList(), dh.getAccountMoneyList());
- dh.setAccountName(accountStr);
- }
- if(dh.getOperTime() != null) {
- dh.setOperTimeStr(getCenternTime(dh.getOperTime()));
- }
- //商品信息简述
- if(materialsListMap!=null) {
- dh.setMaterialsList(materialsListMap.get(dh.getId()));
- }
- //商品总数量
- if(materialCountListMap!=null) {
- dh.setMaterialCount(materialCountListMap.get(dh.getId()));
- }
- resList.add(dh);
- }
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return resList;
- }
- @Override
- public Long waitBillCount(String number, String materialParam, String type, String subType,
- String beginTime, String endTime, String status) {
- Long result=null;
- try{
- String [] depotArray = getDepotArray("其它");
- //给仓管可以看全部的单据(此时可以通过分配仓库去控制权限)
- String [] creatorArray = null;
- String [] subTypeArray = StringUtil.isNotEmpty(subType) ? subType.split(",") : null;
- String [] statusArray = StringUtil.isNotEmpty(status) ? status.split(",") : null;
- beginTime = Tools.parseDayToTime(beginTime,BusinessConstants.DAY_FIRST_TIME);
- endTime = Tools.parseDayToTime(endTime,BusinessConstants.DAY_LAST_TIME);
- result=depotHeadMapperEx.waitBillCount(type, subTypeArray, creatorArray, statusArray, number, beginTime, endTime,
- materialParam, depotArray);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void batchAddDepotHeadAndDetail(String ids, HttpServletRequest request) throws Exception {
- List<DepotHead> dhList = getDepotHeadListByIds(ids);
- StringBuilder sb = new StringBuilder();
- User userInfo=userService.getCurrentUser();
- for(DepotHead depotHead : dhList) {
- String prefixNo = BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType())?"QTRK":"QTCK";
- //关联单据单号
- String oldNumber = depotHead.getNumber();
- //校验单据最新状态不能进行批量操作
- if("0".equals(depotHead.getStatus()) || "2".equals(depotHead.getStatus()) || "9".equals(depotHead.getStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_NEW_STATUS_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_NEW_STATUS_FAILED_MSG, oldNumber, depotHead.getType()));
- }
- //校验是否是部分入库或者部分出库
- if("3".equals(depotHead.getStatus())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_PARTIALLY_STATUS_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_PARTIALLY_STATUS_FAILED_MSG, oldNumber, depotHead.getType()));
- }
- depotHead.setLinkNumber(oldNumber);
- //给单号重新赋值
- String number = prefixNo + sequenceService.buildOnlyNumber();
- depotHead.setNumber(number);
- depotHead.setDefaultNumber(number);
- depotHead.setOperTime(new Date());
- depotHead.setSubType(BusinessConstants.SUB_TYPE_OTHER);
- depotHead.setChangeAmount(BigDecimal.ZERO);
- depotHead.setTotalPrice(BigDecimal.ZERO);
- depotHead.setDiscountLastMoney(BigDecimal.ZERO);
- depotHead.setCreator(userInfo==null?null:userInfo.getId());
- depotHead.setOrganId(null);
- depotHead.setAccountId(null);
- depotHead.setAccountIdList(null);
- depotHead.setAccountMoneyList(null);
- depotHead.setFileName(null);
- depotHead.setSalesMan(null);
- depotHead.setStatus("0");
- depotHead.setTenantId(null);
- //查询明细
- List<DepotItemVo4WithInfoEx> itemList = depotItemService.getDetailList(depotHead.getId());
- depotHead.setId(null);
- JSONArray rowArr = new JSONArray();
- for(DepotItemVo4WithInfoEx item: itemList) {
- if("1".equals(item.getEnableSerialNumber())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_SERIAL_NUMBER_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_SERIAL_NUMBER_FAILED_MSG, oldNumber));
- }
- if("1".equals(item.getEnableBatchNumber())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_BATCH_NUMBER_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_BATCH_NUMBER_FAILED_MSG, oldNumber));
- }
- item.setUnitPrice(BigDecimal.ZERO);
- item.setAllPrice(BigDecimal.ZERO);
- item.setLinkId(item.getId());
- item.setTenantId(null);
- String itemStr = JSONObject.toJSONString(item);
- JSONObject itemObj = JSONObject.parseObject(itemStr);
- itemObj.put("unit", itemObj.getString("materialUnit"));
- rowArr.add(itemObj.toJSONString());
- }
- String rows = rowArr.toJSONString();
- //新增其它入库单或其它出库单
- sb.append("[").append(depotHead.getNumber()).append("]");
- depotHeadMapper.insertSelective(depotHead);
- //根据单据编号查询单据id
- DepotHeadExample dhExample = new DepotHeadExample();
- dhExample.createCriteria().andNumberEqualTo(depotHead.getNumber()).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list = depotHeadMapper.selectByExample(dhExample);
- if(list!=null) {
- Long headId = list.get(0).getId();
- /**入库和出库处理单据子表信息*/
- depotItemService.saveDetails(rows, headId, "add", request);
- }
- }
- logService.insertLog("单据",
- new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_BATCH_ADD).append(sb).toString(),
- ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest());
- }
- @Override
- public String syncOrderToXsdd(DepotHeadXsddRequestVO depotHead, List<DepotItemXsddRequestVO> depotItemList) throws Exception {
- // 校验单号是否重复
- if (checkIsBillNumberExist(0L, depotHead.getNumber()) > 0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_CODE, String.format(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_MSG));
- }
- // 添加主表
- depotHeadMapper.insertSelective(depotHead);
- // 根据单据编号查询单据id
- DepotHeadExample dhExample = new DepotHeadExample();
- dhExample.createCriteria().andNumberEqualTo(depotHead.getNumber()).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotHead> list = depotHeadMapper.selectByExample(dhExample);
- if (list != null) {
- Long headId = list.get(0).getId();
- /** 销售订单 单据子表信息 */
- depotItemService.saveOrderItem(depotHead, depotItemList);
- List<Long> mIdList = depotItemList.stream().map(DepotItemXsddRequestVO::getMaterialId).collect(Collectors.toList());
- //同步集采库存
- syncTescoSystemService.sycnTescoStock(mIdList);
- }
- logService.insertLog("销售订单", new StringBuffer(BusinessConstants.LOG_OPERATION_TYPE_ADD).append(depotHead.getNumber()).toString(), ((ServletRequestAttributes)RequestContextHolder.getRequestAttributes()).getRequest());
- return "";
- }
- /**
- * 根据商品id获取最后一条销售订单
- * @param mid 商品id
- * @return 商品信息
- */
- @Override
- public DepotHead getDepotLastByMaterialId(long mid) {
- return depotHeadMapper.getDepotLastByMaterialId(mid);
- }
- /**
- * pda订单提交
- * @param pdaDepotHeadDTO
- * @return
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public boolean pdaOrderSubmit(PDADepotHeadDTO pdaDepotHeadDTO) throws Exception {
- //根据单据号获取单据信息
- DepotHead depotHead = depotHeadMapper.selectOne(new LambdaQueryWrapperX<DepotHead>().eq(DepotHead::getId, pdaDepotHeadDTO.getId()));
- //根据单据id获取单据子表信息
- List<DepotItem> depotItems = depotItemService.getListByHeaderId(depotHead.getId());
- //将主表信息改为出入库单据主表
- //设置单据主表信息
- depotHead.setId(null);
- //设置关联订单
- depotHead.setLinkNumber(depotHead.getNumber());
- String number = sequenceService.buildOnlyNumber();
- //设置单号
- if ("采购订单".equals(depotHead.getSubType())){
- depotHead.setSubType("采购");
- depotHead.setType("入库");
- depotHead.setDefaultNumber("CGRK" + number) ;
- depotHead.setNumber("CGRK" + number);
- }else if ("销售订单".equals(depotHead.getSubType())){
- depotHead.setSubType("销售");
- depotHead.setType("出库");
- depotHead.setDefaultNumber("XSCK" + number) ;
- depotHead.setNumber("XSCK" + number);
- }
- //校验单号是否重复
- if(checkIsBillNumberExist(0L, depotHead.getNumber())>0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_BILL_NUMBER_EXIST_MSG));
- }
- //校验是否同时录入关联请购单号和关联订单号
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber()) && StringUtil.isNotEmpty(depotHead.getLinkApply())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_EXIST_REPEAT_NO_FAILED_MSG));
- }
- String subType = depotHead.getSubType();
- //结算账户校验
- if("采购".equals(subType) || "采购退货".equals(subType) || "销售".equals(subType) || "销售退货".equals(subType)) {
- if (StringUtil.isEmpty(depotHead.getAccountIdList()) && depotHead.getAccountId() == null) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ACCOUNT_FAILED_MSG));
- }
- }
- //判断用户是否已经登录过,登录过不再处理
- User userInfo = userService.getCurrentUser();
- //创建人
- depotHead.setCreator(userInfo==null?null:userInfo.getId());
- //创建时间
- depotHead.setCreateTime(new Timestamp(System.currentTimeMillis()));
- //操作人
- depotHead.setOperId(userInfo == null ? null : userInfo.getId());
- //单据时间
- depotHead.setOperTime(new Timestamp(System.currentTimeMillis()));
- if(StringUtil.isEmpty(depotHead.getStatus())) {
- depotHead.setStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
- }
- depotHead.setPurchaseStatus(BusinessConstants.BILLS_STATUS_UN_AUDIT);
- depotHead.setPayType(depotHead.getPayType() == null ? "现付" : depotHead.getPayType());
- if(StringUtil.isNotEmpty(depotHead.getAccountIdList())){
- depotHead.setAccountIdList(depotHead.getAccountIdList().replace("[", "").replace("]", "").replaceAll("\"", ""));
- }
- if(StringUtil.isNotEmpty(depotHead.getAccountMoneyList())) {
- //校验多账户的结算金额
- String accountMoneyList = depotHead.getAccountMoneyList().replace("[", "").replace("]", "").replaceAll("\"", "");
- BigDecimal sum = StringUtil.getArrSum(accountMoneyList.split(","));
- BigDecimal manyAccountSum = sum.abs();
- if(manyAccountSum.compareTo(depotHead.getChangeAmount().abs())!=0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_MANY_ACCOUNT_FAILED_MSG));
- }
- depotHead.setAccountMoneyList(accountMoneyList);
- }
- //校验附件的数量
- if(StringUtil.isNotEmpty(depotHead.getFileName())) {
- String[] fileArr = depotHead.getFileName().split(",");
- if(fileArr.length>4) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_FILE_NUM_LIMIT_MSG, 4));
- }
- }
- //商品数量
- int operNumber = 0;
- List<PDADepotMaterialDto> list = pdaDepotHeadDTO.getMaterials();
- Map<String,PDADepotMaterialDto> materialMap = new HashMap<>();
- for (int i = 0; i < list.size(); i++) {
- operNumber = operNumber + list.get(i).getMaterialNumber().intValue();
- materialMap.put(list.get(i).getBarCode(),list.get(i));
- }
- depotHead.setGoodsQuantity(operNumber);
- //商品总类
- depotHead.setGoodsTypeCount(list.size());
- //设置凭证图片、备注
- depotHead.setVoucherPicture(pdaDepotHeadDTO.getVoucherPicture());
- depotHead.setRemark(pdaDepotHeadDTO.getRemark());
- depotHead.setCreateTime(new Timestamp(System.currentTimeMillis()));
- depotHead.setStatus("6");
- //添加主表
- depotHeadMapper.insertSelective(depotHead);
- //根据单据编号查询单据id
- long id = depotHeadMapper.selectOne(new QueryWrapperX<DepotHead>().eq("number",depotHead.getNumber())).getId();
- //设置单据子表信息
- for (DepotItem depotItem : depotItems) {
- //获取子表单商品
- MaterialExtend materialExtend = materialExtendService.getMaterialExtend(depotItem.getMaterialExtendId());
- //获取商品条码
- String barCode = materialExtend.getSku();
- //校验商品保质期
- if (depotHead.getType().equals("入库")){
- //修改采购订单的生产日期和库位
- depotItemService.update(new UpdateWrapper<DepotItem>().set("production_date",depotItem.getProductionDate())
- .set("position",depotItem.getPosition())
- .eq("id",depotItem.getId()));
- //获取商品信息
- Material m = materialService.getMaterial(materialExtend.getMaterialId());
- //获取商品类别
- MaterialCategory mc = materialCategoryService.getMaterialCategory(m.getCategoryId());
- if (mc.getExpiryThreshold() != null && mc.getExpiryThreshold() > 0 && depotItem.getExpiryNum() != null){
- //商品生产天数
- int productionDay = DateUtils.differentDaysByMillisecond(depotItem.getProductionDate(),new Date());
- //保质期剩余天数
- int day = depotItem.getExpiryNum() - productionDay;
- if (day < mc.getExpiryThreshold()){
- throw new BusinessRunTimeException(ExceptionConstants.SERVICE_SYSTEM_ERROR_CODE,
- "商品" + m.getName() + "即将过期,不允许入库");
- }
- }
- }
- //修改原先采购订单实际入库数量
- //depotItemService.update(new UpdateWrapper<DepotItem>().set("actual_quantity_in_storage", materialMap.get(batchNumber).getMaterialNumber()).eq("id", depotItem.getId()));
- //设置单据主表id
- depotItem.setHeaderId(id);
- //设置关联子表id
- depotItem.setLinkId(depotItem.getId());
- depotItem.setId(null);
- //设置实际入库数量
- depotItem.setOperNumber(materialMap.get(barCode).getMaterialNumber());
- depotItem.setProductionDate(materialMap.get(barCode).getProductionDate());
- depotItem.setPosition(materialMap.get(barCode).getPosition());
- //以下进行单位换算
- // Unit unitInfo = materialService.findUnit(depotItem.getMaterialId()); //查询多单位信息
- if (StringUtil.isExist(depotItem.getOperNumber())) {
- //获取子表单商品单位
- // String unit =depotItem.getMaterialUnit();
- BigDecimal oNumber = depotItem.getOperNumber();
- depotItem.setBasicNumber(oNumber); //其他情况
- //设置基础数量
- // if (StringUtil.isNotEmpty(unitInfo.getName())) {
- // String basicUnit = unitInfo.getBasicUnit(); //基本单位
- // if (unit.equals(basicUnit)) { //如果等于基本单位
- // depotItem.setBasicNumber(oNumber); //数量一致
- // } else if (unit.equals(unitInfo.getOtherUnit())) { //如果等于副单位
- // depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatio())); //数量乘以比例
- // } else if (unit.equals(unitInfo.getOtherUnitTwo())) { //如果等于副单位2
- // depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioTwo())); //数量乘以比例
- // } else if (unit.equals(unitInfo.getOtherUnitThree())) { //如果等于副单位3
- // depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioThree())); //数量乘以比例
- // } else {
- // depotItem.setBasicNumber(oNumber); //数量一致
- // }
- // } else {
- // depotItem.setBasicNumber(oNumber); //其他情况
- // }
- }
- User user = userService.getCurrentUser();
- depotItem.setWarehousingUser(user.getId());
- //基本单位数量*单价
- depotItem.setAllPrice(depotItem.getBasicNumber().multiply(depotItem.getUnitPrice()));
- depotItem.setTaxMoney(depotItem.getBasicNumber().multiply(depotItem.getTaxRate()));
- //单价总额 + 税额
- depotItem.setTaxLastMoney(depotItem.getAllPrice().add(depotItem.getTaxMoney()));
- //添加单据子表
- depotItemService.insertDepotItemWithObj(depotItem);
- //更新当前库存
- depotItemService.updateCurrentStock(depotItem);
- //更新当前成本价
- depotItemService.updateCurrentUnitPrice(depotItem);
- //修改商品生产日期
- //materialExtendService.update(new UpdateWrapper<MaterialExtend>().set("production_date",materialMap.get(sku).getProductionDate()).eq("id", materialExtend.getId()));
- }
- //修改订单总额
- updateTotalPriceById(depotHead);
- //修改采购订单状态、操作人、操作时间,数额
- this.update(new UpdateWrapper<DepotHead>().set("status", "6").set("oper_id",userInfo.getId()).set("submit_time",new Date()).set("reject",null).set("reject_time",null).set("auditor",null).eq("id", pdaDepotHeadDTO.getId()));
- return true;
- }
- /**
- * 修改订单总额
- */
- private void updateTotalPriceById(DepotHead depotHead) throws Exception {
- BigDecimal sum = BigDecimal.ZERO;
- //获取单据子表
- List<DepotItem> list = depotItemService.getListByHeaderId(depotHead.getId());
- for (DepotItem depotItem : list) {
- if (depotItem.getTaxLastMoney() != null){
- sum = sum.add(depotItem.getTaxLastMoney());
- }else {
- sum = sum.add(depotItem.getAllPrice());
- }
- }
- BigDecimal totalPrice = sum;
- if (depotHead.getTotalPrice().compareTo(BigDecimal.ZERO) < 0){
- totalPrice = totalPrice.negate();
- }
- this.update(new UpdateWrapper<DepotHead>().set("total_price",totalPrice).set("change_amount",totalPrice).set("discount_last_money",sum).eq("id",depotHead.getId()));
- }
- /**
- * 订单申请审核
- * @param depotHead 订单数据
- */
- private void applyAudit(DepotHead depotHead){
- if (depotHead.getStatus().equals("9")){
- String type = depotHead.getSubType();
- if (depotHead.getType().equals("入库") || depotHead.getType().equals("出库")) {
- type += depotHead.getType();
- }
- auditService.generateAuditByItemId(depotHead.getNumber(),type);
- }
- }
- /**
- * 采购、销售订单发送消息
- */
- @Override
- public void sendMsg(Long dhId){
- DepotHead depotHead = this.getDepotHead(dhId);
- //获取所有用户
- List<User> userList = userService.list(new LambdaQueryWrapperX<User>().eq(User::getDeleteFlag,false));
- List<Msg> msgList = new ArrayList<>();
- userList.forEach(v ->{
- Msg msg = new Msg();
- msg.setMsgTitle(v.getId().toString());
- if (depotHead.getSubType().equals("采购订单")){
- msg.setMsgContent(String.format("您有一条新的入库任务(单号:%s)请点击查看并及时处理!",depotHead.getNumber()));
- }else {
- msg.setMsgContent(String.format("您有一条新的拣货任务(单号:%s)请点击查看并及时处理",depotHead.getNumber()));
- }
- msg.setCreateTime(new Date());
- msg.setType("复核任务");
- msg.setUserId(v.getId());
- msg.setStatus(1);
- msgList.add(msg);
- });
- msgService.saveBatch(msgList);
- }
- }
|