GoodsRepository.cs 113 KB

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