GoodsRepository.cs 113 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788
  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. //物品ID和物品名称只能传一个
  1052. if (dto.GoodsId > 0) dto.GoodsName = string.Empty;
  1053. if (!string.IsNullOrEmpty(dto.GoodsName)) dto.GoodsId = 0;
  1054. //财务人事审核权限处理
  1055. var auditList = GoodsStorageConfirmAuditDep(2);
  1056. bool hrAuditPer = false;
  1057. var hrAuditInfo = auditList.FirstOrDefault(x => x.AuditDep == GoodsAuditDepEnum.Hr);
  1058. if (hrAuditInfo.AuditorIds.Any(x => x == currUserId))
  1059. {
  1060. hrAuditPer = true;
  1061. }
  1062. var beginBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.BeginDt) ? $"{dto.BeginDt} 00:00:00" : string.Empty, out var begin);
  1063. var endBool = DateTime.TryParse(!string.IsNullOrEmpty(dto.EndDt) ? $"{dto.EndDt} 00:00:00" : string.Empty, out var end);
  1064. RefAsync<int> total = 0;
  1065. var data = _sqlSugar.Queryable<Pm_GoodsReceive>()
  1066. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1067. .LeftJoin<Sys_SetData>((gr, gi, sd) => gi.Type == sd.Id)
  1068. .LeftJoin<Sys_Users>((gr, gi, sd, u1) => gr.AuditUserId == u1.Id)
  1069. .LeftJoin<Sys_Users>((gr, gi, sd, u1, u2) => gr.CreateUserId == u2.Id)
  1070. .LeftJoin<Grp_DelegationInfo>((gr, gi, sd, u1, u2, di) => gr.GroupId == di.Id)
  1071. .Where((gr, gi, sd, u1, u2, di) => gr.IsDel == 0)
  1072. .WhereIF(dto.GoodsId > 0, (gr, gi, sd, u1, u2, di) => gr.GoodsId == dto.GoodsId)
  1073. .WhereIF(!string.IsNullOrEmpty(dto.GoodsName), (gr, gi, sd, u1, u2, di) => gi.Name.Contains(dto.GoodsName))
  1074. .WhereIF(auditLabel.Length > 0, (gr, gi, sd, u1, u2, di) => auditLabel.Contains((int)gr.AuditStatus))
  1075. .WhereIF(typeLabel.Length > 0, (gr, gi, sd, u1, u2, di) => typeLabel.Contains(gi.Type))
  1076. .WhereIF(userLabel.Length > 0, (gr, gi, sd, u1, u2, di) => userLabel.Contains(gr.CreateUserId))
  1077. .WhereIF(groupLabel.Length > 0, (gr, gi, sd, u1, u2, di) => groupLabel.Contains(gr.GroupId))
  1078. .WhereIF(beginBool && endBool, (gr, gi, sd, u1, u2, di) => gr.CreateTime >= begin && gr.CreateTime <= end)
  1079. .WhereIF(hrAuditPer, (gr, gi, sd, u1, u2, di) => _goodsTypeIds.Contains(gi.Type))
  1080. .Select((gr, gi, sd, u1, u2, di) => new GoodsReceiveListMobileView
  1081. {
  1082. Id = gr.Id,
  1083. GroupId = gr.GroupId,
  1084. GroupName = di.TeamName,
  1085. GoodsId = gr.GoodsId,
  1086. GoodsName = gi.Name,
  1087. GoodsTypeId = gi.Type,
  1088. GoodsType = sd.Name,
  1089. Quantity = gr.Quantity,
  1090. Unit = gi.Unit,
  1091. Reason = gr.Reason,
  1092. Remark = gr.Remark,
  1093. AuditStatus = gr.AuditStatus,
  1094. StatusDesc = gr.StatusDesc,
  1095. AuditUserId = gr.AuditUserId,
  1096. AuditUserName = u1.CnName,
  1097. AuditTime = gr.AuditTime,
  1098. CreateUserName = u2.CnName,
  1099. CreateTime = gr.CreateTime
  1100. })
  1101. .OrderByDescending(gr => gr.CreateTime);
  1102. //excel导出
  1103. if (dto.IsExcelDownload)
  1104. {
  1105. var fileName = $"物资领用{Guid.NewGuid()}.xlsx";
  1106. var excelTempPath = $"{_excelPath}Template/物资领用Temp.xlsx";
  1107. if (!File.Exists(excelTempPath))
  1108. {
  1109. _jv.Code = StatusCodes.Status204NoContent;
  1110. _jv.Msg = $"该模板文件不存在!";
  1111. return _jv;
  1112. }
  1113. _url = $"{_url}Office/Excel/GoodsFiles/";
  1114. _excelPath = $"{_excelPath}GoodsFiles";
  1115. if (!Directory.Exists(_excelPath))
  1116. {
  1117. Directory.CreateDirectory(_excelPath);
  1118. }
  1119. //载入模板
  1120. WorkbookDesigner designer = new WorkbookDesigner();
  1121. designer.Workbook = new Workbook(excelTempPath);
  1122. var tableData = await data.ToListAsync();
  1123. designer.SetDataSource("Export", tableData);
  1124. designer.Process();
  1125. string serverPath = $"{_url}{fileName}";
  1126. designer.Workbook.Save($"{_excelPath}/{fileName}");
  1127. _jv.Code = StatusCodes.Status200OK;
  1128. _jv.Data = new { url = serverPath };
  1129. _jv.Msg = $"操作成功";
  1130. return _jv;
  1131. }
  1132. //返回分页数据
  1133. //领用审核List只显示多条审核数据 陈湘
  1134. var view = await data.ToPageListAsync(dto.PageIndex, dto.PageSize, total);
  1135. foreach (var item in view)
  1136. {
  1137. //默认审核验证 多条 or 单挑
  1138. var isAudit = GoodsAuditType(item.GoodsTypeId);
  1139. var auditPers = new List<GoodsStorageAuditPerView>()
  1140. {
  1141. new (){ AuditPer = true, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"领用确认"},
  1142. };
  1143. if (isAudit) auditPers.Add(new() { AuditPer = hrAuditPer, AuditDep = GoodsAuditDepEnum.Hr, ButtonText = $"出库确认" });
  1144. item.AuditPers = auditPers.ToArray();
  1145. }
  1146. if (dto.PortType == 2 || dto.PortType == 3)
  1147. {
  1148. _jv.Data = view;
  1149. }
  1150. else if (dto.PortType == 1)
  1151. {
  1152. var view1 = _mapper.Map<List<GoodsReceiveListView>>(view);
  1153. _jv.Data = view1;
  1154. }
  1155. _jv.Code = StatusCodes.Status200OK;
  1156. _jv.Count = total;
  1157. _jv.Msg = $"操作成功";
  1158. return _jv;
  1159. }
  1160. /// <summary>
  1161. /// 物品领用详情
  1162. /// </summary>
  1163. /// <param name="portType"></param>
  1164. /// <param name="id"></param>
  1165. /// <returns></returns>
  1166. public async Task<JsonView> GoodsReceiveInfo(int portType, int id)
  1167. {
  1168. var data = await _sqlSugar.Queryable<Pm_GoodsReceive>()
  1169. .LeftJoin<Pm_GoodsInfo>((gr, gi) => gr.GoodsId == gi.Id)
  1170. .LeftJoin<Sys_Users>((gr, gi, u1) => gr.AuditUserId == u1.Id)
  1171. .LeftJoin<Sys_Users>((gr, gi, u1, u2) => gr.CreateUserId == u2.Id)
  1172. .LeftJoin<Grp_DelegationInfo>((gr, gi, u1, u2, di) => gr.GroupId == di.Id)
  1173. .Where((gr, gi, u1, u2, di) => gr.IsDel == 0)
  1174. .WhereIF(id > 0, (gr, gi, u1, u2, di) => gr.Id == id)
  1175. .Select((gr, gi, u1, u2, di) => new GoodsReceiveInfoMobileView
  1176. {
  1177. Id = gr.Id,
  1178. GroupId = gr.GroupId,
  1179. GroupName = di.TeamName,
  1180. GoodsId = gr.GoodsId,
  1181. GoodsName = gi.Name,
  1182. Quantity = gr.Quantity,
  1183. Reason = gr.Reason,
  1184. Remark = gr.Remark,
  1185. GoodsStorageInfo = gr.GoodsStorageInfo,
  1186. AuditStatus = gr.AuditStatus,
  1187. AuditUserId = gr.AuditUserId,
  1188. AuditUserName = u1.CnName,
  1189. AuditTime = gr.AuditTime,
  1190. CreateUserName = u2.CnName,
  1191. CreateTime = gr.CreateTime
  1192. })
  1193. .FirstAsync();
  1194. if (!string.IsNullOrEmpty(data.GoodsStorageInfo))
  1195. {
  1196. var subData = new List<dynamic>();
  1197. try
  1198. {
  1199. var subData1 = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(data.GoodsStorageInfo);
  1200. if (subData1.Count > 0)
  1201. {
  1202. string goodsStorageInfoStr = string.Empty;
  1203. var storageIds = subData1.Select(x => x.StorageId).ToList();
  1204. var storages = await _sqlSugar.Queryable<Pm_GoodsStorage>().Where(x => x.IsDel == 0 && storageIds.Contains(x.Id)).ToListAsync();
  1205. foreach (var item in subData1)
  1206. {
  1207. var storageInfo = storages.Find(x => x.Id == item.StorageId);
  1208. if (storageInfo != null)
  1209. {
  1210. subData.Add(new
  1211. {
  1212. StorageId = item.StorageId,
  1213. BatchNo = storageInfo.BatchNo,
  1214. RecsiveQuantity = item.Quantity
  1215. });
  1216. goodsStorageInfoStr += $"物品名称:{data.GoodsName} 批次号:{storageInfo.BatchNo} 领用数量:{item.Quantity} \r\n";
  1217. }
  1218. }
  1219. data.QuantityInfos = subData;
  1220. data.GoodsStorageInfoStr = goodsStorageInfoStr;
  1221. }
  1222. }
  1223. catch (Exception e)
  1224. {
  1225. Console.WriteLine(e);
  1226. }
  1227. }
  1228. _jv.Code = StatusCodes.Status200OK;
  1229. _jv.Msg = $"操作成功";
  1230. if (portType == 2 || portType == 3) //移动端
  1231. {
  1232. _jv.Data = data;
  1233. }
  1234. else if (portType == 1) //pc端
  1235. {
  1236. _jv.Data = _mapper.Map<GoodsReceiveInfoView>(data);
  1237. }
  1238. return _jv;
  1239. }
  1240. /// <summary>
  1241. /// 物品领用 OP(Add Or Edit)
  1242. /// </summary>
  1243. /// <param name="dto"></param>
  1244. /// <param name="currUserId"></param>
  1245. /// <returns></returns>
  1246. public async Task<JsonView> GoodsReceiveOp(GoodsReceiveOpDto dto, int currUserId)
  1247. {
  1248. var info = _mapper.Map<Pm_GoodsReceive>(dto);
  1249. info.CreateUserId = currUserId;
  1250. var auditEnums = new List<GoodsAuditEnum>() {
  1251. GoodsAuditEnum.Approved, // 1
  1252. GoodsAuditEnum.UnApproved, // 2
  1253. GoodsAuditEnum.OutConfirmed,// 5
  1254. GoodsAuditEnum.OutRejected // 6
  1255. };
  1256. _sqlSugar.BeginTran();
  1257. //物品现有库存
  1258. var goodsInfo = _sqlSugar.Queryable<Pm_GoodsInfo>().First(x => x.Id == info.GoodsId);
  1259. var stockQuantity = goodsInfo?.StockQuantity ?? 0.00M;
  1260. //待审核 该物品数量
  1261. var waitAuditQuantity = await _sqlSugar.Queryable<Pm_GoodsReceive>().Where(x => x.IsDel == 0 &&
  1262. x.GoodsId == dto.GoodsId &&
  1263. !auditEnums.Contains(x.AuditStatus)
  1264. ).SumAsync(x => x.Quantity);
  1265. //验证默认审核 多条 or 单条
  1266. //出库确认默认审核状态
  1267. var isAuditPer = GoodsAuditType(goodsInfo?.Type ?? 0);
  1268. //验证默认审核 多条审核时添加
  1269. var goodsAuditInfo1 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1270. var goodsAuditInfo2 = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr, info.Id, GoodsConfirmEnum.WaitConfirm, currUserId);
  1271. var goodsAuditInfos = new List<Pm_GoodsAudit>() { goodsAuditInfo1 };
  1272. //状态描述
  1273. StringBuilder stringBuilder = new StringBuilder();
  1274. stringBuilder.Append($"领用确认:状态:待确认 审核人:- 审核时间:-;<br/>");
  1275. if (isAuditPer)
  1276. {
  1277. goodsAuditInfos.Add(goodsAuditInfo2);
  1278. stringBuilder.Append($"人事部:状态:待确认 审核人:- 审核时间:-;<br/>");
  1279. }
  1280. info.StatusDesc = stringBuilder.ToString();
  1281. if (info.Id > 0) //修改
  1282. {
  1283. //审核验证
  1284. var selectInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.Id == info.Id);
  1285. if (auditEnums.Contains(selectInfo.AuditStatus))
  1286. {
  1287. _sqlSugar.RollbackTran();
  1288. _jv.Msg = $"该条数据已执行操作({selectInfo.AuditStatus.GetEnumDescription()}),不可更改!";
  1289. return _jv;
  1290. }
  1291. //物品数量验证
  1292. var editAfterQuantity = waitAuditQuantity - selectInfo.Quantity + info.Quantity;
  1293. if (editAfterQuantity > stockQuantity)
  1294. {
  1295. _sqlSugar.RollbackTran();
  1296. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1297. return _jv;
  1298. }
  1299. var edit = await _sqlSugar.Updateable(info)
  1300. .UpdateColumns(x => new
  1301. {
  1302. x.GroupId,
  1303. x.Quantity,
  1304. x.Reason,
  1305. x.Remark,
  1306. x.StatusDesc,
  1307. })
  1308. .Where(x => x.Id == info.Id)
  1309. .ExecuteCommandAsync();
  1310. if (edit > 0)
  1311. {
  1312. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>().Where(x => x.DataId == info.Id).ToListAsync();
  1313. if (!auditInfos.Any()) await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1314. _sqlSugar.CommitTran();
  1315. _jv.Msg = $"操作成功!";
  1316. _jv.Code = StatusCodes.Status200OK;
  1317. return _jv;
  1318. }
  1319. }
  1320. else if (info.Id < 1) //添加
  1321. {
  1322. //物品数量验证
  1323. decimal addAgoQuantity = waitAuditQuantity + info.Quantity;
  1324. if (addAgoQuantity > stockQuantity)
  1325. {
  1326. _sqlSugar.RollbackTran();
  1327. _jv.Msg = $"该物品现有库存不足,不可更改!请联系采购人员购买!";
  1328. return _jv;
  1329. }
  1330. var add = await _sqlSugar.Insertable(info).ExecuteReturnIdentityAsync();
  1331. if (add > 0)
  1332. {
  1333. goodsAuditInfos.ForEach(x => x.DataId = add);
  1334. await _sqlSugar.Insertable(goodsAuditInfos).ExecuteCommandAsync();
  1335. _sqlSugar.CommitTran();
  1336. _jv.Msg = $"操作成功!";
  1337. _jv.Code = StatusCodes.Status200OK;
  1338. return _jv;
  1339. }
  1340. }
  1341. _sqlSugar.RollbackTran();
  1342. return _jv;
  1343. }
  1344. /// <summary>
  1345. /// 物品领用 Audit
  1346. /// </summary>
  1347. /// <param name="idArray"></param>
  1348. /// <param name="userId"></param>
  1349. /// <param name="auditEnum"></param>
  1350. /// <returns></returns>
  1351. public async Task<JsonView> GoodsReceiveAudit(int[] idArray, int userId, GoodsAuditEnum auditEnum)
  1352. {
  1353. if (idArray.Length < 1) return _jv;
  1354. if (!Enum.IsDefined(typeof(GoodsAuditEnum),(int)auditEnum))
  1355. {
  1356. _jv.Msg = $"出库确认状态超出可用范围!";
  1357. return _jv;
  1358. }
  1359. int receiveId = idArray[0];
  1360. var currUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == userId)?.CnName ?? "-";
  1361. _sqlSugar.BeginTran();
  1362. var receiveInfo = await _sqlSugar.Queryable<Pm_GoodsReceive>().FirstAsync(x => x.IsDel == 0 && receiveId == x.Id);
  1363. if (receiveInfo == null)
  1364. {
  1365. _sqlSugar.RollbackTran();
  1366. _jv.Msg = $"当前领用信息不存在!";
  1367. return _jv;
  1368. }
  1369. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  1370. int goodTypeId = goodsInfo?.Type ?? 0;
  1371. //审核前状态
  1372. var preChangeStatus = receiveInfo.AuditStatus;
  1373. //状态验证
  1374. //if (preChangeStatus == auditEnum)
  1375. //{
  1376. // _sqlSugar.RollbackTran();
  1377. // _jv.Msg = $"该条数据状态已设置,不可重复设置!";
  1378. // return _jv;
  1379. //}
  1380. var isAuditPer = GoodsAuditType(goodTypeId);
  1381. if (isAuditPer) //多人审核
  1382. {
  1383. var auditDep = GoodsAuditDepEnum.Hr_Reception;
  1384. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1385. .FirstAsync(x => x.Type == 2 && x.DataId == receiveInfo.Id && x.Dep == GoodsAuditDepEnum.Hr_Reception);
  1386. if (auditInfo != null && auditInfo.AuditStatus == GoodsConfirmEnum.Confirmed)
  1387. {
  1388. auditDep = GoodsAuditDepEnum.Hr;
  1389. //多人审核的时候验证审核权限
  1390. var auditList = GoodsStorageConfirmAuditDep(2);
  1391. var auditDepInfo = auditList.Find(x => x.AuditorIds.Contains(userId));
  1392. if (auditDepInfo == null)
  1393. {
  1394. _jv.Msg = string.Format("未分配出库确认权限!");
  1395. _sqlSugar.RollbackTran();
  1396. return _jv;
  1397. }
  1398. }
  1399. // 确认中 确认 拒绝
  1400. _jv = await GoodsReceiveOutConfirmingMultiple(receiveInfo, userId, auditDep, auditEnum);
  1401. //switch (auditEnum)
  1402. //{
  1403. // case GoodsAuditEnum.Pending: //领用待确认(取消确认、取消拒绝)
  1404. // _jv = await GoodsReceivePending(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1405. // break;
  1406. // case GoodsAuditEnum.Approved: //领用确认
  1407. // _jv = await GoodsReceiveApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1408. // break;
  1409. // case GoodsAuditEnum.UnApproved: //领用拒绝
  1410. // _jv = await GoodsReceiveUnApproved(preChangeStatus, receiveId, userId, currUserName, auditEnum);
  1411. // break;
  1412. // case GoodsAuditEnum.OutPending: //出库待确认(取消出库确认、取消出库拒绝)
  1413. // _jv = await GoodsReceiveOutPending(preChangeStatus, receiveInfo, userId, auditDep, auditEnum);
  1414. // break;
  1415. // case GoodsAuditEnum.OutConfirmed: //出库确认
  1416. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1417. // break;
  1418. // case GoodsAuditEnum.OutRejected: //出库拒绝
  1419. // _jv = await GoodsReceiveOutConfirming(receiveInfo, userId, auditDep, auditEnum);
  1420. // break;
  1421. //}
  1422. }
  1423. else //单人审核
  1424. {
  1425. //领用确认 --> 已确认 / 领用拒绝 --> 已拒绝
  1426. _jv = await GoodsReceiveOutConfirmingSingle(receiveInfo,userId, currUserName,auditEnum);
  1427. }
  1428. if (_jv.Code == StatusCodes.Status200OK)
  1429. {
  1430. _sqlSugar.CommitTran();
  1431. _jv.Msg = $"操作成功!";
  1432. _jv.Data = auditEnum;
  1433. return _jv;
  1434. }
  1435. _sqlSugar.RollbackTran();
  1436. return _jv;
  1437. }
  1438. #region 多人审核状态变更
  1439. /// <summary>
  1440. /// 物品领用状态 - 领用待确认(取消确认、取消拒绝)
  1441. /// </summary>
  1442. /// <param name="preChangeStatus">更改前状态</param>
  1443. /// <param name="receiveId"></param>
  1444. /// <param name="userId"></param>
  1445. /// <param name="userName"></param>
  1446. /// <param name="auditEnum"></param>
  1447. /// <returns></returns>
  1448. public async Task<JsonView> GoodsReceivePending(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1449. {
  1450. _jv.Code = StatusCodes.Status400BadRequest;
  1451. //领用待确认 操作范围
  1452. var audirEnumPers = new List<GoodsAuditEnum>() {
  1453. GoodsAuditEnum.Approved,
  1454. GoodsAuditEnum.UnApproved
  1455. };
  1456. if (!audirEnumPers.Contains(preChangeStatus))
  1457. {
  1458. _jv.Msg = $"{GoodsAuditEnum.Approved.GetEnumDescription()}、{GoodsAuditEnum.UnApproved.GetEnumDescription()},状态下可取消操作!";
  1459. return _jv;
  1460. }
  1461. var currUserOpDt = DateTime.Now;
  1462. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1463. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1464. auditEnum = GoodsAuditEnum.Pending;
  1465. var changeStatus = await _sqlSugar
  1466. .Updateable<Pm_GoodsReceive>()
  1467. .SetColumns(x => new Pm_GoodsReceive()
  1468. {
  1469. AuditStatus = auditEnum,
  1470. AuditUserId = userId,
  1471. AuditTime = currUserOpDt,
  1472. StatusDesc = statusDesc
  1473. })
  1474. .Where(x => x.Id == receiveId)
  1475. .ExecuteCommandAsync();
  1476. if (changeStatus > 0)
  1477. {
  1478. _jv.Code = StatusCodes.Status200OK;
  1479. }
  1480. return _jv;
  1481. }
  1482. /// <summary>
  1483. /// 物品领用状态 - 领用确认
  1484. /// </summary>
  1485. /// <param name="preChangeStatus">更改前状态</param>
  1486. /// <param name="receiveId"></param>
  1487. /// <param name="userId"></param>
  1488. /// <param name="userName"></param>
  1489. /// <param name="auditEnum"></param>
  1490. /// <returns></returns>
  1491. public async Task<JsonView> GoodsReceiveApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1492. {
  1493. _jv.Code = StatusCodes.Status400BadRequest;
  1494. if (preChangeStatus != GoodsAuditEnum.Pending)
  1495. {
  1496. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1497. return _jv;
  1498. }
  1499. var currUserOpDt = DateTime.Now;
  1500. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1501. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部确认:状态:待确认 审核人:- 审核时间:-;", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1502. auditEnum = GoodsAuditEnum.OutPending;
  1503. var changeStatus = await _sqlSugar
  1504. .Updateable<Pm_GoodsReceive>()
  1505. .SetColumns(x => new Pm_GoodsReceive()
  1506. {
  1507. AuditStatus = auditEnum,
  1508. AuditUserId = userId,
  1509. AuditTime = currUserOpDt,
  1510. StatusDesc = statusDesc
  1511. })
  1512. .Where(x => x.Id == receiveId)
  1513. .ExecuteCommandAsync();
  1514. if (changeStatus > 0)
  1515. {
  1516. _jv.Code = StatusCodes.Status200OK;
  1517. }
  1518. return _jv;
  1519. }
  1520. /// <summary>
  1521. /// 物品领用状态 - 领用拒绝
  1522. /// </summary>
  1523. /// <param name="preChangeStatus">更改前状态</param>
  1524. /// <param name="receiveId"></param>
  1525. /// <param name="userId"></param>
  1526. /// <param name="userName"></param>
  1527. /// <param name="auditEnum"></param>
  1528. /// <returns></returns>
  1529. public async Task<JsonView> GoodsReceiveUnApproved(GoodsAuditEnum preChangeStatus, int receiveId, int userId, string userName, GoodsAuditEnum auditEnum)
  1530. {
  1531. _jv.Code = StatusCodes.Status400BadRequest;
  1532. if (preChangeStatus != GoodsAuditEnum.Pending)
  1533. {
  1534. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()},状态下可取消操作!";
  1535. return _jv;
  1536. }
  1537. auditEnum = GoodsAuditEnum.UnApproved;
  1538. var currUserOpDt = DateTime.Now;
  1539. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1540. var statusDesc = string.Format(@"领用确认:状态:{0} 审核人:{1} 审核时间:{2};<br/>人事部确认:状态:待确认 审核人:- 审核时间:-;<br/>财务部认:状态:待确认 审核人:- 审核时间:-;<br/>", auditEnum.GetEnumDescription(), userName, currUserOpTime);
  1541. var changeStatus = await _sqlSugar
  1542. .Updateable<Pm_GoodsReceive>()
  1543. .SetColumns(x => new Pm_GoodsReceive()
  1544. {
  1545. AuditStatus = auditEnum,
  1546. AuditUserId = userId,
  1547. AuditTime = currUserOpDt,
  1548. StatusDesc = statusDesc
  1549. })
  1550. .Where(x => x.Id == receiveId)
  1551. .ExecuteCommandAsync();
  1552. if (changeStatus > 0)
  1553. {
  1554. _jv.Code = StatusCodes.Status200OK;
  1555. }
  1556. return _jv;
  1557. }
  1558. /// <summary>
  1559. /// 物品领用状态 - 出库待确认(出库确认、出库拒绝)
  1560. /// </summary>
  1561. /// <param name="preChangeStatus">更改前状态</param>
  1562. /// <param name="receiveId"></param>
  1563. /// <param name="userId"></param>
  1564. /// <param name="userName"></param>
  1565. /// <param name="auditEnum"></param>
  1566. /// <returns></returns>
  1567. public async Task<JsonView> GoodsReceiveOutPending(GoodsAuditEnum preChangeStatus, Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1568. {
  1569. _jv.Code = StatusCodes.Status400BadRequest;
  1570. //出库待确认 操作范围
  1571. var audirEnumPers = new List<GoodsAuditEnum>() {
  1572. GoodsAuditEnum.OutConfirmed,
  1573. GoodsAuditEnum.OutRejected
  1574. };
  1575. if (!audirEnumPers.Contains(preChangeStatus))
  1576. {
  1577. _jv.Msg = $"{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可取消操作!";
  1578. return _jv;
  1579. }
  1580. var currUserOpDt = DateTime.Now;
  1581. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1582. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1583. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1584. .ToListAsync();
  1585. auditEnum = GoodsAuditEnum.OutPending;
  1586. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1587. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1588. if (auditInfo == null)
  1589. {
  1590. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1591. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1592. if (addStatus < 1)
  1593. {
  1594. _jv.Msg = $"出库确认取消操作失败!";
  1595. return _jv;
  1596. }
  1597. auditInfos.Add(auditInfo);
  1598. }
  1599. else
  1600. {
  1601. //移除旧数据
  1602. auditInfos.Remove(auditInfo);
  1603. auditInfo.AuditStatus = auditStatus;
  1604. auditInfo.AuditUserId = userId;
  1605. auditInfo.AuditTime = currUserOpDt;
  1606. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1607. .SetColumns(x => new Pm_GoodsAudit()
  1608. {
  1609. AuditStatus = auditStatus,
  1610. AuditUserId = userId,
  1611. AuditTime = currUserOpDt
  1612. })
  1613. .Where(x => x.Id == auditInfo.Id)
  1614. .ExecuteCommandAsync();
  1615. if (updStatus < 1)
  1616. {
  1617. _jv.Msg = $"出库确认操作失败!";
  1618. return _jv;
  1619. }
  1620. //添加更新后的数据
  1621. auditInfos.Add(auditInfo);
  1622. }
  1623. //处理状态描述
  1624. StringBuilder statusDesc = new StringBuilder();
  1625. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1626. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1627. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1628. statusDesc.Append(receiveStatusDesc);
  1629. foreach (var auditInf in auditInfos)
  1630. {
  1631. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1632. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1633. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1634. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1635. statusDesc.Append(auditInfStatusDesc);
  1636. }
  1637. //批次库存信息
  1638. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1639. //出库确认取消是单独处理
  1640. if (preChangeStatus == GoodsAuditEnum.OutConfirming)
  1641. {
  1642. if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1643. {
  1644. auditEnum = GoodsAuditEnum.OutConfirming;
  1645. }
  1646. //2.1 更改库存
  1647. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1648. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1649. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1650. goodsInfo.LastUpdateTime = DateTime.Now;
  1651. goodsInfo.LastUpdateUserId = userId;
  1652. var editGoods = await _sqlSugar
  1653. .Updateable(goodsInfo)
  1654. .UpdateColumns(x => new
  1655. {
  1656. x.StockQuantity,
  1657. x.OQ_Total,
  1658. x.LastUpdateUserId,
  1659. x.LastUpdateTime,
  1660. })
  1661. .Where(x => x.Id == receiveInfo.GoodsId)
  1662. .ExecuteCommandAsync();
  1663. if (editGoods < 1)
  1664. {
  1665. _jv.Msg = $"库存更新失败!";
  1666. return _jv;
  1667. }
  1668. //2.2 入库批次关联领用人 更改批次库存
  1669. var goodsStorages = await _sqlSugar
  1670. .Queryable<Pm_GoodsStorage>()
  1671. .Where(x => x.IsDel == 0 &&
  1672. x.GoodsId == receiveInfo.GoodsId &&
  1673. (x.Quantity - x.ReceiveQuantity) > 0
  1674. )
  1675. .OrderBy(x => x.CreateTime)
  1676. .ToListAsync();
  1677. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1678. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1679. var receiveQuantity = 0.00M; //领用总数量
  1680. foreach (var storage in goodsStorages)
  1681. {
  1682. if (receiveInfo.Quantity == receiveQuantity) break;
  1683. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1684. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1685. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1686. const decimal unit = 0.50M;
  1687. while (receiveQuantity < receiveInfo.Quantity)
  1688. {
  1689. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1690. thisBatchReceiveQuantity += unit;
  1691. receiveQuantity += unit;
  1692. }
  1693. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1694. {
  1695. StorageId = storage.Id,
  1696. Quantity = thisBatchReceiveQuantity
  1697. });
  1698. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1699. var storageUpd = storage;
  1700. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1701. batchStorageInfos.Add(storageUpd);
  1702. }
  1703. //2.2.1 更改批次库存
  1704. if (goodsReceiveInfos.Count > 0)
  1705. {
  1706. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1707. .UpdateColumns(x => x.ReceiveQuantity)
  1708. .WhereColumns(x => x.Id)
  1709. .ExecuteCommandAsync();
  1710. if (edit1 < 1)
  1711. {
  1712. _jv.Msg = $"批次库存更新失败!";
  1713. return _jv;
  1714. }
  1715. }
  1716. //2.2.2 添加入库批次关联领用人
  1717. if (goodsReceiveInfos.Count > 0)
  1718. {
  1719. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1720. }
  1721. }
  1722. var statusDesc1 = statusDesc.ToString();
  1723. var changeStatus = await _sqlSugar
  1724. .Updateable<Pm_GoodsReceive>()
  1725. .SetColumns(x => new Pm_GoodsReceive()
  1726. {
  1727. AuditStatus = auditEnum,
  1728. StatusDesc = statusDesc1,
  1729. GoodsStorageInfo = goodsStorageInfo
  1730. })
  1731. .Where(x => x.Id == receiveInfo.Id)
  1732. .ExecuteCommandAsync();
  1733. if (changeStatus > 0)
  1734. {
  1735. _jv.Code = StatusCodes.Status200OK;
  1736. }
  1737. return _jv;
  1738. }
  1739. /// <summary>
  1740. /// 物品领用状态 - 出库确认中、出库确认完成、出库确认拒绝
  1741. /// </summary>
  1742. /// <param name="receiveInfo"></param>
  1743. /// <param name="userId"></param>
  1744. /// <param name="depEnum"></param>
  1745. /// <param name="auditEnum"></param>
  1746. /// <returns></returns>
  1747. public async Task<JsonView> GoodsReceiveOutConfirming(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1748. {
  1749. _jv.Code = StatusCodes.Status400BadRequest;
  1750. //更改前状态
  1751. var preChangeStatus = receiveInfo.AuditStatus;
  1752. if (preChangeStatus < GoodsAuditEnum.OutPending)
  1753. {
  1754. _jv.Msg = $"领用确认执行成功!才可执行出库确认或出库拒绝操作!";
  1755. return _jv;
  1756. }
  1757. var currUserOpDt = DateTime.Now;
  1758. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1759. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1760. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1761. .ToListAsync();
  1762. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1763. if (auditEnum == GoodsAuditEnum.OutRejected) auditStatus = GoodsConfirmEnum.UnApproved;
  1764. else if (auditEnum == GoodsAuditEnum.OutConfirmed) auditStatus = GoodsConfirmEnum.Confirmed;
  1765. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1766. if (auditInfo == null)
  1767. {
  1768. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1769. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1770. if (addStatus < 1)
  1771. {
  1772. _jv.Msg = $"出库确认操作失败!";
  1773. return _jv;
  1774. }
  1775. auditInfos.Add(auditInfo);
  1776. }
  1777. else
  1778. {
  1779. //移除旧数据
  1780. auditInfos.Remove(auditInfo);
  1781. auditInfo.AuditStatus = auditStatus;
  1782. auditInfo.AuditUserId = userId;
  1783. auditInfo.AuditTime = currUserOpDt;
  1784. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  1785. .SetColumns(x => new Pm_GoodsAudit()
  1786. {
  1787. AuditStatus = auditStatus,
  1788. AuditUserId = userId,
  1789. AuditTime = currUserOpDt
  1790. })
  1791. .Where(x => x.Id == auditInfo.Id)
  1792. .ExecuteCommandAsync();
  1793. if (updStatus < 1)
  1794. {
  1795. _jv.Msg = $"出库确认操作失败!";
  1796. return _jv;
  1797. }
  1798. //添加更新后的数据
  1799. auditInfos.Add(auditInfo);
  1800. }
  1801. //处理状态描述
  1802. StringBuilder statusDesc = new StringBuilder();
  1803. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1804. string receiveStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1805. "领用确认", GoodsAuditEnum.Approved.GetEnumDescription(), receiveUserName, receiveInfo.AuditTime.ToString("yyyy-MM-dd HH:mm:ss"));
  1806. statusDesc.Append(receiveStatusDesc);
  1807. foreach (var auditInf in auditInfos)
  1808. {
  1809. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  1810. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  1811. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  1812. auditInf.Dep.GetEnumDescription(), auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  1813. statusDesc.Append(auditInfStatusDesc);
  1814. }
  1815. if (preChangeStatus == auditEnum)
  1816. {
  1817. _jv.Msg = $"此操作已执行,不可重复操作!";
  1818. return _jv;
  1819. }
  1820. //批次库存信息
  1821. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  1822. //更改领用状态及库存
  1823. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  1824. {
  1825. //出库确认拒绝
  1826. auditEnum = GoodsAuditEnum.OutRejected;
  1827. }
  1828. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  1829. {
  1830. //1.出库确认完成
  1831. auditEnum = GoodsAuditEnum.OutConfirmed;
  1832. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  1833. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  1834. {
  1835. //2.1 更改库存
  1836. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  1837. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  1838. goodsInfo.OQ_Total += receiveInfo.Quantity;
  1839. goodsInfo.LastUpdateTime = DateTime.Now;
  1840. goodsInfo.LastUpdateUserId = userId;
  1841. var editGoods = await _sqlSugar
  1842. .Updateable(goodsInfo)
  1843. .UpdateColumns(x => new
  1844. {
  1845. x.StockQuantity,
  1846. x.OQ_Total,
  1847. x.LastUpdateUserId,
  1848. x.LastUpdateTime,
  1849. })
  1850. .Where(x => x.Id == receiveInfo.GoodsId)
  1851. .ExecuteCommandAsync();
  1852. if (editGoods < 1)
  1853. {
  1854. _jv.Msg = $"库存更新失败!";
  1855. return _jv;
  1856. }
  1857. //2.2 入库批次关联领用人 更改批次库存
  1858. var goodsStorages = await _sqlSugar
  1859. .Queryable<Pm_GoodsStorage>()
  1860. .Where(x => x.IsDel == 0 &&
  1861. x.GoodsId == receiveInfo.GoodsId &&
  1862. (x.Quantity - x.ReceiveQuantity) > 0
  1863. )
  1864. .OrderBy(x => x.CreateTime)
  1865. .ToListAsync();
  1866. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  1867. var batchStorageInfos = new List<Pm_GoodsStorage>();
  1868. var receiveQuantity = 0.00M; //领用总数量
  1869. foreach (var storage in goodsStorages)
  1870. {
  1871. if (receiveInfo.Quantity == receiveQuantity) break;
  1872. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  1873. if (thisBatchSurplusQuantity <= 0.00M) continue;
  1874. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  1875. const decimal unit = 0.50M;
  1876. while (receiveQuantity < receiveInfo.Quantity)
  1877. {
  1878. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  1879. thisBatchReceiveQuantity += unit;
  1880. receiveQuantity += unit;
  1881. }
  1882. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  1883. {
  1884. StorageId = storage.Id,
  1885. Quantity = thisBatchReceiveQuantity
  1886. });
  1887. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  1888. var storageUpd = storage;
  1889. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  1890. batchStorageInfos.Add(storageUpd);
  1891. }
  1892. //2.2.1 更改批次库存
  1893. if (goodsReceiveInfos.Count > 0)
  1894. {
  1895. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  1896. .UpdateColumns(x => x.ReceiveQuantity)
  1897. .WhereColumns(x => x.Id)
  1898. .ExecuteCommandAsync();
  1899. if (edit1 < 1)
  1900. {
  1901. _jv.Msg = $"批次库存更新失败!";
  1902. return _jv;
  1903. }
  1904. }
  1905. //2.2.2 添加入库批次关联领用人
  1906. if (goodsReceiveInfos.Count > 0)
  1907. {
  1908. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  1909. }
  1910. }
  1911. }
  1912. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  1913. {
  1914. //出库确认中
  1915. auditEnum = GoodsAuditEnum.OutConfirming;
  1916. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  1917. {
  1918. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  1919. if (!string.IsNullOrEmpty(goodsStorageInfo))
  1920. {
  1921. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  1922. if (goodsStorageInfos.Any())
  1923. {
  1924. foreach (var item in goodsStorageInfos)
  1925. {
  1926. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  1927. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  1928. .FirstAsync();
  1929. if (newStorageInfo == null) continue;
  1930. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  1931. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  1932. .ExecuteCommandAsync();
  1933. if (newEdit < 1)
  1934. {
  1935. _jv.Msg = $"批次领用库存更新失败!";
  1936. return _jv;
  1937. }
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. else
  1944. {
  1945. auditEnum = GoodsAuditEnum.OutPending;
  1946. }
  1947. var statusDesc1 = statusDesc.ToString();
  1948. var changeStatus = await _sqlSugar
  1949. .Updateable<Pm_GoodsReceive>()
  1950. .SetColumns(x => new Pm_GoodsReceive()
  1951. {
  1952. AuditStatus = auditEnum,
  1953. StatusDesc = statusDesc1,
  1954. GoodsStorageInfo = goodsStorageInfo,
  1955. })
  1956. .Where(x => x.Id == receiveInfo.Id)
  1957. .ExecuteCommandAsync();
  1958. if (changeStatus > 0)
  1959. {
  1960. _jv.Code = StatusCodes.Status200OK;
  1961. }
  1962. return _jv;
  1963. }
  1964. /// <summary>
  1965. ///
  1966. /// </summary>
  1967. /// <param name="receiveInfo"></param>
  1968. /// <param name="userId"></param>
  1969. /// <param name="depEnum"></param>
  1970. /// <param name="auditEnum"></param>
  1971. /// <returns></returns>
  1972. public async Task<JsonView> GoodsReceiveOutConfirmingMultiple(Pm_GoodsReceive receiveInfo, int userId, GoodsAuditDepEnum depEnum, GoodsAuditEnum auditEnum)
  1973. {
  1974. _jv.Code = StatusCodes.Status400BadRequest;
  1975. //更改前状态
  1976. receiveInfo.CreateUserId = userId;
  1977. var preChangeStatus = receiveInfo.AuditStatus;
  1978. var currUserOpDt = DateTime.Now;
  1979. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  1980. var receiveUserName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == receiveInfo.AuditUserId)?.CnName ?? "-";
  1981. var auditInfos = await _sqlSugar.Queryable<Pm_GoodsAudit>()
  1982. .Where(x => x.IsDel == 0 && x.DataId == receiveInfo.Id && x.Type == 2)
  1983. .ToListAsync();
  1984. var auditStatus = GoodsConfirmEnum.WaitConfirm;
  1985. if (auditEnum == GoodsAuditEnum.UnApproved) auditStatus = GoodsConfirmEnum.UnApproved;
  1986. else if (auditEnum == GoodsAuditEnum.Approved) auditStatus = GoodsConfirmEnum.Confirmed;
  1987. var auditInfo = auditInfos.Find(x => x.Dep == depEnum);
  1988. if (auditInfo == null)
  1989. {
  1990. auditInfo = new Pm_GoodsAudit(2, depEnum, receiveInfo.Id, auditStatus, userId);
  1991. auditInfo.AuditUserId = userId;
  1992. var addStatus = await _sqlSugar.Insertable(auditInfo).ExecuteCommandAsync();
  1993. if (addStatus < 1)
  1994. {
  1995. _jv.Msg = $"出库确认操作失败!";
  1996. return _jv;
  1997. }
  1998. auditInfos.Add(auditInfo);
  1999. }
  2000. else
  2001. {
  2002. //移除旧数据
  2003. auditInfos.Remove(auditInfo);
  2004. auditInfo.AuditStatus = auditStatus;
  2005. auditInfo.AuditUserId = userId;
  2006. auditInfo.AuditTime = currUserOpDt;
  2007. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2008. .SetColumns(x => new Pm_GoodsAudit()
  2009. {
  2010. AuditStatus = auditStatus,
  2011. AuditUserId = userId,
  2012. AuditTime = currUserOpDt
  2013. })
  2014. .Where(x => x.Id == auditInfo.Id)
  2015. .ExecuteCommandAsync();
  2016. if (updStatus < 1)
  2017. {
  2018. _jv.Msg = $"出库确认操作失败!";
  2019. return _jv;
  2020. }
  2021. //添加更新后的数据
  2022. auditInfos.Add(auditInfo);
  2023. }
  2024. auditInfos = auditInfos.OrderBy(x => x.Dep).ToList();
  2025. //处理状态描述
  2026. StringBuilder statusDesc = new StringBuilder();
  2027. foreach (var auditInf in auditInfos)
  2028. {
  2029. string auditType = auditInf.Dep == GoodsAuditDepEnum.Hr ? "人事部" : "领用确认";
  2030. string userName = _sqlSugar.Queryable<Sys_Users>().First(x => x.Id == auditInf.AuditUserId)?.CnName ?? "-";
  2031. string userOpTime = !userName.Equals("-") ? auditInf.AuditTime.ToString("yyyy-MM-dd HH:mm:ss") : "-";
  2032. string auditInfStatusDesc = string.Format("{0}:状态:{1} 审核人:{2} 审核时间:{3};<br/>",
  2033. auditType, auditInf.AuditStatus.GetEnumDescription(), userName, currUserOpTime);
  2034. statusDesc.Append(auditInfStatusDesc);
  2035. }
  2036. if (preChangeStatus == auditEnum)
  2037. {
  2038. _jv.Msg = $"此操作已执行,不可重复操作!";
  2039. return _jv;
  2040. }
  2041. //批次库存信息
  2042. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2043. //更改领用状态及库存
  2044. if (auditInfos.Any(x => x.AuditStatus == GoodsConfirmEnum.UnApproved))
  2045. {
  2046. //出库确认拒绝
  2047. auditEnum = GoodsAuditEnum.OutRejected;
  2048. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2049. {
  2050. //回滚库存
  2051. _jv = await GoodsInventoryRollback(receiveInfo);
  2052. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2053. }
  2054. }
  2055. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 2)
  2056. {
  2057. //1.出库确认完成
  2058. auditEnum = GoodsAuditEnum.OutConfirmed;
  2059. //2.更改前的状态为出库确认中 且 更改后的状态为出库确认完成 执行库存减操作
  2060. if (preChangeStatus == GoodsAuditEnum.OutConfirming || preChangeStatus == GoodsAuditEnum.OutRejected)
  2061. {
  2062. //2.1 更改库存
  2063. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2064. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2065. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2066. goodsInfo.LastUpdateTime = DateTime.Now;
  2067. goodsInfo.LastUpdateUserId = userId;
  2068. var editGoods = await _sqlSugar
  2069. .Updateable(goodsInfo)
  2070. .UpdateColumns(x => new
  2071. {
  2072. x.StockQuantity,
  2073. x.OQ_Total,
  2074. x.LastUpdateUserId,
  2075. x.LastUpdateTime,
  2076. })
  2077. .Where(x => x.Id == receiveInfo.GoodsId)
  2078. .ExecuteCommandAsync();
  2079. if (editGoods < 1)
  2080. {
  2081. _jv.Msg = $"库存更新失败!";
  2082. return _jv;
  2083. }
  2084. //2.2 入库批次关联领用人 更改批次库存
  2085. var goodsStorages = await _sqlSugar
  2086. .Queryable<Pm_GoodsStorage>()
  2087. .Where(x => x.IsDel == 0 &&
  2088. x.GoodsId == receiveInfo.GoodsId &&
  2089. (x.Quantity - x.ReceiveQuantity) > 0
  2090. )
  2091. .OrderBy(x => x.CreateTime)
  2092. .ToListAsync();
  2093. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2094. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2095. var receiveQuantity = 0.00M; //领用总数量
  2096. foreach (var storage in goodsStorages)
  2097. {
  2098. if (receiveInfo.Quantity == receiveQuantity) break;
  2099. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2100. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2101. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2102. const decimal unit = 0.50M;
  2103. while (receiveQuantity < receiveInfo.Quantity)
  2104. {
  2105. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2106. thisBatchReceiveQuantity += unit;
  2107. receiveQuantity += unit;
  2108. }
  2109. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2110. {
  2111. StorageId = storage.Id,
  2112. Quantity = thisBatchReceiveQuantity
  2113. });
  2114. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2115. var storageUpd = storage;
  2116. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2117. batchStorageInfos.Add(storageUpd);
  2118. }
  2119. //2.2.1 更改批次库存
  2120. if (goodsReceiveInfos.Count > 0)
  2121. {
  2122. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2123. .UpdateColumns(x => x.ReceiveQuantity)
  2124. .WhereColumns(x => x.Id)
  2125. .ExecuteCommandAsync();
  2126. if (edit1 < 1)
  2127. {
  2128. _jv.Msg = $"批次库存更新失败!";
  2129. return _jv;
  2130. }
  2131. }
  2132. //2.2.2 添加入库批次关联领用人
  2133. if (goodsReceiveInfos.Count > 0)
  2134. {
  2135. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2136. }
  2137. }
  2138. }
  2139. else if (auditInfos.Where(x => x.AuditStatus == GoodsConfirmEnum.Confirmed).Count() == 1)
  2140. {
  2141. //出库确认中
  2142. auditEnum = GoodsAuditEnum.OutConfirming;
  2143. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2144. {
  2145. //回滚库存
  2146. _jv = await GoodsInventoryRollback(receiveInfo);
  2147. if (_jv.Code != StatusCodes.Status200OK) return _jv;
  2148. }
  2149. }
  2150. else auditEnum = GoodsAuditEnum.OutPending;
  2151. var statusDesc1 = statusDesc.ToString();
  2152. var changeStatus = await _sqlSugar
  2153. .Updateable<Pm_GoodsReceive>()
  2154. .SetColumns(x => new Pm_GoodsReceive()
  2155. {
  2156. AuditStatus = auditEnum,
  2157. StatusDesc = statusDesc1,
  2158. GoodsStorageInfo = goodsStorageInfo,
  2159. })
  2160. .Where(x => x.Id == receiveInfo.Id)
  2161. .ExecuteCommandAsync();
  2162. if (changeStatus > 0)
  2163. {
  2164. _jv.Code = StatusCodes.Status200OK;
  2165. }
  2166. return _jv;
  2167. }
  2168. /// <summary>
  2169. /// 物品库存回滚
  2170. /// </summary>
  2171. /// <param name="receiveInfo"></param>
  2172. /// <returns></returns>
  2173. public async Task<JsonView> GoodsInventoryRollback(Pm_GoodsReceive receiveInfo)
  2174. {
  2175. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2176. //回滚库存
  2177. var changeGoods = await _sqlSugar
  2178. .Updateable(goodsInfo)
  2179. .ReSetValue(it => {
  2180. it.StockQuantity += receiveInfo.Quantity;
  2181. it.OQ_Total -= receiveInfo.Quantity;
  2182. it.LastUpdateTime = DateTime.Now;
  2183. it.LastUpdateUserId = receiveInfo.CreateUserId;
  2184. })
  2185. .ExecuteCommandAsync();
  2186. if (changeGoods < 1)
  2187. {
  2188. _jv.Msg = $"库存回滚失败!";
  2189. return _jv;
  2190. }
  2191. //批次领用记录
  2192. _jv.Code = StatusCodes.Status200OK;
  2193. var goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2194. if (string.IsNullOrEmpty(goodsStorageInfo)) return _jv;
  2195. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2196. if (!goodsStorageInfos.Any()) return _jv;
  2197. foreach (var item in goodsStorageInfos)
  2198. {
  2199. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2200. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2201. .FirstAsync();
  2202. if (newStorageInfo == null) continue;
  2203. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2204. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2205. .ExecuteCommandAsync();
  2206. if (newEdit < 1)
  2207. {
  2208. _jv.Code = StatusCodes.Status400BadRequest;
  2209. _jv.Msg = $"批次领用库存更新失败!";
  2210. return _jv;
  2211. }
  2212. }
  2213. _jv.Code = StatusCodes.Status200OK;
  2214. return _jv;
  2215. }
  2216. #endregion
  2217. #region 单人审核状态变更
  2218. /// <summary>
  2219. /// 物品领用状态 - 出库确认完成、出库确认拒绝
  2220. /// </summary>
  2221. /// <param name="receiveInfo"></param>
  2222. /// <param name="userId"></param>
  2223. /// <param name="currUserName"></param>
  2224. /// <param name="auditEnum"></param>
  2225. /// <returns></returns>
  2226. public async Task<JsonView> GoodsReceiveOutConfirmingSingle(Pm_GoodsReceive receiveInfo, int userId,string currUserName,GoodsAuditEnum auditEnum)
  2227. {
  2228. _jv.Code = StatusCodes.Status400BadRequest;
  2229. //状态操作范围验证
  2230. var auditEnums = new List<GoodsAuditEnum>() {
  2231. GoodsAuditEnum.Pending,
  2232. GoodsAuditEnum.Approved,
  2233. GoodsAuditEnum.UnApproved,
  2234. };
  2235. if (!auditEnums.Contains(auditEnum))
  2236. {
  2237. _jv.Msg = $"{GoodsAuditEnum.Pending.GetEnumDescription()}、{GoodsAuditEnum.OutConfirmed.GetEnumDescription()}、{GoodsAuditEnum.OutRejected.GetEnumDescription()},状态下可操作!";
  2238. return _jv;
  2239. }
  2240. var currUserOpDt = DateTime.Now;
  2241. var currUserOpTime = currUserOpDt.ToString("yyyy-MM-dd HH:mm:ss");
  2242. //更改前状态
  2243. var preChangeStatus = receiveInfo.AuditStatus;
  2244. //前台状态验证
  2245. var auditInfo = await _sqlSugar.Queryable<Pm_GoodsAudit>().FirstAsync(x => x.IsDel == 0 && x.Type == 2 && x.DataId == receiveInfo.Id);
  2246. if (auditInfo == null)
  2247. {
  2248. var addInfo = new Pm_GoodsAudit(2, GoodsAuditDepEnum.Hr_Reception, receiveInfo.Id, GoodsConfirmEnum.WaitConfirm, userId);
  2249. auditInfo = await _sqlSugar.Insertable(addInfo).ExecuteReturnEntityAsync();
  2250. if (auditInfo == null)
  2251. {
  2252. _jv.Msg = $"确认操作失败!";
  2253. return _jv;
  2254. }
  2255. }
  2256. if (auditEnum == GoodsAuditEnum.Approved)
  2257. {
  2258. auditInfo.AuditStatus = GoodsConfirmEnum.Confirmed;
  2259. auditEnum = GoodsAuditEnum.OutConfirmed;
  2260. }
  2261. else if (auditEnum == GoodsAuditEnum.UnApproved)
  2262. {
  2263. auditInfo.AuditStatus = GoodsConfirmEnum.UnApproved;
  2264. auditEnum = GoodsAuditEnum.OutRejected;
  2265. }
  2266. if (preChangeStatus == auditEnum)
  2267. {
  2268. _jv.Msg = $"此操作已执行,不可重复操作!";
  2269. return _jv;
  2270. }
  2271. //更新子表审核状态
  2272. var updStatus = await _sqlSugar.Updateable<Pm_GoodsAudit>()
  2273. .SetColumns(x => new Pm_GoodsAudit()
  2274. {
  2275. AuditStatus = auditInfo.AuditStatus,
  2276. AuditUserId = userId,
  2277. AuditTime = currUserOpDt
  2278. })
  2279. .Where(x => x.Id == auditInfo.Id)
  2280. .ExecuteCommandAsync();
  2281. if (updStatus < 1)
  2282. {
  2283. _jv.Msg = $"确认操作失败!";
  2284. return _jv;
  2285. }
  2286. //处理状态描述
  2287. StringBuilder statusDesc = new StringBuilder();
  2288. statusDesc.Append(string.Format("领用确认:状态:{0} 审核人:{1} 审核时间:{2};", auditInfo.AuditStatus.GetEnumDescription(), currUserName, currUserOpTime));
  2289. //批次库存信息
  2290. string goodsStorageInfo = receiveInfo.GoodsStorageInfo;
  2291. if (auditEnum == GoodsAuditEnum.OutConfirmed) //出库确认
  2292. {
  2293. //1 更改库存
  2294. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().Where(x => x.Id == receiveInfo.GoodsId).FirstAsync();
  2295. goodsInfo.StockQuantity -= receiveInfo.Quantity;
  2296. goodsInfo.OQ_Total += receiveInfo.Quantity;
  2297. goodsInfo.LastUpdateTime = DateTime.Now;
  2298. goodsInfo.LastUpdateUserId = userId;
  2299. var editGoods = await _sqlSugar.Updateable(goodsInfo)
  2300. .UpdateColumns(x => new
  2301. {
  2302. x.StockQuantity,
  2303. x.OQ_Total,
  2304. x.LastUpdateUserId,
  2305. x.LastUpdateTime,
  2306. })
  2307. .Where(x => x.Id == receiveInfo.GoodsId)
  2308. .ExecuteCommandAsync();
  2309. if (editGoods < 1)
  2310. {
  2311. _jv.Msg = $"库存更新失败!";
  2312. return _jv;
  2313. }
  2314. //2 入库批次关联领用人 更改批次库存
  2315. var goodsStorages = await _sqlSugar
  2316. .Queryable<Pm_GoodsStorage>()
  2317. .Where(x => x.IsDel == 0 &&
  2318. x.GoodsId == receiveInfo.GoodsId &&
  2319. (x.Quantity - x.ReceiveQuantity) > 0
  2320. )
  2321. .OrderBy(x => x.CreateTime)
  2322. .ToListAsync();
  2323. var goodsReceiveInfos = new List<GoodsReceiveLinkStorageView>();
  2324. var batchStorageInfos = new List<Pm_GoodsStorage>();
  2325. var receiveQuantity = 0.00M; //领用总数量
  2326. foreach (var storage in goodsStorages)
  2327. {
  2328. if (receiveInfo.Quantity == receiveQuantity) break;
  2329. var thisBatchSurplusQuantity = storage.Quantity - storage.ReceiveQuantity;
  2330. if (thisBatchSurplusQuantity <= 0.00M) continue;
  2331. var thisBatchReceiveQuantity = 0.00M; //此批次领用数量
  2332. const decimal unit = 0.50M;
  2333. while (receiveQuantity < receiveInfo.Quantity)
  2334. {
  2335. if (thisBatchSurplusQuantity == thisBatchReceiveQuantity) break;
  2336. thisBatchReceiveQuantity += unit;
  2337. receiveQuantity += unit;
  2338. }
  2339. goodsReceiveInfos.Add(new GoodsReceiveLinkStorageView
  2340. {
  2341. StorageId = storage.Id,
  2342. Quantity = thisBatchReceiveQuantity
  2343. });
  2344. storage.ReceiveQuantity += thisBatchReceiveQuantity;
  2345. var storageUpd = storage;
  2346. //storageUpd.ReceiveQuantity += thisBatchReceiveQuantity;
  2347. batchStorageInfos.Add(storageUpd);
  2348. }
  2349. //3 更改批次库存
  2350. if (goodsReceiveInfos.Count > 0)
  2351. {
  2352. var edit1 = await _sqlSugar.Updateable(batchStorageInfos)
  2353. .UpdateColumns(x => x.ReceiveQuantity)
  2354. .WhereColumns(x => x.Id)
  2355. .ExecuteCommandAsync();
  2356. if (edit1 < 1)
  2357. {
  2358. _jv.Msg = $"批次库存更新失败!";
  2359. return _jv;
  2360. }
  2361. }
  2362. //4 添加入库批次关联领用人
  2363. if (goodsReceiveInfos.Count > 0)
  2364. {
  2365. goodsStorageInfo = JsonConvert.SerializeObject(goodsReceiveInfos);
  2366. }
  2367. }
  2368. else if (auditEnum == GoodsAuditEnum.OutRejected) //出库拒绝
  2369. {
  2370. //拒绝之前的状态为已完成 回滚库存
  2371. if (preChangeStatus == GoodsAuditEnum.OutConfirmed)
  2372. {
  2373. //回滚库存
  2374. var goodsInfo = await _sqlSugar.Queryable<Pm_GoodsInfo>().FirstAsync(x => x.Id == receiveInfo.GoodsId);
  2375. var changeGoods = await _sqlSugar
  2376. .Updateable(goodsInfo)
  2377. .ReSetValue(it => {
  2378. it.StockQuantity += receiveInfo.Quantity;
  2379. it.OQ_Total -= receiveInfo.Quantity;
  2380. it.LastUpdateTime = DateTime.Now;
  2381. it.LastUpdateUserId = userId;
  2382. })
  2383. .ExecuteCommandAsync();
  2384. if (changeGoods < 1)
  2385. {
  2386. _jv.Msg = $"库存回滚失败!";
  2387. return _jv;
  2388. }
  2389. //批次号库存 信息更新
  2390. var goodsStorageInfo1 = receiveInfo.GoodsStorageInfo;
  2391. if (!string.IsNullOrEmpty(goodsStorageInfo))
  2392. {
  2393. var goodsStorageInfos = JsonConvert.DeserializeObject<List<GoodsReceiveLinkStorageView>>(goodsStorageInfo);
  2394. if (goodsStorageInfos.Any())
  2395. {
  2396. foreach (var item in goodsStorageInfos)
  2397. {
  2398. var newStorageInfo = await _sqlSugar.Queryable<Pm_GoodsStorage>()
  2399. .Where(x => x.IsDel == 0 && x.Id == item.StorageId)
  2400. .FirstAsync();
  2401. if (newStorageInfo == null) continue;
  2402. var newEdit = await _sqlSugar.Updateable(newStorageInfo)
  2403. .ReSetValue(x => x.ReceiveQuantity = x.ReceiveQuantity - item.Quantity)
  2404. .ExecuteCommandAsync();
  2405. if (newEdit < 1)
  2406. {
  2407. _jv.Msg = $"批次领用库存更新失败!";
  2408. return _jv;
  2409. }
  2410. }
  2411. }
  2412. }
  2413. }
  2414. }
  2415. var statusDesc1 = statusDesc.ToString();
  2416. var changeStatus = await _sqlSugar
  2417. .Updateable<Pm_GoodsReceive>()
  2418. .SetColumns(x => new Pm_GoodsReceive()
  2419. {
  2420. AuditStatus = auditEnum,
  2421. StatusDesc = statusDesc1,
  2422. GoodsStorageInfo = goodsStorageInfo,
  2423. })
  2424. .Where(x => x.Id == receiveInfo.Id)
  2425. .ExecuteCommandAsync();
  2426. if (changeStatus > 0) _jv.Code = StatusCodes.Status200OK;
  2427. return _jv;
  2428. }
  2429. #endregion
  2430. /// <summary>
  2431. /// 物品领用 Del
  2432. /// </summary>
  2433. /// <param name="id"></param>
  2434. /// <param name="currUserId"></param>
  2435. /// <returns></returns>
  2436. public async Task<JsonView> GoodsReceiveDel(int id, int currUserId)
  2437. {
  2438. var receiveInfo = await _sqlSugar
  2439. .Queryable<Pm_GoodsReceive>()
  2440. .Where(x => x.IsDel == 0 && x.Id == id)
  2441. .FirstAsync();
  2442. if (receiveInfo.AuditStatus >= GoodsAuditEnum.Approved)
  2443. {
  2444. _jv.Msg = $"该条数据已进入审批流程,不可删除!";
  2445. return _jv;
  2446. }
  2447. var edit = await _sqlSugar
  2448. .Updateable<Pm_GoodsReceive>()
  2449. .SetColumns(x => new Pm_GoodsReceive()
  2450. {
  2451. IsDel = 1,
  2452. DeleteUserId = currUserId,
  2453. DeleteTime = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
  2454. })
  2455. .Where(x => x.Id == id)
  2456. .ExecuteCommandAsync();
  2457. if (edit > 0)
  2458. {
  2459. _jv.Msg = $"操作成功!";
  2460. _jv.Code = StatusCodes.Status200OK;
  2461. return _jv;
  2462. }
  2463. return _jv;
  2464. }
  2465. }
  2466. }