GoodsRepository.cs 113 KB

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