123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668 |
- package com.jsh.erp.service.impl;
- import com.alibaba.fastjson.JSONArray;
- import com.alibaba.fastjson.JSONObject;
- 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.*;
- import com.jsh.erp.datasource.pda.vo.PDADepotItemVO;
- import com.jsh.erp.datasource.vo.DepotHeadXsddRequestVO;
- import com.jsh.erp.datasource.vo.DepotItemStockWarningCount;
- import com.jsh.erp.datasource.vo.DepotItemVo4Stock;
- import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList;
- import com.jsh.erp.datasource.vo.DepotItemXsddRequestVO;
- import com.jsh.erp.datasource.vo.InOutPriceVo;
- import com.jsh.erp.exception.BusinessRunTimeException;
- import com.jsh.erp.exception.JshException;
- import com.jsh.erp.query.LambdaQueryWrapperX;
- import com.jsh.erp.service.*;
- import com.jsh.erp.utils.HttpClient;
- import com.jsh.erp.utils.StringUtil;
- import com.jsh.erp.utils.Tools;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.BeanUtils;
- import org.springframework.stereotype.Service;
- import org.springframework.transaction.annotation.Transactional;
- import javax.annotation.Resource;
- import javax.servlet.http.HttpServletRequest;
- import java.math.BigDecimal;
- import java.sql.Timestamp;
- import java.util.*;
- @Service
- public class DepotItemServiceImpl extends ServiceImpl<DepotItemMapper, DepotItem> implements DepotItemService {
- private Logger logger = LoggerFactory.getLogger(DepotItemServiceImpl.class);
- private final static String TYPE = "入库";
- private final static String SUM_TYPE = "number";
- private final static String IN = "in";
- private final static String OUT = "out";
- @Resource
- private DepotItemMapper depotItemMapper;
- @Resource
- private DepotItemMapperEx depotItemMapperEx;
- @Resource
- private MaterialService materialService;
- @Resource
- private MaterialExtendService materialExtendService;
- @Resource
- private SerialNumberMapperEx serialNumberMapperEx;
- @Resource
- private DepotHeadService depotHeadService;
- @Resource
- private DepotHeadMapper depotHeadMapper;
- @Resource
- private SerialNumberService serialNumberService;
- @Resource
- private UserService userService;
- @Resource
- private SystemConfigService systemConfigService;
- @Resource
- private DepotService depotService;
- @Resource
- private UnitService unitService;
- @Resource
- private MaterialCurrentStockMapper materialCurrentStockMapper;
- @Resource
- private MaterialCurrentStockMapperEx materialCurrentStockMapperEx;
- @Resource
- private LogService logService;
- @Resource
- private MaterialExtendMapper materialExtendMapper;
- /**
- * pda根据订单信息查询商品列表
- * @return
- */
- @Override
- public List<PDADepotItemVO> pdaList(Long id) {
- return depotItemMapper.pdaList(id);
- }
- /**
- * pda根据商品订单ID查询商品详情
- * @param id 商品订单ID
- * @return
- */
- @Override
- public PDADepotItemVO pdaDetail(Long id) {
- return depotItemMapper.pdaDetail(id);
- }
- /**
- * 商品库存详情列表
- * @param materialId 商品ID
- * @return
- */
- @Override
- public List<PDADepotItemVO> materialDepotDetail(String type , Long materialId) {
- return depotItemMapper.materialDepotDetail(type, materialId);
- }
- @Override
- public DepotItem getDepotItem(long id)throws Exception {
- DepotItem result=null;
- try{
- result=depotItemMapper.selectByPrimaryKey(id);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotItem> getDepotItem()throws Exception {
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotItem> list=null;
- try{
- list=depotItemMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<DepotItem> select(String name, Integer type, String remark, int offset, int rows)throws Exception {
- List<DepotItem> list=null;
- try{
- list=depotItemMapperEx.selectByConditionDepotItem(name, type, remark, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public Long countDepotItem(String name, Integer type, String remark) throws Exception{
- Long result =null;
- try{
- result=depotItemMapperEx.countsByDepotItem(name, type, remark);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int insertDepotItem(JSONObject obj, HttpServletRequest request)throws Exception {
- DepotItem depotItem = JSONObject.parseObject(obj.toJSONString(), DepotItem.class);
- int result =0;
- try{
- result=depotItemMapper.insertSelective(depotItem);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateDepotItem(JSONObject obj, HttpServletRequest request)throws Exception {
- DepotItem depotItem = JSONObject.parseObject(obj.toJSONString(), DepotItem.class);
- int result =0;
- try{
- result=depotItemMapper.updateByPrimaryKeySelective(depotItem);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int deleteDepotItem(Long id, HttpServletRequest request)throws Exception {
- int result =0;
- try{
- result=depotItemMapper.deleteByPrimaryKey(id);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int batchDeleteDepotItem(String ids, HttpServletRequest request)throws Exception {
- List<Long> idList = StringUtil.strToLongList(ids);
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andIdIn(idList);
- int result =0;
- try{
- result=depotItemMapper.deleteByExample(example);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- public int checkIsNameExist(Long id, String name)throws Exception {
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andIdNotEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotItem> list =null;
- try{
- list=depotItemMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list==null?0:list.size();
- }
- @Override
- public List<DepotItemVo4DetailByTypeAndMId> findDetailByDepotIdsAndMaterialIdList(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
- String number, String beginTime, String endTime, Long mId, Integer offset, Integer rows)throws Exception {
- Long depotId = null;
- if(StringUtil.isNotEmpty(depotIds)) {
- depotId = Long.parseLong(depotIds);
- }
- List<Long> depotList = depotService.parseDepotList(depotId);
- Long[] depotIdArray = StringUtil.listToLongArray(depotList);
- List<DepotItemVo4DetailByTypeAndMId> list =null;
- try{
- list = depotItemMapperEx.findDetailByDepotIdsAndMaterialIdList(depotIdArray, forceFlag, inOutManageFlag, sku, batchNumber, number, beginTime, endTime, mId, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public Long findDetailByDepotIdsAndMaterialIdCount(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
- String number, String beginTime, String endTime, Long mId)throws Exception {
- Long depotId = null;
- if(StringUtil.isNotEmpty(depotIds)) {
- depotId = Long.parseLong(depotIds);
- }
- List<Long> depotList = depotService.parseDepotList(depotId);
- Long[] depotIdArray = StringUtil.listToLongArray(depotList);
- Long result =null;
- try{
- result = depotItemMapperEx.findDetailByDepotIdsAndMaterialIdCount(depotIdArray, forceFlag, inOutManageFlag, sku, batchNumber, number, beginTime, endTime, mId);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- /**
- * 插入单据子表
- * @param depotItem 单据子表
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int insertDepotItemWithObj(DepotItem depotItem)throws Exception {
- User user = userService.getCurrentUser();
- depotItem.setTenantId(user.getId());
- int result =0;
- try{
- result = depotItemMapper.insertSelective(depotItem);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- public int insertDepotItem(DepotItem depotItem)throws Exception {
- int result =0;
- try{
- depotItemMapper.insertSelective(depotItem);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int updateDepotItemWithObj(DepotItem depotItem)throws Exception {
- int result =0;
- try{
- result = depotItemMapper.updateByPrimaryKeySelective(depotItem);
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotItem> getListByHeaderId(Long headerId)throws Exception {
- List<DepotItem> list =null;
- try{
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andHeaderIdEqualTo(headerId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- list = depotItemMapper.selectByExample(example);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- /**
- * 查询当前单据中指定商品的明细信息
- * @param headerId
- * @param meId
- * @return
- * @throws Exception
- */
- @Override
- public DepotItem getItemByHeaderIdAndMaterial(Long headerId, Long meId)throws Exception {
- DepotItem depotItem = new DepotItem();
- try{
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andHeaderIdEqualTo(headerId).andMaterialExtendIdEqualTo(meId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotItem> list = depotItemMapper.selectByExample(example);
- if(list!=null && list.size()>0) {
- depotItem = list.get(0);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return depotItem;
- }
- /**
- * 查询被关联订单中指定商品的明细信息
- * @param linkStr
- * @param meId
- * @return
- * @throws Exception
- */
- @Override
- public DepotItem getPreItemByHeaderIdAndMaterial(String linkStr, Long meId, Long linkId)throws Exception {
- DepotItem depotItem = new DepotItem();
- try{
- DepotHead depotHead = depotHeadService.getDepotHead(linkStr);
- if(null!=depotHead && null!=depotHead.getId()) {
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andHeaderIdEqualTo(depotHead.getId()).andMaterialExtendIdEqualTo(meId).andIdEqualTo(linkId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotItem> list = depotItemMapper.selectByExample(example);
- if(list!=null && list.size()>0) {
- depotItem = list.get(0);
- }
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return depotItem;
- }
- @Override
- public List<DepotItemVo4WithInfoEx> getDetailList(Long headerId)throws Exception {
- List<DepotItemVo4WithInfoEx> list =null;
- try{
- list = depotItemMapperEx.getDetailList(headerId);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public List<DepotItemVo4WithInfoEx> getInOutStock(String materialParam, List<Long> categoryIdList, String endTime, Integer offset, Integer rows)throws Exception {
- List<DepotItemVo4WithInfoEx> list =null;
- try{
- list = depotItemMapperEx.getInOutStock(materialParam, categoryIdList, endTime, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int getInOutStockCount(String materialParam, List<Long> categoryIdList, String endTime)throws Exception {
- int result=0;
- try{
- result = depotItemMapperEx.getInOutStockCount(materialParam, categoryIdList, endTime);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public List<DepotItemVo4WithInfoEx> getListWithBuyOrSale(String materialParam, String billType,
- String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag, Integer offset, Integer rows)throws Exception {
- List<DepotItemVo4WithInfoEx> list =null;
- try{
- list = depotItemMapperEx.getListWithBuyOrSale(materialParam, billType, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag, offset, rows);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- public int getListWithBuyOrSaleCount(String materialParam, String billType,
- String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag)throws Exception {
- int result=0;
- try{
- result = depotItemMapperEx.getListWithBuyOrSaleCount(materialParam, billType, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public BigDecimal buyOrSale(String type, String subType, Long MId, String beginTime, String endTime,
- String[] creatorArray, Long organId, String[] organArray, List<Long> depotList, Boolean forceFlag, String sumType) throws Exception{
- BigDecimal result= BigDecimal.ZERO;
- try{
- if (SUM_TYPE.equals(sumType)) {
- result= depotItemMapperEx.buyOrSaleNumber(type, subType, MId, beginTime, endTime, creatorArray, organId, organArray, depotList, forceFlag, sumType);
- } else {
- result= depotItemMapperEx.buyOrSalePrice(type, subType, MId, beginTime, endTime, creatorArray, organId, organArray, depotList, forceFlag, sumType);
- }
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- @Override
- public BigDecimal buyOrSalePriceTotal(String type, String subType, String materialParam, String beginTime, String endTime,
- String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag) throws Exception{
- BigDecimal result= BigDecimal.ZERO;
- try{
- result= depotItemMapperEx.buyOrSalePriceTotal(type, subType, materialParam, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- /**
- * 统计采购、销售、零售的总金额列表
- * @param beginTime
- * @param endTime
- * @return
- * @throws Exception
- */
- @Override
- public List<InOutPriceVo> inOrOutPriceList(String beginTime, String endTime) throws Exception{
- List<InOutPriceVo> result = new ArrayList<>();
- try{
- String [] creatorArray = depotHeadService.getCreatorArray();
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- result = depotItemMapperEx.inOrOutPriceList(beginTime, endTime, creatorArray, forceFlag);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- /**
- * 保存单据子表
- * @param rows 子表数据字符
- * @param headerId 单据主表id
- * @param actionType 操作类型
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void saveDetials(String rows, Long headerId, String actionType, HttpServletRequest request) throws Exception{
- //查询单据主表信息
- DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(headerId);
- //删除序列号和回收序列号
- deleteOrCancelSerialNumber(actionType, depotHead, headerId);
- //删除单据的明细
- deleteDepotItemHeadId(headerId);
- JSONArray rowArr = JSONArray.parseArray(rows);
- if (null != rowArr && rowArr.size()>0) {
- //针对组装单、拆卸单校验是否存在组合件和普通子件
- checkAssembleWithMaterialType(rowArr, depotHead.getSubType());
- for (int i = 0; i < rowArr.size(); i++) {
- DepotItem depotItem = new DepotItem();
- JSONObject rowObj = JSONObject.parseObject(rowArr.getString(i));
- depotItem.setHeaderId(headerId);
- String batchNumber = rowObj.getString("batchNumber");
- MaterialExtend materialExtend = materialExtendService.getInfoByBatchNumber(batchNumber);
- if(materialExtend == null) {
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_BARCODE_IS_NOT_EXIST_CODE,
- String.format(ExceptionConstants.MATERIAL_BARCODE_IS_NOT_EXIST_MSG, batchNumber));
- }
- depotItem.setMaterialId(materialExtend.getMaterialId());
- depotItem.setMaterialExtendId(materialExtend.getId());
- depotItem.setMaterialUnit(rowObj.getString("unit"));
- depotItem.setActualQuantityInStorage(rowObj.getBigDecimal("actualQuantityInStorage"));
- depotItem.setWarehousingVariance(rowObj.getBigDecimal("warehousingVariance"));
- depotItem.setReasonOfDifference(rowObj.getString("reasonOfDifference"));
- depotItem.setWarehousingUser(rowObj.getLong("warehousingUser"));
- depotItem.setWarehousingTime(rowObj.getString("warehousingTime"));
- Material material= materialService.getMaterial(depotItem.getMaterialId());
- if (BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber()) ||
- BusinessConstants.ENABLE_BATCH_NUMBER_ENABLED.equals(material.getEnableBatchNumber())) {
- //组装拆卸单不能选择批号或序列号商品
- if(BusinessConstants.SUB_TYPE_ASSEMBLE.equals(depotHead.getSubType()) ||
- BusinessConstants.SUB_TYPE_DISASSEMBLE.equals(depotHead.getSubType())) {
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_ASSEMBLE_SELECT_ERROR_CODE,
- String.format(ExceptionConstants.MATERIAL_ASSEMBLE_SELECT_ERROR_MSG, batchNumber));
- }
- //调拨单不能选择批号或序列号商品(该场景走出库和入库单)
- if(BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_TRANSFER_SELECT_ERROR_CODE,
- String.format(ExceptionConstants.MATERIAL_TRANSFER_SELECT_ERROR_MSG, batchNumber));
- }
- //盘点业务不能选择批号或序列号商品(该场景走出库和入库单)
- if(BusinessConstants.SUB_TYPE_CHECK_ENTER.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_REPLAY.equals(depotHead.getSubType())) {
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_STOCK_CHECK_ERROR_CODE,
- String.format(ExceptionConstants.MATERIAL_STOCK_CHECK_ERROR_MSG, batchNumber));
- }
- }
- if (StringUtil.isExist(rowObj.get("snList"))) {
- depotItem.setSnList(rowObj.getString("snList"));
- if(StringUtil.isExist(rowObj.get("depotId"))) {
- String [] snArray = depotItem.getSnList().split(",");
- int operNum = rowObj.getInteger("operNumber");
- if(snArray.length == operNum) {
- Long depotId = rowObj.getLong("depotId");
- BigDecimal inPrice = BigDecimal.ZERO;
- if (StringUtil.isExist(rowObj.get("unitPrice"))) {
- inPrice = rowObj.getBigDecimal("unitPrice");
- }
- serialNumberService.addSerialNumberByBill(depotHead.getType(), depotHead.getSubType(),
- depotHead.getNumber(), materialExtend.getMaterialId(), depotId, inPrice, depotItem.getSnList());
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_SN_NUMBERE_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_SN_NUMBERE_FAILED_MSG, batchNumber));
- }
- }
- } else {
- //入库或出库
- if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) ||
- BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())) {
- //序列号不能为空
- if (BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber())) {
- //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
- if(systemConfigService.getInOutManageFlag() &&
- (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
- //跳过
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_SERIAL_NUMBERE_EMPTY_CODE,
- String.format(ExceptionConstants.MATERIAL_SERIAL_NUMBERE_EMPTY_MSG, batchNumber));
- }
- }
- }
- }
- if (StringUtil.isExist(rowObj.get("batchNumber"))) {
- depotItem.setBatchNumber(rowObj.getString("batchNumber"));
- } else {
- //入库或出库
- if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) ||
- BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())) {
- //批号不能为空
- if (BusinessConstants.ENABLE_BATCH_NUMBER_ENABLED.equals(material.getEnableBatchNumber())) {
- //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
- if(systemConfigService.getInOutManageFlag() &&
- (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
- //跳过
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BATCH_NUMBERE_EMPTY_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_BATCH_NUMBERE_EMPTY_MSG, batchNumber));
- }
- }
- }
- }
- if (StringUtil.isExist(rowObj.get("expirationDate"))) {
- depotItem.setExpirationDate(rowObj.getDate("expirationDate"));
- }
- if (StringUtil.isExist(rowObj.get("sku"))) {
- depotItem.setSku(rowObj.getString("sku"));
- }
- if (StringUtil.isExist(rowObj.get("linkId"))) {
- depotItem.setLinkId(rowObj.getLong("linkId"));
- }
- //以下进行单位换算
- Unit unitInfo = materialService.findUnit(materialExtend.getMaterialId()); //查询多单位信息
- if (StringUtil.isExist(rowObj.get("operNumber"))) {
- //获取子表单商品数量
- depotItem.setOperNumber(rowObj.getBigDecimal("operNumber"));
- //获取子表单商品单位
- String unit = rowObj.get("unit") == null ? "" :rowObj.get("unit").toString();
- //获取实际出入库数量
- BigDecimal oNumber = rowObj.getBigDecimal("actualQuantityInStorage");
- 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); //其他情况
- }
- }
- //如果数量+已完成数量>原订单数量,给出预警(判断前提是存在关联订单|关联请购单)
- String linkStr = StringUtil.isNotEmpty(depotHead.getLinkNumber())? depotHead.getLinkNumber(): depotHead.getLinkApply();
- if (StringUtil.isNotEmpty(linkStr) && StringUtil.isExist(rowObj.get("preNumber")) && StringUtil.isExist(rowObj.get("finishNumber"))) {
- if("add".equals(actionType)) {
- //在新增模式进行状态赋值
- BigDecimal preNumber = rowObj.getBigDecimal("preNumber");
- BigDecimal finishNumber = rowObj.getBigDecimal("finishNumber");
- if(depotItem.getOperNumber().add(finishNumber).compareTo(preNumber)>0) {
- if(!systemConfigService.getOverLinkBillFlag()) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_MSG, batchNumber));
- }
- }
- } else if("update".equals(actionType)) {
- //当前单据的类型
- String currentSubType = depotHead.getSubType();
- //在更新模式进行状态赋值
- String unit = rowObj.get("unit").toString();
- Long preHeaderId = depotHeadService.getDepotHead(linkStr).getId();
- if(null!=preHeaderId) {
- //前一个单据的数量
- BigDecimal preNumber = getPreItemByHeaderIdAndMaterial(linkStr, depotItem.getMaterialExtendId(), depotItem.getLinkId()).getOperNumber();
- //除去此单据之外的已入库|已出库
- BigDecimal realFinishNumber = getRealFinishNumber(currentSubType, depotItem.getMaterialExtendId(), depotItem.getLinkId(), preHeaderId, headerId, unitInfo, unit);
- if(preNumber!=null) {
- if (depotItem.getOperNumber().add(realFinishNumber).compareTo(preNumber) > 0) {
- if (!systemConfigService.getOverLinkBillFlag()) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_MSG, batchNumber));
- }
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_PRE_BILL_IS_CHANGE_CODE,
- ExceptionConstants.DEPOT_ITEM_PRE_BILL_IS_CHANGE_MSG);
- }
- }
- }
- }
- if (StringUtil.isExist(rowObj.get("unitPrice"))) {
- BigDecimal unitPrice = rowObj.getBigDecimal("unitPrice");
- depotItem.setUnitPrice(unitPrice);
- if(materialExtend.getLowDecimal()!=null) {
- //零售或销售单价低于最低售价,进行提示
- if("零售".equals(depotHead.getSubType()) || "销售".equals(depotHead.getSubType())) {
- if (unitPrice.compareTo(materialExtend.getLowDecimal()) < 0) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_UNIT_PRICE_LOW_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_UNIT_PRICE_LOW_MSG, batchNumber));
- }
- }
- }
- }
- //如果是销售出库、销售退货、零售出库、零售退货则给采购单价字段赋值(如果是批次商品,则要根据批号去找之前的入库价)
- if(BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType()) ||
- BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()) ||
- BusinessConstants.SUB_TYPE_RETAIL.equals(depotHead.getSubType()) ||
- BusinessConstants.SUB_TYPE_RETAIL_RETURN.equals(depotHead.getSubType())) {
- boolean moveAvgPriceFlag = systemConfigService.getMoveAvgPriceFlag();
- BigDecimal currentUnitPrice = materialCurrentStockMapperEx.getCurrentUnitPriceByMId(materialExtend.getMaterialId());
- currentUnitPrice = unitService.parseUnitPriceByUnit(currentUnitPrice, unitInfo, depotItem.getMaterialUnit());
- BigDecimal unitPrice = moveAvgPriceFlag? currentUnitPrice: materialExtend.getPurchaseDecimal();
- depotItem.setPurchaseUnitPrice(unitPrice);
- if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
- depotItem.setPurchaseUnitPrice(getDepotItemByBatchNumber(depotItem.getMaterialExtendId(),depotItem.getBatchNumber()).getUnitPrice());
- }
- }
- //含税单价
- if (StringUtil.isExist(rowObj.get("taxUnitPrice"))) {
- depotItem.setTaxUnitPrice(rowObj.getBigDecimal("taxUnitPrice"));
- }
- //总金额,不含税
- if (StringUtil.isExist(rowObj.get("allPrice"))) {
- if (depotItem.getActualQuantityInStorage() == null || depotItem.getActualQuantityInStorage().equals(BigDecimal.ZERO)){
- depotItem.setAllPrice(rowObj.getBigDecimal("allPrice"));
- }else {
- //基本单位数量*单价
- depotItem.setAllPrice(depotItem.getBasicNumber().multiply(depotItem.getUnitPrice()));
- }
- }
- if (StringUtil.isExist(rowObj.get("depotId"))) {
- depotItem.setDepotId(rowObj.getLong("depotId"));
- } else {
- if(!BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())
- && !BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())
- && !BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_DEPOT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_DEPOT_FAILED_MSG));
- }
- }
- if(BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
- if (StringUtil.isExist(rowObj.get("anotherDepotId"))) {
- if(rowObj.getLong("anotherDepotId").equals(rowObj.getLong("depotId"))) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_EQUAL_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_EQUAL_FAILED_MSG));
- } else {
- depotItem.setAnotherDepotId(rowObj.getLong("anotherDepotId"));
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_FAILED_MSG));
- }
- }
- //税率
- if (StringUtil.isExist(rowObj.get("taxRate"))) {
- depotItem.setTaxRate(rowObj.getBigDecimal("taxRate"));
- }
- //税额
- if (StringUtil.isExist(rowObj.get("taxMoney"))) {
- if (depotItem.getActualQuantityInStorage() == null || depotItem.getActualQuantityInStorage().equals(BigDecimal.ZERO)){
- depotItem.setTaxMoney(rowObj.getBigDecimal("taxMoney"));
- }else {
- //单价总金额*税率
- depotItem.setTaxMoney(depotItem.getAllPrice().multiply(depotItem.getTaxRate()));
- }
- }
- //价税合计
- if (StringUtil.isExist(rowObj.get("taxLastMoney"))) {
- //单价总额 + 税额
- depotItem.setTaxLastMoney(depotItem.getAllPrice().add(depotItem.getTaxMoney()));
- //depotItem.setTaxLastMoney(rowObj.getBigDecimal("taxLastMoney"));
- }
- if (StringUtil.isExist(rowObj.get("mType"))) {
- depotItem.setMaterialType(rowObj.getString("mType"));
- }
- if (StringUtil.isExist(rowObj.get("remark"))) {
- depotItem.setRemark(rowObj.getString("remark"));
- }
- //出库时判断库存是否充足
- if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())){
- String stockMsg = material.getName() + "-" + batchNumber;
- BigDecimal stock = getCurrentStockByParam(depotItem.getDepotId(),depotItem.getMaterialId());
- if(StringUtil.isNotEmpty(depotItem.getSku())) {
- //对于sku商品要换个方式计算库存
- stock = getSkuStockByParam(depotItem.getDepotId(),depotItem.getMaterialExtendId(),null,null);
- }
- if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
- //对于批次商品要换个方式计算库存
- stock = getOneBatchNumberStock(depotItem.getDepotId(), batchNumber, depotItem.getBatchNumber());
- stockMsg += "-批号" + depotItem.getBatchNumber();
- }
- BigDecimal thisRealNumber = depotItem.getBasicNumber()==null?BigDecimal.ZERO:depotItem.getBasicNumber();
- if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
- //对于批次商品,直接使用当前填写的数量
- thisRealNumber = depotItem.getOperNumber()==null?BigDecimal.ZERO:depotItem.getOperNumber();
- }
- if(!systemConfigService.getMinusStockFlag() && stock.compareTo(thisRealNumber)<0){
- throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_STOCK_NOT_ENOUGH_CODE,
- String.format(ExceptionConstants.MATERIAL_STOCK_NOT_ENOUGH_MSG, stockMsg));
- }
- //出库时处理序列号
- if(!BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
- //判断商品是否开启序列号,开启的售出序列号,未开启的跳过
- if(BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber())) {
- //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
- if(systemConfigService.getInOutManageFlag() &&
- (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
- ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
- //跳过
- } else {
- //售出序列号,获得当前操作人
- User userInfo = userService.getCurrentUser();
- serialNumberService.checkAndUpdateSerialNumber(depotItem, depotHead.getNumber(), userInfo, StringUtil.toNull(depotItem.getSnList()));
- }
- }
- }
- }
- if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType())){
- //表单入库,修改商品库存
- materialExtend.setInventory(materialExtend.getInventory().add(depotItem.getBasicNumber()));
- materialExtendService.updateInventory("单据",depotItem.getId(),materialExtend);
- }else if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())){
- //表单入库,修改商品库存
- materialExtend.setInventory(materialExtend.getInventory().subtract(depotItem.getBasicNumber()));
- materialExtendService.updateInventory("单据",depotItem.getId(),materialExtend);
- }
- this.insertDepotItemWithObj(depotItem);
- //更新当前库存
- updateCurrentStock(depotItem);
- //更新当前成本价
- updateCurrentUnitPrice(depotItem);
- //更新商品的价格
- updateMaterialExtendPrice(materialExtend.getId(), depotHead.getSubType(), depotHead.getBillType(), rowObj);
- }
- //如果关联单据号非空则更新订单的状态,单据类型:采购入库单、销售出库单、盘点复盘单、其它入库单、其它出库单
- if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_REPLAY.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_OTHER.equals(depotHead.getSubType())) {
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
- //单据状态:是否全部完成 2-全部完成 3-部分完成(针对订单的分批出入库)
- String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkNumber(), "normal");
- changeBillStatus(depotHead.getLinkNumber(), billStatus);
- }
- }
- //当前单据类型为采购订单的逻辑
- if(BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
- //如果关联单据号非空则更新订单的状态,此处针对销售订单转采购订单的场景
- if(StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
- String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkNumber(), "normal");
- changeBillPurchaseStatus(depotHead.getLinkNumber(), billStatus);
- }
- //如果关联单据号非空则更新订单的状态,此处针对请购单转采购订单的场景
- if(StringUtil.isNotEmpty(depotHead.getLinkApply())) {
- String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkApply(), "apply");
- changeBillStatus(depotHead.getLinkApply(), billStatus);
- }
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_MSG));
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void saveOrderItem(DepotHeadXsddRequestVO order, List<DepotItemXsddRequestVO> itemList) throws Exception {
- // 查询单据主表信息
- DepotHead depotHead = depotHeadMapper.selectByPrimaryKey(order.getId());
- if (depotHead == null) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NOT_EXIST_CODE, String.format(ExceptionConstants.DEPOT_HEAD_NOT_EXIST_MSG));
- }
- if (null != itemList && itemList.size() > 0) {
- for (DepotItemXsddRequestVO item : itemList) {
- DepotItem depotItem = new DepotItem();
- BeanUtils.copyProperties(item, depotItem);
- // 以下进行单位换算
- Unit unitInfo = materialService.findUnit(depotItem.getMaterialId()); // 查询多单位信息
- if (depotItem.getOperNumber() != null) {
- // 获取子表单商品单位
- String unit = depotItem.getMaterialUnit() == null ? "" : depotItem.getMaterialUnit();
- BigDecimal oNumber = depotItem.getOperNumber();
- 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); // 其他情况
- }
- }
- depotItem.setHeaderId(depotHead.getId());
- this.insertDepotItem(depotItem);
- // 更新当前库存
- updateCurrentStock(depotItem);
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_CODE, String.format(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_MSG));
- }
- }
- /**
- * 判断单据的状态
- * 通过数组对比:原单据的商品和商品数量(汇总) 与 分批操作后单据的商品和商品数量(汇总)
- * @param depotHead
- * @param linkStr
- * @return
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public String getBillStatusByParam(DepotHead depotHead, String linkStr, String linkType) {
- String res = BusinessConstants.BILLS_STATUS_SKIPED;
- //获取原单据的商品和商品数量(汇总)
- List<DepotItemVo4MaterialAndSum> linkList = depotItemMapperEx.getLinkBillDetailMaterialSum(linkStr);
- //获取分批操作后单据的商品和商品数量(汇总)
- List<DepotItemVo4MaterialAndSum> batchList = depotItemMapperEx.getBatchBillDetailMaterialSum(linkStr, linkType, depotHead.getType());
- //将分批操作后的单据的商品和商品数据构造成Map
- Map<Long, BigDecimal> materialSumMap = new HashMap<>();
- for(DepotItemVo4MaterialAndSum materialAndSum : batchList) {
- materialSumMap.put(materialAndSum.getMaterialExtendId(), materialAndSum.getOperNumber());
- }
- for(DepotItemVo4MaterialAndSum materialAndSum : linkList) {
- //过滤掉原单里面有数量为0的商品
- if(materialAndSum.getOperNumber().compareTo(BigDecimal.ZERO) != 0) {
- BigDecimal materialSum = materialSumMap.get(materialAndSum.getMaterialExtendId());
- if (materialSum != null) {
- if (materialSum.compareTo(materialAndSum.getOperNumber()) < 0) {
- res = BusinessConstants.BILLS_STATUS_SKIPING;
- }
- } else {
- res = BusinessConstants.BILLS_STATUS_SKIPING;
- }
- }
- }
- return res;
- }
- /**
- * 更新单据状态
- * @param linkStr
- * @param billStatus
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void changeBillStatus(String linkStr, String billStatus) {
- DepotHead depotHeadOrders = new DepotHead();
- depotHeadOrders.setStatus(billStatus);
- DepotHeadExample example = new DepotHeadExample();
- List<String> linkNoList = StringUtil.strToStringList(linkStr);
- example.createCriteria().andNumberIn(linkNoList);
- try{
- depotHeadMapper.updateByExampleSelective(depotHeadOrders, example);
- }catch(Exception e){
- logger.error("异常码[{}],异常提示[{}],异常[{}]",
- ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
- throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
- ExceptionConstants.DATA_WRITE_FAIL_MSG);
- }
- }
- /**
- * 更新单据状态,此处针对销售订单转采购订单的场景
- * @param linkStr
- * @param billStatus
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void changeBillPurchaseStatus(String linkStr, String billStatus) {
- DepotHead depotHeadOrders = new DepotHead();
- depotHeadOrders.setPurchaseStatus(billStatus);
- DepotHeadExample example = new DepotHeadExample();
- List<String> linkNoList = StringUtil.strToStringList(linkStr);
- example.createCriteria().andNumberIn(linkNoList);
- try{
- depotHeadMapper.updateByExampleSelective(depotHeadOrders, example);
- }catch(Exception e){
- logger.error("异常码[{}],异常提示[{}],异常[{}]",
- ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
- throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
- ExceptionConstants.DATA_WRITE_FAIL_MSG);
- }
- }
- /**
- * 根据批号查询单据明细信息
- * @param materialExtendId
- * @param batchNumber
- * @return
- */
- @Override
- public DepotItem getDepotItemByBatchNumber(Long materialExtendId, String batchNumber) {
- List<DepotItem> depotItemList = depotItemMapperEx.getDepotItemByBatchNumber(materialExtendId, batchNumber);
- if(null != depotItemList && depotItemList.size() > 0){
- return depotItemList.get(0);
- } else {
- return new DepotItem();
- }
- }
- /**
- * 根据主表id删除单明细
- * @param headerId
- * @throws Exception
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void deleteDepotItemHeadId(Long headerId)throws Exception {
- try{
- //1、查询删除前的单据明细
- List<DepotItem> depotItemList = getListByHeaderId(headerId);
- //2、删除单据明细
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andHeaderIdEqualTo(headerId);
- depotItemMapper.deleteByExample(example);
- //3、计算删除之后单据明细中商品的库存
- for(DepotItem depotItem : depotItemList){
- updateCurrentStock(depotItem);
- }
- }catch(Exception e){
- JshException.writeFail(logger, e);
- }
- }
- /**
- * 删除序列号和回收序列号
- * @param actionType
- * @throws Exception
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void deleteOrCancelSerialNumber(String actionType, DepotHead depotHead, Long headerId) throws Exception {
- if(actionType.equals("update")) {
- User userInfo = userService.getCurrentUser();
- if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType())){
- //入库逻辑
- String number = depotHead.getNumber();
- SerialNumberExample example = new SerialNumberExample();
- example.createCriteria().andInBillNoEqualTo(number);
- serialNumberService.deleteByExample(example);
- } else if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())){
- //出库逻辑
- DepotItemExample example = new DepotItemExample();
- example.createCriteria().andHeaderIdEqualTo(headerId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- List<DepotItem> depotItemList = depotItemMapper.selectByExample(example);
- if(null != depotItemList && depotItemList.size() > 0){
- for (DepotItem depotItem : depotItemList){
- if(StringUtil.isNotEmpty(depotItem.getSnList())){
- serialNumberService.cancelSerialNumber(depotItem.getMaterialId(), depotHead.getNumber(), (depotItem.getBasicNumber() == null ? 0 : depotItem.getBasicNumber()).intValue(), userInfo);
- }
- }
- }
- }
- }
- }
- /**
- * 针对组装单、拆卸单校验是否存在组合件和普通子件
- * @param rowArr
- * @param subType
- */
- @Override
- public void checkAssembleWithMaterialType(JSONArray rowArr, String subType) {
- if(BusinessConstants.SUB_TYPE_ASSEMBLE.equals(subType) ||
- BusinessConstants.SUB_TYPE_DISASSEMBLE.equals(subType)) {
- if(rowArr.size() > 1) {
- JSONObject firstRowObj = JSONObject.parseObject(rowArr.getString(0));
- JSONObject secondRowObj = JSONObject.parseObject(rowArr.getString(1));
- String firstMaterialType = firstRowObj.getString("mType");
- String secondMaterialType = secondRowObj.getString("mType");
- if(!"组合件".equals(firstMaterialType) || !"普通子件".equals(secondMaterialType)) {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_MSG));
- }
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_CODE,
- String.format(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_MSG));
- }
- }
- }
- /**
- * 更新商品的价格
- * @param meId
- * @param subType
- * @param rowObj
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateMaterialExtendPrice(Long meId, String subType, String billType, JSONObject rowObj) throws Exception {
- if(systemConfigService.getUpdateUnitPriceFlag()) {
- if (StringUtil.isExist(rowObj.get("unitPrice"))) {
- BigDecimal unitPrice = rowObj.getBigDecimal("unitPrice");
- MaterialExtend materialExtend = new MaterialExtend();
- materialExtend.setId(meId);
- if(BusinessConstants.SUB_TYPE_PURCHASE.equals(subType)) {
- materialExtend.setPurchaseDecimal(unitPrice);
- }
- if(BusinessConstants.SUB_TYPE_SALES.equals(subType)) {
- materialExtend.setWholesaleDecimal(unitPrice);
- }
- if(BusinessConstants.SUB_TYPE_RETAIL.equals(subType)) {
- materialExtend.setCommodityDecimal(unitPrice);
- }
- //其它入库-生产入库的情况更新采购单价
- if(BusinessConstants.SUB_TYPE_OTHER.equals(subType)) {
- if(BusinessConstants.BILL_TYPE_PRODUCE_IN.equals(billType)) {
- materialExtend.setPurchaseDecimal(unitPrice);
- }
- }
- materialExtendService.updateMaterialExtend(materialExtend);
- }
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public List<DepotItemStockWarningCount> findStockWarningCount(Integer offset, Integer rows, String materialParam, List<Long> depotList, List<Long> categoryList) {
- List<DepotItemStockWarningCount> list = null;
- try{
- list =depotItemMapperEx.findStockWarningCount(offset, rows, materialParam, depotList, categoryList);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return list;
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public int findStockWarningCountTotal(String materialParam, List<Long> depotList, List<Long> categoryList) {
- int result = 0;
- try{
- result =depotItemMapperEx.findStockWarningCountTotal(materialParam, depotList, categoryList);
- }catch(Exception e){
- JshException.readFail(logger, e);
- }
- return result;
- }
- /**
- * 库存统计-sku
- * @param depotId
- * @param meId
- * @param beginTime
- * @param endTime
- * @return
- */
- @Override
- public BigDecimal getSkuStockByParam(Long depotId, Long meId, String beginTime, String endTime) throws Exception {
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
- List<Long> depotList = depotService.parseDepotList(depotId);
- //盘点复盘后数量的变动
- BigDecimal stockCheckSum = depotItemMapperEx.getSkuStockCheckSumByDepotList(depotList, meId, forceFlag, beginTime, endTime);
- DepotItemVo4Stock stockObj = depotItemMapperEx.getSkuStockByParamWithDepotList(depotList, meId, forceFlag, inOutManageFlag, beginTime, endTime);
- BigDecimal stockSum = BigDecimal.ZERO;
- if(stockObj!=null) {
- BigDecimal inTotal = stockObj.getInTotal();
- BigDecimal transfInTotal = stockObj.getTransfInTotal();
- BigDecimal assemInTotal = stockObj.getAssemInTotal();
- BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
- BigDecimal outTotal = stockObj.getOutTotal();
- BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
- BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
- BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
- stockSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal)
- .subtract(outTotal).subtract(transfOutTotal).subtract(assemOutTotal).subtract(disAssemOutTotal);
- }
- return stockCheckSum.add(stockSum);
- }
- /**
- * 库存统计-单仓库
- * @param depotId
- * @param mId
- * @param beginTime
- * @param endTime
- * @return
- */
- @Override
- public BigDecimal getStockByParam(Long depotId, Long mId, String beginTime, String endTime) throws Exception {
- List<Long> depotList = depotService.parseDepotList(depotId);
- return getStockByParamWithDepotList(depotList, mId, beginTime, endTime);
- }
- /**
- * 库存统计-多仓库
- * @param depotList
- * @param mId
- * @param beginTime
- * @param endTime
- * @return
- */
- @Override
- public BigDecimal getStockByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception {
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
- //初始库存
- BigDecimal initStock = materialService.getInitStockByMidAndDepotList(depotList, mId);
- //盘点复盘后数量的变动
- BigDecimal stockCheckSum = depotItemMapperEx.getStockCheckSumByDepotList(depotList, mId, forceFlag, beginTime, endTime);
- //表单的数量
- DepotItemVo4Stock stockObj = depotItemMapperEx.getStockByParamWithDepotList(depotList, mId, forceFlag, inOutManageFlag, beginTime, endTime);
- BigDecimal stockSum = BigDecimal.ZERO;
- //获取商品子表单的库存总数
- BigDecimal inventory = materialExtendMapper.getInventorySumByDepotAndMid(depotList,mId);
- if(stockObj!=null) {
- BigDecimal inTotal = stockObj.getInTotal();
- BigDecimal transfInTotal = stockObj.getTransfInTotal();
- BigDecimal assemInTotal = stockObj.getAssemInTotal();
- BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
- BigDecimal outTotal = stockObj.getOutTotal();
- BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
- BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
- BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
- stockSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal)
- .subtract(outTotal).subtract(transfOutTotal).subtract(assemOutTotal).subtract(disAssemOutTotal);
- }
- return initStock.add(stockCheckSum).add(inventory);
- }
- /**
- * 统计时间段内的入库和出库数量-多仓库
- * @param depotList
- * @param mId
- * @param beginTime
- * @param endTime
- * @return
- */
- @Override
- public Map<String, BigDecimal> getIntervalMapByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception {
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
- Map<String,BigDecimal> intervalMap = new HashMap<>();
- BigDecimal inSum = BigDecimal.ZERO;
- BigDecimal outSum = BigDecimal.ZERO;
- //盘点复盘后数量的变动
- BigDecimal stockCheckSum = depotItemMapperEx.getStockCheckSumByDepotList(depotList, mId, forceFlag, beginTime, endTime);
- DepotItemVo4Stock stockObj = depotItemMapperEx.getStockByParamWithDepotList(depotList, mId, forceFlag, inOutManageFlag, beginTime, endTime);
- if(stockObj!=null) {
- BigDecimal inTotal = stockObj.getInTotal();
- BigDecimal transfInTotal = stockObj.getTransfInTotal();
- BigDecimal assemInTotal = stockObj.getAssemInTotal();
- BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
- inSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal);
- BigDecimal outTotal = stockObj.getOutTotal();
- BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
- BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
- BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
- outSum = outTotal.add(transfOutTotal).add(assemOutTotal).add(disAssemOutTotal);
- }
- if(stockCheckSum.compareTo(BigDecimal.ZERO)>0) {
- inSum = inSum.add(stockCheckSum);
- } else {
- //盘点复盘数量为负数代表出库
- outSum = outSum.subtract(stockCheckSum);
- }
- intervalMap.put("inSum", inSum);
- intervalMap.put("outSum", outSum);
- return intervalMap;
- }
- /**
- * 根据单据明细来批量更新当前库存
- * @param depotItem 单据子表明细
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateCurrentStock(DepotItem depotItem) throws Exception {
- updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getDepotId());
- if(depotItem.getAnotherDepotId()!=null){
- updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getAnotherDepotId());
- }
- }
- /**
- * 根据单据明细来批量更新当前成本价
- * @param depotItem
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public void updateCurrentUnitPrice(DepotItem depotItem) throws Exception {
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
- //查询多单位信息
- Unit unitInfo = materialService.findUnit(depotItem.getMaterialId());
- List<DepotItemVo4DetailByTypeAndMId> itemList = findDetailByDepotIdsAndMaterialIdList(null, forceFlag, inOutManageFlag, depotItem.getSku(),
- depotItem.getBatchNumber(), null, null, null, depotItem.getMaterialId(), null, null);
- Collections.reverse(itemList); //倒序之后变成按时间从前往后排序
- BigDecimal currentNumber = BigDecimal.ZERO;
- BigDecimal currentUnitPrice = BigDecimal.ZERO;
- BigDecimal currentAllPrice = BigDecimal.ZERO;
- for(DepotItemVo4DetailByTypeAndMId item: itemList) {
- BigDecimal basicNumber = item.getBnum()!=null?item.getBnum():BigDecimal.ZERO;
- //数量*单价 另外计算新的成本价
- BigDecimal allPrice = unitService.parseAllPriceByUnit(item.getAllPrice()!=null?item.getAllPrice():BigDecimal.ZERO, unitInfo, item.getMaterialUnit());
- if(basicNumber.compareTo(BigDecimal.ZERO)!=0 && allPrice.compareTo(BigDecimal.ZERO)!=0) {
- //入库
- if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(item.getType())) {
- //零售退货、销售退货
- if (BusinessConstants.SUB_TYPE_RETAIL_RETURN.equals(item.getSubType()) || BusinessConstants.SUB_TYPE_SALES_RETURN.equals(item.getSubType())) {
- //数量*当前的成本单价
- currentNumber = currentNumber.add(basicNumber);
- currentAllPrice = currentAllPrice.add(basicNumber.multiply(currentUnitPrice));
- } else {
- currentAllPrice = currentAllPrice.add(allPrice);
- currentNumber = currentNumber.add(basicNumber);
- //只有当前库存总金额和当前库存数量都大于0才计算移动平均价
- if (currentAllPrice.compareTo(BigDecimal.ZERO) > 0 && currentNumber.compareTo(BigDecimal.ZERO) > 0) {
- currentUnitPrice = currentAllPrice.divide(currentNumber, 2, BigDecimal.ROUND_HALF_UP);
- } else {
- currentUnitPrice = item.getUnitPrice();
- }
- }
- }
- //出库
- if (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(item.getType())) {
- //采购退货
- if (BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(item.getSubType())) {
- currentAllPrice = currentAllPrice.add(allPrice);
- currentNumber = currentNumber.add(basicNumber);
- //只有当前库存总金额和当前库存数量都大于0才计算移动平均价
- if (currentAllPrice.compareTo(BigDecimal.ZERO) > 0 && currentNumber.compareTo(BigDecimal.ZERO) > 0) {
- currentUnitPrice = currentAllPrice.divide(currentNumber, 2, BigDecimal.ROUND_HALF_UP);
- } else {
- currentUnitPrice = item.getUnitPrice();
- }
- } else {
- currentNumber = currentNumber.add(basicNumber);
- //数量*当前的成本单价
- currentAllPrice = currentAllPrice.add(basicNumber.multiply(currentUnitPrice));
- }
- }
- //防止单价金额溢出
- if(currentUnitPrice.compareTo(BigDecimal.valueOf(100000000))>0 || currentUnitPrice.compareTo(BigDecimal.valueOf(-100000000))<0) {
- currentUnitPrice = BigDecimal.ZERO;
- }
- }
- }
- //更新实时库存中的当前单价
- materialCurrentStockMapperEx.updateUnitPriceByMId(currentUnitPrice, depotItem.getMaterialId());
- }
- /**
- * 根据商品和仓库来更新当前库存
- * @param mId
- * @param dId
- */
- @Override
- public void updateCurrentStockFun(Long mId, Long dId) throws Exception {
- if(mId!=null && dId!=null) {
- // MaterialCurrentStockExample example = new MaterialCurrentStockExample();
- //条件 添加商品id,仓库id ,删除状态
- // example.createCriteria().andMaterialIdEqualTo(mId).andDepotIdEqualTo(dId)
- // .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
- //获取商品当前库存信息
- // List<MaterialCurrentStock> list = materialCurrentStockMapper.selectByExample(example);
- //-先清除再插入,防止商品仓库变更,数据对不上
- materialCurrentStockMapper.delete(new LambdaQueryWrapperX<MaterialCurrentStock>().eq(MaterialCurrentStock::getMaterialId,mId).eqIfPresent(MaterialCurrentStock::getDepotId,dId));
- MaterialCurrentStock materialCurrentStock = new MaterialCurrentStock();
- materialCurrentStock.setMaterialId(mId);
- materialCurrentStock.setDepotId(dId);
- //设置当前库存数量
- materialCurrentStock.setCurrentNumber(getStockByParam(dId,mId,null,null));
- // if(list!=null && list.size()>0) {
- // //当前库存信息存在,修改当前库存信息
- // Long mcsId = list.get(0).getId();
- // materialCurrentStock.setId(mcsId);
- // materialCurrentStockMapper.updateByPrimaryKeySelective(materialCurrentStock);
- // } else {
- //当前库存信息不存在,新增当前库存信息
- materialCurrentStockMapper.insertSelective(materialCurrentStock);
- // }
- }
- }
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public BigDecimal getFinishNumber(Long meId, Long id, Long headerId, Unit unitInfo, String materialUnit, String linkType) {
- Long linkId = id;
- String goToType = "";
- DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(headerId);
- String linkStr = depotHead.getNumber(); //订单号
- if("purchase".equals(linkType)) {
- //针对以销定购的情况
- if(BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_PURCHASE_ORDER;
- }
- } else if("other".equals(linkType)) {
- //采购入库、采购退货、销售出库、销售退货都转其它入库
- if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
- || BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_OTHER;
- }
- } else if("basic".equals(linkType)) {
- //采购订单转采购入库
- if(BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_PURCHASE;
- }
- //销售订单转销售出库
- if(BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_SALES;
- }
- //采购入库转采购退货
- if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_PURCHASE_RETURN;
- }
- //销售出库转销售退货
- if(BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())) {
- goToType = BusinessConstants.SUB_TYPE_SALES_RETURN;
- }
- }
- String noType = "normal";
- if(BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())) {
- noType = "apply";
- }
- BigDecimal count = depotItemMapperEx.getFinishNumber(meId, linkId, linkStr, noType, goToType);
- //根据多单位情况进行数量的转换
- if(materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio()!=null && unitInfo.getRatio().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatio(),2,BigDecimal.ROUND_HALF_UP);
- }
- if(materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo()!=null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatioTwo(),2,BigDecimal.ROUND_HALF_UP);
- }
- if(materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree()!=null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatioThree(),2,BigDecimal.ROUND_HALF_UP);
- }
- return count;
- }
- /**
- * 除去此单据之外的已入库|已出库|已转采购
- * @param currentSubType
- * @param meId
- * @param linkId
- * @param preHeaderId
- * @param currentHeaderId
- * @param unitInfo
- * @param materialUnit
- * @return
- */
- @Override
- @Transactional(value = "transactionManager", rollbackFor = Exception.class)
- public BigDecimal getRealFinishNumber(String currentSubType, Long meId, Long linkId, Long preHeaderId, Long currentHeaderId, Unit unitInfo, String materialUnit) {
- String goToType = currentSubType;
- DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(preHeaderId);
- String linkStr = depotHead.getNumber(); //订单号
- String linkType = "normal";
- if(BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())) {
- linkType = "apply";
- }
- BigDecimal count = depotItemMapperEx.getRealFinishNumber(meId, linkId, linkStr, linkType, currentHeaderId, goToType);
- //根据多单位情况进行数量的转换
- if(materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio()!=null && unitInfo.getRatio().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatio(),2,BigDecimal.ROUND_HALF_UP);
- }
- if(materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo()!=null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatioTwo(),2,BigDecimal.ROUND_HALF_UP);
- }
- if(materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree()!=null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO)!=0) {
- count = count.divide(unitInfo.getRatioThree(),2,BigDecimal.ROUND_HALF_UP);
- }
- return count;
- }
- @Override
- public List<DepotItemVoBatchNumberList> getBatchNumberList(String number, String name, Long depotId, String barCode,
- String batchNumber, Boolean forceFlag, Boolean inOutManageFlag) throws Exception {
- List<DepotItemVoBatchNumberList> reslist = new ArrayList<>();
- List<DepotItemVoBatchNumberList> list = depotItemMapperEx.getBatchNumberList(StringUtil.toNull(number), name,
- depotId, barCode, batchNumber, forceFlag, inOutManageFlag);
- for(DepotItemVoBatchNumberList bn: list) {
- if(bn.getTotalNum()!=null && bn.getTotalNum().compareTo(BigDecimal.ZERO)>0) {
- bn.setExpirationDateStr(Tools.parseDateToStr(bn.getExpirationDate()));
- if(bn.getUnitId()!=null) {
- Unit unit = unitService.getUnit(bn.getUnitId());
- String commodityUnit = bn.getCommodityUnit();
- bn.setTotalNum(unitService.parseStockByUnit(bn.getTotalNum(), unit, commodityUnit));
- }
- reslist.add(bn);
- }
- }
- return reslist;
- }
- /**
- * 查询某个批号的商品库存
- * @param depotId
- * @param barCode
- * @param batchNumber
- * @return
- * @throws Exception
- */
- @Override
- public BigDecimal getOneBatchNumberStock(Long depotId, String barCode, String batchNumber) throws Exception {
- BigDecimal totalNum = BigDecimal.ZERO;
- Boolean forceFlag = systemConfigService.getForceApprovalFlag();
- Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
- List<DepotItemVoBatchNumberList> list = depotItemMapperEx.getBatchNumberList(null, null,
- depotId, barCode, batchNumber, forceFlag, inOutManageFlag);
- if(list!=null && list.size()>0) {
- DepotItemVoBatchNumberList bn = list.get(0);
- totalNum = bn.getTotalNum();
- if(bn.getTotalNum()!=null && bn.getTotalNum().compareTo(BigDecimal.ZERO)>0) {
- if(bn.getUnitId()!=null) {
- Unit unit = unitService.getUnit(bn.getUnitId());
- String commodityUnit = bn.getCommodityUnit();
- totalNum = unitService.parseStockByUnit(bn.getTotalNum(), unit, commodityUnit);
- }
- }
- }
- return totalNum;
- }
- @Override
- public Long getCountByMaterialAndDepot(Long mId, Long depotId) {
- return depotItemMapperEx.getCountByMaterialAndDepot(mId, depotId);
- }
- @Override
- public JSONObject parseMapByExcelData(String batchNumbers, List<Map<String, String>> detailList, String prefixNo) throws Exception {
- JSONObject map = new JSONObject();
- JSONArray arr = new JSONArray();
- //根据批次号获取商品数据
- List<MaterialVo4Unit> list = new ArrayList<>();
- if ("XSDD".equals(prefixNo)){ //销售订单导入
- String systemSku = batchNumbers.replace("'","");
- List<String> strings = Arrays.asList(systemSku.split(","));
- //根据系统sku查询商品
- for (String str : strings) {
- MaterialVo4Unit materialVo4Unit = depotItemMapperEx.getDepotMaterialBySystemSku(str);
- list.add(materialVo4Unit);
- }
- }else {
- list = depotItemMapperEx.getBillItemByParam(batchNumbers);
- }
- //商品数据集合
- Map<String, MaterialVo4Unit> materialMap = new HashMap<>();
- //仓库集合
- Map<String, Long> depotMap = new HashMap<>();
- for (MaterialVo4Unit material: list) {
- if ("XSDD".equals(prefixNo)){
- materialMap.put(material.getSystemSku(), material);
- }else {
- materialMap.put(material.getBatchNumber(), material);
- }
- }
- //获取当前用户所有仓库
- JSONArray depotArr = depotService.findDepotByCurrentUser();
- for (Object depotObj: depotArr) {
- if(depotObj!=null) {
- JSONObject depotObject = JSONObject.parseObject(depotObj.toString());
- depotMap.put(depotObject.getString("depotName"), depotObject.getLong("id"));
- }
- }
- for (Map<String, String> detailMap: detailList) {
- JSONObject item = new JSONObject();
- String barCode = detailMap.get("batchNumber");
- if(StringUtil.isNotEmpty(barCode)) {
- MaterialVo4Unit m = materialMap.get(barCode);
- if(m!=null) {
- //判断仓库是否存在
- String depotName = detailMap.get("depotName");
- if(StringUtil.isNotEmpty(depotName)) {
- if(depotMap.get(depotName)!=null) {
- item.put("depotName", depotName);
- item.put("depotId", depotMap.get(depotName));
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_DEPOTNAME_IS_NOT_EXIST_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_DEPOTNAME_IS_NOT_EXIST_MSG, depotName));
- }
- }
- item.put("batchNumber", barCode);
- item.put("name", m.getName());
- item.put("standard", m.getStandard());
- if(StringUtil.isNotEmpty(m.getModel())) {
- item.put("model", m.getModel());
- }
- if(StringUtil.isNotEmpty(m.getColor())) {
- item.put("color", m.getColor());
- }
- if(StringUtil.isNotEmpty(m.getSku())) {
- item.put("sku", m.getSku());
- }
- BigDecimal stock = BigDecimal.ZERO;
- if(StringUtil.isNotEmpty(m.getSku())){
- stock = getSkuStockByParam(null, m.getMeId(),null,null);
- } else {
- stock = getCurrentStockByParam(null, m.getId());
- }
- item.put("stock", stock);
- item.put("unit", m.getCommodityUnit());
- BigDecimal operNumber = BigDecimal.ZERO;
- BigDecimal unitPrice = BigDecimal.ZERO;
- BigDecimal taxRate = BigDecimal.ZERO;
- if(StringUtil.isNotEmpty(detailMap.get("num"))) {
- operNumber = new BigDecimal(detailMap.get("num"));
- }
- if(StringUtil.isNotEmpty(detailMap.get("unitPrice"))) {
- unitPrice = new BigDecimal(detailMap.get("unitPrice"));
- } else {
- if("CGDD".equals(prefixNo)) {
- unitPrice = m.getPurchaseDecimal();
- } else if("XSDD".equals(prefixNo)) {
- unitPrice = m.getWholesaleDecimal();
- }
- }
- if(StringUtil.isNotEmpty(detailMap.get("taxRate"))) {
- taxRate = new BigDecimal(detailMap.get("taxRate"));
- }
- String remark = detailMap.get("remark");
- item.put("operNumber", operNumber);
- item.put("unitPrice", unitPrice);
- BigDecimal allPrice = BigDecimal.ZERO;
- if(unitPrice!=null && unitPrice.compareTo(BigDecimal.ZERO)!=0) {
- allPrice = unitPrice.multiply(operNumber);
- }
- BigDecimal taxMoney = BigDecimal.ZERO;
- if(taxRate.compareTo(BigDecimal.ZERO) != 0) {
- taxMoney = taxRate.multiply(allPrice).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
- }
- BigDecimal taxLastMoney = allPrice.add(taxMoney);
- item.put("allPrice", allPrice);
- item.put("taxRate", taxRate);
- item.put("taxMoney", taxMoney);
- item.put("taxLastMoney", taxLastMoney);
- item.put("remark", remark);
- arr.add(item);
- } else {
- throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_BARCODE_IS_NOT_EXIST_CODE,
- String.format(ExceptionConstants.DEPOT_ITEM_BARCODE_IS_NOT_EXIST_MSG, barCode));
- }
- }
- }
- map.put("rows", arr);
- return map;
- }
- @Override
- public BigDecimal getLastUnitPriceByParam(Long organId, Long meId, String prefixNo) {
- String type = "";
- String subType = "";
- if("XSDD".equals(prefixNo)) {
- type = "其它";
- subType = "销售订单";
- } else if("XSCK".equals(prefixNo)) {
- type = "出库";
- subType = "销售";
- } else if("XSTH".equals(prefixNo)) {
- type = "入库";
- subType = "销售退货";
- } else if("QTCK".equals(prefixNo)) {
- type = "出库";
- subType = "其它";
- }
- return depotItemMapperEx.getLastUnitPriceByParam(organId, meId, type, subType);
- }
- @Override
- public BigDecimal getCurrentStockByParam(Long depotId, Long mId) {
- BigDecimal stock = depotItemMapperEx.getCurrentStockByParam(depotId, mId);
- return stock!=null? stock: BigDecimal.ZERO;
- }
- /**
- * 获取扩展信息
- *
- * @return
- */
- @Override
- public String getOtherInfo(String[] mpArr, DepotItemVo4WithInfoEx diEx)throws Exception {
- String materialOther = "";
- for (int i = 0; i < mpArr.length; i++) {
- if (mpArr[i].equals("自定义1")) {
- materialOther = materialOther + ((diEx.getMOtherField1() == null || diEx.getMOtherField1().equals("")) ? "" : "(" + diEx.getMOtherField1() + ")");
- }
- if (mpArr[i].equals("自定义2")) {
- materialOther = materialOther + ((diEx.getMOtherField2() == null || diEx.getMOtherField2().equals("")) ? "" : "(" + diEx.getMOtherField2() + ")");
- }
- if (mpArr[i].equals("自定义3")) {
- materialOther = materialOther + ((diEx.getMOtherField3() == null || diEx.getMOtherField3().equals("")) ? "" : "(" + diEx.getMOtherField3() + ")");
- }
- }
- return materialOther;
- }
- }
|