GoodsRepository.cs 113 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795
  1. using Aspose.Cells;
  2. using AutoMapper;
  3. using EyeSoft.Runtime.InteropServices;
  4. using Newtonsoft.Json;
  5. using OASystem.Domain;
  6. using OASystem.Domain.Dtos.PersonnelModule;
  7. using OASystem.Domain.Entities.Groups;
  8. using OASystem.Domain.Entities.PersonnelModule;
  9. using OASystem.Domain.ViewModels.PersonnelModule;
  10. using OASystem.Infrastructure.Tools;
  11. namespace OASystem.Infrastructure.Repositories.PersonnelModule
  12. {
  13. /// <summary>
  14. /// 物品进销存
  15. /// 仓储
  16. /// </summary>
  17. public class GoodsRepository : BaseRepository<Pm_GoodsInfo, GoodsInfoView>
  18. {
  19. private readonly IMapper _mapper;
  20. private JsonView _jv;
  21. private string _url;
  22. private string _excelPath;
  23. private List<int> _goodsTypeIds; //多部门审核物品类型Id
  24. public GoodsRepository(SqlSugarClient sqlSugar, IMapper mapper) : base(sqlSugar)
  25. {
  26. _mapper = mapper;
  27. _jv = new JsonView() { Code = StatusCodes.Status400BadRequest, Msg = "操作失败!" };
  28. _url = AppSettingsHelper.Get("ExcelBaseUrl");
  29. _excelPath = $"{AppSettingsHelper.Get("ExcelBasePath")}";
  30. if (!Directory.Exists(_excelPath))
  31. {
  32. Directory.CreateDirectory(_excelPath);
  33. }
  34. _goodsTypeIds = new List<int>() {
  35. 1423, //1423 贵重物品
  36. };
  37. }
  38. /// <summary>
  39. /// 基础数据
  40. /// </summary>
  41. /// <returns></returns>
  42. public async Task<JsonView> InitDataSource()
  43. {
  44. var typeData = await _sqlSugar.Queryable<GoodsTypeView>()
  45. .Includes(x => x.SubTypeItems.Where(z => z.IsDel == 0)
  46. //.Select(z => new {
  47. // z.Id,
  48. // z.STid,
  49. // z.Name
  50. //})
  51. .ToList())
  52. .Where(x => x.IsDel == 0 &&
  53. x.Remark.Equals("GoodsType"))
  54. //.Select(x => new {
  55. // x.Id,
  56. // x.Name,
  57. // x.SubTypeItems
  58. //})
  59. .ToListAsync();
  60. var groupData = await _sqlSugar.Queryable<Grp_DelegationInfo>()
  61. .Where(x => x.IsDel == 0)
  62. .Select(x => new
  63. {
  64. id = x.Id,
  65. groupName = x.TeamName
  66. })
  67. .OrderByDescending(x => x.id)
  68. .ToListAsync();
  69. groupData.Insert(0, new { id = 0, groupName = "其他物资(公司内部物资)" });
  70. groupData.Insert(0, new { id = -1, groupName = "拜访客户所使用的物资" });
  71. groupData.Insert(0, new { id = -2, groupName = "库存调整" });
  72. groupData.Insert(0, new { id = -3, groupName = "全部团组" });
  73. var userData = await _sqlSugar.Queryable<Sys_Users>()
  74. .Where(x => x.IsDel == 0)
  75. .Select(x => new
  76. {
  77. x.Id,
  78. UserName = x.CnName,
  79. })
  80. .ToListAsync();
  81. //库存状态
  82. var stockStatus = new List<dynamic>() {
  83. new { Value = -1, Text = "全部" },
  84. new { Value = 0, Text = "待确认" },
  85. new { Value = 1, Text = "部分确认" },
  86. new { Value = 2, Text = "已确认" },
  87. new { Value = 3, Text = "已拒绝" },
  88. };
  89. //领用出库状态
  90. var receiveStatus = new List<dynamic>() {
  91. new { Value = -1, Text = "全部" },
  92. new { Value = GoodsAuditEnum.Pending, Text = GoodsAuditEnum.Pending.GetEnumDescription() },
  93. new { Value = GoodsAuditEnum.Approved, Text = GoodsAuditEnum.Approved.GetEnumDescription() },
  94. new { Value = GoodsAuditEnum.UnApproved, Text = GoodsAuditEnum.UnApproved.GetEnumDescription() },
  95. new { Value = GoodsAuditEnum.OutPending, Text = GoodsAuditEnum.OutPending.GetEnumDescription() },
  96. new { Value = GoodsAuditEnum.OutConfirming, Text = GoodsAuditEnum.OutConfirming.GetEnumDescription() },
  97. new { Value = GoodsAuditEnum.OutConfirmed, Text = GoodsAuditEnum.OutConfirmed.GetEnumDescription() },
  98. new { Value = GoodsAuditEnum.OutRejected, Text = GoodsAuditEnum.OutRejected.GetEnumDescription() },
  99. };
  100. _jv.Code = StatusCodes.Status200OK;
  101. _jv.Data = new { goodsTypeData = typeData, stockStatus = stockStatus, receiveStatus = receiveStatus, groupNameData = groupData, userNameData = userData };
  102. _jv.Msg = $"操作成功";
  103. return _jv;
  104. }
  105. /// <summary>
  106. /// 物品列表
  107. /// </summary>
  108. /// <param name="dto"></param>
  109. /// <returns></returns>
  110. public async Task<JsonView> GoodsList(GoodsListDto dto)
  111. {
  112. var ids = new List<int>();
  113. if (!string.IsNullOrEmpty(dto.TypeIds))
  114. {
  115. var strArray = dto.TypeIds.Split(',');
  116. foreach (var str in strArray)
  117. {
  118. if (int.TryParse(str, out int id))
  119. {
  120. ids.Add(id);
  121. }
  122. }
  123. }
  124. var auditEnums = new List<GoodsAuditEnum>() {
  125. GoodsAuditEnum.Pending,
  126. GoodsAuditEnum.OutPending,
  127. GoodsAuditEnum.OutConfirming
  128. };
  129. RefAsync<int> total = 0;
  130. var data = await _sqlSugar.Queryable<GoodsListView>()
  131. //.Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && z1.AuditStatus == GoodsAuditEnum.Pending).ToList())
  132. .Includes(glv => glv.Receives.Where(z1 => z1.IsDel == 0 && auditEnums.Contains(z1.AuditStatus)).ToList())
  133. .Includes(glv => glv.TypeData)
  134. .Includes(glv => glv.UserData)
  135. .LeftJoin<Sys_SetData>((glv, sd) => glv.Type == sd.Id)
  136. .LeftJoin<Sys_Users>((glv, sd, u) => glv.LastUpdateUserId == u.Id)
  137. .Where(glv => glv.IsDel == 0)
  138. .WhereIF(ids.Count > 0, glv => ids.Contains(glv.Type))
  139. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), glv => glv.Name.Contains(dto.GoodsName))
  140. .OrderByDescending(glv => glv.LastUpdateTime)
  141. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  142. var view = data.Select(x => new
  143. {
  144. x.Id,
  145. x.Name,
  146. x.Type,
  147. TypeName = x.TypeData?.Name ?? string.Empty,
  148. LastUpdateUserName = x.UserData?.CnName ?? string.Empty,
  149. x.LastUpdateTime,
  150. StockQuantity = x.StockQuantity - x.WaitAuditQuantity,
  151. x.Unit,
  152. x.StockQuantityLabel,
  153. x.Remark
  154. }).ToList();
  155. _jv.Code = StatusCodes.Status200OK;
  156. _jv.Data = view;
  157. _jv.Count = total;
  158. _jv.Msg = $"操作成功";
  159. return _jv;
  160. }
  161. /// <summary>
  162. /// 物品Info
  163. /// </summary>
  164. /// <param name="portType"></param>
  165. /// <param name="id"></param>
  166. /// <returns></returns>
  167. public async Task<JsonView> GoodsInfo(int portType, int id)
  168. {
  169. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  170. .LeftJoin<Sys_SetData>((gi, sd) => gi.Type == sd.Id)
  171. .LeftJoin<Sys_Users>((gi, sd, u1) => gi.LastUpdateUserId == u1.Id)
  172. .LeftJoin<Sys_Users>((gi, sd, u1, u2) => gi.CreateUserId == u2.Id)
  173. .Where((gi, sd, u1, u2) => gi.IsDel == 0 && gi.Id == id)
  174. .Select((gi, sd, u1, u2) => new
  175. {
  176. gi.Id,
  177. gi.Name,
  178. ParentType = sd.STid,
  179. gi.Type,
  180. TypeName = sd.Name,
  181. gi.SQ_Total,
  182. gi.OQ_Total,
  183. gi.PriceTotal,
  184. gi.StockQuantity,
  185. gi.Unit,
  186. gi.Remark,
  187. LastUpdateUserName = u1.CnName,
  188. gi.LastUpdateTime,
  189. CreateUserName = u2.CnName,
  190. gi.CreateTime,
  191. })
  192. .FirstAsync();
  193. _jv.Code = StatusCodes.Status200OK;
  194. _jv.Data = data;
  195. _jv.Msg = $"操作成功";
  196. return _jv;
  197. }
  198. /// <summary>
  199. /// 物品 OP(Create Or Edit)
  200. /// </summary>
  201. /// <param name="dto"></param>
  202. /// <param name="currUserId"></param>
  203. /// <returns></returns>
  204. public async Task<JsonView> GoodsOp(GoodsOpDto dto, int currUserId)
  205. {
  206. var info = new Pm_GoodsInfo()
  207. {
  208. Id = dto.Id,
  209. Name = dto.Name,
  210. Type = dto.Type,
  211. SQ_Total = 0,
  212. OQ_Total = 0,
  213. PriceTotal = 0,
  214. StockQuantity = 0,
  215. Unit = dto.Unit,
  216. Remark = dto.Remark,
  217. LastUpdateUserId = currUserId,
  218. LastUpdateTime = DateTime.Now,
  219. CreateUserId = currUserId
  220. };
  221. if (dto.Id > 0) //Edit
  222. {
  223. var upd = await _sqlSugar.Updateable(info)
  224. .UpdateColumns(x => new
  225. {
  226. x.Name,
  227. x.Type,
  228. x.Unit,
  229. x.Remark,
  230. x.LastUpdateUserId,
  231. x.LastUpdateTime,
  232. })
  233. .ExecuteCommandAsync();
  234. if (upd > 0)
  235. {
  236. _jv.Msg = $"修改成功!";
  237. _jv.Code = StatusCodes.Status200OK;
  238. return _jv;
  239. }
  240. }
  241. else if (dto.Id < 1) //添加
  242. {
  243. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Name.Equals(info.Name));
  244. if (selectInfo != null)
  245. {
  246. _jv.Msg = $"“{info.Name}”该物品已存在,请勿重新添加!";
  247. return _jv;
  248. }
  249. var add = await _sqlSugar.Insertable(info).ExecuteCommandAsync();
  250. if (add > 0)
  251. {
  252. _jv.Msg = $"添加成功!";
  253. _jv.Code = StatusCodes.Status200OK;
  254. return _jv;
  255. }
  256. }
  257. return _jv;
  258. }
  259. /// <summary>
  260. /// 物品 Del
  261. /// </summary>
  262. /// <param name="id"></param>
  263. /// <param name="currUserId"></param>
  264. /// <returns></returns>
  265. public async Task<JsonView> GoodsDel(int id, int currUserId)
  266. {
  267. _sqlSugar.BeginTran();
  268. var goods = await _sqlSugar.Updateable<Pm_GoodsInfo>()
  269. .SetColumns(x => new Pm_GoodsInfo()
  270. {
  271. IsDel = 1,
  272. DeleteUserId = currUserId,
  273. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  274. })
  275. .Where(x => x.Id == id)
  276. .ExecuteCommandAsync();
  277. if (goods < 1)
  278. {
  279. _sqlSugar.RollbackTran();
  280. _jv.Msg = $"操作失败";
  281. return _jv;
  282. }
  283. var goodsStorage = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  284. .SetColumns(x => new Pm_GoodsStorage()
  285. {
  286. IsDel = 1,
  287. DeleteUserId = currUserId,
  288. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  289. })
  290. .Where(x => x.Id == id)
  291. .ExecuteCommandAsync();
  292. var goodsReceive = await _sqlSugar.Updateable<Pm_GoodsReceive>()
  293. .SetColumns(x => new Pm_GoodsReceive()
  294. {
  295. IsDel = 1,
  296. DeleteUserId = currUserId,
  297. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")
  298. })
  299. .Where(x => x.Id == id)
  300. .ExecuteCommandAsync();
  301. _sqlSugar.CommitTran();
  302. _jv.Code = StatusCodes.Status200OK;
  303. _jv.Msg = $"操作成功!";
  304. return _jv;
  305. }
  306. /// <summary>
  307. /// 入库/出库 审核类型
  308. /// 根据物品类型 处理审核是否需要多部门审核
  309. /// </summary>
  310. /// <param name="goodsTypeId">物品类型Id </param>
  311. /// <returns></returns>
  312. public bool GoodsAuditType(int goodsTypeId)
  313. {
  314. if (_goodsTypeIds.Contains(goodsTypeId))
  315. {
  316. return true;
  317. }
  318. return false;
  319. }
  320. /// <summary>
  321. /// 物品入库列表(带审核)
  322. /// </summary>
  323. /// <param name="dto"></param>
  324. /// <returns></returns>
  325. public async Task<JsonView> GoodsStorageList(GoodsStorageListDto dto)
  326. {
  327. string reqAuditLabel = dto.AuditLabel;
  328. var auditLabel = Array.Empty<int>();
  329. int userId = dto.CurrUserId;
  330. if (!string.IsNullOrEmpty(reqAuditLabel))
  331. {
  332. if (!reqAuditLabel.Contains("-1"))
  333. {
  334. auditLabel = reqAuditLabel
  335. .Split(',')
  336. .Select(x =>
  337. {
  338. if (int.TryParse(x, out var id)) return id;
  339. return id;
  340. })
  341. .ToArray();
  342. }
  343. }
  344. var auditList = GoodsStorageConfirmAuditDep(1);
  345. var hrAuditPer = false;
  346. var finAuditPer = false;
  347. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  348. var finAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Financial);
  349. if (hrAuditInfo != null)
  350. {
  351. if (hrAuditInfo.AuditorIds.Any(x => x == userId)) hrAuditPer = true;
  352. }
  353. if (finAuditInfo != null)
  354. {
  355. if (finAuditInfo.AuditorIds.Any(x => x == userId)) finAuditPer = true;
  356. }
  357. RefAsync<int> total = 0;
  358. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  359. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  360. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  361. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  362. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  363. .WhereIF(dto.GoodsId > 0, (gs, gi, u, u1) => gs.GoodsId == dto.GoodsId)
  364. .WhereIF(auditLabel.Length > 0, (gs, gi, u, u1) => auditLabel.Contains((int)gs.ConfirmStatus))
  365. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gs, gi, u, u1) => gi.Name.Contains(dto.GoodsName))
  366. .WhereIF(!string.IsNullOrEmpty(dto.BatchNo), (gs, gi, u, u1) => gs.BatchNo.Contains(dto.BatchNo))
  367. .WhereIF(finAuditPer, (gs, gi, u, u1) => _goodsTypeIds.Contains(gi.Type))
  368. .Select((gs, gi, u, u1) => new GoodsStorageListView()
  369. {
  370. Id = gs.Id,
  371. GoodsId = gs.GoodsId,
  372. GoodsType = gi.Type,
  373. BatchNo = gs.BatchNo,
  374. GoodsName = gi.Name,
  375. Quantity = gs.Quantity,
  376. UnitPrice = gs.UnitPrice,
  377. TotalPrice = gs.TotalPrice,
  378. SupplierName = gs.SupplierName,
  379. SupplierTel = gs.SupplierTel,
  380. SupplierAddress = gs.SupplierAddress,
  381. SupplierSource = gs.SupplierSource,
  382. StorageUserName = u1.CnName,
  383. StorageTime = gs.StorageTime,
  384. CreateUserName = u.CnName,
  385. ConfirmStatus = gs.ConfirmStatus,
  386. StatusDesc = gs.StatusDesc,
  387. CreateTime = gs.CreateTime,
  388. Remark = gs.Remark
  389. })
  390. .OrderByDescending(gs => gs.CreateTime)
  391. .ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  392. foreach (var item in data)
  393. {
  394. var auditDeps = new List<GoodsStorageAuditPerView>();
  395. auditDeps.Add(new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = GoodsAuditDepEnum.Hr.GetEnumDescription() });
  396. var auditPer = GoodsAuditType(item.GoodsType);
  397. if (auditPer)
  398. {
  399. auditDeps.Add(new() { AuditPer = finAuditPer, AuditDep = GoodsAuditDepEnum.Financial, ButtonText = GoodsAuditDepEnum.Financial.GetEnumDescription() });
  400. }
  401. item.AuditPers = auditDeps.ToArray();
  402. }
  403. _jv.Code = StatusCodes.Status200OK;
  404. _jv.Data = data;
  405. _jv.Count = total;
  406. _jv.Msg = $"操作成功";
  407. return _jv;
  408. }
  409. /// <summary>
  410. /// 物品入库详情
  411. /// </summary>
  412. /// <param name="id"></param>
  413. /// <returns></returns>
  414. public async Task<JsonView> GoodsStorageInfo(int portType, int id)
  415. {
  416. var data = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  417. .LeftJoin<Pm_GoodsInfo>((gs, gi) => gs.GoodsId == gi.Id)
  418. .LeftJoin<Sys_Users>((gs, gi, u) => gs.CreateUserId == u.Id)
  419. .LeftJoin<Sys_Users>((gs, gi, u, u1) => gs.StorageUserId == u1.Id)
  420. .Where((gs, gi, u, u1) => gs.IsDel == 0)
  421. .WhereIF(id > 0, (gs, gi, u, u1) => gs.Id == id)
  422. .Select((gs, gi, u, u1) => new
  423. {
  424. gs.Id,
  425. gs.GoodsId,
  426. GoodsName = gi.Name,
  427. gs.Quantity,
  428. gs.UnitPrice,
  429. gs.TotalPrice,
  430. gs.SupplierName,
  431. gs.SupplierTel,
  432. gs.SupplierAddress,
  433. gs.SupplierSource,
  434. gs.ReceiveQuantity,
  435. gs.StorageUserId,
  436. StorageUser = u1.CnName,
  437. gs.StorageTime,
  438. CreateUserName = u.CnName,
  439. gs.CreateUserId,
  440. gs.CreateTime,
  441. gs.Remark,
  442. //gs.IsInConfirm
  443. })
  444. .FirstAsync();
  445. _jv.Msg = $"操作成功!";
  446. _jv.Code = StatusCodes.Status200OK;
  447. _jv.Data = data;
  448. return _jv;
  449. }
  450. /// <summary>
  451. /// 物品入库 操作(Create Or Edit)
  452. /// </summary>
  453. /// <param name="dto"></param>
  454. /// <param name="currUserId"></param>
  455. /// <returns></returns>
  456. public async Task<JsonView> GoodsStorageOp(GoodsStorageOpDto dto, int currUserId)
  457. {
  458. var info = _mapper.Map<Pm_GoodsStorage>(dto);
  459. info.CreateUserId = currUserId;
  460. info.BatchNo = DateTime.Now.ToString("yyyyMMddHHmmssfff");
  461. _sqlSugar.BeginTran();
  462. if (info.Id > 0) //修改
  463. {
  464. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>().FirstAsync(x => x.Id == dto.Id);
  465. var auditStatus = selectInfo.ConfirmStatus;
  466. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  467. {
  468. _sqlSugar.RollbackTran();
  469. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可更改!如若更改请取消已确认或部分确认审核状态!";
  470. return _jv;
  471. }
  472. var storageEdit = await _sqlSugar.Updateable(info)
  473. .UpdateColumns(x => new
  474. {
  475. x.Quantity,
  476. x.UnitPrice,
  477. x.TotalPrice,
  478. x.SupplierName,
  479. x.SupplierTel,
  480. x.SupplierAddress,
  481. x.SupplierSource,
  482. x.StorageUserId,
  483. x.StorageTime
  484. })
  485. .Where(x => x.Id == dto.Id)
  486. .ExecuteCommandAsync();
  487. if (storageEdit < 1)
  488. {
  489. _sqlSugar.RollbackTran();
  490. _jv.Msg = $"修改失败!";
  491. return _jv;
  492. }
  493. //入库确认默认状态 多条 or 单条
  494. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.Type == 1 && x.DataId == dto.Id).ToListAsync();
  495. if (auditInfos.Any())
  496. {
  497. var isAuditPer = false;
  498. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  499. if (goodsInfo != null)
  500. {
  501. isAuditPer = GoodsAuditType(goodsInfo.Type);
  502. }
  503. if (!isAuditPer && auditInfos.Count == 1)
  504. {
  505. var auditInfo = new Pm_GoodsAudit(1, GoodsAuditDepEnum.Financial, dto.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  506. await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  507. }
  508. }
  509. }
  510. else if (info.Id < 1) //添加
  511. {
  512. info.ConfirmStatus = GoodsConfirmEnum.WaitConfirm;
  513. var storageAddId = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  514. if (storageAddId < 1)
  515. {
  516. _sqlSugar.RollbackTran();
  517. _jv.Msg = $"添加失败!";
  518. return _jv;
  519. }
  520. //入库确认默认状态
  521. List<Pm_GoodsAudit> auditInfos = GoodsStorageConfirm(1, storageAddId, info.CreateUserId);
  522. if (auditInfos.Any())
  523. {
  524. //验证添加多条或者单条审核状态
  525. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.IsDel == 0 && x.Id == dto.GoodsId);
  526. if (goodsInfo != null)
  527. {
  528. var isAuditPer = GoodsAuditType(goodsInfo.Type);
  529. if (!isAuditPer) auditInfos = auditInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  530. }
  531. await _sqlSugar.Insertable(auditInfos).ExecuteCommandAsync();
  532. }
  533. }
  534. _sqlSugar.CommitTran();
  535. _jv.Msg = $"操作成功!";
  536. _jv.Code = StatusCodes.Status200OK;
  537. return _jv;
  538. }
  539. /// <summary>
  540. /// 物品入库 确认操作-默认审核部门
  541. /// </summary>
  542. /// <param name="auditType">
  543. /// 审核类型
  544. /// 1.入库 2.出库
  545. /// </param>
  546. /// <param name="dataId">dataId</param>
  547. /// <param name="currUserId">审核人</param>
  548. /// <returns></returns>
  549. public List<Pm_GoodsAudit> GoodsStorageConfirm(int auditType, int dataId, int currUserId)
  550. {
  551. var goodsAuditList = new List<Pm_GoodsAudit>();
  552. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Hr, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  553. goodsAuditList.Add(new Pm_GoodsAudit(auditType, GoodsAuditDepEnum.Financial, dataId, GoodsConfirmEnum.WaitConfirm, currUserId));
  554. return goodsAuditList;
  555. }
  556. /// <summary>
  557. /// 物品入库 确认操作
  558. /// </summary>
  559. /// <param name="dto"></param>
  560. /// <returns></returns>
  561. public async Task<JsonView> GoodsStorageConfirmStatusChange(GoodsStorageConfirmDto dto, int currUserId)
  562. {
  563. int gsId = dto.Id;
  564. var auditDep = dto.AuditDep;
  565. if (gsId < 1)
  566. {
  567. _jv.Msg = string.Format("{0}", MsgTips.Id);
  568. return _jv;
  569. }
  570. if (currUserId < 1)
  571. {
  572. _jv.Msg = string.Format("{0}", MsgTips.UserId);
  573. return _jv;
  574. }
  575. //验证审核部门
  576. (bool auditPer, GoodsAuditDepEnum goodsAuditDep) = GoodsAuditDep(currUserId, auditDep, 1);
  577. if (!auditPer)
  578. {
  579. _jv.Msg = string.Format("未分配入库确认权限!");
  580. return _jv;
  581. }
  582. //入库确认 更改审核状态
  583. _sqlSugar.BeginTran();
  584. var info = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && x.Id == gsId).FirstAsync();
  585. if (info == null)
  586. {
  587. _jv.Msg = string.Format("入库信息不存在!");
  588. return _jv;
  589. }
  590. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.IsDel == 0 && x.Id == info.GoodsId).FirstAsync();
  591. if (goodsInfo == null)
  592. {
  593. _jv.Msg = string.Format("物品信息不存在!");
  594. return _jv;
  595. }
  596. int goodsTypeId = goodsInfo.Type;
  597. var preChangeStatus = info.ConfirmStatus;
  598. if (preChangeStatus == dto.ConfirmStatus)
  599. {
  600. _jv.Msg = string.Format("“{0}”已操作,请勿重复该操作!", dto.ConfirmStatus.GetEnumDescription());
  601. return _jv;
  602. }
  603. var preInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.IsDel == 0 && x.DataId == gsId).ToListAsync();
  604. //验证是否需要多级确认
  605. var isAuditPer = GoodsAuditType(goodsTypeId);
  606. //单部门审核时默认人事部审核
  607. if (!isAuditPer) goodsAuditDep = GoodsAuditDepEnum.Hr;
  608. var auditInfo = preInfos.FirstOrDefault(x => x.Dep == goodsAuditDep);
  609. if (auditInfo != null)
  610. {
  611. //移除部门审核状态
  612. preInfos.Remove(auditInfo);
  613. auditInfo.AuditStatus = dto.ConfirmStatus;
  614. auditInfo.AuditUserId = currUserId;
  615. auditInfo.AuditTime = DateTime.Now;
  616. var updStatus = await _sqlSugar.Updateable(auditInfo)
  617. .UpdateColumns(x => new { x.AuditStatus, x.AuditUserId, x.AuditTime })
  618. .ExecuteCommandAsync();
  619. if (updStatus < 1)
  620. {
  621. _sqlSugar.RollbackTran();
  622. _jv.Msg = string.Format("入库确认失败!");
  623. return _jv;
  624. }
  625. }
  626. else
  627. {
  628. auditInfo = new Pm_GoodsAudit(1, goodsAuditDep, gsId, dto.ConfirmStatus, currUserId, currUserId);
  629. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  630. if (addStatus < 1)
  631. {
  632. _sqlSugar.RollbackTran();
  633. _jv.Msg = string.Format("入库确认失败!");
  634. return _jv;
  635. }
  636. }
  637. preInfos.Add(auditInfo);
  638. //入库确认 更改入库状态及扣或增加除库存数、金额
  639. var confirmStatus = GoodsConfirmEnum.WaitConfirm;
  640. if (isAuditPer) //多级审核确认
  641. {
  642. if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 2)
  643. {
  644. confirmStatus = GoodsConfirmEnum.Confirmed;
  645. }
  646. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() >= 1)
  647. {
  648. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  649. }
  650. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.UnApproved).Count() > 0)
  651. {
  652. confirmStatus = GoodsConfirmEnum.UnApproved;
  653. }
  654. else if (preInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.PartConfirmed).Count() > 0)
  655. {
  656. confirmStatus = GoodsConfirmEnum.PartConfirmed;
  657. }
  658. }
  659. else //人事部审核确认
  660. {
  661. confirmStatus = GoodsConfirmEnum.Confirmed;
  662. confirmStatus = dto.ConfirmStatus;
  663. preInfos = preInfos.Where(x => x.Dep == GoodsAuditDepEnum.Hr).ToList();
  664. }
  665. //入库状态描述
  666. var statusDesc = new StringBuilder();
  667. foreach (var preInfo in preInfos)
  668. {
  669. string depName = preInfo.Dep.GetEnumDescription(),
  670. auditStatus = preInfo.AuditStatus.GetEnumDescription(),
  671. auditUserName = preInfo.AuditUserId > 0 ? _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == preInfo.AuditUserId)?.CnName ?? "-" : "-",
  672. auditTime = preInfo.AuditUserId > 0 ? preInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  673. statusDesc.Append(string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>", depName, auditStatus, auditUserName, auditTime));
  674. }
  675. //更改入库状态及描述
  676. info.ConfirmStatus = confirmStatus;
  677. info.StatusDesc = statusDesc.ToString();
  678. var goodsStorageUpd = await _sqlSugar.Updateable(info)
  679. .UpdateColumns(x => new
  680. {
  681. x.ConfirmStatus,
  682. x.StatusDesc,
  683. })
  684. .Where(x => x.Id == info.Id)
  685. .ExecuteCommandAsync();
  686. if (goodsStorageUpd < 1)
  687. {
  688. _sqlSugar.RollbackTran();
  689. _jv.Msg = $"入库确认状态更改失败!";
  690. return _jv;
  691. }
  692. //入库审核通过数量、金额
  693. decimal auditQuantity = info.Quantity,
  694. auditTotalPrice = info.TotalPrice;
  695. goodsInfo.LastUpdateUserId = currUserId;
  696. goodsInfo.LastUpdateTime = DateTime.Now;
  697. if (confirmStatus == GoodsConfirmEnum.Confirmed) // 确认状态
  698. {
  699. //更改后的状态和更改前的状态不一致时 更改库存数、金额
  700. if (preChangeStatus != confirmStatus) //
  701. {
  702. goodsInfo.SQ_Total += auditQuantity;
  703. goodsInfo.StockQuantity += auditQuantity;
  704. goodsInfo.PriceTotal += auditTotalPrice;
  705. }
  706. }
  707. else //其他状态 拒绝、部分确认、等待确认
  708. {
  709. //更改前状态为确认状态时,减库存数、金额
  710. if (preChangeStatus == GoodsConfirmEnum.Confirmed)
  711. {
  712. goodsInfo.SQ_Total -= auditQuantity;
  713. goodsInfo.StockQuantity -= auditQuantity;
  714. goodsInfo.PriceTotal -= auditTotalPrice;
  715. }
  716. }
  717. var goodsUpd = await _sqlSugar.Updateable(goodsInfo)
  718. .UpdateColumns(x => new
  719. {
  720. x.SQ_Total,
  721. x.StockQuantity,
  722. x.PriceTotal,
  723. x.LastUpdateUserId,
  724. x.LastUpdateTime,
  725. })
  726. .Where(x => x.Id == info.GoodsId)
  727. .ExecuteCommandAsync();
  728. if (goodsUpd > 0)
  729. {
  730. _sqlSugar.CommitTran();
  731. _jv.Msg = $"操作成功!";
  732. _jv.Code = StatusCodes.Status200OK;
  733. return _jv;
  734. }
  735. _sqlSugar.RollbackTran();
  736. _jv.Msg = $"操作失败!";
  737. return _jv;
  738. }
  739. /// <summary>
  740. /// 获取物品审核部门
  741. /// </summary>
  742. /// <param name="userId"> userId </param>
  743. /// <param name="auditDepEnum"> 审核部门枚举 </param>
  744. /// <param name="auditType">
  745. /// 指定审核类型(入库/出库)
  746. /// 1:入库审核
  747. /// 2:出库审核
  748. /// </param>
  749. /// <returns></returns>
  750. public (bool, GoodsAuditDepEnum) GoodsAuditDep(int userId, GoodsAuditDepEnum auditDepEnum, int auditType = 1)
  751. {
  752. if (userId < 1) return (false, GoodsAuditDepEnum.Hr);
  753. var auditList = GoodsStorageConfirmAuditDep(1);
  754. if (auditType == 1)
  755. {
  756. if (auditList.Any(x => x.AuditDep == auditDepEnum && x.AuditorIds.Contains(userId)))
  757. {
  758. return (true, auditDepEnum);
  759. }
  760. }
  761. return (false, GoodsAuditDepEnum.Hr);
  762. }
  763. /// <summary>
  764. /// 物品审核部门列表
  765. /// </summary>
  766. /// <param name="auditType">
  767. /// 指定审核类型(入库/出库)
  768. /// 1:入库审核
  769. /// 2:出库审核
  770. /// </param>
  771. /// <returns></returns>
  772. public List<GoodsAuditDepView> GoodsStorageConfirmAuditDep(int auditType = 1)
  773. {
  774. var auditList = new List<GoodsAuditDepView>();
  775. var hrAuditorIds = new GoodsAuditDepView()
  776. {
  777. AuditDep = GoodsAuditDepEnum.Hr,
  778. AuditorIds = new int[] {
  779. 309, // 赖红燕
  780. 343, // 陈湘
  781. //374, // 罗颖
  782. 208, // 雷怡
  783. }
  784. };
  785. var finAuditorIds = new GoodsAuditDepView()
  786. {
  787. AuditDep = GoodsAuditDepEnum.Financial,
  788. AuditorIds = new int[] {
  789. 187, // 曾艳
  790. 281, // 伏虹瑾
  791. 208, // 雷怡
  792. }
  793. };
  794. if (auditType == 1)//入库
  795. {
  796. hrAuditorIds.AuditorIds = new int[] {
  797. //343, // 陈湘
  798. 374, // 罗颖
  799. 208, // 雷怡
  800. };
  801. auditList.Add(hrAuditorIds);
  802. auditList.Add(finAuditorIds);
  803. }
  804. else if (auditType == 2) //出库
  805. {
  806. auditList.Add(hrAuditorIds);
  807. auditList.Add(finAuditorIds);
  808. }
  809. return auditList;
  810. }
  811. /// <summary>
  812. /// 物品入库 Del
  813. /// </summary>
  814. /// <param name="id"></param>
  815. /// <returns></returns>
  816. public async Task<JsonView> GoodsStorageDel(int id, int userId)
  817. {
  818. var storageInfo = await _sqlSugar
  819. .Queryable<Pm_GoodsStorage>()
  820. .Where(x => x.Id == id)
  821. .FirstAsync();
  822. if (storageInfo == null) return _jv;
  823. var auditStatus = storageInfo.ConfirmStatus;
  824. if (auditStatus == GoodsConfirmEnum.Confirmed || auditStatus == GoodsConfirmEnum.PartConfirmed)
  825. {
  826. _sqlSugar.RollbackTran();
  827. _jv.Msg = $"该条入库信息已确认或部分确认审核,不可删除!如若删除请取消已确认或部分确认审核状态!";
  828. return _jv;
  829. }
  830. decimal delAgoQuantity = storageInfo.Quantity,
  831. delAgoTotalPrice = storageInfo.TotalPrice;
  832. var goodsId = storageInfo.GoodsId;
  833. _sqlSugar.BeginTran();
  834. var storageDel = await _sqlSugar.Updateable<Pm_GoodsStorage>()
  835. .SetColumns(x => new Pm_GoodsStorage
  836. {
  837. DeleteUserId = userId,
  838. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  839. IsDel = 1
  840. })
  841. .Where(x => x.Id == id)
  842. .ExecuteCommandAsync();
  843. if (storageDel < 1)
  844. {
  845. _sqlSugar.RollbackTran();
  846. _jv.Msg = $"操作失败!";
  847. return _jv;
  848. }
  849. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == goodsId);
  850. goodsInfo.SQ_Total = goodsInfo.SQ_Total - delAgoQuantity;
  851. goodsInfo.StockQuantity = goodsInfo.StockQuantity - delAgoQuantity;
  852. goodsInfo.PriceTotal = goodsInfo.PriceTotal - delAgoTotalPrice;
  853. goodsInfo.LastUpdateUserId = userId;
  854. goodsInfo.LastUpdateTime = DateTime.Now;
  855. var goodsEdit = await _sqlSugar.Updateable(goodsInfo)
  856. .UpdateColumns(x => new
  857. {
  858. x.SQ_Total,
  859. x.StockQuantity,
  860. x.PriceTotal,
  861. x.LastUpdateUserId,
  862. x.LastUpdateTime,
  863. })
  864. .Where(x => x.Id == goodsId)
  865. .ExecuteCommandAsync();
  866. if (goodsEdit > 0)
  867. {
  868. _sqlSugar.CommitTran();
  869. _jv.Msg = $"操作成功!";
  870. _jv.Code = StatusCodes.Status200OK;
  871. return _jv;
  872. }
  873. _sqlSugar.RollbackTran();
  874. _jv.Msg = $"操作失败!";
  875. return _jv;
  876. }
  877. /// <summary>
  878. /// 物品入库
  879. /// excelDownload
  880. /// </summary>
  881. /// <param name="dto"></param>
  882. /// <returns></returns>
  883. public async Task<JsonView> GoodsStorageExcelDownload()
  884. {
  885. var fileName = $"物资入库{Guid.NewGuid()}.xlsx";
  886. var excelTempPath = $"{_excelPath}Template/物资入库Temp.xlsx";
  887. if (!File.Exists(excelTempPath))
  888. {
  889. _jv.Code = StatusCodes.Status204NoContent;
  890. _jv.Msg = $"该模板文件不存在!";
  891. return _jv;
  892. }
  893. _url = $"{_url}Office/Excel/GoodsFiles/";
  894. _excelPath = $"{_excelPath}GoodsFiles";
  895. if (!Directory.Exists(_excelPath))
  896. {
  897. Directory.CreateDirectory(_excelPath);
  898. }
  899. //入库记录
  900. var storageData = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  901. .LeftJoin<Sys_Users>((gs, su) => gs.StorageUserId == su.Id)
  902. .Where((gs, su) => gs.IsDel == 0)
  903. .Select((gs, su) => new
  904. {
  905. gs.GoodsId,
  906. gs.Quantity,
  907. su.CnName,
  908. gs.StorageTime
  909. })
  910. .ToListAsync();
  911. //出库记录
  912. var receiveData = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  913. .LeftJoin<Sys_Users>((gr, su) => gr.CreateUserId == su.Id)
  914. .Where((gr, su) => gr.IsDel == 0)
  915. .Select((gr, su) => new
  916. {
  917. gr.GoodsId,
  918. gr.Quantity,
  919. su.CnName,
  920. gr.CreateTime,
  921. gr.Reason
  922. })
  923. .ToListAsync();
  924. var data = await _sqlSugar.Queryable<Pm_GoodsInfo>()
  925. .Where(gi => gi.IsDel == 0)
  926. .Select(gi => new GoodsStorageExcelDownloadView
  927. {
  928. Id = gi.Id,
  929. Name = gi.Name,
  930. SQ_Total = gi.SQ_Total,
  931. OQ_Total = gi.OQ_Total,
  932. StockQuantity = gi.StockQuantity,
  933. Unit = gi.Unit
  934. })
  935. .ToListAsync();
  936. foreach (var item in data)
  937. {
  938. var storageData1 = storageData.Where(x => x.GoodsId == item.Id).ToList();
  939. if (storageData1.Any())
  940. {
  941. item.SQ_Total1 = storageData1.Sum(x => x.Quantity);
  942. item.SQ_Total -= item.SQ_Total1;
  943. item.StorageLabel = string.Join("\r\n", storageData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 入库人:【" + x.CnName + "】 入库时间:【" + x.StorageTime + "】").ToList());
  944. }
  945. var receiveData1 = receiveData.Where(x => x.GoodsId == item.Id).ToList();
  946. if (receiveData1.Any())
  947. {
  948. item.ReceiveLabel = string.Join("\r\n", receiveData1.Select(x => "数量:【" + x.Quantity.ToString("#0.00") + "】 申请人:【" + x.CnName + "】 申请时间:【" + x.CreateTime.ToString("yyyy-MM-dd HH:mm:ss") + "】 原因:【" + x.Reason + "】").ToList());
  949. }
  950. }
  951. //载入模板
  952. WorkbookDesigner designer = new WorkbookDesigner();
  953. designer.Workbook = new Workbook(excelTempPath);
  954. designer.SetDataSource("Export", data);
  955. designer.Process();
  956. #region 渲染Cell批注
  957. var sheet = designer.Workbook.Worksheets[0];
  958. for (int i = 0; i < data.Count; i++)
  959. {
  960. string storageComment = $"C{i + 2}",
  961. receiveComment = $"D{i + 2}",
  962. storageCommentText = data[i].StorageLabel,
  963. receiveCommentText = data[i].ReceiveLabel;
  964. if (!string.IsNullOrEmpty(storageCommentText))
  965. {
  966. int storageIndex = sheet.Comments.Add(storageComment);
  967. Aspose.Cells.Comment comment = sheet.Comments[storageIndex];
  968. comment.Note = storageCommentText;
  969. comment.Width = 500;
  970. comment.Height = 200;
  971. //comment.Font.Color = Color.Red;
  972. }
  973. if (!string.IsNullOrEmpty(receiveCommentText))
  974. {
  975. int receiveIndex = sheet.Comments.Add(receiveComment);
  976. Aspose.Cells.Comment comment = sheet.Comments[receiveIndex];
  977. comment.Note = receiveCommentText;
  978. comment.Width = 800;
  979. comment.Height = 200;
  980. //comment.Font.Color = Color.Red;
  981. }
  982. }
  983. #endregion
  984. string serverPath = $"{_url}{fileName}";
  985. designer.Workbook.Save($"{_excelPath}/{fileName}");
  986. _jv.Code = StatusCodes.Status200OK;
  987. _jv.Data = new { url = serverPath };
  988. _jv.Msg = $"操作成功";
  989. return _jv;
  990. }
  991. /// <summary>
  992. /// 物品领用列表
  993. /// </summary>
  994. /// <param name="dto"></param>
  995. /// <returns></returns>
  996. public async Task<JsonView> GoodsReceiveList(GoodsReceiveListDTO dto)
  997. {
  998. //参数处理
  999. int[] typeLabel = Array.Empty<int>(),
  1000. userLabel = Array.Empty<int>(),
  1001. auditLabel = Array.Empty<int>(),
  1002. groupLabel = Array.Empty<int>();
  1003. int currUserId = dto.CurrUserId;
  1004. if (!string.IsNullOrEmpty(dto.TypeLabel))
  1005. {
  1006. typeLabel = dto.TypeLabel
  1007. .Split(',')
  1008. .Select(x =>
  1009. {
  1010. if (int.TryParse(x, out var id)) return id;
  1011. return id;
  1012. })
  1013. .ToArray();
  1014. }
  1015. if (!string.IsNullOrEmpty(dto.UserLabel))
  1016. {
  1017. userLabel = dto.UserLabel
  1018. .Split(',')
  1019. .Select(x =>
  1020. {
  1021. if (int.TryParse(x, out var id)) return id;
  1022. return id;
  1023. })
  1024. .ToArray();
  1025. }
  1026. if (!string.IsNullOrEmpty(dto.AuditLabel))
  1027. {
  1028. auditLabel = dto.AuditLabel
  1029. .Split(',')
  1030. .Select(x =>
  1031. {
  1032. if (int.TryParse(x, out var id)) return id;
  1033. return id;
  1034. })
  1035. .ToArray();
  1036. if (auditLabel.Any(x => x == -1))
  1037. {
  1038. auditLabel = auditLabel.Where(x => x != -1).ToArray();
  1039. }
  1040. }
  1041. if (!string.IsNullOrEmpty(dto.GroupLabel))
  1042. {
  1043. groupLabel = dto.GroupLabel
  1044. .Split(',')
  1045. .Select(x =>
  1046. {
  1047. if (int.TryParse(x, out var id)) return id;
  1048. return id;
  1049. })
  1050. .ToArray();
  1051. }
  1052. //全部团组
  1053. bool isAllGroups = false;
  1054. if (groupLabel.Contains(-3)) isAllGroups = true;
  1055. //物品ID和物品名称只能传一个
  1056. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  1057. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  1058. //财务人事审核权限处理
  1059. var auditList = GoodsStorageConfirmAuditDep(2);
  1060. bool hrAuditPer = false;
  1061. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  1062. if (hrAuditInfo.AuditorIds.Any(x => x == currUserId))
  1063. {
  1064. hrAuditPer = true;
  1065. }
  1066. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  1067. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  1068. RefAsync<int> total = 0;
  1069. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1070. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1071. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  1072. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1073. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1074. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1075. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  1076. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  1077. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  1078. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  1079. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  1080. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  1081. .WhereIF(isAllGroups, (gr, gi, sd, u1, u2, di) => gr.GroupId > 0)
  1082. .WhereIF(!isAllGroups && groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  1083. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  1084. .WhereIF(hrAuditPer, (gr, gi, sd, u1, u2, di) => _goodsTypeIds.Contains(gi.Type))
  1085. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1086. {
  1087. Id = gr.Id,
  1088. GroupId = gr.GroupId,
  1089. GroupName = di.TeamName,
  1090. GoodsId = gr.GoodsId,
  1091. GoodsName = gi.Name,
  1092. GoodsTypeId = gi.Type,
  1093. GoodsType = sd.Name,
  1094. Quantity = gr.Quantity,
  1095. Unit = gi.Unit,
  1096. Reason = gr.Reason,
  1097. Remark = gr.Remark,
  1098. AuditStatus = gr.AuditStatus,
  1099. StatusDesc = gr.StatusDesc,
  1100. AuditUserId = gr.AuditUserId,
  1101. AuditUserName = u1.CnName,
  1102. AuditTime = gr.AuditTime,
  1103. CreateUserName = u2.CnName,
  1104. CreateTime = gr.CreateTime
  1105. })
  1106. .OrderByDescending(gr => gr.CreateTime);
  1107. //excel导出
  1108. if (dto.IsExcelDownload)
  1109. {
  1110. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1111. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1112. if (!File.Exists(excelTempPath))
  1113. {
  1114. _jv.Code = StatusCodes.Status204NoContent;
  1115. _jv.Msg = $"该模板文件不存在!";
  1116. return _jv;
  1117. }
  1118. _url = $"{_url}Office/Excel/GoodsFiles/";
  1119. _excelPath = $"{_excelPath}GoodsFiles";
  1120. if (!Directory.Exists(_excelPath))
  1121. {
  1122. Directory.CreateDirectory(_excelPath);
  1123. }
  1124. //载入模板
  1125. WorkbookDesigner designer = new WorkbookDesigner();
  1126. designer.Workbook = new Workbook(excelTempPath);
  1127. var tableData = await data.ToListAsync();
  1128. designer.SetDataSource("Export", tableData);
  1129. designer.Process();
  1130. string serverPath = $"{_url}{fileName}";
  1131. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1132. _jv.Code = StatusCodes.Status200OK;
  1133. _jv.Data = new { url = serverPath };
  1134. _jv.Msg = $"操作成功";
  1135. return _jv;
  1136. }
  1137. //返回分页数据
  1138. //领用审核List只显示多条审核数据 陈湘
  1139. var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1140. foreach (var item in view)
  1141. {
  1142. //默认审核验证 多条 or 单挑
  1143. var isAudit = GoodsAuditType(item.GoodsTypeId);
  1144. var auditPers = new List<GoodsStorageAuditPerView>()
  1145. {
  1146. new (){ AuditPer = true, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"领用确认"},
  1147. };
  1148. if (isAudit) auditPers.Add(new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"出库确认" });
  1149. item.AuditPers = auditPers.ToArray();
  1150. }
  1151. if (dto.PortType == 2 || dto.PortType == 3)
  1152. {
  1153. _jv.Data = view;
  1154. }
  1155. else if (dto.PortType == 1)
  1156. {
  1157. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  1158. _jv.Data = view1;
  1159. }
  1160. _jv.Code = StatusCodes.Status200OK;
  1161. _jv.Count = total;
  1162. _jv.Msg = $"操作成功";
  1163. return _jv;
  1164. }
  1165. /// <summary>
  1166. /// 物品领用详情
  1167. /// </summary>
  1168. /// <param name="portType"></param>
  1169. /// <param name="id"></param>
  1170. /// <returns></returns>
  1171. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1172. {
  1173. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1174. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1175. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1176. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1177. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2, di) => gr.GroupId == di.Id)
  1178. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1179. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1180. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1181. {
  1182. Id = gr.Id,
  1183. GroupId = gr.GroupId,
  1184. GroupName = di.TeamName,
  1185. GoodsId = gr.GoodsId,
  1186. GoodsName = gi.Name,
  1187. Quantity = gr.Quantity,
  1188. Reason = gr.Reason,
  1189. Remark = gr.Remark,
  1190. GoodsStorageInfo = gr.GoodsStorageInfo,
  1191. AuditStatus = gr.AuditStatus,
  1192. AuditUserId = gr.AuditUserId,
  1193. AuditUserName = u1.CnName,
  1194. AuditTime = gr.AuditTime,
  1195. CreateUserName = u2.CnName,
  1196. CreateTime = gr.CreateTime
  1197. })
  1198. .FirstAsync();
  1199. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1200. {
  1201. var subData = new List<dynamic>();
  1202. try
  1203. {
  1204. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1205. if (subData1.Count > 0)
  1206. {
  1207. string goodsStorageInfoStr = string.Empty;
  1208. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1209. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1210. foreach (var item in subData1)
  1211. {
  1212. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1213. if (storageInfo != null)
  1214. {
  1215. subData.Add(new
  1216. {
  1217. StorageId = item.StorageId,
  1218. BatchNo = storageInfo.BatchNo,
  1219. RecsiveQuantity = item.Quantity
  1220. });
  1221. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1222. }
  1223. }
  1224. data.QuantityInfos = subData;
  1225. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1226. }
  1227. }
  1228. catch (Exception e)
  1229. {
  1230. Console.WriteLine(e);
  1231. }
  1232. }
  1233. _jv.Code = StatusCodes.Status200OK;
  1234. _jv.Msg = $"操作成功";
  1235. if (portType == 2 || portType == 3) //移动端
  1236. {
  1237. _jv.Data = data;
  1238. }
  1239. else if (portType == 1) //pc端
  1240. {
  1241. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1242. }
  1243. return _jv;
  1244. }
  1245. /// <summary>
  1246. /// 物品领用 OP(Add Or Edit)
  1247. /// </summary>
  1248. /// <param name="dto"></param>
  1249. /// <param name="currUserId"></param>
  1250. /// <returns></returns>
  1251. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1252. {
  1253. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1254. info.CreateUserId = currUserId;
  1255. var auditEnums = new List<GoodsAuditEnum>() {
  1256. GoodsAuditEnum.Approved, // 1
  1257. GoodsAuditEnum.UnApproved, // 2
  1258. GoodsAuditEnum.OutConfirmed,// 5
  1259. GoodsAuditEnum.OutRejected // 6
  1260. };
  1261. _sqlSugar.BeginTran();
  1262. //物品现有库存
  1263. var goodsInfo = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.Id == info.GoodsId);
  1264. var stockQuantity = goodsInfo?.StockQuantity ?? 0.00M;
  1265. //待审核 该物品数量
  1266. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  1267. x.GoodsId == dto.GoodsId &&
  1268. !auditEnums.Contains(x.AuditStatus)
  1269. ).SumAsync(x => x.Quantity);
  1270. //验证默认审核 多条 or 单条
  1271. //出库确认默认审核状态
  1272. var isAuditPer = GoodsAuditType(goodsInfo?.Type ?? 0);
  1273. //验证默认审核 多条审核时添加
  1274. var goodsAuditInfo1 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1275. var goodsAuditInfo2 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1276. var goodsAuditInfos = new List<Pm_GoodsAudit>() { goodsAuditInfo1 };
  1277. //状态描述
  1278. StringBuilder stringBuilder = new StringBuilder();
  1279. stringBuilder.Append($"领用确认:状态:待确认 审核人:- 审核时间:-;<br/>");
  1280. if (isAuditPer)
  1281. {
  1282. goodsAuditInfos.Add(goodsAuditInfo2);
  1283. stringBuilder.Append($"人事部:状态:待确认 审核人:- 审核时间:-;<br/>");
  1284. }
  1285. info.StatusDesc = stringBuilder.ToString();
  1286. if (info.Id > 0) //修改
  1287. {
  1288. //审核验证
  1289. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1290. if (auditEnums.Contains(selectInfo.AuditStatus))
  1291. {
  1292. _sqlSugar.RollbackTran();
  1293. _jv.Msg = $"该条数据已执行操作({selectInfo.AuditStatus.GetEnumDescription()}),不可更改!";
  1294. return _jv;
  1295. }
  1296. //物品数量验证
  1297. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1298. if (editAfterQuantity > stockQuantity)
  1299. {
  1300. _sqlSugar.RollbackTran();
  1301. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1302. return _jv;
  1303. }
  1304. var edit = await _sqlSugar.Updateable(info)
  1305. .UpdateColumns(x => new
  1306. {
  1307. x.GroupId,
  1308. x.Quantity,
  1309. x.Reason,
  1310. x.Remark,
  1311. x.StatusDesc,
  1312. })
  1313. .Where(x => x.Id == info.Id)
  1314. .ExecuteCommandAsync();
  1315. if (edit > 0)
  1316. {
  1317. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.DataId == info.Id).ToListAsync();
  1318. if (!auditInfos.Any()) await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1319. _sqlSugar.CommitTran();
  1320. _jv.Msg = $"操作成功!";
  1321. _jv.Code = StatusCodes.Status200OK;
  1322. return _jv;
  1323. }
  1324. }
  1325. else if (info.Id < 1) //添加
  1326. {
  1327. //物品数量验证
  1328. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1329. if (addAgoQuantity > stockQuantity)
  1330. {
  1331. _sqlSugar.RollbackTran();
  1332. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1333. return _jv;
  1334. }
  1335. var add = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  1336. if (add > 0)
  1337. {
  1338. goodsAuditInfos.ForEach(x => x.DataId = add);
  1339. await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1340. _sqlSugar.CommitTran();
  1341. _jv.Msg = $"操作成功!";
  1342. _jv.Code = StatusCodes.Status200OK;
  1343. return _jv;
  1344. }
  1345. }
  1346. _sqlSugar.RollbackTran();
  1347. return _jv;
  1348. }
  1349. /// <summary>
  1350. /// 物品领用 Audit
  1351. /// </summary>
  1352. /// <param name="idArray"></param>
  1353. /// <param name="userId"></param>
  1354. /// <param name="auditEnum"></param>
  1355. /// <returns></returns>
  1356. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1357. {
  1358. if (idArray.Length < 1) return _jv;
  1359. if (!Enum.IsDefined(typeof(GoodsAuditEnum),(int)auditEnum))
  1360. {
  1361. _jv.Msg = $"出库确认状态超出可用范围!";
  1362. return _jv;
  1363. }
  1364. int receiveId = idArray[0];
  1365. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == userId)?.CnName ?? "-";
  1366. _sqlSugar.BeginTran();
  1367. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.IsDel == 0 && receiveId == x.Id);
  1368. if (receiveInfo == null)
  1369. {
  1370. _sqlSugar.RollbackTran();
  1371. _jv.Msg = $"当前领用信息不存在!";
  1372. return _jv;
  1373. }
  1374. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  1375. int goodTypeId = goodsInfo?.Type ?? 0;
  1376. //审核前状态
  1377. var preChangeStatus = receiveInfo.AuditStatus;
  1378. //状态验证
  1379. //if (preChangeStatus == auditEnum)
  1380. //{
  1381. // _sqlSugar.RollbackTran();
  1382. // _jv.Msg = $"该条数据状态已设置,不可重复设置!";
  1383. // return _jv;
  1384. //}
  1385. var isAuditPer = GoodsAuditType(goodTypeId);
  1386. if (isAuditPer) //多人审核
  1387. {
  1388. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  1389. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1390. .FirstAsync(x => x.Type == 2 && x.DataId == receiveInfo.Id && x.Dep == GoodsAuditDepEnum.Hr_Reception);
  1391. if (auditInfo != null && auditInfo.AuditStatus == GoodsConfirmEnum.Confirmed)
  1392. {
  1393. auditDep = GoodsAuditDepEnum.Hr;
  1394. //多人审核的时候验证审核权限
  1395. var auditList = GoodsStorageConfirmAuditDep(2);
  1396. var auditDepInfo = auditList.Find(x => x.AuditorIds.Contains(userId));
  1397. if (auditDepInfo == null)
  1398. {
  1399. _jv.Msg = string.Format("未分配出库确认权限!");
  1400. _sqlSugar.RollbackTran();
  1401. return _jv;
  1402. }
  1403. }
  1404. // 确认中 确认 拒绝
  1405. _jv = await GoodsReceiveOutConfirmingMultiple(receiveInfo, userId, auditDep, auditEnum);
  1406. //switch (auditEnum)
  1407. //{
  1408. // case GoodsAuditEnum.Pending: //领用待确认(取消确认、取消拒绝)
  1409. // _jv = await GoodsReceivePending(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1410. // break;
  1411. // case GoodsAuditEnum.Approved: //领用确认
  1412. // _jv = await GoodsReceiveApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1413. // break;
  1414. // case GoodsAuditEnum.UnApproved: //领用拒绝
  1415. // _jv = await GoodsReceiveUnApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1416. // break;
  1417. // case GoodsAuditEnum.OutPending: //出库待确认(取消出库确认、取消出库拒绝)
  1418. // _jv = await GoodsReceiveOutPending(preChangeStatus, receiveInfo, userId, auditDep, auditEnum);
  1419. // break;
  1420. // case GoodsAuditEnum.OutConfirmed: //出库确认
  1421. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1422. // break;
  1423. // case GoodsAuditEnum.OutRejected: //出库拒绝
  1424. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1425. // break;
  1426. //}
  1427. }
  1428. else //单人审核
  1429. {
  1430. //领用确认 --> 已确认 / 领用拒绝 --> 已拒绝
  1431. _jv = await GoodsReceiveOutConfirmingSingle(receiveInfo,userId, currUserName,auditEnum);
  1432. }
  1433. if (_jv.Code == StatusCodes.Status200OK)
  1434. {
  1435. _sqlSugar.CommitTran();
  1436. _jv.Msg = $"操作成功!";
  1437. _jv.Data = auditEnum;
  1438. return _jv;
  1439. }
  1440. _sqlSugar.RollbackTran();
  1441. return _jv;
  1442. }
  1443. #region 多人审核状态变更
  1444. /// <summary>
  1445. /// 物品领用状态 - 领用待确认(取消确认、取消拒绝)
  1446. /// </summary>
  1447. /// <param name="preChangeStatus">更改前状态</param>
  1448. /// <param name="receiveId"></param>
  1449. /// <param name="userId"></param>
  1450. /// <param name="userName"></param>
  1451. /// <param name="auditEnum"></param>
  1452. /// <returns></returns>
  1453. public async Task<JsonView> GoodsReceivePending(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1454. {
  1455. _jv.Code = StatusCodes.Status400BadRequest;
  1456. //领用待确认 操作范围
  1457. var audirEnumPers = new List<GoodsAuditEnum>() {
  1458. GoodsAuditEnum.Approved,
  1459. GoodsAuditEnum.UnApproved
  1460. };
  1461. if (!audirEnumPers.Contains(preChangeStatus))
  1462. {
  1463. _jv.Msg = $"{GoodsAuditEnum.Approved.GetEnumDescription()}、{GoodsAuditEnum.UnApproved.GetEnumDescription()},状态下可取消操作!";
  1464. return _jv;
  1465. }
  1466. var currUserOpDt = DateTime.Now;
  1467. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1468. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1469. auditEnum = GoodsAuditEnum.Pending;
  1470. var changeStatus = await _sqlSugar
  1471. .Updateable<Pm_GoodsReceive>()
  1472. .SetColumns(x => new Pm_GoodsReceive()
  1473. {
  1474. AuditStatus = auditEnum,
  1475. AuditUserId = userId,
  1476. AuditTime = currUserOpDt,
  1477. StatusDesc = statusDesc
  1478. })
  1479. .Where(x => x.Id == receiveId)
  1480. .ExecuteCommandAsync();
  1481. if (changeStatus > 0)
  1482. {
  1483. _jv.Code = StatusCodes.Status200OK;
  1484. }
  1485. return _jv;
  1486. }
  1487. /// <summary>
  1488. /// 物品领用状态 - 领用确认
  1489. /// </summary>
  1490. /// <param name="preChangeStatus">更改前状态</param>
  1491. /// <param name="receiveId"></param>
  1492. /// <param name="userId"></param>
  1493. /// <param name="userName"></param>
  1494. /// <param name="auditEnum"></param>
  1495. /// <returns></returns>
  1496. public async Task<JsonView> GoodsReceiveApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1497. {
  1498. _jv.Code = StatusCodes.Status400BadRequest;
  1499. if (preChangeStatus != GoodsAuditEnum.Pending)
  1500. {
  1501. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1502. return _jv;
  1503. }
  1504. var currUserOpDt = DateTime.Now;
  1505. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1506. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1507. auditEnum = GoodsAuditEnum.OutPending;
  1508. var changeStatus = await _sqlSugar
  1509. .Updateable<Pm_GoodsReceive>()
  1510. .SetColumns(x => new Pm_GoodsReceive()
  1511. {
  1512. AuditStatus = auditEnum,
  1513. AuditUserId = userId,
  1514. AuditTime = currUserOpDt,
  1515. StatusDesc = statusDesc
  1516. })
  1517. .Where(x => x.Id == receiveId)
  1518. .ExecuteCommandAsync();
  1519. if (changeStatus > 0)
  1520. {
  1521. _jv.Code = StatusCodes.Status200OK;
  1522. }
  1523. return _jv;
  1524. }
  1525. /// <summary>
  1526. /// 物品领用状态 - 领用拒绝
  1527. /// </summary>
  1528. /// <param name="preChangeStatus">更改前状态</param>
  1529. /// <param name="receiveId"></param>
  1530. /// <param name="userId"></param>
  1531. /// <param name="userName"></param>
  1532. /// <param name="auditEnum"></param>
  1533. /// <returns></returns>
  1534. public async Task<JsonView> GoodsReceiveUnApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1535. {
  1536. _jv.Code = StatusCodes.Status400BadRequest;
  1537. if (preChangeStatus != GoodsAuditEnum.Pending)
  1538. {
  1539. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1540. return _jv;
  1541. }
  1542. auditEnum = GoodsAuditEnum.UnApproved;
  1543. var currUserOpDt = DateTime.Now;
  1544. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1545. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部认:状态:待确认 审核人:- 审核时间:-;<br/>", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1546. var changeStatus = await _sqlSugar
  1547. .Updateable<Pm_GoodsReceive>()
  1548. .SetColumns(x => new Pm_GoodsReceive()
  1549. {
  1550. AuditStatus = auditEnum,
  1551. AuditUserId = userId,
  1552. AuditTime = currUserOpDt,
  1553. StatusDesc = statusDesc
  1554. })
  1555. .Where(x => x.Id == receiveId)
  1556. .ExecuteCommandAsync();
  1557. if (changeStatus > 0)
  1558. {
  1559. _jv.Code = StatusCodes.Status200OK;
  1560. }
  1561. return _jv;
  1562. }
  1563. /// <summary>
  1564. /// 物品领用状态 - 出库待确认(出库确认、出库拒绝)
  1565. /// </summary>
  1566. /// <param name="preChangeStatus">更改前状态</param>
  1567. /// <param name="receiveId"></param>
  1568. /// <param name="userId"></param>
  1569. /// <param name="userName"></param>
  1570. /// <param name="auditEnum"></param>
  1571. /// <returns></returns>
  1572. public async Task<JsonView> GoodsReceiveOutPending(GoodsAuditEnum preChangeStatus, Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1573. {
  1574. _jv.Code = StatusCodes.Status400BadRequest;
  1575. //出库待确认 操作范围
  1576. var audirEnumPers = new List<GoodsAuditEnum>() {
  1577. GoodsAuditEnum.OutConfirmed,
  1578. GoodsAuditEnum.OutRejected
  1579. };
  1580. if (!audirEnumPers.Contains(preChangeStatus))
  1581. {
  1582. _jv.Msg = $"{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可取消操作!";
  1583. return _jv;
  1584. }
  1585. var currUserOpDt = DateTime.Now;
  1586. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1587. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1588. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1589. .ToListAsync();
  1590. auditEnum = GoodsAuditEnum.OutPending;
  1591. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1592. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1593. if (auditInfo == null)
  1594. {
  1595. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1596. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1597. if (addStatus < 1)
  1598. {
  1599. _jv.Msg = $"出库确认取消操作失败!";
  1600. return _jv;
  1601. }
  1602. auditInfos.Add(auditInfo);
  1603. }
  1604. else
  1605. {
  1606. //移除旧数据
  1607. auditInfos.Remove(auditInfo);
  1608. auditInfo.AuditStatus = auditStatus;
  1609. auditInfo.AuditUserId = userId;
  1610. auditInfo.AuditTime = currUserOpDt;
  1611. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1612. .SetColumns(x => new Pm_GoodsAudit()
  1613. {
  1614. AuditStatus = auditStatus,
  1615. AuditUserId = userId,
  1616. AuditTime = currUserOpDt
  1617. })
  1618. .Where(x => x.Id == auditInfo.Id)
  1619. .ExecuteCommandAsync();
  1620. if (updStatus < 1)
  1621. {
  1622. _jv.Msg = $"出库确认操作失败!";
  1623. return _jv;
  1624. }
  1625. //添加更新后的数据
  1626. auditInfos.Add(auditInfo);
  1627. }
  1628. //处理状态描述
  1629. StringBuilder statusDesc = new StringBuilder();
  1630. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1631. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1632. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1633. statusDesc.Append(receiveStatusDesc);
  1634. foreach (var auditInf in auditInfos)
  1635. {
  1636. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1637. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1638. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1639. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1640. statusDesc.Append(auditInfStatusDesc);
  1641. }
  1642. //批次库存信息
  1643. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1644. //出库确认取消是单独处理
  1645. if (preChangeStatus == GoodsAuditEnum.OutConfirming)
  1646. {
  1647. if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1648. {
  1649. auditEnum = GoodsAuditEnum.OutConfirming;
  1650. }
  1651. //2.1 更改库存
  1652. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1653. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1654. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1655. goodsInfo.LastUpdateTime = DateTime.Now;
  1656. goodsInfo.LastUpdateUserId = userId;
  1657. var editGoods = await _sqlSugar
  1658. .Updateable(goodsInfo)
  1659. .UpdateColumns(x => new
  1660. {
  1661. x.StockQuantity,
  1662. x.OQ_Total,
  1663. x.LastUpdateUserId,
  1664. x.LastUpdateTime,
  1665. })
  1666. .Where(x => x.Id == receiveInfo.GoodsId)
  1667. .ExecuteCommandAsync();
  1668. if (editGoods < 1)
  1669. {
  1670. _jv.Msg = $"库存更新失败!";
  1671. return _jv;
  1672. }
  1673. //2.2 入库批次关联领用人 更改批次库存
  1674. var goodsStorages = await _sqlSugar
  1675. .Queryable<Pm_GoodsStorage>()
  1676. .Where(x => x.IsDel == 0 &&
  1677. x.GoodsId == receiveInfo.GoodsId &&
  1678. (x.Quantity - x.ReceiveQuantity) > 0
  1679. )
  1680. .OrderBy(x => x.CreateTime)
  1681. .ToListAsync();
  1682. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1683. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1684. var receiveQuantity = 0.00M; //领用总数量
  1685. foreach (var storage in goodsStorages)
  1686. {
  1687. if (receiveInfo.Quantity == receiveQuantity) break;
  1688. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1689. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1690. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1691. const decimal unit = 0.50M;
  1692. while (receiveQuantity < receiveInfo.Quantity)
  1693. {
  1694. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1695. thisBatchReceiveQuantity += unit;
  1696. receiveQuantity += unit;
  1697. }
  1698. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1699. {
  1700. StorageId = storage.Id,
  1701. Quantity = thisBatchReceiveQuantity
  1702. });
  1703. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1704. var storageUpd = storage;
  1705. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1706. batchStorageInfos.Add(storageUpd);
  1707. }
  1708. //2.2.1 更改批次库存
  1709. if (goodsReceiveInfos.Count > 0)
  1710. {
  1711. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1712. .UpdateColumns(x => x.ReceiveQuantity)
  1713. .WhereColumns(x => x.Id)
  1714. .ExecuteCommandAsync();
  1715. if (edit1 < 1)
  1716. {
  1717. _jv.Msg = $"批次库存更新失败!";
  1718. return _jv;
  1719. }
  1720. }
  1721. //2.2.2 添加入库批次关联领用人
  1722. if (goodsReceiveInfos.Count > 0)
  1723. {
  1724. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1725. }
  1726. }
  1727. var statusDesc1 = statusDesc.ToString();
  1728. var changeStatus = await _sqlSugar
  1729. .Updateable<Pm_GoodsReceive>()
  1730. .SetColumns(x => new Pm_GoodsReceive()
  1731. {
  1732. AuditStatus = auditEnum,
  1733. StatusDesc = statusDesc1,
  1734. GoodsStorageInfo = goodsStorageInfo
  1735. })
  1736. .Where(x => x.Id == receiveInfo.Id)
  1737. .ExecuteCommandAsync();
  1738. if (changeStatus > 0)
  1739. {
  1740. _jv.Code = StatusCodes.Status200OK;
  1741. }
  1742. return _jv;
  1743. }
  1744. /// <summary>
  1745. /// 物品领用状态 - 出库确认中、出库确认完成、出库确认拒绝
  1746. /// </summary>
  1747. /// <param name="receiveInfo"></param>
  1748. /// <param name="userId"></param>
  1749. /// <param name="depEnum"></param>
  1750. /// <param name="auditEnum"></param>
  1751. /// <returns></returns>
  1752. public async Task<JsonView> GoodsReceiveOutConfirming(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1753. {
  1754. _jv.Code = StatusCodes.Status400BadRequest;
  1755. //更改前状态
  1756. var preChangeStatus = receiveInfo.AuditStatus;
  1757. if (preChangeStatus < GoodsAuditEnum.OutPending)
  1758. {
  1759. _jv.Msg = $"领用确认执行成功!才可执行出库确认或出库拒绝操作!";
  1760. return _jv;
  1761. }
  1762. var currUserOpDt = DateTime.Now;
  1763. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1764. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1765. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1766. .ToListAsync();
  1767. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1768. if (auditEnum == GoodsAuditEnum.OutRejected) auditStatus = GoodsConfirmEnum.UnApproved;
  1769. else if (auditEnum == GoodsAuditEnum.OutConfirmed) auditStatus = GoodsConfirmEnum.Confirmed;
  1770. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1771. if (auditInfo == null)
  1772. {
  1773. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1774. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1775. if (addStatus < 1)
  1776. {
  1777. _jv.Msg = $"出库确认操作失败!";
  1778. return _jv;
  1779. }
  1780. auditInfos.Add(auditInfo);
  1781. }
  1782. else
  1783. {
  1784. //移除旧数据
  1785. auditInfos.Remove(auditInfo);
  1786. auditInfo.AuditStatus = auditStatus;
  1787. auditInfo.AuditUserId = userId;
  1788. auditInfo.AuditTime = currUserOpDt;
  1789. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1790. .SetColumns(x => new Pm_GoodsAudit()
  1791. {
  1792. AuditStatus = auditStatus,
  1793. AuditUserId = userId,
  1794. AuditTime = currUserOpDt
  1795. })
  1796. .Where(x => x.Id == auditInfo.Id)
  1797. .ExecuteCommandAsync();
  1798. if (updStatus < 1)
  1799. {
  1800. _jv.Msg = $"出库确认操作失败!";
  1801. return _jv;
  1802. }
  1803. //添加更新后的数据
  1804. auditInfos.Add(auditInfo);
  1805. }
  1806. //处理状态描述
  1807. StringBuilder statusDesc = new StringBuilder();
  1808. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1809. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1810. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1811. statusDesc.Append(receiveStatusDesc);
  1812. foreach (var auditInf in auditInfos)
  1813. {
  1814. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1815. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1816. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1817. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1818. statusDesc.Append(auditInfStatusDesc);
  1819. }
  1820. if (preChangeStatus == auditEnum)
  1821. {
  1822. _jv.Msg = $"此操作已执行,不可重复操作!";
  1823. return _jv;
  1824. }
  1825. //批次库存信息
  1826. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1827. //更改领用状态及库存
  1828. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  1829. {
  1830. //出库确认拒绝
  1831. auditEnum = GoodsAuditEnum.OutRejected;
  1832. }
  1833. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  1834. {
  1835. //1.出库确认完成
  1836. auditEnum = GoodsAuditEnum.OutConfirmed;
  1837. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  1838. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  1839. {
  1840. //2.1 更改库存
  1841. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1842. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1843. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1844. goodsInfo.LastUpdateTime = DateTime.Now;
  1845. goodsInfo.LastUpdateUserId = userId;
  1846. var editGoods = await _sqlSugar
  1847. .Updateable(goodsInfo)
  1848. .UpdateColumns(x => new
  1849. {
  1850. x.StockQuantity,
  1851. x.OQ_Total,
  1852. x.LastUpdateUserId,
  1853. x.LastUpdateTime,
  1854. })
  1855. .Where(x => x.Id == receiveInfo.GoodsId)
  1856. .ExecuteCommandAsync();
  1857. if (editGoods < 1)
  1858. {
  1859. _jv.Msg = $"库存更新失败!";
  1860. return _jv;
  1861. }
  1862. //2.2 入库批次关联领用人 更改批次库存
  1863. var goodsStorages = await _sqlSugar
  1864. .Queryable<Pm_GoodsStorage>()
  1865. .Where(x => x.IsDel == 0 &&
  1866. x.GoodsId == receiveInfo.GoodsId &&
  1867. (x.Quantity - x.ReceiveQuantity) > 0
  1868. )
  1869. .OrderBy(x => x.CreateTime)
  1870. .ToListAsync();
  1871. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1872. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1873. var receiveQuantity = 0.00M; //领用总数量
  1874. foreach (var storage in goodsStorages)
  1875. {
  1876. if (receiveInfo.Quantity == receiveQuantity) break;
  1877. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1878. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1879. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1880. const decimal unit = 0.50M;
  1881. while (receiveQuantity < receiveInfo.Quantity)
  1882. {
  1883. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1884. thisBatchReceiveQuantity += unit;
  1885. receiveQuantity += unit;
  1886. }
  1887. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1888. {
  1889. StorageId = storage.Id,
  1890. Quantity = thisBatchReceiveQuantity
  1891. });
  1892. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1893. var storageUpd = storage;
  1894. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1895. batchStorageInfos.Add(storageUpd);
  1896. }
  1897. //2.2.1 更改批次库存
  1898. if (goodsReceiveInfos.Count > 0)
  1899. {
  1900. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1901. .UpdateColumns(x => x.ReceiveQuantity)
  1902. .WhereColumns(x => x.Id)
  1903. .ExecuteCommandAsync();
  1904. if (edit1 < 1)
  1905. {
  1906. _jv.Msg = $"批次库存更新失败!";
  1907. return _jv;
  1908. }
  1909. }
  1910. //2.2.2 添加入库批次关联领用人
  1911. if (goodsReceiveInfos.Count > 0)
  1912. {
  1913. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1914. }
  1915. }
  1916. }
  1917. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1918. {
  1919. //出库确认中
  1920. auditEnum = GoodsAuditEnum.OutConfirming;
  1921. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  1922. {
  1923. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  1924. if (!string.IsNullOrEmpty(goodsStorageInfo))
  1925. {
  1926. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  1927. if (goodsStorageInfos.Any())
  1928. {
  1929. foreach (var item in goodsStorageInfos)
  1930. {
  1931. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  1932. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  1933. .FirstAsync();
  1934. if (newStorageInfo == null) continue;
  1935. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  1936. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  1937. .ExecuteCommandAsync();
  1938. if (newEdit < 1)
  1939. {
  1940. _jv.Msg = $"批次领用库存更新失败!";
  1941. return _jv;
  1942. }
  1943. }
  1944. }
  1945. }
  1946. }
  1947. }
  1948. else
  1949. {
  1950. auditEnum = GoodsAuditEnum.OutPending;
  1951. }
  1952. var statusDesc1 = statusDesc.ToString();
  1953. var changeStatus = await _sqlSugar
  1954. .Updateable<Pm_GoodsReceive>()
  1955. .SetColumns(x => new Pm_GoodsReceive()
  1956. {
  1957. AuditStatus = auditEnum,
  1958. StatusDesc = statusDesc1,
  1959. GoodsStorageInfo = goodsStorageInfo,
  1960. })
  1961. .Where(x => x.Id == receiveInfo.Id)
  1962. .ExecuteCommandAsync();
  1963. if (changeStatus > 0)
  1964. {
  1965. _jv.Code = StatusCodes.Status200OK;
  1966. }
  1967. return _jv;
  1968. }
  1969. /// <summary>
  1970. ///
  1971. /// </summary>
  1972. /// <param name="receiveInfo"></param>
  1973. /// <param name="userId"></param>
  1974. /// <param name="depEnum"></param>
  1975. /// <param name="auditEnum"></param>
  1976. /// <returns></returns>
  1977. public async Task<JsonView> GoodsReceiveOutConfirmingMultiple(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1978. {
  1979. _jv.Code = StatusCodes.Status400BadRequest;
  1980. //更改前状态
  1981. receiveInfo.CreateUserId = userId;
  1982. var preChangeStatus = receiveInfo.AuditStatus;
  1983. var currUserOpDt = DateTime.Now;
  1984. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1985. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1986. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1987. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1988. .ToListAsync();
  1989. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1990. if (auditEnum == GoodsAuditEnum.UnApproved) auditStatus = GoodsConfirmEnum.UnApproved;
  1991. else if (auditEnum == GoodsAuditEnum.Approved) auditStatus = GoodsConfirmEnum.Confirmed;
  1992. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1993. if (auditInfo == null)
  1994. {
  1995. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1996. auditInfo.AuditUserId = userId;
  1997. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1998. if (addStatus < 1)
  1999. {
  2000. _jv.Msg = $"出库确认操作失败!";
  2001. return _jv;
  2002. }
  2003. auditInfos.Add(auditInfo);
  2004. }
  2005. else
  2006. {
  2007. //移除旧数据
  2008. auditInfos.Remove(auditInfo);
  2009. auditInfo.AuditStatus = auditStatus;
  2010. auditInfo.AuditUserId = userId;
  2011. auditInfo.AuditTime = currUserOpDt;
  2012. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2013. .SetColumns(x => new Pm_GoodsAudit()
  2014. {
  2015. AuditStatus = auditStatus,
  2016. AuditUserId = userId,
  2017. AuditTime = currUserOpDt
  2018. })
  2019. .Where(x => x.Id == auditInfo.Id)
  2020. .ExecuteCommandAsync();
  2021. if (updStatus < 1)
  2022. {
  2023. _jv.Msg = $"出库确认操作失败!";
  2024. return _jv;
  2025. }
  2026. //添加更新后的数据
  2027. auditInfos.Add(auditInfo);
  2028. }
  2029. auditInfos = auditInfos.OrderBy(x => x.Dep).ToList();
  2030. //处理状态描述
  2031. StringBuilder statusDesc = new StringBuilder();
  2032. foreach (var auditInf in auditInfos)
  2033. {
  2034. string auditType = auditInf.Dep == GoodsAuditDepEnum.Hr ? "人事部" : "领用确认";
  2035. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  2036. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  2037. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  2038. auditType, auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  2039. statusDesc.Append(auditInfStatusDesc);
  2040. }
  2041. if (preChangeStatus == auditEnum)
  2042. {
  2043. _jv.Msg = $"此操作已执行,不可重复操作!";
  2044. return _jv;
  2045. }
  2046. //批次库存信息
  2047. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2048. //更改领用状态及库存
  2049. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  2050. {
  2051. //出库确认拒绝
  2052. auditEnum = GoodsAuditEnum.OutRejected;
  2053. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2054. {
  2055. //回滚库存
  2056. _jv = await GoodsInventoryRollback(receiveInfo);
  2057. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2058. }
  2059. }
  2060. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  2061. {
  2062. //1.出库确认完成
  2063. auditEnum = GoodsAuditEnum.OutConfirmed;
  2064. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  2065. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  2066. {
  2067. //2.1 更改库存
  2068. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2069. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2070. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2071. goodsInfo.LastUpdateTime = DateTime.Now;
  2072. goodsInfo.LastUpdateUserId = userId;
  2073. var editGoods = await _sqlSugar
  2074. .Updateable(goodsInfo)
  2075. .UpdateColumns(x => new
  2076. {
  2077. x.StockQuantity,
  2078. x.OQ_Total,
  2079. x.LastUpdateUserId,
  2080. x.LastUpdateTime,
  2081. })
  2082. .Where(x => x.Id == receiveInfo.GoodsId)
  2083. .ExecuteCommandAsync();
  2084. if (editGoods < 1)
  2085. {
  2086. _jv.Msg = $"库存更新失败!";
  2087. return _jv;
  2088. }
  2089. //2.2 入库批次关联领用人 更改批次库存
  2090. var goodsStorages = await _sqlSugar
  2091. .Queryable<Pm_GoodsStorage>()
  2092. .Where(x => x.IsDel == 0 &&
  2093. x.GoodsId == receiveInfo.GoodsId &&
  2094. (x.Quantity - x.ReceiveQuantity) > 0
  2095. )
  2096. .OrderBy(x => x.CreateTime)
  2097. .ToListAsync();
  2098. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2099. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2100. var receiveQuantity = 0.00M; //领用总数量
  2101. foreach (var storage in goodsStorages)
  2102. {
  2103. if (receiveInfo.Quantity == receiveQuantity) break;
  2104. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2105. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2106. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2107. const decimal unit = 0.50M;
  2108. while (receiveQuantity < receiveInfo.Quantity)
  2109. {
  2110. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2111. thisBatchReceiveQuantity += unit;
  2112. receiveQuantity += unit;
  2113. }
  2114. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2115. {
  2116. StorageId = storage.Id,
  2117. Quantity = thisBatchReceiveQuantity
  2118. });
  2119. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2120. var storageUpd = storage;
  2121. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2122. batchStorageInfos.Add(storageUpd);
  2123. }
  2124. //2.2.1 更改批次库存
  2125. if (goodsReceiveInfos.Count > 0)
  2126. {
  2127. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2128. .UpdateColumns(x => x.ReceiveQuantity)
  2129. .WhereColumns(x => x.Id)
  2130. .ExecuteCommandAsync();
  2131. if (edit1 < 1)
  2132. {
  2133. _jv.Msg = $"批次库存更新失败!";
  2134. return _jv;
  2135. }
  2136. }
  2137. //2.2.2 添加入库批次关联领用人
  2138. if (goodsReceiveInfos.Count > 0)
  2139. {
  2140. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2141. }
  2142. }
  2143. }
  2144. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  2145. {
  2146. //出库确认中
  2147. auditEnum = GoodsAuditEnum.OutConfirming;
  2148. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2149. {
  2150. //回滚库存
  2151. _jv = await GoodsInventoryRollback(receiveInfo);
  2152. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2153. }
  2154. }
  2155. else auditEnum = GoodsAuditEnum.OutPending;
  2156. var statusDesc1 = statusDesc.ToString();
  2157. var changeStatus = await _sqlSugar
  2158. .Updateable<Pm_GoodsReceive>()
  2159. .SetColumns(x => new Pm_GoodsReceive()
  2160. {
  2161. AuditStatus = auditEnum,
  2162. StatusDesc = statusDesc1,
  2163. GoodsStorageInfo = goodsStorageInfo,
  2164. })
  2165. .Where(x => x.Id == receiveInfo.Id)
  2166. .ExecuteCommandAsync();
  2167. if (changeStatus > 0)
  2168. {
  2169. _jv.Code = StatusCodes.Status200OK;
  2170. }
  2171. return _jv;
  2172. }
  2173. /// <summary>
  2174. /// 物品库存回滚
  2175. /// </summary>
  2176. /// <param name="receiveInfo"></param>
  2177. /// <returns></returns>
  2178. public async Task<JsonView> GoodsInventoryRollback(Pm_GoodsReceive receiveInfo)
  2179. {
  2180. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2181. //回滚库存
  2182. var changeGoods = await _sqlSugar
  2183. .Updateable(goodsInfo)
  2184. .ReSetValue(it => {
  2185. it.StockQuantity += receiveInfo.Quantity;
  2186. it.OQ_Total -= receiveInfo.Quantity;
  2187. it.LastUpdateTime = DateTime.Now;
  2188. it.LastUpdateUserId = receiveInfo.CreateUserId;
  2189. })
  2190. .ExecuteCommandAsync();
  2191. if (changeGoods < 1)
  2192. {
  2193. _jv.Msg = $"库存回滚失败!";
  2194. return _jv;
  2195. }
  2196. //批次领用记录
  2197. _jv.Code = StatusCodes.Status200OK;
  2198. var goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2199. if (string.IsNullOrEmpty(goodsStorageInfo)) return _jv;
  2200. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2201. if (!goodsStorageInfos.Any()) return _jv;
  2202. foreach (var item in goodsStorageInfos)
  2203. {
  2204. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2205. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2206. .FirstAsync();
  2207. if (newStorageInfo == null) continue;
  2208. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2209. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2210. .ExecuteCommandAsync();
  2211. if (newEdit < 1)
  2212. {
  2213. _jv.Code = StatusCodes.Status400BadRequest;
  2214. _jv.Msg = $"批次领用库存更新失败!";
  2215. return _jv;
  2216. }
  2217. }
  2218. _jv.Code = StatusCodes.Status200OK;
  2219. return _jv;
  2220. }
  2221. #endregion
  2222. #region 单人审核状态变更
  2223. /// <summary>
  2224. /// 物品领用状态 - 出库确认完成、出库确认拒绝
  2225. /// </summary>
  2226. /// <param name="receiveInfo"></param>
  2227. /// <param name="userId"></param>
  2228. /// <param name="currUserName"></param>
  2229. /// <param name="auditEnum"></param>
  2230. /// <returns></returns>
  2231. public async Task<JsonView> GoodsReceiveOutConfirmingSingle(Pm_GoodsReceive receiveInfo, int userId,string currUserName,GoodsAuditEnum auditEnum)
  2232. {
  2233. _jv.Code = StatusCodes.Status400BadRequest;
  2234. //状态操作范围验证
  2235. var auditEnums = new List<GoodsAuditEnum>() {
  2236. GoodsAuditEnum.Pending,
  2237. GoodsAuditEnum.Approved,
  2238. GoodsAuditEnum.UnApproved,
  2239. };
  2240. if (!auditEnums.Contains(auditEnum))
  2241. {
  2242. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()}、{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可操作!";
  2243. return _jv;
  2244. }
  2245. var currUserOpDt = DateTime.Now;
  2246. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  2247. //更改前状态
  2248. var preChangeStatus = receiveInfo.AuditStatus;
  2249. //前台状态验证
  2250. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>().FirstAsync(x => x.IsDel == 0 && x.Type == 2 && x.DataId == receiveInfo.Id);
  2251. if (auditInfo == null)
  2252. {
  2253. var addInfo = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, receiveInfo.Id, GoodsConfirmEnum.WaitConfirm, userId);
  2254. auditInfo = await _sqlSugar.Insertable(addInfo).ExecuteReturnEntityAsync();
  2255. if (auditInfo == null)
  2256. {
  2257. _jv.Msg = $"确认操作失败!";
  2258. return _jv;
  2259. }
  2260. }
  2261. if (auditEnum == GoodsAuditEnum.Approved)
  2262. {
  2263. auditInfo.AuditStatus = GoodsConfirmEnum.Confirmed;
  2264. auditEnum = GoodsAuditEnum.OutConfirmed;
  2265. }
  2266. else if (auditEnum == GoodsAuditEnum.UnApproved)
  2267. {
  2268. auditInfo.AuditStatus = GoodsConfirmEnum.UnApproved;
  2269. auditEnum = GoodsAuditEnum.OutRejected;
  2270. }
  2271. if (preChangeStatus == auditEnum)
  2272. {
  2273. _jv.Msg = $"此操作已执行,不可重复操作!";
  2274. return _jv;
  2275. }
  2276. //更新子表审核状态
  2277. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2278. .SetColumns(x => new Pm_GoodsAudit()
  2279. {
  2280. AuditStatus = auditInfo.AuditStatus,
  2281. AuditUserId = userId,
  2282. AuditTime = currUserOpDt
  2283. })
  2284. .Where(x => x.Id == auditInfo.Id)
  2285. .ExecuteCommandAsync();
  2286. if (updStatus < 1)
  2287. {
  2288. _jv.Msg = $"确认操作失败!";
  2289. return _jv;
  2290. }
  2291. //处理状态描述
  2292. StringBuilder statusDesc = new StringBuilder();
  2293. statusDesc.Append(string.Format("领用确认:状态:{0} 审核人:{1} 审核时间:{2};", auditInfo.AuditStatus.GetEnumDescription(), currUserName, currUserOpTime));
  2294. //批次库存信息
  2295. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2296. if (auditEnum == GoodsAuditEnum.OutConfirmed) //出库确认
  2297. {
  2298. //1 更改库存
  2299. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2300. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2301. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2302. goodsInfo.LastUpdateTime = DateTime.Now;
  2303. goodsInfo.LastUpdateUserId = userId;
  2304. var editGoods = await _sqlSugar.Updateable(goodsInfo)
  2305. .UpdateColumns(x => new
  2306. {
  2307. x.StockQuantity,
  2308. x.OQ_Total,
  2309. x.LastUpdateUserId,
  2310. x.LastUpdateTime,
  2311. })
  2312. .Where(x => x.Id == receiveInfo.GoodsId)
  2313. .ExecuteCommandAsync();
  2314. if (editGoods < 1)
  2315. {
  2316. _jv.Msg = $"库存更新失败!";
  2317. return _jv;
  2318. }
  2319. //2 入库批次关联领用人 更改批次库存
  2320. var goodsStorages = await _sqlSugar
  2321. .Queryable<Pm_GoodsStorage>()
  2322. .Where(x => x.IsDel == 0 &&
  2323. x.GoodsId == receiveInfo.GoodsId &&
  2324. (x.Quantity - x.ReceiveQuantity) > 0
  2325. )
  2326. .OrderBy(x => x.CreateTime)
  2327. .ToListAsync();
  2328. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2329. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2330. var receiveQuantity = 0.00M; //领用总数量
  2331. foreach (var storage in goodsStorages)
  2332. {
  2333. if (receiveInfo.Quantity == receiveQuantity) break;
  2334. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2335. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2336. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2337. const decimal unit = 0.50M;
  2338. while (receiveQuantity < receiveInfo.Quantity)
  2339. {
  2340. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2341. thisBatchReceiveQuantity += unit;
  2342. receiveQuantity += unit;
  2343. }
  2344. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2345. {
  2346. StorageId = storage.Id,
  2347. Quantity = thisBatchReceiveQuantity
  2348. });
  2349. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2350. var storageUpd = storage;
  2351. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2352. batchStorageInfos.Add(storageUpd);
  2353. }
  2354. //3 更改批次库存
  2355. if (goodsReceiveInfos.Count > 0)
  2356. {
  2357. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2358. .UpdateColumns(x => x.ReceiveQuantity)
  2359. .WhereColumns(x => x.Id)
  2360. .ExecuteCommandAsync();
  2361. if (edit1 < 1)
  2362. {
  2363. _jv.Msg = $"批次库存更新失败!";
  2364. return _jv;
  2365. }
  2366. }
  2367. //4 添加入库批次关联领用人
  2368. if (goodsReceiveInfos.Count > 0)
  2369. {
  2370. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2371. }
  2372. }
  2373. else if (auditEnum == GoodsAuditEnum.OutRejected) //出库拒绝
  2374. {
  2375. //拒绝之前的状态为已完成 回滚库存
  2376. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2377. {
  2378. //回滚库存
  2379. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2380. var changeGoods = await _sqlSugar
  2381. .Updateable(goodsInfo)
  2382. .ReSetValue(it => {
  2383. it.StockQuantity += receiveInfo.Quantity;
  2384. it.OQ_Total -= receiveInfo.Quantity;
  2385. it.LastUpdateTime = DateTime.Now;
  2386. it.LastUpdateUserId = userId;
  2387. })
  2388. .ExecuteCommandAsync();
  2389. if (changeGoods < 1)
  2390. {
  2391. _jv.Msg = $"库存回滚失败!";
  2392. return _jv;
  2393. }
  2394. //批次号库存 信息更新
  2395. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  2396. if (!string.IsNullOrEmpty(goodsStorageInfo))
  2397. {
  2398. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2399. if (goodsStorageInfos.Any())
  2400. {
  2401. foreach (var item in goodsStorageInfos)
  2402. {
  2403. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2404. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2405. .FirstAsync();
  2406. if (newStorageInfo == null) continue;
  2407. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2408. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2409. .ExecuteCommandAsync();
  2410. if (newEdit < 1)
  2411. {
  2412. _jv.Msg = $"批次领用库存更新失败!";
  2413. return _jv;
  2414. }
  2415. }
  2416. }
  2417. }
  2418. }
  2419. }
  2420. var statusDesc1 = statusDesc.ToString();
  2421. var changeStatus = await _sqlSugar
  2422. .Updateable<Pm_GoodsReceive>()
  2423. .SetColumns(x => new Pm_GoodsReceive()
  2424. {
  2425. AuditStatus = auditEnum,
  2426. StatusDesc = statusDesc1,
  2427. GoodsStorageInfo = goodsStorageInfo,
  2428. })
  2429. .Where(x => x.Id == receiveInfo.Id)
  2430. .ExecuteCommandAsync();
  2431. if (changeStatus > 0) _jv.Code = StatusCodes.Status200OK;
  2432. return _jv;
  2433. }
  2434. #endregion
  2435. /// <summary>
  2436. /// 物品领用 Del
  2437. /// </summary>
  2438. /// <param name="id"></param>
  2439. /// <param name="currUserId"></param>
  2440. /// <returns></returns>
  2441. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  2442. {
  2443. var receiveInfo = await _sqlSugar
  2444. .Queryable<Pm_GoodsReceive>()
  2445. .Where(x => x.IsDel == 0 && x.Id == id)
  2446. .FirstAsync();
  2447. if (receiveInfo.AuditStatus >= GoodsAuditEnum.Approved)
  2448. {
  2449. _jv.Msg = $"该条数据已进入审批流程,不可删除!";
  2450. return _jv;
  2451. }
  2452. var edit = await _sqlSugar
  2453. .Updateable<Pm_GoodsReceive>()
  2454. .SetColumns(x => new Pm_GoodsReceive()
  2455. {
  2456. IsDel = 1,
  2457. DeleteUserId = currUserId,
  2458. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  2459. })
  2460. .Where(x => x.Id == id)
  2461. .ExecuteCommandAsync();
  2462. if (edit > 0)
  2463. {
  2464. _jv.Msg = $"操作成功!";
  2465. _jv.Code = StatusCodes.Status200OK;
  2466. return _jv;
  2467. }
  2468. return _jv;
  2469. }
  2470. }
  2471. }