GoodsRepository.cs 124 KB

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