DepotItemServiceImpl.java 81 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. package com.jsh.erp.service.impl;
  2. import com.alibaba.fastjson.JSONArray;
  3. import com.alibaba.fastjson.JSONObject;
  4. import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
  5. import com.jsh.erp.constants.BusinessConstants;
  6. import com.jsh.erp.constants.ExceptionConstants;
  7. import com.jsh.erp.datasource.entities.*;
  8. import com.jsh.erp.datasource.mappers.*;
  9. import com.jsh.erp.datasource.pda.vo.PDADepotItemVO;
  10. import com.jsh.erp.datasource.vo.DepotHeadXsddRequestVO;
  11. import com.jsh.erp.datasource.vo.DepotItemStockWarningCount;
  12. import com.jsh.erp.datasource.vo.DepotItemVo4Stock;
  13. import com.jsh.erp.datasource.vo.DepotItemVoBatchNumberList;
  14. import com.jsh.erp.datasource.vo.DepotItemXsddRequestVO;
  15. import com.jsh.erp.datasource.vo.InOutPriceVo;
  16. import com.jsh.erp.exception.BusinessRunTimeException;
  17. import com.jsh.erp.exception.JshException;
  18. import com.jsh.erp.service.*;
  19. import com.jsh.erp.utils.StringUtil;
  20. import com.jsh.erp.utils.Tools;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.BeanUtils;
  24. import org.springframework.stereotype.Service;
  25. import org.springframework.transaction.annotation.Transactional;
  26. import javax.annotation.Resource;
  27. import javax.servlet.http.HttpServletRequest;
  28. import java.math.BigDecimal;
  29. import java.util.*;
  30. @Service
  31. public class DepotItemServiceImpl extends ServiceImpl<DepotItemMapper, DepotItem> implements DepotItemService {
  32. private Logger logger = LoggerFactory.getLogger(DepotItemServiceImpl.class);
  33. private final static String TYPE = "入库";
  34. private final static String SUM_TYPE = "number";
  35. private final static String IN = "in";
  36. private final static String OUT = "out";
  37. @Resource
  38. private DepotItemMapper depotItemMapper;
  39. @Resource
  40. private DepotItemMapperEx depotItemMapperEx;
  41. @Resource
  42. private MaterialService materialService;
  43. @Resource
  44. private MaterialExtendService materialExtendService;
  45. @Resource
  46. private SerialNumberMapperEx serialNumberMapperEx;
  47. @Resource
  48. private DepotHeadService depotHeadService;
  49. @Resource
  50. private DepotHeadMapper depotHeadMapper;
  51. @Resource
  52. private SerialNumberService serialNumberService;
  53. @Resource
  54. private UserService userService;
  55. @Resource
  56. private SystemConfigService systemConfigService;
  57. @Resource
  58. private DepotService depotService;
  59. @Resource
  60. private UnitService unitService;
  61. @Resource
  62. private MaterialCurrentStockMapper materialCurrentStockMapper;
  63. @Resource
  64. private MaterialCurrentStockMapperEx materialCurrentStockMapperEx;
  65. @Resource
  66. private LogService logService;
  67. @Resource
  68. private MaterialExtendMapper materialExtendMapper;
  69. /**
  70. * pda根据订单信息查询商品列表
  71. * @return
  72. */
  73. @Override
  74. public List<PDADepotItemVO> pdaList(Long id) {
  75. return depotItemMapper.pdaList(id);
  76. }
  77. /**
  78. * pda根据商品订单ID查询商品详情
  79. * @param id 商品订单ID
  80. * @return
  81. */
  82. @Override
  83. public PDADepotItemVO pdaDetail(Long id) {
  84. return depotItemMapper.pdaDetail(id);
  85. }
  86. /**
  87. * 商品库存详情列表
  88. * @param materialId 商品ID
  89. * @return
  90. */
  91. @Override
  92. public List<PDADepotItemVO> materialDepotDetail(String type , Long materialId) {
  93. return depotItemMapper.materialDepotDetail(type , materialId);
  94. }
  95. @Override
  96. public DepotItem getDepotItem(long id)throws Exception {
  97. DepotItem result=null;
  98. try{
  99. result=depotItemMapper.selectByPrimaryKey(id);
  100. }catch(Exception e){
  101. JshException.readFail(logger, e);
  102. }
  103. return result;
  104. }
  105. @Override
  106. public List<DepotItem> getDepotItem()throws Exception {
  107. DepotItemExample example = new DepotItemExample();
  108. example.createCriteria().andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  109. List<DepotItem> list=null;
  110. try{
  111. list=depotItemMapper.selectByExample(example);
  112. }catch(Exception e){
  113. JshException.readFail(logger, e);
  114. }
  115. return list;
  116. }
  117. @Override
  118. public List<DepotItem> select(String name, Integer type, String remark, int offset, int rows)throws Exception {
  119. List<DepotItem> list=null;
  120. try{
  121. list=depotItemMapperEx.selectByConditionDepotItem(name, type, remark, offset, rows);
  122. }catch(Exception e){
  123. JshException.readFail(logger, e);
  124. }
  125. return list;
  126. }
  127. @Override
  128. public Long countDepotItem(String name, Integer type, String remark) throws Exception{
  129. Long result =null;
  130. try{
  131. result=depotItemMapperEx.countsByDepotItem(name, type, remark);
  132. }catch(Exception e){
  133. JshException.readFail(logger, e);
  134. }
  135. return result;
  136. }
  137. @Override
  138. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  139. public int insertDepotItem(JSONObject obj, HttpServletRequest request)throws Exception {
  140. DepotItem depotItem = JSONObject.parseObject(obj.toJSONString(), DepotItem.class);
  141. int result =0;
  142. try{
  143. result=depotItemMapper.insertSelective(depotItem);
  144. }catch(Exception e){
  145. JshException.readFail(logger, e);
  146. }
  147. return result;
  148. }
  149. @Override
  150. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  151. public int updateDepotItem(JSONObject obj, HttpServletRequest request)throws Exception {
  152. DepotItem depotItem = JSONObject.parseObject(obj.toJSONString(), DepotItem.class);
  153. int result =0;
  154. try{
  155. result=depotItemMapper.updateByPrimaryKeySelective(depotItem);
  156. }catch(Exception e){
  157. JshException.readFail(logger, e);
  158. }
  159. return result;
  160. }
  161. @Override
  162. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  163. public int deleteDepotItem(Long id, HttpServletRequest request)throws Exception {
  164. int result =0;
  165. try{
  166. result=depotItemMapper.deleteByPrimaryKey(id);
  167. }catch(Exception e){
  168. JshException.writeFail(logger, e);
  169. }
  170. return result;
  171. }
  172. @Override
  173. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  174. public int batchDeleteDepotItem(String ids, HttpServletRequest request)throws Exception {
  175. List<Long> idList = StringUtil.strToLongList(ids);
  176. DepotItemExample example = new DepotItemExample();
  177. example.createCriteria().andIdIn(idList);
  178. int result =0;
  179. try{
  180. result=depotItemMapper.deleteByExample(example);
  181. }catch(Exception e){
  182. JshException.writeFail(logger, e);
  183. }
  184. return result;
  185. }
  186. @Override
  187. public int checkIsNameExist(Long id, String name)throws Exception {
  188. DepotItemExample example = new DepotItemExample();
  189. example.createCriteria().andIdNotEqualTo(id).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  190. List<DepotItem> list =null;
  191. try{
  192. list=depotItemMapper.selectByExample(example);
  193. }catch(Exception e){
  194. JshException.readFail(logger, e);
  195. }
  196. return list==null?0:list.size();
  197. }
  198. @Override
  199. public List<DepotItemVo4DetailByTypeAndMId> findDetailByDepotIdsAndMaterialIdList(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
  200. String number, String beginTime, String endTime, Long mId, Integer offset, Integer rows)throws Exception {
  201. Long depotId = null;
  202. if(StringUtil.isNotEmpty(depotIds)) {
  203. depotId = Long.parseLong(depotIds);
  204. }
  205. List<Long> depotList = depotService.parseDepotList(depotId);
  206. Long[] depotIdArray = StringUtil.listToLongArray(depotList);
  207. List<DepotItemVo4DetailByTypeAndMId> list =null;
  208. try{
  209. list = depotItemMapperEx.findDetailByDepotIdsAndMaterialIdList(depotIdArray, forceFlag, inOutManageFlag, sku, batchNumber, number, beginTime, endTime, mId, offset, rows);
  210. }catch(Exception e){
  211. JshException.readFail(logger, e);
  212. }
  213. return list;
  214. }
  215. @Override
  216. public Long findDetailByDepotIdsAndMaterialIdCount(String depotIds, Boolean forceFlag, Boolean inOutManageFlag, String sku, String batchNumber,
  217. String number, String beginTime, String endTime, Long mId)throws Exception {
  218. Long depotId = null;
  219. if(StringUtil.isNotEmpty(depotIds)) {
  220. depotId = Long.parseLong(depotIds);
  221. }
  222. List<Long> depotList = depotService.parseDepotList(depotId);
  223. Long[] depotIdArray = StringUtil.listToLongArray(depotList);
  224. Long result =null;
  225. try{
  226. result = depotItemMapperEx.findDetailByDepotIdsAndMaterialIdCount(depotIdArray, forceFlag, inOutManageFlag, sku, batchNumber, number, beginTime, endTime, mId);
  227. }catch(Exception e){
  228. JshException.readFail(logger, e);
  229. }
  230. return result;
  231. }
  232. /**
  233. * 插入单据子表
  234. * @param depotItem 单据子表
  235. */
  236. @Override
  237. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  238. public int insertDepotItemWithObj(DepotItem depotItem)throws Exception {
  239. int result =0;
  240. try{
  241. result = depotItemMapper.insertSelective(depotItem);
  242. }catch(Exception e){
  243. JshException.writeFail(logger, e);
  244. }
  245. return result;
  246. }
  247. @Override
  248. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  249. public int updateDepotItemWithObj(DepotItem depotItem)throws Exception {
  250. int result =0;
  251. try{
  252. result = depotItemMapper.updateByPrimaryKeySelective(depotItem);
  253. }catch(Exception e){
  254. JshException.writeFail(logger, e);
  255. }
  256. return result;
  257. }
  258. @Override
  259. public List<DepotItem> getListByHeaderId(Long headerId)throws Exception {
  260. List<DepotItem> list =null;
  261. try{
  262. DepotItemExample example = new DepotItemExample();
  263. example.createCriteria().andHeaderIdEqualTo(headerId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  264. list = depotItemMapper.selectByExample(example);
  265. }catch(Exception e){
  266. JshException.readFail(logger, e);
  267. }
  268. return list;
  269. }
  270. /**
  271. * 查询当前单据中指定商品的明细信息
  272. * @param headerId
  273. * @param meId
  274. * @return
  275. * @throws Exception
  276. */
  277. @Override
  278. public DepotItem getItemByHeaderIdAndMaterial(Long headerId, Long meId)throws Exception {
  279. DepotItem depotItem = new DepotItem();
  280. try{
  281. DepotItemExample example = new DepotItemExample();
  282. example.createCriteria().andHeaderIdEqualTo(headerId).andMaterialExtendIdEqualTo(meId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  283. List<DepotItem> list = depotItemMapper.selectByExample(example);
  284. if(list!=null && list.size()>0) {
  285. depotItem = list.get(0);
  286. }
  287. }catch(Exception e){
  288. JshException.readFail(logger, e);
  289. }
  290. return depotItem;
  291. }
  292. /**
  293. * 查询被关联订单中指定商品的明细信息
  294. * @param linkStr
  295. * @param meId
  296. * @return
  297. * @throws Exception
  298. */
  299. @Override
  300. public DepotItem getPreItemByHeaderIdAndMaterial(String linkStr, Long meId, Long linkId)throws Exception {
  301. DepotItem depotItem = new DepotItem();
  302. try{
  303. DepotHead depotHead = depotHeadService.getDepotHead(linkStr);
  304. if(null!=depotHead && null!=depotHead.getId()) {
  305. DepotItemExample example = new DepotItemExample();
  306. example.createCriteria().andHeaderIdEqualTo(depotHead.getId()).andMaterialExtendIdEqualTo(meId).andIdEqualTo(linkId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  307. List<DepotItem> list = depotItemMapper.selectByExample(example);
  308. if(list!=null && list.size()>0) {
  309. depotItem = list.get(0);
  310. }
  311. }
  312. }catch(Exception e){
  313. JshException.readFail(logger, e);
  314. }
  315. return depotItem;
  316. }
  317. @Override
  318. public List<DepotItemVo4WithInfoEx> getDetailList(Long headerId)throws Exception {
  319. List<DepotItemVo4WithInfoEx> list =null;
  320. try{
  321. list = depotItemMapperEx.getDetailList(headerId);
  322. }catch(Exception e){
  323. JshException.readFail(logger, e);
  324. }
  325. return list;
  326. }
  327. @Override
  328. public List<DepotItemVo4WithInfoEx> getInOutStock(String materialParam, List<Long> categoryIdList, String endTime, Integer offset, Integer rows)throws Exception {
  329. List<DepotItemVo4WithInfoEx> list =null;
  330. try{
  331. list = depotItemMapperEx.getInOutStock(materialParam, categoryIdList, endTime, offset, rows);
  332. }catch(Exception e){
  333. JshException.readFail(logger, e);
  334. }
  335. return list;
  336. }
  337. @Override
  338. public int getInOutStockCount(String materialParam, List<Long> categoryIdList, String endTime)throws Exception {
  339. int result=0;
  340. try{
  341. result = depotItemMapperEx.getInOutStockCount(materialParam, categoryIdList, endTime);
  342. }catch(Exception e){
  343. JshException.readFail(logger, e);
  344. }
  345. return result;
  346. }
  347. @Override
  348. public List<DepotItemVo4WithInfoEx> getListWithBuyOrSale(String materialParam, String billType,
  349. String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag, Integer offset, Integer rows)throws Exception {
  350. List<DepotItemVo4WithInfoEx> list =null;
  351. try{
  352. list = depotItemMapperEx.getListWithBuyOrSale(materialParam, billType, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag, offset, rows);
  353. }catch(Exception e){
  354. JshException.readFail(logger, e);
  355. }
  356. return list;
  357. }
  358. @Override
  359. public int getListWithBuyOrSaleCount(String materialParam, String billType,
  360. String beginTime, String endTime, String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag)throws Exception {
  361. int result=0;
  362. try{
  363. result = depotItemMapperEx.getListWithBuyOrSaleCount(materialParam, billType, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag);
  364. }catch(Exception e){
  365. JshException.readFail(logger, e);
  366. }
  367. return result;
  368. }
  369. @Override
  370. public BigDecimal buyOrSale(String type, String subType, Long MId, String beginTime, String endTime,
  371. String[] creatorArray, Long organId, String[] organArray, List<Long> depotList, Boolean forceFlag, String sumType) throws Exception{
  372. BigDecimal result= BigDecimal.ZERO;
  373. try{
  374. if (SUM_TYPE.equals(sumType)) {
  375. result= depotItemMapperEx.buyOrSaleNumber(type, subType, MId, beginTime, endTime, creatorArray, organId, organArray, depotList, forceFlag, sumType);
  376. } else {
  377. result= depotItemMapperEx.buyOrSalePrice(type, subType, MId, beginTime, endTime, creatorArray, organId, organArray, depotList, forceFlag, sumType);
  378. }
  379. }catch(Exception e){
  380. JshException.readFail(logger, e);
  381. }
  382. return result;
  383. }
  384. @Override
  385. public BigDecimal buyOrSalePriceTotal(String type, String subType, String materialParam, String beginTime, String endTime,
  386. String[] creatorArray, Long organId, String[] organArray, List<Long> categoryList, List<Long> depotList, Boolean forceFlag) throws Exception{
  387. BigDecimal result= BigDecimal.ZERO;
  388. try{
  389. result= depotItemMapperEx.buyOrSalePriceTotal(type, subType, materialParam, beginTime, endTime, creatorArray, organId, organArray, categoryList, depotList, forceFlag);
  390. }catch(Exception e){
  391. JshException.readFail(logger, e);
  392. }
  393. return result;
  394. }
  395. /**
  396. * 统计采购、销售、零售的总金额列表
  397. * @param beginTime
  398. * @param endTime
  399. * @return
  400. * @throws Exception
  401. */
  402. @Override
  403. public List<InOutPriceVo> inOrOutPriceList(String beginTime, String endTime) throws Exception{
  404. List<InOutPriceVo> result = new ArrayList<>();
  405. try{
  406. String [] creatorArray = depotHeadService.getCreatorArray();
  407. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  408. result = depotItemMapperEx.inOrOutPriceList(beginTime, endTime, creatorArray, forceFlag);
  409. }catch(Exception e){
  410. JshException.readFail(logger, e);
  411. }
  412. return result;
  413. }
  414. /**
  415. * 保存单据子表
  416. * @param rows 子表数据字符
  417. * @param headerId 单据主表id
  418. * @param actionType 操作类型
  419. */
  420. @Override
  421. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  422. public void saveDetials(String rows, Long headerId, String actionType, HttpServletRequest request) throws Exception{
  423. //查询单据主表信息
  424. DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(headerId);
  425. //删除序列号和回收序列号
  426. deleteOrCancelSerialNumber(actionType, depotHead, headerId);
  427. //删除单据的明细
  428. deleteDepotItemHeadId(headerId);
  429. JSONArray rowArr = JSONArray.parseArray(rows);
  430. if (null != rowArr && rowArr.size()>0) {
  431. //针对组装单、拆卸单校验是否存在组合件和普通子件
  432. checkAssembleWithMaterialType(rowArr, depotHead.getSubType());
  433. for (int i = 0; i < rowArr.size(); i++) {
  434. DepotItem depotItem = new DepotItem();
  435. JSONObject rowObj = JSONObject.parseObject(rowArr.getString(i));
  436. depotItem.setHeaderId(headerId);
  437. String batchNumber = rowObj.getString("batchNumber");
  438. MaterialExtend materialExtend = materialExtendService.getInfoByBatchNumber(batchNumber);
  439. if(materialExtend == null) {
  440. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_BARCODE_IS_NOT_EXIST_CODE,
  441. String.format(ExceptionConstants.MATERIAL_BARCODE_IS_NOT_EXIST_MSG, batchNumber));
  442. }
  443. depotItem.setMaterialId(materialExtend.getMaterialId());
  444. depotItem.setMaterialExtendId(materialExtend.getId());
  445. depotItem.setMaterialUnit(rowObj.getString("unit"));
  446. depotItem.setActualQuantityInStorage(rowObj.getBigDecimal("actualQuantityInStorage"));
  447. depotItem.setWarehousingVariance(rowObj.getBigDecimal("warehousingVariance"));
  448. depotItem.setReasonOfDifference(rowObj.getString("reasonOfDifference"));
  449. depotItem.setWarehousingUser(rowObj.getLong("warehousingUser"));
  450. depotItem.setWarehousingTime(rowObj.getDate("warehousingTime"));
  451. Material material= materialService.getMaterial(depotItem.getMaterialId());
  452. if (BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber()) ||
  453. BusinessConstants.ENABLE_BATCH_NUMBER_ENABLED.equals(material.getEnableBatchNumber())) {
  454. //组装拆卸单不能选择批号或序列号商品
  455. if(BusinessConstants.SUB_TYPE_ASSEMBLE.equals(depotHead.getSubType()) ||
  456. BusinessConstants.SUB_TYPE_DISASSEMBLE.equals(depotHead.getSubType())) {
  457. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_ASSEMBLE_SELECT_ERROR_CODE,
  458. String.format(ExceptionConstants.MATERIAL_ASSEMBLE_SELECT_ERROR_MSG, batchNumber));
  459. }
  460. //调拨单不能选择批号或序列号商品(该场景走出库和入库单)
  461. if(BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
  462. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_TRANSFER_SELECT_ERROR_CODE,
  463. String.format(ExceptionConstants.MATERIAL_TRANSFER_SELECT_ERROR_MSG, batchNumber));
  464. }
  465. //盘点业务不能选择批号或序列号商品(该场景走出库和入库单)
  466. if(BusinessConstants.SUB_TYPE_CHECK_ENTER.equals(depotHead.getSubType())
  467. ||BusinessConstants.SUB_TYPE_REPLAY.equals(depotHead.getSubType())) {
  468. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_STOCK_CHECK_ERROR_CODE,
  469. String.format(ExceptionConstants.MATERIAL_STOCK_CHECK_ERROR_MSG, batchNumber));
  470. }
  471. }
  472. if (StringUtil.isExist(rowObj.get("snList"))) {
  473. depotItem.setSnList(rowObj.getString("snList"));
  474. if(StringUtil.isExist(rowObj.get("depotId"))) {
  475. String [] snArray = depotItem.getSnList().split(",");
  476. int operNum = rowObj.getInteger("operNumber");
  477. if(snArray.length == operNum) {
  478. Long depotId = rowObj.getLong("depotId");
  479. BigDecimal inPrice = BigDecimal.ZERO;
  480. if (StringUtil.isExist(rowObj.get("unitPrice"))) {
  481. inPrice = rowObj.getBigDecimal("unitPrice");
  482. }
  483. serialNumberService.addSerialNumberByBill(depotHead.getType(), depotHead.getSubType(),
  484. depotHead.getNumber(), materialExtend.getMaterialId(), depotId, inPrice, depotItem.getSnList());
  485. } else {
  486. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_SN_NUMBERE_FAILED_CODE,
  487. String.format(ExceptionConstants.DEPOT_HEAD_SN_NUMBERE_FAILED_MSG, batchNumber));
  488. }
  489. }
  490. } else {
  491. //入库或出库
  492. if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) ||
  493. BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())) {
  494. //序列号不能为空
  495. if (BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber())) {
  496. //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
  497. if(systemConfigService.getInOutManageFlag() &&
  498. (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
  499. ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
  500. ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
  501. ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
  502. //跳过
  503. } else {
  504. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_SERIAL_NUMBERE_EMPTY_CODE,
  505. String.format(ExceptionConstants.MATERIAL_SERIAL_NUMBERE_EMPTY_MSG, batchNumber));
  506. }
  507. }
  508. }
  509. }
  510. if (StringUtil.isExist(rowObj.get("batchNumber"))) {
  511. depotItem.setBatchNumber(rowObj.getString("batchNumber"));
  512. } else {
  513. //入库或出库
  514. if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType()) ||
  515. BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())) {
  516. //批号不能为空
  517. if (BusinessConstants.ENABLE_BATCH_NUMBER_ENABLED.equals(material.getEnableBatchNumber())) {
  518. //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
  519. if(systemConfigService.getInOutManageFlag() &&
  520. (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
  521. ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
  522. ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
  523. ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
  524. //跳过
  525. } else {
  526. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_BATCH_NUMBERE_EMPTY_CODE,
  527. String.format(ExceptionConstants.DEPOT_HEAD_BATCH_NUMBERE_EMPTY_MSG, batchNumber));
  528. }
  529. }
  530. }
  531. }
  532. if (StringUtil.isExist(rowObj.get("expirationDate"))) {
  533. depotItem.setExpirationDate(rowObj.getDate("expirationDate"));
  534. }
  535. if (StringUtil.isExist(rowObj.get("sku"))) {
  536. depotItem.setSku(rowObj.getString("sku"));
  537. }
  538. if (StringUtil.isExist(rowObj.get("linkId"))) {
  539. depotItem.setLinkId(rowObj.getLong("linkId"));
  540. }
  541. //以下进行单位换算
  542. Unit unitInfo = materialService.findUnit(materialExtend.getMaterialId()); //查询多单位信息
  543. if (StringUtil.isExist(rowObj.get("operNumber"))) {
  544. //获取子表单商品数量
  545. depotItem.setOperNumber(rowObj.getBigDecimal("operNumber"));
  546. //获取子表单商品单位
  547. String unit = rowObj.get("unit") == null ? "" :rowObj.get("unit").toString();
  548. BigDecimal oNumber = rowObj.getBigDecimal("operNumber");
  549. if (StringUtil.isNotEmpty(unitInfo.getName())) {
  550. String basicUnit = unitInfo.getBasicUnit(); //基本单位
  551. if (unit.equals(basicUnit)) { //如果等于基本单位
  552. depotItem.setBasicNumber(oNumber); //数量一致
  553. } else if (unit.equals(unitInfo.getOtherUnit())) { //如果等于副单位
  554. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatio())); //数量乘以比例
  555. } else if (unit.equals(unitInfo.getOtherUnitTwo())) { //如果等于副单位2
  556. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioTwo())); //数量乘以比例
  557. } else if (unit.equals(unitInfo.getOtherUnitThree())) { //如果等于副单位3
  558. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioThree())); //数量乘以比例
  559. } else {
  560. depotItem.setBasicNumber(oNumber); //数量一致
  561. }
  562. } else {
  563. depotItem.setBasicNumber(oNumber); //其他情况
  564. }
  565. }
  566. //如果数量+已完成数量>原订单数量,给出预警(判断前提是存在关联订单|关联请购单)
  567. String linkStr = StringUtil.isNotEmpty(depotHead.getLinkNumber())? depotHead.getLinkNumber(): depotHead.getLinkApply();
  568. if (StringUtil.isNotEmpty(linkStr) && StringUtil.isExist(rowObj.get("preNumber")) && StringUtil.isExist(rowObj.get("finishNumber"))) {
  569. if("add".equals(actionType)) {
  570. //在新增模式进行状态赋值
  571. BigDecimal preNumber = rowObj.getBigDecimal("preNumber");
  572. BigDecimal finishNumber = rowObj.getBigDecimal("finishNumber");
  573. if(depotItem.getOperNumber().add(finishNumber).compareTo(preNumber)>0) {
  574. if(!systemConfigService.getOverLinkBillFlag()) {
  575. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_CODE,
  576. String.format(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_MSG, batchNumber));
  577. }
  578. }
  579. } else if("update".equals(actionType)) {
  580. //当前单据的类型
  581. String currentSubType = depotHead.getSubType();
  582. //在更新模式进行状态赋值
  583. String unit = rowObj.get("unit").toString();
  584. Long preHeaderId = depotHeadService.getDepotHead(linkStr).getId();
  585. if(null!=preHeaderId) {
  586. //前一个单据的数量
  587. BigDecimal preNumber = getPreItemByHeaderIdAndMaterial(linkStr, depotItem.getMaterialExtendId(), depotItem.getLinkId()).getOperNumber();
  588. //除去此单据之外的已入库|已出库
  589. BigDecimal realFinishNumber = getRealFinishNumber(currentSubType, depotItem.getMaterialExtendId(), depotItem.getLinkId(), preHeaderId, headerId, unitInfo, unit);
  590. if(preNumber!=null) {
  591. if (depotItem.getOperNumber().add(realFinishNumber).compareTo(preNumber) > 0) {
  592. if (!systemConfigService.getOverLinkBillFlag()) {
  593. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_CODE,
  594. String.format(ExceptionConstants.DEPOT_HEAD_NUMBER_NEED_EDIT_FAILED_MSG, batchNumber));
  595. }
  596. }
  597. } else {
  598. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_PRE_BILL_IS_CHANGE_CODE,
  599. ExceptionConstants.DEPOT_ITEM_PRE_BILL_IS_CHANGE_MSG);
  600. }
  601. }
  602. }
  603. }
  604. if (StringUtil.isExist(rowObj.get("unitPrice"))) {
  605. BigDecimal unitPrice = rowObj.getBigDecimal("unitPrice");
  606. depotItem.setUnitPrice(unitPrice);
  607. if(materialExtend.getLowDecimal()!=null) {
  608. //零售或销售单价低于最低售价,进行提示
  609. if("零售".equals(depotHead.getSubType()) || "销售".equals(depotHead.getSubType())) {
  610. if (unitPrice.compareTo(materialExtend.getLowDecimal()) < 0) {
  611. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_UNIT_PRICE_LOW_CODE,
  612. String.format(ExceptionConstants.DEPOT_HEAD_UNIT_PRICE_LOW_MSG, batchNumber));
  613. }
  614. }
  615. }
  616. }
  617. //如果是销售出库、销售退货、零售出库、零售退货则给采购单价字段赋值(如果是批次商品,则要根据批号去找之前的入库价)
  618. if(BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType()) ||
  619. BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()) ||
  620. BusinessConstants.SUB_TYPE_RETAIL.equals(depotHead.getSubType()) ||
  621. BusinessConstants.SUB_TYPE_RETAIL_RETURN.equals(depotHead.getSubType())) {
  622. boolean moveAvgPriceFlag = systemConfigService.getMoveAvgPriceFlag();
  623. BigDecimal currentUnitPrice = materialCurrentStockMapperEx.getCurrentUnitPriceByMId(materialExtend.getMaterialId());
  624. currentUnitPrice = unitService.parseUnitPriceByUnit(currentUnitPrice, unitInfo, depotItem.getMaterialUnit());
  625. BigDecimal unitPrice = moveAvgPriceFlag? currentUnitPrice: materialExtend.getPurchaseDecimal();
  626. depotItem.setPurchaseUnitPrice(unitPrice);
  627. if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
  628. depotItem.setPurchaseUnitPrice(getDepotItemByBatchNumber(depotItem.getMaterialExtendId(),depotItem.getBatchNumber()).getUnitPrice());
  629. }
  630. }
  631. if (StringUtil.isExist(rowObj.get("taxUnitPrice"))) {
  632. depotItem.setTaxUnitPrice(rowObj.getBigDecimal("taxUnitPrice"));
  633. }
  634. if (StringUtil.isExist(rowObj.get("allPrice"))) {
  635. depotItem.setAllPrice(rowObj.getBigDecimal("allPrice"));
  636. }
  637. if (StringUtil.isExist(rowObj.get("depotId"))) {
  638. depotItem.setDepotId(rowObj.getLong("depotId"));
  639. } else {
  640. if(!BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())
  641. && !BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())
  642. && !BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
  643. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_DEPOT_FAILED_CODE,
  644. String.format(ExceptionConstants.DEPOT_HEAD_DEPOT_FAILED_MSG));
  645. }
  646. }
  647. if(BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
  648. if (StringUtil.isExist(rowObj.get("anotherDepotId"))) {
  649. if(rowObj.getLong("anotherDepotId").equals(rowObj.getLong("depotId"))) {
  650. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_EQUAL_FAILED_CODE,
  651. String.format(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_EQUAL_FAILED_MSG));
  652. } else {
  653. depotItem.setAnotherDepotId(rowObj.getLong("anotherDepotId"));
  654. }
  655. } else {
  656. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_FAILED_CODE,
  657. String.format(ExceptionConstants.DEPOT_HEAD_ANOTHER_DEPOT_FAILED_MSG));
  658. }
  659. }
  660. if (StringUtil.isExist(rowObj.get("taxRate"))) {
  661. depotItem.setTaxRate(rowObj.getBigDecimal("taxRate"));
  662. }
  663. if (StringUtil.isExist(rowObj.get("taxMoney"))) {
  664. depotItem.setTaxMoney(rowObj.getBigDecimal("taxMoney"));
  665. }
  666. if (StringUtil.isExist(rowObj.get("taxLastMoney"))) {
  667. depotItem.setTaxLastMoney(rowObj.getBigDecimal("taxLastMoney"));
  668. }
  669. if (StringUtil.isExist(rowObj.get("mType"))) {
  670. depotItem.setMaterialType(rowObj.getString("mType"));
  671. }
  672. if (StringUtil.isExist(rowObj.get("remark"))) {
  673. depotItem.setRemark(rowObj.getString("remark"));
  674. }
  675. //出库时判断库存是否充足
  676. if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())){
  677. String stockMsg = material.getName() + "-" + batchNumber;
  678. BigDecimal stock = getCurrentStockByParam(depotItem.getDepotId(),depotItem.getMaterialId());
  679. if(StringUtil.isNotEmpty(depotItem.getSku())) {
  680. //对于sku商品要换个方式计算库存
  681. stock = getSkuStockByParam(depotItem.getDepotId(),depotItem.getMaterialExtendId(),null,null);
  682. }
  683. if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
  684. //对于批次商品要换个方式计算库存
  685. stock = getOneBatchNumberStock(depotItem.getDepotId(), batchNumber, depotItem.getBatchNumber());
  686. stockMsg += "-批号" + depotItem.getBatchNumber();
  687. }
  688. BigDecimal thisRealNumber = depotItem.getBasicNumber()==null?BigDecimal.ZERO:depotItem.getBasicNumber();
  689. if(StringUtil.isNotEmpty(depotItem.getBatchNumber())) {
  690. //对于批次商品,直接使用当前填写的数量
  691. thisRealNumber = depotItem.getOperNumber()==null?BigDecimal.ZERO:depotItem.getOperNumber();
  692. }
  693. if(!systemConfigService.getMinusStockFlag() && stock.compareTo(thisRealNumber)<0){
  694. throw new BusinessRunTimeException(ExceptionConstants.MATERIAL_STOCK_NOT_ENOUGH_CODE,
  695. String.format(ExceptionConstants.MATERIAL_STOCK_NOT_ENOUGH_MSG, stockMsg));
  696. }
  697. //出库时处理序列号
  698. if(!BusinessConstants.SUB_TYPE_TRANSFER.equals(depotHead.getSubType())) {
  699. //判断商品是否开启序列号,开启的售出序列号,未开启的跳过
  700. if(BusinessConstants.ENABLE_SERIAL_NUMBER_ENABLED.equals(material.getEnableSerialNumber())) {
  701. //如果开启出入库管理,并且类型等于采购、采购退货、销售、销售退货,则跳过
  702. if(systemConfigService.getInOutManageFlag() &&
  703. (BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
  704. ||BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
  705. ||BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
  706. ||BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType()))) {
  707. //跳过
  708. } else {
  709. //售出序列号,获得当前操作人
  710. User userInfo = userService.getCurrentUser();
  711. serialNumberService.checkAndUpdateSerialNumber(depotItem, depotHead.getNumber(), userInfo, StringUtil.toNull(depotItem.getSnList()));
  712. }
  713. }
  714. }
  715. }
  716. this.insertDepotItemWithObj(depotItem);
  717. //更新当前库存
  718. updateCurrentStock(depotItem);
  719. //更新当前成本价
  720. updateCurrentUnitPrice(depotItem);
  721. //更新商品的价格
  722. updateMaterialExtendPrice(materialExtend.getId(), depotHead.getSubType(), depotHead.getBillType(), rowObj);
  723. }
  724. //如果关联单据号非空则更新订单的状态,单据类型:采购入库单、销售出库单、盘点复盘单、其它入库单、其它出库单
  725. if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
  726. || BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
  727. || BusinessConstants.SUB_TYPE_REPLAY.equals(depotHead.getSubType())
  728. || BusinessConstants.SUB_TYPE_OTHER.equals(depotHead.getSubType())) {
  729. if(StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
  730. //单据状态:是否全部完成 2-全部完成 3-部分完成(针对订单的分批出入库)
  731. String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkNumber(), "normal");
  732. changeBillStatus(depotHead.getLinkNumber(), billStatus);
  733. }
  734. }
  735. //当前单据类型为采购订单的逻辑
  736. if(BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
  737. //如果关联单据号非空则更新订单的状态,此处针对销售订单转采购订单的场景
  738. if(StringUtil.isNotEmpty(depotHead.getLinkNumber())) {
  739. String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkNumber(), "normal");
  740. changeBillPurchaseStatus(depotHead.getLinkNumber(), billStatus);
  741. }
  742. //如果关联单据号非空则更新订单的状态,此处针对请购单转采购订单的场景
  743. if(StringUtil.isNotEmpty(depotHead.getLinkApply())) {
  744. String billStatus = getBillStatusByParam(depotHead, depotHead.getLinkApply(), "apply");
  745. changeBillStatus(depotHead.getLinkApply(), billStatus);
  746. }
  747. }
  748. } else {
  749. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_CODE,
  750. String.format(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_MSG));
  751. }
  752. }
  753. @Override
  754. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  755. public void saveOrderItem(DepotHeadXsddRequestVO order, List<DepotItemXsddRequestVO> itemList) throws Exception {
  756. // 查询单据主表信息
  757. DepotHead depotHead = depotHeadMapper.selectByPrimaryKey(order.getId());
  758. if (depotHead == null) {
  759. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_NOT_EXIST_CODE, String.format(ExceptionConstants.DEPOT_HEAD_NOT_EXIST_MSG));
  760. }
  761. if (null != itemList && itemList.size() > 0) {
  762. for (DepotItemXsddRequestVO item : itemList) {
  763. DepotItem depotItem = new DepotItem();
  764. BeanUtils.copyProperties(item, depotItem);
  765. // 以下进行单位换算
  766. Unit unitInfo = materialService.findUnit(depotItem.getMaterialId()); // 查询多单位信息
  767. if (depotItem.getOperNumber() != null) {
  768. // 获取子表单商品单位
  769. String unit = depotItem.getMaterialUnit() == null ? "" : depotItem.getMaterialUnit();
  770. BigDecimal oNumber = depotItem.getOperNumber();
  771. if (StringUtil.isNotEmpty(unitInfo.getName())) {
  772. String basicUnit = unitInfo.getBasicUnit(); // 基本单位
  773. if (unit.equals(basicUnit)) { // 如果等于基本单位
  774. depotItem.setBasicNumber(oNumber); // 数量一致
  775. } else if (unit.equals(unitInfo.getOtherUnit())) { // 如果等于副单位
  776. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatio())); // 数量乘以比例
  777. } else if (unit.equals(unitInfo.getOtherUnitTwo())) { // 如果等于副单位2
  778. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioTwo())); // 数量乘以比例
  779. } else if (unit.equals(unitInfo.getOtherUnitThree())) { // 如果等于副单位3
  780. depotItem.setBasicNumber(oNumber.multiply(unitInfo.getRatioThree())); // 数量乘以比例
  781. } else {
  782. depotItem.setBasicNumber(oNumber); // 数量一致
  783. }
  784. } else {
  785. depotItem.setBasicNumber(oNumber); // 其他情况
  786. }
  787. }
  788. this.insertDepotItemWithObj(depotItem);
  789. // 更新当前库存
  790. updateCurrentStock(depotItem);
  791. }
  792. } else {
  793. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_CODE, String.format(ExceptionConstants.DEPOT_HEAD_ROW_FAILED_MSG));
  794. }
  795. }
  796. /**
  797. * 判断单据的状态
  798. * 通过数组对比:原单据的商品和商品数量(汇总) 与 分批操作后单据的商品和商品数量(汇总)
  799. * @param depotHead
  800. * @param linkStr
  801. * @return
  802. */
  803. @Override
  804. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  805. public String getBillStatusByParam(DepotHead depotHead, String linkStr, String linkType) {
  806. String res = BusinessConstants.BILLS_STATUS_SKIPED;
  807. //获取原单据的商品和商品数量(汇总)
  808. List<DepotItemVo4MaterialAndSum> linkList = depotItemMapperEx.getLinkBillDetailMaterialSum(linkStr);
  809. //获取分批操作后单据的商品和商品数量(汇总)
  810. List<DepotItemVo4MaterialAndSum> batchList = depotItemMapperEx.getBatchBillDetailMaterialSum(linkStr, linkType, depotHead.getType());
  811. //将分批操作后的单据的商品和商品数据构造成Map
  812. Map<Long, BigDecimal> materialSumMap = new HashMap<>();
  813. for(DepotItemVo4MaterialAndSum materialAndSum : batchList) {
  814. materialSumMap.put(materialAndSum.getMaterialExtendId(), materialAndSum.getOperNumber());
  815. }
  816. for(DepotItemVo4MaterialAndSum materialAndSum : linkList) {
  817. //过滤掉原单里面有数量为0的商品
  818. if(materialAndSum.getOperNumber().compareTo(BigDecimal.ZERO) != 0) {
  819. BigDecimal materialSum = materialSumMap.get(materialAndSum.getMaterialExtendId());
  820. if (materialSum != null) {
  821. if (materialSum.compareTo(materialAndSum.getOperNumber()) < 0) {
  822. res = BusinessConstants.BILLS_STATUS_SKIPING;
  823. }
  824. } else {
  825. res = BusinessConstants.BILLS_STATUS_SKIPING;
  826. }
  827. }
  828. }
  829. return res;
  830. }
  831. /**
  832. * 更新单据状态
  833. * @param linkStr
  834. * @param billStatus
  835. */
  836. @Override
  837. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  838. public void changeBillStatus(String linkStr, String billStatus) {
  839. DepotHead depotHeadOrders = new DepotHead();
  840. depotHeadOrders.setStatus(billStatus);
  841. DepotHeadExample example = new DepotHeadExample();
  842. List<String> linkNoList = StringUtil.strToStringList(linkStr);
  843. example.createCriteria().andNumberIn(linkNoList);
  844. try{
  845. depotHeadMapper.updateByExampleSelective(depotHeadOrders, example);
  846. }catch(Exception e){
  847. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  848. ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  849. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  850. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  851. }
  852. }
  853. /**
  854. * 更新单据状态,此处针对销售订单转采购订单的场景
  855. * @param linkStr
  856. * @param billStatus
  857. */
  858. @Override
  859. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  860. public void changeBillPurchaseStatus(String linkStr, String billStatus) {
  861. DepotHead depotHeadOrders = new DepotHead();
  862. depotHeadOrders.setPurchaseStatus(billStatus);
  863. DepotHeadExample example = new DepotHeadExample();
  864. List<String> linkNoList = StringUtil.strToStringList(linkStr);
  865. example.createCriteria().andNumberIn(linkNoList);
  866. try{
  867. depotHeadMapper.updateByExampleSelective(depotHeadOrders, example);
  868. }catch(Exception e){
  869. logger.error("异常码[{}],异常提示[{}],异常[{}]",
  870. ExceptionConstants.DATA_WRITE_FAIL_CODE,ExceptionConstants.DATA_WRITE_FAIL_MSG,e);
  871. throw new BusinessRunTimeException(ExceptionConstants.DATA_WRITE_FAIL_CODE,
  872. ExceptionConstants.DATA_WRITE_FAIL_MSG);
  873. }
  874. }
  875. /**
  876. * 根据批号查询单据明细信息
  877. * @param materialExtendId
  878. * @param batchNumber
  879. * @return
  880. */
  881. @Override
  882. public DepotItem getDepotItemByBatchNumber(Long materialExtendId, String batchNumber) {
  883. List<DepotItem> depotItemList = depotItemMapperEx.getDepotItemByBatchNumber(materialExtendId, batchNumber);
  884. if(null != depotItemList && depotItemList.size() > 0){
  885. return depotItemList.get(0);
  886. } else {
  887. return new DepotItem();
  888. }
  889. }
  890. /**
  891. * 根据主表id删除单明细
  892. * @param headerId
  893. * @throws Exception
  894. */
  895. @Override
  896. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  897. public void deleteDepotItemHeadId(Long headerId)throws Exception {
  898. try{
  899. //1、查询删除前的单据明细
  900. List<DepotItem> depotItemList = getListByHeaderId(headerId);
  901. //2、删除单据明细
  902. DepotItemExample example = new DepotItemExample();
  903. example.createCriteria().andHeaderIdEqualTo(headerId);
  904. depotItemMapper.deleteByExample(example);
  905. //3、计算删除之后单据明细中商品的库存
  906. for(DepotItem depotItem : depotItemList){
  907. updateCurrentStock(depotItem);
  908. }
  909. }catch(Exception e){
  910. JshException.writeFail(logger, e);
  911. }
  912. }
  913. /**
  914. * 删除序列号和回收序列号
  915. * @param actionType
  916. * @throws Exception
  917. */
  918. @Override
  919. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  920. public void deleteOrCancelSerialNumber(String actionType, DepotHead depotHead, Long headerId) throws Exception {
  921. if(actionType.equals("update")) {
  922. User userInfo = userService.getCurrentUser();
  923. if(BusinessConstants.DEPOTHEAD_TYPE_IN.equals(depotHead.getType())){
  924. //入库逻辑
  925. String number = depotHead.getNumber();
  926. SerialNumberExample example = new SerialNumberExample();
  927. example.createCriteria().andInBillNoEqualTo(number);
  928. serialNumberService.deleteByExample(example);
  929. } else if(BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(depotHead.getType())){
  930. //出库逻辑
  931. DepotItemExample example = new DepotItemExample();
  932. example.createCriteria().andHeaderIdEqualTo(headerId).andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_DELETED);
  933. List<DepotItem> depotItemList = depotItemMapper.selectByExample(example);
  934. if(null != depotItemList && depotItemList.size() > 0){
  935. for (DepotItem depotItem : depotItemList){
  936. if(StringUtil.isNotEmpty(depotItem.getSnList())){
  937. serialNumberService.cancelSerialNumber(depotItem.getMaterialId(), depotHead.getNumber(), (depotItem.getBasicNumber() == null ? 0 : depotItem.getBasicNumber()).intValue(), userInfo);
  938. }
  939. }
  940. }
  941. }
  942. }
  943. }
  944. /**
  945. * 针对组装单、拆卸单校验是否存在组合件和普通子件
  946. * @param rowArr
  947. * @param subType
  948. */
  949. @Override
  950. public void checkAssembleWithMaterialType(JSONArray rowArr, String subType) {
  951. if(BusinessConstants.SUB_TYPE_ASSEMBLE.equals(subType) ||
  952. BusinessConstants.SUB_TYPE_DISASSEMBLE.equals(subType)) {
  953. if(rowArr.size() > 1) {
  954. JSONObject firstRowObj = JSONObject.parseObject(rowArr.getString(0));
  955. JSONObject secondRowObj = JSONObject.parseObject(rowArr.getString(1));
  956. String firstMaterialType = firstRowObj.getString("mType");
  957. String secondMaterialType = secondRowObj.getString("mType");
  958. if(!"组合件".equals(firstMaterialType) || !"普通子件".equals(secondMaterialType)) {
  959. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_CODE,
  960. String.format(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_MSG));
  961. }
  962. } else {
  963. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_CODE,
  964. String.format(ExceptionConstants.DEPOT_HEAD_CHECK_ASSEMBLE_EMPTY_MSG));
  965. }
  966. }
  967. }
  968. /**
  969. * 更新商品的价格
  970. * @param meId
  971. * @param subType
  972. * @param rowObj
  973. */
  974. @Override
  975. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  976. public void updateMaterialExtendPrice(Long meId, String subType, String billType, JSONObject rowObj) throws Exception {
  977. if(systemConfigService.getUpdateUnitPriceFlag()) {
  978. if (StringUtil.isExist(rowObj.get("unitPrice"))) {
  979. BigDecimal unitPrice = rowObj.getBigDecimal("unitPrice");
  980. MaterialExtend materialExtend = new MaterialExtend();
  981. materialExtend.setId(meId);
  982. if(BusinessConstants.SUB_TYPE_PURCHASE.equals(subType)) {
  983. materialExtend.setPurchaseDecimal(unitPrice);
  984. }
  985. if(BusinessConstants.SUB_TYPE_SALES.equals(subType)) {
  986. materialExtend.setWholesaleDecimal(unitPrice);
  987. }
  988. if(BusinessConstants.SUB_TYPE_RETAIL.equals(subType)) {
  989. materialExtend.setCommodityDecimal(unitPrice);
  990. }
  991. //其它入库-生产入库的情况更新采购单价
  992. if(BusinessConstants.SUB_TYPE_OTHER.equals(subType)) {
  993. if(BusinessConstants.BILL_TYPE_PRODUCE_IN.equals(billType)) {
  994. materialExtend.setPurchaseDecimal(unitPrice);
  995. }
  996. }
  997. materialExtendService.updateMaterialExtend(materialExtend);
  998. }
  999. }
  1000. }
  1001. @Override
  1002. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1003. public List<DepotItemStockWarningCount> findStockWarningCount(Integer offset, Integer rows, String materialParam, List<Long> depotList, List<Long> categoryList) {
  1004. List<DepotItemStockWarningCount> list = null;
  1005. try{
  1006. list =depotItemMapperEx.findStockWarningCount(offset, rows, materialParam, depotList, categoryList);
  1007. }catch(Exception e){
  1008. JshException.readFail(logger, e);
  1009. }
  1010. return list;
  1011. }
  1012. @Override
  1013. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1014. public int findStockWarningCountTotal(String materialParam, List<Long> depotList, List<Long> categoryList) {
  1015. int result = 0;
  1016. try{
  1017. result =depotItemMapperEx.findStockWarningCountTotal(materialParam, depotList, categoryList);
  1018. }catch(Exception e){
  1019. JshException.readFail(logger, e);
  1020. }
  1021. return result;
  1022. }
  1023. /**
  1024. * 库存统计-sku
  1025. * @param depotId
  1026. * @param meId
  1027. * @param beginTime
  1028. * @param endTime
  1029. * @return
  1030. */
  1031. @Override
  1032. public BigDecimal getSkuStockByParam(Long depotId, Long meId, String beginTime, String endTime) throws Exception {
  1033. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  1034. Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
  1035. List<Long> depotList = depotService.parseDepotList(depotId);
  1036. //盘点复盘后数量的变动
  1037. BigDecimal stockCheckSum = depotItemMapperEx.getSkuStockCheckSumByDepotList(depotList, meId, forceFlag, beginTime, endTime);
  1038. DepotItemVo4Stock stockObj = depotItemMapperEx.getSkuStockByParamWithDepotList(depotList, meId, forceFlag, inOutManageFlag, beginTime, endTime);
  1039. BigDecimal stockSum = BigDecimal.ZERO;
  1040. if(stockObj!=null) {
  1041. BigDecimal inTotal = stockObj.getInTotal();
  1042. BigDecimal transfInTotal = stockObj.getTransfInTotal();
  1043. BigDecimal assemInTotal = stockObj.getAssemInTotal();
  1044. BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
  1045. BigDecimal outTotal = stockObj.getOutTotal();
  1046. BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
  1047. BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
  1048. BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
  1049. stockSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal)
  1050. .subtract(outTotal).subtract(transfOutTotal).subtract(assemOutTotal).subtract(disAssemOutTotal);
  1051. }
  1052. return stockCheckSum.add(stockSum);
  1053. }
  1054. /**
  1055. * 库存统计-单仓库
  1056. * @param depotId
  1057. * @param mId
  1058. * @param beginTime
  1059. * @param endTime
  1060. * @return
  1061. */
  1062. @Override
  1063. public BigDecimal getStockByParam(Long depotId, Long mId, String beginTime, String endTime) throws Exception {
  1064. List<Long> depotList = depotService.parseDepotList(depotId);
  1065. return getStockByParamWithDepotList(depotList, mId, beginTime, endTime);
  1066. }
  1067. /**
  1068. * 库存统计-多仓库
  1069. * @param depotList
  1070. * @param mId
  1071. * @param beginTime
  1072. * @param endTime
  1073. * @return
  1074. */
  1075. @Override
  1076. public BigDecimal getStockByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception {
  1077. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  1078. Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
  1079. //初始库存
  1080. BigDecimal initStock = materialService.getInitStockByMidAndDepotList(depotList, mId);
  1081. //盘点复盘后数量的变动
  1082. BigDecimal stockCheckSum = depotItemMapperEx.getStockCheckSumByDepotList(depotList, mId, forceFlag, beginTime, endTime);
  1083. //表单的数量
  1084. DepotItemVo4Stock stockObj = depotItemMapperEx.getStockByParamWithDepotList(depotList, mId, forceFlag, inOutManageFlag, beginTime, endTime);
  1085. BigDecimal stockSum = BigDecimal.ZERO;
  1086. //获取商品子表单的库存总数
  1087. BigDecimal inventory = materialExtendMapper.getInventorySumByDepotAndMid(depotList,mId);
  1088. if(stockObj!=null) {
  1089. BigDecimal inTotal = stockObj.getInTotal();
  1090. BigDecimal transfInTotal = stockObj.getTransfInTotal();
  1091. BigDecimal assemInTotal = stockObj.getAssemInTotal();
  1092. BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
  1093. BigDecimal outTotal = stockObj.getOutTotal();
  1094. BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
  1095. BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
  1096. BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
  1097. stockSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal)
  1098. .subtract(outTotal).subtract(transfOutTotal).subtract(assemOutTotal).subtract(disAssemOutTotal);
  1099. }
  1100. return initStock.add(stockCheckSum).add(inventory).add(stockSum);
  1101. }
  1102. /**
  1103. * 统计时间段内的入库和出库数量-多仓库
  1104. * @param depotList
  1105. * @param mId
  1106. * @param beginTime
  1107. * @param endTime
  1108. * @return
  1109. */
  1110. @Override
  1111. public Map<String, BigDecimal> getIntervalMapByParamWithDepotList(List<Long> depotList, Long mId, String beginTime, String endTime) throws Exception {
  1112. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  1113. Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
  1114. Map<String,BigDecimal> intervalMap = new HashMap<>();
  1115. BigDecimal inSum = BigDecimal.ZERO;
  1116. BigDecimal outSum = BigDecimal.ZERO;
  1117. //盘点复盘后数量的变动
  1118. BigDecimal stockCheckSum = depotItemMapperEx.getStockCheckSumByDepotList(depotList, mId, forceFlag, beginTime, endTime);
  1119. DepotItemVo4Stock stockObj = depotItemMapperEx.getStockByParamWithDepotList(depotList, mId, forceFlag, inOutManageFlag, beginTime, endTime);
  1120. if(stockObj!=null) {
  1121. BigDecimal inTotal = stockObj.getInTotal();
  1122. BigDecimal transfInTotal = stockObj.getTransfInTotal();
  1123. BigDecimal assemInTotal = stockObj.getAssemInTotal();
  1124. BigDecimal disAssemInTotal = stockObj.getDisAssemInTotal();
  1125. inSum = inTotal.add(transfInTotal).add(assemInTotal).add(disAssemInTotal);
  1126. BigDecimal outTotal = stockObj.getOutTotal();
  1127. BigDecimal transfOutTotal = stockObj.getTransfOutTotal();
  1128. BigDecimal assemOutTotal = stockObj.getAssemOutTotal();
  1129. BigDecimal disAssemOutTotal = stockObj.getDisAssemOutTotal();
  1130. outSum = outTotal.add(transfOutTotal).add(assemOutTotal).add(disAssemOutTotal);
  1131. }
  1132. if(stockCheckSum.compareTo(BigDecimal.ZERO)>0) {
  1133. inSum = inSum.add(stockCheckSum);
  1134. } else {
  1135. //盘点复盘数量为负数代表出库
  1136. outSum = outSum.subtract(stockCheckSum);
  1137. }
  1138. intervalMap.put("inSum", inSum);
  1139. intervalMap.put("outSum", outSum);
  1140. return intervalMap;
  1141. }
  1142. /**
  1143. * 根据单据明细来批量更新当前库存
  1144. * @param depotItem 单据子表明细
  1145. */
  1146. @Override
  1147. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1148. public void updateCurrentStock(DepotItem depotItem) throws Exception {
  1149. updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getDepotId());
  1150. if(depotItem.getAnotherDepotId()!=null){
  1151. updateCurrentStockFun(depotItem.getMaterialId(), depotItem.getAnotherDepotId());
  1152. }
  1153. }
  1154. /**
  1155. * 根据单据明细来批量更新当前成本价
  1156. * @param depotItem
  1157. */
  1158. @Override
  1159. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1160. public void updateCurrentUnitPrice(DepotItem depotItem) throws Exception {
  1161. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  1162. Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
  1163. //查询多单位信息
  1164. Unit unitInfo = materialService.findUnit(depotItem.getMaterialId());
  1165. List<DepotItemVo4DetailByTypeAndMId> itemList = findDetailByDepotIdsAndMaterialIdList(null, forceFlag, inOutManageFlag, depotItem.getSku(),
  1166. depotItem.getBatchNumber(), null, null, null, depotItem.getMaterialId(), null, null);
  1167. Collections.reverse(itemList); //倒序之后变成按时间从前往后排序
  1168. BigDecimal currentNumber = BigDecimal.ZERO;
  1169. BigDecimal currentUnitPrice = BigDecimal.ZERO;
  1170. BigDecimal currentAllPrice = BigDecimal.ZERO;
  1171. for(DepotItemVo4DetailByTypeAndMId item: itemList) {
  1172. BigDecimal basicNumber = item.getBnum()!=null?item.getBnum():BigDecimal.ZERO;
  1173. //数量*单价 另外计算新的成本价
  1174. BigDecimal allPrice = unitService.parseAllPriceByUnit(item.getAllPrice()!=null?item.getAllPrice():BigDecimal.ZERO, unitInfo, item.getMaterialUnit());
  1175. if(basicNumber.compareTo(BigDecimal.ZERO)!=0 && allPrice.compareTo(BigDecimal.ZERO)!=0) {
  1176. //入库
  1177. if (BusinessConstants.DEPOTHEAD_TYPE_IN.equals(item.getType())) {
  1178. //零售退货、销售退货
  1179. if (BusinessConstants.SUB_TYPE_RETAIL_RETURN.equals(item.getSubType()) || BusinessConstants.SUB_TYPE_SALES_RETURN.equals(item.getSubType())) {
  1180. //数量*当前的成本单价
  1181. currentNumber = currentNumber.add(basicNumber);
  1182. currentAllPrice = currentAllPrice.add(basicNumber.multiply(currentUnitPrice));
  1183. } else {
  1184. currentAllPrice = currentAllPrice.add(allPrice);
  1185. currentNumber = currentNumber.add(basicNumber);
  1186. //只有当前库存总金额和当前库存数量都大于0才计算移动平均价
  1187. if (currentAllPrice.compareTo(BigDecimal.ZERO) > 0 && currentNumber.compareTo(BigDecimal.ZERO) > 0) {
  1188. currentUnitPrice = currentAllPrice.divide(currentNumber, 2, BigDecimal.ROUND_HALF_UP);
  1189. } else {
  1190. currentUnitPrice = item.getUnitPrice();
  1191. }
  1192. }
  1193. }
  1194. //出库
  1195. if (BusinessConstants.DEPOTHEAD_TYPE_OUT.equals(item.getType())) {
  1196. //采购退货
  1197. if (BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(item.getSubType())) {
  1198. currentAllPrice = currentAllPrice.add(allPrice);
  1199. currentNumber = currentNumber.add(basicNumber);
  1200. //只有当前库存总金额和当前库存数量都大于0才计算移动平均价
  1201. if (currentAllPrice.compareTo(BigDecimal.ZERO) > 0 && currentNumber.compareTo(BigDecimal.ZERO) > 0) {
  1202. currentUnitPrice = currentAllPrice.divide(currentNumber, 2, BigDecimal.ROUND_HALF_UP);
  1203. } else {
  1204. currentUnitPrice = item.getUnitPrice();
  1205. }
  1206. } else {
  1207. currentNumber = currentNumber.add(basicNumber);
  1208. //数量*当前的成本单价
  1209. currentAllPrice = currentAllPrice.add(basicNumber.multiply(currentUnitPrice));
  1210. }
  1211. }
  1212. //防止单价金额溢出
  1213. if(currentUnitPrice.compareTo(BigDecimal.valueOf(100000000))>0 || currentUnitPrice.compareTo(BigDecimal.valueOf(-100000000))<0) {
  1214. currentUnitPrice = BigDecimal.ZERO;
  1215. }
  1216. }
  1217. }
  1218. //更新实时库存中的当前单价
  1219. materialCurrentStockMapperEx.updateUnitPriceByMId(currentUnitPrice, depotItem.getMaterialId());
  1220. }
  1221. /**
  1222. * 根据商品和仓库来更新当前库存
  1223. * @param mId
  1224. * @param dId
  1225. */
  1226. @Override
  1227. public void updateCurrentStockFun(Long mId, Long dId) throws Exception {
  1228. if(mId!=null && dId!=null) {
  1229. MaterialCurrentStockExample example = new MaterialCurrentStockExample();
  1230. //条件 添加商品id,仓库id ,删除状态
  1231. example.createCriteria().andMaterialIdEqualTo(mId).andDepotIdEqualTo(dId)
  1232. .andDeleteFlagNotEqualTo(BusinessConstants.DELETE_FLAG_EXISTS);
  1233. //获取商品当前库存信息
  1234. List<MaterialCurrentStock> list = materialCurrentStockMapper.selectByExample(example);
  1235. MaterialCurrentStock materialCurrentStock = new MaterialCurrentStock();
  1236. materialCurrentStock.setMaterialId(mId);
  1237. materialCurrentStock.setDepotId(dId);
  1238. //设置当前库存数量
  1239. materialCurrentStock.setCurrentNumber(getStockByParam(dId,mId,null,null));
  1240. if(list!=null && list.size()>0) {
  1241. //当前库存信息存在,修改当前库存信息
  1242. Long mcsId = list.get(0).getId();
  1243. materialCurrentStock.setId(mcsId);
  1244. materialCurrentStockMapper.updateByPrimaryKeySelective(materialCurrentStock);
  1245. } else {
  1246. //当前库存信息不存在,新增当前库存信息
  1247. materialCurrentStockMapper.insertSelective(materialCurrentStock);
  1248. }
  1249. }
  1250. }
  1251. @Override
  1252. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1253. public BigDecimal getFinishNumber(Long meId, Long id, Long headerId, Unit unitInfo, String materialUnit, String linkType) {
  1254. Long linkId = id;
  1255. String goToType = "";
  1256. DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(headerId);
  1257. String linkStr = depotHead.getNumber(); //订单号
  1258. if("purchase".equals(linkType)) {
  1259. //针对以销定购的情况
  1260. if(BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
  1261. goToType = BusinessConstants.SUB_TYPE_PURCHASE_ORDER;
  1262. }
  1263. } else if("other".equals(linkType)) {
  1264. //采购入库、采购退货、销售出库、销售退货都转其它入库
  1265. if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())
  1266. || BusinessConstants.SUB_TYPE_PURCHASE_RETURN.equals(depotHead.getSubType())
  1267. || BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())
  1268. || BusinessConstants.SUB_TYPE_SALES_RETURN.equals(depotHead.getSubType())) {
  1269. goToType = BusinessConstants.SUB_TYPE_OTHER;
  1270. }
  1271. } else if("basic".equals(linkType)) {
  1272. //采购订单转采购入库
  1273. if(BusinessConstants.SUB_TYPE_PURCHASE_ORDER.equals(depotHead.getSubType())) {
  1274. goToType = BusinessConstants.SUB_TYPE_PURCHASE;
  1275. }
  1276. //销售订单转销售出库
  1277. if(BusinessConstants.SUB_TYPE_SALES_ORDER.equals(depotHead.getSubType())) {
  1278. goToType = BusinessConstants.SUB_TYPE_SALES;
  1279. }
  1280. //采购入库转采购退货
  1281. if(BusinessConstants.SUB_TYPE_PURCHASE.equals(depotHead.getSubType())) {
  1282. goToType = BusinessConstants.SUB_TYPE_PURCHASE_RETURN;
  1283. }
  1284. //销售出库转销售退货
  1285. if(BusinessConstants.SUB_TYPE_SALES.equals(depotHead.getSubType())) {
  1286. goToType = BusinessConstants.SUB_TYPE_SALES_RETURN;
  1287. }
  1288. }
  1289. String noType = "normal";
  1290. if(BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())) {
  1291. noType = "apply";
  1292. }
  1293. BigDecimal count = depotItemMapperEx.getFinishNumber(meId, linkId, linkStr, noType, goToType);
  1294. //根据多单位情况进行数量的转换
  1295. if(materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio()!=null && unitInfo.getRatio().compareTo(BigDecimal.ZERO)!=0) {
  1296. count = count.divide(unitInfo.getRatio(),2,BigDecimal.ROUND_HALF_UP);
  1297. }
  1298. if(materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo()!=null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO)!=0) {
  1299. count = count.divide(unitInfo.getRatioTwo(),2,BigDecimal.ROUND_HALF_UP);
  1300. }
  1301. if(materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree()!=null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO)!=0) {
  1302. count = count.divide(unitInfo.getRatioThree(),2,BigDecimal.ROUND_HALF_UP);
  1303. }
  1304. return count;
  1305. }
  1306. /**
  1307. * 除去此单据之外的已入库|已出库|已转采购
  1308. * @param currentSubType
  1309. * @param meId
  1310. * @param linkId
  1311. * @param preHeaderId
  1312. * @param currentHeaderId
  1313. * @param unitInfo
  1314. * @param materialUnit
  1315. * @return
  1316. */
  1317. @Override
  1318. @Transactional(value = "transactionManager", rollbackFor = Exception.class)
  1319. public BigDecimal getRealFinishNumber(String currentSubType, Long meId, Long linkId, Long preHeaderId, Long currentHeaderId, Unit unitInfo, String materialUnit) {
  1320. String goToType = currentSubType;
  1321. DepotHead depotHead =depotHeadMapper.selectByPrimaryKey(preHeaderId);
  1322. String linkStr = depotHead.getNumber(); //订单号
  1323. String linkType = "normal";
  1324. if(BusinessConstants.SUB_TYPE_PURCHASE_APPLY.equals(depotHead.getSubType())) {
  1325. linkType = "apply";
  1326. }
  1327. BigDecimal count = depotItemMapperEx.getRealFinishNumber(meId, linkId, linkStr, linkType, currentHeaderId, goToType);
  1328. //根据多单位情况进行数量的转换
  1329. if(materialUnit.equals(unitInfo.getOtherUnit()) && unitInfo.getRatio()!=null && unitInfo.getRatio().compareTo(BigDecimal.ZERO)!=0) {
  1330. count = count.divide(unitInfo.getRatio(),2,BigDecimal.ROUND_HALF_UP);
  1331. }
  1332. if(materialUnit.equals(unitInfo.getOtherUnitTwo()) && unitInfo.getRatioTwo()!=null && unitInfo.getRatioTwo().compareTo(BigDecimal.ZERO)!=0) {
  1333. count = count.divide(unitInfo.getRatioTwo(),2,BigDecimal.ROUND_HALF_UP);
  1334. }
  1335. if(materialUnit.equals(unitInfo.getOtherUnitThree()) && unitInfo.getRatioThree()!=null && unitInfo.getRatioThree().compareTo(BigDecimal.ZERO)!=0) {
  1336. count = count.divide(unitInfo.getRatioThree(),2,BigDecimal.ROUND_HALF_UP);
  1337. }
  1338. return count;
  1339. }
  1340. @Override
  1341. public List<DepotItemVoBatchNumberList> getBatchNumberList(String number, String name, Long depotId, String barCode,
  1342. String batchNumber, Boolean forceFlag, Boolean inOutManageFlag) throws Exception {
  1343. List<DepotItemVoBatchNumberList> reslist = new ArrayList<>();
  1344. List<DepotItemVoBatchNumberList> list = depotItemMapperEx.getBatchNumberList(StringUtil.toNull(number), name,
  1345. depotId, barCode, batchNumber, forceFlag, inOutManageFlag);
  1346. for(DepotItemVoBatchNumberList bn: list) {
  1347. if(bn.getTotalNum()!=null && bn.getTotalNum().compareTo(BigDecimal.ZERO)>0) {
  1348. bn.setExpirationDateStr(Tools.parseDateToStr(bn.getExpirationDate()));
  1349. if(bn.getUnitId()!=null) {
  1350. Unit unit = unitService.getUnit(bn.getUnitId());
  1351. String commodityUnit = bn.getCommodityUnit();
  1352. bn.setTotalNum(unitService.parseStockByUnit(bn.getTotalNum(), unit, commodityUnit));
  1353. }
  1354. reslist.add(bn);
  1355. }
  1356. }
  1357. return reslist;
  1358. }
  1359. /**
  1360. * 查询某个批号的商品库存
  1361. * @param depotId
  1362. * @param barCode
  1363. * @param batchNumber
  1364. * @return
  1365. * @throws Exception
  1366. */
  1367. @Override
  1368. public BigDecimal getOneBatchNumberStock(Long depotId, String barCode, String batchNumber) throws Exception {
  1369. BigDecimal totalNum = BigDecimal.ZERO;
  1370. Boolean forceFlag = systemConfigService.getForceApprovalFlag();
  1371. Boolean inOutManageFlag = systemConfigService.getInOutManageFlag();
  1372. List<DepotItemVoBatchNumberList> list = depotItemMapperEx.getBatchNumberList(null, null,
  1373. depotId, barCode, batchNumber, forceFlag, inOutManageFlag);
  1374. if(list!=null && list.size()>0) {
  1375. DepotItemVoBatchNumberList bn = list.get(0);
  1376. totalNum = bn.getTotalNum();
  1377. if(bn.getTotalNum()!=null && bn.getTotalNum().compareTo(BigDecimal.ZERO)>0) {
  1378. if(bn.getUnitId()!=null) {
  1379. Unit unit = unitService.getUnit(bn.getUnitId());
  1380. String commodityUnit = bn.getCommodityUnit();
  1381. totalNum = unitService.parseStockByUnit(bn.getTotalNum(), unit, commodityUnit);
  1382. }
  1383. }
  1384. }
  1385. return totalNum;
  1386. }
  1387. @Override
  1388. public Long getCountByMaterialAndDepot(Long mId, Long depotId) {
  1389. return depotItemMapperEx.getCountByMaterialAndDepot(mId, depotId);
  1390. }
  1391. @Override
  1392. public JSONObject parseMapByExcelData(String batchNumber, List<Map<String, String>> detailList, String prefixNo) throws Exception {
  1393. JSONObject map = new JSONObject();
  1394. JSONArray arr = new JSONArray();
  1395. //根据批次号获取商品数据
  1396. List<MaterialVo4Unit> list = depotItemMapperEx.getBillItemByParam(batchNumber);
  1397. //商品数据集合
  1398. Map<String, MaterialVo4Unit> materialMap = new HashMap<>();
  1399. //仓库集合
  1400. Map<String, Long> depotMap = new HashMap<>();
  1401. for (MaterialVo4Unit material: list) {
  1402. materialMap.put(material.getBatchNumber(), material);
  1403. }
  1404. //获取当前用户所有仓库
  1405. JSONArray depotArr = depotService.findDepotByCurrentUser();
  1406. for (Object depotObj: depotArr) {
  1407. if(depotObj!=null) {
  1408. JSONObject depotObject = JSONObject.parseObject(depotObj.toString());
  1409. depotMap.put(depotObject.getString("depotName"), depotObject.getLong("id"));
  1410. }
  1411. }
  1412. for (Map<String, String> detailMap: detailList) {
  1413. JSONObject item = new JSONObject();
  1414. String barCode = detailMap.get("batchNumber");
  1415. if(StringUtil.isNotEmpty(barCode)) {
  1416. MaterialVo4Unit m = materialMap.get(barCode);
  1417. if(m!=null) {
  1418. //判断仓库是否存在
  1419. String depotName = detailMap.get("depotName");
  1420. if(StringUtil.isNotEmpty(depotName)) {
  1421. if(depotMap.get(depotName)!=null) {
  1422. item.put("depotName", depotName);
  1423. item.put("depotId", depotMap.get(depotName));
  1424. } else {
  1425. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_DEPOTNAME_IS_NOT_EXIST_CODE,
  1426. String.format(ExceptionConstants.DEPOT_ITEM_DEPOTNAME_IS_NOT_EXIST_MSG, depotName));
  1427. }
  1428. }
  1429. item.put("batchNumber", barCode);
  1430. item.put("name", m.getName());
  1431. item.put("standard", m.getStandard());
  1432. if(StringUtil.isNotEmpty(m.getModel())) {
  1433. item.put("model", m.getModel());
  1434. }
  1435. if(StringUtil.isNotEmpty(m.getColor())) {
  1436. item.put("color", m.getColor());
  1437. }
  1438. if(StringUtil.isNotEmpty(m.getSku())) {
  1439. item.put("sku", m.getSku());
  1440. }
  1441. BigDecimal stock = BigDecimal.ZERO;
  1442. if(StringUtil.isNotEmpty(m.getSku())){
  1443. stock = getSkuStockByParam(null, m.getMeId(),null,null);
  1444. } else {
  1445. stock = getCurrentStockByParam(null, m.getId());
  1446. }
  1447. item.put("stock", stock);
  1448. item.put("unit", m.getCommodityUnit());
  1449. BigDecimal operNumber = BigDecimal.ZERO;
  1450. BigDecimal unitPrice = BigDecimal.ZERO;
  1451. BigDecimal taxRate = BigDecimal.ZERO;
  1452. if(StringUtil.isNotEmpty(detailMap.get("num"))) {
  1453. operNumber = new BigDecimal(detailMap.get("num"));
  1454. }
  1455. if(StringUtil.isNotEmpty(detailMap.get("unitPrice"))) {
  1456. unitPrice = new BigDecimal(detailMap.get("unitPrice"));
  1457. } else {
  1458. if("CGDD".equals(prefixNo)) {
  1459. unitPrice = m.getPurchaseDecimal();
  1460. } else if("XSDD".equals(prefixNo)) {
  1461. unitPrice = m.getWholesaleDecimal();
  1462. }
  1463. }
  1464. if(StringUtil.isNotEmpty(detailMap.get("taxRate"))) {
  1465. taxRate = new BigDecimal(detailMap.get("taxRate"));
  1466. }
  1467. String remark = detailMap.get("remark");
  1468. item.put("operNumber", operNumber);
  1469. item.put("unitPrice", unitPrice);
  1470. BigDecimal allPrice = BigDecimal.ZERO;
  1471. if(unitPrice!=null && unitPrice.compareTo(BigDecimal.ZERO)!=0) {
  1472. allPrice = unitPrice.multiply(operNumber);
  1473. }
  1474. BigDecimal taxMoney = BigDecimal.ZERO;
  1475. if(taxRate.compareTo(BigDecimal.ZERO) != 0) {
  1476. taxMoney = taxRate.multiply(allPrice).divide(BigDecimal.valueOf(100), 2, BigDecimal.ROUND_HALF_UP);
  1477. }
  1478. BigDecimal taxLastMoney = allPrice.add(taxMoney);
  1479. item.put("allPrice", allPrice);
  1480. item.put("taxRate", taxRate);
  1481. item.put("taxMoney", taxMoney);
  1482. item.put("taxLastMoney", taxLastMoney);
  1483. item.put("remark", remark);
  1484. arr.add(item);
  1485. } else {
  1486. throw new BusinessRunTimeException(ExceptionConstants.DEPOT_ITEM_BARCODE_IS_NOT_EXIST_CODE,
  1487. String.format(ExceptionConstants.DEPOT_ITEM_BARCODE_IS_NOT_EXIST_MSG, barCode));
  1488. }
  1489. }
  1490. }
  1491. map.put("rows", arr);
  1492. return map;
  1493. }
  1494. @Override
  1495. public BigDecimal getLastUnitPriceByParam(Long organId, Long meId, String prefixNo) {
  1496. String type = "";
  1497. String subType = "";
  1498. if("XSDD".equals(prefixNo)) {
  1499. type = "其它";
  1500. subType = "销售订单";
  1501. } else if("XSCK".equals(prefixNo)) {
  1502. type = "出库";
  1503. subType = "销售";
  1504. } else if("XSTH".equals(prefixNo)) {
  1505. type = "入库";
  1506. subType = "销售退货";
  1507. } else if("QTCK".equals(prefixNo)) {
  1508. type = "出库";
  1509. subType = "其它";
  1510. }
  1511. return depotItemMapperEx.getLastUnitPriceByParam(organId, meId, type, subType);
  1512. }
  1513. @Override
  1514. public BigDecimal getCurrentStockByParam(Long depotId, Long mId) {
  1515. BigDecimal stock = depotItemMapperEx.getCurrentStockByParam(depotId, mId);
  1516. return stock!=null? stock: BigDecimal.ZERO;
  1517. }
  1518. /**
  1519. * 获取扩展信息
  1520. *
  1521. * @return
  1522. */
  1523. @Override
  1524. public String getOtherInfo(String[] mpArr, DepotItemVo4WithInfoEx diEx)throws Exception {
  1525. String materialOther = "";
  1526. for (int i = 0; i < mpArr.length; i++) {
  1527. if (mpArr[i].equals("自定义1")) {
  1528. materialOther = materialOther + ((diEx.getMOtherField1() == null || diEx.getMOtherField1().equals("")) ? "" : "(" + diEx.getMOtherField1() + ")");
  1529. }
  1530. if (mpArr[i].equals("自定义2")) {
  1531. materialOther = materialOther + ((diEx.getMOtherField2() == null || diEx.getMOtherField2().equals("")) ? "" : "(" + diEx.getMOtherField2() + ")");
  1532. }
  1533. if (mpArr[i].equals("自定义3")) {
  1534. materialOther = materialOther + ((diEx.getMOtherField3() == null || diEx.getMOtherField3().equals("")) ? "" : "(" + diEx.getMOtherField3() + ")");
  1535. }
  1536. }
  1537. return materialOther;
  1538. }
  1539. }